MCPcopy Create free account
hub / github.com/TraderAlice/OpenAlice / readWorkspaceFile

Function readWorkspaceFile

ui/src/components/workspace/api.ts:520–543  ·  view source on GitHub ↗
(id: string, relPath: string)

Source from the content-addressed store, hash-verified

518 | { kind: 'error'; message: string };
519
520export async function readWorkspaceFile(id: string, relPath: string): Promise<ReadFileResult> {
521 const bridge = electronWorkspaceBridge();
522 if (bridge) return bridge.readFile({ id, path: relPath });
523 const qs = `?path=${encodeURIComponent(relPath)}`;
524 let res: Response;
525 try {
526 res = await fetch(`/api/workspaces/${encodeURIComponent(id)}/file${qs}`);
527 } catch (err) {
528 return { kind: 'error', message: (err as Error).message };
529 }
530 if (res.ok) {
531 const body = (await res.json()) as { content: string };
532 return { kind: 'ok', content: body.content };
533 }
534 // Map known error shapes to discriminated variants. Unknown → generic error.
535 const body = (await res.json().catch(() => null)) as { error?: string; sizeBytes?: number } | null;
536 switch (body?.error) {
537 case 'workspace_not_found': return { kind: 'workspace_missing' };
538 case 'file_not_found': return { kind: 'file_missing' };
539 case 'file_too_large': return { kind: 'too_large', sizeBytes: body.sizeBytes ?? 0 };
540 case 'invalid_path': return { kind: 'invalid_path' };
541 default: return { kind: 'error', message: body?.error ?? `HTTP ${res.status}` };
542 }
543}
544
545// ── Agent provider config ───────────────────────────────────────────────────
546

Callers 2

DocBlockFunction · 0.90
FileViewerPageFunction · 0.90

Calls 2

electronWorkspaceBridgeFunction · 0.85
fetchFunction · 0.50

Tested by

no test coverage detected