@dudousxd/nestjs-agent 1.3.0 → 1.5.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.
@@ -143,8 +143,15 @@ var AgentRunSteps = class {
143
143
  if (deps.registry.spec(input.toolName)?.kind === "action" && input.toolType !== "action") {
144
144
  throw new Error(`tool "${input.toolName}" is an action tool but was dispatched without approval: the dispatching process resolved its kind against a registry that does not have it`);
145
145
  }
146
+ const sinkRunId = input.sinkRunId;
147
+ let opened;
148
+ const ui = (0, import_nestjs_agent_core.createUiCollector)(input.toolCallId, sinkRunId === void 0 ? void 0 : async (event) => {
149
+ opened ??= Promise.resolve(deps.sink.open(sinkRunId)).then((writer) => input.childSink === true ? childSinkWriter(writer) : writer);
150
+ await (await opened).write((0, import_nestjs_agent_core.encodeStreamEvent)(event));
151
+ });
146
152
  const ctx = {
147
- ...input.ctx
153
+ ...input.ctx,
154
+ emitUi: ui.emit
148
155
  };
149
156
  const localClassify = deps.toolTransientRetry !== false ? deps.toolTransientRetry?.classify : void 0;
150
157
  const transientRetry = input.transientRetry === false ? false : {
@@ -154,11 +161,12 @@ var AgentRunSteps = class {
154
161
  classify: localClassify
155
162
  } : {}
156
163
  };
157
- return (0, import_nestjs_agent_core.traceToolExecution)(input.ctx.runId, {
164
+ const output = await (0, import_nestjs_agent_core.traceToolExecution)(input.ctx.runId, {
158
165
  toolCallId: input.toolCallId,
159
166
  toolName: input.toolName,
160
167
  toolType: input.toolType
161
168
  }, () => (0, import_nestjs_agent_core.invokeWithTransientRetry)(() => {
169
+ ui.restart();
162
170
  const invocation = deps.registry.invoke(input.toolName, input.input, ctx, deps.rolesPolicy);
163
171
  return input.timeoutMs !== void 0 ? (0, import_nestjs_agent_core.withToolTimeout)(invocation, input.timeoutMs, input.toolName) : invocation;
164
172
  }, transientRetry, {
@@ -171,6 +179,7 @@ var AgentRunSteps = class {
171
179
  });
172
180
  }, "onRetry")
173
181
  }));
182
+ return input.collectUi === true ? (0, import_nestjs_agent_core.wrapToolStepOutput)(output, ui.components()) : output;
174
183
  }
175
184
  };
176
185
  _ts_decorate([
@@ -414,7 +423,10 @@ var AgentRunWorkflow = class _AgentRunWorkflow {
414
423
  dispatchTool: /* @__PURE__ */ __name((call, envelope) => ctx.step(AgentRunSteps.prototype.tool, {
415
424
  ...envelope,
416
425
  toolCallId: call.id,
417
- toolType: call.kind === "action" ? "action" : "read"
426
+ toolType: call.kind === "action" ? "action" : "read",
427
+ // Where `ctx.emitUi` streams from the worker — the llm step's routing.
428
+ sinkRunId,
429
+ childSink: input.sinkRunId !== void 0
418
430
  }), "dispatchTool")
419
431
  } : {}
420
432
  };
@@ -1252,6 +1264,7 @@ var AgentService = class {
1252
1264
  */
1253
1265
  async answer(actor, toolCallId2, answers2 = {}) {
1254
1266
  await this.assertOwnsToolCall(actor, toolCallId2);
1267
+ await this.assertAnswersFit(toolCallId2, answers2);
1255
1268
  const reply = {
1256
1269
  answers: answers2,
1257
1270
  answeredByRef: actor.id
@@ -1520,6 +1533,27 @@ var AgentService = class {
1520
1533
  }
1521
1534
  }
1522
1535
  /**
1536
+ * Check a reply against the questions it answers before it is signalled: every submitted value a
1537
+ * question's own rules accept (a typed input's type, bounds and pattern; a pick from the offered
1538
+ * options), and every `required` question answered — by the reply, or by the defaults an omitted
1539
+ * question falls back to. A reply the loop would have to drop parts of is refused here instead, so
1540
+ * the person who typed it hears why. Needs the store to hand back the call's recorded questions;
1541
+ * without that seam the loop's own filtering is the only check.
1542
+ */
1543
+ async assertAnswersFit(toolCallId2, answers2) {
1544
+ if (this.store.toolCallInput === void 0) {
1545
+ return;
1546
+ }
1547
+ const questions = (0, import_nestjs_agent_core8.readElicitationQuestions)(await this.store.toolCallInput(toolCallId2));
1548
+ for (const question of questions) {
1549
+ const submitted = Object.hasOwn(answers2, question.id) ? answers2[question.id] : void 0;
1550
+ const problem = (0, import_nestjs_agent_core8.validateElicitationAnswer)(question, submitted ?? question.defaults ?? []);
1551
+ if (problem !== null) {
1552
+ throw new import_common6.BadRequestException(`answers["${question.id}"] ${problem}`);
1553
+ }
1554
+ }
1555
+ }
1556
+ /**
1523
1557
  * Authorization seam for approve/reject. The call's recorded approver decides who may settle it:
1524
1558
  * the requester (the default, and every call recorded before approvers existed) is the thread's
1525
1559
  * own actor — the ownership check this always was; any other approver goes through the policy's
@@ -3124,6 +3158,9 @@ var AiToolDiscoveryService = class _AiToolDiscoveryService {
3124
3158
  } : {},
3125
3159
  ...meta.presentation !== void 0 ? {
3126
3160
  presentation: meta.presentation
3161
+ } : {},
3162
+ ...meta.terminal === true ? {
3163
+ terminal: true
3127
3164
  } : {}
3128
3165
  }, {
3129
3166
  execute: /* @__PURE__ */ __name((input, ctx) => instance.execute(input, ctx), "execute"),