Two independent bugs made Qoder turns hang or fail outright.
1. The SSE read loop never terminated on the sentinel. `data: [DONE]`
broke only the inner line loop, so the outer loop kept awaiting
reader.read() until the socket closed. Qoder's gateway holds the body
open after the sentinel, so a reply that had already streamed in full
produced no `done` event: the agent sat there with the complete answer
buffered and no error, until something else timed the connection out.
The sentinel also arrives wrapped in an envelope (`body: "[DONE]"`),
which likewise only did `continue`, so both forms now end the loop.
The reader is cancelled once the reply is complete instead of being
left holding the connection.
2. Aborting a turn produced an invalid request. Assistant messages with
stopReason error/aborted are skipped, but the tool result that followed
was still emitted, leaving a `tool` message whose tool_call_id had no
matching tool_calls entry. Upstreams reject that with "tool must follow
a message with tool_calls" -- the same failure the null-content
workaround above it already guards against -- so after one cancelled
tool call, every later request in that session failed. Tool calls
belonging to a dropped turn are now recorded and their results dropped
with them.
Both fixes are covered by regression tests that fail on the current code:
the two stream tests time out at the 8s limit, and the transform tests
produce an orphaned tool message.
A tool result can carry images. pi's `read` tool returns a text note plus an
`image` block for png/jpg/gif/webp/bmp, and screenshot tools do the same.
transformMessagesForQoder() built the `tool` message with getContentText(),
which maps every non-text block to "". The images were dropped silently: the
TUI rendered the picture, the model received only "Read image file [image/png]"
and answered that it could not see images — then fell back to scraping the DOM.
Nothing reported a problem, and the model catalogue advertises input
["text", "image"], so the capability check upstream of this had no reason to
warn either.
The OpenAI-shaped `tool` role has nowhere to put them: its content is a plain
string. So the images now follow as a separate user message, using the same
image_url part the user branch already builds, with a short label so the model
does not read a bare image as something the human just sent.
Tool results without images are unchanged and still produce exactly one
message.
When assistant messages contain only tool_calls (no text/thinking), transform.ts
was setting content to null. Qoder's gateway drops these null-content assistant
messages, orphaning the following tool_result and causing upstream rejections:
- dmodel (OpenAI): 'tool must follow a message with tool_calls'
- ultimate (Anthropic): 'unexpected tool_use_id in tool_result blocks'
Analysis of 28 historical sessions showed 100% failure rate on dmodel and 75%
on ultimate for this pattern, while auto/GLM5.2 (GLM upstream) tolerated it.
Fix: inject a single-space placeholder when tool_calls are present but content
is empty, so the gateway preserves the assistant message and tool pairing stays
valid.
Also fixes pre-existing lint formatting in stream.ts and package.json.
Adds regression test for the full assistant+toolResult pairing scenario.
Verified via behavior test against Qoder API (ultimate model):
- system field with a directive: model ignores it (prompt_tokens=15, normal reply)
- same directive in messages[0] role:system: model obeys (prompt_tokens=97, marker reply)
The server's top-level `system` field in the request body is never fed to
the model, so pi's systemPrompt (language prefs, tool rules, agent instructions)
was silently dropped. Inject it as a leading role:system message instead, which
the server honors.
The custom ctx.ui.custom login menu stole focus from pi's built-in
LoginDialog, so onPrompt never received keystrokes and PAT login
silently failed with 'No API key found'. Drive login via the provided
onPrompt/onAuth/onProgress callbacks instead, and drop login-ui.ts and
the now-unused pi-tui dependency. Also clears remaining lint warnings
by removing any-casts in models/oauth/stream/transform.