Commit Graph
20 Commits
Author SHA1 Message Date
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 85ec02ca8a fix: reuse pi session id as qoder session_id to enable prompt caching
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.
2026-07-28 10:21:20 +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 6964615056 fix: inject system prompt as role:system message (server ignores top-level system field)
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.
2026-07-26 07:16:40 +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 43448f0cc0 feat: default to maximum context size 2026-07-20 20:55:09 +08:00
Simon Shi a98b9573f4 feat: auto-login from Qoder API key 2026-07-20 18:22:35 +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
温习江湖andGitHub 2b2f427628 Support Qoder CN (#4)
* feat(qoder-cn): add region endpoints and model catalog

* feat(qoder-cn): add separate provider auth and streaming

* docs: document Qoder CN provider
2026-06-26 14:42:36 +08:00
Simon Shi 26be3e0e17 refactor: optimize model cache updates by moving to session_start hook & bump to 0.2.1 2026-06-16 13:39:24 +08:00
Simon Shi bcaa66091e fix: route PAT login through pi LoginDialog callbacks
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.
2026-06-16 13:39:16 +08:00
Simon Shi 842bec6eb2 feat: proper PAT support via job-token exchange
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)
2026-06-16 13:39:13 +08:00
Simon Shi 032cf51904 refactor: drop redundant credentials cache and unused deps
- stream: passively rebuild stale/missing models cache on request
- oauth: read identity from pi's auth.json instead of self-managed cache
- deps: remove unused js-tiktoken; move esbuild to devDependencies
2026-06-16 13:38:55 +08:00
Simon Shi c9413961d2 fix: compute max contextWindow dynamically from context_config 2026-06-16 13:38:52 +08:00
Simon Shi dd8c313984 feat: init pi-provider-qoder plugin with oauth, signature and WAF bypass 2026-06-16 13:38:48 +08:00