@langchain/core 0.2.18 → 0.2.20

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 (155) hide show
  1. package/dist/callbacks/manager.cjs +94 -19
  2. package/dist/callbacks/manager.d.ts +3 -1
  3. package/dist/callbacks/manager.js +94 -19
  4. package/dist/language_models/base.d.ts +0 -10
  5. package/dist/language_models/chat_models.cjs +5 -6
  6. package/dist/language_models/chat_models.d.ts +9 -10
  7. package/dist/language_models/chat_models.js +5 -6
  8. package/dist/language_models/llms.cjs +5 -6
  9. package/dist/language_models/llms.d.ts +10 -12
  10. package/dist/language_models/llms.js +5 -6
  11. package/dist/messages/base.cjs +16 -1
  12. package/dist/messages/base.d.ts +9 -0
  13. package/dist/messages/base.js +14 -0
  14. package/dist/messages/tool.cjs +23 -0
  15. package/dist/messages/tool.d.ts +15 -0
  16. package/dist/messages/tool.js +24 -1
  17. package/dist/messages/utils.cjs +1 -1
  18. package/dist/messages/utils.js +1 -1
  19. package/dist/output_parsers/string.cjs +1 -0
  20. package/dist/output_parsers/string.js +1 -0
  21. package/dist/runnables/base.cjs +35 -12
  22. package/dist/runnables/base.js +35 -12
  23. package/dist/runnables/config.cjs +41 -0
  24. package/dist/runnables/config.js +41 -0
  25. package/dist/runnables/remote.cjs +17 -14
  26. package/dist/runnables/remote.js +17 -14
  27. package/dist/runnables/types.d.ts +10 -0
  28. package/dist/tools/index.cjs +15 -6
  29. package/dist/tools/index.d.ts +21 -12
  30. package/dist/tools/index.js +15 -6
  31. package/dist/utils/math.cjs +6 -4
  32. package/dist/utils/math.js +6 -4
  33. package/dist/utils/ml-distance/distances.cjs +18 -0
  34. package/dist/utils/ml-distance/distances.d.ts +8 -0
  35. package/dist/utils/ml-distance/distances.js +14 -0
  36. package/dist/utils/ml-distance/similarities.cjs +21 -0
  37. package/dist/utils/ml-distance/similarities.d.ts +7 -0
  38. package/dist/utils/ml-distance/similarities.js +17 -0
  39. package/dist/utils/ml-distance-euclidean/euclidean.cjs +15 -0
  40. package/dist/utils/ml-distance-euclidean/euclidean.d.ts +2 -0
  41. package/dist/utils/ml-distance-euclidean/euclidean.js +10 -0
  42. package/dist/utils/signal.cjs +28 -0
  43. package/dist/utils/signal.d.ts +1 -0
  44. package/dist/utils/signal.js +24 -0
  45. package/dist/utils/stream.cjs +19 -4
  46. package/dist/utils/stream.d.ts +3 -1
  47. package/dist/utils/stream.js +19 -4
  48. package/dist/utils/testing/index.cjs +168 -4
  49. package/dist/utils/testing/index.d.ts +100 -6
  50. package/dist/utils/testing/index.js +167 -4
  51. package/package.json +4 -4
  52. package/dist/caches/tests/in_memory_cache.test.d.ts +0 -1
  53. package/dist/caches/tests/in_memory_cache.test.js +0 -33
  54. package/dist/callbacks/tests/callbacks.test.d.ts +0 -1
  55. package/dist/callbacks/tests/callbacks.test.js +0 -495
  56. package/dist/callbacks/tests/manager.int.test.d.ts +0 -1
  57. package/dist/callbacks/tests/manager.int.test.js +0 -29
  58. package/dist/callbacks/tests/run_collector.test.d.ts +0 -1
  59. package/dist/callbacks/tests/run_collector.test.js +0 -58
  60. package/dist/language_models/tests/chat_models.test.d.ts +0 -1
  61. package/dist/language_models/tests/chat_models.test.js +0 -204
  62. package/dist/language_models/tests/count_tokens.test.d.ts +0 -1
  63. package/dist/language_models/tests/count_tokens.test.js +0 -19
  64. package/dist/language_models/tests/llms.test.d.ts +0 -1
  65. package/dist/language_models/tests/llms.test.js +0 -52
  66. package/dist/messages/tests/base_message.test.d.ts +0 -1
  67. package/dist/messages/tests/base_message.test.js +0 -245
  68. package/dist/messages/tests/message_utils.test.d.ts +0 -1
  69. package/dist/messages/tests/message_utils.test.js +0 -434
  70. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.d.ts +0 -1
  71. package/dist/output_parsers/openai_tools/tests/json_output_tools_parser.test.js +0 -81
  72. package/dist/output_parsers/tests/json.test.d.ts +0 -1
  73. package/dist/output_parsers/tests/json.test.js +0 -427
  74. package/dist/output_parsers/tests/output_parser.test.d.ts +0 -1
  75. package/dist/output_parsers/tests/output_parser.test.js +0 -78
  76. package/dist/output_parsers/tests/string.test.d.ts +0 -1
  77. package/dist/output_parsers/tests/string.test.js +0 -68
  78. package/dist/output_parsers/tests/structured.test.d.ts +0 -1
  79. package/dist/output_parsers/tests/structured.test.js +0 -166
  80. package/dist/output_parsers/tests/xml.test.d.ts +0 -1
  81. package/dist/output_parsers/tests/xml.test.js +0 -81
  82. package/dist/prompts/tests/chat.mustache.test.d.ts +0 -1
  83. package/dist/prompts/tests/chat.mustache.test.js +0 -129
  84. package/dist/prompts/tests/chat.test.d.ts +0 -1
  85. package/dist/prompts/tests/chat.test.js +0 -557
  86. package/dist/prompts/tests/few_shot.test.d.ts +0 -1
  87. package/dist/prompts/tests/few_shot.test.js +0 -224
  88. package/dist/prompts/tests/pipeline.test.d.ts +0 -1
  89. package/dist/prompts/tests/pipeline.test.js +0 -101
  90. package/dist/prompts/tests/prompt.mustache.test.d.ts +0 -1
  91. package/dist/prompts/tests/prompt.mustache.test.js +0 -105
  92. package/dist/prompts/tests/prompt.test.d.ts +0 -1
  93. package/dist/prompts/tests/prompt.test.js +0 -78
  94. package/dist/prompts/tests/structured.test.d.ts +0 -1
  95. package/dist/prompts/tests/structured.test.js +0 -37
  96. package/dist/prompts/tests/template.test.d.ts +0 -1
  97. package/dist/prompts/tests/template.test.js +0 -24
  98. package/dist/runnables/tests/runnable.test.d.ts +0 -1
  99. package/dist/runnables/tests/runnable.test.js +0 -491
  100. package/dist/runnables/tests/runnable_binding.test.d.ts +0 -1
  101. package/dist/runnables/tests/runnable_binding.test.js +0 -46
  102. package/dist/runnables/tests/runnable_branch.test.d.ts +0 -1
  103. package/dist/runnables/tests/runnable_branch.test.js +0 -116
  104. package/dist/runnables/tests/runnable_graph.test.d.ts +0 -1
  105. package/dist/runnables/tests/runnable_graph.test.js +0 -100
  106. package/dist/runnables/tests/runnable_history.test.d.ts +0 -1
  107. package/dist/runnables/tests/runnable_history.test.js +0 -177
  108. package/dist/runnables/tests/runnable_interface.test.d.ts +0 -1
  109. package/dist/runnables/tests/runnable_interface.test.js +0 -209
  110. package/dist/runnables/tests/runnable_map.test.d.ts +0 -1
  111. package/dist/runnables/tests/runnable_map.test.js +0 -238
  112. package/dist/runnables/tests/runnable_passthrough.test.d.ts +0 -1
  113. package/dist/runnables/tests/runnable_passthrough.test.js +0 -96
  114. package/dist/runnables/tests/runnable_remote.int.test.d.ts +0 -1
  115. package/dist/runnables/tests/runnable_remote.int.test.js +0 -138
  116. package/dist/runnables/tests/runnable_remote.test.d.ts +0 -1
  117. package/dist/runnables/tests/runnable_remote.test.js +0 -200
  118. package/dist/runnables/tests/runnable_retry.test.d.ts +0 -1
  119. package/dist/runnables/tests/runnable_retry.test.js +0 -125
  120. package/dist/runnables/tests/runnable_stream_events.test.d.ts +0 -1
  121. package/dist/runnables/tests/runnable_stream_events.test.js +0 -1013
  122. package/dist/runnables/tests/runnable_stream_events_v2.test.d.ts +0 -1
  123. package/dist/runnables/tests/runnable_stream_events_v2.test.js +0 -2022
  124. package/dist/runnables/tests/runnable_stream_log.test.d.ts +0 -1
  125. package/dist/runnables/tests/runnable_stream_log.test.js +0 -71
  126. package/dist/runnables/tests/runnable_tools.test.d.ts +0 -1
  127. package/dist/runnables/tests/runnable_tools.test.js +0 -149
  128. package/dist/runnables/tests/runnable_tracing.int.test.d.ts +0 -1
  129. package/dist/runnables/tests/runnable_tracing.int.test.js +0 -37
  130. package/dist/runnables/tests/runnable_with_fallbacks.test.d.ts +0 -1
  131. package/dist/runnables/tests/runnable_with_fallbacks.test.js +0 -36
  132. package/dist/singletons/tests/async_local_storage.test.d.ts +0 -1
  133. package/dist/singletons/tests/async_local_storage.test.js +0 -153
  134. package/dist/structured_query/tests/utils.test.d.ts +0 -1
  135. package/dist/structured_query/tests/utils.test.js +0 -47
  136. package/dist/tools/tests/tools.test.d.ts +0 -1
  137. package/dist/tools/tests/tools.test.js +0 -85
  138. package/dist/tracers/tests/langchain_tracer.int.test.d.ts +0 -1
  139. package/dist/tracers/tests/langchain_tracer.int.test.js +0 -74
  140. package/dist/tracers/tests/langsmith_interop.test.d.ts +0 -1
  141. package/dist/tracers/tests/langsmith_interop.test.js +0 -551
  142. package/dist/tracers/tests/tracer.test.d.ts +0 -1
  143. package/dist/tracers/tests/tracer.test.js +0 -378
  144. package/dist/utils/testing/tests/chatfake.test.d.ts +0 -1
  145. package/dist/utils/testing/tests/chatfake.test.js +0 -112
  146. package/dist/utils/tests/async_caller.test.d.ts +0 -1
  147. package/dist/utils/tests/async_caller.test.js +0 -27
  148. package/dist/utils/tests/enviroment.test.d.ts +0 -1
  149. package/dist/utils/tests/enviroment.test.js +0 -6
  150. package/dist/utils/tests/function_calling.test.d.ts +0 -1
  151. package/dist/utils/tests/function_calling.test.js +0 -107
  152. package/dist/utils/tests/math_utils.test.d.ts +0 -1
  153. package/dist/utils/tests/math_utils.test.js +0 -139
  154. package/dist/utils/tests/polyfill_stream.test.d.ts +0 -1
  155. package/dist/utils/tests/polyfill_stream.test.js +0 -15
@@ -141,9 +141,9 @@ export interface DynamicToolInput extends BaseDynamicToolInput {
141
141
  /**
142
142
  * Interface for the input parameters of the DynamicStructuredTool class.
143
143
  */
144
- export interface DynamicStructuredToolInput<T extends ZodObjectAny = ZodObjectAny> extends BaseDynamicToolInput {
145
- func: (input: BaseDynamicToolInput["responseFormat"] extends "content_and_artifact" ? ToolCall : z.infer<T>, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
146
- schema: T;
144
+ export interface DynamicStructuredToolInput<T extends ZodObjectAny | Record<string, any> = ZodObjectAny> extends BaseDynamicToolInput {
145
+ func: (input: BaseDynamicToolInput["responseFormat"] extends "content_and_artifact" ? ToolCall : T extends ZodObjectAny ? z.infer<T> : T, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
146
+ schema: T extends ZodObjectAny ? T : T;
147
147
  }
148
148
  /**
149
149
  * A tool that can be created dynamically from a function, name, and description.
@@ -166,21 +166,24 @@ export declare class DynamicTool extends Tool {
166
166
  * description, designed to work with structured data. It extends the
167
167
  * StructuredTool class and overrides the _call method to execute the
168
168
  * provided function when the tool is called.
169
+ *
170
+ * Schema can be passed as Zod or JSON schema. The tool will not validate
171
+ * input if JSON schema is passed.
169
172
  */
170
- export declare class DynamicStructuredTool<T extends ZodObjectAny = ZodObjectAny> extends StructuredTool<T> {
173
+ export declare class DynamicStructuredTool<T extends ZodObjectAny | Record<string, any> = ZodObjectAny> extends StructuredTool<T extends ZodObjectAny ? T : ZodObjectAny> {
171
174
  static lc_name(): string;
172
175
  name: string;
173
176
  description: string;
174
177
  func: DynamicStructuredToolInput<T>["func"];
175
- schema: T;
178
+ schema: T extends ZodObjectAny ? T : ZodObjectAny;
176
179
  constructor(fields: DynamicStructuredToolInput<T>);
177
180
  /**
178
181
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
179
182
  */
180
- call(arg: z.output<T> | ToolCall, configArg?: RunnableConfig | Callbacks,
183
+ call(arg: (T extends ZodObjectAny ? z.output<T> : T) | ToolCall, configArg?: RunnableConfig | Callbacks,
181
184
  /** @deprecated */
182
185
  tags?: string[]): Promise<ToolReturnType>;
183
- protected _call(arg: z.output<T> | ToolCall, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
186
+ protected _call(arg: (T extends ZodObjectAny ? z.output<T> : T) | ToolCall, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
184
187
  }
185
188
  /**
186
189
  * Abstract base class for toolkits in LangChain. Toolkits are collections
@@ -193,9 +196,11 @@ export declare abstract class BaseToolkit {
193
196
  }
194
197
  /**
195
198
  * Parameters for the tool function.
196
- * @template {ZodObjectAny | z.ZodString = ZodObjectAny} RunInput The input schema for the tool. Either any Zod object, or a Zod string.
199
+ * Schema can be provided as Zod or JSON schema.
200
+ * If you pass JSON schema, tool inputs will not be validated.
201
+ * @template {ZodObjectAny | z.ZodString | Record<string, any> = ZodObjectAny} RunInput The input schema for the tool. Either any Zod object, a Zod string, or JSON schema.
197
202
  */
198
- interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString = ZodObjectAny> extends ToolParams {
203
+ interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString | Record<string, any> = ZodObjectAny> extends ToolParams {
199
204
  /**
200
205
  * The name of the tool. If using with an LLM, this
201
206
  * will be passed as the tool name.
@@ -226,8 +231,11 @@ interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString = ZodObj
226
231
  /**
227
232
  * Creates a new StructuredTool instance with the provided function, name, description, and schema.
228
233
  *
234
+ * Schema can be provided as Zod or JSON schema.
235
+ * If you pass JSON schema, tool inputs will not be validated.
236
+ *
229
237
  * @function
230
- * @template {ZodObjectAny | z.ZodString = ZodObjectAny} T The input schema for the tool. Either any Zod object, or a Zod string.
238
+ * @template {ZodObjectAny | z.ZodString | Record<string, any> = ZodObjectAny} T The input schema for the tool. Either any Zod object, a Zod string, or JSON schema instance.
231
239
  *
232
240
  * @param {RunnableFunc<z.output<T>, ToolReturnType>} func - The function to invoke when the tool is called.
233
241
  * @param {ToolWrapperParams<T>} fields - An object containing the following properties:
@@ -237,5 +245,6 @@ interface ToolWrapperParams<RunInput extends ZodObjectAny | z.ZodString = ZodObj
237
245
  *
238
246
  * @returns {DynamicStructuredTool<T>} A new StructuredTool instance.
239
247
  */
240
- export declare function tool<T extends z.ZodString = z.ZodString>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicTool;
241
- export declare function tool<T extends ZodObjectAny = ZodObjectAny>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
248
+ export declare function tool<T extends z.ZodString>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicTool;
249
+ export declare function tool<T extends ZodObjectAny>(func: RunnableFunc<z.output<T>, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
250
+ export declare function tool<T extends Record<string, any>>(func: RunnableFunc<T, ToolReturnType>, fields: ToolWrapperParams<T>): DynamicStructuredTool<T>;
@@ -5,6 +5,7 @@ import { ensureConfig, patchConfig, } from "../runnables/config.js";
5
5
  import { ToolMessage } from "../messages/tool.js";
6
6
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
7
7
  import { _isToolCall, ToolInputParsingException } from "./utils.js";
8
+ import { isZodSchema } from "../utils/types/is_zod_schema.js";
8
9
  export { ToolInputParsingException };
9
10
  /**
10
11
  * Base class for Tools that accept input of any shape defined by a Zod schema.
@@ -91,6 +92,7 @@ export class StructuredTool extends BaseLangChain {
91
92
  let result;
92
93
  try {
93
94
  result = await this._call(parsed, runManager, config);
95
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
94
96
  }
95
97
  catch (e) {
96
98
  await runManager?.handleToolError(e);
@@ -204,6 +206,9 @@ export class DynamicTool extends Tool {
204
206
  * description, designed to work with structured data. It extends the
205
207
  * StructuredTool class and overrides the _call method to execute the
206
208
  * provided function when the tool is called.
209
+ *
210
+ * Schema can be passed as Zod or JSON schema. The tool will not validate
211
+ * input if JSON schema is passed.
207
212
  */
208
213
  export class DynamicStructuredTool extends StructuredTool {
209
214
  static lc_name() {
@@ -239,7 +244,7 @@ export class DynamicStructuredTool extends StructuredTool {
239
244
  this.description = fields.description;
240
245
  this.func = fields.func;
241
246
  this.returnDirect = fields.returnDirect ?? this.returnDirect;
242
- this.schema = fields.schema;
247
+ this.schema = (isZodSchema(fields.schema) ? fields.schema : z.object({}));
243
248
  }
244
249
  /**
245
250
  * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
@@ -254,6 +259,7 @@ export class DynamicStructuredTool extends StructuredTool {
254
259
  return super.call(arg, config, tags);
255
260
  }
256
261
  _call(arg, runManager, parentConfig) {
262
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
257
263
  return this.func(arg, runManager, parentConfig);
258
264
  }
259
265
  }
@@ -271,18 +277,20 @@ export function tool(func, fields) {
271
277
  // If the schema is not provided, or it's a string schema, create a DynamicTool
272
278
  if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
273
279
  return new DynamicTool({
274
- name: fields.name,
280
+ ...fields,
275
281
  description: fields.description ??
276
282
  fields.schema?.description ??
277
283
  `${fields.name} tool`,
278
- responseFormat: fields.responseFormat,
279
- func,
284
+ // TS doesn't restrict the type here based on the guard above
285
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
286
+ func: func,
280
287
  });
281
288
  }
282
289
  const description = fields.description ?? fields.schema.description ?? `${fields.name} tool`;
283
290
  return new DynamicStructuredTool({
284
- name: fields.name,
291
+ ...fields,
285
292
  description,
293
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
286
294
  schema: fields.schema,
287
295
  // TODO: Consider moving into DynamicStructuredTool constructor
288
296
  func: async (input, runManager, config) => {
@@ -292,6 +300,8 @@ export function tool(func, fields) {
292
300
  });
293
301
  void AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
294
302
  try {
303
+ // TS doesn't restrict the type here based on the guard above
304
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
295
305
  resolve(func(input, childConfig));
296
306
  }
297
307
  catch (e) {
@@ -300,7 +310,6 @@ export function tool(func, fields) {
300
310
  });
301
311
  });
302
312
  },
303
- responseFormat: fields.responseFormat,
304
313
  });
305
314
  }
306
315
  function _formatToolOutput(params) {
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.maximalMarginalRelevance = exports.euclideanDistance = exports.innerProduct = exports.cosineSimilarity = exports.normalize = exports.matrixFunc = void 0;
4
- const ml_distance_1 = require("ml-distance");
4
+ const similarities_js_1 = require("./ml-distance/similarities.cjs");
5
+ const distances_js_1 = require("./ml-distance/distances.cjs");
6
+ const euclidean_js_1 = require("./ml-distance-euclidean/euclidean.cjs");
5
7
  /**
6
8
  * Apply a row-wise function between two matrices with the same number of columns.
7
9
  *
@@ -45,15 +47,15 @@ exports.normalize = normalize;
45
47
  * @returns {number[][] | [[]]} A matrix where each row represents the cosine similarity values between the corresponding rows of X and Y.
46
48
  */
47
49
  function cosineSimilarity(X, Y) {
48
- return matrixFunc(X, Y, ml_distance_1.similarity.cosine);
50
+ return matrixFunc(X, Y, similarities_js_1.cosine);
49
51
  }
50
52
  exports.cosineSimilarity = cosineSimilarity;
51
53
  function innerProduct(X, Y) {
52
- return matrixFunc(X, Y, ml_distance_1.distance.innerProduct);
54
+ return matrixFunc(X, Y, distances_js_1.innerProduct);
53
55
  }
54
56
  exports.innerProduct = innerProduct;
55
57
  function euclideanDistance(X, Y) {
56
- return matrixFunc(X, Y, ml_distance_1.distance.euclidean);
58
+ return matrixFunc(X, Y, euclidean_js_1.euclidean);
57
59
  }
58
60
  exports.euclideanDistance = euclideanDistance;
59
61
  /**
@@ -1,4 +1,6 @@
1
- import { similarity as ml_distance_similarity, distance as ml_distance, } from "ml-distance";
1
+ import { cosine } from "./ml-distance/similarities.js";
2
+ import { innerProduct as innerProductDistance } from "./ml-distance/distances.js";
3
+ import { euclidean } from "./ml-distance-euclidean/euclidean.js";
2
4
  /**
3
5
  * Apply a row-wise function between two matrices with the same number of columns.
4
6
  *
@@ -40,13 +42,13 @@ export function normalize(M, similarity = false) {
40
42
  * @returns {number[][] | [[]]} A matrix where each row represents the cosine similarity values between the corresponding rows of X and Y.
41
43
  */
42
44
  export function cosineSimilarity(X, Y) {
43
- return matrixFunc(X, Y, ml_distance_similarity.cosine);
45
+ return matrixFunc(X, Y, cosine);
44
46
  }
45
47
  export function innerProduct(X, Y) {
46
- return matrixFunc(X, Y, ml_distance.innerProduct);
48
+ return matrixFunc(X, Y, innerProductDistance);
47
49
  }
48
50
  export function euclideanDistance(X, Y) {
49
- return matrixFunc(X, Y, ml_distance.euclidean);
51
+ return matrixFunc(X, Y, euclidean);
50
52
  }
51
53
  /**
52
54
  * This function implements the Maximal Marginal Relevance algorithm
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.innerProduct = void 0;
4
+ /**
5
+ *Returns the Inner Product similarity between vectors a and b
6
+ * @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
7
+ * @param a - first vector
8
+ * @param b - second vector
9
+ *
10
+ */
11
+ function innerProduct(a, b) {
12
+ let ans = 0;
13
+ for (let i = 0; i < a.length; i++) {
14
+ ans += a[i] * b[i];
15
+ }
16
+ return ans;
17
+ }
18
+ exports.innerProduct = innerProduct;
@@ -0,0 +1,8 @@
1
+ /**
2
+ *Returns the Inner Product similarity between vectors a and b
3
+ * @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
4
+ * @param a - first vector
5
+ * @param b - second vector
6
+ *
7
+ */
8
+ export declare function innerProduct(a: number[], b: number[]): number;
@@ -0,0 +1,14 @@
1
+ /**
2
+ *Returns the Inner Product similarity between vectors a and b
3
+ * @link [Inner Product Similarity algorithm](https://www.naun.org/main/NAUN/ijmmas/mmmas-49.pdf)
4
+ * @param a - first vector
5
+ * @param b - second vector
6
+ *
7
+ */
8
+ export function innerProduct(a, b) {
9
+ let ans = 0;
10
+ for (let i = 0; i < a.length; i++) {
11
+ ans += a[i] * b[i];
12
+ }
13
+ return ans;
14
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cosine = void 0;
4
+ /**
5
+ * Returns the average of cosine distances between vectors a and b
6
+ * @param a - first vector
7
+ * @param b - second vector
8
+ *
9
+ */
10
+ function cosine(a, b) {
11
+ let p = 0;
12
+ let p2 = 0;
13
+ let q2 = 0;
14
+ for (let i = 0; i < a.length; i++) {
15
+ p += a[i] * b[i];
16
+ p2 += a[i] * a[i];
17
+ q2 += b[i] * b[i];
18
+ }
19
+ return p / (Math.sqrt(p2) * Math.sqrt(q2));
20
+ }
21
+ exports.cosine = cosine;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns the average of cosine distances between vectors a and b
3
+ * @param a - first vector
4
+ * @param b - second vector
5
+ *
6
+ */
7
+ export declare function cosine(a: number[], b: number[]): number;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Returns the average of cosine distances between vectors a and b
3
+ * @param a - first vector
4
+ * @param b - second vector
5
+ *
6
+ */
7
+ export function cosine(a, b) {
8
+ let p = 0;
9
+ let p2 = 0;
10
+ let q2 = 0;
11
+ for (let i = 0; i < a.length; i++) {
12
+ p += a[i] * b[i];
13
+ p2 += a[i] * a[i];
14
+ q2 += b[i] * b[i];
15
+ }
16
+ return p / (Math.sqrt(p2) * Math.sqrt(q2));
17
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.euclidean = exports.squaredEuclidean = void 0;
4
+ function squaredEuclidean(p, q) {
5
+ let d = 0;
6
+ for (let i = 0; i < p.length; i++) {
7
+ d += (p[i] - q[i]) * (p[i] - q[i]);
8
+ }
9
+ return d;
10
+ }
11
+ exports.squaredEuclidean = squaredEuclidean;
12
+ function euclidean(p, q) {
13
+ return Math.sqrt(squaredEuclidean(p, q));
14
+ }
15
+ exports.euclidean = euclidean;
@@ -0,0 +1,2 @@
1
+ export declare function squaredEuclidean(p: number[], q: number[]): number;
2
+ export declare function euclidean(p: number[], q: number[]): number;
@@ -0,0 +1,10 @@
1
+ export function squaredEuclidean(p, q) {
2
+ let d = 0;
3
+ for (let i = 0; i < p.length; i++) {
4
+ d += (p[i] - q[i]) * (p[i] - q[i]);
5
+ }
6
+ return d;
7
+ }
8
+ export function euclidean(p, q) {
9
+ return Math.sqrt(squaredEuclidean(p, q));
10
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.raceWithSignal = void 0;
4
+ async function raceWithSignal(promise, signal) {
5
+ if (signal === undefined) {
6
+ return promise;
7
+ }
8
+ return Promise.race([
9
+ promise.catch((err) => {
10
+ if (!signal?.aborted) {
11
+ throw err;
12
+ }
13
+ else {
14
+ return undefined;
15
+ }
16
+ }),
17
+ new Promise((_, reject) => {
18
+ signal.addEventListener("abort", () => {
19
+ reject(new Error("Aborted"));
20
+ });
21
+ // Must be here inside the promise to avoid a race condition
22
+ if (signal.aborted) {
23
+ reject(new Error("Aborted"));
24
+ }
25
+ }),
26
+ ]);
27
+ }
28
+ exports.raceWithSignal = raceWithSignal;
@@ -0,0 +1 @@
1
+ export declare function raceWithSignal<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
@@ -0,0 +1,24 @@
1
+ export async function raceWithSignal(promise, signal) {
2
+ if (signal === undefined) {
3
+ return promise;
4
+ }
5
+ return Promise.race([
6
+ promise.catch((err) => {
7
+ if (!signal?.aborted) {
8
+ throw err;
9
+ }
10
+ else {
11
+ return undefined;
12
+ }
13
+ }),
14
+ new Promise((_, reject) => {
15
+ signal.addEventListener("abort", () => {
16
+ reject(new Error("Aborted"));
17
+ });
18
+ // Must be here inside the promise to avoid a race condition
19
+ if (signal.aborted) {
20
+ reject(new Error("Aborted"));
21
+ }
22
+ }),
23
+ ]);
24
+ }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.pipeGeneratorWithSetup = exports.AsyncGeneratorWithSetup = exports.concat = exports.atee = exports.IterableReadableStream = void 0;
4
4
  const index_js_1 = require("../singletons/index.cjs");
5
+ const signal_js_1 = require("./signal.cjs");
5
6
  /*
6
7
  * Support async iterator syntax for ReadableStreams in all environments.
7
8
  * Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
@@ -187,6 +188,12 @@ class AsyncGeneratorWithSetup {
187
188
  writable: true,
188
189
  value: void 0
189
190
  });
191
+ Object.defineProperty(this, "signal", {
192
+ enumerable: true,
193
+ configurable: true,
194
+ writable: true,
195
+ value: void 0
196
+ });
190
197
  Object.defineProperty(this, "firstResult", {
191
198
  enumerable: true,
192
199
  configurable: true,
@@ -201,6 +208,8 @@ class AsyncGeneratorWithSetup {
201
208
  });
202
209
  this.generator = params.generator;
203
210
  this.config = params.config;
211
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
212
+ this.signal = params.signal ?? this.config?.signal;
204
213
  // setup is a promise that resolves only after the first iterator value
205
214
  // is available. this is useful when setup of several piped generators
206
215
  // needs to happen in logical order, ie. in the order in which input to
@@ -218,13 +227,18 @@ class AsyncGeneratorWithSetup {
218
227
  });
219
228
  }
220
229
  async next(...args) {
230
+ this.signal?.throwIfAborted();
221
231
  if (!this.firstResultUsed) {
222
232
  this.firstResultUsed = true;
223
233
  return this.firstResult;
224
234
  }
225
- return index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(this.config, async () => {
226
- return this.generator.next(...args);
227
- }, true);
235
+ return index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(this.config, this.signal
236
+ ? async () => {
237
+ return (0, signal_js_1.raceWithSignal)(this.generator.next(...args), this.signal);
238
+ }
239
+ : async () => {
240
+ return this.generator.next(...args);
241
+ }, true);
228
242
  }
229
243
  async return(value) {
230
244
  return this.generator.return(value);
@@ -237,10 +251,11 @@ class AsyncGeneratorWithSetup {
237
251
  }
238
252
  }
239
253
  exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
240
- async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
254
+ async function pipeGeneratorWithSetup(to, generator, startSetup, signal, ...args) {
241
255
  const gen = new AsyncGeneratorWithSetup({
242
256
  generator,
243
257
  startSetup,
258
+ signal,
244
259
  });
245
260
  const setup = await gen.setup;
246
261
  return { output: to(gen, setup, ...args), setup };
@@ -15,19 +15,21 @@ export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn =
15
15
  private generator;
16
16
  setup: Promise<S>;
17
17
  config?: unknown;
18
+ signal?: AbortSignal;
18
19
  private firstResult;
19
20
  private firstResultUsed;
20
21
  constructor(params: {
21
22
  generator: AsyncGenerator<T>;
22
23
  startSetup?: () => Promise<S>;
23
24
  config?: unknown;
25
+ signal?: AbortSignal;
24
26
  });
25
27
  next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
26
28
  return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
27
29
  throw(e: Error): Promise<IteratorResult<T>>;
28
30
  [Symbol.asyncIterator](): this;
29
31
  }
30
- export declare function pipeGeneratorWithSetup<S, A extends unknown[], T, TReturn, TNext, U, UReturn, UNext>(to: (g: AsyncGenerator<T, TReturn, TNext>, s: S, ...args: A) => AsyncGenerator<U, UReturn, UNext>, generator: AsyncGenerator<T, TReturn, TNext>, startSetup: () => Promise<S>, ...args: A): Promise<{
32
+ export declare function pipeGeneratorWithSetup<S, A extends unknown[], T, TReturn, TNext, U, UReturn, UNext>(to: (g: AsyncGenerator<T, TReturn, TNext>, s: S, ...args: A) => AsyncGenerator<U, UReturn, UNext>, generator: AsyncGenerator<T, TReturn, TNext>, startSetup: () => Promise<S>, signal: AbortSignal | undefined, ...args: A): Promise<{
31
33
  output: AsyncGenerator<U, UReturn, UNext>;
32
34
  setup: Awaited<S>;
33
35
  }>;
@@ -1,4 +1,5 @@
1
1
  import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
2
+ import { raceWithSignal } from "./signal.js";
2
3
  /*
3
4
  * Support async iterator syntax for ReadableStreams in all environments.
4
5
  * Source: https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
@@ -181,6 +182,12 @@ export class AsyncGeneratorWithSetup {
181
182
  writable: true,
182
183
  value: void 0
183
184
  });
185
+ Object.defineProperty(this, "signal", {
186
+ enumerable: true,
187
+ configurable: true,
188
+ writable: true,
189
+ value: void 0
190
+ });
184
191
  Object.defineProperty(this, "firstResult", {
185
192
  enumerable: true,
186
193
  configurable: true,
@@ -195,6 +202,8 @@ export class AsyncGeneratorWithSetup {
195
202
  });
196
203
  this.generator = params.generator;
197
204
  this.config = params.config;
205
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
206
+ this.signal = params.signal ?? this.config?.signal;
198
207
  // setup is a promise that resolves only after the first iterator value
199
208
  // is available. this is useful when setup of several piped generators
200
209
  // needs to happen in logical order, ie. in the order in which input to
@@ -212,13 +221,18 @@ export class AsyncGeneratorWithSetup {
212
221
  });
213
222
  }
214
223
  async next(...args) {
224
+ this.signal?.throwIfAborted();
215
225
  if (!this.firstResultUsed) {
216
226
  this.firstResultUsed = true;
217
227
  return this.firstResult;
218
228
  }
219
- return AsyncLocalStorageProviderSingleton.runWithConfig(this.config, async () => {
220
- return this.generator.next(...args);
221
- }, true);
229
+ return AsyncLocalStorageProviderSingleton.runWithConfig(this.config, this.signal
230
+ ? async () => {
231
+ return raceWithSignal(this.generator.next(...args), this.signal);
232
+ }
233
+ : async () => {
234
+ return this.generator.next(...args);
235
+ }, true);
222
236
  }
223
237
  async return(value) {
224
238
  return this.generator.return(value);
@@ -230,10 +244,11 @@ export class AsyncGeneratorWithSetup {
230
244
  return this;
231
245
  }
232
246
  }
233
- export async function pipeGeneratorWithSetup(to, generator, startSetup, ...args) {
247
+ export async function pipeGeneratorWithSetup(to, generator, startSetup, signal, ...args) {
234
248
  const gen = new AsyncGeneratorWithSetup({
235
249
  generator,
236
250
  startSetup,
251
+ signal,
237
252
  });
238
253
  const setup = await gen.setup;
239
254
  return { output: to(gen, setup, ...args), setup };