@dudousxd/nestjs-agent 0.3.2 → 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.js CHANGED
@@ -2,14 +2,14 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // src/agent.module.ts
5
- import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER5, AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY4, AGENT_DURABLE_RUNNER, AGENT_MODEL as AGENT_MODEL2, AGENT_OPTIONS as AGENT_OPTIONS3, AGENT_PROMPT_CONTRIBUTORS as AGENT_PROMPT_CONTRIBUTORS3, AGENT_QUOTA_STORE as AGENT_QUOTA_STORE3, AGENT_REGISTRY as AGENT_REGISTRY5, AGENT_ROLES_POLICY as AGENT_ROLES_POLICY2, AGENT_RUNNER as AGENT_RUNNER2, AGENT_SINK as AGENT_SINK2, AGENT_STORE as AGENT_STORE4, AGENT_TOOL_REGISTRY as AGENT_TOOL_REGISTRY3, AgentRegistry as AgentRegistry5, DefaultRolesPolicy, ToolRegistry as ToolRegistry3 } from "@dudousxd/nestjs-agent-core";
5
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER6, AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY4, AGENT_DURABLE_RUNNER, AGENT_MODEL as AGENT_MODEL2, AGENT_OPTIONS as AGENT_OPTIONS4, AGENT_PROMPT_CONTRIBUTORS as AGENT_PROMPT_CONTRIBUTORS3, AGENT_QUOTA_STORE as AGENT_QUOTA_STORE3, AGENT_REGISTRY as AGENT_REGISTRY5, AGENT_ROLES_POLICY as AGENT_ROLES_POLICY2, AGENT_RUNNER as AGENT_RUNNER2, AGENT_SINK as AGENT_SINK2, AGENT_STORE as AGENT_STORE4, AGENT_TOOL_REGISTRY as AGENT_TOOL_REGISTRY3, AgentRegistry as AgentRegistry5, DefaultRolesPolicy, ToolRegistry as ToolRegistry3 } from "@dudousxd/nestjs-agent-core";
6
6
  import { Global, Module } from "@nestjs/common";
7
7
  import { DiscoveryModule, RouterModule } from "@nestjs/core";
8
8
 
9
9
  // src/agent-deps.factory.ts
10
- import { AGENT_MODEL, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_ROLES_POLICY, AGENT_SINK, AGENT_STORE, AGENT_TOOL_REGISTRY, AgentRegistry as AgentRegistry2, ToolRegistry as ToolRegistry2 } from "@dudousxd/nestjs-agent-core";
10
+ import { AGENT_MODEL, AGENT_PRICING_STORE, AGENT_PROMPT_CONTRIBUTORS, AGENT_QUOTA_STORE, AGENT_REGISTRY, AGENT_ROLES_POLICY, AGENT_SINK, AGENT_STORE, AGENT_TOOL_REGISTRY, AgentRegistry as AgentRegistry2, ToolRegistry as ToolRegistry2 } from "@dudousxd/nestjs-agent-core";
11
11
  import { AGENT_OPTIONS } from "@dudousxd/nestjs-agent-core";
12
- import { Inject, Injectable } from "@nestjs/common";
12
+ import { Inject, Injectable, Optional } from "@nestjs/common";
13
13
  function _ts_decorate(decorators, target, key, desc) {
14
14
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15
15
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -44,7 +44,8 @@ var AgentDepsFactory2 = class {
44
44
  agents;
45
45
  promptContributors;
46
46
  quota;
47
- constructor(options, model, store, sink, rolesPolicy, registry, agents, promptContributors, quota) {
47
+ pricingStore;
48
+ constructor(options, model, store, sink, rolesPolicy, registry, agents, promptContributors, quota, pricingStore) {
48
49
  this.options = options;
49
50
  this.model = model;
50
51
  this.store = store;
@@ -54,6 +55,7 @@ var AgentDepsFactory2 = class {
54
55
  this.agents = agents;
55
56
  this.promptContributors = promptContributors;
56
57
  this.quota = quota;
58
+ this.pricingStore = pricingStore;
57
59
  }
58
60
  /**
59
61
  * The agent a turn uses when the caller names none: the explicit `defaultAgent` option, else the
@@ -100,6 +102,9 @@ var AgentDepsFactory2 = class {
100
102
  ...this.quota !== void 0 ? {
101
103
  quota: this.quota
102
104
  } : {},
105
+ ...this.pricingStore !== void 0 ? {
106
+ pricingStore: this.pricingStore
107
+ } : {},
103
108
  ...toolAllowList !== void 0 ? {
104
109
  toolAllowList
105
110
  } : {},
@@ -140,6 +145,8 @@ AgentDepsFactory2 = _ts_decorate([
140
145
  _ts_param(6, Inject(AGENT_REGISTRY)),
141
146
  _ts_param(7, Inject(AGENT_PROMPT_CONTRIBUTORS)),
142
147
  _ts_param(8, Inject(AGENT_QUOTA_STORE)),
148
+ _ts_param(9, Optional()),
149
+ _ts_param(9, Inject(AGENT_PRICING_STORE)),
143
150
  _ts_metadata("design:type", Function),
144
151
  _ts_metadata("design:paramtypes", [
145
152
  typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
@@ -150,13 +157,14 @@ AgentDepsFactory2 = _ts_decorate([
150
157
  typeof ToolRegistry2 === "undefined" ? Object : ToolRegistry2,
151
158
  typeof AgentRegistry2 === "undefined" ? Object : AgentRegistry2,
152
159
  Array,
160
+ Object,
153
161
  Object
154
162
  ])
155
163
  ], AgentDepsFactory2);
156
164
 
157
165
  // src/agent.service.ts
158
166
  import { AGENT_DEPS_FACTORY, AGENT_QUOTA_STORE as AGENT_QUOTA_STORE2, AGENT_RUNNER, AGENT_STORE as AGENT_STORE2 } from "@dudousxd/nestjs-agent-core";
159
- import { BadRequestException, ForbiddenException, Inject as Inject2, Injectable as Injectable2, NotFoundException } from "@nestjs/common";
167
+ import { BadRequestException, ForbiddenException, Inject as Inject2, Injectable as Injectable2, NotFoundException, NotImplementedException } from "@nestjs/common";
160
168
 
161
169
  // src/agent-deps.ts
162
170
  function utcDay(date = /* @__PURE__ */ new Date()) {
@@ -207,7 +215,7 @@ var AgentService = class {
207
215
  this.quota = quota;
208
216
  }
209
217
  async chat(params) {
210
- const agentName = params.agentName ?? this.deps.defaultAgentName();
218
+ const agentName = await this.resolveAgentName(params.agentName, params.threadId);
211
219
  let threadId = params.threadId;
212
220
  if (threadId === void 0) {
213
221
  if (params.regenerate === true) {
@@ -232,6 +240,9 @@ var AgentService = class {
232
240
  ...params.regenerate === true ? {
233
241
  regenerate: true
234
242
  } : {},
243
+ ...params.attachments !== void 0 ? {
244
+ attachments: params.attachments
245
+ } : {},
235
246
  ...params.pageContext !== void 0 ? {
236
247
  pageContext: params.pageContext
237
248
  } : {}
@@ -265,12 +276,14 @@ var AgentService = class {
265
276
  await this.assertOwnsActiveStream(actor, runId);
266
277
  return this.runner.cancel(runId);
267
278
  }
268
- listThreads(actorRef) {
269
- return this.store.listThreads(actorRef);
279
+ async listThreads(actorRef) {
280
+ const threads = await this.store.listThreads(actorRef);
281
+ return Promise.all(threads.map((thread) => this.toSummaryView(thread)));
270
282
  }
271
283
  async getThread(actor, threadId) {
272
284
  await this.assertOwnsThread(actor, threadId);
273
- return this.store.getThread(threadId);
285
+ const thread = await this.store.getThread(threadId);
286
+ return thread === null ? null : this.toDetailView(thread);
274
287
  }
275
288
  async deleteThread(actor, threadId) {
276
289
  await this.assertOwnsThread(actor, threadId);
@@ -280,7 +293,38 @@ var AgentService = class {
280
293
  await this.assertOwnsThread(actor, threadId);
281
294
  return this.store.forkThread(threadId, fromMessageId);
282
295
  }
296
+ /** Kept for source compatibility — a thin wrapper over the more general `updateThread`. */
283
297
  async renameThread(actor, threadId, title) {
298
+ return this.updateThread(actor, threadId, {
299
+ title
300
+ });
301
+ }
302
+ /**
303
+ * Rename a thread and/or set its default agent. Title-only patches work against ANY store (via
304
+ * the required `setTitle`); a `defaultAgent` change requires the bound store to implement the
305
+ * optional `updateThread` — 501 with a clear message when it doesn't, rather than silently
306
+ * dropping the field.
307
+ */
308
+ async updateThread(actor, threadId, patch) {
309
+ const title = patch.title !== void 0 ? this.validateTitle(patch.title) : void 0;
310
+ await this.assertOwnsThread(actor, threadId);
311
+ if (patch.defaultAgent === void 0) {
312
+ if (title !== void 0) {
313
+ await this.store.setTitle(threadId, title);
314
+ }
315
+ return;
316
+ }
317
+ if (this.store.updateThread === void 0) {
318
+ throw new NotImplementedException("Setting a thread's defaultAgent requires an AgentStore that implements updateThread(); the bound store does not support it.");
319
+ }
320
+ await this.store.updateThread(threadId, {
321
+ ...title !== void 0 ? {
322
+ title
323
+ } : {},
324
+ defaultAgent: patch.defaultAgent
325
+ });
326
+ }
327
+ validateTitle(title) {
284
328
  const trimmed = title.trim();
285
329
  if (trimmed.length === 0) {
286
330
  throw new BadRequestException("title must not be empty");
@@ -288,8 +332,41 @@ var AgentService = class {
288
332
  if (trimmed.length > 200) {
289
333
  throw new BadRequestException("title must be at most 200 characters");
290
334
  }
291
- await this.assertOwnsThread(actor, threadId);
292
- return this.store.setTitle(threadId, trimmed);
335
+ return trimmed;
336
+ }
337
+ /**
338
+ * Agent-selection precedence for a `chat()` call with no explicit `agentName`: the thread's own
339
+ * `defaultAgent` (if the thread exists and one is set) wins over the module's configured default.
340
+ * Skips the thread lookup entirely when the caller already named an agent or there's no thread yet.
341
+ */
342
+ async resolveAgentName(agentName, threadId) {
343
+ if (agentName !== void 0) {
344
+ return agentName;
345
+ }
346
+ if (threadId !== void 0) {
347
+ const thread = await this.store.getThread(threadId);
348
+ if (thread?.defaultAgent !== void 0 && thread.defaultAgent !== null) {
349
+ return thread.defaultAgent;
350
+ }
351
+ }
352
+ return this.deps.defaultAgentName();
353
+ }
354
+ async toSummaryView(thread) {
355
+ return {
356
+ ...thread,
357
+ defaultAgent: thread.defaultAgent ?? null,
358
+ activeRunId: await this.store.activeRunForThread?.(thread.id) ?? null
359
+ };
360
+ }
361
+ async toDetailView(thread) {
362
+ const summary = await this.toSummaryView(thread);
363
+ return {
364
+ ...summary,
365
+ messages: thread.messages,
366
+ ...thread.activeStreamId !== void 0 ? {
367
+ activeStreamId: thread.activeStreamId
368
+ } : {}
369
+ };
293
370
  }
294
371
  async promoteThread(actor, threadId) {
295
372
  await this.assertOwnsThread(actor, threadId);
@@ -445,9 +522,10 @@ AgentsController = _ts_decorate3([
445
522
  ])
446
523
  ], AgentsController);
447
524
 
448
- // src/controller/chat.controller.ts
449
- import { AGENT_ACTOR_RESOLVER, AgentStreamError } from "@dudousxd/nestjs-agent-core";
450
- import { Body, Controller as Controller2, Get as Get2, Inject as Inject4, Param, Post, Req, Res } from "@nestjs/common";
525
+ // src/controller/attachments.controller.ts
526
+ import { AGENT_ACTOR_RESOLVER, AGENT_ATTACHMENT_STAGING, AGENT_OPTIONS as AGENT_OPTIONS2 } from "@dudousxd/nestjs-agent-core";
527
+ import { BadRequestException as BadRequestException2, Controller as Controller2, Inject as Inject4, NotImplementedException as NotImplementedException2, Optional as Optional2, PayloadTooLargeException, Post, Req, UnsupportedMediaTypeException, UploadedFile, UseInterceptors } from "@nestjs/common";
528
+ import { FileInterceptor } from "@nestjs/platform-express";
451
529
  function _ts_decorate4(decorators, target, key, desc) {
452
530
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
453
531
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -465,6 +543,110 @@ function _ts_param4(paramIndex, decorator) {
465
543
  };
466
544
  }
467
545
  __name(_ts_param4, "_ts_param");
546
+ var DEFAULT_MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024;
547
+ var DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES = [
548
+ "image/png",
549
+ "image/jpeg",
550
+ "image/gif",
551
+ "image/webp",
552
+ "application/pdf",
553
+ "text/plain",
554
+ "text/csv"
555
+ ];
556
+ var AttachmentsController = class {
557
+ static {
558
+ __name(this, "AttachmentsController");
559
+ }
560
+ options;
561
+ actorResolver;
562
+ staging;
563
+ constructor(options, actorResolver, staging) {
564
+ this.options = options;
565
+ this.actorResolver = actorResolver;
566
+ this.staging = staging;
567
+ }
568
+ /**
569
+ * `attachments.upload: true` is what mounts this controller at all (see `agent.module.ts`), so by
570
+ * the time an instance exists the host clearly intends uploads to work. Fail boot loudly rather
571
+ * than mounting a controller that would 501 on every single request because nobody bound
572
+ * `AGENT_ATTACHMENT_STAGING`.
573
+ */
574
+ onModuleInit() {
575
+ if (this.options.attachments?.upload === true && this.staging === void 0) {
576
+ 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.");
577
+ }
578
+ }
579
+ async upload(req, file) {
580
+ if (this.staging === void 0) {
581
+ throw new NotImplementedException2("Attachment upload is not configured on this server.");
582
+ }
583
+ if (file === void 0) {
584
+ throw new BadRequestException2('multipart field "file" is required');
585
+ }
586
+ const allowedContentTypes = this.options.attachments?.allowedContentTypes ?? DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES;
587
+ if (!allowedContentTypes.includes(file.mimetype)) {
588
+ throw new UnsupportedMediaTypeException(`content type "${file.mimetype}" is not allowed (allowed: ${allowedContentTypes.join(", ")})`);
589
+ }
590
+ const maxBytes = this.options.attachments?.maxBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES;
591
+ if (file.size > maxBytes) {
592
+ throw new PayloadTooLargeException(`file exceeds the ${maxBytes}-byte limit`);
593
+ }
594
+ const actor = await this.actorResolver.resolve(req);
595
+ return this.staging.stage({
596
+ data: file.buffer,
597
+ filename: file.originalname,
598
+ contentType: file.mimetype,
599
+ sizeBytes: file.size,
600
+ actor
601
+ });
602
+ }
603
+ };
604
+ _ts_decorate4([
605
+ Post(),
606
+ UseInterceptors(FileInterceptor("file")),
607
+ _ts_param4(0, Req()),
608
+ _ts_param4(1, UploadedFile()),
609
+ _ts_metadata4("design:type", Function),
610
+ _ts_metadata4("design:paramtypes", [
611
+ typeof Request === "undefined" ? Object : Request,
612
+ Object
613
+ ]),
614
+ _ts_metadata4("design:returntype", Promise)
615
+ ], AttachmentsController.prototype, "upload", null);
616
+ AttachmentsController = _ts_decorate4([
617
+ Controller2("attachments"),
618
+ _ts_param4(0, Inject4(AGENT_OPTIONS2)),
619
+ _ts_param4(1, Inject4(AGENT_ACTOR_RESOLVER)),
620
+ _ts_param4(2, Optional2()),
621
+ _ts_param4(2, Inject4(AGENT_ATTACHMENT_STAGING)),
622
+ _ts_metadata4("design:type", Function),
623
+ _ts_metadata4("design:paramtypes", [
624
+ typeof AgentModuleOptions === "undefined" ? Object : AgentModuleOptions,
625
+ typeof ActorResolver === "undefined" ? Object : ActorResolver,
626
+ Object
627
+ ])
628
+ ], AttachmentsController);
629
+
630
+ // src/controller/chat.controller.ts
631
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER2, AgentStreamError } from "@dudousxd/nestjs-agent-core";
632
+ import { Body, Controller as Controller3, Get as Get2, Inject as Inject5, Param, Post as Post2, Req as Req2, Res } from "@nestjs/common";
633
+ function _ts_decorate5(decorators, target, key, desc) {
634
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
635
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
636
+ 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;
637
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
638
+ }
639
+ __name(_ts_decorate5, "_ts_decorate");
640
+ function _ts_metadata5(k, v) {
641
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
642
+ }
643
+ __name(_ts_metadata5, "_ts_metadata");
644
+ function _ts_param5(paramIndex, decorator) {
645
+ return function(target, key) {
646
+ decorator(target, key, paramIndex);
647
+ };
648
+ }
649
+ __name(_ts_param5, "_ts_param");
468
650
  var ChatController = class {
469
651
  static {
470
652
  __name(this, "ChatController");
@@ -486,6 +668,9 @@ var ChatController = class {
486
668
  ...body.agent !== void 0 ? {
487
669
  agentName: body.agent
488
670
  } : {},
671
+ ...body.attachments !== void 0 ? {
672
+ attachments: body.attachments
673
+ } : {},
489
674
  ...body.pageContext !== void 0 ? {
490
675
  pageContext: body.pageContext
491
676
  } : {},
@@ -524,11 +709,26 @@ data: ${JSON.stringify({
524
709
 
525
710
  `);
526
711
  const decoder = new TextDecoder();
712
+ let buffer = "";
527
713
  try {
528
714
  for await (const chunk of this.agent.subscribe(runId)) {
529
- res.write(`data: ${JSON.stringify({
530
- delta: decoder.decode(chunk)
531
- })}
715
+ buffer += decoder.decode(chunk, {
716
+ stream: true
717
+ });
718
+ let newline = buffer.indexOf("\n");
719
+ while (newline !== -1) {
720
+ const line = buffer.slice(0, newline);
721
+ buffer = buffer.slice(newline + 1);
722
+ if (line.length > 0) {
723
+ res.write(`data: ${line}
724
+
725
+ `);
726
+ }
727
+ newline = buffer.indexOf("\n");
728
+ }
729
+ }
730
+ if (buffer.length > 0) {
731
+ res.write(`data: ${buffer}
532
732
 
533
733
  `);
534
734
  }
@@ -549,71 +749,71 @@ data: ${JSON.stringify(payload)}
549
749
  res.end();
550
750
  }
551
751
  };
552
- _ts_decorate4([
553
- Post("chat"),
554
- _ts_param4(0, Req()),
555
- _ts_param4(1, Res()),
556
- _ts_param4(2, Body()),
557
- _ts_metadata4("design:type", Function),
558
- _ts_metadata4("design:paramtypes", [
752
+ _ts_decorate5([
753
+ Post2("chat"),
754
+ _ts_param5(0, Req2()),
755
+ _ts_param5(1, Res()),
756
+ _ts_param5(2, Body()),
757
+ _ts_metadata5("design:type", Function),
758
+ _ts_metadata5("design:paramtypes", [
559
759
  typeof Request === "undefined" ? Object : Request,
560
760
  typeof Response === "undefined" ? Object : Response,
561
761
  typeof ChatBody === "undefined" ? Object : ChatBody
562
762
  ]),
563
- _ts_metadata4("design:returntype", Promise)
763
+ _ts_metadata5("design:returntype", Promise)
564
764
  ], ChatController.prototype, "chat", null);
565
- _ts_decorate4([
765
+ _ts_decorate5([
566
766
  Get2("chat/:runId/stream"),
567
- _ts_param4(0, Param("runId")),
568
- _ts_param4(1, Res()),
569
- _ts_metadata4("design:type", Function),
570
- _ts_metadata4("design:paramtypes", [
767
+ _ts_param5(0, Param("runId")),
768
+ _ts_param5(1, Res()),
769
+ _ts_metadata5("design:type", Function),
770
+ _ts_metadata5("design:paramtypes", [
571
771
  String,
572
772
  typeof Response === "undefined" ? Object : Response
573
773
  ]),
574
- _ts_metadata4("design:returntype", Promise)
774
+ _ts_metadata5("design:returntype", Promise)
575
775
  ], ChatController.prototype, "stream", null);
576
- _ts_decorate4([
577
- Post("chat/:runId/cancel"),
578
- _ts_param4(0, Req()),
579
- _ts_param4(1, Param("runId")),
580
- _ts_metadata4("design:type", Function),
581
- _ts_metadata4("design:paramtypes", [
776
+ _ts_decorate5([
777
+ Post2("chat/:runId/cancel"),
778
+ _ts_param5(0, Req2()),
779
+ _ts_param5(1, Param("runId")),
780
+ _ts_metadata5("design:type", Function),
781
+ _ts_metadata5("design:paramtypes", [
582
782
  typeof Request === "undefined" ? Object : Request,
583
783
  String
584
784
  ]),
585
- _ts_metadata4("design:returntype", Promise)
785
+ _ts_metadata5("design:returntype", Promise)
586
786
  ], ChatController.prototype, "cancel", null);
587
- ChatController = _ts_decorate4([
588
- Controller2(),
589
- _ts_param4(1, Inject4(AGENT_ACTOR_RESOLVER)),
590
- _ts_metadata4("design:type", Function),
591
- _ts_metadata4("design:paramtypes", [
787
+ ChatController = _ts_decorate5([
788
+ Controller3(),
789
+ _ts_param5(1, Inject5(AGENT_ACTOR_RESOLVER2)),
790
+ _ts_metadata5("design:type", Function),
791
+ _ts_metadata5("design:paramtypes", [
592
792
  typeof AgentService === "undefined" ? Object : AgentService,
593
793
  typeof ActorResolver === "undefined" ? Object : ActorResolver
594
794
  ])
595
795
  ], ChatController);
596
796
 
597
797
  // src/controller/quota.controller.ts
598
- import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER2 } from "@dudousxd/nestjs-agent-core";
599
- import { Controller as Controller3, Get as Get3, Inject as Inject5, Req as Req2 } from "@nestjs/common";
600
- function _ts_decorate5(decorators, target, key, desc) {
798
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER3 } from "@dudousxd/nestjs-agent-core";
799
+ import { Controller as Controller4, Get as Get3, Inject as Inject6, Req as Req3 } from "@nestjs/common";
800
+ function _ts_decorate6(decorators, target, key, desc) {
601
801
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
602
802
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
603
803
  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;
604
804
  return c > 3 && r && Object.defineProperty(target, key, r), r;
605
805
  }
606
- __name(_ts_decorate5, "_ts_decorate");
607
- function _ts_metadata5(k, v) {
806
+ __name(_ts_decorate6, "_ts_decorate");
807
+ function _ts_metadata6(k, v) {
608
808
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
609
809
  }
610
- __name(_ts_metadata5, "_ts_metadata");
611
- function _ts_param5(paramIndex, decorator) {
810
+ __name(_ts_metadata6, "_ts_metadata");
811
+ function _ts_param6(paramIndex, decorator) {
612
812
  return function(target, key) {
613
813
  decorator(target, key, paramIndex);
614
814
  };
615
815
  }
616
- __name(_ts_param5, "_ts_param");
816
+ __name(_ts_param6, "_ts_param");
617
817
  var QuotaController = class {
618
818
  static {
619
819
  __name(this, "QuotaController");
@@ -629,45 +829,45 @@ var QuotaController = class {
629
829
  return this.agent.quotaToday(actor.id);
630
830
  }
631
831
  };
632
- _ts_decorate5([
832
+ _ts_decorate6([
633
833
  Get3("today"),
634
- _ts_param5(0, Req2()),
635
- _ts_metadata5("design:type", Function),
636
- _ts_metadata5("design:paramtypes", [
834
+ _ts_param6(0, Req3()),
835
+ _ts_metadata6("design:type", Function),
836
+ _ts_metadata6("design:paramtypes", [
637
837
  typeof Request === "undefined" ? Object : Request
638
838
  ]),
639
- _ts_metadata5("design:returntype", Promise)
839
+ _ts_metadata6("design:returntype", Promise)
640
840
  ], QuotaController.prototype, "today", null);
641
- QuotaController = _ts_decorate5([
642
- Controller3("quota"),
643
- _ts_param5(1, Inject5(AGENT_ACTOR_RESOLVER2)),
644
- _ts_metadata5("design:type", Function),
645
- _ts_metadata5("design:paramtypes", [
841
+ QuotaController = _ts_decorate6([
842
+ Controller4("quota"),
843
+ _ts_param6(1, Inject6(AGENT_ACTOR_RESOLVER3)),
844
+ _ts_metadata6("design:type", Function),
845
+ _ts_metadata6("design:paramtypes", [
646
846
  typeof AgentService === "undefined" ? Object : AgentService,
647
847
  typeof ActorResolver === "undefined" ? Object : ActorResolver
648
848
  ])
649
849
  ], QuotaController);
650
850
 
651
851
  // src/controller/threads.controller.ts
652
- import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER3 } from "@dudousxd/nestjs-agent-core";
653
- import { Body as Body2, Controller as Controller4, Delete, Get as Get4, Inject as Inject6, Param as Param2, Patch, Post as Post2, Req as Req3 } from "@nestjs/common";
654
- function _ts_decorate6(decorators, target, key, desc) {
852
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER4 } from "@dudousxd/nestjs-agent-core";
853
+ import { Body as Body2, Controller as Controller5, Delete, Get as Get4, Inject as Inject7, Param as Param2, Patch, Post as Post3, Req as Req4 } from "@nestjs/common";
854
+ function _ts_decorate7(decorators, target, key, desc) {
655
855
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
656
856
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
657
857
  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;
658
858
  return c > 3 && r && Object.defineProperty(target, key, r), r;
659
859
  }
660
- __name(_ts_decorate6, "_ts_decorate");
661
- function _ts_metadata6(k, v) {
860
+ __name(_ts_decorate7, "_ts_decorate");
861
+ function _ts_metadata7(k, v) {
662
862
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
663
863
  }
664
- __name(_ts_metadata6, "_ts_metadata");
665
- function _ts_param6(paramIndex, decorator) {
864
+ __name(_ts_metadata7, "_ts_metadata");
865
+ function _ts_param7(paramIndex, decorator) {
666
866
  return function(target, key) {
667
867
  decorator(target, key, paramIndex);
668
868
  };
669
869
  }
670
- __name(_ts_param6, "_ts_param");
870
+ __name(_ts_param7, "_ts_param");
671
871
  var ThreadsController = class {
672
872
  static {
673
873
  __name(this, "ThreadsController");
@@ -697,9 +897,21 @@ var ThreadsController = class {
697
897
  const actor = await this.actorResolver.resolve(req);
698
898
  return this.agent.forkThread(actor, id, messageId);
699
899
  }
700
- async rename(req, id, body) {
900
+ /**
901
+ * Renames the thread and/or sets its default agent. `defaultAgent` is only present in the request
902
+ * body when the client actually sent the key (JSON has no `undefined`), so `'defaultAgent' in body`
903
+ * distinguishes "leave it alone" from "set it to null" (clear it) unambiguously.
904
+ */
905
+ async update(req, id, body) {
701
906
  const actor = await this.actorResolver.resolve(req);
702
- await this.agent.renameThread(actor, id, body.title);
907
+ await this.agent.updateThread(actor, id, {
908
+ ...body.title !== void 0 ? {
909
+ title: body.title
910
+ } : {},
911
+ ..."defaultAgent" in body ? {
912
+ defaultAgent: body.defaultAgent ?? null
913
+ } : {}
914
+ });
703
915
  return {
704
916
  ok: true
705
917
  };
@@ -719,117 +931,117 @@ var ThreadsController = class {
719
931
  };
720
932
  }
721
933
  };
722
- _ts_decorate6([
934
+ _ts_decorate7([
723
935
  Get4(),
724
- _ts_param6(0, Req3()),
725
- _ts_metadata6("design:type", Function),
726
- _ts_metadata6("design:paramtypes", [
936
+ _ts_param7(0, Req4()),
937
+ _ts_metadata7("design:type", Function),
938
+ _ts_metadata7("design:paramtypes", [
727
939
  typeof Request === "undefined" ? Object : Request
728
940
  ]),
729
- _ts_metadata6("design:returntype", Promise)
941
+ _ts_metadata7("design:returntype", Promise)
730
942
  ], ThreadsController.prototype, "list", null);
731
- _ts_decorate6([
943
+ _ts_decorate7([
732
944
  Get4(":id"),
733
- _ts_param6(0, Req3()),
734
- _ts_param6(1, Param2("id")),
735
- _ts_metadata6("design:type", Function),
736
- _ts_metadata6("design:paramtypes", [
945
+ _ts_param7(0, Req4()),
946
+ _ts_param7(1, Param2("id")),
947
+ _ts_metadata7("design:type", Function),
948
+ _ts_metadata7("design:paramtypes", [
737
949
  typeof Request === "undefined" ? Object : Request,
738
950
  String
739
951
  ]),
740
- _ts_metadata6("design:returntype", Promise)
952
+ _ts_metadata7("design:returntype", Promise)
741
953
  ], ThreadsController.prototype, "detail", null);
742
- _ts_decorate6([
954
+ _ts_decorate7([
743
955
  Delete(":id"),
744
- _ts_param6(0, Req3()),
745
- _ts_param6(1, Param2("id")),
746
- _ts_metadata6("design:type", Function),
747
- _ts_metadata6("design:paramtypes", [
956
+ _ts_param7(0, Req4()),
957
+ _ts_param7(1, Param2("id")),
958
+ _ts_metadata7("design:type", Function),
959
+ _ts_metadata7("design:paramtypes", [
748
960
  typeof Request === "undefined" ? Object : Request,
749
961
  String
750
962
  ]),
751
- _ts_metadata6("design:returntype", Promise)
963
+ _ts_metadata7("design:returntype", Promise)
752
964
  ], ThreadsController.prototype, "remove", null);
753
- _ts_decorate6([
754
- Post2(":id/fork-from/:messageId"),
755
- _ts_param6(0, Req3()),
756
- _ts_param6(1, Param2("id")),
757
- _ts_param6(2, Param2("messageId")),
758
- _ts_metadata6("design:type", Function),
759
- _ts_metadata6("design:paramtypes", [
965
+ _ts_decorate7([
966
+ Post3(":id/fork-from/:messageId"),
967
+ _ts_param7(0, Req4()),
968
+ _ts_param7(1, Param2("id")),
969
+ _ts_param7(2, Param2("messageId")),
970
+ _ts_metadata7("design:type", Function),
971
+ _ts_metadata7("design:paramtypes", [
760
972
  typeof Request === "undefined" ? Object : Request,
761
973
  String,
762
974
  String
763
975
  ]),
764
- _ts_metadata6("design:returntype", Promise)
976
+ _ts_metadata7("design:returntype", Promise)
765
977
  ], ThreadsController.prototype, "fork", null);
766
- _ts_decorate6([
978
+ _ts_decorate7([
767
979
  Patch(":id"),
768
- _ts_param6(0, Req3()),
769
- _ts_param6(1, Param2("id")),
770
- _ts_param6(2, Body2()),
771
- _ts_metadata6("design:type", Function),
772
- _ts_metadata6("design:paramtypes", [
980
+ _ts_param7(0, Req4()),
981
+ _ts_param7(1, Param2("id")),
982
+ _ts_param7(2, Body2()),
983
+ _ts_metadata7("design:type", Function),
984
+ _ts_metadata7("design:paramtypes", [
773
985
  typeof Request === "undefined" ? Object : Request,
774
986
  String,
775
- typeof RenameThreadBody === "undefined" ? Object : RenameThreadBody
987
+ typeof UpdateThreadBody === "undefined" ? Object : UpdateThreadBody
776
988
  ]),
777
- _ts_metadata6("design:returntype", Promise)
778
- ], ThreadsController.prototype, "rename", null);
779
- _ts_decorate6([
780
- Post2(":id/promote"),
781
- _ts_param6(0, Req3()),
782
- _ts_param6(1, Param2("id")),
783
- _ts_metadata6("design:type", Function),
784
- _ts_metadata6("design:paramtypes", [
989
+ _ts_metadata7("design:returntype", Promise)
990
+ ], ThreadsController.prototype, "update", null);
991
+ _ts_decorate7([
992
+ Post3(":id/promote"),
993
+ _ts_param7(0, Req4()),
994
+ _ts_param7(1, Param2("id")),
995
+ _ts_metadata7("design:type", Function),
996
+ _ts_metadata7("design:paramtypes", [
785
997
  typeof Request === "undefined" ? Object : Request,
786
998
  String
787
999
  ]),
788
- _ts_metadata6("design:returntype", Promise)
1000
+ _ts_metadata7("design:returntype", Promise)
789
1001
  ], ThreadsController.prototype, "promote", null);
790
- _ts_decorate6([
1002
+ _ts_decorate7([
791
1003
  Delete(":id/from/:messageId"),
792
- _ts_param6(0, Req3()),
793
- _ts_param6(1, Param2("id")),
794
- _ts_param6(2, Param2("messageId")),
795
- _ts_metadata6("design:type", Function),
796
- _ts_metadata6("design:paramtypes", [
1004
+ _ts_param7(0, Req4()),
1005
+ _ts_param7(1, Param2("id")),
1006
+ _ts_param7(2, Param2("messageId")),
1007
+ _ts_metadata7("design:type", Function),
1008
+ _ts_metadata7("design:paramtypes", [
797
1009
  typeof Request === "undefined" ? Object : Request,
798
1010
  String,
799
1011
  String
800
1012
  ]),
801
- _ts_metadata6("design:returntype", Promise)
1013
+ _ts_metadata7("design:returntype", Promise)
802
1014
  ], ThreadsController.prototype, "truncateFrom", null);
803
- ThreadsController = _ts_decorate6([
804
- Controller4("threads"),
805
- _ts_param6(1, Inject6(AGENT_ACTOR_RESOLVER3)),
806
- _ts_metadata6("design:type", Function),
807
- _ts_metadata6("design:paramtypes", [
1015
+ ThreadsController = _ts_decorate7([
1016
+ Controller5("threads"),
1017
+ _ts_param7(1, Inject7(AGENT_ACTOR_RESOLVER4)),
1018
+ _ts_metadata7("design:type", Function),
1019
+ _ts_metadata7("design:paramtypes", [
808
1020
  typeof AgentService === "undefined" ? Object : AgentService,
809
1021
  typeof ActorResolver === "undefined" ? Object : ActorResolver
810
1022
  ])
811
1023
  ], ThreadsController);
812
1024
 
813
1025
  // src/controller/tool-call.controller.ts
814
- import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER4 } from "@dudousxd/nestjs-agent-core";
815
- import { Body as Body3, Controller as Controller5, Inject as Inject7, Post as Post3, Req as Req4 } from "@nestjs/common";
816
- function _ts_decorate7(decorators, target, key, desc) {
1026
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER5 } from "@dudousxd/nestjs-agent-core";
1027
+ import { Body as Body3, Controller as Controller6, Inject as Inject8, Post as Post4, Req as Req5 } from "@nestjs/common";
1028
+ function _ts_decorate8(decorators, target, key, desc) {
817
1029
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
818
1030
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
819
1031
  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;
820
1032
  return c > 3 && r && Object.defineProperty(target, key, r), r;
821
1033
  }
822
- __name(_ts_decorate7, "_ts_decorate");
823
- function _ts_metadata7(k, v) {
1034
+ __name(_ts_decorate8, "_ts_decorate");
1035
+ function _ts_metadata8(k, v) {
824
1036
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
825
1037
  }
826
- __name(_ts_metadata7, "_ts_metadata");
827
- function _ts_param7(paramIndex, decorator) {
1038
+ __name(_ts_metadata8, "_ts_metadata");
1039
+ function _ts_param8(paramIndex, decorator) {
828
1040
  return function(target, key) {
829
1041
  decorator(target, key, paramIndex);
830
1042
  };
831
1043
  }
832
- __name(_ts_param7, "_ts_param");
1044
+ __name(_ts_param8, "_ts_param");
833
1045
  var ToolCallController = class {
834
1046
  static {
835
1047
  __name(this, "ToolCallController");
@@ -855,33 +1067,33 @@ var ToolCallController = class {
855
1067
  };
856
1068
  }
857
1069
  };
858
- _ts_decorate7([
859
- Post3("approve"),
860
- _ts_param7(0, Req4()),
861
- _ts_param7(1, Body3()),
862
- _ts_metadata7("design:type", Function),
863
- _ts_metadata7("design:paramtypes", [
1070
+ _ts_decorate8([
1071
+ Post4("approve"),
1072
+ _ts_param8(0, Req5()),
1073
+ _ts_param8(1, Body3()),
1074
+ _ts_metadata8("design:type", Function),
1075
+ _ts_metadata8("design:paramtypes", [
864
1076
  typeof Request === "undefined" ? Object : Request,
865
1077
  typeof ApproveBody === "undefined" ? Object : ApproveBody
866
1078
  ]),
867
- _ts_metadata7("design:returntype", Promise)
1079
+ _ts_metadata8("design:returntype", Promise)
868
1080
  ], ToolCallController.prototype, "approve", null);
869
- _ts_decorate7([
870
- Post3("reject"),
871
- _ts_param7(0, Req4()),
872
- _ts_param7(1, Body3()),
873
- _ts_metadata7("design:type", Function),
874
- _ts_metadata7("design:paramtypes", [
1081
+ _ts_decorate8([
1082
+ Post4("reject"),
1083
+ _ts_param8(0, Req5()),
1084
+ _ts_param8(1, Body3()),
1085
+ _ts_metadata8("design:type", Function),
1086
+ _ts_metadata8("design:paramtypes", [
875
1087
  typeof Request === "undefined" ? Object : Request,
876
1088
  typeof RejectBody === "undefined" ? Object : RejectBody
877
1089
  ]),
878
- _ts_metadata7("design:returntype", Promise)
1090
+ _ts_metadata8("design:returntype", Promise)
879
1091
  ], ToolCallController.prototype, "reject", null);
880
- ToolCallController = _ts_decorate7([
881
- Controller5("tool-call"),
882
- _ts_param7(1, Inject7(AGENT_ACTOR_RESOLVER4)),
883
- _ts_metadata7("design:type", Function),
884
- _ts_metadata7("design:paramtypes", [
1092
+ ToolCallController = _ts_decorate8([
1093
+ Controller6("tool-call"),
1094
+ _ts_param8(1, Inject8(AGENT_ACTOR_RESOLVER5)),
1095
+ _ts_metadata8("design:type", Function),
1096
+ _ts_metadata8("design:paramtypes", [
885
1097
  typeof AgentService === "undefined" ? Object : AgentService,
886
1098
  typeof ActorResolver === "undefined" ? Object : ActorResolver
887
1099
  ])
@@ -889,7 +1101,7 @@ ToolCallController = _ts_decorate7([
889
1101
 
890
1102
  // src/discovery/agent-discovery.service.ts
891
1103
  import { AGENT_PROMPT_CONTRIBUTORS as AGENT_PROMPT_CONTRIBUTORS2, AGENT_REGISTRY as AGENT_REGISTRY3, AgentRegistry as AgentRegistry4 } from "@dudousxd/nestjs-agent-core";
892
- import { Inject as Inject8, Injectable as Injectable3, Logger } from "@nestjs/common";
1104
+ import { Inject as Inject9, Injectable as Injectable3, Logger } from "@nestjs/common";
893
1105
  import { DiscoveryService, MetadataScanner } from "@nestjs/core";
894
1106
 
895
1107
  // src/decorator/agent.decorator.ts
@@ -924,23 +1136,23 @@ function SystemPromptContributor() {
924
1136
  __name(SystemPromptContributor, "SystemPromptContributor");
925
1137
 
926
1138
  // src/discovery/agent-discovery.service.ts
927
- function _ts_decorate8(decorators, target, key, desc) {
1139
+ function _ts_decorate9(decorators, target, key, desc) {
928
1140
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
929
1141
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
930
1142
  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;
931
1143
  return c > 3 && r && Object.defineProperty(target, key, r), r;
932
1144
  }
933
- __name(_ts_decorate8, "_ts_decorate");
934
- function _ts_metadata8(k, v) {
1145
+ __name(_ts_decorate9, "_ts_decorate");
1146
+ function _ts_metadata9(k, v) {
935
1147
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
936
1148
  }
937
- __name(_ts_metadata8, "_ts_metadata");
938
- function _ts_param8(paramIndex, decorator) {
1149
+ __name(_ts_metadata9, "_ts_metadata");
1150
+ function _ts_param9(paramIndex, decorator) {
939
1151
  return function(target, key) {
940
1152
  decorator(target, key, paramIndex);
941
1153
  };
942
1154
  }
943
- __name(_ts_param8, "_ts_param");
1155
+ __name(_ts_param9, "_ts_param");
944
1156
  var AgentDiscoveryService = class _AgentDiscoveryService {
945
1157
  static {
946
1158
  __name(this, "AgentDiscoveryService");
@@ -1037,12 +1249,12 @@ var AgentDiscoveryService = class _AgentDiscoveryService {
1037
1249
  return (ctx) => candidate.call(instance, ctx);
1038
1250
  }
1039
1251
  };
1040
- AgentDiscoveryService = _ts_decorate8([
1252
+ AgentDiscoveryService = _ts_decorate9([
1041
1253
  Injectable3(),
1042
- _ts_param8(2, Inject8(AGENT_REGISTRY3)),
1043
- _ts_param8(3, Inject8(AGENT_PROMPT_CONTRIBUTORS2)),
1044
- _ts_metadata8("design:type", Function),
1045
- _ts_metadata8("design:paramtypes", [
1254
+ _ts_param9(2, Inject9(AGENT_REGISTRY3)),
1255
+ _ts_param9(3, Inject9(AGENT_PROMPT_CONTRIBUTORS2)),
1256
+ _ts_metadata9("design:type", Function),
1257
+ _ts_metadata9("design:paramtypes", [
1046
1258
  typeof DiscoveryService === "undefined" ? Object : DiscoveryService,
1047
1259
  typeof MetadataScanner === "undefined" ? Object : MetadataScanner,
1048
1260
  typeof AgentRegistry4 === "undefined" ? Object : AgentRegistry4,
@@ -1051,8 +1263,8 @@ AgentDiscoveryService = _ts_decorate8([
1051
1263
  ], AgentDiscoveryService);
1052
1264
 
1053
1265
  // src/discovery/ai-tool-discovery.service.ts
1054
- import { AGENT_OPTIONS as AGENT_OPTIONS2, AGENT_REGISTRY as AGENT_REGISTRY4, AGENT_TOOL_REGISTRY as AGENT_TOOL_REGISTRY2 } from "@dudousxd/nestjs-agent-core";
1055
- import { Inject as Inject9, Injectable as Injectable4, Logger as Logger2 } from "@nestjs/common";
1266
+ import { AGENT_OPTIONS as AGENT_OPTIONS3, AGENT_REGISTRY as AGENT_REGISTRY4, AGENT_TOOL_REGISTRY as AGENT_TOOL_REGISTRY2 } from "@dudousxd/nestjs-agent-core";
1267
+ import { Inject as Inject10, Injectable as Injectable4, Logger as Logger2 } from "@nestjs/common";
1056
1268
  import { DiscoveryService as DiscoveryService2 } from "@nestjs/core";
1057
1269
  import { z } from "zod";
1058
1270
 
@@ -1101,23 +1313,23 @@ function provideAgentTool(factoryOrTool, inject) {
1101
1313
  __name(provideAgentTool, "provideAgentTool");
1102
1314
 
1103
1315
  // src/discovery/ai-tool-discovery.service.ts
1104
- function _ts_decorate9(decorators, target, key, desc) {
1316
+ function _ts_decorate10(decorators, target, key, desc) {
1105
1317
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1106
1318
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1107
1319
  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;
1108
1320
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1109
1321
  }
1110
- __name(_ts_decorate9, "_ts_decorate");
1111
- function _ts_metadata9(k, v) {
1322
+ __name(_ts_decorate10, "_ts_decorate");
1323
+ function _ts_metadata10(k, v) {
1112
1324
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1113
1325
  }
1114
- __name(_ts_metadata9, "_ts_metadata");
1115
- function _ts_param9(paramIndex, decorator) {
1326
+ __name(_ts_metadata10, "_ts_metadata");
1327
+ function _ts_param10(paramIndex, decorator) {
1116
1328
  return function(target, key) {
1117
1329
  decorator(target, key, paramIndex);
1118
1330
  };
1119
1331
  }
1120
- __name(_ts_param9, "_ts_param");
1332
+ __name(_ts_param10, "_ts_param");
1121
1333
  var AiToolDiscoveryService = class _AiToolDiscoveryService {
1122
1334
  static {
1123
1335
  __name(this, "AiToolDiscoveryService");
@@ -1224,13 +1436,13 @@ var AiToolDiscoveryService = class _AiToolDiscoveryService {
1224
1436
  return true;
1225
1437
  }
1226
1438
  };
1227
- AiToolDiscoveryService = _ts_decorate9([
1439
+ AiToolDiscoveryService = _ts_decorate10([
1228
1440
  Injectable4(),
1229
- _ts_param9(1, Inject9(AGENT_TOOL_REGISTRY2)),
1230
- _ts_param9(2, Inject9(AGENT_REGISTRY4)),
1231
- _ts_param9(3, Inject9(AGENT_OPTIONS2)),
1232
- _ts_metadata9("design:type", Function),
1233
- _ts_metadata9("design:paramtypes", [
1441
+ _ts_param10(1, Inject10(AGENT_TOOL_REGISTRY2)),
1442
+ _ts_param10(2, Inject10(AGENT_REGISTRY4)),
1443
+ _ts_param10(3, Inject10(AGENT_OPTIONS3)),
1444
+ _ts_metadata10("design:type", Function),
1445
+ _ts_metadata10("design:paramtypes", [
1234
1446
  typeof DiscoveryService2 === "undefined" ? Object : DiscoveryService2,
1235
1447
  typeof ToolRegistry === "undefined" ? Object : ToolRegistry,
1236
1448
  typeof AgentRegistry === "undefined" ? Object : AgentRegistry,
@@ -1331,24 +1543,24 @@ var LedgerQuotaStore = class {
1331
1543
 
1332
1544
  // src/runner/inline-agent-runner.ts
1333
1545
  import { AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY3, AGENT_STORE as AGENT_STORE3, QuotaExceededError, publishAgentRunFailed, runAgentLoop } from "@dudousxd/nestjs-agent-core";
1334
- import { Inject as Inject10, Injectable as Injectable5, Logger as Logger3 } from "@nestjs/common";
1335
- function _ts_decorate10(decorators, target, key, desc) {
1546
+ import { Inject as Inject11, Injectable as Injectable5, Logger as Logger3 } from "@nestjs/common";
1547
+ function _ts_decorate11(decorators, target, key, desc) {
1336
1548
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1337
1549
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1338
1550
  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;
1339
1551
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1340
1552
  }
1341
- __name(_ts_decorate10, "_ts_decorate");
1342
- function _ts_metadata10(k, v) {
1553
+ __name(_ts_decorate11, "_ts_decorate");
1554
+ function _ts_metadata11(k, v) {
1343
1555
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1344
1556
  }
1345
- __name(_ts_metadata10, "_ts_metadata");
1346
- function _ts_param10(paramIndex, decorator) {
1557
+ __name(_ts_metadata11, "_ts_metadata");
1558
+ function _ts_param11(paramIndex, decorator) {
1347
1559
  return function(target, key) {
1348
1560
  decorator(target, key, paramIndex);
1349
1561
  };
1350
1562
  }
1351
- __name(_ts_param10, "_ts_param");
1563
+ __name(_ts_param11, "_ts_param");
1352
1564
  var InlineAgentRunner = class _InlineAgentRunner {
1353
1565
  static {
1354
1566
  __name(this, "InlineAgentRunner");
@@ -1369,7 +1581,7 @@ var InlineAgentRunner = class _InlineAgentRunner {
1369
1581
  void runAgentLoop({
1370
1582
  ...deps,
1371
1583
  day
1372
- }, input, hooks).catch(async (error) => {
1584
+ }, input, hooks).then(() => this.store.setActiveStream(input.threadId, null)).catch(async (error) => {
1373
1585
  const message = error instanceof Error ? error.message : String(error);
1374
1586
  const code = error instanceof QuotaExceededError ? "quota_exceeded" : "run_failed";
1375
1587
  this.logger.error(`agent run ${runId} failed (${code}): ${message}`);
@@ -1378,6 +1590,7 @@ var InlineAgentRunner = class _InlineAgentRunner {
1378
1590
  code,
1379
1591
  message
1380
1592
  });
1593
+ await this.store.setActiveStream(input.threadId, null);
1381
1594
  const writer = await deps.sink.open(runId);
1382
1595
  await writer.fail({
1383
1596
  code,
@@ -1435,39 +1648,43 @@ var InlineAgentRunner = class _InlineAgentRunner {
1435
1648
  step: /* @__PURE__ */ __name((_name, fn) => fn(), "step"),
1436
1649
  runAgent: /* @__PURE__ */ __name((childName, childTask) => this.runNested(childName, childTask, actor, day, depth + 1, sinkRunId), "runAgent")
1437
1650
  };
1438
- return runAgentLoop({
1439
- ...deps,
1440
- day
1441
- }, {
1442
- threadId: subThread.id,
1443
- actor,
1444
- userText: task,
1445
- agentName,
1446
- day,
1447
- delegationDepth: depth,
1448
- sinkRunId
1449
- }, hooks);
1651
+ try {
1652
+ return await runAgentLoop({
1653
+ ...deps,
1654
+ day
1655
+ }, {
1656
+ threadId: subThread.id,
1657
+ actor,
1658
+ userText: task,
1659
+ agentName,
1660
+ day,
1661
+ delegationDepth: depth,
1662
+ sinkRunId
1663
+ }, hooks);
1664
+ } finally {
1665
+ await this.store.setActiveStream(subThread.id, null);
1666
+ }
1450
1667
  }
1451
1668
  };
1452
- InlineAgentRunner = _ts_decorate10([
1669
+ InlineAgentRunner = _ts_decorate11([
1453
1670
  Injectable5(),
1454
- _ts_param10(0, Inject10(AGENT_DEPS_FACTORY3)),
1455
- _ts_param10(1, Inject10(AGENT_STORE3)),
1456
- _ts_metadata10("design:type", Function),
1457
- _ts_metadata10("design:paramtypes", [
1671
+ _ts_param11(0, Inject11(AGENT_DEPS_FACTORY3)),
1672
+ _ts_param11(1, Inject11(AGENT_STORE3)),
1673
+ _ts_metadata11("design:type", Function),
1674
+ _ts_metadata11("design:paramtypes", [
1458
1675
  typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory,
1459
1676
  typeof AgentStore === "undefined" ? Object : AgentStore
1460
1677
  ])
1461
1678
  ], InlineAgentRunner);
1462
1679
 
1463
1680
  // src/agent.module.ts
1464
- function _ts_decorate11(decorators, target, key, desc) {
1681
+ function _ts_decorate12(decorators, target, key, desc) {
1465
1682
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1466
1683
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1467
1684
  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;
1468
1685
  return c > 3 && r && Object.defineProperty(target, key, r), r;
1469
1686
  }
1470
- __name(_ts_decorate11, "_ts_decorate");
1687
+ __name(_ts_decorate12, "_ts_decorate");
1471
1688
  var DEFAULT_PATH = "agent";
1472
1689
  function sharedProviders(durable, includeStore) {
1473
1690
  const providers = [
@@ -1489,28 +1706,28 @@ function sharedProviders(durable, includeStore) {
1489
1706
  provide: AGENT_SINK2,
1490
1707
  useFactory: /* @__PURE__ */ __name((options) => options.sink ?? new InProcessTokenStreamSink(), "useFactory"),
1491
1708
  inject: [
1492
- AGENT_OPTIONS3
1709
+ AGENT_OPTIONS4
1493
1710
  ]
1494
1711
  },
1495
1712
  {
1496
1713
  provide: AGENT_ROLES_POLICY2,
1497
1714
  useFactory: /* @__PURE__ */ __name((options) => options.rolesPolicy ?? new DefaultRolesPolicy(options.defaultRoles), "useFactory"),
1498
1715
  inject: [
1499
- AGENT_OPTIONS3
1716
+ AGENT_OPTIONS4
1500
1717
  ]
1501
1718
  },
1502
1719
  {
1503
- provide: AGENT_ACTOR_RESOLVER5,
1720
+ provide: AGENT_ACTOR_RESOLVER6,
1504
1721
  useFactory: /* @__PURE__ */ __name((options) => options.actorResolver, "useFactory"),
1505
1722
  inject: [
1506
- AGENT_OPTIONS3
1723
+ AGENT_OPTIONS4
1507
1724
  ]
1508
1725
  },
1509
1726
  {
1510
1727
  provide: AGENT_MODEL2,
1511
1728
  useFactory: /* @__PURE__ */ __name((o) => o.model, "useFactory"),
1512
1729
  inject: [
1513
- AGENT_OPTIONS3
1730
+ AGENT_OPTIONS4
1514
1731
  ]
1515
1732
  },
1516
1733
  {
@@ -1520,7 +1737,7 @@ function sharedProviders(durable, includeStore) {
1520
1737
  provide: AGENT_QUOTA_STORE3,
1521
1738
  useFactory: /* @__PURE__ */ __name((o, store) => o.quota ?? (o.quotaLimitTokens !== void 0 ? new LedgerQuotaStore(store, o.quotaLimitTokens) : void 0), "useFactory"),
1522
1739
  inject: [
1523
- AGENT_OPTIONS3,
1740
+ AGENT_OPTIONS4,
1524
1741
  AGENT_STORE4
1525
1742
  ]
1526
1743
  },
@@ -1541,7 +1758,7 @@ function sharedProviders(durable, includeStore) {
1541
1758
  provide: AGENT_STORE4,
1542
1759
  useFactory: /* @__PURE__ */ __name((o) => o.store, "useFactory"),
1543
1760
  inject: [
1544
- AGENT_OPTIONS3
1761
+ AGENT_OPTIONS4
1545
1762
  ]
1546
1763
  });
1547
1764
  }
@@ -1572,12 +1789,12 @@ function sharedProviders(durable, includeStore) {
1572
1789
  __name(sharedProviders, "sharedProviders");
1573
1790
  function exportsFor(includeStore) {
1574
1791
  return [
1575
- AGENT_OPTIONS3,
1792
+ AGENT_OPTIONS4,
1576
1793
  AGENT_TOOL_REGISTRY3,
1577
1794
  AGENT_REGISTRY5,
1578
1795
  AGENT_SINK2,
1579
1796
  AGENT_ROLES_POLICY2,
1580
- AGENT_ACTOR_RESOLVER5,
1797
+ AGENT_ACTOR_RESOLVER6,
1581
1798
  AGENT_MODEL2,
1582
1799
  ...includeStore ? [
1583
1800
  AGENT_STORE4
@@ -1591,13 +1808,38 @@ function exportsFor(includeStore) {
1591
1808
  ];
1592
1809
  }
1593
1810
  __name(exportsFor, "exportsFor");
1594
- var CONTROLLERS = [
1811
+ var BASE_CONTROLLERS = [
1595
1812
  ChatController,
1596
1813
  ThreadsController,
1597
1814
  ToolCallController,
1598
1815
  QuotaController,
1599
1816
  AgentsController
1600
1817
  ];
1818
+ var GUARDABLE_CONTROLLERS = [
1819
+ ...BASE_CONTROLLERS,
1820
+ AttachmentsController
1821
+ ];
1822
+ function controllersFor(mountAttachments) {
1823
+ return mountAttachments ? [
1824
+ ...BASE_CONTROLLERS,
1825
+ AttachmentsController
1826
+ ] : BASE_CONTROLLERS;
1827
+ }
1828
+ __name(controllersFor, "controllersFor");
1829
+ var GUARDS_METADATA = "__guards__";
1830
+ function applyGuards(guards) {
1831
+ const resolved = guards ?? [];
1832
+ for (const controller of GUARDABLE_CONTROLLERS) {
1833
+ Reflect.defineMetadata(GUARDS_METADATA, resolved, controller);
1834
+ }
1835
+ }
1836
+ __name(applyGuards, "applyGuards");
1837
+ function guardProviders(guards) {
1838
+ return [
1839
+ ...new Set(guards ?? [])
1840
+ ];
1841
+ }
1842
+ __name(guardProviders, "guardProviders");
1601
1843
  function routerFor(path) {
1602
1844
  return RouterModule.register([
1603
1845
  {
@@ -1614,6 +1856,7 @@ var AgentModule = class _AgentModule {
1614
1856
  static forRoot(options) {
1615
1857
  const path = options.path ?? DEFAULT_PATH;
1616
1858
  const includeStore = options.store !== void 0;
1859
+ applyGuards(options.guards);
1617
1860
  return {
1618
1861
  module: _AgentModule,
1619
1862
  global: true,
@@ -1621,19 +1864,22 @@ var AgentModule = class _AgentModule {
1621
1864
  DiscoveryModule,
1622
1865
  routerFor(path)
1623
1866
  ],
1624
- controllers: CONTROLLERS,
1867
+ controllers: controllersFor(options.attachments?.upload === true),
1625
1868
  providers: [
1626
1869
  {
1627
- provide: AGENT_OPTIONS3,
1870
+ provide: AGENT_OPTIONS4,
1628
1871
  useValue: options
1629
1872
  },
1630
- ...sharedProviders(options.durable ?? false, includeStore)
1873
+ ...sharedProviders(options.durable ?? false, includeStore),
1874
+ ...guardProviders(options.guards)
1631
1875
  ],
1632
1876
  exports: exportsFor(includeStore)
1633
1877
  };
1634
1878
  }
1635
1879
  static forRootAsync(options) {
1636
1880
  const path = options.path ?? DEFAULT_PATH;
1881
+ const includeStore = options.externalStore !== true;
1882
+ applyGuards(options.guards);
1637
1883
  return {
1638
1884
  module: _AgentModule,
1639
1885
  global: true,
@@ -1642,22 +1888,21 @@ var AgentModule = class _AgentModule {
1642
1888
  routerFor(path),
1643
1889
  ...options.imports ?? []
1644
1890
  ],
1645
- controllers: CONTROLLERS,
1891
+ controllers: controllersFor(options.attachmentsUpload === true),
1646
1892
  providers: [
1647
1893
  {
1648
- provide: AGENT_OPTIONS3,
1894
+ provide: AGENT_OPTIONS4,
1649
1895
  useFactory: options.useFactory,
1650
1896
  inject: options.inject ?? []
1651
1897
  },
1652
- // The async factory resolves too late to inspect `store`, so we always bind AGENT_STORE here
1653
- // (its factory reads the resolved options). Omit-and-import-a-store-module is a `forRoot` path.
1654
- ...sharedProviders(options.durable ?? false, true)
1898
+ ...sharedProviders(options.durable ?? false, includeStore),
1899
+ ...guardProviders(options.guards)
1655
1900
  ],
1656
- exports: exportsFor(true)
1901
+ exports: exportsFor(includeStore)
1657
1902
  };
1658
1903
  }
1659
1904
  };
1660
- AgentModule = _ts_decorate11([
1905
+ AgentModule = _ts_decorate12([
1661
1906
  Global(),
1662
1907
  Module({})
1663
1908
  ], AgentModule);
@@ -1705,6 +1950,9 @@ export {
1705
1950
  AgentService,
1706
1951
  AiTool,
1707
1952
  AiToolDiscoveryService,
1953
+ AttachmentsController,
1954
+ DEFAULT_ALLOWED_ATTACHMENT_CONTENT_TYPES,
1955
+ DEFAULT_MAX_ATTACHMENT_BYTES,
1708
1956
  HeaderActorResolver,
1709
1957
  InProcessTokenStreamSink,
1710
1958
  InlineAgentRunner,