@dudousxd/nestjs-agent-core 0.5.0 → 0.7.0
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/index.cjs +107 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +141 -2
- package/dist/index.d.ts +141 -2
- package/dist/index.js +101 -10
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -23,8 +23,10 @@ var index_exports = {};
|
|
|
23
23
|
__export(index_exports, {
|
|
24
24
|
AGENT_ACTOR_DIRECTORY: () => AGENT_ACTOR_DIRECTORY,
|
|
25
25
|
AGENT_ACTOR_RESOLVER: () => AGENT_ACTOR_RESOLVER,
|
|
26
|
+
AGENT_APPROVAL_PORT: () => AGENT_APPROVAL_PORT,
|
|
26
27
|
AGENT_ATTACHMENT_STAGING: () => AGENT_ATTACHMENT_STAGING,
|
|
27
28
|
AGENT_DEPS_FACTORY: () => AGENT_DEPS_FACTORY,
|
|
29
|
+
AGENT_DIAGNOSTIC_EVENTS: () => AGENT_DIAGNOSTIC_EVENTS,
|
|
28
30
|
AGENT_DURABLE_RUNNER: () => AGENT_DURABLE_RUNNER,
|
|
29
31
|
AGENT_EMBEDDING_PROVIDER: () => AGENT_EMBEDDING_PROVIDER,
|
|
30
32
|
AGENT_GOVERNANCE_QUERIES: () => AGENT_GOVERNANCE_QUERIES,
|
|
@@ -38,6 +40,7 @@ __export(index_exports, {
|
|
|
38
40
|
AGENT_ROLES_POLICY: () => AGENT_ROLES_POLICY,
|
|
39
41
|
AGENT_RUNNER: () => AGENT_RUNNER,
|
|
40
42
|
AGENT_SINK: () => AGENT_SINK,
|
|
43
|
+
AGENT_SPAN_EVENTS: () => AGENT_SPAN_EVENTS,
|
|
41
44
|
AGENT_STORE: () => AGENT_STORE,
|
|
42
45
|
AGENT_TOOL_REGISTRY: () => AGENT_TOOL_REGISTRY,
|
|
43
46
|
AgentRegistry: () => AgentRegistry,
|
|
@@ -48,6 +51,7 @@ __export(index_exports, {
|
|
|
48
51
|
ToolInputInvalidError: () => ToolInputInvalidError,
|
|
49
52
|
ToolNotFoundError: () => ToolNotFoundError,
|
|
50
53
|
ToolRegistry: () => ToolRegistry,
|
|
54
|
+
agentDiagnosticKey: () => agentDiagnosticKey,
|
|
51
55
|
bucketByActor: () => bucketByActor,
|
|
52
56
|
bucketByModel: () => bucketByModel,
|
|
53
57
|
bucketByThread: () => bucketByThread,
|
|
@@ -67,6 +71,8 @@ __export(index_exports, {
|
|
|
67
71
|
publishAgentToolCall: () => publishAgentToolCall,
|
|
68
72
|
runAgentLoop: () => runAgentLoop,
|
|
69
73
|
seedModelPrices: () => seedModelPrices,
|
|
74
|
+
traceLlmTurn: () => traceLlmTurn,
|
|
75
|
+
traceToolExecution: () => traceToolExecution,
|
|
70
76
|
withToolTimeout: () => withToolTimeout
|
|
71
77
|
});
|
|
72
78
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -91,6 +97,7 @@ var AGENT_DEPS_FACTORY = Symbol.for("@dudousxd/nestjs-agent:deps-factory");
|
|
|
91
97
|
var AGENT_PROMPT_CONTRIBUTORS = Symbol.for("@dudousxd/nestjs-agent:prompt-contributors");
|
|
92
98
|
var AGENT_ACTOR_DIRECTORY = Symbol.for("@dudousxd/nestjs-agent:actor-directory");
|
|
93
99
|
var AGENT_ATTACHMENT_STAGING = Symbol.for("@dudousxd/nestjs-agent:attachment-staging");
|
|
100
|
+
var AGENT_APPROVAL_PORT = Symbol.for("@dudousxd/nestjs-agent:approval-port");
|
|
94
101
|
|
|
95
102
|
// src/spi/token-stream-sink.ts
|
|
96
103
|
var AgentStreamError = class extends Error {
|
|
@@ -394,6 +401,10 @@ var DefaultRolesPolicy = class {
|
|
|
394
401
|
}
|
|
395
402
|
};
|
|
396
403
|
|
|
404
|
+
// src/agent-loop.ts
|
|
405
|
+
var import_node_crypto = require("crypto");
|
|
406
|
+
var import_nestjs_diagnostics2 = require("@dudousxd/nestjs-diagnostics");
|
|
407
|
+
|
|
397
408
|
// src/diagnostics.ts
|
|
398
409
|
var import_nestjs_diagnostics = require("@dudousxd/nestjs-diagnostics");
|
|
399
410
|
function publishAgentRunStarted(payload) {
|
|
@@ -428,6 +439,26 @@ function publishAgentRetrieved(payload) {
|
|
|
428
439
|
(0, import_nestjs_diagnostics.emit)("agent", "retrieved", payload);
|
|
429
440
|
}
|
|
430
441
|
__name(publishAgentRetrieved, "publishAgentRetrieved");
|
|
442
|
+
var AGENT_SPAN_EVENTS = [
|
|
443
|
+
"llm.turn",
|
|
444
|
+
"tool.execution",
|
|
445
|
+
"retrieval",
|
|
446
|
+
"follow-ups"
|
|
447
|
+
];
|
|
448
|
+
var AGENT_DIAGNOSTIC_EVENTS = [
|
|
449
|
+
"run.started",
|
|
450
|
+
"message",
|
|
451
|
+
"tool-call",
|
|
452
|
+
"quota.exceeded",
|
|
453
|
+
"run.finished",
|
|
454
|
+
"run.failed",
|
|
455
|
+
"delegated",
|
|
456
|
+
"retrieved"
|
|
457
|
+
];
|
|
458
|
+
function agentDiagnosticKey(event) {
|
|
459
|
+
return `agent:${event}`;
|
|
460
|
+
}
|
|
461
|
+
__name(agentDiagnosticKey, "agentDiagnosticKey");
|
|
431
462
|
|
|
432
463
|
// src/agent-loop.ts
|
|
433
464
|
function resolveCostUsd(usage, reportedCostUsd, price) {
|
|
@@ -555,6 +586,39 @@ async function generateFollowUps(model, messages, count) {
|
|
|
555
586
|
};
|
|
556
587
|
}
|
|
557
588
|
__name(generateFollowUps, "generateFollowUps");
|
|
589
|
+
async function spanned(event, runId, payload, run, summarize) {
|
|
590
|
+
let value;
|
|
591
|
+
await (0, import_nestjs_diagnostics2.trace)("agent", event, async () => {
|
|
592
|
+
value = await run();
|
|
593
|
+
return summarize(value);
|
|
594
|
+
}, payload, {
|
|
595
|
+
traceId: runId
|
|
596
|
+
});
|
|
597
|
+
return value;
|
|
598
|
+
}
|
|
599
|
+
__name(spanned, "spanned");
|
|
600
|
+
function traceLlmTurn(runId, step, run) {
|
|
601
|
+
return spanned("llm.turn", runId, {
|
|
602
|
+
runId,
|
|
603
|
+
step
|
|
604
|
+
}, run, (turn) => ({
|
|
605
|
+
...turn.modelId !== void 0 ? {
|
|
606
|
+
modelId: turn.modelId
|
|
607
|
+
} : {},
|
|
608
|
+
inputTokens: turn.usage.inputTokens,
|
|
609
|
+
outputTokens: turn.usage.outputTokens,
|
|
610
|
+
textLength: turn.text.length,
|
|
611
|
+
toolCalls: turn.toolCalls.length
|
|
612
|
+
}));
|
|
613
|
+
}
|
|
614
|
+
__name(traceLlmTurn, "traceLlmTurn");
|
|
615
|
+
function traceToolExecution(runId, call, run) {
|
|
616
|
+
return spanned("tool.execution", runId, {
|
|
617
|
+
runId,
|
|
618
|
+
...call
|
|
619
|
+
}, run, () => ({}));
|
|
620
|
+
}
|
|
621
|
+
__name(traceToolExecution, "traceToolExecution");
|
|
558
622
|
async function runAgentLoop(deps, input, hooks) {
|
|
559
623
|
const maxSteps = deps.maxSteps ?? 8;
|
|
560
624
|
let system = await resolveSystemPrompt(deps, input);
|
|
@@ -625,21 +689,30 @@ async function runAgentLoop(deps, input, hooks) {
|
|
|
625
689
|
});
|
|
626
690
|
const startedAt = await hooks.step("run:started-at", () => Promise.resolve(Date.now()));
|
|
627
691
|
await hooks.step("persist:run:start", async () => {
|
|
692
|
+
const promptHash = (0, import_node_crypto.createHash)("sha256").update(system).digest("hex");
|
|
628
693
|
await deps.store.recordRunStart?.({
|
|
629
694
|
runId: hooks.runId,
|
|
630
695
|
threadId: input.threadId,
|
|
631
696
|
actorRef: input.actor.id,
|
|
632
697
|
...input.agentName !== void 0 ? {
|
|
633
698
|
agentName: input.agentName
|
|
634
|
-
} : {}
|
|
699
|
+
} : {},
|
|
700
|
+
promptHash
|
|
635
701
|
});
|
|
636
702
|
});
|
|
637
703
|
let injectedPassages;
|
|
638
704
|
if (deps.retriever !== void 0) {
|
|
639
705
|
const retriever = deps.retriever;
|
|
640
|
-
const
|
|
641
|
-
|
|
642
|
-
|
|
706
|
+
const topK = deps.retrievalTopK ?? 5;
|
|
707
|
+
const passages = await hooks.step("retrieve", () => spanned("retrieval", hooks.runId, {
|
|
708
|
+
runId: hooks.runId,
|
|
709
|
+
queryLength: input.userText.length,
|
|
710
|
+
topK
|
|
711
|
+
}, () => retriever.retrieve(input.userText, {
|
|
712
|
+
topK
|
|
713
|
+
}), (retrieved) => ({
|
|
714
|
+
count: retrieved.length
|
|
715
|
+
})));
|
|
643
716
|
if (passages.length > 0) {
|
|
644
717
|
injectedPassages = passages;
|
|
645
718
|
system = `${system}
|
|
@@ -679,12 +752,12 @@ ${buildContextBlock(passages)}`;
|
|
|
679
752
|
});
|
|
680
753
|
} else {
|
|
681
754
|
const tools = await deps.registry.definitionsFor(input.actor, deps.rolesPolicy, deps.toolAllowList);
|
|
682
|
-
turn = await hooks.step(`llm:${i}`, () => deps.model.runTurn({
|
|
755
|
+
turn = await hooks.step(`llm:${i}`, () => traceLlmTurn(hooks.runId, i, () => deps.model.runTurn({
|
|
683
756
|
system,
|
|
684
757
|
messages: modelMessages,
|
|
685
758
|
tools,
|
|
686
759
|
sink: writer
|
|
687
|
-
}));
|
|
760
|
+
})));
|
|
688
761
|
}
|
|
689
762
|
const resolvedModelId = turn.modelId ?? deps.modelId ?? "unknown";
|
|
690
763
|
const costUsd = resolveCostUsd(turn.usage, turn.costUsd, priceByModel.get(resolvedModelId));
|
|
@@ -721,13 +794,24 @@ ${buildContextBlock(passages)}`;
|
|
|
721
794
|
let followUps;
|
|
722
795
|
if (isFinalTurn && deps.followUpsCount !== void 0 && deps.followUpsCount > 0) {
|
|
723
796
|
const count = deps.followUpsCount;
|
|
724
|
-
const generated = await hooks.step(`followups:${i}`, () =>
|
|
797
|
+
const generated = await hooks.step(`followups:${i}`, () => spanned("follow-ups", hooks.runId, {
|
|
798
|
+
runId: hooks.runId,
|
|
799
|
+
step: i,
|
|
800
|
+
count
|
|
801
|
+
}, () => generateFollowUps(deps.model, [
|
|
725
802
|
...modelMessages,
|
|
726
803
|
{
|
|
727
804
|
role: "assistant",
|
|
728
805
|
content: turn.text
|
|
729
806
|
}
|
|
730
|
-
], count))
|
|
807
|
+
], count), (result) => ({
|
|
808
|
+
followUps: result.followUps.length,
|
|
809
|
+
inputTokens: result.usage.inputTokens,
|
|
810
|
+
outputTokens: result.usage.outputTokens,
|
|
811
|
+
...result.modelId !== void 0 ? {
|
|
812
|
+
modelId: result.modelId
|
|
813
|
+
} : {}
|
|
814
|
+
})));
|
|
731
815
|
if (generated.followUps.length > 0) {
|
|
732
816
|
followUps = generated.followUps;
|
|
733
817
|
}
|
|
@@ -860,6 +944,7 @@ ${buildContextBlock(passages)}`;
|
|
|
860
944
|
});
|
|
861
945
|
continue;
|
|
862
946
|
}
|
|
947
|
+
let deciderRef = input.actor.id;
|
|
863
948
|
if (toolType === "action") {
|
|
864
949
|
await hooks.step(`persist:toolcall:${call.id}`, () => deps.store.recordToolCall({
|
|
865
950
|
toolCallId: call.id,
|
|
@@ -870,10 +955,12 @@ ${buildContextBlock(passages)}`;
|
|
|
870
955
|
status: "pending_approval"
|
|
871
956
|
}));
|
|
872
957
|
const decision = await hooks.awaitApproval(call, ctx);
|
|
958
|
+
deciderRef = decision.executedByRef ?? input.actor.id;
|
|
873
959
|
if (!decision.approved) {
|
|
874
960
|
await hooks.step(`persist:toolreject:${call.id}`, () => deps.store.updateToolCall({
|
|
875
961
|
toolCallId: call.id,
|
|
876
962
|
status: "rejected",
|
|
963
|
+
executedByRef: deciderRef,
|
|
877
964
|
...decision.reason !== void 0 ? {
|
|
878
965
|
error: decision.reason
|
|
879
966
|
} : {}
|
|
@@ -931,7 +1018,11 @@ ${buildContextBlock(passages)}`;
|
|
|
931
1018
|
};
|
|
932
1019
|
output = await hooks.dispatchTool(call, envelope);
|
|
933
1020
|
} else {
|
|
934
|
-
const invocation = hooks.step(`tool:${call.id}`, () =>
|
|
1021
|
+
const invocation = hooks.step(`tool:${call.id}`, () => traceToolExecution(hooks.runId, {
|
|
1022
|
+
toolCallId: call.id,
|
|
1023
|
+
toolName: call.name,
|
|
1024
|
+
toolType
|
|
1025
|
+
}, () => deps.registry.invoke(call.name, call.input, ctx, deps.rolesPolicy)));
|
|
935
1026
|
output = deps.toolTimeoutMs !== void 0 ? await withToolTimeout(invocation, deps.toolTimeoutMs, call.name) : await invocation;
|
|
936
1027
|
}
|
|
937
1028
|
const executionMs = Date.now() - startedAt2;
|
|
@@ -941,7 +1032,7 @@ ${buildContextBlock(passages)}`;
|
|
|
941
1032
|
output,
|
|
942
1033
|
executionMs,
|
|
943
1034
|
...toolType === "action" ? {
|
|
944
|
-
executedByRef:
|
|
1035
|
+
executedByRef: deciderRef
|
|
945
1036
|
} : {}
|
|
946
1037
|
}));
|
|
947
1038
|
results.push({
|
|
@@ -1032,8 +1123,10 @@ __name(runAgentLoop, "runAgentLoop");
|
|
|
1032
1123
|
0 && (module.exports = {
|
|
1033
1124
|
AGENT_ACTOR_DIRECTORY,
|
|
1034
1125
|
AGENT_ACTOR_RESOLVER,
|
|
1126
|
+
AGENT_APPROVAL_PORT,
|
|
1035
1127
|
AGENT_ATTACHMENT_STAGING,
|
|
1036
1128
|
AGENT_DEPS_FACTORY,
|
|
1129
|
+
AGENT_DIAGNOSTIC_EVENTS,
|
|
1037
1130
|
AGENT_DURABLE_RUNNER,
|
|
1038
1131
|
AGENT_EMBEDDING_PROVIDER,
|
|
1039
1132
|
AGENT_GOVERNANCE_QUERIES,
|
|
@@ -1047,6 +1140,7 @@ __name(runAgentLoop, "runAgentLoop");
|
|
|
1047
1140
|
AGENT_ROLES_POLICY,
|
|
1048
1141
|
AGENT_RUNNER,
|
|
1049
1142
|
AGENT_SINK,
|
|
1143
|
+
AGENT_SPAN_EVENTS,
|
|
1050
1144
|
AGENT_STORE,
|
|
1051
1145
|
AGENT_TOOL_REGISTRY,
|
|
1052
1146
|
AgentRegistry,
|
|
@@ -1057,6 +1151,7 @@ __name(runAgentLoop, "runAgentLoop");
|
|
|
1057
1151
|
ToolInputInvalidError,
|
|
1058
1152
|
ToolNotFoundError,
|
|
1059
1153
|
ToolRegistry,
|
|
1154
|
+
agentDiagnosticKey,
|
|
1060
1155
|
bucketByActor,
|
|
1061
1156
|
bucketByModel,
|
|
1062
1157
|
bucketByThread,
|
|
@@ -1076,6 +1171,8 @@ __name(runAgentLoop, "runAgentLoop");
|
|
|
1076
1171
|
publishAgentToolCall,
|
|
1077
1172
|
runAgentLoop,
|
|
1078
1173
|
seedModelPrices,
|
|
1174
|
+
traceLlmTurn,
|
|
1175
|
+
traceToolExecution,
|
|
1079
1176
|
withToolTimeout
|
|
1080
1177
|
});
|
|
1081
1178
|
//# sourceMappingURL=index.cjs.map
|