Skip to content

Commit 665c765

Browse files
committed
fix: filter empty assistant messages to prevent provider errors (issue #6056)
Some providers (e.g. Moonshot AI) reject requests with empty assistant messages that have no tool_calls. These orphaned messages are created when a tool call is cancelled mid-stream. Added filterEmptyAssistantMessages() to strip these from the request body before sending to the provider. Messages with tool_calls are kept even if content is empty, as they are part of valid tool-call chains. Ref: anomalyco/opencode#6056
1 parent 4bfc935 commit 665c765

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@keeveeg/opencode-wireguard-proxy",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "OpenCode plugin: route specific hostnames through WireGuard/AmneziaWG via wireproxy (userspace HTTP proxy)",
55
"type": "module",
66
"main": "./dist/index.js",

‎src/fetch-patch.ts‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,44 @@
33
// Bun's native fetch supports a `proxy` string option:
44
// fetch(url, { proxy: "http://127.0.0.1:25345" })
55
//
6-
// We override globalThis.fetch to route matching hostnames through the
7-
// wireproxy HTTP proxy. Everything else goes direct.
6+
// We override globalThis.fetch to:
7+
// 1. Route matching hostnames through the wireproxy HTTP proxy.
8+
// 2. Filter out orphaned empty assistant messages that cause
9+
// "must not be empty" errors on providers like Moonshot AI.
10+
// See: https://github.com/anomalyco/opencode/issues/6056
11+
12+
/**
13+
* Filter empty assistant messages from request body.
14+
*
15+
* Some providers (e.g. Moonshot AI) reject requests with empty assistant
16+
* messages that have no tool_calls. These are created when a tool call is
17+
* cancelled mid-stream. We strip them to prevent provider errors.
18+
*
19+
* Messages with tool_calls are kept even if content is empty — they are part
20+
* of valid tool-call chains.
21+
*/
22+
function filterEmptyAssistantMessages(body: Record<string, unknown>): boolean {
23+
const messages = body.messages
24+
if (!Array.isArray(messages)) return false
25+
26+
const before = messages.length
27+
body.messages = messages.filter((msg: Record<string, unknown>) => {
28+
if (msg.role !== "assistant") return true
29+
30+
const content = msg.content
31+
const hasToolCalls = Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0
32+
33+
const isEmpty =
34+
!content ||
35+
(typeof content === "string" && content.trim() === "") ||
36+
(Array.isArray(content) && content.length === 0)
37+
38+
// Keep if has tool_calls, remove only if truly empty/orphaned
39+
return !(isEmpty && !hasToolCalls)
40+
})
41+
42+
return (body.messages as unknown[]).length !== before
43+
}
844

945
function shouldProxy(url: string, hosts: Set<string>): boolean {
1046
try {
@@ -44,6 +80,18 @@ export function patchFetch(proxiedHosts: string[], proxyUrl: string): () => void
4480
input: string | URL | Request,
4581
init?: RequestInit,
4682
): Promise<Response> {
83+
// Filter empty assistant messages from JSON bodies (issue #6056)
84+
if (init?.body && typeof init.body === "string") {
85+
try {
86+
const body = JSON.parse(init.body)
87+
if (body.messages && filterEmptyAssistantMessages(body)) {
88+
init = { ...init, body: JSON.stringify(body) }
89+
}
90+
} catch {
91+
// Not JSON or parse error — pass through
92+
}
93+
}
94+
4795
const url = extractUrl(input)
4896
if (url && shouldProxy(url, hostSet)) {
4997
// Bun reads `proxy` from the init object before creating the Request.

0 commit comments

Comments
 (0)