@langchain/core 0.3.22 → 0.3.23

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.
Files changed (64) hide show
  1. package/dist/callbacks/base.cjs +9 -1
  2. package/dist/callbacks/base.d.ts +1 -0
  3. package/dist/callbacks/base.js +7 -0
  4. package/dist/callbacks/manager.cjs +18 -0
  5. package/dist/callbacks/manager.js +19 -1
  6. package/dist/context.cjs +13 -123
  7. package/dist/context.d.ts +2 -101
  8. package/dist/context.js +9 -119
  9. package/dist/messages/tool.cjs +14 -1
  10. package/dist/messages/tool.d.ts +13 -1
  11. package/dist/messages/tool.js +12 -0
  12. package/dist/output_parsers/transform.cjs +2 -2
  13. package/dist/output_parsers/transform.js +1 -1
  14. package/dist/singletons/async_local_storage/context.cjs +189 -0
  15. package/dist/singletons/async_local_storage/context.d.ts +155 -0
  16. package/dist/singletons/async_local_storage/context.js +182 -0
  17. package/dist/singletons/async_local_storage/globals.cjs +2 -1
  18. package/dist/singletons/async_local_storage/globals.d.ts +2 -1
  19. package/dist/singletons/async_local_storage/globals.js +1 -0
  20. package/dist/singletons/async_local_storage/index.cjs +4 -5
  21. package/dist/singletons/async_local_storage/index.d.ts +0 -1
  22. package/dist/singletons/async_local_storage/index.js +1 -2
  23. package/dist/singletons/callbacks.cjs +6 -4
  24. package/dist/singletons/callbacks.js +6 -4
  25. package/dist/singletons/index.cjs +3 -3
  26. package/dist/singletons/index.d.ts +4 -2
  27. package/dist/singletons/index.js +4 -3
  28. package/dist/tools/index.cjs +12 -10
  29. package/dist/tools/index.d.ts +11 -8
  30. package/dist/tools/index.js +13 -11
  31. package/dist/utils/json_schema.cjs +3 -3
  32. package/dist/utils/json_schema.d.ts +1 -1
  33. package/dist/utils/json_schema.js +1 -1
  34. package/package.json +2 -1
  35. package/dist/utils/@cfworker/json-schema/index.cjs +0 -17
  36. package/dist/utils/@cfworker/json-schema/index.d.ts +0 -1
  37. package/dist/utils/@cfworker/json-schema/index.js +0 -1
  38. package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
  39. package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
  40. package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
  41. package/dist/utils/@cfworker/json-schema/src/dereference.cjs +0 -171
  42. package/dist/utils/@cfworker/json-schema/src/dereference.d.ts +0 -12
  43. package/dist/utils/@cfworker/json-schema/src/dereference.js +0 -167
  44. package/dist/utils/@cfworker/json-schema/src/format.cjs +0 -139
  45. package/dist/utils/@cfworker/json-schema/src/format.d.ts +0 -2
  46. package/dist/utils/@cfworker/json-schema/src/format.js +0 -136
  47. package/dist/utils/@cfworker/json-schema/src/index.cjs +0 -24
  48. package/dist/utils/@cfworker/json-schema/src/index.d.ts +0 -8
  49. package/dist/utils/@cfworker/json-schema/src/index.js +0 -8
  50. package/dist/utils/@cfworker/json-schema/src/pointer.cjs +0 -11
  51. package/dist/utils/@cfworker/json-schema/src/pointer.d.ts +0 -2
  52. package/dist/utils/@cfworker/json-schema/src/pointer.js +0 -6
  53. package/dist/utils/@cfworker/json-schema/src/types.cjs +0 -2
  54. package/dist/utils/@cfworker/json-schema/src/types.d.ts +0 -72
  55. package/dist/utils/@cfworker/json-schema/src/types.js +0 -1
  56. package/dist/utils/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
  57. package/dist/utils/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
  58. package/dist/utils/@cfworker/json-schema/src/ucs2-length.js +0 -24
  59. package/dist/utils/@cfworker/json-schema/src/validate.cjs +0 -808
  60. package/dist/utils/@cfworker/json-schema/src/validate.d.ts +0 -3
  61. package/dist/utils/@cfworker/json-schema/src/validate.js +0 -804
  62. package/dist/utils/@cfworker/json-schema/src/validator.cjs +0 -44
  63. package/dist/utils/@cfworker/json-schema/src/validator.d.ts +0 -10
  64. package/dist/utils/@cfworker/json-schema/src/validator.js +0 -40
@@ -1,3 +1,4 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { AsyncLocalStorageProviderSingleton, _CONTEXT_VARIABLES_KEY, MockAsyncLocalStorage, } from "./async_local_storage/index.js";
3
- export { AsyncLocalStorageProviderSingleton, _CONTEXT_VARIABLES_KEY, MockAsyncLocalStorage, };
1
+ import { AsyncLocalStorageProviderSingleton, MockAsyncLocalStorage, } from "./async_local_storage/index.js";
2
+ import { _CONTEXT_VARIABLES_KEY } from "./async_local_storage/globals.js";
3
+ export { AsyncLocalStorageProviderSingleton, MockAsyncLocalStorage, };
4
+ export { _CONTEXT_VARIABLES_KEY };
@@ -60,21 +60,23 @@ class StructuredTool extends base_js_1.BaseLangChain {
60
60
  async invoke(input, config) {
61
61
  let tool_call_id;
62
62
  let toolInput;
63
+ let enrichedConfig = (0, config_js_1.ensureConfig)(config);
63
64
  if ((0, utils_js_1._isToolCall)(input)) {
64
65
  tool_call_id = input.id;
65
66
  toolInput = input.args;
67
+ enrichedConfig = {
68
+ ...enrichedConfig,
69
+ toolCall: input,
70
+ configurable: {
71
+ ...enrichedConfig.configurable,
72
+ tool_call_id,
73
+ },
74
+ };
66
75
  }
67
76
  else {
68
77
  toolInput = input;
69
78
  }
70
- const ensuredConfig = (0, config_js_1.ensureConfig)(config);
71
- return this.call(toolInput, {
72
- ...ensuredConfig,
73
- configurable: {
74
- ...ensuredConfig.configurable,
75
- tool_call_id,
76
- },
77
- });
79
+ return this.call(toolInput, enrichedConfig);
78
80
  }
79
81
  /**
80
82
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -103,7 +105,7 @@ class StructuredTool extends base_js_1.BaseLangChain {
103
105
  throw new utils_js_1.ToolInputParsingException(message, JSON.stringify(arg));
104
106
  }
105
107
  const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
106
- const callbackManager_ = await manager_js_1.CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
108
+ const callbackManager_ = manager_js_1.CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
107
109
  const runManager = await callbackManager_?.handleToolStart(this.toJSON(), typeof parsed === "string" ? parsed : JSON.stringify(parsed), config.runId, undefined, undefined, undefined, config.runName);
108
110
  delete config.runId;
109
111
  let result;
@@ -353,7 +355,7 @@ function tool(func, fields) {
353
355
  exports.tool = tool;
354
356
  function _formatToolOutput(params) {
355
357
  const { content, artifact, toolCallId } = params;
356
- if (toolCallId) {
358
+ if (toolCallId && !(0, tool_js_1.isDirectToolOutput)(content)) {
357
359
  if (typeof content === "string" ||
358
360
  (Array.isArray(content) &&
359
361
  content.every((item) => typeof item === "object"))) {
@@ -32,6 +32,9 @@ export interface ToolParams extends BaseLangChainParams {
32
32
  */
33
33
  verboseParsingErrors?: boolean;
34
34
  }
35
+ export type ToolRunnableConfig<ConfigurableFieldType extends Record<string, any> = Record<string, any>> = RunnableConfig<ConfigurableFieldType> & {
36
+ toolCall?: ToolCall;
37
+ };
35
38
  /**
36
39
  * Schema for defining tools.
37
40
  *
@@ -94,7 +97,7 @@ export declare abstract class StructuredTool<T extends ZodObjectAny = ZodObjectA
94
97
  */
95
98
  responseFormat?: ResponseFormat;
96
99
  constructor(fields?: ToolParams);
97
- protected abstract _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
100
+ protected abstract _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun, parentConfig?: ToolRunnableConfig): Promise<ToolReturnType>;
98
101
  /**
99
102
  * Invokes the tool with the provided input and configuration.
100
103
  * @param input The input for the tool.
@@ -113,7 +116,7 @@ export declare abstract class StructuredTool<T extends ZodObjectAny = ZodObjectA
113
116
  * @param tags Optional tags for the tool.
114
117
  * @returns A Promise that resolves with a string.
115
118
  */
116
- call(arg: (z.output<T> extends string ? string : never) | z.input<T> | ToolCall, configArg?: Callbacks | RunnableConfig,
119
+ call(arg: (z.output<T> extends string ? string : never) | z.input<T>, configArg?: Callbacks | ToolRunnableConfig,
117
120
  /** @deprecated */
118
121
  tags?: string[]): Promise<ToolReturnType>;
119
122
  }
@@ -163,7 +166,7 @@ export interface BaseDynamicToolInput extends ToolParams {
163
166
  * Interface for the input parameters of the DynamicTool class.
164
167
  */
165
168
  export interface DynamicToolInput extends BaseDynamicToolInput {
166
- func: (input: string, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
169
+ func: (input: string, runManager?: CallbackManagerForToolRun, config?: ToolRunnableConfig) => Promise<ToolReturnType>;
167
170
  }
168
171
  /**
169
172
  * Interface for the input parameters of the DynamicStructuredTool class.
@@ -184,9 +187,9 @@ export declare class DynamicTool extends Tool {
184
187
  /**
185
188
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
186
189
  */
187
- call(arg: string | undefined | z.input<this["schema"]> | ToolCall, configArg?: RunnableConfig | Callbacks): Promise<ToolReturnType>;
190
+ call(arg: string | undefined | z.input<this["schema"]> | ToolCall, configArg?: ToolRunnableConfig | Callbacks): Promise<ToolReturnType>;
188
191
  /** @ignore */
189
- _call(input: string, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
192
+ _call(input: string, runManager?: CallbackManagerForToolRun, parentConfig?: ToolRunnableConfig): Promise<ToolReturnType>;
190
193
  }
191
194
  /**
192
195
  * A tool that can be created dynamically from a function, name, and
@@ -272,6 +275,6 @@ interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString | Record
272
275
  *
273
276
  * @returns {DynamicStructuredTool<T>} A new StructuredTool instance.
274
277
  */
275
- export declare function tool<T extends z.ZodString>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicTool;
276
- export declare function tool<T extends ZodObjectAny>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
277
- export declare function tool<T extends Record<string, any>>(func: RunnableFunc<T, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
278
+ export declare function tool<T extends z.ZodString>(func: RunnableFunc<z.output<T>, ToolReturnType, ToolRunnableConfig>, fields: ToolWrapperParams<T>): DynamicTool;
279
+ export declare function tool<T extends ZodObjectAny>(func: RunnableFunc<z.output<T>, ToolReturnType, ToolRunnableConfig>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
280
+ export declare function tool<T extends Record<string, any>>(func: RunnableFunc<T, ToolReturnType, ToolRunnableConfig>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
@@ -2,7 +2,7 @@ import { z } from "zod";
2
2
  import { CallbackManager, parseCallbackConfigArg, } from "../callbacks/manager.js";
3
3
  import { BaseLangChain, } from "../language_models/base.js";
4
4
  import { ensureConfig, patchConfig, pickRunnableConfigKeys, } from "../runnables/config.js";
5
- import { ToolMessage } from "../messages/tool.js";
5
+ import { isDirectToolOutput, ToolMessage } from "../messages/tool.js";
6
6
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
7
7
  import { _isToolCall, ToolInputParsingException } from "./utils.js";
8
8
  import { isZodSchema } from "../utils/types/is_zod_schema.js";
@@ -57,21 +57,23 @@ export class StructuredTool extends BaseLangChain {
57
57
  async invoke(input, config) {
58
58
  let tool_call_id;
59
59
  let toolInput;
60
+ let enrichedConfig = ensureConfig(config);
60
61
  if (_isToolCall(input)) {
61
62
  tool_call_id = input.id;
62
63
  toolInput = input.args;
64
+ enrichedConfig = {
65
+ ...enrichedConfig,
66
+ toolCall: input,
67
+ configurable: {
68
+ ...enrichedConfig.configurable,
69
+ tool_call_id,
70
+ },
71
+ };
63
72
  }
64
73
  else {
65
74
  toolInput = input;
66
75
  }
67
- const ensuredConfig = ensureConfig(config);
68
- return this.call(toolInput, {
69
- ...ensuredConfig,
70
- configurable: {
71
- ...ensuredConfig.configurable,
72
- tool_call_id,
73
- },
74
- });
76
+ return this.call(toolInput, enrichedConfig);
75
77
  }
76
78
  /**
77
79
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -100,7 +102,7 @@ export class StructuredTool extends BaseLangChain {
100
102
  throw new ToolInputParsingException(message, JSON.stringify(arg));
101
103
  }
102
104
  const config = parseCallbackConfigArg(configArg);
103
- const callbackManager_ = await CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
105
+ const callbackManager_ = CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
104
106
  const runManager = await callbackManager_?.handleToolStart(this.toJSON(), typeof parsed === "string" ? parsed : JSON.stringify(parsed), config.runId, undefined, undefined, undefined, config.runName);
105
107
  delete config.runId;
106
108
  let result;
@@ -344,7 +346,7 @@ export function tool(func, fields) {
344
346
  }
345
347
  function _formatToolOutput(params) {
346
348
  const { content, artifact, toolCallId } = params;
347
- if (toolCallId) {
349
+ if (toolCallId && !isDirectToolOutput(content)) {
348
350
  if (typeof content === "string" ||
349
351
  (Array.isArray(content) &&
350
352
  content.every((item) => typeof item === "object"))) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Validator = exports.deepCompareStrict = void 0;
4
- var index_js_1 = require("./@cfworker/json-schema/index.cjs");
5
- Object.defineProperty(exports, "deepCompareStrict", { enumerable: true, get: function () { return index_js_1.deepCompareStrict; } });
6
- Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return index_js_1.Validator; } });
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; } });
@@ -1 +1 @@
1
- export { deepCompareStrict, Validator } from "./@cfworker/json-schema/index.js";
1
+ export { deepCompareStrict, Validator } from "@cfworker/json-schema";
@@ -1 +1 @@
1
- export { deepCompareStrict, Validator } from "./@cfworker/json-schema/index.js";
1
+ export { deepCompareStrict, Validator } from "@cfworker/json-schema";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.22",
3
+ "version": "0.3.23",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -33,6 +33,7 @@
33
33
  "author": "LangChain",
34
34
  "license": "MIT",
35
35
  "dependencies": {
36
+ "@cfworker/json-schema": "^4.0.2",
36
37
  "ansi-styles": "^5.0.0",
37
38
  "camelcase": "6",
38
39
  "decamelize": "1.2.0",
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./src/index.cjs"), exports);
@@ -1 +0,0 @@
1
- export * from "./src/index.js";
@@ -1 +0,0 @@
1
- export * from "./src/index.js";
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deepCompareStrict = void 0;
4
- function deepCompareStrict(a, b) {
5
- const typeofa = typeof a;
6
- if (typeofa !== typeof b) {
7
- return false;
8
- }
9
- if (Array.isArray(a)) {
10
- if (!Array.isArray(b)) {
11
- return false;
12
- }
13
- const length = a.length;
14
- if (length !== b.length) {
15
- return false;
16
- }
17
- for (let i = 0; i < length; i++) {
18
- if (!deepCompareStrict(a[i], b[i])) {
19
- return false;
20
- }
21
- }
22
- return true;
23
- }
24
- if (typeofa === "object") {
25
- if (!a || !b) {
26
- return a === b;
27
- }
28
- const aKeys = Object.keys(a);
29
- const bKeys = Object.keys(b);
30
- const length = aKeys.length;
31
- if (length !== bKeys.length) {
32
- return false;
33
- }
34
- for (const k of aKeys) {
35
- if (!deepCompareStrict(a[k], b[k])) {
36
- return false;
37
- }
38
- }
39
- return true;
40
- }
41
- return a === b;
42
- }
43
- exports.deepCompareStrict = deepCompareStrict;
@@ -1 +0,0 @@
1
- export declare function deepCompareStrict(a: any, b: any): boolean;
@@ -1,39 +0,0 @@
1
- export function deepCompareStrict(a, b) {
2
- const typeofa = typeof a;
3
- if (typeofa !== typeof b) {
4
- return false;
5
- }
6
- if (Array.isArray(a)) {
7
- if (!Array.isArray(b)) {
8
- return false;
9
- }
10
- const length = a.length;
11
- if (length !== b.length) {
12
- return false;
13
- }
14
- for (let i = 0; i < length; i++) {
15
- if (!deepCompareStrict(a[i], b[i])) {
16
- return false;
17
- }
18
- }
19
- return true;
20
- }
21
- if (typeofa === "object") {
22
- if (!a || !b) {
23
- return a === b;
24
- }
25
- const aKeys = Object.keys(a);
26
- const bKeys = Object.keys(b);
27
- const length = aKeys.length;
28
- if (length !== bKeys.length) {
29
- return false;
30
- }
31
- for (const k of aKeys) {
32
- if (!deepCompareStrict(a[k], b[k])) {
33
- return false;
34
- }
35
- }
36
- return true;
37
- }
38
- return a === b;
39
- }
@@ -1,171 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.dereference = exports.initialBaseURI = exports.ignoredKeyword = exports.schemaMapKeyword = exports.schemaArrayKeyword = exports.schemaKeyword = void 0;
4
- const pointer_js_1 = require("./pointer.cjs");
5
- exports.schemaKeyword = {
6
- additionalItems: true,
7
- unevaluatedItems: true,
8
- items: true,
9
- contains: true,
10
- additionalProperties: true,
11
- unevaluatedProperties: true,
12
- propertyNames: true,
13
- not: true,
14
- if: true,
15
- then: true,
16
- else: true,
17
- };
18
- exports.schemaArrayKeyword = {
19
- prefixItems: true,
20
- items: true,
21
- allOf: true,
22
- anyOf: true,
23
- oneOf: true,
24
- };
25
- exports.schemaMapKeyword = {
26
- $defs: true,
27
- definitions: true,
28
- properties: true,
29
- patternProperties: true,
30
- dependentSchemas: true,
31
- };
32
- exports.ignoredKeyword = {
33
- id: true,
34
- $id: true,
35
- $ref: true,
36
- $schema: true,
37
- $anchor: true,
38
- $vocabulary: true,
39
- $comment: true,
40
- default: true,
41
- enum: true,
42
- const: true,
43
- required: true,
44
- type: true,
45
- maximum: true,
46
- minimum: true,
47
- exclusiveMaximum: true,
48
- exclusiveMinimum: true,
49
- multipleOf: true,
50
- maxLength: true,
51
- minLength: true,
52
- pattern: true,
53
- format: true,
54
- maxItems: true,
55
- minItems: true,
56
- uniqueItems: true,
57
- maxProperties: true,
58
- minProperties: true,
59
- };
60
- /**
61
- * Default base URI for schemas without an $id.
62
- * https://json-schema.org/draft/2019-09/json-schema-core.html#initial-base
63
- * https://tools.ietf.org/html/rfc3986#section-5.1
64
- */
65
- exports.initialBaseURI =
66
- // @ts-ignore
67
- typeof self !== "undefined" &&
68
- self.location &&
69
- self.location.origin !== "null"
70
- ? //@ts-ignore
71
- /* #__PURE__ */ new URL(self.location.origin + self.location.pathname + location.search)
72
- : /* #__PURE__ */ new URL("https://github.com/cfworker");
73
- function dereference(schema, lookup = Object.create(null), baseURI = exports.initialBaseURI, basePointer = "") {
74
- if (schema && typeof schema === "object" && !Array.isArray(schema)) {
75
- const id = schema.$id || schema.id;
76
- if (id) {
77
- const url = new URL(id, baseURI.href);
78
- if (url.hash.length > 1) {
79
- lookup[url.href] = schema;
80
- }
81
- else {
82
- url.hash = ""; // normalize hash https://url.spec.whatwg.org/#dom-url-hash
83
- if (basePointer === "") {
84
- baseURI = url;
85
- }
86
- else {
87
- dereference(schema, lookup, baseURI);
88
- }
89
- }
90
- }
91
- }
92
- else if (schema !== true && schema !== false) {
93
- return lookup;
94
- }
95
- // compute the schema's URI and add it to the mapping.
96
- const schemaURI = baseURI.href + (basePointer ? "#" + basePointer : "");
97
- if (lookup[schemaURI] !== undefined) {
98
- throw new Error(`Duplicate schema URI "${schemaURI}".`);
99
- }
100
- lookup[schemaURI] = schema;
101
- // exit early if this is a boolean schema.
102
- if (schema === true || schema === false) {
103
- return lookup;
104
- }
105
- // set the schema's absolute URI.
106
- if (schema.__absolute_uri__ === undefined) {
107
- Object.defineProperty(schema, "__absolute_uri__", {
108
- enumerable: false,
109
- value: schemaURI,
110
- });
111
- }
112
- // if a $ref is found, resolve it's absolute URI.
113
- if (schema.$ref && schema.__absolute_ref__ === undefined) {
114
- const url = new URL(schema.$ref, baseURI.href);
115
- url.hash = url.hash; // normalize hash https://url.spec.whatwg.org/#dom-url-hash
116
- Object.defineProperty(schema, "__absolute_ref__", {
117
- enumerable: false,
118
- value: url.href,
119
- });
120
- }
121
- // if a $recursiveRef is found, resolve it's absolute URI.
122
- if (schema.$recursiveRef && schema.__absolute_recursive_ref__ === undefined) {
123
- const url = new URL(schema.$recursiveRef, baseURI.href);
124
- url.hash = url.hash; // normalize hash https://url.spec.whatwg.org/#dom-url-hash
125
- Object.defineProperty(schema, "__absolute_recursive_ref__", {
126
- enumerable: false,
127
- value: url.href,
128
- });
129
- }
130
- // if an $anchor is found, compute it's URI and add it to the mapping.
131
- if (schema.$anchor) {
132
- const url = new URL("#" + schema.$anchor, baseURI.href);
133
- lookup[url.href] = schema;
134
- }
135
- // process subschemas.
136
- for (let key in schema) {
137
- if (exports.ignoredKeyword[key]) {
138
- continue;
139
- }
140
- const keyBase = `${basePointer}/${(0, pointer_js_1.encodePointer)(key)}`;
141
- const subSchema = schema[key];
142
- if (Array.isArray(subSchema)) {
143
- if (exports.schemaArrayKeyword[key]) {
144
- const length = subSchema.length;
145
- for (let i = 0; i < length; i++) {
146
- dereference(subSchema[i], lookup, baseURI, `${keyBase}/${i}`);
147
- }
148
- }
149
- }
150
- else if (exports.schemaMapKeyword[key]) {
151
- for (let subKey in subSchema) {
152
- dereference(subSchema[subKey], lookup, baseURI, `${keyBase}/${(0, pointer_js_1.encodePointer)(subKey)}`);
153
- }
154
- }
155
- else {
156
- dereference(subSchema, lookup, baseURI, keyBase);
157
- }
158
- }
159
- return lookup;
160
- }
161
- exports.dereference = dereference;
162
- // schema identification examples
163
- // https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.appendix.A
164
- // $ref delegation
165
- // https://github.com/json-schema-org/json-schema-spec/issues/514
166
- // output format
167
- // https://json-schema.org/draft/2019-09/json-schema-core.html#output
168
- // JSON pointer
169
- // https://tools.ietf.org/html/rfc6901
170
- // JSON relative pointer
171
- // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
@@ -1,12 +0,0 @@
1
- import { Schema } from "./types.js";
2
- export declare const schemaKeyword: Record<string, boolean>;
3
- export declare const schemaArrayKeyword: Record<string, boolean>;
4
- export declare const schemaMapKeyword: Record<string, boolean>;
5
- export declare const ignoredKeyword: Record<string, boolean>;
6
- /**
7
- * Default base URI for schemas without an $id.
8
- * https://json-schema.org/draft/2019-09/json-schema-core.html#initial-base
9
- * https://tools.ietf.org/html/rfc3986#section-5.1
10
- */
11
- export declare let initialBaseURI: URL;
12
- export declare function dereference(schema: Schema | boolean, lookup?: Record<string, Schema | boolean>, baseURI?: URL, basePointer?: string): Record<string, boolean | Schema>;