Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ This project follows Semantic Versioning.

## Unreleased

## [0.11.2] - 2026-06-06

### Fixed

- Fixed Telegram prompt failures on OpenCode servers that reject async prompt admission by falling back to the synchronous prompt endpoint. (#55)

## [0.11.1] - 2026-06-06

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@crankshift/opencode-remote",
"description": "A messenger-based chat interface for OpenCode, starting with Telegram.",
"version": "0.11.1",
"version": "0.11.2",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/bin/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function createGatewayProgram({
const program = new Command()
const afterCreate = createStartupAfterConfigHook({ enableGatewayStartup, output })

program.name("opencode-remote").description("OpenCode messaging gateway").version("0.11.1")
program.name("opencode-remote").description("OpenCode messaging gateway").version("0.11.2")

program
.command("setup")
Expand Down
32 changes: 27 additions & 5 deletions src/core/opencode/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ export function createOpenCodeClient({
})
try {
if (typeof client.session?.promptAsync === "function" && progressStream.active) {
await client.session.promptAsync({
path: { id: sessionId },
body: { ...promptBody, messageID: asyncPrompt.messageId },
})
return await asyncPrompt.wait()
try {
await client.session.promptAsync({
path: { id: sessionId },
body: { ...promptBody, messageID: asyncPrompt.messageId },
})
return await asyncPrompt.wait()
} catch (error) {
if (!shouldFallbackToSynchronousPrompt(error)) {
throw error
}
}
}

const response = toData(
Expand Down Expand Up @@ -257,6 +263,22 @@ function noopProgressStream() {
return { active: false, stop: async () => undefined }
}

function shouldFallbackToSynchronousPrompt(error) {
const status = error?.cause?.status
if (status === 400 || status === 404 || status === 405) {
return true
}
const text = [error?.name, error?.code, error?.message]
.filter((value) => typeof value === "string")
.join(" ")
.toLocaleLowerCase("en-US")

return (
text.includes("prompt_async") &&
(text.includes("400") || text.includes("404") || text.includes("405"))
)
}

function createAsyncPromptCompletion(sessionId, timeoutMs) {
const messageId = randomUUID()
const textPartsByMessage = new Map()
Expand Down
23 changes: 23 additions & 0 deletions tests/core/opencodeClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ describe("createOpenCodeClient", () => {
}
})

test("falls back to synchronous prompts when async admission fails", async () => {
const stream = createControlledEventStream()
const sdkClient = {
event: { list: vi.fn(async () => stream) },
session: {
promptAsync: vi.fn(async () => {
throw new Error("opencode server POST /session/ses_1/prompt_async -> 404")
}),
prompt: vi.fn(async () => ({ parts: [{ type: "text", text: "fallback answer" }] })),
},
}
const client = createOpenCodeClient({ sdkClient })

await expect(client.sendPrompt("ses_1", "research task")).resolves.toBe("fallback answer")

expect(sdkClient.session.promptAsync).toHaveBeenCalled()
expect(sdkClient.session.prompt).toHaveBeenCalledWith({
path: { id: "ses_1" },
body: { parts: [{ type: "text", text: "research task" }] },
})
expect(stream.controller.abort).toHaveBeenCalled()
})

test("uses current SDK event.subscribe stream shape for progress", async () => {
let subscribeSignal
const stream = createEventStream([
Expand Down