@dudousxd/nestjs-agent-core 0.17.0 → 0.19.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.js CHANGED
@@ -58,6 +58,105 @@ function decodeStreamEvent(line) {
58
58
  }
59
59
  __name(decodeStreamEvent, "decodeStreamEvent");
60
60
 
61
+ // src/turn-frames.ts
62
+ function observeTurnFrames(writer, now = Date.now) {
63
+ const decoder2 = new TextDecoder();
64
+ let pending = "";
65
+ let reasoning = "";
66
+ let reasoningMs = 0;
67
+ let burstStartedAt;
68
+ const ui = /* @__PURE__ */ new Map();
69
+ function closeBurst() {
70
+ if (burstStartedAt !== void 0) {
71
+ reasoningMs += Math.max(0, now() - burstStartedAt);
72
+ burstStartedAt = void 0;
73
+ }
74
+ }
75
+ __name(closeBurst, "closeBurst");
76
+ function observeLine(line) {
77
+ if (line.trim().length === 0) {
78
+ return;
79
+ }
80
+ const event = decodeStreamEvent(line);
81
+ if (event === null) {
82
+ return;
83
+ }
84
+ if (event.kind === "reasoning") {
85
+ burstStartedAt ??= now();
86
+ reasoning += event.text;
87
+ return;
88
+ }
89
+ closeBurst();
90
+ if (event.kind === "ui") {
91
+ ui.set(event.id, {
92
+ id: event.id,
93
+ component: event.component,
94
+ props: event.props,
95
+ ...event.version !== void 0 ? {
96
+ version: event.version
97
+ } : {}
98
+ });
99
+ }
100
+ }
101
+ __name(observeLine, "observeLine");
102
+ function observe(chunk) {
103
+ pending += decoder2.decode(chunk, {
104
+ stream: true
105
+ });
106
+ let newline = pending.indexOf("\n");
107
+ while (newline !== -1) {
108
+ observeLine(pending.slice(0, newline));
109
+ pending = pending.slice(newline + 1);
110
+ newline = pending.indexOf("\n");
111
+ }
112
+ }
113
+ __name(observe, "observe");
114
+ return {
115
+ writer: {
116
+ write(chunk) {
117
+ observe(chunk);
118
+ return writer.write(chunk);
119
+ },
120
+ end: /* @__PURE__ */ __name(() => writer.end(), "end"),
121
+ fail: /* @__PURE__ */ __name((error) => writer.fail(error), "fail")
122
+ },
123
+ summary() {
124
+ if (pending.length > 0) {
125
+ observeLine(pending);
126
+ pending = "";
127
+ }
128
+ closeBurst();
129
+ return {
130
+ ...reasoning.length > 0 ? {
131
+ reasoning,
132
+ reasoningMs: Math.round(reasoningMs)
133
+ } : {},
134
+ ...ui.size > 0 ? {
135
+ ui: [
136
+ ...ui.values()
137
+ ]
138
+ } : {}
139
+ };
140
+ }
141
+ };
142
+ }
143
+ __name(observeTurnFrames, "observeTurnFrames");
144
+ function withTurnFrames(result, frames) {
145
+ return {
146
+ ...result,
147
+ ...result.reasoning === void 0 && frames.reasoning !== void 0 ? {
148
+ reasoning: frames.reasoning
149
+ } : {},
150
+ ...result.reasoningMs === void 0 && frames.reasoningMs !== void 0 ? {
151
+ reasoningMs: frames.reasoningMs
152
+ } : {},
153
+ ...result.ui === void 0 && frames.ui !== void 0 ? {
154
+ ui: frames.ui
155
+ } : {}
156
+ };
157
+ }
158
+ __name(withTurnFrames, "withTurnFrames");
159
+
61
160
  // src/spi/pricing-store.ts
62
161
  async function seedModelPrices(store, prices) {
63
162
  for (const price of prices) {
@@ -1778,6 +1877,19 @@ var ToolRegistry = class {
1778
1877
  * tool the allow-list has already excluded is a call whose answer nothing reads.
1779
1878
  */
1780
1879
  async definitionsFor(actor, policy, allowedTools) {
1880
+ return (await this.visibleSpecs(actor, policy, allowedTools)).map((spec) => ({
1881
+ name: spec.name,
1882
+ kind: spec.kind,
1883
+ description: spec.description,
1884
+ inputSchema: spec.inputSchema
1885
+ }));
1886
+ }
1887
+ /**
1888
+ * The specs {@link definitionsFor} offers the model, whole — the same four gates (allow-list,
1889
+ * enabled, role, `canUse`) in the same order. For a surface that lists what an actor can reach
1890
+ * (`GET <base>/tools`), which must never disagree with what the model is actually shown.
1891
+ */
1892
+ async visibleSpecs(actor, policy, allowedTools) {
1781
1893
  const pinnedNames = new Set(filterToolsByAllowList(this.allSpecs(), allowedTools).map((spec) => spec.name));
1782
1894
  const pinned = [
1783
1895
  ...this.entries.values()
@@ -1786,12 +1898,7 @@ var ToolRegistry = class {
1786
1898
  const allowedByRole = new Set((await filterToolsByRole(live.map((entry) => entry.spec), actor, policy)).map((spec) => spec.name));
1787
1899
  const roleScoped = live.filter((entry) => allowedByRole.has(entry.spec.name));
1788
1900
  const actorScoped = await filterToolsByCanUse(roleScoped, actor);
1789
- return actorScoped.map(({ spec }) => ({
1790
- name: spec.name,
1791
- kind: spec.kind,
1792
- description: spec.description,
1793
- inputSchema: spec.inputSchema
1794
- }));
1901
+ return actorScoped.map(({ spec }) => spec);
1795
1902
  }
1796
1903
  /**
1797
1904
  * Run a tool. Re-checks that the tool is enabled and that the role allows it (defense-in-depth —
@@ -3363,12 +3470,13 @@ ${block}`;
3363
3470
  writer
3364
3471
  }) : void 0;
3365
3472
  const buffer = gateMode === "whole" ? createFrameBuffer() : void 0;
3366
- const result = stampToolKinds(await traceLlmTurn(hooks.runId, i, () => deps.model.runTurn({
3473
+ const frames = observeTurnFrames(incremental?.writer ?? buffer?.writer ?? writer);
3474
+ const result = stampToolKinds(withTurnFrames(await traceLlmTurn(hooks.runId, i, () => deps.model.runTurn({
3367
3475
  system: prompt.system,
3368
3476
  messages: prompt.messages,
3369
3477
  tools,
3370
- sink: incremental?.writer ?? buffer?.writer ?? writer
3371
- })), deps);
3478
+ sink: frames.writer
3479
+ })), frames.summary()), deps);
3372
3480
  if (incremental !== void 0) {
3373
3481
  await incremental.settled();
3374
3482
  const refusal = incremental.rejection();
@@ -3556,6 +3664,15 @@ ${block}`;
3556
3664
  ...input.agentName !== void 0 ? {
3557
3665
  agentName: input.agentName
3558
3666
  } : {},
3667
+ ...turn.reasoning !== void 0 ? {
3668
+ reasoning: turn.reasoning
3669
+ } : {},
3670
+ ...turn.reasoningMs !== void 0 ? {
3671
+ reasoningMs: turn.reasoningMs
3672
+ } : {},
3673
+ ...turn.ui !== void 0 && turn.ui.length > 0 ? {
3674
+ ui: turn.ui
3675
+ } : {},
3559
3676
  ...messageCalls.length > 0 ? {
3560
3677
  toolCalls: messageCalls
3561
3678
  } : {},
@@ -3613,7 +3730,10 @@ ${block}`;
3613
3730
  await writer.write(encodeStreamEvent({
3614
3731
  kind: "step-finish",
3615
3732
  usage: turn.usage,
3616
- costUsd
3733
+ costUsd,
3734
+ ...turn.reasoningMs !== void 0 ? {
3735
+ reasoningMs: turn.reasoningMs
3736
+ } : {}
3617
3737
  }));
3618
3738
  });
3619
3739
  break;
@@ -3666,7 +3786,10 @@ ${block}`;
3666
3786
  await writer.write(encodeStreamEvent({
3667
3787
  kind: "step-finish",
3668
3788
  usage: turn.usage,
3669
- costUsd
3789
+ costUsd,
3790
+ ...turn.reasoningMs !== void 0 ? {
3791
+ reasoningMs: turn.reasoningMs
3792
+ } : {}
3670
3793
  }));
3671
3794
  });
3672
3795
  }
@@ -3820,6 +3943,7 @@ export {
3820
3943
  memoryWriteVerdict,
3821
3944
  normalizeDelegation,
3822
3945
  normalizeElicitationReply,
3946
+ observeTurnFrames,
3823
3947
  offerMemories,
3824
3948
  offerSkills,
3825
3949
  publishAgentDelegated,
@@ -3869,6 +3993,7 @@ export {
3869
3993
  withMemoryTool,
3870
3994
  withSkillTool,
3871
3995
  withToolTimeout,
3996
+ withTurnFrames,
3872
3997
  writeMemory
3873
3998
  };
3874
3999
  //# sourceMappingURL=index.js.map