@bastani/atomic 0.8.31-alpha.1 → 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.
- package/CHANGELOG.md +17 -5
- package/README.md +12 -10
- package/dist/builtin/cursor/CHANGELOG.md +1 -1
- package/dist/builtin/cursor/package.json +2 -2
- package/dist/builtin/intercom/CHANGELOG.md +1 -1
- package/dist/builtin/intercom/package.json +2 -2
- package/dist/builtin/mcp/CHANGELOG.md +1 -1
- package/dist/builtin/mcp/package.json +3 -3
- package/dist/builtin/subagents/CHANGELOG.md +10 -1
- package/dist/builtin/subagents/agents/codebase-online-researcher.md +8 -8
- package/dist/builtin/subagents/agents/debugger.md +6 -6
- package/dist/builtin/subagents/package.json +4 -4
- package/dist/builtin/subagents/skills/effective-liteparse/SKILL.md +118 -0
- package/dist/builtin/subagents/skills/effective-liteparse/scripts/search.py +128 -0
- package/dist/builtin/subagents/skills/playwright-cli/SKILL.md +404 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/element-attributes.md +23 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/playwright-tests.md +39 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/request-mocking.md +87 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/running-code.md +241 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/session-management.md +225 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/spec-driven-testing.md +305 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/storage-state.md +275 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/test-generation.md +134 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/tracing.md +139 -0
- package/dist/builtin/subagents/skills/playwright-cli/references/video-recording.md +143 -0
- package/dist/builtin/web-access/CHANGELOG.md +1 -1
- package/dist/builtin/web-access/package.json +2 -2
- package/dist/builtin/workflows/CHANGELOG.md +7 -1
- package/dist/builtin/workflows/README.md +4 -4
- package/dist/builtin/workflows/builtin/open-claude-design.ts +59 -56
- package/dist/builtin/workflows/builtin/ralph.ts +56 -3
- package/dist/builtin/workflows/builtin/shared-prompts.ts +1 -1
- package/dist/builtin/workflows/package.json +2 -2
- package/dist/builtin/workflows/skills/research-codebase/SKILL.md +1 -1
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +1 -1
- package/dist/cli/args.js.map +1 -1
- package/dist/core/agent-session.d.ts +1 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +49 -21
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/context-window.d.ts +26 -1
- package/dist/core/context-window.d.ts.map +1 -1
- package/dist/core/context-window.js +30 -6
- package/dist/core/context-window.js.map +1 -1
- package/dist/core/copilot-model-catalog.d.ts +39 -21
- package/dist/core/copilot-model-catalog.d.ts.map +1 -1
- package/dist/core/copilot-model-catalog.js +44 -16
- package/dist/core/copilot-model-catalog.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +6 -4
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/project-trust.d.ts.map +1 -1
- package/dist/core/project-trust.js +2 -1
- package/dist/core/project-trust.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +18 -7
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/settings-manager.d.ts +11 -2
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +62 -8
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +1 -0
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/edit-diff.d.ts +1 -2
- package/dist/core/tools/edit-diff.d.ts.map +1 -1
- package/dist/core/tools/edit-diff.js +1 -2
- package/dist/core/tools/edit-diff.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/config-selector.js +5 -7
- package/dist/modes/interactive/components/config-selector.js.map +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +2 -1
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/scoped-models-selector.js +4 -1
- package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +2 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +165 -15
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/tree-selector.js +44 -4
- package/dist/modes/interactive/components/tree-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +24 -54
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/model-search.d.ts +7 -0
- package/dist/modes/interactive/model-search.d.ts.map +1 -0
- package/dist/modes/interactive/model-search.js +6 -0
- package/dist/modes/interactive/model-search.js.map +1 -0
- package/dist/modes/interactive/theme/theme-controller.d.ts +30 -0
- package/dist/modes/interactive/theme/theme-controller.d.ts.map +1 -0
- package/dist/modes/interactive/theme/theme-controller.js +108 -0
- package/dist/modes/interactive/theme/theme-controller.js.map +1 -0
- package/dist/modes/interactive/theme/theme-schema.json +2 -1
- package/dist/modes/interactive/theme/theme.d.ts +5 -0
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +70 -29
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +1 -1
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +1 -1
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +1 -1
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/package-manager-cli.d.ts.map +1 -1
- package/dist/package-manager-cli.js +39 -9
- package/dist/package-manager-cli.js.map +1 -1
- package/docs/extensions.md +21 -0
- package/docs/models.md +3 -3
- package/docs/packages.md +13 -9
- package/docs/providers.md +3 -3
- package/docs/quickstart.md +14 -0
- package/docs/rpc.md +3 -3
- package/docs/sdk.md +15 -11
- package/docs/session-format.md +1 -1
- package/docs/settings.md +8 -3
- package/docs/themes.md +3 -1
- package/docs/tui.md +1 -1
- package/docs/usage.md +12 -9
- package/docs/workflows.md +9 -7
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/preset.ts +10 -4
- package/examples/extensions/provider-payload.ts +5 -5
- package/examples/extensions/sandbox/index.ts +2 -2
- package/examples/extensions/sandbox/package-lock.json +3 -3
- package/examples/extensions/sandbox/package.json +2 -2
- package/examples/extensions/subagent/agents.ts +2 -2
- package/examples/extensions/subagent/index.ts +4 -2
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/package.json +5 -5
- package/dist/builtin/subagents/skills/browser/EXAMPLES.md +0 -151
- package/dist/builtin/subagents/skills/browser/LICENSE.txt +0 -21
- package/dist/builtin/subagents/skills/browser/REFERENCE.md +0 -451
- package/dist/builtin/subagents/skills/browser/SKILL.md +0 -170
|
@@ -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 {
|
|
@@ -18,12 +25,30 @@ export interface ContextWindowSelection<TApi extends Api = Api> {
|
|
|
18
25
|
export interface ContextWindowSelectionError {
|
|
19
26
|
error: string;
|
|
20
27
|
}
|
|
28
|
+
export interface ContextWindowSelectionOptions {
|
|
29
|
+
/**
|
|
30
|
+
* GitHub Copilot advertises some long-context tiers below their branded 1M size
|
|
31
|
+
* (for example 936k or 922k input tokens). When enabled, a request above an
|
|
32
|
+
* advertised long tier selects the largest supported Copilot window not
|
|
33
|
+
* exceeding the request, but never silently falls back to the model's base
|
|
34
|
+
* window.
|
|
35
|
+
*/
|
|
36
|
+
allowCopilotLongContextFallback?: boolean;
|
|
37
|
+
}
|
|
21
38
|
export declare function validateContextWindowValue(value: number): string | undefined;
|
|
22
39
|
export declare function parseContextWindowValue(input: string): ContextWindowParseResult;
|
|
23
40
|
export declare function formatContextWindow(value: number): string;
|
|
24
41
|
export declare function normalizeContextWindowOptions(values: readonly number[] | undefined): number[];
|
|
25
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;
|
|
26
51
|
export declare function getSupportedContextWindows(model: Model<Api>): number[];
|
|
27
52
|
export declare function withContextWindowOptions<TApi extends Api>(model: Model<TApi>, contextWindowOptions: readonly number[]): Model<TApi>;
|
|
28
|
-
export declare function selectContextWindow<TApi extends Api>(model: Model<TApi>, contextWindow: number): ContextWindowSelection<TApi> | ContextWindowSelectionError;
|
|
53
|
+
export declare function selectContextWindow<TApi extends Api>(model: Model<TApi>, contextWindow: number, options?: ContextWindowSelectionOptions): ContextWindowSelection<TApi> | ContextWindowSelectionError;
|
|
29
54
|
//# sourceMappingURL=context-window.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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;IACvC,UAAU,KAAK,CAAC,IAAI,SAAS,GAAG;QAC/B,oHAAoH;QACpH,oBAAoB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;QACzC,wGAAwG;QACxG,oBAAoB,CAAC,EAAE,MAAM,CAAC;
|
|
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;IACvC,UAAU,KAAK,CAAC,IAAI,SAAS,GAAG;QAC/B,oHAAoH;QACpH,oBAAoB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;QACzC,wGAAwG;QACxG,oBAAoB,CAAC,EAAE,MAAM,CAAC;QAC9B;;;;;WAKG;QACH,cAAc,CAAC,EAA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AAK,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
|
}
|
|
@@ -62,13 +73,26 @@ export function withContextWindowOptions(model, contextWindowOptions) {
|
|
|
62
73
|
contextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),
|
|
63
74
|
};
|
|
64
75
|
}
|
|
65
|
-
|
|
76
|
+
function resolveSelectableContextWindow(model, requestedContextWindow, supported, options) {
|
|
77
|
+
if (supported.includes(requestedContextWindow)) {
|
|
78
|
+
return requestedContextWindow;
|
|
79
|
+
}
|
|
80
|
+
if (options.allowCopilotLongContextFallback !== true || model.provider !== "github-copilot") {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
const defaultContextWindow = getModelDefaultContextWindow(model);
|
|
84
|
+
const candidates = supported.filter((contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow);
|
|
85
|
+
return candidates.length > 0 ? Math.max(...candidates) : undefined;
|
|
86
|
+
}
|
|
87
|
+
export function selectContextWindow(model, contextWindow, options = {}) {
|
|
66
88
|
const validationError = validateContextWindowValue(contextWindow);
|
|
67
89
|
if (validationError) {
|
|
68
90
|
return { error: validationError };
|
|
69
91
|
}
|
|
70
|
-
const
|
|
71
|
-
|
|
92
|
+
const apiModel = model;
|
|
93
|
+
const supported = getSupportedContextWindows(apiModel);
|
|
94
|
+
const selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);
|
|
95
|
+
if (selectedContextWindow === undefined) {
|
|
72
96
|
return {
|
|
73
97
|
error: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(", ")}.`,
|
|
74
98
|
};
|
|
@@ -76,11 +100,11 @@ export function selectContextWindow(model, contextWindow) {
|
|
|
76
100
|
return {
|
|
77
101
|
model: {
|
|
78
102
|
...model,
|
|
79
|
-
defaultContextWindow: getModelDefaultContextWindow(
|
|
80
|
-
contextWindow,
|
|
103
|
+
defaultContextWindow: getModelDefaultContextWindow(apiModel),
|
|
104
|
+
contextWindow: selectedContextWindow,
|
|
81
105
|
contextWindowOptions: supported,
|
|
82
106
|
},
|
|
83
|
-
contextWindow,
|
|
107
|
+
contextWindow: selectedContextWindow,
|
|
84
108
|
};
|
|
85
109
|
}
|
|
86
110
|
//# sourceMappingURL=context-window.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context-window.js","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AAyBA,MAAM,oBAAoB,GAA2B;IACpD,CAAC,EAAE,KAAK;IACR,CAAC,EAAE,SAAS;CACZ,CAAC;AAEF,SAA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CAAC,CAAC,KAAK,CAAC,aAAa,CAAC;AAC/G,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,KAAiB;IAC3D,OAAO,6BAA6B,CAAC,CAAC,4BAA4B,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,oBAAoB,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;AACpH,CAAC;AAED,MAAM,UAAU,wBAAwB,CACvC,KAAkB,EAClB,oBAAuC;IAEvC,OAAO;QACN,GAAG,KAAK;QACR,oBAAoB,EAAE,4BAA4B,CAAC,KAAmB,CAAC;QACvE,oBAAoB,EAAE,6BAA6B,CAAC,oBAAoB,CAAC;KACzE,CAAC;AACH,CAAC;AAED,MAAM,UAAU,mBAAmB,CAClC,KAAkB,EAClB,aAAqB;IAErB,MAAM,eAAe,GAAG,0BAA0B,CAAC,aAAa,CAAC,CAAC;IAClE,IAAI,eAAe,EAAE,CAAC;QACrB,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,CAAC;IACnC,CAAC;IAED,MAAM,SAAS,GAAG,0BAA0B,CAAC,KAAmB,CAAC,CAAC;IAClE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,aAAa,CAAC,EAAE,CAAC;QACxC,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,KAAmB,CAAC;YACvE,aAAa;YACb,oBAAoB,EAAE,SAAS;SAC/B;QACD,aAAa;KACb,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\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\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst supported = getSupportedContextWindows(model as Model<Api>);\n\tif (!supported.includes(contextWindow)) {\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(model as Model<Api>),\n\t\t\tcontextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow,\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"context-window.js","sour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-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"]}
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* GitHub Copilot model catalog (CAPI) — dynamic
|
|
2
|
+
* GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.
|
|
3
3
|
*
|
|
4
|
-
* GitHub's Copilot API (CAPI) exposes
|
|
5
|
-
* Atomic shows for a Copilot model is measured in INPUT (prompt) tokens, exactly like every other
|
|
6
|
-
* provider's `contextWindow`, so GitHub models read consistently across the UI:
|
|
4
|
+
* GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:
|
|
7
5
|
*
|
|
8
|
-
* -
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* (e.g. gpt-5.5 272k, Claude 200k); a
|
|
14
|
-
*
|
|
6
|
+
* - `capabilities.limits.max_context_window_tokens` is the model's total context capacity
|
|
7
|
+
* (prompt + completion reserve).
|
|
8
|
+
* - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely
|
|
9
|
+
* fill before the provider must reserve output tokens.
|
|
10
|
+
* - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The
|
|
11
|
+
* `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a
|
|
12
|
+
* `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).
|
|
13
|
+
*
|
|
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.
|
|
15
22
|
*
|
|
16
23
|
* This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers
|
|
17
24
|
* windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes
|
|
@@ -21,15 +28,24 @@
|
|
|
21
28
|
/** Resolved input-token context window(s) for a single Copilot model. */
|
|
22
29
|
export interface CopilotModelContext {
|
|
23
30
|
/**
|
|
24
|
-
* Base context window
|
|
25
|
-
*
|
|
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.
|
|
26
33
|
*/
|
|
27
34
|
contextWindow: number;
|
|
28
35
|
/**
|
|
29
|
-
* Selectable
|
|
30
|
-
*
|
|
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/*`.
|
|
31
40
|
*/
|
|
32
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;
|
|
33
49
|
}
|
|
34
50
|
/** Map of model id → resolved input-token context window(s). */
|
|
35
51
|
export type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;
|
|
@@ -46,7 +62,7 @@ export declare const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.gith
|
|
|
46
62
|
/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */
|
|
47
63
|
export declare const COPILOT_CATALOG_CACHE_TTL_MS: number;
|
|
48
64
|
/** Current on-disk cache schema version. */
|
|
49
|
-
export declare const COPILOT_CATALOG_CACHE_VERSION:
|
|
65
|
+
export declare const COPILOT_CATALOG_CACHE_VERSION: 3;
|
|
50
66
|
/**
|
|
51
67
|
* Resolve the Copilot CAPI base URL.
|
|
52
68
|
*
|
|
@@ -56,15 +72,17 @@ export declare const COPILOT_CATALOG_CACHE_VERSION: 2;
|
|
|
56
72
|
* stable parsing logic is reimplemented here.)
|
|
57
73
|
*/
|
|
58
74
|
export declare function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string;
|
|
59
|
-
/** Raw
|
|
75
|
+
/** Raw token limits parsed from a CAPI model entry. */
|
|
60
76
|
export interface CopilotModelLimits {
|
|
61
|
-
/** `capabilities.limits.max_prompt_tokens
|
|
77
|
+
/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget (the hard input cap). */
|
|
62
78
|
maxPromptTokens?: number;
|
|
63
|
-
/** `capabilities.limits.max_context_window_tokens
|
|
79
|
+
/** `capabilities.limits.max_context_window_tokens` — total context capacity (the displayed long tier). */
|
|
64
80
|
maxContextWindowTokens?: number;
|
|
65
|
-
/** `
|
|
81
|
+
/** `capabilities.limits.max_output_tokens` — output reserve; derives the input cap when `max_prompt_tokens` is absent. */
|
|
82
|
+
maxOutputTokens?: number;
|
|
83
|
+
/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */
|
|
66
84
|
defaultContextMax?: number;
|
|
67
|
-
/** `billing.token_prices.long_context.context_max
|
|
85
|
+
/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */
|
|
68
86
|
longContextMax?: number;
|
|
69
87
|
}
|
|
70
88
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copilot-model-catalog.d.ts","sourceRoot":"","sources":["../../src/core/copilot-model-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAKH,yEAAyE;AACzE,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,aAAa,E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MAAM,GAAG,SAAS,EAC/B,SAAS,EAAE,MAAM,EACjB,GAAG,CAAC,EAAE,MAAM,GACV,OAAO,CAOT","sourcesContent":["/**\n * GitHub Copilot model catalog (CAPI) — dynamic, input-token context windows.\n *\n * GitHub's Copilot API (CAPI) exposes per-model limits via `GET {baseUrl}/models`. Every window\n * Atomic shows for a Copilot model is measured in INPUT (prompt) tokens, exactly like every other\n * provider's `contextWindow`, so GitHub models read consistently across the UI:\n *\n * - A model's input budget = `capabilities.limits.max_prompt_tokens`\n * ?? `capabilities.limits.max_context_window_tokens`\n * ?? 128_000 (the two fallbacks are safeties real CAPI entries never hit).\n * - Models with tiered pricing expose per-tier input budgets via\n * `billing.token_prices.<tier>.context_max`. The `default` tier is the base context window\n * (e.g. gpt-5.5 272k, Claude 200k); a `long_context` tier adds a selectable larger input\n * window (e.g. gpt-5.5 922k, Claude 936k) the user can switch to via the `/model` picker.\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 input-token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens`. */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens`. */\n\tmaxContextWindowTokens?: number;\n\t/** `billing.token_prices.default.context_max`. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max`. */\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,CA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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"]}
|
|
@@ -1,17 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* GitHub Copilot model catalog (CAPI) — dynamic
|
|
2
|
+
* GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.
|
|
3
3
|
*
|
|
4
|
-
* GitHub's Copilot API (CAPI) exposes
|
|
5
|
-
* Atomic shows for a Copilot model is measured in INPUT (prompt) tokens, exactly like every other
|
|
6
|
-
* provider's `contextWindow`, so GitHub models read consistently across the UI:
|
|
4
|
+
* GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:
|
|
7
5
|
*
|
|
8
|
-
* -
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* (e.g. gpt-5.5 272k, Claude 200k); a
|
|
14
|
-
*
|
|
6
|
+
* - `capabilities.limits.max_context_window_tokens` is the model's total context capacity
|
|
7
|
+
* (prompt + completion reserve).
|
|
8
|
+
* - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely
|
|
9
|
+
* fill before the provider must reserve output tokens.
|
|
10
|
+
* - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The
|
|
11
|
+
* `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a
|
|
12
|
+
* `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).
|
|
13
|
+
*
|
|
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.
|
|
15
22
|
*
|
|
16
23
|
* This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers
|
|
17
24
|
* windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes
|
|
@@ -39,7 +46,7 @@ export const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.githubcopilo
|
|
|
39
46
|
/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */
|
|
40
47
|
export const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;
|
|
41
48
|
/** Current on-disk cache schema version. */
|
|
42
|
-
export const COPILOT_CATALOG_CACHE_VERSION =
|
|
49
|
+
export const COPILOT_CATALOG_CACHE_VERSION = 3;
|
|
43
50
|
/**
|
|
44
51
|
* Resolve the Copilot CAPI base URL.
|
|
45
52
|
*
|
|
@@ -86,7 +93,23 @@ export function resolveCopilotModelContext(limits) {
|
|
|
86
93
|
const maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;
|
|
87
94
|
const base = limits.defaultContextMax ?? maxInput;
|
|
88
95
|
if (limits.longContextMax !== undefined && limits.longContextMax > base) {
|
|
89
|
-
|
|
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] };
|
|
90
113
|
}
|
|
91
114
|
return { contextWindow: base };
|
|
92
115
|
}
|
|
@@ -110,6 +133,7 @@ export function parseCopilotModelCatalog(body) {
|
|
|
110
133
|
const context = resolveCopilotModelContext({
|
|
111
134
|
maxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),
|
|
112
135
|
maxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),
|
|
136
|
+
maxOutputTokens: toPositiveInt(limits?.max_output_tokens),
|
|
113
137
|
defaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),
|
|
114
138
|
longContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),
|
|
115
139
|
});
|
|
@@ -169,13 +193,17 @@ function sanitizeCachedContext(value) {
|
|
|
169
193
|
const contextWindow = toPositiveInt(record?.contextWindow);
|
|
170
194
|
if (contextWindow === undefined)
|
|
171
195
|
return undefined;
|
|
196
|
+
const maxInputTokens = toPositiveInt(record?.maxInputTokens);
|
|
172
197
|
const rawOptions = record?.contextWindowOptions;
|
|
173
198
|
if (Array.isArray(rawOptions)) {
|
|
174
199
|
const options = rawOptions.map(toPositiveInt).filter((n) => n !== undefined);
|
|
175
|
-
if (options.length > 1)
|
|
176
|
-
return
|
|
200
|
+
if (options.length > 1) {
|
|
201
|
+
return maxInputTokens !== undefined
|
|
202
|
+
? { contextWindow, contextWindowOptions: options, maxInputTokens }
|
|
203
|
+
: { contextWindow, contextWindowOptions: options };
|
|
204
|
+
}
|
|
177
205
|
}
|
|
178
|
-
return { contextWindow };
|
|
206
|
+
return maxInputTokens !== undefined ? { contextWindow, maxInputTokens } : { contextWindow };
|
|
179
207
|
}
|
|
180
208
|
/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */
|
|
181
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;;;;;;;;;;;;;;;;;;;GAm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ACzF,MAAM,UAAU,uBAAuB,CAAC,OAAe;IACtD,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,uBAAuB,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, input-token context windows.\n *\n * GitHub's Copilot API (CAPI) exposes per-model limits via `GET {baseUrl}/models`. Every window\n * Atomic shows for a Copilot model is measured in INPUT (prompt) tokens, exactly like every other\n * provider's `contextWindow`, so GitHub models read consistently across the UI:\n *\n * - A model's input budget = `capabilities.limits.max_prompt_tokens`\n * ?? `capabilities.limits.max_context_window_tokens`\n * ?? 128_000 (the two fallbacks are safeties real CAPI entries never hit).\n * - Models with tiered pricing expose per-tier input budgets via\n * `billing.token_prices.<tier>.context_max`. The `default` tier is the base context window\n * (e.g. gpt-5.5 272k, Claude 200k); a `long_context` tier adds a selectable larger input\n * window (e.g. gpt-5.5 922k, Claude 936k) the user can switch to via the `/model` picker.\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 input-token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens`. */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens`. */\n\tmaxContextWindowTokens?: number;\n\t/** `billing.token_prices.default.context_max`. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max`. */\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-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AA4B1C,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,aAAa;IACvC,sBAAsB,EAAE,2BAA2B;CACnD,CAAC;AAEF,kGAAkG;AAClG,MAAM,CAAC,MAAM,4BAA4B,GAAG,0CAA0C,CAAC;AAEvF,qFAAqF;AACrF,MAAM,CAAC,MAAM,4BAA4B,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE3D,4CAA4C;AAC5C,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAU,CAAC;AAExD;;;;;;;GAOG;AACH,MAAM,UAAU,0BAA0B,CAAC,KAAyB,EAAE,gBAAyB;IAC9F,IAAI,KAAK,EAAE,CAAC;QACX,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;QAC9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,aAAa,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,CAAC;IACtD,CAAC;IAAC,MAAM,CAAC;QACR,2CAA2C;IAC5C,CAAC;AACF,CAAC;AAED,yFAAyF;AACzF,MAAM,UAAU,uBAAuB,CAAC,OAAe;IACtD,OAAO,eAAe,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,uBAAuB,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"]}
|