The "files" field in package.json only includes "dist" and "README.md",
so src/index.ts is not available in the published npm package.
This was introduced in commit 1b110a6 ("fix index") which changed
the path from the correct "./dist/index.js" to "./src/index.ts".
A tool call whose arguments never arrive — a no-argument tool, or a model that
sends id and name and then stops — was accumulated into toolCallsState but
never added to the message, because the content block was only created inside
`if (tc.function?.arguments)`.
The finalizer then checked `toolCallsState.length > 0` and set
stopReason "toolUse" on a message containing no tool call at all. pi's agent
loop continues only on tool calls, so it had nothing to execute and the turn
ended: mid-task, no tool run, no error, nothing in the transcript except the
assistant saying what it was about to do. Observed against a real session where
the model announced "let me consult the advisor" four times in a row and the
run then stopped dead; its final message on disk is stopReason "toolUse" with a
single text block and no toolCall.
Two changes:
- open the block as soon as the call is identifiable (id or name), and keep
its id/name in step if they arrive in a later delta, since the finalizer
only rewrites `arguments`;
- guard the stopReason on blocks that actually reached the message rather
than on the state array being non-empty, so a genuinely malformed stream
reports "stop" instead of an unactionable "toolUse".
Tests: no-argument call, identity arriving after the block is open, and a
malformed delta that must not claim toolUse.
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.
Follow the fix in stream.ts: prompt_tokens (42) includes cached_tokens (5),
so input = 42 - 5 - 10 = 27. Use cache_write_tokens (not cacheable_tokens)
for cacheWrite, and keep cacheable_tokens:99 in the payload to prove it is
no longer mapped to cacheWrite.
pi-core computes promptTokens = input + cacheRead + cacheWrite (Anthropic
convention: input EXCLUDES cached/written tokens), but qoder follows OpenAI
semantics where prompt_tokens INCLUDES cached_tokens. pi-ai's own OpenAI
provider subtracts them; this provider didn't, which under-reported the
cache rate (45% vs the true ~83%) and tripped pi-core's miss detector on
every cached turn.
- input = max(0, prompt_tokens - cacheRead - cache_write_tokens)
- cacheWrite reads cache_write_tokens, not cacheable_tokens (a capacity
metric that is 0 even on first-turn writes, not a write count)
Qoder's backend sometimes routes a literal <thinking> opener into
reasoning_content while the matching closer lands in the content
stream, leaving tag fragments in the thinking block. Strip them during
streaming and add live repro scripts.
A fresh random session_id on every request meant the Qoder server could
never match a previous cache entry, so cacheRead was ~0% in all agentic
sessions. Ablation testing showed a stable session_id alone raises the
turn-2 cache hit rate from 0% to ~99.6% of prompt tokens (affinity
headers alone contribute nothing). The previously observed "406 Session
blocked" no longer reproduces with a per-pi-session stable id.
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.
Three stability fixes for the streaming layer. Together they eliminate
the "agent silently stops mid-task" failure mode users hit during agentic
loops.
1. Surface upstream errors instead of swallowing them
The SSE parsing loop wrapped every line in try { ... } catch {}, which
silently dropped the throw raised when Qoder returned an error envelope
(e.g. 406 Session blocked, provider_error). The stream then completed
with empty content and stopReason "stop", so pi saw a clean stop instead
of an error and never retried or informed the user.
The catch now only skips genuinely malformed SSE lines (SyntaxError) and
re-throws everything else so the outer handler can set stopReason "error"
+ errorMessage. A QODER_DEBUG env var logs skipped lines for diagnosis.
2. Stop overwriting the real finish_reason with "stop"
At end-of-stream the code unconditionally did:
output.stopReason = toolCalls.length ? "toolUse" : "stop"
clobbering any meaningful finish_reason ("length", "content_filter", ...)
captured earlier. Now only toolUse is forced when tool calls are present;
otherwise the upstream finish_reason is preserved (defaults to "stop").
The done event reason cast now includes "length".
3. Give each request a fresh server-side session id
session_id was stableHash("qoder-session", userID, qoderModel) —
constant across all requests for a given user+model. Qoder enforces a
per-session lock and replies 406 Session blocked when a second request
arrives before the previous one fully drains server-side, which happens
routinely in tight agentic tool loops. Appending crypto.randomUUID()
makes every request use a fresh session. We send full conversation
history on each request, so losing server-side session continuity is
safe.
4. Capture upstream usage, responseId and responseModel
The provider reported all-zero usage and no responseId/responseModel.
Now the finish chunk usage (prompt/completion/total tokens, cache
read/write) is mapped into output.usage, and id/model into
responseId/responseModel, so pi sees real token accounting and can
correlate responses.
Add stream.test.ts with regression coverage for the 406 path, the
finish_reason preservation, the toolUse stop reason, and usage/response
metadata capture.
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.
Qoder PATs (pt-...) cannot authenticate directly; they must be exchanged
for a short-lived job token (jt-...), mirroring the official qodercli flow.
- pat.ts: exchangeJobToken + credentialsFromPat (exchange + identity)
- oauth: fix QODER_PERSONAL_ACCESS_TOKEN env path; re-exchange PAT on refresh
- login: add 'Use API Key (PAT)' option to /login menu and terminal fallback
- README: correct PAT docs (exchange required, two entry points)