@langchain/core 1.1.28 → 1.1.30
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 +32 -0
- package/dist/callbacks/manager.cjs +9 -5
- package/dist/callbacks/manager.cjs.map +1 -1
- package/dist/callbacks/manager.d.cts.map +1 -1
- package/dist/callbacks/manager.d.ts.map +1 -1
- package/dist/callbacks/manager.js +9 -5
- package/dist/callbacks/manager.js.map +1 -1
- package/dist/language_models/base.cjs +11 -0
- package/dist/language_models/base.cjs.map +1 -1
- package/dist/language_models/base.d.cts +7 -0
- package/dist/language_models/base.d.cts.map +1 -1
- package/dist/language_models/base.d.ts +7 -0
- package/dist/language_models/base.d.ts.map +1 -1
- package/dist/language_models/base.js +11 -0
- package/dist/language_models/base.js.map +1 -1
- package/dist/language_models/chat_models.cjs +8 -23
- package/dist/language_models/chat_models.cjs.map +1 -1
- package/dist/language_models/chat_models.d.cts +6 -0
- package/dist/language_models/chat_models.d.cts.map +1 -1
- package/dist/language_models/chat_models.d.ts +6 -0
- package/dist/language_models/chat_models.d.ts.map +1 -1
- package/dist/language_models/chat_models.js +9 -24
- package/dist/language_models/chat_models.js.map +1 -1
- package/dist/language_models/structured_output.cjs +88 -0
- package/dist/language_models/structured_output.cjs.map +1 -0
- package/dist/language_models/structured_output.d.cts +46 -0
- package/dist/language_models/structured_output.d.cts.map +1 -0
- package/dist/language_models/structured_output.d.ts +50 -0
- package/dist/language_models/structured_output.d.ts.map +1 -0
- package/dist/language_models/structured_output.js +79 -0
- package/dist/language_models/structured_output.js.map +1 -0
- package/dist/load/import_map.cjs +8 -4
- package/dist/load/import_map.cjs.map +1 -1
- package/dist/load/import_map.js +8 -4
- package/dist/load/import_map.js.map +1 -1
- package/dist/messages/ai.cjs +4 -2
- package/dist/messages/ai.cjs.map +1 -1
- package/dist/messages/ai.d.cts.map +1 -1
- package/dist/messages/ai.d.ts.map +1 -1
- package/dist/messages/ai.js +4 -2
- package/dist/messages/ai.js.map +1 -1
- package/dist/messages/base.cjs.map +1 -1
- package/dist/messages/base.d.cts +2 -2
- package/dist/messages/base.d.cts.map +1 -1
- package/dist/messages/base.d.ts +2 -2
- package/dist/messages/base.d.ts.map +1 -1
- package/dist/messages/base.js.map +1 -1
- package/dist/output_parsers/index.cjs +4 -1
- package/dist/output_parsers/index.cjs.map +1 -1
- package/dist/output_parsers/index.d.cts +2 -1
- package/dist/output_parsers/index.d.ts +2 -1
- package/dist/output_parsers/index.js +4 -2
- package/dist/output_parsers/index.js.map +1 -1
- package/dist/output_parsers/openai_tools/index.d.cts +2 -2
- package/dist/output_parsers/openai_tools/index.d.ts +2 -2
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +8 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.cts +6 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.cts.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +6 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts.map +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +8 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js.map +1 -1
- package/dist/output_parsers/standard_schema.cjs +40 -0
- package/dist/output_parsers/standard_schema.cjs.map +1 -0
- package/dist/output_parsers/standard_schema.d.cts +16 -0
- package/dist/output_parsers/standard_schema.d.cts.map +1 -0
- package/dist/output_parsers/standard_schema.d.ts +16 -0
- package/dist/output_parsers/standard_schema.d.ts.map +1 -0
- package/dist/output_parsers/standard_schema.js +40 -0
- package/dist/output_parsers/standard_schema.js.map +1 -0
- package/dist/utils/json_schema.cjs +3 -1
- package/dist/utils/json_schema.cjs.map +1 -1
- package/dist/utils/json_schema.d.cts +3 -2
- package/dist/utils/json_schema.d.cts.map +1 -1
- package/dist/utils/json_schema.d.ts +3 -2
- package/dist/utils/json_schema.d.ts.map +1 -1
- package/dist/utils/json_schema.js +3 -1
- package/dist/utils/json_schema.js.map +1 -1
- package/dist/utils/standard_schema.cjs +43 -0
- package/dist/utils/standard_schema.cjs.map +1 -0
- package/dist/utils/standard_schema.d.cts +27 -0
- package/dist/utils/standard_schema.d.cts.map +1 -0
- package/dist/utils/standard_schema.d.ts +27 -0
- package/dist/utils/standard_schema.d.ts.map +1 -0
- package/dist/utils/standard_schema.js +34 -0
- package/dist/utils/standard_schema.js.map +1 -0
- package/dist/utils/testing/fake_model_builder.cjs +174 -0
- package/dist/utils/testing/fake_model_builder.cjs.map +1 -0
- package/dist/utils/testing/fake_model_builder.d.cts +87 -0
- package/dist/utils/testing/fake_model_builder.d.cts.map +1 -0
- package/dist/utils/testing/fake_model_builder.d.ts +88 -0
- package/dist/utils/testing/fake_model_builder.d.ts.map +1 -0
- package/dist/utils/testing/fake_model_builder.js +174 -0
- package/dist/utils/testing/fake_model_builder.js.map +1 -0
- package/dist/utils/testing/index.cjs +4 -1
- package/dist/utils/testing/index.cjs.map +1 -1
- package/dist/utils/testing/index.d.cts +2 -1
- package/dist/utils/testing/index.d.ts +2 -1
- package/dist/utils/testing/index.js +4 -2
- package/dist/utils/testing/index.js.map +1 -1
- package/dist/utils/types/zod.cjs +1 -1
- package/dist/utils/types/zod.cjs.map +1 -1
- package/dist/utils/types/zod.d.cts +3 -2
- package/dist/utils/types/zod.d.cts.map +1 -1
- package/dist/utils/types/zod.d.ts +3 -2
- package/dist/utils/types/zod.d.ts.map +1 -1
- package/dist/utils/types/zod.js +1 -1
- package/dist/utils/types/zod.js.map +1 -1
- package/errors.cjs +1 -0
- package/errors.d.cts +1 -0
- package/errors.d.ts +1 -0
- package/errors.js +1 -0
- package/language_models/structured_output.cjs +1 -0
- package/language_models/structured_output.d.cts +1 -0
- package/language_models/structured_output.d.ts +1 -0
- package/language_models/structured_output.js +1 -0
- package/package.json +33 -6
- package/utils/standard_schema.cjs +1 -0
- package/utils/standard_schema.d.cts +1 -0
- package/utils/standard_schema.d.ts +1 -0
- package/utils/standard_schema.js +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard_schema.d.cts","names":[],"sources":["../../src/output_parsers/standard_schema.ts"],"mappings":";;;;cAIa,0BAAA,mBAEO,MAAA,gBAAsB,MAAA,uBAChC,gBAAA,CAAiB,SAAA;EAAA,OAClB,OAAA,CAAA;EAIP,YAAA;EAAA,iBAEiB,MAAA;EAEjB,WAAA,CAAY,MAAA,EAAQ,gBAAA,CAAiB,SAAA;EAAA,OAK9B,sBAAA,mBAEa,MAAA,gBAAsB,MAAA,cAAA,CACxC,MAAA,EAAQ,gBAAA,CAAiB,SAAA,IAAU,0BAAA,CAAA,SAAA;EAI/B,KAAA,CAAM,IAAA,WAAe,OAAA,CAAQ,SAAA;EAgBnC,qBAAA,CAAA;AAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BaseOutputParser } from "./base.js";
|
|
2
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
3
|
+
|
|
4
|
+
//#region src/output_parsers/standard_schema.d.ts
|
|
5
|
+
declare class StandardSchemaOutputParser<RunOutput extends Record<string, any> = Record<string, any>> extends BaseOutputParser<RunOutput> {
|
|
6
|
+
static lc_name(): string;
|
|
7
|
+
lc_namespace: string[];
|
|
8
|
+
private readonly schema;
|
|
9
|
+
constructor(schema: StandardSchemaV1<RunOutput>);
|
|
10
|
+
static fromSerializableSchema<RunOutput extends Record<string, any> = Record<string, any>>(schema: StandardSchemaV1<RunOutput>): StandardSchemaOutputParser<RunOutput>;
|
|
11
|
+
parse(text: string): Promise<RunOutput>;
|
|
12
|
+
getFormatInstructions(): string;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { StandardSchemaOutputParser };
|
|
16
|
+
//# sourceMappingURL=standard_schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard_schema.d.ts","names":[],"sources":["../../src/output_parsers/standard_schema.ts"],"mappings":";;;;cAIa,0BAAA,mBAEO,MAAA,gBAAsB,MAAA,uBAChC,gBAAA,CAAiB,SAAA;EAAA,OAClB,OAAA,CAAA;EAIP,YAAA;EAAA,iBAEiB,MAAA;EAEjB,WAAA,CAAY,MAAA,EAAQ,gBAAA,CAAiB,SAAA;EAAA,OAK9B,sBAAA,mBAEa,MAAA,gBAAsB,MAAA,cAAA,CACxC,MAAA,EAAQ,gBAAA,CAAiB,SAAA,IAAU,0BAAA,CAAA,SAAA;EAI/B,KAAA,CAAM,IAAA,WAAe,OAAA,CAAQ,SAAA;EAgBnC,qBAAA,CAAA;AAAA"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { parseJsonMarkdown } from "../utils/json.js";
|
|
2
|
+
import { BaseOutputParser, OutputParserException } from "./base.js";
|
|
3
|
+
import "./json.js";
|
|
4
|
+
|
|
5
|
+
//#region src/output_parsers/standard_schema.ts
|
|
6
|
+
var StandardSchemaOutputParser = class extends BaseOutputParser {
|
|
7
|
+
static lc_name() {
|
|
8
|
+
return "StandardSchemaOutputParser";
|
|
9
|
+
}
|
|
10
|
+
lc_namespace = [
|
|
11
|
+
"langchain",
|
|
12
|
+
"output_parsers",
|
|
13
|
+
"standard_schema"
|
|
14
|
+
];
|
|
15
|
+
schema;
|
|
16
|
+
constructor(schema) {
|
|
17
|
+
super();
|
|
18
|
+
this.schema = schema;
|
|
19
|
+
}
|
|
20
|
+
static fromSerializableSchema(schema) {
|
|
21
|
+
return new this(schema);
|
|
22
|
+
}
|
|
23
|
+
async parse(text) {
|
|
24
|
+
try {
|
|
25
|
+
const json = parseJsonMarkdown(text, JSON.parse);
|
|
26
|
+
const result = await this.schema["~standard"].validate(json);
|
|
27
|
+
if (result.issues) throw new Error(`Validation failed: ${JSON.stringify(result.issues)}`);
|
|
28
|
+
return result.value;
|
|
29
|
+
} catch (e) {
|
|
30
|
+
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
getFormatInstructions() {
|
|
34
|
+
return "";
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { StandardSchemaOutputParser };
|
|
40
|
+
//# sourceMappingURL=standard_schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard_schema.js","names":[],"sources":["../../src/output_parsers/standard_schema.ts"],"sourcesContent":["import { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport { BaseOutputParser, OutputParserException } from \"./base.js\";\nimport { parseJsonMarkdown } from \"./json.js\";\n\nexport class StandardSchemaOutputParser<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n> extends BaseOutputParser<RunOutput> {\n static lc_name() {\n return \"StandardSchemaOutputParser\";\n }\n\n lc_namespace = [\"langchain\", \"output_parsers\", \"standard_schema\"];\n\n private readonly schema: StandardSchemaV1<RunOutput>;\n\n constructor(schema: StandardSchemaV1<RunOutput>) {\n super();\n this.schema = schema;\n }\n\n static fromSerializableSchema<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(schema: StandardSchemaV1<RunOutput>) {\n return new this(schema);\n }\n\n async parse(text: string): Promise<RunOutput> {\n try {\n const json = parseJsonMarkdown(text, JSON.parse);\n const result = await this.schema[\"~standard\"].validate(json);\n if (result.issues) {\n throw new Error(`Validation failed: ${JSON.stringify(result.issues)}`);\n }\n return result.value as RunOutput;\n } catch (e) {\n throw new OutputParserException(\n `Failed to parse. Text: \"${text}\". Error: ${e}`,\n text\n );\n }\n }\n\n getFormatInstructions(): string {\n return \"\";\n }\n}\n"],"mappings":";;;;;AAIA,IAAa,6BAAb,cAGU,iBAA4B;CACpC,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe;EAAC;EAAa;EAAkB;EAAkB;CAEjE,AAAiB;CAEjB,YAAY,QAAqC;AAC/C,SAAO;AACP,OAAK,SAAS;;CAGhB,OAAO,uBAGL,QAAqC;AACrC,SAAO,IAAI,KAAK,OAAO;;CAGzB,MAAM,MAAM,MAAkC;AAC5C,MAAI;GACF,MAAM,OAAO,kBAAkB,MAAM,KAAK,MAAM;GAChD,MAAM,SAAS,MAAM,KAAK,OAAO,aAAa,SAAS,KAAK;AAC5D,OAAI,OAAO,OACT,OAAM,IAAI,MAAM,sBAAsB,KAAK,UAAU,OAAO,OAAO,GAAG;AAExE,UAAO,OAAO;WACP,GAAG;AACV,SAAM,IAAI,sBACR,2BAA2B,KAAK,YAAY,KAC5C,KACD;;;CAIL,wBAAgC;AAC9B,SAAO"}
|
|
@@ -3,6 +3,7 @@ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
|
3
3
|
const require_zod = require('./types/zod.cjs');
|
|
4
4
|
const require_zodToJsonSchema = require('./zod-to-json-schema/zodToJsonSchema.cjs');
|
|
5
5
|
require('./zod-to-json-schema/index.cjs');
|
|
6
|
+
const require_utils_standard_schema = require('./standard_schema.cjs');
|
|
6
7
|
let zod_v4_core = require("zod/v4/core");
|
|
7
8
|
let _cfworker_json_schema = require("@cfworker/json-schema");
|
|
8
9
|
|
|
@@ -14,12 +15,13 @@ var json_schema_exports = /* @__PURE__ */ require_runtime.__exportAll({
|
|
|
14
15
|
validatesOnlyStrings: () => validatesOnlyStrings
|
|
15
16
|
});
|
|
16
17
|
/**
|
|
17
|
-
* Converts a Zod schema or JSON schema to a JSON schema.
|
|
18
|
+
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
18
19
|
* @param schema - The schema to convert.
|
|
19
20
|
* @param params - The parameters to pass to the toJSONSchema function.
|
|
20
21
|
* @returns The converted schema.
|
|
21
22
|
*/
|
|
22
23
|
function toJsonSchema(schema, params) {
|
|
24
|
+
if (require_utils_standard_schema.isStandardJsonSchema(schema) && !require_zod.isZodSchemaV4(schema)) return schema["~standard"].jsonSchema.input({ target: "draft-07" });
|
|
23
25
|
if (require_zod.isZodSchemaV4(schema)) {
|
|
24
26
|
const inputSchema = require_zod.interopZodTransformInputSchema(schema, true);
|
|
25
27
|
if (require_zod.isZodObjectV4(inputSchema)) return (0, zod_v4_core.toJSONSchema)(require_zod.interopZodObjectStrict(inputSchema, true), params);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.cjs","names":["isZodSchemaV4","interopZodTransformInputSchema","isZodObjectV4","interopZodObjectStrict","isZodSchemaV3","zodToJsonSchema"],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n ZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.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 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: InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(\n inputSchema,\n true\n ) as ZodObjectV4;\n return toJSONSchema(strictSchema, params);\n } else {\n return toJSONSchema(schema, params);\n }\n }\n if (isZodSchemaV3(schema)) {\n return zodToJsonSchema(schema);\n }\n return schema as JSONSchema;\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":"
|
|
1
|
+
{"version":3,"file":"json_schema.cjs","names":["isStandardJsonSchema","isZodSchemaV4","interopZodTransformInputSchema","isZodObjectV4","interopZodObjectStrict","isZodSchemaV3","zodToJsonSchema"],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n ZodObjectV4,\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 return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n }\n if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(\n inputSchema,\n true\n ) as ZodObjectV4;\n return toJSONSchema(strictSchema, params);\n } else {\n return toJSONSchema(schema, params);\n }\n }\n if (isZodSchemaV3(schema)) {\n return zodToJsonSchema(schema);\n }\n return schema as JSONSchema;\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":";;;;;;;;;;;;;;;;;;;;;;AA8BA,SAAgB,aACd,QACA,QACY;AACZ,KAAIA,mDAAqB,OAAO,IAAI,CAACC,0BAAc,OAAO,CACxD,QAAO,OAAO,aAAa,WAAW,MAAM,EAC1C,QAAQ,YACT,CAAC;AAEJ,KAAIA,0BAAc,OAAO,EAAE;EACzB,MAAM,cAAcC,2CAA+B,QAAQ,KAAK;AAChE,MAAIC,0BAAc,YAAY,CAK5B,sCAJqBC,mCACnB,aACA,KACD,EACiC,OAAO;MAEzC,sCAAoB,QAAQ,OAAO;;AAGvC,KAAIC,0BAAc,OAAO,CACvB,QAAOC,wCAAgB,OAAO;AAEhC,QAAO;;;;;;;;;AAUT,SAAgB,qBAAqB,QAA0B;AAE7D,KACE,CAAC,UACD,OAAO,WAAW,YAClB,OAAO,KAAK,OAAO,CAAC,WAAW,KAC/B,MAAM,QAAQ,OAAO,CAErB,QAAO;AAIT,KAAI,UAAU,QAAQ;AACpB,MAAI,OAAO,OAAO,SAAS,SACzB,QAAO,OAAO,SAAS;AAGzB,MAAI,MAAM,QAAQ,OAAO,KAAK,CAG5B,QAAO,OAAO,KAAK,OAAO,MAAM,MAAM,SAAS;AAEjD,SAAO;;AAIT,KAAI,UAAU,OACZ,QACE,MAAM,QAAQ,OAAO,KAAK,IAC1B,OAAO,KAAK,SAAS,KACrB,OAAO,KAAK,OAAO,QAAQ,OAAO,QAAQ,SAAS;AAKvD,KAAI,WAAW,OACb,QAAO,OAAO,OAAO,UAAU;AAIjC,KAAI,WAAW,UAAU,MAAM,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,kDAAuB,OAAiB;AAC9C,MAAI,SAAS,KACX,QAAO,qBAAqB,SAAS,KAAK;AAE5C,SAAO;;AAMT,QAAO"}
|
|
@@ -6,17 +6,18 @@ import { JsonSchema7NullableType } from "./zod-to-json-schema/parsers/nullable.c
|
|
|
6
6
|
import { JsonSchema7ObjectType } from "./zod-to-json-schema/parsers/object.cjs";
|
|
7
7
|
import { JsonSchema7Type } from "./zod-to-json-schema/parseTypes.cjs";
|
|
8
8
|
import { toJSONSchema } from "zod/v4/core";
|
|
9
|
+
import { StandardJSONSchemaV1 } from "@standard-schema/spec";
|
|
9
10
|
import { Validator, deepCompareStrict } from "@cfworker/json-schema";
|
|
10
11
|
|
|
11
12
|
//#region src/utils/json_schema.d.ts
|
|
12
13
|
type ToJSONSchemaParams = NonNullable<Parameters<typeof toJSONSchema>[1]>;
|
|
13
14
|
/**
|
|
14
|
-
* Converts a Zod schema or JSON schema to a JSON schema.
|
|
15
|
+
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
15
16
|
* @param schema - The schema to convert.
|
|
16
17
|
* @param params - The parameters to pass to the toJSONSchema function.
|
|
17
18
|
* @returns The converted schema.
|
|
18
19
|
*/
|
|
19
|
-
declare function toJsonSchema(schema: InteropZodType | JsonSchema7Type, params?: ToJSONSchemaParams): JsonSchema7Type;
|
|
20
|
+
declare function toJsonSchema(schema: StandardJSONSchemaV1 | InteropZodType | JsonSchema7Type, params?: ToJSONSchemaParams): JsonSchema7Type;
|
|
20
21
|
/**
|
|
21
22
|
* Validates if a JSON schema validates only strings. May return false negatives in some edge cases
|
|
22
23
|
* (like recursive or unresolvable refs).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.d.cts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"json_schema.d.cts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;KAoBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;iBASJ,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;;;;;;;;iBA+Ba,oBAAA,CAAqB,MAAA"}
|
|
@@ -8,16 +8,17 @@ import { JsonSchema7Type } from "./zod-to-json-schema/parseTypes.js";
|
|
|
8
8
|
import "./zod-to-json-schema/index.js";
|
|
9
9
|
import { toJSONSchema } from "zod/v4/core";
|
|
10
10
|
import { Validator, deepCompareStrict } from "@cfworker/json-schema";
|
|
11
|
+
import { StandardJSONSchemaV1 } from "@standard-schema/spec";
|
|
11
12
|
|
|
12
13
|
//#region src/utils/json_schema.d.ts
|
|
13
14
|
type ToJSONSchemaParams = NonNullable<Parameters<typeof toJSONSchema>[1]>;
|
|
14
15
|
/**
|
|
15
|
-
* Converts a Zod schema or JSON schema to a JSON schema.
|
|
16
|
+
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
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.
|
|
19
20
|
*/
|
|
20
|
-
declare function toJsonSchema(schema: InteropZodType | JsonSchema7Type, params?: ToJSONSchemaParams): JsonSchema7Type;
|
|
21
|
+
declare function toJsonSchema(schema: StandardJSONSchemaV1 | InteropZodType | JsonSchema7Type, params?: ToJSONSchemaParams): JsonSchema7Type;
|
|
21
22
|
/**
|
|
22
23
|
* Validates if a JSON schema validates only strings. May return false negatives in some edge cases
|
|
23
24
|
* (like recursive or unresolvable refs).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.d.ts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"json_schema.d.ts","names":[],"sources":["../../src/utils/json_schema.ts"],"mappings":";;;;;;;;;;;;;KAoBY,kBAAA,GAAqB,WAAA,CAC/B,UAAA,QAAkB,YAAA;;;;;;;iBASJ,YAAA,CACd,MAAA,EAAQ,oBAAA,GAAuB,cAAA,GAAiB,eAAA,EAChD,MAAA,GAAS,kBAAA,GACR,eAAA;AAbH;;;;;;;AAAA,iBA4CgB,oBAAA,CAAqB,MAAA"}
|
|
@@ -2,6 +2,7 @@ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
|
2
2
|
import { interopZodObjectStrict, interopZodTransformInputSchema, isZodObjectV4, isZodSchemaV3, isZodSchemaV4 } from "./types/zod.js";
|
|
3
3
|
import { zodToJsonSchema } from "./zod-to-json-schema/zodToJsonSchema.js";
|
|
4
4
|
import "./zod-to-json-schema/index.js";
|
|
5
|
+
import { isStandardJsonSchema } from "./standard_schema.js";
|
|
5
6
|
import { toJSONSchema } from "zod/v4/core";
|
|
6
7
|
import { Validator, deepCompareStrict, dereference } from "@cfworker/json-schema";
|
|
7
8
|
|
|
@@ -13,12 +14,13 @@ var json_schema_exports = /* @__PURE__ */ __exportAll({
|
|
|
13
14
|
validatesOnlyStrings: () => validatesOnlyStrings
|
|
14
15
|
});
|
|
15
16
|
/**
|
|
16
|
-
* Converts a Zod schema or JSON schema to a JSON schema.
|
|
17
|
+
* Converts a Standard JSON schema, Zod schema or JSON schema to a JSON schema.
|
|
17
18
|
* @param schema - The schema to convert.
|
|
18
19
|
* @param params - The parameters to pass to the toJSONSchema function.
|
|
19
20
|
* @returns The converted schema.
|
|
20
21
|
*/
|
|
21
22
|
function toJsonSchema(schema, params) {
|
|
23
|
+
if (isStandardJsonSchema(schema) && !isZodSchemaV4(schema)) return schema["~standard"].jsonSchema.input({ target: "draft-07" });
|
|
22
24
|
if (isZodSchemaV4(schema)) {
|
|
23
25
|
const inputSchema = interopZodTransformInputSchema(schema, true);
|
|
24
26
|
if (isZodObjectV4(inputSchema)) return toJSONSchema(interopZodObjectStrict(inputSchema, true), params);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json_schema.js","names":[],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n ZodObjectV4,\n interopZodTransformInputSchema,\n} from \"./types/zod.js\";\nimport {\n type JsonSchema7Type as JSONSchema,\n zodToJsonSchema,\n} from \"./zod-to-json-schema/index.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 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: InteropZodType | JSONSchema,\n params?: ToJSONSchemaParams\n): JSONSchema {\n if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(\n inputSchema,\n true\n ) as ZodObjectV4;\n return toJSONSchema(strictSchema, params);\n } else {\n return toJSONSchema(schema, params);\n }\n }\n if (isZodSchemaV3(schema)) {\n return zodToJsonSchema(schema);\n }\n return schema as JSONSchema;\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":"
|
|
1
|
+
{"version":3,"file":"json_schema.js","names":[],"sources":["../../src/utils/json_schema.ts"],"sourcesContent":["import { toJSONSchema } from \"zod/v4/core\";\nimport { dereference, type Schema } from \"@cfworker/json-schema\";\nimport {\n isZodSchemaV3,\n isZodSchemaV4,\n InteropZodType,\n interopZodObjectStrict,\n isZodObjectV4,\n ZodObjectV4,\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 return schema[\"~standard\"].jsonSchema.input({\n target: \"draft-07\",\n }) as JSONSchema;\n }\n if (isZodSchemaV4(schema)) {\n const inputSchema = interopZodTransformInputSchema(schema, true);\n if (isZodObjectV4(inputSchema)) {\n const strictSchema = interopZodObjectStrict(\n inputSchema,\n true\n ) as ZodObjectV4;\n return toJSONSchema(strictSchema, params);\n } else {\n return toJSONSchema(schema, params);\n }\n }\n if (isZodSchemaV3(schema)) {\n return zodToJsonSchema(schema);\n }\n return schema as JSONSchema;\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":";;;;;;;;;;;;;;;;;;;;;AA8BA,SAAgB,aACd,QACA,QACY;AACZ,KAAI,qBAAqB,OAAO,IAAI,CAAC,cAAc,OAAO,CACxD,QAAO,OAAO,aAAa,WAAW,MAAM,EAC1C,QAAQ,YACT,CAAC;AAEJ,KAAI,cAAc,OAAO,EAAE;EACzB,MAAM,cAAc,+BAA+B,QAAQ,KAAK;AAChE,MAAI,cAAc,YAAY,CAK5B,QAAO,aAJc,uBACnB,aACA,KACD,EACiC,OAAO;MAEzC,QAAO,aAAa,QAAQ,OAAO;;AAGvC,KAAI,cAAc,OAAO,CACvB,QAAO,gBAAgB,OAAO;AAEhC,QAAO;;;;;;;;;AAUT,SAAgB,qBAAqB,QAA0B;AAE7D,KACE,CAAC,UACD,OAAO,WAAW,YAClB,OAAO,KAAK,OAAO,CAAC,WAAW,KAC/B,MAAM,QAAQ,OAAO,CAErB,QAAO;AAIT,KAAI,UAAU,QAAQ;AACpB,MAAI,OAAO,OAAO,SAAS,SACzB,QAAO,OAAO,SAAS;AAGzB,MAAI,MAAM,QAAQ,OAAO,KAAK,CAG5B,QAAO,OAAO,KAAK,OAAO,MAAM,MAAM,SAAS;AAEjD,SAAO;;AAIT,KAAI,UAAU,OACZ,QACE,MAAM,QAAQ,OAAO,KAAK,IAC1B,OAAO,KAAK,SAAS,KACrB,OAAO,KAAK,OAAO,QAAQ,OAAO,QAAQ,SAAS;AAKvD,KAAI,WAAW,OACb,QAAO,OAAO,OAAO,UAAU;AAIjC,KAAI,WAAW,UAAU,MAAM,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,WAAW,YAAY,OAAiB;AAC9C,MAAI,SAAS,KACX,QAAO,qBAAqB,SAAS,KAAK;AAE5C,SAAO;;AAMT,QAAO"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/utils/standard_schema.ts
|
|
5
|
+
var standard_schema_exports = /* @__PURE__ */ require_runtime.__exportAll({
|
|
6
|
+
isSerializableSchema: () => isSerializableSchema,
|
|
7
|
+
isStandardJsonSchema: () => isStandardJsonSchema,
|
|
8
|
+
isStandardSchema: () => isStandardSchema
|
|
9
|
+
});
|
|
10
|
+
/**
|
|
11
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate`
|
|
12
|
+
* interface, indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
13
|
+
*/
|
|
14
|
+
function isStandardSchema(schema) {
|
|
15
|
+
return (typeof schema === "object" || typeof schema === "function") && schema !== null && "~standard" in schema && typeof schema["~standard"] === "object" && schema["~standard"] !== null && "validate" in schema["~standard"];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Type guard for Standard JSON Schema V1. Returns true if the value has a `~standard.jsonSchema`
|
|
19
|
+
* interface, indicating it can be converted to a JSON Schema object (e.g. for sending as a tool
|
|
20
|
+
* definition to an LLM).
|
|
21
|
+
*/
|
|
22
|
+
function isStandardJsonSchema(schema) {
|
|
23
|
+
return (typeof schema === "object" || typeof schema === "function") && schema !== null && "~standard" in schema && typeof schema["~standard"] === "object" && schema["~standard"] !== null && "jsonSchema" in schema["~standard"];
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate` interface,
|
|
27
|
+
* indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
28
|
+
*/
|
|
29
|
+
function isSerializableSchema(schema) {
|
|
30
|
+
return isStandardSchema(schema) && isStandardJsonSchema(schema);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
exports.isSerializableSchema = isSerializableSchema;
|
|
35
|
+
exports.isStandardJsonSchema = isStandardJsonSchema;
|
|
36
|
+
exports.isStandardSchema = isStandardSchema;
|
|
37
|
+
Object.defineProperty(exports, 'standard_schema_exports', {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return standard_schema_exports;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
//# sourceMappingURL=standard_schema.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard_schema.cjs","names":[],"sources":["../../src/utils/standard_schema.ts"],"sourcesContent":["import { StandardJSONSchemaV1, StandardSchemaV1 } from \"@standard-schema/spec\";\n\n/**\n * A schema that supports both runtime validation and JSON Schema generation. Any schema passed\n * to withStructuredOutput must satisfy both interfaces.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type SerializableSchema<Input = any, Output = Input> = StandardSchemaV1<\n Input,\n Output\n> &\n StandardJSONSchemaV1<Input, Output>;\n\n/**\n * Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate`\n * interface, indicating it can validate unknown values at runtime (e.g. for parsing LLM output).\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isStandardSchema<Input = any, Output = Input>(\n schema: unknown\n): schema is StandardSchemaV1<Input, Output> {\n return (\n (typeof schema === \"object\" || typeof schema === \"function\") &&\n schema !== null &&\n \"~standard\" in schema &&\n typeof schema[\"~standard\"] === \"object\" &&\n schema[\"~standard\"] !== null &&\n \"validate\" in schema[\"~standard\"]\n );\n}\n\n/**\n * Type guard for Standard JSON Schema V1. Returns true if the value has a `~standard.jsonSchema`\n * interface, indicating it can be converted to a JSON Schema object (e.g. for sending as a tool\n * definition to an LLM).\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isStandardJsonSchema<Input = any, Output = Input>(\n schema: unknown\n): schema is StandardJSONSchemaV1<Input, Output> {\n return (\n (typeof schema === \"object\" || typeof schema === \"function\") &&\n schema !== null &&\n \"~standard\" in schema &&\n typeof schema[\"~standard\"] === \"object\" &&\n schema[\"~standard\"] !== null &&\n \"jsonSchema\" in schema[\"~standard\"]\n );\n}\n\n/**\n * Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate` interface,\n * indicating it can validate unknown values at runtime (e.g. for parsing LLM output).\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isSerializableSchema<Input = any, Output = Input>(\n schema: unknown\n): schema is SerializableSchema<Input, Output> {\n return isStandardSchema(schema) && isStandardJsonSchema(schema);\n}\n"],"mappings":";;;;;;;;;;;;;AAkBA,SAAgB,iBACd,QAC2C;AAC3C,SACG,OAAO,WAAW,YAAY,OAAO,WAAW,eACjD,WAAW,QACX,eAAe,UACf,OAAO,OAAO,iBAAiB,YAC/B,OAAO,iBAAiB,QACxB,cAAc,OAAO;;;;;;;AAUzB,SAAgB,qBACd,QAC+C;AAC/C,SACG,OAAO,WAAW,YAAY,OAAO,WAAW,eACjD,WAAW,QACX,eAAe,UACf,OAAO,OAAO,iBAAiB,YAC/B,OAAO,iBAAiB,QACxB,gBAAgB,OAAO;;;;;;AAS3B,SAAgB,qBACd,QAC6C;AAC7C,QAAO,iBAAiB,OAAO,IAAI,qBAAqB,OAAO"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/standard_schema.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A schema that supports both runtime validation and JSON Schema generation. Any schema passed
|
|
6
|
+
* to withStructuredOutput must satisfy both interfaces.
|
|
7
|
+
*/
|
|
8
|
+
type SerializableSchema<Input = any, Output = Input> = StandardSchemaV1<Input, Output> & StandardJSONSchemaV1<Input, Output>;
|
|
9
|
+
/**
|
|
10
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate`
|
|
11
|
+
* interface, indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
12
|
+
*/
|
|
13
|
+
declare function isStandardSchema<Input = any, Output = Input>(schema: unknown): schema is StandardSchemaV1<Input, Output>;
|
|
14
|
+
/**
|
|
15
|
+
* Type guard for Standard JSON Schema V1. Returns true if the value has a `~standard.jsonSchema`
|
|
16
|
+
* interface, indicating it can be converted to a JSON Schema object (e.g. for sending as a tool
|
|
17
|
+
* definition to an LLM).
|
|
18
|
+
*/
|
|
19
|
+
declare function isStandardJsonSchema<Input = any, Output = Input>(schema: unknown): schema is StandardJSONSchemaV1<Input, Output>;
|
|
20
|
+
/**
|
|
21
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate` interface,
|
|
22
|
+
* indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
23
|
+
*/
|
|
24
|
+
declare function isSerializableSchema<Input = any, Output = Input>(schema: unknown): schema is SerializableSchema<Input, Output>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { SerializableSchema, isSerializableSchema, isStandardJsonSchema, isStandardSchema };
|
|
27
|
+
//# sourceMappingURL=standard_schema.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard_schema.d.cts","names":[],"sources":["../../src/utils/standard_schema.ts"],"mappings":";;;;;AAOA;;KAAY,kBAAA,uBAAyC,KAAA,IAAS,gBAAA,CAC5D,KAAA,EACA,MAAA,IAEA,oBAAA,CAAqB,KAAA,EAAO,MAAA;;;;;iBAOd,gBAAA,uBAAuC,KAAA,CAAA,CACrD,MAAA,YACC,MAAA,IAAU,gBAAA,CAAiB,KAAA,EAAO,MAAA;;;;;;iBAiBrB,oBAAA,uBAA2C,KAAA,CAAA,CACzD,MAAA,YACC,MAAA,IAAU,oBAAA,CAAqB,KAAA,EAAO,MAAA;;;;;iBAgBzB,oBAAA,uBAA2C,KAAA,CAAA,CACzD,MAAA,YACC,MAAA,IAAU,kBAAA,CAAmB,KAAA,EAAO,MAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/standard_schema.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A schema that supports both runtime validation and JSON Schema generation. Any schema passed
|
|
6
|
+
* to withStructuredOutput must satisfy both interfaces.
|
|
7
|
+
*/
|
|
8
|
+
type SerializableSchema<Input = any, Output = Input> = StandardSchemaV1<Input, Output> & StandardJSONSchemaV1<Input, Output>;
|
|
9
|
+
/**
|
|
10
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate`
|
|
11
|
+
* interface, indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
12
|
+
*/
|
|
13
|
+
declare function isStandardSchema<Input = any, Output = Input>(schema: unknown): schema is StandardSchemaV1<Input, Output>;
|
|
14
|
+
/**
|
|
15
|
+
* Type guard for Standard JSON Schema V1. Returns true if the value has a `~standard.jsonSchema`
|
|
16
|
+
* interface, indicating it can be converted to a JSON Schema object (e.g. for sending as a tool
|
|
17
|
+
* definition to an LLM).
|
|
18
|
+
*/
|
|
19
|
+
declare function isStandardJsonSchema<Input = any, Output = Input>(schema: unknown): schema is StandardJSONSchemaV1<Input, Output>;
|
|
20
|
+
/**
|
|
21
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate` interface,
|
|
22
|
+
* indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
23
|
+
*/
|
|
24
|
+
declare function isSerializableSchema<Input = any, Output = Input>(schema: unknown): schema is SerializableSchema<Input, Output>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { SerializableSchema, isSerializableSchema, isStandardJsonSchema, isStandardSchema };
|
|
27
|
+
//# sourceMappingURL=standard_schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard_schema.d.ts","names":[],"sources":["../../src/utils/standard_schema.ts"],"mappings":";;;;;AAOA;;KAAY,kBAAA,uBAAyC,KAAA,IAAS,gBAAA,CAC5D,KAAA,EACA,MAAA,IAEA,oBAAA,CAAqB,KAAA,EAAO,MAAA;;;;;iBAOd,gBAAA,uBAAuC,KAAA,CAAA,CACrD,MAAA,YACC,MAAA,IAAU,gBAAA,CAAiB,KAAA,EAAO,MAAA;;;;;;iBAiBrB,oBAAA,uBAA2C,KAAA,CAAA,CACzD,MAAA,YACC,MAAA,IAAU,oBAAA,CAAqB,KAAA,EAAO,MAAA;;;;;iBAgBzB,oBAAA,uBAA2C,KAAA,CAAA,CACzD,MAAA,YACC,MAAA,IAAU,kBAAA,CAAmB,KAAA,EAAO,MAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/standard_schema.ts
|
|
4
|
+
var standard_schema_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
+
isSerializableSchema: () => isSerializableSchema,
|
|
6
|
+
isStandardJsonSchema: () => isStandardJsonSchema,
|
|
7
|
+
isStandardSchema: () => isStandardSchema
|
|
8
|
+
});
|
|
9
|
+
/**
|
|
10
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate`
|
|
11
|
+
* interface, indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
12
|
+
*/
|
|
13
|
+
function isStandardSchema(schema) {
|
|
14
|
+
return (typeof schema === "object" || typeof schema === "function") && schema !== null && "~standard" in schema && typeof schema["~standard"] === "object" && schema["~standard"] !== null && "validate" in schema["~standard"];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Type guard for Standard JSON Schema V1. Returns true if the value has a `~standard.jsonSchema`
|
|
18
|
+
* interface, indicating it can be converted to a JSON Schema object (e.g. for sending as a tool
|
|
19
|
+
* definition to an LLM).
|
|
20
|
+
*/
|
|
21
|
+
function isStandardJsonSchema(schema) {
|
|
22
|
+
return (typeof schema === "object" || typeof schema === "function") && schema !== null && "~standard" in schema && typeof schema["~standard"] === "object" && schema["~standard"] !== null && "jsonSchema" in schema["~standard"];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate` interface,
|
|
26
|
+
* indicating it can validate unknown values at runtime (e.g. for parsing LLM output).
|
|
27
|
+
*/
|
|
28
|
+
function isSerializableSchema(schema) {
|
|
29
|
+
return isStandardSchema(schema) && isStandardJsonSchema(schema);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { isSerializableSchema, isStandardJsonSchema, isStandardSchema, standard_schema_exports };
|
|
34
|
+
//# sourceMappingURL=standard_schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard_schema.js","names":[],"sources":["../../src/utils/standard_schema.ts"],"sourcesContent":["import { StandardJSONSchemaV1, StandardSchemaV1 } from \"@standard-schema/spec\";\n\n/**\n * A schema that supports both runtime validation and JSON Schema generation. Any schema passed\n * to withStructuredOutput must satisfy both interfaces.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type SerializableSchema<Input = any, Output = Input> = StandardSchemaV1<\n Input,\n Output\n> &\n StandardJSONSchemaV1<Input, Output>;\n\n/**\n * Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate`\n * interface, indicating it can validate unknown values at runtime (e.g. for parsing LLM output).\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isStandardSchema<Input = any, Output = Input>(\n schema: unknown\n): schema is StandardSchemaV1<Input, Output> {\n return (\n (typeof schema === \"object\" || typeof schema === \"function\") &&\n schema !== null &&\n \"~standard\" in schema &&\n typeof schema[\"~standard\"] === \"object\" &&\n schema[\"~standard\"] !== null &&\n \"validate\" in schema[\"~standard\"]\n );\n}\n\n/**\n * Type guard for Standard JSON Schema V1. Returns true if the value has a `~standard.jsonSchema`\n * interface, indicating it can be converted to a JSON Schema object (e.g. for sending as a tool\n * definition to an LLM).\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isStandardJsonSchema<Input = any, Output = Input>(\n schema: unknown\n): schema is StandardJSONSchemaV1<Input, Output> {\n return (\n (typeof schema === \"object\" || typeof schema === \"function\") &&\n schema !== null &&\n \"~standard\" in schema &&\n typeof schema[\"~standard\"] === \"object\" &&\n schema[\"~standard\"] !== null &&\n \"jsonSchema\" in schema[\"~standard\"]\n );\n}\n\n/**\n * Type guard for Standard Schema V1. Returns true if the value has a `~standard.validate` interface,\n * indicating it can validate unknown values at runtime (e.g. for parsing LLM output).\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function isSerializableSchema<Input = any, Output = Input>(\n schema: unknown\n): schema is SerializableSchema<Input, Output> {\n return isStandardSchema(schema) && isStandardJsonSchema(schema);\n}\n"],"mappings":";;;;;;;;;;;;AAkBA,SAAgB,iBACd,QAC2C;AAC3C,SACG,OAAO,WAAW,YAAY,OAAO,WAAW,eACjD,WAAW,QACX,eAAe,UACf,OAAO,OAAO,iBAAiB,YAC/B,OAAO,iBAAiB,QACxB,cAAc,OAAO;;;;;;;AAUzB,SAAgB,qBACd,QAC+C;AAC/C,SACG,OAAO,WAAW,YAAY,OAAO,WAAW,eACjD,WAAW,QACX,eAAe,UACf,OAAO,OAAO,iBAAiB,YAC/B,OAAO,iBAAiB,QACxB,gBAAgB,OAAO;;;;;;AAS3B,SAAgB,qBACd,QAC6C;AAC7C,QAAO,iBAAiB,OAAO,IAAI,qBAAqB,OAAO"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
const require_base = require('../../messages/base.cjs');
|
|
2
|
+
const require_ai = require('../../messages/ai.cjs');
|
|
3
|
+
const require_base$1 = require('../../runnables/base.cjs');
|
|
4
|
+
require('../../messages/index.cjs');
|
|
5
|
+
const require_language_models_chat_models = require('../../language_models/chat_models.cjs');
|
|
6
|
+
|
|
7
|
+
//#region src/utils/testing/fake_model_builder.ts
|
|
8
|
+
function deriveContent(messages) {
|
|
9
|
+
return messages.map((m) => m.text).filter(Boolean).join("-");
|
|
10
|
+
}
|
|
11
|
+
let idCounter = 0;
|
|
12
|
+
function nextToolCallId() {
|
|
13
|
+
idCounter += 1;
|
|
14
|
+
return `fake_tc_${idCounter}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* A fake chat model for testing, created via {@link fakeModel}.
|
|
18
|
+
*
|
|
19
|
+
* Queue responses with `.respond()` and `.respondWithTools()`, then
|
|
20
|
+
* pass the instance directly wherever a chat model is expected.
|
|
21
|
+
*
|
|
22
|
+
* Each builder method queues a model response consumed in order per `invoke()`:
|
|
23
|
+
*
|
|
24
|
+
* `.respond(entry)` — enqueue a `BaseMessage`, `Error`, or factory function.
|
|
25
|
+
*
|
|
26
|
+
* `.respondWithTools(toolCalls[])` — enqueue an `AIMessage` with the given
|
|
27
|
+
* tool calls. Content is derived from the input messages automatically.
|
|
28
|
+
*
|
|
29
|
+
* Both can be mixed freely in one chain. When all queued responses are
|
|
30
|
+
* consumed, further invocations throw.
|
|
31
|
+
*
|
|
32
|
+
* Additional configuration:
|
|
33
|
+
* - `.alwaysThrow(error)` — every call throws (overrides the queue)
|
|
34
|
+
* - `.structuredResponse(value)` — value returned by `withStructuredOutput()`
|
|
35
|
+
*
|
|
36
|
+
* The model records all invocations in `.calls` / `.callCount`.
|
|
37
|
+
*/
|
|
38
|
+
var FakeBuiltModel = class FakeBuiltModel extends require_language_models_chat_models.BaseChatModel {
|
|
39
|
+
queue = [];
|
|
40
|
+
_alwaysThrowError;
|
|
41
|
+
_structuredResponseValue;
|
|
42
|
+
_tools = [];
|
|
43
|
+
_callIndex = 0;
|
|
44
|
+
_calls = [];
|
|
45
|
+
get calls() {
|
|
46
|
+
return this._calls;
|
|
47
|
+
}
|
|
48
|
+
get callCount() {
|
|
49
|
+
return this._calls.length;
|
|
50
|
+
}
|
|
51
|
+
constructor() {
|
|
52
|
+
super({});
|
|
53
|
+
}
|
|
54
|
+
_llmType() {
|
|
55
|
+
return "fake-model-builder";
|
|
56
|
+
}
|
|
57
|
+
_combineLLMOutput() {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
respond(entry) {
|
|
61
|
+
if (typeof entry === "function") this.queue.push({
|
|
62
|
+
kind: "factory",
|
|
63
|
+
factory: entry
|
|
64
|
+
});
|
|
65
|
+
else if (require_base.BaseMessage.isInstance(entry)) this.queue.push({
|
|
66
|
+
kind: "message",
|
|
67
|
+
message: entry
|
|
68
|
+
});
|
|
69
|
+
else this.queue.push({
|
|
70
|
+
kind: "error",
|
|
71
|
+
error: entry
|
|
72
|
+
});
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
respondWithTools(toolCalls) {
|
|
76
|
+
this.queue.push({
|
|
77
|
+
kind: "toolCalls",
|
|
78
|
+
toolCalls: toolCalls.map((tc) => ({
|
|
79
|
+
name: tc.name,
|
|
80
|
+
args: tc.args,
|
|
81
|
+
id: tc.id ?? nextToolCallId(),
|
|
82
|
+
type: "tool_call"
|
|
83
|
+
}))
|
|
84
|
+
});
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
alwaysThrow(error) {
|
|
88
|
+
this._alwaysThrowError = error;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
structuredResponse(value) {
|
|
92
|
+
this._structuredResponseValue = value;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
bindTools(tools) {
|
|
96
|
+
const merged = [...this._tools, ...tools];
|
|
97
|
+
const next = new FakeBuiltModel();
|
|
98
|
+
next.queue = this.queue;
|
|
99
|
+
next._alwaysThrowError = this._alwaysThrowError;
|
|
100
|
+
next._structuredResponseValue = this._structuredResponseValue;
|
|
101
|
+
next._tools = merged;
|
|
102
|
+
next._calls = this._calls;
|
|
103
|
+
next._callIndex = this._callIndex;
|
|
104
|
+
return next.withConfig({});
|
|
105
|
+
}
|
|
106
|
+
withStructuredOutput(_params, _config) {
|
|
107
|
+
const { _structuredResponseValue } = this;
|
|
108
|
+
return require_base$1.RunnableLambda.from(async () => {
|
|
109
|
+
return _structuredResponseValue;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async _generate(messages, options, _runManager) {
|
|
113
|
+
this._calls.push({
|
|
114
|
+
messages: [...messages],
|
|
115
|
+
options
|
|
116
|
+
});
|
|
117
|
+
const currentCallIndex = this._callIndex;
|
|
118
|
+
this._callIndex += 1;
|
|
119
|
+
if (this._alwaysThrowError) throw this._alwaysThrowError;
|
|
120
|
+
const entry = this.queue[currentCallIndex];
|
|
121
|
+
if (!entry) throw new Error(`FakeModel: no response queued for invocation ${currentCallIndex} (${this.queue.length} total queued).`);
|
|
122
|
+
if (entry.kind === "error") throw entry.error;
|
|
123
|
+
if (entry.kind === "factory") {
|
|
124
|
+
const result = entry.factory(messages);
|
|
125
|
+
if (!require_base.BaseMessage.isInstance(result)) throw result;
|
|
126
|
+
return { generations: [{
|
|
127
|
+
text: "",
|
|
128
|
+
message: result
|
|
129
|
+
}] };
|
|
130
|
+
}
|
|
131
|
+
if (entry.kind === "message") return { generations: [{
|
|
132
|
+
text: "",
|
|
133
|
+
message: entry.message
|
|
134
|
+
}] };
|
|
135
|
+
const content = deriveContent(messages);
|
|
136
|
+
return {
|
|
137
|
+
generations: [{
|
|
138
|
+
text: content,
|
|
139
|
+
message: new require_ai.AIMessage({
|
|
140
|
+
content,
|
|
141
|
+
id: currentCallIndex.toString(),
|
|
142
|
+
tool_calls: entry.toolCalls.length > 0 ? entry.toolCalls.map((tc) => ({
|
|
143
|
+
...tc,
|
|
144
|
+
type: "tool_call"
|
|
145
|
+
})) : void 0
|
|
146
|
+
})
|
|
147
|
+
}],
|
|
148
|
+
llmOutput: {}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Creates a fake chat model for testing.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const model = fakeModel()
|
|
158
|
+
* .respondWithTools([{ name: "search", args: { query: "weather" } }])
|
|
159
|
+
* .respond(new AIMessage("Sunny and warm."));
|
|
160
|
+
*
|
|
161
|
+
* const r1 = await model.invoke([new HumanMessage("What's the weather?")]);
|
|
162
|
+
* // r1.tool_calls[0].name === "search"
|
|
163
|
+
*
|
|
164
|
+
* const r2 = await model.invoke([new HumanMessage("Thanks")]);
|
|
165
|
+
* // r2.content === "Sunny and warm."
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
function fakeModel() {
|
|
169
|
+
return new FakeBuiltModel();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
exports.fakeModel = fakeModel;
|
|
174
|
+
//# sourceMappingURL=fake_model_builder.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake_model_builder.cjs","names":["BaseChatModel","BaseMessage","RunnableLambda","AIMessage"],"sources":["../../../src/utils/testing/fake_model_builder.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { CallbackManagerForLLMRun } from \"../../callbacks/manager.js\";\nimport {\n BaseChatModel,\n BaseChatModelCallOptions,\n} from \"../../language_models/chat_models.js\";\nimport {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n StructuredOutputMethodParams,\n} from \"../../language_models/base.js\";\nimport { BaseMessage, AIMessage } from \"../../messages/index.js\";\nimport type { ToolCall } from \"../../messages/tool.js\";\nimport type { ChatResult } from \"../../outputs.js\";\nimport { Runnable, RunnableLambda } from \"../../runnables/base.js\";\nimport { StructuredTool } from \"../../tools/index.js\";\nimport type { InteropZodType } from \"../types/zod.js\";\nimport type { ToolSpec } from \"./chat_models.js\";\n\ntype ResponseFactory = (messages: BaseMessage[]) => BaseMessage | Error;\n\ntype QueueEntry =\n | { kind: \"message\"; message: BaseMessage }\n | { kind: \"toolCalls\"; toolCalls: ToolCall[] }\n | { kind: \"error\"; error: Error }\n | { kind: \"factory\"; factory: ResponseFactory };\n\ninterface FakeModelCall {\n messages: BaseMessage[];\n options: any;\n}\n\nfunction deriveContent(messages: BaseMessage[]): string {\n return messages\n .map((m) => m.text)\n .filter(Boolean)\n .join(\"-\");\n}\n\nlet idCounter = 0;\nfunction nextToolCallId(): string {\n idCounter += 1;\n return `fake_tc_${idCounter}`;\n}\n\n/**\n * A fake chat model for testing, created via {@link fakeModel}.\n *\n * Queue responses with `.respond()` and `.respondWithTools()`, then\n * pass the instance directly wherever a chat model is expected.\n *\n * Each builder method queues a model response consumed in order per `invoke()`:\n *\n * `.respond(entry)` — enqueue a `BaseMessage`, `Error`, or factory function.\n *\n * `.respondWithTools(toolCalls[])` — enqueue an `AIMessage` with the given\n * tool calls. Content is derived from the input messages automatically.\n *\n * Both can be mixed freely in one chain. When all queued responses are\n * consumed, further invocations throw.\n *\n * Additional configuration:\n * - `.alwaysThrow(error)` — every call throws (overrides the queue)\n * - `.structuredResponse(value)` — value returned by `withStructuredOutput()`\n *\n * The model records all invocations in `.calls` / `.callCount`.\n */\nclass FakeBuiltModel extends BaseChatModel {\n private queue: QueueEntry[] = [];\n\n private _alwaysThrowError: Error | undefined;\n\n private _structuredResponseValue: any;\n\n private _tools: (StructuredTool | ToolSpec)[] = [];\n\n private _callIndex = 0;\n\n private _calls: FakeModelCall[] = [];\n\n get calls(): FakeModelCall[] {\n return this._calls;\n }\n\n get callCount(): number {\n return this._calls.length;\n }\n\n constructor() {\n super({});\n }\n\n _llmType(): string {\n return \"fake-model-builder\";\n }\n\n _combineLLMOutput() {\n return [];\n }\n\n respond(entry: BaseMessage | Error | ResponseFactory): this {\n if (typeof entry === \"function\") {\n this.queue.push({ kind: \"factory\", factory: entry });\n } else if (BaseMessage.isInstance(entry)) {\n this.queue.push({ kind: \"message\", message: entry });\n } else {\n this.queue.push({ kind: \"error\", error: entry });\n }\n return this;\n }\n\n respondWithTools(\n toolCalls: Array<{ name: string; args: Record<string, any>; id?: string }>\n ): this {\n this.queue.push({\n kind: \"toolCalls\",\n toolCalls: toolCalls.map((tc) => ({\n name: tc.name,\n args: tc.args,\n id: tc.id ?? nextToolCallId(),\n type: \"tool_call\" as const,\n })),\n });\n return this;\n }\n\n alwaysThrow(error: Error): this {\n this._alwaysThrowError = error;\n return this;\n }\n\n structuredResponse(value: Record<string, any>): this {\n this._structuredResponseValue = value;\n return this;\n }\n\n bindTools(tools: (StructuredTool | ToolSpec)[]) {\n const merged = [...this._tools, ...tools];\n const next = new FakeBuiltModel();\n next.queue = this.queue;\n next._alwaysThrowError = this._alwaysThrowError;\n next._structuredResponseValue = this._structuredResponseValue;\n next._tools = merged;\n next._calls = this._calls;\n next._callIndex = this._callIndex;\n\n return next.withConfig({} as BaseChatModelCallOptions);\n }\n\n withStructuredOutput<\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n _params:\n | StructuredOutputMethodParams<RunOutput, boolean>\n | InteropZodType<RunOutput>\n | Record<string, any>,\n _config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n const { _structuredResponseValue } = this;\n return RunnableLambda.from(async () => {\n return _structuredResponseValue as RunOutput;\n }) as Runnable;\n }\n\n async _generate(\n messages: BaseMessage[],\n options?: this[\"ParsedCallOptions\"],\n _runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n this._calls.push({ messages: [...messages], options });\n\n const currentCallIndex = this._callIndex;\n this._callIndex += 1;\n\n if (this._alwaysThrowError) {\n throw this._alwaysThrowError;\n }\n\n const entry = this.queue[currentCallIndex];\n if (!entry) {\n throw new Error(\n `FakeModel: no response queued for invocation ${currentCallIndex} (${this.queue.length} total queued).`\n );\n }\n\n if (entry.kind === \"error\") {\n throw entry.error;\n }\n\n if (entry.kind === \"factory\") {\n const result = entry.factory(messages);\n if (!BaseMessage.isInstance(result)) {\n throw result;\n }\n return {\n generations: [{ text: \"\", message: result }],\n };\n }\n\n if (entry.kind === \"message\") {\n return {\n generations: [{ text: \"\", message: entry.message }],\n };\n }\n\n const content = deriveContent(messages);\n const message = new AIMessage({\n content,\n id: currentCallIndex.toString(),\n tool_calls:\n entry.toolCalls.length > 0\n ? entry.toolCalls.map((tc) => ({\n ...tc,\n type: \"tool_call\" as const,\n }))\n : undefined,\n });\n\n return {\n generations: [{ text: content, message }],\n llmOutput: {},\n };\n }\n}\n\n/**\n * Creates a fake chat model for testing.\n *\n * @example\n * ```typescript\n * const model = fakeModel()\n * .respondWithTools([{ name: \"search\", args: { query: \"weather\" } }])\n * .respond(new AIMessage(\"Sunny and warm.\"));\n *\n * const r1 = await model.invoke([new HumanMessage(\"What's the weather?\")]);\n * // r1.tool_calls[0].name === \"search\"\n *\n * const r2 = await model.invoke([new HumanMessage(\"Thanks\")]);\n * // r2.content === \"Sunny and warm.\"\n * ```\n */\nexport function fakeModel(): FakeBuiltModel {\n return new FakeBuiltModel();\n}\n"],"mappings":";;;;;;;AAgCA,SAAS,cAAc,UAAiC;AACtD,QAAO,SACJ,KAAK,MAAM,EAAE,KAAK,CAClB,OAAO,QAAQ,CACf,KAAK,IAAI;;AAGd,IAAI,YAAY;AAChB,SAAS,iBAAyB;AAChC,cAAa;AACb,QAAO,WAAW;;;;;;;;;;;;;;;;;;;;;;;;AAyBpB,IAAM,iBAAN,MAAM,uBAAuBA,kDAAc;CACzC,AAAQ,QAAsB,EAAE;CAEhC,AAAQ;CAER,AAAQ;CAER,AAAQ,SAAwC,EAAE;CAElD,AAAQ,aAAa;CAErB,AAAQ,SAA0B,EAAE;CAEpC,IAAI,QAAyB;AAC3B,SAAO,KAAK;;CAGd,IAAI,YAAoB;AACtB,SAAO,KAAK,OAAO;;CAGrB,cAAc;AACZ,QAAM,EAAE,CAAC;;CAGX,WAAmB;AACjB,SAAO;;CAGT,oBAAoB;AAClB,SAAO,EAAE;;CAGX,QAAQ,OAAoD;AAC1D,MAAI,OAAO,UAAU,WACnB,MAAK,MAAM,KAAK;GAAE,MAAM;GAAW,SAAS;GAAO,CAAC;WAC3CC,yBAAY,WAAW,MAAM,CACtC,MAAK,MAAM,KAAK;GAAE,MAAM;GAAW,SAAS;GAAO,CAAC;MAEpD,MAAK,MAAM,KAAK;GAAE,MAAM;GAAS,OAAO;GAAO,CAAC;AAElD,SAAO;;CAGT,iBACE,WACM;AACN,OAAK,MAAM,KAAK;GACd,MAAM;GACN,WAAW,UAAU,KAAK,QAAQ;IAChC,MAAM,GAAG;IACT,MAAM,GAAG;IACT,IAAI,GAAG,MAAM,gBAAgB;IAC7B,MAAM;IACP,EAAE;GACJ,CAAC;AACF,SAAO;;CAGT,YAAY,OAAoB;AAC9B,OAAK,oBAAoB;AACzB,SAAO;;CAGT,mBAAmB,OAAkC;AACnD,OAAK,2BAA2B;AAChC,SAAO;;CAGT,UAAU,OAAsC;EAC9C,MAAM,SAAS,CAAC,GAAG,KAAK,QAAQ,GAAG,MAAM;EACzC,MAAM,OAAO,IAAI,gBAAgB;AACjC,OAAK,QAAQ,KAAK;AAClB,OAAK,oBAAoB,KAAK;AAC9B,OAAK,2BAA2B,KAAK;AACrC,OAAK,SAAS;AACd,OAAK,SAAS,KAAK;AACnB,OAAK,aAAa,KAAK;AAEvB,SAAO,KAAK,WAAW,EAAE,CAA6B;;CAGxD,qBAGE,SAIA,SAMI;EACJ,MAAM,EAAE,6BAA6B;AACrC,SAAOC,8BAAe,KAAK,YAAY;AACrC,UAAO;IACP;;CAGJ,MAAM,UACJ,UACA,SACA,aACqB;AACrB,OAAK,OAAO,KAAK;GAAE,UAAU,CAAC,GAAG,SAAS;GAAE;GAAS,CAAC;EAEtD,MAAM,mBAAmB,KAAK;AAC9B,OAAK,cAAc;AAEnB,MAAI,KAAK,kBACP,OAAM,KAAK;EAGb,MAAM,QAAQ,KAAK,MAAM;AACzB,MAAI,CAAC,MACH,OAAM,IAAI,MACR,gDAAgD,iBAAiB,IAAI,KAAK,MAAM,OAAO,iBACxF;AAGH,MAAI,MAAM,SAAS,QACjB,OAAM,MAAM;AAGd,MAAI,MAAM,SAAS,WAAW;GAC5B,MAAM,SAAS,MAAM,QAAQ,SAAS;AACtC,OAAI,CAACD,yBAAY,WAAW,OAAO,CACjC,OAAM;AAER,UAAO,EACL,aAAa,CAAC;IAAE,MAAM;IAAI,SAAS;IAAQ,CAAC,EAC7C;;AAGH,MAAI,MAAM,SAAS,UACjB,QAAO,EACL,aAAa,CAAC;GAAE,MAAM;GAAI,SAAS,MAAM;GAAS,CAAC,EACpD;EAGH,MAAM,UAAU,cAAc,SAAS;AAavC,SAAO;GACL,aAAa,CAAC;IAAE,MAAM;IAAS,SAbjB,IAAIE,qBAAU;KAC5B;KACA,IAAI,iBAAiB,UAAU;KAC/B,YACE,MAAM,UAAU,SAAS,IACrB,MAAM,UAAU,KAAK,QAAQ;MAC3B,GAAG;MACH,MAAM;MACP,EAAE,GACH;KACP,CAAC;IAGwC,CAAC;GACzC,WAAW,EAAE;GACd;;;;;;;;;;;;;;;;;;;AAoBL,SAAgB,YAA4B;AAC1C,QAAO,IAAI,gBAAgB"}
|