@dudousxd/nestjs-agent 0.3.3 → 0.4.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.
@@ -111,10 +111,12 @@ var AgentRunWorkflow = class _AgentRunWorkflow {
111
111
  }, "runAgent")
112
112
  };
113
113
  try {
114
- return await (0, import_nestjs_agent_core.runAgentLoop)({
114
+ const result = await (0, import_nestjs_agent_core.runAgentLoop)({
115
115
  ...deps,
116
116
  day
117
117
  }, input, hooks);
118
+ await ctx.step("deactivate", () => this.store.setActiveStream(input.threadId, null));
119
+ return result;
118
120
  } catch (error) {
119
121
  if (error instanceof import_nestjs_durable_core.WorkflowSuspended || error instanceof import_nestjs_durable_core.ContinueAsNew) {
120
122
  throw error;
@@ -126,6 +128,7 @@ var AgentRunWorkflow = class _AgentRunWorkflow {
126
128
  code,
127
129
  message
128
130
  });
131
+ await ctx.step("deactivate", () => this.store.setActiveStream(input.threadId, null));
129
132
  const writer = await hooks.openSink();
130
133
  await writer.fail({
131
134
  code,
@@ -251,8 +254,8 @@ AgentDurableModule = _ts_decorate3([
251
254
  ], AgentDurableModule);
252
255
 
253
256
  // src/agent.module.ts
254
- var import_nestjs_agent_core15 = require("@dudousxd/nestjs-agent-core");
255
- var import_common14 = require("@nestjs/common");
257
+ var import_nestjs_agent_core16 = require("@dudousxd/nestjs-agent-core");
258
+ var import_common15 = require("@nestjs/common");
256
259
  var import_core3 = require("@nestjs/core");
257
260
 
258
261
  // src/agent-deps.factory.ts
@@ -293,7 +296,8 @@ var AgentDepsFactory2 = class {
293
296
  agents;
294
297
  promptContributors;
295
298
  quota;
296
- constructor(options, model, store, sink, rolesPolicy, registry, agents, promptContributors, quota) {
299
+ pricingStore;
300
+ constructor(options, model, store, sink, rolesPolicy, registry, agents, promptContributors, quota, pricingStore) {
297
301
  this.options = options;
298
302
  this.model = model;
299
303
  this.store = store;
@@ -303,6 +307,7 @@ var AgentDepsFactory2 = class {
303
307
  this.agents = agents;
304
308
  this.promptContributors = promptContributors;
305
309
  this.quota = quota;
310
+ this.pricingStore = pricingStore;
306
311
  }
307
312
  /**
308
313
  * The agent a turn uses when the caller names none: the explicit `defaultAgent` option, else the
@@ -349,6 +354,9 @@ var AgentDepsFactory2 = class {
349
354
  ...this.quota !== void 0 ? {
350
355
  quota: this.quota
351
356
  } : {},
357
+ ...this.pricingStore !== void 0 ? {
358
+ pricingStore: this.pricingStore
359
+ } : {},
352
360
  ...toolAllowList !== void 0 ? {
353
361
  toolAllowList
354
362
  } : {},
@@ -389,6 +397,8 @@ AgentDepsFactory2 = _ts_decorate4([
389
397
  _ts_param2(6, (0, import_common4.Inject)(import_nestjs_agent_core3.AGENT_REGISTRY)),
390
398
  _ts_param2(7, (0, import_common4.Inject)(import_nestjs_agent_core3.AGENT_PROMPT_CONTRIBUTORS)),
391
399
  _ts_param2(8, (0, import_common4.Inject)(import_nestjs_agent_core3.AGENT_QUOTA_STORE)),
400
+ _ts_param2(9, (0, import_common4.Optional)()),
401
+ _ts_param2(9, (0, import_common4.Inject)(import_nestjs_agent_core3.AGENT_PRICING_STORE)),
392
402
  _ts_metadata3("design:type", Function),
393
403
  _ts_metadata3("design:paramtypes", [
394
404
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
@@ -399,6 +409,7 @@ AgentDepsFactory2 = _ts_decorate4([
399
409
  typeof import_nestjs_agent_core3.ToolRegistry === "undefined" ? Object : import_nestjs_agent_core3.ToolRegistry,
400
410
  typeof import_nestjs_agent_core3.AgentRegistry === "undefined" ? Object : import_nestjs_agent_core3.AgentRegistry,
401
411
  Array,
412
+ Object,
402
413
  Object
403
414
  ])
404
415
  ], AgentDepsFactory2);
@@ -438,7 +449,7 @@ var AgentService = class {
438
449
  this.quota = quota;
439
450
  }
440
451
  async chat(params) {
441
- const agentName = params.agentName ?? this.deps.defaultAgentName();
452
+ const agentName = await this.resolveAgentName(params.agentName, params.threadId);
442
453
  let threadId = params.threadId;
443
454
  if (threadId === void 0) {
444
455
  if (params.regenerate === true) {
@@ -499,12 +510,14 @@ var AgentService = class {
499
510
  await this.assertOwnsActiveStream(actor, runId);
500
511
  return this.runner.cancel(runId);
501
512
  }
502
- listThreads(actorRef) {
503
- return this.store.listThreads(actorRef);
513
+ async listThreads(actorRef) {
514
+ const threads = await this.store.listThreads(actorRef);
515
+ return Promise.all(threads.map((thread) => this.toSummaryView(thread)));
504
516
  }
505
517
  async getThread(actor, threadId) {
506
518
  await this.assertOwnsThread(actor, threadId);
507
- return this.store.getThread(threadId);
519
+ const thread = await this.store.getThread(threadId);
520
+ return thread === null ? null : this.toDetailView(thread);
508
521
  }
509
522
  async deleteThread(actor, threadId) {
510
523
  await this.assertOwnsThread(actor, threadId);
@@ -514,7 +527,38 @@ var AgentService = class {
514
527
  await this.assertOwnsThread(actor, threadId);
515
528
  return this.store.forkThread(threadId, fromMessageId);
516
529
  }
530
+ /** Kept for source compatibility — a thin wrapper over the more general `updateThread`. */
517
531
  async renameThread(actor, threadId, title) {
532
+ return this.updateThread(actor, threadId, {
533
+ title
534
+ });
535
+ }
536
+ /**
537
+ * Rename a thread and/or set its default agent. Title-only patches work against ANY store (via
538
+ * the required `setTitle`); a `defaultAgent` change requires the bound store to implement the
539
+ * optional `updateThread` — 501 with a clear message when it doesn't, rather than silently
540
+ * dropping the field.
541
+ */
542
+ async updateThread(actor, threadId, patch) {
543
+ const title = patch.title !== void 0 ? this.validateTitle(patch.title) : void 0;
544
+ await this.assertOwnsThread(actor, threadId);
545
+ if (patch.defaultAgent === void 0) {
546
+ if (title !== void 0) {
547
+ await this.store.setTitle(threadId, title);
548
+ }
549
+ return;
550
+ }
551
+ if (this.store.updateThread === void 0) {
552
+ throw new import_common5.NotImplementedException("Setting a thread's defaultAgent requires an AgentStore that implements updateThread(); the bound store does not support it.");
553
+ }
554
+ await this.store.updateThread(threadId, {
555
+ ...title !== void 0 ? {
556
+ title
557
+ } : {},
558
+ defaultAgent: patch.defaultAgent
559
+ });
560
+ }
561
+ validateTitle(title) {
518
562
  const trimmed = title.trim();
519
563
  if (trimmed.length === 0) {
520
564
  throw new import_common5.BadRequestException("title must not be empty");
@@ -522,8 +566,41 @@ var AgentService = class {
522
566
  if (trimmed.length > 200) {
523
567
  throw new import_common5.BadRequestException("title must be at most 200 characters");
524
568
  }
525
- await this.assertOwnsThread(actor, threadId);
526
- return this.store.setTitle(threadId, trimmed);
569
+ return trimmed;
570
+ }
571
+ /**
572
+ * Agent-selection precedence for a `chat()` call with no explicit `agentName`: the thread's own
573
+ * `defaultAgent` (if the thread exists and one is set) wins over the module's configured default.
574
+ * Skips the thread lookup entirely when the caller already named an agent or there's no thread yet.
575
+ */
576
+ async resolveAgentName(agentName, threadId) {
577
+ if (agentName !== void 0) {
578
+ return agentName;
579
+ }
580
+ if (threadId !== void 0) {
581
+ const thread = await this.store.getThread(threadId);
582
+ if (thread?.defaultAgent !== void 0 && thread.defaultAgent !== null) {
583
+ return thread.defaultAgent;
584
+ }
585
+ }
586
+ return this.deps.defaultAgentName();
587
+ }
588
+ async toSummaryView(thread) {
589
+ return {
590
+ ...thread,
591
+ defaultAgent: thread.defaultAgent ?? null,
592
+ activeRunId: await this.store.activeRunForThread?.(thread.id) ?? null
593
+ };
594
+ }
595
+ async toDetailView(thread) {
596
+ const summary = await this.toSummaryView(thread);
597
+ return {
598
+ ...summary,
599
+ messages: thread.messages,
600
+ ...thread.activeStreamId !== void 0 ? {
601
+ activeStreamId: thread.activeStreamId
602
+ } : {}
603
+ };
527
604
  }
528
605
  async promoteThread(actor, threadId) {
529
606
  await this.assertOwnsThread(actor, threadId);
@@ -679,9 +756,10 @@ AgentsController = _ts_decorate6([
679
756
  ])
680
757
  ], AgentsController);
681
758
 
682
- // src/controller/chat.controller.ts
759
+ // src/controller/attachments.controller.ts
683
760
  var import_nestjs_agent_core7 = require("@dudousxd/nestjs-agent-core");
684
761
  var import_common7 = require("@nestjs/common");
762
+ var import_platform_express = require("@nestjs/platform-express");
685
763
  function _ts_decorate7(decorators, target, key, desc) {
686
764
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
687
765
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -699,6 +777,110 @@ function _ts_param5(paramIndex, decorator) {
699
777
  };
700
778
  }
701
779
  __name(_ts_param5, "_ts_param");
780
+ var DEFAULT_MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024;
781
+ var DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES = [
782
+ "image/png",
783
+ "image/jpeg",
784
+ "image/gif",
785
+ "image/webp",
786
+ "application/pdf",
787
+ "text/plain",
788
+ "text/csv"
789
+ ];
790
+ var AttachmentsController = class {
791
+ static {
792
+ __name(this, "AttachmentsController");
793
+ }
794
+ options;
795
+ actorResolver;
796
+ staging;
797
+ constructor(options, actorResolver, staging) {
798
+ this.options = options;
799
+ this.actorResolver = actorResolver;
800
+ this.staging = staging;
801
+ }
802
+ /**
803
+ * `attachments.upload: true` is what mounts this controller at all (see `agent.module.ts`), so by
804
+ * the time an instance exists the host clearly intends uploads to work. Fail boot loudly rather
805
+ * than mounting a controller that would 501 on every single request because nobody bound
806
+ * `AGENT_ATTACHMENT_STAGING`.
807
+ */
808
+ onModuleInit() {
809
+ if (this.options.attachments?.upload === true && this.staging === void 0) {
810
+ throw new Error("AgentModule: attachments.upload is true but no AGENT_ATTACHMENT_STAGING provider is bound. Bind one (e.g. `{ provide: AGENT_ATTACHMENT_STAGING, useClass: MyStagingStore }`) in a module imported alongside AgentModule, or set attachments.upload to false.");
811
+ }
812
+ }
813
+ async upload(req, file) {
814
+ if (this.staging === void 0) {
815
+ throw new import_common7.NotImplementedException("Attachment upload is not configured on this server.");
816
+ }
817
+ if (file === void 0) {
818
+ throw new import_common7.BadRequestException('multipart field "file" is required');
819
+ }
820
+ const allowedContentTypes = this.options.attachments?.allowedContentTypes ?? DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES;
821
+ if (!allowedContentTypes.includes(file.mimetype)) {
822
+ throw new import_common7.UnsupportedMediaTypeException(`content type "${file.mimetype}" is not allowed (allowed: ${allowedContentTypes.join(", ")})`);
823
+ }
824
+ const maxBytes = this.options.attachments?.maxBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES;
825
+ if (file.size > maxBytes) {
826
+ throw new import_common7.PayloadTooLargeException(`file exceeds the ${maxBytes}-byte limit`);
827
+ }
828
+ const actor = await this.actorResolver.resolve(req);
829
+ return this.staging.stage({
830
+ data: file.buffer,
831
+ filename: file.originalname,
832
+ contentType: file.mimetype,
833
+ sizeBytes: file.size,
834
+ actor
835
+ });
836
+ }
837
+ };
838
+ _ts_decorate7([
839
+ (0, import_common7.Post)(),
840
+ (0, import_common7.UseInterceptors)((0, import_platform_express.FileInterceptor)("file")),
841
+ _ts_param5(0, (0, import_common7.Req)()),
842
+ _ts_param5(1, (0, import_common7.UploadedFile)()),
843
+ _ts_metadata6("design:type", Function),
844
+ _ts_metadata6("design:paramtypes", [
845
+ typeof Request === "undefined" ? Object : Request,
846
+ Object
847
+ ]),
848
+ _ts_metadata6("design:returntype", Promise)
849
+ ], AttachmentsController.prototype, "upload", null);
850
+ AttachmentsController = _ts_decorate7([
851
+ (0, import_common7.Controller)("attachments"),
852
+ _ts_param5(0, (0, import_common7.Inject)(import_nestjs_agent_core7.AGENT_OPTIONS)),
853
+ _ts_param5(1, (0, import_common7.Inject)(import_nestjs_agent_core7.AGENT_ACTOR_RESOLVER)),
854
+ _ts_param5(2, (0, import_common7.Optional)()),
855
+ _ts_param5(2, (0, import_common7.Inject)(import_nestjs_agent_core7.AGENT_ATTACHMENT_STAGING)),
856
+ _ts_metadata6("design:type", Function),
857
+ _ts_metadata6("design:paramtypes", [
858
+ typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
859
+ typeof ActorResolver === "undefined" ? Object : ActorResolver,
860
+ Object
861
+ ])
862
+ ], AttachmentsController);
863
+
864
+ // src/controller/chat.controller.ts
865
+ var import_nestjs_agent_core8 = require("@dudousxd/nestjs-agent-core");
866
+ var import_common8 = require("@nestjs/common");
867
+ function _ts_decorate8(decorators, target, key, desc) {
868
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
869
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
870
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
871
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
872
+ }
873
+ __name(_ts_decorate8, "_ts_decorate");
874
+ function _ts_metadata7(k, v) {
875
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
876
+ }
877
+ __name(_ts_metadata7, "_ts_metadata");
878
+ function _ts_param6(paramIndex, decorator) {
879
+ return function(target, key) {
880
+ decorator(target, key, paramIndex);
881
+ };
882
+ }
883
+ __name(_ts_param6, "_ts_param");
702
884
  var ChatController = class {
703
885
  static {
704
886
  __name(this, "ChatController");
@@ -786,7 +968,7 @@ data: ${JSON.stringify({
786
968
  }
787
969
  res.write("event: done\ndata: {}\n\n");
788
970
  } catch (error) {
789
- const payload = error instanceof import_nestjs_agent_core7.AgentStreamError ? {
971
+ const payload = error instanceof import_nestjs_agent_core8.AgentStreamError ? {
790
972
  code: error.code,
791
973
  message: error.message
792
974
  } : {
@@ -801,71 +983,71 @@ data: ${JSON.stringify(payload)}
801
983
  res.end();
802
984
  }
803
985
  };
804
- _ts_decorate7([
805
- (0, import_common7.Post)("chat"),
806
- _ts_param5(0, (0, import_common7.Req)()),
807
- _ts_param5(1, (0, import_common7.Res)()),
808
- _ts_param5(2, (0, import_common7.Body)()),
809
- _ts_metadata6("design:type", Function),
810
- _ts_metadata6("design:paramtypes", [
986
+ _ts_decorate8([
987
+ (0, import_common8.Post)("chat"),
988
+ _ts_param6(0, (0, import_common8.Req)()),
989
+ _ts_param6(1, (0, import_common8.Res)()),
990
+ _ts_param6(2, (0, import_common8.Body)()),
991
+ _ts_metadata7("design:type", Function),
992
+ _ts_metadata7("design:paramtypes", [
811
993
  typeof Request === "undefined" ? Object : Request,
812
994
  typeof Response === "undefined" ? Object : Response,
813
995
  typeof ChatBody === "undefined" ? Object : ChatBody
814
996
  ]),
815
- _ts_metadata6("design:returntype", Promise)
997
+ _ts_metadata7("design:returntype", Promise)
816
998
  ], ChatController.prototype, "chat", null);
817
- _ts_decorate7([
818
- (0, import_common7.Get)("chat/:runId/stream"),
819
- _ts_param5(0, (0, import_common7.Param)("runId")),
820
- _ts_param5(1, (0, import_common7.Res)()),
821
- _ts_metadata6("design:type", Function),
822
- _ts_metadata6("design:paramtypes", [
999
+ _ts_decorate8([
1000
+ (0, import_common8.Get)("chat/:runId/stream"),
1001
+ _ts_param6(0, (0, import_common8.Param)("runId")),
1002
+ _ts_param6(1, (0, import_common8.Res)()),
1003
+ _ts_metadata7("design:type", Function),
1004
+ _ts_metadata7("design:paramtypes", [
823
1005
  String,
824
1006
  typeof Response === "undefined" ? Object : Response
825
1007
  ]),
826
- _ts_metadata6("design:returntype", Promise)
1008
+ _ts_metadata7("design:returntype", Promise)
827
1009
  ], ChatController.prototype, "stream", null);
828
- _ts_decorate7([
829
- (0, import_common7.Post)("chat/:runId/cancel"),
830
- _ts_param5(0, (0, import_common7.Req)()),
831
- _ts_param5(1, (0, import_common7.Param)("runId")),
832
- _ts_metadata6("design:type", Function),
833
- _ts_metadata6("design:paramtypes", [
1010
+ _ts_decorate8([
1011
+ (0, import_common8.Post)("chat/:runId/cancel"),
1012
+ _ts_param6(0, (0, import_common8.Req)()),
1013
+ _ts_param6(1, (0, import_common8.Param)("runId")),
1014
+ _ts_metadata7("design:type", Function),
1015
+ _ts_metadata7("design:paramtypes", [
834
1016
  typeof Request === "undefined" ? Object : Request,
835
1017
  String
836
1018
  ]),
837
- _ts_metadata6("design:returntype", Promise)
1019
+ _ts_metadata7("design:returntype", Promise)
838
1020
  ], ChatController.prototype, "cancel", null);
839
- ChatController = _ts_decorate7([
840
- (0, import_common7.Controller)(),
841
- _ts_param5(1, (0, import_common7.Inject)(import_nestjs_agent_core7.AGENT_ACTOR_RESOLVER)),
842
- _ts_metadata6("design:type", Function),
843
- _ts_metadata6("design:paramtypes", [
1021
+ ChatController = _ts_decorate8([
1022
+ (0, import_common8.Controller)(),
1023
+ _ts_param6(1, (0, import_common8.Inject)(import_nestjs_agent_core8.AGENT_ACTOR_RESOLVER)),
1024
+ _ts_metadata7("design:type", Function),
1025
+ _ts_metadata7("design:paramtypes", [
844
1026
  typeof AgentService === "undefined" ? Object : AgentService,
845
1027
  typeof ActorResolver === "undefined" ? Object : ActorResolver
846
1028
  ])
847
1029
  ], ChatController);
848
1030
 
849
1031
  // src/controller/quota.controller.ts
850
- var import_nestjs_agent_core8 = require("@dudousxd/nestjs-agent-core");
851
- var import_common8 = require("@nestjs/common");
852
- function _ts_decorate8(decorators, target, key, desc) {
1032
+ var import_nestjs_agent_core9 = require("@dudousxd/nestjs-agent-core");
1033
+ var import_common9 = require("@nestjs/common");
1034
+ function _ts_decorate9(decorators, target, key, desc) {
853
1035
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
854
1036
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
855
1037
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
856
1038
  return c > 3 && r && Object.defineProperty(target, key, r), r;
857
1039
  }
858
- __name(_ts_decorate8, "_ts_decorate");
859
- function _ts_metadata7(k, v) {
1040
+ __name(_ts_decorate9, "_ts_decorate");
1041
+ function _ts_metadata8(k, v) {
860
1042
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
861
1043
  }
862
- __name(_ts_metadata7, "_ts_metadata");
863
- function _ts_param6(paramIndex, decorator) {
1044
+ __name(_ts_metadata8, "_ts_metadata");
1045
+ function _ts_param7(paramIndex, decorator) {
864
1046
  return function(target, key) {
865
1047
  decorator(target, key, paramIndex);
866
1048
  };
867
1049
  }
868
- __name(_ts_param6, "_ts_param");
1050
+ __name(_ts_param7, "_ts_param");
869
1051
  var QuotaController = class {
870
1052
  static {
871
1053
  __name(this, "QuotaController");
@@ -881,45 +1063,45 @@ var QuotaController = class {
881
1063
  return this.agent.quotaToday(actor.id);
882
1064
  }
883
1065
  };
884
- _ts_decorate8([
885
- (0, import_common8.Get)("today"),
886
- _ts_param6(0, (0, import_common8.Req)()),
887
- _ts_metadata7("design:type", Function),
888
- _ts_metadata7("design:paramtypes", [
1066
+ _ts_decorate9([
1067
+ (0, import_common9.Get)("today"),
1068
+ _ts_param7(0, (0, import_common9.Req)()),
1069
+ _ts_metadata8("design:type", Function),
1070
+ _ts_metadata8("design:paramtypes", [
889
1071
  typeof Request === "undefined" ? Object : Request
890
1072
  ]),
891
- _ts_metadata7("design:returntype", Promise)
1073
+ _ts_metadata8("design:returntype", Promise)
892
1074
  ], QuotaController.prototype, "today", null);
893
- QuotaController = _ts_decorate8([
894
- (0, import_common8.Controller)("quota"),
895
- _ts_param6(1, (0, import_common8.Inject)(import_nestjs_agent_core8.AGENT_ACTOR_RESOLVER)),
896
- _ts_metadata7("design:type", Function),
897
- _ts_metadata7("design:paramtypes", [
1075
+ QuotaController = _ts_decorate9([
1076
+ (0, import_common9.Controller)("quota"),
1077
+ _ts_param7(1, (0, import_common9.Inject)(import_nestjs_agent_core9.AGENT_ACTOR_RESOLVER)),
1078
+ _ts_metadata8("design:type", Function),
1079
+ _ts_metadata8("design:paramtypes", [
898
1080
  typeof AgentService === "undefined" ? Object : AgentService,
899
1081
  typeof ActorResolver === "undefined" ? Object : ActorResolver
900
1082
  ])
901
1083
  ], QuotaController);
902
1084
 
903
1085
  // src/controller/threads.controller.ts
904
- var import_nestjs_agent_core9 = require("@dudousxd/nestjs-agent-core");
905
- var import_common9 = require("@nestjs/common");
906
- function _ts_decorate9(decorators, target, key, desc) {
1086
+ var import_nestjs_agent_core10 = require("@dudousxd/nestjs-agent-core");
1087
+ var import_common10 = require("@nestjs/common");
1088
+ function _ts_decorate10(decorators, target, key, desc) {
907
1089
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
908
1090
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
909
1091
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
910
1092
  return c > 3 && r && Object.defineProperty(target, key, r), r;
911
1093
  }
912
- __name(_ts_decorate9, "_ts_decorate");
913
- function _ts_metadata8(k, v) {
1094
+ __name(_ts_decorate10, "_ts_decorate");
1095
+ function _ts_metadata9(k, v) {
914
1096
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
915
1097
  }
916
- __name(_ts_metadata8, "_ts_metadata");
917
- function _ts_param7(paramIndex, decorator) {
1098
+ __name(_ts_metadata9, "_ts_metadata");
1099
+ function _ts_param8(paramIndex, decorator) {
918
1100
  return function(target, key) {
919
1101
  decorator(target, key, paramIndex);
920
1102
  };
921
1103
  }
922
- __name(_ts_param7, "_ts_param");
1104
+ __name(_ts_param8, "_ts_param");
923
1105
  var ThreadsController = class {
924
1106
  static {
925
1107
  __name(this, "ThreadsController");
@@ -949,9 +1131,21 @@ var ThreadsController = class {
949
1131
  const actor = await this.actorResolver.resolve(req);
950
1132
  return this.agent.forkThread(actor, id, messageId);
951
1133
  }
952
- async rename(req, id, body) {
1134
+ /**
1135
+ * Renames the thread and/or sets its default agent. `defaultAgent` is only present in the request
1136
+ * body when the client actually sent the key (JSON has no `undefined`), so `'defaultAgent' in body`
1137
+ * distinguishes "leave it alone" from "set it to null" (clear it) unambiguously.
1138
+ */
1139
+ async update(req, id, body) {
953
1140
  const actor = await this.actorResolver.resolve(req);
954
- await this.agent.renameThread(actor, id, body.title);
1141
+ await this.agent.updateThread(actor, id, {
1142
+ ...body.title !== void 0 ? {
1143
+ title: body.title
1144
+ } : {},
1145
+ ..."defaultAgent" in body ? {
1146
+ defaultAgent: body.defaultAgent ?? null
1147
+ } : {}
1148
+ });
955
1149
  return {
956
1150
  ok: true
957
1151
  };
@@ -971,117 +1165,117 @@ var ThreadsController = class {
971
1165
  };
972
1166
  }
973
1167
  };
974
- _ts_decorate9([
975
- (0, import_common9.Get)(),
976
- _ts_param7(0, (0, import_common9.Req)()),
977
- _ts_metadata8("design:type", Function),
978
- _ts_metadata8("design:paramtypes", [
1168
+ _ts_decorate10([
1169
+ (0, import_common10.Get)(),
1170
+ _ts_param8(0, (0, import_common10.Req)()),
1171
+ _ts_metadata9("design:type", Function),
1172
+ _ts_metadata9("design:paramtypes", [
979
1173
  typeof Request === "undefined" ? Object : Request
980
1174
  ]),
981
- _ts_metadata8("design:returntype", Promise)
1175
+ _ts_metadata9("design:returntype", Promise)
982
1176
  ], ThreadsController.prototype, "list", null);
983
- _ts_decorate9([
984
- (0, import_common9.Get)(":id"),
985
- _ts_param7(0, (0, import_common9.Req)()),
986
- _ts_param7(1, (0, import_common9.Param)("id")),
987
- _ts_metadata8("design:type", Function),
988
- _ts_metadata8("design:paramtypes", [
1177
+ _ts_decorate10([
1178
+ (0, import_common10.Get)(":id"),
1179
+ _ts_param8(0, (0, import_common10.Req)()),
1180
+ _ts_param8(1, (0, import_common10.Param)("id")),
1181
+ _ts_metadata9("design:type", Function),
1182
+ _ts_metadata9("design:paramtypes", [
989
1183
  typeof Request === "undefined" ? Object : Request,
990
1184
  String
991
1185
  ]),
992
- _ts_metadata8("design:returntype", Promise)
1186
+ _ts_metadata9("design:returntype", Promise)
993
1187
  ], ThreadsController.prototype, "detail", null);
994
- _ts_decorate9([
995
- (0, import_common9.Delete)(":id"),
996
- _ts_param7(0, (0, import_common9.Req)()),
997
- _ts_param7(1, (0, import_common9.Param)("id")),
998
- _ts_metadata8("design:type", Function),
999
- _ts_metadata8("design:paramtypes", [
1188
+ _ts_decorate10([
1189
+ (0, import_common10.Delete)(":id"),
1190
+ _ts_param8(0, (0, import_common10.Req)()),
1191
+ _ts_param8(1, (0, import_common10.Param)("id")),
1192
+ _ts_metadata9("design:type", Function),
1193
+ _ts_metadata9("design:paramtypes", [
1000
1194
  typeof Request === "undefined" ? Object : Request,
1001
1195
  String
1002
1196
  ]),
1003
- _ts_metadata8("design:returntype", Promise)
1197
+ _ts_metadata9("design:returntype", Promise)
1004
1198
  ], ThreadsController.prototype, "remove", null);
1005
- _ts_decorate9([
1006
- (0, import_common9.Post)(":id/fork-from/:messageId"),
1007
- _ts_param7(0, (0, import_common9.Req)()),
1008
- _ts_param7(1, (0, import_common9.Param)("id")),
1009
- _ts_param7(2, (0, import_common9.Param)("messageId")),
1010
- _ts_metadata8("design:type", Function),
1011
- _ts_metadata8("design:paramtypes", [
1199
+ _ts_decorate10([
1200
+ (0, import_common10.Post)(":id/fork-from/:messageId"),
1201
+ _ts_param8(0, (0, import_common10.Req)()),
1202
+ _ts_param8(1, (0, import_common10.Param)("id")),
1203
+ _ts_param8(2, (0, import_common10.Param)("messageId")),
1204
+ _ts_metadata9("design:type", Function),
1205
+ _ts_metadata9("design:paramtypes", [
1012
1206
  typeof Request === "undefined" ? Object : Request,
1013
1207
  String,
1014
1208
  String
1015
1209
  ]),
1016
- _ts_metadata8("design:returntype", Promise)
1210
+ _ts_metadata9("design:returntype", Promise)
1017
1211
  ], ThreadsController.prototype, "fork", null);
1018
- _ts_decorate9([
1019
- (0, import_common9.Patch)(":id"),
1020
- _ts_param7(0, (0, import_common9.Req)()),
1021
- _ts_param7(1, (0, import_common9.Param)("id")),
1022
- _ts_param7(2, (0, import_common9.Body)()),
1023
- _ts_metadata8("design:type", Function),
1024
- _ts_metadata8("design:paramtypes", [
1212
+ _ts_decorate10([
1213
+ (0, import_common10.Patch)(":id"),
1214
+ _ts_param8(0, (0, import_common10.Req)()),
1215
+ _ts_param8(1, (0, import_common10.Param)("id")),
1216
+ _ts_param8(2, (0, import_common10.Body)()),
1217
+ _ts_metadata9("design:type", Function),
1218
+ _ts_metadata9("design:paramtypes", [
1025
1219
  typeof Request === "undefined" ? Object : Request,
1026
1220
  String,
1027
- typeof RenameThreadBody === "undefined" ? Object : RenameThreadBody
1221
+ typeof UpdateThreadBody === "undefined" ? Object : UpdateThreadBody
1028
1222
  ]),
1029
- _ts_metadata8("design:returntype", Promise)
1030
- ], ThreadsController.prototype, "rename", null);
1031
- _ts_decorate9([
1032
- (0, import_common9.Post)(":id/promote"),
1033
- _ts_param7(0, (0, import_common9.Req)()),
1034
- _ts_param7(1, (0, import_common9.Param)("id")),
1035
- _ts_metadata8("design:type", Function),
1036
- _ts_metadata8("design:paramtypes", [
1223
+ _ts_metadata9("design:returntype", Promise)
1224
+ ], ThreadsController.prototype, "update", null);
1225
+ _ts_decorate10([
1226
+ (0, import_common10.Post)(":id/promote"),
1227
+ _ts_param8(0, (0, import_common10.Req)()),
1228
+ _ts_param8(1, (0, import_common10.Param)("id")),
1229
+ _ts_metadata9("design:type", Function),
1230
+ _ts_metadata9("design:paramtypes", [
1037
1231
  typeof Request === "undefined" ? Object : Request,
1038
1232
  String
1039
1233
  ]),
1040
- _ts_metadata8("design:returntype", Promise)
1234
+ _ts_metadata9("design:returntype", Promise)
1041
1235
  ], ThreadsController.prototype, "promote", null);
1042
- _ts_decorate9([
1043
- (0, import_common9.Delete)(":id/from/:messageId"),
1044
- _ts_param7(0, (0, import_common9.Req)()),
1045
- _ts_param7(1, (0, import_common9.Param)("id")),
1046
- _ts_param7(2, (0, import_common9.Param)("messageId")),
1047
- _ts_metadata8("design:type", Function),
1048
- _ts_metadata8("design:paramtypes", [
1236
+ _ts_decorate10([
1237
+ (0, import_common10.Delete)(":id/from/:messageId"),
1238
+ _ts_param8(0, (0, import_common10.Req)()),
1239
+ _ts_param8(1, (0, import_common10.Param)("id")),
1240
+ _ts_param8(2, (0, import_common10.Param)("messageId")),
1241
+ _ts_metadata9("design:type", Function),
1242
+ _ts_metadata9("design:paramtypes", [
1049
1243
  typeof Request === "undefined" ? Object : Request,
1050
1244
  String,
1051
1245
  String
1052
1246
  ]),
1053
- _ts_metadata8("design:returntype", Promise)
1247
+ _ts_metadata9("design:returntype", Promise)
1054
1248
  ], ThreadsController.prototype, "truncateFrom", null);
1055
- ThreadsController = _ts_decorate9([
1056
- (0, import_common9.Controller)("threads"),
1057
- _ts_param7(1, (0, import_common9.Inject)(import_nestjs_agent_core9.AGENT_ACTOR_RESOLVER)),
1058
- _ts_metadata8("design:type", Function),
1059
- _ts_metadata8("design:paramtypes", [
1249
+ ThreadsController = _ts_decorate10([
1250
+ (0, import_common10.Controller)("threads"),
1251
+ _ts_param8(1, (0, import_common10.Inject)(import_nestjs_agent_core10.AGENT_ACTOR_RESOLVER)),
1252
+ _ts_metadata9("design:type", Function),
1253
+ _ts_metadata9("design:paramtypes", [
1060
1254
  typeof AgentService === "undefined" ? Object : AgentService,
1061
1255
  typeof ActorResolver === "undefined" ? Object : ActorResolver
1062
1256
  ])
1063
1257
  ], ThreadsController);
1064
1258
 
1065
1259
  // src/controller/tool-call.controller.ts
1066
- var import_nestjs_agent_core10 = require("@dudousxd/nestjs-agent-core");
1067
- var import_common10 = require("@nestjs/common");
1068
- function _ts_decorate10(decorators, target, key, desc) {
1260
+ var import_nestjs_agent_core11 = require("@dudousxd/nestjs-agent-core");
1261
+ var import_common11 = require("@nestjs/common");
1262
+ function _ts_decorate11(decorators, target, key, desc) {
1069
1263
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1070
1264
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1071
1265
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1072
1266
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1073
1267
  }
1074
- __name(_ts_decorate10, "_ts_decorate");
1075
- function _ts_metadata9(k, v) {
1268
+ __name(_ts_decorate11, "_ts_decorate");
1269
+ function _ts_metadata10(k, v) {
1076
1270
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1077
1271
  }
1078
- __name(_ts_metadata9, "_ts_metadata");
1079
- function _ts_param8(paramIndex, decorator) {
1272
+ __name(_ts_metadata10, "_ts_metadata");
1273
+ function _ts_param9(paramIndex, decorator) {
1080
1274
  return function(target, key) {
1081
1275
  decorator(target, key, paramIndex);
1082
1276
  };
1083
1277
  }
1084
- __name(_ts_param8, "_ts_param");
1278
+ __name(_ts_param9, "_ts_param");
1085
1279
  var ToolCallController = class {
1086
1280
  static {
1087
1281
  __name(this, "ToolCallController");
@@ -1107,41 +1301,41 @@ var ToolCallController = class {
1107
1301
  };
1108
1302
  }
1109
1303
  };
1110
- _ts_decorate10([
1111
- (0, import_common10.Post)("approve"),
1112
- _ts_param8(0, (0, import_common10.Req)()),
1113
- _ts_param8(1, (0, import_common10.Body)()),
1114
- _ts_metadata9("design:type", Function),
1115
- _ts_metadata9("design:paramtypes", [
1304
+ _ts_decorate11([
1305
+ (0, import_common11.Post)("approve"),
1306
+ _ts_param9(0, (0, import_common11.Req)()),
1307
+ _ts_param9(1, (0, import_common11.Body)()),
1308
+ _ts_metadata10("design:type", Function),
1309
+ _ts_metadata10("design:paramtypes", [
1116
1310
  typeof Request === "undefined" ? Object : Request,
1117
1311
  typeof ApproveBody === "undefined" ? Object : ApproveBody
1118
1312
  ]),
1119
- _ts_metadata9("design:returntype", Promise)
1313
+ _ts_metadata10("design:returntype", Promise)
1120
1314
  ], ToolCallController.prototype, "approve", null);
1121
- _ts_decorate10([
1122
- (0, import_common10.Post)("reject"),
1123
- _ts_param8(0, (0, import_common10.Req)()),
1124
- _ts_param8(1, (0, import_common10.Body)()),
1125
- _ts_metadata9("design:type", Function),
1126
- _ts_metadata9("design:paramtypes", [
1315
+ _ts_decorate11([
1316
+ (0, import_common11.Post)("reject"),
1317
+ _ts_param9(0, (0, import_common11.Req)()),
1318
+ _ts_param9(1, (0, import_common11.Body)()),
1319
+ _ts_metadata10("design:type", Function),
1320
+ _ts_metadata10("design:paramtypes", [
1127
1321
  typeof Request === "undefined" ? Object : Request,
1128
1322
  typeof RejectBody === "undefined" ? Object : RejectBody
1129
1323
  ]),
1130
- _ts_metadata9("design:returntype", Promise)
1324
+ _ts_metadata10("design:returntype", Promise)
1131
1325
  ], ToolCallController.prototype, "reject", null);
1132
- ToolCallController = _ts_decorate10([
1133
- (0, import_common10.Controller)("tool-call"),
1134
- _ts_param8(1, (0, import_common10.Inject)(import_nestjs_agent_core10.AGENT_ACTOR_RESOLVER)),
1135
- _ts_metadata9("design:type", Function),
1136
- _ts_metadata9("design:paramtypes", [
1326
+ ToolCallController = _ts_decorate11([
1327
+ (0, import_common11.Controller)("tool-call"),
1328
+ _ts_param9(1, (0, import_common11.Inject)(import_nestjs_agent_core11.AGENT_ACTOR_RESOLVER)),
1329
+ _ts_metadata10("design:type", Function),
1330
+ _ts_metadata10("design:paramtypes", [
1137
1331
  typeof AgentService === "undefined" ? Object : AgentService,
1138
1332
  typeof ActorResolver === "undefined" ? Object : ActorResolver
1139
1333
  ])
1140
1334
  ], ToolCallController);
1141
1335
 
1142
1336
  // src/discovery/agent-discovery.service.ts
1143
- var import_nestjs_agent_core11 = require("@dudousxd/nestjs-agent-core");
1144
- var import_common11 = require("@nestjs/common");
1337
+ var import_nestjs_agent_core12 = require("@dudousxd/nestjs-agent-core");
1338
+ var import_common12 = require("@nestjs/common");
1145
1339
  var import_core = require("@nestjs/core");
1146
1340
 
1147
1341
  // src/decorator/agent.decorator.ts
@@ -1158,23 +1352,23 @@ var SYSTEM_PROMPT_METADATA = Symbol.for("@dudousxd/nestjs-agent:system-prompt");
1158
1352
  var SYSTEM_PROMPT_CONTRIBUTOR_METADATA = Symbol.for("@dudousxd/nestjs-agent:system-prompt-contributor");
1159
1353
 
1160
1354
  // src/discovery/agent-discovery.service.ts
1161
- function _ts_decorate11(decorators, target, key, desc) {
1355
+ function _ts_decorate12(decorators, target, key, desc) {
1162
1356
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1163
1357
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1164
1358
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1165
1359
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1166
1360
  }
1167
- __name(_ts_decorate11, "_ts_decorate");
1168
- function _ts_metadata10(k, v) {
1361
+ __name(_ts_decorate12, "_ts_decorate");
1362
+ function _ts_metadata11(k, v) {
1169
1363
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1170
1364
  }
1171
- __name(_ts_metadata10, "_ts_metadata");
1172
- function _ts_param9(paramIndex, decorator) {
1365
+ __name(_ts_metadata11, "_ts_metadata");
1366
+ function _ts_param10(paramIndex, decorator) {
1173
1367
  return function(target, key) {
1174
1368
  decorator(target, key, paramIndex);
1175
1369
  };
1176
1370
  }
1177
- __name(_ts_param9, "_ts_param");
1371
+ __name(_ts_param10, "_ts_param");
1178
1372
  var AgentDiscoveryService = class _AgentDiscoveryService {
1179
1373
  static {
1180
1374
  __name(this, "AgentDiscoveryService");
@@ -1183,7 +1377,7 @@ var AgentDiscoveryService = class _AgentDiscoveryService {
1183
1377
  scanner;
1184
1378
  registry;
1185
1379
  contributors;
1186
- logger = new import_common11.Logger(_AgentDiscoveryService.name);
1380
+ logger = new import_common12.Logger(_AgentDiscoveryService.name);
1187
1381
  constructor(discovery, scanner, registry, contributors) {
1188
1382
  this.discovery = discovery;
1189
1383
  this.scanner = scanner;
@@ -1271,22 +1465,22 @@ var AgentDiscoveryService = class _AgentDiscoveryService {
1271
1465
  return (ctx) => candidate.call(instance, ctx);
1272
1466
  }
1273
1467
  };
1274
- AgentDiscoveryService = _ts_decorate11([
1275
- (0, import_common11.Injectable)(),
1276
- _ts_param9(2, (0, import_common11.Inject)(import_nestjs_agent_core11.AGENT_REGISTRY)),
1277
- _ts_param9(3, (0, import_common11.Inject)(import_nestjs_agent_core11.AGENT_PROMPT_CONTRIBUTORS)),
1278
- _ts_metadata10("design:type", Function),
1279
- _ts_metadata10("design:paramtypes", [
1468
+ AgentDiscoveryService = _ts_decorate12([
1469
+ (0, import_common12.Injectable)(),
1470
+ _ts_param10(2, (0, import_common12.Inject)(import_nestjs_agent_core12.AGENT_REGISTRY)),
1471
+ _ts_param10(3, (0, import_common12.Inject)(import_nestjs_agent_core12.AGENT_PROMPT_CONTRIBUTORS)),
1472
+ _ts_metadata11("design:type", Function),
1473
+ _ts_metadata11("design:paramtypes", [
1280
1474
  typeof import_core.DiscoveryService === "undefined" ? Object : import_core.DiscoveryService,
1281
1475
  typeof import_core.MetadataScanner === "undefined" ? Object : import_core.MetadataScanner,
1282
- typeof import_nestjs_agent_core11.AgentRegistry === "undefined" ? Object : import_nestjs_agent_core11.AgentRegistry,
1476
+ typeof import_nestjs_agent_core12.AgentRegistry === "undefined" ? Object : import_nestjs_agent_core12.AgentRegistry,
1283
1477
  Array
1284
1478
  ])
1285
1479
  ], AgentDiscoveryService);
1286
1480
 
1287
1481
  // src/discovery/ai-tool-discovery.service.ts
1288
- var import_nestjs_agent_core12 = require("@dudousxd/nestjs-agent-core");
1289
- var import_common12 = require("@nestjs/common");
1482
+ var import_nestjs_agent_core13 = require("@dudousxd/nestjs-agent-core");
1483
+ var import_common13 = require("@nestjs/common");
1290
1484
  var import_core2 = require("@nestjs/core");
1291
1485
  var import_zod = require("zod");
1292
1486
 
@@ -1306,23 +1500,23 @@ function isBrandedFunctionalTool(value) {
1306
1500
  __name(isBrandedFunctionalTool, "isBrandedFunctionalTool");
1307
1501
 
1308
1502
  // src/discovery/ai-tool-discovery.service.ts
1309
- function _ts_decorate12(decorators, target, key, desc) {
1503
+ function _ts_decorate13(decorators, target, key, desc) {
1310
1504
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1311
1505
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1312
1506
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1313
1507
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1314
1508
  }
1315
- __name(_ts_decorate12, "_ts_decorate");
1316
- function _ts_metadata11(k, v) {
1509
+ __name(_ts_decorate13, "_ts_decorate");
1510
+ function _ts_metadata12(k, v) {
1317
1511
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1318
1512
  }
1319
- __name(_ts_metadata11, "_ts_metadata");
1320
- function _ts_param10(paramIndex, decorator) {
1513
+ __name(_ts_metadata12, "_ts_metadata");
1514
+ function _ts_param11(paramIndex, decorator) {
1321
1515
  return function(target, key) {
1322
1516
  decorator(target, key, paramIndex);
1323
1517
  };
1324
1518
  }
1325
- __name(_ts_param10, "_ts_param");
1519
+ __name(_ts_param11, "_ts_param");
1326
1520
  var AiToolDiscoveryService = class _AiToolDiscoveryService {
1327
1521
  static {
1328
1522
  __name(this, "AiToolDiscoveryService");
@@ -1331,7 +1525,7 @@ var AiToolDiscoveryService = class _AiToolDiscoveryService {
1331
1525
  registry;
1332
1526
  agents;
1333
1527
  options;
1334
- logger = new import_common12.Logger(_AiToolDiscoveryService.name);
1528
+ logger = new import_common13.Logger(_AiToolDiscoveryService.name);
1335
1529
  constructor(discovery, registry, agents, options) {
1336
1530
  this.discovery = discovery;
1337
1531
  this.registry = registry;
@@ -1429,13 +1623,13 @@ var AiToolDiscoveryService = class _AiToolDiscoveryService {
1429
1623
  return true;
1430
1624
  }
1431
1625
  };
1432
- AiToolDiscoveryService = _ts_decorate12([
1433
- (0, import_common12.Injectable)(),
1434
- _ts_param10(1, (0, import_common12.Inject)(import_nestjs_agent_core12.AGENT_TOOL_REGISTRY)),
1435
- _ts_param10(2, (0, import_common12.Inject)(import_nestjs_agent_core12.AGENT_REGISTRY)),
1436
- _ts_param10(3, (0, import_common12.Inject)(import_nestjs_agent_core12.AGENT_OPTIONS)),
1437
- _ts_metadata11("design:type", Function),
1438
- _ts_metadata11("design:paramtypes", [
1626
+ AiToolDiscoveryService = _ts_decorate13([
1627
+ (0, import_common13.Injectable)(),
1628
+ _ts_param11(1, (0, import_common13.Inject)(import_nestjs_agent_core13.AGENT_TOOL_REGISTRY)),
1629
+ _ts_param11(2, (0, import_common13.Inject)(import_nestjs_agent_core13.AGENT_REGISTRY)),
1630
+ _ts_param11(3, (0, import_common13.Inject)(import_nestjs_agent_core13.AGENT_OPTIONS)),
1631
+ _ts_metadata12("design:type", Function),
1632
+ _ts_metadata12("design:paramtypes", [
1439
1633
  typeof import_core2.DiscoveryService === "undefined" ? Object : import_core2.DiscoveryService,
1440
1634
  typeof ToolRegistry === "undefined" ? Object : ToolRegistry,
1441
1635
  typeof AgentRegistry === "undefined" ? Object : AgentRegistry,
@@ -1444,7 +1638,7 @@ AiToolDiscoveryService = _ts_decorate12([
1444
1638
  ], AiToolDiscoveryService);
1445
1639
 
1446
1640
  // src/in-process-sink.ts
1447
- var import_nestjs_agent_core13 = require("@dudousxd/nestjs-agent-core");
1641
+ var import_nestjs_agent_core14 = require("@dudousxd/nestjs-agent-core");
1448
1642
  var InProcessTokenStreamSink = class {
1449
1643
  static {
1450
1644
  __name(this, "InProcessTokenStreamSink");
@@ -1499,7 +1693,7 @@ var InProcessTokenStreamSink = class {
1499
1693
  }
1500
1694
  if (buf.ended) {
1501
1695
  if (buf.failure !== void 0) {
1502
- throw new import_nestjs_agent_core13.AgentStreamError(buf.failure);
1696
+ throw new import_nestjs_agent_core14.AgentStreamError(buf.failure);
1503
1697
  }
1504
1698
  return;
1505
1699
  }
@@ -1535,32 +1729,32 @@ var LedgerQuotaStore = class {
1535
1729
  };
1536
1730
 
1537
1731
  // src/runner/inline-agent-runner.ts
1538
- var import_nestjs_agent_core14 = require("@dudousxd/nestjs-agent-core");
1539
- var import_common13 = require("@nestjs/common");
1540
- function _ts_decorate13(decorators, target, key, desc) {
1732
+ var import_nestjs_agent_core15 = require("@dudousxd/nestjs-agent-core");
1733
+ var import_common14 = require("@nestjs/common");
1734
+ function _ts_decorate14(decorators, target, key, desc) {
1541
1735
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1542
1736
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1543
1737
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1544
1738
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1545
1739
  }
1546
- __name(_ts_decorate13, "_ts_decorate");
1547
- function _ts_metadata12(k, v) {
1740
+ __name(_ts_decorate14, "_ts_decorate");
1741
+ function _ts_metadata13(k, v) {
1548
1742
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1549
1743
  }
1550
- __name(_ts_metadata12, "_ts_metadata");
1551
- function _ts_param11(paramIndex, decorator) {
1744
+ __name(_ts_metadata13, "_ts_metadata");
1745
+ function _ts_param12(paramIndex, decorator) {
1552
1746
  return function(target, key) {
1553
1747
  decorator(target, key, paramIndex);
1554
1748
  };
1555
1749
  }
1556
- __name(_ts_param11, "_ts_param");
1750
+ __name(_ts_param12, "_ts_param");
1557
1751
  var InlineAgentRunner = class _InlineAgentRunner {
1558
1752
  static {
1559
1753
  __name(this, "InlineAgentRunner");
1560
1754
  }
1561
1755
  factory;
1562
1756
  store;
1563
- logger = new import_common13.Logger(_InlineAgentRunner.name);
1757
+ logger = new import_common14.Logger(_InlineAgentRunner.name);
1564
1758
  pending = /* @__PURE__ */ new Map();
1565
1759
  constructor(factory, store) {
1566
1760
  this.factory = factory;
@@ -1571,18 +1765,19 @@ var InlineAgentRunner = class _InlineAgentRunner {
1571
1765
  const day = input.day ?? utcDay();
1572
1766
  const deps = this.factory.forAgent(input.agentName);
1573
1767
  const hooks = this.topLevelHooks(runId, deps, input.actor, day);
1574
- void (0, import_nestjs_agent_core14.runAgentLoop)({
1768
+ void (0, import_nestjs_agent_core15.runAgentLoop)({
1575
1769
  ...deps,
1576
1770
  day
1577
- }, input, hooks).catch(async (error) => {
1771
+ }, input, hooks).then(() => this.store.setActiveStream(input.threadId, null)).catch(async (error) => {
1578
1772
  const message = error instanceof Error ? error.message : String(error);
1579
- const code = error instanceof import_nestjs_agent_core14.QuotaExceededError ? "quota_exceeded" : "run_failed";
1773
+ const code = error instanceof import_nestjs_agent_core15.QuotaExceededError ? "quota_exceeded" : "run_failed";
1580
1774
  this.logger.error(`agent run ${runId} failed (${code}): ${message}`);
1581
- (0, import_nestjs_agent_core14.publishAgentRunFailed)({
1775
+ (0, import_nestjs_agent_core15.publishAgentRunFailed)({
1582
1776
  runId,
1583
1777
  code,
1584
1778
  message
1585
1779
  });
1780
+ await this.store.setActiveStream(input.threadId, null);
1586
1781
  const writer = await deps.sink.open(runId);
1587
1782
  await writer.fail({
1588
1783
  code,
@@ -1640,98 +1835,102 @@ var InlineAgentRunner = class _InlineAgentRunner {
1640
1835
  step: /* @__PURE__ */ __name((_name, fn) => fn(), "step"),
1641
1836
  runAgent: /* @__PURE__ */ __name((childName, childTask) => this.runNested(childName, childTask, actor, day, depth + 1, sinkRunId), "runAgent")
1642
1837
  };
1643
- return (0, import_nestjs_agent_core14.runAgentLoop)({
1644
- ...deps,
1645
- day
1646
- }, {
1647
- threadId: subThread.id,
1648
- actor,
1649
- userText: task,
1650
- agentName,
1651
- day,
1652
- delegationDepth: depth,
1653
- sinkRunId
1654
- }, hooks);
1838
+ try {
1839
+ return await (0, import_nestjs_agent_core15.runAgentLoop)({
1840
+ ...deps,
1841
+ day
1842
+ }, {
1843
+ threadId: subThread.id,
1844
+ actor,
1845
+ userText: task,
1846
+ agentName,
1847
+ day,
1848
+ delegationDepth: depth,
1849
+ sinkRunId
1850
+ }, hooks);
1851
+ } finally {
1852
+ await this.store.setActiveStream(subThread.id, null);
1853
+ }
1655
1854
  }
1656
1855
  };
1657
- InlineAgentRunner = _ts_decorate13([
1658
- (0, import_common13.Injectable)(),
1659
- _ts_param11(0, (0, import_common13.Inject)(import_nestjs_agent_core14.AGENT_DEPS_FACTORY)),
1660
- _ts_param11(1, (0, import_common13.Inject)(import_nestjs_agent_core14.AGENT_STORE)),
1661
- _ts_metadata12("design:type", Function),
1662
- _ts_metadata12("design:paramtypes", [
1856
+ InlineAgentRunner = _ts_decorate14([
1857
+ (0, import_common14.Injectable)(),
1858
+ _ts_param12(0, (0, import_common14.Inject)(import_nestjs_agent_core15.AGENT_DEPS_FACTORY)),
1859
+ _ts_param12(1, (0, import_common14.Inject)(import_nestjs_agent_core15.AGENT_STORE)),
1860
+ _ts_metadata13("design:type", Function),
1861
+ _ts_metadata13("design:paramtypes", [
1663
1862
  typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory,
1664
1863
  typeof AgentStore === "undefined" ? Object : AgentStore
1665
1864
  ])
1666
1865
  ], InlineAgentRunner);
1667
1866
 
1668
1867
  // src/agent.module.ts
1669
- function _ts_decorate14(decorators, target, key, desc) {
1868
+ function _ts_decorate15(decorators, target, key, desc) {
1670
1869
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1671
1870
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1672
1871
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1673
1872
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1674
1873
  }
1675
- __name(_ts_decorate14, "_ts_decorate");
1874
+ __name(_ts_decorate15, "_ts_decorate");
1676
1875
  var DEFAULT_PATH = "agent";
1677
1876
  function sharedProviders(durable, includeStore) {
1678
1877
  const providers = [
1679
1878
  {
1680
- provide: import_nestjs_agent_core15.AGENT_TOOL_REGISTRY,
1681
- useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core15.ToolRegistry(), "useFactory")
1879
+ provide: import_nestjs_agent_core16.AGENT_TOOL_REGISTRY,
1880
+ useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core16.ToolRegistry(), "useFactory")
1682
1881
  },
1683
1882
  // Starts empty; AgentDiscoveryService populates it from `@Agent`-decorated providers at boot.
1684
1883
  {
1685
- provide: import_nestjs_agent_core15.AGENT_REGISTRY,
1686
- useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core15.AgentRegistry(), "useFactory")
1884
+ provide: import_nestjs_agent_core16.AGENT_REGISTRY,
1885
+ useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core16.AgentRegistry(), "useFactory")
1687
1886
  },
1688
1887
  // A shared, mutable list AgentDiscoveryService fills with `@SystemPromptContributor()` methods.
1689
1888
  {
1690
- provide: import_nestjs_agent_core15.AGENT_PROMPT_CONTRIBUTORS,
1889
+ provide: import_nestjs_agent_core16.AGENT_PROMPT_CONTRIBUTORS,
1691
1890
  useFactory: /* @__PURE__ */ __name(() => [], "useFactory")
1692
1891
  },
1693
1892
  {
1694
- provide: import_nestjs_agent_core15.AGENT_SINK,
1893
+ provide: import_nestjs_agent_core16.AGENT_SINK,
1695
1894
  useFactory: /* @__PURE__ */ __name((options) => options.sink ?? new InProcessTokenStreamSink(), "useFactory"),
1696
1895
  inject: [
1697
- import_nestjs_agent_core15.AGENT_OPTIONS
1896
+ import_nestjs_agent_core16.AGENT_OPTIONS
1698
1897
  ]
1699
1898
  },
1700
1899
  {
1701
- provide: import_nestjs_agent_core15.AGENT_ROLES_POLICY,
1702
- useFactory: /* @__PURE__ */ __name((options) => options.rolesPolicy ?? new import_nestjs_agent_core15.DefaultRolesPolicy(options.defaultRoles), "useFactory"),
1900
+ provide: import_nestjs_agent_core16.AGENT_ROLES_POLICY,
1901
+ useFactory: /* @__PURE__ */ __name((options) => options.rolesPolicy ?? new import_nestjs_agent_core16.DefaultRolesPolicy(options.defaultRoles), "useFactory"),
1703
1902
  inject: [
1704
- import_nestjs_agent_core15.AGENT_OPTIONS
1903
+ import_nestjs_agent_core16.AGENT_OPTIONS
1705
1904
  ]
1706
1905
  },
1707
1906
  {
1708
- provide: import_nestjs_agent_core15.AGENT_ACTOR_RESOLVER,
1907
+ provide: import_nestjs_agent_core16.AGENT_ACTOR_RESOLVER,
1709
1908
  useFactory: /* @__PURE__ */ __name((options) => options.actorResolver, "useFactory"),
1710
1909
  inject: [
1711
- import_nestjs_agent_core15.AGENT_OPTIONS
1910
+ import_nestjs_agent_core16.AGENT_OPTIONS
1712
1911
  ]
1713
1912
  },
1714
1913
  {
1715
- provide: import_nestjs_agent_core15.AGENT_MODEL,
1914
+ provide: import_nestjs_agent_core16.AGENT_MODEL,
1716
1915
  useFactory: /* @__PURE__ */ __name((o) => o.model, "useFactory"),
1717
1916
  inject: [
1718
- import_nestjs_agent_core15.AGENT_OPTIONS
1917
+ import_nestjs_agent_core16.AGENT_OPTIONS
1719
1918
  ]
1720
1919
  },
1721
1920
  {
1722
1921
  // An explicit `quota` wins; otherwise `quotaLimitTokens` binds the built-in ledger-backed
1723
1922
  // store; otherwise quotas are off (undefined). The ledger store reads the same usage rows
1724
1923
  // the loop already writes, so enforcement needs no extra shared state across replicas.
1725
- provide: import_nestjs_agent_core15.AGENT_QUOTA_STORE,
1924
+ provide: import_nestjs_agent_core16.AGENT_QUOTA_STORE,
1726
1925
  useFactory: /* @__PURE__ */ __name((o, store) => o.quota ?? (o.quotaLimitTokens !== void 0 ? new LedgerQuotaStore(store, o.quotaLimitTokens) : void 0), "useFactory"),
1727
1926
  inject: [
1728
- import_nestjs_agent_core15.AGENT_OPTIONS,
1729
- import_nestjs_agent_core15.AGENT_STORE
1927
+ import_nestjs_agent_core16.AGENT_OPTIONS,
1928
+ import_nestjs_agent_core16.AGENT_STORE
1730
1929
  ]
1731
1930
  },
1732
1931
  AgentDepsFactory2,
1733
1932
  {
1734
- provide: import_nestjs_agent_core15.AGENT_DEPS_FACTORY,
1933
+ provide: import_nestjs_agent_core16.AGENT_DEPS_FACTORY,
1735
1934
  useExisting: AgentDepsFactory2
1736
1935
  },
1737
1936
  // Populates the registry + contributors (onModuleInit) BEFORE AiToolDiscoveryService synthesizes
@@ -1743,16 +1942,16 @@ function sharedProviders(durable, includeStore) {
1743
1942
  ];
1744
1943
  if (includeStore) {
1745
1944
  providers.push({
1746
- provide: import_nestjs_agent_core15.AGENT_STORE,
1945
+ provide: import_nestjs_agent_core16.AGENT_STORE,
1747
1946
  useFactory: /* @__PURE__ */ __name((o) => o.store, "useFactory"),
1748
1947
  inject: [
1749
- import_nestjs_agent_core15.AGENT_OPTIONS
1948
+ import_nestjs_agent_core16.AGENT_OPTIONS
1750
1949
  ]
1751
1950
  });
1752
1951
  }
1753
1952
  if (durable) {
1754
1953
  providers.push({
1755
- provide: import_nestjs_agent_core15.AGENT_RUNNER,
1954
+ provide: import_nestjs_agent_core16.AGENT_RUNNER,
1756
1955
  useFactory: /* @__PURE__ */ __name((durableRunner) => {
1757
1956
  if (durableRunner === void 0) {
1758
1957
  throw new Error("AgentModule.forRoot({ durable: true }) requires importing AgentDurableModule from '@dudousxd/nestjs-agent/durable' (alongside a configured DurableModule). It was not found.");
@@ -1761,14 +1960,14 @@ function sharedProviders(durable, includeStore) {
1761
1960
  }, "useFactory"),
1762
1961
  inject: [
1763
1962
  {
1764
- token: import_nestjs_agent_core15.AGENT_DURABLE_RUNNER,
1963
+ token: import_nestjs_agent_core16.AGENT_DURABLE_RUNNER,
1765
1964
  optional: true
1766
1965
  }
1767
1966
  ]
1768
1967
  });
1769
1968
  } else {
1770
1969
  providers.push({
1771
- provide: import_nestjs_agent_core15.AGENT_RUNNER,
1970
+ provide: import_nestjs_agent_core16.AGENT_RUNNER,
1772
1971
  useExisting: InlineAgentRunner
1773
1972
  });
1774
1973
  }
@@ -1777,32 +1976,57 @@ function sharedProviders(durable, includeStore) {
1777
1976
  __name(sharedProviders, "sharedProviders");
1778
1977
  function exportsFor(includeStore) {
1779
1978
  return [
1780
- import_nestjs_agent_core15.AGENT_OPTIONS,
1781
- import_nestjs_agent_core15.AGENT_TOOL_REGISTRY,
1782
- import_nestjs_agent_core15.AGENT_REGISTRY,
1783
- import_nestjs_agent_core15.AGENT_SINK,
1784
- import_nestjs_agent_core15.AGENT_ROLES_POLICY,
1785
- import_nestjs_agent_core15.AGENT_ACTOR_RESOLVER,
1786
- import_nestjs_agent_core15.AGENT_MODEL,
1979
+ import_nestjs_agent_core16.AGENT_OPTIONS,
1980
+ import_nestjs_agent_core16.AGENT_TOOL_REGISTRY,
1981
+ import_nestjs_agent_core16.AGENT_REGISTRY,
1982
+ import_nestjs_agent_core16.AGENT_SINK,
1983
+ import_nestjs_agent_core16.AGENT_ROLES_POLICY,
1984
+ import_nestjs_agent_core16.AGENT_ACTOR_RESOLVER,
1985
+ import_nestjs_agent_core16.AGENT_MODEL,
1787
1986
  ...includeStore ? [
1788
- import_nestjs_agent_core15.AGENT_STORE
1987
+ import_nestjs_agent_core16.AGENT_STORE
1789
1988
  ] : [],
1790
- import_nestjs_agent_core15.AGENT_QUOTA_STORE,
1791
- import_nestjs_agent_core15.AGENT_PROMPT_CONTRIBUTORS,
1792
- import_nestjs_agent_core15.AGENT_DEPS_FACTORY,
1989
+ import_nestjs_agent_core16.AGENT_QUOTA_STORE,
1990
+ import_nestjs_agent_core16.AGENT_PROMPT_CONTRIBUTORS,
1991
+ import_nestjs_agent_core16.AGENT_DEPS_FACTORY,
1793
1992
  AgentDepsFactory2,
1794
1993
  AgentService,
1795
1994
  InlineAgentRunner
1796
1995
  ];
1797
1996
  }
1798
1997
  __name(exportsFor, "exportsFor");
1799
- var CONTROLLERS = [
1998
+ var BASE_CONTROLLERS = [
1800
1999
  ChatController,
1801
2000
  ThreadsController,
1802
2001
  ToolCallController,
1803
2002
  QuotaController,
1804
2003
  AgentsController
1805
2004
  ];
2005
+ var GUARDABLE_CONTROLLERS = [
2006
+ ...BASE_CONTROLLERS,
2007
+ AttachmentsController
2008
+ ];
2009
+ function controllersFor(mountAttachments) {
2010
+ return mountAttachments ? [
2011
+ ...BASE_CONTROLLERS,
2012
+ AttachmentsController
2013
+ ] : BASE_CONTROLLERS;
2014
+ }
2015
+ __name(controllersFor, "controllersFor");
2016
+ var GUARDS_METADATA = "__guards__";
2017
+ function applyGuards(guards) {
2018
+ const resolved = guards ?? [];
2019
+ for (const controller of GUARDABLE_CONTROLLERS) {
2020
+ Reflect.defineMetadata(GUARDS_METADATA, resolved, controller);
2021
+ }
2022
+ }
2023
+ __name(applyGuards, "applyGuards");
2024
+ function guardProviders(guards) {
2025
+ return [
2026
+ ...new Set(guards ?? [])
2027
+ ];
2028
+ }
2029
+ __name(guardProviders, "guardProviders");
1806
2030
  function routerFor(path) {
1807
2031
  return import_core3.RouterModule.register([
1808
2032
  {
@@ -1819,6 +2043,7 @@ var AgentModule = class _AgentModule {
1819
2043
  static forRoot(options) {
1820
2044
  const path = options.path ?? DEFAULT_PATH;
1821
2045
  const includeStore = options.store !== void 0;
2046
+ applyGuards(options.guards);
1822
2047
  return {
1823
2048
  module: _AgentModule,
1824
2049
  global: true,
@@ -1826,13 +2051,14 @@ var AgentModule = class _AgentModule {
1826
2051
  import_core3.DiscoveryModule,
1827
2052
  routerFor(path)
1828
2053
  ],
1829
- controllers: CONTROLLERS,
2054
+ controllers: controllersFor(options.attachments?.upload === true),
1830
2055
  providers: [
1831
2056
  {
1832
- provide: import_nestjs_agent_core15.AGENT_OPTIONS,
2057
+ provide: import_nestjs_agent_core16.AGENT_OPTIONS,
1833
2058
  useValue: options
1834
2059
  },
1835
- ...sharedProviders(options.durable ?? false, includeStore)
2060
+ ...sharedProviders(options.durable ?? false, includeStore),
2061
+ ...guardProviders(options.guards)
1836
2062
  ],
1837
2063
  exports: exportsFor(includeStore)
1838
2064
  };
@@ -1840,6 +2066,7 @@ var AgentModule = class _AgentModule {
1840
2066
  static forRootAsync(options) {
1841
2067
  const path = options.path ?? DEFAULT_PATH;
1842
2068
  const includeStore = options.externalStore !== true;
2069
+ applyGuards(options.guards);
1843
2070
  return {
1844
2071
  module: _AgentModule,
1845
2072
  global: true,
@@ -1848,22 +2075,23 @@ var AgentModule = class _AgentModule {
1848
2075
  routerFor(path),
1849
2076
  ...options.imports ?? []
1850
2077
  ],
1851
- controllers: CONTROLLERS,
2078
+ controllers: controllersFor(options.attachmentsUpload === true),
1852
2079
  providers: [
1853
2080
  {
1854
- provide: import_nestjs_agent_core15.AGENT_OPTIONS,
2081
+ provide: import_nestjs_agent_core16.AGENT_OPTIONS,
1855
2082
  useFactory: options.useFactory,
1856
2083
  inject: options.inject ?? []
1857
2084
  },
1858
- ...sharedProviders(options.durable ?? false, includeStore)
2085
+ ...sharedProviders(options.durable ?? false, includeStore),
2086
+ ...guardProviders(options.guards)
1859
2087
  ],
1860
2088
  exports: exportsFor(includeStore)
1861
2089
  };
1862
2090
  }
1863
2091
  };
1864
- AgentModule = _ts_decorate14([
1865
- (0, import_common14.Global)(),
1866
- (0, import_common14.Module)({})
2092
+ AgentModule = _ts_decorate15([
2093
+ (0, import_common15.Global)(),
2094
+ (0, import_common15.Module)({})
1867
2095
  ], AgentModule);
1868
2096
 
1869
2097
  // src/durable/agent-durable.ts