@bastani/pi-ai 0.9.74 → 0.9.75
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 +43 -0
- package/README.md +3 -1
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +21 -7
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +4 -2
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/constrained-sampling.d.ts +0 -2
- package/dist/api/constrained-sampling.d.ts.map +1 -1
- package/dist/api/constrained-sampling.js +0 -13
- package/dist/api/constrained-sampling.js.map +1 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +9 -5
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-images-options.d.ts +18 -5
- package/dist/api/openai-images-options.d.ts.map +1 -1
- package/dist/api/openai-images-options.js +9 -3
- package/dist/api/openai-images-options.js.map +1 -1
- package/dist/api/openai-images.d.ts.map +1 -1
- package/dist/api/openai-images.js +121 -19
- package/dist/api/openai-images.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +10 -3
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/strict-tool-limits.d.ts +13 -0
- package/dist/api/strict-tool-limits.d.ts.map +1 -0
- package/dist/api/strict-tool-limits.js +50 -0
- package/dist/api/strict-tool-limits.js.map +1 -0
- package/dist/auth/types.d.ts +7 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/auth/types.js.map +1 -1
- package/dist/cache-warming.d.ts +53 -0
- package/dist/cache-warming.d.ts.map +1 -0
- package/dist/cache-warming.js +82 -0
- package/dist/cache-warming.js.map +1 -0
- package/dist/image-generation-error.d.ts +10 -3
- package/dist/image-generation-error.d.ts.map +1 -1
- package/dist/image-generation-error.js +3 -2
- package/dist/image-generation-error.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +15 -6
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +22 -7
- package/dist/models.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/providers/data/anthropic.json +1 -1
- package/dist/providers/data/azure.json +1 -1
- package/dist/providers/data/google.json +1 -1
- package/dist/providers/data/meta.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/github-copilot-errors.d.ts +17 -5
- package/dist/providers/github-copilot-errors.d.ts.map +1 -1
- package/dist/providers/github-copilot-errors.js +55 -11
- package/dist/providers/github-copilot-errors.js.map +1 -1
- package/dist/providers/github-copilot-stream-attempt.d.ts +27 -0
- package/dist/providers/github-copilot-stream-attempt.d.ts.map +1 -0
- package/dist/providers/github-copilot-stream-attempt.js +45 -0
- package/dist/providers/github-copilot-stream-attempt.js.map +1 -0
- package/dist/providers/github-copilot.d.ts +10 -2
- package/dist/providers/github-copilot.d.ts.map +1 -1
- package/dist/providers/github-copilot.js +62 -36
- package/dist/providers/github-copilot.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/stream-deadline.d.ts +6 -2
- package/dist/utils/stream-deadline.d.ts.map +1 -1
- package/dist/utils/stream-deadline.js +12 -3
- package/dist/utils/stream-deadline.js.map +1 -1
- package/dist/utils/version.d.ts +1 -1
- package/dist/utils/version.js +1 -1
- package/dist/utils/version.js.map +1 -1
- package/package.json +3 -3
|
@@ -139,19 +139,6 @@ export function prefersJsonSchemaStrictSampling(tool) {
|
|
|
139
139
|
const config = tool.constrainedSampling;
|
|
140
140
|
return !!config && config.type === "json_schema" && config.strict === "prefer";
|
|
141
141
|
}
|
|
142
|
-
/**
|
|
143
|
-
* Claude rejects (on the Anthropic API and on Bedrock) a request whose strict tools exceed its limits with one of these
|
|
144
|
-
* messages. Observed limits: 20 strict tools, and 16 optional or union-typed parameters across all strict tools.
|
|
145
|
-
*/
|
|
146
|
-
const STRICT_TOOL_LIMIT_ERROR_MESSAGES = [
|
|
147
|
-
"The compiled grammar is too large",
|
|
148
|
-
"Too many strict tools (",
|
|
149
|
-
"Schemas contains too many parameters with union types (",
|
|
150
|
-
];
|
|
151
|
-
/** Returns true when a provider error message says the request's strict tools exceed the provider's limits. */
|
|
152
|
-
export function isStrictToolLimitErrorMessage(message) {
|
|
153
|
-
return STRICT_TOOL_LIMIT_ERROR_MESSAGES.some((limitMessage) => message.includes(limitMessage));
|
|
154
|
-
}
|
|
155
142
|
export function getJsonSchemaToolParameters(tool, strict, profile) {
|
|
156
143
|
return (strict === true ? makeStrictJsonSchema(tool.parameters, profile) : tool.parameters);
|
|
157
144
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constrained-sampling.js","sourceRoot":"","sources":["../../src/api/constrained-sampling.ts"],"names":[],"mappings":"AASA,MAAM,gCAAiC,SAAQ,KAAK;CAAG;AAEvD,MAAM,8BAA8B,GAAG;IACtC,MAAM;IACN,OAAO;IACP,aAAa;IACb,OAAO;IACP,OAAO;IACP,mBAAmB;IACnB,kBAAkB;IAClB,cAAc;IACd,uBAAuB;IACvB,eAAe;IACf,UAAU;IACV,aAAa;IACb,KAAK;IACL,IAAI;IACJ,MAAM;IACN,MAAM;CACG,CAAC;AAQX,MAAM,CAAC,MAAM,oCAAoC,GAA4B,MAAM,CAAC,MAAM,CAAC;IAC1F,mBAAmB,EAAE,MAAM,CAAC,MAAM,CAAC;QAClC,SAAS;QACT,SAAS;QACT,kBAAkB;QAClB,kBAAkB;QAClB,YAAY;QACZ,aAAa;QACb,aAAa;QACb,aAAa;QACb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iB,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,eAAgB;YACjE,aAAa,EAAE,yBAAyB,CAAC,IAAI,CAAC;SAC9C,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,MAAM,IAAI,KAAK,CAAC,SAAS,IAAI,CAAC,IAAI,8CAA8C,OAAO,GAAG,CAAC,CAAC;IAC7F,CAAC;AACF,CAAC;AAED,MAAM,UAAU,gCAAgC,CAC/C,KAAyB,EACzB,0BAAmC;IAEnC,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC7C,KAAK,MAAM,IAAI,IAAI,KAAK,IAAI,EAAE,EAAE,CAAC;QAChC,MAAM,OAAO,GAAG,iCAAiC,CAAC,IAAI,EAAE,0BAA0B,CAAC,CAAC;QACpF,IAAI,OAAO,EAAE,CAAC;YACb,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;QAClD,CAAC;IACF,CAAC;IACD,OAAO,UAAU,CAAC;AACnB,CAAC","sourcesContent":["import type { Tool } from \"../types.ts\";\n\ninterface JsonSchemaObject {\n\t[key: string]: unknown;\n\ttype?: unknown;\n\tproperties?: Record<string, JsonSchemaObject | undefined>;\n\trequired?: unknown;\n}\n\nclass UnsupportedStrictJsonSchemaError extends Error {}\n\nconst UNSUPPORTED_STRICT_SCHEMA_KEYS = [\n\t\"$ref\",\n\t\"$defs\",\n\t\"definitions\",\n\t\"allOf\",\n\t\"oneOf\",\n\t\"patternProperties\",\n\t\"dependentSchemas\",\n\t\"dependencies\",\n\t\"unevaluatedProperties\",\n\t\"propertyNames\",\n\t\"contains\",\n\t\"prefixItems\",\n\t\"not\",\n\t\"if\",\n\t\"then\",\n\t\"else\",\n] as const;\n\nexport interface StrictJsonSchemaProfile {\n\treadonly unsupportedKeywords: readonly string[];\n\treadonly maxMinItems?: number;\n\treadonly supportedStringFormats?: readonly string[];\n}\n\nexport const ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE: StrictJsonSchemaProfile = Object.freeze({\n\tunsupportedKeywords: Object.freeze([\n\t\t\"minimum\",\n\t\t\"maximum\",\n\t\t\"exclusiveMinimum\",\n\t\t\"exclusiveMaximum\",\n\t\t\"multipleOf\",\n\t\t\"uniqueItems\",\n\t\t\"minContains\",\n\t\t\"maxContains\",\n\t\t\"minProperties\",\n\t\t\"maxProperties\",\n\t\t\"maxItems\",\n\t]),\n\tmaxMinItems: 1,\n\tsupportedStringFormats: Object.freeze([\"date-time\", \"time\", \"date\", \"duration\", \"email\", \"hostname\", \"uri\", \"ipv4\", \"ipv6\", \"uuid\"]),\n});\n\nfunction isJsonSchemaObject(value: unknown): value is JsonSchemaObject {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction isStructuredSchema(schema: unknown): boolean {\n\tif (!isJsonSchemaObject(schema)) return false;\n\tconst types = typeof schema.type === \"string\" ? [schema.type] : Array.isArray(schema.type) ? schema.type : [];\n\treturn (\n\t\ttypes.includes(\"object\") ||\n\t\ttypes.includes(\"array\") ||\n\t\tschema.properties !== undefined ||\n\t\tschema.items !== undefined\n\t);\n}\n\nfunction schemaAllowsNull(schema: unknown): boolean {\n\tif (!isJsonSchemaObject(schema)) return false;\n\tif (schema.type === \"null\" || (Array.isArray(schema.type) && schema.type.includes(\"null\"))) return true;\n\tif (schema.const === null || (Array.isArray(schema.enum) && schema.enum.includes(null))) return true;\n\treturn Array.isArray(schema.anyOf) && schema.anyOf.some((variant) => schemaAllowsNull(variant));\n}\n\nfunction makeJsonSchemaNodeStrict(schema: unknown, profile: StrictJsonSchemaProfile | undefined): void {\n\tif (!isJsonSchemaObject(schema)) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"boolean schemas are unsupported\");\n\t}\n\tfor (const key of [...UNSUPPORTED_STRICT_SCHEMA_KEYS, ...(profile?.unsupportedKeywords ?? [])]) {\n\t\tif (schema[key] !== undefined) {\n\t\t\tthrow new UnsupportedStrictJsonSchemaError(`${key} schemas are unsupported`);\n\t\t}\n\t}\n\tif (\n\t\tprofile?.maxMinItems !== undefined &&\n\t\tschema.minItems !== undefined &&\n\t\t(typeof schema.minItems !== \"number\" || !Number.isInteger(schema.minItems) || schema.minItems < 0 || schema.minItems > profile.maxMinItems)\n\t) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(`minItems must be an integer from 0 to ${profile.maxMinItems}`);\n\t}\n\tif (profile?.supportedStringFormats && schema.format !== undefined &&\n\t\t(typeof schema.format !== \"string\" || !profile.supportedStringFormats.includes(schema.format))) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(`format: ${JSON.stringify(schema.format)} is unsupported`);\n\t}\n\n\tif (schema.anyOf !== undefined) {\n\t\tif (!Array.isArray(schema.anyOf) || schema.anyOf.length === 0) {\n\t\t\tthrow new UnsupportedStrictJsonSchemaError(\"anyOf must contain at least one schema\");\n\t\t}\n\t\tfor (const variant of schema.anyOf) {\n\t\t\tif (isStructuredSchema(variant)) {\n\t\t\t\tthrow new UnsupportedStrictJsonSchemaError(\"object and array unions are unsupported\");\n\t\t\t}\n\t\t\tmakeJsonSchemaNodeStrict(variant, profile);\n\t\t}\n\t}\n\n\tif (schema.items !== undefined) {\n\t\tif (Array.isArray(schema.items)) {\n\t\t\tthrow new UnsupportedStrictJsonSchemaError(\"tuple schemas are unsupported\");\n\t\t}\n\t\tmakeJsonSchemaNodeStrict(schema.items, profile);\n\t}\n\n\tconst isObjectSchema = schema.type === \"object\";\n\tif (schema.properties !== undefined && !isObjectSchema) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"properties require type object\");\n\t}\n\tif (!isObjectSchema) return;\n\tif (schema.additionalProperties !== undefined && schema.additionalProperties !== false) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"schema-valued or true additionalProperties is unsupported\");\n\t}\n\tif (schema.properties !== undefined && !isJsonSchemaObject(schema.properties)) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"object properties must be a schema map\");\n\t}\n\tif (\n\t\tschema.required !== undefined &&\n\t\t(!Array.isArray(schema.required) || schema.required.some((key) => typeof key !== \"string\"))\n\t) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"object required must be a string array\");\n\t}\n\n\tconst properties = schema.properties ?? {};\n\tconst propertyNames = Object.keys(properties);\n\tconst required = new Set(Array.isArray(schema.required) ? schema.required : []);\n\tif ([...required].some((key) => !propertyNames.includes(key))) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"required contains an unknown property\");\n\t}\n\tfor (const [key, property] of Object.entries(properties)) {\n\t\tmakeJsonSchemaNodeStrict(property, profile);\n\t\tif (!required.has(key) && !schemaAllowsNull(property)) {\n\t\t\tproperties[key] = { anyOf: [property, { type: \"null\" }] };\n\t\t}\n\t}\n\tschema.required = propertyNames;\n\tschema.additionalProperties = false;\n}\n\n/** Convert a tool schema to the strict subset expected by provider constrained sampling. */\nexport function makeStrictJsonSchema(\n\tschema: Tool[\"parameters\"],\n\tprofile?: StrictJsonSchemaProfile,\n): Record<string, unknown> {\n\tconst cloned: unknown = structuredClone(schema);\n\tif (!isJsonSchemaObject(cloned)) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"root schema must have type object\");\n\t}\n\tmakeJsonSchemaNodeStrict(cloned, profile);\n\tif (cloned.type !== \"object\") {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"root schema must have type object\");\n\t}\n\treturn cloned;\n}\n\n/** Returns true for a tool that asks for strict JSON-schema sampling only when possible (`strict: \"prefer\"`). */\nexport function prefersJsonSchemaStrictSampling(tool: Tool): boolean {\n\tconst config = tool.constrainedSampling;\n\treturn !!config && config.type === \"json_schema\" && config.strict === \"prefer\";\n}\n\n/**\n * Claude rejects (on the Anthropic API and on Bedrock) a request whose strict tools exceed its limits with one of these\n * messages. Observed limits: 20 strict tools, and 16 optional or union-typed parameters across all strict tools.\n */\nconst STRICT_TOOL_LIMIT_ERROR_MESSAGES = [\n\t\"The compiled grammar is too large\",\n\t\"Too many strict tools (\",\n\t\"Schemas contains too many parameters with union types (\",\n];\n\n/** Returns true when a provider error message says the request's strict tools exceed the provider's limits. */\nexport function isStrictToolLimitErrorMessage(message: string): boolean {\n\treturn STRICT_TOOL_LIMIT_ERROR_MESSAGES.some((limitMessage) => message.includes(limitMessage));\n}\n\nexport function getJsonSchemaToolParameters(\n\ttool: Tool,\n\tstrict: boolean | undefined,\n\tprofile?: StrictJsonSchemaProfile,\n): Tool[\"parameters\"] {\n\treturn (strict === true ? makeStrictJsonSchema(tool.parameters, profile) : tool.parameters) as Tool[\"parameters\"];\n}\n\nexport interface GrammarConstrainedSampling {\n\tformat: \"lark\" | \"regex\";\n\tdefinition: string;\n\tinputProperty: string;\n}\n\nexport interface GrammarToolInputJsonBuffer {\n\tinput: string;\n\tstarted: boolean;\n\tclosed: boolean;\n}\n\nexport function getGrammarToolInput(\n\ttoolName: string,\n\targuments_: Record<string, unknown>,\n\tinputProperty: string,\n): string {\n\tconst input = arguments_[inputProperty];\n\tif (typeof input !== \"string\") {\n\t\tthrow new Error(`Grammar tool call \"${toolName}\" requires argument \"${inputProperty}\" to be a string.`);\n\t}\n\treturn input;\n}\n\n/**\n * Append `inputDelta` to a streaming grammar tool input and return the matching piece of its JSON arguments text, or\n * `undefined` for an empty delta. Costs time linear in the delta, never in the input so far.\n */\nexport function appendGrammarToolInputJsonDelta(\n\tbuffer: GrammarToolInputJsonBuffer,\n\tinputProperty: string,\n\tinputDelta: string,\n): string | undefined {\n\tif (buffer.closed) {\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed after it was closed`);\n\t}\n\tif (inputDelta.length === 0) return undefined;\n\tbuffer.input += inputDelta;\n\treturn grammarToolInputJsonStart(buffer, inputProperty) + JSON.stringify(inputDelta).slice(1, -1);\n}\n\n/**\n * Close a streaming grammar tool input and return the rest of its JSON arguments text. `finalInput` is the complete\n * input when the provider sends one; it must continue the input streamed so far. Closing again with the same input\n * returns `undefined`.\n */\nexport function closeGrammarToolInputJson(\n\tbuffer: GrammarToolInputJsonBuffer,\n\tinputProperty: string,\n\tfinalInput: string | undefined,\n): string | undefined {\n\tif (buffer.closed) {\n\t\tif (finalInput === undefined || finalInput === buffer.input) return undefined;\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed after it was closed`);\n\t}\n\tif (finalInput !== undefined && !finalInput.startsWith(buffer.input)) {\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed non-monotonically`);\n\t}\n\tconst rest = finalInput === undefined ? \"\" : finalInput.slice(buffer.input.length);\n\tif (finalInput !== undefined) buffer.input = finalInput;\n\tbuffer.closed = true;\n\treturn `${grammarToolInputJsonStart(buffer, inputProperty)}${JSON.stringify(rest).slice(1, -1)}\"}`;\n}\n\n/** The opening of the JSON arguments text, once. */\nfunction grammarToolInputJsonStart(buffer: GrammarToolInputJsonBuffer, inputProperty: string): string {\n\tif (buffer.started) return \"\";\n\tbuffer.started = true;\n\treturn `{${JSON.stringify(inputProperty)}:\"`;\n}\n\nfunction inferGrammarInputProperty(tool: Tool): string {\n\tconst schema = tool.parameters as JsonSchemaObject;\n\tif (schema.type !== \"object\") {\n\t\tthrow new Error(\"grammar constrained sampling requires an object parameter schema\");\n\t}\n\tif (!Array.isArray(schema.required) || schema.required.length !== 1 || typeof schema.required[0] !== \"string\") {\n\t\tthrow new Error(\"grammar constrained sampling requires exactly one required string property\");\n\t}\n\n\tconst inputProperty = schema.required[0];\n\tif (!schema.properties?.[inputProperty]) {\n\t\tthrow new Error(`grammar constrained sampling requires a properties entry for ${inputProperty}`);\n\t}\n\tif (schema.properties[inputProperty]?.type !== \"string\") {\n\t\tthrow new Error(`grammar constrained sampling property ${inputProperty} must have type string`);\n\t}\n\treturn inputProperty;\n}\n\nexport function resolveJsonSchemaStrictSampling(\n\ttool: Tool,\n\tsupportsStrictMode: boolean,\n\tprofile?: StrictJsonSchemaProfile,\n): boolean | undefined {\n\tconst config = tool.constrainedSampling;\n\tif (!config || config.type !== \"json_schema\") return undefined;\n\n\tif (supportsStrictMode) {\n\t\ttry {\n\t\t\tmakeStrictJsonSchema(tool.parameters, profile);\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tif (!(error instanceof UnsupportedStrictJsonSchemaError)) throw error;\n\t\t\tif (config.strict !== \"require\") return undefined;\n\t\t\tthrow new Error(`Tool \"${tool.name}\" requires JSON-schema constrained sampling, but ${error.message}.`);\n\t\t}\n\t}\n\tif (config.strict === \"require\") {\n\t\tthrow new Error(\n\t\t\t`Tool \"${tool.name}\" requires JSON-schema constrained sampling, but strict tools are unsupported.`,\n\t\t);\n\t}\n\treturn undefined;\n}\n\nexport function resolveGrammarConstrainedSampling(\n\ttool: Tool,\n\tsupportsOpenAIGrammarTools: boolean,\n): GrammarConstrainedSampling | undefined {\n\tconst config = tool.constrainedSampling;\n\tif (!config || config.type !== \"grammar\") {\n\t\treturn undefined;\n\t}\n\n\tif (!supportsOpenAIGrammarTools) {\n\t\treturn undefined;\n\t}\n\n\tconst larkDefinition = config.variants.openai_lark;\n\tconst regexDefinition = config.variants.openai_regex;\n\tconst hasLarkDefinition = typeof larkDefinition === \"string\" && larkDefinition.trim().length > 0;\n\tconst hasRegexDefinition = typeof regexDefinition === \"string\" && regexDefinition.trim().length > 0;\n\tif (!hasLarkDefinition && !hasRegexDefinition) {\n\t\tthrow new Error(\n\t\t\t`Tool \"${tool.name}\" cannot use grammar constrained sampling: no supported grammar variant was provided.`,\n\t\t);\n\t}\n\n\ttry {\n\t\treturn {\n\t\t\tformat: hasLarkDefinition ? \"lark\" : \"regex\",\n\t\t\tdefinition: hasLarkDefinition ? larkDefinition : regexDefinition!,\n\t\t\tinputProperty: inferGrammarInputProperty(tool),\n\t\t};\n\t} catch (error) {\n\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\tthrow new Error(`Tool \"${tool.name}\" cannot use grammar constrained sampling: ${message}.`);\n\t}\n}\n\nexport function createGrammarToolInputProperties(\n\ttools: Tool[] | undefined,\n\tsupportsOpenAIGrammarTools: boolean,\n): ReadonlyMap<string, string> {\n\tconst properties = new Map<string, string>();\n\tfor (const tool of tools ?? []) {\n\t\tconst grammar = resolveGrammarConstrainedSampling(tool, supportsOpenAIGrammarTools);\n\t\tif (grammar) {\n\t\t\tproperties.set(tool.name, grammar.inputProperty);\n\t\t}\n\t}\n\treturn properties;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"constrained-sampling.js","sourceRoot":"","sources":["../../src/api/constrained-sampling.ts"],"names":[],"mappings":"AASA,MAAM,gCAAiC,SAAQ,KAAK;CAAG;AAEvD,MAAM,8BAA8B,GAAG;IACtC,MAAM;IACN,OAAO;IACP,aAAa;IACb,OAAO;IACP,OAAO;IACP,mBAAmB;IACnB,kBAAkB;IAClB,cAAc;IACd,uBAAuB;IACvB,eAAe;IACf,UAAU;IACV,aAAa;IACb,KAAK;IACL,IAAI;IACJ,MAAM;IACN,MAAM;CACG,CAAC;AAQX,MAAM,CAAC,MAAM,oCAAoC,GAA4B,MAAM,CAAC,MAAM,CAAC;IAC1F,mBAAmB,EAAE,MAAM,CAAC,MAAM,CAAC;QAClC,SAAS;QACT,SAAS;QACT,kBAAkB;QAClB,kBAAkB;QAClB,YAAY;QACZ,aAAa;QACb,aAAa;QACb,aAAa;QACb,eAAe;QACf,eAAe;QACf,UAAU;KACV,CAAC;IACF,WAAW,EAAE,CAAC;IACd,sBAAsB,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;CACpI,CAAC,CAAC;AAEH,SAAS,kBAAkB,CAAC,KAAc;IACzC,OAAO,OAAO,KAAK,KAAK,QAAQ,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0BAA0B,CAAC,CAAC;QACpF,IAAI,OAAO,EAAE,CAAC;YACb,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;QAClD,CAAC;IACF,CAAC;IACD,OAAO,UAAU,CAAC;AACnB,CAAC","sourcesContent":["import type { Tool } from \"../types.ts\";\n\ninterface JsonSchemaObject {\n\t[key: string]: unknown;\n\ttype?: unknown;\n\tproperties?: Record<string, JsonSchemaObject | undefined>;\n\trequired?: unknown;\n}\n\nclass UnsupportedStrictJsonSchemaError extends Error {}\n\nconst UNSUPPORTED_STRICT_SCHEMA_KEYS = [\n\t\"$ref\",\n\t\"$defs\",\n\t\"definitions\",\n\t\"allOf\",\n\t\"oneOf\",\n\t\"patternProperties\",\n\t\"dependentSchemas\",\n\t\"dependencies\",\n\t\"unevaluatedProperties\",\n\t\"propertyNames\",\n\t\"contains\",\n\t\"prefixItems\",\n\t\"not\",\n\t\"if\",\n\t\"then\",\n\t\"else\",\n] as const;\n\nexport interface StrictJsonSchemaProfile {\n\treadonly unsupportedKeywords: readonly string[];\n\treadonly maxMinItems?: number;\n\treadonly supportedStringFormats?: readonly string[];\n}\n\nexport const ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE: StrictJsonSchemaProfile = Object.freeze({\n\tunsupportedKeywords: Object.freeze([\n\t\t\"minimum\",\n\t\t\"maximum\",\n\t\t\"exclusiveMinimum\",\n\t\t\"exclusiveMaximum\",\n\t\t\"multipleOf\",\n\t\t\"uniqueItems\",\n\t\t\"minContains\",\n\t\t\"maxContains\",\n\t\t\"minProperties\",\n\t\t\"maxProperties\",\n\t\t\"maxItems\",\n\t]),\n\tmaxMinItems: 1,\n\tsupportedStringFormats: Object.freeze([\"date-time\", \"time\", \"date\", \"duration\", \"email\", \"hostname\", \"uri\", \"ipv4\", \"ipv6\", \"uuid\"]),\n});\n\nfunction isJsonSchemaObject(value: unknown): value is JsonSchemaObject {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction isStructuredSchema(schema: unknown): boolean {\n\tif (!isJsonSchemaObject(schema)) return false;\n\tconst types = typeof schema.type === \"string\" ? [schema.type] : Array.isArray(schema.type) ? schema.type : [];\n\treturn (\n\t\ttypes.includes(\"object\") ||\n\t\ttypes.includes(\"array\") ||\n\t\tschema.properties !== undefined ||\n\t\tschema.items !== undefined\n\t);\n}\n\nfunction schemaAllowsNull(schema: unknown): boolean {\n\tif (!isJsonSchemaObject(schema)) return false;\n\tif (schema.type === \"null\" || (Array.isArray(schema.type) && schema.type.includes(\"null\"))) return true;\n\tif (schema.const === null || (Array.isArray(schema.enum) && schema.enum.includes(null))) return true;\n\treturn Array.isArray(schema.anyOf) && schema.anyOf.some((variant) => schemaAllowsNull(variant));\n}\n\nfunction makeJsonSchemaNodeStrict(schema: unknown, profile: StrictJsonSchemaProfile | undefined): void {\n\tif (!isJsonSchemaObject(schema)) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"boolean schemas are unsupported\");\n\t}\n\tfor (const key of [...UNSUPPORTED_STRICT_SCHEMA_KEYS, ...(profile?.unsupportedKeywords ?? [])]) {\n\t\tif (schema[key] !== undefined) {\n\t\t\tthrow new UnsupportedStrictJsonSchemaError(`${key} schemas are unsupported`);\n\t\t}\n\t}\n\tif (\n\t\tprofile?.maxMinItems !== undefined &&\n\t\tschema.minItems !== undefined &&\n\t\t(typeof schema.minItems !== \"number\" || !Number.isInteger(schema.minItems) || schema.minItems < 0 || schema.minItems > profile.maxMinItems)\n\t) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(`minItems must be an integer from 0 to ${profile.maxMinItems}`);\n\t}\n\tif (profile?.supportedStringFormats && schema.format !== undefined &&\n\t\t(typeof schema.format !== \"string\" || !profile.supportedStringFormats.includes(schema.format))) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(`format: ${JSON.stringify(schema.format)} is unsupported`);\n\t}\n\n\tif (schema.anyOf !== undefined) {\n\t\tif (!Array.isArray(schema.anyOf) || schema.anyOf.length === 0) {\n\t\t\tthrow new UnsupportedStrictJsonSchemaError(\"anyOf must contain at least one schema\");\n\t\t}\n\t\tfor (const variant of schema.anyOf) {\n\t\t\tif (isStructuredSchema(variant)) {\n\t\t\t\tthrow new UnsupportedStrictJsonSchemaError(\"object and array unions are unsupported\");\n\t\t\t}\n\t\t\tmakeJsonSchemaNodeStrict(variant, profile);\n\t\t}\n\t}\n\n\tif (schema.items !== undefined) {\n\t\tif (Array.isArray(schema.items)) {\n\t\t\tthrow new UnsupportedStrictJsonSchemaError(\"tuple schemas are unsupported\");\n\t\t}\n\t\tmakeJsonSchemaNodeStrict(schema.items, profile);\n\t}\n\n\tconst isObjectSchema = schema.type === \"object\";\n\tif (schema.properties !== undefined && !isObjectSchema) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"properties require type object\");\n\t}\n\tif (!isObjectSchema) return;\n\tif (schema.additionalProperties !== undefined && schema.additionalProperties !== false) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"schema-valued or true additionalProperties is unsupported\");\n\t}\n\tif (schema.properties !== undefined && !isJsonSchemaObject(schema.properties)) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"object properties must be a schema map\");\n\t}\n\tif (\n\t\tschema.required !== undefined &&\n\t\t(!Array.isArray(schema.required) || schema.required.some((key) => typeof key !== \"string\"))\n\t) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"object required must be a string array\");\n\t}\n\n\tconst properties = schema.properties ?? {};\n\tconst propertyNames = Object.keys(properties);\n\tconst required = new Set(Array.isArray(schema.required) ? schema.required : []);\n\tif ([...required].some((key) => !propertyNames.includes(key))) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"required contains an unknown property\");\n\t}\n\tfor (const [key, property] of Object.entries(properties)) {\n\t\tmakeJsonSchemaNodeStrict(property, profile);\n\t\tif (!required.has(key) && !schemaAllowsNull(property)) {\n\t\t\tproperties[key] = { anyOf: [property, { type: \"null\" }] };\n\t\t}\n\t}\n\tschema.required = propertyNames;\n\tschema.additionalProperties = false;\n}\n\n/** Convert a tool schema to the strict subset expected by provider constrained sampling. */\nexport function makeStrictJsonSchema(\n\tschema: Tool[\"parameters\"],\n\tprofile?: StrictJsonSchemaProfile,\n): Record<string, unknown> {\n\tconst cloned: unknown = structuredClone(schema);\n\tif (!isJsonSchemaObject(cloned)) {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"root schema must have type object\");\n\t}\n\tmakeJsonSchemaNodeStrict(cloned, profile);\n\tif (cloned.type !== \"object\") {\n\t\tthrow new UnsupportedStrictJsonSchemaError(\"root schema must have type object\");\n\t}\n\treturn cloned;\n}\n\n/** Returns true for a tool that asks for strict JSON-schema sampling only when possible (`strict: \"prefer\"`). */\nexport function prefersJsonSchemaStrictSampling(tool: Tool): boolean {\n\tconst config = tool.constrainedSampling;\n\treturn !!config && config.type === \"json_schema\" && config.strict === \"prefer\";\n}\n\nexport function getJsonSchemaToolParameters(\n\ttool: Tool,\n\tstrict: boolean | undefined,\n\tprofile?: StrictJsonSchemaProfile,\n): Tool[\"parameters\"] {\n\treturn (strict === true ? makeStrictJsonSchema(tool.parameters, profile) : tool.parameters) as Tool[\"parameters\"];\n}\n\nexport interface GrammarConstrainedSampling {\n\tformat: \"lark\" | \"regex\";\n\tdefinition: string;\n\tinputProperty: string;\n}\n\nexport interface GrammarToolInputJsonBuffer {\n\tinput: string;\n\tstarted: boolean;\n\tclosed: boolean;\n}\n\nexport function getGrammarToolInput(\n\ttoolName: string,\n\targuments_: Record<string, unknown>,\n\tinputProperty: string,\n): string {\n\tconst input = arguments_[inputProperty];\n\tif (typeof input !== \"string\") {\n\t\tthrow new Error(`Grammar tool call \"${toolName}\" requires argument \"${inputProperty}\" to be a string.`);\n\t}\n\treturn input;\n}\n\n/**\n * Append `inputDelta` to a streaming grammar tool input and return the matching piece of its JSON arguments text, or\n * `undefined` for an empty delta. Costs time linear in the delta, never in the input so far.\n */\nexport function appendGrammarToolInputJsonDelta(\n\tbuffer: GrammarToolInputJsonBuffer,\n\tinputProperty: string,\n\tinputDelta: string,\n): string | undefined {\n\tif (buffer.closed) {\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed after it was closed`);\n\t}\n\tif (inputDelta.length === 0) return undefined;\n\tbuffer.input += inputDelta;\n\treturn grammarToolInputJsonStart(buffer, inputProperty) + JSON.stringify(inputDelta).slice(1, -1);\n}\n\n/**\n * Close a streaming grammar tool input and return the rest of its JSON arguments text. `finalInput` is the complete\n * input when the provider sends one; it must continue the input streamed so far. Closing again with the same input\n * returns `undefined`.\n */\nexport function closeGrammarToolInputJson(\n\tbuffer: GrammarToolInputJsonBuffer,\n\tinputProperty: string,\n\tfinalInput: string | undefined,\n): string | undefined {\n\tif (buffer.closed) {\n\t\tif (finalInput === undefined || finalInput === buffer.input) return undefined;\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed after it was closed`);\n\t}\n\tif (finalInput !== undefined && !finalInput.startsWith(buffer.input)) {\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed non-monotonically`);\n\t}\n\tconst rest = finalInput === undefined ? \"\" : finalInput.slice(buffer.input.length);\n\tif (finalInput !== undefined) buffer.input = finalInput;\n\tbuffer.closed = true;\n\treturn `${grammarToolInputJsonStart(buffer, inputProperty)}${JSON.stringify(rest).slice(1, -1)}\"}`;\n}\n\n/** The opening of the JSON arguments text, once. */\nfunction grammarToolInputJsonStart(buffer: GrammarToolInputJsonBuffer, inputProperty: string): string {\n\tif (buffer.started) return \"\";\n\tbuffer.started = true;\n\treturn `{${JSON.stringify(inputProperty)}:\"`;\n}\n\nfunction inferGrammarInputProperty(tool: Tool): string {\n\tconst schema = tool.parameters as JsonSchemaObject;\n\tif (schema.type !== \"object\") {\n\t\tthrow new Error(\"grammar constrained sampling requires an object parameter schema\");\n\t}\n\tif (!Array.isArray(schema.required) || schema.required.length !== 1 || typeof schema.required[0] !== \"string\") {\n\t\tthrow new Error(\"grammar constrained sampling requires exactly one required string property\");\n\t}\n\n\tconst inputProperty = schema.required[0];\n\tif (!schema.properties?.[inputProperty]) {\n\t\tthrow new Error(`grammar constrained sampling requires a properties entry for ${inputProperty}`);\n\t}\n\tif (schema.properties[inputProperty]?.type !== \"string\") {\n\t\tthrow new Error(`grammar constrained sampling property ${inputProperty} must have type string`);\n\t}\n\treturn inputProperty;\n}\n\nexport function resolveJsonSchemaStrictSampling(\n\ttool: Tool,\n\tsupportsStrictMode: boolean,\n\tprofile?: StrictJsonSchemaProfile,\n): boolean | undefined {\n\tconst config = tool.constrainedSampling;\n\tif (!config || config.type !== \"json_schema\") return undefined;\n\n\tif (supportsStrictMode) {\n\t\ttry {\n\t\t\tmakeStrictJsonSchema(tool.parameters, profile);\n\t\t\treturn true;\n\t\t} catch (error) {\n\t\t\tif (!(error instanceof UnsupportedStrictJsonSchemaError)) throw error;\n\t\t\tif (config.strict !== \"require\") return undefined;\n\t\t\tthrow new Error(`Tool \"${tool.name}\" requires JSON-schema constrained sampling, but ${error.message}.`);\n\t\t}\n\t}\n\tif (config.strict === \"require\") {\n\t\tthrow new Error(\n\t\t\t`Tool \"${tool.name}\" requires JSON-schema constrained sampling, but strict tools are unsupported.`,\n\t\t);\n\t}\n\treturn undefined;\n}\n\nexport function resolveGrammarConstrainedSampling(\n\ttool: Tool,\n\tsupportsOpenAIGrammarTools: boolean,\n): GrammarConstrainedSampling | undefined {\n\tconst config = tool.constrainedSampling;\n\tif (!config || config.type !== \"grammar\") {\n\t\treturn undefined;\n\t}\n\n\tif (!supportsOpenAIGrammarTools) {\n\t\treturn undefined;\n\t}\n\n\tconst larkDefinition = config.variants.openai_lark;\n\tconst regexDefinition = config.variants.openai_regex;\n\tconst hasLarkDefinition = typeof larkDefinition === \"string\" && larkDefinition.trim().length > 0;\n\tconst hasRegexDefinition = typeof regexDefinition === \"string\" && regexDefinition.trim().length > 0;\n\tif (!hasLarkDefinition && !hasRegexDefinition) {\n\t\tthrow new Error(\n\t\t\t`Tool \"${tool.name}\" cannot use grammar constrained sampling: no supported grammar variant was provided.`,\n\t\t);\n\t}\n\n\ttry {\n\t\treturn {\n\t\t\tformat: hasLarkDefinition ? \"lark\" : \"regex\",\n\t\t\tdefinition: hasLarkDefinition ? larkDefinition : regexDefinition!,\n\t\t\tinputProperty: inferGrammarInputProperty(tool),\n\t\t};\n\t} catch (error) {\n\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\tthrow new Error(`Tool \"${tool.name}\" cannot use grammar constrained sampling: ${message}.`);\n\t}\n}\n\nexport function createGrammarToolInputProperties(\n\ttools: Tool[] | undefined,\n\tsupportsOpenAIGrammarTools: boolean,\n): ReadonlyMap<string, string> {\n\tconst properties = new Map<string, string>();\n\tfor (const tool of tools ?? []) {\n\t\tconst grammar = resolveGrammarConstrainedSampling(tool, supportsOpenAIGrammarTools);\n\t\tif (grammar) {\n\t\t\tproperties.set(tool.name, grammar.inputProperty);\n\t\t}\n\t}\n\treturn properties;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-completions.d.ts","sourceRoot":"","sources":["../../src/api/openai-completions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAOX,0BAA0B,EAI1B,MAAM,sCAAsC,CAAC;
|
|
1
|
+
{"version":3,"file":"openai-completions.d.ts","sourceRoot":"","sources":["../../src/api/openai-completions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,EAOX,0BAA0B,EAI1B,MAAM,sCAAsC,CAAC;AAI9C,OAAO,KAAK,EAOX,KAAK,EACL,uBAAuB,EAGvB,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAEb,eAAe,EAOf,MAAM,aAAa,CAAC;AAoBrB,OAAO,EAIN,KAAK,iBAAiB,EACtB,MAAM,wBAAwB,CAAC;AA2GhC,MAAM,WAAW,wBAAyB,SAAQ,aAAa;IAC9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c,CAAC,oBAAoB,EAAE,mBAAmB,CAmBlF,CAAC;AAmhBF,wBAAgB,eAAe,CAC9B,KAAK,EAAE,KAAK,CAAC,oBAAoB,CAAC,EAClC,OAAO,EAAE,iBAAiB,EAC1B,MAAM,EAAE,+BAA+B,EACvC,OAAO,CAAC,EAAE,iCAAiC,GACzC,0BAA0B,EAAE,CAkS9B"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { calculateCost, clampThinkingLevel } from "../models.js";
|
|
2
2
|
import { githubCopilotEmptyResponse, recordGitHubCopilotFailure } from "../providers/github-copilot-errors.js";
|
|
3
|
+
import { GitHubCopilotStreamAttempt } from "../providers/github-copilot-stream-attempt.js";
|
|
3
4
|
import { formatProviderError, normalizeProviderError } from "../utils/error-body.js";
|
|
4
5
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
5
6
|
import { shortHash } from "../utils/hash.js";
|
|
@@ -210,7 +211,7 @@ export const stream = (model, context, options) => {
|
|
|
210
211
|
}
|
|
211
212
|
};
|
|
212
213
|
const streamDeadline = createStreamDeadline(options?.streamDeadlineMs, options?.signal);
|
|
213
|
-
|
|
214
|
+
const streamAttempt = new GitHubCopilotStreamAttempt();
|
|
214
215
|
try {
|
|
215
216
|
const apiKey = getClientApiKey(model.provider, options?.apiKey, options?.headers);
|
|
216
217
|
const compat = getCompat(model);
|
|
@@ -232,14 +233,17 @@ export const stream = (model, context, options) => {
|
|
|
232
233
|
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
|
|
233
234
|
maxRetries: 0,
|
|
234
235
|
};
|
|
235
|
-
const { data: openaiStream, response } = await retryProviderRequest(() =>
|
|
236
|
+
const { data: openaiStream, response } = await retryProviderRequest(() => {
|
|
237
|
+
streamAttempt.begin();
|
|
238
|
+
return client.chat.completions.create(params, requestOptions).withResponse();
|
|
239
|
+
}, {
|
|
236
240
|
maxRetries: options?.maxRetries,
|
|
237
241
|
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
238
242
|
signal: streamDeadline.signal,
|
|
239
243
|
});
|
|
240
244
|
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
241
245
|
stream.push({ type: "start", partial: output });
|
|
242
|
-
|
|
246
|
+
streamAttempt.respond(response.headers);
|
|
243
247
|
let textBlock = null;
|
|
244
248
|
let thinkingBlock = null;
|
|
245
249
|
let opaqueReasoning = null;
|
|
@@ -388,7 +392,7 @@ export const stream = (model, context, options) => {
|
|
|
388
392
|
}
|
|
389
393
|
return { block, state };
|
|
390
394
|
};
|
|
391
|
-
for await (const chunk of withStreamDeadline(openaiStream, streamDeadline.deadlineMs, streamDeadline.abort)) {
|
|
395
|
+
for await (const chunk of streamAttempt.observe(withStreamDeadline(openaiStream, streamDeadline.deadlineMs, streamDeadline.abort))) {
|
|
392
396
|
await options?.onProviderStreamEvent?.(chunk, model);
|
|
393
397
|
if (!chunk || typeof chunk !== "object")
|
|
394
398
|
continue;
|
|
@@ -552,7 +556,7 @@ export const stream = (model, context, options) => {
|
|
|
552
556
|
output.errorMessage += `\n${rawMetadata}`;
|
|
553
557
|
}
|
|
554
558
|
if (model.provider === "github-copilot")
|
|
555
|
-
recordGitHubCopilotFailure(output, error,
|
|
559
|
+
recordGitHubCopilotFailure(output, error, streamAttempt.snapshot());
|
|
556
560
|
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
557
561
|
stream.end();
|
|
558
562
|
}
|