@dudousxd/nestjs-agent 1.27.0 → 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.js CHANGED
@@ -13,7 +13,7 @@ import { AGENT_OPTIONS } from "@dudousxd/nestjs-agent-core";
13
13
  import { BadRequestException, Inject as Inject2, Injectable, Optional } from "@nestjs/common";
14
14
 
15
15
  // src/genui/agent-genui.module.ts
16
- import { defineCatalog, genuiTools } from "@dudousxd/nestjs-agent-core/genui";
16
+ import { defineCatalog, defineSandbox, genuiTools } from "@dudousxd/nestjs-agent-core/genui";
17
17
  import { Inject, Module } from "@nestjs/common";
18
18
 
19
19
  // src/functional-tool.ts
@@ -92,7 +92,7 @@ function resolverProviders(resolver) {
92
92
  }
93
93
  __name(resolverProviders, "resolverProviders");
94
94
  function toolOptions(options, resolver) {
95
- const { catalog: _catalog, ...rest } = options;
95
+ const { catalog: _catalog, sandbox: _sandbox, ...rest } = options;
96
96
  return {
97
97
  ...rest,
98
98
  ...resolver !== void 0 ? {
@@ -101,11 +101,19 @@ function toolOptions(options, resolver) {
101
101
  };
102
102
  }
103
103
  __name(toolOptions, "toolOptions");
104
+ function withSandbox(options) {
105
+ const base = options.catalog ?? defineCatalog([]);
106
+ const { sandbox } = options;
107
+ return sandbox === void 0 || sandbox === false ? base : base.extend([
108
+ defineSandbox(sandbox === true ? {} : sandbox)
109
+ ]);
110
+ }
111
+ __name(withSandbox, "withSandbox");
104
112
  function coreProviders(resolver) {
105
113
  return [
106
114
  {
107
115
  provide: GENUI_CATALOG,
108
- useFactory: /* @__PURE__ */ __name((options) => options.catalog ?? defineCatalog([]), "useFactory"),
116
+ useFactory: /* @__PURE__ */ __name((options) => withSandbox(options), "useFactory"),
109
117
  inject: [
110
118
  GENUI_OPTIONS
111
119
  ]
@@ -564,6 +572,9 @@ AgentDepsFactory2 = _ts_decorate2([
564
572
  ])
565
573
  ], AgentDepsFactory2);
566
574
 
575
+ // src/agent-service.token.ts
576
+ var AGENT_SERVICE = /* @__PURE__ */ Symbol.for("@dudousxd/nestjs-agent:agent-service");
577
+
567
578
  // src/agent.service.ts
568
579
  import { AGENT_ATTACHMENT_STAGING as AGENT_ATTACHMENT_STAGING2, AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY2, AGENT_MODEL_CATALOG, AGENT_OPTIONS as AGENT_OPTIONS4, AGENT_QUOTA_PROVIDER as AGENT_QUOTA_PROVIDER2, AGENT_RUNNER, AGENT_STORE as AGENT_STORE4, DEFAULT_TEXT_ACTION_PROPOSAL_VOCABULARY as DEFAULT_TEXT_ACTION_PROPOSAL_VOCABULARY2, REQUESTER_APPROVER, findCatalogModel, mayDecideApproval as mayDecideApproval2, readElicitationQuestions, settleDeadRun, textActionProposalReply as textActionProposalReply2, validateElicitationAnswer } from "@dudousxd/nestjs-agent-core";
569
580
  import { BadRequestException as BadRequestException2, ConflictException as ConflictException2, ForbiddenException as ForbiddenException2, GoneException, HttpException, HttpStatus, Inject as Inject5, Injectable as Injectable4, NotFoundException as NotFoundException2, NotImplementedException as NotImplementedException3, Optional as Optional3, PayloadTooLargeException, UnsupportedMediaTypeException } from "@nestjs/common";
@@ -1393,7 +1404,7 @@ function sendableParams(params) {
1393
1404
  };
1394
1405
  }
1395
1406
  __name(sendableParams, "sendableParams");
1396
- var AgentService = class {
1407
+ var AgentService2 = class {
1397
1408
  static {
1398
1409
  __name(this, "AgentService");
1399
1410
  }
@@ -2576,7 +2587,7 @@ var AgentService = class {
2576
2587
  }
2577
2588
  }
2578
2589
  };
2579
- AgentService = _ts_decorate5([
2590
+ AgentService2 = _ts_decorate5([
2580
2591
  Injectable4(),
2581
2592
  _ts_param4(0, Inject5(AGENT_RUNNER)),
2582
2593
  _ts_param4(1, Inject5(AGENT_STORE4)),
@@ -2603,7 +2614,7 @@ AgentService = _ts_decorate5([
2603
2614
  typeof ChatQueueService === "undefined" ? Object : ChatQueueService,
2604
2615
  typeof ActionProposalService === "undefined" ? Object : ActionProposalService
2605
2616
  ])
2606
- ], AgentService);
2617
+ ], AgentService2);
2607
2618
 
2608
2619
  // src/approval-port.adapter.ts
2609
2620
  import { Injectable as Injectable5, NotImplementedException as NotImplementedException4, Optional as Optional4 } from "@nestjs/common";
@@ -2697,7 +2708,7 @@ AgentApprovalPortAdapter = _ts_decorate6([
2697
2708
  _ts_param5(1, Optional4()),
2698
2709
  _ts_metadata5("design:type", Function),
2699
2710
  _ts_metadata5("design:paramtypes", [
2700
- typeof AgentService === "undefined" ? Object : AgentService,
2711
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
2701
2712
  typeof ActionProposalService === "undefined" ? Object : ActionProposalService
2702
2713
  ])
2703
2714
  ], AgentApprovalPortAdapter);
@@ -2898,7 +2909,7 @@ AttachmentsController = _ts_decorate8([
2898
2909
  _ts_metadata7("design:paramtypes", [
2899
2910
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
2900
2911
  typeof ActorResolver === "undefined" ? Object : ActorResolver,
2901
- typeof AgentService === "undefined" ? Object : AgentService,
2912
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
2902
2913
  Object
2903
2914
  ])
2904
2915
  ], AttachmentsController);
@@ -3170,7 +3181,7 @@ ChatController = _ts_decorate9([
3170
3181
  _ts_param8(1, Inject8(AGENT_ACTOR_RESOLVER2)),
3171
3182
  _ts_metadata8("design:type", Function),
3172
3183
  _ts_metadata8("design:paramtypes", [
3173
- typeof AgentService === "undefined" ? Object : AgentService,
3184
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3174
3185
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3175
3186
  ])
3176
3187
  ], ChatController);
@@ -3568,7 +3579,7 @@ MessagesController = _ts_decorate12([
3568
3579
  _ts_param11(1, Inject11(AGENT_ACTOR_RESOLVER5)),
3569
3580
  _ts_metadata11("design:type", Function),
3570
3581
  _ts_metadata11("design:paramtypes", [
3571
- typeof AgentService === "undefined" ? Object : AgentService,
3582
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3572
3583
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3573
3584
  ])
3574
3585
  ], MessagesController);
@@ -3637,7 +3648,7 @@ ModelsController = _ts_decorate13([
3637
3648
  _ts_param12(1, Inject12(AGENT_ACTOR_RESOLVER6)),
3638
3649
  _ts_metadata12("design:type", Function),
3639
3650
  _ts_metadata12("design:paramtypes", [
3640
- typeof AgentService === "undefined" ? Object : AgentService,
3651
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3641
3652
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3642
3653
  ])
3643
3654
  ], ModelsController);
@@ -3797,7 +3808,7 @@ QueueController = _ts_decorate14([
3797
3808
  _ts_param13(1, Inject13(AGENT_ACTOR_RESOLVER7)),
3798
3809
  _ts_metadata13("design:type", Function),
3799
3810
  _ts_metadata13("design:paramtypes", [
3800
- typeof AgentService === "undefined" ? Object : AgentService,
3811
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3801
3812
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3802
3813
  ])
3803
3814
  ], QueueController);
@@ -3864,7 +3875,7 @@ QuotaController = _ts_decorate15([
3864
3875
  _ts_param14(1, Inject14(AGENT_ACTOR_RESOLVER8)),
3865
3876
  _ts_metadata14("design:type", Function),
3866
3877
  _ts_metadata14("design:paramtypes", [
3867
- typeof AgentService === "undefined" ? Object : AgentService,
3878
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
3868
3879
  typeof ActorResolver === "undefined" ? Object : ActorResolver
3869
3880
  ])
3870
3881
  ], QuotaController);
@@ -4127,7 +4138,7 @@ ThreadsController = _ts_decorate17([
4127
4138
  _ts_param16(1, Inject16(AGENT_ACTOR_RESOLVER10)),
4128
4139
  _ts_metadata16("design:type", Function),
4129
4140
  _ts_metadata16("design:paramtypes", [
4130
- typeof AgentService === "undefined" ? Object : AgentService,
4141
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
4131
4142
  typeof ActorResolver === "undefined" ? Object : ActorResolver
4132
4143
  ])
4133
4144
  ], ThreadsController);
@@ -4342,7 +4353,7 @@ ToolCallController = _ts_decorate18([
4342
4353
  _ts_param17(1, Inject17(AGENT_ACTOR_RESOLVER11)),
4343
4354
  _ts_metadata17("design:type", Function),
4344
4355
  _ts_metadata17("design:paramtypes", [
4345
- typeof AgentService === "undefined" ? Object : AgentService,
4356
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
4346
4357
  typeof ActorResolver === "undefined" ? Object : ActorResolver
4347
4358
  ])
4348
4359
  ], ToolCallController);
@@ -6276,7 +6287,11 @@ function sharedProviders(durable, engine) {
6276
6287
  useExisting: ChatQueueService
6277
6288
  },
6278
6289
  InlineAgentRunner,
6279
- AgentService,
6290
+ AgentService2,
6291
+ {
6292
+ provide: AGENT_SERVICE,
6293
+ useExisting: AgentService2
6294
+ },
6280
6295
  ActionProposalService,
6281
6296
  ActionProposalWorkerService,
6282
6297
  // Bound ALWAYS (durable or inline) — the console's cross-thread approvals inbox routes decisions
@@ -6343,7 +6358,8 @@ function exportsFor(engine) {
6343
6358
  AGENT_MEMORY3,
6344
6359
  AGENT_DEPS_FACTORY7,
6345
6360
  AgentDepsFactory2,
6346
- AgentService,
6361
+ AgentService2,
6362
+ AGENT_SERVICE,
6347
6363
  ActionProposalService,
6348
6364
  ActionProposalWorkerService,
6349
6365
  ChatQueueService,
@@ -6505,6 +6521,7 @@ AgentModule = _ts_decorate27([
6505
6521
  // src/ag-ui/ag-ui.adapter.ts
6506
6522
  import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER15, AGENT_ATTACHMENT_STAGING as AGENT_ATTACHMENT_STAGING5, AGENT_OPTIONS as AGENT_OPTIONS11 } from "@dudousxd/nestjs-agent-core";
6507
6523
  import { AG_UI_CUSTOM, actionProposalDecisionEvents, agUiEvents, agUiFramesFromNdjson, agUiSse, parseRunInput, planResume, readAnswersPayload, readApprovalPayload, readContext, readForwardedProps, readUserTurn } from "@dudousxd/nestjs-agent-core/ag-ui";
6524
+ import { uiActionText } from "@dudousxd/nestjs-agent-core/genui";
6508
6525
  import { BadRequestException as BadRequestException8, Body as Body7, Controller as Controller15, ForbiddenException as ForbiddenException4, HttpCode as HttpCode2, Inject as Inject26, Optional as Optional11, Post as Post8, Req as Req14, Res as Res3 } from "@nestjs/common";
6509
6526
  function _ts_decorate28(decorators, target, key, desc) {
6510
6527
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -6560,12 +6577,14 @@ var AgUiRunHandler = class {
6560
6577
  options;
6561
6578
  staging;
6562
6579
  quietMs;
6563
- constructor(agent, actorResolver, options, staging, quietMs) {
6580
+ a2ui;
6581
+ constructor(agent, actorResolver, options, staging, quietMs, a2ui = void 0) {
6564
6582
  this.agent = agent;
6565
6583
  this.actorResolver = actorResolver;
6566
6584
  this.options = options;
6567
6585
  this.staging = staging;
6568
6586
  this.quietMs = quietMs;
6587
+ this.a2ui = a2ui;
6569
6588
  }
6570
6589
  async handle(req, res, body) {
6571
6590
  const input = parseRunInput(body);
@@ -6619,7 +6638,16 @@ var AgUiRunHandler = class {
6619
6638
  return;
6620
6639
  }
6621
6640
  }
6622
- const turn = readUserTurn(input.messages);
6641
+ const action = forwarded.uiAction;
6642
+ if (typeof action === "string") {
6643
+ throw invalid("invalid_ui_action", action);
6644
+ }
6645
+ const turn = action !== void 0 ? {
6646
+ text: uiActionText(action),
6647
+ media: [],
6648
+ staged: [],
6649
+ dropped: []
6650
+ } : readUserTurn(input.messages);
6623
6651
  if (turn === null) {
6624
6652
  throw invalid(NO_USER_MESSAGE_CODE, "messages carries no user message to answer");
6625
6653
  }
@@ -6631,7 +6659,7 @@ var AgUiRunHandler = class {
6631
6659
  if (forwarded.regenerate === true && owner === null) {
6632
6660
  throw invalid("invalid_input", "regenerate needs a thread that already has an exchange");
6633
6661
  }
6634
- if (owner !== null && forwarded.regenerate !== true && turn.media.length === 0 && turn.staged.length === 0) {
6662
+ if (owner !== null && forwarded.regenerate !== true && action === void 0 && turn.media.length === 0 && turn.staged.length === 0) {
6635
6663
  const decision = await this.agent.handleTextDecision(input.threadId, actor, turn.text);
6636
6664
  if ("proposalDecision" in decision) {
6637
6665
  this.writeEvents(res, [
@@ -6858,8 +6886,12 @@ var AgUiRunHandler = class {
6858
6886
  };
6859
6887
  try {
6860
6888
  const frames = agUiFramesFromNdjson(this.agent.subscribe(runId));
6889
+ const a2ui = this.a2ui !== void 0 ? {
6890
+ a2ui: this.a2ui
6891
+ } : {};
6861
6892
  for await (const event of agUiEvents(frames, {
6862
6893
  ...options,
6894
+ ...a2ui,
6863
6895
  cursor
6864
6896
  })) {
6865
6897
  if (event.type === "RUN_FINISHED" || event.type === "RUN_ERROR") terminal = true;
@@ -6880,12 +6912,13 @@ var AgUiRunHandler = class {
6880
6912
  function agUiAdapter(options = {}) {
6881
6913
  const path = (options.path ?? "ag-ui").replace(/^\/+|\/+$/g, "");
6882
6914
  const quietMs = options.quietMs;
6915
+ const a2ui = options.a2ui === void 0 || options.a2ui === false ? void 0 : options.a2ui === true ? {} : options.a2ui;
6883
6916
  let AgUiController = class AgUiController extends AgUiRunHandler {
6884
6917
  static {
6885
6918
  __name(this, "AgUiController");
6886
6919
  }
6887
- constructor(agent, actorResolver, moduleOptions, staging) {
6888
- super(agent, actorResolver, moduleOptions, staging, quietMs);
6920
+ constructor(agent, actorResolver, moduleOptions, staging, catalog) {
6921
+ super(agent, actorResolver, moduleOptions, staging, quietMs, a2ui !== void 0 ? withCatalog(a2ui, catalog) : void 0);
6889
6922
  }
6890
6923
  run(req, res, body) {
6891
6924
  return this.handle(req, res, body);
@@ -6907,17 +6940,20 @@ function agUiAdapter(options = {}) {
6907
6940
  ], AgUiController.prototype, "run", null);
6908
6941
  AgUiController = _ts_decorate28([
6909
6942
  Controller15(),
6910
- _ts_param26(0, Inject26(AgentService)),
6943
+ _ts_param26(0, Inject26(AgentService2)),
6911
6944
  _ts_param26(1, Inject26(AGENT_ACTOR_RESOLVER15)),
6912
6945
  _ts_param26(2, Inject26(AGENT_OPTIONS11)),
6913
6946
  _ts_param26(3, Optional11()),
6914
6947
  _ts_param26(3, Inject26(AGENT_ATTACHMENT_STAGING5)),
6948
+ _ts_param26(4, Optional11()),
6949
+ _ts_param26(4, Inject26(GENUI_CATALOG)),
6915
6950
  _ts_metadata26("design:type", Function),
6916
6951
  _ts_metadata26("design:paramtypes", [
6917
- typeof AgentService === "undefined" ? Object : AgentService,
6952
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
6918
6953
  typeof ActorResolver === "undefined" ? Object : ActorResolver,
6919
6954
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
6920
- typeof AttachmentStagingStore === "undefined" ? Object : AttachmentStagingStore
6955
+ typeof AttachmentStagingStore === "undefined" ? Object : AttachmentStagingStore,
6956
+ typeof Catalog === "undefined" ? Object : Catalog
6921
6957
  ])
6922
6958
  ], AgUiController);
6923
6959
  return {
@@ -6928,6 +6964,288 @@ function agUiAdapter(options = {}) {
6928
6964
  };
6929
6965
  }
6930
6966
  __name(agUiAdapter, "agUiAdapter");
6967
+ function withCatalog(options, catalog) {
6968
+ if (options.catalog !== void 0 || catalog === void 0) return options;
6969
+ return {
6970
+ ...options,
6971
+ catalog
6972
+ };
6973
+ }
6974
+ __name(withCatalog, "withCatalog");
6975
+
6976
+ // src/a2ui/a2ui.adapter.ts
6977
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER16 } from "@dudousxd/nestjs-agent-core";
6978
+ import { A2UI_APPROVE_ACTION, A2UI_REJECT_ACTION, A2uiProjector, readA2uiAction } from "@dudousxd/nestjs-agent-core/a2ui";
6979
+ import { agUiEvents as agUiEvents2, agUiFramesFromNdjson as agUiFramesFromNdjson2, decodeInterruptId } from "@dudousxd/nestjs-agent-core/ag-ui";
6980
+ import { uiActionText as uiActionText2, validateUiCapabilities as validateUiCapabilities2 } from "@dudousxd/nestjs-agent-core/genui";
6981
+ import { BadRequestException as BadRequestException9, Body as Body8, Controller as Controller16, ForbiddenException as ForbiddenException5, HttpCode as HttpCode3, Inject as Inject27, Optional as Optional12, Post as Post9, Req as Req15, Res as Res4 } from "@nestjs/common";
6982
+ function _ts_decorate29(decorators, target, key, desc) {
6983
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
6984
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
6985
+ r = Reflect.decorate(decorators, target, key, desc);
6986
+ } else {
6987
+ for (var i = decorators.length - 1; i >= 0; i--) {
6988
+ if (d = decorators[i]) {
6989
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6990
+ }
6991
+ }
6992
+ }
6993
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6994
+ }
6995
+ __name(_ts_decorate29, "_ts_decorate");
6996
+ function _ts_metadata27(metadataKey, metadataValue) {
6997
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
6998
+ return Reflect.metadata(metadataKey, metadataValue);
6999
+ }
7000
+ }
7001
+ __name(_ts_metadata27, "_ts_metadata");
7002
+ function _ts_param27(paramIndex, decorator) {
7003
+ return function(target, key) {
7004
+ decorator(target, key, paramIndex);
7005
+ };
7006
+ }
7007
+ __name(_ts_param27, "_ts_param");
7008
+ var A2UI_VIA = "a2ui";
7009
+ function isRecord(value) {
7010
+ return typeof value === "object" && value !== null && !Array.isArray(value);
7011
+ }
7012
+ __name(isRecord, "isRecord");
7013
+ function invalid2(code, message) {
7014
+ return new BadRequestException9({
7015
+ statusCode: 400,
7016
+ code,
7017
+ message
7018
+ });
7019
+ }
7020
+ __name(invalid2, "invalid");
7021
+ var A2uiRunHandler = class {
7022
+ static {
7023
+ __name(this, "A2uiRunHandler");
7024
+ }
7025
+ agent;
7026
+ actorResolver;
7027
+ projection;
7028
+ quietMs;
7029
+ maxActionBytes;
7030
+ constructor(agent, actorResolver, projection, quietMs, maxActionBytes) {
7031
+ this.agent = agent;
7032
+ this.actorResolver = actorResolver;
7033
+ this.projection = projection;
7034
+ this.quietMs = quietMs;
7035
+ this.maxActionBytes = maxActionBytes;
7036
+ }
7037
+ async handle(req, res, raw) {
7038
+ const body = isRecord(raw) ? raw : {};
7039
+ const agentName = typeof body.agent === "string" && body.agent.length > 0 ? body.agent : void 0;
7040
+ const actor = await this.actorResolver.resolve(req);
7041
+ let action;
7042
+ if (body.action !== void 0) {
7043
+ const read = isRecord(body.action) ? readA2uiAction(body.action, {
7044
+ ...this.maxActionBytes !== void 0 ? {
7045
+ maxBytes: this.maxActionBytes
7046
+ } : {}
7047
+ }) : "action must be an A2UI action message";
7048
+ if (typeof read === "string") throw invalid2("invalid_action", read);
7049
+ action = read;
7050
+ }
7051
+ if (action?.name === A2UI_APPROVE_ACTION || action?.name === A2UI_REJECT_ACTION) {
7052
+ const interruptId = action.context.interruptId;
7053
+ const address = typeof interruptId === "string" ? decodeInterruptId(interruptId) : void 0;
7054
+ if (address === void 0 || address === null || address.kind === "elicitation") {
7055
+ throw invalid2("invalid_action", "an approval action carries the interruptId it decides");
7056
+ }
7057
+ if (address.kind === "proposal") {
7058
+ if (address.proposalId === void 0 || address.threadId === void 0) {
7059
+ throw invalid2("invalid_action", "the interruptId names no proposal");
7060
+ }
7061
+ const decided = await this.agent.decideActionProposal(actor, address.threadId, address.proposalId, {
7062
+ decision: action.name === A2UI_APPROVE_ACTION ? "approved" : "rejected",
7063
+ via: A2UI_VIA
7064
+ });
7065
+ this.writeHead(res, {
7066
+ "X-Agent-Thread-Id": address.threadId
7067
+ });
7068
+ const projector = new A2uiProjector(this.projection);
7069
+ const messageId = `proposal-${address.proposalId}`;
7070
+ for (const event of [
7071
+ {
7072
+ type: "TEXT_MESSAGE_START",
7073
+ messageId,
7074
+ role: "assistant"
7075
+ },
7076
+ {
7077
+ type: "TEXT_MESSAGE_CONTENT",
7078
+ messageId,
7079
+ delta: decided.text
7080
+ }
7081
+ ]) {
7082
+ for (const message2 of projector.project(event)) res.write(`${JSON.stringify(message2)}
7083
+ `);
7084
+ }
7085
+ res.end();
7086
+ return;
7087
+ }
7088
+ await this.agent.assertResumable(actor, address.stream);
7089
+ await this.agent.checkDecision(actor, address.toolCallId);
7090
+ if (action.name === A2UI_APPROVE_ACTION) {
7091
+ await this.agent.approve(actor, address.toolCallId, {
7092
+ via: A2UI_VIA
7093
+ });
7094
+ } else {
7095
+ await this.agent.reject(actor, address.toolCallId, void 0, {
7096
+ via: A2UI_VIA
7097
+ });
7098
+ }
7099
+ await this.stream(res, address.stream, {
7100
+ skip: address.position,
7101
+ answered: [
7102
+ address.toolCallId
7103
+ ]
7104
+ });
7105
+ return;
7106
+ }
7107
+ const message = typeof body.message === "string" ? body.message : "";
7108
+ const text = action !== void 0 ? uiActionText2(action) : message;
7109
+ if (text.trim().length === 0) {
7110
+ throw invalid2("no_user_message", "send a message or an action");
7111
+ }
7112
+ const threadId = typeof body.threadId === "string" && body.threadId.length > 0 ? body.threadId : void 0;
7113
+ const owner = threadId !== void 0 ? await this.agent.threadOwner(threadId) : null;
7114
+ if (owner !== null && owner !== actor.id) {
7115
+ throw new ForbiddenException5("thread belongs to another actor");
7116
+ }
7117
+ let uiCapabilities;
7118
+ if (Object.hasOwn(body, "uiCapabilities")) {
7119
+ try {
7120
+ uiCapabilities = validateUiCapabilities2(body.uiCapabilities);
7121
+ } catch (error) {
7122
+ throw invalid2("invalid_input", error.message);
7123
+ }
7124
+ }
7125
+ const started = await this.agent.chat({
7126
+ actor,
7127
+ message: text,
7128
+ ...threadId === void 0 ? {} : owner !== null ? {
7129
+ threadId
7130
+ } : {
7131
+ newThreadId: threadId
7132
+ },
7133
+ ...agentName !== void 0 ? {
7134
+ agentName
7135
+ } : {},
7136
+ ...uiCapabilities !== void 0 ? {
7137
+ uiCapabilities
7138
+ } : {}
7139
+ });
7140
+ await this.stream(res, started.runId, {
7141
+ threadId: started.threadId
7142
+ });
7143
+ }
7144
+ /** Open the JSON Lines response. */
7145
+ writeHead(res, extra) {
7146
+ res.status(200);
7147
+ res.setHeader("Content-Type", "application/jsonl; charset=utf-8");
7148
+ res.setHeader("Cache-Control", "no-cache, no-transform");
7149
+ res.setHeader("Connection", "keep-alive");
7150
+ res.setHeader("X-Accel-Buffering", "no");
7151
+ for (const [name, value] of Object.entries(extra)) res.setHeader(name, value);
7152
+ res.flushHeaders();
7153
+ }
7154
+ /** Write one run as A2UI JSON Lines, until it ends or stops to ask. */
7155
+ async stream(res, runId, options) {
7156
+ this.writeHead(res, {
7157
+ "X-Agent-Run-Id": runId,
7158
+ ...options.threadId !== void 0 ? {
7159
+ "X-Agent-Thread-Id": options.threadId
7160
+ } : {}
7161
+ });
7162
+ const projector = new A2uiProjector(this.projection);
7163
+ const write = /* @__PURE__ */ __name((messages) => {
7164
+ for (const message of messages) res.write(`${JSON.stringify(message)}
7165
+ `);
7166
+ }, "write");
7167
+ try {
7168
+ const frames = agUiFramesFromNdjson2(this.agent.subscribe(runId));
7169
+ for await (const event of agUiEvents2(frames, {
7170
+ threadId: options.threadId ?? runId,
7171
+ runId,
7172
+ streamRunId: runId,
7173
+ ...options.threadId !== void 0 ? {
7174
+ streamThreadId: options.threadId
7175
+ } : {},
7176
+ ...options.skip !== void 0 ? {
7177
+ skip: options.skip
7178
+ } : {},
7179
+ ...options.answered !== void 0 ? {
7180
+ answered: options.answered
7181
+ } : {},
7182
+ ...this.quietMs !== void 0 ? {
7183
+ quietMs: this.quietMs
7184
+ } : {}
7185
+ })) {
7186
+ write(projector.project(event));
7187
+ }
7188
+ } catch {
7189
+ write(projector.project({
7190
+ type: "RUN_ERROR",
7191
+ message: "The run could not be followed."
7192
+ }));
7193
+ }
7194
+ res.end();
7195
+ }
7196
+ };
7197
+ function a2uiAdapter(options = {}) {
7198
+ const path = (options.path ?? "a2ui").replace(/^\/+|\/+$/g, "");
7199
+ const { path: _path, quietMs, maxActionBytes, ...projection } = options;
7200
+ let A2uiController = class A2uiController extends A2uiRunHandler {
7201
+ static {
7202
+ __name(this, "A2uiController");
7203
+ }
7204
+ constructor(agent, actorResolver, catalog) {
7205
+ super(agent, actorResolver, projection.catalog === void 0 && catalog !== void 0 ? {
7206
+ ...projection,
7207
+ catalog
7208
+ } : projection, quietMs, maxActionBytes);
7209
+ }
7210
+ run(req, res, body) {
7211
+ return this.handle(req, res, body);
7212
+ }
7213
+ };
7214
+ _ts_decorate29([
7215
+ Post9(path),
7216
+ HttpCode3(200),
7217
+ _ts_param27(0, Req15()),
7218
+ _ts_param27(1, Res4()),
7219
+ _ts_param27(2, Body8()),
7220
+ _ts_metadata27("design:type", Function),
7221
+ _ts_metadata27("design:paramtypes", [
7222
+ typeof Request === "undefined" ? Object : Request,
7223
+ typeof Response === "undefined" ? Object : Response,
7224
+ Object
7225
+ ]),
7226
+ _ts_metadata27("design:returntype", typeof Promise === "undefined" ? Object : Promise)
7227
+ ], A2uiController.prototype, "run", null);
7228
+ A2uiController = _ts_decorate29([
7229
+ Controller16(),
7230
+ _ts_param27(0, Inject27(AGENT_SERVICE)),
7231
+ _ts_param27(1, Inject27(AGENT_ACTOR_RESOLVER16)),
7232
+ _ts_param27(2, Optional12()),
7233
+ _ts_param27(2, Inject27(GENUI_CATALOG)),
7234
+ _ts_metadata27("design:type", Function),
7235
+ _ts_metadata27("design:paramtypes", [
7236
+ typeof AgentService === "undefined" ? Object : AgentService,
7237
+ typeof ActorResolver === "undefined" ? Object : ActorResolver,
7238
+ typeof Catalog === "undefined" ? Object : Catalog
7239
+ ])
7240
+ ], A2uiController);
7241
+ return {
7242
+ name: "a2ui",
7243
+ controllers: [
7244
+ A2uiController
7245
+ ]
7246
+ };
7247
+ }
7248
+ __name(a2uiAdapter, "a2uiAdapter");
6931
7249
 
6932
7250
  // src/resolver/header-actor-resolver.ts
6933
7251
  import { UnauthorizedException } from "@nestjs/common";
@@ -7006,8 +7324,10 @@ __name(requestUserActorResolver, "requestUserActorResolver");
7006
7324
  // src/index.ts
7007
7325
  export * from "@dudousxd/nestjs-agent-core";
7008
7326
  export {
7327
+ A2uiRunHandler,
7009
7328
  AGENT_CHAT_QUEUE,
7010
7329
  AGENT_METADATA,
7330
+ AGENT_SERVICE,
7011
7331
  AGENT_TOOL_BRAND,
7012
7332
  AI_TOOL_METADATA,
7013
7333
  ATTACHMENT_PAGE_SIZE,
@@ -7020,7 +7340,7 @@ export {
7020
7340
  AgentDepsFactory2 as AgentDepsFactory,
7021
7341
  AgentDiscoveryService,
7022
7342
  AgentModule,
7023
- AgentService,
7343
+ AgentService2 as AgentService,
7024
7344
  AiTool,
7025
7345
  AiToolDiscoveryService,
7026
7346
  AnonymousActorResolver,
@@ -7049,6 +7369,7 @@ export {
7049
7369
  SystemPrompt,
7050
7370
  SystemPromptContributor,
7051
7371
  ToolsController,
7372
+ a2uiAdapter,
7052
7373
  agUiAdapter,
7053
7374
  attachmentLimits,
7054
7375
  declaredSkillProvider,