@dudousxd/nestjs-agent 1.27.1 → 1.29.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,
@@ -6504,7 +6520,9 @@ AgentModule = _ts_decorate27([
6504
6520
 
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";
6523
+ import { A2UI_LEGACY_BASIC_CATALOG_ID, negotiateA2uiCatalog, readAgUiA2uiCatalogIds } from "@dudousxd/nestjs-agent-core/a2ui";
6507
6524
  import { AG_UI_CUSTOM, actionProposalDecisionEvents, agUiEvents, agUiFramesFromNdjson, agUiSse, parseRunInput, planResume, readAnswersPayload, readApprovalPayload, readContext, readForwardedProps, readUserTurn } from "@dudousxd/nestjs-agent-core/ag-ui";
6525
+ import { uiActionText } from "@dudousxd/nestjs-agent-core/genui";
6508
6526
  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
6527
  function _ts_decorate28(decorators, target, key, desc) {
6510
6528
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -6560,12 +6578,14 @@ var AgUiRunHandler = class {
6560
6578
  options;
6561
6579
  staging;
6562
6580
  quietMs;
6563
- constructor(agent, actorResolver, options, staging, quietMs) {
6581
+ a2ui;
6582
+ constructor(agent, actorResolver, options, staging, quietMs, a2ui = void 0) {
6564
6583
  this.agent = agent;
6565
6584
  this.actorResolver = actorResolver;
6566
6585
  this.options = options;
6567
6586
  this.staging = staging;
6568
6587
  this.quietMs = quietMs;
6588
+ this.a2ui = a2ui;
6569
6589
  }
6570
6590
  async handle(req, res, body) {
6571
6591
  const input = parseRunInput(body);
@@ -6614,12 +6634,22 @@ var AgUiRunHandler = class {
6614
6634
  skip: first.address.position,
6615
6635
  answered: plan.decisions.map((decision) => decision.address.toolCallId),
6616
6636
  ...quiet,
6637
+ ...this.a2uiFor(input),
6617
6638
  preamble: warningEvents(warnings)
6618
6639
  });
6619
6640
  return;
6620
6641
  }
6621
6642
  }
6622
- const turn = readUserTurn(input.messages);
6643
+ const action = forwarded.uiAction;
6644
+ if (typeof action === "string") {
6645
+ throw invalid("invalid_ui_action", action);
6646
+ }
6647
+ const turn = action !== void 0 ? {
6648
+ text: uiActionText(action),
6649
+ media: [],
6650
+ staged: [],
6651
+ dropped: []
6652
+ } : readUserTurn(input.messages);
6623
6653
  if (turn === null) {
6624
6654
  throw invalid(NO_USER_MESSAGE_CODE, "messages carries no user message to answer");
6625
6655
  }
@@ -6631,7 +6661,7 @@ var AgUiRunHandler = class {
6631
6661
  if (forwarded.regenerate === true && owner === null) {
6632
6662
  throw invalid("invalid_input", "regenerate needs a thread that already has an exchange");
6633
6663
  }
6634
- if (owner !== null && forwarded.regenerate !== true && turn.media.length === 0 && turn.staged.length === 0) {
6664
+ if (owner !== null && forwarded.regenerate !== true && action === void 0 && turn.media.length === 0 && turn.staged.length === 0) {
6635
6665
  const decision = await this.agent.handleTextDecision(input.threadId, actor, turn.text);
6636
6666
  if ("proposalDecision" in decision) {
6637
6667
  this.writeEvents(res, [
@@ -6693,6 +6723,7 @@ var AgUiRunHandler = class {
6693
6723
  streamRunId: started.runId,
6694
6724
  streamThreadId: started.threadId,
6695
6725
  ...quiet,
6726
+ ...this.a2uiFor(input),
6696
6727
  preamble: warningEvents(warnings)
6697
6728
  });
6698
6729
  }
@@ -6839,6 +6870,13 @@ var AgUiRunHandler = class {
6839
6870
  for (const event of events) res.write(agUiSse(event));
6840
6871
  res.end();
6841
6872
  }
6873
+ /** A2UI for this request's client: the basic catalog under the id it advertises. Off when off. */
6874
+ a2uiFor(input) {
6875
+ if (this.a2ui === void 0) return {};
6876
+ return {
6877
+ a2ui: negotiateA2uiCatalog(this.a2ui, readAgUiA2uiCatalogIds(input), A2UI_LEGACY_BASIC_CATALOG_ID)
6878
+ };
6879
+ }
6842
6880
  /**
6843
6881
  * Pipe a run to the client as AG-UI 1.0 over SSE: one event per `data:` line, from `RUN_STARTED`
6844
6882
  * to the run's terminal event. It ends when the run stops to ask — the library run stays parked,
@@ -6880,12 +6918,13 @@ var AgUiRunHandler = class {
6880
6918
  function agUiAdapter(options = {}) {
6881
6919
  const path = (options.path ?? "ag-ui").replace(/^\/+|\/+$/g, "");
6882
6920
  const quietMs = options.quietMs;
6921
+ const a2ui = options.a2ui === void 0 || options.a2ui === false ? void 0 : options.a2ui === true ? {} : options.a2ui;
6883
6922
  let AgUiController = class AgUiController extends AgUiRunHandler {
6884
6923
  static {
6885
6924
  __name(this, "AgUiController");
6886
6925
  }
6887
- constructor(agent, actorResolver, moduleOptions, staging) {
6888
- super(agent, actorResolver, moduleOptions, staging, quietMs);
6926
+ constructor(agent, actorResolver, moduleOptions, staging, catalog) {
6927
+ super(agent, actorResolver, moduleOptions, staging, quietMs, a2ui !== void 0 ? withCatalog(a2ui, catalog) : void 0);
6889
6928
  }
6890
6929
  run(req, res, body) {
6891
6930
  return this.handle(req, res, body);
@@ -6907,17 +6946,20 @@ function agUiAdapter(options = {}) {
6907
6946
  ], AgUiController.prototype, "run", null);
6908
6947
  AgUiController = _ts_decorate28([
6909
6948
  Controller15(),
6910
- _ts_param26(0, Inject26(AgentService)),
6949
+ _ts_param26(0, Inject26(AgentService2)),
6911
6950
  _ts_param26(1, Inject26(AGENT_ACTOR_RESOLVER15)),
6912
6951
  _ts_param26(2, Inject26(AGENT_OPTIONS11)),
6913
6952
  _ts_param26(3, Optional11()),
6914
6953
  _ts_param26(3, Inject26(AGENT_ATTACHMENT_STAGING5)),
6954
+ _ts_param26(4, Optional11()),
6955
+ _ts_param26(4, Inject26(GENUI_CATALOG)),
6915
6956
  _ts_metadata26("design:type", Function),
6916
6957
  _ts_metadata26("design:paramtypes", [
6917
- typeof AgentService === "undefined" ? Object : AgentService,
6958
+ typeof AgentService2 === "undefined" ? Object : AgentService2,
6918
6959
  typeof ActorResolver === "undefined" ? Object : ActorResolver,
6919
6960
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
6920
- typeof AttachmentStagingStore === "undefined" ? Object : AttachmentStagingStore
6961
+ typeof AttachmentStagingStore === "undefined" ? Object : AttachmentStagingStore,
6962
+ typeof Catalog === "undefined" ? Object : Catalog
6921
6963
  ])
6922
6964
  ], AgUiController);
6923
6965
  return {
@@ -6928,6 +6970,359 @@ function agUiAdapter(options = {}) {
6928
6970
  };
6929
6971
  }
6930
6972
  __name(agUiAdapter, "agUiAdapter");
6973
+ function withCatalog(options, catalog) {
6974
+ if (options.catalog !== void 0 || catalog === void 0) return options;
6975
+ return {
6976
+ ...options,
6977
+ catalog
6978
+ };
6979
+ }
6980
+ __name(withCatalog, "withCatalog");
6981
+
6982
+ // src/a2ui/a2ui.adapter.ts
6983
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER16 } from "@dudousxd/nestjs-agent-core";
6984
+ import { A2UI_APPROVE_ACTION, A2UI_REJECT_ACTION, A2uiProjector, a2uiThreadReplay, negotiateA2uiCatalog as negotiateA2uiCatalog2, readA2uiAction, readA2uiClientCapabilities } from "@dudousxd/nestjs-agent-core/a2ui";
6985
+ import { agUiEvents as agUiEvents2, agUiFramesFromNdjson as agUiFramesFromNdjson2, decodeInterruptId } from "@dudousxd/nestjs-agent-core/ag-ui";
6986
+ import { uiActionText as uiActionText2, validateUiCapabilities as validateUiCapabilities2 } from "@dudousxd/nestjs-agent-core/genui";
6987
+ import { BadRequestException as BadRequestException9, Body as Body8, Controller as Controller16, ForbiddenException as ForbiddenException5, Get as Get13, HttpCode as HttpCode3, Inject as Inject27, Optional as Optional12, Param as Param7, Post as Post9, Query as Query7, Req as Req15, Res as Res4 } from "@nestjs/common";
6988
+ function _ts_decorate29(decorators, target, key, desc) {
6989
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
6990
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
6991
+ r = Reflect.decorate(decorators, target, key, desc);
6992
+ } else {
6993
+ for (var i = decorators.length - 1; i >= 0; i--) {
6994
+ if (d = decorators[i]) {
6995
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6996
+ }
6997
+ }
6998
+ }
6999
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7000
+ }
7001
+ __name(_ts_decorate29, "_ts_decorate");
7002
+ function _ts_metadata27(metadataKey, metadataValue) {
7003
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") {
7004
+ return Reflect.metadata(metadataKey, metadataValue);
7005
+ }
7006
+ }
7007
+ __name(_ts_metadata27, "_ts_metadata");
7008
+ function _ts_param27(paramIndex, decorator) {
7009
+ return function(target, key) {
7010
+ decorator(target, key, paramIndex);
7011
+ };
7012
+ }
7013
+ __name(_ts_param27, "_ts_param");
7014
+ var A2UI_VIA = "a2ui";
7015
+ function isRecord(value) {
7016
+ return typeof value === "object" && value !== null && !Array.isArray(value);
7017
+ }
7018
+ __name(isRecord, "isRecord");
7019
+ function invalid2(code, message) {
7020
+ return new BadRequestException9({
7021
+ statusCode: 400,
7022
+ code,
7023
+ message
7024
+ });
7025
+ }
7026
+ __name(invalid2, "invalid");
7027
+ function bodyField(body, key) {
7028
+ if (body[key] !== void 0) return body[key];
7029
+ return isRecord(body.metadata) ? body.metadata[key] : void 0;
7030
+ }
7031
+ __name(bodyField, "bodyField");
7032
+ function readDataModel(raw, maxBytes) {
7033
+ if (raw === void 0) return void 0;
7034
+ if (!isRecord(raw) || !isRecord(raw.surfaces)) {
7035
+ return "a2uiClientDataModel carries { version, surfaces }";
7036
+ }
7037
+ let size;
7038
+ try {
7039
+ size = new TextEncoder().encode(JSON.stringify(raw.surfaces)).length;
7040
+ } catch {
7041
+ return "a2uiClientDataModel must be JSON";
7042
+ }
7043
+ if (size > maxBytes) {
7044
+ return `a2uiClientDataModel may be at most ${maxBytes} bytes (this one is ${size})`;
7045
+ }
7046
+ return Object.keys(raw.surfaces).length > 0 ? raw.surfaces : void 0;
7047
+ }
7048
+ __name(readDataModel, "readDataModel");
7049
+ var A2uiRunHandler = class {
7050
+ static {
7051
+ __name(this, "A2uiRunHandler");
7052
+ }
7053
+ agent;
7054
+ actorResolver;
7055
+ projection;
7056
+ quietMs;
7057
+ maxActionBytes;
7058
+ maxDataModelBytes;
7059
+ constructor(agent, actorResolver, projection, quietMs, maxActionBytes, maxDataModelBytes = void 0) {
7060
+ this.agent = agent;
7061
+ this.actorResolver = actorResolver;
7062
+ this.projection = projection;
7063
+ this.quietMs = quietMs;
7064
+ this.maxActionBytes = maxActionBytes;
7065
+ this.maxDataModelBytes = maxDataModelBytes;
7066
+ }
7067
+ /**
7068
+ * `GET <path>/a2ui/threads/:threadId`: a stored thread as `{ threadId, entries }`
7069
+ * (`a2uiThreadReplay`) — what a client draws again after a reload. Owner-scoped; a thread nobody
7070
+ * has answers `{ entries: [] }`. `?catalog=<id>` names the client's basic catalog id.
7071
+ */
7072
+ async replay(req, threadId, catalog) {
7073
+ const actor = await this.actorResolver.resolve(req);
7074
+ const owner = await this.agent.threadOwner(threadId);
7075
+ if (owner === null) return {
7076
+ threadId,
7077
+ entries: []
7078
+ };
7079
+ const thread = await this.agent.getThread(actor, threadId);
7080
+ const supported = typeof catalog === "string" ? [
7081
+ catalog
7082
+ ] : Array.isArray(catalog) ? catalog.filter((id) => typeof id === "string") : void 0;
7083
+ return {
7084
+ threadId,
7085
+ entries: a2uiThreadReplay(thread?.messages ?? [], negotiateA2uiCatalog2(this.projection, supported))
7086
+ };
7087
+ }
7088
+ async handle(req, res, raw) {
7089
+ const body = isRecord(raw) ? raw : {};
7090
+ const agentName = typeof body.agent === "string" && body.agent.length > 0 ? body.agent : void 0;
7091
+ const actor = await this.actorResolver.resolve(req);
7092
+ const projection = negotiateA2uiCatalog2(this.projection, readA2uiClientCapabilities(bodyField(body, "a2uiClientCapabilities")));
7093
+ const dataModel = readDataModel(bodyField(body, "a2uiClientDataModel"), this.maxDataModelBytes ?? 32768);
7094
+ if (typeof dataModel === "string") throw invalid2("invalid_data_model", dataModel);
7095
+ let action;
7096
+ if (body.action !== void 0) {
7097
+ const read = isRecord(body.action) ? readA2uiAction(body.action, {
7098
+ ...this.maxActionBytes !== void 0 ? {
7099
+ maxBytes: this.maxActionBytes
7100
+ } : {}
7101
+ }) : "action must be an A2UI action message";
7102
+ if (typeof read === "string") throw invalid2("invalid_action", read);
7103
+ action = read;
7104
+ }
7105
+ if (action?.name === A2UI_APPROVE_ACTION || action?.name === A2UI_REJECT_ACTION) {
7106
+ const interruptId = action.context.interruptId;
7107
+ const address = typeof interruptId === "string" ? decodeInterruptId(interruptId) : void 0;
7108
+ if (address === void 0 || address === null || address.kind === "elicitation") {
7109
+ throw invalid2("invalid_action", "an approval action carries the interruptId it decides");
7110
+ }
7111
+ if (address.kind === "proposal") {
7112
+ if (address.proposalId === void 0 || address.threadId === void 0) {
7113
+ throw invalid2("invalid_action", "the interruptId names no proposal");
7114
+ }
7115
+ const decided = await this.agent.decideActionProposal(actor, address.threadId, address.proposalId, {
7116
+ decision: action.name === A2UI_APPROVE_ACTION ? "approved" : "rejected",
7117
+ via: A2UI_VIA
7118
+ });
7119
+ this.writeHead(res, {
7120
+ "X-Agent-Thread-Id": address.threadId
7121
+ });
7122
+ const projector = new A2uiProjector(projection);
7123
+ const messageId = `proposal-${address.proposalId}`;
7124
+ for (const event of [
7125
+ {
7126
+ type: "TEXT_MESSAGE_START",
7127
+ messageId,
7128
+ role: "assistant"
7129
+ },
7130
+ {
7131
+ type: "TEXT_MESSAGE_CONTENT",
7132
+ messageId,
7133
+ delta: decided.text
7134
+ }
7135
+ ]) {
7136
+ for (const message2 of projector.project(event)) res.write(`${JSON.stringify(message2)}
7137
+ `);
7138
+ }
7139
+ res.end();
7140
+ return;
7141
+ }
7142
+ await this.agent.assertResumable(actor, address.stream);
7143
+ await this.agent.checkDecision(actor, address.toolCallId);
7144
+ if (action.name === A2UI_APPROVE_ACTION) {
7145
+ await this.agent.approve(actor, address.toolCallId, {
7146
+ via: A2UI_VIA
7147
+ });
7148
+ } else {
7149
+ await this.agent.reject(actor, address.toolCallId, void 0, {
7150
+ via: A2UI_VIA
7151
+ });
7152
+ }
7153
+ await this.stream(res, address.stream, {
7154
+ skip: address.position,
7155
+ answered: [
7156
+ address.toolCallId
7157
+ ],
7158
+ projection
7159
+ });
7160
+ return;
7161
+ }
7162
+ const message = typeof body.message === "string" ? body.message : "";
7163
+ const text = action !== void 0 ? uiActionText2(action) : message;
7164
+ if (text.trim().length === 0) {
7165
+ throw invalid2("no_user_message", "send a message or an action");
7166
+ }
7167
+ const threadId = typeof body.threadId === "string" && body.threadId.length > 0 ? body.threadId : void 0;
7168
+ const owner = threadId !== void 0 ? await this.agent.threadOwner(threadId) : null;
7169
+ if (owner !== null && owner !== actor.id) {
7170
+ throw new ForbiddenException5("thread belongs to another actor");
7171
+ }
7172
+ let uiCapabilities;
7173
+ if (Object.hasOwn(body, "uiCapabilities")) {
7174
+ try {
7175
+ uiCapabilities = validateUiCapabilities2(body.uiCapabilities);
7176
+ } catch (error) {
7177
+ throw invalid2("invalid_input", error.message);
7178
+ }
7179
+ }
7180
+ const started = await this.agent.chat({
7181
+ actor,
7182
+ message: text,
7183
+ ...threadId === void 0 ? {} : owner !== null ? {
7184
+ threadId
7185
+ } : {
7186
+ newThreadId: threadId
7187
+ },
7188
+ ...agentName !== void 0 ? {
7189
+ agentName
7190
+ } : {},
7191
+ ...uiCapabilities !== void 0 ? {
7192
+ uiCapabilities
7193
+ } : {},
7194
+ ...dataModel !== void 0 ? {
7195
+ pageContext: {
7196
+ a2uiDataModel: dataModel
7197
+ }
7198
+ } : {}
7199
+ });
7200
+ await this.stream(res, started.runId, {
7201
+ threadId: started.threadId,
7202
+ projection
7203
+ });
7204
+ }
7205
+ /** Open the JSON Lines response. */
7206
+ writeHead(res, extra) {
7207
+ res.status(200);
7208
+ res.setHeader("Content-Type", "application/jsonl; charset=utf-8");
7209
+ res.setHeader("Cache-Control", "no-cache, no-transform");
7210
+ res.setHeader("Connection", "keep-alive");
7211
+ res.setHeader("X-Accel-Buffering", "no");
7212
+ for (const [name, value] of Object.entries(extra)) res.setHeader(name, value);
7213
+ res.flushHeaders();
7214
+ }
7215
+ /** Write one run as A2UI JSON Lines, until it ends or stops to ask. */
7216
+ async stream(res, runId, options) {
7217
+ this.writeHead(res, {
7218
+ "X-Agent-Run-Id": runId,
7219
+ ...options.threadId !== void 0 ? {
7220
+ "X-Agent-Thread-Id": options.threadId
7221
+ } : {}
7222
+ });
7223
+ const projector = new A2uiProjector(options.projection ?? this.projection);
7224
+ const write = /* @__PURE__ */ __name((messages) => {
7225
+ for (const message of messages) res.write(`${JSON.stringify(message)}
7226
+ `);
7227
+ }, "write");
7228
+ try {
7229
+ const frames = agUiFramesFromNdjson2(this.agent.subscribe(runId));
7230
+ for await (const event of agUiEvents2(frames, {
7231
+ threadId: options.threadId ?? runId,
7232
+ runId,
7233
+ streamRunId: runId,
7234
+ ...options.threadId !== void 0 ? {
7235
+ streamThreadId: options.threadId
7236
+ } : {},
7237
+ ...options.skip !== void 0 ? {
7238
+ skip: options.skip
7239
+ } : {},
7240
+ ...options.answered !== void 0 ? {
7241
+ answered: options.answered
7242
+ } : {},
7243
+ ...this.quietMs !== void 0 ? {
7244
+ quietMs: this.quietMs
7245
+ } : {}
7246
+ })) {
7247
+ write(projector.project(event));
7248
+ }
7249
+ } catch {
7250
+ write(projector.project({
7251
+ type: "RUN_ERROR",
7252
+ message: "The run could not be followed."
7253
+ }));
7254
+ }
7255
+ res.end();
7256
+ }
7257
+ };
7258
+ function a2uiAdapter(options = {}) {
7259
+ const path = (options.path ?? "a2ui").replace(/^\/+|\/+$/g, "");
7260
+ const { path: _path, quietMs, maxActionBytes, maxDataModelBytes, ...projection } = options;
7261
+ let A2uiController = class A2uiController extends A2uiRunHandler {
7262
+ static {
7263
+ __name(this, "A2uiController");
7264
+ }
7265
+ constructor(agent, actorResolver, catalog) {
7266
+ super(agent, actorResolver, projection.catalog === void 0 && catalog !== void 0 ? {
7267
+ ...projection,
7268
+ catalog
7269
+ } : projection, quietMs, maxActionBytes, maxDataModelBytes);
7270
+ }
7271
+ thread(req, threadId, catalog) {
7272
+ return this.replay(req, threadId, catalog);
7273
+ }
7274
+ run(req, res, body) {
7275
+ return this.handle(req, res, body);
7276
+ }
7277
+ };
7278
+ _ts_decorate29([
7279
+ Get13(`${path}/threads/:threadId`),
7280
+ _ts_param27(0, Req15()),
7281
+ _ts_param27(1, Param7("threadId")),
7282
+ _ts_param27(2, Query7("catalog")),
7283
+ _ts_metadata27("design:type", Function),
7284
+ _ts_metadata27("design:paramtypes", [
7285
+ typeof Request === "undefined" ? Object : Request,
7286
+ String,
7287
+ Object
7288
+ ]),
7289
+ _ts_metadata27("design:returntype", typeof ReturnType === "undefined" ? Object : ReturnType)
7290
+ ], A2uiController.prototype, "thread", null);
7291
+ _ts_decorate29([
7292
+ Post9(path),
7293
+ HttpCode3(200),
7294
+ _ts_param27(0, Req15()),
7295
+ _ts_param27(1, Res4()),
7296
+ _ts_param27(2, Body8()),
7297
+ _ts_metadata27("design:type", Function),
7298
+ _ts_metadata27("design:paramtypes", [
7299
+ typeof Request === "undefined" ? Object : Request,
7300
+ typeof Response === "undefined" ? Object : Response,
7301
+ Object
7302
+ ]),
7303
+ _ts_metadata27("design:returntype", typeof Promise === "undefined" ? Object : Promise)
7304
+ ], A2uiController.prototype, "run", null);
7305
+ A2uiController = _ts_decorate29([
7306
+ Controller16(),
7307
+ _ts_param27(0, Inject27(AGENT_SERVICE)),
7308
+ _ts_param27(1, Inject27(AGENT_ACTOR_RESOLVER16)),
7309
+ _ts_param27(2, Optional12()),
7310
+ _ts_param27(2, Inject27(GENUI_CATALOG)),
7311
+ _ts_metadata27("design:type", Function),
7312
+ _ts_metadata27("design:paramtypes", [
7313
+ typeof AgentService === "undefined" ? Object : AgentService,
7314
+ typeof ActorResolver === "undefined" ? Object : ActorResolver,
7315
+ typeof Catalog === "undefined" ? Object : Catalog
7316
+ ])
7317
+ ], A2uiController);
7318
+ return {
7319
+ name: "a2ui",
7320
+ controllers: [
7321
+ A2uiController
7322
+ ]
7323
+ };
7324
+ }
7325
+ __name(a2uiAdapter, "a2uiAdapter");
6931
7326
 
6932
7327
  // src/resolver/header-actor-resolver.ts
6933
7328
  import { UnauthorizedException } from "@nestjs/common";
@@ -7006,8 +7401,10 @@ __name(requestUserActorResolver, "requestUserActorResolver");
7006
7401
  // src/index.ts
7007
7402
  export * from "@dudousxd/nestjs-agent-core";
7008
7403
  export {
7404
+ A2uiRunHandler,
7009
7405
  AGENT_CHAT_QUEUE,
7010
7406
  AGENT_METADATA,
7407
+ AGENT_SERVICE,
7011
7408
  AGENT_TOOL_BRAND,
7012
7409
  AI_TOOL_METADATA,
7013
7410
  ATTACHMENT_PAGE_SIZE,
@@ -7020,7 +7417,7 @@ export {
7020
7417
  AgentDepsFactory2 as AgentDepsFactory,
7021
7418
  AgentDiscoveryService,
7022
7419
  AgentModule,
7023
- AgentService,
7420
+ AgentService2 as AgentService,
7024
7421
  AiTool,
7025
7422
  AiToolDiscoveryService,
7026
7423
  AnonymousActorResolver,
@@ -7049,6 +7446,7 @@ export {
7049
7446
  SystemPrompt,
7050
7447
  SystemPromptContributor,
7051
7448
  ToolsController,
7449
+ a2uiAdapter,
7052
7450
  agUiAdapter,
7053
7451
  attachmentLimits,
7054
7452
  declaredSkillProvider,