@dudousxd/nestjs-agent-core 0.6.0 → 0.8.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 CHANGED
@@ -40,6 +40,7 @@ __export(index_exports, {
40
40
  AGENT_ROLES_POLICY: () => AGENT_ROLES_POLICY,
41
41
  AGENT_RUNNER: () => AGENT_RUNNER,
42
42
  AGENT_SINK: () => AGENT_SINK,
43
+ AGENT_SPAN_EVENTS: () => AGENT_SPAN_EVENTS,
43
44
  AGENT_STORE: () => AGENT_STORE,
44
45
  AGENT_TOOL_REGISTRY: () => AGENT_TOOL_REGISTRY,
45
46
  AgentRegistry: () => AgentRegistry,
@@ -70,6 +71,8 @@ __export(index_exports, {
70
71
  publishAgentToolCall: () => publishAgentToolCall,
71
72
  runAgentLoop: () => runAgentLoop,
72
73
  seedModelPrices: () => seedModelPrices,
74
+ traceLlmTurn: () => traceLlmTurn,
75
+ traceToolExecution: () => traceToolExecution,
73
76
  withToolTimeout: () => withToolTimeout
74
77
  });
75
78
  module.exports = __toCommonJS(index_exports);
@@ -400,6 +403,7 @@ var DefaultRolesPolicy = class {
400
403
 
401
404
  // src/agent-loop.ts
402
405
  var import_node_crypto = require("crypto");
406
+ var import_nestjs_diagnostics2 = require("@dudousxd/nestjs-diagnostics");
403
407
 
404
408
  // src/diagnostics.ts
405
409
  var import_nestjs_diagnostics = require("@dudousxd/nestjs-diagnostics");
@@ -435,6 +439,12 @@ function publishAgentRetrieved(payload) {
435
439
  (0, import_nestjs_diagnostics.emit)("agent", "retrieved", payload);
436
440
  }
437
441
  __name(publishAgentRetrieved, "publishAgentRetrieved");
442
+ var AGENT_SPAN_EVENTS = [
443
+ "llm.turn",
444
+ "tool.execution",
445
+ "retrieval",
446
+ "follow-ups"
447
+ ];
438
448
  var AGENT_DIAGNOSTIC_EVENTS = [
439
449
  "run.started",
440
450
  "message",
@@ -576,6 +586,39 @@ async function generateFollowUps(model, messages, count) {
576
586
  };
577
587
  }
578
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");
579
622
  async function runAgentLoop(deps, input, hooks) {
580
623
  const maxSteps = deps.maxSteps ?? 8;
581
624
  let system = await resolveSystemPrompt(deps, input);
@@ -660,9 +703,16 @@ async function runAgentLoop(deps, input, hooks) {
660
703
  let injectedPassages;
661
704
  if (deps.retriever !== void 0) {
662
705
  const retriever = deps.retriever;
663
- const passages = await hooks.step("retrieve", () => retriever.retrieve(input.userText, {
664
- topK: deps.retrievalTopK ?? 5
665
- }));
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
+ })));
666
716
  if (passages.length > 0) {
667
717
  injectedPassages = passages;
668
718
  system = `${system}
@@ -702,12 +752,12 @@ ${buildContextBlock(passages)}`;
702
752
  });
703
753
  } else {
704
754
  const tools = await deps.registry.definitionsFor(input.actor, deps.rolesPolicy, deps.toolAllowList);
705
- turn = await hooks.step(`llm:${i}`, () => deps.model.runTurn({
755
+ turn = await hooks.step(`llm:${i}`, () => traceLlmTurn(hooks.runId, i, () => deps.model.runTurn({
706
756
  system,
707
757
  messages: modelMessages,
708
758
  tools,
709
759
  sink: writer
710
- }));
760
+ })));
711
761
  }
712
762
  const resolvedModelId = turn.modelId ?? deps.modelId ?? "unknown";
713
763
  const costUsd = resolveCostUsd(turn.usage, turn.costUsd, priceByModel.get(resolvedModelId));
@@ -744,13 +794,24 @@ ${buildContextBlock(passages)}`;
744
794
  let followUps;
745
795
  if (isFinalTurn && deps.followUpsCount !== void 0 && deps.followUpsCount > 0) {
746
796
  const count = deps.followUpsCount;
747
- const generated = await hooks.step(`followups:${i}`, () => generateFollowUps(deps.model, [
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, [
748
802
  ...modelMessages,
749
803
  {
750
804
  role: "assistant",
751
805
  content: turn.text
752
806
  }
753
- ], 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
+ })));
754
815
  if (generated.followUps.length > 0) {
755
816
  followUps = generated.followUps;
756
817
  }
@@ -957,7 +1018,11 @@ ${buildContextBlock(passages)}`;
957
1018
  };
958
1019
  output = await hooks.dispatchTool(call, envelope);
959
1020
  } else {
960
- const invocation = hooks.step(`tool:${call.id}`, () => deps.registry.invoke(call.name, call.input, ctx, deps.rolesPolicy));
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)));
961
1026
  output = deps.toolTimeoutMs !== void 0 ? await withToolTimeout(invocation, deps.toolTimeoutMs, call.name) : await invocation;
962
1027
  }
963
1028
  const executionMs = Date.now() - startedAt2;
@@ -1075,6 +1140,7 @@ __name(runAgentLoop, "runAgentLoop");
1075
1140
  AGENT_ROLES_POLICY,
1076
1141
  AGENT_RUNNER,
1077
1142
  AGENT_SINK,
1143
+ AGENT_SPAN_EVENTS,
1078
1144
  AGENT_STORE,
1079
1145
  AGENT_TOOL_REGISTRY,
1080
1146
  AgentRegistry,
@@ -1105,6 +1171,8 @@ __name(runAgentLoop, "runAgentLoop");
1105
1171
  publishAgentToolCall,
1106
1172
  runAgentLoop,
1107
1173
  seedModelPrices,
1174
+ traceLlmTurn,
1175
+ traceToolExecution,
1108
1176
  withToolTimeout
1109
1177
  });
1110
1178
  //# sourceMappingURL=index.cjs.map