illuma-agents 1.0.2 → 1.0.4

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 (225) hide show
  1. package/LICENSE +25 -21
  2. package/dist/cjs/agents/AgentContext.cjs +222 -0
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -0
  4. package/dist/cjs/common/enum.cjs +5 -4
  5. package/dist/cjs/common/enum.cjs.map +1 -1
  6. package/dist/cjs/events.cjs +7 -5
  7. package/dist/cjs/events.cjs.map +1 -1
  8. package/dist/cjs/graphs/Graph.cjs +328 -207
  9. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
  11. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
  12. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  13. package/dist/cjs/llm/google/index.cjs.map +1 -1
  14. package/dist/cjs/llm/ollama/index.cjs.map +1 -1
  15. package/dist/cjs/llm/openai/index.cjs +35 -0
  16. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  17. package/dist/cjs/llm/openai/utils/index.cjs +3 -1
  18. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  19. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  20. package/dist/cjs/llm/providers.cjs +0 -2
  21. package/dist/cjs/llm/providers.cjs.map +1 -1
  22. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  23. package/dist/cjs/main.cjs +12 -1
  24. package/dist/cjs/main.cjs.map +1 -1
  25. package/dist/cjs/messages/cache.cjs +123 -0
  26. package/dist/cjs/messages/cache.cjs.map +1 -0
  27. package/dist/cjs/messages/content.cjs +53 -0
  28. package/dist/cjs/messages/content.cjs.map +1 -0
  29. package/dist/cjs/messages/format.cjs +17 -29
  30. package/dist/cjs/messages/format.cjs.map +1 -1
  31. package/dist/cjs/run.cjs +119 -74
  32. package/dist/cjs/run.cjs.map +1 -1
  33. package/dist/cjs/stream.cjs +77 -73
  34. package/dist/cjs/stream.cjs.map +1 -1
  35. package/dist/cjs/tools/Calculator.cjs +45 -0
  36. package/dist/cjs/tools/Calculator.cjs.map +1 -0
  37. package/dist/cjs/tools/CodeExecutor.cjs +22 -22
  38. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  39. package/dist/cjs/tools/ToolNode.cjs +5 -3
  40. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  41. package/dist/cjs/tools/handlers.cjs +20 -20
  42. package/dist/cjs/tools/handlers.cjs.map +1 -1
  43. package/dist/cjs/utils/events.cjs +31 -0
  44. package/dist/cjs/utils/events.cjs.map +1 -0
  45. package/dist/cjs/utils/handlers.cjs +70 -0
  46. package/dist/cjs/utils/handlers.cjs.map +1 -0
  47. package/dist/cjs/utils/tokens.cjs +54 -7
  48. package/dist/cjs/utils/tokens.cjs.map +1 -1
  49. package/dist/esm/agents/AgentContext.mjs +220 -0
  50. package/dist/esm/agents/AgentContext.mjs.map +1 -0
  51. package/dist/esm/common/enum.mjs +5 -4
  52. package/dist/esm/common/enum.mjs.map +1 -1
  53. package/dist/esm/events.mjs +7 -5
  54. package/dist/esm/events.mjs.map +1 -1
  55. package/dist/esm/graphs/Graph.mjs +330 -209
  56. package/dist/esm/graphs/Graph.mjs.map +1 -1
  57. package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
  58. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
  59. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  60. package/dist/esm/llm/google/index.mjs.map +1 -1
  61. package/dist/esm/llm/ollama/index.mjs.map +1 -1
  62. package/dist/esm/llm/openai/index.mjs +35 -0
  63. package/dist/esm/llm/openai/index.mjs.map +1 -1
  64. package/dist/esm/llm/openai/utils/index.mjs +3 -1
  65. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  66. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  67. package/dist/esm/llm/providers.mjs +0 -2
  68. package/dist/esm/llm/providers.mjs.map +1 -1
  69. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  70. package/dist/esm/main.mjs +7 -2
  71. package/dist/esm/main.mjs.map +1 -1
  72. package/dist/esm/messages/cache.mjs +120 -0
  73. package/dist/esm/messages/cache.mjs.map +1 -0
  74. package/dist/esm/messages/content.mjs +51 -0
  75. package/dist/esm/messages/content.mjs.map +1 -0
  76. package/dist/esm/messages/format.mjs +18 -29
  77. package/dist/esm/messages/format.mjs.map +1 -1
  78. package/dist/esm/run.mjs +119 -74
  79. package/dist/esm/run.mjs.map +1 -1
  80. package/dist/esm/stream.mjs +77 -73
  81. package/dist/esm/stream.mjs.map +1 -1
  82. package/dist/esm/tools/Calculator.mjs +24 -0
  83. package/dist/esm/tools/Calculator.mjs.map +1 -0
  84. package/dist/esm/tools/CodeExecutor.mjs +22 -22
  85. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  86. package/dist/esm/tools/ToolNode.mjs +5 -3
  87. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  88. package/dist/esm/tools/handlers.mjs +20 -20
  89. package/dist/esm/tools/handlers.mjs.map +1 -1
  90. package/dist/esm/utils/events.mjs +29 -0
  91. package/dist/esm/utils/events.mjs.map +1 -0
  92. package/dist/esm/utils/handlers.mjs +68 -0
  93. package/dist/esm/utils/handlers.mjs.map +1 -0
  94. package/dist/esm/utils/tokens.mjs +54 -8
  95. package/dist/esm/utils/tokens.mjs.map +1 -1
  96. package/dist/types/agents/AgentContext.d.ts +94 -0
  97. package/dist/types/common/enum.d.ts +7 -5
  98. package/dist/types/events.d.ts +3 -3
  99. package/dist/types/graphs/Graph.d.ts +60 -66
  100. package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
  101. package/dist/types/graphs/index.d.ts +1 -0
  102. package/dist/types/index.d.ts +1 -0
  103. package/dist/types/llm/openai/index.d.ts +10 -0
  104. package/dist/types/messages/cache.d.ts +20 -0
  105. package/dist/types/messages/content.d.ts +7 -0
  106. package/dist/types/messages/format.d.ts +1 -7
  107. package/dist/types/messages/index.d.ts +2 -0
  108. package/dist/types/messages/reducer.d.ts +9 -0
  109. package/dist/types/run.d.ts +16 -10
  110. package/dist/types/stream.d.ts +4 -3
  111. package/dist/types/tools/Calculator.d.ts +8 -0
  112. package/dist/types/tools/ToolNode.d.ts +1 -1
  113. package/dist/types/tools/handlers.d.ts +9 -7
  114. package/dist/types/tools/search/tool.d.ts +4 -4
  115. package/dist/types/types/graph.d.ts +124 -11
  116. package/dist/types/types/llm.d.ts +13 -9
  117. package/dist/types/types/messages.d.ts +4 -0
  118. package/dist/types/types/run.d.ts +46 -8
  119. package/dist/types/types/stream.d.ts +3 -2
  120. package/dist/types/utils/events.d.ts +6 -0
  121. package/dist/types/utils/handlers.d.ts +34 -0
  122. package/dist/types/utils/index.d.ts +1 -0
  123. package/dist/types/utils/tokens.d.ts +24 -0
  124. package/package.json +162 -145
  125. package/src/agents/AgentContext.ts +323 -0
  126. package/src/common/enum.ts +177 -176
  127. package/src/events.ts +197 -191
  128. package/src/graphs/Graph.ts +1058 -846
  129. package/src/graphs/MultiAgentGraph.ts +598 -0
  130. package/src/graphs/index.ts +2 -1
  131. package/src/index.ts +25 -24
  132. package/src/llm/anthropic/index.ts +413 -413
  133. package/src/llm/google/index.ts +222 -222
  134. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -88
  135. package/src/llm/ollama/index.ts +92 -92
  136. package/src/llm/openai/index.ts +894 -853
  137. package/src/llm/openai/utils/index.ts +920 -918
  138. package/src/llm/openrouter/index.ts +60 -60
  139. package/src/llm/providers.ts +55 -57
  140. package/src/llm/vertexai/index.ts +360 -360
  141. package/src/messages/cache.test.ts +461 -0
  142. package/src/messages/cache.ts +151 -0
  143. package/src/messages/content.test.ts +362 -0
  144. package/src/messages/content.ts +63 -0
  145. package/src/messages/format.ts +611 -625
  146. package/src/messages/formatAgentMessages.test.ts +1144 -917
  147. package/src/messages/index.ts +6 -4
  148. package/src/messages/reducer.ts +80 -0
  149. package/src/run.ts +447 -381
  150. package/src/scripts/abort.ts +157 -138
  151. package/src/scripts/ant_web_search.ts +158 -158
  152. package/src/scripts/cli.ts +172 -167
  153. package/src/scripts/cli2.ts +133 -125
  154. package/src/scripts/cli3.ts +184 -178
  155. package/src/scripts/cli4.ts +191 -184
  156. package/src/scripts/cli5.ts +191 -184
  157. package/src/scripts/code_exec.ts +213 -214
  158. package/src/scripts/code_exec_simple.ts +147 -129
  159. package/src/scripts/content.ts +138 -120
  160. package/src/scripts/handoff-test.ts +135 -0
  161. package/src/scripts/multi-agent-chain.ts +278 -0
  162. package/src/scripts/multi-agent-conditional.ts +220 -0
  163. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  164. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  165. package/src/scripts/multi-agent-parallel.ts +343 -0
  166. package/src/scripts/multi-agent-sequence.ts +212 -0
  167. package/src/scripts/multi-agent-supervisor.ts +364 -0
  168. package/src/scripts/multi-agent-test.ts +186 -0
  169. package/src/scripts/search.ts +146 -150
  170. package/src/scripts/simple.ts +225 -225
  171. package/src/scripts/stream.ts +140 -122
  172. package/src/scripts/test-custom-prompt-key.ts +145 -0
  173. package/src/scripts/test-handoff-input.ts +170 -0
  174. package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
  175. package/src/scripts/test-tools-before-handoff.ts +222 -0
  176. package/src/scripts/tools.ts +153 -155
  177. package/src/specs/agent-handoffs.test.ts +889 -0
  178. package/src/specs/anthropic.simple.test.ts +320 -317
  179. package/src/specs/azure.simple.test.ts +325 -316
  180. package/src/specs/openai.simple.test.ts +311 -316
  181. package/src/specs/openrouter.simple.test.ts +107 -0
  182. package/src/specs/prune.test.ts +758 -763
  183. package/src/specs/reasoning.test.ts +201 -165
  184. package/src/specs/thinking-prune.test.ts +769 -703
  185. package/src/specs/token-memoization.test.ts +39 -0
  186. package/src/stream.ts +664 -651
  187. package/src/tools/Calculator.test.ts +278 -0
  188. package/src/tools/Calculator.ts +25 -0
  189. package/src/tools/CodeExecutor.ts +220 -220
  190. package/src/tools/ToolNode.ts +170 -170
  191. package/src/tools/handlers.ts +341 -336
  192. package/src/types/graph.ts +372 -185
  193. package/src/types/llm.ts +141 -140
  194. package/src/types/messages.ts +4 -0
  195. package/src/types/run.ts +128 -89
  196. package/src/types/stream.ts +401 -400
  197. package/src/utils/events.ts +32 -0
  198. package/src/utils/handlers.ts +107 -0
  199. package/src/utils/index.ts +6 -5
  200. package/src/utils/llmConfig.ts +183 -183
  201. package/src/utils/tokens.ts +129 -70
  202. package/dist/types/scripts/abort.d.ts +0 -1
  203. package/dist/types/scripts/ant_web_search.d.ts +0 -1
  204. package/dist/types/scripts/args.d.ts +0 -7
  205. package/dist/types/scripts/caching.d.ts +0 -1
  206. package/dist/types/scripts/cli.d.ts +0 -1
  207. package/dist/types/scripts/cli2.d.ts +0 -1
  208. package/dist/types/scripts/cli3.d.ts +0 -1
  209. package/dist/types/scripts/cli4.d.ts +0 -1
  210. package/dist/types/scripts/cli5.d.ts +0 -1
  211. package/dist/types/scripts/code_exec.d.ts +0 -1
  212. package/dist/types/scripts/code_exec_files.d.ts +0 -1
  213. package/dist/types/scripts/code_exec_simple.d.ts +0 -1
  214. package/dist/types/scripts/content.d.ts +0 -1
  215. package/dist/types/scripts/empty_input.d.ts +0 -1
  216. package/dist/types/scripts/image.d.ts +0 -1
  217. package/dist/types/scripts/memory.d.ts +0 -1
  218. package/dist/types/scripts/search.d.ts +0 -1
  219. package/dist/types/scripts/simple.d.ts +0 -1
  220. package/dist/types/scripts/stream.d.ts +0 -1
  221. package/dist/types/scripts/thinking.d.ts +0 -1
  222. package/dist/types/scripts/tools.d.ts +0 -1
  223. package/dist/types/specs/spec.utils.d.ts +0 -1
  224. package/dist/types/tools/example.d.ts +0 -78
  225. package/src/tools/example.ts +0 -129
package/src/run.ts CHANGED
@@ -1,381 +1,447 @@
1
- // src/run.ts
2
- import './instrumentation';
3
- import { zodToJsonSchema } from 'zod-to-json-schema';
4
- import { CallbackHandler } from '@langfuse/langchain';
5
- import { PromptTemplate } from '@langchain/core/prompts';
6
- import { SystemMessage } from '@langchain/core/messages';
7
- import { RunnableLambda } from '@langchain/core/runnables';
8
- import { AzureChatOpenAI, ChatOpenAI } from '@langchain/openai';
9
- import type {
10
- MessageContentComplex,
11
- BaseMessage,
12
- } from '@langchain/core/messages';
13
- import type { StringPromptValue } from '@langchain/core/prompt_values';
14
- import type { RunnableConfig } from '@langchain/core/runnables';
15
- import type { ClientCallbacks, SystemCallbacks } from '@/graphs/Graph';
16
- import type * as t from '@/types';
17
- import { GraphEvents, Providers, Callback, TitleMethod } from '@/common';
18
- import { manualToolStreamProviders } from '@/llm/providers';
19
- import { shiftIndexTokenCountMap } from '@/messages/format';
20
- import {
21
- createTitleRunnable,
22
- createCompletionTitleRunnable,
23
- } from '@/utils/title';
24
- import { createTokenCounter } from '@/utils/tokens';
25
- import { StandardGraph } from '@/graphs/Graph';
26
- import { HandlerRegistry } from '@/events';
27
- import { isOpenAILike } from '@/utils/llm';
28
- import { isPresent } from '@/utils/misc';
29
-
30
- export const defaultOmitOptions = new Set([
31
- 'stream',
32
- 'thinking',
33
- 'streaming',
34
- 'maxTokens',
35
- 'clientOptions',
36
- 'thinkingConfig',
37
- 'thinkingBudget',
38
- 'includeThoughts',
39
- 'maxOutputTokens',
40
- 'additionalModelRequestFields',
41
- ]);
42
-
43
- export class Run<T extends t.BaseGraphState> {
44
- graphRunnable?: t.CompiledWorkflow<T, Partial<T>, string>;
45
- // private collab!: CollabGraph;
46
- // private taskManager!: TaskManager;
47
- private handlerRegistry: HandlerRegistry;
48
- id: string;
49
- Graph: StandardGraph | undefined;
50
- provider: Providers | undefined;
51
- returnContent: boolean = false;
52
-
53
- private constructor(config: Partial<t.RunConfig>) {
54
- const runId = config.runId ?? '';
55
- if (!runId) {
56
- throw new Error('Run ID not provided');
57
- }
58
-
59
- this.id = runId;
60
-
61
- const handlerRegistry = new HandlerRegistry();
62
-
63
- if (config.customHandlers) {
64
- for (const [eventType, handler] of Object.entries(
65
- config.customHandlers
66
- )) {
67
- handlerRegistry.register(eventType, handler);
68
- }
69
- }
70
-
71
- this.handlerRegistry = handlerRegistry;
72
-
73
- if (!config.graphConfig) {
74
- throw new Error('Graph config not provided');
75
- }
76
-
77
- if (config.graphConfig.type === 'standard' || !config.graphConfig.type) {
78
- this.provider = config.graphConfig.llmConfig.provider;
79
- this.graphRunnable = this.createStandardGraph(
80
- config.graphConfig
81
- ) as unknown as t.CompiledWorkflow<T, Partial<T>, string>;
82
- if (this.Graph) {
83
- this.Graph.handlerRegistry = handlerRegistry;
84
- }
85
- }
86
-
87
- this.returnContent = config.returnContent ?? false;
88
- }
89
-
90
- private createStandardGraph(
91
- config: t.StandardGraphConfig
92
- ): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {
93
- const { llmConfig, tools = [], ...graphInput } = config;
94
- const { provider, ...clientOptions } = llmConfig;
95
-
96
- const standardGraph = new StandardGraph({
97
- tools,
98
- provider,
99
- clientOptions,
100
- ...graphInput,
101
- runId: this.id,
102
- });
103
- this.Graph = standardGraph;
104
- return standardGraph.createWorkflow();
105
- }
106
-
107
- static async create<T extends t.BaseGraphState>(
108
- config: t.RunConfig
109
- ): Promise<Run<T>> {
110
- return new Run<T>(config);
111
- }
112
-
113
- getRunMessages(): BaseMessage[] | undefined {
114
- if (!this.Graph) {
115
- throw new Error(
116
- 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'
117
- );
118
- }
119
- return this.Graph.getRunMessages();
120
- }
121
-
122
- async processStream(
123
- inputs: t.IState,
124
- config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },
125
- streamOptions?: t.EventStreamOptions
126
- ): Promise<MessageContentComplex[] | undefined> {
127
- if (!this.graphRunnable) {
128
- throw new Error(
129
- 'Run not initialized. Make sure to use Run.create() to instantiate the Run.'
130
- );
131
- }
132
- if (!this.Graph) {
133
- throw new Error(
134
- 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'
135
- );
136
- }
137
-
138
- this.Graph.resetValues(streamOptions?.keepContent);
139
- const provider = this.Graph.provider;
140
- const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;
141
- if (streamOptions?.callbacks) {
142
- /* TODO: conflicts with callback manager */
143
- const callbacks = (config.callbacks as t.ProvidedCallbacks) ?? [];
144
- config.callbacks = callbacks.concat(
145
- this.getCallbacks(streamOptions.callbacks)
146
- );
147
- }
148
-
149
- if (
150
- isPresent(process.env.LANGFUSE_SECRET_KEY) &&
151
- isPresent(process.env.LANGFUSE_PUBLIC_KEY) &&
152
- isPresent(process.env.LANGFUSE_BASE_URL)
153
- ) {
154
- const userId = config.configurable?.user_id;
155
- const sessionId = config.configurable?.thread_id;
156
- const traceMetadata = {
157
- messageId: this.id,
158
- parentMessageId: config.configurable?.requestBody?.parentMessageId,
159
- };
160
- const handler = new CallbackHandler({
161
- userId,
162
- sessionId,
163
- traceMetadata,
164
- });
165
- config.callbacks = (
166
- (config.callbacks as t.ProvidedCallbacks) ?? []
167
- ).concat([handler]);
168
- }
169
-
170
- if (!this.id) {
171
- throw new Error('Run ID not provided');
172
- }
173
-
174
- const tokenCounter =
175
- streamOptions?.tokenCounter ??
176
- (streamOptions?.indexTokenCountMap
177
- ? await createTokenCounter()
178
- : undefined);
179
- const tools = this.Graph.tools as
180
- | Array<t.GenericTool | undefined>
181
- | undefined;
182
- const toolTokens = tokenCounter
183
- ? (tools?.reduce((acc, tool) => {
184
- if (!(tool as Partial<t.GenericTool>).schema) {
185
- return acc;
186
- }
187
-
188
- const jsonSchema = zodToJsonSchema(
189
- (tool?.schema as t.ZodObjectAny).describe(tool?.description ?? ''),
190
- tool?.name ?? ''
191
- );
192
- return (
193
- acc + tokenCounter(new SystemMessage(JSON.stringify(jsonSchema)))
194
- );
195
- }, 0) ?? 0)
196
- : 0;
197
- let instructionTokens = toolTokens;
198
- if (this.Graph.systemMessage && tokenCounter) {
199
- instructionTokens += tokenCounter(this.Graph.systemMessage);
200
- }
201
- const tokenMap = streamOptions?.indexTokenCountMap ?? {};
202
- if (this.Graph.systemMessage && instructionTokens > 0) {
203
- this.Graph.indexTokenCountMap = shiftIndexTokenCountMap(
204
- tokenMap,
205
- instructionTokens
206
- );
207
- } else if (instructionTokens > 0) {
208
- tokenMap[0] = tokenMap[0] + instructionTokens;
209
- this.Graph.indexTokenCountMap = tokenMap;
210
- } else {
211
- this.Graph.indexTokenCountMap = tokenMap;
212
- }
213
-
214
- this.Graph.maxContextTokens = streamOptions?.maxContextTokens;
215
- this.Graph.tokenCounter = tokenCounter;
216
-
217
- config.run_id = this.id;
218
- config.configurable = Object.assign(config.configurable ?? {}, {
219
- run_id: this.id,
220
- provider: this.provider,
221
- });
222
-
223
- const stream = this.graphRunnable.streamEvents(inputs, config, {
224
- raiseError: true,
225
- });
226
-
227
- for await (const event of stream) {
228
- const { data, name, metadata, ...info } = event;
229
-
230
- let eventName: t.EventName = info.event;
231
- if (
232
- hasTools &&
233
- manualToolStreamProviders.has(provider) &&
234
- eventName === GraphEvents.CHAT_MODEL_STREAM
235
- ) {
236
- /* Skipping CHAT_MODEL_STREAM event due to double-call edge case */
237
- continue;
238
- }
239
-
240
- if (eventName && eventName === GraphEvents.ON_CUSTOM_EVENT) {
241
- eventName = name;
242
- }
243
-
244
- const handler = this.handlerRegistry.getHandler(eventName);
245
- if (handler) {
246
- handler.handle(eventName, data, metadata, this.Graph);
247
- }
248
- }
249
-
250
- if (this.returnContent) {
251
- return this.Graph.getContentParts();
252
- }
253
- }
254
-
255
- private createSystemCallback<K extends keyof ClientCallbacks>(
256
- clientCallbacks: ClientCallbacks,
257
- key: K
258
- ): SystemCallbacks[K] {
259
- return ((...args: unknown[]) => {
260
- const clientCallback = clientCallbacks[key];
261
- if (clientCallback && this.Graph) {
262
- (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);
263
- }
264
- }) as SystemCallbacks[K];
265
- }
266
-
267
- getCallbacks(clientCallbacks: ClientCallbacks): SystemCallbacks {
268
- return {
269
- [Callback.TOOL_ERROR]: this.createSystemCallback(
270
- clientCallbacks,
271
- Callback.TOOL_ERROR
272
- ),
273
- [Callback.TOOL_START]: this.createSystemCallback(
274
- clientCallbacks,
275
- Callback.TOOL_START
276
- ),
277
- [Callback.TOOL_END]: this.createSystemCallback(
278
- clientCallbacks,
279
- Callback.TOOL_END
280
- ),
281
- };
282
- }
283
-
284
- async generateTitle({
285
- provider,
286
- inputText,
287
- contentParts,
288
- titlePrompt,
289
- clientOptions,
290
- chainOptions,
291
- skipLanguage,
292
- omitOptions = defaultOmitOptions,
293
- titleMethod = TitleMethod.COMPLETION,
294
- titlePromptTemplate,
295
- }: t.RunTitleOptions): Promise<{ language?: string; title?: string }> {
296
- if (
297
- chainOptions != null &&
298
- isPresent(process.env.LANGFUSE_SECRET_KEY) &&
299
- isPresent(process.env.LANGFUSE_PUBLIC_KEY) &&
300
- isPresent(process.env.LANGFUSE_BASE_URL)
301
- ) {
302
- const userId = chainOptions.configurable?.user_id;
303
- const sessionId = chainOptions.configurable?.thread_id;
304
- const traceMetadata = {
305
- messageId: 'title-' + this.id,
306
- };
307
- const handler = new CallbackHandler({
308
- userId,
309
- sessionId,
310
- traceMetadata,
311
- });
312
- chainOptions.callbacks = (
313
- (chainOptions.callbacks as t.ProvidedCallbacks) ?? []
314
- ).concat([handler]);
315
- }
316
-
317
- const convoTemplate = PromptTemplate.fromTemplate(
318
- titlePromptTemplate ?? 'User: {input}\nAI: {output}'
319
- );
320
-
321
- const response = contentParts
322
- .map((part) => {
323
- if (part?.type === 'text') return part.text;
324
- return '';
325
- })
326
- .join('\n');
327
-
328
- const model = this.Graph?.getNewModel({
329
- provider,
330
- omitOptions,
331
- clientOptions,
332
- });
333
- if (!model) {
334
- return { language: '', title: '' };
335
- }
336
- if (
337
- isOpenAILike(provider) &&
338
- (model instanceof ChatOpenAI || model instanceof AzureChatOpenAI)
339
- ) {
340
- model.temperature = (clientOptions as t.OpenAIClientOptions | undefined)
341
- ?.temperature as number;
342
- model.topP = (clientOptions as t.OpenAIClientOptions | undefined)
343
- ?.topP as number;
344
- model.frequencyPenalty = (
345
- clientOptions as t.OpenAIClientOptions | undefined
346
- )?.frequencyPenalty as number;
347
- model.presencePenalty = (
348
- clientOptions as t.OpenAIClientOptions | undefined
349
- )?.presencePenalty as number;
350
- model.n = (clientOptions as t.OpenAIClientOptions | undefined)
351
- ?.n as number;
352
- }
353
-
354
- const convoToTitleInput = new RunnableLambda({
355
- func: (
356
- promptValue: StringPromptValue
357
- ): { convo: string; inputText: string; skipLanguage?: boolean } => ({
358
- convo: promptValue.value,
359
- inputText,
360
- skipLanguage,
361
- }),
362
- }).withConfig({ runName: 'ConvoTransform' });
363
-
364
- const titleChain =
365
- titleMethod === TitleMethod.COMPLETION
366
- ? await createCompletionTitleRunnable(model, titlePrompt)
367
- : await createTitleRunnable(model, titlePrompt);
368
-
369
- /** Pipes `convoTemplate` -> `transformer` -> `titleChain` */
370
- const fullChain = convoTemplate
371
- .withConfig({ runName: 'ConvoTemplate' })
372
- .pipe(convoToTitleInput)
373
- .pipe(titleChain)
374
- .withConfig({ runName: 'TitleChain' });
375
-
376
- return await fullChain.invoke(
377
- { input: inputText, output: response },
378
- chainOptions
379
- );
380
- }
381
- }
1
+ // src/run.ts
2
+ import './instrumentation';
3
+ import { CallbackHandler } from '@langfuse/langchain';
4
+ import { PromptTemplate } from '@langchain/core/prompts';
5
+ import { RunnableLambda } from '@langchain/core/runnables';
6
+ import { AzureChatOpenAI, ChatOpenAI } from '@langchain/openai';
7
+ import type {
8
+ MessageContentComplex,
9
+ BaseMessage,
10
+ } from '@langchain/core/messages';
11
+ import type { StringPromptValue } from '@langchain/core/prompt_values';
12
+ import type { RunnableConfig } from '@langchain/core/runnables';
13
+ import type * as t from '@/types';
14
+ import {
15
+ createCompletionTitleRunnable,
16
+ createTitleRunnable,
17
+ } from '@/utils/title';
18
+ import { GraphEvents, Callback, TitleMethod } from '@/common';
19
+ import { MultiAgentGraph } from '@/graphs/MultiAgentGraph';
20
+ import { createTokenCounter } from '@/utils/tokens';
21
+ import { StandardGraph } from '@/graphs/Graph';
22
+ import { HandlerRegistry } from '@/events';
23
+ import { isOpenAILike } from '@/utils/llm';
24
+ import { isPresent } from '@/utils/misc';
25
+
26
+ export const defaultOmitOptions = new Set([
27
+ 'stream',
28
+ 'thinking',
29
+ 'streaming',
30
+ 'maxTokens',
31
+ 'clientOptions',
32
+ 'thinkingConfig',
33
+ 'thinkingBudget',
34
+ 'includeThoughts',
35
+ 'maxOutputTokens',
36
+ 'additionalModelRequestFields',
37
+ ]);
38
+
39
+ export class Run<_T extends t.BaseGraphState> {
40
+ id: string;
41
+ private tokenCounter?: t.TokenCounter;
42
+ private handlerRegistry?: HandlerRegistry;
43
+ private indexTokenCountMap?: Record<string, number>;
44
+ graphRunnable?: t.CompiledStateWorkflow;
45
+ Graph: StandardGraph | MultiAgentGraph | undefined;
46
+ returnContent: boolean = false;
47
+
48
+ private constructor(config: Partial<t.RunConfig>) {
49
+ const runId = config.runId ?? '';
50
+ if (!runId) {
51
+ throw new Error('Run ID not provided');
52
+ }
53
+
54
+ this.id = runId;
55
+ this.tokenCounter = config.tokenCounter;
56
+ this.indexTokenCountMap = config.indexTokenCountMap;
57
+
58
+ const handlerRegistry = new HandlerRegistry();
59
+
60
+ if (config.customHandlers) {
61
+ for (const [eventType, handler] of Object.entries(
62
+ config.customHandlers
63
+ )) {
64
+ handlerRegistry.register(eventType, handler);
65
+ }
66
+ }
67
+
68
+ this.handlerRegistry = handlerRegistry;
69
+
70
+ if (!config.graphConfig) {
71
+ throw new Error('Graph config not provided');
72
+ }
73
+
74
+ /** Handle different graph types */
75
+ if (config.graphConfig.type === 'multi-agent') {
76
+ this.graphRunnable = this.createMultiAgentGraph(config.graphConfig);
77
+ if (this.Graph) {
78
+ this.Graph.handlerRegistry = handlerRegistry;
79
+ }
80
+ } else {
81
+ /** Default to legacy graph for 'standard' or undefined type */
82
+ this.graphRunnable = this.createLegacyGraph(config.graphConfig);
83
+ if (this.Graph) {
84
+ this.Graph.compileOptions =
85
+ config.graphConfig.compileOptions ?? this.Graph.compileOptions;
86
+ this.Graph.handlerRegistry = handlerRegistry;
87
+ }
88
+ }
89
+
90
+ this.returnContent = config.returnContent ?? false;
91
+ }
92
+
93
+ private createLegacyGraph(
94
+ config: t.LegacyGraphConfig | t.StandardGraphConfig
95
+ ): t.CompiledStateWorkflow {
96
+ let agentConfig: t.AgentInputs;
97
+ let signal: AbortSignal | undefined;
98
+
99
+ /** Check if this is a multi-agent style config (has agents array) */
100
+ if ('agents' in config && Array.isArray(config.agents)) {
101
+ if (config.agents.length === 0) {
102
+ throw new Error('At least one agent must be provided');
103
+ }
104
+ agentConfig = config.agents[0];
105
+ signal = config.signal;
106
+ } else {
107
+ /** Legacy path: build agent config from llmConfig */
108
+ const {
109
+ type: _type,
110
+ llmConfig,
111
+ signal: legacySignal,
112
+ tools = [],
113
+ ...agentInputs
114
+ } = config as t.LegacyGraphConfig;
115
+ const { provider, ...clientOptions } = llmConfig;
116
+
117
+ agentConfig = {
118
+ ...agentInputs,
119
+ tools,
120
+ provider,
121
+ clientOptions,
122
+ agentId: 'default',
123
+ };
124
+ signal = legacySignal;
125
+ }
126
+
127
+ const standardGraph = new StandardGraph({
128
+ signal,
129
+ runId: this.id,
130
+ agents: [agentConfig],
131
+ tokenCounter: this.tokenCounter,
132
+ indexTokenCountMap: this.indexTokenCountMap,
133
+ });
134
+ /** Propagate compile options from graph config */
135
+ standardGraph.compileOptions = config.compileOptions;
136
+ this.Graph = standardGraph;
137
+ return standardGraph.createWorkflow();
138
+ }
139
+
140
+ private createMultiAgentGraph(
141
+ config: t.MultiAgentGraphConfig
142
+ ): t.CompiledStateWorkflow {
143
+ const { agents, edges, compileOptions } = config;
144
+
145
+ const multiAgentGraph = new MultiAgentGraph({
146
+ runId: this.id,
147
+ agents,
148
+ edges,
149
+ tokenCounter: this.tokenCounter,
150
+ indexTokenCountMap: this.indexTokenCountMap,
151
+ });
152
+
153
+ if (compileOptions != null) {
154
+ multiAgentGraph.compileOptions = compileOptions;
155
+ }
156
+
157
+ this.Graph = multiAgentGraph;
158
+ return multiAgentGraph.createWorkflow();
159
+ }
160
+
161
+ static async create<T extends t.BaseGraphState>(
162
+ config: t.RunConfig
163
+ ): Promise<Run<T>> {
164
+ /** Create tokenCounter if indexTokenCountMap is provided but tokenCounter is not */
165
+ if (config.indexTokenCountMap && !config.tokenCounter) {
166
+ config.tokenCounter = await createTokenCounter();
167
+ }
168
+ return new Run<T>(config);
169
+ }
170
+
171
+ getRunMessages(): BaseMessage[] | undefined {
172
+ if (!this.Graph) {
173
+ throw new Error(
174
+ 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'
175
+ );
176
+ }
177
+ return this.Graph.getRunMessages();
178
+ }
179
+
180
+ /**
181
+ * Creates a custom event callback handler that intercepts custom events
182
+ * and processes them through our handler registry instead of EventStreamCallbackHandler
183
+ */
184
+ private createCustomEventCallback() {
185
+ return async (
186
+ eventName: string,
187
+ data: unknown,
188
+ runId: string,
189
+ tags?: string[],
190
+ metadata?: Record<string, unknown>
191
+ ): Promise<void> => {
192
+ if (
193
+ (data as t.StreamEventData)['emitted'] === true &&
194
+ eventName === GraphEvents.CHAT_MODEL_STREAM
195
+ ) {
196
+ return;
197
+ }
198
+ const handler = this.handlerRegistry?.getHandler(eventName);
199
+ if (handler && this.Graph) {
200
+ await handler.handle(
201
+ eventName,
202
+ data as
203
+ | t.StreamEventData
204
+ | t.ModelEndData
205
+ | t.RunStep
206
+ | t.RunStepDeltaEvent
207
+ | t.MessageDeltaEvent
208
+ | t.ReasoningDeltaEvent
209
+ | { result: t.ToolEndEvent },
210
+ metadata,
211
+ this.Graph
212
+ );
213
+ }
214
+ };
215
+ }
216
+
217
+ async processStream(
218
+ inputs: t.IState,
219
+ config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },
220
+ streamOptions?: t.EventStreamOptions
221
+ ): Promise<MessageContentComplex[] | undefined> {
222
+ if (this.graphRunnable == null) {
223
+ throw new Error(
224
+ 'Run not initialized. Make sure to use Run.create() to instantiate the Run.'
225
+ );
226
+ }
227
+ if (!this.Graph) {
228
+ throw new Error(
229
+ 'Graph not initialized. Make sure to use Run.create() to instantiate the Run.'
230
+ );
231
+ }
232
+
233
+ this.Graph.resetValues(streamOptions?.keepContent);
234
+
235
+ /** Custom event callback to intercept and handle custom events */
236
+ const customEventCallback = this.createCustomEventCallback();
237
+
238
+ const baseCallbacks = (config.callbacks as t.ProvidedCallbacks) ?? [];
239
+ const streamCallbacks = streamOptions?.callbacks
240
+ ? this.getCallbacks(streamOptions.callbacks)
241
+ : [];
242
+
243
+ config.callbacks = baseCallbacks.concat(streamCallbacks).concat({
244
+ [Callback.CUSTOM_EVENT]: customEventCallback,
245
+ });
246
+
247
+ if (
248
+ isPresent(process.env.LANGFUSE_SECRET_KEY) &&
249
+ isPresent(process.env.LANGFUSE_PUBLIC_KEY) &&
250
+ isPresent(process.env.LANGFUSE_BASE_URL)
251
+ ) {
252
+ const userId = config.configurable?.user_id;
253
+ const sessionId = config.configurable?.thread_id;
254
+ const traceMetadata = {
255
+ messageId: this.id,
256
+ parentMessageId: config.configurable?.requestBody?.parentMessageId,
257
+ };
258
+ const handler = new CallbackHandler({
259
+ userId,
260
+ sessionId,
261
+ traceMetadata,
262
+ });
263
+ config.callbacks = (
264
+ (config.callbacks as t.ProvidedCallbacks) ?? []
265
+ ).concat([handler]);
266
+ }
267
+
268
+ if (!this.id) {
269
+ throw new Error('Run ID not provided');
270
+ }
271
+
272
+ config.run_id = this.id;
273
+ config.configurable = Object.assign(config.configurable ?? {}, {
274
+ run_id: this.id,
275
+ });
276
+
277
+ const stream = this.graphRunnable.streamEvents(inputs, config, {
278
+ raiseError: true,
279
+ });
280
+
281
+ for await (const event of stream) {
282
+ const { data, metadata, ...info } = event;
283
+
284
+ const eventName: t.EventName = info.event;
285
+
286
+ /** Skip custom events as they're handled by our callback */
287
+ if (eventName === GraphEvents.ON_CUSTOM_EVENT) {
288
+ continue;
289
+ }
290
+
291
+ const handler = this.handlerRegistry?.getHandler(eventName);
292
+ if (handler) {
293
+ await handler.handle(eventName, data, metadata, this.Graph);
294
+ }
295
+ }
296
+
297
+ if (this.returnContent) {
298
+ return this.Graph.getContentParts();
299
+ }
300
+ }
301
+
302
+ private createSystemCallback<K extends keyof t.ClientCallbacks>(
303
+ clientCallbacks: t.ClientCallbacks,
304
+ key: K
305
+ ): t.SystemCallbacks[K] {
306
+ return ((...args: unknown[]) => {
307
+ const clientCallback = clientCallbacks[key];
308
+ if (clientCallback && this.Graph) {
309
+ (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);
310
+ }
311
+ }) as t.SystemCallbacks[K];
312
+ }
313
+
314
+ getCallbacks(clientCallbacks: t.ClientCallbacks): t.SystemCallbacks {
315
+ return {
316
+ [Callback.TOOL_ERROR]: this.createSystemCallback(
317
+ clientCallbacks,
318
+ Callback.TOOL_ERROR
319
+ ),
320
+ [Callback.TOOL_START]: this.createSystemCallback(
321
+ clientCallbacks,
322
+ Callback.TOOL_START
323
+ ),
324
+ [Callback.TOOL_END]: this.createSystemCallback(
325
+ clientCallbacks,
326
+ Callback.TOOL_END
327
+ ),
328
+ };
329
+ }
330
+
331
+ async generateTitle({
332
+ provider,
333
+ inputText,
334
+ contentParts,
335
+ titlePrompt,
336
+ clientOptions,
337
+ chainOptions,
338
+ skipLanguage,
339
+ titleMethod = TitleMethod.COMPLETION,
340
+ titlePromptTemplate,
341
+ }: t.RunTitleOptions): Promise<{ language?: string; title?: string }> {
342
+ if (
343
+ chainOptions != null &&
344
+ isPresent(process.env.LANGFUSE_SECRET_KEY) &&
345
+ isPresent(process.env.LANGFUSE_PUBLIC_KEY) &&
346
+ isPresent(process.env.LANGFUSE_BASE_URL)
347
+ ) {
348
+ const userId = chainOptions.configurable?.user_id;
349
+ const sessionId = chainOptions.configurable?.thread_id;
350
+ const traceMetadata = {
351
+ messageId: 'title-' + this.id,
352
+ };
353
+ const handler = new CallbackHandler({
354
+ userId,
355
+ sessionId,
356
+ traceMetadata,
357
+ });
358
+ chainOptions.callbacks = (
359
+ (chainOptions.callbacks as t.ProvidedCallbacks) ?? []
360
+ ).concat([handler]);
361
+ }
362
+
363
+ const convoTemplate = PromptTemplate.fromTemplate(
364
+ titlePromptTemplate ?? 'User: {input}\nAI: {output}'
365
+ );
366
+
367
+ const response = contentParts
368
+ .map((part) => {
369
+ if (part?.type === 'text') return part.text;
370
+ return '';
371
+ })
372
+ .join('\n');
373
+
374
+ const model = this.Graph?.getNewModel({
375
+ provider,
376
+ clientOptions,
377
+ });
378
+ if (!model) {
379
+ return { language: '', title: '' };
380
+ }
381
+ if (
382
+ isOpenAILike(provider) &&
383
+ (model instanceof ChatOpenAI || model instanceof AzureChatOpenAI)
384
+ ) {
385
+ model.temperature = (clientOptions as t.OpenAIClientOptions | undefined)
386
+ ?.temperature as number;
387
+ model.topP = (clientOptions as t.OpenAIClientOptions | undefined)
388
+ ?.topP as number;
389
+ model.frequencyPenalty = (
390
+ clientOptions as t.OpenAIClientOptions | undefined
391
+ )?.frequencyPenalty as number;
392
+ model.presencePenalty = (
393
+ clientOptions as t.OpenAIClientOptions | undefined
394
+ )?.presencePenalty as number;
395
+ model.n = (clientOptions as t.OpenAIClientOptions | undefined)
396
+ ?.n as number;
397
+ }
398
+
399
+ const convoToTitleInput = new RunnableLambda({
400
+ func: (
401
+ promptValue: StringPromptValue
402
+ ): { convo: string; inputText: string; skipLanguage?: boolean } => ({
403
+ convo: promptValue.value,
404
+ inputText,
405
+ skipLanguage,
406
+ }),
407
+ }).withConfig({ runName: 'ConvoTransform' });
408
+
409
+ const titleChain =
410
+ titleMethod === TitleMethod.COMPLETION
411
+ ? await createCompletionTitleRunnable(model, titlePrompt)
412
+ : await createTitleRunnable(model, titlePrompt);
413
+
414
+ /** Pipes `convoTemplate` -> `transformer` -> `titleChain` */
415
+ const fullChain = convoTemplate
416
+ .withConfig({ runName: 'ConvoTemplate' })
417
+ .pipe(convoToTitleInput)
418
+ .pipe(titleChain)
419
+ .withConfig({ runName: 'TitleChain' });
420
+
421
+ const invokeConfig = Object.assign({}, chainOptions, {
422
+ run_id: this.id,
423
+ runId: this.id,
424
+ });
425
+
426
+ try {
427
+ return await fullChain.invoke(
428
+ { input: inputText, output: response },
429
+ invokeConfig
430
+ );
431
+ } catch (_e) {
432
+ // Fallback: strip callbacks to avoid EventStream tracer errors in certain environments
433
+ // But preserve langfuse handler if it exists
434
+ const langfuseHandler = (invokeConfig.callbacks as t.ProvidedCallbacks)?.find(
435
+ (cb) => cb instanceof CallbackHandler
436
+ );
437
+ const { callbacks: _cb, ...rest } = invokeConfig;
438
+ const safeConfig = Object.assign({}, rest, {
439
+ callbacks: langfuseHandler ? [langfuseHandler] : [],
440
+ });
441
+ return await fullChain.invoke(
442
+ { input: inputText, output: response },
443
+ safeConfig as Partial<RunnableConfig>
444
+ );
445
+ }
446
+ }
447
+ }