Files
plugins/packages
unship a6f09a0e9a qoder: add PAT (Personal Access Token) sign-in method (#103)
* qoder: add PAT (Personal Access Token) sign-in method

Add a new auth method that exchanges a PAT for a job token via
/api/v1/jobToken/exchange, bypassing the OAuth device flow. PATs are
long-lived (up to 1 year), avoiding the ~3-day re-authentication cycle
of OAuth refresh tokens.

Changes:
- Add patSignIn function for PAT → job token exchange
- Add "Sign in with Qoder PAT" method with password prompt
- Handle PAT accounts in usage (no device token available)
- Add pat.test.mjs with 10 tests covering both sites
- Update vpc.test.mjs for new method count

* qoder: use text prompt type for PAT (magpie has no password type)

magpie's prompt system only supports "text" and "select" types.
Change the PAT prompt from "password" to "text" to match.

* qoder: use password prompt type for PAT input

Now that magpie supports "password" prompt type, use it for PAT input
so the token is masked when typed in both GUI and TUI.

* qoder: address PR review — PAT kept, fields read as Qoder's client does

1. Store the PAT on the credential; when the job refresh lapses
   (SignInGone from refreshJob), re-exchange the PAT instead of
   marking the account signed out. The PAT lasts a year, the refresh
   two days.

2. Read the exchange response as Qoder's client does:
   - token from token, device_token or access_token
   - refresh from refresh_token or refreshToken
   - expiry from expire_time or expires_at (date) first, then
     expires_in in seconds (not milliseconds)

3. Use the openapi() helper for the exchange (no custom User-Agent);
   400/401/403 say "refused the PAT" rather than passing through the
   raw body.

4. Try the usage endpoint with the job token for PAT accounts; fall
   back to the "PAT" plan label if it won't accept it.

5. Exclude the PAT method from Qoder CN: its CLI uses device chat
   directly and never makes a job token, so a PAT exchange may not
   produce one the chat endpoint accepts.

* qoder: fix expires_in unit — milliseconds, not seconds

The real exchange response shows expires_in: 86400000 (24h in ms),
not 86400 (seconds). The expires_at date field takes precedence in
exchangeExpiry, so the fallback was not hit in practice, but correct
it for completeness.

* qoder: drive the renew test through the real refresh path, usage through fetchUsage

The renew test called the PAT sign-in directly, so it never met the
fallback it was named for. It now loads a stored expired credential
whose job refresh is refused, and checks the exchange is asked with the
stored PAT, the request goes on with the new token, and the saved
credential still carries the PAT — and fails when the fallback is
taken out. A second case keeps no PAT on the credential and expects
the sign-in gone to come out.

The PAT branch of usage built fetchUsage's request by hand and turned
every error into a plan of its own; it calls fetchUsage now, its
errors surfacing as the device path's do. A live PAT account answered
the usage endpoint 200 with a qoder envelope on the job token from the
exchange, said so in the comment there.

* qoder: a refused PAT re-exchange ends the sign-in

The renew fallback let a refused PAT through as a plain error, which
magpie read as a server fault and kept the account on. A PAT lapses
like a device sign-in does — expired, or revoked — so renew turns the
exchange's refusal into a lapsed sign-in: usage says "expired" and the
request answers 401, asking for a new PAT. Interactive sign-in keeps
the plain error.
2026-10-14 18:34:37 +08:00
..
middleware: param-override, model-map, system-prompt, word-guard and think-tags — gateway middleware that magpie runs on every agent's requests and replies, mostly New API's channel settings with the same JSON (the owner) — magpie can now run a plugin as gateway middleware (onRequest, onEvent, onResponse, in-process on moejs), and these are the first ones: param-override is New API's param_override (both forms, operations with gjson paths and conditions, return_error); model-map is model_mapping (chains, /regex/ keys with $1) and puts the asked-for name back into replies; system-prompt adds a prompt in each API's own place, keeping an agent's own unless override (New API's semantics), by agent or model; word-guard is the sensitive-word filter (reject or mask the user's text, optionally replies); think-tags strips a leading <think>…</think> from replies across stream chunks, or puts Chat reasoning_content into the text (thinking_to_content). Each is one file with no imports, named @magpie-community/middleware-<name>, with magpie.options as the example the app's editor starts from; registry.json lists them with "kind": "middleware" and no providers; check.mjs skips middleware-only packages. scripts/middleware.mjs calls the hooks as the gateway does (events split, [DONE] passed by) and runs each package's cases.json. Tested with a real DeepSeek account and real Claude Code through magpie's gateway: a mapped model named back in message_start, max_tokens capped, a system prompt appended for claude only, a word turned away as a 400 in Anthropic's shape, return_error as a 403 in Chat's, <think> stripped from Chat and Responses streams and an Anthropic whole reply, reasoning_content moved into the text. bun test (61 for these five) and bun scripts/check.mjs pass; packages/devin's two image tests fail on this machine only, as on main. Their first npm version has to be published by hand (trusted publishing can't create a package); until then magpie shows them as Coming soon.
2026-10-05 20:49:42 +08:00
middleware: param-override, model-map, system-prompt, word-guard and think-tags — gateway middleware that magpie runs on every agent's requests and replies, mostly New API's channel settings with the same JSON (the owner) — magpie can now run a plugin as gateway middleware (onRequest, onEvent, onResponse, in-process on moejs), and these are the first ones: param-override is New API's param_override (both forms, operations with gjson paths and conditions, return_error); model-map is model_mapping (chains, /regex/ keys with $1) and puts the asked-for name back into replies; system-prompt adds a prompt in each API's own place, keeping an agent's own unless override (New API's semantics), by agent or model; word-guard is the sensitive-word filter (reject or mask the user's text, optionally replies); think-tags strips a leading <think>…</think> from replies across stream chunks, or puts Chat reasoning_content into the text (thinking_to_content). Each is one file with no imports, named @magpie-community/middleware-<name>, with magpie.options as the example the app's editor starts from; registry.json lists them with "kind": "middleware" and no providers; check.mjs skips middleware-only packages. scripts/middleware.mjs calls the hooks as the gateway does (events split, [DONE] passed by) and runs each package's cases.json. Tested with a real DeepSeek account and real Claude Code through magpie's gateway: a mapped model named back in message_start, max_tokens capped, a system prompt appended for claude only, a word turned away as a 400 in Anthropic's shape, return_error as a 403 in Chat's, <think> stripped from Chat and Responses streams and an Anthropic whole reply, reasoning_content moved into the text. bun test (61 for these five) and bun scripts/check.mjs pass; packages/devin's two image tests fail on this machine only, as on main. Their first npm version has to be published by hand (trusted publishing can't create a package); until then magpie shows them as Coming soon.
2026-10-05 20:49:42 +08:00
middleware: param-override, model-map, system-prompt, word-guard and think-tags — gateway middleware that magpie runs on every agent's requests and replies, mostly New API's channel settings with the same JSON (the owner) — magpie can now run a plugin as gateway middleware (onRequest, onEvent, onResponse, in-process on moejs), and these are the first ones: param-override is New API's param_override (both forms, operations with gjson paths and conditions, return_error); model-map is model_mapping (chains, /regex/ keys with $1) and puts the asked-for name back into replies; system-prompt adds a prompt in each API's own place, keeping an agent's own unless override (New API's semantics), by agent or model; word-guard is the sensitive-word filter (reject or mask the user's text, optionally replies); think-tags strips a leading <think>…</think> from replies across stream chunks, or puts Chat reasoning_content into the text (thinking_to_content). Each is one file with no imports, named @magpie-community/middleware-<name>, with magpie.options as the example the app's editor starts from; registry.json lists them with "kind": "middleware" and no providers; check.mjs skips middleware-only packages. scripts/middleware.mjs calls the hooks as the gateway does (events split, [DONE] passed by) and runs each package's cases.json. Tested with a real DeepSeek account and real Claude Code through magpie's gateway: a mapped model named back in message_start, max_tokens capped, a system prompt appended for claude only, a word turned away as a 400 in Anthropic's shape, return_error as a 403 in Chat's, <think> stripped from Chat and Responses streams and an Anthropic whole reply, reasoning_content moved into the text. bun test (61 for these five) and bun scripts/check.mjs pass; packages/devin's two image tests fail on this machine only, as on main. Their first npm version has to be published by hand (trusted publishing can't create a package); until then magpie shows them as Coming soon.
2026-10-05 20:49:42 +08:00