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

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +16 -3
  2. package/dist/builtin/cursor/CHANGELOG.md +1 -1
  3. package/dist/builtin/cursor/package.json +2 -2
  4. package/dist/builtin/intercom/package.json +1 -1
  5. package/dist/builtin/mcp/CHANGELOG.md +5 -0
  6. package/dist/builtin/mcp/direct-tools.ts +4 -2
  7. package/dist/builtin/mcp/package.json +1 -1
  8. package/dist/builtin/mcp/proxy-modes.ts +4 -2
  9. package/dist/builtin/mcp/utils.ts +25 -0
  10. package/dist/builtin/subagents/package.json +1 -1
  11. package/dist/builtin/web-access/package.json +1 -1
  12. package/dist/builtin/workflows/CHANGELOG.md +5 -0
  13. package/dist/builtin/workflows/builtin/ralph.ts +1 -0
  14. package/dist/builtin/workflows/package.json +1 -1
  15. package/dist/builtin/workflows/src/runs/foreground/stage-runner.ts +114 -4
  16. package/dist/core/agent-session.d.ts +25 -0
  17. package/dist/core/agent-session.d.ts.map +1 -1
  18. package/dist/core/agent-session.js +135 -11
  19. package/dist/core/agent-session.js.map +1 -1
  20. package/dist/core/auth-guidance.d.ts +12 -0
  21. package/dist/core/auth-guidance.d.ts.map +1 -1
  22. package/dist/core/auth-guidance.js +24 -0
  23. package/dist/core/auth-guidance.js.map +1 -1
  24. package/dist/core/auth-storage.d.ts +42 -0
  25. package/dist/core/auth-storage.d.ts.map +1 -1
  26. package/dist/core/auth-storage.js +71 -10
  27. package/dist/core/auth-storage.js.map +1 -1
  28. package/dist/core/context-window.d.ts +15 -0
  29. package/dist/core/context-window.d.ts.map +1 -1
  30. package/dist/core/context-window.js +11 -0
  31. package/dist/core/context-window.js.map +1 -1
  32. package/dist/core/copilot-gemini-payload-sanitizer.d.ts +72 -0
  33. package/dist/core/copilot-gemini-payload-sanitizer.d.ts.map +1 -0
  34. package/dist/core/copilot-gemini-payload-sanitizer.js +296 -0
  35. package/dist/core/copilot-gemini-payload-sanitizer.js.map +1 -0
  36. package/dist/core/copilot-gemini-reasoning.d.ts +118 -0
  37. package/dist/core/copilot-gemini-reasoning.d.ts.map +1 -0
  38. package/dist/core/copilot-gemini-reasoning.js +260 -0
  39. package/dist/core/copilot-gemini-reasoning.js.map +1 -0
  40. package/dist/core/copilot-gemini-tool-arguments.d.ts +42 -0
  41. package/dist/core/copilot-gemini-tool-arguments.d.ts.map +1 -0
  42. package/dist/core/copilot-gemini-tool-arguments.js +179 -0
  43. package/dist/core/copilot-gemini-tool-arguments.js.map +1 -0
  44. package/dist/core/copilot-model-catalog.d.ts +26 -11
  45. package/dist/core/copilot-model-catalog.d.ts.map +1 -1
  46. package/dist/core/copilot-model-catalog.js +34 -9
  47. package/dist/core/copilot-model-catalog.js.map +1 -1
  48. package/dist/core/flattened-tool-arguments.d.ts +41 -0
  49. package/dist/core/flattened-tool-arguments.d.ts.map +1 -0
  50. package/dist/core/flattened-tool-arguments.js +136 -0
  51. package/dist/core/flattened-tool-arguments.js.map +1 -0
  52. package/dist/core/http-dispatcher.d.ts.map +1 -1
  53. package/dist/core/http-dispatcher.js +5 -0
  54. package/dist/core/http-dispatcher.js.map +1 -1
  55. package/dist/core/model-registry.d.ts.map +1 -1
  56. package/dist/core/model-registry.js +6 -4
  57. package/dist/core/model-registry.js.map +1 -1
  58. package/dist/core/sdk.d.ts.map +1 -1
  59. package/dist/core/sdk.js +38 -8
  60. package/dist/core/sdk.js.map +1 -1
  61. package/dist/index.d.ts +2 -1
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +2 -1
  64. package/dist/index.js.map +1 -1
  65. package/docs/providers.md +4 -3
  66. package/docs/workflows.md +2 -0
  67. package/package.json +2 -2
@@ -0,0 +1,179 @@
1
+ import { isCopilotGeminiModel } from "./copilot-gemini-payload-sanitizer.js";
2
+ import { reconstructFlattenedKeys } from "./flattened-tool-arguments.js";
3
+ function isPlainObject(value) {
4
+ return typeof value === "object" && value !== null && !Array.isArray(value);
5
+ }
6
+ /** A flattened key contains a bracket index like `foo[0]`. */
7
+ function hasFlattenedKey(keys) {
8
+ return keys.some((key) => /\[\d+\]/.test(key));
9
+ }
10
+ /** A schema node that holds a nested object/array (so dotted keys are real paths). */
11
+ function isContainerSchema(schema) {
12
+ if (!isPlainObject(schema))
13
+ return false;
14
+ if (schema.type === "object" || schema.type === "array")
15
+ return true;
16
+ if ("properties" in schema || "items" in schema)
17
+ return true;
18
+ const union = schema.anyOf ?? schema.oneOf;
19
+ if (Array.isArray(union))
20
+ return union.some((branch) => isContainerSchema(branch));
21
+ return false;
22
+ }
23
+ /** Top-level property names whose schema is an object/array container. */
24
+ function containerPropertyNames(schema) {
25
+ const names = new Set();
26
+ if (!isPlainObject(schema))
27
+ return names;
28
+ const properties = schema.properties;
29
+ if (!isPlainObject(properties))
30
+ return names;
31
+ for (const [name, sub] of Object.entries(properties)) {
32
+ if (isContainerSchema(sub))
33
+ names.add(name);
34
+ }
35
+ return names;
36
+ }
37
+ /** Whether `key` is a pure dotted path (`parent.child`) headed by a container prop. */
38
+ function isDottedContainerKey(key, containers) {
39
+ const dot = key.indexOf(".");
40
+ if (dot <= 0)
41
+ return false;
42
+ return containers.has(key.slice(0, dot));
43
+ }
44
+ /**
45
+ * Decide whether a flattened key should be split into nested path segments.
46
+ * Bracket-indexed keys always split. When a bracket key is present anywhere in
47
+ * the payload, dotted keys split too (they are part of the same flattened
48
+ * object). Otherwise a dotted key only splits when the schema marks its head as
49
+ * a container property, which keeps legitimate dot-containing keys intact.
50
+ */
51
+ function shouldSplitKey(key, hasBracket, containers) {
52
+ if (/\[\d+\]/.test(key))
53
+ return true;
54
+ if (hasBracket)
55
+ return true;
56
+ return isDottedContainerKey(key, containers);
57
+ }
58
+ /**
59
+ * Reconstruct flattened Gemini tool-call arguments into proper nested
60
+ * arrays/objects. Returns the original reference unchanged when there is nothing
61
+ * to reconstruct. Bracket-indexed keys are always reconstructed; purely dotted
62
+ * keys are reconstructed only when the optional `schema` marks their head
63
+ * segment as an object/array container property. Reconstruction (and its
64
+ * prototype-pollution guard) is delegated to the shared canonical helper.
65
+ */
66
+ export function unflattenGeminiToolArguments(args, schema) {
67
+ if (!isPlainObject(args))
68
+ return args;
69
+ const keys = Object.keys(args);
70
+ const hasBracket = hasFlattenedKey(keys);
71
+ const containers = hasBracket ? new Set() : containerPropertyNames(schema);
72
+ const hasDottedContainer = !hasBracket && keys.some((key) => isDottedContainerKey(key, containers));
73
+ if (!hasBracket && !hasDottedContainer)
74
+ return args;
75
+ return reconstructFlattenedKeys(args, (key) => shouldSplitKey(key, hasBracket, containers));
76
+ }
77
+ /**
78
+ * If `model` is a GitHub Copilot Gemini model, normalize flattened tool-call
79
+ * arguments; otherwise return them unchanged. Used to gate
80
+ * {@link unflattenGeminiToolArguments} by model at tool-call time. The optional
81
+ * `schema` is the tool's parameter schema, used to disambiguate dotted keys.
82
+ */
83
+ export function normalizeToolArgumentsForModel(args, model, schema) {
84
+ if (!model || !isCopilotGeminiModel(model))
85
+ return args;
86
+ return unflattenGeminiToolArguments(args, schema);
87
+ }
88
+ /** Map each tool name in an OpenAI chat-completions payload to its parameter schema. */
89
+ function toolParameterSchemas(tools) {
90
+ const schemas = new Map();
91
+ if (!Array.isArray(tools))
92
+ return schemas;
93
+ for (const tool of tools) {
94
+ if (!isPlainObject(tool))
95
+ continue;
96
+ // OpenAI chat-completions tool shape: { type: "function", function: { name, parameters } }.
97
+ const fn = tool.function;
98
+ if (isPlainObject(fn) && typeof fn.name === "string") {
99
+ schemas.set(fn.name, fn.parameters);
100
+ continue;
101
+ }
102
+ // Defensive: flat tool shape { name, parameters }.
103
+ if (typeof tool.name === "string")
104
+ schemas.set(tool.name, tool.parameters);
105
+ }
106
+ return schemas;
107
+ }
108
+ /**
109
+ * Reconstruct flattened GitHub Copilot Gemini tool-call arguments on the
110
+ * **outbound replay payload**, so prior assistant tool calls are sent back to
111
+ * CAPI in the nested array/object shape Gemini originally produced.
112
+ *
113
+ * Why this exists
114
+ * ---------------
115
+ * {@link normalizeToolArgumentsForModel} only unflattens at tool *execution*
116
+ * time; the persisted assistant message keeps the raw flattened arguments CAPI
117
+ * delivered (for example `{ "edits[0].newText": "..." }`). When that message is
118
+ * replayed on the next turn, CAPI parses those literal keys straight into the
119
+ * Gemini `FunctionCall.Args`, producing a function call that does not match the
120
+ * tool's declared schema (nor the structure Gemini signed). Gemini then ends
121
+ * the turn with `MALFORMED_FUNCTION_CALL` / `UNEXPECTED_TOOL_CALL` / `OTHER`,
122
+ * which CAPI surfaces as a bare `finish_reason: "error"` — so multi-turn tool
123
+ * use dies one turn after any array/object tool call (such as `edit`).
124
+ *
125
+ * This rewrites each replayed assistant `tool_calls[].function.arguments` JSON
126
+ * into the reconstructed nested shape (reusing {@link unflattenGeminiToolArguments}
127
+ * with the tool's own parameter schema, looked up from the payload's `tools`),
128
+ * fixing both new and already-persisted sessions. Gated to GitHub Copilot Gemini
129
+ * models, fail-open on non-JSON arguments, and a no-op for well-formed args.
130
+ */
131
+ export function normalizeCopilotGeminiReplayToolArguments(payload, model) {
132
+ if (!isCopilotGeminiModel(model))
133
+ return payload;
134
+ if (!isPlainObject(payload))
135
+ return payload;
136
+ const messages = payload.messages;
137
+ if (!Array.isArray(messages))
138
+ return payload;
139
+ const schemas = toolParameterSchemas(payload.tools);
140
+ let mutated = false;
141
+ const nextMessages = messages.map((message) => {
142
+ if (!isPlainObject(message) || message.role !== "assistant")
143
+ return message;
144
+ const toolCalls = message.tool_calls;
145
+ if (!Array.isArray(toolCalls) || toolCalls.length === 0)
146
+ return message;
147
+ let messageMutated = false;
148
+ const nextToolCalls = toolCalls.map((toolCall) => {
149
+ if (!isPlainObject(toolCall))
150
+ return toolCall;
151
+ const fn = toolCall.function;
152
+ if (!isPlainObject(fn) || typeof fn.arguments !== "string")
153
+ return toolCall;
154
+ let parsed;
155
+ try {
156
+ parsed = JSON.parse(fn.arguments);
157
+ }
158
+ catch {
159
+ return toolCall; // fail open: never corrupt a replayed argument string
160
+ }
161
+ if (!isPlainObject(parsed))
162
+ return toolCall;
163
+ const schema = typeof fn.name === "string" ? schemas.get(fn.name) : undefined;
164
+ const reconstructed = unflattenGeminiToolArguments(parsed, schema);
165
+ if (reconstructed === parsed)
166
+ return toolCall;
167
+ messageMutated = true;
168
+ return { ...toolCall, function: { ...fn, arguments: JSON.stringify(reconstructed) } };
169
+ });
170
+ if (!messageMutated)
171
+ return message;
172
+ mutated = true;
173
+ return { ...message, tool_calls: nextToolCalls };
174
+ });
175
+ if (!mutated)
176
+ return payload;
177
+ return { ...payload, messages: nextMessages };
178
+ }
179
+ //# sourceMappingURL=copilot-gemini-tool-arguments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copilot-gemini-tool-arguments.js","sourceRoot":"","sources":["../../src/core/copilot-gemini-tool-arguments.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAC7E,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAwCzE,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,8DAA8D;AAC9D,SAAS,eAAe,CAAC,IAAc;IACrC,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AACjD,CAAC;AAED,sFAAsF;AACtF,SAAS,iBAAiB,CAAC,MAAe;IACxC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;QAAE,OAAO,KAAK,CAAC;IACzC,IAAI,MAAM,CAAC,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,KAAK,OAAO;QAAE,OAAO,IAAI,CAAC;IACrE,IAAI,YAA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SAAS,CAAC,aAAa,CAAC,EAAE,EAAE,CAAC;QACxF,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,cAAc;YAAE,OAAO,OAAO,CAAC;QACpC,OAAO,GAAG,IAAI,CAAC;QACf,OAAO,EAAE,GAAG,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,OAAO;QAAE,OAAO,OAAO,CAAC;IAC7B,OAAO,EAAE,GAAG,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAC;AAChD,CAAC","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\nimport { isCopilotGeminiModel } from \"./copilot-gemini-payload-sanitizer.ts\";\nimport { reconstructFlattenedKeys } from \"./flattened-tool-arguments.ts\";\n\n/**\n * Normalizes GitHub Copilot Gemini tool-call arguments.\n *\n * Why this exists\n * ---------------\n * `github-copilot` Gemini models are served through Copilot's CAPI gateway,\n * which proxies to Google's GenAI API. When a function/tool argument is an\n * array (or a nested object/array), Gemini serializes it on the wire as\n * **flattened, indexed keys** instead of a real JSON array/object. For example\n * a tool called with `{ keywords: [\"a\", \"b\"] }` arrives as:\n *\n * ```json\n * { \"keywords[0]\": \"a\", \"keywords[1]\": \"b\" }\n * ```\n *\n * This was confirmed by capturing the raw CAPI SSE stream: the\n * `tool_calls[].function.arguments` JSON itself contains the `name[index]`\n * keys, so the runtime parses valid-but-wrong JSON. Schema validation then\n * fails (`keywords: must have required properties keywords` and\n * `root: must not have additional properties`) and the model retries forever,\n * because it keeps re-emitting the same flattened shape. This is most visible\n * with the workflow `structured_output` tool but affects any Gemini tool call\n * whose schema contains an array or nested object.\n *\n * What it does\n * ------------\n * Reconstructs flattened keys (`name[i]`, `name[i].sub`, `parent.child`) back\n * into the intended nested arrays/objects, before tool-argument validation\n * runs. Bracket-indexed keys (`name[<digit>]`) are always reconstructed. A\n * purely dotted key (`parent.child`, with no array anywhere) is ambiguous —\n * a legitimate argument key can itself contain a dot — so it is only split when\n * the optional tool `schema` marks its head segment as an object/array\n * container property. The transform is gated to GitHub Copilot Gemini models,\n * so it never touches well-formed arguments from any other provider/model.\n */\n\ntype JsonRecord = Record<string, unknown>;\n\nfunction isPlainObject(value: unknown): value is JsonRecord {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** A flattened key contains a bracket index like `foo[0]`. */\nfunction hasFlattenedKey(keys: string[]): boolean {\n return keys.some((key) => /\\[\\d+\\]/.test(key));\n}\n\n/** A schema node that holds a nested object/array (so dotted keys are real paths). */\nfunction isContainerSchema(schema: unknown): boolean {\n if (!isPlainObject(schema)) return false;\n if (schema.type === \"object\" || schema.type === \"array\") return true;\n if (\"properties\" in schema || \"items\" in schema) return true;\n const union = schema.anyOf ?? schema.oneOf;\n if (Array.isArray(union)) return union.some((branch) => isContainerSchema(branch));\n return false;\n}\n\n/** Top-level property names whose schema is an object/array container. */\nfunction containerPropertyNames(schema: unknown): Set<string> {\n const names = new Set<string>();\n if (!isPlainObject(schema)) return names;\n const properties = schema.properties;\n if (!isPlainObject(properties)) return names;\n for (const [name, sub] of Object.entries(properties)) {\n if (isContainerSchema(sub)) names.add(name);\n }\n return names;\n}\n\n/** Whether `key` is a pure dotted path (`parent.child`) headed by a container prop. */\nfunction isDottedContainerKey(key: string, containers: Set<string>): boolean {\n const dot = key.indexOf(\".\");\n if (dot <= 0) return false;\n return containers.has(key.slice(0, dot));\n}\n\n/**\n * Decide whether a flattened key should be split into nested path segments.\n * Bracket-indexed keys always split. When a bracket key is present anywhere in\n * the payload, dotted keys split too (they are part of the same flattened\n * object). Otherwise a dotted key only splits when the schema marks its head as\n * a container property, which keeps legitimate dot-containing keys intact.\n */\nfunction shouldSplitKey(key: string, hasBracket: boolean, containers: Set<string>): boolean {\n if (/\\[\\d+\\]/.test(key)) return true;\n if (hasBracket) return true;\n return isDottedContainerKey(key, containers);\n}\n\n/**\n * Reconstruct flattened Gemini tool-call arguments into proper nested\n * arrays/objects. Returns the original reference unchanged when there is nothing\n * to reconstruct. Bracket-indexed keys are always reconstructed; purely dotted\n * keys are reconstructed only when the optional `schema` marks their head\n * segment as an object/array container property. Reconstruction (and its\n * prototype-pollution guard) is delegated to the shared canonical helper.\n */\nexport function unflattenGeminiToolArguments(args: unknown, schema?: unknown): unknown {\n if (!isPlainObject(args)) return args;\n const keys = Object.keys(args);\n const hasBracket = hasFlattenedKey(keys);\n const containers = hasBracket ? new Set<string>() : containerPropertyNames(schema);\n const hasDottedContainer =\n !hasBracket && keys.some((key) => isDottedContainerKey(key, containers));\n if (!hasBracket && !hasDottedContainer) return args;\n\n return reconstructFlattenedKeys(args, (key) => shouldSplitKey(key, hasBracket, containers));\n}\n\n/**\n * If `model` is a GitHub Copilot Gemini model, normalize flattened tool-call\n * arguments; otherwise return them unchanged. Used to gate\n * {@link unflattenGeminiToolArguments} by model at tool-call time. The optional\n * `schema` is the tool's parameter schema, used to disambiguate dotted keys.\n */\nexport function normalizeToolArgumentsForModel(\n args: unknown,\n model: Pick<Model<Api>, \"provider\" | \"api\" | \"id\"> | undefined,\n schema?: unknown,\n): unknown {\n if (!model || !isCopilotGeminiModel(model)) return args;\n return unflattenGeminiToolArguments(args, schema);\n}\n\n/** Map each tool name in an OpenAI chat-completions payload to its parameter schema. */\nfunction toolParameterSchemas(tools: unknown): Map<string, unknown> {\n const schemas = new Map<string, unknown>();\n if (!Array.isArray(tools)) return schemas;\n for (const tool of tools) {\n if (!isPlainObject(tool)) continue;\n // OpenAI chat-completions tool shape: { type: \"function\", function: { name, parameters } }.\n const fn = tool.function;\n if (isPlainObject(fn) && typeof fn.name === \"string\") {\n schemas.set(fn.name, fn.parameters);\n continue;\n }\n // Defensive: flat tool shape { name, parameters }.\n if (typeof tool.name === \"string\") schemas.set(tool.name, tool.parameters);\n }\n return schemas;\n}\n\n/**\n * Reconstruct flattened GitHub Copilot Gemini tool-call arguments on the\n * **outbound replay payload**, so prior assistant tool calls are sent back to\n * CAPI in the nested array/object shape Gemini originally produced.\n *\n * Why this exists\n * ---------------\n * {@link normalizeToolArgumentsForModel} only unflattens at tool *execution*\n * time; the persisted assistant message keeps the raw flattened arguments CAPI\n * delivered (for example `{ \"edits[0].newText\": \"...\" }`). When that message is\n * replayed on the next turn, CAPI parses those literal keys straight into the\n * Gemini `FunctionCall.Args`, producing a function call that does not match the\n * tool's declared schema (nor the structure Gemini signed). Gemini then ends\n * the turn with `MALFORMED_FUNCTION_CALL` / `UNEXPECTED_TOOL_CALL` / `OTHER`,\n * which CAPI surfaces as a bare `finish_reason: \"error\"` — so multi-turn tool\n * use dies one turn after any array/object tool call (such as `edit`).\n *\n * This rewrites each replayed assistant `tool_calls[].function.arguments` JSON\n * into the reconstructed nested shape (reusing {@link unflattenGeminiToolArguments}\n * with the tool's own parameter schema, looked up from the payload's `tools`),\n * fixing both new and already-persisted sessions. Gated to GitHub Copilot Gemini\n * models, fail-open on non-JSON arguments, and a no-op for well-formed args.\n */\nexport function normalizeCopilotGeminiReplayToolArguments(\n payload: unknown,\n model: Pick<Model<Api>, \"provider\" | \"api\" | \"id\">,\n): unknown {\n if (!isCopilotGeminiModel(model)) return payload;\n if (!isPlainObject(payload)) return payload;\n const messages = payload.messages;\n if (!Array.isArray(messages)) return payload;\n\n const schemas = toolParameterSchemas(payload.tools);\n let mutated = false;\n\n const nextMessages = messages.map((message) => {\n if (!isPlainObject(message) || message.role !== \"assistant\") return message;\n const toolCalls = message.tool_calls;\n if (!Array.isArray(toolCalls) || toolCalls.length === 0) return message;\n\n let messageMutated = false;\n const nextToolCalls = toolCalls.map((toolCall) => {\n if (!isPlainObject(toolCall)) return toolCall;\n const fn = toolCall.function;\n if (!isPlainObject(fn) || typeof fn.arguments !== \"string\") return toolCall;\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(fn.arguments);\n } catch {\n return toolCall; // fail open: never corrupt a replayed argument string\n }\n if (!isPlainObject(parsed)) return toolCall;\n\n const schema = typeof fn.name === \"string\" ? schemas.get(fn.name) : undefined;\n const reconstructed = unflattenGeminiToolArguments(parsed, schema);\n if (reconstructed === parsed) return toolCall;\n\n messageMutated = true;\n return { ...toolCall, function: { ...fn, arguments: JSON.stringify(reconstructed) } };\n });\n\n if (!messageMutated) return message;\n mutated = true;\n return { ...message, tool_calls: nextToolCalls };\n });\n\n if (!mutated) return payload;\n return { ...payload, messages: nextMessages };\n}\n"]}
@@ -11,10 +11,14 @@
11
11
  * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a
12
12
  * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).
13
13
  *
14
- * Atomic's current `contextWindow` drives local prompt collection, compaction thresholds, footer
15
- * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token
16
- * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field
17
- * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.
14
+ * Atomic shows the model's full context window for the selectable long tier (the
15
+ * `max_context_window_tokens` total, e.g. 1_000_000/1_050_000), matching how the native `openai/*`
16
+ * and `anthropic/*` providers advertise these models. Because GitHub enforces a lower server-side
17
+ * prompt cap (`max_prompt_tokens`, e.g. 936k/922k) below that total, the prompt cap is retained as
18
+ * an internal effective input budget (`CopilotModelContext.maxInputTokens`) that drives compaction
19
+ * thresholds and the overflow-recovery guard, so the branded total can be displayed without
20
+ * overrunning the server limit. The default (short) tier stays at the `default` billing tier's
21
+ * prompt budget.
18
22
  *
19
23
  * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers
20
24
  * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes
@@ -24,15 +28,24 @@
24
28
  /** Resolved input-token context window(s) for a single Copilot model. */
25
29
  export interface CopilotModelContext {
26
30
  /**
27
- * Base context window in INPUT tokens — shown in the footer and used for compaction. The
28
- * default tier's `context_max`, or the model-level `max_prompt_tokens` fallback otherwise.
31
+ * Base/displayed context window — shown in the footer. The default tier's `context_max`, or the
32
+ * model-level `max_prompt_tokens` fallback otherwise.
29
33
  */
30
34
  contextWindow: number;
31
35
  /**
32
- * Selectable input-token windows (`[default, long]`) when the model exposes a `long_context`
33
- * tier larger than its default; absent for single-window models.
36
+ * Selectable windows (`[default, long]`) when the model exposes a `long_context` tier larger than
37
+ * its default; absent for single-window models. The long entry is the model's full
38
+ * `max_context_window_tokens` (total capacity) when advertised, matching `openai/*` and
39
+ * `anthropic/*`.
34
40
  */
35
41
  contextWindowOptions?: readonly number[];
42
+ /**
43
+ * Hard prompt/input cap (`max_prompt_tokens`) when it sits below the displayed long window. Used
44
+ * as the effective input budget for compaction thresholds and overflow recovery so the branded
45
+ * total can be shown without overrunning GitHub's server-side prompt limit. Absent when the
46
+ * displayed window already equals the input cap.
47
+ */
48
+ maxInputTokens?: number;
36
49
  }
37
50
  /** Map of model id → resolved input-token context window(s). */
38
51
  export type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;
@@ -49,7 +62,7 @@ export declare const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.gith
49
62
  /** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */
50
63
  export declare const COPILOT_CATALOG_CACHE_TTL_MS: number;
51
64
  /** Current on-disk cache schema version. */
52
- export declare const COPILOT_CATALOG_CACHE_VERSION: 2;
65
+ export declare const COPILOT_CATALOG_CACHE_VERSION: 3;
53
66
  /**
54
67
  * Resolve the Copilot CAPI base URL.
55
68
  *
@@ -61,10 +74,12 @@ export declare const COPILOT_CATALOG_CACHE_VERSION: 2;
61
74
  export declare function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string;
62
75
  /** Raw token limits parsed from a CAPI model entry. */
63
76
  export interface CopilotModelLimits {
64
- /** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget. */
77
+ /** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget (the hard input cap). */
65
78
  maxPromptTokens?: number;
66
- /** `capabilities.limits.max_context_window_tokens` — total context capacity, used only as a fallback. */
79
+ /** `capabilities.limits.max_context_window_tokens` — total context capacity (the displayed long tier). */
67
80
  maxContextWindowTokens?: number;
81
+ /** `capabilities.limits.max_output_tokens` — output reserve; derives the input cap when `max_prompt_tokens` is absent. */
82
+ maxOutputTokens?: number;
68
83
  /** `billing.token_prices.default.context_max` — default-tier prompt threshold. */
69
84
  defaultContextMax?: number;
70
85
  /** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */
@@ -1 +1 @@
1
- {"version":3,"file":"copilot-model-catalog.d.ts","sourceRoot":"","sources":["../../src/core/copilot-model-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAKH,yEAAyE;AACzE,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACzC;AAED,gEAAgE;AAChE,MAAM,MAAM,mBAAmB,GAAG,WAAW,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;AAE3E,wGAAwG;AACxG,eAAO,MAAM,+BAA+B,SAAU,CAAC;AAEvD,eAAO,MAAM,2BAA2B,eAAe,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAMpE,CAAC;AAEF,kGAAkG;AAClG,eAAO,MAAM,4BAA4B,6CAA6C,CAAC;AAEvF,qFAAqF;AACrF,eAAO,MAAM,4BAA4B,QAAiB,CAAC;AAE3D,4CAA4C;AAC5C,eAAO,MAAM,6BAA6B,EAAG,CAAU,CAAC;AAExD;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,CASvG;AAcD,uDAAuD;AACvD,MAAM,WAAW,kBAAkB;IAClC,6EAA6E;IAC7E,eAAe,CAAC,EAAE,MAAM,CAAC;IACz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AI,EAAE,MAAM,EACZ,OAAO,EAAE,8BAA8B,GACrC,mBAAmB,GAAG,SAAS,CAsBjC;AAED,6FAA6F;AAC7F,wBAAgB,wBAAwB,CACvC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,mBAAmB,EAC5B,GAAG,CAAC,EAAE,MAAM,GACV,IAAI,CAaN;AAED,yFAAyF;AACzF,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED,0FAA0F;AAC1F,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;;;;;;;GASG;AACH,wBAAgB,sCAAsC,CACrD,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,SAAS,EAAE,MAAM,EACjB,GAAG,CAAC,EAAE,MAAM,GACV,OAAO,CAOT","sourcesContent":["/**\n * GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.\n *\n * GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:\n *\n * - `capabilities.limits.max_context_window_tokens` is the model's total context capacity\n * (prompt + completion reserve).\n * - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely\n * fill before the provider must reserve output tokens.\n * - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The\n * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a\n * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).\n *\n * Atomic's current `contextWindow` drives local prompt collection, compaction thresholds, footer\n * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token\n * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field\n * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.\n *\n * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers\n * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes\n * stale. Instead the catalog is fetched live (gated on the user actually having the GitHub Copilot\n * provider) and cached on disk for a short TTL, exactly like the Copilot CLI.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/** Resolved input-token context window(s) for a single Copilot model. */\nexport interface CopilotModelContext {\n\t/**\n\t * Base context window in INPUT tokens — shown in the footer and used for compaction. The\n\t * default tier's `context_max`, or the model-level `max_prompt_tokens` fallback otherwise.\n\t */\n\tcontextWindow: number;\n\t/**\n\t * Selectable input-token windows (`[default, long]`) when the model exposes a `long_context`\n\t * tier larger than its default; absent for single-window models.\n\t */\n\tcontextWindowOptions?: readonly number[];\n}\n\n/** Map of model id → resolved input-token context window(s). */\nexport type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;\n\n/** Safety fallback when a model reports neither `max_prompt_tokens` nor `max_context_window_tokens`. */\nexport const COPILOT_CONTEXT_WINDOW_FALLBACK = 128_000;\n\nexport const COPILOT_CATALOG_API_VERSION = \"2026-06-01\";\n\n/**\n * Headers GitHub's CAPI expects for catalog reads. Mirrors the editor headers pi-ai already sends\n * for Copilot token refresh and model-policy calls, plus the dated API version.\n */\nexport const COPILOT_CATALOG_HEADERS: Readonly<Record<string, string>> = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n\t\"X-GitHub-Api-Version\": COPILOT_CATALOG_API_VERSION,\n};\n\n/** Default (non-enterprise) Copilot CAPI base URL when the token has no resolvable `proxy-ep`. */\nexport const DEFAULT_COPILOT_API_BASE_URL = \"https://api.individual.githubcopilot.com\";\n\n/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */\nexport const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;\n\n/** Current on-disk cache schema version. */\nexport const COPILOT_CATALOG_CACHE_VERSION = 2 as const;\n\n/**\n * Resolve the Copilot CAPI base URL.\n *\n * Copilot access tokens embed a `proxy-ep=proxy.<host>` segment; the API host is the same host with\n * `proxy.` swapped for `api.`. Falls back to the enterprise host or the individual default. (pi-ai\n * exposes an equivalent helper, but its published `dist` mangles the export name, so the small,\n * stable parsing logic is reimplemented here.)\n */\nexport function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string {\n\tif (token) {\n\t\tconst match = token.match(/proxy-ep=([^;]+)/);\n\t\tif (match) {\n\t\t\treturn `https://${match[1].replace(/^proxy\\./, \"api.\")}`;\n\t\t}\n\t}\n\tif (enterpriseDomain) return `https://copilot-api.${enterpriseDomain}`;\n\treturn DEFAULT_COPILOT_API_BASE_URL;\n}\n\nfunction trimTrailingSlash(url: string): string {\n\treturn url.replace(/\\/+$/, \"\");\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n\treturn value && typeof value === \"object\" ? (value as Record<string, unknown>) : undefined;\n}\n\nfunction toPositiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\n/** Raw token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget. */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens` — total context capacity, used only as a fallback. */\n\tmaxContextWindowTokens?: number;\n\t/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */\n\tlongContextMax?: number;\n}\n\n/**\n * Resolve a model's input-token context window(s) from its CAPI limits.\n *\n * `contextWindow` is the model's base input budget — the default tier's `context_max` when tiered,\n * otherwise `max_prompt_tokens ?? max_context_window_tokens ?? 128_000`. A `long_context` tier that\n * is larger than the base adds a second selectable window. Returns `undefined` when the entry\n * carries no usable limit signal at all.\n */\nexport function resolveCopilotModelContext(limits: CopilotModelLimits): CopilotModelContext | undefined {\n\tconst hasSignal =\n\t\tlimits.maxPromptTokens !== undefined ||\n\t\tlimits.maxContextWindowTokens !== undefined ||\n\t\tlimits.defaultContextMax !== undefined ||\n\t\tlimits.longContextMax !== undefined;\n\tif (!hasSignal) return undefined;\n\n\tconst maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;\n\tconst base = limits.defaultContextMax ?? maxInput;\n\tif (limits.longContextMax !== undefined && limits.longContextMax > base) {\n\t\treturn { contextWindow: base, contextWindowOptions: [base, limits.longContextMax] };\n\t}\n\treturn { contextWindow: base };\n}\n\n/**\n * Parse a raw CAPI `/models` response body into an input-token context-window catalog.\n */\nexport function parseCopilotModelCatalog(body: unknown): CopilotModelCatalog {\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tconst data = asRecord(body)?.data;\n\tif (!Array.isArray(data)) return catalog;\n\n\tfor (const entry of data) {\n\t\tconst record = asRecord(entry);\n\t\tif (!record) continue;\n\t\tconst id = record.id;\n\t\tif (typeof id !== \"string\" || id.length === 0) continue;\n\n\t\tconst limits = asRecord(asRecord(record.capabilities)?.limits);\n\t\tconst prices = asRecord(asRecord(record.billing)?.token_prices);\n\t\tconst context = resolveCopilotModelContext({\n\t\t\tmaxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),\n\t\t\tmaxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),\n\t\t\tdefaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),\n\t\t\tlongContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),\n\t\t});\n\t\tif (context) catalog.set(id, context);\n\t}\n\n\treturn catalog;\n}\n\nexport interface FetchCopilotModelCatalogOptions {\n\t/** Valid Copilot CAPI bearer token (e.g. from `modelRegistry.getApiKeyForProvider`). */\n\ttoken: string;\n\t/** Override the resolved base URL; defaults to one derived from the token. */\n\tbaseUrl?: string;\n\t/** Enterprise domain, used for base-URL resolution when the token lacks a `proxy-ep`. */\n\tenterpriseDomain?: string;\n\t/** Extra/override request headers. */\n\theaders?: Record<string, string>;\n\t/** Injectable `fetch` for testing. */\n\tfetchImpl?: typeof fetch;\n\t/** Abort signal. */\n\tsignal?: AbortSignal;\n}\n\n/** Fetch and parse the live Copilot model catalog from CAPI `GET {baseUrl}/models`. */\nexport async function fetchCopilotModelCatalog(options: FetchCopilotModelCatalogOptions): Promise<CopilotModelCatalog> {\n\tconst fetchImpl = options.fetchImpl ?? fetch;\n\tconst baseUrl = options.baseUrl ?? copilotApiBaseUrlFromToken(options.token, options.enterpriseDomain);\n\tconst response = await fetchImpl(`${trimTrailingSlash(baseUrl)}/models`, {\n\t\tmethod: \"GET\",\n\t\theaders: {\n\t\t\tAccept: \"application/json\",\n\t\t\tAuthorization: `Bearer ${options.token}`,\n\t\t\t...COPILOT_CATALOG_HEADERS,\n\t\t\t...options.headers,\n\t\t},\n\t\t...(options.signal ? { signal: options.signal } : {}),\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot /models request failed: ${response.status} ${response.statusText}`);\n\t}\n\treturn parseCopilotModelCatalog(await response.json());\n}\n\n// ----------------------------------------------------------------------------\n// Active in-memory catalog (consulted by the model registry).\n//\n// Empty by default, so with no Copilot auth / no successful fetch the registry leaves Copilot\n// model context windows untouched and the picker never appears.\n// ----------------------------------------------------------------------------\n\nlet activeCatalog: CopilotModelCatalog = new Map();\n\n/** Replace the active catalog the registry derives context windows from. */\nexport function setActiveCopilotModelCatalog(catalog: CopilotModelCatalog): void {\n\tactiveCatalog = catalog;\n}\n\n/** The active catalog (empty until a successful auth-gated fetch/cache load). */\nexport function getActiveCopilotModelCatalog(): CopilotModelCatalog {\n\treturn activeCatalog;\n}\n\n/** Reset the active catalog (primarily for tests). */\nexport function clearActiveCopilotModelCatalog(): void {\n\tactiveCatalog = new Map();\n}\n\n// ----------------------------------------------------------------------------\n// Disk cache.\n// ----------------------------------------------------------------------------\n\ninterface CopilotCatalogCacheFile {\n\tversion: typeof COPILOT_CATALOG_CACHE_VERSION;\n\t/** CAPI host the catalog was fetched from; cache misses on host change (e.g. enterprise switch). */\n\thost: string;\n\t/** Epoch ms the catalog was fetched. */\n\tfetchedAt: number;\n\tmodels: Record<string, CopilotModelContext>;\n}\n\nfunction hostFromBaseUrl(baseUrl: string): string {\n\ttry {\n\t\treturn new URL(baseUrl).host;\n\t} catch {\n\t\treturn baseUrl;\n\t}\n}\n\nexport interface ReadCopilotCatalogCacheOptions {\n\t/** Expected CAPI host; a cached file from a different host is ignored. */\n\thost: string;\n\t/** Current epoch ms (injectable for tests). */\n\tnow?: number;\n\t/** Freshness window; defaults to {@link COPILOT_CATALOG_CACHE_TTL_MS}. */\n\tttlMs?: number;\n}\n\nfunction sanitizeCachedContext(value: unknown): CopilotModelContext | undefined {\n\tconst record = asRecord(value);\n\tconst contextWindow = toPositiveInt(record?.contextWindow);\n\tif (contextWindow === undefined) return undefined;\n\tconst rawOptions = record?.contextWindowOptions;\n\tif (Array.isArray(rawOptions)) {\n\t\tconst options = rawOptions.map(toPositiveInt).filter((n): n is number => n !== undefined);\n\t\tif (options.length > 1) return { contextWindow, contextWindowOptions: options };\n\t}\n\treturn { contextWindow };\n}\n\n/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */\nexport function readCopilotCatalogCache(\n\tpath: string,\n\toptions: ReadCopilotCatalogCacheOptions,\n): CopilotModelCatalog | undefined {\n\tlet parsed: CopilotCatalogCacheFile;\n\ttry {\n\t\tif (!existsSync(path)) return undefined;\n\t\tparsed = JSON.parse(readFileSync(path, \"utf8\")) as CopilotCatalogCacheFile;\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!parsed || parsed.version !== COPILOT_CATALOG_CACHE_VERSION) return undefined;\n\tif (parsed.host !== options.host) return undefined;\n\tconst now = options.now ?? Date.now();\n\tconst ttlMs = options.ttlMs ?? COPILOT_CATALOG_CACHE_TTL_MS;\n\tif (typeof parsed.fetchedAt !== \"number\" || now - parsed.fetchedAt >= ttlMs) return undefined;\n\tconst models = asRecord(parsed.models);\n\tif (!models) return undefined;\n\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tfor (const [id, value] of Object.entries(models)) {\n\t\tconst context = sanitizeCachedContext(value);\n\t\tif (context) catalog.set(id, context);\n\t}\n\treturn catalog;\n}\n\n/** Write the catalog to the cache file (creating parent dirs). Best-effort; never throws. */\nexport function writeCopilotCatalogCache(\n\tpath: string,\n\tbaseUrl: string,\n\tcatalog: CopilotModelCatalog,\n\tnow?: number,\n): void {\n\tconst payload: CopilotCatalogCacheFile = {\n\t\tversion: COPILOT_CATALOG_CACHE_VERSION,\n\t\thost: hostFromBaseUrl(baseUrl),\n\t\tfetchedAt: now ?? Date.now(),\n\t\tmodels: Object.fromEntries(catalog),\n\t};\n\ttry {\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\twriteFileSync(path, JSON.stringify(payload), \"utf8\");\n\t} catch {\n\t\t// best-effort cache; ignore write failures\n\t}\n}\n\n/** Host component of a base URL, for matching {@link readCopilotCatalogCache} `host`. */\nexport function copilotCatalogCacheHost(baseUrl: string): string {\n\treturn hostFromBaseUrl(baseUrl);\n}\n\n/** Standard on-disk cache path for the Copilot model catalog under an agent directory. */\nexport function copilotCatalogCachePath(agentDir: string): string {\n\treturn join(agentDir, \"cache\", \"copilot-models.json\");\n}\n\n/**\n * Seed the active catalog synchronously from the on-disk cache, gated on a Copilot access token.\n *\n * Called at model-registry construction so a returning user's previously selected long-context\n * window is recognized before startup validation runs — otherwise the persisted choice would warn\n * (\"context window 936k is not supported…\") and reset until the async refresh completes. The cache\n * TTL is intentionally ignored here: stale-but-present windows are still valid for selection, and\n * the async loader independently refetches on its own freshness window. Returns true when a catalog\n * was applied. No-op (returns false) without a token or a host-matching cached catalog.\n */\nexport function seedActiveCopilotModelCatalogFromCache(\n\taccessToken: string | undefined,\n\tcachePath: string,\n\tnow?: number,\n): boolean {\n\tif (typeof accessToken !== \"string\" || accessToken.length === 0) return false;\n\tconst host = copilotCatalogCacheHost(copilotApiBaseUrlFromToken(accessToken));\n\tconst cached = readCopilotCatalogCache(cachePath, { host, now, ttlMs: Number.POSITIVE_INFINITY });\n\tif (!cached) return false;\n\tsetActiveCopilotModelCatalog(cached);\n\treturn true;\n}\n"]}
1
+ {"version":3,"file":"copilot-model-catalog.d.ts","sourceRoot":"","sources":["../../src/core/copilot-model-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAKH,yEAAyE;AACzE,MAAM,WAAW,mBAAmB;IACnC;;;OAGG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB;;;;;OAKG;IACH,oBAAoB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,gEAAgE;AAChE,MAAM,MAAM,mBAAmB,GAAG,WAAW,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAC;AAE3E,wGAAwG;AACxG,eAAO,MAAM,+BAA+B,SAAU,CAAC;AAEvD,eAAO,MAAM,2BAA2B,eAAe,CAAC;AAExD;;;GAGG;AACH,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAMpE,CAAC;AAEF,kGAAkG;AAClG,eAAO,MAAM,4BAA4B,6CAA6C,CAAC;AAEvF,qFAAqF;AACrF,eAAO,MAAM,4BAA4B,QAAiB,CAAC;AAE3D,4CAA4C;AAC5C,eAAO,MAAM,6BAA6B,EAAG,CAAU,CAAC;AAExD;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,MAAM,CASvG;AAcD,uDAAuD;AACvD,MAAM,WAAW,kBAAkB;IAClC,kGAAkG;IAClG,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,0GAA0G;IAC1G,sBAAsB,CAAC,EA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AAgB,uBAAuB,CACtC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,8BAA8B,GACrC,mBAAmB,GAAG,SAAS,CAsBjC;AAED,6FAA6F;AAC7F,wBAAgB,wBAAwB,CACvC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,mBAAmB,EAC5B,GAAG,CAAC,EAAE,MAAM,GACV,IAAI,CAaN;AAED,yFAAyF;AACzF,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED,0FAA0F;AAC1F,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;;;;;;;GASG;AACH,wBAAgB,sCAAsC,CACrD,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,SAAS,EAAE,MAAM,EACjB,GAAG,CAAC,EAAE,MAAM,GACV,OAAO,CAOT","sourcesContent":["/**\n * GitHub Copilot model catalog (CAPI) — dynamic prompt-token budgets.\n *\n * GitHub's Copilot API (CAPI) exposes distinct model limits via `GET {baseUrl}/models`:\n *\n * - `capabilities.limits.max_context_window_tokens` is the model's total context capacity\n * (prompt + completion reserve).\n * - `capabilities.limits.max_prompt_tokens` is the maximum prompt/input budget Atomic can safely\n * fill before the provider must reserve output tokens.\n * - `billing.token_prices.<tier>.context_max` is a prompt-token billing/selection threshold. The\n * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a\n * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).\n *\n * Atomic shows the model's full context window for the selectable long tier (the\n * `max_context_window_tokens` total, e.g. 1_000_000/1_050_000), matching how the native `openai/*`\n * and `anthropic/*` providers advertise these models. Because GitHub enforces a lower server-side\n * prompt cap (`max_prompt_tokens`, e.g. 936k/922k) below that total, the prompt cap is retained as\n * an internal effective input budget (`CopilotModelContext.maxInputTokens`) that drives compaction\n * thresholds and the overflow-recovery guard, so the branded total can be displayed without\n * overrunning the server limit. The default (short) tier stays at the `default` billing tier's\n * prompt budget.\n *\n * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers\n * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes\n * stale. Instead the catalog is fetched live (gated on the user actually having the GitHub Copilot\n * provider) and cached on disk for a short TTL, exactly like the Copilot CLI.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/** Resolved input-token context window(s) for a single Copilot model. */\nexport interface CopilotModelContext {\n\t/**\n\t * Base/displayed context window — shown in the footer. The default tier's `context_max`, or the\n\t * model-level `max_prompt_tokens` fallback otherwise.\n\t */\n\tcontextWindow: number;\n\t/**\n\t * Selectable windows (`[default, long]`) when the model exposes a `long_context` tier larger than\n\t * its default; absent for single-window models. The long entry is the model's full\n\t * `max_context_window_tokens` (total capacity) when advertised, matching `openai/*` and\n\t * `anthropic/*`.\n\t */\n\tcontextWindowOptions?: readonly number[];\n\t/**\n\t * Hard prompt/input cap (`max_prompt_tokens`) when it sits below the displayed long window. Used\n\t * as the effective input budget for compaction thresholds and overflow recovery so the branded\n\t * total can be shown without overrunning GitHub's server-side prompt limit. Absent when the\n\t * displayed window already equals the input cap.\n\t */\n\tmaxInputTokens?: number;\n}\n\n/** Map of model id → resolved input-token context window(s). */\nexport type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;\n\n/** Safety fallback when a model reports neither `max_prompt_tokens` nor `max_context_window_tokens`. */\nexport const COPILOT_CONTEXT_WINDOW_FALLBACK = 128_000;\n\nexport const COPILOT_CATALOG_API_VERSION = \"2026-06-01\";\n\n/**\n * Headers GitHub's CAPI expects for catalog reads. Mirrors the editor headers pi-ai already sends\n * for Copilot token refresh and model-policy calls, plus the dated API version.\n */\nexport const COPILOT_CATALOG_HEADERS: Readonly<Record<string, string>> = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n\t\"X-GitHub-Api-Version\": COPILOT_CATALOG_API_VERSION,\n};\n\n/** Default (non-enterprise) Copilot CAPI base URL when the token has no resolvable `proxy-ep`. */\nexport const DEFAULT_COPILOT_API_BASE_URL = \"https://api.individual.githubcopilot.com\";\n\n/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */\nexport const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;\n\n/** Current on-disk cache schema version. */\nexport const COPILOT_CATALOG_CACHE_VERSION = 3 as const;\n\n/**\n * Resolve the Copilot CAPI base URL.\n *\n * Copilot access tokens embed a `proxy-ep=proxy.<host>` segment; the API host is the same host with\n * `proxy.` swapped for `api.`. Falls back to the enterprise host or the individual default. (pi-ai\n * exposes an equivalent helper, but its published `dist` mangles the export name, so the small,\n * stable parsing logic is reimplemented here.)\n */\nexport function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string {\n\tif (token) {\n\t\tconst match = token.match(/proxy-ep=([^;]+)/);\n\t\tif (match) {\n\t\t\treturn `https://${match[1].replace(/^proxy\\./, \"api.\")}`;\n\t\t}\n\t}\n\tif (enterpriseDomain) return `https://copilot-api.${enterpriseDomain}`;\n\treturn DEFAULT_COPILOT_API_BASE_URL;\n}\n\nfunction trimTrailingSlash(url: string): string {\n\treturn url.replace(/\\/+$/, \"\");\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n\treturn value && typeof value === \"object\" ? (value as Record<string, unknown>) : undefined;\n}\n\nfunction toPositiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\n/** Raw token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget (the hard input cap). */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens` — total context capacity (the displayed long tier). */\n\tmaxContextWindowTokens?: number;\n\t/** `capabilities.limits.max_output_tokens` — output reserve; derives the input cap when `max_prompt_tokens` is absent. */\n\tmaxOutputTokens?: number;\n\t/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */\n\tlongContextMax?: number;\n}\n\n/**\n * Resolve a model's input-token context window(s) from its CAPI limits.\n *\n * `contextWindow` is the model's base input budget — the default tier's `context_max` when tiered,\n * otherwise `max_prompt_tokens ?? max_context_window_tokens ?? 128_000`. A `long_context` tier that\n * is larger than the base adds a second selectable window. Returns `undefined` when the entry\n * carries no usable limit signal at all.\n */\nexport function resolveCopilotModelContext(limits: CopilotModelLimits): CopilotModelContext | undefined {\n\tconst hasSignal =\n\t\tlimits.maxPromptTokens !== undefined ||\n\t\tlimits.maxContextWindowTokens !== undefined ||\n\t\tlimits.defaultContextMax !== undefined ||\n\t\tlimits.longContextMax !== undefined;\n\tif (!hasSignal) return undefined;\n\n\tconst maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;\n\tconst base = limits.defaultContextMax ?? maxInput;\n\tif (limits.longContextMax !== undefined && limits.longContextMax > base) {\n\t\t// Display the model's full context window as the long tier (matching openai/* and anthropic/*)\n\t\t// when CAPI advertises it; otherwise fall back to the long-context prompt threshold for\n\t\t// older/sparse payloads.\n\t\tconst longWindow = limits.maxContextWindowTokens ?? limits.longContextMax;\n\t\t// The hard prompt/input cap GitHub enforces server-side: prefer max_prompt_tokens, else derive\n\t\t// it from total − output reserve, else fall back to the long-context prompt threshold.\n\t\tconst derivedInputCap =\n\t\t\tlimits.maxContextWindowTokens !== undefined && limits.maxOutputTokens !== undefined\n\t\t\t\t? limits.maxContextWindowTokens - limits.maxOutputTokens\n\t\t\t\t: undefined;\n\t\tconst inputCap =\n\t\t\tlimits.maxPromptTokens ??\n\t\t\t(derivedInputCap !== undefined && derivedInputCap > 0 ? derivedInputCap : undefined) ??\n\t\t\tlimits.longContextMax;\n\t\t// Only carry the cap when the displayed long window actually exceeds it (the branded-total\n\t\t// case); when they coincide there is no gap and the input budget is just the window.\n\t\treturn longWindow > inputCap\n\t\t\t? { contextWindow: base, contextWindowOptions: [base, longWindow], maxInputTokens: inputCap }\n\t\t\t: { contextWindow: base, contextWindowOptions: [base, longWindow] };\n\t}\n\treturn { contextWindow: base };\n}\n\n/**\n * Parse a raw CAPI `/models` response body into an input-token context-window catalog.\n */\nexport function parseCopilotModelCatalog(body: unknown): CopilotModelCatalog {\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tconst data = asRecord(body)?.data;\n\tif (!Array.isArray(data)) return catalog;\n\n\tfor (const entry of data) {\n\t\tconst record = asRecord(entry);\n\t\tif (!record) continue;\n\t\tconst id = record.id;\n\t\tif (typeof id !== \"string\" || id.length === 0) continue;\n\n\t\tconst limits = asRecord(asRecord(record.capabilities)?.limits);\n\t\tconst prices = asRecord(asRecord(record.billing)?.token_prices);\n\t\tconst context = resolveCopilotModelContext({\n\t\t\tmaxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),\n\t\t\tmaxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),\n\t\t\tmaxOutputTokens: toPositiveInt(limits?.max_output_tokens),\n\t\t\tdefaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),\n\t\t\tlongContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),\n\t\t});\n\t\tif (context) catalog.set(id, context);\n\t}\n\n\treturn catalog;\n}\n\nexport interface FetchCopilotModelCatalogOptions {\n\t/** Valid Copilot CAPI bearer token (e.g. from `modelRegistry.getApiKeyForProvider`). */\n\ttoken: string;\n\t/** Override the resolved base URL; defaults to one derived from the token. */\n\tbaseUrl?: string;\n\t/** Enterprise domain, used for base-URL resolution when the token lacks a `proxy-ep`. */\n\tenterpriseDomain?: string;\n\t/** Extra/override request headers. */\n\theaders?: Record<string, string>;\n\t/** Injectable `fetch` for testing. */\n\tfetchImpl?: typeof fetch;\n\t/** Abort signal. */\n\tsignal?: AbortSignal;\n}\n\n/** Fetch and parse the live Copilot model catalog from CAPI `GET {baseUrl}/models`. */\nexport async function fetchCopilotModelCatalog(options: FetchCopilotModelCatalogOptions): Promise<CopilotModelCatalog> {\n\tconst fetchImpl = options.fetchImpl ?? fetch;\n\tconst baseUrl = options.baseUrl ?? copilotApiBaseUrlFromToken(options.token, options.enterpriseDomain);\n\tconst response = await fetchImpl(`${trimTrailingSlash(baseUrl)}/models`, {\n\t\tmethod: \"GET\",\n\t\theaders: {\n\t\t\tAccept: \"application/json\",\n\t\t\tAuthorization: `Bearer ${options.token}`,\n\t\t\t...COPILOT_CATALOG_HEADERS,\n\t\t\t...options.headers,\n\t\t},\n\t\t...(options.signal ? { signal: options.signal } : {}),\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot /models request failed: ${response.status} ${response.statusText}`);\n\t}\n\treturn parseCopilotModelCatalog(await response.json());\n}\n\n// ----------------------------------------------------------------------------\n// Active in-memory catalog (consulted by the model registry).\n//\n// Empty by default, so with no Copilot auth / no successful fetch the registry leaves Copilot\n// model context windows untouched and the picker never appears.\n// ----------------------------------------------------------------------------\n\nlet activeCatalog: CopilotModelCatalog = new Map();\n\n/** Replace the active catalog the registry derives context windows from. */\nexport function setActiveCopilotModelCatalog(catalog: CopilotModelCatalog): void {\n\tactiveCatalog = catalog;\n}\n\n/** The active catalog (empty until a successful auth-gated fetch/cache load). */\nexport function getActiveCopilotModelCatalog(): CopilotModelCatalog {\n\treturn activeCatalog;\n}\n\n/** Reset the active catalog (primarily for tests). */\nexport function clearActiveCopilotModelCatalog(): void {\n\tactiveCatalog = new Map();\n}\n\n// ----------------------------------------------------------------------------\n// Disk cache.\n// ----------------------------------------------------------------------------\n\ninterface CopilotCatalogCacheFile {\n\tversion: typeof COPILOT_CATALOG_CACHE_VERSION;\n\t/** CAPI host the catalog was fetched from; cache misses on host change (e.g. enterprise switch). */\n\thost: string;\n\t/** Epoch ms the catalog was fetched. */\n\tfetchedAt: number;\n\tmodels: Record<string, CopilotModelContext>;\n}\n\nfunction hostFromBaseUrl(baseUrl: string): string {\n\ttry {\n\t\treturn new URL(baseUrl).host;\n\t} catch {\n\t\treturn baseUrl;\n\t}\n}\n\nexport interface ReadCopilotCatalogCacheOptions {\n\t/** Expected CAPI host; a cached file from a different host is ignored. */\n\thost: string;\n\t/** Current epoch ms (injectable for tests). */\n\tnow?: number;\n\t/** Freshness window; defaults to {@link COPILOT_CATALOG_CACHE_TTL_MS}. */\n\tttlMs?: number;\n}\n\nfunction sanitizeCachedContext(value: unknown): CopilotModelContext | undefined {\n\tconst record = asRecord(value);\n\tconst contextWindow = toPositiveInt(record?.contextWindow);\n\tif (contextWindow === undefined) return undefined;\n\tconst maxInputTokens = toPositiveInt(record?.maxInputTokens);\n\tconst rawOptions = record?.contextWindowOptions;\n\tif (Array.isArray(rawOptions)) {\n\t\tconst options = rawOptions.map(toPositiveInt).filter((n): n is number => n !== undefined);\n\t\tif (options.length > 1) {\n\t\t\treturn maxInputTokens !== undefined\n\t\t\t\t? { contextWindow, contextWindowOptions: options, maxInputTokens }\n\t\t\t\t: { contextWindow, contextWindowOptions: options };\n\t\t}\n\t}\n\treturn maxInputTokens !== undefined ? { contextWindow, maxInputTokens } : { contextWindow };\n}\n\n/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */\nexport function readCopilotCatalogCache(\n\tpath: string,\n\toptions: ReadCopilotCatalogCacheOptions,\n): CopilotModelCatalog | undefined {\n\tlet parsed: CopilotCatalogCacheFile;\n\ttry {\n\t\tif (!existsSync(path)) return undefined;\n\t\tparsed = JSON.parse(readFileSync(path, \"utf8\")) as CopilotCatalogCacheFile;\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!parsed || parsed.version !== COPILOT_CATALOG_CACHE_VERSION) return undefined;\n\tif (parsed.host !== options.host) return undefined;\n\tconst now = options.now ?? Date.now();\n\tconst ttlMs = options.ttlMs ?? COPILOT_CATALOG_CACHE_TTL_MS;\n\tif (typeof parsed.fetchedAt !== \"number\" || now - parsed.fetchedAt >= ttlMs) return undefined;\n\tconst models = asRecord(parsed.models);\n\tif (!models) return undefined;\n\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tfor (const [id, value] of Object.entries(models)) {\n\t\tconst context = sanitizeCachedContext(value);\n\t\tif (context) catalog.set(id, context);\n\t}\n\treturn catalog;\n}\n\n/** Write the catalog to the cache file (creating parent dirs). Best-effort; never throws. */\nexport function writeCopilotCatalogCache(\n\tpath: string,\n\tbaseUrl: string,\n\tcatalog: CopilotModelCatalog,\n\tnow?: number,\n): void {\n\tconst payload: CopilotCatalogCacheFile = {\n\t\tversion: COPILOT_CATALOG_CACHE_VERSION,\n\t\thost: hostFromBaseUrl(baseUrl),\n\t\tfetchedAt: now ?? Date.now(),\n\t\tmodels: Object.fromEntries(catalog),\n\t};\n\ttry {\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\twriteFileSync(path, JSON.stringify(payload), \"utf8\");\n\t} catch {\n\t\t// best-effort cache; ignore write failures\n\t}\n}\n\n/** Host component of a base URL, for matching {@link readCopilotCatalogCache} `host`. */\nexport function copilotCatalogCacheHost(baseUrl: string): string {\n\treturn hostFromBaseUrl(baseUrl);\n}\n\n/** Standard on-disk cache path for the Copilot model catalog under an agent directory. */\nexport function copilotCatalogCachePath(agentDir: string): string {\n\treturn join(agentDir, \"cache\", \"copilot-models.json\");\n}\n\n/**\n * Seed the active catalog synchronously from the on-disk cache, gated on a Copilot access token.\n *\n * Called at model-registry construction so a returning user's previously selected long-context\n * window is recognized before startup validation runs — otherwise the persisted choice would warn\n * (\"context window 936k is not supported…\") and reset until the async refresh completes. The cache\n * TTL is intentionally ignored here: stale-but-present windows are still valid for selection, and\n * the async loader independently refetches on its own freshness window. Returns true when a catalog\n * was applied. No-op (returns false) without a token or a host-matching cached catalog.\n */\nexport function seedActiveCopilotModelCatalogFromCache(\n\taccessToken: string | undefined,\n\tcachePath: string,\n\tnow?: number,\n): boolean {\n\tif (typeof accessToken !== \"string\" || accessToken.length === 0) return false;\n\tconst host = copilotCatalogCacheHost(copilotApiBaseUrlFromToken(accessToken));\n\tconst cached = readCopilotCatalogCache(cachePath, { host, now, ttlMs: Number.POSITIVE_INFINITY });\n\tif (!cached) return false;\n\tsetActiveCopilotModelCatalog(cached);\n\treturn true;\n}\n"]}
@@ -11,10 +11,14 @@
11
11
  * `default` tier is the short prompt budget (e.g. gpt-5.5 272k, Claude 200k); a
12
12
  * `long_context` tier adds a selectable larger prompt budget (e.g. gpt-5.5 922k, Claude 936k).
13
13
  *
14
- * Atomic's current `contextWindow` drives local prompt collection, compaction thresholds, footer
15
- * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token
16
- * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field
17
- * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.
14
+ * Atomic shows the model's full context window for the selectable long tier (the
15
+ * `max_context_window_tokens` total, e.g. 1_000_000/1_050_000), matching how the native `openai/*`
16
+ * and `anthropic/*` providers advertise these models. Because GitHub enforces a lower server-side
17
+ * prompt cap (`max_prompt_tokens`, e.g. 936k/922k) below that total, the prompt cap is retained as
18
+ * an internal effective input budget (`CopilotModelContext.maxInputTokens`) that drives compaction
19
+ * thresholds and the overflow-recovery guard, so the branded total can be displayed without
20
+ * overrunning the server limit. The default (short) tier stays at the `default` billing tier's
21
+ * prompt budget.
18
22
  *
19
23
  * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers
20
24
  * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes
@@ -42,7 +46,7 @@ export const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.githubcopilo
42
46
  /** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */
43
47
  export const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;
44
48
  /** Current on-disk cache schema version. */
45
- export const COPILOT_CATALOG_CACHE_VERSION = 2;
49
+ export const COPILOT_CATALOG_CACHE_VERSION = 3;
46
50
  /**
47
51
  * Resolve the Copilot CAPI base URL.
48
52
  *
@@ -89,7 +93,23 @@ export function resolveCopilotModelContext(limits) {
89
93
  const maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;
90
94
  const base = limits.defaultContextMax ?? maxInput;
91
95
  if (limits.longContextMax !== undefined && limits.longContextMax > base) {
92
- return { contextWindow: base, contextWindowOptions: [base, limits.longContextMax] };
96
+ // Display the model's full context window as the long tier (matching openai/* and anthropic/*)
97
+ // when CAPI advertises it; otherwise fall back to the long-context prompt threshold for
98
+ // older/sparse payloads.
99
+ const longWindow = limits.maxContextWindowTokens ?? limits.longContextMax;
100
+ // The hard prompt/input cap GitHub enforces server-side: prefer max_prompt_tokens, else derive
101
+ // it from total − output reserve, else fall back to the long-context prompt threshold.
102
+ const derivedInputCap = limits.maxContextWindowTokens !== undefined && limits.maxOutputTokens !== undefined
103
+ ? limits.maxContextWindowTokens - limits.maxOutputTokens
104
+ : undefined;
105
+ const inputCap = limits.maxPromptTokens ??
106
+ (derivedInputCap !== undefined && derivedInputCap > 0 ? derivedInputCap : undefined) ??
107
+ limits.longContextMax;
108
+ // Only carry the cap when the displayed long window actually exceeds it (the branded-total
109
+ // case); when they coincide there is no gap and the input budget is just the window.
110
+ return longWindow > inputCap
111
+ ? { contextWindow: base, contextWindowOptions: [base, longWindow], maxInputTokens: inputCap }
112
+ : { contextWindow: base, contextWindowOptions: [base, longWindow] };
93
113
  }
94
114
  return { contextWindow: base };
95
115
  }
@@ -113,6 +133,7 @@ export function parseCopilotModelCatalog(body) {
113
133
  const context = resolveCopilotModelContext({
114
134
  maxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),
115
135
  maxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),
136
+ maxOutputTokens: toPositiveInt(limits?.max_output_tokens),
116
137
  defaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),
117
138
  longContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),
118
139
  });
@@ -172,13 +193,17 @@ function sanitizeCachedContext(value) {
172
193
  const contextWindow = toPositiveInt(record?.contextWindow);
173
194
  if (contextWindow === undefined)
174
195
  return undefined;
196
+ const maxInputTokens = toPositiveInt(record?.maxInputTokens);
175
197
  const rawOptions = record?.contextWindowOptions;
176
198
  if (Array.isArray(rawOptions)) {
177
199
  const options = rawOptions.map(toPositiveInt).filter((n) => n !== undefined);
178
- if (options.length > 1)
179
- return { contextWindow, contextWindowOptions: options };
200
+ if (options.length > 1) {
201
+ return maxInputTokens !== undefined
202
+ ? { contextWindow, contextWindowOptions: options, maxInputTokens }
203
+ : { contextWindow, contextWindowOptions: options };
204
+ }
180
205
  }
181
- return { contextWindow };
206
+ return maxInputTokens !== undefined ? { contextWindow, maxInputTokens } : { contextWindow };
182
207
  }
183
208
  /** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */
184
209
  export function readCopilotCatalogCache(path, options) {
@@ -1 +1 @@
1
- {"version":3,"file":"copilot-model-catalog.js","sourceRoot":"","sources":["../../src/core/copilot-model-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAmB1C,wGAAwG;AACxG,MAAM,CAAC,MAAM,+BAA+B,GAAG,OAAO,CAAC;AAEvD,MAAM,CAAC,MAAM,2BAA2B,GAAG,YAAY,CAAC;AAExD;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAqC;IACxE,YAAY,EAAE,0BAA0B;IACxC,gBAAgB,EAAE,gBAAgB;IAClC,uBAAuB,EAAE,qBAAqB;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AAC;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's current `contextWindow` drives local prompt collection, compaction thresholds, footer\n * usage, and overflow avoidance, so Copilot selectable windows intentionally use prompt-token\n * budgets rather than total context capacities such as 1_000_000/1_050_000. The total context field\n * remains a compatibility fallback for older/sparse payloads that omit `max_prompt_tokens`.\n *\n * This data is intentionally NOT baked into a static map: GitHub adds/removes models and retiers\n * windows over time (e.g. a model that disappears from the catalog), so a hardcoded snapshot goes\n * stale. Instead the catalog is fetched live (gated on the user actually having the GitHub Copilot\n * provider) and cached on disk for a short TTL, exactly like the Copilot CLI.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\n\n/** Resolved input-token context window(s) for a single Copilot model. */\nexport interface CopilotModelContext {\n\t/**\n\t * Base context window in INPUT tokens — shown in the footer and used for compaction. The\n\t * default tier's `context_max`, or the model-level `max_prompt_tokens` fallback otherwise.\n\t */\n\tcontextWindow: number;\n\t/**\n\t * Selectable input-token windows (`[default, long]`) when the model exposes a `long_context`\n\t * tier larger than its default; absent for single-window models.\n\t */\n\tcontextWindowOptions?: readonly number[];\n}\n\n/** Map of model id → resolved input-token context window(s). */\nexport type CopilotModelCatalog = ReadonlyMap<string, CopilotModelContext>;\n\n/** Safety fallback when a model reports neither `max_prompt_tokens` nor `max_context_window_tokens`. */\nexport const COPILOT_CONTEXT_WINDOW_FALLBACK = 128_000;\n\nexport const COPILOT_CATALOG_API_VERSION = \"2026-06-01\";\n\n/**\n * Headers GitHub's CAPI expects for catalog reads. Mirrors the editor headers pi-ai already sends\n * for Copilot token refresh and model-policy calls, plus the dated API version.\n */\nexport const COPILOT_CATALOG_HEADERS: Readonly<Record<string, string>> = {\n\t\"User-Agent\": \"GitHubCopilotChat/0.35.0\",\n\t\"Editor-Version\": \"vscode/1.107.0\",\n\t\"Editor-Plugin-Version\": \"copilot-chat/0.35.0\",\n\t\"Copilot-Integration-Id\": \"vscode-chat\",\n\t\"X-GitHub-Api-Version\": COPILOT_CATALOG_API_VERSION,\n};\n\n/** Default (non-enterprise) Copilot CAPI base URL when the token has no resolvable `proxy-ep`. */\nexport const DEFAULT_COPILOT_API_BASE_URL = \"https://api.individual.githubcopilot.com\";\n\n/** Disk-cache freshness window, matching the Copilot CLI's list-models cache TTL. */\nexport const COPILOT_CATALOG_CACHE_TTL_MS = 30 * 60 * 1000;\n\n/** Current on-disk cache schema version. */\nexport const COPILOT_CATALOG_CACHE_VERSION = 2 as const;\n\n/**\n * Resolve the Copilot CAPI base URL.\n *\n * Copilot access tokens embed a `proxy-ep=proxy.<host>` segment; the API host is the same host with\n * `proxy.` swapped for `api.`. Falls back to the enterprise host or the individual default. (pi-ai\n * exposes an equivalent helper, but its published `dist` mangles the export name, so the small,\n * stable parsing logic is reimplemented here.)\n */\nexport function copilotApiBaseUrlFromToken(token: string | undefined, enterpriseDomain?: string): string {\n\tif (token) {\n\t\tconst match = token.match(/proxy-ep=([^;]+)/);\n\t\tif (match) {\n\t\t\treturn `https://${match[1].replace(/^proxy\\./, \"api.\")}`;\n\t\t}\n\t}\n\tif (enterpriseDomain) return `https://copilot-api.${enterpriseDomain}`;\n\treturn DEFAULT_COPILOT_API_BASE_URL;\n}\n\nfunction trimTrailingSlash(url: string): string {\n\treturn url.replace(/\\/+$/, \"\");\n}\n\nfunction asRecord(value: unknown): Record<string, unknown> | undefined {\n\treturn value && typeof value === \"object\" ? (value as Record<string, unknown>) : undefined;\n}\n\nfunction toPositiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\n/** Raw token limits parsed from a CAPI model entry. */\nexport interface CopilotModelLimits {\n\t/** `capabilities.limits.max_prompt_tokens` — maximum prompt/input budget. */\n\tmaxPromptTokens?: number;\n\t/** `capabilities.limits.max_context_window_tokens` — total context capacity, used only as a fallback. */\n\tmaxContextWindowTokens?: number;\n\t/** `billing.token_prices.default.context_max` — default-tier prompt threshold. */\n\tdefaultContextMax?: number;\n\t/** `billing.token_prices.long_context.context_max` — long-context prompt threshold. */\n\tlongContextMax?: number;\n}\n\n/**\n * Resolve a model's input-token context window(s) from its CAPI limits.\n *\n * `contextWindow` is the model's base input budget — the default tier's `context_max` when tiered,\n * otherwise `max_prompt_tokens ?? max_context_window_tokens ?? 128_000`. A `long_context` tier that\n * is larger than the base adds a second selectable window. Returns `undefined` when the entry\n * carries no usable limit signal at all.\n */\nexport function resolveCopilotModelContext(limits: CopilotModelLimits): CopilotModelContext | undefined {\n\tconst hasSignal =\n\t\tlimits.maxPromptTokens !== undefined ||\n\t\tlimits.maxContextWindowTokens !== undefined ||\n\t\tlimits.defaultContextMax !== undefined ||\n\t\tlimits.longContextMax !== undefined;\n\tif (!hasSignal) return undefined;\n\n\tconst maxInput = limits.maxPromptTokens ?? limits.maxContextWindowTokens ?? COPILOT_CONTEXT_WINDOW_FALLBACK;\n\tconst base = limits.defaultContextMax ?? maxInput;\n\tif (limits.longContextMax !== undefined && limits.longContextMax > base) {\n\t\treturn { contextWindow: base, contextWindowOptions: [base, limits.longContextMax] };\n\t}\n\treturn { contextWindow: base };\n}\n\n/**\n * Parse a raw CAPI `/models` response body into an input-token context-window catalog.\n */\nexport function parseCopilotModelCatalog(body: unknown): CopilotModelCatalog {\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tconst data = asRecord(body)?.data;\n\tif (!Array.isArray(data)) return catalog;\n\n\tfor (const entry of data) {\n\t\tconst record = asRecord(entry);\n\t\tif (!record) continue;\n\t\tconst id = record.id;\n\t\tif (typeof id !== \"string\" || id.length === 0) continue;\n\n\t\tconst limits = asRecord(asRecord(record.capabilities)?.limits);\n\t\tconst prices = asRecord(asRecord(record.billing)?.token_prices);\n\t\tconst context = resolveCopilotModelContext({\n\t\t\tmaxPromptTokens: toPositiveInt(limits?.max_prompt_tokens),\n\t\t\tmaxContextWindowTokens: toPositiveInt(limits?.max_context_window_tokens),\n\t\t\tdefaultContextMax: toPositiveInt(asRecord(prices?.default)?.context_max),\n\t\t\tlongContextMax: toPositiveInt(asRecord(prices?.long_context)?.context_max),\n\t\t});\n\t\tif (context) catalog.set(id, context);\n\t}\n\n\treturn catalog;\n}\n\nexport interface FetchCopilotModelCatalogOptions {\n\t/** Valid Copilot CAPI bearer token (e.g. from `modelRegistry.getApiKeyForProvider`). */\n\ttoken: string;\n\t/** Override the resolved base URL; defaults to one derived from the token. */\n\tbaseUrl?: string;\n\t/** Enterprise domain, used for base-URL resolution when the token lacks a `proxy-ep`. */\n\tenterpriseDomain?: string;\n\t/** Extra/override request headers. */\n\theaders?: Record<string, string>;\n\t/** Injectable `fetch` for testing. */\n\tfetchImpl?: typeof fetch;\n\t/** Abort signal. */\n\tsignal?: AbortSignal;\n}\n\n/** Fetch and parse the live Copilot model catalog from CAPI `GET {baseUrl}/models`. */\nexport async function fetchCopilotModelCatalog(options: FetchCopilotModelCatalogOptions): Promise<CopilotModelCatalog> {\n\tconst fetchImpl = options.fetchImpl ?? fetch;\n\tconst baseUrl = options.baseUrl ?? copilotApiBaseUrlFromToken(options.token, options.enterpriseDomain);\n\tconst response = await fetchImpl(`${trimTrailingSlash(baseUrl)}/models`, {\n\t\tmethod: \"GET\",\n\t\theaders: {\n\t\t\tAccept: \"application/json\",\n\t\t\tAuthorization: `Bearer ${options.token}`,\n\t\t\t...COPILOT_CATALOG_HEADERS,\n\t\t\t...options.headers,\n\t\t},\n\t\t...(options.signal ? { signal: options.signal } : {}),\n\t});\n\tif (!response.ok) {\n\t\tthrow new Error(`GitHub Copilot /models request failed: ${response.status} ${response.statusText}`);\n\t}\n\treturn parseCopilotModelCatalog(await response.json());\n}\n\n// ----------------------------------------------------------------------------\n// Active in-memory catalog (consulted by the model registry).\n//\n// Empty by default, so with no Copilot auth / no successful fetch the registry leaves Copilot\n// model context windows untouched and the picker never appears.\n// ----------------------------------------------------------------------------\n\nlet activeCatalog: CopilotModelCatalog = new Map();\n\n/** Replace the active catalog the registry derives context windows from. */\nexport function setActiveCopilotModelCatalog(catalog: CopilotModelCatalog): void {\n\tactiveCatalog = catalog;\n}\n\n/** The active catalog (empty until a successful auth-gated fetch/cache load). */\nexport function getActiveCopilotModelCatalog(): CopilotModelCatalog {\n\treturn activeCatalog;\n}\n\n/** Reset the active catalog (primarily for tests). */\nexport function clearActiveCopilotModelCatalog(): void {\n\tactiveCatalog = new Map();\n}\n\n// ----------------------------------------------------------------------------\n// Disk cache.\n// ----------------------------------------------------------------------------\n\ninterface CopilotCatalogCacheFile {\n\tversion: typeof COPILOT_CATALOG_CACHE_VERSION;\n\t/** CAPI host the catalog was fetched from; cache misses on host change (e.g. enterprise switch). */\n\thost: string;\n\t/** Epoch ms the catalog was fetched. */\n\tfetchedAt: number;\n\tmodels: Record<string, CopilotModelContext>;\n}\n\nfunction hostFromBaseUrl(baseUrl: string): string {\n\ttry {\n\t\treturn new URL(baseUrl).host;\n\t} catch {\n\t\treturn baseUrl;\n\t}\n}\n\nexport interface ReadCopilotCatalogCacheOptions {\n\t/** Expected CAPI host; a cached file from a different host is ignored. */\n\thost: string;\n\t/** Current epoch ms (injectable for tests). */\n\tnow?: number;\n\t/** Freshness window; defaults to {@link COPILOT_CATALOG_CACHE_TTL_MS}. */\n\tttlMs?: number;\n}\n\nfunction sanitizeCachedContext(value: unknown): CopilotModelContext | undefined {\n\tconst record = asRecord(value);\n\tconst contextWindow = toPositiveInt(record?.contextWindow);\n\tif (contextWindow === undefined) return undefined;\n\tconst rawOptions = record?.contextWindowOptions;\n\tif (Array.isArray(rawOptions)) {\n\t\tconst options = rawOptions.map(toPositiveInt).filter((n): n is number => n !== undefined);\n\t\tif (options.length > 1) return { contextWindow, contextWindowOptions: options };\n\t}\n\treturn { contextWindow };\n}\n\n/** Read a fresh, host-matching catalog from the cache file, or `undefined` if missing/stale/invalid. */\nexport function readCopilotCatalogCache(\n\tpath: string,\n\toptions: ReadCopilotCatalogCacheOptions,\n): CopilotModelCatalog | undefined {\n\tlet parsed: CopilotCatalogCacheFile;\n\ttry {\n\t\tif (!existsSync(path)) return undefined;\n\t\tparsed = JSON.parse(readFileSync(path, \"utf8\")) as CopilotCatalogCacheFile;\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!parsed || parsed.version !== COPILOT_CATALOG_CACHE_VERSION) return undefined;\n\tif (parsed.host !== options.host) return undefined;\n\tconst now = options.now ?? Date.now();\n\tconst ttlMs = options.ttlMs ?? COPILOT_CATALOG_CACHE_TTL_MS;\n\tif (typeof parsed.fetchedAt !== \"number\" || now - parsed.fetchedAt >= ttlMs) return undefined;\n\tconst models = asRecord(parsed.models);\n\tif (!models) return undefined;\n\n\tconst catalog = new Map<string, CopilotModelContext>();\n\tfor (const [id, value] of Object.entries(models)) {\n\t\tconst context = sanitizeCachedContext(value);\n\t\tif (context) catalog.set(id, context);\n\t}\n\treturn catalog;\n}\n\n/** Write the catalog to the cache file (creating parent dirs). Best-effort; never throws. */\nexport function writeCopilotCatalogCache(\n\tpath: string,\n\tbaseUrl: string,\n\tcatalog: CopilotModelCatalog,\n\tnow?: number,\n): void {\n\tconst payload: CopilotCatalogCacheFile = {\n\t\tversion: COPILOT_CATALOG_CACHE_VERSION,\n\t\thost: hostFromBaseUrl(baseUrl),\n\t\tfetchedAt: now ?? Date.now(),\n\t\tmodels: Object.fromEntries(catalog),\n\t};\n\ttry {\n\t\tmkdirSync(dirname(path), { recursive: true });\n\t\twriteFileSync(path, JSON.stringify(payload), \"utf8\");\n\t} catch {\n\t\t// best-effort cache; ignore write failures\n\t}\n}\n\n/** Host component of a base URL, for matching {@link readCopilotCatalogCache} `host`. */\nexport function copilotCatalogCacheHost(baseUrl: string): string {\n\treturn hostFromBaseUrl(baseUrl);\n}\n\n/** Standard on-disk cache path for the Copilot model catalog under an agent directory. */\nexport function copilotCatalogCachePath(agentDir: string): string {\n\treturn join(agentDir, \"cache\", \"copilot-models.json\");\n}\n\n/**\n * Seed the active catalog synchronously from the on-disk cache, gated on a Copilot access token.\n *\n * Called at model-registry construction so a returning user's previously selected long-context\n * window is recognized before startup validation runs — otherwise the persisted choice would warn\n * (\"context window 936k is not supported…\") and reset until the async refresh completes. The cache\n * TTL is intentionally ignored here: stale-but-present windows are still valid for selection, and\n * the async loader independently refetches on its own freshness window. Returns true when a catalog\n * was applied. No-op (returns false) without a token or a host-matching cached catalog.\n */\nexport function seedActiveCopilotModelCatalogFromCache(\n\taccessToken: string | undefined,\n\tcachePath: string,\n\tnow?: number,\n): boolean {\n\tif (typeof accessToken !== \"string\" || accessToken.length === 0) return false;\n\tconst host = copilotCatalogCacheHost(copilotApiBaseUrlFromToken(accessToken));\n\tconst cached = readCopilotCatalogCache(cachePath, { host, now, ttlMs: Number.POSITIVE_INFINITY });\n\tif (!cached) return false;\n\tsetActiveCopilotModelCatalog(cached);\n\treturn true;\n}\n"]}
1
+ {"version":3,"file":"copilot-model-catalog.js","sourceRo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;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"]}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Canonical reconstruction of flattened tool-call arguments.
3
+ *
4
+ * Some upstream providers — notably GitHub Copilot Gemini models proxied through
5
+ * Google's GenAI API — serialize array/object function-call arguments as
6
+ * flattened, indexed keys on the wire. For example a tool called with
7
+ * `{ keywords: ["a", "b"] }` arrives as `{ "keywords[0]": "a", "keywords[1]": "b" }`,
8
+ * and `{ files: [{ path }] }` as `{ "files[0].path": "..." }`.
9
+ *
10
+ * This module is the single source of truth for turning those flattened keys
11
+ * back into nested arrays/objects. Both the host runtime's per-tool
12
+ * normalization (gated to Copilot Gemini, schema-aware) and the MCP `callTool`
13
+ * boundary (provider-agnostic, bracket self-gating) delegate here so the two
14
+ * paths cannot drift — in particular so the prototype-pollution guard lives in
15
+ * exactly one place.
16
+ *
17
+ * Security: argument keys cross a trust boundary (model/provider wire → tool /
18
+ * MCP server validation). A key path that walks through `__proto__`,
19
+ * `constructor`, or `prototype` could otherwise reach `Object.prototype` and
20
+ * mutate it process-wide. Any key whose path contains such a segment — at any
21
+ * position, including the final segment and a literal plain key — is dropped.
22
+ */
23
+ /**
24
+ * Parse a flattened key such as `a.b[0].c` into path segments
25
+ * `["a", "b", 0, "c"]`. Returns `undefined` for a plain key with no `.`/`[`, or
26
+ * for a malformed bracket expression (left untouched by the caller).
27
+ */
28
+ export declare function parseFlattenedKeyPath(key: string): Array<string | number> | undefined;
29
+ /**
30
+ * Reconstruct (unflatten) flattened keys into nested arrays/objects — for
31
+ * example `"items[0]"` -> `{ items: [...] }` and `"parent.child"` ->
32
+ * `{ parent: { child: ... } }`. `shouldSplit` decides, per key, whether it is a
33
+ * flattened path (true) or an opaque literal key to be preserved (false);
34
+ * callers apply their own gating/schema logic there.
35
+ *
36
+ * Prototype-pollution safe: a key whose parsed path contains `__proto__`,
37
+ * `constructor`, or `prototype` (at any position) is dropped, as is a literal
38
+ * plain key equal to one of those names.
39
+ */
40
+ export declare function reconstructFlattenedKeys(args: Record<string, unknown>, shouldSplit: (key: string) => boolean): Record<string, unknown>;
41
+ //# sourceMappingURL=flattened-tool-arguments.d.ts.map