@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
@@ -1 +1 @@
1
- {"version":3,"file":"context-window.d.ts","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,K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AS,MAAM,EAAE,GACrC,KAAK,CAAC,IAAI,CAAC,CAMb;AAuBD,wBAAgB,mBAAmB,CAAC,IAAI,SAAS,GAAG,EACnD,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,6BAAkC,GACzC,sBAAsB,CAAC,IAAI,CAAC,GAAG,2BAA2B,CAwB5D","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
1
+ {"version":3,"file":"context-window.d.ts","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAExD,OAAO,QAAQ,uBAAuB,CAAC,CAAC;IACvC,UAAU,KAAK,CAAC,IAAI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AAE,KAAK,CAAC,IAAI,CAAC,EAClB,oBAAoB,EAAE,SAAS,MAAM,EAAE,GACrC,KAAK,CAAC,IAAI,CAAC,CAMb;AAuBD,wBAAgB,mBAAmB,CAAC,IAAI,SAAS,GAAG,EACnD,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAClB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,6BAAkC,GACzC,sBAAsB,CAAC,IAAI,CAAC,GAAG,2BAA2B,CAwB5D","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t\t/**\n\t\t * Hard prompt/input cap for providers (e.g. GitHub Copilot `max_prompt_tokens`) that enforce an\n\t\t * input budget below the displayed context window. When set and below `contextWindow`, it is the\n\t\t * effective input budget for compaction thresholds and overflow recovery; `contextWindow` remains\n\t\t * the displayed/branded window.\n\t\t */\n\t\tmaxInputTokens?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\n/**\n * Effective input-token budget for compaction/overflow decisions. Equals the displayed\n * `contextWindow` unless a smaller hard input cap (`maxInputTokens`, e.g. GitHub Copilot's\n * `max_prompt_tokens`) is advertised, in which case the lower of the two is used. This lets a model\n * display its full/branded window while compaction and overflow recovery respect the real,\n * server-enforced input limit.\n */\nexport function getEffectiveInputBudget(model: Model<Api>): number {\n\tconst cap = model.maxInputTokens;\n\treturn isPositiveInteger(cap ?? 0) ? Math.min(model.contextWindow, cap as number) : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
@@ -52,6 +52,17 @@ export function normalizeContextWindowOptions(values) {
52
52
  export function getModelDefaultContextWindow(model) {
53
53
  return isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow : model.contextWindow;
54
54
  }
55
+ /**
56
+ * Effective input-token budget for compaction/overflow decisions. Equals the displayed
57
+ * `contextWindow` unless a smaller hard input cap (`maxInputTokens`, e.g. GitHub Copilot's
58
+ * `max_prompt_tokens`) is advertised, in which case the lower of the two is used. This lets a model
59
+ * display its full/branded window while compaction and overflow recovery respect the real,
60
+ * server-enforced input limit.
61
+ */
62
+ export function getEffectiveInputBudget(model) {
63
+ const cap = model.maxInputTokens;
64
+ return isPositiveInteger(cap ?? 0) ? Math.min(model.contextWindow, cap) : model.contextWindow;
65
+ }
55
66
  export function getSupportedContextWindows(model) {
56
67
  return normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);
57
68
  }
@@ -1 +1 @@
1
- {"version":3,"file":"context-window.js","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AAoCA,MAAM,oBAAoB,GAA2B;IACpD,CAAC,EAAE,KAAK;IACR,CAAC,EAAE,SAAS;CACZ,CAAC;AAEF,SAAS,iBAAiB,CAAC,KAAa;IACvC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAA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IACrC,MAAM,SAAS,GAAG,0BAA0B,CAAC,QAAQ,CAAC,CAAC;IACvD,MAAM,qBAAqB,GAAG,8BAA8B,CAAC,QAAQ,EAAE,aAAa,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;IAC1G,IAAI,qBAAqB,KAAK,SAAS,EAAE,CAAC;QACzC,OAAO;YACN,KAAK,EAAE,kBAAkB,mBAAmB,CAAC,aAAa,CAAC,wBAAwB,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,EAAE,uBAAuB,SAAS,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG;SACpL,CAAC;IACH,CAAC;IAED,OAAO;QACN,KAAK,EAAE;YACN,GAAG,KAAK;YACR,oBAAoB,EAAE,4BAA4B,CAAC,QAAQ,CAAC;YAC5D,aAAa,EAAE,qBAAqB;YACpC,oBAAoB,EAAE,SAAS;SAC/B;QACD,aAAa,EAAE,qBAAqB;KACpC,CAAC;AACH,CAAC","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
1
+ {"version":3,"file":"context-window.js","sourceRoot":"","sources":["../../src/core/context-window.ts"],"names":[],"mappings":"AA2CA,MAAM,oBAAoB,GAA2B;IACpD,CAAC,EAAE,KAAK;IACR,CAAC,EAAE,SAAS;CACZ,CAAC;AAEF,SAAS,iBAAiB,CAAC,KAAa;IACvC,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,KAAa;IACvD,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,uDAAuD,CAAC;AACvG,CAAC;AAED,MAAM,UAAU,uBAAuB,CAAC,KAAa;IACpD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,CAAC,OAAO,EAAE,CAAC;QACd,OAAO,EAAE,KAAK,EAAE,iCAAiC,EAAE,CAAC;IACrD,CAAC;IAED,MAAM,KAAK,GAAG,4BAA4B,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACzD,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,OAAO,EAAE,KAAK,EAAE,2BAA2B,KAAK,+DAA+D,EAAE,CAAC;IACnH,CAAC;IAED,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC;IACrC,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC,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C,QAAQ,CAAC;YAC5D,aAAa,EAAE,qBAAqB;YACpC,oBAAoB,EAAE,SAAS;SAC/B;QACD,aAAa,EAAE,qBAAqB;KACpC,CAAC;AACH,CAAC","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\ndeclare module \"@earendil-works/pi-ai\" {\n\tinterface Model<TApi extends Api> {\n\t\t/** Selectable context-window sizes for this model. The scalar contextWindow remains the default/effective value. */\n\t\tcontextWindowOptions?: readonly number[];\n\t\t/** Original/default scalar context window, preserved when contextWindow is overridden for a session. */\n\t\tdefaultContextWindow?: number;\n\t\t/**\n\t\t * Hard prompt/input cap for providers (e.g. GitHub Copilot `max_prompt_tokens`) that enforce an\n\t\t * input budget below the displayed context window. When set and below `contextWindow`, it is the\n\t\t * effective input budget for compaction thresholds and overflow recovery; `contextWindow` remains\n\t\t * the displayed/branded window.\n\t\t */\n\t\tmaxInputTokens?: number;\n\t}\n}\n\nexport interface ContextWindowParseResult {\n\tvalue?: number;\n\terror?: string;\n}\n\nexport interface ContextWindowSelection<TApi extends Api = Api> {\n\tmodel: Model<TApi>;\n\tcontextWindow: number;\n}\n\nexport interface ContextWindowSelectionError {\n\terror: string;\n}\n\nexport interface ContextWindowSelectionOptions {\n\t/**\n\t * GitHub Copilot advertises some long-context tiers below their branded 1M size\n\t * (for example 936k or 922k input tokens). When enabled, a request above an\n\t * advertised long tier selects the largest supported Copilot window not\n\t * exceeding the request, but never silently falls back to the model's base\n\t * window.\n\t */\n\tallowCopilotLongContextFallback?: boolean;\n}\n\nconst CONTEXT_WINDOW_UNITS: Record<string, number> = {\n\tk: 1_000,\n\tm: 1_000_000,\n};\n\nfunction isPositiveInteger(value: number): boolean {\n\treturn Number.isFinite(value) && Number.isInteger(value) && value > 0;\n}\n\nexport function validateContextWindowValue(value: number): string | undefined {\n\treturn isPositiveInteger(value) ? undefined : \"Context window must be a positive integer token count\";\n}\n\nexport function parseContextWindowValue(input: string): ContextWindowParseResult {\n\tconst trimmed = input.trim();\n\tif (!trimmed) {\n\t\treturn { error: \"Context window requires a value\" };\n\t}\n\n\tconst match = /^(\\d+(?:\\.\\d+)?)([kKmM])?$/.exec(trimmed);\n\tif (!match) {\n\t\treturn { error: `Invalid context window \"${input}\". Use a positive number, or a compact value like 400k or 1m.` };\n\t}\n\n\tconst numericValue = Number(match[1]);\n\tconst unit = match[2]?.toLowerCase();\n\tconst multiplier = unit ? CONTEXT_WINDOW_UNITS[unit] : 1;\n\tconst tokens = numericValue * multiplier;\n\tconst validationError = validateContextWindowValue(tokens);\n\tif (validationError) {\n\t\treturn { error: `Invalid context window \"${input}\". ${validationError}.` };\n\t}\n\n\treturn { value: tokens };\n}\n\nexport function formatContextWindow(value: number): string {\n\tif (value >= 1_000_000) {\n\t\tconst millions = value / 1_000_000;\n\t\treturn millions % 1 === 0 ? `${millions}m` : `${millions.toFixed(1)}m`;\n\t}\n\tif (value >= 1_000) {\n\t\tconst thousands = value / 1_000;\n\t\treturn thousands % 1 === 0 ? `${thousands}k` : `${thousands.toFixed(1)}k`;\n\t}\n\treturn String(value);\n}\n\nexport function normalizeContextWindowOptions(values: readonly number[] | undefined): number[] {\n\tconst seen = new Set<number>();\n\tconst normalized: number[] = [];\n\tfor (const value of values ?? []) {\n\t\tif (!isPositiveInteger(value) || seen.has(value)) continue;\n\t\tseen.add(value);\n\t\tnormalized.push(value);\n\t}\n\treturn normalized.sort((a, b) => a - b);\n}\n\nexport function getModelDefaultContextWindow(model: Model<Api>): number {\n\treturn isPositiveInteger(model.defaultContextWindow ?? 0) ? model.defaultContextWindow! : model.contextWindow;\n}\n\n/**\n * Effective input-token budget for compaction/overflow decisions. Equals the displayed\n * `contextWindow` unless a smaller hard input cap (`maxInputTokens`, e.g. GitHub Copilot's\n * `max_prompt_tokens`) is advertised, in which case the lower of the two is used. This lets a model\n * display its full/branded window while compaction and overflow recovery respect the real,\n * server-enforced input limit.\n */\nexport function getEffectiveInputBudget(model: Model<Api>): number {\n\tconst cap = model.maxInputTokens;\n\treturn isPositiveInteger(cap ?? 0) ? Math.min(model.contextWindow, cap as number) : model.contextWindow;\n}\n\nexport function getSupportedContextWindows(model: Model<Api>): number[] {\n\treturn normalizeContextWindowOptions([getModelDefaultContextWindow(model), ...(model.contextWindowOptions ?? [])]);\n}\n\nexport function withContextWindowOptions<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindowOptions: readonly number[],\n): Model<TApi> {\n\treturn {\n\t\t...model,\n\t\tdefaultContextWindow: getModelDefaultContextWindow(model as Model<Api>),\n\t\tcontextWindowOptions: normalizeContextWindowOptions(contextWindowOptions),\n\t};\n}\n\nfunction resolveSelectableContextWindow(\n\tmodel: Model<Api>,\n\trequestedContextWindow: number,\n\tsupported: readonly number[],\n\toptions: ContextWindowSelectionOptions,\n): number | undefined {\n\tif (supported.includes(requestedContextWindow)) {\n\t\treturn requestedContextWindow;\n\t}\n\n\tif (options.allowCopilotLongContextFallback !== true || model.provider !== \"github-copilot\") {\n\t\treturn undefined;\n\t}\n\n\tconst defaultContextWindow = getModelDefaultContextWindow(model);\n\tconst candidates = supported.filter(\n\t\t(contextWindow) => contextWindow <= requestedContextWindow && contextWindow > defaultContextWindow,\n\t);\n\treturn candidates.length > 0 ? Math.max(...candidates) : undefined;\n}\n\nexport function selectContextWindow<TApi extends Api>(\n\tmodel: Model<TApi>,\n\tcontextWindow: number,\n\toptions: ContextWindowSelectionOptions = {},\n): ContextWindowSelection<TApi> | ContextWindowSelectionError {\n\tconst validationError = validateContextWindowValue(contextWindow);\n\tif (validationError) {\n\t\treturn { error: validationError };\n\t}\n\n\tconst apiModel = model as Model<Api>;\n\tconst supported = getSupportedContextWindows(apiModel);\n\tconst selectedContextWindow = resolveSelectableContextWindow(apiModel, contextWindow, supported, options);\n\tif (selectedContextWindow === undefined) {\n\t\treturn {\n\t\t\terror: `Context window ${formatContextWindow(contextWindow)} is not supported by ${model.provider}/${model.id}. Supported values: ${supported.map(formatContextWindow).join(\", \")}.`,\n\t\t};\n\t}\n\n\treturn {\n\t\tmodel: {\n\t\t\t...model,\n\t\t\tdefaultContextWindow: getModelDefaultContextWindow(apiModel),\n\t\t\tcontextWindow: selectedContextWindow,\n\t\t\tcontextWindowOptions: supported,\n\t\t},\n\t\tcontextWindow: selectedContextWindow,\n\t};\n}\n"]}
@@ -0,0 +1,72 @@
1
+ import type { Api, Model } from "@earendil-works/pi-ai";
2
+ /**
3
+ * Sanitizes outbound OpenAI-compatible request payloads for GitHub Copilot
4
+ * Gemini models so their tool/function JSON Schemas survive translation to
5
+ * Google's GenAI `FunctionDeclaration` schema.
6
+ *
7
+ * Why this exists
8
+ * ---------------
9
+ * `github-copilot` Gemini models (e.g. `gemini-3.1-pro-preview`) are served
10
+ * through Copilot's CAPI gateway at `api.*.githubcopilot.com` using the
11
+ * `openai-completions` API. CAPI receives the OpenAI chat-completions request
12
+ * and translates it into a Google GenAI `GenerateContent` request. During that
13
+ * translation CAPI forwards JSON Schema `anyOf`/`oneOf` verbatim into the Gemini
14
+ * `FunctionDeclaration` schema. Gemini's function-declaration schema rejects an
15
+ * `anyOf`/`oneOf` whose branch is a complex *object* schema, so Google returns
16
+ * HTTP 400 and CAPI relabels it `{"error":{"code":"invalid_request_body"}}`.
17
+ *
18
+ * Atomic's bundled tools (notably the `workflow` tool) use the TypeBox
19
+ * `Type.Union([Type.Object(...), Type.String()])` pattern for fields like
20
+ * `task`, `chain`, and `parallel`, which emit exactly that construct. Because
21
+ * those tools are present in normal chat turns, every Gemini request fails with
22
+ * `400 invalid request body` until the schema is sanitized.
23
+ *
24
+ * What it does
25
+ * ------------
26
+ * Recursively rewrites each tool's `parameters` JSON Schema into the reduced
27
+ * subset that CAPI/Gemini actually honors (`type`, `description`, `enum`,
28
+ * `properties`, `required`, `items`, `nullable`, and scalar-only `anyOf`):
29
+ * - Resolves `anyOf`/`oneOf` that contain an object/array branch to the most
30
+ * expressive object/array branch (the core fix), preserving `description`
31
+ * and collapsing a `"null"` branch into `nullable: true`.
32
+ * - Converts `const` (and `anyOf` of `const`/`enum` scalars) into `enum`,
33
+ * which Gemini supports, so `Type.Literal(...)` unions keep their constraint.
34
+ * - Drops JSON Schema keywords CAPI strips or Gemini rejects (`$schema`,
35
+ * `$ref`, `$defs`, `patternProperties`, `additionalProperties`, `allOf`,
36
+ * `not`, `format`, `pattern`, numeric/length bounds, `default`, etc.).
37
+ * - Filters `required` down to keys still present under `properties`.
38
+ *
39
+ * The transform is gated to GitHub Copilot Gemini models only, so it never
40
+ * changes payloads for any currently-working provider/model.
41
+ */
42
+ type JsonObject = {
43
+ [key: string]: JsonValue;
44
+ };
45
+ type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
46
+ /**
47
+ * Keywords that are dropped because CAPI strips them before Google and/or
48
+ * Gemini's function schema rejects them. Listing them explicitly keeps the
49
+ * intent auditable; any keyword not in {@link KEPT_SCHEMA_KEYWORDS} and not a
50
+ * union keyword is dropped regardless. Exported so the documented drop-list has
51
+ * a real consumer (and stays linted) without a per-iteration `void` reference.
52
+ */
53
+ export declare const DROPPED_SCHEMA_KEYWORDS: Set<string>;
54
+ /**
55
+ * Whether this model is a GitHub Copilot Gemini model routed through the
56
+ * OpenAI-completions CAPI path that needs schema sanitization.
57
+ */
58
+ export declare function isCopilotGeminiModel(model: Pick<Model<Api>, "provider" | "api" | "id">): boolean;
59
+ /**
60
+ * Recursively rewrite a JSON Schema node into the Gemini-compatible subset.
61
+ */
62
+ export declare function sanitizeGeminiSchema(schema: JsonValue): JsonValue;
63
+ /**
64
+ * Sanitize an outbound provider payload for GitHub Copilot Gemini models.
65
+ *
66
+ * Returns the payload unchanged for any other provider/model, or when the
67
+ * payload has no sanitizable `tools`. Mirrors the `onPayload`
68
+ * `(payload: unknown) => unknown` contract used elsewhere in the SDK wiring.
69
+ */
70
+ export declare function sanitizeCopilotGeminiPayload(payload: unknown, model: Pick<Model<Api>, "provider" | "api" | "id">): unknown;
71
+ export {};
72
+ //# sourceMappingURL=copilot-gemini-payload-sanitizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copilot-gemini-payload-sanitizer.d.ts","sourceRoot":"","sources":["../../src/core/copilot-gemini-payload-sanitizer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAExD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AAEH,KAAK,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAC/C,KAAK,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,EAAE,GAAG,UAAU,CAAC;AAa7E;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,aAwClC,CAAC;AAMH;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,KAAK,GAAG,IAAI,CAAC,GAAG,OAAO,CAMhG;AAqCD;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,CAgDjE;AA2GD;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,KAAK,GAAG,IAAI,CAAC,GACjD,OAAO,CA+BT","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\n/**\n * Sanitizes outbound OpenAI-compatible request payloads for GitHub Copilot\n * Gemini models so their tool/function JSON Schemas survive translation to\n * Google's GenAI `FunctionDeclaration` schema.\n *\n * Why this exists\n * ---------------\n * `github-copilot` Gemini models (e.g. `gemini-3.1-pro-preview`) are served\n * through Copilot's CAPI gateway at `api.*.githubcopilot.com` using the\n * `openai-completions` API. CAPI receives the OpenAI chat-completions request\n * and translates it into a Google GenAI `GenerateContent` request. During that\n * translation CAPI forwards JSON Schema `anyOf`/`oneOf` verbatim into the Gemini\n * `FunctionDeclaration` schema. Gemini's function-declaration schema rejects an\n * `anyOf`/`oneOf` whose branch is a complex *object* schema, so Google returns\n * HTTP 400 and CAPI relabels it `{\"error\":{\"code\":\"invalid_request_body\"}}`.\n *\n * Atomic's bundled tools (notably the `workflow` tool) use the TypeBox\n * `Type.Union([Type.Object(...), Type.String()])` pattern for fields like\n * `task`, `chain`, and `parallel`, which emit exactly that construct. Because\n * those tools are present in normal chat turns, every Gemini request fails with\n * `400 invalid request body` until the schema is sanitized.\n *\n * What it does\n * ------------\n * Recursively rewrites each tool's `parameters` JSON Schema into the reduced\n * subset that CAPI/Gemini actually honors (`type`, `description`, `enum`,\n * `properties`, `required`, `items`, `nullable`, and scalar-only `anyOf`):\n * - Resolves `anyOf`/`oneOf` that contain an object/array branch to the most\n * expressive object/array branch (the core fix), preserving `description`\n * and collapsing a `\"null\"` branch into `nullable: true`.\n * - Converts `const` (and `anyOf` of `const`/`enum` scalars) into `enum`,\n * which Gemini supports, so `Type.Literal(...)` unions keep their constraint.\n * - Drops JSON Schema keywords CAPI strips or Gemini rejects (`$schema`,\n * `$ref`, `$defs`, `patternProperties`, `additionalProperties`, `allOf`,\n * `not`, `format`, `pattern`, numeric/length bounds, `default`, etc.).\n * - Filters `required` down to keys still present under `properties`.\n *\n * The transform is gated to GitHub Copilot Gemini models only, so it never\n * changes payloads for any currently-working provider/model.\n */\n\ntype JsonObject = { [key: string]: JsonValue };\ntype JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;\n\n/** JSON Schema keywords Gemini's function-declaration schema honors. */\nconst KEPT_SCHEMA_KEYWORDS = new Set<string>([\n \"type\",\n \"description\",\n \"enum\",\n \"properties\",\n \"required\",\n \"items\",\n \"nullable\",\n]);\n\n/**\n * Keywords that are dropped because CAPI strips them before Google and/or\n * Gemini's function schema rejects them. Listing them explicitly keeps the\n * intent auditable; any keyword not in {@link KEPT_SCHEMA_KEYWORDS} and not a\n * union keyword is dropped regardless. Exported so the documented drop-list has\n * a real consumer (and stays linted) without a per-iteration `void` reference.\n */\nexport const DROPPED_SCHEMA_KEYWORDS = new Set<string>([\n \"$schema\",\n \"$id\",\n \"$ref\",\n \"$defs\",\n \"$comment\",\n \"definitions\",\n \"patternProperties\",\n \"propertyNames\",\n \"unevaluatedProperties\",\n \"additionalProperties\",\n \"additionalItems\",\n \"unevaluatedItems\",\n \"allOf\",\n \"not\",\n \"if\",\n \"then\",\n \"else\",\n \"format\",\n \"pattern\",\n \"minLength\",\n \"maxLength\",\n \"minItems\",\n \"maxItems\",\n \"uniqueItems\",\n \"minimum\",\n \"maximum\",\n \"exclusiveMinimum\",\n \"exclusiveMaximum\",\n \"multipleOf\",\n \"minProperties\",\n \"maxProperties\",\n \"default\",\n \"examples\",\n \"title\",\n \"readOnly\",\n \"writeOnly\",\n \"deprecated\",\n \"contentEncoding\",\n \"contentMediaType\",\n]);\n\nfunction isPlainObject(value: JsonValue | undefined): value is JsonObject {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * Whether this model is a GitHub Copilot Gemini model routed through the\n * OpenAI-completions CAPI path that needs schema sanitization.\n */\nexport function isCopilotGeminiModel(model: Pick<Model<Api>, \"provider\" | \"api\" | \"id\">): boolean {\n return (\n model.provider === \"github-copilot\" &&\n model.api === \"openai-completions\" &&\n /(^|[/-])gemini/i.test(model.id)\n );\n}\n\nfunction jsonScalarType(value: JsonValue): string | undefined {\n switch (typeof value) {\n case \"string\":\n return \"string\";\n case \"boolean\":\n return \"boolean\";\n case \"number\":\n return Number.isInteger(value) ? \"integer\" : \"number\";\n default:\n return value === null ? \"null\" : undefined;\n }\n}\n\nfunction isObjectOrArraySchema(schema: JsonValue): boolean {\n if (!isPlainObject(schema)) return false;\n return (\n schema.type === \"object\" ||\n schema.type === \"array\" ||\n \"properties\" in schema ||\n \"items\" in schema\n );\n}\n\nfunction isNullSchema(schema: JsonValue): boolean {\n return isPlainObject(schema) && schema.type === \"null\";\n}\n\n/** Collect literal values from a scalar branch expressed as `const` or `enum`. */\nfunction literalValues(schema: JsonValue): JsonValue[] | undefined {\n if (!isPlainObject(schema)) return undefined;\n if (\"const\" in schema) return [schema.const as JsonValue];\n if (Array.isArray(schema.enum)) return schema.enum as JsonValue[];\n return undefined;\n}\n\n/**\n * Recursively rewrite a JSON Schema node into the Gemini-compatible subset.\n */\nexport function sanitizeGeminiSchema(schema: JsonValue): JsonValue {\n if (Array.isArray(schema)) {\n return schema.map((entry) => sanitizeGeminiSchema(entry));\n }\n if (!isPlainObject(schema)) {\n return schema;\n }\n\n const union = (schema.anyOf ?? schema.oneOf) as JsonValue | undefined;\n if (Array.isArray(union)) {\n return sanitizeUnion(schema, union);\n }\n\n const result: JsonObject = {};\n\n // const -> enum (Gemini supports enum, not const).\n if (\"const\" in schema && !(\"enum\" in schema)) {\n const constValue = schema.const as JsonValue;\n const inferred = jsonScalarType(constValue);\n if (inferred && inferred !== \"null\") result.type = inferred;\n result.enum = [constValue];\n }\n\n for (const [key, value] of Object.entries(schema)) {\n if (key === \"const\") continue;\n if (key === \"properties\" && isPlainObject(value)) {\n const props: JsonObject = {};\n for (const [propName, propSchema] of Object.entries(value)) {\n props[propName] = sanitizeGeminiSchema(propSchema);\n }\n result.properties = props;\n continue;\n }\n if (key === \"items\") {\n result.items = sanitizeItems(value);\n continue;\n }\n if (KEPT_SCHEMA_KEYWORDS.has(key)) {\n result[key] = value;\n continue;\n }\n // Everything else (the documented DROPPED_SCHEMA_KEYWORDS plus any unknown\n // keyword) is omitted: the rule is simply \"keep only KEPT_SCHEMA_KEYWORDS\".\n }\n\n inferContainerType(result);\n pruneRequired(result);\n return result;\n}\n\n/**\n * Resolve an `items` schema. Gemini's function-declaration schema expects a\n * single `items` schema, so a tuple-form `items` (array of schemas) is collapsed\n * to its most expressive (object/array) entry, falling back to the first entry.\n */\nfunction sanitizeItems(items: JsonValue): JsonValue {\n if (!Array.isArray(items)) return sanitizeGeminiSchema(items);\n const sanitized = items.map((entry) => sanitizeGeminiSchema(entry));\n const objectOrArray = sanitized.find((entry) => isObjectOrArraySchema(entry));\n return objectOrArray ?? sanitized[0] ?? { type: \"string\" };\n}\n\n/**\n * Gemini resolves function arguments more reliably when container nodes carry an\n * explicit `type`. Infer it when omitted: a node with `properties`/`required` is\n * an object, and a node with `items` is an array.\n */\nfunction inferContainerType(schema: JsonObject): void {\n if (schema.type !== undefined) return;\n if (isPlainObject(schema.properties) || Array.isArray(schema.required)) {\n schema.type = \"object\";\n } else if (schema.items !== undefined) {\n schema.type = \"array\";\n }\n}\n\n/** Resolve an `anyOf`/`oneOf` union node into the Gemini-compatible subset. */\nfunction sanitizeUnion(parent: JsonObject, branches: JsonValue[]): JsonValue {\n const sanitizedBranches = branches.map((branch) => sanitizeGeminiSchema(branch));\n const nullable =\n branches.some((branch) => isNullSchema(branch)) || parent.nullable === true;\n const nonNull = sanitizedBranches.filter((branch) => !isNullSchema(branch));\n\n const carryDescription = (target: JsonValue): JsonValue => {\n if (isPlainObject(target) && typeof parent.description === \"string\" && !(\"description\" in target)) {\n target.description = parent.description;\n }\n if (nullable && isPlainObject(target)) target.nullable = true;\n return target;\n };\n\n // Core fix: if any branch is an object/array schema, collapse to the first\n // such branch (Gemini rejects unions whose branch is a complex object). For\n // the TypeBox `Type.Union([Type.Object(...), Type.String()])` pattern this is\n // the object branch; a union of two distinct object shapes keeps only the\n // first, so the others' properties are intentionally dropped.\n const objectOrArray = nonNull.find((branch) => isObjectOrArraySchema(branch));\n if (objectOrArray) {\n return carryDescription(objectOrArray);\n }\n\n if (nonNull.length === 0) {\n return carryDescription({ type: \"string\" });\n }\n if (nonNull.length === 1) {\n return carryDescription(nonNull[0]);\n }\n\n // All-scalar union: prefer an `enum` when every branch is a literal/enum of\n // one underlying type; otherwise keep a scalar `anyOf` (Gemini accepts it).\n const literals: JsonValue[] = [];\n const scalarTypes = new Set<string>();\n let allLiteral = true;\n for (const branch of nonNull) {\n const values = literalValues(branch);\n if (!values) {\n allLiteral = false;\n break;\n }\n for (const value of values) {\n literals.push(value);\n const inferred = jsonScalarType(value);\n if (inferred) scalarTypes.add(inferred);\n }\n }\n if (allLiteral && literals.length > 0) {\n const collapsed: JsonObject = { enum: literals };\n if (scalarTypes.size === 1) collapsed.type = [...scalarTypes][0];\n return carryDescription(collapsed);\n }\n\n const node: JsonObject = { anyOf: nonNull };\n return carryDescription(node);\n}\n\n/** Drop `required` entries that are not present under `properties`. */\nfunction pruneRequired(schema: JsonObject): void {\n if (!Array.isArray(schema.required)) return;\n const properties = isPlainObject(schema.properties) ? schema.properties : undefined;\n if (!properties) {\n delete schema.required;\n return;\n }\n const filtered = schema.required.filter(\n (key): key is string => typeof key === \"string\" && key in properties,\n );\n if (filtered.length > 0) schema.required = filtered;\n else delete schema.required;\n}\n\n/** Sanitize a single tool's `parameters` schema in place-safe (returns new value). */\nfunction sanitizeToolParameters(parameters: JsonValue): JsonValue {\n return sanitizeGeminiSchema(parameters);\n}\n\n/**\n * Sanitize an outbound provider payload for GitHub Copilot Gemini models.\n *\n * Returns the payload unchanged for any other provider/model, or when the\n * payload has no sanitizable `tools`. Mirrors the `onPayload`\n * `(payload: unknown) => unknown` contract used elsewhere in the SDK wiring.\n */\nexport function sanitizeCopilotGeminiPayload(\n payload: unknown,\n model: Pick<Model<Api>, \"provider\" | \"api\" | \"id\">,\n): unknown {\n if (!isCopilotGeminiModel(model)) return payload;\n if (!isPlainObject(payload as JsonValue)) return payload;\n const payloadObject = payload as JsonObject;\n const tools = payloadObject.tools;\n if (!Array.isArray(tools) || tools.length === 0) return payload;\n\n let mutated = false;\n const sanitizedTools = tools.map((tool) => {\n if (!isPlainObject(tool)) return tool;\n // OpenAI chat-completions tool shape: { type: \"function\", function: { parameters } }\n if (isPlainObject(tool.function) && tool.function.parameters !== undefined) {\n mutated = true;\n return {\n ...tool,\n function: {\n ...tool.function,\n parameters: sanitizeToolParameters(tool.function.parameters),\n },\n };\n }\n // Defensive: flat tool shape { name, parameters }.\n if (tool.parameters !== undefined) {\n mutated = true;\n return { ...tool, parameters: sanitizeToolParameters(tool.parameters) };\n }\n return tool;\n });\n\n if (!mutated) return payload;\n return { ...payloadObject, tools: sanitizedTools };\n}\n"]}
@@ -0,0 +1,296 @@
1
+ /** JSON Schema keywords Gemini's function-declaration schema honors. */
2
+ const KEPT_SCHEMA_KEYWORDS = new Set([
3
+ "type",
4
+ "description",
5
+ "enum",
6
+ "properties",
7
+ "required",
8
+ "items",
9
+ "nullable",
10
+ ]);
11
+ /**
12
+ * Keywords that are dropped because CAPI strips them before Google and/or
13
+ * Gemini's function schema rejects them. Listing them explicitly keeps the
14
+ * intent auditable; any keyword not in {@link KEPT_SCHEMA_KEYWORDS} and not a
15
+ * union keyword is dropped regardless. Exported so the documented drop-list has
16
+ * a real consumer (and stays linted) without a per-iteration `void` reference.
17
+ */
18
+ export const DROPPED_SCHEMA_KEYWORDS = new Set([
19
+ "$schema",
20
+ "$id",
21
+ "$ref",
22
+ "$defs",
23
+ "$comment",
24
+ "definitions",
25
+ "patternProperties",
26
+ "propertyNames",
27
+ "unevaluatedProperties",
28
+ "additionalProperties",
29
+ "additionalItems",
30
+ "unevaluatedItems",
31
+ "allOf",
32
+ "not",
33
+ "if",
34
+ "then",
35
+ "else",
36
+ "format",
37
+ "pattern",
38
+ "minLength",
39
+ "maxLength",
40
+ "minItems",
41
+ "maxItems",
42
+ "uniqueItems",
43
+ "minimum",
44
+ "maximum",
45
+ "exclusiveMinimum",
46
+ "exclusiveMaximum",
47
+ "multipleOf",
48
+ "minProperties",
49
+ "maxProperties",
50
+ "default",
51
+ "examples",
52
+ "title",
53
+ "readOnly",
54
+ "writeOnly",
55
+ "deprecated",
56
+ "contentEncoding",
57
+ "contentMediaType",
58
+ ]);
59
+ function isPlainObject(value) {
60
+ return typeof value === "object" && value !== null && !Array.isArray(value);
61
+ }
62
+ /**
63
+ * Whether this model is a GitHub Copilot Gemini model routed through the
64
+ * OpenAI-completions CAPI path that needs schema sanitization.
65
+ */
66
+ export function isCopilotGeminiModel(model) {
67
+ return (model.provider === "github-copilot" &&
68
+ model.api === "openai-completions" &&
69
+ /(^|[/-])gemini/i.test(model.id));
70
+ }
71
+ function jsonScalarType(value) {
72
+ switch (typeof value) {
73
+ case "string":
74
+ return "string";
75
+ case "boolean":
76
+ return "boolean";
77
+ case "number":
78
+ return Number.isInteger(value) ? "integer" : "number";
79
+ default:
80
+ return value === null ? "null" : undefined;
81
+ }
82
+ }
83
+ function isObjectOrArraySchema(schema) {
84
+ if (!isPlainObject(schema))
85
+ return false;
86
+ return (schema.type === "object" ||
87
+ schema.type === "array" ||
88
+ "properties" in schema ||
89
+ "items" in schema);
90
+ }
91
+ function isNullSchema(schema) {
92
+ return isPlainObject(schema) && schema.type === "null";
93
+ }
94
+ /** Collect literal values from a scalar branch expressed as `const` or `enum`. */
95
+ function literalValues(schema) {
96
+ if (!isPlainObject(schema))
97
+ return undefined;
98
+ if ("const" in schema)
99
+ return [schema.const];
100
+ if (Array.isArray(schema.enum))
101
+ return schema.enum;
102
+ return undefined;
103
+ }
104
+ /**
105
+ * Recursively rewrite a JSON Schema node into the Gemini-compatible subset.
106
+ */
107
+ export function sanitizeGeminiSchema(schema) {
108
+ if (Array.isArray(schema)) {
109
+ return schema.map((entry) => sanitizeGeminiSchema(entry));
110
+ }
111
+ if (!isPlainObject(schema)) {
112
+ return schema;
113
+ }
114
+ const union = (schema.anyOf ?? schema.oneOf);
115
+ if (Array.isArray(union)) {
116
+ return sanitizeUnion(schema, union);
117
+ }
118
+ const result = {};
119
+ // const -> enum (Gemini supports enum, not const).
120
+ if ("const" in schema && !("enum" in schema)) {
121
+ const constValue = schema.const;
122
+ const inferred = jsonScalarType(constValue);
123
+ if (inferred && inferred !== "null")
124
+ result.type = inferred;
125
+ result.enum = [constValue];
126
+ }
127
+ for (const [key, value] of Object.entries(schema)) {
128
+ if (key === "const")
129
+ continue;
130
+ if (key === "properties" && isPlainObject(value)) {
131
+ const props = {};
132
+ for (const [propName, propSchema] of Object.entries(value)) {
133
+ props[propName] = sanitizeGeminiSchema(propSchema);
134
+ }
135
+ result.properties = props;
136
+ continue;
137
+ }
138
+ if (key === "items") {
139
+ result.items = sanitizeItems(value);
140
+ continue;
141
+ }
142
+ if (KEPT_SCHEMA_KEYWORDS.has(key)) {
143
+ result[key] = value;
144
+ continue;
145
+ }
146
+ // Everything else (the documented DROPPED_SCHEMA_KEYWORDS plus any unknown
147
+ // keyword) is omitted: the rule is simply "keep only KEPT_SCHEMA_KEYWORDS".
148
+ }
149
+ inferContainerType(result);
150
+ pruneRequired(result);
151
+ return result;
152
+ }
153
+ /**
154
+ * Resolve an `items` schema. Gemini's function-declaration schema expects a
155
+ * single `items` schema, so a tuple-form `items` (array of schemas) is collapsed
156
+ * to its most expressive (object/array) entry, falling back to the first entry.
157
+ */
158
+ function sanitizeItems(items) {
159
+ if (!Array.isArray(items))
160
+ return sanitizeGeminiSchema(items);
161
+ const sanitized = items.map((entry) => sanitizeGeminiSchema(entry));
162
+ const objectOrArray = sanitized.find((entry) => isObjectOrArraySchema(entry));
163
+ return objectOrArray ?? sanitized[0] ?? { type: "string" };
164
+ }
165
+ /**
166
+ * Gemini resolves function arguments more reliably when container nodes carry an
167
+ * explicit `type`. Infer it when omitted: a node with `properties`/`required` is
168
+ * an object, and a node with `items` is an array.
169
+ */
170
+ function inferContainerType(schema) {
171
+ if (schema.type !== undefined)
172
+ return;
173
+ if (isPlainObject(schema.properties) || Array.isArray(schema.required)) {
174
+ schema.type = "object";
175
+ }
176
+ else if (schema.items !== undefined) {
177
+ schema.type = "array";
178
+ }
179
+ }
180
+ /** Resolve an `anyOf`/`oneOf` union node into the Gemini-compatible subset. */
181
+ function sanitizeUnion(parent, branches) {
182
+ const sanitizedBranches = branches.map((branch) => sanitizeGeminiSchema(branch));
183
+ const nullable = branches.some((branch) => isNullSchema(branch)) || parent.nullable === true;
184
+ const nonNull = sanitizedBranches.filter((branch) => !isNullSchema(branch));
185
+ const carryDescription = (target) => {
186
+ if (isPlainObject(target) && typeof parent.description === "string" && !("description" in target)) {
187
+ target.description = parent.description;
188
+ }
189
+ if (nullable && isPlainObject(target))
190
+ target.nullable = true;
191
+ return target;
192
+ };
193
+ // Core fix: if any branch is an object/array schema, collapse to the first
194
+ // such branch (Gemini rejects unions whose branch is a complex object). For
195
+ // the TypeBox `Type.Union([Type.Object(...), Type.String()])` pattern this is
196
+ // the object branch; a union of two distinct object shapes keeps only the
197
+ // first, so the others' properties are intentionally dropped.
198
+ const objectOrArray = nonNull.find((branch) => isObjectOrArraySchema(branch));
199
+ if (objectOrArray) {
200
+ return carryDescription(objectOrArray);
201
+ }
202
+ if (nonNull.length === 0) {
203
+ return carryDescription({ type: "string" });
204
+ }
205
+ if (nonNull.length === 1) {
206
+ return carryDescription(nonNull[0]);
207
+ }
208
+ // All-scalar union: prefer an `enum` when every branch is a literal/enum of
209
+ // one underlying type; otherwise keep a scalar `anyOf` (Gemini accepts it).
210
+ const literals = [];
211
+ const scalarTypes = new Set();
212
+ let allLiteral = true;
213
+ for (const branch of nonNull) {
214
+ const values = literalValues(branch);
215
+ if (!values) {
216
+ allLiteral = false;
217
+ break;
218
+ }
219
+ for (const value of values) {
220
+ literals.push(value);
221
+ const inferred = jsonScalarType(value);
222
+ if (inferred)
223
+ scalarTypes.add(inferred);
224
+ }
225
+ }
226
+ if (allLiteral && literals.length > 0) {
227
+ const collapsed = { enum: literals };
228
+ if (scalarTypes.size === 1)
229
+ collapsed.type = [...scalarTypes][0];
230
+ return carryDescription(collapsed);
231
+ }
232
+ const node = { anyOf: nonNull };
233
+ return carryDescription(node);
234
+ }
235
+ /** Drop `required` entries that are not present under `properties`. */
236
+ function pruneRequired(schema) {
237
+ if (!Array.isArray(schema.required))
238
+ return;
239
+ const properties = isPlainObject(schema.properties) ? schema.properties : undefined;
240
+ if (!properties) {
241
+ delete schema.required;
242
+ return;
243
+ }
244
+ const filtered = schema.required.filter((key) => typeof key === "string" && key in properties);
245
+ if (filtered.length > 0)
246
+ schema.required = filtered;
247
+ else
248
+ delete schema.required;
249
+ }
250
+ /** Sanitize a single tool's `parameters` schema in place-safe (returns new value). */
251
+ function sanitizeToolParameters(parameters) {
252
+ return sanitizeGeminiSchema(parameters);
253
+ }
254
+ /**
255
+ * Sanitize an outbound provider payload for GitHub Copilot Gemini models.
256
+ *
257
+ * Returns the payload unchanged for any other provider/model, or when the
258
+ * payload has no sanitizable `tools`. Mirrors the `onPayload`
259
+ * `(payload: unknown) => unknown` contract used elsewhere in the SDK wiring.
260
+ */
261
+ export function sanitizeCopilotGeminiPayload(payload, model) {
262
+ if (!isCopilotGeminiModel(model))
263
+ return payload;
264
+ if (!isPlainObject(payload))
265
+ return payload;
266
+ const payloadObject = payload;
267
+ const tools = payloadObject.tools;
268
+ if (!Array.isArray(tools) || tools.length === 0)
269
+ return payload;
270
+ let mutated = false;
271
+ const sanitizedTools = tools.map((tool) => {
272
+ if (!isPlainObject(tool))
273
+ return tool;
274
+ // OpenAI chat-completions tool shape: { type: "function", function: { parameters } }
275
+ if (isPlainObject(tool.function) && tool.function.parameters !== undefined) {
276
+ mutated = true;
277
+ return {
278
+ ...tool,
279
+ function: {
280
+ ...tool.function,
281
+ parameters: sanitizeToolParameters(tool.function.parameters),
282
+ },
283
+ };
284
+ }
285
+ // Defensive: flat tool shape { name, parameters }.
286
+ if (tool.parameters !== undefined) {
287
+ mutated = true;
288
+ return { ...tool, parameters: sanitizeToolParameters(tool.parameters) };
289
+ }
290
+ return tool;
291
+ });
292
+ if (!mutated)
293
+ return payload;
294
+ return { ...payloadObject, tools: sanitizedTools };
295
+ }
296
+ //# sourceMappingURL=copilot-gemini-payload-sanitizer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copilot-gemini-payload-sanitizer.js","sourceRoot":"","sources":["../../src/core/copilot-gemini-payload-sanitizer.ts"],"names":[],"mappings":"AA8CA,wEAAwE;AACxE,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAAS;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,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YAC3E,OAAO,GAAG,IAAI,CAAC;YACf,OAAO;gBACL,GAAG,IAAI;gBACP,QAAQ,EAAE;oBACR,GAAG,IAAI,CAAC,QAAQ;oBAChB,UAAU,EAAE,sBAAsB,CAAC,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC;iBAC7D;aACF,CAAC;QACJ,CAAC;QACD,mDAAmD;QACnD,IAAI,IAAI,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YAClC,OAAO,GAAG,IAAI,CAAC;YACf,OAAO,EAAE,GAAG,IAAI,EAAE,UAAU,EAAE,sBAAsB,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAC1E,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,OAAO;QAAE,OAAO,OAAO,CAAC;IAC7B,OAAO,EAAE,GAAG,aAAa,EAAE,KAAK,EAAE,cAAc,EAAE,CAAC;AACrD,CAAC","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\n\n/**\n * Sanitizes outbound OpenAI-compatible request payloads for GitHub Copilot\n * Gemini models so their tool/function JSON Schemas survive translation to\n * Google's GenAI `FunctionDeclaration` schema.\n *\n * Why this exists\n * ---------------\n * `github-copilot` Gemini models (e.g. `gemini-3.1-pro-preview`) are served\n * through Copilot's CAPI gateway at `api.*.githubcopilot.com` using the\n * `openai-completions` API. CAPI receives the OpenAI chat-completions request\n * and translates it into a Google GenAI `GenerateContent` request. During that\n * translation CAPI forwards JSON Schema `anyOf`/`oneOf` verbatim into the Gemini\n * `FunctionDeclaration` schema. Gemini's function-declaration schema rejects an\n * `anyOf`/`oneOf` whose branch is a complex *object* schema, so Google returns\n * HTTP 400 and CAPI relabels it `{\"error\":{\"code\":\"invalid_request_body\"}}`.\n *\n * Atomic's bundled tools (notably the `workflow` tool) use the TypeBox\n * `Type.Union([Type.Object(...), Type.String()])` pattern for fields like\n * `task`, `chain`, and `parallel`, which emit exactly that construct. Because\n * those tools are present in normal chat turns, every Gemini request fails with\n * `400 invalid request body` until the schema is sanitized.\n *\n * What it does\n * ------------\n * Recursively rewrites each tool's `parameters` JSON Schema into the reduced\n * subset that CAPI/Gemini actually honors (`type`, `description`, `enum`,\n * `properties`, `required`, `items`, `nullable`, and scalar-only `anyOf`):\n * - Resolves `anyOf`/`oneOf` that contain an object/array branch to the most\n * expressive object/array branch (the core fix), preserving `description`\n * and collapsing a `\"null\"` branch into `nullable: true`.\n * - Converts `const` (and `anyOf` of `const`/`enum` scalars) into `enum`,\n * which Gemini supports, so `Type.Literal(...)` unions keep their constraint.\n * - Drops JSON Schema keywords CAPI strips or Gemini rejects (`$schema`,\n * `$ref`, `$defs`, `patternProperties`, `additionalProperties`, `allOf`,\n * `not`, `format`, `pattern`, numeric/length bounds, `default`, etc.).\n * - Filters `required` down to keys still present under `properties`.\n *\n * The transform is gated to GitHub Copilot Gemini models only, so it never\n * changes payloads for any currently-working provider/model.\n */\n\ntype JsonObject = { [key: string]: JsonValue };\ntype JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;\n\n/** JSON Schema keywords Gemini's function-declaration schema honors. */\nconst KEPT_SCHEMA_KEYWORDS = new Set<string>([\n \"type\",\n \"description\",\n \"enum\",\n \"properties\",\n \"required\",\n \"items\",\n \"nullable\",\n]);\n\n/**\n * Keywords that are dropped because CAPI strips them before Google and/or\n * Gemini's function schema rejects them. Listing them explicitly keeps the\n * intent auditable; any keyword not in {@link KEPT_SCHEMA_KEYWORDS} and not a\n * union keyword is dropped regardless. Exported so the documented drop-list has\n * a real consumer (and stays linted) without a per-iteration `void` reference.\n */\nexport const DROPPED_SCHEMA_KEYWORDS = new Set<string>([\n \"$schema\",\n \"$id\",\n \"$ref\",\n \"$defs\",\n \"$comment\",\n \"definitions\",\n \"patternProperties\",\n \"propertyNames\",\n \"unevaluatedProperties\",\n \"additionalProperties\",\n \"additionalItems\",\n \"unevaluatedItems\",\n \"allOf\",\n \"not\",\n \"if\",\n \"then\",\n \"else\",\n \"format\",\n \"pattern\",\n \"minLength\",\n \"maxLength\",\n \"minItems\",\n \"maxItems\",\n \"uniqueItems\",\n \"minimum\",\n \"maximum\",\n \"exclusiveMinimum\",\n \"exclusiveMaximum\",\n \"multipleOf\",\n \"minProperties\",\n \"maxProperties\",\n \"default\",\n \"examples\",\n \"title\",\n \"readOnly\",\n \"writeOnly\",\n \"deprecated\",\n \"contentEncoding\",\n \"contentMediaType\",\n]);\n\nfunction isPlainObject(value: JsonValue | undefined): value is JsonObject {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * Whether this model is a GitHub Copilot Gemini model routed through the\n * OpenAI-completions CAPI path that needs schema sanitization.\n */\nexport function isCopilotGeminiModel(model: Pick<Model<Api>, \"provider\" | \"api\" | \"id\">): boolean {\n return (\n model.provider === \"github-copilot\" &&\n model.api === \"openai-completions\" &&\n /(^|[/-])gemini/i.test(model.id)\n );\n}\n\nfunction jsonScalarType(value: JsonValue): string | undefined {\n switch (typeof value) {\n case \"string\":\n return \"string\";\n case \"boolean\":\n return \"boolean\";\n case \"number\":\n return Number.isInteger(value) ? \"integer\" : \"number\";\n default:\n return value === null ? \"null\" : undefined;\n }\n}\n\nfunction isObjectOrArraySchema(schema: JsonValue): boolean {\n if (!isPlainObject(schema)) return false;\n return (\n schema.type === \"object\" ||\n schema.type === \"array\" ||\n \"properties\" in schema ||\n \"items\" in schema\n );\n}\n\nfunction isNullSchema(schema: JsonValue): boolean {\n return isPlainObject(schema) && schema.type === \"null\";\n}\n\n/** Collect literal values from a scalar branch expressed as `const` or `enum`. */\nfunction literalValues(schema: JsonValue): JsonValue[] | undefined {\n if (!isPlainObject(schema)) return undefined;\n if (\"const\" in schema) return [schema.const as JsonValue];\n if (Array.isArray(schema.enum)) return schema.enum as JsonValue[];\n return undefined;\n}\n\n/**\n * Recursively rewrite a JSON Schema node into the Gemini-compatible subset.\n */\nexport function sanitizeGeminiSchema(schema: JsonValue): JsonValue {\n if (Array.isArray(schema)) {\n return schema.map((entry) => sanitizeGeminiSchema(entry));\n }\n if (!isPlainObject(schema)) {\n return schema;\n }\n\n const union = (schema.anyOf ?? schema.oneOf) as JsonValue | undefined;\n if (Array.isArray(union)) {\n return sanitizeUnion(schema, union);\n }\n\n const result: JsonObject = {};\n\n // const -> enum (Gemini supports enum, not const).\n if (\"const\" in schema && !(\"enum\" in schema)) {\n const constValue = schema.const as JsonValue;\n const inferred = jsonScalarType(constValue);\n if (inferred && inferred !== \"null\") result.type = inferred;\n result.enum = [constValue];\n }\n\n for (const [key, value] of Object.entries(schema)) {\n if (key === \"const\") continue;\n if (key === \"properties\" && isPlainObject(value)) {\n const props: JsonObject = {};\n for (const [propName, propSchema] of Object.entries(value)) {\n props[propName] = sanitizeGeminiSchema(propSchema);\n }\n result.properties = props;\n continue;\n }\n if (key === \"items\") {\n result.items = sanitizeItems(value);\n continue;\n }\n if (KEPT_SCHEMA_KEYWORDS.has(key)) {\n result[key] = value;\n continue;\n }\n // Everything else (the documented DROPPED_SCHEMA_KEYWORDS plus any unknown\n // keyword) is omitted: the rule is simply \"keep only KEPT_SCHEMA_KEYWORDS\".\n }\n\n inferContainerType(result);\n pruneRequired(result);\n return result;\n}\n\n/**\n * Resolve an `items` schema. Gemini's function-declaration schema expects a\n * single `items` schema, so a tuple-form `items` (array of schemas) is collapsed\n * to its most expressive (object/array) entry, falling back to the first entry.\n */\nfunction sanitizeItems(items: JsonValue): JsonValue {\n if (!Array.isArray(items)) return sanitizeGeminiSchema(items);\n const sanitized = items.map((entry) => sanitizeGeminiSchema(entry));\n const objectOrArray = sanitized.find((entry) => isObjectOrArraySchema(entry));\n return objectOrArray ?? sanitized[0] ?? { type: \"string\" };\n}\n\n/**\n * Gemini resolves function arguments more reliably when container nodes carry an\n * explicit `type`. Infer it when omitted: a node with `properties`/`required` is\n * an object, and a node with `items` is an array.\n */\nfunction inferContainerType(schema: JsonObject): void {\n if (schema.type !== undefined) return;\n if (isPlainObject(schema.properties) || Array.isArray(schema.required)) {\n schema.type = \"object\";\n } else if (schema.items !== undefined) {\n schema.type = \"array\";\n }\n}\n\n/** Resolve an `anyOf`/`oneOf` union node into the Gemini-compatible subset. */\nfunction sanitizeUnion(parent: JsonObject, branches: JsonValue[]): JsonValue {\n const sanitizedBranches = branches.map((branch) => sanitizeGeminiSchema(branch));\n const nullable =\n branches.some((branch) => isNullSchema(branch)) || parent.nullable === true;\n const nonNull = sanitizedBranches.filter((branch) => !isNullSchema(branch));\n\n const carryDescription = (target: JsonValue): JsonValue => {\n if (isPlainObject(target) && typeof parent.description === \"string\" && !(\"description\" in target)) {\n target.description = parent.description;\n }\n if (nullable && isPlainObject(target)) target.nullable = true;\n return target;\n };\n\n // Core fix: if any branch is an object/array schema, collapse to the first\n // such branch (Gemini rejects unions whose branch is a complex object). For\n // the TypeBox `Type.Union([Type.Object(...), Type.String()])` pattern this is\n // the object branch; a union of two distinct object shapes keeps only the\n // first, so the others' properties are intentionally dropped.\n const objectOrArray = nonNull.find((branch) => isObjectOrArraySchema(branch));\n if (objectOrArray) {\n return carryDescription(objectOrArray);\n }\n\n if (nonNull.length === 0) {\n return carryDescription({ type: \"string\" });\n }\n if (nonNull.length === 1) {\n return carryDescription(nonNull[0]);\n }\n\n // All-scalar union: prefer an `enum` when every branch is a literal/enum of\n // one underlying type; otherwise keep a scalar `anyOf` (Gemini accepts it).\n const literals: JsonValue[] = [];\n const scalarTypes = new Set<string>();\n let allLiteral = true;\n for (const branch of nonNull) {\n const values = literalValues(branch);\n if (!values) {\n allLiteral = false;\n break;\n }\n for (const value of values) {\n literals.push(value);\n const inferred = jsonScalarType(value);\n if (inferred) scalarTypes.add(inferred);\n }\n }\n if (allLiteral && literals.length > 0) {\n const collapsed: JsonObject = { enum: literals };\n if (scalarTypes.size === 1) collapsed.type = [...scalarTypes][0];\n return carryDescription(collapsed);\n }\n\n const node: JsonObject = { anyOf: nonNull };\n return carryDescription(node);\n}\n\n/** Drop `required` entries that are not present under `properties`. */\nfunction pruneRequired(schema: JsonObject): void {\n if (!Array.isArray(schema.required)) return;\n const properties = isPlainObject(schema.properties) ? schema.properties : undefined;\n if (!properties) {\n delete schema.required;\n return;\n }\n const filtered = schema.required.filter(\n (key): key is string => typeof key === \"string\" && key in properties,\n );\n if (filtered.length > 0) schema.required = filtered;\n else delete schema.required;\n}\n\n/** Sanitize a single tool's `parameters` schema in place-safe (returns new value). */\nfunction sanitizeToolParameters(parameters: JsonValue): JsonValue {\n return sanitizeGeminiSchema(parameters);\n}\n\n/**\n * Sanitize an outbound provider payload for GitHub Copilot Gemini models.\n *\n * Returns the payload unchanged for any other provider/model, or when the\n * payload has no sanitizable `tools`. Mirrors the `onPayload`\n * `(payload: unknown) => unknown` contract used elsewhere in the SDK wiring.\n */\nexport function sanitizeCopilotGeminiPayload(\n payload: unknown,\n model: Pick<Model<Api>, \"provider\" | \"api\" | \"id\">,\n): unknown {\n if (!isCopilotGeminiModel(model)) return payload;\n if (!isPlainObject(payload as JsonValue)) return payload;\n const payloadObject = payload as JsonObject;\n const tools = payloadObject.tools;\n if (!Array.isArray(tools) || tools.length === 0) return payload;\n\n let mutated = false;\n const sanitizedTools = tools.map((tool) => {\n if (!isPlainObject(tool)) return tool;\n // OpenAI chat-completions tool shape: { type: \"function\", function: { parameters } }\n if (isPlainObject(tool.function) && tool.function.parameters !== undefined) {\n mutated = true;\n return {\n ...tool,\n function: {\n ...tool.function,\n parameters: sanitizeToolParameters(tool.function.parameters),\n },\n };\n }\n // Defensive: flat tool shape { name, parameters }.\n if (tool.parameters !== undefined) {\n mutated = true;\n return { ...tool, parameters: sanitizeToolParameters(tool.parameters) };\n }\n return tool;\n });\n\n if (!mutated) return payload;\n return { ...payloadObject, tools: sanitizedTools };\n}\n"]}