@dudousxd/nestjs-agent 0.2.0 → 0.3.1

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.
@@ -421,10 +421,12 @@ var AgentService = class {
421
421
  runner;
422
422
  store;
423
423
  deps;
424
- constructor(runner, store, deps) {
424
+ quota;
425
+ constructor(runner, store, deps, quota) {
425
426
  this.runner = runner;
426
427
  this.store = store;
427
428
  this.deps = deps;
429
+ this.quota = quota;
428
430
  }
429
431
  async chat(params) {
430
432
  const agentName = params.agentName ?? this.deps.defaultAgentName();
@@ -434,7 +436,10 @@ var AgentService = class {
434
436
  throw new import_common5.BadRequestException("regenerate requires an existing threadId");
435
437
  }
436
438
  const created = await this.store.createThread({
437
- actor: params.actor
439
+ actor: params.actor,
440
+ ...params.transient === true ? {
441
+ transient: true
442
+ } : {}
438
443
  });
439
444
  threadId = created.id;
440
445
  } else if (params.regenerate === true) {
@@ -497,8 +502,53 @@ var AgentService = class {
497
502
  await this.assertOwnsThread(actor, threadId);
498
503
  return this.store.forkThread(threadId, fromMessageId);
499
504
  }
505
+ async renameThread(actor, threadId, title) {
506
+ const trimmed = title.trim();
507
+ if (trimmed.length === 0) {
508
+ throw new import_common5.BadRequestException("title must not be empty");
509
+ }
510
+ if (trimmed.length > 200) {
511
+ throw new import_common5.BadRequestException("title must be at most 200 characters");
512
+ }
513
+ await this.assertOwnsThread(actor, threadId);
514
+ return this.store.setTitle(threadId, trimmed);
515
+ }
516
+ async promoteThread(actor, threadId) {
517
+ await this.assertOwnsThread(actor, threadId);
518
+ return this.store.promoteThread(threadId);
519
+ }
520
+ /**
521
+ * Drop a message and everything after it — the "edit and resend" / "delete from here" primitive.
522
+ * The client then sends a fresh turn on the truncated thread. Ownership-gated like the other
523
+ * thread-scoped mutations.
524
+ */
525
+ async truncateThreadFrom(actor, threadId, messageId) {
526
+ await this.assertOwnsThread(actor, threadId);
527
+ return this.store.truncateFrom(threadId, messageId);
528
+ }
529
+ /**
530
+ * The day's usage for the badge: tokens + summed USD cost from the store, and the configured
531
+ * limit from the quota store (null → unlimited). `withinLimit` comes from the quota store so it
532
+ * can never drift from what enforcement uses.
533
+ */
500
534
  async quotaToday(actorRef) {
501
- return this.store.quotaToday(actorRef, utcDay());
535
+ const day = utcDay();
536
+ const { usedTokens, costUsd } = await this.store.quotaToday(actorRef, day);
537
+ if (this.quota === void 0) {
538
+ return {
539
+ usedTokens,
540
+ costUsd,
541
+ limitTokens: null,
542
+ withinLimit: true
543
+ };
544
+ }
545
+ const state = await this.quota.check(actorRef, day);
546
+ return {
547
+ usedTokens: state.usedTokens,
548
+ costUsd,
549
+ limitTokens: state.limitTokens,
550
+ withinLimit: state.withinLimit
551
+ };
502
552
  }
503
553
  /**
504
554
  * Authorization seam for thread-scoped endpoints: the caller must own the thread. A missing
@@ -549,11 +599,13 @@ AgentService = _ts_decorate5([
549
599
  _ts_param3(0, (0, import_common5.Inject)(import_nestjs_agent_core5.AGENT_RUNNER)),
550
600
  _ts_param3(1, (0, import_common5.Inject)(import_nestjs_agent_core5.AGENT_STORE)),
551
601
  _ts_param3(2, (0, import_common5.Inject)(import_nestjs_agent_core5.AGENT_DEPS_FACTORY)),
602
+ _ts_param3(3, (0, import_common5.Inject)(import_nestjs_agent_core5.AGENT_QUOTA_STORE)),
552
603
  _ts_metadata4("design:type", Function),
553
604
  _ts_metadata4("design:paramtypes", [
554
605
  typeof AgentRunner === "undefined" ? Object : AgentRunner,
555
606
  typeof AgentStore === "undefined" ? Object : AgentStore,
556
- typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory
607
+ typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory,
608
+ Object
557
609
  ])
558
610
  ], AgentService);
559
611
 
@@ -603,6 +655,9 @@ var ChatController = class {
603
655
  } : {},
604
656
  ...body.regenerate === true ? {
605
657
  regenerate: true
658
+ } : {},
659
+ ...body.transient === true ? {
660
+ transient: true
606
661
  } : {}
607
662
  });
608
663
  await this.pipe(res, runId, threadId);
@@ -806,6 +861,27 @@ var ThreadsController = class {
806
861
  const actor = await this.actorResolver.resolve(req);
807
862
  return this.agent.forkThread(actor, id, messageId);
808
863
  }
864
+ async rename(req, id, body) {
865
+ const actor = await this.actorResolver.resolve(req);
866
+ await this.agent.renameThread(actor, id, body.title);
867
+ return {
868
+ ok: true
869
+ };
870
+ }
871
+ async promote(req, id) {
872
+ const actor = await this.actorResolver.resolve(req);
873
+ await this.agent.promoteThread(actor, id);
874
+ return {
875
+ ok: true
876
+ };
877
+ }
878
+ async truncateFrom(req, id, messageId) {
879
+ const actor = await this.actorResolver.resolve(req);
880
+ await this.agent.truncateThreadFrom(actor, id, messageId);
881
+ return {
882
+ ok: true
883
+ };
884
+ }
809
885
  };
810
886
  _ts_decorate8([
811
887
  (0, import_common8.Get)(),
@@ -851,6 +927,43 @@ _ts_decorate8([
851
927
  ]),
852
928
  _ts_metadata7("design:returntype", Promise)
853
929
  ], ThreadsController.prototype, "fork", null);
930
+ _ts_decorate8([
931
+ (0, import_common8.Patch)(":id"),
932
+ _ts_param6(0, (0, import_common8.Req)()),
933
+ _ts_param6(1, (0, import_common8.Param)("id")),
934
+ _ts_param6(2, (0, import_common8.Body)()),
935
+ _ts_metadata7("design:type", Function),
936
+ _ts_metadata7("design:paramtypes", [
937
+ typeof Request === "undefined" ? Object : Request,
938
+ String,
939
+ typeof RenameThreadBody === "undefined" ? Object : RenameThreadBody
940
+ ]),
941
+ _ts_metadata7("design:returntype", Promise)
942
+ ], ThreadsController.prototype, "rename", null);
943
+ _ts_decorate8([
944
+ (0, import_common8.Post)(":id/promote"),
945
+ _ts_param6(0, (0, import_common8.Req)()),
946
+ _ts_param6(1, (0, import_common8.Param)("id")),
947
+ _ts_metadata7("design:type", Function),
948
+ _ts_metadata7("design:paramtypes", [
949
+ typeof Request === "undefined" ? Object : Request,
950
+ String
951
+ ]),
952
+ _ts_metadata7("design:returntype", Promise)
953
+ ], ThreadsController.prototype, "promote", null);
954
+ _ts_decorate8([
955
+ (0, import_common8.Delete)(":id/from/:messageId"),
956
+ _ts_param6(0, (0, import_common8.Req)()),
957
+ _ts_param6(1, (0, import_common8.Param)("id")),
958
+ _ts_param6(2, (0, import_common8.Param)("messageId")),
959
+ _ts_metadata7("design:type", Function),
960
+ _ts_metadata7("design:paramtypes", [
961
+ typeof Request === "undefined" ? Object : Request,
962
+ String,
963
+ String
964
+ ]),
965
+ _ts_metadata7("design:returntype", Promise)
966
+ ], ThreadsController.prototype, "truncateFrom", null);
854
967
  ThreadsController = _ts_decorate8([
855
968
  (0, import_common8.Controller)("threads"),
856
969
  _ts_param6(1, (0, import_common8.Inject)(import_nestjs_agent_core8.AGENT_ACTOR_RESOLVER)),