@dudousxd/nestjs-agent-core 0.17.0 → 0.18.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) {
@@ -3363,12 +3462,13 @@ ${block}`;
3363
3462
  writer
3364
3463
  }) : void 0;
3365
3464
  const buffer = gateMode === "whole" ? createFrameBuffer() : void 0;
3366
- const result = stampToolKinds(await traceLlmTurn(hooks.runId, i, () => deps.model.runTurn({
3465
+ const frames = observeTurnFrames(incremental?.writer ?? buffer?.writer ?? writer);
3466
+ const result = stampToolKinds(withTurnFrames(await traceLlmTurn(hooks.runId, i, () => deps.model.runTurn({
3367
3467
  system: prompt.system,
3368
3468
  messages: prompt.messages,
3369
3469
  tools,
3370
- sink: incremental?.writer ?? buffer?.writer ?? writer
3371
- })), deps);
3470
+ sink: frames.writer
3471
+ })), frames.summary()), deps);
3372
3472
  if (incremental !== void 0) {
3373
3473
  await incremental.settled();
3374
3474
  const refusal = incremental.rejection();
@@ -3556,6 +3656,15 @@ ${block}`;
3556
3656
  ...input.agentName !== void 0 ? {
3557
3657
  agentName: input.agentName
3558
3658
  } : {},
3659
+ ...turn.reasoning !== void 0 ? {
3660
+ reasoning: turn.reasoning
3661
+ } : {},
3662
+ ...turn.reasoningMs !== void 0 ? {
3663
+ reasoningMs: turn.reasoningMs
3664
+ } : {},
3665
+ ...turn.ui !== void 0 && turn.ui.length > 0 ? {
3666
+ ui: turn.ui
3667
+ } : {},
3559
3668
  ...messageCalls.length > 0 ? {
3560
3669
  toolCalls: messageCalls
3561
3670
  } : {},
@@ -3613,7 +3722,10 @@ ${block}`;
3613
3722
  await writer.write(encodeStreamEvent({
3614
3723
  kind: "step-finish",
3615
3724
  usage: turn.usage,
3616
- costUsd
3725
+ costUsd,
3726
+ ...turn.reasoningMs !== void 0 ? {
3727
+ reasoningMs: turn.reasoningMs
3728
+ } : {}
3617
3729
  }));
3618
3730
  });
3619
3731
  break;
@@ -3666,7 +3778,10 @@ ${block}`;
3666
3778
  await writer.write(encodeStreamEvent({
3667
3779
  kind: "step-finish",
3668
3780
  usage: turn.usage,
3669
- costUsd
3781
+ costUsd,
3782
+ ...turn.reasoningMs !== void 0 ? {
3783
+ reasoningMs: turn.reasoningMs
3784
+ } : {}
3670
3785
  }));
3671
3786
  });
3672
3787
  }
@@ -3820,6 +3935,7 @@ export {
3820
3935
  memoryWriteVerdict,
3821
3936
  normalizeDelegation,
3822
3937
  normalizeElicitationReply,
3938
+ observeTurnFrames,
3823
3939
  offerMemories,
3824
3940
  offerSkills,
3825
3941
  publishAgentDelegated,
@@ -3869,6 +3985,7 @@ export {
3869
3985
  withMemoryTool,
3870
3986
  withSkillTool,
3871
3987
  withToolTimeout,
3988
+ withTurnFrames,
3872
3989
  writeMemory
3873
3990
  };
3874
3991
  //# sourceMappingURL=index.js.map