|
3 | 3 | // Bun's native fetch supports a `proxy` string option: |
4 | 4 | // fetch(url, { proxy: "http://127.0.0.1:25345" }) |
5 | 5 | // |
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 | +} |
8 | 44 |
|
9 | 45 | function shouldProxy(url: string, hosts: Set<string>): boolean { |
10 | 46 | try { |
@@ -44,6 +80,18 @@ export function patchFetch(proxiedHosts: string[], proxyUrl: string): () => void |
44 | 80 | input: string | URL | Request, |
45 | 81 | init?: RequestInit, |
46 | 82 | ): 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 | + |
47 | 95 | const url = extractUrl(input) |
48 | 96 | if (url && shouldProxy(url, hostSet)) { |
49 | 97 | // Bun reads `proxy` from the init object before creating the Request. |
|
0 commit comments