@dudousxd/nestjs-agent 0.2.0 → 0.3.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.
@@ -215,7 +215,7 @@ AgentDurableModule = _ts_decorate3([
215
215
  ], AgentDurableModule);
216
216
 
217
217
  // src/agent.module.ts
218
- import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER5, AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY4, AGENT_DURABLE_RUNNER as AGENT_DURABLE_RUNNER2, AGENT_MODEL as AGENT_MODEL2, AGENT_OPTIONS as AGENT_OPTIONS3, AGENT_PROMPT_CONTRIBUTORS as AGENT_PROMPT_CONTRIBUTORS3, AGENT_QUOTA_STORE as AGENT_QUOTA_STORE2, AGENT_REGISTRY as AGENT_REGISTRY4, AGENT_ROLES_POLICY as AGENT_ROLES_POLICY2, AGENT_RUNNER as AGENT_RUNNER2, AGENT_SINK as AGENT_SINK2, AGENT_STORE as AGENT_STORE5, AGENT_TOOL_REGISTRY as AGENT_TOOL_REGISTRY3, AgentRegistry as AgentRegistry4, DefaultRolesPolicy, ToolRegistry as ToolRegistry3 } from "@dudousxd/nestjs-agent-core";
218
+ import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER5, AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY4, AGENT_DURABLE_RUNNER as AGENT_DURABLE_RUNNER2, 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_REGISTRY4, AGENT_ROLES_POLICY as AGENT_ROLES_POLICY2, AGENT_RUNNER as AGENT_RUNNER2, AGENT_SINK as AGENT_SINK2, AGENT_STORE as AGENT_STORE5, AGENT_TOOL_REGISTRY as AGENT_TOOL_REGISTRY3, AgentRegistry as AgentRegistry4, DefaultRolesPolicy, ToolRegistry as ToolRegistry3 } from "@dudousxd/nestjs-agent-core";
219
219
  import { Global as Global2, Module as Module2 } from "@nestjs/common";
220
220
  import { DiscoveryModule, RouterModule } from "@nestjs/core";
221
221
 
@@ -368,7 +368,7 @@ AgentDepsFactory2 = _ts_decorate4([
368
368
  ], AgentDepsFactory2);
369
369
 
370
370
  // src/agent.service.ts
371
- import { AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY2, AGENT_RUNNER, AGENT_STORE as AGENT_STORE3 } from "@dudousxd/nestjs-agent-core";
371
+ import { AGENT_DEPS_FACTORY as AGENT_DEPS_FACTORY2, AGENT_QUOTA_STORE as AGENT_QUOTA_STORE2, AGENT_RUNNER, AGENT_STORE as AGENT_STORE3 } from "@dudousxd/nestjs-agent-core";
372
372
  import { BadRequestException, ForbiddenException, Inject as Inject3, Injectable as Injectable4, NotFoundException } from "@nestjs/common";
373
373
  function _ts_decorate5(decorators, target, key, desc) {
374
374
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -394,10 +394,12 @@ var AgentService = class {
394
394
  runner;
395
395
  store;
396
396
  deps;
397
- constructor(runner, store, deps) {
397
+ quota;
398
+ constructor(runner, store, deps, quota) {
398
399
  this.runner = runner;
399
400
  this.store = store;
400
401
  this.deps = deps;
402
+ this.quota = quota;
401
403
  }
402
404
  async chat(params) {
403
405
  const agentName = params.agentName ?? this.deps.defaultAgentName();
@@ -407,7 +409,10 @@ var AgentService = class {
407
409
  throw new BadRequestException("regenerate requires an existing threadId");
408
410
  }
409
411
  const created = await this.store.createThread({
410
- actor: params.actor
412
+ actor: params.actor,
413
+ ...params.transient === true ? {
414
+ transient: true
415
+ } : {}
411
416
  });
412
417
  threadId = created.id;
413
418
  } else if (params.regenerate === true) {
@@ -470,8 +475,53 @@ var AgentService = class {
470
475
  await this.assertOwnsThread(actor, threadId);
471
476
  return this.store.forkThread(threadId, fromMessageId);
472
477
  }
478
+ async renameThread(actor, threadId, title) {
479
+ const trimmed = title.trim();
480
+ if (trimmed.length === 0) {
481
+ throw new BadRequestException("title must not be empty");
482
+ }
483
+ if (trimmed.length > 200) {
484
+ throw new BadRequestException("title must be at most 200 characters");
485
+ }
486
+ await this.assertOwnsThread(actor, threadId);
487
+ return this.store.setTitle(threadId, trimmed);
488
+ }
489
+ async promoteThread(actor, threadId) {
490
+ await this.assertOwnsThread(actor, threadId);
491
+ return this.store.promoteThread(threadId);
492
+ }
493
+ /**
494
+ * Drop a message and everything after it — the "edit and resend" / "delete from here" primitive.
495
+ * The client then sends a fresh turn on the truncated thread. Ownership-gated like the other
496
+ * thread-scoped mutations.
497
+ */
498
+ async truncateThreadFrom(actor, threadId, messageId) {
499
+ await this.assertOwnsThread(actor, threadId);
500
+ return this.store.truncateFrom(threadId, messageId);
501
+ }
502
+ /**
503
+ * The day's usage for the badge: tokens + summed USD cost from the store, and the configured
504
+ * limit from the quota store (null → unlimited). `withinLimit` comes from the quota store so it
505
+ * can never drift from what enforcement uses.
506
+ */
473
507
  async quotaToday(actorRef) {
474
- return this.store.quotaToday(actorRef, utcDay());
508
+ const day = utcDay();
509
+ const { usedTokens, costUsd } = await this.store.quotaToday(actorRef, day);
510
+ if (this.quota === void 0) {
511
+ return {
512
+ usedTokens,
513
+ costUsd,
514
+ limitTokens: null,
515
+ withinLimit: true
516
+ };
517
+ }
518
+ const state = await this.quota.check(actorRef, day);
519
+ return {
520
+ usedTokens: state.usedTokens,
521
+ costUsd,
522
+ limitTokens: state.limitTokens,
523
+ withinLimit: state.withinLimit
524
+ };
475
525
  }
476
526
  /**
477
527
  * Authorization seam for thread-scoped endpoints: the caller must own the thread. A missing
@@ -522,11 +572,13 @@ AgentService = _ts_decorate5([
522
572
  _ts_param3(0, Inject3(AGENT_RUNNER)),
523
573
  _ts_param3(1, Inject3(AGENT_STORE3)),
524
574
  _ts_param3(2, Inject3(AGENT_DEPS_FACTORY2)),
575
+ _ts_param3(3, Inject3(AGENT_QUOTA_STORE2)),
525
576
  _ts_metadata4("design:type", Function),
526
577
  _ts_metadata4("design:paramtypes", [
527
578
  typeof AgentRunner === "undefined" ? Object : AgentRunner,
528
579
  typeof AgentStore === "undefined" ? Object : AgentStore,
529
- typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory
580
+ typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory,
581
+ Object
530
582
  ])
531
583
  ], AgentService);
532
584
 
@@ -576,6 +628,9 @@ var ChatController = class {
576
628
  } : {},
577
629
  ...body.regenerate === true ? {
578
630
  regenerate: true
631
+ } : {},
632
+ ...body.transient === true ? {
633
+ transient: true
579
634
  } : {}
580
635
  });
581
636
  await this.pipe(res, runId, threadId);
@@ -732,7 +787,7 @@ QuotaController = _ts_decorate7([
732
787
 
733
788
  // src/controller/threads.controller.ts
734
789
  import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER3 } from "@dudousxd/nestjs-agent-core";
735
- import { Controller as Controller3, Delete, Get as Get3, Inject as Inject6, Param as Param2, Post as Post2, Req as Req3 } from "@nestjs/common";
790
+ import { Body as Body2, Controller as Controller3, Delete, Get as Get3, Inject as Inject6, Param as Param2, Patch, Post as Post2, Req as Req3 } from "@nestjs/common";
736
791
  function _ts_decorate8(decorators, target, key, desc) {
737
792
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
738
793
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -779,6 +834,27 @@ var ThreadsController = class {
779
834
  const actor = await this.actorResolver.resolve(req);
780
835
  return this.agent.forkThread(actor, id, messageId);
781
836
  }
837
+ async rename(req, id, body) {
838
+ const actor = await this.actorResolver.resolve(req);
839
+ await this.agent.renameThread(actor, id, body.title);
840
+ return {
841
+ ok: true
842
+ };
843
+ }
844
+ async promote(req, id) {
845
+ const actor = await this.actorResolver.resolve(req);
846
+ await this.agent.promoteThread(actor, id);
847
+ return {
848
+ ok: true
849
+ };
850
+ }
851
+ async truncateFrom(req, id, messageId) {
852
+ const actor = await this.actorResolver.resolve(req);
853
+ await this.agent.truncateThreadFrom(actor, id, messageId);
854
+ return {
855
+ ok: true
856
+ };
857
+ }
782
858
  };
783
859
  _ts_decorate8([
784
860
  Get3(),
@@ -824,6 +900,43 @@ _ts_decorate8([
824
900
  ]),
825
901
  _ts_metadata7("design:returntype", Promise)
826
902
  ], ThreadsController.prototype, "fork", null);
903
+ _ts_decorate8([
904
+ Patch(":id"),
905
+ _ts_param6(0, Req3()),
906
+ _ts_param6(1, Param2("id")),
907
+ _ts_param6(2, Body2()),
908
+ _ts_metadata7("design:type", Function),
909
+ _ts_metadata7("design:paramtypes", [
910
+ typeof Request === "undefined" ? Object : Request,
911
+ String,
912
+ typeof RenameThreadBody === "undefined" ? Object : RenameThreadBody
913
+ ]),
914
+ _ts_metadata7("design:returntype", Promise)
915
+ ], ThreadsController.prototype, "rename", null);
916
+ _ts_decorate8([
917
+ Post2(":id/promote"),
918
+ _ts_param6(0, Req3()),
919
+ _ts_param6(1, Param2("id")),
920
+ _ts_metadata7("design:type", Function),
921
+ _ts_metadata7("design:paramtypes", [
922
+ typeof Request === "undefined" ? Object : Request,
923
+ String
924
+ ]),
925
+ _ts_metadata7("design:returntype", Promise)
926
+ ], ThreadsController.prototype, "promote", null);
927
+ _ts_decorate8([
928
+ Delete(":id/from/:messageId"),
929
+ _ts_param6(0, Req3()),
930
+ _ts_param6(1, Param2("id")),
931
+ _ts_param6(2, Param2("messageId")),
932
+ _ts_metadata7("design:type", Function),
933
+ _ts_metadata7("design:paramtypes", [
934
+ typeof Request === "undefined" ? Object : Request,
935
+ String,
936
+ String
937
+ ]),
938
+ _ts_metadata7("design:returntype", Promise)
939
+ ], ThreadsController.prototype, "truncateFrom", null);
827
940
  ThreadsController = _ts_decorate8([
828
941
  Controller3("threads"),
829
942
  _ts_param6(1, Inject6(AGENT_ACTOR_RESOLVER3)),
@@ -836,7 +949,7 @@ ThreadsController = _ts_decorate8([
836
949
 
837
950
  // src/controller/tool-call.controller.ts
838
951
  import { AGENT_ACTOR_RESOLVER as AGENT_ACTOR_RESOLVER4 } from "@dudousxd/nestjs-agent-core";
839
- import { Body as Body2, Controller as Controller4, Inject as Inject7, Post as Post3, Req as Req4 } from "@nestjs/common";
952
+ import { Body as Body3, Controller as Controller4, Inject as Inject7, Post as Post3, Req as Req4 } from "@nestjs/common";
840
953
  function _ts_decorate9(decorators, target, key, desc) {
841
954
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
842
955
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -882,7 +995,7 @@ var ToolCallController = class {
882
995
  _ts_decorate9([
883
996
  Post3("approve"),
884
997
  _ts_param7(0, Req4()),
885
- _ts_param7(1, Body2()),
998
+ _ts_param7(1, Body3()),
886
999
  _ts_metadata8("design:type", Function),
887
1000
  _ts_metadata8("design:paramtypes", [
888
1001
  typeof Request === "undefined" ? Object : Request,
@@ -893,7 +1006,7 @@ _ts_decorate9([
893
1006
  _ts_decorate9([
894
1007
  Post3("reject"),
895
1008
  _ts_param7(0, Req4()),
896
- _ts_param7(1, Body2()),
1009
+ _ts_param7(1, Body3()),
897
1010
  _ts_metadata8("design:type", Function),
898
1011
  _ts_metadata8("design:paramtypes", [
899
1012
  typeof Request === "undefined" ? Object : Request,
@@ -1491,7 +1604,7 @@ function sharedProviders(durable, includeStore) {
1491
1604
  // An explicit `quota` wins; otherwise `quotaLimitTokens` binds the built-in ledger-backed
1492
1605
  // store; otherwise quotas are off (undefined). The ledger store reads the same usage rows
1493
1606
  // the loop already writes, so enforcement needs no extra shared state across replicas.
1494
- provide: AGENT_QUOTA_STORE2,
1607
+ provide: AGENT_QUOTA_STORE3,
1495
1608
  useFactory: /* @__PURE__ */ __name((o, store) => o.quota ?? (o.quotaLimitTokens !== void 0 ? new LedgerQuotaStore(store, o.quotaLimitTokens) : void 0), "useFactory"),
1496
1609
  inject: [
1497
1610
  AGENT_OPTIONS3,
@@ -1556,7 +1669,7 @@ function exportsFor(includeStore) {
1556
1669
  ...includeStore ? [
1557
1670
  AGENT_STORE5
1558
1671
  ] : [],
1559
- AGENT_QUOTA_STORE2,
1672
+ AGENT_QUOTA_STORE3,
1560
1673
  AGENT_PROMPT_CONTRIBUTORS3,
1561
1674
  AGENT_DEPS_FACTORY4,
1562
1675
  AgentDepsFactory2,