Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ node_modules.bak/
.npm-cache/
# DSH STORE reviews the fixed Git Commit, so the built runtime files ship in
# git. `lib/` artifacts are committed explicitly with `git add -f`; the ignore
# stays so map churn and stray local rebuilds do not show up as noise.
# stays so stray local rebuilds do not show up as noise. Source maps are
# deliberately outside both: nothing loads them, `files` keeps them out of the
# tarball, and they are the one tsdown output that does not rebuild
# byte-identically across machines, so tracking them only breaks the CI
# `git diff --exit-code -- lib/` guard.
lib/
*.tgz
.DS_Store
Expand Down
10 changes: 8 additions & 2 deletions lib/client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/client.js.map

This file was deleted.

14 changes: 10 additions & 4 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ interface CommandCodeConnectionOptions {
* text-only request never depends on the attachment seam.
*/
type ResolveAttachments = () => AttachmentStore | undefined;
/** What the rotation hook knows about the request it is rotating within. */
/**
* What the rotation hook knows about the request it is rotating within.
*
* Not exported: the only consumer is the `rotateApiKey` shape below, so the
* name never reaches the published `.d.ts` and the package's `exports` map
* leaves no deep-import path to it.
*/
interface AccountRotationContext {
/** Every API key this request has already used, just-rejected key included. */
tried: readonly string[];
Expand Down Expand Up @@ -265,13 +271,13 @@ interface CommandCodeAdapterDeps<C extends CommandCodeConnectionOptions = Comman
*/
imageOffload?: SurfaceImagePolicy;
}
/** Account identity from `/alpha/whoami`. */
/** Account identity from `/alpha/whoami`; reached only through `CommandCodeUsageReport`. */
interface CommandCodeAccount {
id: string;
name: string;
userName: string;
}
/** Usage summary from `/alpha/usage/summary`. */
/** Usage summary from `/alpha/usage/summary`; reached only through `CommandCodeUsageReport`. */
interface CommandCodeUsage {
totalCount: number;
totalCost: number;
Expand Down Expand Up @@ -326,7 +332,7 @@ interface CommandCodeCredits {
resetAt: number;
};
}
/** Subscription plan state from `/alpha/billing/subscriptions`. */
/** Subscription plan state from `/alpha/billing/subscriptions`; reached only through `CommandCodeUsageReport`. */
interface CommandCodePlan {
/** Raw subscription plan id (e.g. `individual-pro`); empty when unreported. */
planId: string;
Expand Down
94 changes: 75 additions & 19 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion lib/index.js.map

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"files": [
"lib",
"!lib/**/*.map",
"assets",
"icon.svg",
"locale",
Expand Down
56 changes: 43 additions & 13 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,16 @@ export const DEFAULT_API_BASE = 'https://api.commandcode.ai'
*/
export const DEFAULT_GENERATE_MAX_TOKENS = MAX_OUTPUT_TOKEN_LIMIT

export const MODELS_TIMEOUT_MS = 10_000
/** Catalog fetch budget. Internal: both call sites are in this module. */
const MODELS_TIMEOUT_MS = 10_000
/** How long the picker's plan-filter billing facts stay cached before refetching. */
export const BILLING_ACCESS_TTL_MS = 5 * 60_000
/**
* How long a learned CLI-only protocol preference stays cached per account.
* After this TTL a request may probe Provider API again, so an upgraded Go
* account can recover without a restart.
*/
export const PROTOCOL_CACHE_TTL_MS = 15 * 60_000
const PROTOCOL_CACHE_TTL_MS = 15 * 60_000

/** The entry-plan tier weight (`individual-go` in KNOWN_SUBSCRIPTION_PLANS). */
const GO_TIER_WEIGHT = 0
Expand Down Expand Up @@ -1003,6 +1004,29 @@ function priceRequestImage(
return { visualTokens: commandCodeImageTokens(model, target.width, target.height), text: '' }
}

/**
* Base64 payloads keyed by the cached request version itself.
*
* `readImageRequest()` returns one codec object per (attachment, encoder
* target) and `requestImages()` reuses that same object for every occurrence in
* one generation, but a history that carries an image also replays it in every
* later message. Encoding per occurrence therefore re-base64s the same
* multi-megabyte buffer once per replay — the single most expensive
* per-occurrence step of an image request. Keying on the object makes an
* occurrence a map hit, and the WeakMap ties the copy's lifetime to the
* harness's own version cache rather than to this plugin.
*/
const encodedRequestImages = new WeakMap<RequestImageAttachment, { base64: string; dataUrl: string }>()
function encodedRequestImage(version: RequestImageAttachment): { base64: string; dataUrl: string } {
let encoded = encodedRequestImages.get(version)
if (encoded === undefined) {
const base64 = Buffer.from(version.data).toString('base64')
encoded = { base64, dataUrl: `data:${version.mediaType};base64,${base64}` }
encodedRequestImages.set(version, encoded)
}
return encoded
}

/**
* Convert one image reference to the final `/alpha/generate` wire format.
* The official CLI accepts a base64 source internally, then toWireMessages()
Expand All @@ -1016,7 +1040,7 @@ async function imageToCommandCode(
const version = await readImage(ref)
return {
type: 'image',
image: `data:${version.mediaType};base64,${Buffer.from(version.data).toString('base64')}`,
image: encodedRequestImage(version).dataUrl,
mimeType: version.mediaType,
}
}
Expand Down Expand Up @@ -1187,7 +1211,7 @@ async function imageToOpenAI(
return {
type: 'image_url',
image_url: {
url: `data:${version.mediaType};base64,${Buffer.from(version.data).toString('base64')}`,
url: encodedRequestImage(version).dataUrl,
},
}
}
Expand Down Expand Up @@ -1264,7 +1288,7 @@ async function imageToMessages(
const version = await readImage(ref)
return {
type: 'image',
source: { type: 'base64', media_type: version.mediaType, data: Buffer.from(version.data).toString('base64') },
source: { type: 'base64', media_type: version.mediaType, data: encodedRequestImage(version).base64 },
}
}

Expand Down Expand Up @@ -1451,8 +1475,14 @@ export interface CommandCodeConnectionOptions {
*/
export type ResolveAttachments = () => AttachmentStore | undefined

/** What the rotation hook knows about the request it is rotating within. */
export interface AccountRotationContext {
/**
* What the rotation hook knows about the request it is rotating within.
*
* Not exported: the only consumer is the `rotateApiKey` shape below, so the
* name never reaches the published `.d.ts` and the package's `exports` map
* leaves no deep-import path to it.
*/
interface AccountRotationContext {
/** Every API key this request has already used, just-rejected key included. */
tried: readonly string[]
/** The provider's own reset time for a `rate-limit` rejection, in millis. */
Expand Down Expand Up @@ -1517,15 +1547,15 @@ export interface CommandCodeAdapterDeps<C extends CommandCodeConnectionOptions =
imageOffload?: SurfaceImagePolicy
}

/** Account identity from `/alpha/whoami`. */
export interface CommandCodeAccount {
/** Account identity from `/alpha/whoami`; reached only through `CommandCodeUsageReport`. */
interface CommandCodeAccount {
id: string
name: string
userName: string
}

/** Usage summary from `/alpha/usage/summary`. */
export interface CommandCodeUsage {
/** Usage summary from `/alpha/usage/summary`; reached only through `CommandCodeUsageReport`. */
interface CommandCodeUsage {
totalCount: number
totalCost: number
successRate: number
Expand Down Expand Up @@ -1571,8 +1601,8 @@ export interface CommandCodeCredits {
weekly?: { used: number; cap: number; exceeded: boolean; resetAt: number }
}

/** Subscription plan state from `/alpha/billing/subscriptions`. */
export interface CommandCodePlan {
/** Subscription plan state from `/alpha/billing/subscriptions`; reached only through `CommandCodeUsageReport`. */
interface CommandCodePlan {
/** Raw subscription plan id (e.g. `individual-pro`); empty when unreported. */
planId: string
/** Display name (e.g. `Pro`); falls back to the raw id for unknown plans. */
Expand Down
Loading
Loading