@bastani/atomic 0.8.31-alpha.2 → 0.8.31-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,13 @@ declare module "@earendil-works/pi-ai" {
5
5
  contextWindowOptions?: readonly number[];
6
6
  /** Original/default scalar context window, preserved when contextWindow is overridden for a session. */
7
7
  defaultContextWindow?: number;
8
+ /**
9
+ * Hard prompt/input cap for providers (e.g. GitHub Copilot `max_prompt_tokens`) that enforce an
10
+ * input budget below the displayed context window. When set and below `contextWindow`, it is the
11
+ * effective input budget for compaction thresholds and overflow recovery; `contextWindow` remains
12
+ * the displayed/branded window.
13
+ */
14
+ maxInputTokens?: number;
8
15
  }
9
16
  }
10
17
  export interface ContextWindowParseResult {
@@ -33,6 +40,14 @@ export declare function parseContextWindowValue(input: string): ContextWindowPar
33
40
  export declare function formatContextWindow(value: number): string;
34
41
  export declare function normalizeContextWindowOptions(values: readonly number[] | undefined): number[];
35
42
  export declare function getModelDefaultContextWindow(model: Model<Api>): number;
43
+ /**
44
+ * Effective input-token budget for compaction/overflow decisions. Equals the displayed
45
+ * `contextWindow` unless a smaller hard input cap (`maxInputTokens`, e.g. GitHub Copilot's
46
+ * `max_prompt_tokens`) is advertised, in which case the lower of the two is used. This lets a model
47
+ * display its full/branded window while compaction and overflow recovery respect the real,
48
+ * server-enforced input limit.
49
+ */
50
+ export declare function getEffectiveInputBudget(model: Model<Api>): number;
36
51
  export declare function getSupportedContextWindows(model: Model<Api>): number[];
37
52
  export declare function withContextWindowOptions<TApi extends Api>(model: Model<TApi>, contextWindowOptions: readonly number[]): Model<TApi>;
38
53
  export declare function selectContextWindow<TApi extends Api>(model: Model<TApi>, contextWindow: number, options?: ContextWindowSelectionOptions): ContextWindowSelection<TApi> | ContextWindowSelectionError;
@@ -1 +1 @@
1
- {"version":3,"file":"context-window.d.ts","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AAAA,OA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CAAC,EAClB,oBAAoB,EAAE,SAAS,MAAM,EAAE,GACrC,KAAK,CAAC,IAAI,CAAC,CAMb;AAuBD,wBAAgB,mBAAmB,CAAC,IAAI,SAAS,GAAG,EACnD,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,6BAAkC,GACzC,sBAAsB,CAAC,IAAI,CAAC,GAAG,2BAA2B,CAwB5D","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
1
+ {"version":3,"file":"context-window.d.ts","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAExD,OAAO,QAAQ,uBAAuB,CAAC,CAAC;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AI,SAAS,GAAG,EACxD,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,oBAAoB,EAAE,SAAS,MAAM,EAAE,GACrC,KAAK,CAAC,IAAI,CAAC,CAMb;AAuBD,wBAAgB,mBAAmB,CAAC,IAAI,SAAS,GAAG,EACnD,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,6BAAkC,GACzC,sBAAsB,CAAC,IAAI,CAAC,GAAG,2BAA2B,CAwB5D","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t\t/**\n\t\t * Hard prompt/input cap for providers (e.g. GitHub Copilot `max_prompt_tokens`) that enforce an\n\t\t * input budget below the displayed context window. When set and below `contextWindow`, it is the\n\t\t * effective input budget for compaction thresholds and overflow recovery; `contextWindow` remains\n\t\t * the displayed/branded window.\n\t\t */\n\t\tmaxInputTokens?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\n/**\n * Effective input-token budget for compaction/overflow decisions. Equals the displayed\n * `contextWindow` unless a smaller hard input cap (`maxInputTokens`, e.g. GitHub Copilot's\n * `max_prompt_tokens`) is advertised, in which case the lower of the two is used. This lets a model\n * display its full/branded window while compaction and overflow recovery respect the real,\n * server-enforced input limit.\n */\nexport function getEffectiveInputBudget(model: Model<Api>): number {\n\tconst cap = model.maxInputTokens;\n\treturn isPositiveInteger(cap ?? 0) ? Math.min(model.contextWindow, cap as number) : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
@@ -52,6 +52,17 @@ export function normalizeContextWindowOptions(values) {
52
52
  export function getModelDefaultContextWindow(model) {
53
53
  return isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow : model.contextWindow;
54
54
  }
55
+ /**
56
+ * Effective input-token budget for compaction/overflow decisions. Equals the displayed
57
+ * `contextWindow` unless a smaller hard input cap (`maxInputTokens`, e.g. GitHub Copilot's
58
+ * `max_prompt_tokens`) is advertised, in which case the lower of the two is used. This lets a model
59
+ * display its full/branded window while compaction and overflow recovery respect the real,
60
+ * server-enforced input limit.
61
+ */
62
+ export function getEffectiveInputBudget(model) {
63
+ const cap = model.maxInputTokens;
64
+ return isPositiveInteger(cap ?? 0) ? Math.min(model.contextWindow, cap) : model.contextWindow;
65
+ }
55
66
  export function getSupportedContextWindows(model) {
56
67
  return normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);
57
68
  }
@@ -1 +1 @@
1
- {"version":3,"file":"context-window.js","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AAoCA,MAAM,oBAAoB,GAA2B;IACpD,CAAC,EAAE,KAAK;IACR,CAAC,EAAE,SAAS;CACZ,CAAC;AAEF,SAAS,iBAAiB,CAAC,KAAa;IACvC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC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M,QAAQ,GAAG,KAAmB,CAAC;IACrC,MAAM,SAAS,GAAG,0BAA0B,CAAC,QAAQ,CAAC,CAAC;IACvD,MAAM,qBAAqB,GAAG,8BAA8B,CAAC,QAAQ,EAAE,aAAa,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;IAC1G,IAAI,qBAAqB,KAAK,SAAS,EAAE,CAAC;QACzC,OAAO;YACN,KAAK,EAAE,kBAAkB,mBAAmB,CAAC,aAAa,CAAC,wBAAwB,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,EAAE,uBAAuB,SAAS,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;SACpL,CAAC;IACH,CAAC;IAED,OAAO;QACN,KAAK,EAAE;YACN,GAAG,KAAK;YACR,oBAAoB,EAAE,4BAA4B,CAAC,QAAQ,CAAC;YAC5D,aAAa,EAAE,qBAAqB;YACpC,oBAAoB,EAAE,SAAS;SAC/B;QACD,aAAa,EAAE,qBAAqB;KACpC,CAAC;AACH,CAAC","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
1
+ {"version":3,"file":"context-window.js","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AA2CA,MAAM,oBAAoB,GAA2B;IACpD,CAAC,EAAE,KAAK;IACR,CAAC,EAAE,SAAS;CACZ,CAAC;AAEF,SAAS,iBAAiB,CAAC,KAAa;IACvC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,KAAa;IACvD,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,uDAAuD,CAAC;AACvG,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,KAAa;IACpD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,OAAO,EAAE,KAAK,EAAE,iCAAiC,EAAE,CAAC;IACrD,CAAC;IAED,MAAM,KAAK,GAAG,4BAA4B,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACzD,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,OAAO,EAAE,KAAK,EAAE,2BAA2B,KAAK,+DAA+D,EAAE,CAAC;IACnH,CAAC;IAED,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC;IACrC,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,oBAAoB,C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R,oBAAoB,EAAE,4BAA4B,CAAC,QAAQ,CAAC;YAC5D,aAAa,EAAE,qBAAqB;YACpC,oBAAoB,EAAE,SAAS;SAC/B;QACD,aAAa,EAAE,qBAAqB;KACpC,CAAC;AACH,CAAC","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t\t/**\n\t\t * Hard prompt/input cap for providers (e.g. GitHub Copilot `max_prompt_tokens`) that enforce an\n\t\t * input budget below the displayed context window. When set and below `contextWindow`, it is the\n\t\t * effective input budget for compaction thresholds and overflow recovery; `contextWindow` remains\n\t\t * the displayed/branded window.\n\t\t */\n\t\tmaxInputTokens?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\n/**\n * Effective input-token budget for compaction/overflow decisions. Equals the displayed\n * `contextWindow` unless a smaller hard input cap (`maxInputTokens`, e.g. GitHub Copilot's\n * `max_prompt_tokens`) is advertised, in which case the lower of the two is used. This lets a model\n * display its full/branded window while compaction and overflow recovery respect the real,\n * server-enforced input limit.\n */\nexport function getEffectiveInputBudget(model: Model<Api>): number {\n\tconst cap = model.maxInputTokens;\n\treturn isPositiveInteger(cap ?? 0) ? Math.min(model.contextWindow, cap as number) : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
@@ -11,10 +11,14 @@
11
11
  * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a
12
12
  * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).
13
13
  *
14
- * Atomic's current `contextWindow` drives local prompt collection, compaction thresholds, footer
15
- * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token
16
- * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field
17
- * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.
14
+ * Atomic shows the model's full context window for the selectable long tier (the
15
+ * `max_context_window_tokens` total, e.g. 1_000_000/1_050_000), matching how the native `openai/*`
16
+ * and `anthropic/*` providers advertise these models. Because GitHub enforces a lower server-side
17
+ * prompt cap (`max_prompt_tokens`, e.g. 936k/922k) below that total, the prompt cap is retained as
18
+ * an internal effective input budget (`CopilotModelContext.maxInputTokens`) that drives compaction
19
+ * thresholds and the overflow-recovery guard, so the branded total can be displayed without
20
+ * overrunning the server limit. The default (short) tier stays at the `default` billing tier's
21
+ * prompt budget.
18
22
  *
19
23
  * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers
20
24
  * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes
@@ -24,15 +28,24 @@
24
28
  /** Resolved input-token context window(s) for a single Copilot model. */
25
29
  export interface CopilotModelContext {
26
30
  /**
27
- * Base context window in INPUT tokens — shown in the footer and used for compaction. The
28
- * default tier's `context_max`, or the model-level `max_prompt_tokens` fallback otherwise.
31
+ * Base/displayed context window — shown in the footer. The default tier's `context_max`, or the
32
+ * model-level `max_prompt_tokens` fallback otherwise.
29
33
  */
30
34
  contextWindow: number;
31
35
  /**
32
- * Selectable input-token windows (`[default, long]`) when the model exposes a `long_context`
33
- * tier larger than its default; absent for single-window models.
36
+ * Selectable windows (`[default, long]`) when the model exposes a `long_context` tier larger than
37
+ * its default; absent for single-window models. The long entry is the model's full
38
+ * `max_context_window_tokens` (total capacity) when advertised, matching `openai/*` and
39
+ * `anthropic/*`.
34
40
  */
35
41
  contextWindowOptions?: readonly number[];
42
+ /**
43
+ * Hard prompt/input cap (`max_prompt_tokens`) when it sits below the displayed long window. Used
44
+ * as the effective input budget for compaction thresholds and overflow recovery so the branded
45
+ * total can be shown without overrunning GitHub's server-side prompt limit. Absent when the
46
+ * displayed window already equals the input cap.
47
+ */
48
+ maxInputTokens?: number;
36
49
  }
37
50
  /** Map of model id → resolved input-token context window(s). */
38
51
  export type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;
@@ -49,7 +62,7 @@ export declare const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.gith
49
62
  /** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */
50
63
  export declare const COPILOT_CATALOG_CACHE_TTL_MS: number;
51
64
  /** Current on-disk cache schema version. */
52
- export declare const COPILOT_CATALOG_CACHE_VERSION: 2;
65
+ export declare const COPILOT_CATALOG_CACHE_VERSION: 3;
53
66
  /**
54
67
  * Resolve the Copilot CAPI base URL.
55
68
  *
@@ -61,10 +74,12 @@ export declare const COPILOT_CATALOG_CACHE_VERSION: 2;
61
74
  export declare function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string;
62
75
  /** Raw token limits parsed from a CAPI model entry. */
63
76
  export interface CopilotModelLimits {
64
- /** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget. */
77
+ /** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget (the hard input cap). */
65
78
  maxPromptTokens?: number;
66
- /** `capabilities.limits.max_context_window_tokens` — total context capacity, used only as a fallback. */
79
+ /** `capabilities.limits.max_context_window_tokens` — total context capacity (the displayed long tier). */
67
80
  maxContextWindowTokens?: number;
81
+ /** `capabilities.limits.max_output_tokens` — output reserve; derives the input cap when `max_prompt_tokens` is absent. */
82
+ maxOutputTokens?: number;
68
83
  /** `billing.token_prices.default.context_max` — default-tier prompt threshold. */
69
84
  defaultContextMax?: number;
70
85
  /** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */
@@ -1 +1 @@
1
- {"version":3,"file":"copilot-model-catalog.d.ts","sourceRoot":"","sources":["../../src/core/copilot-model-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAKH,yEAAyE;AACzE,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACzC;AAED,gEAAgE;AAChE,MAAM,MAAM,mBAAmB,GAAG,WAAW,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;AAE3E,wGAAwG;AACxG,eAAO,MAAM,+BAA+B,SAAU,CAAC;AAEvD,eAAO,MAAM,2BAA2B,eAAe,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAMpE,CAAC;AAEF,kGAAkG;AAClG,eAAO,MAAM,4BAA4B,6CAA6C,CAAC;AAEvF,qFAAqF;AACrF,eAAO,MAAM,4BAA4B,QAAiB,CAAC;AAE3D,4CAA4C;AAC5C,eAAO,MAAM,6BAA6B,EAAG,CAAU,CAAC;AAExD;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,CASvG;AAcD,uDAAuD;AACvD,MAAM,WAAW,kBAAkB;IAClC,6EAA6E;IAC7E,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,yGAAyG;IACzG,sBAAs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AAO,EAAE,8BAA8B,GACrC,mBAAmB,GAAG,SAAS,CAsBjC;AAED,6FAA6F;AAC7F,wBAAgB,wBAAwB,CACvC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,mBAAmB,EAC5B,GAAG,CAAC,EAAE,MAAM,GACV,IAAI,CAaN;AAED,yFAAyF;AACzF,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED,0FAA0F;AAC1F,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;;;;;;;GASG;AACH,wBAAgB,sCAAsC,CACrD,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,SAAS,EAAE,MAAM,EACjB,GAAG,CAAC,EAAE,MAAM,GACV,OAAO,CAOT","sourcesContent":["/**\n * GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.\n *\n * GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:\n *\n * - `capabilities.limits.max_context_window_tokens` is the model's total context capacity\n * (prompt + completion reserve).\n * - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely\n * fill before the provider must reserve output tokens.\n * - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The\n * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a\n * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).\n *\n * Atomic's current `contextWindow` drives local prompt collection, compaction thresholds, footer\n * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token\n * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field\n * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.\n *\n * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers\n * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes\n * stale. Instead the catalog is fetched live (gated on the user actually having the GitHub Copilot\n * provider) and cached on disk for a short TTL, exactly like the Copilot CLI.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/** Resolved input-token context window(s) for a single Copilot model. */\nexport interface CopilotModelContext {\n\t/**\n\t * Base context window in INPUT tokens — shown in the footer and used for compaction. The\n\t * default tier's `context_max`, or the model-level `max_prompt_tokens` fallback otherwise.\n\t */\n\tcontextWindow: number;\n\t/**\n\t * Selectable input-token windows (`[default, long]`) when the model exposes a `long_context`\n\t * tier larger than its default; absent for single-window models.\n\t */\n\tcontextWindowOptions?: readonly number[];\n}\n\n/** Map of model id → resolved input-token context window(s). */\nexport type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;\n\n/** Safety fallback when a model reports neither `max_prompt_tokens` nor `max_context_window_tokens`. */\nexport const COPILOT_CONTEXT_WINDOW_FALLBACK = 128_000;\n\nexport const COPILOT_CATALOG_API_VERSION = \"2026-06-01\";\n\n/**\n * Headers GitHub's CAPI expects for catalog reads. Mirrors the editor headers pi-ai already sends\n * for Copilot token refresh and model-policy calls, plus the dated API version.\n */\nexport const COPILOT_CATALOG_HEADERS: Readonly<Record<string, string>> = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n\t\"X-GitHub-Api-Version\": COPILOT_CATALOG_API_VERSION,\n};\n\n/** Default (non-enterprise) Copilot CAPI base URL when the token has no resolvable `proxy-ep`. */\nexport const DEFAULT_COPILOT_API_BASE_URL = \"https://api.individual.githubcopilot.com\";\n\n/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */\nexport const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;\n\n/** Current on-disk cache schema version. */\nexport const COPILOT_CATALOG_CACHE_VERSION = 2 as const;\n\n/**\n * Resolve the Copilot CAPI base URL.\n *\n * Copilot access tokens embed a `proxy-ep=proxy.<host>` segment; the API host is the same host with\n * `proxy.` swapped for `api.`. Falls back to the enterprise host or the individual default. (pi-ai\n * exposes an equivalent helper, but its published `dist` mangles the export name, so the small,\n * stable parsing logic is reimplemented here.)\n */\nexport function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string {\n\tif (token) {\n\t\tconst match = token.match(/proxy-ep=([^;]+)/);\n\t\tif (match) {\n\t\t\treturn `https://${match[1].replace(/^proxy\\./, \"api.\")}`;\n\t\t}\n\t}\n\tif (enterpriseDomain) return `https://copilot-api.${enterpriseDomain}`;\n\treturn DEFAULT_COPILOT_API_BASE_URL;\n}\n\nfunction trimTrailingSlash(url: string): string {\n\treturn url.replace(/\\/+$/, \"\");\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n\treturn value && typeof value === \"object\" ? (value as Record<string, unknown>) : undefined;\n}\n\nfunction toPositiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\n/** Raw token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget. */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens` — total context capacity, used only as a fallback. */\n\tmaxContextWindowTokens?: number;\n\t/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */\n\tlongContextMax?: number;\n}\n\n/**\n * Resolve a model's input-token context window(s) from its CAPI limits.\n *\n * `contextWindow` is the model's base input budget — the default tier's `context_max` when tiered,\n * otherwise `max_prompt_tokens ?? max_context_window_tokens ?? 128_000`. A `long_context` tier that\n * is larger than the base adds a second selectable window. Returns `undefined` when the entry\n * carries no usable limit signal at all.\n */\nexport function resolveCopilotModelContext(limits: CopilotModelLimits): CopilotModelContext | undefined {\n\tconst hasSignal =\n\t\tlimits.maxPromptTokens !== undefined ||\n\t\tlimits.maxContextWindowTokens !== undefined ||\n\t\tlimits.defaultContextMax !== undefined ||\n\t\tlimits.longContextMax !== undefined;\n\tif (!hasSignal) return undefined;\n\n\tconst maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;\n\tconst base = limits.defaultContextMax ?? maxInput;\n\tif (limits.longContextMax !== undefined && limits.longContextMax > base) {\n\t\treturn { contextWindow: base, contextWindowOptions: [base, limits.longContextMax] };\n\t}\n\treturn { contextWindow: base };\n}\n\n/**\n * Parse a raw CAPI `/models` response body into an input-token context-window catalog.\n */\nexport function parseCopilotModelCatalog(body: unknown): CopilotModelCatalog {\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tconst data = asRecord(body)?.data;\n\tif (!Array.isArray(data)) return catalog;\n\n\tfor (const entry of data) {\n\t\tconst record = asRecord(entry);\n\t\tif (!record) continue;\n\t\tconst id = record.id;\n\t\tif (typeof id !== \"string\" || id.length === 0) continue;\n\n\t\tconst limits = asRecord(asRecord(record.capabilities)?.limits);\n\t\tconst prices = asRecord(asRecord(record.billing)?.token_prices);\n\t\tconst context = resolveCopilotModelContext({\n\t\t\tmaxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),\n\t\t\tmaxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),\n\t\t\tdefaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),\n\t\t\tlongContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),\n\t\t});\n\t\tif (context) catalog.set(id, context);\n\t}\n\n\treturn catalog;\n}\n\nexport interface FetchCopilotModelCatalogOptions {\n\t/** Valid Copilot CAPI bearer token (e.g. from `modelRegistry.getApiKeyForProvider`). */\n\ttoken: string;\n\t/** Override the resolved base URL; defaults to one derived from the token. */\n\tbaseUrl?: string;\n\t/** Enterprise domain, used for base-URL resolution when the token lacks a `proxy-ep`. */\n\tenterpriseDomain?: string;\n\t/** Extra/override request headers. */\n\theaders?: Record<string, string>;\n\t/** Injectable `fetch` for testing. */\n\tfetchImpl?: typeof fetch;\n\t/** Abort signal. */\n\tsignal?: AbortSignal;\n}\n\n/** Fetch and parse the live Copilot model catalog from CAPI `GET {baseUrl}/models`. */\nexport async function fetchCopilotModelCatalog(options: FetchCopilotModelCatalogOptions): Promise<CopilotModelCatalog> {\n\tconst fetchImpl = options.fetchImpl ?? fetch;\n\tconst baseUrl = options.baseUrl ?? copilotApiBaseUrlFromToken(options.token, options.enterpriseDomain);\n\tconst response = await fetchImpl(`${trimTrailingSlash(baseUrl)}/models`, {\n\t\tmethod: \"GET\",\n\t\theaders: {\n\t\t\tAccept: \"application/json\",\n\t\t\tAuthorization: `Bearer ${options.token}`,\n\t\t\t...COPILOT_CATALOG_HEADERS,\n\t\t\t...options.headers,\n\t\t},\n\t\t...(options.signal ? { signal: options.signal } : {}),\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot /models request failed: ${response.status} ${response.statusText}`);\n\t}\n\treturn parseCopilotModelCatalog(await response.json());\n}\n\n// ----------------------------------------------------------------------------\n// Active in-memory catalog (consulted by the model registry).\n//\n// Empty by default, so with no Copilot auth / no successful fetch the registry leaves Copilot\n// model context windows untouched and the picker never appears.\n// ----------------------------------------------------------------------------\n\nlet activeCatalog: CopilotModelCatalog = new Map();\n\n/** Replace the active catalog the registry derives context windows from. */\nexport function setActiveCopilotModelCatalog(catalog: CopilotModelCatalog): void {\n\tactiveCatalog = catalog;\n}\n\n/** The active catalog (empty until a successful auth-gated fetch/cache load). */\nexport function getActiveCopilotModelCatalog(): CopilotModelCatalog {\n\treturn activeCatalog;\n}\n\n/** Reset the active catalog (primarily for tests). */\nexport function clearActiveCopilotModelCatalog(): void {\n\tactiveCatalog = new Map();\n}\n\n// ----------------------------------------------------------------------------\n// Disk cache.\n// ----------------------------------------------------------------------------\n\ninterface CopilotCatalogCacheFile {\n\tversion: typeof COPILOT_CATALOG_CACHE_VERSION;\n\t/** CAPI host the catalog was fetched from; cache misses on host change (e.g. enterprise switch). */\n\thost: string;\n\t/** Epoch ms the catalog was fetched. */\n\tfetchedAt: number;\n\tmodels: Record<string, CopilotModelContext>;\n}\n\nfunction hostFromBaseUrl(baseUrl: string): string {\n\ttry {\n\t\treturn new URL(baseUrl).host;\n\t} catch {\n\t\treturn baseUrl;\n\t}\n}\n\nexport interface ReadCopilotCatalogCacheOptions {\n\t/** Expected CAPI host; a cached file from a different host is ignored. */\n\thost: string;\n\t/** Current epoch ms (injectable for tests). */\n\tnow?: number;\n\t/** Freshness window; defaults to {@link COPILOT_CATALOG_CACHE_TTL_MS}. */\n\tttlMs?: number;\n}\n\nfunction sanitizeCachedContext(value: unknown): CopilotModelContext | undefined {\n\tconst record = asRecord(value);\n\tconst contextWindow = toPositiveInt(record?.contextWindow);\n\tif (contextWindow === undefined) return undefined;\n\tconst rawOptions = record?.contextWindowOptions;\n\tif (Array.isArray(rawOptions)) {\n\t\tconst options = rawOptions.map(toPositiveInt).filter((n): n is number => n !== undefined);\n\t\tif (options.length > 1) return { contextWindow, contextWindowOptions: options };\n\t}\n\treturn { contextWindow };\n}\n\n/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */\nexport function readCopilotCatalogCache(\n\tpath: string,\n\toptions: ReadCopilotCatalogCacheOptions,\n): CopilotModelCatalog | undefined {\n\tlet parsed: CopilotCatalogCacheFile;\n\ttry {\n\t\tif (!existsSync(path)) return undefined;\n\t\tparsed = JSON.parse(readFileSync(path, \"utf8\")) as CopilotCatalogCacheFile;\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!parsed || parsed.version !== COPILOT_CATALOG_CACHE_VERSION) return undefined;\n\tif (parsed.host !== options.host) return undefined;\n\tconst now = options.now ?? Date.now();\n\tconst ttlMs = options.ttlMs ?? COPILOT_CATALOG_CACHE_TTL_MS;\n\tif (typeof parsed.fetchedAt !== \"number\" || now - parsed.fetchedAt >= ttlMs) return undefined;\n\tconst models = asRecord(parsed.models);\n\tif (!models) return undefined;\n\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tfor (const [id, value] of Object.entries(models)) {\n\t\tconst context = sanitizeCachedContext(value);\n\t\tif (context) catalog.set(id, context);\n\t}\n\treturn catalog;\n}\n\n/** Write the catalog to the cache file (creating parent dirs). Best-effort; never throws. */\nexport function writeCopilotCatalogCache(\n\tpath: string,\n\tbaseUrl: string,\n\tcatalog: CopilotModelCatalog,\n\tnow?: number,\n): void {\n\tconst payload: CopilotCatalogCacheFile = {\n\t\tversion: COPILOT_CATALOG_CACHE_VERSION,\n\t\thost: hostFromBaseUrl(baseUrl),\n\t\tfetchedAt: now ?? Date.now(),\n\t\tmodels: Object.fromEntries(catalog),\n\t};\n\ttry {\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\twriteFileSync(path, JSON.stringify(payload), \"utf8\");\n\t} catch {\n\t\t// best-effort cache; ignore write failures\n\t}\n}\n\n/** Host component of a base URL, for matching {@link readCopilotCatalogCache} `host`. */\nexport function copilotCatalogCacheHost(baseUrl: string): string {\n\treturn hostFromBaseUrl(baseUrl);\n}\n\n/** Standard on-disk cache path for the Copilot model catalog under an agent directory. */\nexport function copilotCatalogCachePath(agentDir: string): string {\n\treturn join(agentDir, \"cache\", \"copilot-models.json\");\n}\n\n/**\n * Seed the active catalog synchronously from the on-disk cache, gated on a Copilot access token.\n *\n * Called at model-registry construction so a returning user's previously selected long-context\n * window is recognized before startup validation runs — otherwise the persisted choice would warn\n * (\"context window 936k is not supported…\") and reset until the async refresh completes. The cache\n * TTL is intentionally ignored here: stale-but-present windows are still valid for selection, and\n * the async loader independently refetches on its own freshness window. Returns true when a catalog\n * was applied. No-op (returns false) without a token or a host-matching cached catalog.\n */\nexport function seedActiveCopilotModelCatalogFromCache(\n\taccessToken: string | undefined,\n\tcachePath: string,\n\tnow?: number,\n): boolean {\n\tif (typeof accessToken !== \"string\" || accessToken.length === 0) return false;\n\tconst host = copilotCatalogCacheHost(copilotApiBaseUrlFromToken(accessToken));\n\tconst cached = readCopilotCatalogCache(cachePath, { host, now, ttlMs: Number.POSITIVE_INFINITY });\n\tif (!cached) return false;\n\tsetActiveCopilotModelCatalog(cached);\n\treturn true;\n}\n"]}
1
+ {"version":3,"file":"copilot-model-catalog.d.ts","sourceRoot":"","sources":["../../src/core/copilot-model-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAKH,yEAAyE;AACzE,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,gEAAgE;AAChE,MAAM,MAAM,mBAAmB,GAAG,WAAW,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;AAE3E,wGAAwG;AACxG,eAAO,MAAM,+BAA+B,SAAU,CAAC;AAEvD,eAAO,MAAM,2BAA2B,eAAe,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAMpE,CAAC;AAEF,kGAAkG;AAClG,eAAO,MAAM,4BAA4B,6CAA6C,CAAC;AAEvF,qFAAqF;AACrF,eAAO,MAAM,4BAA4B,QAAiB,CAAC;AAE3D,4CAA4C;AAC5C,eAAO,MAAM,6BAA6B,EAAG,CAAU,CAAC;AAExD;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,CASvG;AAcD,uDAAuD;AACvD,MAAM,WAAW,kBAAkB;IAClC,kGAAkG;IAClG,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,0GAA0G;IAC1G,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,0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AI,EAAE,MAAM,EACZ,OAAO,EAAE,8BAA8B,GACrC,mBAAmB,GAAG,SAAS,CAsBjC;AAED,6FAA6F;AAC7F,wBAAgB,wBAAwB,CACvC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,mBAAmB,EAC5B,GAAG,CAAC,EAAE,MAAM,GACV,IAAI,CAaN;AAED,yFAAyF;AACzF,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED,0FAA0F;AAC1F,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;;;;;;;GASG;AACH,wBAAgB,sCAAsC,CACrD,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,SAAS,EAAE,MAAM,EACjB,GAAG,CAAC,EAAE,MAAM,GACV,OAAO,CAOT","sourcesContent":["/**\n * GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.\n *\n * GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:\n *\n * - `capabilities.limits.max_context_window_tokens` is the model's total context capacity\n * (prompt + completion reserve).\n * - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely\n * fill before the provider must reserve output tokens.\n * - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The\n * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a\n * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).\n *\n * Atomic shows the model's full context window for the selectable long tier (the\n * `max_context_window_tokens` total, e.g. 1_000_000/1_050_000), matching how the native `openai/*`\n * and `anthropic/*` providers advertise these models. Because GitHub enforces a lower server-side\n * prompt cap (`max_prompt_tokens`, e.g. 936k/922k) below that total, the prompt cap is retained as\n * an internal effective input budget (`CopilotModelContext.maxInputTokens`) that drives compaction\n * thresholds and the overflow-recovery guard, so the branded total can be displayed without\n * overrunning the server limit. The default (short) tier stays at the `default` billing tier's\n * prompt budget.\n *\n * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers\n * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes\n * stale. Instead the catalog is fetched live (gated on the user actually having the GitHub Copilot\n * provider) and cached on disk for a short TTL, exactly like the Copilot CLI.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/** Resolved input-token context window(s) for a single Copilot model. */\nexport interface CopilotModelContext {\n\t/**\n\t * Base/displayed context window — shown in the footer. The default tier's `context_max`, or the\n\t * model-level `max_prompt_tokens` fallback otherwise.\n\t */\n\tcontextWindow: number;\n\t/**\n\t * Selectable windows (`[default, long]`) when the model exposes a `long_context` tier larger than\n\t * its default; absent for single-window models. The long entry is the model's full\n\t * `max_context_window_tokens` (total capacity) when advertised, matching `openai/*` and\n\t * `anthropic/*`.\n\t */\n\tcontextWindowOptions?: readonly number[];\n\t/**\n\t * Hard prompt/input cap (`max_prompt_tokens`) when it sits below the displayed long window. Used\n\t * as the effective input budget for compaction thresholds and overflow recovery so the branded\n\t * total can be shown without overrunning GitHub's server-side prompt limit. Absent when the\n\t * displayed window already equals the input cap.\n\t */\n\tmaxInputTokens?: number;\n}\n\n/** Map of model id → resolved input-token context window(s). */\nexport type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;\n\n/** Safety fallback when a model reports neither `max_prompt_tokens` nor `max_context_window_tokens`. */\nexport const COPILOT_CONTEXT_WINDOW_FALLBACK = 128_000;\n\nexport const COPILOT_CATALOG_API_VERSION = \"2026-06-01\";\n\n/**\n * Headers GitHub's CAPI expects for catalog reads. Mirrors the editor headers pi-ai already sends\n * for Copilot token refresh and model-policy calls, plus the dated API version.\n */\nexport const COPILOT_CATALOG_HEADERS: Readonly<Record<string, string>> = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n\t\"X-GitHub-Api-Version\": COPILOT_CATALOG_API_VERSION,\n};\n\n/** Default (non-enterprise) Copilot CAPI base URL when the token has no resolvable `proxy-ep`. */\nexport const DEFAULT_COPILOT_API_BASE_URL = \"https://api.individual.githubcopilot.com\";\n\n/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */\nexport const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;\n\n/** Current on-disk cache schema version. */\nexport const COPILOT_CATALOG_CACHE_VERSION = 3 as const;\n\n/**\n * Resolve the Copilot CAPI base URL.\n *\n * Copilot access tokens embed a `proxy-ep=proxy.<host>` segment; the API host is the same host with\n * `proxy.` swapped for `api.`. Falls back to the enterprise host or the individual default. (pi-ai\n * exposes an equivalent helper, but its published `dist` mangles the export name, so the small,\n * stable parsing logic is reimplemented here.)\n */\nexport function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string {\n\tif (token) {\n\t\tconst match = token.match(/proxy-ep=([^;]+)/);\n\t\tif (match) {\n\t\t\treturn `https://${match[1].replace(/^proxy\\./, \"api.\")}`;\n\t\t}\n\t}\n\tif (enterpriseDomain) return `https://copilot-api.${enterpriseDomain}`;\n\treturn DEFAULT_COPILOT_API_BASE_URL;\n}\n\nfunction trimTrailingSlash(url: string): string {\n\treturn url.replace(/\\/+$/, \"\");\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n\treturn value && typeof value === \"object\" ? (value as Record<string, unknown>) : undefined;\n}\n\nfunction toPositiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\n/** Raw token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget (the hard input cap). */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens` — total context capacity (the displayed long tier). */\n\tmaxContextWindowTokens?: number;\n\t/** `capabilities.limits.max_output_tokens` — output reserve; derives the input cap when `max_prompt_tokens` is absent. */\n\tmaxOutputTokens?: number;\n\t/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */\n\tlongContextMax?: number;\n}\n\n/**\n * Resolve a model's input-token context window(s) from its CAPI limits.\n *\n * `contextWindow` is the model's base input budget — the default tier's `context_max` when tiered,\n * otherwise `max_prompt_tokens ?? max_context_window_tokens ?? 128_000`. A `long_context` tier that\n * is larger than the base adds a second selectable window. Returns `undefined` when the entry\n * carries no usable limit signal at all.\n */\nexport function resolveCopilotModelContext(limits: CopilotModelLimits): CopilotModelContext | undefined {\n\tconst hasSignal =\n\t\tlimits.maxPromptTokens !== undefined ||\n\t\tlimits.maxContextWindowTokens !== undefined ||\n\t\tlimits.defaultContextMax !== undefined ||\n\t\tlimits.longContextMax !== undefined;\n\tif (!hasSignal) return undefined;\n\n\tconst maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;\n\tconst base = limits.defaultContextMax ?? maxInput;\n\tif (limits.longContextMax !== undefined && limits.longContextMax > base) {\n\t\t// Display the model's full context window as the long tier (matching openai/* and anthropic/*)\n\t\t// when CAPI advertises it; otherwise fall back to the long-context prompt threshold for\n\t\t// older/sparse payloads.\n\t\tconst longWindow = limits.maxContextWindowTokens ?? limits.longContextMax;\n\t\t// The hard prompt/input cap GitHub enforces server-side: prefer max_prompt_tokens, else derive\n\t\t// it from total − output reserve, else fall back to the long-context prompt threshold.\n\t\tconst derivedInputCap =\n\t\t\tlimits.maxContextWindowTokens !== undefined && limits.maxOutputTokens !== undefined\n\t\t\t\t? limits.maxContextWindowTokens - limits.maxOutputTokens\n\t\t\t\t: undefined;\n\t\tconst inputCap =\n\t\t\tlimits.maxPromptTokens ??\n\t\t\t(derivedInputCap !== undefined && derivedInputCap > 0 ? derivedInputCap : undefined) ??\n\t\t\tlimits.longContextMax;\n\t\t// Only carry the cap when the displayed long window actually exceeds it (the branded-total\n\t\t// case); when they coincide there is no gap and the input budget is just the window.\n\t\treturn longWindow > inputCap\n\t\t\t? { contextWindow: base, contextWindowOptions: [base, longWindow], maxInputTokens: inputCap }\n\t\t\t: { contextWindow: base, contextWindowOptions: [base, longWindow] };\n\t}\n\treturn { contextWindow: base };\n}\n\n/**\n * Parse a raw CAPI `/models` response body into an input-token context-window catalog.\n */\nexport function parseCopilotModelCatalog(body: unknown): CopilotModelCatalog {\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tconst data = asRecord(body)?.data;\n\tif (!Array.isArray(data)) return catalog;\n\n\tfor (const entry of data) {\n\t\tconst record = asRecord(entry);\n\t\tif (!record) continue;\n\t\tconst id = record.id;\n\t\tif (typeof id !== \"string\" || id.length === 0) continue;\n\n\t\tconst limits = asRecord(asRecord(record.capabilities)?.limits);\n\t\tconst prices = asRecord(asRecord(record.billing)?.token_prices);\n\t\tconst context = resolveCopilotModelContext({\n\t\t\tmaxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),\n\t\t\tmaxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),\n\t\t\tmaxOutputTokens: toPositiveInt(limits?.max_output_tokens),\n\t\t\tdefaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),\n\t\t\tlongContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),\n\t\t});\n\t\tif (context) catalog.set(id, context);\n\t}\n\n\treturn catalog;\n}\n\nexport interface FetchCopilotModelCatalogOptions {\n\t/** Valid Copilot CAPI bearer token (e.g. from `modelRegistry.getApiKeyForProvider`). */\n\ttoken: string;\n\t/** Override the resolved base URL; defaults to one derived from the token. */\n\tbaseUrl?: string;\n\t/** Enterprise domain, used for base-URL resolution when the token lacks a `proxy-ep`. */\n\tenterpriseDomain?: string;\n\t/** Extra/override request headers. */\n\theaders?: Record<string, string>;\n\t/** Injectable `fetch` for testing. */\n\tfetchImpl?: typeof fetch;\n\t/** Abort signal. */\n\tsignal?: AbortSignal;\n}\n\n/** Fetch and parse the live Copilot model catalog from CAPI `GET {baseUrl}/models`. */\nexport async function fetchCopilotModelCatalog(options: FetchCopilotModelCatalogOptions): Promise<CopilotModelCatalog> {\n\tconst fetchImpl = options.fetchImpl ?? fetch;\n\tconst baseUrl = options.baseUrl ?? copilotApiBaseUrlFromToken(options.token, options.enterpriseDomain);\n\tconst response = await fetchImpl(`${trimTrailingSlash(baseUrl)}/models`, {\n\t\tmethod: \"GET\",\n\t\theaders: {\n\t\t\tAccept: \"application/json\",\n\t\t\tAuthorization: `Bearer ${options.token}`,\n\t\t\t...COPILOT_CATALOG_HEADERS,\n\t\t\t...options.headers,\n\t\t},\n\t\t...(options.signal ? { signal: options.signal } : {}),\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot /models request failed: ${response.status} ${response.statusText}`);\n\t}\n\treturn parseCopilotModelCatalog(await response.json());\n}\n\n// ----------------------------------------------------------------------------\n// Active in-memory catalog (consulted by the model registry).\n//\n// Empty by default, so with no Copilot auth / no successful fetch the registry leaves Copilot\n// model context windows untouched and the picker never appears.\n// ----------------------------------------------------------------------------\n\nlet activeCatalog: CopilotModelCatalog = new Map();\n\n/** Replace the active catalog the registry derives context windows from. */\nexport function setActiveCopilotModelCatalog(catalog: CopilotModelCatalog): void {\n\tactiveCatalog = catalog;\n}\n\n/** The active catalog (empty until a successful auth-gated fetch/cache load). */\nexport function getActiveCopilotModelCatalog(): CopilotModelCatalog {\n\treturn activeCatalog;\n}\n\n/** Reset the active catalog (primarily for tests). */\nexport function clearActiveCopilotModelCatalog(): void {\n\tactiveCatalog = new Map();\n}\n\n// ----------------------------------------------------------------------------\n// Disk cache.\n// ----------------------------------------------------------------------------\n\ninterface CopilotCatalogCacheFile {\n\tversion: typeof COPILOT_CATALOG_CACHE_VERSION;\n\t/** CAPI host the catalog was fetched from; cache misses on host change (e.g. enterprise switch). */\n\thost: string;\n\t/** Epoch ms the catalog was fetched. */\n\tfetchedAt: number;\n\tmodels: Record<string, CopilotModelContext>;\n}\n\nfunction hostFromBaseUrl(baseUrl: string): string {\n\ttry {\n\t\treturn new URL(baseUrl).host;\n\t} catch {\n\t\treturn baseUrl;\n\t}\n}\n\nexport interface ReadCopilotCatalogCacheOptions {\n\t/** Expected CAPI host; a cached file from a different host is ignored. */\n\thost: string;\n\t/** Current epoch ms (injectable for tests). */\n\tnow?: number;\n\t/** Freshness window; defaults to {@link COPILOT_CATALOG_CACHE_TTL_MS}. */\n\tttlMs?: number;\n}\n\nfunction sanitizeCachedContext(value: unknown): CopilotModelContext | undefined {\n\tconst record = asRecord(value);\n\tconst contextWindow = toPositiveInt(record?.contextWindow);\n\tif (contextWindow === undefined) return undefined;\n\tconst maxInputTokens = toPositiveInt(record?.maxInputTokens);\n\tconst rawOptions = record?.contextWindowOptions;\n\tif (Array.isArray(rawOptions)) {\n\t\tconst options = rawOptions.map(toPositiveInt).filter((n): n is number => n !== undefined);\n\t\tif (options.length > 1) {\n\t\t\treturn maxInputTokens !== undefined\n\t\t\t\t? { contextWindow, contextWindowOptions: options, maxInputTokens }\n\t\t\t\t: { contextWindow, contextWindowOptions: options };\n\t\t}\n\t}\n\treturn maxInputTokens !== undefined ? { contextWindow, maxInputTokens } : { contextWindow };\n}\n\n/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */\nexport function readCopilotCatalogCache(\n\tpath: string,\n\toptions: ReadCopilotCatalogCacheOptions,\n): CopilotModelCatalog | undefined {\n\tlet parsed: CopilotCatalogCacheFile;\n\ttry {\n\t\tif (!existsSync(path)) return undefined;\n\t\tparsed = JSON.parse(readFileSync(path, \"utf8\")) as CopilotCatalogCacheFile;\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!parsed || parsed.version !== COPILOT_CATALOG_CACHE_VERSION) return undefined;\n\tif (parsed.host !== options.host) return undefined;\n\tconst now = options.now ?? Date.now();\n\tconst ttlMs = options.ttlMs ?? COPILOT_CATALOG_CACHE_TTL_MS;\n\tif (typeof parsed.fetchedAt !== \"number\" || now - parsed.fetchedAt >= ttlMs) return undefined;\n\tconst models = asRecord(parsed.models);\n\tif (!models) return undefined;\n\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tfor (const [id, value] of Object.entries(models)) {\n\t\tconst context = sanitizeCachedContext(value);\n\t\tif (context) catalog.set(id, context);\n\t}\n\treturn catalog;\n}\n\n/** Write the catalog to the cache file (creating parent dirs). Best-effort; never throws. */\nexport function writeCopilotCatalogCache(\n\tpath: string,\n\tbaseUrl: string,\n\tcatalog: CopilotModelCatalog,\n\tnow?: number,\n): void {\n\tconst payload: CopilotCatalogCacheFile = {\n\t\tversion: COPILOT_CATALOG_CACHE_VERSION,\n\t\thost: hostFromBaseUrl(baseUrl),\n\t\tfetchedAt: now ?? Date.now(),\n\t\tmodels: Object.fromEntries(catalog),\n\t};\n\ttry {\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\twriteFileSync(path, JSON.stringify(payload), \"utf8\");\n\t} catch {\n\t\t// best-effort cache; ignore write failures\n\t}\n}\n\n/** Host component of a base URL, for matching {@link readCopilotCatalogCache} `host`. */\nexport function copilotCatalogCacheHost(baseUrl: string): string {\n\treturn hostFromBaseUrl(baseUrl);\n}\n\n/** Standard on-disk cache path for the Copilot model catalog under an agent directory. */\nexport function copilotCatalogCachePath(agentDir: string): string {\n\treturn join(agentDir, \"cache\", \"copilot-models.json\");\n}\n\n/**\n * Seed the active catalog synchronously from the on-disk cache, gated on a Copilot access token.\n *\n * Called at model-registry construction so a returning user's previously selected long-context\n * window is recognized before startup validation runs — otherwise the persisted choice would warn\n * (\"context window 936k is not supported…\") and reset until the async refresh completes. The cache\n * TTL is intentionally ignored here: stale-but-present windows are still valid for selection, and\n * the async loader independently refetches on its own freshness window. Returns true when a catalog\n * was applied. No-op (returns false) without a token or a host-matching cached catalog.\n */\nexport function seedActiveCopilotModelCatalogFromCache(\n\taccessToken: string | undefined,\n\tcachePath: string,\n\tnow?: number,\n): boolean {\n\tif (typeof accessToken !== \"string\" || accessToken.length === 0) return false;\n\tconst host = copilotCatalogCacheHost(copilotApiBaseUrlFromToken(accessToken));\n\tconst cached = readCopilotCatalogCache(cachePath, { host, now, ttlMs: Number.POSITIVE_INFINITY });\n\tif (!cached) return false;\n\tsetActiveCopilotModelCatalog(cached);\n\treturn true;\n}\n"]}
@@ -11,10 +11,14 @@
11
11
  * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a
12
12
  * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).
13
13
  *
14
- * Atomic's current `contextWindow` drives local prompt collection, compaction thresholds, footer
15
- * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token
16
- * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field
17
- * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.
14
+ * Atomic shows the model's full context window for the selectable long tier (the
15
+ * `max_context_window_tokens` total, e.g. 1_000_000/1_050_000), matching how the native `openai/*`
16
+ * and `anthropic/*` providers advertise these models. Because GitHub enforces a lower server-side
17
+ * prompt cap (`max_prompt_tokens`, e.g. 936k/922k) below that total, the prompt cap is retained as
18
+ * an internal effective input budget (`CopilotModelContext.maxInputTokens`) that drives compaction
19
+ * thresholds and the overflow-recovery guard, so the branded total can be displayed without
20
+ * overrunning the server limit. The default (short) tier stays at the `default` billing tier's
21
+ * prompt budget.
18
22
  *
19
23
  * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers
20
24
  * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes
@@ -42,7 +46,7 @@ export const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.githubcopilo
42
46
  /** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */
43
47
  export const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;
44
48
  /** Current on-disk cache schema version. */
45
- export const COPILOT_CATALOG_CACHE_VERSION = 2;
49
+ export const COPILOT_CATALOG_CACHE_VERSION = 3;
46
50
  /**
47
51
  * Resolve the Copilot CAPI base URL.
48
52
  *
@@ -89,7 +93,23 @@ export function resolveCopilotModelContext(limits) {
89
93
  const maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;
90
94
  const base = limits.defaultContextMax ?? maxInput;
91
95
  if (limits.longContextMax !== undefined && limits.longContextMax > base) {
92
- return { contextWindow: base, contextWindowOptions: [base, limits.longContextMax] };
96
+ // Display the model's full context window as the long tier (matching openai/* and anthropic/*)
97
+ // when CAPI advertises it; otherwise fall back to the long-context prompt threshold for
98
+ // older/sparse payloads.
99
+ const longWindow = limits.maxContextWindowTokens ?? limits.longContextMax;
100
+ // The hard prompt/input cap GitHub enforces server-side: prefer max_prompt_tokens, else derive
101
+ // it from total − output reserve, else fall back to the long-context prompt threshold.
102
+ const derivedInputCap = limits.maxContextWindowTokens !== undefined && limits.maxOutputTokens !== undefined
103
+ ? limits.maxContextWindowTokens - limits.maxOutputTokens
104
+ : undefined;
105
+ const inputCap = limits.maxPromptTokens ??
106
+ (derivedInputCap !== undefined && derivedInputCap > 0 ? derivedInputCap : undefined) ??
107
+ limits.longContextMax;
108
+ // Only carry the cap when the displayed long window actually exceeds it (the branded-total
109
+ // case); when they coincide there is no gap and the input budget is just the window.
110
+ return longWindow > inputCap
111
+ ? { contextWindow: base, contextWindowOptions: [base, longWindow], maxInputTokens: inputCap }
112
+ : { contextWindow: base, contextWindowOptions: [base, longWindow] };
93
113
  }
94
114
  return { contextWindow: base };
95
115
  }
@@ -113,6 +133,7 @@ export function parseCopilotModelCatalog(body) {
113
133
  const context = resolveCopilotModelContext({
114
134
  maxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),
115
135
  maxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),
136
+ maxOutputTokens: toPositiveInt(limits?.max_output_tokens),
116
137
  defaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),
117
138
  longContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),
118
139
  });
@@ -172,13 +193,17 @@ function sanitizeCachedContext(value) {
172
193
  const contextWindow = toPositiveInt(record?.contextWindow);
173
194
  if (contextWindow === undefined)
174
195
  return undefined;
196
+ const maxInputTokens = toPositiveInt(record?.maxInputTokens);
175
197
  const rawOptions = record?.contextWindowOptions;
176
198
  if (Array.isArray(rawOptions)) {
177
199
  const options = rawOptions.map(toPositiveInt).filter((n) => n !== undefined);
178
- if (options.length > 1)
179
- return { contextWindow, contextWindowOptions: options };
200
+ if (options.length > 1) {
201
+ return maxInputTokens !== undefined
202
+ ? { contextWindow, contextWindowOptions: options, maxInputTokens }
203
+ : { contextWindow, contextWindowOptions: options };
204
+ }
180
205
  }
181
- return { contextWindow };
206
+ return maxInputTokens !== undefined ? { contextWindow, maxInputTokens } : { contextWindow };
182
207
  }
183
208
  /** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */
184
209
  export function readCopilotCatalogCache(path, options) {
@@ -1 +1 @@
1
- {"version":3,"file":"copilot-model-catalog.js","sourceRoot":"","sources":["../../src/core/copilot-model-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAmB1C,wGAAwG;AACxG,MAAM,CAAC,MAAM,+BAA+B,GAAG,OAAO,CAAC;AAEvD,MAAM,CAAC,MAAM,2BAA2B,GAAG,YAAY,CAAC;AAExD;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAqC;IACxE,YAAY,EAAE,0BAA0B;IACxC,gBAAgB,EAAE,gBAAgB;IAClC,uBAAuB,EAAE,qBAAqB;IAC9C,wBAAwB,EAAE,a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,GAAG,uBAAuB,CAAC,0BAA0B,CAAC,WAAW,CAAC,CAAC,CAAC;IAC9E,MAAM,MAAM,GAAG,uBAAuB,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAClG,IAAI,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAC1B,4BAA4B,CAAC,MAAM,CAAC,CAAC;IACrC,OAAO,IAAI,CAAC;AACb,CAAC","sourcesContent":["/**\n * GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.\n *\n * GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:\n *\n * - `capabilities.limits.max_context_window_tokens` is the model's total context capacity\n * (prompt + completion reserve).\n * - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely\n * fill before the provider must reserve output tokens.\n * - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The\n * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a\n * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).\n *\n * Atomic's current `contextWindow` drives local prompt collection, compaction thresholds, footer\n * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token\n * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field\n * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.\n *\n * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers\n * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes\n * stale. Instead the catalog is fetched live (gated on the user actually having the GitHub Copilot\n * provider) and cached on disk for a short TTL, exactly like the Copilot CLI.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/** Resolved input-token context window(s) for a single Copilot model. */\nexport interface CopilotModelContext {\n\t/**\n\t * Base context window in INPUT tokens — shown in the footer and used for compaction. The\n\t * default tier's `context_max`, or the model-level `max_prompt_tokens` fallback otherwise.\n\t */\n\tcontextWindow: number;\n\t/**\n\t * Selectable input-token windows (`[default, long]`) when the model exposes a `long_context`\n\t * tier larger than its default; absent for single-window models.\n\t */\n\tcontextWindowOptions?: readonly number[];\n}\n\n/** Map of model id → resolved input-token context window(s). */\nexport type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;\n\n/** Safety fallback when a model reports neither `max_prompt_tokens` nor `max_context_window_tokens`. */\nexport const COPILOT_CONTEXT_WINDOW_FALLBACK = 128_000;\n\nexport const COPILOT_CATALOG_API_VERSION = \"2026-06-01\";\n\n/**\n * Headers GitHub's CAPI expects for catalog reads. Mirrors the editor headers pi-ai already sends\n * for Copilot token refresh and model-policy calls, plus the dated API version.\n */\nexport const COPILOT_CATALOG_HEADERS: Readonly<Record<string, string>> = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n\t\"X-GitHub-Api-Version\": COPILOT_CATALOG_API_VERSION,\n};\n\n/** Default (non-enterprise) Copilot CAPI base URL when the token has no resolvable `proxy-ep`. */\nexport const DEFAULT_COPILOT_API_BASE_URL = \"https://api.individual.githubcopilot.com\";\n\n/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */\nexport const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;\n\n/** Current on-disk cache schema version. */\nexport const COPILOT_CATALOG_CACHE_VERSION = 2 as const;\n\n/**\n * Resolve the Copilot CAPI base URL.\n *\n * Copilot access tokens embed a `proxy-ep=proxy.<host>` segment; the API host is the same host with\n * `proxy.` swapped for `api.`. Falls back to the enterprise host or the individual default. (pi-ai\n * exposes an equivalent helper, but its published `dist` mangles the export name, so the small,\n * stable parsing logic is reimplemented here.)\n */\nexport function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string {\n\tif (token) {\n\t\tconst match = token.match(/proxy-ep=([^;]+)/);\n\t\tif (match) {\n\t\t\treturn `https://${match[1].replace(/^proxy\\./, \"api.\")}`;\n\t\t}\n\t}\n\tif (enterpriseDomain) return `https://copilot-api.${enterpriseDomain}`;\n\treturn DEFAULT_COPILOT_API_BASE_URL;\n}\n\nfunction trimTrailingSlash(url: string): string {\n\treturn url.replace(/\\/+$/, \"\");\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n\treturn value && typeof value === \"object\" ? (value as Record<string, unknown>) : undefined;\n}\n\nfunction toPositiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\n/** Raw token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget. */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens` — total context capacity, used only as a fallback. */\n\tmaxContextWindowTokens?: number;\n\t/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */\n\tlongContextMax?: number;\n}\n\n/**\n * Resolve a model's input-token context window(s) from its CAPI limits.\n *\n * `contextWindow` is the model's base input budget — the default tier's `context_max` when tiered,\n * otherwise `max_prompt_tokens ?? max_context_window_tokens ?? 128_000`. A `long_context` tier that\n * is larger than the base adds a second selectable window. Returns `undefined` when the entry\n * carries no usable limit signal at all.\n */\nexport function resolveCopilotModelContext(limits: CopilotModelLimits): CopilotModelContext | undefined {\n\tconst hasSignal =\n\t\tlimits.maxPromptTokens !== undefined ||\n\t\tlimits.maxContextWindowTokens !== undefined ||\n\t\tlimits.defaultContextMax !== undefined ||\n\t\tlimits.longContextMax !== undefined;\n\tif (!hasSignal) return undefined;\n\n\tconst maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;\n\tconst base = limits.defaultContextMax ?? maxInput;\n\tif (limits.longContextMax !== undefined && limits.longContextMax > base) {\n\t\treturn { contextWindow: base, contextWindowOptions: [base, limits.longContextMax] };\n\t}\n\treturn { contextWindow: base };\n}\n\n/**\n * Parse a raw CAPI `/models` response body into an input-token context-window catalog.\n */\nexport function parseCopilotModelCatalog(body: unknown): CopilotModelCatalog {\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tconst data = asRecord(body)?.data;\n\tif (!Array.isArray(data)) return catalog;\n\n\tfor (const entry of data) {\n\t\tconst record = asRecord(entry);\n\t\tif (!record) continue;\n\t\tconst id = record.id;\n\t\tif (typeof id !== \"string\" || id.length === 0) continue;\n\n\t\tconst limits = asRecord(asRecord(record.capabilities)?.limits);\n\t\tconst prices = asRecord(asRecord(record.billing)?.token_prices);\n\t\tconst context = resolveCopilotModelContext({\n\t\t\tmaxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),\n\t\t\tmaxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),\n\t\t\tdefaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),\n\t\t\tlongContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),\n\t\t});\n\t\tif (context) catalog.set(id, context);\n\t}\n\n\treturn catalog;\n}\n\nexport interface FetchCopilotModelCatalogOptions {\n\t/** Valid Copilot CAPI bearer token (e.g. from `modelRegistry.getApiKeyForProvider`). */\n\ttoken: string;\n\t/** Override the resolved base URL; defaults to one derived from the token. */\n\tbaseUrl?: string;\n\t/** Enterprise domain, used for base-URL resolution when the token lacks a `proxy-ep`. */\n\tenterpriseDomain?: string;\n\t/** Extra/override request headers. */\n\theaders?: Record<string, string>;\n\t/** Injectable `fetch` for testing. */\n\tfetchImpl?: typeof fetch;\n\t/** Abort signal. */\n\tsignal?: AbortSignal;\n}\n\n/** Fetch and parse the live Copilot model catalog from CAPI `GET {baseUrl}/models`. */\nexport async function fetchCopilotModelCatalog(options: FetchCopilotModelCatalogOptions): Promise<CopilotModelCatalog> {\n\tconst fetchImpl = options.fetchImpl ?? fetch;\n\tconst baseUrl = options.baseUrl ?? copilotApiBaseUrlFromToken(options.token, options.enterpriseDomain);\n\tconst response = await fetchImpl(`${trimTrailingSlash(baseUrl)}/models`, {\n\t\tmethod: \"GET\",\n\t\theaders: {\n\t\t\tAccept: \"application/json\",\n\t\t\tAuthorization: `Bearer ${options.token}`,\n\t\t\t...COPILOT_CATALOG_HEADERS,\n\t\t\t...options.headers,\n\t\t},\n\t\t...(options.signal ? { signal: options.signal } : {}),\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot /models request failed: ${response.status} ${response.statusText}`);\n\t}\n\treturn parseCopilotModelCatalog(await response.json());\n}\n\n// ----------------------------------------------------------------------------\n// Active in-memory catalog (consulted by the model registry).\n//\n// Empty by default, so with no Copilot auth / no successful fetch the registry leaves Copilot\n// model context windows untouched and the picker never appears.\n// ----------------------------------------------------------------------------\n\nlet activeCatalog: CopilotModelCatalog = new Map();\n\n/** Replace the active catalog the registry derives context windows from. */\nexport function setActiveCopilotModelCatalog(catalog: CopilotModelCatalog): void {\n\tactiveCatalog = catalog;\n}\n\n/** The active catalog (empty until a successful auth-gated fetch/cache load). */\nexport function getActiveCopilotModelCatalog(): CopilotModelCatalog {\n\treturn activeCatalog;\n}\n\n/** Reset the active catalog (primarily for tests). */\nexport function clearActiveCopilotModelCatalog(): void {\n\tactiveCatalog = new Map();\n}\n\n// ----------------------------------------------------------------------------\n// Disk cache.\n// ----------------------------------------------------------------------------\n\ninterface CopilotCatalogCacheFile {\n\tversion: typeof COPILOT_CATALOG_CACHE_VERSION;\n\t/** CAPI host the catalog was fetched from; cache misses on host change (e.g. enterprise switch). */\n\thost: string;\n\t/** Epoch ms the catalog was fetched. */\n\tfetchedAt: number;\n\tmodels: Record<string, CopilotModelContext>;\n}\n\nfunction hostFromBaseUrl(baseUrl: string): string {\n\ttry {\n\t\treturn new URL(baseUrl).host;\n\t} catch {\n\t\treturn baseUrl;\n\t}\n}\n\nexport interface ReadCopilotCatalogCacheOptions {\n\t/** Expected CAPI host; a cached file from a different host is ignored. */\n\thost: string;\n\t/** Current epoch ms (injectable for tests). */\n\tnow?: number;\n\t/** Freshness window; defaults to {@link COPILOT_CATALOG_CACHE_TTL_MS}. */\n\tttlMs?: number;\n}\n\nfunction sanitizeCachedContext(value: unknown): CopilotModelContext | undefined {\n\tconst record = asRecord(value);\n\tconst contextWindow = toPositiveInt(record?.contextWindow);\n\tif (contextWindow === undefined) return undefined;\n\tconst rawOptions = record?.contextWindowOptions;\n\tif (Array.isArray(rawOptions)) {\n\t\tconst options = rawOptions.map(toPositiveInt).filter((n): n is number => n !== undefined);\n\t\tif (options.length > 1) return { contextWindow, contextWindowOptions: options };\n\t}\n\treturn { contextWindow };\n}\n\n/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */\nexport function readCopilotCatalogCache(\n\tpath: string,\n\toptions: ReadCopilotCatalogCacheOptions,\n): CopilotModelCatalog | undefined {\n\tlet parsed: CopilotCatalogCacheFile;\n\ttry {\n\t\tif (!existsSync(path)) return undefined;\n\t\tparsed = JSON.parse(readFileSync(path, \"utf8\")) as CopilotCatalogCacheFile;\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!parsed || parsed.version !== COPILOT_CATALOG_CACHE_VERSION) return undefined;\n\tif (parsed.host !== options.host) return undefined;\n\tconst now = options.now ?? Date.now();\n\tconst ttlMs = options.ttlMs ?? COPILOT_CATALOG_CACHE_TTL_MS;\n\tif (typeof parsed.fetchedAt !== \"number\" || now - parsed.fetchedAt >= ttlMs) return undefined;\n\tconst models = asRecord(parsed.models);\n\tif (!models) return undefined;\n\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tfor (const [id, value] of Object.entries(models)) {\n\t\tconst context = sanitizeCachedContext(value);\n\t\tif (context) catalog.set(id, context);\n\t}\n\treturn catalog;\n}\n\n/** Write the catalog to the cache file (creating parent dirs). Best-effort; never throws. */\nexport function writeCopilotCatalogCache(\n\tpath: string,\n\tbaseUrl: string,\n\tcatalog: CopilotModelCatalog,\n\tnow?: number,\n): void {\n\tconst payload: CopilotCatalogCacheFile = {\n\t\tversion: COPILOT_CATALOG_CACHE_VERSION,\n\t\thost: hostFromBaseUrl(baseUrl),\n\t\tfetchedAt: now ?? Date.now(),\n\t\tmodels: Object.fromEntries(catalog),\n\t};\n\ttry {\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\twriteFileSync(path, JSON.stringify(payload), \"utf8\");\n\t} catch {\n\t\t// best-effort cache; ignore write failures\n\t}\n}\n\n/** Host component of a base URL, for matching {@link readCopilotCatalogCache} `host`. */\nexport function copilotCatalogCacheHost(baseUrl: string): string {\n\treturn hostFromBaseUrl(baseUrl);\n}\n\n/** Standard on-disk cache path for the Copilot model catalog under an agent directory. */\nexport function copilotCatalogCachePath(agentDir: string): string {\n\treturn join(agentDir, \"cache\", \"copilot-models.json\");\n}\n\n/**\n * Seed the active catalog synchronously from the on-disk cache, gated on a Copilot access token.\n *\n * Called at model-registry construction so a returning user's previously selected long-context\n * window is recognized before startup validation runs — otherwise the persisted choice would warn\n * (\"context window 936k is not supported…\") and reset until the async refresh completes. The cache\n * TTL is intentionally ignored here: stale-but-present windows are still valid for selection, and\n * the async loader independently refetches on its own freshness window. Returns true when a catalog\n * was applied. No-op (returns false) without a token or a host-matching cached catalog.\n */\nexport function seedActiveCopilotModelCatalogFromCache(\n\taccessToken: string | undefined,\n\tcachePath: string,\n\tnow?: number,\n): boolean {\n\tif (typeof accessToken !== \"string\" || accessToken.length === 0) return false;\n\tconst host = copilotCatalogCacheHost(copilotApiBaseUrlFromToken(accessToken));\n\tconst cached = readCopilotCatalogCache(cachePath, { host, now, ttlMs: Number.POSITIVE_INFINITY });\n\tif (!cached) return false;\n\tsetActiveCopilotModelCatalog(cached);\n\treturn true;\n}\n"]}
1
+ {"version":3,"file":"copilot-model-catalog.js","sourceRoot":"","sources":["../../src/core/copilot-model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AAuB,CAAC,QAAgB;IACvD,OAAO,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,qBAAqB,CAAC,CAAC;AACvD,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,sCAAsC,CACrD,WAA+B,EAC/B,SAAiB,EACjB,GAAY;IAEZ,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC9E,MAAM,IAAI,GAAG,uBAAuB,CAAC,0BAA0B,CAAC,WAAW,CAAC,CAAC,CAAC;IAC9E,MAAM,MAAM,GAAG,uBAAuB,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAClG,IAAI,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAC1B,4BAA4B,CAAC,MAAM,CAAC,CAAC;IACrC,OAAO,IAAI,CAAC;AACb,CAAC","sourcesContent":["/**\n * GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.\n *\n * GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:\n *\n * - `capabilities.limits.max_context_window_tokens` is the model's total context capacity\n * (prompt + completion reserve).\n * - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely\n * fill before the provider must reserve output tokens.\n * - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The\n * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a\n * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).\n *\n * Atomic shows the model's full context window for the selectable long tier (the\n * `max_context_window_tokens` total, e.g. 1_000_000/1_050_000), matching how the native `openai/*`\n * and `anthropic/*` providers advertise these models. Because GitHub enforces a lower server-side\n * prompt cap (`max_prompt_tokens`, e.g. 936k/922k) below that total, the prompt cap is retained as\n * an internal effective input budget (`CopilotModelContext.maxInputTokens`) that drives compaction\n * thresholds and the overflow-recovery guard, so the branded total can be displayed without\n * overrunning the server limit. The default (short) tier stays at the `default` billing tier's\n * prompt budget.\n *\n * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers\n * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes\n * stale. Instead the catalog is fetched live (gated on the user actually having the GitHub Copilot\n * provider) and cached on disk for a short TTL, exactly like the Copilot CLI.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/** Resolved input-token context window(s) for a single Copilot model. */\nexport interface CopilotModelContext {\n\t/**\n\t * Base/displayed context window — shown in the footer. The default tier's `context_max`, or the\n\t * model-level `max_prompt_tokens` fallback otherwise.\n\t */\n\tcontextWindow: number;\n\t/**\n\t * Selectable windows (`[default, long]`) when the model exposes a `long_context` tier larger than\n\t * its default; absent for single-window models. The long entry is the model's full\n\t * `max_context_window_tokens` (total capacity) when advertised, matching `openai/*` and\n\t * `anthropic/*`.\n\t */\n\tcontextWindowOptions?: readonly number[];\n\t/**\n\t * Hard prompt/input cap (`max_prompt_tokens`) when it sits below the displayed long window. Used\n\t * as the effective input budget for compaction thresholds and overflow recovery so the branded\n\t * total can be shown without overrunning GitHub's server-side prompt limit. Absent when the\n\t * displayed window already equals the input cap.\n\t */\n\tmaxInputTokens?: number;\n}\n\n/** Map of model id → resolved input-token context window(s). */\nexport type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;\n\n/** Safety fallback when a model reports neither `max_prompt_tokens` nor `max_context_window_tokens`. */\nexport const COPILOT_CONTEXT_WINDOW_FALLBACK = 128_000;\n\nexport const COPILOT_CATALOG_API_VERSION = \"2026-06-01\";\n\n/**\n * Headers GitHub's CAPI expects for catalog reads. Mirrors the editor headers pi-ai already sends\n * for Copilot token refresh and model-policy calls, plus the dated API version.\n */\nexport const COPILOT_CATALOG_HEADERS: Readonly<Record<string, string>> = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n\t\"X-GitHub-Api-Version\": COPILOT_CATALOG_API_VERSION,\n};\n\n/** Default (non-enterprise) Copilot CAPI base URL when the token has no resolvable `proxy-ep`. */\nexport const DEFAULT_COPILOT_API_BASE_URL = \"https://api.individual.githubcopilot.com\";\n\n/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */\nexport const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;\n\n/** Current on-disk cache schema version. */\nexport const COPILOT_CATALOG_CACHE_VERSION = 3 as const;\n\n/**\n * Resolve the Copilot CAPI base URL.\n *\n * Copilot access tokens embed a `proxy-ep=proxy.<host>` segment; the API host is the same host with\n * `proxy.` swapped for `api.`. Falls back to the enterprise host or the individual default. (pi-ai\n * exposes an equivalent helper, but its published `dist` mangles the export name, so the small,\n * stable parsing logic is reimplemented here.)\n */\nexport function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string {\n\tif (token) {\n\t\tconst match = token.match(/proxy-ep=([^;]+)/);\n\t\tif (match) {\n\t\t\treturn `https://${match[1].replace(/^proxy\\./, \"api.\")}`;\n\t\t}\n\t}\n\tif (enterpriseDomain) return `https://copilot-api.${enterpriseDomain}`;\n\treturn DEFAULT_COPILOT_API_BASE_URL;\n}\n\nfunction trimTrailingSlash(url: string): string {\n\treturn url.replace(/\\/+$/, \"\");\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n\treturn value && typeof value === \"object\" ? (value as Record<string, unknown>) : undefined;\n}\n\nfunction toPositiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\n/** Raw token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget (the hard input cap). */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens` — total context capacity (the displayed long tier). */\n\tmaxContextWindowTokens?: number;\n\t/** `capabilities.limits.max_output_tokens` — output reserve; derives the input cap when `max_prompt_tokens` is absent. */\n\tmaxOutputTokens?: number;\n\t/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */\n\tlongContextMax?: number;\n}\n\n/**\n * Resolve a model's input-token context window(s) from its CAPI limits.\n *\n * `contextWindow` is the model's base input budget — the default tier's `context_max` when tiered,\n * otherwise `max_prompt_tokens ?? max_context_window_tokens ?? 128_000`. A `long_context` tier that\n * is larger than the base adds a second selectable window. Returns `undefined` when the entry\n * carries no usable limit signal at all.\n */\nexport function resolveCopilotModelContext(limits: CopilotModelLimits): CopilotModelContext | undefined {\n\tconst hasSignal =\n\t\tlimits.maxPromptTokens !== undefined ||\n\t\tlimits.maxContextWindowTokens !== undefined ||\n\t\tlimits.defaultContextMax !== undefined ||\n\t\tlimits.longContextMax !== undefined;\n\tif (!hasSignal) return undefined;\n\n\tconst maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;\n\tconst base = limits.defaultContextMax ?? maxInput;\n\tif (limits.longContextMax !== undefined && limits.longContextMax > base) {\n\t\t// Display the model's full context window as the long tier (matching openai/* and anthropic/*)\n\t\t// when CAPI advertises it; otherwise fall back to the long-context prompt threshold for\n\t\t// older/sparse payloads.\n\t\tconst longWindow = limits.maxContextWindowTokens ?? limits.longContextMax;\n\t\t// The hard prompt/input cap GitHub enforces server-side: prefer max_prompt_tokens, else derive\n\t\t// it from total − output reserve, else fall back to the long-context prompt threshold.\n\t\tconst derivedInputCap =\n\t\t\tlimits.maxContextWindowTokens !== undefined && limits.maxOutputTokens !== undefined\n\t\t\t\t? limits.maxContextWindowTokens - limits.maxOutputTokens\n\t\t\t\t: undefined;\n\t\tconst inputCap =\n\t\t\tlimits.maxPromptTokens ??\n\t\t\t(derivedInputCap !== undefined && derivedInputCap > 0 ? derivedInputCap : undefined) ??\n\t\t\tlimits.longContextMax;\n\t\t// Only carry the cap when the displayed long window actually exceeds it (the branded-total\n\t\t// case); when they coincide there is no gap and the input budget is just the window.\n\t\treturn longWindow > inputCap\n\t\t\t? { contextWindow: base, contextWindowOptions: [base, longWindow], maxInputTokens: inputCap }\n\t\t\t: { contextWindow: base, contextWindowOptions: [base, longWindow] };\n\t}\n\treturn { contextWindow: base };\n}\n\n/**\n * Parse a raw CAPI `/models` response body into an input-token context-window catalog.\n */\nexport function parseCopilotModelCatalog(body: unknown): CopilotModelCatalog {\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tconst data = asRecord(body)?.data;\n\tif (!Array.isArray(data)) return catalog;\n\n\tfor (const entry of data) {\n\t\tconst record = asRecord(entry);\n\t\tif (!record) continue;\n\t\tconst id = record.id;\n\t\tif (typeof id !== \"string\" || id.length === 0) continue;\n\n\t\tconst limits = asRecord(asRecord(record.capabilities)?.limits);\n\t\tconst prices = asRecord(asRecord(record.billing)?.token_prices);\n\t\tconst context = resolveCopilotModelContext({\n\t\t\tmaxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),\n\t\t\tmaxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),\n\t\t\tmaxOutputTokens: toPositiveInt(limits?.max_output_tokens),\n\t\t\tdefaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),\n\t\t\tlongContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),\n\t\t});\n\t\tif (context) catalog.set(id, context);\n\t}\n\n\treturn catalog;\n}\n\nexport interface FetchCopilotModelCatalogOptions {\n\t/** Valid Copilot CAPI bearer token (e.g. from `modelRegistry.getApiKeyForProvider`). */\n\ttoken: string;\n\t/** Override the resolved base URL; defaults to one derived from the token. */\n\tbaseUrl?: string;\n\t/** Enterprise domain, used for base-URL resolution when the token lacks a `proxy-ep`. */\n\tenterpriseDomain?: string;\n\t/** Extra/override request headers. */\n\theaders?: Record<string, string>;\n\t/** Injectable `fetch` for testing. */\n\tfetchImpl?: typeof fetch;\n\t/** Abort signal. */\n\tsignal?: AbortSignal;\n}\n\n/** Fetch and parse the live Copilot model catalog from CAPI `GET {baseUrl}/models`. */\nexport async function fetchCopilotModelCatalog(options: FetchCopilotModelCatalogOptions): Promise<CopilotModelCatalog> {\n\tconst fetchImpl = options.fetchImpl ?? fetch;\n\tconst baseUrl = options.baseUrl ?? copilotApiBaseUrlFromToken(options.token, options.enterpriseDomain);\n\tconst response = await fetchImpl(`${trimTrailingSlash(baseUrl)}/models`, {\n\t\tmethod: \"GET\",\n\t\theaders: {\n\t\t\tAccept: \"application/json\",\n\t\t\tAuthorization: `Bearer ${options.token}`,\n\t\t\t...COPILOT_CATALOG_HEADERS,\n\t\t\t...options.headers,\n\t\t},\n\t\t...(options.signal ? { signal: options.signal } : {}),\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot /models request failed: ${response.status} ${response.statusText}`);\n\t}\n\treturn parseCopilotModelCatalog(await response.json());\n}\n\n// ----------------------------------------------------------------------------\n// Active in-memory catalog (consulted by the model registry).\n//\n// Empty by default, so with no Copilot auth / no successful fetch the registry leaves Copilot\n// model context windows untouched and the picker never appears.\n// ----------------------------------------------------------------------------\n\nlet activeCatalog: CopilotModelCatalog = new Map();\n\n/** Replace the active catalog the registry derives context windows from. */\nexport function setActiveCopilotModelCatalog(catalog: CopilotModelCatalog): void {\n\tactiveCatalog = catalog;\n}\n\n/** The active catalog (empty until a successful auth-gated fetch/cache load). */\nexport function getActiveCopilotModelCatalog(): CopilotModelCatalog {\n\treturn activeCatalog;\n}\n\n/** Reset the active catalog (primarily for tests). */\nexport function clearActiveCopilotModelCatalog(): void {\n\tactiveCatalog = new Map();\n}\n\n// ----------------------------------------------------------------------------\n// Disk cache.\n// ----------------------------------------------------------------------------\n\ninterface CopilotCatalogCacheFile {\n\tversion: typeof COPILOT_CATALOG_CACHE_VERSION;\n\t/** CAPI host the catalog was fetched from; cache misses on host change (e.g. enterprise switch). */\n\thost: string;\n\t/** Epoch ms the catalog was fetched. */\n\tfetchedAt: number;\n\tmodels: Record<string, CopilotModelContext>;\n}\n\nfunction hostFromBaseUrl(baseUrl: string): string {\n\ttry {\n\t\treturn new URL(baseUrl).host;\n\t} catch {\n\t\treturn baseUrl;\n\t}\n}\n\nexport interface ReadCopilotCatalogCacheOptions {\n\t/** Expected CAPI host; a cached file from a different host is ignored. */\n\thost: string;\n\t/** Current epoch ms (injectable for tests). */\n\tnow?: number;\n\t/** Freshness window; defaults to {@link COPILOT_CATALOG_CACHE_TTL_MS}. */\n\tttlMs?: number;\n}\n\nfunction sanitizeCachedContext(value: unknown): CopilotModelContext | undefined {\n\tconst record = asRecord(value);\n\tconst contextWindow = toPositiveInt(record?.contextWindow);\n\tif (contextWindow === undefined) return undefined;\n\tconst maxInputTokens = toPositiveInt(record?.maxInputTokens);\n\tconst rawOptions = record?.contextWindowOptions;\n\tif (Array.isArray(rawOptions)) {\n\t\tconst options = rawOptions.map(toPositiveInt).filter((n): n is number => n !== undefined);\n\t\tif (options.length > 1) {\n\t\t\treturn maxInputTokens !== undefined\n\t\t\t\t? { contextWindow, contextWindowOptions: options, maxInputTokens }\n\t\t\t\t: { contextWindow, contextWindowOptions: options };\n\t\t}\n\t}\n\treturn maxInputTokens !== undefined ? { contextWindow, maxInputTokens } : { contextWindow };\n}\n\n/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */\nexport function readCopilotCatalogCache(\n\tpath: string,\n\toptions: ReadCopilotCatalogCacheOptions,\n): CopilotModelCatalog | undefined {\n\tlet parsed: CopilotCatalogCacheFile;\n\ttry {\n\t\tif (!existsSync(path)) return undefined;\n\t\tparsed = JSON.parse(readFileSync(path, \"utf8\")) as CopilotCatalogCacheFile;\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!parsed || parsed.version !== COPILOT_CATALOG_CACHE_VERSION) return undefined;\n\tif (parsed.host !== options.host) return undefined;\n\tconst now = options.now ?? Date.now();\n\tconst ttlMs = options.ttlMs ?? COPILOT_CATALOG_CACHE_TTL_MS;\n\tif (typeof parsed.fetchedAt !== \"number\" || now - parsed.fetchedAt >= ttlMs) return undefined;\n\tconst models = asRecord(parsed.models);\n\tif (!models) return undefined;\n\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tfor (const [id, value] of Object.entries(models)) {\n\t\tconst context = sanitizeCachedContext(value);\n\t\tif (context) catalog.set(id, context);\n\t}\n\treturn catalog;\n}\n\n/** Write the catalog to the cache file (creating parent dirs). Best-effort; never throws. */\nexport function writeCopilotCatalogCache(\n\tpath: string,\n\tbaseUrl: string,\n\tcatalog: CopilotModelCatalog,\n\tnow?: number,\n): void {\n\tconst payload: CopilotCatalogCacheFile = {\n\t\tversion: COPILOT_CATALOG_CACHE_VERSION,\n\t\thost: hostFromBaseUrl(baseUrl),\n\t\tfetchedAt: now ?? Date.now(),\n\t\tmodels: Object.fromEntries(catalog),\n\t};\n\ttry {\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\twriteFileSync(path, JSON.stringify(payload), \"utf8\");\n\t} catch {\n\t\t// best-effort cache; ignore write failures\n\t}\n}\n\n/** Host component of a base URL, for matching {@link readCopilotCatalogCache} `host`. */\nexport function copilotCatalogCacheHost(baseUrl: string): string {\n\treturn hostFromBaseUrl(baseUrl);\n}\n\n/** Standard on-disk cache path for the Copilot model catalog under an agent directory. */\nexport function copilotCatalogCachePath(agentDir: string): string {\n\treturn join(agentDir, \"cache\", \"copilot-models.json\");\n}\n\n/**\n * Seed the active catalog synchronously from the on-disk cache, gated on a Copilot access token.\n *\n * Called at model-registry construction so a returning user's previously selected long-context\n * window is recognized before startup validation runs — otherwise the persisted choice would warn\n * (\"context window 936k is not supported…\") and reset until the async refresh completes. The cache\n * TTL is intentionally ignored here: stale-but-present windows are still valid for selection, and\n * the async loader independently refetches on its own freshness window. Returns true when a catalog\n * was applied. No-op (returns false) without a token or a host-matching cached catalog.\n */\nexport function seedActiveCopilotModelCatalogFromCache(\n\taccessToken: string | undefined,\n\tcachePath: string,\n\tnow?: number,\n): boolean {\n\tif (typeof accessToken !== \"string\" || accessToken.length === 0) return false;\n\tconst host = copilotCatalogCacheHost(copilotApiBaseUrlFromToken(accessToken));\n\tconst cached = readCopilotCatalogCache(cachePath, { host, now, ttlMs: Number.POSITIVE_INFINITY });\n\tif (!cached) return false;\n\tsetActiveCopilotModelCatalog(cached);\n\treturn true;\n}\n"]}