tui-sdk.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import type { GlobalEvent } from "@opencode-ai/sdk/v2"
  2. import type { EventSource } from "../../src/context/sdk"
  3. export const worktree = "/tmp/opencode"
  4. export const directory = `${worktree}/packages/tui`
  5. export function json(data: unknown, init?: ResponseInit) {
  6. return new Response(JSON.stringify(data), {
  7. ...init,
  8. headers: { "content-type": "application/json", ...(init?.headers ?? {}) },
  9. })
  10. }
  11. export function eventSource(): EventSource {
  12. return { subscribe: async () => () => {} }
  13. }
  14. export function createEventSource() {
  15. let fn: ((event: GlobalEvent) => void) | undefined
  16. return {
  17. source: {
  18. subscribe: async (handler: (event: GlobalEvent) => void) => {
  19. fn = handler
  20. return () => {
  21. if (fn === handler) fn = undefined
  22. }
  23. },
  24. } satisfies EventSource,
  25. emit(event: GlobalEvent) {
  26. if (!fn) throw new Error("event source not ready")
  27. fn(event)
  28. },
  29. }
  30. }
  31. export type FetchHandler = (url: URL) => Response | Promise<Response> | undefined
  32. export function createFetch(override?: FetchHandler) {
  33. const session = [] as URL[]
  34. const fetch = (async (input: RequestInfo | URL) => {
  35. const url = new URL(input instanceof Request ? input.url : String(input))
  36. if (url.pathname === "/session") session.push(url)
  37. const overridden = await override?.(url)
  38. if (overridden) return overridden
  39. if (
  40. [
  41. "/agent",
  42. "/command",
  43. "/experimental/workspace",
  44. "/experimental/workspace/status",
  45. "/formatter",
  46. "/lsp",
  47. ].includes(url.pathname)
  48. )
  49. return json([])
  50. if (["/config", "/experimental/resource", "/mcp", "/provider/auth", "/session/status"].includes(url.pathname))
  51. return json({})
  52. if (url.pathname === "/config/providers") return json({ providers: {}, default: {} })
  53. if (url.pathname === "/experimental/console") return json({ consoleManagedProviders: [], switchableOrgCount: 0 })
  54. if (url.pathname === "/path") return json({ home: "", state: "", config: "", worktree, directory })
  55. if (url.pathname === "/api/location") return json({ directory, project: { id: "proj_test", directory: worktree } })
  56. if (
  57. ["/api/agent", "/api/model", "/api/provider", "/api/integration", "/api/command", "/api/skill"].includes(
  58. url.pathname,
  59. )
  60. )
  61. return json({
  62. location: { directory, project: { id: "proj_test", directory: worktree } },
  63. data: [],
  64. })
  65. if (url.pathname === "/project/current") return json({ id: "proj_test" })
  66. if (url.pathname === "/api/reference")
  67. return json({ location: { directory, project: { id: "proj_test", directory } }, data: [] })
  68. if (url.pathname === "/provider") return json({ all: [], default: {}, connected: [] })
  69. if (url.pathname === "/session") return json([])
  70. if (url.pathname === "/vcs") return json({ branch: "main" })
  71. throw new Error(`unexpected request: ${url.pathname}`)
  72. }) as typeof globalThis.fetch
  73. return { fetch, session }
  74. }