Repository navigation
fix(provider): stabilize Pollinations tool calls across protocol dialects - #871
Conversation
…ects
Models routed through some Pollinations backends (observed: GLM-5.3-Flash
and GPT-6-Luna via Poe) return tool calls as text. The merged middleware
already decoded the nested {"name","arguments"} envelope, but real agent
loops still broke:
- Turns died with "unterminated reasoning block" when a think block was
truncated; now salvaged as reasoning.
- Turns died when an envelope named an unoffered tool; now demoted to
visible text instead of throwing.
- The flat dialect seen in real agent loops ({"name":"shell","command":...})
never decoded, so calls leaked as raw JSON and the shell never ran; both
dialects now decode, with promotion still requiring an offered tool and
the anchored protocol scope (content must start with a think block or an
envelope, so quoted examples in prose are never executed).
- All text was buffered until stream end; plain text now streams live and
protocol reasoning emits as each think block closes.
- Stream event shape is clean: real text wrappers are held until the mode
resolves, synthetic parts are sequential, tool call ids are unique across
parts, and stream/generate classification uses the same anchored rule.
Known limitation (deliberate): text before the protocol (a preamble) stays
ordinary text, because a preamble cannot be distinguished deterministically
from documentation that quotes the protocol.
Validated with 17 provider tests, a fixture matrix from real API captures,
an SDK-level two-round tool loop, and a live headless agent run in which
glm-5.3-flash-free executed echo hello-world through the shell tool and
finished cleanly.
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
| if (keys === "arguments,name") return isRecord(value.arguments) | ||
| // Flat dialect observed in the real agent loop (e.g. GLM calling shell): | ||
| // {"name":"shell","command":"pwd"} instead of nesting under "arguments". | ||
| return !("arguments" in value) |
There was a problem hiding this comment.
SUGGESTION: Flat-dialect envelope matching is overly broad and can promote non-tool JSON as tool calls
isEnvelopeShape treats any object with a string name and no arguments key as a flat-dialect tool call (line 406). If such an object appears at the start of content and a same-named tool is offered, it is promoted and executed -- for example {"name":"read","path":"./env"} could invoke a read tool. Promotion still requires an offered tool, but the broad match raises false-positive risk. Consider narrowing the flat-dialect match to expected parameter shapes, or documenting the accepted risk.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (2 files)
Previous Review Summary (commit a56e1ad)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit a56e1ad)Status: 1 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)SUGGESTION
Files Reviewed (2 files)
Reviewed by free · Input: 102.9K · Output: 32K · Cached: 429.3K |
Issue for this PR
Fixes #870
Type of change
What does this PR do?
Some Pollinations backends (observed:
community/morriszdweck/glm-5.3-flash-freeandcommunity/scriptsnsenses-sys/gpt-6-luna:free, both Poe-routed) return tool calls as text instead of structuredtool_calls. The middleware from #867 decoded the nested{"name":"...","arguments":{...}}envelope, but real agent loops still broke:{"name":"shell","command":"echo hello-world"}— not nested underarguments. The decoder only accepted the nested shape, so calls leaked as raw JSON text, the command never ran, the model hallucinated the output and the turn died after finish-tool nudges.unterminated reasoning blockand killed the turn.model requested unavailable tool.Changes in
packages/opencode/src/provider/pollinations-protocol.ts:argumentsand flat top-level parameters). Promotion still requires the tool to be offered and the content to start (after whitespace) with a think block or an envelope-shaped object, so prose or docs quoting the protocol never execute.Deliberate limitation: text before the protocol (a preamble like
Sure! <think>...) stays ordinary text and never executes — a preamble cannot be distinguished deterministically from documentation quoting the protocol. The raw protocol stays visible in that case.How did you verify your code works?
bun test test/provider/pollinations-protocol.test.ts: 17/17, including new regression tests for salvage, demote, false-promotion cases, chunk-boundary whitespace parity vs batch, progressive reasoning, clean stream shape for demoted tools, unique ids across parts, the flat dialect, and no-tool generate decoding.@ai-sdk/openai-compatiblestack against a local SSE replay; a two-round SDK tool loop round-trips the synthetic tool-call id.opencode run, this build, live API, free models only, no nudges):glm-5.3-flash-freeecho task executes and finishes cleanly (before this PR: raw JSON text, hallucinated output, turn killed); same model on a multi-step write-file / read-back / verify task passes; repeat echo passes;gpt-6-luna:freeecho passes;Saauf/mistral-large-4(native structured path, control) passes with no regression.Interactive TUI was not verifiable from the Linux sandbox used for this work (the TUI boots under a pty but frames/input do not materialize there); the validated
runpath shares the same provider wiring. An interactive check on a real terminal is still worthwhile.Screenshots / recordings
N/A — provider middleware, no UI change.
Checklist