Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ export function Prompt(props: PromptProps) {
}, 5000)

if (store.interrupt >= 2) {
sdk.client.session.abort({
sessionID: props.sessionID,
})
sdk.client.session
.abort({
sessionID: props.sessionID,
})
.catch(() => {})
setStore("interrupt", 0)
}
dialog.clear()
Expand Down
23 changes: 9 additions & 14 deletions packages/opencode/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,14 +1424,10 @@ export namespace Server {
),
validator("json", SessionPrompt.PromptInput.omit({ sessionID: true })),
async (c) => {
c.status(200)
c.header("Content-Type", "application/json")
return stream(c, async (stream) => {
const sessionID = c.req.valid("param").sessionID
const body = c.req.valid("json")
const msg = await SessionPrompt.prompt({ ...body, sessionID })
stream.write(JSON.stringify(msg))
})
const sessionID = c.req.valid("param").sessionID
const body = c.req.valid("json")
const msg = await SessionPrompt.prompt({ ...body, sessionID })
return c.json(msg)
},
)
.post(
Expand All @@ -1456,13 +1452,12 @@ export namespace Server {
),
validator("json", SessionPrompt.PromptInput.omit({ sessionID: true })),
async (c) => {
c.status(204)
c.header("Content-Type", "application/json")
return stream(c, async () => {
const sessionID = c.req.valid("param").sessionID
const body = c.req.valid("json")
SessionPrompt.prompt({ ...body, sessionID })
const sessionID = c.req.valid("param").sessionID
const body = c.req.valid("json")
void SessionPrompt.prompt({ ...body, sessionID }).catch((error) => {
log.error("prompt_async failed", { error, sessionID })
})
return c.body(null, 204)
},
)
.post(
Expand Down
33 changes: 25 additions & 8 deletions packages/opencode/src/util/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ export namespace Rpc {
onmessage = async (evt) => {
const parsed = JSON.parse(evt.data)
if (parsed.type === "rpc.request") {
const result = await rpc[parsed.method](parsed.input)
postMessage(JSON.stringify({ type: "rpc.result", result, id: parsed.id }))
try {
const result = await rpc[parsed.method](parsed.input)
postMessage(JSON.stringify({ type: "rpc.result", result, id: parsed.id }))
} catch (error) {
postMessage(
JSON.stringify({
type: "rpc.error",
error: error instanceof Error ? error.message : String(error),
id: parsed.id,
}),
)
}
}
}
}
Expand All @@ -21,15 +31,22 @@ export namespace Rpc {
postMessage: (data: string) => void | null
onmessage: ((this: Worker, ev: MessageEvent<any>) => any) | null
}) {
const pending = new Map<number, (result: any) => void>()
const pending = new Map<number, { resolve: (result: any) => void; reject: (error: Error) => void }>()
const listeners = new Map<string, Set<(data: any) => void>>()
let id = 0
target.onmessage = async (evt) => {
const parsed = JSON.parse(evt.data)
if (parsed.type === "rpc.result") {
const resolve = pending.get(parsed.id)
if (resolve) {
resolve(parsed.result)
const callbacks = pending.get(parsed.id)
if (callbacks) {
callbacks.resolve(parsed.result)
pending.delete(parsed.id)
}
}
if (parsed.type === "rpc.error") {
const callbacks = pending.get(parsed.id)
if (callbacks) {
callbacks.reject(new Error(parsed.error))
pending.delete(parsed.id)
}
}
Expand All @@ -45,8 +62,8 @@ export namespace Rpc {
return {
call<Method extends keyof T>(method: Method, input: Parameters<T[Method]>[0]): Promise<ReturnType<T[Method]>> {
const requestId = id++
return new Promise((resolve) => {
pending.set(requestId, resolve)
return new Promise((resolve, reject) => {
pending.set(requestId, { resolve, reject })
target.postMessage(JSON.stringify({ type: "rpc.request", method, input, id: requestId }))
})
},
Expand Down