@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.
package/dist/index.cjs CHANGED
@@ -32,6 +32,9 @@ __export(index_exports, {
32
32
  AgentService: () => AgentService,
33
33
  AiTool: () => AiTool,
34
34
  AiToolDiscoveryService: () => AiToolDiscoveryService,
35
+ AttachmentsController: () => AttachmentsController,
36
+ DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES: () => DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES,
37
+ DEFAULT_MAX_ATTACHMENT_BYTES: () => DEFAULT_MAX_ATTACHMENT_BYTES,
35
38
  HeaderActorResolver: () => HeaderActorResolver,
36
39
  InProcessTokenStreamSink: () => InProcessTokenStreamSink,
37
40
  InlineAgentRunner: () => InlineAgentRunner,
@@ -49,8 +52,8 @@ __export(index_exports, {
49
52
  module.exports = __toCommonJS(index_exports);
50
53
 
51
54
  // src/agent.module.ts
52
- var import_nestjs_agent_core13 = require("@dudousxd/nestjs-agent-core");
53
- var import_common11 = require("@nestjs/common");
55
+ var import_nestjs_agent_core14 = require("@dudousxd/nestjs-agent-core");
56
+ var import_common12 = require("@nestjs/common");
54
57
  var import_core3 = require("@nestjs/core");
55
58
 
56
59
  // src/agent-deps.factory.ts
@@ -91,7 +94,8 @@ var AgentDepsFactory2 = class {
91
94
  agents;
92
95
  promptContributors;
93
96
  quota;
94
- constructor(options, model, store, sink, rolesPolicy, registry, agents, promptContributors, quota) {
97
+ pricingStore;
98
+ constructor(options, model, store, sink, rolesPolicy, registry, agents, promptContributors, quota, pricingStore) {
95
99
  this.options = options;
96
100
  this.model = model;
97
101
  this.store = store;
@@ -101,6 +105,7 @@ var AgentDepsFactory2 = class {
101
105
  this.agents = agents;
102
106
  this.promptContributors = promptContributors;
103
107
  this.quota = quota;
108
+ this.pricingStore = pricingStore;
104
109
  }
105
110
  /**
106
111
  * The agent a turn uses when the caller names none: the explicit `defaultAgent` option, else the
@@ -147,6 +152,9 @@ var AgentDepsFactory2 = class {
147
152
  ...this.quota !== void 0 ? {
148
153
  quota: this.quota
149
154
  } : {},
155
+ ...this.pricingStore !== void 0 ? {
156
+ pricingStore: this.pricingStore
157
+ } : {},
150
158
  ...toolAllowList !== void 0 ? {
151
159
  toolAllowList
152
160
  } : {},
@@ -187,6 +195,8 @@ AgentDepsFactory2 = _ts_decorate([
187
195
  _ts_param(6, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_REGISTRY)),
188
196
  _ts_param(7, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_PROMPT_CONTRIBUTORS)),
189
197
  _ts_param(8, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_QUOTA_STORE)),
198
+ _ts_param(9, (0, import_common.Optional)()),
199
+ _ts_param(9, (0, import_common.Inject)(import_nestjs_agent_core.AGENT_PRICING_STORE)),
190
200
  _ts_metadata("design:type", Function),
191
201
  _ts_metadata("design:paramtypes", [
192
202
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
@@ -197,6 +207,7 @@ AgentDepsFactory2 = _ts_decorate([
197
207
  typeof import_nestjs_agent_core.ToolRegistry === "undefined" ? Object : import_nestjs_agent_core.ToolRegistry,
198
208
  typeof import_nestjs_agent_core.AgentRegistry === "undefined" ? Object : import_nestjs_agent_core.AgentRegistry,
199
209
  Array,
210
+ Object,
200
211
  Object
201
212
  ])
202
213
  ], AgentDepsFactory2);
@@ -254,7 +265,7 @@ var AgentService = class {
254
265
  this.quota = quota;
255
266
  }
256
267
  async chat(params) {
257
- const agentName = params.agentName ?? this.deps.defaultAgentName();
268
+ const agentName = await this.resolveAgentName(params.agentName, params.threadId);
258
269
  let threadId = params.threadId;
259
270
  if (threadId === void 0) {
260
271
  if (params.regenerate === true) {
@@ -315,12 +326,14 @@ var AgentService = class {
315
326
  await this.assertOwnsActiveStream(actor, runId);
316
327
  return this.runner.cancel(runId);
317
328
  }
318
- listThreads(actorRef) {
319
- return this.store.listThreads(actorRef);
329
+ async listThreads(actorRef) {
330
+ const threads = await this.store.listThreads(actorRef);
331
+ return Promise.all(threads.map((thread) => this.toSummaryView(thread)));
320
332
  }
321
333
  async getThread(actor, threadId) {
322
334
  await this.assertOwnsThread(actor, threadId);
323
- return this.store.getThread(threadId);
335
+ const thread = await this.store.getThread(threadId);
336
+ return thread === null ? null : this.toDetailView(thread);
324
337
  }
325
338
  async deleteThread(actor, threadId) {
326
339
  await this.assertOwnsThread(actor, threadId);
@@ -330,7 +343,38 @@ var AgentService = class {
330
343
  await this.assertOwnsThread(actor, threadId);
331
344
  return this.store.forkThread(threadId, fromMessageId);
332
345
  }
346
+ /** Kept for source compatibility — a thin wrapper over the more general `updateThread`. */
333
347
  async renameThread(actor, threadId, title) {
348
+ return this.updateThread(actor, threadId, {
349
+ title
350
+ });
351
+ }
352
+ /**
353
+ * Rename a thread and/or set its default agent. Title-only patches work against ANY store (via
354
+ * the required `setTitle`); a `defaultAgent` change requires the bound store to implement the
355
+ * optional `updateThread` — 501 with a clear message when it doesn't, rather than silently
356
+ * dropping the field.
357
+ */
358
+ async updateThread(actor, threadId, patch) {
359
+ const title = patch.title !== void 0 ? this.validateTitle(patch.title) : void 0;
360
+ await this.assertOwnsThread(actor, threadId);
361
+ if (patch.defaultAgent === void 0) {
362
+ if (title !== void 0) {
363
+ await this.store.setTitle(threadId, title);
364
+ }
365
+ return;
366
+ }
367
+ if (this.store.updateThread === void 0) {
368
+ throw new import_common2.NotImplementedException("Setting a thread's defaultAgent requires an AgentStore that implements updateThread(); the bound store does not support it.");
369
+ }
370
+ await this.store.updateThread(threadId, {
371
+ ...title !== void 0 ? {
372
+ title
373
+ } : {},
374
+ defaultAgent: patch.defaultAgent
375
+ });
376
+ }
377
+ validateTitle(title) {
334
378
  const trimmed = title.trim();
335
379
  if (trimmed.length === 0) {
336
380
  throw new import_common2.BadRequestException("title must not be empty");
@@ -338,8 +382,41 @@ var AgentService = class {
338
382
  if (trimmed.length > 200) {
339
383
  throw new import_common2.BadRequestException("title must be at most 200 characters");
340
384
  }
341
- await this.assertOwnsThread(actor, threadId);
342
- return this.store.setTitle(threadId, trimmed);
385
+ return trimmed;
386
+ }
387
+ /**
388
+ * Agent-selection precedence for a `chat()` call with no explicit `agentName`: the thread's own
389
+ * `defaultAgent` (if the thread exists and one is set) wins over the module's configured default.
390
+ * Skips the thread lookup entirely when the caller already named an agent or there's no thread yet.
391
+ */
392
+ async resolveAgentName(agentName, threadId) {
393
+ if (agentName !== void 0) {
394
+ return agentName;
395
+ }
396
+ if (threadId !== void 0) {
397
+ const thread = await this.store.getThread(threadId);
398
+ if (thread?.defaultAgent !== void 0 && thread.defaultAgent !== null) {
399
+ return thread.defaultAgent;
400
+ }
401
+ }
402
+ return this.deps.defaultAgentName();
403
+ }
404
+ async toSummaryView(thread) {
405
+ return {
406
+ ...thread,
407
+ defaultAgent: thread.defaultAgent ?? null,
408
+ activeRunId: await this.store.activeRunForThread?.(thread.id) ?? null
409
+ };
410
+ }
411
+ async toDetailView(thread) {
412
+ const summary = await this.toSummaryView(thread);
413
+ return {
414
+ ...summary,
415
+ messages: thread.messages,
416
+ ...thread.activeStreamId !== void 0 ? {
417
+ activeStreamId: thread.activeStreamId
418
+ } : {}
419
+ };
343
420
  }
344
421
  async promoteThread(actor, threadId) {
345
422
  await this.assertOwnsThread(actor, threadId);
@@ -495,9 +572,10 @@ AgentsController = _ts_decorate3([
495
572
  ])
496
573
  ], AgentsController);
497
574
 
498
- // src/controller/chat.controller.ts
575
+ // src/controller/attachments.controller.ts
499
576
  var import_nestjs_agent_core5 = require("@dudousxd/nestjs-agent-core");
500
577
  var import_common4 = require("@nestjs/common");
578
+ var import_platform_express = require("@nestjs/platform-express");
501
579
  function _ts_decorate4(decorators, target, key, desc) {
502
580
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
503
581
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -515,6 +593,110 @@ function _ts_param4(paramIndex, decorator) {
515
593
  };
516
594
  }
517
595
  __name(_ts_param4, "_ts_param");
596
+ var DEFAULT_MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024;
597
+ var DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES = [
598
+ "image/png",
599
+ "image/jpeg",
600
+ "image/gif",
601
+ "image/webp",
602
+ "application/pdf",
603
+ "text/plain",
604
+ "text/csv"
605
+ ];
606
+ var AttachmentsController = class {
607
+ static {
608
+ __name(this, "AttachmentsController");
609
+ }
610
+ options;
611
+ actorResolver;
612
+ staging;
613
+ constructor(options, actorResolver, staging) {
614
+ this.options = options;
615
+ this.actorResolver = actorResolver;
616
+ this.staging = staging;
617
+ }
618
+ /**
619
+ * `attachments.upload: true` is what mounts this controller at all (see `agent.module.ts`), so by
620
+ * the time an instance exists the host clearly intends uploads to work. Fail boot loudly rather
621
+ * than mounting a controller that would 501 on every single request because nobody bound
622
+ * `AGENT_ATTACHMENT_STAGING`.
623
+ */
624
+ onModuleInit() {
625
+ if (this.options.attachments?.upload === true && this.staging === void 0) {
626
+ 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.");
627
+ }
628
+ }
629
+ async upload(req, file) {
630
+ if (this.staging === void 0) {
631
+ throw new import_common4.NotImplementedException("Attachment upload is not configured on this server.");
632
+ }
633
+ if (file === void 0) {
634
+ throw new import_common4.BadRequestException('multipart field "file" is required');
635
+ }
636
+ const allowedContentTypes = this.options.attachments?.allowedContentTypes ?? DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES;
637
+ if (!allowedContentTypes.includes(file.mimetype)) {
638
+ throw new import_common4.UnsupportedMediaTypeException(`content type "${file.mimetype}" is not allowed (allowed: ${allowedContentTypes.join(", ")})`);
639
+ }
640
+ const maxBytes = this.options.attachments?.maxBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES;
641
+ if (file.size > maxBytes) {
642
+ throw new import_common4.PayloadTooLargeException(`file exceeds the ${maxBytes}-byte limit`);
643
+ }
644
+ const actor = await this.actorResolver.resolve(req);
645
+ return this.staging.stage({
646
+ data: file.buffer,
647
+ filename: file.originalname,
648
+ contentType: file.mimetype,
649
+ sizeBytes: file.size,
650
+ actor
651
+ });
652
+ }
653
+ };
654
+ _ts_decorate4([
655
+ (0, import_common4.Post)(),
656
+ (0, import_common4.UseInterceptors)((0, import_platform_express.FileInterceptor)("file")),
657
+ _ts_param4(0, (0, import_common4.Req)()),
658
+ _ts_param4(1, (0, import_common4.UploadedFile)()),
659
+ _ts_metadata4("design:type", Function),
660
+ _ts_metadata4("design:paramtypes", [
661
+ typeof Request === "undefined" ? Object : Request,
662
+ Object
663
+ ]),
664
+ _ts_metadata4("design:returntype", Promise)
665
+ ], AttachmentsController.prototype, "upload", null);
666
+ AttachmentsController = _ts_decorate4([
667
+ (0, import_common4.Controller)("attachments"),
668
+ _ts_param4(0, (0, import_common4.Inject)(import_nestjs_agent_core5.AGENT_OPTIONS)),
669
+ _ts_param4(1, (0, import_common4.Inject)(import_nestjs_agent_core5.AGENT_ACTOR_RESOLVER)),
670
+ _ts_param4(2, (0, import_common4.Optional)()),
671
+ _ts_param4(2, (0, import_common4.Inject)(import_nestjs_agent_core5.AGENT_ATTACHMENT_STAGING)),
672
+ _ts_metadata4("design:type", Function),
673
+ _ts_metadata4("design:paramtypes", [
674
+ typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
675
+ typeof ActorResolver === "undefined" ? Object : ActorResolver,
676
+ Object
677
+ ])
678
+ ], AttachmentsController);
679
+
680
+ // src/controller/chat.controller.ts
681
+ var import_nestjs_agent_core6 = require("@dudousxd/nestjs-agent-core");
682
+ var import_common5 = require("@nestjs/common");
683
+ function _ts_decorate5(decorators, target, key, desc) {
684
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
685
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
686
+ 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;
687
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
688
+ }
689
+ __name(_ts_decorate5, "_ts_decorate");
690
+ function _ts_metadata5(k, v) {
691
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
692
+ }
693
+ __name(_ts_metadata5, "_ts_metadata");
694
+ function _ts_param5(paramIndex, decorator) {
695
+ return function(target, key) {
696
+ decorator(target, key, paramIndex);
697
+ };
698
+ }
699
+ __name(_ts_param5, "_ts_param");
518
700
  var ChatController = class {
519
701
  static {
520
702
  __name(this, "ChatController");
@@ -602,7 +784,7 @@ data: ${JSON.stringify({
602
784
  }
603
785
  res.write("event: done\ndata: {}\n\n");
604
786
  } catch (error) {
605
- const payload = error instanceof import_nestjs_agent_core5.AgentStreamError ? {
787
+ const payload = error instanceof import_nestjs_agent_core6.AgentStreamError ? {
606
788
  code: error.code,
607
789
  message: error.message
608
790
  } : {
@@ -617,71 +799,71 @@ data: ${JSON.stringify(payload)}
617
799
  res.end();
618
800
  }
619
801
  };
620
- _ts_decorate4([
621
- (0, import_common4.Post)("chat"),
622
- _ts_param4(0, (0, import_common4.Req)()),
623
- _ts_param4(1, (0, import_common4.Res)()),
624
- _ts_param4(2, (0, import_common4.Body)()),
625
- _ts_metadata4("design:type", Function),
626
- _ts_metadata4("design:paramtypes", [
802
+ _ts_decorate5([
803
+ (0, import_common5.Post)("chat"),
804
+ _ts_param5(0, (0, import_common5.Req)()),
805
+ _ts_param5(1, (0, import_common5.Res)()),
806
+ _ts_param5(2, (0, import_common5.Body)()),
807
+ _ts_metadata5("design:type", Function),
808
+ _ts_metadata5("design:paramtypes", [
627
809
  typeof Request === "undefined" ? Object : Request,
628
810
  typeof Response === "undefined" ? Object : Response,
629
811
  typeof ChatBody === "undefined" ? Object : ChatBody
630
812
  ]),
631
- _ts_metadata4("design:returntype", Promise)
813
+ _ts_metadata5("design:returntype", Promise)
632
814
  ], ChatController.prototype, "chat", null);
633
- _ts_decorate4([
634
- (0, import_common4.Get)("chat/:runId/stream"),
635
- _ts_param4(0, (0, import_common4.Param)("runId")),
636
- _ts_param4(1, (0, import_common4.Res)()),
637
- _ts_metadata4("design:type", Function),
638
- _ts_metadata4("design:paramtypes", [
815
+ _ts_decorate5([
816
+ (0, import_common5.Get)("chat/:runId/stream"),
817
+ _ts_param5(0, (0, import_common5.Param)("runId")),
818
+ _ts_param5(1, (0, import_common5.Res)()),
819
+ _ts_metadata5("design:type", Function),
820
+ _ts_metadata5("design:paramtypes", [
639
821
  String,
640
822
  typeof Response === "undefined" ? Object : Response
641
823
  ]),
642
- _ts_metadata4("design:returntype", Promise)
824
+ _ts_metadata5("design:returntype", Promise)
643
825
  ], ChatController.prototype, "stream", null);
644
- _ts_decorate4([
645
- (0, import_common4.Post)("chat/:runId/cancel"),
646
- _ts_param4(0, (0, import_common4.Req)()),
647
- _ts_param4(1, (0, import_common4.Param)("runId")),
648
- _ts_metadata4("design:type", Function),
649
- _ts_metadata4("design:paramtypes", [
826
+ _ts_decorate5([
827
+ (0, import_common5.Post)("chat/:runId/cancel"),
828
+ _ts_param5(0, (0, import_common5.Req)()),
829
+ _ts_param5(1, (0, import_common5.Param)("runId")),
830
+ _ts_metadata5("design:type", Function),
831
+ _ts_metadata5("design:paramtypes", [
650
832
  typeof Request === "undefined" ? Object : Request,
651
833
  String
652
834
  ]),
653
- _ts_metadata4("design:returntype", Promise)
835
+ _ts_metadata5("design:returntype", Promise)
654
836
  ], ChatController.prototype, "cancel", null);
655
- ChatController = _ts_decorate4([
656
- (0, import_common4.Controller)(),
657
- _ts_param4(1, (0, import_common4.Inject)(import_nestjs_agent_core5.AGENT_ACTOR_RESOLVER)),
658
- _ts_metadata4("design:type", Function),
659
- _ts_metadata4("design:paramtypes", [
837
+ ChatController = _ts_decorate5([
838
+ (0, import_common5.Controller)(),
839
+ _ts_param5(1, (0, import_common5.Inject)(import_nestjs_agent_core6.AGENT_ACTOR_RESOLVER)),
840
+ _ts_metadata5("design:type", Function),
841
+ _ts_metadata5("design:paramtypes", [
660
842
  typeof AgentService === "undefined" ? Object : AgentService,
661
843
  typeof ActorResolver === "undefined" ? Object : ActorResolver
662
844
  ])
663
845
  ], ChatController);
664
846
 
665
847
  // src/controller/quota.controller.ts
666
- var import_nestjs_agent_core6 = require("@dudousxd/nestjs-agent-core");
667
- var import_common5 = require("@nestjs/common");
668
- function _ts_decorate5(decorators, target, key, desc) {
848
+ var import_nestjs_agent_core7 = require("@dudousxd/nestjs-agent-core");
849
+ var import_common6 = require("@nestjs/common");
850
+ function _ts_decorate6(decorators, target, key, desc) {
669
851
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
670
852
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
671
853
  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;
672
854
  return c > 3 && r && Object.defineProperty(target, key, r), r;
673
855
  }
674
- __name(_ts_decorate5, "_ts_decorate");
675
- function _ts_metadata5(k, v) {
856
+ __name(_ts_decorate6, "_ts_decorate");
857
+ function _ts_metadata6(k, v) {
676
858
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
677
859
  }
678
- __name(_ts_metadata5, "_ts_metadata");
679
- function _ts_param5(paramIndex, decorator) {
860
+ __name(_ts_metadata6, "_ts_metadata");
861
+ function _ts_param6(paramIndex, decorator) {
680
862
  return function(target, key) {
681
863
  decorator(target, key, paramIndex);
682
864
  };
683
865
  }
684
- __name(_ts_param5, "_ts_param");
866
+ __name(_ts_param6, "_ts_param");
685
867
  var QuotaController = class {
686
868
  static {
687
869
  __name(this, "QuotaController");
@@ -697,45 +879,45 @@ var QuotaController = class {
697
879
  return this.agent.quotaToday(actor.id);
698
880
  }
699
881
  };
700
- _ts_decorate5([
701
- (0, import_common5.Get)("today"),
702
- _ts_param5(0, (0, import_common5.Req)()),
703
- _ts_metadata5("design:type", Function),
704
- _ts_metadata5("design:paramtypes", [
882
+ _ts_decorate6([
883
+ (0, import_common6.Get)("today"),
884
+ _ts_param6(0, (0, import_common6.Req)()),
885
+ _ts_metadata6("design:type", Function),
886
+ _ts_metadata6("design:paramtypes", [
705
887
  typeof Request === "undefined" ? Object : Request
706
888
  ]),
707
- _ts_metadata5("design:returntype", Promise)
889
+ _ts_metadata6("design:returntype", Promise)
708
890
  ], QuotaController.prototype, "today", null);
709
- QuotaController = _ts_decorate5([
710
- (0, import_common5.Controller)("quota"),
711
- _ts_param5(1, (0, import_common5.Inject)(import_nestjs_agent_core6.AGENT_ACTOR_RESOLVER)),
712
- _ts_metadata5("design:type", Function),
713
- _ts_metadata5("design:paramtypes", [
891
+ QuotaController = _ts_decorate6([
892
+ (0, import_common6.Controller)("quota"),
893
+ _ts_param6(1, (0, import_common6.Inject)(import_nestjs_agent_core7.AGENT_ACTOR_RESOLVER)),
894
+ _ts_metadata6("design:type", Function),
895
+ _ts_metadata6("design:paramtypes", [
714
896
  typeof AgentService === "undefined" ? Object : AgentService,
715
897
  typeof ActorResolver === "undefined" ? Object : ActorResolver
716
898
  ])
717
899
  ], QuotaController);
718
900
 
719
901
  // src/controller/threads.controller.ts
720
- var import_nestjs_agent_core7 = require("@dudousxd/nestjs-agent-core");
721
- var import_common6 = require("@nestjs/common");
722
- function _ts_decorate6(decorators, target, key, desc) {
902
+ var import_nestjs_agent_core8 = require("@dudousxd/nestjs-agent-core");
903
+ var import_common7 = require("@nestjs/common");
904
+ function _ts_decorate7(decorators, target, key, desc) {
723
905
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
724
906
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
725
907
  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;
726
908
  return c > 3 && r && Object.defineProperty(target, key, r), r;
727
909
  }
728
- __name(_ts_decorate6, "_ts_decorate");
729
- function _ts_metadata6(k, v) {
910
+ __name(_ts_decorate7, "_ts_decorate");
911
+ function _ts_metadata7(k, v) {
730
912
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
731
913
  }
732
- __name(_ts_metadata6, "_ts_metadata");
733
- function _ts_param6(paramIndex, decorator) {
914
+ __name(_ts_metadata7, "_ts_metadata");
915
+ function _ts_param7(paramIndex, decorator) {
734
916
  return function(target, key) {
735
917
  decorator(target, key, paramIndex);
736
918
  };
737
919
  }
738
- __name(_ts_param6, "_ts_param");
920
+ __name(_ts_param7, "_ts_param");
739
921
  var ThreadsController = class {
740
922
  static {
741
923
  __name(this, "ThreadsController");
@@ -765,9 +947,21 @@ var ThreadsController = class {
765
947
  const actor = await this.actorResolver.resolve(req);
766
948
  return this.agent.forkThread(actor, id, messageId);
767
949
  }
768
- async rename(req, id, body) {
950
+ /**
951
+ * Renames the thread and/or sets its default agent. `defaultAgent` is only present in the request
952
+ * body when the client actually sent the key (JSON has no `undefined`), so `'defaultAgent' in body`
953
+ * distinguishes "leave it alone" from "set it to null" (clear it) unambiguously.
954
+ */
955
+ async update(req, id, body) {
769
956
  const actor = await this.actorResolver.resolve(req);
770
- await this.agent.renameThread(actor, id, body.title);
957
+ await this.agent.updateThread(actor, id, {
958
+ ...body.title !== void 0 ? {
959
+ title: body.title
960
+ } : {},
961
+ ..."defaultAgent" in body ? {
962
+ defaultAgent: body.defaultAgent ?? null
963
+ } : {}
964
+ });
771
965
  return {
772
966
  ok: true
773
967
  };
@@ -787,117 +981,117 @@ var ThreadsController = class {
787
981
  };
788
982
  }
789
983
  };
790
- _ts_decorate6([
791
- (0, import_common6.Get)(),
792
- _ts_param6(0, (0, import_common6.Req)()),
793
- _ts_metadata6("design:type", Function),
794
- _ts_metadata6("design:paramtypes", [
984
+ _ts_decorate7([
985
+ (0, import_common7.Get)(),
986
+ _ts_param7(0, (0, import_common7.Req)()),
987
+ _ts_metadata7("design:type", Function),
988
+ _ts_metadata7("design:paramtypes", [
795
989
  typeof Request === "undefined" ? Object : Request
796
990
  ]),
797
- _ts_metadata6("design:returntype", Promise)
991
+ _ts_metadata7("design:returntype", Promise)
798
992
  ], ThreadsController.prototype, "list", null);
799
- _ts_decorate6([
800
- (0, import_common6.Get)(":id"),
801
- _ts_param6(0, (0, import_common6.Req)()),
802
- _ts_param6(1, (0, import_common6.Param)("id")),
803
- _ts_metadata6("design:type", Function),
804
- _ts_metadata6("design:paramtypes", [
993
+ _ts_decorate7([
994
+ (0, import_common7.Get)(":id"),
995
+ _ts_param7(0, (0, import_common7.Req)()),
996
+ _ts_param7(1, (0, import_common7.Param)("id")),
997
+ _ts_metadata7("design:type", Function),
998
+ _ts_metadata7("design:paramtypes", [
805
999
  typeof Request === "undefined" ? Object : Request,
806
1000
  String
807
1001
  ]),
808
- _ts_metadata6("design:returntype", Promise)
1002
+ _ts_metadata7("design:returntype", Promise)
809
1003
  ], ThreadsController.prototype, "detail", null);
810
- _ts_decorate6([
811
- (0, import_common6.Delete)(":id"),
812
- _ts_param6(0, (0, import_common6.Req)()),
813
- _ts_param6(1, (0, import_common6.Param)("id")),
814
- _ts_metadata6("design:type", Function),
815
- _ts_metadata6("design:paramtypes", [
1004
+ _ts_decorate7([
1005
+ (0, import_common7.Delete)(":id"),
1006
+ _ts_param7(0, (0, import_common7.Req)()),
1007
+ _ts_param7(1, (0, import_common7.Param)("id")),
1008
+ _ts_metadata7("design:type", Function),
1009
+ _ts_metadata7("design:paramtypes", [
816
1010
  typeof Request === "undefined" ? Object : Request,
817
1011
  String
818
1012
  ]),
819
- _ts_metadata6("design:returntype", Promise)
1013
+ _ts_metadata7("design:returntype", Promise)
820
1014
  ], ThreadsController.prototype, "remove", null);
821
- _ts_decorate6([
822
- (0, import_common6.Post)(":id/fork-from/:messageId"),
823
- _ts_param6(0, (0, import_common6.Req)()),
824
- _ts_param6(1, (0, import_common6.Param)("id")),
825
- _ts_param6(2, (0, import_common6.Param)("messageId")),
826
- _ts_metadata6("design:type", Function),
827
- _ts_metadata6("design:paramtypes", [
1015
+ _ts_decorate7([
1016
+ (0, import_common7.Post)(":id/fork-from/:messageId"),
1017
+ _ts_param7(0, (0, import_common7.Req)()),
1018
+ _ts_param7(1, (0, import_common7.Param)("id")),
1019
+ _ts_param7(2, (0, import_common7.Param)("messageId")),
1020
+ _ts_metadata7("design:type", Function),
1021
+ _ts_metadata7("design:paramtypes", [
828
1022
  typeof Request === "undefined" ? Object : Request,
829
1023
  String,
830
1024
  String
831
1025
  ]),
832
- _ts_metadata6("design:returntype", Promise)
1026
+ _ts_metadata7("design:returntype", Promise)
833
1027
  ], ThreadsController.prototype, "fork", null);
834
- _ts_decorate6([
835
- (0, import_common6.Patch)(":id"),
836
- _ts_param6(0, (0, import_common6.Req)()),
837
- _ts_param6(1, (0, import_common6.Param)("id")),
838
- _ts_param6(2, (0, import_common6.Body)()),
839
- _ts_metadata6("design:type", Function),
840
- _ts_metadata6("design:paramtypes", [
1028
+ _ts_decorate7([
1029
+ (0, import_common7.Patch)(":id"),
1030
+ _ts_param7(0, (0, import_common7.Req)()),
1031
+ _ts_param7(1, (0, import_common7.Param)("id")),
1032
+ _ts_param7(2, (0, import_common7.Body)()),
1033
+ _ts_metadata7("design:type", Function),
1034
+ _ts_metadata7("design:paramtypes", [
841
1035
  typeof Request === "undefined" ? Object : Request,
842
1036
  String,
843
- typeof RenameThreadBody === "undefined" ? Object : RenameThreadBody
1037
+ typeof UpdateThreadBody === "undefined" ? Object : UpdateThreadBody
844
1038
  ]),
845
- _ts_metadata6("design:returntype", Promise)
846
- ], ThreadsController.prototype, "rename", null);
847
- _ts_decorate6([
848
- (0, import_common6.Post)(":id/promote"),
849
- _ts_param6(0, (0, import_common6.Req)()),
850
- _ts_param6(1, (0, import_common6.Param)("id")),
851
- _ts_metadata6("design:type", Function),
852
- _ts_metadata6("design:paramtypes", [
1039
+ _ts_metadata7("design:returntype", Promise)
1040
+ ], ThreadsController.prototype, "update", null);
1041
+ _ts_decorate7([
1042
+ (0, import_common7.Post)(":id/promote"),
1043
+ _ts_param7(0, (0, import_common7.Req)()),
1044
+ _ts_param7(1, (0, import_common7.Param)("id")),
1045
+ _ts_metadata7("design:type", Function),
1046
+ _ts_metadata7("design:paramtypes", [
853
1047
  typeof Request === "undefined" ? Object : Request,
854
1048
  String
855
1049
  ]),
856
- _ts_metadata6("design:returntype", Promise)
1050
+ _ts_metadata7("design:returntype", Promise)
857
1051
  ], ThreadsController.prototype, "promote", null);
858
- _ts_decorate6([
859
- (0, import_common6.Delete)(":id/from/:messageId"),
860
- _ts_param6(0, (0, import_common6.Req)()),
861
- _ts_param6(1, (0, import_common6.Param)("id")),
862
- _ts_param6(2, (0, import_common6.Param)("messageId")),
863
- _ts_metadata6("design:type", Function),
864
- _ts_metadata6("design:paramtypes", [
1052
+ _ts_decorate7([
1053
+ (0, import_common7.Delete)(":id/from/:messageId"),
1054
+ _ts_param7(0, (0, import_common7.Req)()),
1055
+ _ts_param7(1, (0, import_common7.Param)("id")),
1056
+ _ts_param7(2, (0, import_common7.Param)("messageId")),
1057
+ _ts_metadata7("design:type", Function),
1058
+ _ts_metadata7("design:paramtypes", [
865
1059
  typeof Request === "undefined" ? Object : Request,
866
1060
  String,
867
1061
  String
868
1062
  ]),
869
- _ts_metadata6("design:returntype", Promise)
1063
+ _ts_metadata7("design:returntype", Promise)
870
1064
  ], ThreadsController.prototype, "truncateFrom", null);
871
- ThreadsController = _ts_decorate6([
872
- (0, import_common6.Controller)("threads"),
873
- _ts_param6(1, (0, import_common6.Inject)(import_nestjs_agent_core7.AGENT_ACTOR_RESOLVER)),
874
- _ts_metadata6("design:type", Function),
875
- _ts_metadata6("design:paramtypes", [
1065
+ ThreadsController = _ts_decorate7([
1066
+ (0, import_common7.Controller)("threads"),
1067
+ _ts_param7(1, (0, import_common7.Inject)(import_nestjs_agent_core8.AGENT_ACTOR_RESOLVER)),
1068
+ _ts_metadata7("design:type", Function),
1069
+ _ts_metadata7("design:paramtypes", [
876
1070
  typeof AgentService === "undefined" ? Object : AgentService,
877
1071
  typeof ActorResolver === "undefined" ? Object : ActorResolver
878
1072
  ])
879
1073
  ], ThreadsController);
880
1074
 
881
1075
  // src/controller/tool-call.controller.ts
882
- var import_nestjs_agent_core8 = require("@dudousxd/nestjs-agent-core");
883
- var import_common7 = require("@nestjs/common");
884
- function _ts_decorate7(decorators, target, key, desc) {
1076
+ var import_nestjs_agent_core9 = require("@dudousxd/nestjs-agent-core");
1077
+ var import_common8 = require("@nestjs/common");
1078
+ function _ts_decorate8(decorators, target, key, desc) {
885
1079
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
886
1080
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
887
1081
  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;
888
1082
  return c > 3 && r && Object.defineProperty(target, key, r), r;
889
1083
  }
890
- __name(_ts_decorate7, "_ts_decorate");
891
- function _ts_metadata7(k, v) {
1084
+ __name(_ts_decorate8, "_ts_decorate");
1085
+ function _ts_metadata8(k, v) {
892
1086
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
893
1087
  }
894
- __name(_ts_metadata7, "_ts_metadata");
895
- function _ts_param7(paramIndex, decorator) {
1088
+ __name(_ts_metadata8, "_ts_metadata");
1089
+ function _ts_param8(paramIndex, decorator) {
896
1090
  return function(target, key) {
897
1091
  decorator(target, key, paramIndex);
898
1092
  };
899
1093
  }
900
- __name(_ts_param7, "_ts_param");
1094
+ __name(_ts_param8, "_ts_param");
901
1095
  var ToolCallController = class {
902
1096
  static {
903
1097
  __name(this, "ToolCallController");
@@ -923,41 +1117,41 @@ var ToolCallController = class {
923
1117
  };
924
1118
  }
925
1119
  };
926
- _ts_decorate7([
927
- (0, import_common7.Post)("approve"),
928
- _ts_param7(0, (0, import_common7.Req)()),
929
- _ts_param7(1, (0, import_common7.Body)()),
930
- _ts_metadata7("design:type", Function),
931
- _ts_metadata7("design:paramtypes", [
1120
+ _ts_decorate8([
1121
+ (0, import_common8.Post)("approve"),
1122
+ _ts_param8(0, (0, import_common8.Req)()),
1123
+ _ts_param8(1, (0, import_common8.Body)()),
1124
+ _ts_metadata8("design:type", Function),
1125
+ _ts_metadata8("design:paramtypes", [
932
1126
  typeof Request === "undefined" ? Object : Request,
933
1127
  typeof ApproveBody === "undefined" ? Object : ApproveBody
934
1128
  ]),
935
- _ts_metadata7("design:returntype", Promise)
1129
+ _ts_metadata8("design:returntype", Promise)
936
1130
  ], ToolCallController.prototype, "approve", null);
937
- _ts_decorate7([
938
- (0, import_common7.Post)("reject"),
939
- _ts_param7(0, (0, import_common7.Req)()),
940
- _ts_param7(1, (0, import_common7.Body)()),
941
- _ts_metadata7("design:type", Function),
942
- _ts_metadata7("design:paramtypes", [
1131
+ _ts_decorate8([
1132
+ (0, import_common8.Post)("reject"),
1133
+ _ts_param8(0, (0, import_common8.Req)()),
1134
+ _ts_param8(1, (0, import_common8.Body)()),
1135
+ _ts_metadata8("design:type", Function),
1136
+ _ts_metadata8("design:paramtypes", [
943
1137
  typeof Request === "undefined" ? Object : Request,
944
1138
  typeof RejectBody === "undefined" ? Object : RejectBody
945
1139
  ]),
946
- _ts_metadata7("design:returntype", Promise)
1140
+ _ts_metadata8("design:returntype", Promise)
947
1141
  ], ToolCallController.prototype, "reject", null);
948
- ToolCallController = _ts_decorate7([
949
- (0, import_common7.Controller)("tool-call"),
950
- _ts_param7(1, (0, import_common7.Inject)(import_nestjs_agent_core8.AGENT_ACTOR_RESOLVER)),
951
- _ts_metadata7("design:type", Function),
952
- _ts_metadata7("design:paramtypes", [
1142
+ ToolCallController = _ts_decorate8([
1143
+ (0, import_common8.Controller)("tool-call"),
1144
+ _ts_param8(1, (0, import_common8.Inject)(import_nestjs_agent_core9.AGENT_ACTOR_RESOLVER)),
1145
+ _ts_metadata8("design:type", Function),
1146
+ _ts_metadata8("design:paramtypes", [
953
1147
  typeof AgentService === "undefined" ? Object : AgentService,
954
1148
  typeof ActorResolver === "undefined" ? Object : ActorResolver
955
1149
  ])
956
1150
  ], ToolCallController);
957
1151
 
958
1152
  // src/discovery/agent-discovery.service.ts
959
- var import_nestjs_agent_core9 = require("@dudousxd/nestjs-agent-core");
960
- var import_common8 = require("@nestjs/common");
1153
+ var import_nestjs_agent_core10 = require("@dudousxd/nestjs-agent-core");
1154
+ var import_common9 = require("@nestjs/common");
961
1155
  var import_core = require("@nestjs/core");
962
1156
 
963
1157
  // src/decorator/agent.decorator.ts
@@ -992,23 +1186,23 @@ function SystemPromptContributor() {
992
1186
  __name(SystemPromptContributor, "SystemPromptContributor");
993
1187
 
994
1188
  // src/discovery/agent-discovery.service.ts
995
- function _ts_decorate8(decorators, target, key, desc) {
1189
+ function _ts_decorate9(decorators, target, key, desc) {
996
1190
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
997
1191
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
998
1192
  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;
999
1193
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1000
1194
  }
1001
- __name(_ts_decorate8, "_ts_decorate");
1002
- function _ts_metadata8(k, v) {
1195
+ __name(_ts_decorate9, "_ts_decorate");
1196
+ function _ts_metadata9(k, v) {
1003
1197
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1004
1198
  }
1005
- __name(_ts_metadata8, "_ts_metadata");
1006
- function _ts_param8(paramIndex, decorator) {
1199
+ __name(_ts_metadata9, "_ts_metadata");
1200
+ function _ts_param9(paramIndex, decorator) {
1007
1201
  return function(target, key) {
1008
1202
  decorator(target, key, paramIndex);
1009
1203
  };
1010
1204
  }
1011
- __name(_ts_param8, "_ts_param");
1205
+ __name(_ts_param9, "_ts_param");
1012
1206
  var AgentDiscoveryService = class _AgentDiscoveryService {
1013
1207
  static {
1014
1208
  __name(this, "AgentDiscoveryService");
@@ -1017,7 +1211,7 @@ var AgentDiscoveryService = class _AgentDiscoveryService {
1017
1211
  scanner;
1018
1212
  registry;
1019
1213
  contributors;
1020
- logger = new import_common8.Logger(_AgentDiscoveryService.name);
1214
+ logger = new import_common9.Logger(_AgentDiscoveryService.name);
1021
1215
  constructor(discovery, scanner, registry, contributors) {
1022
1216
  this.discovery = discovery;
1023
1217
  this.scanner = scanner;
@@ -1105,22 +1299,22 @@ var AgentDiscoveryService = class _AgentDiscoveryService {
1105
1299
  return (ctx) => candidate.call(instance, ctx);
1106
1300
  }
1107
1301
  };
1108
- AgentDiscoveryService = _ts_decorate8([
1109
- (0, import_common8.Injectable)(),
1110
- _ts_param8(2, (0, import_common8.Inject)(import_nestjs_agent_core9.AGENT_REGISTRY)),
1111
- _ts_param8(3, (0, import_common8.Inject)(import_nestjs_agent_core9.AGENT_PROMPT_CONTRIBUTORS)),
1112
- _ts_metadata8("design:type", Function),
1113
- _ts_metadata8("design:paramtypes", [
1302
+ AgentDiscoveryService = _ts_decorate9([
1303
+ (0, import_common9.Injectable)(),
1304
+ _ts_param9(2, (0, import_common9.Inject)(import_nestjs_agent_core10.AGENT_REGISTRY)),
1305
+ _ts_param9(3, (0, import_common9.Inject)(import_nestjs_agent_core10.AGENT_PROMPT_CONTRIBUTORS)),
1306
+ _ts_metadata9("design:type", Function),
1307
+ _ts_metadata9("design:paramtypes", [
1114
1308
  typeof import_core.DiscoveryService === "undefined" ? Object : import_core.DiscoveryService,
1115
1309
  typeof import_core.MetadataScanner === "undefined" ? Object : import_core.MetadataScanner,
1116
- typeof import_nestjs_agent_core9.AgentRegistry === "undefined" ? Object : import_nestjs_agent_core9.AgentRegistry,
1310
+ typeof import_nestjs_agent_core10.AgentRegistry === "undefined" ? Object : import_nestjs_agent_core10.AgentRegistry,
1117
1311
  Array
1118
1312
  ])
1119
1313
  ], AgentDiscoveryService);
1120
1314
 
1121
1315
  // src/discovery/ai-tool-discovery.service.ts
1122
- var import_nestjs_agent_core10 = require("@dudousxd/nestjs-agent-core");
1123
- var import_common9 = require("@nestjs/common");
1316
+ var import_nestjs_agent_core11 = require("@dudousxd/nestjs-agent-core");
1317
+ var import_common10 = require("@nestjs/common");
1124
1318
  var import_core2 = require("@nestjs/core");
1125
1319
  var import_zod = require("zod");
1126
1320
 
@@ -1169,23 +1363,23 @@ function provideAgentTool(factoryOrTool, inject) {
1169
1363
  __name(provideAgentTool, "provideAgentTool");
1170
1364
 
1171
1365
  // src/discovery/ai-tool-discovery.service.ts
1172
- function _ts_decorate9(decorators, target, key, desc) {
1366
+ function _ts_decorate10(decorators, target, key, desc) {
1173
1367
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1174
1368
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1175
1369
  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;
1176
1370
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1177
1371
  }
1178
- __name(_ts_decorate9, "_ts_decorate");
1179
- function _ts_metadata9(k, v) {
1372
+ __name(_ts_decorate10, "_ts_decorate");
1373
+ function _ts_metadata10(k, v) {
1180
1374
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1181
1375
  }
1182
- __name(_ts_metadata9, "_ts_metadata");
1183
- function _ts_param9(paramIndex, decorator) {
1376
+ __name(_ts_metadata10, "_ts_metadata");
1377
+ function _ts_param10(paramIndex, decorator) {
1184
1378
  return function(target, key) {
1185
1379
  decorator(target, key, paramIndex);
1186
1380
  };
1187
1381
  }
1188
- __name(_ts_param9, "_ts_param");
1382
+ __name(_ts_param10, "_ts_param");
1189
1383
  var AiToolDiscoveryService = class _AiToolDiscoveryService {
1190
1384
  static {
1191
1385
  __name(this, "AiToolDiscoveryService");
@@ -1194,7 +1388,7 @@ var AiToolDiscoveryService = class _AiToolDiscoveryService {
1194
1388
  registry;
1195
1389
  agents;
1196
1390
  options;
1197
- logger = new import_common9.Logger(_AiToolDiscoveryService.name);
1391
+ logger = new import_common10.Logger(_AiToolDiscoveryService.name);
1198
1392
  constructor(discovery, registry, agents, options) {
1199
1393
  this.discovery = discovery;
1200
1394
  this.registry = registry;
@@ -1292,13 +1486,13 @@ var AiToolDiscoveryService = class _AiToolDiscoveryService {
1292
1486
  return true;
1293
1487
  }
1294
1488
  };
1295
- AiToolDiscoveryService = _ts_decorate9([
1296
- (0, import_common9.Injectable)(),
1297
- _ts_param9(1, (0, import_common9.Inject)(import_nestjs_agent_core10.AGENT_TOOL_REGISTRY)),
1298
- _ts_param9(2, (0, import_common9.Inject)(import_nestjs_agent_core10.AGENT_REGISTRY)),
1299
- _ts_param9(3, (0, import_common9.Inject)(import_nestjs_agent_core10.AGENT_OPTIONS)),
1300
- _ts_metadata9("design:type", Function),
1301
- _ts_metadata9("design:paramtypes", [
1489
+ AiToolDiscoveryService = _ts_decorate10([
1490
+ (0, import_common10.Injectable)(),
1491
+ _ts_param10(1, (0, import_common10.Inject)(import_nestjs_agent_core11.AGENT_TOOL_REGISTRY)),
1492
+ _ts_param10(2, (0, import_common10.Inject)(import_nestjs_agent_core11.AGENT_REGISTRY)),
1493
+ _ts_param10(3, (0, import_common10.Inject)(import_nestjs_agent_core11.AGENT_OPTIONS)),
1494
+ _ts_metadata10("design:type", Function),
1495
+ _ts_metadata10("design:paramtypes", [
1302
1496
  typeof import_core2.DiscoveryService === "undefined" ? Object : import_core2.DiscoveryService,
1303
1497
  typeof ToolRegistry === "undefined" ? Object : ToolRegistry,
1304
1498
  typeof AgentRegistry === "undefined" ? Object : AgentRegistry,
@@ -1307,7 +1501,7 @@ AiToolDiscoveryService = _ts_decorate9([
1307
1501
  ], AiToolDiscoveryService);
1308
1502
 
1309
1503
  // src/in-process-sink.ts
1310
- var import_nestjs_agent_core11 = require("@dudousxd/nestjs-agent-core");
1504
+ var import_nestjs_agent_core12 = require("@dudousxd/nestjs-agent-core");
1311
1505
  var InProcessTokenStreamSink = class {
1312
1506
  static {
1313
1507
  __name(this, "InProcessTokenStreamSink");
@@ -1362,7 +1556,7 @@ var InProcessTokenStreamSink = class {
1362
1556
  }
1363
1557
  if (buf.ended) {
1364
1558
  if (buf.failure !== void 0) {
1365
- throw new import_nestjs_agent_core11.AgentStreamError(buf.failure);
1559
+ throw new import_nestjs_agent_core12.AgentStreamError(buf.failure);
1366
1560
  }
1367
1561
  return;
1368
1562
  }
@@ -1398,32 +1592,32 @@ var LedgerQuotaStore = class {
1398
1592
  };
1399
1593
 
1400
1594
  // src/runner/inline-agent-runner.ts
1401
- var import_nestjs_agent_core12 = require("@dudousxd/nestjs-agent-core");
1402
- var import_common10 = require("@nestjs/common");
1403
- function _ts_decorate10(decorators, target, key, desc) {
1595
+ var import_nestjs_agent_core13 = require("@dudousxd/nestjs-agent-core");
1596
+ var import_common11 = require("@nestjs/common");
1597
+ function _ts_decorate11(decorators, target, key, desc) {
1404
1598
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1405
1599
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1406
1600
  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;
1407
1601
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1408
1602
  }
1409
- __name(_ts_decorate10, "_ts_decorate");
1410
- function _ts_metadata10(k, v) {
1603
+ __name(_ts_decorate11, "_ts_decorate");
1604
+ function _ts_metadata11(k, v) {
1411
1605
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1412
1606
  }
1413
- __name(_ts_metadata10, "_ts_metadata");
1414
- function _ts_param10(paramIndex, decorator) {
1607
+ __name(_ts_metadata11, "_ts_metadata");
1608
+ function _ts_param11(paramIndex, decorator) {
1415
1609
  return function(target, key) {
1416
1610
  decorator(target, key, paramIndex);
1417
1611
  };
1418
1612
  }
1419
- __name(_ts_param10, "_ts_param");
1613
+ __name(_ts_param11, "_ts_param");
1420
1614
  var InlineAgentRunner = class _InlineAgentRunner {
1421
1615
  static {
1422
1616
  __name(this, "InlineAgentRunner");
1423
1617
  }
1424
1618
  factory;
1425
1619
  store;
1426
- logger = new import_common10.Logger(_InlineAgentRunner.name);
1620
+ logger = new import_common11.Logger(_InlineAgentRunner.name);
1427
1621
  pending = /* @__PURE__ */ new Map();
1428
1622
  constructor(factory, store) {
1429
1623
  this.factory = factory;
@@ -1434,18 +1628,19 @@ var InlineAgentRunner = class _InlineAgentRunner {
1434
1628
  const day = input.day ?? utcDay();
1435
1629
  const deps = this.factory.forAgent(input.agentName);
1436
1630
  const hooks = this.topLevelHooks(runId, deps, input.actor, day);
1437
- void (0, import_nestjs_agent_core12.runAgentLoop)({
1631
+ void (0, import_nestjs_agent_core13.runAgentLoop)({
1438
1632
  ...deps,
1439
1633
  day
1440
- }, input, hooks).catch(async (error) => {
1634
+ }, input, hooks).then(() => this.store.setActiveStream(input.threadId, null)).catch(async (error) => {
1441
1635
  const message = error instanceof Error ? error.message : String(error);
1442
- const code = error instanceof import_nestjs_agent_core12.QuotaExceededError ? "quota_exceeded" : "run_failed";
1636
+ const code = error instanceof import_nestjs_agent_core13.QuotaExceededError ? "quota_exceeded" : "run_failed";
1443
1637
  this.logger.error(`agent run ${runId} failed (${code}): ${message}`);
1444
- (0, import_nestjs_agent_core12.publishAgentRunFailed)({
1638
+ (0, import_nestjs_agent_core13.publishAgentRunFailed)({
1445
1639
  runId,
1446
1640
  code,
1447
1641
  message
1448
1642
  });
1643
+ await this.store.setActiveStream(input.threadId, null);
1449
1644
  const writer = await deps.sink.open(runId);
1450
1645
  await writer.fail({
1451
1646
  code,
@@ -1503,98 +1698,102 @@ var InlineAgentRunner = class _InlineAgentRunner {
1503
1698
  step: /* @__PURE__ */ __name((_name, fn) => fn(), "step"),
1504
1699
  runAgent: /* @__PURE__ */ __name((childName, childTask) => this.runNested(childName, childTask, actor, day, depth + 1, sinkRunId), "runAgent")
1505
1700
  };
1506
- return (0, import_nestjs_agent_core12.runAgentLoop)({
1507
- ...deps,
1508
- day
1509
- }, {
1510
- threadId: subThread.id,
1511
- actor,
1512
- userText: task,
1513
- agentName,
1514
- day,
1515
- delegationDepth: depth,
1516
- sinkRunId
1517
- }, hooks);
1701
+ try {
1702
+ return await (0, import_nestjs_agent_core13.runAgentLoop)({
1703
+ ...deps,
1704
+ day
1705
+ }, {
1706
+ threadId: subThread.id,
1707
+ actor,
1708
+ userText: task,
1709
+ agentName,
1710
+ day,
1711
+ delegationDepth: depth,
1712
+ sinkRunId
1713
+ }, hooks);
1714
+ } finally {
1715
+ await this.store.setActiveStream(subThread.id, null);
1716
+ }
1518
1717
  }
1519
1718
  };
1520
- InlineAgentRunner = _ts_decorate10([
1521
- (0, import_common10.Injectable)(),
1522
- _ts_param10(0, (0, import_common10.Inject)(import_nestjs_agent_core12.AGENT_DEPS_FACTORY)),
1523
- _ts_param10(1, (0, import_common10.Inject)(import_nestjs_agent_core12.AGENT_STORE)),
1524
- _ts_metadata10("design:type", Function),
1525
- _ts_metadata10("design:paramtypes", [
1719
+ InlineAgentRunner = _ts_decorate11([
1720
+ (0, import_common11.Injectable)(),
1721
+ _ts_param11(0, (0, import_common11.Inject)(import_nestjs_agent_core13.AGENT_DEPS_FACTORY)),
1722
+ _ts_param11(1, (0, import_common11.Inject)(import_nestjs_agent_core13.AGENT_STORE)),
1723
+ _ts_metadata11("design:type", Function),
1724
+ _ts_metadata11("design:paramtypes", [
1526
1725
  typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory,
1527
1726
  typeof AgentStore === "undefined" ? Object : AgentStore
1528
1727
  ])
1529
1728
  ], InlineAgentRunner);
1530
1729
 
1531
1730
  // src/agent.module.ts
1532
- function _ts_decorate11(decorators, target, key, desc) {
1731
+ function _ts_decorate12(decorators, target, key, desc) {
1533
1732
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1534
1733
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1535
1734
  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;
1536
1735
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1537
1736
  }
1538
- __name(_ts_decorate11, "_ts_decorate");
1737
+ __name(_ts_decorate12, "_ts_decorate");
1539
1738
  var DEFAULT_PATH = "agent";
1540
1739
  function sharedProviders(durable, includeStore) {
1541
1740
  const providers = [
1542
1741
  {
1543
- provide: import_nestjs_agent_core13.AGENT_TOOL_REGISTRY,
1544
- useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core13.ToolRegistry(), "useFactory")
1742
+ provide: import_nestjs_agent_core14.AGENT_TOOL_REGISTRY,
1743
+ useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core14.ToolRegistry(), "useFactory")
1545
1744
  },
1546
1745
  // Starts empty; AgentDiscoveryService populates it from `@Agent`-decorated providers at boot.
1547
1746
  {
1548
- provide: import_nestjs_agent_core13.AGENT_REGISTRY,
1549
- useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core13.AgentRegistry(), "useFactory")
1747
+ provide: import_nestjs_agent_core14.AGENT_REGISTRY,
1748
+ useFactory: /* @__PURE__ */ __name(() => new import_nestjs_agent_core14.AgentRegistry(), "useFactory")
1550
1749
  },
1551
1750
  // A shared, mutable list AgentDiscoveryService fills with `@SystemPromptContributor()` methods.
1552
1751
  {
1553
- provide: import_nestjs_agent_core13.AGENT_PROMPT_CONTRIBUTORS,
1752
+ provide: import_nestjs_agent_core14.AGENT_PROMPT_CONTRIBUTORS,
1554
1753
  useFactory: /* @__PURE__ */ __name(() => [], "useFactory")
1555
1754
  },
1556
1755
  {
1557
- provide: import_nestjs_agent_core13.AGENT_SINK,
1756
+ provide: import_nestjs_agent_core14.AGENT_SINK,
1558
1757
  useFactory: /* @__PURE__ */ __name((options) => options.sink ?? new InProcessTokenStreamSink(), "useFactory"),
1559
1758
  inject: [
1560
- import_nestjs_agent_core13.AGENT_OPTIONS
1759
+ import_nestjs_agent_core14.AGENT_OPTIONS
1561
1760
  ]
1562
1761
  },
1563
1762
  {
1564
- provide: import_nestjs_agent_core13.AGENT_ROLES_POLICY,
1565
- useFactory: /* @__PURE__ */ __name((options) => options.rolesPolicy ?? new import_nestjs_agent_core13.DefaultRolesPolicy(options.defaultRoles), "useFactory"),
1763
+ provide: import_nestjs_agent_core14.AGENT_ROLES_POLICY,
1764
+ useFactory: /* @__PURE__ */ __name((options) => options.rolesPolicy ?? new import_nestjs_agent_core14.DefaultRolesPolicy(options.defaultRoles), "useFactory"),
1566
1765
  inject: [
1567
- import_nestjs_agent_core13.AGENT_OPTIONS
1766
+ import_nestjs_agent_core14.AGENT_OPTIONS
1568
1767
  ]
1569
1768
  },
1570
1769
  {
1571
- provide: import_nestjs_agent_core13.AGENT_ACTOR_RESOLVER,
1770
+ provide: import_nestjs_agent_core14.AGENT_ACTOR_RESOLVER,
1572
1771
  useFactory: /* @__PURE__ */ __name((options) => options.actorResolver, "useFactory"),
1573
1772
  inject: [
1574
- import_nestjs_agent_core13.AGENT_OPTIONS
1773
+ import_nestjs_agent_core14.AGENT_OPTIONS
1575
1774
  ]
1576
1775
  },
1577
1776
  {
1578
- provide: import_nestjs_agent_core13.AGENT_MODEL,
1777
+ provide: import_nestjs_agent_core14.AGENT_MODEL,
1579
1778
  useFactory: /* @__PURE__ */ __name((o) => o.model, "useFactory"),
1580
1779
  inject: [
1581
- import_nestjs_agent_core13.AGENT_OPTIONS
1780
+ import_nestjs_agent_core14.AGENT_OPTIONS
1582
1781
  ]
1583
1782
  },
1584
1783
  {
1585
1784
  // An explicit `quota` wins; otherwise `quotaLimitTokens` binds the built-in ledger-backed
1586
1785
  // store; otherwise quotas are off (undefined). The ledger store reads the same usage rows
1587
1786
  // the loop already writes, so enforcement needs no extra shared state across replicas.
1588
- provide: import_nestjs_agent_core13.AGENT_QUOTA_STORE,
1787
+ provide: import_nestjs_agent_core14.AGENT_QUOTA_STORE,
1589
1788
  useFactory: /* @__PURE__ */ __name((o, store) => o.quota ?? (o.quotaLimitTokens !== void 0 ? new LedgerQuotaStore(store, o.quotaLimitTokens) : void 0), "useFactory"),
1590
1789
  inject: [
1591
- import_nestjs_agent_core13.AGENT_OPTIONS,
1592
- import_nestjs_agent_core13.AGENT_STORE
1790
+ import_nestjs_agent_core14.AGENT_OPTIONS,
1791
+ import_nestjs_agent_core14.AGENT_STORE
1593
1792
  ]
1594
1793
  },
1595
1794
  AgentDepsFactory2,
1596
1795
  {
1597
- provide: import_nestjs_agent_core13.AGENT_DEPS_FACTORY,
1796
+ provide: import_nestjs_agent_core14.AGENT_DEPS_FACTORY,
1598
1797
  useExisting: AgentDepsFactory2
1599
1798
  },
1600
1799
  // Populates the registry + contributors (onModuleInit) BEFORE AiToolDiscoveryService synthesizes
@@ -1606,16 +1805,16 @@ function sharedProviders(durable, includeStore) {
1606
1805
  ];
1607
1806
  if (includeStore) {
1608
1807
  providers.push({
1609
- provide: import_nestjs_agent_core13.AGENT_STORE,
1808
+ provide: import_nestjs_agent_core14.AGENT_STORE,
1610
1809
  useFactory: /* @__PURE__ */ __name((o) => o.store, "useFactory"),
1611
1810
  inject: [
1612
- import_nestjs_agent_core13.AGENT_OPTIONS
1811
+ import_nestjs_agent_core14.AGENT_OPTIONS
1613
1812
  ]
1614
1813
  });
1615
1814
  }
1616
1815
  if (durable) {
1617
1816
  providers.push({
1618
- provide: import_nestjs_agent_core13.AGENT_RUNNER,
1817
+ provide: import_nestjs_agent_core14.AGENT_RUNNER,
1619
1818
  useFactory: /* @__PURE__ */ __name((durableRunner) => {
1620
1819
  if (durableRunner === void 0) {
1621
1820
  throw new Error("AgentModule.forRoot({ durable: true }) requires importing AgentDurableModule from '@dudousxd/nestjs-agent/durable' (alongside a configured DurableModule). It was not found.");
@@ -1624,14 +1823,14 @@ function sharedProviders(durable, includeStore) {
1624
1823
  }, "useFactory"),
1625
1824
  inject: [
1626
1825
  {
1627
- token: import_nestjs_agent_core13.AGENT_DURABLE_RUNNER,
1826
+ token: import_nestjs_agent_core14.AGENT_DURABLE_RUNNER,
1628
1827
  optional: true
1629
1828
  }
1630
1829
  ]
1631
1830
  });
1632
1831
  } else {
1633
1832
  providers.push({
1634
- provide: import_nestjs_agent_core13.AGENT_RUNNER,
1833
+ provide: import_nestjs_agent_core14.AGENT_RUNNER,
1635
1834
  useExisting: InlineAgentRunner
1636
1835
  });
1637
1836
  }
@@ -1640,32 +1839,57 @@ function sharedProviders(durable, includeStore) {
1640
1839
  __name(sharedProviders, "sharedProviders");
1641
1840
  function exportsFor(includeStore) {
1642
1841
  return [
1643
- import_nestjs_agent_core13.AGENT_OPTIONS,
1644
- import_nestjs_agent_core13.AGENT_TOOL_REGISTRY,
1645
- import_nestjs_agent_core13.AGENT_REGISTRY,
1646
- import_nestjs_agent_core13.AGENT_SINK,
1647
- import_nestjs_agent_core13.AGENT_ROLES_POLICY,
1648
- import_nestjs_agent_core13.AGENT_ACTOR_RESOLVER,
1649
- import_nestjs_agent_core13.AGENT_MODEL,
1842
+ import_nestjs_agent_core14.AGENT_OPTIONS,
1843
+ import_nestjs_agent_core14.AGENT_TOOL_REGISTRY,
1844
+ import_nestjs_agent_core14.AGENT_REGISTRY,
1845
+ import_nestjs_agent_core14.AGENT_SINK,
1846
+ import_nestjs_agent_core14.AGENT_ROLES_POLICY,
1847
+ import_nestjs_agent_core14.AGENT_ACTOR_RESOLVER,
1848
+ import_nestjs_agent_core14.AGENT_MODEL,
1650
1849
  ...includeStore ? [
1651
- import_nestjs_agent_core13.AGENT_STORE
1850
+ import_nestjs_agent_core14.AGENT_STORE
1652
1851
  ] : [],
1653
- import_nestjs_agent_core13.AGENT_QUOTA_STORE,
1654
- import_nestjs_agent_core13.AGENT_PROMPT_CONTRIBUTORS,
1655
- import_nestjs_agent_core13.AGENT_DEPS_FACTORY,
1852
+ import_nestjs_agent_core14.AGENT_QUOTA_STORE,
1853
+ import_nestjs_agent_core14.AGENT_PROMPT_CONTRIBUTORS,
1854
+ import_nestjs_agent_core14.AGENT_DEPS_FACTORY,
1656
1855
  AgentDepsFactory2,
1657
1856
  AgentService,
1658
1857
  InlineAgentRunner
1659
1858
  ];
1660
1859
  }
1661
1860
  __name(exportsFor, "exportsFor");
1662
- var CONTROLLERS = [
1861
+ var BASE_CONTROLLERS = [
1663
1862
  ChatController,
1664
1863
  ThreadsController,
1665
1864
  ToolCallController,
1666
1865
  QuotaController,
1667
1866
  AgentsController
1668
1867
  ];
1868
+ var GUARDABLE_CONTROLLERS = [
1869
+ ...BASE_CONTROLLERS,
1870
+ AttachmentsController
1871
+ ];
1872
+ function controllersFor(mountAttachments) {
1873
+ return mountAttachments ? [
1874
+ ...BASE_CONTROLLERS,
1875
+ AttachmentsController
1876
+ ] : BASE_CONTROLLERS;
1877
+ }
1878
+ __name(controllersFor, "controllersFor");
1879
+ var GUARDS_METADATA = "__guards__";
1880
+ function applyGuards(guards) {
1881
+ const resolved = guards ?? [];
1882
+ for (const controller of GUARDABLE_CONTROLLERS) {
1883
+ Reflect.defineMetadata(GUARDS_METADATA, resolved, controller);
1884
+ }
1885
+ }
1886
+ __name(applyGuards, "applyGuards");
1887
+ function guardProviders(guards) {
1888
+ return [
1889
+ ...new Set(guards ?? [])
1890
+ ];
1891
+ }
1892
+ __name(guardProviders, "guardProviders");
1669
1893
  function routerFor(path) {
1670
1894
  return import_core3.RouterModule.register([
1671
1895
  {
@@ -1682,6 +1906,7 @@ var AgentModule = class _AgentModule {
1682
1906
  static forRoot(options) {
1683
1907
  const path = options.path ?? DEFAULT_PATH;
1684
1908
  const includeStore = options.store !== void 0;
1909
+ applyGuards(options.guards);
1685
1910
  return {
1686
1911
  module: _AgentModule,
1687
1912
  global: true,
@@ -1689,13 +1914,14 @@ var AgentModule = class _AgentModule {
1689
1914
  import_core3.DiscoveryModule,
1690
1915
  routerFor(path)
1691
1916
  ],
1692
- controllers: CONTROLLERS,
1917
+ controllers: controllersFor(options.attachments?.upload === true),
1693
1918
  providers: [
1694
1919
  {
1695
- provide: import_nestjs_agent_core13.AGENT_OPTIONS,
1920
+ provide: import_nestjs_agent_core14.AGENT_OPTIONS,
1696
1921
  useValue: options
1697
1922
  },
1698
- ...sharedProviders(options.durable ?? false, includeStore)
1923
+ ...sharedProviders(options.durable ?? false, includeStore),
1924
+ ...guardProviders(options.guards)
1699
1925
  ],
1700
1926
  exports: exportsFor(includeStore)
1701
1927
  };
@@ -1703,6 +1929,7 @@ var AgentModule = class _AgentModule {
1703
1929
  static forRootAsync(options) {
1704
1930
  const path = options.path ?? DEFAULT_PATH;
1705
1931
  const includeStore = options.externalStore !== true;
1932
+ applyGuards(options.guards);
1706
1933
  return {
1707
1934
  module: _AgentModule,
1708
1935
  global: true,
@@ -1711,22 +1938,23 @@ var AgentModule = class _AgentModule {
1711
1938
  routerFor(path),
1712
1939
  ...options.imports ?? []
1713
1940
  ],
1714
- controllers: CONTROLLERS,
1941
+ controllers: controllersFor(options.attachmentsUpload === true),
1715
1942
  providers: [
1716
1943
  {
1717
- provide: import_nestjs_agent_core13.AGENT_OPTIONS,
1944
+ provide: import_nestjs_agent_core14.AGENT_OPTIONS,
1718
1945
  useFactory: options.useFactory,
1719
1946
  inject: options.inject ?? []
1720
1947
  },
1721
- ...sharedProviders(options.durable ?? false, includeStore)
1948
+ ...sharedProviders(options.durable ?? false, includeStore),
1949
+ ...guardProviders(options.guards)
1722
1950
  ],
1723
1951
  exports: exportsFor(includeStore)
1724
1952
  };
1725
1953
  }
1726
1954
  };
1727
- AgentModule = _ts_decorate11([
1728
- (0, import_common11.Global)(),
1729
- (0, import_common11.Module)({})
1955
+ AgentModule = _ts_decorate12([
1956
+ (0, import_common12.Global)(),
1957
+ (0, import_common12.Module)({})
1730
1958
  ], AgentModule);
1731
1959
 
1732
1960
  // src/resolver/header-actor-resolver.ts
@@ -1773,6 +2001,9 @@ __reExport(index_exports, require("@dudousxd/nestjs-agent-core"), module.exports
1773
2001
  AgentService,
1774
2002
  AiTool,
1775
2003
  AiToolDiscoveryService,
2004
+ AttachmentsController,
2005
+ DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES,
2006
+ DEFAULT_MAX_ATTACHMENT_BYTES,
1776
2007
  HeaderActorResolver,
1777
2008
  InProcessTokenStreamSink,
1778
2009
  InlineAgentRunner,