@ai-setting/roy-agent-core 1.6.42 → 1.7.1
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 -4
- package/dist/env/index.js +7 -8
- package/dist/env/llm/index.js +17 -6
- package/dist/env/session/index.js +4 -4
- package/dist/env/session/storage/index.js +3 -3
- package/dist/env/workflow/index.js +6 -6
- package/dist/env/workflow/tools/index.js +5 -5
- package/dist/index.js +13 -14
- package/dist/shared/@ai-setting/{roy-agent-core-c1stgqnw.js → roy-agent-core-2xb3f3ye.js} +143 -23
- package/dist/shared/@ai-setting/{roy-agent-core-fzyn4fxx.js → roy-agent-core-3dwcwgn5.js} +163 -30
- package/dist/shared/@ai-setting/{roy-agent-core-dbwvfe0m.js → roy-agent-core-qatz3q31.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-7wv7gh6t.js → roy-agent-core-w93wrd77.js} +627 -46
- package/dist/shared/@ai-setting/{roy-agent-core-afb03wwp.js → roy-agent-core-wgp3q62x.js} +7 -4
- package/dist/shared/@ai-setting/{roy-agent-core-9yf6we04.js → roy-agent-core-xmacq00w.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-f2nync7q.js → roy-agent-core-xvag5pd0.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-1k9j90s7.js → roy-agent-core-zcx8nf4a.js} +1 -1
- package/package.json +5 -2
- package/dist/shared/@ai-setting/roy-agent-core-12gac5zn.js +0 -44
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
withTimeout
|
|
3
|
-
} from "./roy-agent-core-c1stgqnw.js";
|
|
4
1
|
import {
|
|
5
2
|
truncateOutputInline
|
|
6
3
|
} from "./roy-agent-core-h28gcd77.js";
|
|
@@ -22,10 +19,11 @@ import {
|
|
|
22
19
|
} from "./roy-agent-core-ctdhjv68.js";
|
|
23
20
|
import {
|
|
24
21
|
SessionMessageConverter
|
|
25
|
-
} from "./roy-agent-core-
|
|
22
|
+
} from "./roy-agent-core-zcx8nf4a.js";
|
|
26
23
|
import {
|
|
27
|
-
safeParseToolArgs
|
|
28
|
-
|
|
24
|
+
safeParseToolArgs,
|
|
25
|
+
withTimeout
|
|
26
|
+
} from "./roy-agent-core-2xb3f3ye.js";
|
|
29
27
|
import {
|
|
30
28
|
envKeyToConfigKey
|
|
31
29
|
} from "./roy-agent-core-7hh0brvs.js";
|
|
@@ -810,20 +808,6 @@ class AgentComponent extends BaseComponent {
|
|
|
810
808
|
const workspaceDir = this.env?.getComponent?.("config")?.get?.("workspace_dir") || process.cwd();
|
|
811
809
|
resolvedSystemPrompt = resolvedSystemPrompt.replaceAll("{{workspace_dir}}", String(workspaceDir));
|
|
812
810
|
}
|
|
813
|
-
if (resolvedSystemPrompt.includes("{{memory}}")) {
|
|
814
|
-
try {
|
|
815
|
-
const memoryComponent = this.env?.getComponent?.("memory");
|
|
816
|
-
if (memoryComponent && typeof memoryComponent.recallMemory === "function") {
|
|
817
|
-
const memoryContent = await memoryComponent.recallMemory();
|
|
818
|
-
resolvedSystemPrompt = resolvedSystemPrompt.replaceAll("{{memory}}", memoryContent || "(No memory)");
|
|
819
|
-
} else {
|
|
820
|
-
resolvedSystemPrompt = resolvedSystemPrompt.replaceAll("{{memory}}", "(Memory component not available)");
|
|
821
|
-
}
|
|
822
|
-
} catch (err) {
|
|
823
|
-
logger2.warn(`[AgentComponent._run] Failed to replace {{memory}}: ${err}`);
|
|
824
|
-
resolvedSystemPrompt = resolvedSystemPrompt.replaceAll("{{memory}}", "(Failed to load memory)");
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
811
|
}
|
|
828
812
|
const effectiveContext = {
|
|
829
813
|
...context,
|
|
@@ -846,6 +830,12 @@ class AgentComponent extends BaseComponent {
|
|
|
846
830
|
resolvedSystemPrompt = eventSourceSystemPrompt;
|
|
847
831
|
}
|
|
848
832
|
}
|
|
833
|
+
const { snapshot: cacheSnapshot, systemPromptOverride } = await this._loadOrCreateCacheSnapshot(effectiveContext.sessionId, effectiveContext, resolvedSystemPrompt, agent.config.systemPrompt);
|
|
834
|
+
if (systemPromptOverride !== undefined) {
|
|
835
|
+
resolvedSystemPrompt = systemPromptOverride;
|
|
836
|
+
} else if (resolvedSystemPrompt) {
|
|
837
|
+
resolvedSystemPrompt = await this._fallbackReplaceMemory(resolvedSystemPrompt);
|
|
838
|
+
}
|
|
849
839
|
const hookCtx = {
|
|
850
840
|
agent,
|
|
851
841
|
iteration: 0,
|
|
@@ -853,7 +843,8 @@ class AgentComponent extends BaseComponent {
|
|
|
853
843
|
messages: [],
|
|
854
844
|
tools: this.getAvailableTools(agent, effectiveContext),
|
|
855
845
|
systemPrompt: resolvedSystemPrompt ?? agent.config.systemPrompt ?? "You are a helpful assistant.",
|
|
856
|
-
context: effectiveContext
|
|
846
|
+
context: effectiveContext,
|
|
847
|
+
...cacheSnapshot ? { _cacheSnapshot: cacheSnapshot } : {}
|
|
857
848
|
};
|
|
858
849
|
let historyMessageCount = 0;
|
|
859
850
|
if (effectiveContext.messages && effectiveContext.messages.length > 0) {
|
|
@@ -1034,7 +1025,7 @@ class AgentComponent extends BaseComponent {
|
|
|
1034
1025
|
}
|
|
1035
1026
|
const func = toolCall.function;
|
|
1036
1027
|
const tcName = func?.name || toolCall.name || "unknown";
|
|
1037
|
-
const tcArgs =
|
|
1028
|
+
const tcArgs = this.parseToolCallArguments(toolCall);
|
|
1038
1029
|
hookCtx.currentToolCall = {
|
|
1039
1030
|
id: toolCall.id,
|
|
1040
1031
|
name: tcName,
|
|
@@ -1328,7 +1319,80 @@ class AgentComponent extends BaseComponent {
|
|
|
1328
1319
|
}
|
|
1329
1320
|
logger2.info(`All agents aborted (${this.abortControllers.size} runs)`);
|
|
1330
1321
|
}
|
|
1322
|
+
async _loadOrCreateCacheSnapshot(sessionId, effectiveContext, initialSystemPrompt, agentConfigSystemPrompt) {
|
|
1323
|
+
if (!sessionId) {
|
|
1324
|
+
return { snapshot: null, systemPromptOverride: undefined };
|
|
1325
|
+
}
|
|
1326
|
+
try {
|
|
1327
|
+
const sessionComponent = this.env?.getComponent?.("session");
|
|
1328
|
+
if (!sessionComponent) {
|
|
1329
|
+
return { snapshot: null, systemPromptOverride: undefined };
|
|
1330
|
+
}
|
|
1331
|
+
const session = await sessionComponent.get(sessionId);
|
|
1332
|
+
const existingSnapshot = session?.metadata?.cacheSnapshot;
|
|
1333
|
+
if (existingSnapshot && typeof existingSnapshot === "object" && existingSnapshot.systemPrompt) {
|
|
1334
|
+
logger2.debug(`[AgentComponent._run] Reusing cacheSnapshot for session ${sessionId} (snapshotAt=${existingSnapshot.snapshotAt})`);
|
|
1335
|
+
return {
|
|
1336
|
+
snapshot: existingSnapshot,
|
|
1337
|
+
systemPromptOverride: existingSnapshot.systemPrompt
|
|
1338
|
+
};
|
|
1339
|
+
}
|
|
1340
|
+
let resolvedSystemPrompt = initialSystemPrompt;
|
|
1341
|
+
if (resolvedSystemPrompt && resolvedSystemPrompt.includes("{{memory}}")) {
|
|
1342
|
+
try {
|
|
1343
|
+
const memoryComponent = this.env?.getComponent?.("memory");
|
|
1344
|
+
if (memoryComponent && typeof memoryComponent.recallMemory === "function") {
|
|
1345
|
+
const memoryContent = await memoryComponent.recallMemory();
|
|
1346
|
+
resolvedSystemPrompt = resolvedSystemPrompt.replaceAll("{{memory}}", memoryContent || "(No memory)");
|
|
1347
|
+
} else {
|
|
1348
|
+
resolvedSystemPrompt = resolvedSystemPrompt.replaceAll("{{memory}}", "(Memory component not available)");
|
|
1349
|
+
}
|
|
1350
|
+
} catch (err) {
|
|
1351
|
+
logger2.warn(`[AgentComponent._run] Failed to replace {{memory}}: ${err}`);
|
|
1352
|
+
resolvedSystemPrompt = resolvedSystemPrompt.replaceAll("{{memory}}", "(Failed to load memory)");
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
const snapshot = {
|
|
1356
|
+
systemPrompt: resolvedSystemPrompt ?? agentConfigSystemPrompt ?? "",
|
|
1357
|
+
additionInfo: effectiveContext.additionInfo || "",
|
|
1358
|
+
snapshotAt: Date.now()
|
|
1359
|
+
};
|
|
1360
|
+
try {
|
|
1361
|
+
await sessionComponent.update(sessionId, {
|
|
1362
|
+
metadata: {
|
|
1363
|
+
...session?.metadata ?? {},
|
|
1364
|
+
cacheSnapshot: snapshot
|
|
1365
|
+
}
|
|
1366
|
+
});
|
|
1367
|
+
logger2.debug(`[AgentComponent._run] Created cacheSnapshot for session ${sessionId} (snapshotAt=${snapshot.snapshotAt})`);
|
|
1368
|
+
} catch (updateErr) {
|
|
1369
|
+
logger2.warn(`[AgentComponent._run] Failed to persist cacheSnapshot for session ${sessionId}: ${updateErr}`);
|
|
1370
|
+
}
|
|
1371
|
+
return { snapshot, systemPromptOverride: resolvedSystemPrompt };
|
|
1372
|
+
} catch (err) {
|
|
1373
|
+
logger2.warn(`[AgentComponent._run] Failed to load cacheSnapshot: ${err}`);
|
|
1374
|
+
return { snapshot: null, systemPromptOverride: undefined };
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
async _fallbackReplaceMemory(systemPrompt) {
|
|
1378
|
+
if (!systemPrompt.includes("{{memory}}"))
|
|
1379
|
+
return systemPrompt;
|
|
1380
|
+
try {
|
|
1381
|
+
const memoryComponent = this.env?.getComponent?.("memory");
|
|
1382
|
+
if (memoryComponent && typeof memoryComponent.recallMemory === "function") {
|
|
1383
|
+
const memoryContent = await memoryComponent.recallMemory();
|
|
1384
|
+
return systemPrompt.replaceAll("{{memory}}", memoryContent || "(No memory)");
|
|
1385
|
+
}
|
|
1386
|
+
return systemPrompt.replaceAll("{{memory}}", "(Memory component not available)");
|
|
1387
|
+
} catch (err) {
|
|
1388
|
+
logger2.warn(`[AgentComponent._run] Failed to replace {{memory}}: ${err}`);
|
|
1389
|
+
return systemPrompt.replaceAll("{{memory}}", "(Failed to load memory)");
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1331
1392
|
async _buildAdditionInfo(ctx) {
|
|
1393
|
+
if (ctx._cacheSnapshot) {
|
|
1394
|
+
return ctx._cacheSnapshot.additionInfo;
|
|
1395
|
+
}
|
|
1332
1396
|
let additionInfo = ctx.context?.additionInfo || "";
|
|
1333
1397
|
return additionInfo;
|
|
1334
1398
|
}
|
|
@@ -1336,21 +1400,28 @@ class AgentComponent extends BaseComponent {
|
|
|
1336
1400
|
const messages = [];
|
|
1337
1401
|
const additionInfo = await this._buildAdditionInfo(ctx);
|
|
1338
1402
|
if (ctx.systemPrompt) {
|
|
1403
|
+
messages.push(...buildMultiBlockSystem({
|
|
1404
|
+
characterSheet: ctx.systemPrompt,
|
|
1405
|
+
toolDefinitions: undefined,
|
|
1406
|
+
memoryBlock: undefined,
|
|
1407
|
+
dynamicContext: additionInfo
|
|
1408
|
+
}));
|
|
1409
|
+
} else if (additionInfo) {
|
|
1339
1410
|
messages.push({
|
|
1340
1411
|
role: "system",
|
|
1341
|
-
content:
|
|
1342
|
-
|
|
1343
|
-
${additionInfo}`
|
|
1344
|
-
});
|
|
1345
|
-
} else {
|
|
1346
|
-
messages.push({
|
|
1347
|
-
role: "system",
|
|
1348
|
-
content: `${additionInfo}`
|
|
1412
|
+
content: additionInfo
|
|
1349
1413
|
});
|
|
1350
1414
|
}
|
|
1351
1415
|
messages.push(...ctx.messages);
|
|
1352
1416
|
return messages;
|
|
1353
1417
|
}
|
|
1418
|
+
_formatToolDefinitions(tools) {
|
|
1419
|
+
if (!tools || tools.length === 0)
|
|
1420
|
+
return "";
|
|
1421
|
+
const sorted = [...tools].sort((a, b) => a.name.localeCompare(b.name));
|
|
1422
|
+
return sorted.map((t) => `${t.name}: ${t.description || ""}`).join(`
|
|
1423
|
+
`);
|
|
1424
|
+
}
|
|
1354
1425
|
async invokeLLM(ctx) {
|
|
1355
1426
|
logger2.debug("[invokeLLM] Starting LLM invocation");
|
|
1356
1427
|
if (this.llmComponent?.invoke) {
|
|
@@ -1401,6 +1472,10 @@ ${additionInfo}`
|
|
|
1401
1472
|
finishReason: "stop"
|
|
1402
1473
|
};
|
|
1403
1474
|
}
|
|
1475
|
+
parseToolCallArguments(toolCall) {
|
|
1476
|
+
const rawArgs = toolCall.function?.arguments ?? toolCall.arguments;
|
|
1477
|
+
return safeParseToolArgs(rawArgs);
|
|
1478
|
+
}
|
|
1404
1479
|
async executeTool(ctx) {
|
|
1405
1480
|
const toolCall = ctx.currentToolCall;
|
|
1406
1481
|
const toolName = toolCall.function?.name || toolCall.name || "unknown";
|
|
@@ -1689,12 +1764,37 @@ __legacyDecorateClassTS([
|
|
|
1689
1764
|
__legacyDecorateClassTS([
|
|
1690
1765
|
TracedAs("agent.component.run", { recordParams: true, recordResult: true, log: true })
|
|
1691
1766
|
], AgentComponent.prototype, "_run", null);
|
|
1767
|
+
__legacyDecorateClassTS([
|
|
1768
|
+
TracedAs("agent.component.loadOrCreateCacheSnapshot", { recordParams: true, recordResult: true, log: true })
|
|
1769
|
+
], AgentComponent.prototype, "_loadOrCreateCacheSnapshot", null);
|
|
1692
1770
|
__legacyDecorateClassTS([
|
|
1693
1771
|
TracedAs("agent.component.buildAdditionInfo", { recordParams: true, recordResult: true, log: true })
|
|
1694
1772
|
], AgentComponent.prototype, "_buildAdditionInfo", null);
|
|
1695
1773
|
__legacyDecorateClassTS([
|
|
1696
1774
|
TracedAs("agent.component.invokeLLM", { recordParams: true, recordResult: true, log: true })
|
|
1697
1775
|
], AgentComponent.prototype, "invokeLLM", null);
|
|
1776
|
+
__legacyDecorateClassTS([
|
|
1777
|
+
TracedAs("agent.tool_call.parse", {
|
|
1778
|
+
recordParams: true,
|
|
1779
|
+
recordResult: true,
|
|
1780
|
+
log: true,
|
|
1781
|
+
paramFilter: (args) => {
|
|
1782
|
+
const toolCall = args[0];
|
|
1783
|
+
const hasFunctionArgs = toolCall?.function?.arguments !== undefined;
|
|
1784
|
+
const rawArgs = hasFunctionArgs ? toolCall.function.arguments : toolCall?.arguments;
|
|
1785
|
+
return {
|
|
1786
|
+
tool_call_id: toolCall?.id,
|
|
1787
|
+
tool_name: toolCall?.function?.name ?? toolCall?.name ?? "unknown",
|
|
1788
|
+
argument_source: hasFunctionArgs ? "function.arguments" : "arguments",
|
|
1789
|
+
argument_type: rawArgs === null ? "null" : Array.isArray(rawArgs) ? "array" : typeof rawArgs
|
|
1790
|
+
};
|
|
1791
|
+
},
|
|
1792
|
+
resultFilter: (result) => ({
|
|
1793
|
+
argument_type: "object",
|
|
1794
|
+
keys: Object.keys(result).sort()
|
|
1795
|
+
})
|
|
1796
|
+
})
|
|
1797
|
+
], AgentComponent.prototype, "parseToolCallArguments", null);
|
|
1698
1798
|
__legacyDecorateClassTS([
|
|
1699
1799
|
TracedAs("agent.component.executeTool", { recordParams: true, recordResult: true, log: true })
|
|
1700
1800
|
], AgentComponent.prototype, "executeTool", null);
|
|
@@ -1704,6 +1804,39 @@ __legacyDecorateClassTS([
|
|
|
1704
1804
|
__legacyDecorateClassTS([
|
|
1705
1805
|
TracedAs("agent.component.getConversationHistory", { recordParams: true, recordResult: true, log: true })
|
|
1706
1806
|
], AgentComponent.prototype, "getConversationHistory", null);
|
|
1807
|
+
function buildMultiBlockSystem(ctx) {
|
|
1808
|
+
const ANTHROPIC_CACHE_CONTROL = {
|
|
1809
|
+
anthropic: { cacheControl: { type: "ephemeral" } }
|
|
1810
|
+
};
|
|
1811
|
+
const messages = [];
|
|
1812
|
+
if (ctx.characterSheet) {
|
|
1813
|
+
messages.push({
|
|
1814
|
+
role: "system",
|
|
1815
|
+
content: ctx.characterSheet,
|
|
1816
|
+
providerOptions: ANTHROPIC_CACHE_CONTROL
|
|
1817
|
+
});
|
|
1818
|
+
}
|
|
1819
|
+
if (ctx.toolDefinitions) {
|
|
1820
|
+
messages.push({
|
|
1821
|
+
role: "system",
|
|
1822
|
+
content: ctx.toolDefinitions,
|
|
1823
|
+
providerOptions: ANTHROPIC_CACHE_CONTROL
|
|
1824
|
+
});
|
|
1825
|
+
}
|
|
1826
|
+
if (ctx.memoryBlock) {
|
|
1827
|
+
messages.push({
|
|
1828
|
+
role: "system",
|
|
1829
|
+
content: ctx.memoryBlock
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
if (ctx.dynamicContext) {
|
|
1833
|
+
messages.push({
|
|
1834
|
+
role: "system",
|
|
1835
|
+
content: ctx.dynamicContext
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
return messages;
|
|
1839
|
+
}
|
|
1707
1840
|
// src/env/agent/summary-agent.ts
|
|
1708
1841
|
class SummaryAgent {
|
|
1709
1842
|
llmComponent;
|