1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
const API_CONFIG = {
local: {
http: "http://localhost:8080",
ws: "ws://localhost:8080",
},
production: {
http: "https://api.makima.jp",
ws: "wss://api.makima.jp",
},
} as const;
type Environment = "local" | "production";
function detectEnvironment(): Environment {
// Check if explicitly set via env var
const envOverride = import.meta.env.VITE_API_ENV as Environment | undefined;
if (envOverride && (envOverride === "local" || envOverride === "production")) {
return envOverride;
}
// Auto-detect based on hostname
if (typeof window !== "undefined") {
const hostname = window.location.hostname;
if (hostname === "localhost" || hostname === "127.0.0.1") {
return "local";
}
}
return "production";
}
const env = detectEnvironment();
export const API_BASE = API_CONFIG[env].http;
export const WS_BASE = API_CONFIG[env].ws;
export const LISTEN_ENDPOINT = `${WS_BASE}/api/v1/listen`;
export const FILE_SUBSCRIBE_ENDPOINT = `${WS_BASE}/api/v1/files/subscribe`;
export function getEnvironment(): Environment {
return env;
}
// File API types
export interface TranscriptEntry {
id: string;
speaker: string;
start: number;
end: number;
text: string;
isFinal: boolean;
}
// Chart types for visualization
export type ChartType = "line" | "bar" | "pie" | "area";
// Body element types for structured content
export type BodyElement =
| { type: "heading"; level: number; text: string }
| { type: "paragraph"; text: string }
| {
type: "chart";
chartType: ChartType;
title?: string;
data: Record<string, unknown>[];
config?: Record<string, unknown>;
}
| { type: "image"; src: string; alt?: string; caption?: string };
export interface FileSummary {
id: string;
name: string;
description: string | null;
transcriptCount: number;
duration: number | null;
version: number;
createdAt: string;
updatedAt: string;
}
export interface FileDetail {
id: string;
ownerId: string;
name: string;
description: string | null;
transcript: TranscriptEntry[];
location: string | null;
summary: string | null;
body: BodyElement[];
version: number;
createdAt: string;
updatedAt: string;
}
export interface FileListResponse {
files: FileSummary[];
total: number;
}
export interface CreateFileRequest {
name?: string;
description?: string;
transcript: TranscriptEntry[];
location?: string;
}
export interface UpdateFileRequest {
name?: string;
description?: string;
transcript?: TranscriptEntry[];
summary?: string;
body?: BodyElement[];
version?: number;
}
// Conflict error types
export interface ConflictErrorResponse {
code: "VERSION_CONFLICT";
message: string;
expectedVersion: number;
actualVersion: number;
}
export class VersionConflictError extends Error {
expectedVersion: number;
actualVersion: number;
constructor(conflict: ConflictErrorResponse) {
super(conflict.message);
this.name = "VersionConflictError";
this.expectedVersion = conflict.expectedVersion;
this.actualVersion = conflict.actualVersion;
}
}
// Available LLM models
export type LlmModel = "claude-sonnet" | "claude-opus" | "groq";
// Chat API types
export interface ChatMessage {
role: "user" | "assistant";
content: string;
}
export interface ChatRequest {
message: string;
model?: LlmModel;
history?: ChatMessage[];
}
export interface ToolCallInfo {
name: string;
result: {
success: boolean;
message: string;
};
}
export interface ChatResponse {
response: string;
toolCalls: ToolCallInfo[];
updatedBody: BodyElement[];
updatedSummary: string | null;
}
// File API functions
export async function listFiles(): Promise<FileListResponse> {
const res = await fetch(`${API_BASE}/api/v1/files`);
if (!res.ok) {
throw new Error(`Failed to list files: ${res.statusText}`);
}
return res.json();
}
export async function getFile(id: string): Promise<FileDetail> {
const res = await fetch(`${API_BASE}/api/v1/files/${id}`);
if (!res.ok) {
throw new Error(`Failed to get file: ${res.statusText}`);
}
return res.json();
}
export async function createFile(data: CreateFileRequest): Promise<FileDetail> {
const res = await fetch(`${API_BASE}/api/v1/files`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (!res.ok) {
throw new Error(`Failed to create file: ${res.statusText}`);
}
return res.json();
}
export async function updateFile(
id: string,
data: UpdateFileRequest
): Promise<FileDetail> {
const res = await fetch(`${API_BASE}/api/v1/files/${id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (res.status === 409) {
const conflict = (await res.json()) as ConflictErrorResponse;
throw new VersionConflictError(conflict);
}
if (!res.ok) {
throw new Error(`Failed to update file: ${res.statusText}`);
}
return res.json();
}
export async function deleteFile(id: string): Promise<void> {
const res = await fetch(`${API_BASE}/api/v1/files/${id}`, {
method: "DELETE",
});
if (!res.ok) {
throw new Error(`Failed to delete file: ${res.statusText}`);
}
}
// Chat API function
export async function chatWithFile(
id: string,
message: string,
model?: LlmModel,
history?: ChatMessage[]
): Promise<ChatResponse> {
const body: ChatRequest = { message };
if (model) {
body.model = model;
}
if (history && history.length > 0) {
body.history = history;
}
const res = await fetch(`${API_BASE}/api/v1/files/${id}/chat`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) {
const errorText = await res.text();
throw new Error(`Chat failed: ${errorText || res.statusText}`);
}
return res.json();
}
// Version history types
export type VersionSource = "user" | "llm" | "system";
export interface FileVersion {
version: number;
name: string;
description: string | null;
summary: string | null;
body: BodyElement[];
source: VersionSource;
createdAt: string;
changeDescription?: string;
}
export interface FileVersionSummary {
version: number;
source: VersionSource;
createdAt: string;
changeDescription?: string;
}
export interface FileVersionListResponse {
versions: FileVersionSummary[];
total: number;
}
export interface RestoreVersionRequest {
targetVersion: number;
}
// Version history API functions
export async function listFileVersions(fileId: string): Promise<FileVersionListResponse> {
const res = await fetch(`${API_BASE}/api/v1/files/${fileId}/versions`);
if (!res.ok) {
throw new Error(`Failed to list versions: ${res.statusText}`);
}
return res.json();
}
export async function getFileVersion(fileId: string, version: number): Promise<FileVersion> {
const res = await fetch(`${API_BASE}/api/v1/files/${fileId}/versions/${version}`);
if (!res.ok) {
throw new Error(`Failed to get version: ${res.statusText}`);
}
return res.json();
}
export async function restoreFileVersion(
fileId: string,
targetVersion: number,
currentVersion: number
): Promise<FileDetail> {
const res = await fetch(`${API_BASE}/api/v1/files/${fileId}/versions/restore`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ targetVersion, currentVersion }),
});
if (res.status === 409) {
const conflict = (await res.json()) as ConflictErrorResponse;
throw new VersionConflictError(conflict);
}
if (!res.ok) {
throw new Error(`Failed to restore version: ${res.statusText}`);
}
return res.json();
}
// =============================================================================
// LLM Tool Definitions for Version History
// =============================================================================
// These types define the tools available to the LLM for version history access.
// The backend should implement handlers for these tools.
/**
* Tool: read_version
* Allows the LLM to read the content of a specific historical version.
* This is read-only - it does not modify the document.
*/
export interface ReadVersionToolInput {
version: number;
}
export interface ReadVersionToolOutput {
success: boolean;
version: number;
body: BodyElement[];
summary: string | null;
source: VersionSource;
createdAt: string;
changeDescription?: string;
message: string;
}
/**
* Tool: list_versions
* Allows the LLM to list all available versions of the document.
*/
export interface ListVersionsToolOutput {
success: boolean;
versions: FileVersionSummary[];
currentVersion: number;
message: string;
}
/**
* Tool: restore_version
* Allows the LLM to restore the document to a specific historical version.
* This creates a new version with the content from the target version.
*/
export interface RestoreVersionToolInput {
targetVersion: number;
reason?: string;
}
export interface RestoreVersionToolOutput {
success: boolean;
previousVersion: number;
newVersion: number;
restoredFromVersion: number;
message: string;
}
// LLM Tool type definitions for the backend
export type LlmVersionTool =
| { name: "read_version"; input: ReadVersionToolInput }
| { name: "list_versions"; input: Record<string, never> }
| { name: "restore_version"; input: RestoreVersionToolInput };
export type LlmVersionToolResult =
| { name: "read_version"; result: ReadVersionToolOutput }
| { name: "list_versions"; result: ListVersionsToolOutput }
| { name: "restore_version"; result: RestoreVersionToolOutput };
|