@langchain/core 0.1.43 → 0.1.45-rc.0

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 (44) hide show
  1. package/dist/callbacks/base.cjs +3 -4
  2. package/dist/callbacks/base.d.ts +1 -0
  3. package/dist/callbacks/base.js +3 -4
  4. package/dist/callbacks/manager.cjs +10 -0
  5. package/dist/callbacks/manager.js +10 -0
  6. package/dist/language_models/base.d.ts +1 -0
  7. package/dist/load/serializable.d.ts +6 -1
  8. package/dist/output_parsers/base.cjs +9 -3
  9. package/dist/output_parsers/base.d.ts +3 -1
  10. package/dist/output_parsers/base.js +9 -3
  11. package/dist/output_parsers/index.cjs +1 -0
  12. package/dist/output_parsers/index.d.ts +1 -0
  13. package/dist/output_parsers/index.js +1 -0
  14. package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +22 -2
  15. package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +6 -2
  16. package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +23 -3
  17. package/dist/output_parsers/string.cjs +21 -0
  18. package/dist/output_parsers/string.d.ts +5 -0
  19. package/dist/output_parsers/string.js +21 -0
  20. package/dist/output_parsers/structured.cjs +190 -0
  21. package/dist/output_parsers/structured.d.ts +88 -0
  22. package/dist/output_parsers/structured.js +184 -0
  23. package/dist/output_parsers/transform.cjs +1 -3
  24. package/dist/output_parsers/transform.js +1 -3
  25. package/dist/runnables/base.cjs +44 -1
  26. package/dist/runnables/base.d.ts +6 -25
  27. package/dist/runnables/base.js +45 -2
  28. package/dist/runnables/graph.cjs +164 -0
  29. package/dist/runnables/graph.d.ts +25 -0
  30. package/dist/runnables/graph.js +159 -0
  31. package/dist/runnables/index.d.ts +2 -1
  32. package/dist/runnables/types.cjs +2 -0
  33. package/dist/runnables/types.d.ts +33 -0
  34. package/dist/runnables/types.js +1 -0
  35. package/dist/runnables/utils.cjs +6 -1
  36. package/dist/runnables/utils.d.ts +2 -0
  37. package/dist/runnables/utils.js +4 -0
  38. package/dist/tracers/log_stream.cjs +1 -1
  39. package/dist/tracers/log_stream.js +1 -1
  40. package/dist/tracers/root_listener.cjs +1 -1
  41. package/dist/tracers/root_listener.js +1 -1
  42. package/dist/tracers/run_collector.cjs +1 -1
  43. package/dist/tracers/run_collector.js +1 -1
  44. package/package.json +1 -1
@@ -0,0 +1,25 @@
1
+ import type { RunnableInterface, RunnableIOSchema } from "./types.js";
2
+ interface Edge {
3
+ source: string;
4
+ target: string;
5
+ data?: string;
6
+ }
7
+ interface Node {
8
+ id: string;
9
+ data: RunnableIOSchema | RunnableInterface;
10
+ }
11
+ export declare function nodeDataStr(node: Node): string;
12
+ export declare class Graph {
13
+ nodes: Record<string, Node>;
14
+ edges: Edge[];
15
+ toJSON(): Record<string, any>;
16
+ addNode(data: RunnableInterface | RunnableIOSchema, id?: string): Node;
17
+ removeNode(node: Node): void;
18
+ addEdge(source: Node, target: Node, data?: string): Edge;
19
+ firstNode(): Node | undefined;
20
+ lastNode(): Node | undefined;
21
+ extend(graph: Graph): void;
22
+ trimFirstNode(): void;
23
+ trimLastNode(): void;
24
+ }
25
+ export {};
@@ -0,0 +1,159 @@
1
+ import { zodToJsonSchema } from "zod-to-json-schema";
2
+ import { v4 as uuidv4, validate as isUuid } from "uuid";
3
+ import { isRunnableInterface } from "./utils.js";
4
+ const MAX_DATA_DISPLAY_NAME_LENGTH = 42;
5
+ export function nodeDataStr(node) {
6
+ if (!isUuid(node.id)) {
7
+ return node.id;
8
+ }
9
+ else if (isRunnableInterface(node.data)) {
10
+ try {
11
+ let data = node.data.toString();
12
+ if (data.startsWith("<") ||
13
+ data[0] !== data[0].toUpperCase() ||
14
+ data.split("\n").length > 1) {
15
+ data = node.data.getName();
16
+ }
17
+ else if (data.length > MAX_DATA_DISPLAY_NAME_LENGTH) {
18
+ data = `${data.substring(0, MAX_DATA_DISPLAY_NAME_LENGTH)}...`;
19
+ }
20
+ return data.startsWith("Runnable") ? data.slice("Runnable".length) : data;
21
+ }
22
+ catch (error) {
23
+ return node.data.getName();
24
+ }
25
+ }
26
+ else {
27
+ return node.data.name ?? "UnknownSchema";
28
+ }
29
+ }
30
+ function nodeDataJson(node) {
31
+ // if node.data is implements Runnable
32
+ if (isRunnableInterface(node.data)) {
33
+ return {
34
+ type: "runnable",
35
+ data: {
36
+ id: node.data.lc_id,
37
+ name: node.data.getName(),
38
+ },
39
+ };
40
+ }
41
+ else {
42
+ return {
43
+ type: "schema",
44
+ data: { ...zodToJsonSchema(node.data.schema), title: node.data.name },
45
+ };
46
+ }
47
+ }
48
+ export class Graph {
49
+ constructor() {
50
+ Object.defineProperty(this, "nodes", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: {}
55
+ });
56
+ Object.defineProperty(this, "edges", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: []
61
+ });
62
+ }
63
+ // Convert the graph to a JSON-serializable format.
64
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
+ toJSON() {
66
+ const stableNodeIds = {};
67
+ Object.values(this.nodes).forEach((node, i) => {
68
+ stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;
69
+ });
70
+ return {
71
+ nodes: Object.values(this.nodes).map((node) => ({
72
+ id: stableNodeIds[node.id],
73
+ ...nodeDataJson(node),
74
+ })),
75
+ edges: this.edges.map((edge) => edge.data
76
+ ? {
77
+ source: stableNodeIds[edge.source],
78
+ target: stableNodeIds[edge.target],
79
+ data: edge.data,
80
+ }
81
+ : {
82
+ source: stableNodeIds[edge.source],
83
+ target: stableNodeIds[edge.target],
84
+ }),
85
+ };
86
+ }
87
+ addNode(data, id) {
88
+ if (id !== undefined && this.nodes[id] !== undefined) {
89
+ throw new Error(`Node with id ${id} already exists`);
90
+ }
91
+ const nodeId = id || uuidv4();
92
+ const node = { id: nodeId, data };
93
+ this.nodes[nodeId] = node;
94
+ return node;
95
+ }
96
+ removeNode(node) {
97
+ // Remove the node from the nodes map
98
+ delete this.nodes[node.id];
99
+ // Filter out edges connected to the node
100
+ this.edges = this.edges.filter((edge) => edge.source !== node.id && edge.target !== node.id);
101
+ }
102
+ addEdge(source, target, data) {
103
+ if (this.nodes[source.id] === undefined) {
104
+ throw new Error(`Source node ${source.id} not in graph`);
105
+ }
106
+ if (this.nodes[target.id] === undefined) {
107
+ throw new Error(`Target node ${target.id} not in graph`);
108
+ }
109
+ const edge = { source: source.id, target: target.id, data };
110
+ this.edges.push(edge);
111
+ return edge;
112
+ }
113
+ firstNode() {
114
+ const targets = new Set(this.edges.map((edge) => edge.target));
115
+ const found = [];
116
+ Object.values(this.nodes).forEach((node) => {
117
+ if (!targets.has(node.id)) {
118
+ found.push(node);
119
+ }
120
+ });
121
+ return found[0];
122
+ }
123
+ lastNode() {
124
+ const sources = new Set(this.edges.map((edge) => edge.source));
125
+ const found = [];
126
+ Object.values(this.nodes).forEach((node) => {
127
+ if (!sources.has(node.id)) {
128
+ found.push(node);
129
+ }
130
+ });
131
+ return found[0];
132
+ }
133
+ extend(graph) {
134
+ // Add all nodes from the other graph, taking care to avoid duplicates
135
+ Object.entries(graph.nodes).forEach(([key, value]) => {
136
+ this.nodes[key] = value;
137
+ });
138
+ // Add all edges from the other graph
139
+ this.edges = [...this.edges, ...graph.edges];
140
+ }
141
+ trimFirstNode() {
142
+ const firstNode = this.firstNode();
143
+ if (firstNode) {
144
+ const outgoingEdges = this.edges.filter((edge) => edge.source === firstNode.id);
145
+ if (Object.keys(this.nodes).length === 1 || outgoingEdges.length === 1) {
146
+ this.removeNode(firstNode);
147
+ }
148
+ }
149
+ }
150
+ trimLastNode() {
151
+ const lastNode = this.lastNode();
152
+ if (lastNode) {
153
+ const incomingEdges = this.edges.filter((edge) => edge.target === lastNode.id);
154
+ if (Object.keys(this.nodes).length === 1 || incomingEdges.length === 1) {
155
+ this.removeNode(lastNode);
156
+ }
157
+ }
158
+ }
159
+ }
@@ -1,4 +1,5 @@
1
- export { type RunnableFunc, type RunnableLike, type RunnableBatchOptions, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableInterface, 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, } from "./base.js";
2
+ export { type RunnableBatchOptions, type RunnableInterface, type RunnableIOSchema, } from "./types.js";
2
3
  export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, ensureConfig, } from "./config.js";
3
4
  export { RunnablePassthrough } from "./passthrough.js";
4
5
  export { type RouterInput, RouterRunnable } from "./router.js";
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,33 @@
1
+ import type { z } from "zod";
2
+ import type { RunnableConfig } from "./config.js";
3
+ import type { IterableReadableStreamInterface } from "../utils/stream.js";
4
+ import type { SerializableInterface } from "../load/serializable.js";
5
+ export type RunnableBatchOptions = {
6
+ /** @deprecated Pass in via the standard runnable config object instead */
7
+ maxConcurrency?: number;
8
+ returnExceptions?: boolean;
9
+ };
10
+ export type RunnableIOSchema = {
11
+ name?: string;
12
+ schema: z.ZodType;
13
+ };
14
+ /**
15
+ * Base interface implemented by all runnables.
16
+ * Used for cross-compatibility between different versions of LangChain core.
17
+ *
18
+ * Should not change on patch releases.
19
+ */
20
+ export interface RunnableInterface<RunInput = any, RunOutput = any, CallOptions extends RunnableConfig = RunnableConfig> extends SerializableInterface {
21
+ lc_serializable: boolean;
22
+ invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
23
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
24
+ returnExceptions?: false;
25
+ }): Promise<RunOutput[]>;
26
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
27
+ returnExceptions: true;
28
+ }): Promise<(RunOutput | Error)[]>;
29
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
30
+ stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStreamInterface<RunOutput>>;
31
+ transform(generator: AsyncGenerator<RunInput>, options: Partial<CallOptions>): AsyncGenerator<RunOutput>;
32
+ getName(suffix?: string): string;
33
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._RootEventFilter = void 0;
3
+ exports._RootEventFilter = exports.isRunnableInterface = void 0;
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ function isRunnableInterface(thing) {
6
+ return thing ? thing.lc_runnable : false;
7
+ }
8
+ exports.isRunnableInterface = isRunnableInterface;
4
9
  /**
5
10
  * Utility to filter the root event in the streamEvents implementation.
6
11
  * This is simply binding the arguments to the namespace to make save on
@@ -1,4 +1,6 @@
1
1
  import { StreamEvent } from "../tracers/log_stream.js";
2
+ import type { RunnableInterface } from "./types.js";
3
+ export declare function isRunnableInterface(thing: any): thing is RunnableInterface;
2
4
  /**
3
5
  * Utility to filter the root event in the streamEvents implementation.
4
6
  * This is simply binding the arguments to the namespace to make save on
@@ -1,3 +1,7 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export function isRunnableInterface(thing) {
3
+ return thing ? thing.lc_runnable : false;
4
+ }
1
5
  /**
2
6
  * Utility to filter the root event in the streamEvents implementation.
3
7
  * This is simply binding the arguments to the namespace to make save on
@@ -120,7 +120,7 @@ function isChatGenerationChunk(x) {
120
120
  */
121
121
  class LogStreamCallbackHandler extends base_js_1.BaseTracer {
122
122
  constructor(fields) {
123
- super(fields);
123
+ super({ _awaitHandler: true, ...fields });
124
124
  Object.defineProperty(this, "autoClose", {
125
125
  enumerable: true,
126
126
  configurable: true,
@@ -115,7 +115,7 @@ function isChatGenerationChunk(x) {
115
115
  */
116
116
  export class LogStreamCallbackHandler extends BaseTracer {
117
117
  constructor(fields) {
118
- super(fields);
118
+ super({ _awaitHandler: true, ...fields });
119
119
  Object.defineProperty(this, "autoClose", {
120
120
  enumerable: true,
121
121
  configurable: true,
@@ -4,7 +4,7 @@ exports.RootListenersTracer = void 0;
4
4
  const base_js_1 = require("./base.cjs");
5
5
  class RootListenersTracer extends base_js_1.BaseTracer {
6
6
  constructor({ config, onStart, onEnd, onError, }) {
7
- super();
7
+ super({ _awaitHandler: true });
8
8
  Object.defineProperty(this, "name", {
9
9
  enumerable: true,
10
10
  configurable: true,
@@ -1,7 +1,7 @@
1
1
  import { BaseTracer } from "./base.js";
2
2
  export class RootListenersTracer extends BaseTracer {
3
3
  constructor({ config, onStart, onEnd, onError, }) {
4
- super();
4
+ super({ _awaitHandler: true });
5
5
  Object.defineProperty(this, "name", {
6
6
  enumerable: true,
7
7
  configurable: true,
@@ -12,7 +12,7 @@ class RunCollectorCallbackHandler extends base_js_1.BaseTracer {
12
12
  * @param exampleId The ID of the example.
13
13
  */
14
14
  constructor({ exampleId } = {}) {
15
- super();
15
+ super({ _awaitHandler: true });
16
16
  /** The name of the callback handler. */
17
17
  Object.defineProperty(this, "name", {
18
18
  enumerable: true,
@@ -9,7 +9,7 @@ export class RunCollectorCallbackHandler extends BaseTracer {
9
9
  * @param exampleId The ID of the example.
10
10
  */
11
11
  constructor({ exampleId } = {}) {
12
- super();
12
+ super({ _awaitHandler: true });
13
13
  /** The name of the callback handler. */
14
14
  Object.defineProperty(this, "name", {
15
15
  enumerable: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.43",
3
+ "version": "0.1.45-rc.0",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {