@dudousxd/nestjs-agent 1.27.1 → 1.28.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.cjs CHANGED
@@ -22,8 +22,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
22
22
  // src/index.ts
23
23
  var index_exports = {};
24
24
  __export(index_exports, {
25
+ A2uiRunHandler: () => A2uiRunHandler,
25
26
  AGENT_CHAT_QUEUE: () => AGENT_CHAT_QUEUE,
26
27
  AGENT_METADATA: () => AGENT_METADATA,
28
+ AGENT_SERVICE: () => AGENT_SERVICE,
27
29
  AGENT_TOOL_BRAND: () => AGENT_TOOL_BRAND,
28
30
  AI_TOOL_METADATA: () => AI_TOOL_METADATA,
29
31
  ATTACHMENT_PAGE_SIZE: () => ATTACHMENT_PAGE_SIZE,
@@ -36,7 +38,7 @@ __export(index_exports, {
36
38
  AgentDepsFactory: () => AgentDepsFactory2,
37
39
  AgentDiscoveryService: () => AgentDiscoveryService,
38
40
  AgentModule: () => AgentModule,
39
- AgentService: () => AgentService,
41
+ AgentService: () => AgentService2,
40
42
  AiTool: () => AiTool,
41
43
  AiToolDiscoveryService: () => AiToolDiscoveryService,
42
44
  AnonymousActorResolver: () => AnonymousActorResolver,
@@ -65,6 +67,7 @@ __export(index_exports, {
65
67
  SystemPrompt: () => SystemPrompt,
66
68
  SystemPromptContributor: () => SystemPromptContributor,
67
69
  ToolsController: () => ToolsController,
70
+ a2uiAdapter: () => a2uiAdapter,
68
71
  agUiAdapter: () => agUiAdapter,
69
72
  attachmentLimits: () => attachmentLimits,
70
73
  declaredSkillProvider: () => declaredSkillProvider,
@@ -176,7 +179,7 @@ function resolverProviders(resolver) {
176
179
  }
177
180
  __name(resolverProviders, "resolverProviders");
178
181
  function toolOptions(options, resolver) {
179
- const { catalog: _catalog, ...rest } = options;
182
+ const { catalog: _catalog, sandbox: _sandbox, ...rest } = options;
180
183
  return {
181
184
  ...rest,
182
185
  ...resolver !== void 0 ? {
@@ -185,11 +188,19 @@ function toolOptions(options, resolver) {
185
188
  };
186
189
  }
187
190
  __name(toolOptions, "toolOptions");
191
+ function withSandbox(options) {
192
+ const base = options.catalog ?? (0, import_genui.defineCatalog)([]);
193
+ const { sandbox } = options;
194
+ return sandbox === void 0 || sandbox === false ? base : base.extend([
195
+ (0, import_genui.defineSandbox)(sandbox === true ? {} : sandbox)
196
+ ]);
197
+ }
198
+ __name(withSandbox, "withSandbox");
188
199
  function coreProviders(resolver) {
189
200
  return [
190
201
  {
191
202
  provide: GENUI_CATALOG,
192
- useFactory: /* @__PURE__ */ __name((options) => options.catalog ?? (0, import_genui.defineCatalog)([]), "useFactory"),
203
+ useFactory: /* @__PURE__ */ __name((options) => withSandbox(options), "useFactory"),
193
204
  inject: [
194
205
  GENUI_OPTIONS
195
206
  ]
@@ -648,6 +659,9 @@ AgentDepsFactory2 = _ts_decorate2([
648
659
  ])
649
660
  ], AgentDepsFactory2);
650
661
 
662
+ // src/agent-service.token.ts
663
+ var AGENT_SERVICE = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:agent-service");
664
+
651
665
  // src/agent.service.ts
652
666
  var import_nestjs_agent_core9 = require("@dudousxd/nestjs-agent-core");
653
667
  var import_common6 = require("@nestjs/common");
@@ -1477,7 +1491,7 @@ function sendableParams(params) {
1477
1491
  };
1478
1492
  }
1479
1493
  __name(sendableParams, "sendableParams");
1480
- var AgentService = class {
1494
+ var AgentService2 = class {
1481
1495
  static {
1482
1496
  __name(this, "AgentService");
1483
1497
  }
@@ -2660,7 +2674,7 @@ var AgentService = class {
2660
2674
  }
2661
2675
  }
2662
2676
  };
2663
- AgentService = _ts_decorate5([
2677
+ AgentService2 = _ts_decorate5([
2664
2678
  (0, import_common6.Injectable)(),
2665
2679
  _ts_param4(0, (0, import_common6.Inject)(import_nestjs_agent_core9.AGENT_RUNNER)),
2666
2680
  _ts_param4(1, (0, import_common6.Inject)(import_nestjs_agent_core9.AGENT_STORE)),
@@ -2687,7 +2701,7 @@ AgentService = _ts_decorate5([
2687
2701
  typeof ChatQueueService === "undefined" ? Object : ChatQueueService,
2688
2702
  typeof ActionProposalService === "undefined" ? Object : ActionProposalService
2689
2703
  ])
2690
- ], AgentService);
2704
+ ], AgentService2);
2691
2705
 
2692
2706
  // src/approval-port.adapter.ts
2693
2707
  var import_common7 = require("@nestjs/common");
@@ -2781,7 +2795,7 @@ AgentApprovalPortAdapter = _ts_decorate6([
2781
2795
  _ts_param5(1, (0, import_common7.Optional)()),
2782
2796
  _ts_metadata5("design:type", Function),
2783
2797
  _ts_metadata5("design:paramtypes", [
2784
- typeof AgentService === "undefined" ? Object : AgentService,
2798
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
2785
2799
  typeof ActionProposalService === "undefined" ? Object : ActionProposalService
2786
2800
  ])
2787
2801
  ], AgentApprovalPortAdapter);
@@ -2982,7 +2996,7 @@ AttachmentsController = _ts_decorate8([
2982
2996
  _ts_metadata7("design:paramtypes", [
2983
2997
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
2984
2998
  typeof ActorResolver === "undefined" ? Object : ActorResolver,
2985
- typeof AgentService === "undefined" ? Object : AgentService,
2999
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
2986
3000
  Object
2987
3001
  ])
2988
3002
  ], AttachmentsController);
@@ -3254,7 +3268,7 @@ ChatController = _ts_decorate9([
3254
3268
  _ts_param8(1, (0, import_common10.Inject)(import_nestjs_agent_core12.AGENT_ACTOR_RESOLVER)),
3255
3269
  _ts_metadata8("design:type", Function),
3256
3270
  _ts_metadata8("design:paramtypes", [
3257
- typeof AgentService === "undefined" ? Object : AgentService,
3271
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3258
3272
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3259
3273
  ])
3260
3274
  ], ChatController);
@@ -3652,7 +3666,7 @@ MessagesController = _ts_decorate12([
3652
3666
  _ts_param11(1, (0, import_common13.Inject)(import_nestjs_agent_core15.AGENT_ACTOR_RESOLVER)),
3653
3667
  _ts_metadata11("design:type", Function),
3654
3668
  _ts_metadata11("design:paramtypes", [
3655
- typeof AgentService === "undefined" ? Object : AgentService,
3669
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3656
3670
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3657
3671
  ])
3658
3672
  ], MessagesController);
@@ -3721,7 +3735,7 @@ ModelsController = _ts_decorate13([
3721
3735
  _ts_param12(1, (0, import_common14.Inject)(import_nestjs_agent_core16.AGENT_ACTOR_RESOLVER)),
3722
3736
  _ts_metadata12("design:type", Function),
3723
3737
  _ts_metadata12("design:paramtypes", [
3724
- typeof AgentService === "undefined" ? Object : AgentService,
3738
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3725
3739
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3726
3740
  ])
3727
3741
  ], ModelsController);
@@ -3881,7 +3895,7 @@ QueueController = _ts_decorate14([
3881
3895
  _ts_param13(1, (0, import_common15.Inject)(import_nestjs_agent_core17.AGENT_ACTOR_RESOLVER)),
3882
3896
  _ts_metadata13("design:type", Function),
3883
3897
  _ts_metadata13("design:paramtypes", [
3884
- typeof AgentService === "undefined" ? Object : AgentService,
3898
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3885
3899
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3886
3900
  ])
3887
3901
  ], QueueController);
@@ -3948,7 +3962,7 @@ QuotaController = _ts_decorate15([
3948
3962
  _ts_param14(1, (0, import_common16.Inject)(import_nestjs_agent_core18.AGENT_ACTOR_RESOLVER)),
3949
3963
  _ts_metadata14("design:type", Function),
3950
3964
  _ts_metadata14("design:paramtypes", [
3951
- typeof AgentService === "undefined" ? Object : AgentService,
3965
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3952
3966
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3953
3967
  ])
3954
3968
  ], QuotaController);
@@ -4211,7 +4225,7 @@ ThreadsController = _ts_decorate17([
4211
4225
  _ts_param16(1, (0, import_common18.Inject)(import_nestjs_agent_core20.AGENT_ACTOR_RESOLVER)),
4212
4226
  _ts_metadata16("design:type", Function),
4213
4227
  _ts_metadata16("design:paramtypes", [
4214
- typeof AgentService === "undefined" ? Object : AgentService,
4228
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
4215
4229
  typeof ActorResolver === "undefined" ? Object : ActorResolver
4216
4230
  ])
4217
4231
  ], ThreadsController);
@@ -4426,7 +4440,7 @@ ToolCallController = _ts_decorate18([
4426
4440
  _ts_param17(1, (0, import_common19.Inject)(import_nestjs_agent_core21.AGENT_ACTOR_RESOLVER)),
4427
4441
  _ts_metadata17("design:type", Function),
4428
4442
  _ts_metadata17("design:paramtypes", [
4429
- typeof AgentService === "undefined" ? Object : AgentService,
4443
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
4430
4444
  typeof ActorResolver === "undefined" ? Object : ActorResolver
4431
4445
  ])
4432
4446
  ], ToolCallController);
@@ -6360,7 +6374,11 @@ function sharedProviders(durable, engine) {
6360
6374
  useExisting: ChatQueueService
6361
6375
  },
6362
6376
  InlineAgentRunner,
6363
- AgentService,
6377
+ AgentService2,
6378
+ {
6379
+ provide: AGENT_SERVICE,
6380
+ useExisting: AgentService2
6381
+ },
6364
6382
  ActionProposalService,
6365
6383
  ActionProposalWorkerService,
6366
6384
  // Bound ALWAYS (durable or inline) — the console's cross-thread approvals inbox routes decisions
@@ -6427,7 +6445,8 @@ function exportsFor(engine) {
6427
6445
  import_nestjs_agent_core34.AGENT_MEMORY,
6428
6446
  import_nestjs_agent_core34.AGENT_DEPS_FACTORY,
6429
6447
  AgentDepsFactory2,
6430
- AgentService,
6448
+ AgentService2,
6449
+ AGENT_SERVICE,
6431
6450
  ActionProposalService,
6432
6451
  ActionProposalWorkerService,
6433
6452
  ChatQueueService,
@@ -6589,6 +6608,7 @@ AgentModule = _ts_decorate27([
6589
6608
  // src/ag-ui/ag-ui.adapter.ts
6590
6609
  var import_nestjs_agent_core35 = require("@dudousxd/nestjs-agent-core");
6591
6610
  var import_ag_ui = require("@dudousxd/nestjs-agent-core/ag-ui");
6611
+ var import_genui3 = require("@dudousxd/nestjs-agent-core/genui");
6592
6612
  var import_common29 = require("@nestjs/common");
6593
6613
  function _ts_decorate28(decorators, target, key, desc) {
6594
6614
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -6644,12 +6664,14 @@ var AgUiRunHandler = class {
6644
6664
  options;
6645
6665
  staging;
6646
6666
  quietMs;
6647
- constructor(agent, actorResolver, options, staging, quietMs) {
6667
+ a2ui;
6668
+ constructor(agent, actorResolver, options, staging, quietMs, a2ui = void 0) {
6648
6669
  this.agent = agent;
6649
6670
  this.actorResolver = actorResolver;
6650
6671
  this.options = options;
6651
6672
  this.staging = staging;
6652
6673
  this.quietMs = quietMs;
6674
+ this.a2ui = a2ui;
6653
6675
  }
6654
6676
  async handle(req, res, body) {
6655
6677
  const input = (0, import_ag_ui.parseRunInput)(body);
@@ -6703,7 +6725,16 @@ var AgUiRunHandler = class {
6703
6725
  return;
6704
6726
  }
6705
6727
  }
6706
- const turn = (0, import_ag_ui.readUserTurn)(input.messages);
6728
+ const action = forwarded.uiAction;
6729
+ if (typeof action === "string") {
6730
+ throw invalid("invalid_ui_action", action);
6731
+ }
6732
+ const turn = action !== void 0 ? {
6733
+ text: (0, import_genui3.uiActionText)(action),
6734
+ media: [],
6735
+ staged: [],
6736
+ dropped: []
6737
+ } : (0, import_ag_ui.readUserTurn)(input.messages);
6707
6738
  if (turn === null) {
6708
6739
  throw invalid(NO_USER_MESSAGE_CODE, "messages carries no user message to answer");
6709
6740
  }
@@ -6715,7 +6746,7 @@ var AgUiRunHandler = class {
6715
6746
  if (forwarded.regenerate === true && owner === null) {
6716
6747
  throw invalid("invalid_input", "regenerate needs a thread that already has an exchange");
6717
6748
  }
6718
- if (owner !== null && forwarded.regenerate !== true && turn.media.length === 0 && turn.staged.length === 0) {
6749
+ if (owner !== null && forwarded.regenerate !== true && action === void 0 && turn.media.length === 0 && turn.staged.length === 0) {
6719
6750
  const decision = await this.agent.handleTextDecision(input.threadId, actor, turn.text);
6720
6751
  if ("proposalDecision" in decision) {
6721
6752
  this.writeEvents(res, [
@@ -6942,8 +6973,12 @@ var AgUiRunHandler = class {
6942
6973
  };
6943
6974
  try {
6944
6975
  const frames = (0, import_ag_ui.agUiFramesFromNdjson)(this.agent.subscribe(runId));
6976
+ const a2ui = this.a2ui !== void 0 ? {
6977
+ a2ui: this.a2ui
6978
+ } : {};
6945
6979
  for await (const event of (0, import_ag_ui.agUiEvents)(frames, {
6946
6980
  ...options,
6981
+ ...a2ui,
6947
6982
  cursor
6948
6983
  })) {
6949
6984
  if (event.type === "RUN_FINISHED" || event.type === "RUN_ERROR") terminal = true;
@@ -6964,12 +6999,13 @@ var AgUiRunHandler = class {
6964
6999
  function agUiAdapter(options = {}) {
6965
7000
  const path = (options.path ?? "ag-ui").replace(/^\/+|\/+$/g, "");
6966
7001
  const quietMs = options.quietMs;
7002
+ const a2ui = options.a2ui === void 0 || options.a2ui === false ? void 0 : options.a2ui === true ? {} : options.a2ui;
6967
7003
  let AgUiController = class AgUiController extends AgUiRunHandler {
6968
7004
  static {
6969
7005
  __name(this, "AgUiController");
6970
7006
  }
6971
- constructor(agent, actorResolver, moduleOptions, staging) {
6972
- super(agent, actorResolver, moduleOptions, staging, quietMs);
7007
+ constructor(agent, actorResolver, moduleOptions, staging, catalog) {
7008
+ super(agent, actorResolver, moduleOptions, staging, quietMs, a2ui !== void 0 ? withCatalog(a2ui, catalog) : void 0);
6973
7009
  }
6974
7010
  run(req, res, body) {
6975
7011
  return this.handle(req, res, body);
@@ -6991,17 +7027,20 @@ function agUiAdapter(options = {}) {
6991
7027
  ], AgUiController.prototype, "run", null);
6992
7028
  AgUiController = _ts_decorate28([
6993
7029
  (0, import_common29.Controller)(),
6994
- _ts_param26(0, (0, import_common29.Inject)(AgentService)),
7030
+ _ts_param26(0, (0, import_common29.Inject)(AgentService2)),
6995
7031
  _ts_param26(1, (0, import_common29.Inject)(import_nestjs_agent_core35.AGENT_ACTOR_RESOLVER)),
6996
7032
  _ts_param26(2, (0, import_common29.Inject)(import_nestjs_agent_core35.AGENT_OPTIONS)),
6997
7033
  _ts_param26(3, (0, import_common29.Optional)()),
6998
7034
  _ts_param26(3, (0, import_common29.Inject)(import_nestjs_agent_core35.AGENT_ATTACHMENT_STAGING)),
7035
+ _ts_param26(4, (0, import_common29.Optional)()),
7036
+ _ts_param26(4, (0, import_common29.Inject)(GENUI_CATALOG)),
6999
7037
  _ts_metadata26("design:type", Function),
7000
7038
  _ts_metadata26("design:paramtypes", [
7001
- typeof AgentService === "undefined" ? Object : AgentService,
7039
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
7002
7040
  typeof ActorResolver === "undefined" ? Object : ActorResolver,
7003
7041
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
7004
- typeof AttachmentStagingStore === "undefined" ? Object : AttachmentStagingStore
7042
+ typeof AttachmentStagingStore === "undefined" ? Object : AttachmentStagingStore,
7043
+ typeof Catalog === "undefined" ? Object : Catalog
7005
7044
  ])
7006
7045
  ], AgUiController);
7007
7046
  return {
@@ -7012,9 +7051,291 @@ function agUiAdapter(options = {}) {
7012
7051
  };
7013
7052
  }
7014
7053
  __name(agUiAdapter, "agUiAdapter");
7054
+ function withCatalog(options, catalog) {
7055
+ if (options.catalog !== void 0 || catalog === void 0) return options;
7056
+ return {
7057
+ ...options,
7058
+ catalog
7059
+ };
7060
+ }
7061
+ __name(withCatalog, "withCatalog");
7015
7062
 
7016
- // src/resolver/header-actor-resolver.ts
7063
+ // src/a2ui/a2ui.adapter.ts
7064
+ var import_nestjs_agent_core36 = require("@dudousxd/nestjs-agent-core");
7065
+ var import_a2ui = require("@dudousxd/nestjs-agent-core/a2ui");
7066
+ var import_ag_ui2 = require("@dudousxd/nestjs-agent-core/ag-ui");
7067
+ var import_genui4 = require("@dudousxd/nestjs-agent-core/genui");
7017
7068
  var import_common30 = require("@nestjs/common");
7069
+ function _ts_decorate29(decorators, target, key, desc) {
7070
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
7071
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
7072
+ r = Reflect.decorate(decorators, target, key, desc);
7073
+ } else {
7074
+ for (var i = decorators.length - 1; i >= 0; i--) {
7075
+ if (d = decorators[i]) {
7076
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
7077
+ }
7078
+ }
7079
+ }
7080
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7081
+ }
7082
+ __name(_ts_decorate29, "_ts_decorate");
7083
+ function _ts_metadata27(metadataKey, metadataValue) {
7084
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
7085
+ return Reflect.metadata(metadataKey, metadataValue);
7086
+ }
7087
+ }
7088
+ __name(_ts_metadata27, "_ts_metadata");
7089
+ function _ts_param27(paramIndex, decorator) {
7090
+ return function(target, key) {
7091
+ decorator(target, key, paramIndex);
7092
+ };
7093
+ }
7094
+ __name(_ts_param27, "_ts_param");
7095
+ var A2UI_VIA = "a2ui";
7096
+ function isRecord(value) {
7097
+ return typeof value === "object" && value !== null && !Array.isArray(value);
7098
+ }
7099
+ __name(isRecord, "isRecord");
7100
+ function invalid2(code, message) {
7101
+ return new import_common30.BadRequestException({
7102
+ statusCode: 400,
7103
+ code,
7104
+ message
7105
+ });
7106
+ }
7107
+ __name(invalid2, "invalid");
7108
+ var A2uiRunHandler = class {
7109
+ static {
7110
+ __name(this, "A2uiRunHandler");
7111
+ }
7112
+ agent;
7113
+ actorResolver;
7114
+ projection;
7115
+ quietMs;
7116
+ maxActionBytes;
7117
+ constructor(agent, actorResolver, projection, quietMs, maxActionBytes) {
7118
+ this.agent = agent;
7119
+ this.actorResolver = actorResolver;
7120
+ this.projection = projection;
7121
+ this.quietMs = quietMs;
7122
+ this.maxActionBytes = maxActionBytes;
7123
+ }
7124
+ async handle(req, res, raw) {
7125
+ const body = isRecord(raw) ? raw : {};
7126
+ const agentName = typeof body.agent === "string" && body.agent.length > 0 ? body.agent : void 0;
7127
+ const actor = await this.actorResolver.resolve(req);
7128
+ let action;
7129
+ if (body.action !== void 0) {
7130
+ const read = isRecord(body.action) ? (0, import_a2ui.readA2uiAction)(body.action, {
7131
+ ...this.maxActionBytes !== void 0 ? {
7132
+ maxBytes: this.maxActionBytes
7133
+ } : {}
7134
+ }) : "action must be an A2UI action message";
7135
+ if (typeof read === "string") throw invalid2("invalid_action", read);
7136
+ action = read;
7137
+ }
7138
+ if (action?.name === import_a2ui.A2UI_APPROVE_ACTION || action?.name === import_a2ui.A2UI_REJECT_ACTION) {
7139
+ const interruptId = action.context.interruptId;
7140
+ const address = typeof interruptId === "string" ? (0, import_ag_ui2.decodeInterruptId)(interruptId) : void 0;
7141
+ if (address === void 0 || address === null || address.kind === "elicitation") {
7142
+ throw invalid2("invalid_action", "an approval action carries the interruptId it decides");
7143
+ }
7144
+ if (address.kind === "proposal") {
7145
+ if (address.proposalId === void 0 || address.threadId === void 0) {
7146
+ throw invalid2("invalid_action", "the interruptId names no proposal");
7147
+ }
7148
+ const decided = await this.agent.decideActionProposal(actor, address.threadId, address.proposalId, {
7149
+ decision: action.name === import_a2ui.A2UI_APPROVE_ACTION ? "approved" : "rejected",
7150
+ via: A2UI_VIA
7151
+ });
7152
+ this.writeHead(res, {
7153
+ "X-Agent-Thread-Id": address.threadId
7154
+ });
7155
+ const projector = new import_a2ui.A2uiProjector(this.projection);
7156
+ const messageId = `proposal-${address.proposalId}`;
7157
+ for (const event of [
7158
+ {
7159
+ type: "TEXT_MESSAGE_START",
7160
+ messageId,
7161
+ role: "assistant"
7162
+ },
7163
+ {
7164
+ type: "TEXT_MESSAGE_CONTENT",
7165
+ messageId,
7166
+ delta: decided.text
7167
+ }
7168
+ ]) {
7169
+ for (const message2 of projector.project(event)) res.write(`${JSON.stringify(message2)}
7170
+ `);
7171
+ }
7172
+ res.end();
7173
+ return;
7174
+ }
7175
+ await this.agent.assertResumable(actor, address.stream);
7176
+ await this.agent.checkDecision(actor, address.toolCallId);
7177
+ if (action.name === import_a2ui.A2UI_APPROVE_ACTION) {
7178
+ await this.agent.approve(actor, address.toolCallId, {
7179
+ via: A2UI_VIA
7180
+ });
7181
+ } else {
7182
+ await this.agent.reject(actor, address.toolCallId, void 0, {
7183
+ via: A2UI_VIA
7184
+ });
7185
+ }
7186
+ await this.stream(res, address.stream, {
7187
+ skip: address.position,
7188
+ answered: [
7189
+ address.toolCallId
7190
+ ]
7191
+ });
7192
+ return;
7193
+ }
7194
+ const message = typeof body.message === "string" ? body.message : "";
7195
+ const text = action !== void 0 ? (0, import_genui4.uiActionText)(action) : message;
7196
+ if (text.trim().length === 0) {
7197
+ throw invalid2("no_user_message", "send a message or an action");
7198
+ }
7199
+ const threadId = typeof body.threadId === "string" && body.threadId.length > 0 ? body.threadId : void 0;
7200
+ const owner = threadId !== void 0 ? await this.agent.threadOwner(threadId) : null;
7201
+ if (owner !== null && owner !== actor.id) {
7202
+ throw new import_common30.ForbiddenException("thread belongs to another actor");
7203
+ }
7204
+ let uiCapabilities;
7205
+ if (Object.hasOwn(body, "uiCapabilities")) {
7206
+ try {
7207
+ uiCapabilities = (0, import_genui4.validateUiCapabilities)(body.uiCapabilities);
7208
+ } catch (error) {
7209
+ throw invalid2("invalid_input", error.message);
7210
+ }
7211
+ }
7212
+ const started = await this.agent.chat({
7213
+ actor,
7214
+ message: text,
7215
+ ...threadId === void 0 ? {} : owner !== null ? {
7216
+ threadId
7217
+ } : {
7218
+ newThreadId: threadId
7219
+ },
7220
+ ...agentName !== void 0 ? {
7221
+ agentName
7222
+ } : {},
7223
+ ...uiCapabilities !== void 0 ? {
7224
+ uiCapabilities
7225
+ } : {}
7226
+ });
7227
+ await this.stream(res, started.runId, {
7228
+ threadId: started.threadId
7229
+ });
7230
+ }
7231
+ /** Open the JSON Lines response. */
7232
+ writeHead(res, extra) {
7233
+ res.status(200);
7234
+ res.setHeader("Content-Type", "application/jsonl; charset=utf-8");
7235
+ res.setHeader("Cache-Control", "no-cache, no-transform");
7236
+ res.setHeader("Connection", "keep-alive");
7237
+ res.setHeader("X-Accel-Buffering", "no");
7238
+ for (const [name, value] of Object.entries(extra)) res.setHeader(name, value);
7239
+ res.flushHeaders();
7240
+ }
7241
+ /** Write one run as A2UI JSON Lines, until it ends or stops to ask. */
7242
+ async stream(res, runId, options) {
7243
+ this.writeHead(res, {
7244
+ "X-Agent-Run-Id": runId,
7245
+ ...options.threadId !== void 0 ? {
7246
+ "X-Agent-Thread-Id": options.threadId
7247
+ } : {}
7248
+ });
7249
+ const projector = new import_a2ui.A2uiProjector(this.projection);
7250
+ const write = /* @__PURE__ */ __name((messages) => {
7251
+ for (const message of messages) res.write(`${JSON.stringify(message)}
7252
+ `);
7253
+ }, "write");
7254
+ try {
7255
+ const frames = (0, import_ag_ui2.agUiFramesFromNdjson)(this.agent.subscribe(runId));
7256
+ for await (const event of (0, import_ag_ui2.agUiEvents)(frames, {
7257
+ threadId: options.threadId ?? runId,
7258
+ runId,
7259
+ streamRunId: runId,
7260
+ ...options.threadId !== void 0 ? {
7261
+ streamThreadId: options.threadId
7262
+ } : {},
7263
+ ...options.skip !== void 0 ? {
7264
+ skip: options.skip
7265
+ } : {},
7266
+ ...options.answered !== void 0 ? {
7267
+ answered: options.answered
7268
+ } : {},
7269
+ ...this.quietMs !== void 0 ? {
7270
+ quietMs: this.quietMs
7271
+ } : {}
7272
+ })) {
7273
+ write(projector.project(event));
7274
+ }
7275
+ } catch {
7276
+ write(projector.project({
7277
+ type: "RUN_ERROR",
7278
+ message: "The run could not be followed."
7279
+ }));
7280
+ }
7281
+ res.end();
7282
+ }
7283
+ };
7284
+ function a2uiAdapter(options = {}) {
7285
+ const path = (options.path ?? "a2ui").replace(/^\/+|\/+$/g, "");
7286
+ const { path: _path, quietMs, maxActionBytes, ...projection } = options;
7287
+ let A2uiController = class A2uiController extends A2uiRunHandler {
7288
+ static {
7289
+ __name(this, "A2uiController");
7290
+ }
7291
+ constructor(agent, actorResolver, catalog) {
7292
+ super(agent, actorResolver, projection.catalog === void 0 && catalog !== void 0 ? {
7293
+ ...projection,
7294
+ catalog
7295
+ } : projection, quietMs, maxActionBytes);
7296
+ }
7297
+ run(req, res, body) {
7298
+ return this.handle(req, res, body);
7299
+ }
7300
+ };
7301
+ _ts_decorate29([
7302
+ (0, import_common30.Post)(path),
7303
+ (0, import_common30.HttpCode)(200),
7304
+ _ts_param27(0, (0, import_common30.Req)()),
7305
+ _ts_param27(1, (0, import_common30.Res)()),
7306
+ _ts_param27(2, (0, import_common30.Body)()),
7307
+ _ts_metadata27("design:type", Function),
7308
+ _ts_metadata27("design:paramtypes", [
7309
+ typeof Request === "undefined" ? Object : Request,
7310
+ typeof Response === "undefined" ? Object : Response,
7311
+ Object
7312
+ ]),
7313
+ _ts_metadata27("design:returntype", typeof Promise === "undefined" ? Object : Promise)
7314
+ ], A2uiController.prototype, "run", null);
7315
+ A2uiController = _ts_decorate29([
7316
+ (0, import_common30.Controller)(),
7317
+ _ts_param27(0, (0, import_common30.Inject)(AGENT_SERVICE)),
7318
+ _ts_param27(1, (0, import_common30.Inject)(import_nestjs_agent_core36.AGENT_ACTOR_RESOLVER)),
7319
+ _ts_param27(2, (0, import_common30.Optional)()),
7320
+ _ts_param27(2, (0, import_common30.Inject)(GENUI_CATALOG)),
7321
+ _ts_metadata27("design:type", Function),
7322
+ _ts_metadata27("design:paramtypes", [
7323
+ typeof AgentService === "undefined" ? Object : AgentService,
7324
+ typeof ActorResolver === "undefined" ? Object : ActorResolver,
7325
+ typeof Catalog === "undefined" ? Object : Catalog
7326
+ ])
7327
+ ], A2uiController);
7328
+ return {
7329
+ name: "a2ui",
7330
+ controllers: [
7331
+ A2uiController
7332
+ ]
7333
+ };
7334
+ }
7335
+ __name(a2uiAdapter, "a2uiAdapter");
7336
+
7337
+ // src/resolver/header-actor-resolver.ts
7338
+ var import_common31 = require("@nestjs/common");
7018
7339
  function readHeader(req, name) {
7019
7340
  if (typeof req !== "object" || req === null || !("headers" in req)) return void 0;
7020
7341
  const { headers } = req;
@@ -7030,7 +7351,7 @@ var HeaderActorResolver = class {
7030
7351
  resolve(req) {
7031
7352
  const id = readHeader(req, "x-actor-id");
7032
7353
  if (id === void 0 || id.length === 0) {
7033
- throw new import_common30.UnauthorizedException("HeaderActorResolver: missing x-actor-id header. No default actor is fabricated. Send x-actor-id from a trusted gateway, or configure AgentModule.forRoot({ actorResolver }) with a resolver that reads your authenticated principal.");
7354
+ throw new import_common31.UnauthorizedException("HeaderActorResolver: missing x-actor-id header. No default actor is fabricated. Send x-actor-id from a trusted gateway, or configure AgentModule.forRoot({ actorResolver }) with a resolver that reads your authenticated principal.");
7034
7355
  }
7035
7356
  const roles = (readHeader(req, "x-actor-role") ?? "").split(",").map((role) => role.trim()).filter((role) => role.length > 0);
7036
7357
  const tenantRef = readHeader(req, "x-tenant-ref");
@@ -7045,7 +7366,7 @@ var HeaderActorResolver = class {
7045
7366
  };
7046
7367
 
7047
7368
  // src/resolver/request-user-actor-resolver.ts
7048
- var import_common31 = require("@nestjs/common");
7369
+ var import_common32 = require("@nestjs/common");
7049
7370
  function field(user, ...names) {
7050
7371
  for (const name of names) {
7051
7372
  const value = user[name];
@@ -7057,7 +7378,7 @@ __name(field, "field");
7057
7378
  function defaultRequestUserMapper(user) {
7058
7379
  const id = field(user, "id", "sub", "_id", "userId");
7059
7380
  if (id === void 0) {
7060
- throw new import_common31.UnauthorizedException("requestUserActorResolver: req.user has no id/sub/_id/userId \u2014 pass a mapper: requestUserActorResolver((user) => ({ id: \u2026, roles: \u2026 }))");
7381
+ throw new import_common32.UnauthorizedException("requestUserActorResolver: req.user has no id/sub/_id/userId \u2014 pass a mapper: requestUserActorResolver((user) => ({ id: \u2026, roles: \u2026 }))");
7061
7382
  }
7062
7383
  const rawRoles = field(user, "roles", "role");
7063
7384
  const roles = Array.isArray(rawRoles) ? rawRoles.map(String) : typeof rawRoles === "string" ? [
@@ -7078,7 +7399,7 @@ function requestUserActorResolver(map) {
7078
7399
  async resolve(req) {
7079
7400
  const user = typeof req === "object" && req !== null ? req.user : void 0;
7080
7401
  if (user === void 0 || user === null || user === false) {
7081
- throw new import_common31.UnauthorizedException("Sign in to use the agent.");
7402
+ throw new import_common32.UnauthorizedException("Sign in to use the agent.");
7082
7403
  }
7083
7404
  if (map !== void 0) return map(user, req);
7084
7405
  return defaultRequestUserMapper(user);
@@ -7091,8 +7412,10 @@ __name(requestUserActorResolver, "requestUserActorResolver");
7091
7412
  __reExport(index_exports, require("@dudousxd/nestjs-agent-core"), module.exports);
7092
7413
  // Annotate the CommonJS export names for ESM import in node:
7093
7414
  0 && (module.exports = {
7415
+ A2uiRunHandler,
7094
7416
  AGENT_CHAT_QUEUE,
7095
7417
  AGENT_METADATA,
7418
+ AGENT_SERVICE,
7096
7419
  AGENT_TOOL_BRAND,
7097
7420
  AI_TOOL_METADATA,
7098
7421
  ATTACHMENT_PAGE_SIZE,
@@ -7134,6 +7457,7 @@ __reExport(index_exports, require("@dudousxd/nestjs-agent-core"), module.exports
7134
7457
  SystemPrompt,
7135
7458
  SystemPromptContributor,
7136
7459
  ToolsController,
7460
+ a2uiAdapter,
7137
7461
  agUiAdapter,
7138
7462
  attachmentLimits,
7139
7463
  declaredSkillProvider,