Skip to content

Commit 3a30773

Browse files
committed
tui: refactor event streaming to use SDK instead of manual RPC subscription
1 parent 0c0057a commit 3a30773

3 files changed

Lines changed: 125 additions & 29 deletions

File tree

‎STYLE_GUIDE.md‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,71 @@
44
- AVOID unnecessary destructuring of variables. instead of doing `const { a, b }
55
= obj` just reference it as obj.a and obj.b. this preserves context
66
- AVOID `try`/`catch` where possible
7-
- AVOID `else` statements
87
- AVOID using `any` type
9-
- AVOID `let` statements
108
- PREFER single word variable names where possible
119
- Use as many bun apis as possible like Bun.file()
10+
11+
# Avoid let statements
12+
13+
we don't like let statements, especially combined with if/else statements.
14+
prefer const
15+
16+
This is bad:
17+
18+
Good:
19+
20+
```ts
21+
const foo = condition ? 1 : 2
22+
```
23+
24+
Bad:
25+
26+
```ts
27+
let foo
28+
29+
if (condition) foo = 1
30+
else foo = 2
31+
```
32+
33+
# Avoid else statements
34+
35+
Prefer early returns or even using `iife` to avoid else statements
36+
37+
Good:
38+
39+
```ts
40+
function foo() {
41+
if (condition) return 1
42+
return 2
43+
}
44+
```
45+
46+
Bad:
47+
48+
```ts
49+
function foo() {
50+
if (condition) return 1
51+
else return 2
52+
}
53+
```
54+
55+
# Prefer single word naming
56+
57+
Try your best to find a single word name for your variables, functions, etc.
58+
Only use multiple words if you cannot.
59+
60+
Good:
61+
62+
```ts
63+
const foo = 1
64+
const bar = 2
65+
const baz = 3
66+
```
67+
68+
Bad:
69+
70+
```ts
71+
const fooBar = 1
72+
const barBaz = 2
73+
const bazFoo = 3
74+
```

‎packages/opencode/src/cli/cmd/tui/thread.ts‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,9 @@ function createWorkerFetch(client: RpcClient): typeof fetch {
3434
return fn as typeof fetch
3535
}
3636

37-
function createEventSource(client: RpcClient, directory: string): EventSource {
37+
function createEventSource(client: RpcClient): EventSource {
3838
return {
39-
on: (handler) =>
40-
client.on<Event>("event", (event) => {
41-
handler(event)
42-
if (event.type === "server.instance.disposed") {
43-
client.call("subscribe", { directory }).catch(() => {})
44-
}
45-
}),
39+
on: (handler) => client.on<Event>("event", handler),
4640
}
4741
}
4842

@@ -131,9 +125,6 @@ export const TuiThreadCommand = cmd({
131125
networkOpts.port !== 0 ||
132126
networkOpts.hostname !== "127.0.0.1"
133127

134-
// Subscribe to events from worker
135-
await client.call("subscribe", { directory: cwd })
136-
137128
let url: string
138129
let customFetch: typeof fetch | undefined
139130
let events: EventSource | undefined
@@ -146,7 +137,7 @@ export const TuiThreadCommand = cmd({
146137
// Use direct RPC communication (no HTTP)
147138
url = "http://opencode.internal"
148139
customFetch = createWorkerFetch(client)
149-
events = createEventSource(client, cwd)
140+
events = createEventSource(client)
150141
}
151142

152143
const tuiPromise = tui({

‎packages/opencode/src/cli/cmd/tui/worker.ts‎

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { InstanceBootstrap } from "@/project/bootstrap"
66
import { Rpc } from "@/util/rpc"
77
import { upgrade } from "@/cli/upgrade"
88
import { Config } from "@/config/config"
9-
import { Bus } from "@/bus"
109
import { GlobalBus } from "@/bus/global"
10+
import { createOpencodeClient, type Event } from "@opencode-ai/sdk/v2"
1111
import type { BunWebSocketData } from "hono/bun"
1212

1313
await Log.init({
@@ -38,6 +38,61 @@ GlobalBus.on("event", (event) => {
3838

3939
let server: Bun.Server<BunWebSocketData> | undefined
4040

41+
const eventStream = {
42+
abort: undefined as AbortController | undefined,
43+
}
44+
45+
const startEventStream = (directory: string) => {
46+
if (eventStream.abort) eventStream.abort.abort()
47+
const abort = new AbortController()
48+
eventStream.abort = abort
49+
const signal = abort.signal
50+
51+
const fetchFn = (async (input: RequestInfo | URL, init?: RequestInit) => {
52+
const request = new Request(input, init)
53+
return Server.App().fetch(request)
54+
}) as typeof globalThis.fetch
55+
56+
const sdk = createOpencodeClient({
57+
baseUrl: "http://opencode.internal",
58+
directory,
59+
fetch: fetchFn,
60+
signal,
61+
})
62+
63+
;(async () => {
64+
while (!signal.aborted) {
65+
const events = await Promise.resolve(
66+
sdk.event.subscribe(
67+
{},
68+
{
69+
signal,
70+
},
71+
),
72+
).catch(() => undefined)
73+
74+
if (!events) {
75+
await Bun.sleep(250)
76+
continue
77+
}
78+
79+
for await (const event of events.stream) {
80+
Rpc.emit("event", event as Event)
81+
}
82+
83+
if (!signal.aborted) {
84+
await Bun.sleep(250)
85+
}
86+
}
87+
})().catch((error) => {
88+
Log.Default.error("event stream error", {
89+
error: error instanceof Error ? error.message : error,
90+
})
91+
})
92+
}
93+
94+
startEventStream(process.cwd())
95+
4196
export const rpc = {
4297
async fetch(input: { url: string; method: string; headers: Record<string, string>; body?: string }) {
4398
const request = new Request(input.url, {
@@ -58,20 +113,6 @@ export const rpc = {
58113
server = Server.listen(input)
59114
return { url: server.url.toString() }
60115
},
61-
async subscribe(input: { directory: string }) {
62-
return Instance.provide({
63-
directory: input.directory,
64-
init: InstanceBootstrap,
65-
fn: async () => {
66-
Bus.subscribeAll((event) => {
67-
Rpc.emit("event", event)
68-
})
69-
// Emit connected event
70-
Rpc.emit("event", { type: "server.connected", properties: {} })
71-
return { subscribed: true }
72-
},
73-
})
74-
},
75116
async checkUpgrade(input: { directory: string }) {
76117
await Instance.provide({
77118
directory: input.directory,
@@ -87,6 +128,7 @@ export const rpc = {
87128
},
88129
async shutdown() {
89130
Log.Default.info("worker shutting down")
131+
if (eventStream.abort) eventStream.abort.abort()
90132
await Instance.disposeAll()
91133
if (server) server.stop(true)
92134
},

0 commit comments

Comments
 (0)