Files
plugins/packages/nous/README.md
T
Jeff Li 08ff18a438 nous: Nous Portal models with Nous Portal's device sign-in (#163, laojianzi)
New package @magpie-community/opencode-nous-auth (provider nous-portal): Nous Portal models, signed in with Nous Portal's own OAuth device flow (public hermes-cli client, inference:invoke scope). The plugin keeps its own sign-in (never reads ~/.hermes), renews the single-use refresh token through x-nous-refresh-token, and sends requests to the Nous inference API with the JWT, retrying once after a 401.

Review fixes in d2f8fa0 (from the review at e8831a8):
- A refresh refused with 401/403 and no OAuth error code (HTML or empty body from an edge or proxy) keeps the sign-in and retries after 30s. Only invalid_grant, invalid_token or refresh_token_reused ask for a new sign-in.
- registry.json and README describe the plugin as Nous Portal models with Nous Portal's device sign-in; plan sharing is one line pointing to Nous's docs.
- content-encoding and content-length are dropped from the decoded response, like grok and cline.
- A token reply with neither expires_in nor JWT exp is renewed after one hour, not every 30s. The fallback never extends an explicit expires_in or exp (the earlier bound wins).

Run at d2f8fa0 merged with origin/main 8e479f1 (clean merge), sandbox HOME/TMPDIR:
- bun test (whole repo): 2305 pass, 0 fail, 194 files.
- node --test packages/nous/index.test.mjs: 55 pass, 0 fail.
- bun scripts/check.mjs: ok, nous listed.
- Probe through createAuth with a stubbed request: 403 HTML, empty 401, empty 403 -> oauth_request_failed, kept (e8831a8: invalid_grant, expired); 400 invalid_grant, 401 invalid_token, 400 refresh_token_reused -> expired; 401 invalid_client -> kept. Expiry: none -> +3600s; expires_in 900 -> +900s; exp 600 -> +600s; expires_in 300 -> +300s; expires_in 0, exp in the past, or expires_in 7200 with a past exp -> 1.
- Broken by hand: restoring the old 401/403 -> invalid_grant mapping turns 3 tests red; fallback back to 1 turns 3 red; letting the fallback outrank explicit bounds turns 4 red; keeping content-encoding/content-length turns 2 red.

Not tried: a real Nous Portal sign-in or request.
Follow-up: #328 (null or string expires_in beside a valid JWT exp gives expires=1).
2026-10-23 12:16:41 +08:00

148 lines
6.7 KiB
Markdown

# @magpie-community/opencode-nous-auth
Signs in to [Nous Portal](https://portal.nousresearch.com) with its OAuth
Device Authorization flow and sends model requests in
[magpie](https://usemagpie.ai) and [OpenCode](https://opencode.ai). Provider id:
`nous-portal`. This is separate from magpie's `nous` API-key preset.
## Signing in
Once the package is available from magpie's plugin registry:
```sh
magpie plugin add @magpie-community/opencode-nous-auth
magpie plugin login nous-portal
```
Magpie displays a verification URL and a code. Open the page, sign in and
approve the code there; finish any CAPTCHA in your browser. The plugin polls
for approval and completes sign-in without a local callback server.
Hermes Agent does not need to be installed or running.
In OpenCode, add `"@magpie-community/opencode-nous-auth"` to the `plugin`
array in `opencode.json`, then run `opencode auth login`.
The flow uses Nous Portal's `/api/oauth/device/code` and `/api/oauth/token`
with public client `hermes-cli`, scope `inference:invoke`, and no client
secret. Device polling follows [RFC 8628](https://www.rfc-editor.org/rfc/rfc8628.html#section-3.5),
including the server's interval and `slow_down` responses. Nous has not
confirmed whether independent clients may use the public `hermes-cli`
OAuth client ID.
For ChatGPT plan sharing, see [Nous Portal's documentation](https://portal.nousresearch.com/help/chatgpt-plan-sharing).
## Where the sign-in is kept
Magpie keeps the account in its plugin credential store; OpenCode uses its own
sign-in store. The saved OAuth fields are `access` (inference JWT), `expires`
(epoch milliseconds) and `refresh` (versioned JSON containing the refresh
token, scope, token type and inference endpoint). **The entire `refresh` value
is secret**, even though it looks like JSON.
Expiry uses the earlier of the token response's lifetime and the JWT's `exp`.
If both are absent, renewal is scheduled one hour after the exchange, with
the same two-minute lead. This is a local fallback, not a Nous lifetime
guarantee; a 401 can still trigger renewal sooner. Explicitly expired or
malformed lifetimes are not extended.
Nothing is read from or written to `~/.hermes/auth.json`. A separate device
sign-in is intentional: Nous refresh tokens are single-use, and copying one
from Hermes could invalidate Hermes's session when either program renews it.
Magpie uses `auth.refresh` with a two-minute lead and saves the rotated
tokens. OpenCode does not call that hook, so the plugin also checks before
each request and persists renewals through `client.auth.set`. Concurrent
refreshes within one plugin instance share one exchange. A rotated pair is
kept in memory before a save is attempted, so a failed save does not cause
that instance to reuse the old refresh token.
**Do not run multiple hosts against the same saved account.** The per-request
save is not an atomic cross-process operation; a crash during an unsaved
rotation may require signing in again. Only explicit `invalid_grant`,
`invalid_token` or `refresh_token_reused` errors mark the grant expired.
Other refresh refusals, including HTML or empty 401/403 responses, keep the
credentials and allow a later retry.
## Requests
Models use OpenAI Chat Completions (`@ai-sdk/openai-compatible`) at
`https://inference-api.nousresearch.com/v1`. Magpie translates requests from
other agent protocols. Every request uses the current inference JWT as a
Bearer token; the refresh token is sent only to Portal's token endpoint in
`x-nous-refresh-token`, never to inference.
The plugin accepts only known inference paths (`/models`, `/chat/completions`,
`/completions`, `/embeddings`) at the production Nous inference or welcome
host. A Portal response cannot redirect Bearer tokens to an arbitrary host;
HTTP redirects are refused. Caller-supplied credentials and magpie-internal
headers are removed before forwarding.
A 401 triggers at most one refresh and retry. Other upstream errors keep
their status, and streaming responses pass through without buffering or
rewriting their content. The upstream `content-encoding` and `content-length`
headers are removed because fetch may already have decoded the body.
## Models
After sign-in, the account's live `/v1/models` list replaces the single
bootstrap entry (`Hermes-4-70B`). Model ids are preserved, including the
vendor prefix such as `openai/gpt-6-luna`. In magpie an exposed model is
addressed as `nous-portal/openai/gpt-6-luna`.
Magpie may initially expose only part of the catalog. To choose a model
listed for your account and test it:
```sh
magpie provider refresh nous-portal
magpie provider models nous-portal +openai/gpt-6-luna
magpie provider test nous-portal openai/gpt-6-luna
```
The `provider test` command takes the upstream id (`openai/gpt-6-luna`), not
the magpie-prefixed id. Model availability and billing depend on the account;
a model appearing in `/models` does not guarantee access. The plugin does
not guess model prices, context limits or vision/reasoning capabilities when
the catalog does not provide them. A failed catalog read does not become an
empty entitlement list.
## Not included
- Nous Portal API keys (`nous` remains magpie's separate built-in preset).
- Importing Hermes credentials or running Hermes's subscription proxy.
- Direct ChatGPT/Codex OAuth, billing or quota reporting, or daily check-in.
- An arbitrary inference URL override or cross-process token coordination.
## Test
Run the offline checks from the repository root with a temporary HOME:
```sh
h=$(mktemp -d)
HOME="$h" node --test packages/nous/index.test.mjs
HOME="$h" bun test
rm -rf "$h"
bun scripts/check.mjs
```
Before publication, try the local package with magpie's isolated test script:
```sh
export MAGPIE=/absolute/path/to/magpie
sh scripts/try.sh nous
sh scripts/try.sh nous login nous-portal
sh scripts/try.sh nous provider refresh nous-portal
sh scripts/try.sh nous provider test nous-portal openai/gpt-6-luna
```
`try.sh` uses `.sandbox/nous` rather than your real magpie sign-ins. It clears
ordinary environment variables, including proxy settings; configure a proxy
inside the sandbox if needed. Do not use a portable Windows magpie binary
that ignores HOME isolation. Sign-in and `provider test` contact Nous Portal;
the latter may consume account credits. Test a model your account can use,
and never share the sandbox's credential files.
Protocol reference: Hermes Agent's
[device flow](https://github.com/NousResearch/hermes-agent/blob/8b9ac5773ea07189f23aa9dfcdba1e59fd759c21/hermes_cli/auth_device_flow.py),
[refresh handling](https://github.com/NousResearch/hermes-agent/blob/8b9ac5773ea07189f23aa9dfcdba1e59fd759c21/hermes_cli/auth_nous.py)
and [Nous Portal proxy adapter](https://github.com/NousResearch/hermes-agent/blob/8b9ac5773ea07189f23aa9dfcdba1e59fd759c21/hermes_cli/proxy/adapters/nous_portal.py).