@langchain/core 0.2.14 → 0.2.16
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/README.md +1 -1
- package/caches.cjs +1 -1
- package/caches.d.cts +1 -1
- package/caches.d.ts +1 -1
- package/caches.js +1 -1
- package/callbacks/dispatch/web.cjs +1 -0
- package/callbacks/dispatch/web.d.cts +1 -0
- package/callbacks/dispatch/web.d.ts +1 -0
- package/callbacks/dispatch/web.js +1 -0
- package/callbacks/dispatch.cjs +1 -0
- package/callbacks/dispatch.d.cts +1 -0
- package/callbacks/dispatch.d.ts +1 -0
- package/callbacks/dispatch.js +1 -0
- package/dist/{caches.cjs → caches/base.cjs} +6 -6
- package/dist/{caches.d.ts → caches/base.d.ts} +7 -7
- package/dist/{caches.js → caches/base.js} +6 -6
- package/dist/caches/tests/in_memory_cache.test.d.ts +1 -0
- package/dist/caches/tests/in_memory_cache.test.js +33 -0
- package/dist/callbacks/base.cjs +8 -0
- package/dist/callbacks/base.d.ts +16 -10
- package/dist/callbacks/base.js +8 -0
- package/dist/callbacks/dispatch/index.cjs +49 -0
- package/dist/callbacks/dispatch/index.d.ts +35 -0
- package/dist/callbacks/dispatch/index.js +45 -0
- package/dist/callbacks/dispatch/web.cjs +61 -0
- package/dist/callbacks/dispatch/web.d.ts +32 -0
- package/dist/callbacks/dispatch/web.js +57 -0
- package/dist/callbacks/manager.cjs +32 -5
- package/dist/callbacks/manager.d.ts +5 -2
- package/dist/callbacks/manager.js +31 -5
- package/dist/language_models/base.cjs +4 -4
- package/dist/language_models/base.d.ts +2 -2
- package/dist/language_models/base.js +1 -1
- package/dist/language_models/chat_models.d.ts +4 -4
- package/dist/language_models/llms.d.ts +1 -1
- package/dist/language_models/tests/chat_models.test.js +33 -0
- package/dist/load/import_map.cjs +2 -2
- package/dist/load/import_map.d.ts +2 -2
- package/dist/load/import_map.js +2 -2
- package/dist/messages/ai.cjs +2 -0
- package/dist/messages/ai.js +2 -0
- package/dist/messages/base.cjs +45 -5
- package/dist/messages/base.d.ts +1 -0
- package/dist/messages/base.js +43 -4
- package/dist/messages/index.d.ts +1 -1
- package/dist/messages/tests/base_message.test.js +134 -2
- package/dist/messages/tests/message_utils.test.js +54 -2
- package/dist/messages/tool.cjs +31 -0
- package/dist/messages/tool.d.ts +27 -0
- package/dist/messages/tool.js +32 -1
- package/dist/messages/transformers.cjs +1 -0
- package/dist/messages/transformers.js +1 -0
- package/dist/messages/utils.cjs +5 -1
- package/dist/messages/utils.js +5 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +2 -0
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +2 -0
- package/dist/runnables/base.cjs +89 -16
- package/dist/runnables/base.d.ts +51 -0
- package/dist/runnables/base.js +86 -15
- package/dist/runnables/config.cjs +30 -8
- package/dist/runnables/config.js +30 -8
- package/dist/runnables/history.cjs +1 -1
- package/dist/runnables/history.d.ts +1 -3
- package/dist/runnables/history.js +1 -1
- package/dist/runnables/index.cjs +2 -1
- package/dist/runnables/index.d.ts +1 -1
- package/dist/runnables/index.js +1 -1
- package/dist/runnables/tests/runnable.test.js +3 -3
- package/dist/runnables/tests/runnable_stream_events.test.js +1 -1
- package/dist/runnables/tests/runnable_stream_events_v2.test.js +124 -19
- package/dist/runnables/tests/runnable_tools.test.d.ts +1 -0
- package/dist/runnables/tests/runnable_tools.test.js +111 -0
- package/dist/singletons/tests/async_local_storage.test.js +38 -4
- package/dist/{tools.cjs → tools/index.cjs} +130 -14
- package/dist/{tools.d.ts → tools/index.d.ts} +69 -31
- package/dist/{tools.js → tools/index.js} +130 -14
- package/dist/tools/tests/tools.test.d.ts +1 -0
- package/dist/tools/tests/tools.test.js +74 -0
- package/dist/tracers/base.cjs +1 -0
- package/dist/tracers/base.d.ts +1 -1
- package/dist/tracers/base.js +1 -0
- package/dist/tracers/event_stream.cjs +15 -0
- package/dist/tracers/event_stream.d.ts +1 -0
- package/dist/tracers/event_stream.js +15 -0
- package/dist/tracers/initialize.cjs +2 -0
- package/dist/tracers/initialize.d.ts +2 -0
- package/dist/tracers/initialize.js +2 -0
- package/dist/types/zod.cjs +2 -0
- package/dist/types/zod.d.ts +2 -0
- package/dist/types/zod.js +1 -0
- package/dist/utils/callbacks.cjs +17 -0
- package/dist/utils/callbacks.d.ts +1 -0
- package/dist/utils/callbacks.js +13 -0
- package/dist/utils/function_calling.cjs +38 -10
- package/dist/utils/function_calling.d.ts +32 -11
- package/dist/utils/function_calling.js +36 -9
- package/dist/utils/testing/index.cjs +10 -3
- package/dist/utils/testing/index.d.ts +1 -1
- package/dist/utils/testing/index.js +9 -2
- package/package.json +28 -14
- package/tools.cjs +1 -1
- package/tools.d.cts +1 -1
- package/tools.d.ts +1 -1
- package/tools.js +1 -1
- package/dist/utils/beta_warning.cjs +0 -11
- package/dist/utils/beta_warning.d.ts +0 -5
- package/dist/utils/beta_warning.js +0 -7
- package/utils/beta_warning.cjs +0 -1
- package/utils/beta_warning.d.cts +0 -1
- package/utils/beta_warning.d.ts +0 -1
- package/utils/beta_warning.js +0 -1
package/dist/runnables/base.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { type TraceableFunction } from "langsmith/singletons/traceable";
|
|
2
3
|
import type { RunnableInterface, RunnableBatchOptions } from "./types.js";
|
|
3
4
|
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
@@ -10,8 +11,10 @@ import { Run } from "../tracers/base.js";
|
|
|
10
11
|
import { Graph } from "./graph.js";
|
|
11
12
|
export { type RunnableInterface, RunnableBatchOptions };
|
|
12
13
|
export type RunnableFunc<RunInput, RunOutput> = (input: RunInput, options?: ({
|
|
14
|
+
/** @deprecated Use top-level config fields instead. */
|
|
13
15
|
config?: RunnableConfig;
|
|
14
16
|
} & RunnableConfig) | Record<string, any> | (Record<string, any> & {
|
|
17
|
+
/** @deprecated Use top-level config fields instead. */
|
|
15
18
|
config: RunnableConfig;
|
|
16
19
|
} & RunnableConfig)) => RunOutput | Promise<RunOutput>;
|
|
17
20
|
export type RunnableMapLike<RunInput, RunOutput> = {
|
|
@@ -243,6 +246,23 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
243
246
|
onEnd?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
|
|
244
247
|
onError?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
|
|
245
248
|
}): Runnable<RunInput, RunOutput, CallOptions>;
|
|
249
|
+
/**
|
|
250
|
+
* Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
|
|
251
|
+
* which contains the runnable, name, description and schema.
|
|
252
|
+
*
|
|
253
|
+
* @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
|
|
254
|
+
*
|
|
255
|
+
* @param fields
|
|
256
|
+
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
257
|
+
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
258
|
+
* @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
259
|
+
* @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
260
|
+
*/
|
|
261
|
+
asTool<T extends RunInput = RunInput>(fields: {
|
|
262
|
+
name?: string;
|
|
263
|
+
description?: string;
|
|
264
|
+
schema: z.ZodType<T>;
|
|
265
|
+
}): RunnableToolLike<z.ZodType<T>, RunOutput>;
|
|
246
266
|
}
|
|
247
267
|
export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = {
|
|
248
268
|
bound: Runnable<RunInput, RunOutput, CallOptions>;
|
|
@@ -558,3 +578,34 @@ export declare class RunnablePick<RunInput extends Record<string, any> = Record<
|
|
|
558
578
|
transform(generator: AsyncGenerator<RunInput>, options?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
|
|
559
579
|
stream(input: RunInput, options?: Partial<RunnableConfig>): Promise<IterableReadableStream<RunOutput>>;
|
|
560
580
|
}
|
|
581
|
+
export interface RunnableToolLikeArgs<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends Omit<RunnableBindingArgs<z.infer<RunInput>, RunOutput>, "config"> {
|
|
582
|
+
name: string;
|
|
583
|
+
description?: string;
|
|
584
|
+
schema: RunInput;
|
|
585
|
+
config?: RunnableConfig;
|
|
586
|
+
}
|
|
587
|
+
export declare class RunnableToolLike<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends RunnableBinding<z.infer<RunInput>, RunOutput> {
|
|
588
|
+
name: string;
|
|
589
|
+
description?: string;
|
|
590
|
+
schema: RunInput;
|
|
591
|
+
constructor(fields: RunnableToolLikeArgs<RunInput, RunOutput>);
|
|
592
|
+
static lc_name(): string;
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Given a runnable and a Zod schema, convert the runnable to a tool.
|
|
596
|
+
*
|
|
597
|
+
* @template RunInput The input type for the runnable.
|
|
598
|
+
* @template RunOutput The output type for the runnable.
|
|
599
|
+
*
|
|
600
|
+
* @param {Runnable<RunInput, RunOutput>} runnable The runnable to convert to a tool.
|
|
601
|
+
* @param fields
|
|
602
|
+
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
603
|
+
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
604
|
+
* @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
605
|
+
* @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
606
|
+
*/
|
|
607
|
+
export declare function convertRunnableToTool<RunInput, RunOutput>(runnable: Runnable<RunInput, RunOutput>, fields: {
|
|
608
|
+
name?: string;
|
|
609
|
+
description?: string;
|
|
610
|
+
schema: z.ZodType<RunInput>;
|
|
611
|
+
}): RunnableToolLike<z.ZodType<RunInput>, RunOutput>;
|
package/dist/runnables/base.js
CHANGED
|
@@ -2,7 +2,6 @@ import { z } from "zod";
|
|
|
2
2
|
import pRetry from "p-retry";
|
|
3
3
|
import { v4 as uuidv4 } from "uuid";
|
|
4
4
|
import { isTraceableFunction, } from "langsmith/singletons/traceable";
|
|
5
|
-
import { CallbackManager, } from "../callbacks/manager.js";
|
|
6
5
|
import { LogStreamCallbackHandler, RunLog, RunLogPatch, isLogStreamHandler, } from "../tracers/log_stream.js";
|
|
7
6
|
import { EventStreamCallbackHandler, isStreamEventsHandler, } from "../tracers/event_stream.js";
|
|
8
7
|
import { Serializable } from "../load/serializable.js";
|
|
@@ -712,6 +711,21 @@ export class Runnable extends Serializable {
|
|
|
712
711
|
],
|
|
713
712
|
});
|
|
714
713
|
}
|
|
714
|
+
/**
|
|
715
|
+
* Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
|
|
716
|
+
* which contains the runnable, name, description and schema.
|
|
717
|
+
*
|
|
718
|
+
* @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
|
|
719
|
+
*
|
|
720
|
+
* @param fields
|
|
721
|
+
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
722
|
+
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
723
|
+
* @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
724
|
+
* @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
725
|
+
*/
|
|
726
|
+
asTool(fields) {
|
|
727
|
+
return convertRunnableToTool(this, fields);
|
|
728
|
+
}
|
|
715
729
|
}
|
|
716
730
|
/**
|
|
717
731
|
* A runnable that delegates calls to another runnable with a set of kwargs.
|
|
@@ -1560,12 +1574,16 @@ export class RunnableLambda extends Runnable {
|
|
|
1560
1574
|
}
|
|
1561
1575
|
}
|
|
1562
1576
|
}
|
|
1577
|
+
const childConfig = patchConfig(config, {
|
|
1578
|
+
callbacks: runManager?.getChild(),
|
|
1579
|
+
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1580
|
+
});
|
|
1563
1581
|
const output = await new Promise((resolve, reject) => {
|
|
1564
|
-
void AsyncLocalStorageProviderSingleton.getInstance().run(
|
|
1582
|
+
void AsyncLocalStorageProviderSingleton.getInstance().run(childConfig, async () => {
|
|
1565
1583
|
try {
|
|
1566
1584
|
const res = await this.func(finalChunk, {
|
|
1567
|
-
...
|
|
1568
|
-
config,
|
|
1585
|
+
...childConfig,
|
|
1586
|
+
config: childConfig,
|
|
1569
1587
|
});
|
|
1570
1588
|
resolve(res);
|
|
1571
1589
|
}
|
|
@@ -1578,21 +1596,18 @@ export class RunnableLambda extends Runnable {
|
|
|
1578
1596
|
if (config?.recursionLimit === 0) {
|
|
1579
1597
|
throw new Error("Recursion limit reached.");
|
|
1580
1598
|
}
|
|
1581
|
-
const stream = await output.stream(finalChunk,
|
|
1582
|
-
callbacks: runManager?.getChild(),
|
|
1583
|
-
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1584
|
-
}));
|
|
1599
|
+
const stream = await output.stream(finalChunk, childConfig);
|
|
1585
1600
|
for await (const chunk of stream) {
|
|
1586
1601
|
yield chunk;
|
|
1587
1602
|
}
|
|
1588
1603
|
}
|
|
1589
1604
|
else if (isAsyncIterable(output)) {
|
|
1590
|
-
for await (const chunk of consumeAsyncIterableInContext(
|
|
1605
|
+
for await (const chunk of consumeAsyncIterableInContext(childConfig, output)) {
|
|
1591
1606
|
yield chunk;
|
|
1592
1607
|
}
|
|
1593
1608
|
}
|
|
1594
1609
|
else if (isIterableIterator(output)) {
|
|
1595
|
-
for (const chunk of consumeIteratorInContext(
|
|
1610
|
+
for (const chunk of consumeIteratorInContext(childConfig, output)) {
|
|
1596
1611
|
yield chunk;
|
|
1597
1612
|
}
|
|
1598
1613
|
}
|
|
@@ -1661,13 +1676,14 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
1661
1676
|
}
|
|
1662
1677
|
}
|
|
1663
1678
|
async invoke(input, options) {
|
|
1664
|
-
const
|
|
1665
|
-
const
|
|
1666
|
-
const
|
|
1679
|
+
const config = ensureConfig(options);
|
|
1680
|
+
const callbackManager_ = await getCallbackManagerForConfig(options);
|
|
1681
|
+
const { runId, ...otherConfigFields } = config;
|
|
1682
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
|
|
1667
1683
|
let firstError;
|
|
1668
1684
|
for (const runnable of this.runnables()) {
|
|
1669
1685
|
try {
|
|
1670
|
-
const output = await runnable.invoke(input, patchConfig(
|
|
1686
|
+
const output = await runnable.invoke(input, patchConfig(otherConfigFields, { callbacks: runManager?.getChild() }));
|
|
1671
1687
|
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1672
1688
|
return output;
|
|
1673
1689
|
}
|
|
@@ -1688,7 +1704,7 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
1688
1704
|
throw new Error("Not implemented.");
|
|
1689
1705
|
}
|
|
1690
1706
|
const configList = this._getOptionsList(options ?? {}, inputs.length);
|
|
1691
|
-
const callbackManagers = await Promise.all(configList.map((config) =>
|
|
1707
|
+
const callbackManagers = await Promise.all(configList.map((config) => getCallbackManagerForConfig(config)));
|
|
1692
1708
|
const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
|
|
1693
1709
|
const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), configList[i].runId, undefined, undefined, undefined, configList[i].runName);
|
|
1694
1710
|
delete configList[i].runId;
|
|
@@ -1891,3 +1907,58 @@ export class RunnablePick extends Runnable {
|
|
|
1891
1907
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1892
1908
|
}
|
|
1893
1909
|
}
|
|
1910
|
+
export class RunnableToolLike extends RunnableBinding {
|
|
1911
|
+
constructor(fields) {
|
|
1912
|
+
super({
|
|
1913
|
+
bound: fields.bound,
|
|
1914
|
+
config: fields.config ?? {},
|
|
1915
|
+
});
|
|
1916
|
+
Object.defineProperty(this, "name", {
|
|
1917
|
+
enumerable: true,
|
|
1918
|
+
configurable: true,
|
|
1919
|
+
writable: true,
|
|
1920
|
+
value: void 0
|
|
1921
|
+
});
|
|
1922
|
+
Object.defineProperty(this, "description", {
|
|
1923
|
+
enumerable: true,
|
|
1924
|
+
configurable: true,
|
|
1925
|
+
writable: true,
|
|
1926
|
+
value: void 0
|
|
1927
|
+
});
|
|
1928
|
+
Object.defineProperty(this, "schema", {
|
|
1929
|
+
enumerable: true,
|
|
1930
|
+
configurable: true,
|
|
1931
|
+
writable: true,
|
|
1932
|
+
value: void 0
|
|
1933
|
+
});
|
|
1934
|
+
this.name = fields.name;
|
|
1935
|
+
this.description = fields.description;
|
|
1936
|
+
this.schema = fields.schema;
|
|
1937
|
+
}
|
|
1938
|
+
static lc_name() {
|
|
1939
|
+
return "RunnableToolLike";
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
/**
|
|
1943
|
+
* Given a runnable and a Zod schema, convert the runnable to a tool.
|
|
1944
|
+
*
|
|
1945
|
+
* @template RunInput The input type for the runnable.
|
|
1946
|
+
* @template RunOutput The output type for the runnable.
|
|
1947
|
+
*
|
|
1948
|
+
* @param {Runnable<RunInput, RunOutput>} runnable The runnable to convert to a tool.
|
|
1949
|
+
* @param fields
|
|
1950
|
+
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
1951
|
+
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
1952
|
+
* @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
1953
|
+
* @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
1954
|
+
*/
|
|
1955
|
+
export function convertRunnableToTool(runnable, fields) {
|
|
1956
|
+
const name = fields.name ?? runnable.getName();
|
|
1957
|
+
const description = fields.description ?? fields.schema.description;
|
|
1958
|
+
return new RunnableToolLike({
|
|
1959
|
+
name,
|
|
1960
|
+
description,
|
|
1961
|
+
schema: fields.schema,
|
|
1962
|
+
bound: runnable,
|
|
1963
|
+
});
|
|
1964
|
+
}
|
|
@@ -89,25 +89,47 @@ const PRIMITIVES = new Set(["string", "number", "boolean"]);
|
|
|
89
89
|
* should not be called with a default or prepopulated config argument.
|
|
90
90
|
*/
|
|
91
91
|
function ensureConfig(config) {
|
|
92
|
-
const
|
|
92
|
+
const implicitConfig = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
93
93
|
let empty = {
|
|
94
94
|
tags: [],
|
|
95
95
|
metadata: {},
|
|
96
|
-
callbacks: undefined,
|
|
97
96
|
recursionLimit: 25,
|
|
98
97
|
runId: undefined,
|
|
99
98
|
};
|
|
100
|
-
if (
|
|
101
|
-
|
|
99
|
+
if (implicitConfig) {
|
|
100
|
+
// Don't allow runId to be loaded implicitly, as this can cause
|
|
101
|
+
// child runs to improperly inherit their parents' run ids.
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
103
|
+
const { runId, ...rest } = implicitConfig;
|
|
104
|
+
empty = Object.entries(rest).reduce(
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
106
|
+
(currentConfig, [key, value]) => {
|
|
107
|
+
if (value !== undefined) {
|
|
108
|
+
// eslint-disable-next-line no-param-reassign
|
|
109
|
+
currentConfig[key] = value;
|
|
110
|
+
}
|
|
111
|
+
return currentConfig;
|
|
112
|
+
}, empty);
|
|
113
|
+
}
|
|
114
|
+
if (config) {
|
|
115
|
+
empty = Object.entries(config).reduce(
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
|
+
(currentConfig, [key, value]) => {
|
|
118
|
+
if (value !== undefined) {
|
|
119
|
+
// eslint-disable-next-line no-param-reassign
|
|
120
|
+
currentConfig[key] = value;
|
|
121
|
+
}
|
|
122
|
+
return currentConfig;
|
|
123
|
+
}, empty);
|
|
102
124
|
}
|
|
103
|
-
if (
|
|
104
|
-
for (const key of Object.keys(
|
|
105
|
-
if (PRIMITIVES.has(typeof
|
|
125
|
+
if (empty?.configurable) {
|
|
126
|
+
for (const key of Object.keys(empty.configurable)) {
|
|
127
|
+
if (PRIMITIVES.has(typeof empty.configurable[key]) &&
|
|
106
128
|
!empty.metadata?.[key]) {
|
|
107
129
|
if (!empty.metadata) {
|
|
108
130
|
empty.metadata = {};
|
|
109
131
|
}
|
|
110
|
-
empty.metadata[key] =
|
|
132
|
+
empty.metadata[key] = empty.configurable[key];
|
|
111
133
|
}
|
|
112
134
|
}
|
|
113
135
|
}
|
package/dist/runnables/config.js
CHANGED
|
@@ -84,25 +84,47 @@ const PRIMITIVES = new Set(["string", "number", "boolean"]);
|
|
|
84
84
|
* should not be called with a default or prepopulated config argument.
|
|
85
85
|
*/
|
|
86
86
|
export function ensureConfig(config) {
|
|
87
|
-
const
|
|
87
|
+
const implicitConfig = AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
88
88
|
let empty = {
|
|
89
89
|
tags: [],
|
|
90
90
|
metadata: {},
|
|
91
|
-
callbacks: undefined,
|
|
92
91
|
recursionLimit: 25,
|
|
93
92
|
runId: undefined,
|
|
94
93
|
};
|
|
95
|
-
if (
|
|
96
|
-
|
|
94
|
+
if (implicitConfig) {
|
|
95
|
+
// Don't allow runId to be loaded implicitly, as this can cause
|
|
96
|
+
// child runs to improperly inherit their parents' run ids.
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
98
|
+
const { runId, ...rest } = implicitConfig;
|
|
99
|
+
empty = Object.entries(rest).reduce(
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
(currentConfig, [key, value]) => {
|
|
102
|
+
if (value !== undefined) {
|
|
103
|
+
// eslint-disable-next-line no-param-reassign
|
|
104
|
+
currentConfig[key] = value;
|
|
105
|
+
}
|
|
106
|
+
return currentConfig;
|
|
107
|
+
}, empty);
|
|
108
|
+
}
|
|
109
|
+
if (config) {
|
|
110
|
+
empty = Object.entries(config).reduce(
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
+
(currentConfig, [key, value]) => {
|
|
113
|
+
if (value !== undefined) {
|
|
114
|
+
// eslint-disable-next-line no-param-reassign
|
|
115
|
+
currentConfig[key] = value;
|
|
116
|
+
}
|
|
117
|
+
return currentConfig;
|
|
118
|
+
}, empty);
|
|
97
119
|
}
|
|
98
|
-
if (
|
|
99
|
-
for (const key of Object.keys(
|
|
100
|
-
if (PRIMITIVES.has(typeof
|
|
120
|
+
if (empty?.configurable) {
|
|
121
|
+
for (const key of Object.keys(empty.configurable)) {
|
|
122
|
+
if (PRIMITIVES.has(typeof empty.configurable[key]) &&
|
|
101
123
|
!empty.metadata?.[key]) {
|
|
102
124
|
if (!empty.metadata) {
|
|
103
125
|
empty.metadata = {};
|
|
104
126
|
}
|
|
105
|
-
empty.metadata[key] =
|
|
127
|
+
empty.metadata[key] = empty.configurable[key];
|
|
106
128
|
}
|
|
107
129
|
}
|
|
108
130
|
}
|
|
@@ -211,7 +211,7 @@ class RunnableWithMessageHistory extends base_js_1.RunnableBinding {
|
|
|
211
211
|
async _enterHistory(
|
|
212
212
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
213
213
|
input, kwargs) {
|
|
214
|
-
const history = kwargs?.
|
|
214
|
+
const history = kwargs?.configurable?.messageHistory;
|
|
215
215
|
const messages = await history.getMessages();
|
|
216
216
|
if (this.historyMessagesKey === undefined) {
|
|
217
217
|
return messages.concat(this._getInputMessages(input));
|
|
@@ -85,9 +85,7 @@ export declare class RunnableWithMessageHistory<RunInput, RunOutput> extends Run
|
|
|
85
85
|
constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>);
|
|
86
86
|
_getInputMessages(inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;
|
|
87
87
|
_getOutputMessages(outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;
|
|
88
|
-
_enterHistory(input: any, kwargs?:
|
|
89
|
-
config?: RunnableConfig;
|
|
90
|
-
}): Promise<BaseMessage[]>;
|
|
88
|
+
_enterHistory(input: any, kwargs?: RunnableConfig): Promise<BaseMessage[]>;
|
|
91
89
|
_exitHistory(run: Run, config: RunnableConfig): Promise<void>;
|
|
92
90
|
_mergeConfig(...configs: Array<RunnableConfig | undefined>): Promise<Partial<RunnableConfig>>;
|
|
93
91
|
}
|
|
@@ -208,7 +208,7 @@ export class RunnableWithMessageHistory extends RunnableBinding {
|
|
|
208
208
|
async _enterHistory(
|
|
209
209
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
210
210
|
input, kwargs) {
|
|
211
|
-
const history = kwargs?.
|
|
211
|
+
const history = kwargs?.configurable?.messageHistory;
|
|
212
212
|
const messages = await history.getMessages();
|
|
213
213
|
if (this.historyMessagesKey === undefined) {
|
|
214
214
|
return messages.concat(this._getInputMessages(input));
|
package/dist/runnables/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.mergeConfigs = exports.ensureConfig = exports.patchConfig = exports.getCallbackManagerForConfig = exports._coerceToRunnable = exports.RunnablePick = exports.RunnableAssign = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableParallel = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
|
|
3
|
+
exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.mergeConfigs = exports.ensureConfig = exports.patchConfig = exports.getCallbackManagerForConfig = exports.RunnableToolLike = exports._coerceToRunnable = exports.RunnablePick = exports.RunnableAssign = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableParallel = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
|
|
4
4
|
var base_js_1 = require("./base.cjs");
|
|
5
5
|
Object.defineProperty(exports, "Runnable", { enumerable: true, get: function () { return base_js_1.Runnable; } });
|
|
6
6
|
Object.defineProperty(exports, "RunnableBinding", { enumerable: true, get: function () { return base_js_1.RunnableBinding; } });
|
|
@@ -14,6 +14,7 @@ Object.defineProperty(exports, "RunnableWithFallbacks", { enumerable: true, get:
|
|
|
14
14
|
Object.defineProperty(exports, "RunnableAssign", { enumerable: true, get: function () { return base_js_1.RunnableAssign; } });
|
|
15
15
|
Object.defineProperty(exports, "RunnablePick", { enumerable: true, get: function () { return base_js_1.RunnablePick; } });
|
|
16
16
|
Object.defineProperty(exports, "_coerceToRunnable", { enumerable: true, get: function () { return base_js_1._coerceToRunnable; } });
|
|
17
|
+
Object.defineProperty(exports, "RunnableToolLike", { enumerable: true, get: function () { return base_js_1.RunnableToolLike; } });
|
|
17
18
|
var config_js_1 = require("./config.cjs");
|
|
18
19
|
Object.defineProperty(exports, "getCallbackManagerForConfig", { enumerable: true, get: function () { return config_js_1.getCallbackManagerForConfig; } });
|
|
19
20
|
Object.defineProperty(exports, "patchConfig", { enumerable: true, get: function () { return config_js_1.patchConfig; } });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { type RunnableFunc, type RunnableLike, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
|
|
1
|
+
export { type RunnableFunc, type RunnableLike, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, RunnableToolLike, type RunnableToolLikeArgs, } from "./base.js";
|
|
2
2
|
export { type RunnableBatchOptions, type RunnableInterface, type RunnableIOSchema, } from "./types.js";
|
|
3
3
|
export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
|
|
4
4
|
export { RunnablePassthrough } from "./passthrough.js";
|
package/dist/runnables/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
|
|
1
|
+
export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, RunnableToolLike, } from "./base.js";
|
|
2
2
|
export { getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
|
|
3
3
|
export { RunnablePassthrough } from "./passthrough.js";
|
|
4
4
|
export { RouterRunnable } from "./router.js";
|
|
@@ -373,19 +373,19 @@ test("Create a runnable sequence with a static method with invalid output and ca
|
|
|
373
373
|
test("RunnableSequence can pass config to every step in batched request", async () => {
|
|
374
374
|
let numSeen = 0;
|
|
375
375
|
const addOne = (x, options) => {
|
|
376
|
-
if (options?.
|
|
376
|
+
if (options?.configurable?.isPresent === true) {
|
|
377
377
|
numSeen += 1;
|
|
378
378
|
}
|
|
379
379
|
return x + 1;
|
|
380
380
|
};
|
|
381
381
|
const addTwo = (x, options) => {
|
|
382
|
-
if (options?.
|
|
382
|
+
if (options?.configurable?.isPresent === true) {
|
|
383
383
|
numSeen += 1;
|
|
384
384
|
}
|
|
385
385
|
return x + 2;
|
|
386
386
|
};
|
|
387
387
|
const addThree = (x, options) => {
|
|
388
|
-
if (options?.
|
|
388
|
+
if (options?.configurable?.isPresent === true) {
|
|
389
389
|
numSeen += 1;
|
|
390
390
|
}
|
|
391
391
|
return x + 3;
|
|
@@ -8,7 +8,7 @@ import { ChatPromptTemplate } from "../../prompts/chat.js";
|
|
|
8
8
|
import { FakeListChatModel, FakeRetriever, FakeStreamingLLM, } from "../../utils/testing/index.js";
|
|
9
9
|
import { AIMessageChunk, HumanMessage, SystemMessage, } from "../../messages/index.js";
|
|
10
10
|
import { ChatGenerationChunk, GenerationChunk } from "../../outputs.js";
|
|
11
|
-
import { DynamicStructuredTool, DynamicTool } from "../../tools.js";
|
|
11
|
+
import { DynamicStructuredTool, DynamicTool } from "../../tools/index.js";
|
|
12
12
|
import { Document } from "../../documents/document.js";
|
|
13
13
|
function reverse(s) {
|
|
14
14
|
// Reverse a string.
|