@dudousxd/nestjs-agent 1.35.1 → 1.36.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.
@@ -227,11 +227,13 @@ var AgentRunSteps = class {
227
227
  });
228
228
  const buffer = input.bufferOutput === true ? (0, import_nestjs_agent_core3.createFrameBuffer)() : void 0;
229
229
  let writer;
230
+ let firstText;
230
231
  if (buffer !== void 0) {
231
232
  writer = buffer.writer;
232
233
  } else {
233
234
  const opened = await deps.sink.open(input.sinkRunId);
234
- writer = input.childSink ? childSinkWriter(opened) : opened;
235
+ firstText = (0, import_nestjs_agent_core3.observeFirstText)(input.childSink ? childSinkWriter(opened) : opened);
236
+ writer = firstText.writer;
235
237
  }
236
238
  const announced = (0, import_nestjs_agent_core3.announcedToolCalls)(writer);
237
239
  const previews = (0, import_nestjs_agent_core3.previewToolInputs)(announced.writer, (0, import_nestjs_agent_core3.registryInputPreviews)(deps.registry, tools, {
@@ -325,9 +327,16 @@ var AgentRunSteps = class {
325
327
  ...stamped,
326
328
  previews: shown
327
329
  } : stamped;
328
- return buffer === void 0 ? previewed : {
330
+ const firstTextAt = firstText?.firstTextAt();
331
+ if (buffer !== void 0) {
332
+ return {
333
+ ...previewed,
334
+ bufferedFrames: buffer.frames()
335
+ };
336
+ }
337
+ return firstTextAt === void 0 ? previewed : {
329
338
  ...previewed,
330
- bufferedFrames: buffer.frames()
339
+ firstTextAt
331
340
  };
332
341
  }
333
342
  /**
@@ -370,6 +379,7 @@ var AgentRunSteps = class {
370
379
  ...input.allowedTools
371
380
  ]);
372
381
  let output;
382
+ const toolStartedAt = Date.now();
373
383
  try {
374
384
  output = await (0, import_nestjs_agent_core3.traceToolExecution)(input.ctx.runId, {
375
385
  toolCallId: input.toolCallId,
@@ -397,8 +407,9 @@ var AgentRunSteps = class {
397
407
  }
398
408
  throw error;
399
409
  }
410
+ const executionMs = Date.now() - toolStartedAt;
400
411
  output = await (0, import_nestjs_agent_core3.withChannelDelivery)(deps, input.ctx, output, ui.components());
401
- return input.collectUi === true ? (0, import_nestjs_agent_core3.wrapToolStepOutput)(output, ui.components(), ui.text()) : output;
412
+ return input.collectUi === true ? (0, import_nestjs_agent_core3.wrapToolStepOutput)(output, ui.components(), ui.text(), executionMs) : output;
402
413
  }
403
414
  };
404
415
  _ts_decorate([
@@ -798,21 +809,29 @@ var AgentRunWorkflow = class _AgentRunWorkflow {
798
809
  });
799
810
  throw error;
800
811
  }
812
+ const durationMs = (0, import_nestjs_agent_core4.runDurationMs)(error);
813
+ const timed = durationMs !== void 0 ? {
814
+ durationMs
815
+ } : {};
801
816
  if ((0, import_nestjs_agent_core4.isReplayIntegrityError)(error)) {
802
817
  const detail = error.message;
803
818
  const code2 = (0, import_nestjs_agent_core4.agentFailureCode)(error);
804
819
  this.logger.error(`agent run ${ctx.runId} failed (${code2}): ${detail}`);
805
820
  (0, import_nestjs_agent_core4.publishAgentRunFailed)({
806
821
  runId: ctx.runId,
822
+ threadId: input.threadId,
807
823
  code: code2,
808
- message: detail
824
+ message: detail,
825
+ ...timed,
826
+ context: (0, import_nestjs_agent_core4.agentRunContext)(input)
809
827
  });
810
828
  await (0, import_nestjs_agent_core4.settleDeadRun)(this.store, {
811
829
  runId: ctx.runId,
812
830
  threadId: input.threadId,
813
831
  failure: {
814
832
  code: code2,
815
- message: detail
833
+ message: detail,
834
+ ...timed
816
835
  }
817
836
  });
818
837
  await (await hooks.openSink()).fail((0, import_nestjs_agent_core4.streamFailure)(error));
@@ -823,13 +842,17 @@ var AgentRunWorkflow = class _AgentRunWorkflow {
823
842
  this.logger.error(`agent run ${ctx.runId} failed (${code}): ${message}`);
824
843
  (0, import_nestjs_agent_core4.publishAgentRunFailed)({
825
844
  runId: ctx.runId,
845
+ threadId: input.threadId,
826
846
  code,
827
- message
847
+ message,
848
+ ...timed,
849
+ context: (0, import_nestjs_agent_core4.agentRunContext)(input)
828
850
  });
829
851
  await stepOf(ctx)("persist:run:fail", async () => {
830
852
  await this.store.recordRunEnd?.({
831
853
  runId: ctx.runId,
832
854
  status: "failed",
855
+ ...timed,
833
856
  errorCode: code,
834
857
  errorMessage: message
835
858
  });
@@ -1004,6 +1027,57 @@ function deliveryOf(input) {
1004
1027
  };
1005
1028
  }
1006
1029
  __name(deliveryOf, "deliveryOf");
1030
+ function runContextOf(input) {
1031
+ if (typeof input !== "object" || input === null) {
1032
+ return void 0;
1033
+ }
1034
+ const { actor, agentName, parentRunId } = input;
1035
+ if (typeof actor?.id !== "string") {
1036
+ return void 0;
1037
+ }
1038
+ return {
1039
+ actorId: actor.id,
1040
+ ...typeof agentName === "string" ? {
1041
+ agentName
1042
+ } : {},
1043
+ ...typeof actor.tenantRef === "string" ? {
1044
+ tenantRef: actor.tenantRef
1045
+ } : {},
1046
+ ...typeof parentRunId === "string" ? {
1047
+ parentRunId
1048
+ } : {}
1049
+ };
1050
+ }
1051
+ __name(runContextOf, "runContextOf");
1052
+ function cancelledAfterMs(detail) {
1053
+ const createdAt = detail?.run.createdAt;
1054
+ if (createdAt === void 0) {
1055
+ return {};
1056
+ }
1057
+ const started = new Date(createdAt).getTime();
1058
+ return Number.isFinite(started) ? {
1059
+ durationMs: Math.max(0, Date.now() - started)
1060
+ } : {};
1061
+ }
1062
+ __name(cancelledAfterMs, "cancelledAfterMs");
1063
+ function publishCancelled(runId, detail) {
1064
+ const input = detail?.run.input;
1065
+ const threadId = threadOfRun(input);
1066
+ const context = runContextOf(input);
1067
+ const timed = cancelledAfterMs(detail);
1068
+ (0, import_nestjs_agent_core6.publishAgentRunCancelled)({
1069
+ runId,
1070
+ ...threadId !== void 0 ? {
1071
+ threadId
1072
+ } : {},
1073
+ ...timed,
1074
+ ...context !== void 0 ? {
1075
+ context
1076
+ } : {}
1077
+ });
1078
+ return timed;
1079
+ }
1080
+ __name(publishCancelled, "publishCancelled");
1007
1081
  var DurableAgentRunner = class _DurableAgentRunner {
1008
1082
  static {
1009
1083
  __name(this, "DurableAgentRunner");
@@ -1117,9 +1191,11 @@ var DurableAgentRunner = class _DurableAgentRunner {
1117
1191
  kind: "cancelled"
1118
1192
  }));
1119
1193
  await writer.end();
1194
+ const timed = publishCancelled(runId, detail);
1120
1195
  await this.store.recordRunEnd?.({
1121
1196
  runId,
1122
- status: "cancelled"
1197
+ status: "cancelled",
1198
+ ...timed
1123
1199
  });
1124
1200
  }
1125
1201
  /**
@@ -1167,9 +1243,11 @@ var DurableAgentRunner = class _DurableAgentRunner {
1167
1243
  kind: "cancelled"
1168
1244
  }));
1169
1245
  await writer.end();
1246
+ const timed = publishCancelled(childId, child);
1170
1247
  await this.store.recordRunEnd?.({
1171
1248
  runId: childId,
1172
- status: "cancelled"
1249
+ status: "cancelled",
1250
+ ...timed
1173
1251
  });
1174
1252
  }
1175
1253
  /**
@@ -6980,8 +7058,12 @@ var InlineAgentRunner = class _InlineAgentRunner {
6980
7058
  ...deps,
6981
7059
  day
6982
7060
  }, input, hooks).then(() => (0, import_nestjs_agent_core38.releaseThreadRun)(this.store, input.threadId, runId)).catch(async (error) => {
7061
+ const durationMs = (0, import_nestjs_agent_core38.runDurationMs)(error);
7062
+ const timed = durationMs !== void 0 ? {
7063
+ durationMs
7064
+ } : {};
6983
7065
  if (error instanceof import_nestjs_agent_core38.RunCancelledError) {
6984
- await this.settleCancelled(runId, input, deps);
7066
+ await this.settleCancelled(runId, input, deps, durationMs);
6985
7067
  return;
6986
7068
  }
6987
7069
  const message = error instanceof Error ? error.message : String(error);
@@ -6989,12 +7071,16 @@ var InlineAgentRunner = class _InlineAgentRunner {
6989
7071
  this.logger.error(`agent run ${runId} failed (${code}): ${message}`);
6990
7072
  (0, import_nestjs_agent_core38.publishAgentRunFailed)({
6991
7073
  runId,
7074
+ threadId: input.threadId,
6992
7075
  code,
6993
- message
7076
+ message,
7077
+ ...timed,
7078
+ context: (0, import_nestjs_agent_core38.agentRunContext)(input)
6994
7079
  });
6995
7080
  await this.store.recordRunEnd?.({
6996
7081
  runId,
6997
7082
  status: "failed",
7083
+ ...timed,
6998
7084
  errorCode: code,
6999
7085
  errorMessage: message
7000
7086
  });
@@ -7066,8 +7152,17 @@ var InlineAgentRunner = class _InlineAgentRunner {
7066
7152
  * the stream ENDED after a `cancelled` frame rather than failed, so a client that retries failed
7067
7153
  * streams does not retry this one.
7068
7154
  */
7069
- async settleCancelled(runId, input, deps) {
7155
+ async settleCancelled(runId, input, deps, durationMs) {
7070
7156
  this.logger.log(`agent run ${runId} cancelled`);
7157
+ const timed = durationMs !== void 0 ? {
7158
+ durationMs
7159
+ } : {};
7160
+ (0, import_nestjs_agent_core38.publishAgentRunCancelled)({
7161
+ runId,
7162
+ threadId: input.threadId,
7163
+ ...timed,
7164
+ context: (0, import_nestjs_agent_core38.agentRunContext)(input)
7165
+ });
7071
7166
  const writer = await deps.sink.open(runId);
7072
7167
  await this.settleQueue(writer, input, runId, "cancelled");
7073
7168
  await (0, import_nestjs_agent_core38.releaseThreadRun)(this.store, input.threadId, runId);
@@ -7077,7 +7172,8 @@ var InlineAgentRunner = class _InlineAgentRunner {
7077
7172
  await writer.end();
7078
7173
  await this.store.recordRunEnd?.({
7079
7174
  runId,
7080
- status: "cancelled"
7175
+ status: "cancelled",
7176
+ ...timed
7081
7177
  });
7082
7178
  }
7083
7179
  /**
@@ -7263,10 +7359,7 @@ var InlineAgentRunner = class _InlineAgentRunner {
7263
7359
  }
7264
7360
  }), "startAgent")
7265
7361
  };
7266
- void (0, import_nestjs_agent_core38.runAgentLoop)({
7267
- ...deps,
7268
- day
7269
- }, {
7362
+ const childInput = {
7270
7363
  threadId: subThread.id,
7271
7364
  actor,
7272
7365
  userText: task,
@@ -7276,13 +7369,29 @@ var InlineAgentRunner = class _InlineAgentRunner {
7276
7369
  delegationPath: path,
7277
7370
  parentRunId,
7278
7371
  deliverTo
7279
- }, hooks).catch(async (error) => {
7372
+ };
7373
+ void (0, import_nestjs_agent_core38.runAgentLoop)({
7374
+ ...deps,
7375
+ day
7376
+ }, childInput, hooks).catch(async (error) => {
7280
7377
  const cancelled = error instanceof import_nestjs_agent_core38.RunCancelledError;
7281
7378
  const message = error instanceof Error ? error.message : String(error);
7282
- if (!cancelled) {
7379
+ const durationMs = (0, import_nestjs_agent_core38.runDurationMs)(error);
7380
+ const timed = durationMs !== void 0 ? {
7381
+ durationMs
7382
+ } : {};
7383
+ const settled = {
7384
+ runId,
7385
+ threadId: subThread.id,
7386
+ ...timed,
7387
+ context: (0, import_nestjs_agent_core38.agentRunContext)(childInput)
7388
+ };
7389
+ if (cancelled) {
7390
+ (0, import_nestjs_agent_core38.publishAgentRunCancelled)(settled);
7391
+ } else {
7283
7392
  this.logger.error(`detached agent run ${runId} failed: ${message}`);
7284
7393
  (0, import_nestjs_agent_core38.publishAgentRunFailed)({
7285
- runId,
7394
+ ...settled,
7286
7395
  code: (0, import_nestjs_agent_core38.agentFailureCode)(error),
7287
7396
  message
7288
7397
  });
@@ -7290,6 +7399,7 @@ var InlineAgentRunner = class _InlineAgentRunner {
7290
7399
  await this.store.recordRunEnd?.({
7291
7400
  runId,
7292
7401
  status: cancelled ? "cancelled" : "failed",
7402
+ ...timed,
7293
7403
  ...cancelled ? {} : {
7294
7404
  errorCode: (0, import_nestjs_agent_core38.agentFailureCode)(error),
7295
7405
  errorMessage: message