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
@@ -1,400 +1,401 @@
1
- // src/types/stream.ts
2
- import type OpenAITypes from 'openai';
3
- import type {
4
- MessageContentImageUrl,
5
- MessageContentText,
6
- ToolMessage,
7
- BaseMessage,
8
- } from '@langchain/core/messages';
9
- import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
10
- import type { LLMResult, Generation } from '@langchain/core/outputs';
11
- import type { AnthropicContentBlock } from '@/llm/anthropic/types';
12
- import type { ToolEndEvent } from '@/types/tools';
13
- import { StepTypes, ContentTypes, GraphEvents } from '@/common/enum';
14
-
15
- export type HandleLLMEnd = (
16
- output: LLMResult,
17
- runId: string,
18
- parentRunId?: string,
19
- tags?: string[]
20
- ) => void;
21
-
22
- export type MetadataAggregatorResult = {
23
- handleLLMEnd: HandleLLMEnd;
24
- collected: Record<string, unknown>[];
25
- };
26
-
27
- export type StreamGeneration = Generation & {
28
- text?: string;
29
- message?: BaseMessage;
30
- };
31
-
32
- /** Event names are of the format: on_[runnable_type]_(start|stream|end).
33
-
34
- Runnable types are one of:
35
-
36
- llm - used by non chat models
37
- chat_model - used by chat models
38
- prompt -- e.g., ChatPromptTemplate
39
- tool -- LangChain tools
40
- chain - most Runnables are of this type
41
- Further, the events are categorized as one of:
42
-
43
- start - when the runnable starts
44
- stream - when the runnable is streaming
45
- end - when the runnable ends
46
- start, stream and end are associated with slightly different data payload.
47
-
48
- Please see the documentation for EventData for more details. */
49
- export type EventName = string;
50
-
51
- export type RunStep = {
52
- // id: string;
53
- // object: 'thread.run.step'; // Updated from 'run.step' # missing
54
- // created_at: number;
55
- // run_id: string;
56
- // assistant_id: string;
57
- // thread_id: string;
58
- type: StepTypes;
59
- // status: 'in_progress' | 'completed' | 'failed' | 'cancelled'; // Add other possible status values if needed
60
- // cancelled_at: number | null;
61
- // completed_at: number | null;
62
- // expires_at: number;
63
- // failed_at: number | null;
64
- // last_error: string | null;
65
- id: string; // #new
66
- runId?: string; // #new
67
- index: number; // #new
68
- stepIndex?: number; // #new
69
- stepDetails: StepDetails;
70
- usage?: null | object;
71
- // {
72
- // Define usage structure if it's ever non-null
73
- // prompt_tokens: number; // #new
74
- // completion_tokens: number; // #new
75
- // total_tokens: number; // #new
76
- // };
77
- };
78
-
79
- /**
80
- * Represents a run step delta i.e. any changed fields on a run step during
81
- * streaming.
82
- */
83
- export interface RunStepDeltaEvent {
84
- /**
85
- * The identifier of the run step, which can be referenced in API endpoints.
86
- */
87
- id: string;
88
- /**
89
- * The delta containing the fields that have changed on the run step.
90
- */
91
- delta: ToolCallDelta;
92
- }
93
-
94
- export type StepDetails = MessageCreationDetails | ToolCallsDetails;
95
-
96
- export type StepCompleted = ToolCallCompleted;
97
-
98
- export type MessageCreationDetails = {
99
- type: StepTypes.MESSAGE_CREATION;
100
- message_creation: {
101
- message_id: string;
102
- };
103
- };
104
-
105
- export type ToolEndData = {
106
- input: string | Record<string, unknown>;
107
- output?: ToolMessage;
108
- };
109
- export type ToolErrorData = {
110
- id: string;
111
- name: string;
112
- error?: Error;
113
- } & Pick<ToolEndData, 'input'>;
114
- export type ToolEndCallback = (
115
- data: ToolEndData,
116
- metadata?: Record<string, unknown>
117
- ) => void;
118
-
119
- export type ProcessedToolCall = {
120
- name: string;
121
- args: string | Record<string, unknown>;
122
- id: string;
123
- output: string;
124
- progress: number;
125
- };
126
-
127
- export type ProcessedContent = {
128
- type: ContentType;
129
- text?: string;
130
- tool_call?: ProcessedToolCall;
131
- };
132
-
133
- export type ToolCallCompleted = {
134
- type: 'tool_call';
135
- tool_call: ProcessedToolCall;
136
- };
137
-
138
- export type ToolCompleteEvent = ToolCallCompleted & {
139
- /** The Step Id of the Tool Call */
140
- id: string;
141
- /** The content index of the tool call */
142
- index: number;
143
- type: 'tool_call';
144
- };
145
-
146
- export type ToolCallsDetails = {
147
- type: StepTypes.TOOL_CALLS;
148
- tool_calls?: AgentToolCall[]; // #new
149
- };
150
-
151
- export type ToolCallDelta = {
152
- type: StepTypes;
153
- tool_calls?: ToolCallChunk[]; // #new
154
- };
155
-
156
- export type AgentToolCall =
157
- | {
158
- id: string; // #new
159
- type: 'function'; // #new
160
- function: {
161
- name: string; // #new
162
- arguments: string | object; // JSON string // #new
163
- };
164
- }
165
- | ToolCall;
166
-
167
- export interface ExtendedMessageContent {
168
- type?: string;
169
- text?: string;
170
- input?: string;
171
- index?: number;
172
- id?: string;
173
- name?: string;
174
- }
175
-
176
- export type AgentUpdate = {
177
- type: ContentTypes.AGENT_UPDATE;
178
- agent_update: {
179
- index: number;
180
- runId: string;
181
- agentId: string;
182
- };
183
- };
184
-
185
- /**
186
- * Represents a message delta i.e. any changed fields on a message during
187
- * streaming.
188
- */
189
- export interface MessageDeltaEvent {
190
- /**
191
- * The identifier of the message, which can be referenced in API endpoints.
192
- */
193
- id: string;
194
-
195
- /**
196
- * The delta containing the fields that have changed on the Message.
197
- */
198
- delta: MessageDelta;
199
- }
200
-
201
- /**
202
- * The delta containing the fields that have changed on the Message.
203
- */
204
- export interface MessageDelta {
205
- /**
206
- * The content of the message in array of text and/or images.
207
- */
208
- content?: MessageContentComplex[];
209
- /**
210
- * The tool call ids associated with the message.
211
- */
212
- tool_call_ids?: string[];
213
- }
214
-
215
- /**
216
- * Represents a reasoning delta i.e. any changed fields on a message during
217
- * streaming.
218
- */
219
- export interface ReasoningDeltaEvent {
220
- /**
221
- * The identifier of the message, which can be referenced in API endpoints.
222
- */
223
- id: string;
224
-
225
- /**
226
- * The delta containing the fields that have changed.
227
- */
228
- delta: ReasoningDelta;
229
- }
230
-
231
- /**
232
- * The reasoning delta containing the fields that have changed on the Message.
233
- */
234
- export interface ReasoningDelta {
235
- /**
236
- * The content of the message in array of text and/or images.
237
- */
238
- content?: MessageContentComplex[];
239
- }
240
-
241
- export type MessageDeltaUpdate = {
242
- type: ContentTypes.TEXT;
243
- text: string;
244
- tool_call_ids?: string[];
245
- };
246
- export type ReasoningDeltaUpdate = { type: ContentTypes.THINK; think: string };
247
-
248
- export type ContentType = 'text' | 'image_url' | 'tool_call' | 'think' | string;
249
-
250
- export type ReasoningContentText = {
251
- type: ContentTypes.THINK;
252
- think: string;
253
- };
254
-
255
- /** Vertex AI / Google Common - Reasoning Content Block Format */
256
- export type GoogleReasoningContentText = {
257
- type: ContentTypes.REASONING;
258
- reasoning: string;
259
- };
260
-
261
- /** Anthropic's Reasoning Content Block Format */
262
- export type ThinkingContentText = {
263
- type: ContentTypes.THINKING;
264
- index?: number;
265
- signature?: string;
266
- thinking?: string;
267
- };
268
-
269
- /** Bedrock's Reasoning Content Block Format */
270
- export type BedrockReasoningContentText = {
271
- type: ContentTypes.REASONING_CONTENT;
272
- index?: number;
273
- reasoningText: { text?: string; signature?: string };
274
- };
275
-
276
- /**
277
- * A call to a tool.
278
- */
279
- export type ToolCallPart = {
280
- /** Type ("tool_call") according to Assistants Tool Call Structure */
281
- type: ContentTypes.TOOL_CALL;
282
- /** The name of the tool to be called */
283
- name: string;
284
- /** The arguments to the tool call */
285
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
286
- args?: string | Record<string, any>;
287
-
288
- /** If provided, an identifier associated with the tool call */
289
- id?: string;
290
- /** If provided, the output of the tool call */
291
- output?: string;
292
- /** Auth URL */
293
- auth?: string;
294
- /** Expiration time */
295
- expires_at?: number;
296
- };
297
-
298
- export type ToolCallContent = {
299
- type: ContentTypes.TOOL_CALL;
300
- tool_call?: ToolCallPart;
301
- };
302
-
303
- export type ToolResultContent = {
304
- content:
305
- | string
306
- | Record<string, unknown>
307
- | Array<string | Record<string, unknown>>
308
- | AnthropicContentBlock[];
309
- type: 'tool_result' | 'web_search_result' | 'web_search_tool_result';
310
- tool_use_id?: string;
311
- input?: string | Record<string, unknown>;
312
- index?: number;
313
- };
314
-
315
- export type MessageContentComplex = (
316
- | ToolResultContent
317
- | ThinkingContentText
318
- | AgentUpdate
319
- | ToolCallContent
320
- | ReasoningContentText
321
- | MessageContentText
322
- | MessageContentImageUrl
323
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
324
- | (Record<string, any> & {
325
- type?: 'text' | 'image_url' | 'think' | 'thinking' | string;
326
- })
327
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
328
- | (Record<string, any> & {
329
- type?: never;
330
- })
331
- ) & {
332
- tool_call_ids?: string[];
333
- };
334
-
335
- export interface TMessage {
336
- role?: string;
337
- content?: MessageContentComplex[] | string;
338
- [key: string]: unknown;
339
- }
340
-
341
- export type TPayload = Array<Partial<TMessage>>;
342
-
343
- export type CustomChunkDelta =
344
- | null
345
- | undefined
346
- | (Partial<OpenAITypes.Chat.Completions.ChatCompletionChunk.Choice.Delta> & {
347
- reasoning?: string | null;
348
- reasoning_content?: string | null;
349
- });
350
- export type CustomChunkChoice = Partial<
351
- Omit<OpenAITypes.Chat.Completions.ChatCompletionChunk.Choice, 'delta'> & {
352
- delta?: CustomChunkDelta;
353
- }
354
- >;
355
- export type CustomChunk = Partial<OpenAITypes.ChatCompletionChunk> & {
356
- choices?: Partial<Array<CustomChunkChoice>>;
357
- };
358
-
359
- export type SplitStreamHandlers = Partial<{
360
- [GraphEvents.ON_RUN_STEP]: ({
361
- event,
362
- data,
363
- }: {
364
- event: GraphEvents;
365
- data: RunStep;
366
- }) => void;
367
- [GraphEvents.ON_MESSAGE_DELTA]: ({
368
- event,
369
- data,
370
- }: {
371
- event: GraphEvents;
372
- data: MessageDeltaEvent;
373
- }) => void;
374
- [GraphEvents.ON_REASONING_DELTA]: ({
375
- event,
376
- data,
377
- }: {
378
- event: GraphEvents;
379
- data: ReasoningDeltaEvent;
380
- }) => void;
381
- }>;
382
-
383
- export type ContentAggregator = ({
384
- event,
385
- data,
386
- }: {
387
- event: GraphEvents;
388
- data:
389
- | RunStep
390
- | MessageDeltaEvent
391
- | RunStepDeltaEvent
392
- | {
393
- result: ToolEndEvent;
394
- };
395
- }) => void;
396
- export type ContentAggregatorResult = {
397
- stepMap: Map<string, RunStep | undefined>;
398
- contentParts: Array<MessageContentComplex | undefined>;
399
- aggregateContent: ContentAggregator;
400
- };
1
+ // src/types/stream.ts
2
+ import type OpenAITypes from 'openai';
3
+ import type {
4
+ MessageContentImageUrl,
5
+ MessageContentText,
6
+ ToolMessage,
7
+ BaseMessage,
8
+ } from '@langchain/core/messages';
9
+ import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
10
+ import type { LLMResult, Generation } from '@langchain/core/outputs';
11
+ import type { AnthropicContentBlock } from '@/llm/anthropic/types';
12
+ import type { Command } from '@langchain/langgraph';
13
+ import type { ToolEndEvent } from '@/types/tools';
14
+ import { StepTypes, ContentTypes, GraphEvents } from '@/common/enum';
15
+
16
+ export type HandleLLMEnd = (
17
+ output: LLMResult,
18
+ runId: string,
19
+ parentRunId?: string,
20
+ tags?: string[]
21
+ ) => void;
22
+
23
+ export type MetadataAggregatorResult = {
24
+ handleLLMEnd: HandleLLMEnd;
25
+ collected: Record<string, unknown>[];
26
+ };
27
+
28
+ export type StreamGeneration = Generation & {
29
+ text?: string;
30
+ message?: BaseMessage;
31
+ };
32
+
33
+ /** Event names are of the format: on_[runnable_type]_(start|stream|end).
34
+
35
+ Runnable types are one of:
36
+
37
+ llm - used by non chat models
38
+ chat_model - used by chat models
39
+ prompt -- e.g., ChatPromptTemplate
40
+ tool -- LangChain tools
41
+ chain - most Runnables are of this type
42
+ Further, the events are categorized as one of:
43
+
44
+ start - when the runnable starts
45
+ stream - when the runnable is streaming
46
+ end - when the runnable ends
47
+ start, stream and end are associated with slightly different data payload.
48
+
49
+ Please see the documentation for EventData for more details. */
50
+ export type EventName = string;
51
+
52
+ export type RunStep = {
53
+ // id: string;
54
+ // object: 'thread.run.step'; // Updated from 'run.step' # missing
55
+ // created_at: number;
56
+ // run_id: string;
57
+ // assistant_id: string;
58
+ // thread_id: string;
59
+ type: StepTypes;
60
+ // status: 'in_progress' | 'completed' | 'failed' | 'cancelled'; // Add other possible status values if needed
61
+ // cancelled_at: number | null;
62
+ // completed_at: number | null;
63
+ // expires_at: number;
64
+ // failed_at: number | null;
65
+ // last_error: string | null;
66
+ id: string; // #new
67
+ runId?: string; // #new
68
+ index: number; // #new
69
+ stepIndex?: number; // #new
70
+ stepDetails: StepDetails;
71
+ usage?: null | object;
72
+ // {
73
+ // Define usage structure if it's ever non-null
74
+ // prompt_tokens: number; // #new
75
+ // completion_tokens: number; // #new
76
+ // total_tokens: number; // #new
77
+ // };
78
+ };
79
+
80
+ /**
81
+ * Represents a run step delta i.e. any changed fields on a run step during
82
+ * streaming.
83
+ */
84
+ export interface RunStepDeltaEvent {
85
+ /**
86
+ * The identifier of the run step, which can be referenced in API endpoints.
87
+ */
88
+ id: string;
89
+ /**
90
+ * The delta containing the fields that have changed on the run step.
91
+ */
92
+ delta: ToolCallDelta;
93
+ }
94
+
95
+ export type StepDetails = MessageCreationDetails | ToolCallsDetails;
96
+
97
+ export type StepCompleted = ToolCallCompleted;
98
+
99
+ export type MessageCreationDetails = {
100
+ type: StepTypes.MESSAGE_CREATION;
101
+ message_creation: {
102
+ message_id: string;
103
+ };
104
+ };
105
+
106
+ export type ToolEndData = {
107
+ input: string | Record<string, unknown>;
108
+ output?: ToolMessage | Command;
109
+ };
110
+ export type ToolErrorData = {
111
+ id: string;
112
+ name: string;
113
+ error?: Error;
114
+ } & Pick<ToolEndData, 'input'>;
115
+ export type ToolEndCallback = (
116
+ data: ToolEndData,
117
+ metadata?: Record<string, unknown>
118
+ ) => void;
119
+
120
+ export type ProcessedToolCall = {
121
+ name: string;
122
+ args: string | Record<string, unknown>;
123
+ id: string;
124
+ output: string;
125
+ progress: number;
126
+ };
127
+
128
+ export type ProcessedContent = {
129
+ type: ContentType;
130
+ text?: string;
131
+ tool_call?: ProcessedToolCall;
132
+ };
133
+
134
+ export type ToolCallCompleted = {
135
+ type: 'tool_call';
136
+ tool_call: ProcessedToolCall;
137
+ };
138
+
139
+ export type ToolCompleteEvent = ToolCallCompleted & {
140
+ /** The Step Id of the Tool Call */
141
+ id: string;
142
+ /** The content index of the tool call */
143
+ index: number;
144
+ type: 'tool_call';
145
+ };
146
+
147
+ export type ToolCallsDetails = {
148
+ type: StepTypes.TOOL_CALLS;
149
+ tool_calls?: AgentToolCall[]; // #new
150
+ };
151
+
152
+ export type ToolCallDelta = {
153
+ type: StepTypes;
154
+ tool_calls?: ToolCallChunk[]; // #new
155
+ };
156
+
157
+ export type AgentToolCall =
158
+ | {
159
+ id: string; // #new
160
+ type: 'function'; // #new
161
+ function: {
162
+ name: string; // #new
163
+ arguments: string | object; // JSON string // #new
164
+ };
165
+ }
166
+ | ToolCall;
167
+
168
+ export interface ExtendedMessageContent {
169
+ type?: string;
170
+ text?: string;
171
+ input?: string;
172
+ index?: number;
173
+ id?: string;
174
+ name?: string;
175
+ }
176
+
177
+ export type AgentUpdate = {
178
+ type: ContentTypes.AGENT_UPDATE;
179
+ agent_update: {
180
+ index: number;
181
+ runId: string;
182
+ agentId: string;
183
+ };
184
+ };
185
+
186
+ /**
187
+ * Represents a message delta i.e. any changed fields on a message during
188
+ * streaming.
189
+ */
190
+ export interface MessageDeltaEvent {
191
+ /**
192
+ * The identifier of the message, which can be referenced in API endpoints.
193
+ */
194
+ id: string;
195
+
196
+ /**
197
+ * The delta containing the fields that have changed on the Message.
198
+ */
199
+ delta: MessageDelta;
200
+ }
201
+
202
+ /**
203
+ * The delta containing the fields that have changed on the Message.
204
+ */
205
+ export interface MessageDelta {
206
+ /**
207
+ * The content of the message in array of text and/or images.
208
+ */
209
+ content?: MessageContentComplex[];
210
+ /**
211
+ * The tool call ids associated with the message.
212
+ */
213
+ tool_call_ids?: string[];
214
+ }
215
+
216
+ /**
217
+ * Represents a reasoning delta i.e. any changed fields on a message during
218
+ * streaming.
219
+ */
220
+ export interface ReasoningDeltaEvent {
221
+ /**
222
+ * The identifier of the message, which can be referenced in API endpoints.
223
+ */
224
+ id: string;
225
+
226
+ /**
227
+ * The delta containing the fields that have changed.
228
+ */
229
+ delta: ReasoningDelta;
230
+ }
231
+
232
+ /**
233
+ * The reasoning delta containing the fields that have changed on the Message.
234
+ */
235
+ export interface ReasoningDelta {
236
+ /**
237
+ * The content of the message in array of text and/or images.
238
+ */
239
+ content?: MessageContentComplex[];
240
+ }
241
+
242
+ export type MessageDeltaUpdate = {
243
+ type: ContentTypes.TEXT;
244
+ text: string;
245
+ tool_call_ids?: string[];
246
+ };
247
+ export type ReasoningDeltaUpdate = { type: ContentTypes.THINK; think: string };
248
+
249
+ export type ContentType = 'text' | 'image_url' | 'tool_call' | 'think' | string;
250
+
251
+ export type ReasoningContentText = {
252
+ type: ContentTypes.THINK;
253
+ think: string;
254
+ };
255
+
256
+ /** Vertex AI / Google Common - Reasoning Content Block Format */
257
+ export type GoogleReasoningContentText = {
258
+ type: ContentTypes.REASONING;
259
+ reasoning: string;
260
+ };
261
+
262
+ /** Anthropic's Reasoning Content Block Format */
263
+ export type ThinkingContentText = {
264
+ type: ContentTypes.THINKING;
265
+ index?: number;
266
+ signature?: string;
267
+ thinking?: string;
268
+ };
269
+
270
+ /** Bedrock's Reasoning Content Block Format */
271
+ export type BedrockReasoningContentText = {
272
+ type: ContentTypes.REASONING_CONTENT;
273
+ index?: number;
274
+ reasoningText: { text?: string; signature?: string };
275
+ };
276
+
277
+ /**
278
+ * A call to a tool.
279
+ */
280
+ export type ToolCallPart = {
281
+ /** Type ("tool_call") according to Assistants Tool Call Structure */
282
+ type: ContentTypes.TOOL_CALL;
283
+ /** The name of the tool to be called */
284
+ name?: string;
285
+ /** The arguments to the tool call */
286
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
287
+ args?: string | Record<string, any>;
288
+
289
+ /** If provided, an identifier associated with the tool call */
290
+ id?: string;
291
+ /** If provided, the output of the tool call */
292
+ output?: string;
293
+ /** Auth URL */
294
+ auth?: string;
295
+ /** Expiration time */
296
+ expires_at?: number;
297
+ };
298
+
299
+ export type ToolCallContent = {
300
+ type: ContentTypes.TOOL_CALL;
301
+ tool_call?: ToolCallPart;
302
+ };
303
+
304
+ export type ToolResultContent = {
305
+ content:
306
+ | string
307
+ | Record<string, unknown>
308
+ | Array<string | Record<string, unknown>>
309
+ | AnthropicContentBlock[];
310
+ type: 'tool_result' | 'web_search_result' | 'web_search_tool_result';
311
+ tool_use_id?: string;
312
+ input?: string | Record<string, unknown>;
313
+ index?: number;
314
+ };
315
+
316
+ export type MessageContentComplex = (
317
+ | ToolResultContent
318
+ | ThinkingContentText
319
+ | AgentUpdate
320
+ | ToolCallContent
321
+ | ReasoningContentText
322
+ | MessageContentText
323
+ | MessageContentImageUrl
324
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
325
+ | (Record<string, any> & {
326
+ type?: 'text' | 'image_url' | 'think' | 'thinking' | string;
327
+ })
328
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
329
+ | (Record<string, any> & {
330
+ type?: never;
331
+ })
332
+ ) & {
333
+ tool_call_ids?: string[];
334
+ };
335
+
336
+ export interface TMessage {
337
+ role?: string;
338
+ content?: MessageContentComplex[] | string;
339
+ [key: string]: unknown;
340
+ }
341
+
342
+ export type TPayload = Array<Partial<TMessage>>;
343
+
344
+ export type CustomChunkDelta =
345
+ | null
346
+ | undefined
347
+ | (Partial<OpenAITypes.Chat.Completions.ChatCompletionChunk.Choice.Delta> & {
348
+ reasoning?: string | null;
349
+ reasoning_content?: string | null;
350
+ });
351
+ export type CustomChunkChoice = Partial<
352
+ Omit<OpenAITypes.Chat.Completions.ChatCompletionChunk.Choice, 'delta'> & {
353
+ delta?: CustomChunkDelta;
354
+ }
355
+ >;
356
+ export type CustomChunk = Partial<OpenAITypes.ChatCompletionChunk> & {
357
+ choices?: Partial<Array<CustomChunkChoice>>;
358
+ };
359
+
360
+ export type SplitStreamHandlers = Partial<{
361
+ [GraphEvents.ON_RUN_STEP]: ({
362
+ event,
363
+ data,
364
+ }: {
365
+ event: GraphEvents;
366
+ data: RunStep;
367
+ }) => void;
368
+ [GraphEvents.ON_MESSAGE_DELTA]: ({
369
+ event,
370
+ data,
371
+ }: {
372
+ event: GraphEvents;
373
+ data: MessageDeltaEvent;
374
+ }) => void;
375
+ [GraphEvents.ON_REASONING_DELTA]: ({
376
+ event,
377
+ data,
378
+ }: {
379
+ event: GraphEvents;
380
+ data: ReasoningDeltaEvent;
381
+ }) => void;
382
+ }>;
383
+
384
+ export type ContentAggregator = ({
385
+ event,
386
+ data,
387
+ }: {
388
+ event: GraphEvents;
389
+ data:
390
+ | RunStep
391
+ | MessageDeltaEvent
392
+ | RunStepDeltaEvent
393
+ | {
394
+ result: ToolEndEvent;
395
+ };
396
+ }) => void;
397
+ export type ContentAggregatorResult = {
398
+ stepMap: Map<string, RunStep | undefined>;
399
+ contentParts: Array<MessageContentComplex | undefined>;
400
+ aggregateContent: ContentAggregator;
401
+ };