@langchain/core 1.1.35 → 1.1.37
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 +26 -0
- package/dist/language_models/base.cjs +1 -1
- package/dist/language_models/base.js +1 -1
- package/dist/language_models/chat_models.cjs +6 -21
- package/dist/language_models/chat_models.cjs.map +1 -1
- package/dist/language_models/chat_models.d.cts.map +1 -1
- package/dist/language_models/chat_models.d.ts.map +1 -1
- package/dist/language_models/chat_models.js +7 -22
- package/dist/language_models/chat_models.js.map +1 -1
- package/dist/language_models/llms.cjs +6 -22
- package/dist/language_models/llms.cjs.map +1 -1
- package/dist/language_models/llms.d.cts.map +1 -1
- package/dist/language_models/llms.d.ts.map +1 -1
- package/dist/language_models/llms.js +6 -22
- package/dist/language_models/llms.js.map +1 -1
- package/dist/language_models/utils.cjs +0 -4
- package/dist/language_models/utils.cjs.map +1 -1
- package/dist/language_models/utils.js +1 -4
- package/dist/language_models/utils.js.map +1 -1
- package/dist/messages/base.cjs +55 -15
- package/dist/messages/base.cjs.map +1 -1
- package/dist/messages/base.d.cts.map +1 -1
- package/dist/messages/base.d.ts.map +1 -1
- package/dist/messages/base.js +55 -15
- package/dist/messages/base.js.map +1 -1
- package/dist/messages/block_translators/openai.cjs +6 -1
- package/dist/messages/block_translators/openai.cjs.map +1 -1
- package/dist/messages/block_translators/openai.js +6 -1
- package/dist/messages/block_translators/openai.js.map +1 -1
- package/dist/messages/system.cjs +8 -4
- package/dist/messages/system.cjs.map +1 -1
- package/dist/messages/system.d.cts.map +1 -1
- package/dist/messages/system.d.ts.map +1 -1
- package/dist/messages/system.js +8 -4
- package/dist/messages/system.js.map +1 -1
- package/dist/utils/json_schema.cjs +31 -7
- package/dist/utils/json_schema.cjs.map +1 -1
- package/dist/utils/json_schema.d.cts +1 -0
- package/dist/utils/json_schema.d.cts.map +1 -1
- package/dist/utils/json_schema.d.ts +1 -0
- package/dist/utils/json_schema.d.ts.map +1 -1
- package/dist/utils/json_schema.js +31 -7
- package/dist/utils/json_schema.js.map +1 -1
- package/package.json +1 -1
|
@@ -14,20 +14,44 @@ var json_schema_exports = /* @__PURE__ */ require_runtime.__exportAll({
|
|
|
14
14
|
validatesOnlyStrings: () => validatesOnlyStrings
|
|
15
15
|
});
|
|
16
16
|
/**
|
|
17
|
+
* WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.
|
|
18
|
+
*
|
|
19
|
+
* Keyed on the schema object reference. Since Zod schemas are immutable and
|
|
20
|
+
* the same `tool.schema` reference is passed on every LLM call, this
|
|
21
|
+
* eliminates redundant serializations. For example, an agent with 6 tools
|
|
22
|
+
* doing 15 steps across 3 parallel subagents would otherwise run 270
|
|
23
|
+
* identical conversions per invocation.
|
|
24
|
+
*
|
|
25
|
+
* Only used when no custom `params` are passed (the common case for tool
|
|
26
|
+
* binding). WeakMap ensures cached entries are GC'd when the schema goes
|
|
27
|
+
* out of scope.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
const _jsonSchemaCache = /* @__PURE__ */ new WeakMap();
|
|
32
|
+
/**
|
|
17
33
|
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
34
|
+
* Results are cached by schema reference when no custom params are passed.
|
|
18
35
|
* @param schema - The schema to convert.
|
|
19
36
|
* @param params - The parameters to pass to the toJSONSchema function.
|
|
20
37
|
* @returns The converted schema.
|
|
21
38
|
*/
|
|
22
39
|
function toJsonSchema(schema, params) {
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
27
|
-
else return (0, zod_v4_core.toJSONSchema)(schema, params);
|
|
40
|
+
const canCache = !params && schema != null && typeof schema === "object";
|
|
41
|
+
if (canCache) {
|
|
42
|
+
const cached = _jsonSchemaCache.get(schema);
|
|
43
|
+
if (cached) return cached;
|
|
28
44
|
}
|
|
29
|
-
|
|
30
|
-
|
|
45
|
+
let result;
|
|
46
|
+
if (require_utils_standard_schema.isStandardJsonSchema(schema) && !require_zod.isZodSchemaV4(schema)) result = schema["~standard"].jsonSchema.input({ target: "draft-07" });
|
|
47
|
+
else if (require_zod.isZodSchemaV4(schema)) {
|
|
48
|
+
const inputSchema = require_zod.interopZodTransformInputSchema(schema, true);
|
|
49
|
+
if (require_zod.isZodObjectV4(inputSchema)) result = (0, zod_v4_core.toJSONSchema)(require_zod.interopZodObjectStrict(inputSchema, true), params);
|
|
50
|
+
else result = (0, zod_v4_core.toJSONSchema)(schema, params);
|
|
51
|
+
} else if (require_zod.isZodSchemaV3(schema)) result = require_zodToJsonSchema.zodToJsonSchema(schema);
|
|
52
|
+
else result = schema;
|
|
53
|
+
if (canCache && result != null && typeof result === "object") _jsonSchemaCache.set(schema, result);
|
|
54
|
+
return result;
|
|
31
55
|
}
|
|
32
56
|
/**
|
|
33
57
|
* Validates if a JSON schema validates only strings. May return false negatives in some edge cases
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.cjs","names":["isStandardJsonSchema","isZodSchemaV4","interopZodTransformInputSchema","isZodObjectV4","interopZodObjectStrict","isZodSchemaV3","zodToJsonSchema"],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n
|
|
1
|
+
{"version":3,"file":"json_schema.cjs","names":["isStandardJsonSchema","isZodSchemaV4","interopZodTransformInputSchema","isZodObjectV4","interopZodObjectStrict","isZodSchemaV3","zodToJsonSchema"],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.\n *\n * Keyed on the schema object reference. Since Zod schemas are immutable and\n * the same `tool.schema` reference is passed on every LLM call, this\n * eliminates redundant serializations. For example, an agent with 6 tools\n * doing 15 steps across 3 parallel subagents would otherwise run 270\n * identical conversions per invocation.\n *\n * Only used when no custom `params` are passed (the common case for tool\n * binding). WeakMap ensures cached entries are GC'd when the schema goes\n * out of scope.\n *\n * @internal\n */\nconst _jsonSchemaCache = new WeakMap<object, JSONSchema>();\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * Results are cached by schema reference when no custom params are passed.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n // Cache when no custom params are passed (the default for tool binding).\n // Params can change the output (e.g., different target draft), so we\n // bypass the cache when they're provided.\n const canCache = !params && schema != null && typeof schema === \"object\";\n\n if (canCache) {\n const cached = _jsonSchemaCache.get(schema);\n if (cached) return cached;\n }\n\n let result: JSONSchema;\n\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n result = schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n } else if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(inputSchema, true);\n result = toJSONSchema(strictSchema as unknown as $ZodType, params);\n } else {\n result = toJSONSchema(schema as unknown as $ZodType, params);\n }\n } else if (isZodSchemaV3(schema)) {\n result = zodToJsonSchema(schema as never);\n } else {\n result = schema as JSONSchema;\n }\n\n if (canCache && result != null && typeof result === \"object\") {\n _jsonSchemaCache.set(schema, result);\n }\n\n return result;\n}\n\n/**\n * Validates if a JSON schema validates only strings. May return false negatives in some edge cases\n * (like recursive or unresolvable refs).\n *\n * @param schema - The schema to validate.\n * @returns `true` if the schema validates only strings, `false` otherwise.\n */\nexport function validatesOnlyStrings(schema: unknown): boolean {\n // Null, undefined, or empty schema\n if (\n !schema ||\n typeof schema !== \"object\" ||\n Object.keys(schema).length === 0 ||\n Array.isArray(schema)\n ) {\n return false; // Validates anything, not just strings\n }\n\n // Explicit type constraint\n if (\"type\" in schema) {\n if (typeof schema.type === \"string\") {\n return schema.type === \"string\";\n }\n\n if (Array.isArray(schema.type)) {\n // not sure why someone would do `\"type\": [\"string\"]` or especially `\"type\": [\"string\",\n // \"string\", \"string\", ...]` but we're not here to judge\n return schema.type.every((t) => t === \"string\");\n }\n return false; // Invalid or non-string type\n }\n\n // Enum with only string values\n if (\"enum\" in schema) {\n return (\n Array.isArray(schema.enum) &&\n schema.enum.length > 0 &&\n schema.enum.every((val) => typeof val === \"string\")\n );\n }\n\n // String constant\n if (\"const\" in schema) {\n return typeof schema.const === \"string\";\n }\n\n // Schema combinations\n if (\"allOf\" in schema && Array.isArray(schema.allOf)) {\n // If any subschema validates only strings, then the overall schema validates only strings\n return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));\n }\n\n if (\n (\"anyOf\" in schema && Array.isArray(schema.anyOf)) ||\n (\"oneOf\" in schema && Array.isArray(schema.oneOf))\n ) {\n const subschemas = (\n \"anyOf\" in schema ? schema.anyOf : schema.oneOf\n ) as unknown[];\n\n // All subschemas must validate only strings\n return (\n subschemas.length > 0 &&\n subschemas.every((subschema) => validatesOnlyStrings(subschema))\n );\n }\n\n // We're not going to try on this one, it's too complex - we just assume if it has a \"not\" key and hasn't matched one of the above checks, it's not a string schema.\n if (\"not\" in schema) {\n return false; // The not case can validate non-strings\n }\n\n if (\"$ref\" in schema && typeof schema.$ref === \"string\") {\n const ref = schema.$ref as string;\n const resolved = dereference(schema as Schema);\n if (resolved[ref]) {\n return validatesOnlyStrings(resolved[ref]);\n }\n return false;\n }\n\n // ignore recursive refs and other cases where type is omitted for now\n // ignore other cases for now where type is omitted\n\n return false;\n}\n\n// Re-export of the types used throughout langchain for json schema serialization.\n// The plan is to eventually nix zod-to-json-schema altogether in place for\n// zod v4 / a more standardized way of serializing validated inputs, so its re-exported\n// here to remove the dependency on zod-to-json-schema in downstream packages until\n// a determination is made.\n\nexport {\n type JsonSchema7Type,\n type JsonSchema7Type as JSONSchema,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"./zod-to-json-schema/index.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,MAAM,mCAAmB,IAAI,SAA6B;;;;;;;;AAS1D,SAAg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AM,QAAQ,OAAO,MAAM,CAElD,QAAO,OAAO,MAAM,MAAM,cAAc,qBAAqB,UAAU,CAAC;AAG1E,KACG,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,IAChD,WAAW,UAAU,MAAM,QAAQ,OAAO,MAAM,EACjD;EACA,MAAM,aACJ,WAAW,SAAS,OAAO,QAAQ,OAAO;AAI5C,SACE,WAAW,SAAS,KACpB,WAAW,OAAO,cAAc,qBAAqB,UAAU,CAAC;;AAKpE,KAAI,SAAS,OACX,QAAO;AAGT,KAAI,UAAU,UAAU,OAAO,OAAO,SAAS,UAAU;EACvD,MAAM,MAAM,OAAO;EACnB,MAAM,YAAA,GAAA,sBAAA,aAAuB,OAAiB;AAC9C,MAAI,SAAS,KACX,QAAO,qBAAqB,SAAS,KAAK;AAE5C,SAAO;;AAMT,QAAO"}
|
|
@@ -13,6 +13,7 @@ import { Validator, deepCompareStrict } from "@cfworker/json-schema";
|
|
|
13
13
|
type ToJSONSchemaParams = NonNullable<Parameters<typeof toJSONSchema>[1]>;
|
|
14
14
|
/**
|
|
15
15
|
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
16
|
+
* Results are cached by schema reference when no custom params are passed.
|
|
16
17
|
* @param schema - The schema to convert.
|
|
17
18
|
* @param params - The parameters to pass to the toJSONSchema function.
|
|
18
19
|
* @returns The converted schema.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.d.cts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;
|
|
1
|
+
{"version":3,"file":"json_schema.d.cts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;AADpB;iBA4BgB,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;;;;;;;;iBA6Ca,oBAAA,CAAqB,MAAA"}
|
|
@@ -13,6 +13,7 @@ import { StandardJSONSchemaV1 } from "@standard-schema/spec";
|
|
|
13
13
|
type ToJSONSchemaParams = NonNullable<Parameters<typeof toJSONSchema>[1]>;
|
|
14
14
|
/**
|
|
15
15
|
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
16
|
+
* Results are cached by schema reference when no custom params are passed.
|
|
16
17
|
* @param schema - The schema to convert.
|
|
17
18
|
* @param params - The parameters to pass to the toJSONSchema function.
|
|
18
19
|
* @returns The converted schema.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.d.ts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA
|
|
1
|
+
{"version":3,"file":"json_schema.d.ts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAmBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;;iBA2BJ,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;;;;;;;;iBA6Ca,oBAAA,CAAqB,MAAA"}
|
|
@@ -13,20 +13,44 @@ var json_schema_exports = /* @__PURE__ */ __exportAll({
|
|
|
13
13
|
validatesOnlyStrings: () => validatesOnlyStrings
|
|
14
14
|
});
|
|
15
15
|
/**
|
|
16
|
+
* WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.
|
|
17
|
+
*
|
|
18
|
+
* Keyed on the schema object reference. Since Zod schemas are immutable and
|
|
19
|
+
* the same `tool.schema` reference is passed on every LLM call, this
|
|
20
|
+
* eliminates redundant serializations. For example, an agent with 6 tools
|
|
21
|
+
* doing 15 steps across 3 parallel subagents would otherwise run 270
|
|
22
|
+
* identical conversions per invocation.
|
|
23
|
+
*
|
|
24
|
+
* Only used when no custom `params` are passed (the common case for tool
|
|
25
|
+
* binding). WeakMap ensures cached entries are GC'd when the schema goes
|
|
26
|
+
* out of scope.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
const _jsonSchemaCache = /* @__PURE__ */ new WeakMap();
|
|
31
|
+
/**
|
|
16
32
|
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
33
|
+
* Results are cached by schema reference when no custom params are passed.
|
|
17
34
|
* @param schema - The schema to convert.
|
|
18
35
|
* @param params - The parameters to pass to the toJSONSchema function.
|
|
19
36
|
* @returns The converted schema.
|
|
20
37
|
*/
|
|
21
38
|
function toJsonSchema(schema, params) {
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
const
|
|
25
|
-
if (
|
|
26
|
-
else return toJSONSchema(schema, params);
|
|
39
|
+
const canCache = !params && schema != null && typeof schema === "object";
|
|
40
|
+
if (canCache) {
|
|
41
|
+
const cached = _jsonSchemaCache.get(schema);
|
|
42
|
+
if (cached) return cached;
|
|
27
43
|
}
|
|
28
|
-
|
|
29
|
-
|
|
44
|
+
let result;
|
|
45
|
+
if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) result = schema["~standard"].jsonSchema.input({ target: "draft-07" });
|
|
46
|
+
else if (isZodSchemaV4(schema)) {
|
|
47
|
+
const inputSchema = interopZodTransformInputSchema(schema, true);
|
|
48
|
+
if (isZodObjectV4(inputSchema)) result = toJSONSchema(interopZodObjectStrict(inputSchema, true), params);
|
|
49
|
+
else result = toJSONSchema(schema, params);
|
|
50
|
+
} else if (isZodSchemaV3(schema)) result = zodToJsonSchema(schema);
|
|
51
|
+
else result = schema;
|
|
52
|
+
if (canCache && result != null && typeof result === "object") _jsonSchemaCache.set(schema, result);
|
|
53
|
+
return result;
|
|
30
54
|
}
|
|
31
55
|
/**
|
|
32
56
|
* Validates if a JSON schema validates only strings. May return false negatives in some edge cases
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.js","names":[],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n
|
|
1
|
+
{"version":3,"file":"json_schema.js","names":[],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema, type $ZodType } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.js\";\nimport { StandardJSONSchemaV1 } from \"@standard-schema/spec\";\nimport { isStandardJsonSchema } from \"./standard_schema.js\";\n\nexport { deepCompareStrict, Validator } from \"@cfworker/json-schema\";\n\nexport type ToJSONSchemaParams = NonNullable<\n Parameters<typeof toJSONSchema>[1]\n>;\n\n/**\n * WeakMap cache for Zod/Standard-Schema → JSON Schema conversions.\n *\n * Keyed on the schema object reference. Since Zod schemas are immutable and\n * the same `tool.schema` reference is passed on every LLM call, this\n * eliminates redundant serializations. For example, an agent with 6 tools\n * doing 15 steps across 3 parallel subagents would otherwise run 270\n * identical conversions per invocation.\n *\n * Only used when no custom `params` are passed (the common case for tool\n * binding). WeakMap ensures cached entries are GC'd when the schema goes\n * out of scope.\n *\n * @internal\n */\nconst _jsonSchemaCache = new WeakMap<object, JSONSchema>();\n\n/**\n * Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.\n * Results are cached by schema reference when no custom params are passed.\n * @param schema - The schema to convert.\n * @param params - The parameters to pass to the toJSONSchema function.\n * @returns The converted schema.\n */\nexport function toJsonSchema(\n schema: StandardJSONSchemaV1 | InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n // Cache when no custom params are passed (the default for tool binding).\n // Params can change the output (e.g., different target draft), so we\n // bypass the cache when they're provided.\n const canCache = !params && schema != null && typeof schema === \"object\";\n\n if (canCache) {\n const cached = _jsonSchemaCache.get(schema);\n if (cached) return cached;\n }\n\n let result: JSONSchema;\n\n if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) {\n result = schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n } else if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(inputSchema, true);\n result = toJSONSchema(strictSchema as unknown as $ZodType, params);\n } else {\n result = toJSONSchema(schema as unknown as $ZodType, params);\n }\n } else if (isZodSchemaV3(schema)) {\n result = zodToJsonSchema(schema as never);\n } else {\n result = schema as JSONSchema;\n }\n\n if (canCache && result != null && typeof result === \"object\") {\n _jsonSchemaCache.set(schema, result);\n }\n\n return result;\n}\n\n/**\n * Validates if a JSON schema validates only strings. May return false negatives in some edge cases\n * (like recursive or unresolvable refs).\n *\n * @param schema - The schema to validate.\n * @returns `true` if the schema validates only strings, `false` otherwise.\n */\nexport function validatesOnlyStrings(schema: unknown): boolean {\n // Null, undefined, or empty schema\n if (\n !schema ||\n typeof schema !== \"object\" ||\n Object.keys(schema).length === 0 ||\n Array.isArray(schema)\n ) {\n return false; // Validates anything, not just strings\n }\n\n // Explicit type constraint\n if (\"type\" in schema) {\n if (typeof schema.type === \"string\") {\n return schema.type === \"string\";\n }\n\n if (Array.isArray(schema.type)) {\n // not sure why someone would do `\"type\": [\"string\"]` or especially `\"type\": [\"string\",\n // \"string\", \"string\", ...]` but we're not here to judge\n return schema.type.every((t) => t === \"string\");\n }\n return false; // Invalid or non-string type\n }\n\n // Enum with only string values\n if (\"enum\" in schema) {\n return (\n Array.isArray(schema.enum) &&\n schema.enum.length > 0 &&\n schema.enum.every((val) => typeof val === \"string\")\n );\n }\n\n // String constant\n if (\"const\" in schema) {\n return typeof schema.const === \"string\";\n }\n\n // Schema combinations\n if (\"allOf\" in schema && Array.isArray(schema.allOf)) {\n // If any subschema validates only strings, then the overall schema validates only strings\n return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));\n }\n\n if (\n (\"anyOf\" in schema && Array.isArray(schema.anyOf)) ||\n (\"oneOf\" in schema && Array.isArray(schema.oneOf))\n ) {\n const subschemas = (\n \"anyOf\" in schema ? schema.anyOf : schema.oneOf\n ) as unknown[];\n\n // All subschemas must validate only strings\n return (\n subschemas.length > 0 &&\n subschemas.every((subschema) => validatesOnlyStrings(subschema))\n );\n }\n\n // We're not going to try on this one, it's too complex - we just assume if it has a \"not\" key and hasn't matched one of the above checks, it's not a string schema.\n if (\"not\" in schema) {\n return false; // The not case can validate non-strings\n }\n\n if (\"$ref\" in schema && typeof schema.$ref === \"string\") {\n const ref = schema.$ref as string;\n const resolved = dereference(schema as Schema);\n if (resolved[ref]) {\n return validatesOnlyStrings(resolved[ref]);\n }\n return false;\n }\n\n // ignore recursive refs and other cases where type is omitted for now\n // ignore other cases for now where type is omitted\n\n return false;\n}\n\n// Re-export of the types used throughout langchain for json schema serialization.\n// The plan is to eventually nix zod-to-json-schema altogether in place for\n// zod v4 / a more standardized way of serializing validated inputs, so its re-exported\n// here to remove the dependency on zod-to-json-schema in downstream packages until\n// a determination is made.\n\nexport {\n type JsonSchema7Type,\n type JsonSchema7Type as JSONSchema,\n type JsonSchema7ArrayType,\n type JsonSchema7ObjectType,\n type JsonSchema7StringType,\n type JsonSchema7NumberType,\n type JsonSchema7NullableType,\n} from \"./zod-to-json-schema/index.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,MAAM,mCAAmB,IAAI,SAA6B;;;;;;;;AAS1D,SAAgB,aACd,QACA,QACY;CAIZ,MAAM,WAAW,CAAC,UAAU,UAAU,QAAQ,OAAO,WAAW;AAEhE,KAAI,UAAU;EACZ,MAAM,SAAS,iBAAiB,IAAI,OAAO;AAC3C,MAAI,OAAQ,QAAO;;CAGrB,IAAI;AAEJ,KAAI,qBAAqB,OAAO,IAAI,CAAC,cAAc,OAAO,CACxD,UAAS,OAAO,aAAa,WAAW,MAAM,EAC5C,QAAQ,YACT,CAAC;UACO,cAAc,OAAO,EAAE;EAChC,MAAM,cAAc,+BAA+B,QAAQ,KAAK;AAChE,MAAI,cAAc,YAAY,CAE5B,UAAS,aADY,uBAAuB,aAAa,KAAK,EACH,OAAO;MAElE,UAAS,aAAa,QAA+B,OAAO;YAErD,cAAc,OAAO,CAC9B,UAAS,gBAAgB,OAAgB;KAEzC,UAAS;AAGX,KAAI,YAAY,UAAU,QAAQ,OAAO,WAAW,SAClD,kBAAiB,IAAI,QAAQ,OAAO;AAGtC,QAAO;;;;;;;;;AAUT,SAAgB,qBAAqB,QAA0B;AAE7D,KACE,CAAC,UACD,OAAO,WAAW,YAClB,OAAO,KAAK,OAAO,CAAC,WAAW,KAC/B,MAAM,QAAQ,OAA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