Files
plugins/packages/nous
yetone 17e5ef7457 all: load on OpenCode 2.x through a default {id, setup} (#657)
nbczw8750 (#657): trae 0.2.26 on OpenCode 2.2.5 fails with "Plugin
must export a default definition with an id and an effect or setup
function. (cause: SchemaError(Missing key at ["default"]))". 2.x
imports a package's exports["."] (a plugin dir and an npm install
alike) and has no V1 path at all, so every package here that serves
models failed the same way, not only trae.

Every one of the 32 model packages now has:
- opencode2.mjs, exports["."]: a copy of scripts/opencode2.mjs, an
  adapter that runs the package's V1 plugins inside 2.x's promise
  API. Config providers become 2.x providers (package pinned so the
  aisdk sdk hook runs), V1 auth methods become integration methods
  (oauth auto/code with forms; api without authorize as a key method),
  V1 credentials are kept in metadata.v1, the loader's baseURL,
  apiKey, headers and fetch are applied through the sdk hook, an
  auth.set renewal is stored per credential, and credential.switched
  /updated reload the account's own model list.
- opencode.mjs, main: re-exports the V1 plugin functions only.
  OpenCode 1.x and magpie's host.js take main. On 1.x, trae 0.2.26's
  main exported a non-plugin (_internal) and 1.19.5 refused it ("Plugin
  export is not a function"); the new main fixes that too.
- both files in "files"; patch version bumped. devin already had 2.x
  and is unchanged.

scripts/check.mjs fails a model package whose 2.x entry has no default
{id, setup}, whose opencode2.mjs differs from the canonical one, whose
entry is missing from files, or whose main exports a non-plugin.

Run at this head (mote):
- HOME=$(mktemp -d) bun test, as CI runs it: 4900 pass, 0 fail
  (4914 tests, 374 files). A first run set globalThis.fetch for the
  whole process from opencode2.test and failed 528 tests in other
  packages; the block is now set and put back around that file's tests.
- bun scripts/check.mjs: every package passes. With the network
  blocked cloudflare-workers-ai fails on its fetched catalog, the same
  on origin/main.
- Real OpenCode 2.2.5 (npm opencode-ai), sandbox HOME, packed trae
  0.2.27: loads with no error; /api/integration lists trae-cn "Trae CN"
  and trae-global "Trae Global" with their browser sign-ins; `auth
  login` starts the trae.cn authorization and waits (cancelled there);
  with a fake credential POSTed to /api/credential, `opencode models`
  lists trae-cn's six models.
- Real 2.2.5 with a fake V1 key plugin through the same adapter
  against a local fake server: `auth login` with a key, `models`,
  `opencode run` reached /real/v1/chat/completions with the loader's
  "Bearer loader-sk-fake" and header, and printed the fake's reply;
  with @ai-sdk/anthropic it reached /real/v1/messages with x-api-key
  from the loader. This run found the key method missing after 2.x's
  second integration transform (a once-only flag outside the
  transform); fixed, and opencode2.test checks a rebuild.
- Real OpenCode 1.19.5, sandbox HOME, trae 0.2.27: `opencode models`
  lists trae-cn and trae-global models; 0.2.26 failed there.

Broken by hand (scratch copy, bun test scripts/ and check.mjs):
- trae's exports back to "./index.mjs": the trae 2.x test fails
  (default.id undefined), check fails trae with "OpenCode 2.x:
  ./index.mjs has no default {id, setup} (plugins#657)".
- zed's opencode2.mjs changed: the copies test fails, check fails zed
  "differs from scripts/opencode2.mjs".
- the key flag back outside the transform: the 1.x plugin test fails
  on the rebuilt sign-ins (key missing).

Not tried: a real sign-in with a real account on 2.x; Windows (the
reporter's OS); chat.headers, chat.params, usage and tool hooks, which
the adapter doesn't carry to 2.x; the tools/pages packages (calc,
goanyapi, layerfs, search1api, aelios, agent-console, chat,
fingerprint, nowledge-mem, kimi-desktop), which still load only on 1.x.
2026-11-03 13:54:26 +08:00
..

@magpie-community/opencode-nous-auth

Signs in to Nous Portal with its OAuth Device Authorization flow and sends model requests in magpie and OpenCode. 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:

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, 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.

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:

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:

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:

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, refresh handling and Nous Portal proxy adapter.