(id: string, relPath: string)
| 518 | | { kind: 'error'; message: string }; |
| 519 | |
| 520 | export 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 |
no test coverage detected