/** * CodeBuddy (CodeBuddy / copilot.tencent.com) upstream client: chat streaming, * token refresh, model catalog, and credit balance. The wire behavior is * ported from Sliverkiss/codebuddy2api (MIT), whose Go implementation is * battle-tested against the real endpoint. * * @module dsh-codebuddy-cli/upstream */ import type { CodeBuddyCredential } from './auth.ts' import type { CodeBuddyClientIdentity } from './client-identity.ts' import type { CodeBuddyCheckInOutcome } from './status-paths.ts' import { clientIdentityHeaders, CODEBUDDY_IDE_NAME, CODEBUDDY_IDE_TYPE, CODEBUDDY_UNKNOWN_VERSION, resolveClientIdentity, } from './client-identity.ts' export type { CodeBuddyCheckInOutcome } from './status-paths.ts' /** CodeBuddy region selected by the credential's login domain. */ export type CodeBuddyRegion = 'cn' | 'global' /** Upstream failure classes the shim maps onto distinct HTTP answers. */ export type UpstreamErrorKind = | 'hard_credit' | 'soft_rate' | 'session_dead' | 'not_found' | 'server' | 'client' /** One CLI-usable model as the upstream catalog describes it. */ export interface CodeBuddyUpstreamModel { id: string name: string contextWindow: number maxTokens: number /** * Upstream-declared image input capability. Missing or false upstream data * resolves to false, so an unknown model stays text-only: over-claiming * admits an image the provider then rejects after the message is durable. */ supportsImages: boolean /** * Reasoning metadata the upstream catalog declares per model. The wire * effort values (`low`, `medium`, `high`, `xhigh`, `max`) map directly onto * pi-ai's thinking levels, and the supported set decides which levels the * DSH model selector offers. */ reasoning?: CodeBuddyModelReasoning /** * Billing convenience metadata: the credits multiplier string the upstream * reports (e.g. `"x0.00"` for free) and promotional badges like * `badge:限时免费:#FF0000` or `badge:夜间折扣:#1E90FF`. * * The multiplier reaches the browser through the host LLM seam, which has no * locale service, so {@link normalizeCredits} trims it to a * language-neutral display form (`x0.79`) that reads the same in every UI * language. The raw upstream string (which may spell `x0.79 credits`) stays * on {@link CodeBuddyModelBilling.credits} for diagnostics. */ billing?: CodeBuddyModelBilling } /** Reasoning metadata the upstream catalog declares for one model. */ export interface CodeBuddyModelReasoning { /** Whether the model does any reasoning at all (upstream `supportsReasoning`). */ supports: boolean /** Whether the model can only think (upstream `onlyReasoning`). */ onlyReasoning: boolean /** Selectable effort values; absent means the model has no explicit set. */ supportedEfforts?: readonly CodeBuddyEffort[] /** Default effort the upstream uses when none is chosen. */ defaultEffort?: CodeBuddyEffort /** Whether thinking can be switched off; false means it is always on. */ canDisableThinking: boolean } /** The concrete effort spellings CodeBuddy exposes on the wire. */ export type CodeBuddyEffort = 'low' | 'medium' | 'high' | 'xhigh' | 'max' /** Billing convenience metadata reported for one model. */ export interface CodeBuddyModelBilling { /** Credits multiplier, e.g. `"x0.00"` (free) or `"x0.79"`. */ credits?: string /** Promotional tags, e.g. `"限时免费"`, `"夜间折扣"`. */ badges?: readonly string[] /** Whether the model is currently free (`x0.00` credits). */ free: boolean } /** One billing package and its remaining credit (a full resource package). */ export interface CodeBuddyCreditAccount { /** Package code as the upstream reports it (may be absent). */ packageCode?: string packageName: string /** Total capacity of this package. */ total: number /** Remaining capacity of this package. */ remain: number /** Used capacity of this package. */ used: number /** Backward-compatible alias for the total (the older card's `size`). */ size: number /** Expiry as epoch milliseconds; absent means no known expiry (long-lived). */ expireAtMs?: number /** Whether the package has already expired. */ expired: boolean /** Whether the package expires within the soon window (7 days). */ expiringSoon: boolean } /** Aggregated credit answer for one credential. */ export interface CodeBuddyCredits { total: number accounts: readonly CodeBuddyCreditAccount[] } /** One official usage row (a single billing request). */ export interface CodeBuddyUsageRow { requestId: string model: string client: string credit: number /** Local display of the request time (upstream format preserved). */ requestTime: string /** Epoch milliseconds of the request, for sorting. */ ts: number /** Local date key `YYYY-MM-DD`. */ date: string } /** Daily usage aggregation, optionally broken down by model. */ export interface CodeBuddyUsageDaily { /** Local date key `YYYY-MM-DD`. */ date: string /** Total credits consumed that day. */ usage: number /** Per-model breakdown for that day, when the upstream row carries a model. */ models?: { model: string; requestCount: number; credit: number }[] } /** Aggregate usage stats for one credential over a 31-day window. */ export interface CodeBuddyUsageStats { /** `complete` when the window was fetched in full; `partial` / `unavailable` degrade. */ status: 'complete' | 'partial' | 'unavailable' /** Inclusive window start date `YYYY-MM-DD`. */ rangeStart: string /** Inclusive window end date `YYYY-MM-DD`. */ rangeEnd: string /** When the window was collected (epoch ms). */ collectedAt: number summary: { usageToday: number usage7Days: number usageThisMonth: number } /** One entry per day in the window (zero-filled). */ daily: readonly CodeBuddyUsageDaily[] /** Per-model aggregation across the whole window. */ models: readonly { model: string; requestCount: number; credit: number }[] /** Most recent requests, newest first. */ requests: readonly CodeBuddyUsageRow[] /** Max per-account detail rows kept in `requests`. */ detailLimit: number } /** Token refresh answer; fields the upstream omits stay absent. */ export interface CodeBuddyRefreshOutcome { accessToken: string refreshToken?: string expiresInSec?: number /** Refresh-token lifetime in seconds, when the upstream reports one. */ refreshExpiresInSec?: number domain?: string } /** Chat answer: either a live SSE response or a classified failure. */ export type CodeBuddyChatResult = | { ok: true; response: Response } | { ok: false; status: number; kind: UpstreamErrorKind; message: string } const CN_CHAT_BASE = 'https://copilot.tencent.com' const CN_BILLING_BASE = 'https://www.codebuddy.cn' const GLOBAL_BASE = 'https://www.workbuddy.ai' const JSON_TIMEOUT_MS = 30_000 const ERROR_BODY_LIMIT = 4096 /** * Client identity carried on every upstream request. * * The backend attributes a request to a client only when the `X-IDE-*` family * is present; without it the request shows up as an unattributed client. The * identity is cached after the first successful resolution because reading the * installed CLI's version touches the filesystem and the value is stable for * the lifetime of the process. */ let cachedIdentity: CodeBuddyClientIdentity | undefined /** * Identity resolution in flight, so concurrent first requests share one * filesystem probe instead of racing to read the same file. */ let pendingIdentity: Promise | undefined /** Identity used before (or instead of) a successful filesystem resolution. */ const FALLBACK_IDENTITY: CodeBuddyClientIdentity = { ideType: CODEBUDDY_IDE_TYPE, ideName: CODEBUDDY_IDE_NAME, ideVersion: CODEBUDDY_UNKNOWN_VERSION, productVersion: CODEBUDDY_UNKNOWN_VERSION, } /** * Resolve and cache the CLI client identity, tolerating any failure: identity * metadata must never be able to break a chat request. * * @param packageJsonPath - optional explicit CLI `package.json` path. * @returns the resolved identity. */ export async function ensureClientIdentity(packageJsonPath?: string): Promise { return await startClientIdentityResolution(packageJsonPath) } /** * Drop the cached identity. Exposed for tests, which must be able to exercise * the resolved and unresolved paths independently within one process. */ export function resetClientIdentity(): void { cachedIdentity = undefined pendingIdentity = undefined } /** * Identity for the next request, starting resolution if it has not run yet. * * Request headers are built synchronously while version resolution is async, * so the first request cannot wait for the real version. Rather than send a * versionless request and rely on the caller to have warmed the cache, this * kicks off resolution on first use: `startClientIdentityResolution` is called * from the client constructor, and the version headers appear as soon as the * probe settles. */ export function startClientIdentityResolution(packageJsonPath?: string): Promise { if (cachedIdentity !== undefined) return Promise.resolve(cachedIdentity) pendingIdentity ??= (async () => { try { cachedIdentity = await resolveClientIdentity(packageJsonPath) } catch { cachedIdentity = FALLBACK_IDENTITY } return cachedIdentity })() return pendingIdentity } /** Identity headers for the current request, resolving identity on first use. */ function identityHeaders(): Record { return clientIdentityHeaders(cachedIdentity ?? FALLBACK_IDENTITY) } /** Daily check-in endpoint on the CN side. */ const CN_CHECKIN_PATH = '/v2/billing/meter/daily-checkin' /** Upstream messages that mean "already checked in today". */ const CHECKIN_ALREADY_MARKERS: readonly string[] = [ 'already', 'checked in today', 'already signed', 'already checked', '已签到', '已签', '今日已', '签过', ] /** Insufficient-credit markers, ASCII lowercase plus the original Chinese. */ const HARD_CREDIT_MARKERS: readonly string[] = [ 'insufficient credit', 'no credit', 'credit exhausted', 'out of credit', 'quota exceeded', 'quota exhaust', 'payment required', 'credit not enough', 'not enough credit', '积分不足', '额度不足', '余额不足', '积分用完', '额度用尽', '没有积分', ] /** The concrete effort spellings CodeBuddy exposes on the wire. */ const EFFORT_VALUES: readonly CodeBuddyEffort[] = ['low', 'medium', 'high', 'xhigh', 'max'] /** Promotional badge keys the upstream tags carry, minus their color suffix. */ const BADGE_PREFIX = 'badge:' /** Parse the upstream `reasoning` object into {@link CodeBuddyModelReasoning}. */ function resolveUpstreamReasoning(wrapped: Record): { reasoning: CodeBuddyModelReasoning } { const supports = wrapped['supportsReasoning'] === true const onlyReasoning = wrapped['onlyReasoning'] === true const rawReasoning = wrapped['reasoning'] let supportedEfforts: CodeBuddyEffort[] | undefined let defaultEffort: CodeBuddyEffort | undefined let canDisableThinking = true if (typeof rawReasoning === 'object' && rawReasoning !== null && !Array.isArray(rawReasoning)) { const reasoning = rawReasoning as Record const rawEfforts = reasoning['supportedEfforts'] if (Array.isArray(rawEfforts)) { const efforts = rawEfforts.filter((value): value is CodeBuddyEffort => typeof value === 'string' && (EFFORT_VALUES as readonly string[]).includes(value)) if (efforts.length > 0) supportedEfforts = efforts } if (typeof reasoning['defaultEffort'] === 'string' && (EFFORT_VALUES as readonly string[]).includes(reasoning['defaultEffort'] as string)) { defaultEffort = reasoning['defaultEffort'] as CodeBuddyEffort } else if (typeof reasoning['effort'] === 'string' && (EFFORT_VALUES as readonly string[]).includes(reasoning['effort'] as string)) { defaultEffort = reasoning['effort'] as CodeBuddyEffort } // Only an explicit `canDisableThinking: true` offers "thinking off"; older // rows omit the field and several of them reject `off` on the wire, so the // conservative default is "cannot be disabled". canDisableThinking = reasoning['canDisableThinking'] === true } return { reasoning: { supports, onlyReasoning, ...supportedEfforts === undefined ? {} : { supportedEfforts }, ...defaultEffort === undefined ? {} : { defaultEffort }, canDisableThinking, }, } } /** * Reduce an upstream credits string to its language-neutral display form. * * The host LLM seam carries this text to the browser, and the host has no * locale service — whatever string is produced here is shown verbatim in every * UI language. The upstream is inconsistent in a way that matters: some catalog * rows report a bare multiplier (`x0.79`) and others append a unit word * (`x0.79 credits`), and the unit word would pin the display to English. * Dropping a trailing `credits` (case-insensitive, singular or plural) yields * the one spelling that reads identically in every language. * * @param credits - raw upstream credits string, e.g. `"x0.79 credits"`. * @returns the bare multiplier, or undefined when nothing displayable remains. */ export function normalizeCredits(credits: string | undefined): string | undefined { if (credits === undefined) return undefined const trimmed = credits.trim() if (trimmed === '') return undefined // A string that is only the unit word (`credits`) carries no multiplier. if (/^credits?$/iu.test(trimmed)) return undefined const bare = trimmed.replace(/\s+credits?$/iu, '').trim() return bare === '' ? undefined : bare } /** * Parse a usage row's request time into a local date key and epoch ms. * Accepts numeric epoch (seconds or ms) and common `YYYY-MM-DD HH:MM:SS` * spellings. Returns undefined when the value is unusable. */ function parseUsageTime(value: unknown, now: Date): { date: Date; ts: number } | undefined { if (typeof value === 'number' && Number.isFinite(value)) { const ms = value < 1e12 ? value * 1000 : value const date = new Date(ms) if (Number.isNaN(date.getTime())) return undefined return { date, ts: ms } } if (typeof value === 'string' && value.trim() !== '') { const text = value.trim() const numeric = Number(text) if (Number.isFinite(numeric)) { const ms = numeric < 1e12 ? numeric * 1000 : numeric const date = new Date(ms) if (Number.isNaN(date.getTime())) return undefined return { date, ts: ms } } // Accept `YYYY-MM-DD HH:MM:SS` and `YYYY-MM-DD` (both interpreted as local). const withTime = /^(\d{4})-(\d{2})-(\d{2})[T\s](\d{2}):(\d{2})(?::(\d{2}))?/u.exec(text) if (withTime !== null) { const date = new Date( Number(withTime[1]), Number(withTime[2]) - 1, Number(withTime[3]), Number(withTime[4]), Number(withTime[5]), Number(withTime[6] ?? 0), ) if (Number.isNaN(date.getTime())) return undefined return { date, ts: date.getTime() } } const dateOnly = /^(\d{4})-(\d{2})-(\d{2})$/u.exec(text) if (dateOnly !== null) { const date = new Date(Number(dateOnly[1]), Number(dateOnly[2]) - 1, Number(dateOnly[3]), 12, 0, 0) if (Number.isNaN(date.getTime())) return undefined return { date, ts: date.getTime() } } } void now return undefined } /** Whole days from `fromKey` to `toKey` (positive when to is later). */ function daysBetween(fromKey: string, toKey: string): number { const [fy, fm, fd] = fromKey.split('-').map(Number) const [ty, tm, td] = toKey.split('-').map(Number) const from = Date.UTC(fy!, (fm ?? 1) - 1, fd ?? 1) const to = Date.UTC(ty!, (tm ?? 1) - 1, td ?? 1) return Math.round((to - from) / 86_400_000) } /** Parse the upstream `tags` / `credits` fields into billing metadata. */ function resolveUpstreamBilling(wrapped: Record): { billing: CodeBuddyModelBilling } { const rawCredits = wrapped['credits'] const credits = typeof rawCredits === 'string' && rawCredits.trim() !== '' ? rawCredits.trim() : undefined const badges: string[] = [] const rawTags = wrapped['tags'] if (Array.isArray(rawTags)) { for (const tag of rawTags) { if (typeof tag !== 'string') continue const lowered = tag.toLowerCase() if (!lowered.startsWith(BADGE_PREFIX)) continue const label = tag.slice(BADGE_PREFIX.length).split(':')[0] ?? tag.slice(BADGE_PREFIX.length) if (label !== '') badges.push(label) } } // A `x0.00` multiplier means the model is currently free. const free = credits !== undefined && /^x?0\.0+$/u.test(credits) return { billing: { ...credits === undefined ? {} : { credits }, ...badges.length === 0 ? {} : { badges }, free, }, } } /** Session-invalidation markers that mean "sign in again in the CodeBuddy app". */ const SESSION_DEAD_MARKERS: readonly string[] = ['Offline user session not found', '12153'] /** Classify an upstream failure from its HTTP status and body excerpt. */ export function classifyUpstreamError(status: number, body: string): UpstreamErrorKind { if (status === 402) return 'hard_credit' const lower = body.toLowerCase() for (const marker of HARD_CREDIT_MARKERS) { if (lower.includes(marker.toLowerCase()) || body.includes(marker)) return 'hard_credit' } for (const marker of SESSION_DEAD_MARKERS) { if (body.includes(marker)) return 'session_dead' } if (status === 429) return 'soft_rate' if (status === 404) return 'not_found' if (status >= 500) return 'server' if (status >= 400) return 'client' return 'client' } /** Region for a login domain; an empty domain means CN (matching upstream tooling). */ export function regionOf(domain: string): CodeBuddyRegion { const lowered = domain.trim().toLowerCase() if (lowered === 'workbuddy.ai' || lowered.endsWith('.workbuddy.ai')) return 'global' return 'cn' } function chatBase(credential: CodeBuddyCredential): string { return regionOf(credential.domain) === 'global' ? GLOBAL_BASE : CN_CHAT_BASE } function billingBase(credential: CodeBuddyCredential): string { return regionOf(credential.domain) === 'global' ? GLOBAL_BASE : CN_BILLING_BASE } function originReferer(credential: CodeBuddyCredential): string { return regionOf(credential.domain) === 'global' ? GLOBAL_BASE : CN_BILLING_BASE } /** Headers every upstream request shares. */ function commonHeaders(credential: CodeBuddyCredential): Record { return { 'Accept': 'application/json, text/plain, */*', 'X-Requested-With': 'XMLHttpRequest', 'Origin': originReferer(credential), 'Referer': `${originReferer(credential)}/`, ...identityHeaders(), } } /** Chat request headers, including the X-No-* conventions the official CLI uses. */ function chatHeaders(credential: CodeBuddyCredential): Record { const headers: Record = { ...commonHeaders(credential), 'Content-Type': 'application/json', // 安全红线:chat 请求绝不携带 refresh token。 ...credential.uid === '' ? { 'X-No-User-Id': '1' } : { 'X-User-Id': credential.uid }, ...credential.enterpriseId === undefined || credential.enterpriseId === '' ? { 'X-No-Enterprise-Id': '1' } : { 'X-Enterprise-Id': credential.enterpriseId }, ...credential.domain === '' ? { 'X-No-Department-Info': '1' } : { 'X-Domain': credential.domain }, 'X-Product': 'SaaS', } return headers } /** Refresh-endpoint headers; X-Refresh-Token appears here and nowhere else. */ function refreshHeaders(credential: CodeBuddyCredential): Record { const headers: Record = { ...commonHeaders(credential), 'X-Refresh-Token': credential.refreshToken, 'X-Auth-Refresh-Source': 'workbuddy', } if (credential.enterpriseId !== undefined && credential.enterpriseId !== '') { headers['X-Enterprise-Id'] = credential.enterpriseId } return headers } /** Billing request headers. */ function billingHeaders(credential: CodeBuddyCredential): Record { const headers: Record = { 'Authorization': `Bearer ${credential.accessToken}`, 'Accept': 'application/json', 'Content-Type': 'application/json', ...identityHeaders(), } if (credential.uid !== '') headers['X-User-Id'] = credential.uid if (credential.enterpriseId !== undefined && credential.enterpriseId !== '') { headers['X-Enterprise-Id'] = credential.enterpriseId headers['X-Tenant-Id'] = credential.enterpriseId } if (credential.domain !== '') headers['X-Domain'] = credential.domain return headers } /** * Daily check-in request headers. The billing headers already carry the * authorization and identity fields; the check-in endpoint additionally wants * an explicit domain header, so an empty credential domain falls back to the * CN web domain rather than omitting the field. */ function checkInHeaders(credential: CodeBuddyCredential): Record { return { ...billingHeaders(credential), 'X-Domain': credential.domain.trim() === '' ? 'www.codebuddy.cn' : credential.domain, } } /** * Normalize an OpenAI chat-completions body for the CodeBuddy upstream: * force `stream: true` (the upstream rejects non-streaming), flatten * `tool_choice` (the upstream's field is a string; object forms return 400), * and rewrite `developer` messages as `system`. * * The `developer` rewrite is load-bearing: pi-ai emits the system prompt as * `role: "developer"` (the OpenAI convention it adopted), but the CodeBuddy * upstream rejects that role with HTTP 400 code 11128 ("Illegal API * invocation from an unapproved channel"). Rewriting to `system` is the * compatible spelling the upstream accepts. */ export function prepareChatBody(source: string): string { let body: unknown try { body = JSON.parse(source) } catch { return source } if (typeof body !== 'object' || body === null || Array.isArray(body)) return source const obj = body as Record obj['stream'] = true normalizeDeveloperRole(obj) normalizeMessageContent(obj) neutralizeHarnessFingerprints(obj) normalizeToolChoice(obj) normalizeReasoningEffort(obj) return JSON.stringify(obj) } /** * Content block types the upstream understands. * * Everything else is either dropped or flattened to text — see * {@link normalizeMessageContent}. Kept as a set so the check reads as one * vocabulary rather than a chain of negations. */ const SUPPORTED_CONTENT_TYPES: ReadonlySet = new Set(['text', 'image_url']) /** * Rewrite message content into the block vocabulary the upstream accepts. * * The upstream parses `content` strictly and fails the whole turn on a block it * does not know, with a message that names the offending type: * * - `400 code 11101 unsupported content type at index 0: tool-addition` * * Two producers create such blocks in normal use: * * 1. **Tool-registry notices (#13).** After the tool registry changes mid * session, DSH inserts a `role: "developer"` message whose content is * `[{ type: 'tool-addition', toolName: 'x' }]`. {@link normalizeDeveloperRole} * only fixes the role, so the block itself reached upstream and failed every * turn after a `dsh plugin add/remove`. Flattened to text rather than dropped: * the model should still learn that the tool set changed. * 2. **Any future block type.** A host or pi-ai upgrade that introduces a new * content kind must degrade to "the model sees a note", not to a dead * channel. Unknown blocks therefore flatten the same way instead of passing * through, which is the difference between a stale plugin and a broken one. * * A message whose content flattens to nothing is left with an empty string, not * deleted: dropping a message from the middle of a tool-call sequence breaks the * pairing the upstream validates. */ function normalizeMessageContent(obj: Record): void { const messages = obj['messages'] if (!Array.isArray(messages)) return for (const message of messages) { if (typeof message !== 'object' || message === null || Array.isArray(message)) continue const wrapped = message as Record const content = wrapped['content'] if (!Array.isArray(content)) continue const rewritten: unknown[] = [] for (const block of content) { if (typeof block !== 'object' || block === null || Array.isArray(block)) { rewritten.push(block) continue } const typed = block as Record const type = typeof typed['type'] === 'string' ? typed['type'] : '' if (SUPPORTED_CONTENT_TYPES.has(type)) { rewritten.push(block) continue } const note = contentBlockNote(typed, type) if (note !== '') rewritten.push({ type: 'text', text: note }) } wrapped['content'] = rewritten } } /** * The text that replaces an unsupported content block. * * Deliberately factual and short: the model only needs to know what was there. * A `tool-addition` names its tool, which is the one detail worth keeping; every * other unknown type degrades to a bracketed type name so nothing is invented. */ function contentBlockNote(block: Record, type: string): string { if (type === 'tool-addition') { const name = typeof block['toolName'] === 'string' ? block['toolName'].trim() : '' return name !== '' ? `[tool added: ${name}]` : '[tool added]' } if (type === '') return '' return `[unsupported content: ${type}]` } /** * Drop or spell-check the `reasoning_effort` field before it reaches upstream. * * pi-ai maps a thinking level through `model.thinkingLevelMap` and, when the * map holds `null`, falls back to the raw level name (`null ?? level`). Any * spelling outside `EFFORT_VALUES` is rejected by the upstream with `code 11150 * the reasoning effort value is not supported by the current model`, which * makes the whole turn fail — this is what a Default selection used to send * when `off` was mapped to the string `'off'`. * * Two corrections: * * - `'off'` (and `'none'`) mean "no explicit effort": the field is removed so * the model applies its own default, which is what the UI's Default option * offers. Sending the literal string is not a disable instruction upstream. * - Any other unrecognized value is also removed rather than forwarded, so a * host/pi-ai change degrades to "use the model default" instead of a failed * request. */ function normalizeReasoningEffort(obj: Record): void { const effort = obj['reasoning_effort'] if (effort === undefined) return if (typeof effort === 'string' && (EFFORT_VALUES as readonly string[]).includes(effort)) return delete obj['reasoning_effort'] } /** Rewrite `role: "developer"` messages to `role: "system"` (upstream rejects developer). */ function normalizeDeveloperRole(obj: Record): void { const messages = obj['messages'] if (!Array.isArray(messages)) return for (const message of messages) { if (typeof message !== 'object' || message === null || Array.isArray(message)) continue const wrapped = message as Record if (wrapped['role'] === 'developer') wrapped['role'] = 'system' } } /** * Verbatim DSH prompt sentences the CodeBuddy channel check blacklists, each * with a meaning-preserving rewrite. * * The upstream answers HTTP 400 code 11128 ("Illegal API invocation from an * unapproved channel") when a non-user role carries another harness's prompt * fingerprint. The check is exact-phrase, not semantic: the identity banner * trips it alone, the two Web-GUI orientation sentences trip it only when both * appear verbatim, and the whole `@`-file-reference paragraph trips it even * though none of its sentences does on its own. Each rewrite therefore rewords * one sentence while keeping what it tells the model — the Web GUI stays a Web * GUI (an issue suggestion to call it a "command-line interface" would mislead * the model about its environment and is deliberately not taken). The third * entry is the prefix of the legacy paragraph that embedded the GUI URL * directly instead of naming it through the runtime context, so any URL in that * slot is covered. */ const HARNESS_FINGERPRINT_REWRITES: readonly { readonly find: string readonly replacement: string }[] = [ { find: 'You are an AI agent powered by DeepSeek Harness.', replacement: 'You are an AI agent.', }, { find: 'You are interacting with the user through the DeepSeek Harness Web GUI whose URL the runtime context names.', replacement: 'You are interacting with the user through the Web GUI whose URL the runtime context names.', }, { find: 'You are interacting with the user through the DeepSeek Harness Web GUI at ', replacement: 'You are interacting with the user through the Web GUI at ', }, { find: 'When the user refers to "this page", "this GUI", or "this app" without naming another target, they mean this GUI.', replacement: 'When the user refers to "this page", "this GUI", or "this app" and names no other target, that reference means this GUI.', }, { // `FILE_REFERENCE_PROMPT` from `dsh-file-reference`, rendered as one // paragraph of the system prompt. Unlike the entries above it is benign // prose with no product name, yet the whole paragraph trips the check // (measured in issue #12: each sentence alone passes, all of them together // do not). Rewording the opening sentence is the only single-sentence edit // that clears it — the other four sentences were each tried in place and // still returned 11128. find: 'Tokens prefixed with @ are paths the user explicitly referenced.', replacement: 'A token prefixed with @ names a path the user explicitly referenced.', }, ] /** Rewrite every blacklisted fingerprint sentence in one piece of message text. */ function scrubHarnessFingerprints(text: string): string { let scrubbed = text for (const { find, replacement } of HARNESS_FINGERPRINT_REWRITES) { // split/join, not replaceAll: replacement must be taken literally, and // replaceAll would read `$`-patterns into it. if (scrubbed.includes(find)) scrubbed = scrubbed.split(find).join(replacement) } return scrubbed } /** * Rewrite the DSH prompt fingerprints the channel check rejects, across every * role the check reads. * * Only system and assistant content is scrubbed: the channel check does not * read user or tool roles, and rewriting what the user said or what a tool * returned would corrupt the conversation the model sees. Assistant messages * are included because a reply that quoted the fingerprint sentences verbatim * (issue #10 bisected one such failure to exactly that) trips the same check * on the next turn. Runs after {@link normalizeDeveloperRole} so a developer * message is already system here. */ function neutralizeHarnessFingerprints(obj: Record): void { if (typeof obj['system'] === 'string') { obj['system'] = scrubHarnessFingerprints(obj['system']) } const messages = obj['messages'] if (!Array.isArray(messages)) return for (const message of messages) { if (typeof message !== 'object' || message === null || Array.isArray(message)) continue const wrapped = message as Record if (wrapped['role'] !== 'system' && wrapped['role'] !== 'assistant') continue const content = wrapped['content'] if (typeof content === 'string') { wrapped['content'] = scrubHarnessFingerprints(content) } else if (Array.isArray(content)) { for (const part of content) { if (typeof part !== 'object' || part === null || Array.isArray(part)) continue const shard = part as Record if (shard['type'] === 'text' && typeof shard['text'] === 'string') { shard['text'] = scrubHarnessFingerprints(shard['text']) } } } } } /** Rewrite OpenAI `tool_choice` spellings into the upstream's string form. */ function normalizeToolChoice(obj: Record): void { const suppress = (): void => { delete obj['tools'] delete obj['functions'] } const present = 'tool_choice' in obj if (!present) return const choice: unknown = obj['tool_choice'] if (typeof choice === 'string') { if (choice.trim().toLowerCase() === 'none') { delete obj['tool_choice'] suppress() } return } if (typeof choice === 'object' && choice !== null && !Array.isArray(choice)) { const wrapped = choice as Record const type = typeof wrapped['type'] === 'string' ? wrapped['type'].trim().toLowerCase() : '' if (type === 'none') { delete obj['tool_choice'] suppress() } else if (type === 'auto' || type === 'required') { obj['tool_choice'] = type } else if (type === 'function') { const fn = typeof wrapped['function'] === 'object' && wrapped['function'] !== null ? (wrapped['function'] as Record) : undefined let name = typeof fn?.['name'] === 'string' ? fn['name'] : '' if (name === '' && typeof wrapped['name'] === 'string') name = wrapped['name'] name = name.trim() obj['tool_choice'] = name !== '' ? name : 'auto' } else { delete obj['tool_choice'] } return } delete obj['tool_choice'] } /** * Inline `http(s)` image URLs into `data:` URLs, in place, before sending. * * The upstream accepts only `data:image/...;base64,` for image parts and answers * `400 code 11133 Invalid request parameters` for a remote URL (#13). DSH * produces exactly that shape for images captured earlier in a session — a * `browser_screenshot` attachment becomes * `{ type: 'image_url', image_url: { url: 'http://127.0.0.1:3080/...' } }` — so * a session that switches provider mid-flight fails on its next turn even though * nothing about the request is wrong. * * Asynchronous by necessity (it fetches), which is why it is separate from * {@link prepareChatBody}: the caller awaits this one. * * A URL that cannot be inlined is replaced with a text note rather than left in * place. Leaving it would reproduce the upstream's opaque 400; dropping it * silently would lose the fact that an image was there. The note names the * reason so the model — and the user reading the transcript — can tell "this * image was unreachable" from "there was no image". * * Only `http(s)` is fetched. `data:` and anything else is left untouched: the * former is already inline, and fetching a `file:` or `blob:` URL from here * would be a local-file read this layer has no business performing. */ export async function inlineRemoteImages( body: string, fetchImpl: typeof fetch, options: { readonly timeoutMs?: number; readonly maxBytes?: number } = {}, ): Promise { const timeoutMs = options.timeoutMs ?? 10_000 const maxBytes = options.maxBytes ?? 8 * 1024 * 1024 let parsed: unknown try { parsed = JSON.parse(body) } catch { return body } if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) return body const messages = (parsed as Record)['messages'] if (!Array.isArray(messages)) return body // Collect first, then fetch: a body with no remote image must not pay for a // single network call, which is the overwhelmingly common case. const targets: { block: Record; url: string }[] = [] for (const message of messages) { if (typeof message !== 'object' || message === null || Array.isArray(message)) continue const content = (message as Record)['content'] if (!Array.isArray(content)) continue for (const block of content) { if (typeof block !== 'object' || block === null || Array.isArray(block)) continue const typed = block as Record if (typed['type'] !== 'image_url') continue const imageUrl = typed['image_url'] if (typeof imageUrl !== 'object' || imageUrl === null) continue const url = (imageUrl as Record)['url'] if (typeof url === 'string' && /^https?:\/\//i.test(url)) targets.push({ block: typed, url }) } } if (targets.length === 0) return body await Promise.all(targets.map(async ({ block, url }) => { const inlined = await fetchAsDataUrl(url, fetchImpl, timeoutMs, maxBytes) if (inlined.ok) { block['image_url'] = { url: inlined.dataUrl } return } // Replace the whole block: an `image_url` part whose url is not an image is // still a parse error upstream. block['type'] = 'text' block['text'] = `[image unavailable: ${inlined.reason}]` delete block['image_url'] })) return JSON.stringify(parsed) } /** Fetch one URL and encode it as a `data:` URL, or explain why it cannot be. */ async function fetchAsDataUrl( url: string, fetchImpl: typeof fetch, timeoutMs: number, maxBytes: number, ): Promise<{ ok: true; dataUrl: string } | { ok: false; reason: string }> { const controller = new AbortController() const timer = setTimeout(() => { controller.abort() }, timeoutMs) try { const response = await fetchImpl(url, { signal: controller.signal }) if (!response.ok) return { ok: false, reason: `http ${response.status}` } const declared = Number(response.headers.get('content-length') ?? '') if (Number.isFinite(declared) && declared > maxBytes) { return { ok: false, reason: `too large (${Math.round(declared / 1024)} KB)` } } const bytes = Buffer.from(await response.arrayBuffer()) if (bytes.byteLength > maxBytes) { return { ok: false, reason: `too large (${Math.round(bytes.byteLength / 1024)} KB)` } } if (bytes.byteLength === 0) return { ok: false, reason: 'empty response' } // Sniff from the magic bytes rather than trusting Content-Type: an attachment // endpoint may serve octet-stream for a perfectly good PNG, and the upstream // validates the data URL's media type. const mime = sniffImageMime(bytes) if (mime === undefined) return { ok: false, reason: 'not an image' } return { ok: true, dataUrl: `data:${mime};base64,${bytes.toString('base64')}` } } catch (error: unknown) { const aborted = error instanceof Error && error.name === 'AbortError' return { ok: false, reason: aborted ? 'timed out' : 'fetch failed' } } finally { clearTimeout(timer) } } /** Detect the image type from magic bytes, or `undefined` when it is not one. */ function sniffImageMime(bytes: Buffer): string | undefined { if (bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47) return 'image/png' if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return 'image/jpeg' if (bytes.length >= 6 && bytes.subarray(0, 3).toString('latin1') === 'GIF') return 'image/gif' if (bytes.length >= 12 && bytes.subarray(0, 4).toString('latin1') === 'RIFF' && bytes.subarray(8, 12).toString('latin1') === 'WEBP') return 'image/webp' return undefined } /** One JSON-envelope response from the upstream, already unwrapped. */ interface Envelope { code: number msg: string data: unknown } async function readEnvelope(response: Response): Promise { const text = await response.text() let parsed: unknown try { parsed = JSON.parse(text) } catch { throw new Error(`codebuddy upstream returned non-JSON (http ${response.status}): ${text.slice(0, 160)}`) } if (typeof parsed !== 'object' || parsed === null) { throw new Error(`codebuddy upstream returned an unexpected document (http ${response.status})`) } const document = parsed as Record const envelope: Envelope = { code: typeof document['code'] === 'number' ? document['code'] : 0, msg: typeof document['msg'] === 'string' ? document['msg'] : '', data: 'data' in document ? document['data'] : undefined, } return envelope } /** Fail an envelope whose business code is non-zero, classified like HTTP errors. */ function envelopeError(status: number, envelope: Envelope): Error { const kind = classifyUpstreamError(status, envelope.msg) return new Error(`codebuddy upstream ${kind} (http ${status}): ${envelope.msg.slice(0, 160)}`) } /** * Upstream HTTP client. One instance serves the whole plugin; requests take * the credential explicitly so token refreshes apply on the next call. */ export class CodeBuddyUpstreamClient { /** * Begin resolving the client identity as soon as a client exists. * * Request headers are assembled synchronously, so the version headers can * only appear once resolution has settled. Warming it here means the first * request already carries them in practice, and a request issued before the * probe finishes still carries `X-IDE-Type`/`X-IDE-Name` in the meantime. */ constructor() { void startClientIdentityResolution().catch(() => {}) } /** POST the chat endpoint; a successful answer is the raw SSE response. */ async chatStream( credential: CodeBuddyCredential, bodyJson: string, signal?: AbortSignal, ): Promise { let response: Response try { response = await fetch(`${chatBase(credential)}/v2/chat/completions`, { method: 'POST', headers: { ...chatHeaders(credential), 'Authorization': `Bearer ${credential.accessToken}` }, body: bodyJson, ...signal === undefined ? {} : { signal }, }) } catch (error: unknown) { return { ok: false, status: 0, kind: 'server', message: `transport error: ${String(error)}` } } if (response.ok) return { ok: true, response } const text = (await response.text()).slice(0, ERROR_BODY_LIMIT) return { ok: false, status: response.status, kind: classifyUpstreamError(response.status, text), message: text, } } /** POST the token-refresh endpoint; the caller merges the outcome. */ async refreshToken(credential: CodeBuddyCredential): Promise { const response = await fetch(`${chatBase(credential)}/v2/plugin/auth/token/refresh`, { method: 'POST', headers: refreshHeaders(credential), signal: AbortSignal.timeout(JSON_TIMEOUT_MS), }) const envelope = await readEnvelope(response) if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope) const data = typeof envelope.data === 'object' && envelope.data !== null ? envelope.data as Record : {} const accessToken = typeof data['accessToken'] === 'string' ? data['accessToken'] : '' if (accessToken === '') throw new Error('codebuddy token refresh returned no accessToken; sign in again in the CodeBuddy app') const outcome: CodeBuddyRefreshOutcome = { accessToken } if (typeof data['refreshToken'] === 'string' && data['refreshToken'] !== '') outcome.refreshToken = data['refreshToken'] if (typeof data['expiresIn'] === 'number' && data['expiresIn'] > 0) outcome.expiresInSec = data['expiresIn'] if (typeof data['refreshExpiresIn'] === 'number' && data['refreshExpiresIn'] > 0) { outcome.refreshExpiresInSec = data['refreshExpiresIn'] } if (typeof data['domain'] === 'string' && data['domain'] !== '') outcome.domain = data['domain'] return outcome } /** GET the personal model catalog and keep the `cli` agent's models only. */ async fetchModels(credential: CodeBuddyCredential): Promise { const response = await fetch(`${chatBase(credential)}/console/enterprises/personal/models`, { headers: { 'Authorization': `Bearer ${credential.accessToken}`, 'Accept': 'application/json', 'Origin': originReferer(credential), 'Referer': `${originReferer(credential)}/`, ...identityHeaders(), }, signal: AbortSignal.timeout(JSON_TIMEOUT_MS), }) const envelope = await readEnvelope(response) if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope) const data = typeof envelope.data === 'object' && envelope.data !== null ? envelope.data as Record : {} const rawModels = Array.isArray(data['models']) ? data['models'] : [] const agents = Array.isArray(data['agents']) ? data['agents'] : [] let cliIds: readonly string[] | undefined for (const agent of agents) { if (typeof agent === 'object' && agent !== null) { const wrapped = agent as Record if (wrapped['name'] === 'cli' && Array.isArray(wrapped['models'])) { cliIds = wrapped['models'].filter((id): id is string => typeof id === 'string') break } } } if (cliIds === undefined || cliIds.length === 0) { throw new Error('codebuddy model catalog lists no cli agent models') } const byId = new Map() for (const model of rawModels) { if (typeof model !== 'object' || model === null) continue const wrapped = model as Record const id = typeof wrapped['id'] === 'string' ? wrapped['id'] : '' if (id === '' || wrapped['disabled'] === true) continue const input = typeof wrapped['maxInputTokens'] === 'number' ? wrapped['maxInputTokens'] : 0 const output = typeof wrapped['maxOutputTokens'] === 'number' ? wrapped['maxOutputTokens'] : 0 if (input <= 0 || output <= 0) continue byId.set(id, { id, name: typeof wrapped['name'] === 'string' && wrapped['name'] !== '' ? wrapped['name'] : id, contextWindow: input, maxTokens: output, supportsImages: wrapped['supportsImages'] === true && wrapped['disabledMultimodal'] !== true, ...resolveUpstreamReasoning(wrapped), ...resolveUpstreamBilling(wrapped), }) } const models = cliIds .map(id => byId.get(id)) .filter((model): model is CodeBuddyUpstreamModel => model !== undefined) if (models.length === 0) throw new Error('codebuddy model catalog resolved to an empty list') return models } /** POST the billing endpoint for the aggregated remaining credit. */ async fetchCredits(credential: CodeBuddyCredential): Promise { const now = new Date() const format = (date: Date): string => [ date.getFullYear().toString().padStart(4, '0'), (date.getMonth() + 1).toString().padStart(2, '0'), date.getDate().toString().padStart(2, '0'), ].join('-') + ' ' + [ date.getHours().toString().padStart(2, '0'), date.getMinutes().toString().padStart(2, '0'), date.getSeconds().toString().padStart(2, '0'), ].join(':') const response = await fetch(`${billingBase(credential)}/v2/billing/meter/get-user-resource`, { method: 'POST', headers: billingHeaders(credential), body: JSON.stringify({ PageNumber: 1, PageSize: 100, ProductCode: 'p_tcaca', Status: [0, 3], PackageEndTimeRangeBegin: format(now), PackageEndTimeRangeEnd: format(new Date(now.getTime() + 365 * 101 * 24 * 3600 * 1000)), }), signal: AbortSignal.timeout(JSON_TIMEOUT_MS), }) const envelope = await readEnvelope(response) if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope) const responseWrapper = typeof envelope.data === 'object' && envelope.data !== null ? envelope.data as Record : {} const data = typeof responseWrapper['Response'] === 'object' && responseWrapper['Response'] !== null ? responseWrapper['Response'] as Record : {} const inner = typeof data['Data'] === 'object' && data['Data'] !== null ? data['Data'] as Record : {} const rawAccounts = Array.isArray(inner['Accounts']) ? inner['Accounts'] : [] const accounts: CodeBuddyCreditAccount[] = [] let total = 0 const nowMs = Date.now() const struct = (raw: unknown): Record | undefined => { if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) return undefined return raw as Record } const num = (account: Record, ...keys: string[]): number => { for (const key of keys) { const value = account[key] const parsed = typeof value === 'number' ? value : typeof value === 'string' ? Number(value) : Number.NaN if (Number.isFinite(parsed)) return parsed } return 0 } const tsMs = (value: unknown): number | undefined => { const parsed = typeof value === 'number' ? value : typeof value === 'string' ? Number(value) : Number.NaN if (!Number.isFinite(parsed)) return undefined // seconds vs milliseconds (epoch heuristics: < 1e12 is seconds) const ms = parsed < 1e12 ? parsed * 1000 : parsed return ms > 0 ? ms : undefined } for (const rawItem of rawAccounts) { const account = struct(rawItem) if (account === undefined) continue const cycleSize = num(account, 'CycleCapacitySize', 'CycleCapacitySizePrecise') const cycleRemain = num(account, 'CycleCapacityRemain', 'CycleCapacityRemainPrecise', 'CycleRemainCapacity') const cycleUsed = num(account, 'CycleCapacityUsed', 'CycleCapacityUsedPrecise') const capacitySize = num(account, 'CapacitySize', 'CapacitySizePrecise') const capacityRemain = num(account, 'CapacityRemain', 'CapacityRemainPrecise') const capacityUsed = num(account, 'CapacityUsed', 'CapacityUsedPrecise') let size: number let remain: number let used: number if (cycleSize > 0) { size = cycleSize remain = cycleRemain used = cycleUsed } else if (cycleRemain > 0 || cycleUsed > 0) { size = cycleSize remain = cycleRemain used = cycleUsed } else { size = capacitySize remain = capacityRemain used = capacityUsed } if (size <= 0 && cycleSize > 0) size = cycleSize // A package with no capacity at all is treated as long-lived (no size bar). if (remain < 0) remain = 0 if (used < 0) used = 0 const expireAtMs = tsMs(account['DeductionEndTime'] ?? account['deductionEndTime'] ?? account['ExpiredTime'] ?? account['expiredTime'] ?? account['CycleEndTime'] ?? account['cycleEndTime']) const expired = expireAtMs !== undefined && expireAtMs <= nowMs const expiringSoon = expireAtMs !== undefined && !expired && expireAtMs - nowMs <= 7 * 24 * 3600 * 1000 total += remain accounts.push({ ...typeof account['PackageCode'] === 'string' && account['PackageCode'] !== '' ? { packageCode: account['PackageCode'] } : {}, packageName: typeof account['PackageName'] === 'string' && account['PackageName'] !== '' ? account['PackageName'] : '(unnamed)', total: size, remain, used, size, ...expireAtMs !== undefined ? { expireAtMs } : {}, expired, expiringSoon, } satisfies CodeBuddyCreditAccount) } return { total, accounts } } /** * Fetch official per-request usage over a window and aggregate it. * * The upstream billing endpoint (`get-user-request-usage`) returns paginated * rows of individual billing requests (credit consumed, model, client, * request time). The rows are aggregated in-process into today / 7-day / * month totals, a zero-filled daily series, and a per-model breakdown, * which back the plugin's credit-statistics panel. * * Pagination follows the upstream page contract; a capped page count guards * against runaway loops. Window days default to 31 (the upstream's range). */ async fetchUsage(credential: CodeBuddyCredential, windowDays = 31): Promise { const now = new Date() const dateKey = (date: Date): string => [ date.getFullYear().toString().padStart(4, '0'), (date.getMonth() + 1).toString().padStart(2, '0'), date.getDate().toString().padStart(2, '0'), ].join('-') const timeParts = (date: Date): string => [ date.getFullYear().toString().padStart(4, '0'), (date.getMonth() + 1).toString().padStart(2, '0'), date.getDate().toString().padStart(2, '0'), ].join('-') + ' ' + [ date.getHours().toString().padStart(2, '0'), date.getMinutes().toString().padStart(2, '0'), date.getSeconds().toString().padStart(2, '0'), ].join(':') const rangeStart = new Date(now) rangeStart.setDate(rangeStart.getDate() - (windowDays - 1)) rangeStart.setHours(0, 0, 0, 0) const rangeEnd = new Date(now) rangeEnd.setHours(23, 59, 59, 999) const rangeStartKey = dateKey(rangeStart) const rangeEndKey = dateKey(rangeEnd) const url = `${billingBase(credential)}/billing/meter/get-user-request-usage` const pageSize = 3000 const maxPages = 100 const rows: CodeBuddyUsageRow[] = [] let reportedTotal = 0 let fetchedRaw = 0 let pages = 0 let lastError: string | undefined for (let pageNum = 1; pageNum <= maxPages; pageNum += 1) { let response: Response try { response = await fetch(url, { method: 'POST', headers: billingHeaders(credential), body: JSON.stringify({ startTime: timeParts(rangeStart), endTime: timeParts(rangeEnd), pageNum, pageSize, }), signal: AbortSignal.timeout(JSON_TIMEOUT_MS), }) } catch (error: unknown) { lastError = `transport error: ${String(error)}` break } let envelope: Envelope try { envelope = await readEnvelope(response) } catch (error: unknown) { lastError = error instanceof Error ? error.message : String(error) break } if (!response.ok || (envelope.code !== 0 && envelope.code !== 200)) { lastError = envelope.msg.trim() === '' ? `http ${response.status}` : envelope.msg break } const data = typeof envelope.data === 'object' && envelope.data !== null ? envelope.data as Record : {} const rawRows = Array.isArray(data['data']) ? data['data'] : [] const total = typeof data['total'] === 'number' ? data['total'] : (typeof data['total'] === 'string' ? Number(data['total']) : rawRows.length) if (Number.isFinite(total) && total > reportedTotal) reportedTotal = Math.round(total) fetchedRaw += rawRows.length for (const raw of rawRows) { if (typeof raw !== 'object' || raw === null) continue const row = raw as Record const credit = typeof row['credit'] === 'number' ? row['credit'] : (typeof row['credit'] === 'string' ? Number(row['credit']) : Number.NaN) if (!Number.isFinite(credit) || credit < 0) continue const rawTime = row['requestTime'] ?? row['request_time'] ?? row['requesttime'] const parsed = parseUsageTime(rawTime, now) if (parsed === undefined) continue const date = dateKey(parsed.date) if (date < rangeStartKey || date > rangeEndKey) continue rows.push({ requestId: typeof row['requestId'] === 'string' ? row['requestId'] : (typeof row['request_id'] === 'string' ? row['request_id'] : 'unknown'), model: typeof row['model'] === 'string' && row['model'] !== '' ? row['model'] : '—', client: typeof row['client'] === 'string' && row['client'] !== '' ? row['client'] : '—', credit, requestTime: typeof rawTime === 'string' ? rawTime : String(rawTime ?? date), ts: parsed.ts, date, }) } pages += 1 const hasMore = (rawRows.length > 0 && fetchedRaw < reportedTotal && pageNum < maxPages) if (!hasMore) break } // When the window exceeds maxPages the upstream may have more pages than we // fetched; the totals are then a lower bound and the stats must not present // themselves as complete. const truncated = reportedTotal > 0 && fetchedRaw < reportedTotal // Aggregate into today / week / month + daily + per-model counts. const todayKey = dateKey(now) const monthPrefix = now.getFullYear().toString() + '-' + (now.getMonth() + 1).toString().padStart(2, '0') let usageToday = 0 let usage7Days = 0 let usageThisMonth = 0 const daily = new Map() const dailyModels = new Map>() const modelTotals = new Map() const sorted = [...rows].sort((a, b) => b.ts - a.ts) for (const row of sorted) { const distance = daysBetween(row.date, todayKey) if (distance === 0) usageToday += row.credit if (distance >= 0 && distance < 7) usage7Days += row.credit if (row.date.startsWith(monthPrefix)) usageThisMonth += row.credit daily.set(row.date, (daily.get(row.date) ?? 0) + row.credit) const modelKey = row.model === '—' ? '未知模型' : row.model const entry = modelTotals.get(modelKey) ?? { requestCount: 0, credit: 0 } entry.requestCount += 1 entry.credit += row.credit modelTotals.set(modelKey, entry) const perDay = dailyModels.get(row.date) if (perDay !== undefined) { const modelDay = perDay.get(modelKey) ?? { requestCount: 0, credit: 0 } modelDay.requestCount += 1 modelDay.credit += row.credit perDay.set(modelKey, modelDay) } else { dailyModels.set(row.date, new Map([[modelKey, { requestCount: 1, credit: row.credit }]])) } } // Zero-filled daily series across the window. const dailySeries: CodeBuddyUsageDaily[] = [] const walk = new Date(rangeStart) while (walk <= rangeEnd) { const key = dateKey(walk) const dayTotal = daily.get(key) ?? 0 const dayModels = dailyModels.get(key) dailySeries.push({ date: key, usage: dayTotal, ...dayModels !== undefined && dayModels.size > 0 ? { models: [...dayModels.entries()].map(([model, { requestCount, credit }]) => ({ model, requestCount, credit })) } : {}, }) walk.setDate(walk.getDate() + 1) } const models = [...modelTotals.entries()] .map(([model, { requestCount, credit }]) => ({ model, requestCount, credit })) .sort((a, b) => b.credit - a.credit || b.requestCount - a.requestCount || a.model.localeCompare(b.model)) const status: CodeBuddyUsageStats['status'] = lastError !== undefined || truncated ? (reportedTotal > 0 ? 'partial' : 'unavailable') : 'complete' // Deduplicate identical request ids close in time (upstream may repeat). const uniqueRows = new Map() for (const row of sorted) uniqueRows.set(`${row.requestId}@${row.ts}`, row) const detailLimit = 100 return { status, rangeStart: rangeStartKey, rangeEnd: rangeEndKey, collectedAt: Date.now(), summary: { usageToday, usage7Days, usageThisMonth }, daily: dailySeries, models, requests: [...uniqueRows.values()].slice(0, detailLimit), detailLimit, } } /** * POST the CN daily check-in endpoint. The answer is classified for the * card rather than thrown: a transport or envelope failure, and an upstream * "already checked in" business code, all come back as plain outcomes so * the browser half never has to interpret upstream error text. */ async checkIn(credential: CodeBuddyCredential): Promise { if (regionOf(credential.domain) !== 'cn') { return { status: 'failed', message: 'daily check-in is only available for the CodeBuddy CN account' } } let response: Response try { response = await fetch(`${CN_CHAT_BASE}${CN_CHECKIN_PATH}`, { method: 'POST', headers: checkInHeaders(credential), body: '{}', signal: AbortSignal.timeout(JSON_TIMEOUT_MS), }) } catch (error: unknown) { return { status: 'failed', message: `transport error: ${String(error)}` } } let envelope: Envelope try { envelope = await readEnvelope(response) } catch (error: unknown) { return { status: 'failed', message: error instanceof Error ? error.message : String(error) } } if (response.ok && envelope.code === 0) { return { status: 'ok', message: envelope.msg.trim() === '' ? 'checked in' : envelope.msg } } const message = envelope.msg.trim() === '' ? `http ${response.status}` : envelope.msg const lowered = message.toLowerCase() for (const marker of CHECKIN_ALREADY_MARKERS) { if (lowered.includes(marker.toLowerCase())) return { status: 'already', message } } return { status: 'failed', message } } }