@langchain/core 0.3.42 → 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.
@@ -1,6 +1,87 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Validator = exports.deepCompareStrict = void 0;
4
- var json_schema_1 = require("@cfworker/json-schema");
5
- Object.defineProperty(exports, "deepCompareStrict", { enumerable: true, get: function () { return json_schema_1.deepCompareStrict; } });
6
- Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return json_schema_1.Validator; } });
3
+ exports.validatesOnlyStrings = exports.toJsonSchema = exports.Validator = exports.deepCompareStrict = void 0;
4
+ const zod_to_json_schema_1 = require("zod-to-json-schema");
5
+ const json_schema_1 = require("@cfworker/json-schema");
6
+ const is_zod_schema_js_1 = require("./types/is_zod_schema.cjs");
7
+ var json_schema_2 = require("@cfworker/json-schema");
8
+ Object.defineProperty(exports, "deepCompareStrict", { enumerable: true, get: function () { return json_schema_2.deepCompareStrict; } });
9
+ Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return json_schema_2.Validator; } });
10
+ /**
11
+ * Converts a Zod schema or JSON schema to a JSON schema.
12
+ * @param schema - The schema to convert.
13
+ * @returns The converted schema.
14
+ */
15
+ function toJsonSchema(schema) {
16
+ if ((0, is_zod_schema_js_1.isZodSchema)(schema)) {
17
+ return (0, zod_to_json_schema_1.zodToJsonSchema)(schema);
18
+ }
19
+ return schema;
20
+ }
21
+ exports.toJsonSchema = toJsonSchema;
22
+ /**
23
+ * Validates if a JSON schema validates only strings. May return false negatives in some edge cases
24
+ * (like recursive or unresolvable refs).
25
+ *
26
+ * @param schema - The schema to validate.
27
+ * @returns `true` if the schema validates only strings, `false` otherwise.
28
+ */
29
+ function validatesOnlyStrings(schema) {
30
+ // Null, undefined, or empty schema
31
+ if (!schema ||
32
+ typeof schema !== "object" ||
33
+ Object.keys(schema).length === 0 ||
34
+ Array.isArray(schema)) {
35
+ return false; // Validates anything, not just strings
36
+ }
37
+ // Explicit type constraint
38
+ if ("type" in schema) {
39
+ if (typeof schema.type === "string") {
40
+ return schema.type === "string";
41
+ }
42
+ if (Array.isArray(schema.type)) {
43
+ // not sure why someone would do `"type": ["string"]` or especially `"type": ["string",
44
+ // "string", "string", ...]` but we're not here to judge
45
+ return schema.type.every((t) => t === "string");
46
+ }
47
+ return false; // Invalid or non-string type
48
+ }
49
+ // Enum with only string values
50
+ if ("enum" in schema) {
51
+ return (Array.isArray(schema.enum) &&
52
+ schema.enum.length > 0 &&
53
+ schema.enum.every((val) => typeof val === "string"));
54
+ }
55
+ // String constant
56
+ if ("const" in schema) {
57
+ return typeof schema.const === "string";
58
+ }
59
+ // Schema combinations
60
+ if ("allOf" in schema && Array.isArray(schema.allOf)) {
61
+ // If any subschema validates only strings, then the overall schema validates only strings
62
+ return schema.allOf.some((subschema) => validatesOnlyStrings(subschema));
63
+ }
64
+ if (("anyOf" in schema && Array.isArray(schema.anyOf)) ||
65
+ ("oneOf" in schema && Array.isArray(schema.oneOf))) {
66
+ const subschemas = ("anyOf" in schema ? schema.anyOf : schema.oneOf);
67
+ // All subschemas must validate only strings
68
+ return (subschemas.length > 0 &&
69
+ subschemas.every((subschema) => validatesOnlyStrings(subschema)));
70
+ }
71
+ // 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.
72
+ if ("not" in schema) {
73
+ return false; // The not case can validate non-strings
74
+ }
75
+ if ("$ref" in schema && typeof schema.$ref === "string") {
76
+ const ref = schema.$ref;
77
+ const resolved = (0, json_schema_1.dereference)(schema);
78
+ if (resolved[ref]) {
79
+ return validatesOnlyStrings(resolved[ref]);
80
+ }
81
+ return false;
82
+ }
83
+ // ignore recursive refs and other cases where type is omitted for now
84
+ // ignore other cases for now where type is omitted
85
+ return false;
86
+ }
87
+ exports.validatesOnlyStrings = validatesOnlyStrings;
@@ -1 +1,18 @@
1
+ import type { z } from "zod";
2
+ import { type JsonSchema7Type } from "zod-to-json-schema";
3
+ export type JSONSchema = JsonSchema7Type;
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 declare function toJsonSchema(schema: z.ZodType | JSONSchema): JSONSchema;
11
+ /**
12
+ * Validates if a JSON schema validates only strings. May return false negatives in some edge cases
13
+ * (like recursive or unresolvable refs).
14
+ *
15
+ * @param schema - The schema to validate.
16
+ * @returns `true` if the schema validates only strings, `false` otherwise.
17
+ */
18
+ export declare function validatesOnlyStrings(schema: unknown): boolean;
@@ -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, any>} input
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
- input) {
13
- // Check for a characteristic method of Zod schemas
14
- return typeof input?.parse === "function";
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 { type z } from "zod";
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, any>} input
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, any> = Record<string, any>>(input: z.ZodType<RunOutput> | Record<string, any>): input is z.ZodType<RunOutput>;
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, any>} input
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
- input) {
10
- // Check for a characteristic method of Zod schemas
11
- return typeof input?.parse === "function";
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.42",
3
+ "version": "0.3.44",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {