@dudousxd/nestjs-agent 1.4.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.
@@ -83,6 +83,13 @@ interface DispatchedLlmInput extends LlmStepEnvelope {
83
83
  interface DispatchedToolInput extends ToolStepEnvelope {
84
84
  toolCallId: string;
85
85
  toolType: 'read' | 'action';
86
+ /**
87
+ * Where a component the tool pushes (`ctx.emitUi`) streams live — the same routing as
88
+ * {@link DispatchedLlmInput}. Absent on a task enqueued by a workflow that predates it: the
89
+ * components are then still collected and persisted, just not streamed.
90
+ */
91
+ sinkRunId?: string;
92
+ childSink?: boolean;
86
93
  }
87
94
  /**
88
95
  * The two long steps every turn dispatches as routed remote steps (`AgentRunSteps.llm` /
@@ -83,6 +83,13 @@ interface DispatchedLlmInput extends LlmStepEnvelope {
83
83
  interface DispatchedToolInput extends ToolStepEnvelope {
84
84
  toolCallId: string;
85
85
  toolType: 'read' | 'action';
86
+ /**
87
+ * Where a component the tool pushes (`ctx.emitUi`) streams live — the same routing as
88
+ * {@link DispatchedLlmInput}. Absent on a task enqueued by a workflow that predates it: the
89
+ * components are then still collected and persisted, just not streamed.
90
+ */
91
+ sinkRunId?: string;
92
+ childSink?: boolean;
86
93
  }
87
94
  /**
88
95
  * The two long steps every turn dispatches as routed remote steps (`AgentRunSteps.llm` /
@@ -24,7 +24,7 @@ function childSinkWriter(writer) {
24
24
  __name(childSinkWriter, "childSinkWriter");
25
25
 
26
26
  // src/durable/agent-run.steps.ts
27
- import { AGENT_DEPS_FACTORY, createFrameBuffer, invokeWithTransientRetry, observeTurnFrames, publishAgentToolRetry, stampToolKinds, traceLlmTurn, traceToolExecution, withAskTool, withMemoryTool, withSkillTool, withToolTimeout, withTurnFrames } from "@dudousxd/nestjs-agent-core";
27
+ import { AGENT_DEPS_FACTORY, createFrameBuffer, createUiCollector, encodeStreamEvent, invokeWithTransientRetry, observeTurnFrames, publishAgentToolRetry, stampToolKinds, traceLlmTurn, traceToolExecution, withAskTool, withMemoryTool, withSkillTool, withToolTimeout, withTurnFrames, wrapToolStepOutput } from "@dudousxd/nestjs-agent-core";
28
28
  import { Step } from "@dudousxd/nestjs-durable";
29
29
  import { Inject, Injectable } from "@nestjs/common";
30
30
  function _ts_decorate(decorators, target, key, desc) {
@@ -115,8 +115,15 @@ var AgentRunSteps = class {
115
115
  if (deps.registry.spec(input.toolName)?.kind === "action" && input.toolType !== "action") {
116
116
  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`);
117
117
  }
118
+ const sinkRunId = input.sinkRunId;
119
+ let opened;
120
+ const ui = createUiCollector(input.toolCallId, sinkRunId === void 0 ? void 0 : async (event) => {
121
+ opened ??= Promise.resolve(deps.sink.open(sinkRunId)).then((writer) => input.childSink === true ? childSinkWriter(writer) : writer);
122
+ await (await opened).write(encodeStreamEvent(event));
123
+ });
118
124
  const ctx = {
119
- ...input.ctx
125
+ ...input.ctx,
126
+ emitUi: ui.emit
120
127
  };
121
128
  const localClassify = deps.toolTransientRetry !== false ? deps.toolTransientRetry?.classify : void 0;
122
129
  const transientRetry = input.transientRetry === false ? false : {
@@ -126,11 +133,12 @@ var AgentRunSteps = class {
126
133
  classify: localClassify
127
134
  } : {}
128
135
  };
129
- return traceToolExecution(input.ctx.runId, {
136
+ const output = await traceToolExecution(input.ctx.runId, {
130
137
  toolCallId: input.toolCallId,
131
138
  toolName: input.toolName,
132
139
  toolType: input.toolType
133
140
  }, () => invokeWithTransientRetry(() => {
141
+ ui.restart();
134
142
  const invocation = deps.registry.invoke(input.toolName, input.input, ctx, deps.rolesPolicy);
135
143
  return input.timeoutMs !== void 0 ? withToolTimeout(invocation, input.timeoutMs, input.toolName) : invocation;
136
144
  }, transientRetry, {
@@ -143,6 +151,7 @@ var AgentRunSteps = class {
143
151
  });
144
152
  }, "onRetry")
145
153
  }));
154
+ return input.collectUi === true ? wrapToolStepOutput(output, ui.components()) : output;
146
155
  }
147
156
  };
148
157
  _ts_decorate([
@@ -386,7 +395,10 @@ var AgentRunWorkflow = class _AgentRunWorkflow {
386
395
  dispatchTool: /* @__PURE__ */ __name((call, envelope) => ctx.step(AgentRunSteps.prototype.tool, {
387
396
  ...envelope,
388
397
  toolCallId: call.id,
389
- toolType: call.kind === "action" ? "action" : "read"
398
+ toolType: call.kind === "action" ? "action" : "read",
399
+ // Where `ctx.emitUi` streams from the worker — the llm step's routing.
400
+ sinkRunId,
401
+ childSink: input.sinkRunId !== void 0
390
402
  }), "dispatchTool")
391
403
  } : {}
392
404
  };
@@ -471,7 +483,7 @@ __name(isSignalTimeout, "isSignalTimeout");
471
483
 
472
484
  // src/durable/durable-agent-runner.ts
473
485
  import { randomUUID } from "crypto";
474
- import { AGENT_SINK, AGENT_STORE as AGENT_STORE2, encodeStreamEvent } from "@dudousxd/nestjs-agent-core";
486
+ import { AGENT_SINK, AGENT_STORE as AGENT_STORE2, encodeStreamEvent as encodeStreamEvent2 } from "@dudousxd/nestjs-agent-core";
475
487
  import { RUN_GATEWAY as RUN_GATEWAY2, WorkflowService } from "@dudousxd/nestjs-durable";
476
488
  import { isWorkflowControlFlowSignal as isWorkflowControlFlowSignal2 } from "@dudousxd/nestjs-durable-core";
477
489
  import { Inject as Inject3, Injectable as Injectable3, Logger } from "@nestjs/common";
@@ -650,7 +662,7 @@ var DurableAgentRunner = class _DurableAgentRunner {
650
662
  await this.store.setActiveStream(threadId, null);
651
663
  }
652
664
  const writer = await this.sink.open(runId);
653
- await writer.write(encodeStreamEvent({
665
+ await writer.write(encodeStreamEvent2({
654
666
  kind: "cancelled"
655
667
  }));
656
668
  await writer.end();
@@ -3118,6 +3130,9 @@ var AiToolDiscoveryService = class _AiToolDiscoveryService {
3118
3130
  } : {},
3119
3131
  ...meta.presentation !== void 0 ? {
3120
3132
  presentation: meta.presentation
3133
+ } : {},
3134
+ ...meta.terminal === true ? {
3135
+ terminal: true
3121
3136
  } : {}
3122
3137
  }, {
3123
3138
  execute: /* @__PURE__ */ __name((input, ctx) => instance.execute(input, ctx), "execute"),
@@ -3327,7 +3342,7 @@ var LedgerQuotaStore = class {
3327
3342
  };
3328
3343
 
3329
3344
  // src/runner/inline-agent-runner.ts
3330
- import { AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY6, AGENT_STORE as AGENT_STORE5, RunCancelledError as RunCancelledError2, agentFailureCode as agentFailureCode2, encodeStreamEvent as encodeStreamEvent2, publishAgentRunFailed as publishAgentRunFailed2, runAgentLoop as runAgentLoop2, settleAll as settleAll2, settleUnsettledDelegation as settleUnsettledDelegation2 } from "@dudousxd/nestjs-agent-core";
3345
+ import { AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY6, AGENT_STORE as AGENT_STORE5, RunCancelledError as RunCancelledError2, agentFailureCode as agentFailureCode2, encodeStreamEvent as encodeStreamEvent3, publishAgentRunFailed as publishAgentRunFailed2, runAgentLoop as runAgentLoop2, settleAll as settleAll2, settleUnsettledDelegation as settleUnsettledDelegation2 } from "@dudousxd/nestjs-agent-core";
3331
3346
  import { Inject as Inject18, Injectable as Injectable10, Logger as Logger5 } from "@nestjs/common";
3332
3347
  function _ts_decorate20(decorators, target, key, desc) {
3333
3348
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -3465,7 +3480,7 @@ var InlineAgentRunner = class _InlineAgentRunner {
3465
3480
  this.logger.log(`agent run ${runId} cancelled`);
3466
3481
  await this.store.setActiveStream(threadId, null);
3467
3482
  const writer = await deps.sink.open(runId);
3468
- await writer.write(encodeStreamEvent2({
3483
+ await writer.write(encodeStreamEvent3({
3469
3484
  kind: "cancelled"
3470
3485
  }));
3471
3486
  await writer.end();