|
| 1 | +import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2/client"; |
| 2 | +import { createGlobalEmitter } from "@solid-primitives/event-bus"; |
| 3 | +import { batch, createContext, onCleanup, useContext, type ParentProps } from "solid-js"; |
| 4 | + |
| 5 | +import { usePlatform } from "./platform"; |
| 6 | +import { useServer } from "./server"; |
| 7 | + |
| 8 | +type GlobalSDKContextValue = { |
| 9 | + url: string; |
| 10 | + client: ReturnType<typeof createOpencodeClient>; |
| 11 | + event: ReturnType<typeof createGlobalEmitter<{ [key: string]: Event }>>; |
| 12 | +}; |
| 13 | + |
| 14 | +const GlobalSDKContext = createContext<GlobalSDKContextValue | undefined>(undefined); |
| 15 | + |
| 16 | +export function GlobalSDKProvider(props: ParentProps) { |
| 17 | + const server = useServer(); |
| 18 | + const platform = usePlatform(); |
| 19 | + const abort = new AbortController(); |
| 20 | + |
| 21 | + const eventClient = createOpencodeClient({ |
| 22 | + baseUrl: server.url, |
| 23 | + signal: abort.signal, |
| 24 | + fetch: platform.fetch, |
| 25 | + }); |
| 26 | + |
| 27 | + const emitter = createGlobalEmitter<{ [key: string]: Event }>(); |
| 28 | + |
| 29 | + type Queued = { directory: string; payload: Event }; |
| 30 | + |
| 31 | + let queue: Array<Queued | undefined> = []; |
| 32 | + const coalesced = new Map<string, number>(); |
| 33 | + let timer: ReturnType<typeof setTimeout> | undefined; |
| 34 | + let last = 0; |
| 35 | + |
| 36 | + const keyForEvent = (directory: string, payload: Event) => { |
| 37 | + if (payload.type === "session.status") return `session.status:${directory}:${payload.properties.sessionID}`; |
| 38 | + if (payload.type === "lsp.updated") return `lsp.updated:${directory}`; |
| 39 | + if (payload.type === "todo.updated") return `todo.updated:${directory}:${payload.properties.sessionID}`; |
| 40 | + if (payload.type === "mcp.tools.changed") return `mcp.tools.changed:${directory}:${payload.properties.server}`; |
| 41 | + if (payload.type === "message.part.updated") { |
| 42 | + const part = payload.properties.part; |
| 43 | + return `message.part.updated:${directory}:${part.messageID}:${part.id}`; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + const flush = () => { |
| 48 | + if (timer) clearTimeout(timer); |
| 49 | + timer = undefined; |
| 50 | + |
| 51 | + const events = queue; |
| 52 | + queue = []; |
| 53 | + coalesced.clear(); |
| 54 | + if (events.length === 0) return; |
| 55 | + |
| 56 | + last = Date.now(); |
| 57 | + batch(() => { |
| 58 | + for (const entry of events) { |
| 59 | + if (!entry) continue; |
| 60 | + emitter.emit(entry.directory, entry.payload); |
| 61 | + } |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + const schedule = () => { |
| 66 | + if (timer) return; |
| 67 | + const elapsed = Date.now() - last; |
| 68 | + timer = setTimeout(flush, Math.max(0, 16 - elapsed)); |
| 69 | + }; |
| 70 | + |
| 71 | + const stop = () => { |
| 72 | + flush(); |
| 73 | + }; |
| 74 | + |
| 75 | + void (async () => { |
| 76 | + const subscription = await eventClient.event.subscribe(undefined, { signal: abort.signal }); |
| 77 | + let yielded = Date.now(); |
| 78 | + |
| 79 | + for await (const event of subscription.stream as AsyncIterable<unknown>) { |
| 80 | + const record = event as Event & { directory?: string; payload?: Event }; |
| 81 | + const payload = record.payload ?? record; |
| 82 | + if (!payload?.type) continue; |
| 83 | + |
| 84 | + const directory = typeof record.directory === "string" ? record.directory : "global"; |
| 85 | + const key = keyForEvent(directory, payload); |
| 86 | + if (key) { |
| 87 | + const index = coalesced.get(key); |
| 88 | + if (index !== undefined) { |
| 89 | + queue[index] = undefined; |
| 90 | + } |
| 91 | + coalesced.set(key, queue.length); |
| 92 | + } |
| 93 | + |
| 94 | + queue.push({ directory, payload }); |
| 95 | + schedule(); |
| 96 | + |
| 97 | + if (Date.now() - yielded < 8) continue; |
| 98 | + yielded = Date.now(); |
| 99 | + await new Promise<void>((resolve) => setTimeout(resolve, 0)); |
| 100 | + } |
| 101 | + })() |
| 102 | + .finally(stop) |
| 103 | + .catch(() => undefined); |
| 104 | + |
| 105 | + onCleanup(() => { |
| 106 | + abort.abort(); |
| 107 | + stop(); |
| 108 | + }); |
| 109 | + |
| 110 | + const client = createOpencodeClient({ |
| 111 | + baseUrl: server.url, |
| 112 | + fetch: platform.fetch, |
| 113 | + throwOnError: true, |
| 114 | + }); |
| 115 | + |
| 116 | + const value: GlobalSDKContextValue = { |
| 117 | + url: server.url, |
| 118 | + client, |
| 119 | + event: emitter, |
| 120 | + }; |
| 121 | + |
| 122 | + return <GlobalSDKContext.Provider value={value}>{props.children}</GlobalSDKContext.Provider>; |
| 123 | +} |
| 124 | + |
| 125 | +export function useGlobalSDK() { |
| 126 | + const context = useContext(GlobalSDKContext); |
| 127 | + if (!context) { |
| 128 | + throw new Error("Global SDK context is missing"); |
| 129 | + } |
| 130 | + return context; |
| 131 | +} |
0 commit comments