@bastani/atomic 0.8.31-alpha.3 → 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.
- package/CHANGELOG.md +12 -0
- package/dist/builtin/cursor/package.json +2 -2
- package/dist/builtin/intercom/package.json +1 -1
- package/dist/builtin/mcp/CHANGELOG.md +5 -0
- package/dist/builtin/mcp/direct-tools.ts +4 -2
- package/dist/builtin/mcp/package.json +1 -1
- package/dist/builtin/mcp/proxy-modes.ts +4 -2
- package/dist/builtin/mcp/utils.ts +25 -0
- package/dist/builtin/subagents/package.json +1 -1
- package/dist/builtin/web-access/package.json +1 -1
- package/dist/builtin/workflows/CHANGELOG.md +5 -0
- package/dist/builtin/workflows/builtin/ralph.ts +1 -0
- package/dist/builtin/workflows/package.json +1 -1
- package/dist/builtin/workflows/src/runs/foreground/stage-runner.ts +114 -4
- package/dist/core/agent-session.d.ts +25 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +124 -8
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-guidance.d.ts +12 -0
- package/dist/core/auth-guidance.d.ts.map +1 -1
- package/dist/core/auth-guidance.js +24 -0
- package/dist/core/auth-guidance.js.map +1 -1
- package/dist/core/auth-storage.d.ts +42 -0
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +71 -10
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/copilot-gemini-payload-sanitizer.d.ts +72 -0
- package/dist/core/copilot-gemini-payload-sanitizer.d.ts.map +1 -0
- package/dist/core/copilot-gemini-payload-sanitizer.js +296 -0
- package/dist/core/copilot-gemini-payload-sanitizer.js.map +1 -0
- package/dist/core/copilot-gemini-reasoning.d.ts +118 -0
- package/dist/core/copilot-gemini-reasoning.d.ts.map +1 -0
- package/dist/core/copilot-gemini-reasoning.js +260 -0
- package/dist/core/copilot-gemini-reasoning.js.map +1 -0
- package/dist/core/copilot-gemini-tool-arguments.d.ts +42 -0
- package/dist/core/copilot-gemini-tool-arguments.d.ts.map +1 -0
- package/dist/core/copilot-gemini-tool-arguments.js +179 -0
- package/dist/core/copilot-gemini-tool-arguments.js.map +1 -0
- package/dist/core/flattened-tool-arguments.d.ts +41 -0
- package/dist/core/flattened-tool-arguments.d.ts.map +1 -0
- package/dist/core/flattened-tool-arguments.js +136 -0
- package/dist/core/flattened-tool-arguments.js.map +1 -0
- package/dist/core/http-dispatcher.d.ts.map +1 -1
- package/dist/core/http-dispatcher.js +5 -0
- package/dist/core/http-dispatcher.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +38 -8
- package/dist/core/sdk.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/docs/providers.md +1 -0
- package/docs/workflows.md +2 -0
- package/package.json +2 -2
|
@@ -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;IAC3C,MAAM;IACN,aAAa;IACb,MAAM;IACN,YAAY;IACZ,UAAU;IACV,OAAO;IACP,UAAU;CACX,CAAC,CAAC;AAEH;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,IAAI,GAAG,CAAS;IACrD,SAAS;IACT,KAAK;IACL,MAAM;IACN,OAAO;IACP,UAAU;IACV,aAAa;IACb,mBAAmB;IACnB,eAAe;IACf,uBAAuB;IACvB,sBAAsB;IACtB,iBAAiB;IACjB,kBAAkB;IAClB,OAAO;IACP,KAAK;IACL,IAAI;IACJ,MAAM;IACN,MAAM;IACN,QAAQ;IACR,SAAS;IACT,WAAW;IACX,WAAW;IACX,UAAU;IACV,UAAU;IACV,aAAa;IACb,SAAS;IACT,SAAS;IACT,kBAAkB;IAClB,kBAAkB;IAClB,YAAY;IACZ,eAAe;IACf,eAAe;IACf,SAAS;IACT,UAAU;IACV,OAAO;IACP,UAAU;IACV,WAAW;IAC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AC7B,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"]}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
2
|
+
/**
|
|
3
|
+
* Round-trips GitHub Copilot Gemini "thought signatures" so multi-turn tool use
|
|
4
|
+
* does not silently die after the first tool call.
|
|
5
|
+
*
|
|
6
|
+
* Why this exists
|
|
7
|
+
* ---------------
|
|
8
|
+
* `github-copilot` Gemini models (e.g. `gemini-3.1-pro-preview`) are served
|
|
9
|
+
* through Copilot's CAPI gateway, which proxies to Google's GenAI API. Gemini
|
|
10
|
+
* is a thinking model: when it emits a function/tool call it also returns an
|
|
11
|
+
* opaque **thought signature** that must be sent back, verbatim, on the next
|
|
12
|
+
* request or Gemini refuses to continue the reasoning chain.
|
|
13
|
+
*
|
|
14
|
+
* CAPI carries that signature in a non-standard OpenAI-completions field named
|
|
15
|
+
* **`reasoning_opaque`** (an encrypted blob) on the assistant message / streamed
|
|
16
|
+
* delta, and on replay it reads the same `reasoning_opaque` back off the
|
|
17
|
+
* assistant message and re-attaches the signature to each Gemini function-call
|
|
18
|
+
* part (keyed by `tool_call.id`). The underlying OpenAI-completions client
|
|
19
|
+
* (`@earendil-works/pi-ai`) does not understand `reasoning_opaque`; it captures
|
|
20
|
+
* thought signatures only from the OpenRouter-style
|
|
21
|
+
* `reasoning_details: [{ type: "reasoning.encrypted", id, data }]` shape, which
|
|
22
|
+
* CAPI never emits. So the real Gemini thought signature was being dropped on
|
|
23
|
+
* the way in and never replayed on the way out.
|
|
24
|
+
*
|
|
25
|
+
* With the signature missing, CAPI substitutes the sentinel
|
|
26
|
+
* `skip_thought_signature_validator` on the first replayed function call, and
|
|
27
|
+
* Gemini responds with an empty candidate / `finish_reason: "stop"` and zero
|
|
28
|
+
* output tokens — the harness sees a degenerate empty completion, retries with
|
|
29
|
+
* the same signature-less history, and eventually gives up: "Gemini just stops
|
|
30
|
+
* responding."
|
|
31
|
+
*
|
|
32
|
+
* What this does
|
|
33
|
+
* --------------
|
|
34
|
+
* Two gated, self-contained transforms bridge CAPI's `reasoning_opaque` to the
|
|
35
|
+
* `reasoning_details` mechanism the client already round-trips:
|
|
36
|
+
*
|
|
37
|
+
* - **Inbound** ({@link rewriteCopilotGeminiSseData} via
|
|
38
|
+
* {@link createCopilotGeminiSseStream}): rewrites the CAPI Gemini SSE
|
|
39
|
+
* stream so each streamed delta that carries both `reasoning_opaque` and a
|
|
40
|
+
* `tool_calls[].id` gains a
|
|
41
|
+
* `reasoning_details: [{ type: "reasoning.encrypted", id, data: <opaque> }]`
|
|
42
|
+
* entry. The client then stores it as the tool call's `thoughtSignature`.
|
|
43
|
+
* CAPI confirms `reasoning_opaque` rides on the same streamed delta as the
|
|
44
|
+
* first (id-bearing) tool-call chunk, so the association is exact.
|
|
45
|
+
* - **Outbound** ({@link restoreCopilotGeminiReasoningOpaque} from the
|
|
46
|
+
* `onPayload` hook): converts the `reasoning_details` the client re-emits on
|
|
47
|
+
* replayed assistant messages back into a single `reasoning_opaque` field on
|
|
48
|
+
* that assistant message, which is the only shape CAPI reads.
|
|
49
|
+
*
|
|
50
|
+
* Both transforms are gated to GitHub Copilot Gemini and are no-ops for every
|
|
51
|
+
* other provider/model (and for Gemini turns that carry no thought signature).
|
|
52
|
+
*/
|
|
53
|
+
type JsonObject = {
|
|
54
|
+
[key: string]: JsonValue;
|
|
55
|
+
};
|
|
56
|
+
type JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;
|
|
57
|
+
/**
|
|
58
|
+
* Inject `reasoning_details` into a parsed CAPI Gemini streaming chunk so the
|
|
59
|
+
* pi-ai OpenAI-completions parser captures the Gemini thought signature.
|
|
60
|
+
*
|
|
61
|
+
* For each `choices[].delta` that carries a non-empty `reasoning_opaque` string
|
|
62
|
+
* and a `tool_calls[]` entry with an `id`, adds a single
|
|
63
|
+
* `reasoning_details: [{ type: "reasoning.encrypted", id, data: <opaque> }]`
|
|
64
|
+
* entry keyed by that tool-call id. Returns whether the chunk was mutated.
|
|
65
|
+
*
|
|
66
|
+
* No-op when the delta already has `reasoning_details`, has no id-bearing tool
|
|
67
|
+
* call (e.g. argument-continuation deltas or pure-text thought chunks), or has
|
|
68
|
+
* no `reasoning_opaque`.
|
|
69
|
+
*/
|
|
70
|
+
export declare function injectCopilotGeminiReasoningDetails(chunk: JsonValue): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Rewrite the JSON payload of a single SSE `data:` line. Returns the original
|
|
73
|
+
* string unchanged when it is not a Gemini chunk that needs a thought signature
|
|
74
|
+
* bridged, or when parsing fails (fail-open: never corrupt the stream).
|
|
75
|
+
*/
|
|
76
|
+
export declare function rewriteCopilotGeminiSseData(dataPayload: string): string;
|
|
77
|
+
/**
|
|
78
|
+
* Wrap a CAPI Gemini SSE byte stream so `reasoning_opaque` is bridged into
|
|
79
|
+
* `reasoning_details`. Buffers across chunk boundaries and rewrites whole lines
|
|
80
|
+
* only; bytes that are not affected pass through unchanged.
|
|
81
|
+
*
|
|
82
|
+
* Implemented as a `ReadableStream` over the source reader (rather than a
|
|
83
|
+
* `TransformStream` piped via `pipeThrough`) so the transform pulls lazily and
|
|
84
|
+
* propagates cancellation to the upstream body.
|
|
85
|
+
*/
|
|
86
|
+
export declare function createCopilotGeminiSseStream(source: ReadableStream<Uint8Array>): ReadableStream<Uint8Array>;
|
|
87
|
+
/**
|
|
88
|
+
* Convert the `reasoning_details` the pi-ai client re-emits on replayed
|
|
89
|
+
* assistant messages back into the single `reasoning_opaque` field CAPI reads.
|
|
90
|
+
*
|
|
91
|
+
* For GitHub Copilot Gemini payloads, each assistant message that carries a
|
|
92
|
+
* `reasoning_details` entry of `type: "reasoning.encrypted"` has its `data`
|
|
93
|
+
* (the original CAPI thought-signature blob) promoted to `reasoning_opaque`,
|
|
94
|
+
* and the now-redundant `reasoning_details` removed. No-op for every other
|
|
95
|
+
* provider/model and for payloads without such messages.
|
|
96
|
+
*/
|
|
97
|
+
export declare function restoreCopilotGeminiReasoningOpaque(payload: unknown, model: Pick<Model<Api>, "provider" | "api" | "id">): unknown;
|
|
98
|
+
/**
|
|
99
|
+
* Rewrite a streaming CAPI Gemini response so its SSE body bridges
|
|
100
|
+
* `reasoning_opaque` into `reasoning_details`. Returns the original response
|
|
101
|
+
* untouched for non-Copilot hosts, non-event-stream responses, or bodyless
|
|
102
|
+
* responses, keeping the blast radius to streaming CAPI Gemini turns only.
|
|
103
|
+
*/
|
|
104
|
+
export declare function maybeRewriteCopilotGeminiResponse(url: string | undefined, response: Response): Response;
|
|
105
|
+
/**
|
|
106
|
+
* Install a `globalThis.fetch` wrapper that rewrites CAPI Gemini SSE responses
|
|
107
|
+
* to bridge `reasoning_opaque` into `reasoning_details` (see
|
|
108
|
+
* {@link createCopilotGeminiSseStream}). Idempotent.
|
|
109
|
+
*
|
|
110
|
+
* The OpenAI SDK used by the `openai-completions` provider resolves
|
|
111
|
+
* `globalThis.fetch` at client-construction time, and a new client is built per
|
|
112
|
+
* request, so wrapping the global before the first request is reliably picked
|
|
113
|
+
* up. Non-Copilot hosts and non-event-stream responses are returned untouched,
|
|
114
|
+
* keeping the blast radius to streaming CAPI Gemini turns only.
|
|
115
|
+
*/
|
|
116
|
+
export declare function installCopilotGeminiReasoningInterceptor(): void;
|
|
117
|
+
export {};
|
|
118
|
+
//# sourceMappingURL=copilot-gemini-reasoning.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copilot-gemini-reasoning.d.ts","sourceRoot":"","sources":["../../src/core/copilot-gemini-reasoning.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;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;AAS7E;;;;;;;;;;;;GAYG;AACH,wBAAgB,mCAAmC,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAiC7E;AAED;;;;GAIG;AACH,wBAAgB,2BAA2B,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAWvE;AAeD;;;;;;;;GAQG;AACH,wBAAgB,4BAA4B,CAC1C,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,cAAc,CAAC,UAAU,CAAC,CAyC5B;AAED;;;;;;;;;GASG;AACH,wBAAgB,mCAAmC,CACjD,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,KAAK,GAAG,IAAI,CAAC,GACjD,OAAO,CA2BT;AAyBD;;;;;GAKG;AACH,wBAAgB,iCAAiC,CAC/C,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,QAAQ,EAAE,QAAQ,GACjB,QAAQ,CAWV;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,wCAAwC,IAAI,IAAI,CAuB/D","sourcesContent":["import type { Api, Model } from \"@earendil-works/pi-ai\";\nimport { isCopilotGeminiModel } from \"./copilot-gemini-payload-sanitizer.ts\";\n\n/**\n * Round-trips GitHub Copilot Gemini \"thought signatures\" so multi-turn tool use\n * does not silently die after the first tool call.\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, which proxies to Google's GenAI API. Gemini\n * is a thinking model: when it emits a function/tool call it also returns an\n * opaque **thought signature** that must be sent back, verbatim, on the next\n * request or Gemini refuses to continue the reasoning chain.\n *\n * CAPI carries that signature in a non-standard OpenAI-completions field named\n * **`reasoning_opaque`** (an encrypted blob) on the assistant message / streamed\n * delta, and on replay it reads the same `reasoning_opaque` back off the\n * assistant message and re-attaches the signature to each Gemini function-call\n * part (keyed by `tool_call.id`). The underlying OpenAI-completions client\n * (`@earendil-works/pi-ai`) does not understand `reasoning_opaque`; it captures\n * thought signatures only from the OpenRouter-style\n * `reasoning_details: [{ type: \"reasoning.encrypted\", id, data }]` shape, which\n * CAPI never emits. So the real Gemini thought signature was being dropped on\n * the way in and never replayed on the way out.\n *\n * With the signature missing, CAPI substitutes the sentinel\n * `skip_thought_signature_validator` on the first replayed function call, and\n * Gemini responds with an empty candidate / `finish_reason: \"stop\"` and zero\n * output tokens — the harness sees a degenerate empty completion, retries with\n * the same signature-less history, and eventually gives up: \"Gemini just stops\n * responding.\"\n *\n * What this does\n * --------------\n * Two gated, self-contained transforms bridge CAPI's `reasoning_opaque` to the\n * `reasoning_details` mechanism the client already round-trips:\n *\n * - **Inbound** ({@link rewriteCopilotGeminiSseData} via\n * {@link createCopilotGeminiSseStream}): rewrites the CAPI Gemini SSE\n * stream so each streamed delta that carries both `reasoning_opaque` and a\n * `tool_calls[].id` gains a\n * `reasoning_details: [{ type: \"reasoning.encrypted\", id, data: <opaque> }]`\n * entry. The client then stores it as the tool call's `thoughtSignature`.\n * CAPI confirms `reasoning_opaque` rides on the same streamed delta as the\n * first (id-bearing) tool-call chunk, so the association is exact.\n * - **Outbound** ({@link restoreCopilotGeminiReasoningOpaque} from the\n * `onPayload` hook): converts the `reasoning_details` the client re-emits on\n * replayed assistant messages back into a single `reasoning_opaque` field on\n * that assistant message, which is the only shape CAPI reads.\n *\n * Both transforms are gated to GitHub Copilot Gemini and are no-ops for every\n * other provider/model (and for Gemini turns that carry no thought signature).\n */\n\ntype JsonObject = { [key: string]: JsonValue };\ntype JsonValue = string | number | boolean | null | JsonValue[] | JsonObject;\n\n/** OpenRouter-style encrypted reasoning detail the pi-ai client round-trips. */\nconst REASONING_ENCRYPTED_TYPE = \"reasoning.encrypted\";\n\nfunction isPlainObject(value: unknown): value is JsonObject {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * Inject `reasoning_details` into a parsed CAPI Gemini streaming chunk so the\n * pi-ai OpenAI-completions parser captures the Gemini thought signature.\n *\n * For each `choices[].delta` that carries a non-empty `reasoning_opaque` string\n * and a `tool_calls[]` entry with an `id`, adds a single\n * `reasoning_details: [{ type: \"reasoning.encrypted\", id, data: <opaque> }]`\n * entry keyed by that tool-call id. Returns whether the chunk was mutated.\n *\n * No-op when the delta already has `reasoning_details`, has no id-bearing tool\n * call (e.g. argument-continuation deltas or pure-text thought chunks), or has\n * no `reasoning_opaque`.\n */\nexport function injectCopilotGeminiReasoningDetails(chunk: JsonValue): boolean {\n if (!isPlainObject(chunk)) return false;\n const choices = chunk.choices;\n if (!Array.isArray(choices)) return false;\n\n let mutated = false;\n for (const choice of choices) {\n if (!isPlainObject(choice)) continue;\n const delta = choice.delta;\n if (!isPlainObject(delta)) continue;\n\n const opaque = delta.reasoning_opaque;\n if (typeof opaque !== \"string\" || opaque.length === 0) continue;\n\n // Already carries the encrypted detail (don't double-inject / clobber).\n if (Array.isArray(delta.reasoning_details) && delta.reasoning_details.length > 0) {\n continue;\n }\n\n const toolCalls = delta.tool_calls;\n if (!Array.isArray(toolCalls)) continue;\n const idBearing = toolCalls.find(\n (call): call is JsonObject =>\n isPlainObject(call) && typeof call.id === \"string\" && call.id.length > 0,\n );\n if (!idBearing) continue;\n\n delta.reasoning_details = [\n { type: REASONING_ENCRYPTED_TYPE, id: idBearing.id as string, data: opaque },\n ];\n mutated = true;\n }\n return mutated;\n}\n\n/**\n * Rewrite the JSON payload of a single SSE `data:` line. Returns the original\n * string unchanged when it is not a Gemini chunk that needs a thought signature\n * bridged, or when parsing fails (fail-open: never corrupt the stream).\n */\nexport function rewriteCopilotGeminiSseData(dataPayload: string): string {\n // Cheap gate: only chunks that actually carry a thought signature are touched.\n if (!dataPayload.includes(\"reasoning_opaque\")) return dataPayload;\n let parsed: JsonValue;\n try {\n parsed = JSON.parse(dataPayload) as JsonValue;\n } catch {\n return dataPayload;\n }\n if (!injectCopilotGeminiReasoningDetails(parsed)) return dataPayload;\n return JSON.stringify(parsed);\n}\n\n/** Rewrite one SSE line, preserving a trailing carriage return when present. */\nfunction rewriteSseLine(line: string): string {\n const hasCr = line.endsWith(\"\\r\");\n const core = hasCr ? line.slice(0, -1) : line;\n if (!core.startsWith(\"data:\")) return line;\n const payload = core.slice(\"data:\".length).trimStart();\n if (payload.length === 0 || payload === \"[DONE]\") return line;\n const rewritten = rewriteCopilotGeminiSseData(payload);\n if (rewritten === payload) return line;\n const rebuilt = `data: ${rewritten}`;\n return hasCr ? `${rebuilt}\\r` : rebuilt;\n}\n\n/**\n * Wrap a CAPI Gemini SSE byte stream so `reasoning_opaque` is bridged into\n * `reasoning_details`. Buffers across chunk boundaries and rewrites whole lines\n * only; bytes that are not affected pass through unchanged.\n *\n * Implemented as a `ReadableStream` over the source reader (rather than a\n * `TransformStream` piped via `pipeThrough`) so the transform pulls lazily and\n * propagates cancellation to the upstream body.\n */\nexport function createCopilotGeminiSseStream(\n source: ReadableStream<Uint8Array>,\n): ReadableStream<Uint8Array> {\n const reader = source.getReader();\n const decoder = new TextDecoder();\n const encoder = new TextEncoder();\n let buffer = \"\";\n\n return new ReadableStream<Uint8Array>({\n async pull(controller) {\n // Loop until we emit at least one chunk or close, so a read that yields no\n // complete line still makes progress without relying on the runtime to\n // re-invoke pull after a no-enqueue return.\n for (;;) {\n const { done, value } = await reader.read();\n if (done) {\n buffer += decoder.decode();\n if (buffer.length > 0) {\n controller.enqueue(encoder.encode(rewriteSseLine(buffer)));\n buffer = \"\";\n }\n controller.close();\n return;\n }\n buffer += decoder.decode(value, { stream: true });\n let newlineIndex = buffer.indexOf(\"\\n\");\n let out = \"\";\n while (newlineIndex !== -1) {\n const line = buffer.slice(0, newlineIndex);\n buffer = buffer.slice(newlineIndex + 1);\n out += `${rewriteSseLine(line)}\\n`;\n newlineIndex = buffer.indexOf(\"\\n\");\n }\n if (out.length > 0) {\n controller.enqueue(encoder.encode(out));\n return;\n }\n }\n },\n cancel(reason) {\n return reader.cancel(reason);\n },\n });\n}\n\n/**\n * Convert the `reasoning_details` the pi-ai client re-emits on replayed\n * assistant messages back into the single `reasoning_opaque` field CAPI reads.\n *\n * For GitHub Copilot Gemini payloads, each assistant message that carries a\n * `reasoning_details` entry of `type: \"reasoning.encrypted\"` has its `data`\n * (the original CAPI thought-signature blob) promoted to `reasoning_opaque`,\n * and the now-redundant `reasoning_details` removed. No-op for every other\n * provider/model and for payloads without such messages.\n */\nexport function restoreCopilotGeminiReasoningOpaque(\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 messages = payloadObject.messages;\n if (!Array.isArray(messages)) return payload;\n\n let mutated = false;\n const nextMessages = messages.map((message) => {\n if (!isPlainObject(message) || message.role !== \"assistant\") return message;\n const details = message.reasoning_details;\n if (!Array.isArray(details) || details.length === 0) return message;\n const encrypted = details.find(\n (detail): detail is JsonObject =>\n isPlainObject(detail) &&\n detail.type === REASONING_ENCRYPTED_TYPE &&\n typeof detail.data === \"string\" &&\n detail.data.length > 0,\n );\n if (!encrypted) return message;\n mutated = true;\n const { reasoning_details: _omitted, ...rest } = message;\n return { ...rest, reasoning_opaque: encrypted.data as string };\n });\n\n if (!mutated) return payload;\n return { ...payloadObject, messages: nextMessages };\n}\n\n/** Whether the URL targets Copilot's CAPI gateway (`*.githubcopilot.com`). */\nfunction isCopilotApiHost(url: string): boolean {\n try {\n const host = new URL(url).hostname.toLowerCase();\n // Exact host or a real subdomain only — never a look-alike suffix such as\n // `notgithubcopilot.com` (CodeQL: incomplete URL substring sanitization).\n return host === \"githubcopilot.com\" || host.endsWith(\".githubcopilot.com\");\n } catch {\n return false;\n }\n}\n\n/** Resolve the request URL string from a `fetch` input argument. */\nfunction resolveRequestUrl(input: Parameters<typeof fetch>[0]): string | undefined {\n if (typeof input === \"string\") return input;\n if (input instanceof URL) return input.href;\n if (typeof input === \"object\" && input !== null && \"url\" in input) {\n const url = (input as { url?: unknown }).url;\n if (typeof url === \"string\") return url;\n }\n return undefined;\n}\n\n/**\n * Rewrite a streaming CAPI Gemini response so its SSE body bridges\n * `reasoning_opaque` into `reasoning_details`. Returns the original response\n * untouched for non-Copilot hosts, non-event-stream responses, or bodyless\n * responses, keeping the blast radius to streaming CAPI Gemini turns only.\n */\nexport function maybeRewriteCopilotGeminiResponse(\n url: string | undefined,\n response: Response,\n): Response {\n if (!url || !isCopilotApiHost(url)) return response;\n const contentType = response.headers.get(\"content-type\") ?? \"\";\n if (!contentType.includes(\"text/event-stream\")) return response;\n if (!response.body) return response;\n const transformed = createCopilotGeminiSseStream(response.body);\n return new Response(transformed, {\n status: response.status,\n statusText: response.statusText,\n headers: response.headers,\n });\n}\n\nlet originalFetch: typeof fetch | undefined;\n\n/**\n * Install a `globalThis.fetch` wrapper that rewrites CAPI Gemini SSE responses\n * to bridge `reasoning_opaque` into `reasoning_details` (see\n * {@link createCopilotGeminiSseStream}). Idempotent.\n *\n * The OpenAI SDK used by the `openai-completions` provider resolves\n * `globalThis.fetch` at client-construction time, and a new client is built per\n * request, so wrapping the global before the first request is reliably picked\n * up. Non-Copilot hosts and non-event-stream responses are returned untouched,\n * keeping the blast radius to streaming CAPI Gemini turns only.\n */\nexport function installCopilotGeminiReasoningInterceptor(): void {\n if (originalFetch) return;\n if (typeof globalThis.fetch !== \"function\") return;\n const base = globalThis.fetch;\n originalFetch = base;\n const boundFetch = base.bind(globalThis);\n\n const wrapped = (async (input, init) => {\n const response = await boundFetch(input, init);\n try {\n return maybeRewriteCopilotGeminiResponse(resolveRequestUrl(input), response);\n } catch {\n return response;\n }\n }) as typeof fetch;\n\n // Preserve `fetch.preconnect` so the wrapper remains a drop-in replacement.\n const preconnect = (base as { preconnect?: unknown }).preconnect;\n if (typeof preconnect === \"function\") {\n (wrapped as { preconnect?: unknown }).preconnect = preconnect.bind(globalThis);\n }\n\n globalThis.fetch = wrapped;\n}\n"]}
|