| 62 | export type FetchHandler = (url: URL) => Response | Promise<Response> | undefined |
| 63 | |
| 64 | export function createFetch(override?: FetchHandler, events?: ReturnType<typeof createEventSource>) { |
| 65 | const session = [] as URL[] |
| 66 | const fetch = (async (input: RequestInfo | URL) => { |
| 67 | const url = new URL(input instanceof Request ? input.url : String(input)) |
| 68 | if (url.pathname === "/session") session.push(url) |
| 69 | const overridden = await override?.(url) |
| 70 | if (overridden) return overridden |
| 71 | if (url.pathname === "/api/event" && events) return events.response() |
| 72 | |
| 73 | if ( |
| 74 | [ |
| 75 | "/agent", |
| 76 | "/command", |
| 77 | "/experimental/workspace", |
| 78 | "/experimental/workspace/status", |
| 79 | "/formatter", |
| 80 | "/lsp", |
| 81 | ].includes(url.pathname) |
| 82 | ) |
| 83 | return json([]) |
| 84 | if (["/config", "/experimental/resource", "/mcp", "/provider/auth", "/session/status"].includes(url.pathname)) |
| 85 | return json({}) |
| 86 | if (url.pathname === "/config/providers") return json({ providers: {}, default: {} }) |
| 87 | if (url.pathname === "/experimental/console") return json({ consoleManagedProviders: [], switchableOrgCount: 0 }) |
| 88 | if (url.pathname === "/experimental/capabilities") return json({ backgroundSubagents: false }) |
| 89 | if (url.pathname === "/path") return json({ home: "", state: "", config: "", worktree, directory }) |
| 90 | if (url.pathname === "/api/location") return json({ directory, project: { id: "proj_test", directory: worktree } }) |
| 91 | if ( |
| 92 | ["/api/agent", "/api/model", "/api/provider", "/api/integration", "/api/command", "/api/skill"].includes( |
| 93 | url.pathname, |
| 94 | ) |
| 95 | ) |
| 96 | return json({ |
| 97 | location: { directory, project: { id: "proj_test", directory: worktree } }, |
| 98 | data: [], |
| 99 | }) |
| 100 | if (url.pathname === "/project/current") return json({ id: "proj_test" }) |
| 101 | if (url.pathname === "/api/reference") |
| 102 | return json({ location: { directory, project: { id: "proj_test", directory } }, data: [] }) |
| 103 | if (url.pathname === "/provider") return json({ all: [], default: {}, connected: [] }) |
| 104 | if (url.pathname === "/session") return json([]) |
| 105 | if (url.pathname === "/vcs") return json({ branch: "main" }) |
| 106 | throw new Error(`unexpected request: ${url.pathname}`) |
| 107 | }) as typeof globalThis.fetch |
| 108 | return { fetch, session } |
| 109 | } |