@langchain/core 0.1.14 → 0.1.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.
@@ -1,27 +1,25 @@
1
- import { CallbackManager, } from "../callbacks/manager.js";
1
+ import { CallbackManager, ensureHandler, } from "../callbacks/manager.js";
2
2
  export const DEFAULT_RECURSION_LIMIT = 25;
3
3
  export async function getCallbackManagerForConfig(config) {
4
4
  return CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
5
5
  }
6
- export function mergeConfigs(config,
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- options) {
6
+ export function mergeConfigs(...configs) {
9
7
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
- const copy = { ...config };
11
- if (options) {
8
+ const copy = ensureConfig();
9
+ for (const options of configs.filter((c) => !!c)) {
12
10
  for (const key of Object.keys(options)) {
13
11
  if (key === "metadata") {
14
12
  copy[key] = { ...copy[key], ...options[key] };
15
13
  }
16
14
  else if (key === "tags") {
17
- copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
15
+ copy[key] = [...new Set(copy[key].concat(options[key] ?? []))];
18
16
  }
19
17
  else if (key === "configurable") {
20
18
  copy[key] = { ...copy[key], ...options[key] };
21
19
  }
22
20
  else if (key === "callbacks") {
23
21
  const baseCallbacks = copy.callbacks;
24
- const providedCallbacks = options.callbacks ?? config.callbacks;
22
+ const providedCallbacks = options.callbacks;
25
23
  // callbacks can be either undefined, Array<handler> or manager
26
24
  // so merging two callbacks values has 6 cases
27
25
  if (Array.isArray(providedCallbacks)) {
@@ -35,7 +33,7 @@ options) {
35
33
  // baseCallbacks is a manager
36
34
  const manager = baseCallbacks.copy();
37
35
  for (const callback of providedCallbacks) {
38
- manager.addHandler(callback, true);
36
+ manager.addHandler(ensureHandler(callback), true);
39
37
  }
40
38
  copy.callbacks = manager;
41
39
  }
@@ -48,13 +46,13 @@ options) {
48
46
  else if (Array.isArray(baseCallbacks)) {
49
47
  const manager = providedCallbacks.copy();
50
48
  for (const callback of baseCallbacks) {
51
- manager.addHandler(callback, true);
49
+ manager.addHandler(ensureHandler(callback), true);
52
50
  }
53
51
  copy.callbacks = manager;
54
52
  }
55
53
  else {
56
54
  // baseCallbacks is also a manager
57
- copy.callbacks = new CallbackManager(providedCallbacks.parentRunId, {
55
+ copy.callbacks = new CallbackManager(providedCallbacks._parentRunId, {
58
56
  handlers: baseCallbacks.handlers.concat(providedCallbacks.handlers),
59
57
  inheritableHandlers: baseCallbacks.inheritableHandlers.concat(providedCallbacks.inheritableHandlers),
60
58
  tags: Array.from(new Set(baseCallbacks.tags.concat(providedCallbacks.tags))),
@@ -68,9 +66,64 @@ options) {
68
66
  }
69
67
  }
70
68
  else {
71
- copy[key] = options[key] ?? copy[key];
69
+ const typedKey = key;
70
+ copy[typedKey] = options[typedKey] ?? copy[typedKey];
72
71
  }
73
72
  }
74
73
  }
75
74
  return copy;
76
75
  }
76
+ const PRIMITIVES = new Set(["string", "number", "boolean"]);
77
+ /**
78
+ * Ensure that a passed config is an object with all required keys present.
79
+ */
80
+ export function ensureConfig(config) {
81
+ let empty = {
82
+ tags: [],
83
+ metadata: {},
84
+ callbacks: undefined,
85
+ recursionLimit: 25,
86
+ };
87
+ if (config) {
88
+ empty = { ...empty, ...config };
89
+ }
90
+ if (config?.configurable) {
91
+ for (const key of Object.keys(config.configurable)) {
92
+ if (PRIMITIVES.has(typeof config.configurable[key]) &&
93
+ !empty.metadata?.[key]) {
94
+ if (!empty.metadata) {
95
+ empty.metadata = {};
96
+ }
97
+ empty.metadata[key] = config.configurable[key];
98
+ }
99
+ }
100
+ }
101
+ return empty;
102
+ }
103
+ /**
104
+ * Helper function that patches runnable configs with updated properties.
105
+ */
106
+ export function patchConfig(config = {}, { callbacks, maxConcurrency, recursionLimit, runName, configurable, } = {}) {
107
+ const newConfig = ensureConfig(config);
108
+ if (callbacks !== undefined) {
109
+ /**
110
+ * If we're replacing callbacks we need to unset runName
111
+ * since that should apply only to the same run as the original callbacks
112
+ */
113
+ delete newConfig.runName;
114
+ newConfig.callbacks = callbacks;
115
+ }
116
+ if (recursionLimit !== undefined) {
117
+ newConfig.recursionLimit = recursionLimit;
118
+ }
119
+ if (maxConcurrency !== undefined) {
120
+ newConfig.maxConcurrency = maxConcurrency;
121
+ }
122
+ if (runName !== undefined) {
123
+ newConfig.runName = runName;
124
+ }
125
+ if (configurable !== undefined) {
126
+ newConfig.configurable = { ...newConfig.configurable, ...configurable };
127
+ }
128
+ return newConfig;
129
+ }
@@ -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.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.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;
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; } });
@@ -16,6 +16,7 @@ Object.defineProperty(exports, "RunnablePick", { enumerable: true, get: function
16
16
  Object.defineProperty(exports, "_coerceToRunnable", { enumerable: true, get: function () { return base_js_1._coerceToRunnable; } });
17
17
  var config_js_1 = require("./config.cjs");
18
18
  Object.defineProperty(exports, "getCallbackManagerForConfig", { enumerable: true, get: function () { return config_js_1.getCallbackManagerForConfig; } });
19
+ Object.defineProperty(exports, "patchConfig", { enumerable: true, get: function () { return config_js_1.patchConfig; } });
19
20
  var passthrough_js_1 = require("./passthrough.cjs");
20
21
  Object.defineProperty(exports, "RunnablePassthrough", { enumerable: true, get: function () { return passthrough_js_1.RunnablePassthrough; } });
21
22
  var router_js_1 = require("./router.cjs");
@@ -1,5 +1,5 @@
1
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";
2
- export { type RunnableConfig, getCallbackManagerForConfig } from "./config.js";
2
+ export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, } from "./config.js";
3
3
  export { RunnablePassthrough } from "./passthrough.js";
4
4
  export { type RouterInput, RouterRunnable } from "./router.js";
5
5
  export { RunnableBranch, type Branch, type BranchLike } from "./branch.js";
@@ -1,5 +1,5 @@
1
1
  export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, } from "./base.js";
2
- export { getCallbackManagerForConfig } from "./config.js";
2
+ export { getCallbackManagerForConfig, patchConfig, } from "./config.js";
3
3
  export { RunnablePassthrough } from "./passthrough.js";
4
4
  export { RouterRunnable } from "./router.js";
5
5
  export { RunnableBranch } from "./branch.js";
@@ -49,9 +49,8 @@ class RouterRunnable extends base_js_1.Runnable {
49
49
  }
50
50
  const runnables = keys.map((key) => this.runnables[key]);
51
51
  const optionsList = this._getOptionsList(options ?? {}, inputs.length);
52
- const batchSize = batchOptions?.maxConcurrency && batchOptions.maxConcurrency > 0
53
- ? batchOptions?.maxConcurrency
54
- : inputs.length;
52
+ const maxConcurrency = optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
53
+ const batchSize = maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;
55
54
  const batchResults = [];
56
55
  for (let i = 0; i < actualInputs.length; i += batchSize) {
57
56
  const batchPromises = actualInputs
@@ -46,9 +46,8 @@ export class RouterRunnable extends Runnable {
46
46
  }
47
47
  const runnables = keys.map((key) => this.runnables[key]);
48
48
  const optionsList = this._getOptionsList(options ?? {}, inputs.length);
49
- const batchSize = batchOptions?.maxConcurrency && batchOptions.maxConcurrency > 0
50
- ? batchOptions?.maxConcurrency
51
- : inputs.length;
49
+ const maxConcurrency = optionsList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
50
+ const batchSize = maxConcurrency && maxConcurrency > 0 ? maxConcurrency : inputs.length;
52
51
  const batchResults = [];
53
52
  for (let i = 0; i < actualInputs.length; i += batchSize) {
54
53
  const batchPromises = actualInputs
package/dist/tools.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Tool = exports.StructuredTool = exports.ToolInputParsingException = void 0;
3
+ exports.DynamicStructuredTool = exports.DynamicTool = exports.Tool = exports.StructuredTool = exports.ToolInputParsingException = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const manager_js_1 = require("./callbacks/manager.cjs");
6
6
  const base_js_1 = require("./language_models/base.cjs");
@@ -109,3 +109,104 @@ class Tool extends StructuredTool {
109
109
  }
110
110
  }
111
111
  exports.Tool = Tool;
112
+ /**
113
+ * A tool that can be created dynamically from a function, name, and description.
114
+ */
115
+ class DynamicTool extends Tool {
116
+ static lc_name() {
117
+ return "DynamicTool";
118
+ }
119
+ constructor(fields) {
120
+ super(fields);
121
+ Object.defineProperty(this, "name", {
122
+ enumerable: true,
123
+ configurable: true,
124
+ writable: true,
125
+ value: void 0
126
+ });
127
+ Object.defineProperty(this, "description", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: void 0
132
+ });
133
+ Object.defineProperty(this, "func", {
134
+ enumerable: true,
135
+ configurable: true,
136
+ writable: true,
137
+ value: void 0
138
+ });
139
+ this.name = fields.name;
140
+ this.description = fields.description;
141
+ this.func = fields.func;
142
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
143
+ }
144
+ async call(arg, configArg) {
145
+ const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
146
+ if (config.runName === undefined) {
147
+ config.runName = this.name;
148
+ }
149
+ return super.call(arg, config);
150
+ }
151
+ /** @ignore */
152
+ async _call(input, runManager) {
153
+ return this.func(input, runManager);
154
+ }
155
+ }
156
+ exports.DynamicTool = DynamicTool;
157
+ /**
158
+ * A tool that can be created dynamically from a function, name, and
159
+ * description, designed to work with structured data. It extends the
160
+ * StructuredTool class and overrides the _call method to execute the
161
+ * provided function when the tool is called.
162
+ */
163
+ class DynamicStructuredTool extends StructuredTool {
164
+ static lc_name() {
165
+ return "DynamicStructuredTool";
166
+ }
167
+ constructor(fields) {
168
+ super(fields);
169
+ Object.defineProperty(this, "name", {
170
+ enumerable: true,
171
+ configurable: true,
172
+ writable: true,
173
+ value: void 0
174
+ });
175
+ Object.defineProperty(this, "description", {
176
+ enumerable: true,
177
+ configurable: true,
178
+ writable: true,
179
+ value: void 0
180
+ });
181
+ Object.defineProperty(this, "func", {
182
+ enumerable: true,
183
+ configurable: true,
184
+ writable: true,
185
+ value: void 0
186
+ });
187
+ Object.defineProperty(this, "schema", {
188
+ enumerable: true,
189
+ configurable: true,
190
+ writable: true,
191
+ value: void 0
192
+ });
193
+ this.name = fields.name;
194
+ this.description = fields.description;
195
+ this.func = fields.func;
196
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
197
+ this.schema = fields.schema;
198
+ }
199
+ async call(arg, configArg,
200
+ /** @deprecated */
201
+ tags) {
202
+ const config = (0, manager_js_1.parseCallbackConfigArg)(configArg);
203
+ if (config.runName === undefined) {
204
+ config.runName = this.name;
205
+ }
206
+ return super.call(arg, config, tags);
207
+ }
208
+ _call(arg, runManager) {
209
+ return this.func(arg, runManager);
210
+ }
211
+ }
212
+ exports.DynamicStructuredTool = DynamicStructuredTool;
package/dist/tools.d.ts CHANGED
@@ -100,3 +100,52 @@ export declare abstract class Tool extends StructuredTool {
100
100
  */
101
101
  call(arg: string | undefined | z.input<this["schema"]>, callbacks?: Callbacks | RunnableConfig): Promise<string>;
102
102
  }
103
+ export interface BaseDynamicToolInput extends ToolParams {
104
+ name: string;
105
+ description: string;
106
+ returnDirect?: boolean;
107
+ }
108
+ /**
109
+ * Interface for the input parameters of the DynamicTool class.
110
+ */
111
+ export interface DynamicToolInput extends BaseDynamicToolInput {
112
+ func: (input: string, runManager?: CallbackManagerForToolRun) => Promise<string>;
113
+ }
114
+ /**
115
+ * Interface for the input parameters of the DynamicStructuredTool class.
116
+ */
117
+ export interface DynamicStructuredToolInput<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends BaseDynamicToolInput {
118
+ func: (input: z.infer<T>, runManager?: CallbackManagerForToolRun) => Promise<string>;
119
+ schema: T;
120
+ }
121
+ /**
122
+ * A tool that can be created dynamically from a function, name, and description.
123
+ */
124
+ export declare class DynamicTool extends Tool {
125
+ static lc_name(): string;
126
+ name: string;
127
+ description: string;
128
+ func: DynamicToolInput["func"];
129
+ constructor(fields: DynamicToolInput);
130
+ call(arg: string | undefined | z.input<this["schema"]>, configArg?: RunnableConfig | Callbacks): Promise<string>;
131
+ /** @ignore */
132
+ _call(input: string, runManager?: CallbackManagerForToolRun): Promise<string>;
133
+ }
134
+ /**
135
+ * A tool that can be created dynamically from a function, name, and
136
+ * description, designed to work with structured data. It extends the
137
+ * StructuredTool class and overrides the _call method to execute the
138
+ * provided function when the tool is called.
139
+ */
140
+ export declare class DynamicStructuredTool<T extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>> extends StructuredTool {
141
+ static lc_name(): string;
142
+ name: string;
143
+ description: string;
144
+ func: DynamicStructuredToolInput["func"];
145
+ schema: T;
146
+ constructor(fields: DynamicStructuredToolInput<T>);
147
+ call(arg: z.output<T>, configArg?: RunnableConfig | Callbacks,
148
+ /** @deprecated */
149
+ tags?: string[]): Promise<string>;
150
+ protected _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun): Promise<string>;
151
+ }
package/dist/tools.js CHANGED
@@ -103,3 +103,102 @@ export class Tool extends StructuredTool {
103
103
  return super.call(typeof arg === "string" || !arg ? { input: arg } : arg, callbacks);
104
104
  }
105
105
  }
106
+ /**
107
+ * A tool that can be created dynamically from a function, name, and description.
108
+ */
109
+ export class DynamicTool extends Tool {
110
+ static lc_name() {
111
+ return "DynamicTool";
112
+ }
113
+ constructor(fields) {
114
+ super(fields);
115
+ Object.defineProperty(this, "name", {
116
+ enumerable: true,
117
+ configurable: true,
118
+ writable: true,
119
+ value: void 0
120
+ });
121
+ Object.defineProperty(this, "description", {
122
+ enumerable: true,
123
+ configurable: true,
124
+ writable: true,
125
+ value: void 0
126
+ });
127
+ Object.defineProperty(this, "func", {
128
+ enumerable: true,
129
+ configurable: true,
130
+ writable: true,
131
+ value: void 0
132
+ });
133
+ this.name = fields.name;
134
+ this.description = fields.description;
135
+ this.func = fields.func;
136
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
137
+ }
138
+ async call(arg, configArg) {
139
+ const config = parseCallbackConfigArg(configArg);
140
+ if (config.runName === undefined) {
141
+ config.runName = this.name;
142
+ }
143
+ return super.call(arg, config);
144
+ }
145
+ /** @ignore */
146
+ async _call(input, runManager) {
147
+ return this.func(input, runManager);
148
+ }
149
+ }
150
+ /**
151
+ * A tool that can be created dynamically from a function, name, and
152
+ * description, designed to work with structured data. It extends the
153
+ * StructuredTool class and overrides the _call method to execute the
154
+ * provided function when the tool is called.
155
+ */
156
+ export class DynamicStructuredTool extends StructuredTool {
157
+ static lc_name() {
158
+ return "DynamicStructuredTool";
159
+ }
160
+ constructor(fields) {
161
+ super(fields);
162
+ Object.defineProperty(this, "name", {
163
+ enumerable: true,
164
+ configurable: true,
165
+ writable: true,
166
+ value: void 0
167
+ });
168
+ Object.defineProperty(this, "description", {
169
+ enumerable: true,
170
+ configurable: true,
171
+ writable: true,
172
+ value: void 0
173
+ });
174
+ Object.defineProperty(this, "func", {
175
+ enumerable: true,
176
+ configurable: true,
177
+ writable: true,
178
+ value: void 0
179
+ });
180
+ Object.defineProperty(this, "schema", {
181
+ enumerable: true,
182
+ configurable: true,
183
+ writable: true,
184
+ value: void 0
185
+ });
186
+ this.name = fields.name;
187
+ this.description = fields.description;
188
+ this.func = fields.func;
189
+ this.returnDirect = fields.returnDirect ?? this.returnDirect;
190
+ this.schema = fields.schema;
191
+ }
192
+ async call(arg, configArg,
193
+ /** @deprecated */
194
+ tags) {
195
+ const config = parseCallbackConfigArg(configArg);
196
+ if (config.runName === undefined) {
197
+ config.runName = this.name;
198
+ }
199
+ return super.call(arg, config, tags);
200
+ }
201
+ _call(arg, runManager) {
202
+ return this.func(arg, runManager);
203
+ }
204
+ }
@@ -47,9 +47,16 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
47
47
  ].join(".");
48
48
  }
49
49
  else {
50
- console.warn(`Parent run with UUID ${storedRun.parent_run_id} not found.`);
50
+ // This can happen naturally for callbacks added within a run
51
+ // console.debug(`Parent run with UUID ${storedRun.parent_run_id} has no dotted order.`);
51
52
  }
52
53
  }
54
+ else {
55
+ // This can happen naturally for callbacks added within a run
56
+ // console.debug(
57
+ // `Parent run with UUID ${storedRun.parent_run_id} not found.`
58
+ // );
59
+ }
53
60
  }
54
61
  else {
55
62
  storedRun.trace_id = storedRun.id;
@@ -44,9 +44,16 @@ export class BaseTracer extends BaseCallbackHandler {
44
44
  ].join(".");
45
45
  }
46
46
  else {
47
- console.warn(`Parent run with UUID ${storedRun.parent_run_id} not found.`);
47
+ // This can happen naturally for callbacks added within a run
48
+ // console.debug(`Parent run with UUID ${storedRun.parent_run_id} has no dotted order.`);
48
49
  }
49
50
  }
51
+ else {
52
+ // This can happen naturally for callbacks added within a run
53
+ // console.debug(
54
+ // `Parent run with UUID ${storedRun.parent_run_id} not found.`
55
+ // );
56
+ }
50
57
  }
51
58
  else {
52
59
  storedRun.trace_id = storedRun.id;
@@ -101,6 +101,12 @@ class LogStreamCallbackHandler extends base_js_1.BaseTracer {
101
101
  writable: true,
102
102
  value: void 0
103
103
  });
104
+ Object.defineProperty(this, "rootId", {
105
+ enumerable: true,
106
+ configurable: true,
107
+ writable: true,
108
+ value: void 0
109
+ });
104
110
  Object.defineProperty(this, "keyMapByRunId", {
105
111
  enumerable: true,
106
112
  configurable: true,
@@ -156,7 +162,7 @@ class LogStreamCallbackHandler extends base_js_1.BaseTracer {
156
162
  // and is therefore not useful here
157
163
  }
158
164
  _includeRun(run) {
159
- if (run.parent_run_id === undefined) {
165
+ if (run.id === this.rootId) {
160
166
  return false;
161
167
  }
162
168
  const runTags = run.tags ?? [];
@@ -186,8 +192,32 @@ class LogStreamCallbackHandler extends base_js_1.BaseTracer {
186
192
  }
187
193
  return include;
188
194
  }
195
+ async *tapOutputIterable(runId, output) {
196
+ // Tap an output async iterator to stream its values to the log.
197
+ for await (const chunk of output) {
198
+ // root run is handled in .streamLog()
199
+ if (runId !== this.rootId) {
200
+ // if we can't find the run silently ignore
201
+ // eg. because this run wasn't included in the log
202
+ const key = this.keyMapByRunId[runId];
203
+ if (key) {
204
+ await this.writer.write(new RunLogPatch({
205
+ ops: [
206
+ {
207
+ op: "add",
208
+ path: `/logs/${key}/streamed_output/-`,
209
+ value: chunk,
210
+ },
211
+ ],
212
+ }));
213
+ }
214
+ }
215
+ yield chunk;
216
+ }
217
+ }
189
218
  async onRunCreate(run) {
190
- if (run.parent_run_id === undefined) {
219
+ if (this.rootId === undefined) {
220
+ this.rootId = run.id;
191
221
  await this.writer.write(new RunLogPatch({
192
222
  ops: [
193
223
  {
@@ -220,6 +250,7 @@ class LogStreamCallbackHandler extends base_js_1.BaseTracer {
220
250
  tags: run.tags ?? [],
221
251
  metadata: run.extra?.metadata ?? {},
222
252
  start_time: new Date(run.start_time).toISOString(),
253
+ streamed_output: [],
223
254
  streamed_output_str: [],
224
255
  final_output: undefined,
225
256
  end_time: undefined,
@@ -258,7 +289,7 @@ class LogStreamCallbackHandler extends base_js_1.BaseTracer {
258
289
  await this.writer.write(patch);
259
290
  }
260
291
  finally {
261
- if (run.parent_run_id === undefined) {
292
+ if (run.id === this.rootId) {
262
293
  const patch = new RunLogPatch({
263
294
  ops: [
264
295
  {
@@ -19,6 +19,8 @@ export type LogEntry = {
19
19
  metadata: Record<string, any>;
20
20
  /** ISO-8601 timestamp of when the run started. */
21
21
  start_time: string;
22
+ /** List of general output chunks streamed by this run. */
23
+ streamed_output: any[];
22
24
  /** List of LLM tokens streamed by this run, if applicable. */
23
25
  streamed_output_str: string[];
24
26
  /** Final output of this run. Only available after the run has finished successfully. */
@@ -84,6 +86,7 @@ export declare class LogStreamCallbackHandler extends BaseTracer {
84
86
  protected excludeNames?: string[];
85
87
  protected excludeTypes?: string[];
86
88
  protected excludeTags?: string[];
89
+ protected rootId?: string;
87
90
  private keyMapByRunId;
88
91
  private counterMapByRunName;
89
92
  protected transformStream: TransformStream;
@@ -94,6 +97,7 @@ export declare class LogStreamCallbackHandler extends BaseTracer {
94
97
  [Symbol.asyncIterator](): IterableReadableStream<RunLogPatch>;
95
98
  protected persistRun(_run: Run): Promise<void>;
96
99
  _includeRun(run: Run): boolean;
100
+ tapOutputIterable<T>(runId: string, output: AsyncGenerator<T>): AsyncGenerator<T>;
97
101
  onRunCreate(run: Run): Promise<void>;
98
102
  onRunUpdate(run: Run): Promise<void>;
99
103
  onLLMNewToken(run: Run, token: string): Promise<void>;
@@ -96,6 +96,12 @@ export class LogStreamCallbackHandler extends BaseTracer {
96
96
  writable: true,
97
97
  value: void 0
98
98
  });
99
+ Object.defineProperty(this, "rootId", {
100
+ enumerable: true,
101
+ configurable: true,
102
+ writable: true,
103
+ value: void 0
104
+ });
99
105
  Object.defineProperty(this, "keyMapByRunId", {
100
106
  enumerable: true,
101
107
  configurable: true,
@@ -151,7 +157,7 @@ export class LogStreamCallbackHandler extends BaseTracer {
151
157
  // and is therefore not useful here
152
158
  }
153
159
  _includeRun(run) {
154
- if (run.parent_run_id === undefined) {
160
+ if (run.id === this.rootId) {
155
161
  return false;
156
162
  }
157
163
  const runTags = run.tags ?? [];
@@ -181,8 +187,32 @@ export class LogStreamCallbackHandler extends BaseTracer {
181
187
  }
182
188
  return include;
183
189
  }
190
+ async *tapOutputIterable(runId, output) {
191
+ // Tap an output async iterator to stream its values to the log.
192
+ for await (const chunk of output) {
193
+ // root run is handled in .streamLog()
194
+ if (runId !== this.rootId) {
195
+ // if we can't find the run silently ignore
196
+ // eg. because this run wasn't included in the log
197
+ const key = this.keyMapByRunId[runId];
198
+ if (key) {
199
+ await this.writer.write(new RunLogPatch({
200
+ ops: [
201
+ {
202
+ op: "add",
203
+ path: `/logs/${key}/streamed_output/-`,
204
+ value: chunk,
205
+ },
206
+ ],
207
+ }));
208
+ }
209
+ }
210
+ yield chunk;
211
+ }
212
+ }
184
213
  async onRunCreate(run) {
185
- if (run.parent_run_id === undefined) {
214
+ if (this.rootId === undefined) {
215
+ this.rootId = run.id;
186
216
  await this.writer.write(new RunLogPatch({
187
217
  ops: [
188
218
  {
@@ -215,6 +245,7 @@ export class LogStreamCallbackHandler extends BaseTracer {
215
245
  tags: run.tags ?? [],
216
246
  metadata: run.extra?.metadata ?? {},
217
247
  start_time: new Date(run.start_time).toISOString(),
248
+ streamed_output: [],
218
249
  streamed_output_str: [],
219
250
  final_output: undefined,
220
251
  end_time: undefined,
@@ -253,7 +284,7 @@ export class LogStreamCallbackHandler extends BaseTracer {
253
284
  await this.writer.write(patch);
254
285
  }
255
286
  finally {
256
- if (run.parent_run_id === undefined) {
287
+ if (run.id === this.rootId) {
257
288
  const patch = new RunLogPatch({
258
289
  ops: [
259
290
  {