Commit Graph
30 Commits
Author SHA1 Message Date
Cursor AgentandSimon Shi 11249d12c4 Format reorganized provider modules
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 17:11:43 +00:00
Cursor AgentandSimon Shi 9d88e995ee Refactor provider around fixed regions and friendly model ids
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 17:11:18 +00:00
Cursor AgentandSimon Shi 63c517002e Fix provider binding test mocks
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:53:00 +00:00
Cursor AgentandSimon Shi 66f950f9c5 Bind Qoder providers to fixed regions
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:51:59 +00:00
Cursor AgentandSimon Shi 72a81d672f test: cover live chat endpoint classification
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:33:24 +00:00
Cursor AgentandSimon Shi 476a466076 style: format stream adapter test
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:32:09 +00:00
Cursor AgentandSimon Shi 9dc71be404 fix: correct friendly model table typing
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:30:15 +00:00
Cursor AgentandSimon Shi aaac381d76 refactor: use friendly model ids globally
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:29:52 +00:00
Cursor AgentandSimon Shi ca17a95270 test: correct fixture and abort harness
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:28:09 +00:00
Cursor AgentandSimon Shi 3d7df5d1e4 test: cover provider adapter scenarios
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:27:32 +00:00
Cursor AgentandSimon Shi 6d5cdfd171 test: add redacted Qoder protocol recorder and replays
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:14:37 +00:00
Cursor AgentandSimon Shi 1eee02455c fix: bump COSY client identity to qodercli 1.1.38
Older Cosy-Version / cosyVersion values cause the model catalog and
gateway to treat the client as a stale qodercli and return a reduced
list. Align QoderIDEVersion with current CLI 1.1.38.

Leave PAT exchange and userinfo Cosy-Version at 1.0.1: those openapi
endpoints do not use a COSY signature, and I could not verify that
official qodercli 1.1.38 changed them.

Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 16:07:41 +00:00
Cursor AgentandSimon Shi 52f7752717 test: guard the published pi.extensions entry (#18, #21)
The npm tarball ships only dist/ and README.md, so the pi.extensions
"./src/index.ts" that shipped in 0.3.0 left Pi listing the package as
installed while never loading the provider. main already points at
./dist/index.js (6d9635e), but nothing stopped it regressing again -- it
had already flipped once in 1b110a6.

Assert the entry is ./dist/index.js and, more generally, that every
pi.extensions path falls inside the published files set.

Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 15:57:56 +00:00
Cursor AgentandSimon Shi 1e44cde685 fix(models): use the 1M context window Qoder actually supports (#13)
auto, efficient and lite carried a 180K contextWindow, so pi showed >100%
context usage and auto-compacted early. Live testing in #13 drove global
lite to 1,000K tokens successfully, so the 180K value was a stale
max_input_tokens floor rather than a real limit.

Introduce DEFAULT_CONTEXT_WINDOW (1M) and use it for the global static
models, including gm51model. Apply it as the catalog fallback too, but only
when /model/list advertises no context_config: models that do advertise a
smaller window (kmodel at 256K, CN GLM-5.2 and MiniMax at 200K) keep it
rather than being inflated to 1M. CN Auto stays at 200K since only global
lite was verified at 1M.

Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 15:57:49 +00:00
Cursor AgentandSimon Shi 1dd1b0988b test: isolate suites from the real ~/.pi and from each other
Several suites read and write ~/.pi/agent/{auth,qoder-models-cache}.json.
Vitest runs files in parallel, so one file could truncate a file another
was reading, and a run also clobbered the developer's own ~/.pi state.
Give each test file its own HOME.

Isolation exposed a latent coupling in the stream suite: with no resolved
identity on disk, streamQoder fetches /userinfo first and consumes the
single mocked Response, so the chat read failed on a locked stream. The
suite only passed because another file left credentials behind. Stub the
identity so the mock only ever serves the chat request.

Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
2026-08-31 15:57:41 +00:00
Simon ShiandGitHub 3e66ceb1a8 Merge pull request #20 from snufkintomo/fix/done-sentinel-hang-and-orphaned-tool-results
fix: end the stream at [DONE] and drop orphaned tool results
2026-08-31 23:42:52 +08:00
Loongphy b462207447 feat(cn): expose upstream display_name as model id, drop key mapping tables
The CN model picker used to translate Qoder's internal keys (qfmodel,
qmodel_latest, ...) into hardcoded "friendly" ids (qwen3.6-flash, ...) via a
manual mapping table, with the display label hardcoded alongside. Both
sides rotted independently of upstream: when Qoder upgraded qfmodel to
Qwen3.8-Flash the label stayed "Qwen 3.6 Flash", and new keys (qmodel_38max,
q37fmodel) had no entry so their raw key leaked into the picker as the id.

The catalog API already returns every key with its display_name, so the
mapping table duplicates data the server provides and can only ever be stale.

Rework: use the upstream display_name (whitespace-stripped) directly as the
pi-visible Model.id, and read the request-time upstream key back from the
cached model config at send time. No key<->friendlyId table is needed.

- cosy.ts: remove qoderCNFriendlyModels, getQoderCNDirectModel,
  getQoderCNFriendlyModelInfo, toQoderCNFriendlyModel, prettifyQoderCNModelName.
  Add toQoderCNModelId(display) = display with whitespace stripped.
- models.ts: updateQoderModelsCache now sets CN model id =
  toQoderCNModelId(display_name); the original key is still stored in configs
  (keyed by both key and id) and read back via getCachedModelConfig. Simplify
  the CN fallback in getCachedModelConfig (drop the stale reasoning set).
  staticCnModels ids/names rewritten to the display_name form.
- stream.ts: the upstream key comes from modelConfig.key (the cached entry)
  instead of a reverse-lookup table; falls back to model.id for the
  international site, which already uses the key as id.
- index.ts: modelsForProvider no longer re-maps CN models through a friendly
  transform — the cached/static ids are already the display name.

Trade-off: a renamed/upgraded model changes its id, so a previously saved
default model may no longer resolve after an upstream rename and the user
reselects. This is expected: users should learn about new models. This is
accepted.

Tests updated for the new contract; e2e against the live CN catalog confirms
id=Qwen3.8-Flash resolves to send key=qfmodel with no mapping table.
2026-08-28 08:16:36 +08:00
snufkintomo e4c55f4871 fix: end the stream at [DONE] and drop orphaned tool results
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.
2026-08-21 19:12:00 +08:00
Oyster Lee b4416909f5 fix: emit a tool call that arrives with no arguments
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.
2026-08-03 06:50:04 +08:00
Oyster Lee a9a3521b9b fix: forward images returned by tool calls to the model
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.
2026-08-03 06:20:10 +08:00
liyanan 37ce615e62 test(stream): assert usage input excludes cached/written tokens
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.
2026-07-30 12:42:27 +08:00
liyanan 47141df66e fix: strip thinking tag artifacts leaked into reasoning_content
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.
2026-07-28 10:21:28 +08:00
liyanan 273368fc0e fix: prevent provider_error 400 on dmodel/ultimate by using content placeholder
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.
2026-07-26 15:26:13 +08:00
liyanan b8980b68b8 fix(stream): stop silently swallowing upstream errors and session-lock stalls
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.
2026-07-25 20:55:40 +08:00
Simon Shi 077e651fc7 fix: expose full Qoder model catalog 2026-07-22 20:06:05 +09:00
Simon Shi ef672a5461 fix: refresh Qoder models before startup 2026-07-22 19:54:46 +09:00
Simon Shi 55e87cbdef fix: stop adding auto as Qoder model fallback 2026-07-22 19:42:46 +09:00
Simon Shi 1c44a09141 fix: safely handle missing AuthStorage export and bump version to 0.2.6 2026-07-22 13:03:48 +08:00
Simon Shi 049b164279 fix: resolve biome lint errors in test files
Replace all explicit any types with proper type annotations and fix
formatting issues to pass CI lint checks.
2026-06-27 20:27:29 +08:00
Simon Shi c83fa0b8f3 test: add unit tests and remove dead code
- Add vitest with 98 tests across 6 test files covering:
  - cosy.ts: URL builders, mode detection, model name mapping
  - thinking-parser.ts: ThinkingTagParser with all tag variants and chunked streaming
  - transform.ts: getContentText, transformTools, transformMessagesForQoder
  - pat.ts: isPatRefresh, encodePatRefresh/decodePatRefresh roundtrip
  - qoder-encoding.ts: custom encoding correctness and edge cases
  - models.ts: static model definitions integrity checks

- Remove dead code:
  - QODER_CN_DIRECT_BASE_URL (unused export in cosy.ts)
  - THINKING_START_TAG / THINKING_END_TAG (unused constants in thinking-parser.ts)

- Add test/test:watch scripts to package.json
2026-06-27 20:03:05 +08:00