@ai-setting/roy-agent-core 1.5.52 → 1.5.53

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.
@@ -0,0 +1,610 @@
1
+ import {
2
+ createLogger,
3
+ init_logger
4
+ } from "./roy-agent-core-10n2jh7p.js";
5
+ import {
6
+ __require
7
+ } from "./roy-agent-core-fs0mn2jk.js";
8
+
9
+ // src/env/llm/invoke.ts
10
+ init_logger();
11
+ import { randomUUID } from "crypto";
12
+ import { streamText, jsonSchema } from "ai";
13
+ import { createOpenAI } from "@ai-sdk/openai";
14
+ import { createAnthropic } from "@ai-sdk/anthropic";
15
+ import { createGoogleGenerativeAI } from "@ai-sdk/google";
16
+ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
17
+ import { zodToJsonSchema } from "zod-to-json-schema";
18
+ var logger = createLogger("llm:invoke");
19
+ var MAX_RETRIES = 3;
20
+ var DEFAULT_TIMEOUT = 60000;
21
+
22
+ class TimeoutError extends Error {
23
+ timeout;
24
+ constructor(message, timeout) {
25
+ super(message);
26
+ this.timeout = timeout;
27
+ this.name = "TimeoutError";
28
+ }
29
+ }
30
+ async function withTimeout(promise, timeoutMs = DEFAULT_TIMEOUT, message) {
31
+ let timer;
32
+ const timeoutPromise = new Promise((_, reject) => {
33
+ timer = setTimeout(() => {
34
+ reject(new TimeoutError(message || `LLM invocation timeout after ${timeoutMs}ms`, timeoutMs));
35
+ }, timeoutMs);
36
+ });
37
+ try {
38
+ return await Promise.race([promise, timeoutPromise]);
39
+ } finally {
40
+ if (timer) {
41
+ clearTimeout(timer);
42
+ }
43
+ }
44
+ }
45
+ function parseModelString(model) {
46
+ if (!model) {
47
+ return { providerId: "openai", modelId: "gpt-4o" };
48
+ }
49
+ const parts = model.split("/");
50
+ if (parts.length >= 2) {
51
+ return { providerId: parts[0], modelId: parts.slice(1).join("/") };
52
+ }
53
+ return { providerId: "openai", modelId: model };
54
+ }
55
+ function extractUsageInfo(usage) {
56
+ if (!usage)
57
+ return;
58
+ if (usage.inputTokens && typeof usage.inputTokens === "object") {
59
+ return {
60
+ promptTokens: usage.inputTokens.total ?? 0,
61
+ completionTokens: usage.outputTokens?.total ?? 0,
62
+ totalTokens: usage.totalTokens ?? 0
63
+ };
64
+ }
65
+ if (typeof usage.promptTokens === "number") {
66
+ return {
67
+ promptTokens: usage.promptTokens,
68
+ completionTokens: usage.completionTokens ?? 0,
69
+ totalTokens: usage.totalTokens ?? 0
70
+ };
71
+ }
72
+ if (usage.inputTokenDetails || usage.outputTokenDetails) {
73
+ return {
74
+ promptTokens: usage.inputTokenDetails?.tokens ?? 0,
75
+ completionTokens: usage.outputTokenDetails?.tokens ?? 0,
76
+ totalTokens: usage.totalTokens ?? 0
77
+ };
78
+ }
79
+ if (usage.raw && typeof usage.raw.total_tokens === "number") {
80
+ return {
81
+ promptTokens: 0,
82
+ completionTokens: 0,
83
+ totalTokens: usage.raw.total_tokens
84
+ };
85
+ }
86
+ return;
87
+ }
88
+ function convertToSDKMessages(messages) {
89
+ return messages.map((msg) => {
90
+ if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) {
91
+ const toolCallParts = msg.toolCalls.map((tc) => {
92
+ let input = {};
93
+ if (tc.function?.arguments) {
94
+ if (typeof tc.function.arguments === "string") {
95
+ try {
96
+ const parsed = JSON.parse(tc.function.arguments);
97
+ input = typeof parsed === "string" ? { _raw: parsed } : parsed;
98
+ } catch {
99
+ input = { _raw: tc.function.arguments };
100
+ }
101
+ } else {
102
+ input = tc.function.arguments;
103
+ }
104
+ } else if (tc.arguments) {
105
+ if (typeof tc.arguments === "string") {
106
+ try {
107
+ const parsed = JSON.parse(tc.arguments);
108
+ input = typeof parsed === "string" ? { _raw: parsed } : parsed;
109
+ } catch {
110
+ input = { _raw: tc.arguments };
111
+ }
112
+ } else {
113
+ input = tc.arguments;
114
+ }
115
+ }
116
+ return {
117
+ type: "tool-call",
118
+ toolCallId: tc.id,
119
+ toolName: tc.function?.name || tc.name || "unknown",
120
+ input
121
+ };
122
+ });
123
+ return {
124
+ role: "assistant",
125
+ content: toolCallParts
126
+ };
127
+ }
128
+ if (msg.role === "tool") {
129
+ if (!msg.toolCallId) {
130
+ logger.warn("Tool message missing toolCallId", { msg });
131
+ }
132
+ let outputValue = "";
133
+ const content = msg.content;
134
+ if (typeof content === "string") {
135
+ outputValue = content;
136
+ } else if (Array.isArray(content)) {
137
+ const toolResultPart = content.find((p) => p?.type === "tool-result");
138
+ if (toolResultPart) {
139
+ outputValue = typeof toolResultPart.output === "string" ? toolResultPart.output : JSON.stringify(toolResultPart.output);
140
+ } else {
141
+ outputValue = JSON.stringify(content);
142
+ }
143
+ } else {
144
+ outputValue = JSON.stringify(content);
145
+ }
146
+ const converted = {
147
+ role: "tool",
148
+ content: [{
149
+ type: "tool-result",
150
+ toolCallId: msg.toolCallId || "unknown",
151
+ toolName: msg.name || "unknown",
152
+ output: {
153
+ type: "text",
154
+ value: outputValue
155
+ }
156
+ }]
157
+ };
158
+ return converted;
159
+ }
160
+ return msg;
161
+ });
162
+ }
163
+ function convertToolsToSDK(tools) {
164
+ const result = {};
165
+ for (const tool of tools) {
166
+ const jsonSchemaObj = extractToolSchema(tool.parameters);
167
+ result[tool.name] = {
168
+ description: tool.description || "",
169
+ inputSchema: jsonSchema(jsonSchemaObj)
170
+ };
171
+ }
172
+ return result;
173
+ }
174
+ function extractToolSchema(parameters) {
175
+ if (parameters && typeof parameters === "object" && "_def" in parameters) {
176
+ const zodSchema = parameters;
177
+ const schema = zodToJsonSchema(zodSchema, "zod");
178
+ if ("$ref" in schema && schema.definitions) {
179
+ const def = schema.definitions.zod;
180
+ if (def && def.type === "object" && def.properties) {
181
+ return {
182
+ type: "object",
183
+ properties: def.properties,
184
+ required: def.required,
185
+ additionalProperties: true
186
+ };
187
+ }
188
+ }
189
+ return schema;
190
+ }
191
+ return parameters;
192
+ }
193
+ function generateProviderOptions(providerType, options) {
194
+ const result = {
195
+ providerOptions: {}
196
+ };
197
+ if (options.temperature !== undefined) {
198
+ result.temperature = options.temperature;
199
+ }
200
+ if (options.maxTokens !== undefined) {
201
+ result.maxTokens = options.maxTokens;
202
+ }
203
+ switch (providerType) {
204
+ case "anthropic":
205
+ break;
206
+ case "openai-compatible":
207
+ result.providerOptions.includeUsage = true;
208
+ break;
209
+ }
210
+ return result;
211
+ }
212
+ function createEnvEventEmitter(env, context) {
213
+ if (!env) {
214
+ logger.warn("[invoke] createEnvEventEmitter: env is null/undefined, streaming events will be disabled");
215
+ return null;
216
+ }
217
+ return (event) => {
218
+ try {
219
+ const fullType = `llm.${event.type}`;
220
+ logger.info(`[invoke] Pushing LLM event: ${fullType}`, {
221
+ hasPayload: !!event,
222
+ payloadType: typeof event
223
+ });
224
+ env.pushEnvEvent({
225
+ id: randomUUID(),
226
+ type: fullType,
227
+ timestamp: Date.now(),
228
+ metadata: {
229
+ source: "llm.invoke",
230
+ sessionId: context?.sessionId,
231
+ messageId: context?.messageId
232
+ },
233
+ payload: event
234
+ });
235
+ logger.info(`[invoke] LLM event pushed successfully: ${fullType}`);
236
+ } catch (err) {
237
+ logger.warn("Failed to push env event", { error: String(err), eventType: event.type });
238
+ }
239
+ };
240
+ }
241
+ function getStreamPartText(part) {
242
+ const value = part.text ?? part.delta ?? part.textDelta;
243
+ return typeof value === "string" ? value : "";
244
+ }
245
+ function processThinkingStream(textDelta, config, state) {
246
+ if (!config.enabled || !textDelta) {
247
+ return {
248
+ cleanedText: textDelta,
249
+ isThinkingTagOpen: state.isOpen,
250
+ currentThinkingContent: state.content,
251
+ reasoningEvents: []
252
+ };
253
+ }
254
+ const tags = config.tags || ["thinking"];
255
+ let remainingText = textDelta;
256
+ const reasoningEvents2 = [];
257
+ let isOpen2 = state.isOpen;
258
+ let currentContent2 = state.content;
259
+ for (const tag of tags) {
260
+ const openTag = `<${tag}>`;
261
+ const closeTag = `</${tag}>`;
262
+ let text = remainingText;
263
+ let result = "";
264
+ const openIndex = text.toLowerCase().indexOf(openTag.toLowerCase());
265
+ const closeIndex = text.toLowerCase().indexOf(closeTag.toLowerCase());
266
+ if (openIndex !== -1 && (closeIndex === -1 || openIndex < closeIndex)) {
267
+ const beforeOpen = text.substring(0, openIndex);
268
+ const afterOpen = text.substring(openIndex + openTag.length);
269
+ if (!isOpen2) {
270
+ isOpen2 = true;
271
+ currentContent2 = "";
272
+ reasoningEvents2.push("");
273
+ }
274
+ result += beforeOpen;
275
+ const innerCloseIndex = afterOpen.toLowerCase().indexOf(closeTag.toLowerCase());
276
+ if (innerCloseIndex !== -1) {
277
+ const thinkingContent = afterOpen.substring(0, innerCloseIndex);
278
+ const afterClose = afterOpen.substring(innerCloseIndex + closeTag.length);
279
+ currentContent2 += thinkingContent;
280
+ reasoningEvents2.push(currentContent2);
281
+ isOpen2 = false;
282
+ currentContent2 = "";
283
+ result += afterClose;
284
+ } else {
285
+ currentContent2 += afterOpen;
286
+ reasoningEvents2.push(currentContent2);
287
+ result += "";
288
+ }
289
+ remainingText = result;
290
+ } else if (closeIndex !== -1) {
291
+ const beforeClose = text.substring(0, closeIndex);
292
+ const afterClose = text.substring(closeIndex + closeTag.length);
293
+ if (isOpen2) {
294
+ currentContent2 += beforeClose;
295
+ reasoningEvents2.push(currentContent2);
296
+ isOpen2 = false;
297
+ currentContent2 = "";
298
+ result = afterClose;
299
+ } else {
300
+ result = text;
301
+ }
302
+ remainingText = result;
303
+ }
304
+ }
305
+ if (isOpen2 && remainingText === textDelta) {
306
+ currentContent2 += remainingText;
307
+ reasoningEvents2.push(currentContent2);
308
+ remainingText = "";
309
+ }
310
+ return {
311
+ cleanedText: remainingText,
312
+ isThinkingTagOpen: isOpen2,
313
+ currentThinkingContent: currentContent2,
314
+ reasoningEvents: reasoningEvents2
315
+ };
316
+ }
317
+ async function invoke(config, options, ctx) {
318
+ try {
319
+ const emitToEnv = createEnvEventEmitter(options.env, options.context);
320
+ logger.info("[invoke] Created event emitter, env available:", !!options.env, "context:", options.context);
321
+ const { providerId, modelId } = parseModelString(options.model || config.model);
322
+ const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
323
+ if (!provider) {
324
+ throw new Error(`Failed to create provider for ${providerId}`);
325
+ }
326
+ const messages = convertToSDKMessages(options.messages);
327
+ const toolCallIds = new Set;
328
+ const assistantToolCalls = [];
329
+ const toolResultIds = [];
330
+ for (const msg of messages) {
331
+ if (msg.role === "assistant") {
332
+ const assistantContent = Array.isArray(msg.content) ? msg.content : [];
333
+ for (const part of assistantContent) {
334
+ if (part && typeof part === "object" && part.type === "tool-call") {
335
+ const tc = part.toolCall || part;
336
+ assistantToolCalls.push({ id: tc.id, name: tc.name || tc.function && tc.function.name });
337
+ toolCallIds.add(tc.id);
338
+ }
339
+ }
340
+ }
341
+ if (msg.role === "tool") {
342
+ const toolContent = Array.isArray(msg.content) ? msg.content : [];
343
+ for (const part of toolContent) {
344
+ if (part && typeof part === "object" && part.type === "tool-result") {
345
+ toolResultIds.push(part.toolCallId);
346
+ }
347
+ }
348
+ }
349
+ }
350
+ const missingToolResults = assistantToolCalls.filter((tc) => !toolResultIds.includes(tc.id));
351
+ const orphanedToolResults = toolResultIds.filter((id) => !toolCallIds.has(id));
352
+ if (assistantToolCalls.length > 0 || toolResultIds.length > 0) {
353
+ logger.debug("[invoke] ToolCallId matching analysis:", {
354
+ assistantToolCalls,
355
+ toolResultIds,
356
+ missingToolResults: missingToolResults.length > 0 ? missingToolResults : "none",
357
+ orphanedToolResults: orphanedToolResults.length > 0 ? orphanedToolResults : "none"
358
+ });
359
+ }
360
+ const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
361
+ const providerOptions = generateProviderOptions(providerId, {
362
+ temperature: options.temperature,
363
+ maxTokens: options.maxTokens
364
+ });
365
+ const model = `${providerId}/${modelId}`;
366
+ if (emitToEnv) {
367
+ logger.info("[invoke] Emitting llm.start event");
368
+ emitToEnv({ type: "start", metadata: { model } });
369
+ } else {
370
+ logger.warn("[invoke] emitToEnv is null, skipping llm.start event");
371
+ }
372
+ const streamTextOptions = {
373
+ model: provider.languageModel(modelId),
374
+ messages,
375
+ tools,
376
+ temperature: providerOptions.temperature,
377
+ maxTokens: providerOptions.maxTokens,
378
+ abortSignal: ctx.abort,
379
+ maxRetries: MAX_RETRIES,
380
+ streamOptions: {
381
+ includeUsage: true
382
+ },
383
+ allowSystemInMessages: true
384
+ };
385
+ const result = await streamText(streamTextOptions);
386
+ let fullContent = "";
387
+ let reasoningContent = "";
388
+ const toolCalls = [];
389
+ let usageInfo;
390
+ let isThinkingTagOpen = false;
391
+ let currentThinkingContent = "";
392
+ for await (const part of result.fullStream) {
393
+ const streamPart = part;
394
+ switch (streamPart.type) {
395
+ case "text-delta": {
396
+ const textDelta = getStreamPartText(streamPart);
397
+ if (!textDelta)
398
+ break;
399
+ const thinkingConfig = config.options?.thinkingInText;
400
+ if (thinkingConfig?.enabled) {
401
+ const thinkingResult = processThinkingStream(textDelta, thinkingConfig, {
402
+ isOpen: isThinkingTagOpen,
403
+ content: currentThinkingContent
404
+ });
405
+ isThinkingTagOpen = thinkingResult.isThinkingTagOpen;
406
+ currentThinkingContent = thinkingResult.currentThinkingContent;
407
+ fullContent += thinkingResult.cleanedText;
408
+ for (const reasoningSnapshot of thinkingResult.reasoningEvents) {
409
+ const delta = reasoningSnapshot.slice(reasoningContent.length);
410
+ reasoningContent = reasoningSnapshot;
411
+ if (emitToEnv && delta) {
412
+ emitToEnv({ type: "reasoning", content: reasoningContent, delta });
413
+ }
414
+ }
415
+ if (emitToEnv && thinkingResult.cleanedText) {
416
+ emitToEnv({ type: "text", content: fullContent, delta: thinkingResult.cleanedText });
417
+ }
418
+ } else {
419
+ fullContent += textDelta;
420
+ if (emitToEnv) {
421
+ emitToEnv({ type: "text", content: fullContent, delta: textDelta });
422
+ }
423
+ }
424
+ break;
425
+ }
426
+ case "reasoning-delta": {
427
+ const reasoningDelta = getStreamPartText(streamPart);
428
+ if (!reasoningDelta)
429
+ break;
430
+ reasoningContent += reasoningDelta;
431
+ if (emitToEnv) {
432
+ emitToEnv({ type: "reasoning", content: reasoningContent, delta: reasoningDelta });
433
+ }
434
+ break;
435
+ }
436
+ case "reasoning-end":
437
+ break;
438
+ case "tool-call": {
439
+ const toolInput = streamPart.input;
440
+ const toolCallId = streamPart.toolCallId ?? streamPart.id;
441
+ const toolName = streamPart.toolName;
442
+ toolCalls.push({
443
+ id: toolCallId,
444
+ name: toolName,
445
+ args: toolInput
446
+ });
447
+ break;
448
+ }
449
+ case "finish-step":
450
+ const stepUsage = extractUsageInfo(streamPart.usage);
451
+ if (stepUsage) {
452
+ usageInfo = stepUsage;
453
+ }
454
+ break;
455
+ case "error":
456
+ throw streamPart.error;
457
+ case "finish":
458
+ usageInfo = extractUsageInfo(streamPart.totalUsage);
459
+ if (!usageInfo && streamPart.usage) {
460
+ usageInfo = extractUsageInfo(streamPart.usage);
461
+ }
462
+ break;
463
+ }
464
+ }
465
+ if (emitToEnv) {
466
+ emitToEnv({
467
+ type: "completed",
468
+ content: fullContent,
469
+ metadata: {
470
+ model,
471
+ usage: usageInfo
472
+ }
473
+ });
474
+ }
475
+ if (toolCalls.length > 0) {
476
+ const firstToolCall = toolCalls[0];
477
+ return {
478
+ toolCallId: firstToolCall.id,
479
+ result: JSON.stringify({
480
+ content: fullContent,
481
+ reasoning: reasoningContent || undefined,
482
+ tool_calls: toolCalls.map((tc) => ({
483
+ id: tc.id,
484
+ function: {
485
+ name: tc.name,
486
+ arguments: JSON.stringify(tc.args)
487
+ }
488
+ }))
489
+ })
490
+ };
491
+ }
492
+ return {
493
+ toolCallId: "invoke-" + Date.now(),
494
+ result: JSON.stringify({
495
+ content: fullContent,
496
+ reasoning: reasoningContent || undefined,
497
+ usage: usageInfo,
498
+ model
499
+ })
500
+ };
501
+ } catch (error) {
502
+ logger.error("Error during invocation", {
503
+ error: error instanceof Error ? error.message : String(error),
504
+ stack: error instanceof Error ? error.stack : undefined
505
+ });
506
+ return {
507
+ toolCallId: "error-" + Date.now(),
508
+ result: error instanceof Error ? error.message : String(error),
509
+ isError: true
510
+ };
511
+ }
512
+ }
513
+ function createProviderInstance(providerId, apiKey, baseURL) {
514
+ try {
515
+ if (providerId === "openai") {
516
+ const openai = createOpenAI({
517
+ apiKey,
518
+ baseURL
519
+ });
520
+ return openai;
521
+ }
522
+ if (providerId === "anthropic") {
523
+ const anthropic = createAnthropic({
524
+ apiKey
525
+ });
526
+ return anthropic;
527
+ }
528
+ if (providerId === "google") {
529
+ const google = createGoogleGenerativeAI({
530
+ apiKey
531
+ });
532
+ return google;
533
+ }
534
+ const defaultProvider = createOpenAICompatible({
535
+ name: providerId,
536
+ apiKey,
537
+ baseURL: baseURL || "",
538
+ includeUsage: true
539
+ });
540
+ return defaultProvider;
541
+ } catch (error) {
542
+ logger.error(`Failed to create provider ${providerId}`, { error: String(error) });
543
+ return null;
544
+ }
545
+ }
546
+ async function invokeNonStream(config, options, ctx) {
547
+ const { providerId, modelId } = parseModelString(options.model || config.model);
548
+ const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
549
+ if (!provider) {
550
+ throw new Error(`Failed to create provider for ${providerId}`);
551
+ }
552
+ const messages = convertToSDKMessages(options.messages);
553
+ const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
554
+ const providerOptions = generateProviderOptions(providerId, {
555
+ temperature: options.temperature,
556
+ maxTokens: options.maxTokens
557
+ });
558
+ const { generateText } = await import("ai");
559
+ const genTextFn = generateText;
560
+ const result = await genTextFn({
561
+ model: provider.languageModel(modelId),
562
+ messages,
563
+ tools,
564
+ temperature: providerOptions.temperature,
565
+ maxTokens: providerOptions.maxTokens,
566
+ abortSignal: ctx.abort,
567
+ allowSystemInMessages: true
568
+ });
569
+ const usage = result.usage;
570
+ const usageInfo = usage ? {
571
+ promptTokens: usage.promptTokens ?? 0,
572
+ completionTokens: usage.completionTokens ?? 0,
573
+ totalTokens: usage.totalTokens ?? 0
574
+ } : undefined;
575
+ if (options.env) {
576
+ options.env.pushEnvEvent({
577
+ id: randomUUID(),
578
+ type: "llm.completed",
579
+ timestamp: Date.now(),
580
+ metadata: {
581
+ source: "llm.invokeNonStream",
582
+ sessionId: options.context?.sessionId,
583
+ messageId: options.context?.messageId
584
+ },
585
+ payload: {
586
+ type: "completed",
587
+ content: result.text,
588
+ metadata: {
589
+ model: `${providerId}/${modelId}`,
590
+ usage: usageInfo
591
+ }
592
+ }
593
+ });
594
+ }
595
+ return {
596
+ content: result.text,
597
+ finishReason: result.finishReason === "stop" ? "stop" : "tool-calls",
598
+ model: `${providerId}/${modelId}`,
599
+ usage: usageInfo
600
+ };
601
+ }
602
+ function createInvokeConfig(model, apiKey, baseURL) {
603
+ return {
604
+ model,
605
+ apiKey,
606
+ baseURL
607
+ };
608
+ }
609
+
610
+ export { withTimeout, parseModelString, invoke, invokeNonStream, createInvokeConfig };
@@ -92,6 +92,16 @@ var makeBashSpawner = () => {
92
92
  stderrChunks.push(data);
93
93
  });
94
94
  const exitCode = yield* Effect.async((resume) => {
95
+ if (child.exitCode !== null) {
96
+ exited = true;
97
+ resume(Effect.succeed(child.exitCode));
98
+ return;
99
+ }
100
+ if (child.killed) {
101
+ exited = true;
102
+ resume(Effect.succeed(1));
103
+ return;
104
+ }
95
105
  exitResolve = (code) => {
96
106
  if (!exited) {
97
107
  exited = true;
@@ -5,7 +5,7 @@ import {
5
5
  BackgroundTaskManager,
6
6
  createDelegateTool,
7
7
  createStopTool
8
- } from "./roy-agent-core-vehgmfj1.js";
8
+ } from "./roy-agent-core-nfa6fc3a.js";
9
9
  import {
10
10
  SQLiteTaskStore,
11
11
  getDefaultTaskDbPath
@@ -1,3 +1,6 @@
1
+ import {
2
+ withTimeout
3
+ } from "./roy-agent-core-4ch3ghj6.js";
1
4
  import {
2
5
  AskUserError,
3
6
  init_workflow_hil
@@ -965,6 +968,16 @@ class AgentComponent extends BaseComponent {
965
968
  }
966
969
  logger.info(`Agent aborted: ${agentName}`);
967
970
  }
971
+ abortAll() {
972
+ for (const key of this.aborted.keys()) {
973
+ this.aborted.set(key, true);
974
+ }
975
+ for (const [key, controller] of this.abortControllers) {
976
+ controller.abort();
977
+ logger.debug(`[abortAll] AbortController.abort() called for run: ${key}`);
978
+ }
979
+ logger.info(`All agents aborted (${this.abortControllers.size} runs)`);
980
+ }
968
981
  async _buildAdditionInfo(ctx) {
969
982
  let additionInfo = ctx.context?.additionInfo || "";
970
983
  const currentTaskId = getCurrentTaskId();
@@ -1002,7 +1015,7 @@ ${additionInfo}`
1002
1015
  const convertedMessages = messages.map(toLLMMessage);
1003
1016
  logger.debug(`[invokeLLM] Calling LLMComponent.invoke with ${convertedMessages.length} messages`);
1004
1017
  try {
1005
- const result = await this.llmComponent.invoke({
1018
+ const llmPromise = this.llmComponent.invoke({
1006
1019
  messages: convertedMessages,
1007
1020
  tools: ctx.tools.map((t) => ({
1008
1021
  name: t.name,
@@ -1016,6 +1029,7 @@ ${additionInfo}`
1016
1029
  },
1017
1030
  abortSignal: ctx.context.abort
1018
1031
  });
1032
+ const result = await withTimeout(llmPromise, 120000, "LLM invocation timeout after 120s");
1019
1033
  logger.debug("[invokeLLM] LLMComponent.invoke returned successfully");
1020
1034
  return {
1021
1035
  content: result.output?.content || "",
@@ -1089,11 +1103,22 @@ ${additionInfo}`
1089
1103
  context: toolContext
1090
1104
  };
1091
1105
  let result;
1106
+ const isLongRunningTool = toolName === "delegate_task" || toolName === "stop_task" || toolName === "bash";
1092
1107
  if (this.toolComponent?.execute) {
1093
- result = await this.toolComponent.execute(request);
1108
+ const toolPromise = this.toolComponent.execute(request);
1109
+ if (isLongRunningTool) {
1110
+ result = await toolPromise;
1111
+ } else {
1112
+ result = await withTimeout(toolPromise, 120000, `Tool '${toolName}' execution timeout after 120s`);
1113
+ }
1094
1114
  } else {
1095
1115
  logger.debug(`[executeTool] No ToolComponent available, executing tool directly`);
1096
- result = await tool.execute(toolCall.arguments, toolContext);
1116
+ const toolPromise = tool.execute(toolCall.arguments, toolContext);
1117
+ if (isLongRunningTool) {
1118
+ result = await toolPromise;
1119
+ } else {
1120
+ result = await withTimeout(toolPromise, 120000, `Tool '${toolName}' execution timeout after 120s`);
1121
+ }
1097
1122
  }
1098
1123
  this.pushEnvEvent({
1099
1124
  type: "tool.result",