Files
plugins/packages/cursor/README.md
T
yetone 288c6bc9c9 cursor 0.1.10: far from Cursor a conversation's tool steps add up to what Cursor counted, cache included (yetone/magpie#1053, #33). The tool-step usage-event wait of 0.1.9 (first ask at 1.5 s, every 0.5 s, up to 6 s) never met an event on the reporter's network (Windows, mainland China: a GetFilteredUsageEvents round trip of ~4.2 s, the event 4–5 s after the Run closes), so tool steps were counted as their whole prompt estimated with no cache (fable 13/13 steps, 0% cached; opus 27%) where Cursor counted 95–99%, and every tool step waited the full 6 s. A tool step's Run is now owed by its conversation (only when a conversation_id names the session): the step counts every event of the conversation's owed Runs that shows within the same wait, its own or earlier ones, and says 0 used when none has; the conversation's next Runs collect the rest in the background from 5 s after the owed Run closed, and a step that ends the turn waits for whatever is still owed (in-flight collections, then a poll for the rest). Events are claimed once, by count, so the conversation's prompt, cache read, cache write and output add up to Cursor's dashboard, some of it a step late. After 3 steps in a row left something owed an account's tool steps stop waiting (asked again every 10th). A Run that fails after collecting gives it back; an account that can't read its events (4xx) and a Run with no session id are estimated as before. latency.test.mjs runs whole conversations through the plugin against a fake Cursor with the reporter's latency scaled 1/20 and the reporter's literal events (in 2 / cacheRead 63457 / cacheWrite 3081, in 2 / 66538 / 14756, in 4 / 67872 / 2392): on 0.1.9 the 3-step conversation sums to 137936 prompt / 131329 cached instead of 218104 / 197867, and skipped steps still wait the full window (292 ms > 150); both pass now, 20 of 20 runs, and stepusage + latency pass with --rerun-each 20. HOME=$(mktemp -d) bun test: 682 pass, 0 fail; check.mjs cursor passes. Not tried with a real Cursor account on a high-latency network.
2026-10-07 03:17:08 +08:00

103 lines
5.4 KiB
Markdown

# @magpie-community/opencode-cursor-auth
Signs in to a [Cursor](https://cursor.com) subscription (Pro, Pro+, Ultra,
Teams) and makes its requests on the API `cursor-agent` talks to, in
OpenCode and in magpie. Provider id: `cursor`.
## Signing in
An account is a Cursor access token. There are three ways to get one:
- **Cursor (browser)**: the sign-in `cursor-agent login` does. The page
`cursor.com/loginDeepControl` is opened with a PKCE challenge, and the
plugin polls `api2.cursor.sh/auth/poll` until the browser has signed in.
The account is named by its email (`DashboardService/GetMe`).
- **cursor-agent's sign-in**: takes the account `cursor-agent login`
signed in to. Its token is read on every request from where the CLI
keeps it: the macOS keychain (`cursor-access-token`), else its
`auth.json` (`~/.cursor/`, `$XDG_CONFIG_HOME/cursor/` or
`%APPDATA%\Cursor\`). The plugin never writes it; when it has less than
5 minutes left, `cursor-agent status` is run, which renews it.
- **Cursor API key**: a key from cursor.com/dashboard → Integrations. It is
exchanged for a token at `/auth/exchange_user_api_key`, as the CLI does
with `CURSOR_API_KEY`, and exchanged again when the token runs out.
Sign-ins are kept where OpenCode keeps them (`auth.json`; in magpie,
`plugin-auth.json`). A browser sign-in's token lasts about two months.
Cursor gives the CLI no way to renew it, so after that you sign in again.
## Requests
OpenCode speaks chat completions (`@ai-sdk/openai-compatible`); the
plugin's `fetch` answers them on Cursor's agent API:
- The agent API is the one `ServerConfigService/GetServerConfig` names for
the account (a team may be served in one region only), else
`agentn.global.api5.cursor.sh`. A refusal that names a region is tried
once more with the config asked again.
- Each request is one `agent.v1.AgentService/Run`, a Connect stream both
ways over HTTP/2, with the CLI's headers (`x-cursor-client-type: cli`,
its version, privacy mode on).
- The whole conversation goes each time, as AI SDK messages kept as blobs
the server asks for. The caller's tools are MCP tools, listed in the
system prompt; the model calls them through Cursor's `CallDynamicTool`,
and the calls come back as chat completion tool calls. Cursor's own tools
(shell, file reads, edits) are never run.
- A conversation's Runs share one `conversation_id`, so Cursor sends them
to the machine that has the prompt cached (Grok on Cursor caches by
machine). It is made from what names the session — the request's
`prompt_cache_key` (Codex's thread id), else the session the
`chat.headers` hook is told — and the conversation's first user message,
so subagents under one session are conversations of their own. With
nothing naming the session, each Run has a new one.
- Text, thinking (`reasoning_content`) and token usage come back streamed
or not. Cursor's `input_tokens` counts the cached prompt too: the cache
read and written is taken out of it, and given as `cached_tokens` and
`cache_write_tokens`, so it is counted once; its reasoning tokens are
`reasoning_tokens`. A step that calls tools gets no usage in its Run
(Cursor tells it only once the tools' results come back in the same
Run), so once the Run is closed its usage is read from the dashboard's
usage events, by the Run's conversation id: it shows in about 2.5 s,
and is looked for up to 6 s. Far from Cursor it shows later than that
(yetone/magpie#1053), so the step says 0 used and the conversation owes
it: its next steps collect the event in the background and count it
with their own, and a step that ends the turn waits for what is still
owed, so the conversation adds up to what Cursor counted. An account
whose events miss the wait 3 steps in a row stops waiting for them.
With nothing naming the session (so no later step to collect it), or
an account that can't read its usage events, the step is estimated as
before.
Failures keep their statuses: 401 to sign in again, 429 at the
usage limit, 400 for a prompt too long, 403 for a region refusal.
## Models
The `config` hook declares `auto` (Cursor's pick). Once signed in, the
`provider.models` hook lists the account's models
(`AgentService/GetUsableModels`, cached 10 minutes), by the ids
`cursor-agent models` shows:
- Cursor lists a model once per effort (`gpt-5.6-low`, `gpt-5.6-high`, …).
Each family is one model here, with the efforts as variants; the
request's `reasoning_effort` picks the id, fitted to the nearest one there
is. `-fast` and `-thinking` models stay models of their own; a
`service_tier` of `priority` picks the fast one.
- The context window is 1M for a model whose name says 1M, else 200K.
- Cursor's model picker (`AiService/AvailableModels`, as the CLI asks for
it) adds the models it offers that the usable list leaves out (GLM-5.3,
GLM-5.3 Flash), run with the picker variant's parameters; hidden and
Tab-only models stay out.
- A model Cursor serves only in Max Mode (the usable list's `maxMode`, a
picker model with no other mode, or a Max Mode variant) is asked for in
Max Mode, as the CLI turns it on for one. A model neither says it of is
asked again in Max Mode when Cursor answers "Max Mode Required", and so
from then on.
## Not included
- Usage and plan limits.
- Web search: Cursor's own web search is refused like its other tools.
- Switching between several accounts. OpenCode keeps one sign-in per
provider.
- The Responses and Messages APIs: only chat completions are answered.