@langchain/core 0.3.43 → 0.3.44
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/dist/language_models/base.d.ts +2 -1
- package/dist/tools/index.cjs +36 -22
- package/dist/tools/index.d.ts +30 -138
- package/dist/tools/index.js +37 -23
- package/dist/tools/types.cjs +60 -0
- package/dist/tools/types.d.ts +215 -0
- package/dist/tools/types.js +53 -0
- package/dist/utils/function_calling.cjs +18 -68
- package/dist/utils/function_calling.d.ts +8 -36
- package/dist/utils/function_calling.js +12 -62
- package/dist/utils/json_schema.cjs +85 -4
- package/dist/utils/json_schema.d.ts +17 -0
- package/dist/utils/json_schema.js +79 -0
- package/dist/utils/types/is_zod_schema.cjs +28 -6
- package/dist/utils/types/is_zod_schema.d.ts +3 -3
- package/dist/utils/types/is_zod_schema.js +28 -6
- package/package.json +1 -1
|
@@ -1 +1,80 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
import { dereference } from "@cfworker/json-schema";
|
|
3
|
+
import { isZodSchema } from "./types/is_zod_schema.js";
|
|
1
4
|
export { deepCompareStrict, Validator } from "@cfworker/json-schema";
|
|
5
|
+
/**
|
|
6
|
+
* Converts a Zod schema or JSON schema to a JSON schema.
|
|
7
|
+
* @param schema - The schema to convert.
|
|
8
|
+
* @returns The converted schema.
|
|
9
|
+
*/
|
|
10
|
+
export function toJsonSchema(schema) {
|
|
11
|
+
if (isZodSchema(schema)) {
|
|
12
|
+
return zodToJsonSchema(schema);
|
|
13
|
+
}
|
|
14
|
+
return schema;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Validates if a JSON schema validates only strings. May return false negatives in some edge cases
|
|
18
|
+
* (like recursive or unresolvable refs).
|
|
19
|
+
*
|
|
20
|
+
* @param schema - The schema to validate.
|
|
21
|
+
* @returns `true` if the schema validates only strings, `false` otherwise.
|
|
22
|
+
*/
|
|
23
|
+
export function validatesOnlyStrings(schema) {
|
|
24
|
+
// Null, undefined, or empty schema
|
|
25
|
+
if (!schema ||
|
|
26
|
+
typeof schema !== "object" ||
|
|
27
|
+
Object.keys(schema).length === 0 ||
|
|
28
|
+
Array.isArray(schema)) {
|
|
29
|
+
return false; // Validates anything, not just strings
|
|
30
|
+
}
|
|
31
|
+
// Explicit type constraint
|
|
32
|
+
if ("type" in schema) {
|
|
33
|
+
if (typeof schema.type === "string") {
|
|
34
|
+
return schema.type === "string";
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(schema.type)) {
|
|
37
|
+
// not sure why someone would do `"type": ["string"]` or especially `"type": ["string",
|
|
38
|
+
// "string", "string", ...]` but we're not here to judge
|
|
39
|
+
return schema.type.every((t) => t === "string");
|
|
40
|
+
}
|
|
41
|
+
return false; // Invalid or non-string type
|
|
42
|
+
}
|
|
43
|
+
// Enum with only string values
|
|
44
|
+
if ("enum" in schema) {
|
|
45
|
+
return (Array.isArray(schema.enum) &&
|
|
46
|
+
schema.enum.length > 0 &&
|
|
47
|
+
schema.enum.every((val) => typeof val === "string"));
|
|
48
|
+
}
|
|
49
|
+
// String constant
|
|
50
|
+
if ("const" in schema) {
|
|
51
|
+
return typeof schema.const === "string";
|
|
52
|
+
}
|
|
53
|
+
// Schema combinations
|
|
54
|
+
if ("allOf" in schema && Array.isArray(schema.allOf)) {
|
|
55
|
+
// If any subschema validates only strings, then the overall schema validates only strings
|
|
56
|
+
return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));
|
|
57
|
+
}
|
|
58
|
+
if (("anyOf" in schema && Array.isArray(schema.anyOf)) ||
|
|
59
|
+
("oneOf" in schema && Array.isArray(schema.oneOf))) {
|
|
60
|
+
const subschemas = ("anyOf" in schema ? schema.anyOf : schema.oneOf);
|
|
61
|
+
// All subschemas must validate only strings
|
|
62
|
+
return (subschemas.length > 0 &&
|
|
63
|
+
subschemas.every((subschema) => validatesOnlyStrings(subschema)));
|
|
64
|
+
}
|
|
65
|
+
// 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.
|
|
66
|
+
if ("not" in schema) {
|
|
67
|
+
return false; // The not case can validate non-strings
|
|
68
|
+
}
|
|
69
|
+
if ("$ref" in schema && typeof schema.$ref === "string") {
|
|
70
|
+
const ref = schema.$ref;
|
|
71
|
+
const resolved = dereference(schema);
|
|
72
|
+
if (resolved[ref]) {
|
|
73
|
+
return validatesOnlyStrings(resolved[ref]);
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
// ignore recursive refs and other cases where type is omitted for now
|
|
78
|
+
// ignore other cases for now where type is omitted
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
@@ -1,16 +1,38 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isZodSchema = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
4
5
|
/**
|
|
5
6
|
* Given either a Zod schema, or plain object, determine if the input is a Zod schema.
|
|
6
7
|
*
|
|
7
|
-
* @param {z.ZodType<RunOutput> | Record<string,
|
|
8
|
+
* @param {z.ZodType<RunOutput> | Record<string, unknown>} input
|
|
8
9
|
* @returns {boolean} Whether or not the provided input is a Zod schema.
|
|
9
10
|
*/
|
|
10
|
-
function isZodSchema(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
function isZodSchema(input) {
|
|
12
|
+
if (!input) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
if (typeof input !== "object") {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(input)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const asZodSchema = input;
|
|
22
|
+
// relies on an internal property of Zod schemas, so this may break in the future, hence the
|
|
23
|
+
// additional fallback checks below
|
|
24
|
+
if (asZodSchema._def) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
const zodFirstPartyTypeKinds = Object.values(zod_1.z.ZodFirstPartyTypeKind);
|
|
28
|
+
if (zodFirstPartyTypeKinds.includes(asZodSchema.constructor?.name ?? "NOT_INCLUDED")) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
// if all else fails, assume based on the presence of parse, parseAsync, safeParse, and
|
|
32
|
+
// safeParseAsync, as these are characteristic of Zod schemas
|
|
33
|
+
return (typeof asZodSchema.parse === "function" &&
|
|
34
|
+
typeof asZodSchema.parseAsync === "function" &&
|
|
35
|
+
typeof asZodSchema.safeParse === "function" &&
|
|
36
|
+
typeof asZodSchema.safeParseAsync === "function");
|
|
15
37
|
}
|
|
16
38
|
exports.isZodSchema = isZodSchema;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { z } from "zod";
|
|
2
2
|
/**
|
|
3
3
|
* Given either a Zod schema, or plain object, determine if the input is a Zod schema.
|
|
4
4
|
*
|
|
5
|
-
* @param {z.ZodType<RunOutput> | Record<string,
|
|
5
|
+
* @param {z.ZodType<RunOutput> | Record<string, unknown>} input
|
|
6
6
|
* @returns {boolean} Whether or not the provided input is a Zod schema.
|
|
7
7
|
*/
|
|
8
|
-
export declare function isZodSchema<RunOutput extends Record<string,
|
|
8
|
+
export declare function isZodSchema<RunOutput extends Record<string, unknown> = Record<string, unknown>>(input: z.ZodType<RunOutput> | Record<string, unknown>): input is z.ZodType<RunOutput>;
|
|
@@ -1,12 +1,34 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
/**
|
|
2
3
|
* Given either a Zod schema, or plain object, determine if the input is a Zod schema.
|
|
3
4
|
*
|
|
4
|
-
* @param {z.ZodType<RunOutput> | Record<string,
|
|
5
|
+
* @param {z.ZodType<RunOutput> | Record<string, unknown>} input
|
|
5
6
|
* @returns {boolean} Whether or not the provided input is a Zod schema.
|
|
6
7
|
*/
|
|
7
|
-
export function isZodSchema(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
export function isZodSchema(input) {
|
|
9
|
+
if (!input) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
if (typeof input !== "object") {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
if (Array.isArray(input)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const asZodSchema = input;
|
|
19
|
+
// relies on an internal property of Zod schemas, so this may break in the future, hence the
|
|
20
|
+
// additional fallback checks below
|
|
21
|
+
if (asZodSchema._def) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
const zodFirstPartyTypeKinds = Object.values(z.ZodFirstPartyTypeKind);
|
|
25
|
+
if (zodFirstPartyTypeKinds.includes(asZodSchema.constructor?.name ?? "NOT_INCLUDED")) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
// if all else fails, assume based on the presence of parse, parseAsync, safeParse, and
|
|
29
|
+
// safeParseAsync, as these are characteristic of Zod schemas
|
|
30
|
+
return (typeof asZodSchema.parse === "function" &&
|
|
31
|
+
typeof asZodSchema.parseAsync === "function" &&
|
|
32
|
+
typeof asZodSchema.safeParse === "function" &&
|
|
33
|
+
typeof asZodSchema.safeParseAsync === "function");
|
|
12
34
|
}
|