langchain 0.0.154 → 0.0.156

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 (78) hide show
  1. package/chat_models/bedrock.cjs +1 -0
  2. package/chat_models/bedrock.d.ts +1 -0
  3. package/chat_models/bedrock.js +1 -0
  4. package/dist/callbacks/base.d.ts +42 -28
  5. package/dist/callbacks/handlers/log_stream.cjs +283 -0
  6. package/dist/callbacks/handlers/log_stream.d.ts +99 -0
  7. package/dist/callbacks/handlers/log_stream.js +277 -0
  8. package/dist/callbacks/handlers/tracer.cjs +34 -18
  9. package/dist/callbacks/handlers/tracer.d.ts +18 -16
  10. package/dist/callbacks/handlers/tracer.js +34 -18
  11. package/dist/chat_models/bedrock.cjs +260 -0
  12. package/dist/chat_models/bedrock.d.ts +58 -0
  13. package/dist/chat_models/bedrock.js +254 -0
  14. package/dist/document_loaders/web/notionapi.cjs +8 -4
  15. package/dist/document_loaders/web/notionapi.js +8 -4
  16. package/dist/document_loaders/web/searchapi.cjs +134 -0
  17. package/dist/document_loaders/web/searchapi.d.ts +65 -0
  18. package/dist/document_loaders/web/searchapi.js +130 -0
  19. package/dist/embeddings/cloudflare_workersai.cjs +69 -0
  20. package/dist/embeddings/cloudflare_workersai.d.ts +28 -0
  21. package/dist/embeddings/cloudflare_workersai.js +65 -0
  22. package/dist/llms/bedrock.cjs +57 -67
  23. package/dist/llms/bedrock.d.ts +8 -35
  24. package/dist/llms/bedrock.js +57 -67
  25. package/dist/load/import_constants.cjs +4 -0
  26. package/dist/load/import_constants.js +4 -0
  27. package/dist/load/import_map.cjs +3 -2
  28. package/dist/load/import_map.d.ts +1 -0
  29. package/dist/load/import_map.js +1 -0
  30. package/dist/schema/runnable/base.cjs +64 -5
  31. package/dist/schema/runnable/base.d.ts +13 -0
  32. package/dist/schema/runnable/base.js +64 -5
  33. package/dist/tools/index.cjs +3 -1
  34. package/dist/tools/index.d.ts +1 -0
  35. package/dist/tools/index.js +1 -0
  36. package/dist/tools/searchapi.cjs +139 -0
  37. package/dist/tools/searchapi.d.ts +64 -0
  38. package/dist/tools/searchapi.js +135 -0
  39. package/dist/util/bedrock.cjs +54 -0
  40. package/dist/util/bedrock.d.ts +59 -0
  41. package/dist/util/bedrock.js +50 -0
  42. package/dist/util/fast-json-patch/index.cjs +48 -0
  43. package/dist/util/fast-json-patch/index.d.ts +21 -0
  44. package/dist/util/fast-json-patch/index.js +15 -0
  45. package/dist/util/fast-json-patch/src/core.cjs +469 -0
  46. package/dist/util/fast-json-patch/src/core.d.ts +111 -0
  47. package/dist/util/fast-json-patch/src/core.js +459 -0
  48. package/dist/util/fast-json-patch/src/helpers.cjs +194 -0
  49. package/dist/util/fast-json-patch/src/helpers.d.ts +36 -0
  50. package/dist/util/fast-json-patch/src/helpers.js +181 -0
  51. package/dist/util/googlevertexai-webauth.cjs +6 -2
  52. package/dist/util/googlevertexai-webauth.d.ts +1 -0
  53. package/dist/util/googlevertexai-webauth.js +6 -2
  54. package/dist/util/stream.cjs +2 -40
  55. package/dist/util/stream.d.ts +1 -2
  56. package/dist/util/stream.js +1 -38
  57. package/dist/vectorstores/cloudflare_vectorize.cjs +200 -0
  58. package/dist/vectorstores/cloudflare_vectorize.d.ts +90 -0
  59. package/dist/vectorstores/cloudflare_vectorize.js +173 -0
  60. package/dist/vectorstores/pgvector.cjs +1 -1
  61. package/dist/vectorstores/pgvector.js +1 -1
  62. package/dist/vectorstores/supabase.d.ts +1 -1
  63. package/dist/vectorstores/vercel_postgres.cjs +300 -0
  64. package/dist/vectorstores/vercel_postgres.d.ts +145 -0
  65. package/dist/vectorstores/vercel_postgres.js +296 -0
  66. package/document_loaders/web/searchapi.cjs +1 -0
  67. package/document_loaders/web/searchapi.d.ts +1 -0
  68. package/document_loaders/web/searchapi.js +1 -0
  69. package/embeddings/cloudflare_workersai.cjs +1 -0
  70. package/embeddings/cloudflare_workersai.d.ts +1 -0
  71. package/embeddings/cloudflare_workersai.js +1 -0
  72. package/package.json +60 -14
  73. package/vectorstores/cloudflare_vectorize.cjs +1 -0
  74. package/vectorstores/cloudflare_vectorize.d.ts +1 -0
  75. package/vectorstores/cloudflare_vectorize.js +1 -0
  76. package/vectorstores/vercel_postgres.cjs +1 -0
  77. package/vectorstores/vercel_postgres.d.ts +1 -0
  78. package/vectorstores/vercel_postgres.js +1 -0
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/chat_models/bedrock.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/bedrock.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/chat_models/bedrock.js'
@@ -35,7 +35,8 @@ declare abstract class BaseCallbackHandlerMethodsClass {
35
35
  * Called at the start of an LLM or Chat Model run, with the prompt(s)
36
36
  * and the run ID.
37
37
  */
38
- handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>): Promise<void> | void;
38
+ handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
+ Promise<any> | any;
39
40
  /**
40
41
  * Called when an LLM/ChatModel in `streaming` mode produces a new token
41
42
  */
@@ -46,50 +47,60 @@ declare abstract class BaseCallbackHandlerMethodsClass {
46
47
  * idx.completion is the index of the completion that produced the token
47
48
  * (if multiple completions per prompt are requested)
48
49
  */
49
- idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void> | void;
50
+ idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[], fields?: HandleLLMNewTokenCallbackFields): // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ Promise<any> | any;
50
52
  /**
51
53
  * Called if an LLM/ChatModel run encounters an error
52
54
  */
53
- handleLLMError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
55
+ handleLLMError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ Promise<any> | any;
54
57
  /**
55
58
  * Called at the end of an LLM/ChatModel run, with the output and the run ID.
56
59
  */
57
- handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
60
+ handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ Promise<any> | any;
58
62
  /**
59
63
  * Called at the start of a Chat Model run, with the prompt(s)
60
64
  * and the run ID.
61
65
  */
62
- handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>): Promise<void> | void;
66
+ handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
67
+ Promise<any> | any;
63
68
  /**
64
69
  * Called at the start of a Chain run, with the chain name and inputs
65
70
  * and the run ID.
66
71
  */
67
- handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string): Promise<void> | void;
72
+ handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
+ Promise<any> | any;
68
74
  /**
69
75
  * Called if a Chain run encounters an error
70
76
  */
71
77
  handleChainError?(err: Error, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
72
78
  inputs?: Record<string, unknown>;
73
- }): Promise<void> | void;
79
+ }): // eslint-disable-next-line @typescript-eslint/no-explicit-any
80
+ Promise<any> | any;
74
81
  /**
75
82
  * Called at the end of a Chain run, with the outputs and the run ID.
76
83
  */
77
84
  handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
78
85
  inputs?: Record<string, unknown>;
79
- }): Promise<void> | void;
86
+ }): // eslint-disable-next-line @typescript-eslint/no-explicit-any
87
+ Promise<any> | any;
80
88
  /**
81
89
  * Called at the start of a Tool run, with the tool name and input
82
90
  * and the run ID.
83
91
  */
84
- handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>): Promise<void> | void;
92
+ handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
+ Promise<any> | any;
85
94
  /**
86
95
  * Called if a Tool run encounters an error
87
96
  */
88
- handleToolError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
97
+ handleToolError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
+ Promise<any> | any;
89
99
  /**
90
100
  * Called at the end of a Tool run, with the tool output and the run ID.
91
101
  */
92
- handleToolEnd?(output: string, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
102
+ handleToolEnd?(output: string, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
+ Promise<any> | any;
93
104
  handleText?(text: string, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
94
105
  /**
95
106
  * Called when an agent is about to execute an action,
@@ -101,9 +112,12 @@ declare abstract class BaseCallbackHandlerMethodsClass {
101
112
  * with the final output and the run ID.
102
113
  */
103
114
  handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
104
- handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>): Promise<void> | void;
105
- handleRetrieverEnd?(documents: Document[], runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
106
- handleRetrieverError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
115
+ handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
116
+ Promise<any> | any;
117
+ handleRetrieverEnd?(documents: Document[], runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
118
+ Promise<any> | any;
119
+ handleRetrieverError?(err: Error, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
+ Promise<any> | any;
107
121
  }
108
122
  /**
109
123
  * Base interface for callbacks. All methods are optional. If a method is not
@@ -185,54 +199,54 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
185
199
  * Called at the start of an LLM or Chat Model run, with the prompt(s)
186
200
  * and the run ID.
187
201
  */
188
- handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): void | Promise<void>;
202
+ handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
189
203
  /**
190
204
  * Called when an LLM/ChatModel in `streaming` mode produces a new token
191
205
  */
192
- handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, fields?: HandleLLMNewTokenCallbackFields | undefined): void | Promise<void>;
206
+ handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, fields?: HandleLLMNewTokenCallbackFields | undefined): any;
193
207
  /**
194
208
  * Called if an LLM/ChatModel run encounters an error
195
209
  */
196
- handleLLMError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
210
+ handleLLMError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
197
211
  /**
198
212
  * Called at the end of an LLM/ChatModel run, with the output and the run ID.
199
213
  */
200
- handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
214
+ handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
201
215
  /**
202
216
  * Called at the start of a Chat Model run, with the prompt(s)
203
217
  * and the run ID.
204
218
  */
205
- handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): void | Promise<void>;
219
+ handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
206
220
  /**
207
221
  * Called at the start of a Chain run, with the chain name and inputs
208
222
  * and the run ID.
209
223
  */
210
- handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined): void | Promise<void>;
224
+ handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined): any;
211
225
  /**
212
226
  * Called if a Chain run encounters an error
213
227
  */
214
228
  handleChainError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
215
229
  inputs?: Record<string, unknown> | undefined;
216
- } | undefined): void | Promise<void>;
230
+ } | undefined): any;
217
231
  /**
218
232
  * Called at the end of a Chain run, with the outputs and the run ID.
219
233
  */
220
234
  handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
221
235
  inputs?: Record<string, unknown> | undefined;
222
- } | undefined): void | Promise<void>;
236
+ } | undefined): any;
223
237
  /**
224
238
  * Called at the start of a Tool run, with the tool name and input
225
239
  * and the run ID.
226
240
  */
227
- handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): void | Promise<void>;
241
+ handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
228
242
  /**
229
243
  * Called if a Tool run encounters an error
230
244
  */
231
- handleToolError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
245
+ handleToolError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
232
246
  /**
233
247
  * Called at the end of a Tool run, with the tool output and the run ID.
234
248
  */
235
- handleToolEnd?(output: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
249
+ handleToolEnd?(output: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
236
250
  handleText?(text: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
237
251
  /**
238
252
  * Called when an agent is about to execute an action,
@@ -244,9 +258,9 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
244
258
  * with the final output and the run ID.
245
259
  */
246
260
  handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
247
- handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): void | Promise<void>;
248
- handleRetrieverEnd?(documents: Document<Record<string, any>>[], runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
249
- handleRetrieverError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
261
+ handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
262
+ handleRetrieverEnd?(documents: Document<Record<string, any>>[], runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
263
+ handleRetrieverError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
250
264
  };
251
265
  }
252
266
  export {};
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LogStreamCallbackHandler = exports.RunLog = exports.RunLogPatch = void 0;
4
+ const index_js_1 = require("../../util/fast-json-patch/index.cjs");
5
+ const tracer_js_1 = require("./tracer.cjs");
6
+ const stream_js_1 = require("../../util/stream.cjs");
7
+ /**
8
+ * List of jsonpatch JSONPatchOperations, which describe how to create the run state
9
+ * from an empty dict. This is the minimal representation of the log, designed to
10
+ * be serialized as JSON and sent over the wire to reconstruct the log on the other
11
+ * side. Reconstruction of the state can be done with any jsonpatch-compliant library,
12
+ * see https://jsonpatch.com for more information.
13
+ */
14
+ class RunLogPatch {
15
+ constructor(fields) {
16
+ Object.defineProperty(this, "ops", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ this.ops = fields.ops;
23
+ }
24
+ concat(other) {
25
+ const ops = this.ops.concat(other.ops);
26
+ const states = (0, index_js_1.applyPatch)({}, ops);
27
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
28
+ return new RunLog({
29
+ ops,
30
+ state: states[states.length - 1].newDocument,
31
+ });
32
+ }
33
+ }
34
+ exports.RunLogPatch = RunLogPatch;
35
+ class RunLog extends RunLogPatch {
36
+ constructor(fields) {
37
+ super(fields);
38
+ Object.defineProperty(this, "state", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ this.state = fields.state;
45
+ }
46
+ concat(other) {
47
+ const ops = this.ops.concat(other.ops);
48
+ const states = (0, index_js_1.applyPatch)(this.state, other.ops);
49
+ return new RunLog({ ops, state: states[states.length - 1].newDocument });
50
+ }
51
+ }
52
+ exports.RunLog = RunLog;
53
+ /**
54
+ * Class that extends the `BaseTracer` class from the
55
+ * `langchain.callbacks.tracers.base` module. It represents a callback
56
+ * handler that logs the execution of runs and emits `RunLog` instances to a
57
+ * `RunLogStream`.
58
+ */
59
+ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
60
+ constructor(fields) {
61
+ super(fields);
62
+ Object.defineProperty(this, "autoClose", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: true
67
+ });
68
+ Object.defineProperty(this, "includeNames", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: void 0
73
+ });
74
+ Object.defineProperty(this, "includeTypes", {
75
+ enumerable: true,
76
+ configurable: true,
77
+ writable: true,
78
+ value: void 0
79
+ });
80
+ Object.defineProperty(this, "includeTags", {
81
+ enumerable: true,
82
+ configurable: true,
83
+ writable: true,
84
+ value: void 0
85
+ });
86
+ Object.defineProperty(this, "excludeNames", {
87
+ enumerable: true,
88
+ configurable: true,
89
+ writable: true,
90
+ value: void 0
91
+ });
92
+ Object.defineProperty(this, "excludeTypes", {
93
+ enumerable: true,
94
+ configurable: true,
95
+ writable: true,
96
+ value: void 0
97
+ });
98
+ Object.defineProperty(this, "excludeTags", {
99
+ enumerable: true,
100
+ configurable: true,
101
+ writable: true,
102
+ value: void 0
103
+ });
104
+ Object.defineProperty(this, "indexMap", {
105
+ enumerable: true,
106
+ configurable: true,
107
+ writable: true,
108
+ value: {}
109
+ });
110
+ Object.defineProperty(this, "transformStream", {
111
+ enumerable: true,
112
+ configurable: true,
113
+ writable: true,
114
+ value: void 0
115
+ });
116
+ Object.defineProperty(this, "writer", {
117
+ enumerable: true,
118
+ configurable: true,
119
+ writable: true,
120
+ value: void 0
121
+ });
122
+ Object.defineProperty(this, "receiveStream", {
123
+ enumerable: true,
124
+ configurable: true,
125
+ writable: true,
126
+ value: void 0
127
+ });
128
+ Object.defineProperty(this, "name", {
129
+ enumerable: true,
130
+ configurable: true,
131
+ writable: true,
132
+ value: "log_stream_tracer"
133
+ });
134
+ this.autoClose = fields?.autoClose ?? true;
135
+ this.includeNames = fields?.includeNames;
136
+ this.includeTypes = fields?.includeTypes;
137
+ this.includeTags = fields?.includeTags;
138
+ this.excludeNames = fields?.excludeNames;
139
+ this.excludeTypes = fields?.excludeTypes;
140
+ this.excludeTags = fields?.excludeTags;
141
+ this.transformStream = new TransformStream();
142
+ this.writer = this.transformStream.writable.getWriter();
143
+ this.receiveStream = stream_js_1.IterableReadableStream.fromReadableStream(this.transformStream.readable);
144
+ }
145
+ [Symbol.asyncIterator]() {
146
+ return this.receiveStream;
147
+ }
148
+ async persistRun(_run) {
149
+ // This is a legacy method only called once for an entire run tree
150
+ // and is therefore not useful here
151
+ }
152
+ _includeRun(run) {
153
+ if (run.parent_run_id === undefined) {
154
+ return false;
155
+ }
156
+ const runTags = run.tags ?? [];
157
+ let include = this.includeNames === undefined &&
158
+ this.includeTags === undefined &&
159
+ this.includeTypes === undefined;
160
+ if (this.includeNames !== undefined) {
161
+ include = include || this.includeNames.includes(run.name);
162
+ }
163
+ if (this.includeTypes !== undefined) {
164
+ include = include || this.includeTypes.includes(run.run_type);
165
+ }
166
+ if (this.includeTags !== undefined) {
167
+ include =
168
+ include ||
169
+ runTags.find((tag) => this.includeTags?.includes(tag)) !== undefined;
170
+ }
171
+ if (this.excludeNames !== undefined) {
172
+ include = include && !this.excludeNames.includes(run.name);
173
+ }
174
+ if (this.excludeTypes !== undefined) {
175
+ include = include && !this.excludeTypes.includes(run.run_type);
176
+ }
177
+ if (this.excludeTags !== undefined) {
178
+ include =
179
+ include && runTags.every((tag) => !this.excludeTags?.includes(tag));
180
+ }
181
+ return include;
182
+ }
183
+ async onRunCreate(run) {
184
+ if (run.parent_run_id === undefined) {
185
+ await this.writer.write(new RunLogPatch({
186
+ ops: [
187
+ {
188
+ op: "replace",
189
+ path: "",
190
+ value: {
191
+ id: run.id,
192
+ streamed_output: [],
193
+ final_output: undefined,
194
+ logs: [],
195
+ },
196
+ },
197
+ ],
198
+ }));
199
+ }
200
+ if (!this._includeRun(run)) {
201
+ return;
202
+ }
203
+ this.indexMap[run.id] = Math.max(...Object.values(this.indexMap), -1) + 1;
204
+ const logEntry = {
205
+ id: run.id,
206
+ name: run.name,
207
+ type: run.run_type,
208
+ tags: run.tags ?? [],
209
+ metadata: run.extra?.metadata ?? {},
210
+ start_time: new Date(run.start_time).toISOString(),
211
+ streamed_output_str: [],
212
+ final_output: undefined,
213
+ end_time: undefined,
214
+ };
215
+ await this.writer.write(new RunLogPatch({
216
+ ops: [
217
+ {
218
+ op: "add",
219
+ path: `/logs/${this.indexMap[run.id]}`,
220
+ value: logEntry,
221
+ },
222
+ ],
223
+ }));
224
+ }
225
+ async onRunUpdate(run) {
226
+ try {
227
+ const index = this.indexMap[run.id];
228
+ if (index === undefined) {
229
+ return;
230
+ }
231
+ const ops = [
232
+ {
233
+ op: "add",
234
+ path: `/logs/${index}/final_output`,
235
+ value: run.outputs,
236
+ },
237
+ ];
238
+ if (run.end_time !== undefined) {
239
+ ops.push({
240
+ op: "add",
241
+ path: `/logs/${index}/end_time`,
242
+ value: new Date(run.end_time).toISOString(),
243
+ });
244
+ }
245
+ const patch = new RunLogPatch({ ops });
246
+ await this.writer.write(patch);
247
+ }
248
+ finally {
249
+ if (run.parent_run_id === undefined) {
250
+ const patch = new RunLogPatch({
251
+ ops: [
252
+ {
253
+ op: "replace",
254
+ path: "/final_output",
255
+ value: run.outputs,
256
+ },
257
+ ],
258
+ });
259
+ await this.writer.write(patch);
260
+ if (this.autoClose) {
261
+ await this.writer.close();
262
+ }
263
+ }
264
+ }
265
+ }
266
+ async onLLMNewToken(run, token) {
267
+ const index = this.indexMap[run.id];
268
+ if (index === undefined) {
269
+ return;
270
+ }
271
+ const patch = new RunLogPatch({
272
+ ops: [
273
+ {
274
+ op: "add",
275
+ path: `/logs/${index}/streamed_output_str/-`,
276
+ value: token,
277
+ },
278
+ ],
279
+ });
280
+ await this.writer.write(patch);
281
+ }
282
+ }
283
+ exports.LogStreamCallbackHandler = LogStreamCallbackHandler;
@@ -0,0 +1,99 @@
1
+ import { type Operation as JSONPatchOperation } from "../../util/fast-json-patch/index.js";
2
+ import { BaseTracer, type Run } from "./tracer.js";
3
+ import { BaseCallbackHandlerInput } from "../base.js";
4
+ import { IterableReadableStream } from "../../util/stream.js";
5
+ /**
6
+ * Interface that represents the structure of a log entry in the
7
+ * `LogStreamCallbackHandler`.
8
+ */
9
+ export type LogEntry = {
10
+ /** ID of the sub-run. */
11
+ id: string;
12
+ /** Name of the object being run. */
13
+ name: string;
14
+ /** Type of the object being run, eg. prompt, chain, llm, etc. */
15
+ type: string;
16
+ /** List of tags for the run. */
17
+ tags: string[];
18
+ /** Key-value pairs of metadata for the run. */
19
+ metadata: Record<string, any>;
20
+ /** ISO-8601 timestamp of when the run started. */
21
+ start_time: string;
22
+ /** List of LLM tokens streamed by this run, if applicable. */
23
+ streamed_output_str: string[];
24
+ /** Final output of this run. Only available after the run has finished successfully. */
25
+ final_output?: any;
26
+ /** ISO-8601 timestamp of when the run ended. Only available after the run has finished. */
27
+ end_time?: string;
28
+ };
29
+ export type RunState = {
30
+ /** ID of the sub-run. */
31
+ id: string;
32
+ /** List of output chunks streamed by Runnable.stream() */
33
+ streamed_output: any[];
34
+ /** Final output of the run, usually the result of aggregating streamed_output. Only available after the run has finished successfully. */
35
+ final_output?: any;
36
+ /**
37
+ * List of sub-runs contained in this run, if any, in the order they were started.
38
+ * If filters were supplied, this list will contain only the runs that matched the filters.
39
+ */
40
+ logs: LogEntry[];
41
+ };
42
+ /**
43
+ * List of jsonpatch JSONPatchOperations, which describe how to create the run state
44
+ * from an empty dict. This is the minimal representation of the log, designed to
45
+ * be serialized as JSON and sent over the wire to reconstruct the log on the other
46
+ * side. Reconstruction of the state can be done with any jsonpatch-compliant library,
47
+ * see https://jsonpatch.com for more information.
48
+ */
49
+ export declare class RunLogPatch {
50
+ ops: JSONPatchOperation[];
51
+ constructor(fields: {
52
+ ops: JSONPatchOperation[];
53
+ });
54
+ concat(other: RunLogPatch): RunLog;
55
+ }
56
+ export declare class RunLog extends RunLogPatch {
57
+ state: RunState;
58
+ constructor(fields: {
59
+ ops: JSONPatchOperation[];
60
+ state: RunState;
61
+ });
62
+ concat(other: RunLogPatch): RunLog;
63
+ }
64
+ export interface LogStreamCallbackHandlerInput extends BaseCallbackHandlerInput {
65
+ autoClose?: boolean;
66
+ includeNames?: string[];
67
+ includeTypes?: string[];
68
+ includeTags?: string[];
69
+ excludeNames?: string[];
70
+ excludeTypes?: string[];
71
+ excludeTags?: string[];
72
+ }
73
+ /**
74
+ * Class that extends the `BaseTracer` class from the
75
+ * `langchain.callbacks.tracers.base` module. It represents a callback
76
+ * handler that logs the execution of runs and emits `RunLog` instances to a
77
+ * `RunLogStream`.
78
+ */
79
+ export declare class LogStreamCallbackHandler extends BaseTracer {
80
+ protected autoClose: boolean;
81
+ protected includeNames?: string[];
82
+ protected includeTypes?: string[];
83
+ protected includeTags?: string[];
84
+ protected excludeNames?: string[];
85
+ protected excludeTypes?: string[];
86
+ protected excludeTags?: string[];
87
+ private indexMap;
88
+ protected transformStream: TransformStream;
89
+ writer: WritableStreamDefaultWriter;
90
+ receiveStream: IterableReadableStream<RunLogPatch>;
91
+ name: string;
92
+ constructor(fields?: LogStreamCallbackHandlerInput);
93
+ [Symbol.asyncIterator](): IterableReadableStream<RunLogPatch>;
94
+ protected persistRun(_run: Run): Promise<void>;
95
+ _includeRun(run: Run): boolean;
96
+ onRunCreate(run: Run): Promise<void>;
97
+ onRunUpdate(run: Run): Promise<void>;
98
+ onLLMNewToken(run: Run, token: string): Promise<void>;
99
+ }