{"version":3,"file":"constrained-sampling.d.ts","sourceRoot":"","sources":["../../src/api/constrained-sampling.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAQxC,MAAM,WAAW,0BAA0B;IAC1C,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,0BAA0B;IAC1C,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;CAChB;AAED,wBAAgB,mBAAmB,CAClC,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,aAAa,EAAE,MAAM,GACnB,MAAM,CAMR;AAED,wBAAgB,+BAA+B,CAC9C,MAAM,EAAE,0BAA0B,EAClC,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,GACZ,MAAM,GAAG,SAAS,CAyBpB;AAqBD,wBAAgB,+BAA+B,CAAC,IAAI,EAAE,IAAI,EAAE,kBAAkB,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAe5G;AAED,wBAAgB,iCAAiC,CAChD,IAAI,EAAE,IAAI,EACV,0BAA0B,EAAE,OAAO,GACjC,0BAA0B,GAAG,SAAS,CA8BxC;AAED,wBAAgB,gCAAgC,CAC/C,KAAK,EAAE,IAAI,EAAE,GAAG,SAAS,EACzB,0BAA0B,EAAE,OAAO,GACjC,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAS7B","sourcesContent":["import type { Tool } from \"../types.ts\";\n\ninterface JsonSchemaObject {\n\ttype?: unknown;\n\tproperties?: Record<string, JsonSchemaObject | undefined>;\n\trequired?: unknown;\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\nexport function appendGrammarToolInputJsonDelta(\n\tbuffer: GrammarToolInputJsonBuffer,\n\tinputProperty: string,\n\tnextInput: string,\n\tclose: boolean,\n): string | undefined {\n\tif (buffer.closed) {\n\t\tif (close && nextInput === buffer.input) return undefined;\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed after it was closed`);\n\t}\n\tif (!nextInput.startsWith(buffer.input)) {\n\t\tthrow new Error(`grammar tool input for property \"${inputProperty}\" changed non-monotonically`);\n\t}\n\n\tconst inputDelta = nextInput.slice(buffer.input.length);\n\tif (!close && inputDelta.length === 0) return undefined;\n\n\tlet delta = \"\";\n\tif (!buffer.started) {\n\t\tdelta += `{${JSON.stringify(inputProperty)}:\"`;\n\t\tbuffer.started = true;\n\t}\n\tdelta += JSON.stringify(inputDelta).slice(1, -1);\n\tbuffer.input = nextInput;\n\n\tif (close) {\n\t\tdelta += '\"}';\n\t\tbuffer.closed = true;\n\t}\n\treturn delta;\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(tool: Tool, supportsStrictMode: boolean): boolean | undefined {\n\tconst config = tool.constrainedSampling;\n\tif (!config || config.type !== \"json_schema\") {\n\t\treturn undefined;\n\t}\n\n\tif (supportsStrictMode) {\n\t\treturn true;\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"]}