@ai-setting/roy-agent-core 1.5.51 → 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.
Files changed (27) hide show
  1. package/dist/env/agent/index.js +3 -2
  2. package/dist/env/index.js +7 -6
  3. package/dist/env/llm/index.js +4 -2
  4. package/dist/env/prompt/index.js +1 -1
  5. package/dist/env/session/index.js +3 -3
  6. package/dist/env/session/storage/index.js +2 -2
  7. package/dist/env/task/delegate/index.js +1 -1
  8. package/dist/env/task/index.js +2 -2
  9. package/dist/env/tool/built-in/index.js +1 -1
  10. package/dist/env/tool/index.js +2 -2
  11. package/dist/env/workflow/engine/index.js +1 -1
  12. package/dist/env/workflow/index.js +2 -2
  13. package/dist/index.js +19 -17
  14. package/dist/shared/@ai-setting/{roy-agent-core-smwwze1m.js → roy-agent-core-0vt0trev.js} +1 -1
  15. package/dist/shared/@ai-setting/roy-agent-core-4ch3ghj6.js +610 -0
  16. package/dist/shared/@ai-setting/{roy-agent-core-6de35s2n.js → roy-agent-core-9m9m6fe1.js} +1 -1
  17. package/dist/shared/@ai-setting/{roy-agent-core-ysvh8er9.js → roy-agent-core-bwrzwq5x.js} +10 -0
  18. package/dist/shared/@ai-setting/{roy-agent-core-7b35emr7.js → roy-agent-core-ew29335n.js} +3 -2
  19. package/dist/shared/@ai-setting/{roy-agent-core-wbqmmavh.js → roy-agent-core-fwq0hs6e.js} +1 -1
  20. package/dist/shared/@ai-setting/{roy-agent-core-2k4r9grg.js → roy-agent-core-g7qhnsz3.js} +46 -8
  21. package/dist/shared/@ai-setting/{roy-agent-core-qjh1ec46.js → roy-agent-core-hv713d36.js} +13 -582
  22. package/dist/shared/@ai-setting/{roy-agent-core-rmndq0sb.js → roy-agent-core-jkhhsa3t.js} +13 -6
  23. package/dist/shared/@ai-setting/{roy-agent-core-vehgmfj1.js → roy-agent-core-nfa6fc3a.js} +2 -2
  24. package/dist/shared/@ai-setting/{roy-agent-core-6mcb7nqa.js → roy-agent-core-pcjenbaf.js} +1 -1
  25. package/dist/shared/@ai-setting/{roy-agent-core-h9tpga25.js → roy-agent-core-vbyct0e7.js} +18 -13
  26. package/package.json +1 -1
  27. /package/dist/shared/@ai-setting/{roy-agent-core-4hk6ey73.js → roy-agent-core-nt1q2g91.js} +0 -0
@@ -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 };
@@ -6,7 +6,7 @@ import {
6
6
  import {
7
7
  MemorySessionStore,
8
8
  SQLiteSessionStore
9
- } from "./roy-agent-core-6mcb7nqa.js";
9
+ } from "./roy-agent-core-pcjenbaf.js";
10
10
  import {
11
11
  SummaryAgent
12
12
  } from "./roy-agent-core-kwhv9dcd.js";
@@ -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;
@@ -26,7 +26,8 @@ class SessionMessageConverter {
26
26
  let parsedArgs = {};
27
27
  if (typeof toolPart.arguments === "string") {
28
28
  try {
29
- parsedArgs = JSON.parse(toolPart.arguments);
29
+ const parsed = JSON.parse(toolPart.arguments);
30
+ parsedArgs = typeof parsed === "string" ? { _raw: parsed } : parsed;
30
31
  } catch {
31
32
  parsedArgs = { _raw: toolPart.arguments };
32
33
  }
@@ -98,7 +99,7 @@ class SessionMessageConverter {
98
99
  type: "tool-call",
99
100
  toolCallId: p.toolCallId,
100
101
  toolName: p.toolName,
101
- arguments: p.input,
102
+ arguments: typeof p.input === "string" ? { _raw: p.input } : p.input,
102
103
  state: "pending"
103
104
  });
104
105
  } else if (p.type === "tool-result") {
@@ -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