@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.
- package/dist/env/agent/index.js +3 -2
- package/dist/env/index.js +7 -6
- package/dist/env/llm/index.js +4 -2
- package/dist/env/prompt/index.js +1 -1
- package/dist/env/session/index.js +3 -3
- package/dist/env/session/storage/index.js +2 -2
- package/dist/env/task/delegate/index.js +1 -1
- package/dist/env/task/index.js +2 -2
- package/dist/env/tool/built-in/index.js +1 -1
- package/dist/env/tool/index.js +2 -2
- package/dist/env/workflow/engine/index.js +1 -1
- package/dist/env/workflow/index.js +2 -2
- package/dist/index.js +19 -17
- package/dist/shared/@ai-setting/{roy-agent-core-smwwze1m.js → roy-agent-core-0vt0trev.js} +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-4ch3ghj6.js +610 -0
- package/dist/shared/@ai-setting/{roy-agent-core-6de35s2n.js → roy-agent-core-9m9m6fe1.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-ysvh8er9.js → roy-agent-core-bwrzwq5x.js} +10 -0
- package/dist/shared/@ai-setting/{roy-agent-core-7b35emr7.js → roy-agent-core-ew29335n.js} +3 -2
- package/dist/shared/@ai-setting/{roy-agent-core-wbqmmavh.js → roy-agent-core-fwq0hs6e.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-2k4r9grg.js → roy-agent-core-g7qhnsz3.js} +46 -8
- package/dist/shared/@ai-setting/{roy-agent-core-qjh1ec46.js → roy-agent-core-hv713d36.js} +13 -582
- package/dist/shared/@ai-setting/{roy-agent-core-rmndq0sb.js → roy-agent-core-jkhhsa3t.js} +13 -6
- package/dist/shared/@ai-setting/{roy-agent-core-vehgmfj1.js → roy-agent-core-nfa6fc3a.js} +2 -2
- package/dist/shared/@ai-setting/{roy-agent-core-6mcb7nqa.js → roy-agent-core-pcjenbaf.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-h9tpga25.js → roy-agent-core-vbyct0e7.js} +18 -13
- package/package.json +1 -1
- /package/dist/shared/@ai-setting/{roy-agent-core-4hk6ey73.js → roy-agent-core-nt1q2g91.js} +0 -0
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {
|
|
2
|
+
invoke
|
|
3
|
+
} from "./roy-agent-core-4ch3ghj6.js";
|
|
1
4
|
import {
|
|
2
5
|
ContextError,
|
|
3
6
|
ErrorCodes
|
|
@@ -23,8 +26,7 @@ import {
|
|
|
23
26
|
init_logger
|
|
24
27
|
} from "./roy-agent-core-10n2jh7p.js";
|
|
25
28
|
import {
|
|
26
|
-
__legacyDecorateClassTS
|
|
27
|
-
__require
|
|
29
|
+
__legacyDecorateClassTS
|
|
28
30
|
} from "./roy-agent-core-fs0mn2jk.js";
|
|
29
31
|
|
|
30
32
|
// src/env/llm/types.ts
|
|
@@ -309,577 +311,6 @@ class LLMTransform {
|
|
|
309
311
|
return JSON.stringify(args);
|
|
310
312
|
}
|
|
311
313
|
}
|
|
312
|
-
// src/env/llm/invoke.ts
|
|
313
|
-
init_logger();
|
|
314
|
-
import { randomUUID } from "crypto";
|
|
315
|
-
import { streamText, jsonSchema } from "ai";
|
|
316
|
-
import { createOpenAI } from "@ai-sdk/openai";
|
|
317
|
-
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
318
|
-
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
319
|
-
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
320
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
321
|
-
var logger = createLogger("llm:invoke");
|
|
322
|
-
var MAX_RETRIES = 3;
|
|
323
|
-
function parseModelString(model) {
|
|
324
|
-
if (!model) {
|
|
325
|
-
return { providerId: "openai", modelId: "gpt-4o" };
|
|
326
|
-
}
|
|
327
|
-
const parts = model.split("/");
|
|
328
|
-
if (parts.length >= 2) {
|
|
329
|
-
return { providerId: parts[0], modelId: parts.slice(1).join("/") };
|
|
330
|
-
}
|
|
331
|
-
return { providerId: "openai", modelId: model };
|
|
332
|
-
}
|
|
333
|
-
function extractUsageInfo(usage) {
|
|
334
|
-
if (!usage)
|
|
335
|
-
return;
|
|
336
|
-
if (usage.inputTokens && typeof usage.inputTokens === "object") {
|
|
337
|
-
return {
|
|
338
|
-
promptTokens: usage.inputTokens.total ?? 0,
|
|
339
|
-
completionTokens: usage.outputTokens?.total ?? 0,
|
|
340
|
-
totalTokens: usage.totalTokens ?? 0
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
if (typeof usage.promptTokens === "number") {
|
|
344
|
-
return {
|
|
345
|
-
promptTokens: usage.promptTokens,
|
|
346
|
-
completionTokens: usage.completionTokens ?? 0,
|
|
347
|
-
totalTokens: usage.totalTokens ?? 0
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
if (usage.inputTokenDetails || usage.outputTokenDetails) {
|
|
351
|
-
return {
|
|
352
|
-
promptTokens: usage.inputTokenDetails?.tokens ?? 0,
|
|
353
|
-
completionTokens: usage.outputTokenDetails?.tokens ?? 0,
|
|
354
|
-
totalTokens: usage.totalTokens ?? 0
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
if (usage.raw && typeof usage.raw.total_tokens === "number") {
|
|
358
|
-
return {
|
|
359
|
-
promptTokens: 0,
|
|
360
|
-
completionTokens: 0,
|
|
361
|
-
totalTokens: usage.raw.total_tokens
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
function convertToSDKMessages(messages) {
|
|
367
|
-
return messages.map((msg) => {
|
|
368
|
-
if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) {
|
|
369
|
-
const toolCallParts = msg.toolCalls.map((tc) => {
|
|
370
|
-
let input = {};
|
|
371
|
-
if (tc.function?.arguments) {
|
|
372
|
-
if (typeof tc.function.arguments === "string") {
|
|
373
|
-
try {
|
|
374
|
-
input = JSON.parse(tc.function.arguments);
|
|
375
|
-
} catch {
|
|
376
|
-
input = tc.function.arguments;
|
|
377
|
-
}
|
|
378
|
-
} else {
|
|
379
|
-
input = tc.function.arguments;
|
|
380
|
-
}
|
|
381
|
-
} else if (tc.arguments) {
|
|
382
|
-
if (typeof tc.arguments === "string") {
|
|
383
|
-
try {
|
|
384
|
-
input = JSON.parse(tc.arguments);
|
|
385
|
-
} catch {
|
|
386
|
-
input = tc.arguments;
|
|
387
|
-
}
|
|
388
|
-
} else {
|
|
389
|
-
input = tc.arguments;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
return {
|
|
393
|
-
type: "tool-call",
|
|
394
|
-
toolCallId: tc.id,
|
|
395
|
-
toolName: tc.function?.name || tc.name || "unknown",
|
|
396
|
-
input
|
|
397
|
-
};
|
|
398
|
-
});
|
|
399
|
-
return {
|
|
400
|
-
role: "assistant",
|
|
401
|
-
content: toolCallParts
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
if (msg.role === "tool") {
|
|
405
|
-
if (!msg.toolCallId) {
|
|
406
|
-
logger.warn("Tool message missing toolCallId", { msg });
|
|
407
|
-
}
|
|
408
|
-
let outputValue = "";
|
|
409
|
-
const content = msg.content;
|
|
410
|
-
if (typeof content === "string") {
|
|
411
|
-
outputValue = content;
|
|
412
|
-
} else if (Array.isArray(content)) {
|
|
413
|
-
const toolResultPart = content.find((p) => p?.type === "tool-result");
|
|
414
|
-
if (toolResultPart) {
|
|
415
|
-
outputValue = typeof toolResultPart.output === "string" ? toolResultPart.output : JSON.stringify(toolResultPart.output);
|
|
416
|
-
} else {
|
|
417
|
-
outputValue = JSON.stringify(content);
|
|
418
|
-
}
|
|
419
|
-
} else {
|
|
420
|
-
outputValue = JSON.stringify(content);
|
|
421
|
-
}
|
|
422
|
-
const converted = {
|
|
423
|
-
role: "tool",
|
|
424
|
-
content: [{
|
|
425
|
-
type: "tool-result",
|
|
426
|
-
toolCallId: msg.toolCallId || "unknown",
|
|
427
|
-
toolName: msg.name || "unknown",
|
|
428
|
-
output: {
|
|
429
|
-
type: "text",
|
|
430
|
-
value: outputValue
|
|
431
|
-
}
|
|
432
|
-
}]
|
|
433
|
-
};
|
|
434
|
-
return converted;
|
|
435
|
-
}
|
|
436
|
-
return msg;
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
function convertToolsToSDK(tools) {
|
|
440
|
-
const result = {};
|
|
441
|
-
for (const tool of tools) {
|
|
442
|
-
const jsonSchemaObj = extractToolSchema(tool.parameters);
|
|
443
|
-
result[tool.name] = {
|
|
444
|
-
description: tool.description || "",
|
|
445
|
-
inputSchema: jsonSchema(jsonSchemaObj)
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
return result;
|
|
449
|
-
}
|
|
450
|
-
function extractToolSchema(parameters) {
|
|
451
|
-
if (parameters && typeof parameters === "object" && "_def" in parameters) {
|
|
452
|
-
const zodSchema = parameters;
|
|
453
|
-
const schema = zodToJsonSchema(zodSchema, "zod");
|
|
454
|
-
if ("$ref" in schema && schema.definitions) {
|
|
455
|
-
const def = schema.definitions.zod;
|
|
456
|
-
if (def && def.type === "object" && def.properties) {
|
|
457
|
-
return {
|
|
458
|
-
type: "object",
|
|
459
|
-
properties: def.properties,
|
|
460
|
-
required: def.required,
|
|
461
|
-
additionalProperties: true
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
return schema;
|
|
466
|
-
}
|
|
467
|
-
return parameters;
|
|
468
|
-
}
|
|
469
|
-
function generateProviderOptions(providerType, options) {
|
|
470
|
-
const result = {
|
|
471
|
-
providerOptions: {}
|
|
472
|
-
};
|
|
473
|
-
if (options.temperature !== undefined) {
|
|
474
|
-
result.temperature = options.temperature;
|
|
475
|
-
}
|
|
476
|
-
if (options.maxTokens !== undefined) {
|
|
477
|
-
result.maxTokens = options.maxTokens;
|
|
478
|
-
}
|
|
479
|
-
switch (providerType) {
|
|
480
|
-
case "anthropic":
|
|
481
|
-
break;
|
|
482
|
-
case "openai-compatible":
|
|
483
|
-
result.providerOptions.includeUsage = true;
|
|
484
|
-
break;
|
|
485
|
-
}
|
|
486
|
-
return result;
|
|
487
|
-
}
|
|
488
|
-
function createEnvEventEmitter(env, context) {
|
|
489
|
-
if (!env) {
|
|
490
|
-
logger.warn("[invoke] createEnvEventEmitter: env is null/undefined, streaming events will be disabled");
|
|
491
|
-
return null;
|
|
492
|
-
}
|
|
493
|
-
return (event) => {
|
|
494
|
-
try {
|
|
495
|
-
const fullType = `llm.${event.type}`;
|
|
496
|
-
logger.info(`[invoke] Pushing LLM event: ${fullType}`, {
|
|
497
|
-
hasPayload: !!event,
|
|
498
|
-
payloadType: typeof event
|
|
499
|
-
});
|
|
500
|
-
env.pushEnvEvent({
|
|
501
|
-
id: randomUUID(),
|
|
502
|
-
type: fullType,
|
|
503
|
-
timestamp: Date.now(),
|
|
504
|
-
metadata: {
|
|
505
|
-
source: "llm.invoke",
|
|
506
|
-
sessionId: context?.sessionId,
|
|
507
|
-
messageId: context?.messageId
|
|
508
|
-
},
|
|
509
|
-
payload: event
|
|
510
|
-
});
|
|
511
|
-
logger.info(`[invoke] LLM event pushed successfully: ${fullType}`);
|
|
512
|
-
} catch (err) {
|
|
513
|
-
logger.warn("Failed to push env event", { error: String(err), eventType: event.type });
|
|
514
|
-
}
|
|
515
|
-
};
|
|
516
|
-
}
|
|
517
|
-
function getStreamPartText(part) {
|
|
518
|
-
const value = part.text ?? part.delta ?? part.textDelta;
|
|
519
|
-
return typeof value === "string" ? value : "";
|
|
520
|
-
}
|
|
521
|
-
function processThinkingStream(textDelta, config, state) {
|
|
522
|
-
if (!config.enabled || !textDelta) {
|
|
523
|
-
return {
|
|
524
|
-
cleanedText: textDelta,
|
|
525
|
-
isThinkingTagOpen: state.isOpen,
|
|
526
|
-
currentThinkingContent: state.content,
|
|
527
|
-
reasoningEvents: []
|
|
528
|
-
};
|
|
529
|
-
}
|
|
530
|
-
const tags = config.tags || ["thinking"];
|
|
531
|
-
let remainingText = textDelta;
|
|
532
|
-
const reasoningEvents = [];
|
|
533
|
-
let isOpen = state.isOpen;
|
|
534
|
-
let currentContent = state.content;
|
|
535
|
-
for (const tag of tags) {
|
|
536
|
-
const openTag = `<${tag}>`;
|
|
537
|
-
const closeTag = `</${tag}>`;
|
|
538
|
-
let text = remainingText;
|
|
539
|
-
let result = "";
|
|
540
|
-
const openIndex = text.toLowerCase().indexOf(openTag.toLowerCase());
|
|
541
|
-
const closeIndex = text.toLowerCase().indexOf(closeTag.toLowerCase());
|
|
542
|
-
if (openIndex !== -1 && (closeIndex === -1 || openIndex < closeIndex)) {
|
|
543
|
-
const beforeOpen = text.substring(0, openIndex);
|
|
544
|
-
const afterOpen = text.substring(openIndex + openTag.length);
|
|
545
|
-
if (!isOpen) {
|
|
546
|
-
isOpen = true;
|
|
547
|
-
currentContent = "";
|
|
548
|
-
reasoningEvents.push("");
|
|
549
|
-
}
|
|
550
|
-
result += beforeOpen;
|
|
551
|
-
const innerCloseIndex = afterOpen.toLowerCase().indexOf(closeTag.toLowerCase());
|
|
552
|
-
if (innerCloseIndex !== -1) {
|
|
553
|
-
const thinkingContent = afterOpen.substring(0, innerCloseIndex);
|
|
554
|
-
const afterClose = afterOpen.substring(innerCloseIndex + closeTag.length);
|
|
555
|
-
currentContent += thinkingContent;
|
|
556
|
-
reasoningEvents.push(currentContent);
|
|
557
|
-
isOpen = false;
|
|
558
|
-
currentContent = "";
|
|
559
|
-
result += afterClose;
|
|
560
|
-
} else {
|
|
561
|
-
currentContent += afterOpen;
|
|
562
|
-
reasoningEvents.push(currentContent);
|
|
563
|
-
result += "";
|
|
564
|
-
}
|
|
565
|
-
remainingText = result;
|
|
566
|
-
} else if (closeIndex !== -1) {
|
|
567
|
-
const beforeClose = text.substring(0, closeIndex);
|
|
568
|
-
const afterClose = text.substring(closeIndex + closeTag.length);
|
|
569
|
-
if (isOpen) {
|
|
570
|
-
currentContent += beforeClose;
|
|
571
|
-
reasoningEvents.push(currentContent);
|
|
572
|
-
isOpen = false;
|
|
573
|
-
currentContent = "";
|
|
574
|
-
result = afterClose;
|
|
575
|
-
} else {
|
|
576
|
-
result = text;
|
|
577
|
-
}
|
|
578
|
-
remainingText = result;
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
return {
|
|
582
|
-
cleanedText: remainingText,
|
|
583
|
-
isThinkingTagOpen: isOpen,
|
|
584
|
-
currentThinkingContent: currentContent,
|
|
585
|
-
reasoningEvents
|
|
586
|
-
};
|
|
587
|
-
}
|
|
588
|
-
async function invoke(config, options, ctx) {
|
|
589
|
-
try {
|
|
590
|
-
const emitToEnv = createEnvEventEmitter(options.env, options.context);
|
|
591
|
-
logger.info("[invoke] Created event emitter, env available:", !!options.env, "context:", options.context);
|
|
592
|
-
const { providerId, modelId } = parseModelString(options.model || config.model);
|
|
593
|
-
const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
|
|
594
|
-
if (!provider) {
|
|
595
|
-
throw new Error(`Failed to create provider for ${providerId}`);
|
|
596
|
-
}
|
|
597
|
-
const messages = convertToSDKMessages(options.messages);
|
|
598
|
-
const toolCallIds = new Set;
|
|
599
|
-
const assistantToolCalls = [];
|
|
600
|
-
const toolResultIds = [];
|
|
601
|
-
for (const msg of messages) {
|
|
602
|
-
if (msg.role === "assistant") {
|
|
603
|
-
const assistantContent = Array.isArray(msg.content) ? msg.content : [];
|
|
604
|
-
for (const part of assistantContent) {
|
|
605
|
-
if (part && typeof part === "object" && part.type === "tool-call") {
|
|
606
|
-
const tc = part.toolCall || part;
|
|
607
|
-
assistantToolCalls.push({ id: tc.id, name: tc.name || tc.function && tc.function.name });
|
|
608
|
-
toolCallIds.add(tc.id);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
if (msg.role === "tool") {
|
|
613
|
-
const toolContent = Array.isArray(msg.content) ? msg.content : [];
|
|
614
|
-
for (const part of toolContent) {
|
|
615
|
-
if (part && typeof part === "object" && part.type === "tool-result") {
|
|
616
|
-
toolResultIds.push(part.toolCallId);
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
const missingToolResults = assistantToolCalls.filter((tc) => !toolResultIds.includes(tc.id));
|
|
622
|
-
const orphanedToolResults = toolResultIds.filter((id) => !toolCallIds.has(id));
|
|
623
|
-
if (assistantToolCalls.length > 0 || toolResultIds.length > 0) {
|
|
624
|
-
logger.debug("[invoke] ToolCallId matching analysis:", {
|
|
625
|
-
assistantToolCalls,
|
|
626
|
-
toolResultIds,
|
|
627
|
-
missingToolResults: missingToolResults.length > 0 ? missingToolResults : "none",
|
|
628
|
-
orphanedToolResults: orphanedToolResults.length > 0 ? orphanedToolResults : "none"
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
|
|
632
|
-
const providerOptions = generateProviderOptions(providerId, {
|
|
633
|
-
temperature: options.temperature,
|
|
634
|
-
maxTokens: options.maxTokens
|
|
635
|
-
});
|
|
636
|
-
const model = `${providerId}/${modelId}`;
|
|
637
|
-
if (emitToEnv) {
|
|
638
|
-
logger.info("[invoke] Emitting llm.start event");
|
|
639
|
-
emitToEnv({ type: "start", metadata: { model } });
|
|
640
|
-
} else {
|
|
641
|
-
logger.warn("[invoke] emitToEnv is null, skipping llm.start event");
|
|
642
|
-
}
|
|
643
|
-
const streamTextOptions = {
|
|
644
|
-
model: provider.languageModel(modelId),
|
|
645
|
-
messages,
|
|
646
|
-
tools,
|
|
647
|
-
temperature: providerOptions.temperature,
|
|
648
|
-
maxTokens: providerOptions.maxTokens,
|
|
649
|
-
abortSignal: ctx.abort,
|
|
650
|
-
maxRetries: MAX_RETRIES,
|
|
651
|
-
streamOptions: {
|
|
652
|
-
includeUsage: true
|
|
653
|
-
},
|
|
654
|
-
allowSystemInMessages: true
|
|
655
|
-
};
|
|
656
|
-
const result = await streamText(streamTextOptions);
|
|
657
|
-
let fullContent = "";
|
|
658
|
-
let reasoningContent = "";
|
|
659
|
-
const toolCalls = [];
|
|
660
|
-
let usageInfo;
|
|
661
|
-
let isThinkingTagOpen = false;
|
|
662
|
-
let currentThinkingContent = "";
|
|
663
|
-
for await (const part of result.fullStream) {
|
|
664
|
-
const streamPart = part;
|
|
665
|
-
switch (streamPart.type) {
|
|
666
|
-
case "text-delta": {
|
|
667
|
-
const textDelta = getStreamPartText(streamPart);
|
|
668
|
-
if (!textDelta)
|
|
669
|
-
break;
|
|
670
|
-
const thinkingConfig = config.options?.thinkingInText;
|
|
671
|
-
if (thinkingConfig?.enabled) {
|
|
672
|
-
const thinkingResult = processThinkingStream(textDelta, thinkingConfig, {
|
|
673
|
-
isOpen: isThinkingTagOpen,
|
|
674
|
-
content: currentThinkingContent
|
|
675
|
-
});
|
|
676
|
-
isThinkingTagOpen = thinkingResult.isThinkingTagOpen;
|
|
677
|
-
currentThinkingContent = thinkingResult.currentThinkingContent;
|
|
678
|
-
fullContent += thinkingResult.cleanedText;
|
|
679
|
-
for (const reasoningSnapshot of thinkingResult.reasoningEvents) {
|
|
680
|
-
const delta = reasoningSnapshot.slice(reasoningContent.length);
|
|
681
|
-
reasoningContent = reasoningSnapshot;
|
|
682
|
-
if (emitToEnv && delta) {
|
|
683
|
-
emitToEnv({ type: "reasoning", content: reasoningContent, delta });
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
if (emitToEnv && thinkingResult.cleanedText) {
|
|
687
|
-
emitToEnv({ type: "text", content: fullContent, delta: thinkingResult.cleanedText });
|
|
688
|
-
}
|
|
689
|
-
} else {
|
|
690
|
-
fullContent += textDelta;
|
|
691
|
-
if (emitToEnv) {
|
|
692
|
-
emitToEnv({ type: "text", content: fullContent, delta: textDelta });
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
break;
|
|
696
|
-
}
|
|
697
|
-
case "reasoning-delta": {
|
|
698
|
-
const reasoningDelta = getStreamPartText(streamPart);
|
|
699
|
-
if (!reasoningDelta)
|
|
700
|
-
break;
|
|
701
|
-
reasoningContent += reasoningDelta;
|
|
702
|
-
if (emitToEnv) {
|
|
703
|
-
emitToEnv({ type: "reasoning", content: reasoningContent, delta: reasoningDelta });
|
|
704
|
-
}
|
|
705
|
-
break;
|
|
706
|
-
}
|
|
707
|
-
case "reasoning-end":
|
|
708
|
-
if (emitToEnv) {
|
|
709
|
-
emitToEnv({ type: "reasoning", content: reasoningContent, phase: "end" });
|
|
710
|
-
}
|
|
711
|
-
break;
|
|
712
|
-
case "tool-call": {
|
|
713
|
-
const toolInput = streamPart.input;
|
|
714
|
-
const toolCallId = streamPart.toolCallId ?? streamPart.id;
|
|
715
|
-
const toolName = streamPart.toolName;
|
|
716
|
-
toolCalls.push({
|
|
717
|
-
id: toolCallId,
|
|
718
|
-
name: toolName,
|
|
719
|
-
args: toolInput
|
|
720
|
-
});
|
|
721
|
-
break;
|
|
722
|
-
}
|
|
723
|
-
case "finish-step":
|
|
724
|
-
const stepUsage = extractUsageInfo(streamPart.usage);
|
|
725
|
-
if (stepUsage) {
|
|
726
|
-
usageInfo = stepUsage;
|
|
727
|
-
}
|
|
728
|
-
break;
|
|
729
|
-
case "error":
|
|
730
|
-
throw streamPart.error;
|
|
731
|
-
case "finish":
|
|
732
|
-
usageInfo = extractUsageInfo(streamPart.totalUsage);
|
|
733
|
-
if (!usageInfo && streamPart.usage) {
|
|
734
|
-
usageInfo = extractUsageInfo(streamPart.usage);
|
|
735
|
-
}
|
|
736
|
-
break;
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
if (emitToEnv) {
|
|
740
|
-
emitToEnv({
|
|
741
|
-
type: "completed",
|
|
742
|
-
content: fullContent,
|
|
743
|
-
metadata: {
|
|
744
|
-
model,
|
|
745
|
-
usage: usageInfo
|
|
746
|
-
}
|
|
747
|
-
});
|
|
748
|
-
}
|
|
749
|
-
if (toolCalls.length > 0) {
|
|
750
|
-
const firstToolCall = toolCalls[0];
|
|
751
|
-
return {
|
|
752
|
-
toolCallId: firstToolCall.id,
|
|
753
|
-
result: JSON.stringify({
|
|
754
|
-
content: fullContent,
|
|
755
|
-
reasoning: reasoningContent || undefined,
|
|
756
|
-
tool_calls: toolCalls.map((tc) => ({
|
|
757
|
-
id: tc.id,
|
|
758
|
-
function: {
|
|
759
|
-
name: tc.name,
|
|
760
|
-
arguments: JSON.stringify(tc.args)
|
|
761
|
-
}
|
|
762
|
-
}))
|
|
763
|
-
})
|
|
764
|
-
};
|
|
765
|
-
}
|
|
766
|
-
return {
|
|
767
|
-
toolCallId: "invoke-" + Date.now(),
|
|
768
|
-
result: JSON.stringify({
|
|
769
|
-
content: fullContent,
|
|
770
|
-
reasoning: reasoningContent || undefined,
|
|
771
|
-
usage: usageInfo,
|
|
772
|
-
model
|
|
773
|
-
})
|
|
774
|
-
};
|
|
775
|
-
} catch (error) {
|
|
776
|
-
logger.error("Error during invocation", {
|
|
777
|
-
error: error instanceof Error ? error.message : String(error),
|
|
778
|
-
stack: error instanceof Error ? error.stack : undefined
|
|
779
|
-
});
|
|
780
|
-
return {
|
|
781
|
-
toolCallId: "error-" + Date.now(),
|
|
782
|
-
result: error instanceof Error ? error.message : String(error),
|
|
783
|
-
isError: true
|
|
784
|
-
};
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
function createProviderInstance(providerId, apiKey, baseURL) {
|
|
788
|
-
try {
|
|
789
|
-
if (providerId === "openai") {
|
|
790
|
-
const openai = createOpenAI({
|
|
791
|
-
apiKey,
|
|
792
|
-
baseURL
|
|
793
|
-
});
|
|
794
|
-
return openai;
|
|
795
|
-
}
|
|
796
|
-
if (providerId === "anthropic") {
|
|
797
|
-
const anthropic = createAnthropic({
|
|
798
|
-
apiKey
|
|
799
|
-
});
|
|
800
|
-
return anthropic;
|
|
801
|
-
}
|
|
802
|
-
if (providerId === "google") {
|
|
803
|
-
const google = createGoogleGenerativeAI({
|
|
804
|
-
apiKey
|
|
805
|
-
});
|
|
806
|
-
return google;
|
|
807
|
-
}
|
|
808
|
-
const defaultProvider = createOpenAICompatible({
|
|
809
|
-
name: providerId,
|
|
810
|
-
apiKey,
|
|
811
|
-
baseURL: baseURL || "",
|
|
812
|
-
includeUsage: true
|
|
813
|
-
});
|
|
814
|
-
return defaultProvider;
|
|
815
|
-
} catch (error) {
|
|
816
|
-
logger.error(`Failed to create provider ${providerId}`, { error: String(error) });
|
|
817
|
-
return null;
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
async function invokeNonStream(config, options, ctx) {
|
|
821
|
-
const { providerId, modelId } = parseModelString(options.model || config.model);
|
|
822
|
-
const provider = createProviderInstance(providerId, config.apiKey, config.baseURL);
|
|
823
|
-
if (!provider) {
|
|
824
|
-
throw new Error(`Failed to create provider for ${providerId}`);
|
|
825
|
-
}
|
|
826
|
-
const messages = convertToSDKMessages(options.messages);
|
|
827
|
-
const tools = options.tools && options.tools.length > 0 ? convertToolsToSDK(options.tools) : undefined;
|
|
828
|
-
const providerOptions = generateProviderOptions(providerId, {
|
|
829
|
-
temperature: options.temperature,
|
|
830
|
-
maxTokens: options.maxTokens
|
|
831
|
-
});
|
|
832
|
-
const { generateText } = await import("ai");
|
|
833
|
-
const genTextFn = generateText;
|
|
834
|
-
const result = await genTextFn({
|
|
835
|
-
model: provider.languageModel(modelId),
|
|
836
|
-
messages,
|
|
837
|
-
tools,
|
|
838
|
-
temperature: providerOptions.temperature,
|
|
839
|
-
maxTokens: providerOptions.maxTokens,
|
|
840
|
-
abortSignal: ctx.abort,
|
|
841
|
-
allowSystemInMessages: true
|
|
842
|
-
});
|
|
843
|
-
const usage = result.usage;
|
|
844
|
-
const usageInfo = usage ? {
|
|
845
|
-
promptTokens: usage.promptTokens ?? 0,
|
|
846
|
-
completionTokens: usage.completionTokens ?? 0,
|
|
847
|
-
totalTokens: usage.totalTokens ?? 0
|
|
848
|
-
} : undefined;
|
|
849
|
-
if (options.env) {
|
|
850
|
-
options.env.pushEnvEvent({
|
|
851
|
-
id: randomUUID(),
|
|
852
|
-
type: "llm.completed",
|
|
853
|
-
timestamp: Date.now(),
|
|
854
|
-
metadata: {
|
|
855
|
-
source: "llm.invokeNonStream",
|
|
856
|
-
sessionId: options.context?.sessionId,
|
|
857
|
-
messageId: options.context?.messageId
|
|
858
|
-
},
|
|
859
|
-
payload: {
|
|
860
|
-
type: "completed",
|
|
861
|
-
content: result.text,
|
|
862
|
-
metadata: {
|
|
863
|
-
model: `${providerId}/${modelId}`,
|
|
864
|
-
usage: usageInfo
|
|
865
|
-
}
|
|
866
|
-
}
|
|
867
|
-
});
|
|
868
|
-
}
|
|
869
|
-
return {
|
|
870
|
-
content: result.text,
|
|
871
|
-
finishReason: result.finishReason === "stop" ? "stop" : "tool-calls",
|
|
872
|
-
model: `${providerId}/${modelId}`,
|
|
873
|
-
usage: usageInfo
|
|
874
|
-
};
|
|
875
|
-
}
|
|
876
|
-
function createInvokeConfig(model, apiKey, baseURL) {
|
|
877
|
-
return {
|
|
878
|
-
model,
|
|
879
|
-
apiKey,
|
|
880
|
-
baseURL
|
|
881
|
-
};
|
|
882
|
-
}
|
|
883
314
|
// src/env/llm/hooks.ts
|
|
884
315
|
init_global_hook_manager();
|
|
885
316
|
|
|
@@ -1016,7 +447,7 @@ class LLMHooks {
|
|
|
1016
447
|
// src/env/llm/llm.ts
|
|
1017
448
|
init_logger();
|
|
1018
449
|
init_decorator();
|
|
1019
|
-
var
|
|
450
|
+
var logger = createLogger("llm");
|
|
1020
451
|
|
|
1021
452
|
class LLMComponent extends BaseComponent {
|
|
1022
453
|
name = "llm";
|
|
@@ -1132,24 +563,24 @@ class LLMComponent extends BaseComponent {
|
|
|
1132
563
|
}
|
|
1133
564
|
registerConfigWatcher(configComponent) {
|
|
1134
565
|
if (typeof configComponent.watch !== "function") {
|
|
1135
|
-
|
|
566
|
+
logger.debug("ConfigComponent does not support watch, hot reload disabled");
|
|
1136
567
|
return;
|
|
1137
568
|
}
|
|
1138
569
|
this.configWatcher = configComponent.watch("llm.*", (event) => {
|
|
1139
570
|
this.onConfigChange(event);
|
|
1140
571
|
});
|
|
1141
|
-
|
|
572
|
+
logger.debug("Config hot reload watcher registered for llm.*");
|
|
1142
573
|
}
|
|
1143
574
|
onConfigChange(event) {
|
|
1144
|
-
|
|
575
|
+
logger.info(`LLM config changed: ${event.key}`);
|
|
1145
576
|
if (event.key === "llm.providers" || event.key.startsWith("llm.providers.")) {
|
|
1146
|
-
|
|
577
|
+
logger.info(`LLM provider config changed, will use new config on next call`);
|
|
1147
578
|
} else if (event.key === "llm.defaultModel") {
|
|
1148
|
-
|
|
579
|
+
logger.info(`LLM default model changed to: ${event.newValue}`);
|
|
1149
580
|
} else if (event.key === "llm.defaultProvider") {
|
|
1150
|
-
|
|
581
|
+
logger.info(`LLM default provider changed to: ${event.newValue}`);
|
|
1151
582
|
} else {
|
|
1152
|
-
|
|
583
|
+
logger.debug(`LLM config updated: ${event.key}`);
|
|
1153
584
|
}
|
|
1154
585
|
}
|
|
1155
586
|
async onStart() {
|
|
@@ -1334,4 +765,4 @@ class LLMComponent extends BaseComponent {
|
|
|
1334
765
|
__legacyDecorateClassTS([
|
|
1335
766
|
TracedAs("llm.component.invoke", { recordParams: true, recordResult: true, log: true })
|
|
1336
767
|
], LLMComponent.prototype, "invoke", null);
|
|
1337
|
-
export { ProviderCapabilitiesSchema, ModelLimitsSchema, ProviderConfigSchema, LLMDefaultConfigSchema, LLMConfigSchema, ProviderManager, LLMTransform,
|
|
768
|
+
export { ProviderCapabilitiesSchema, ModelLimitsSchema, ProviderConfigSchema, LLMDefaultConfigSchema, LLMConfigSchema, ProviderManager, LLMTransform, LLMHooks, LLMComponent };
|
|
@@ -2434,16 +2434,23 @@ var init_engine = __esm(() => {
|
|
|
2434
2434
|
if (!sessionState) {
|
|
2435
2435
|
throw new Error(`Session not found: ${sessionId}`);
|
|
2436
2436
|
}
|
|
2437
|
-
const timeoutMs = timeout ??
|
|
2437
|
+
const timeoutMs = timeout ?? 1200000;
|
|
2438
|
+
let timer;
|
|
2438
2439
|
const timeoutPromise = new Promise((_, reject) => {
|
|
2439
|
-
setTimeout(() => {
|
|
2440
|
+
timer = setTimeout(() => {
|
|
2440
2441
|
reject(new Error(`Workflow execution timeout: ${timeoutMs}ms`));
|
|
2441
2442
|
}, timeoutMs);
|
|
2442
2443
|
});
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2444
|
+
try {
|
|
2445
|
+
return await Promise.race([
|
|
2446
|
+
sessionState.completedPromise,
|
|
2447
|
+
timeoutPromise
|
|
2448
|
+
]);
|
|
2449
|
+
} finally {
|
|
2450
|
+
if (timer) {
|
|
2451
|
+
clearTimeout(timer);
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2447
2454
|
}
|
|
2448
2455
|
async getSessionMetadata(sessionId) {
|
|
2449
2456
|
if (!this.sessionComponent) {
|
|
@@ -206,12 +206,12 @@ var DelegateToolParameters = z.object({
|
|
|
206
206
|
prompt: z.string().describe("The task for the agent to perform"),
|
|
207
207
|
subagent_type: z.string().describe("The type of specialized agent to use for this task").default("general"),
|
|
208
208
|
background: z.boolean().describe("Whether to run the task in background. If true, returns immediately and notifies when complete (default: false)").default(false),
|
|
209
|
-
timeout: z.number().describe("Task timeout in milliseconds. Default:
|
|
209
|
+
timeout: z.number().describe("Task timeout in milliseconds. Default: 1800000 (30 minutes). If set, task will be terminated after timeout.").optional(),
|
|
210
210
|
cleanup: z.enum(["delete", "keep"]).describe("Whether to delete sub session after completion. 'delete' removes the session, 'keep' retains it (default: keep)").default("keep").optional(),
|
|
211
211
|
task_id: z.number().describe("Optional task ID to associate with this delegate task, for tracking in operation records").optional(),
|
|
212
212
|
reason: z.string().describe("Brief reason for calling this tool (max 30 chars, e.g., 'Delegate refactor task')").optional()
|
|
213
213
|
});
|
|
214
|
-
var DEFAULT_TIMEOUT =
|
|
214
|
+
var DEFAULT_TIMEOUT = 1800000;
|
|
215
215
|
var PROGRESS_INTERVAL = 120000;
|
|
216
216
|
|
|
217
217
|
class BackgroundTaskManager {
|