opencode-visual-cache icon indicating copy to clipboard operation
opencode-visual-cache copied to clipboard

QwenCloud Token Plan quota: is a qwencloud-CLI subprocess adapter acceptable, or too much of a workaround?

Open aiev opened this issue 1 month ago • 1 comments

Context

QwenCloud Token Plan users get Balance query unsupported in the sidebar, because the balance adapters in src/balance-providers.ts all speak "monetary balance with an API key", while Token Plan bills in Credits from a subscription quota.

Worse, the quota cannot be queried with the key at all: the Token Plan dedicated key (sk-sp-*) is inference-only. Every billing route answers ConsoleNeedLogin, and the endpoint only routes /compatible-mode/v1/*, /apps/anthropic/v1/messages and /api/v1/services/aigc/*. Confirmed in the QwenCloud Token Plan FAQ and independently in steipete/CodexBar#2328.

So there are only two ways to read the number: the console gateway (browser cookie + sec_token, undocumented) or the official @qwencloud/qwencloud-cli (device-flow login).

What my fork does

Branch: aiev/opencode-visual-cache → feat/qwencloud-token-plan-quota (commits c90258d, 2a9594d)

  • New balance provider qwencloud that runs qwencloud usage summary --format json as a subprocess and parses token_plan (remainingCredits / totalCredits / usedPct / resetDate) into the existing BalanceEntry shape, rendered as Token Plan 72% with an expandable detail row.
  • BalanceProvider gains two optional fields: aliases (so OpenCode providerID alibaba-token-plan / bailian-token-plan* resolve to this adapter) and requiresKey: false (poll without an API key; the credential lives in the CLI login).
  • node:child_process is resolved lazily through process.getBuiltinModule, so hosts without it degrade to an error message instead of a crash. 8s timeout, inside the existing 10s poll budget.
  • Distinct error codes and copy in all four locales: CLI (not installed), AUTH (needs qwencloud auth login), NOPLAN (the CLI account has no Token Plan), and EMPTY (response missing fields).
  • /cache-balance-key is a no-op for this provider (a pasted key would be useless) and the provider menu marks it as (qwencloud CLI).

No new runtime dependency; the plugin only requires the CLI to be present on PATH.

Why I consider it a gambiarra

  1. A TUI panel spawning a global CLI is an unusual dependency shape — it silently depends on the user installing and logging into a second tool that OpenCode knows nothing about.
  2. The CLI's own quota path is currently broken for a large slice of users: for accounts in the gray cohort, fetchTokenPlan() in QwenCloud/qwencloud-cli src/services/tokenplan-service.ts calls GetSeatSubscriptionSummary with productCode: codes.teams only, and an Individual plan never appears there — so usage summary reports subscribed: false, totalCredits: 0 for an active Individual Pro subscription (I verified this against a real account that the console shows as Individual Plan · Pro · Active · Monthly Quota 180,000 · Remaining 99.6%). My fork shows the honest NOPLAN message in that case, but the number is still missing until the CLI is fixed.
  3. Polling spawns a process every 5 minutes per running TUI.

Alternatives I rejected

  • Console gateway with a pasted cookie (cs-data.qwencloud.com/data/api.json, zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/*): works today, but it is undocumented, breaks whenever the console changes, and would store a full session credential in the plugin KV (which is already plaintext for other providers — a session cookie is strictly worse than an API key).
  • Nothing at all: hide the balance row for Token Plan. Simple, but leaves the user without any quota signal in the tool where they burn it.

Question for the maintainers

Would you accept this as-is behind an opt-in flag, accept it only as a separate "quota providers" module (so balance-providers.ts stays HTTP-only), or do you prefer not to ship any CLI-subprocess dependency at all? If the last one, I am happy to keep it in my fork and just fix the unsupported copy so it says "quota-based plan, see console" instead of a generic unsupported message.

Happy to open a PR once you tell me which shape you want.

aiev avatar Oct 09 '26 22:10 aiev

Update: the console-cookie source now works and is implemented — the CLI source does not

I went down both paths since filing this. Result, with evidence from a real account (Individual / Pro, ap-southeast-1):

The CLI adapter is useless for Individual plans. usage summary returns token_plan.subscribed = false, totalCredits = 0 for an account the console shows as Individual Plan · Pro · Active · Monthly quota 180,000 · Remaining 99.6%. I patched the CLI locally (gray-cohort fallback to the legacy DescribeFrInstances path) and ran the patched build against the same account: still false. Direct probes show why no client-side fix exists — GetSeatSubscriptionSummary returns {"Code":"Success","Data":{}} for the teams and personal product codes, DescribeFrInstances returns TotalCount: 0, GetSubscriptionDetail errors out, and the apikeyMgr./tokenplan/personal/* APIs only answer when they are wrapped in a console session. Tracked upstream as QwenCloud/qwencloud-cli#13. So the NOPLAN message my fork shows is accurate but the number never arrives.

The console gateway does return it, and that is now implemented in the same branch (14a440f):

call returns
GET home.qwencloud.com/tool/user/info.json data.secToken (CSRF)
POST cs-data.qwencloud.com/data/api.json → tokenplan/personal/api/v2/usage per1MonthPercentage: 0.015465678, per1MonthResetTime: 1794326400000
→ …/subscription specCode: "pro", status: "VALID", remainingDays: 31, endTime
→ …/quota-config pro: { five_hour: 12000, monthly: 180000 }, plus lite/essential/standard/addon

Rendered as Token Plan 98.5% with details Plan: PRO, Credits: 177216 / 180000, Used: 1.5%, Remaining: 98.5%, Reset: 31 days. Multi-window accounts (5h + week + month) emit one used/remaining/reset row per window, Credits taken from the widest ceiling.

Shape of the change:

  • BalanceProvider gains credentialSlot / optionalKeyPrompt / keySourceLabel, so an adapter can take an optional credential in its own KV slot (cache_panel.balance.qwencloud.cookie) instead of the API-key slot.
  • Keyless providers no longer auto-reuse an OpenCode API key in pollBalance — that key cannot query billing anyway, and reusing it would have shipped an sk-* string to the console gateway as if it were a cookie. The pasted value is also shape-checked (looksLikeQwenCookie) before any request.
  • Console source is used when a cookie is configured; otherwise the CLI subprocess fallback from the first commit still runs. New COOKIE error code (expired session) in all four locales; the menu marks the provider (qwencloud CLI) → (console cookie).
  • parseQwenConsoleQuota is pure and tested against the captured payloads (tests/qwen-token-plan.test.ts, +106 lines). typecheck / build / tests all green.

What I need from you before this can be a PR

The remaining objection is mine, not yours: storing a browser session cookie in the plugin KV is worse than storing an API key. It is broader than this one feature (full account access, login_qwencloud_ticket is httpOnly so it can only be obtained from DevTools), it expires on the console's schedule, and the endpoint is undocumented so it can break without notice. Mitigations already in the commit: the value is only sent to home.qwencloud.com / cs-data.qwencloud.com, it is masked in the UI like any other key, never logged, and removing it falls back to the CLI.

So, concretely — which of these would you take?

  1. As-is: cookie-capable provider, documented in README, opt-in by construction (nothing happens until the user pastes a cookie).
  2. Hard-gated: the console path only compiles/loads behind an explicit setting (e.g. cache_qwen_console=1), so the default build never ships the code path.
  3. Only the honest failure: drop both adapters, keep aliases so Token Plan stops saying Balance query unsupported and instead says "quota-based plan, see the console", and let #13 in the CLI repo decide when the number becomes queryable with the device-flow token.

I would take 1, ship 2 if you prefer, and I would not be sad with 3. Fork branch is aiev/opencode-visual-cache → feat/qwencloud-token-plan-quota (c90258d, 2a9594d, 14a440f) and it is what I run daily.

aiev avatar Oct 09 '26 23:10 aiev