@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.
- package/dist/durable/index.cjs +117 -4
- package/dist/durable/index.cjs.map +1 -1
- package/dist/durable/index.js +125 -12
- package/dist/durable/index.js.map +1 -1
- package/dist/index.cjs +117 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +27 -6
- package/dist/index.d.ts +27 -6
- package/dist/index.js +125 -12
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -246,10 +246,12 @@ var AgentService = class {
|
|
|
246
246
|
runner;
|
|
247
247
|
store;
|
|
248
248
|
deps;
|
|
249
|
-
|
|
249
|
+
quota;
|
|
250
|
+
constructor(runner, store, deps, quota) {
|
|
250
251
|
this.runner = runner;
|
|
251
252
|
this.store = store;
|
|
252
253
|
this.deps = deps;
|
|
254
|
+
this.quota = quota;
|
|
253
255
|
}
|
|
254
256
|
async chat(params) {
|
|
255
257
|
const agentName = params.agentName ?? this.deps.defaultAgentName();
|
|
@@ -259,7 +261,10 @@ var AgentService = class {
|
|
|
259
261
|
throw new import_common2.BadRequestException("regenerate requires an existing threadId");
|
|
260
262
|
}
|
|
261
263
|
const created = await this.store.createThread({
|
|
262
|
-
actor: params.actor
|
|
264
|
+
actor: params.actor,
|
|
265
|
+
...params.transient === true ? {
|
|
266
|
+
transient: true
|
|
267
|
+
} : {}
|
|
263
268
|
});
|
|
264
269
|
threadId = created.id;
|
|
265
270
|
} else if (params.regenerate === true) {
|
|
@@ -322,8 +327,53 @@ var AgentService = class {
|
|
|
322
327
|
await this.assertOwnsThread(actor, threadId);
|
|
323
328
|
return this.store.forkThread(threadId, fromMessageId);
|
|
324
329
|
}
|
|
330
|
+
async renameThread(actor, threadId, title) {
|
|
331
|
+
const trimmed = title.trim();
|
|
332
|
+
if (trimmed.length === 0) {
|
|
333
|
+
throw new import_common2.BadRequestException("title must not be empty");
|
|
334
|
+
}
|
|
335
|
+
if (trimmed.length > 200) {
|
|
336
|
+
throw new import_common2.BadRequestException("title must be at most 200 characters");
|
|
337
|
+
}
|
|
338
|
+
await this.assertOwnsThread(actor, threadId);
|
|
339
|
+
return this.store.setTitle(threadId, trimmed);
|
|
340
|
+
}
|
|
341
|
+
async promoteThread(actor, threadId) {
|
|
342
|
+
await this.assertOwnsThread(actor, threadId);
|
|
343
|
+
return this.store.promoteThread(threadId);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Drop a message and everything after it — the "edit and resend" / "delete from here" primitive.
|
|
347
|
+
* The client then sends a fresh turn on the truncated thread. Ownership-gated like the other
|
|
348
|
+
* thread-scoped mutations.
|
|
349
|
+
*/
|
|
350
|
+
async truncateThreadFrom(actor, threadId, messageId) {
|
|
351
|
+
await this.assertOwnsThread(actor, threadId);
|
|
352
|
+
return this.store.truncateFrom(threadId, messageId);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* The day's usage for the badge: tokens + summed USD cost from the store, and the configured
|
|
356
|
+
* limit from the quota store (null → unlimited). `withinLimit` comes from the quota store so it
|
|
357
|
+
* can never drift from what enforcement uses.
|
|
358
|
+
*/
|
|
325
359
|
async quotaToday(actorRef) {
|
|
326
|
-
|
|
360
|
+
const day = utcDay();
|
|
361
|
+
const { usedTokens, costUsd } = await this.store.quotaToday(actorRef, day);
|
|
362
|
+
if (this.quota === void 0) {
|
|
363
|
+
return {
|
|
364
|
+
usedTokens,
|
|
365
|
+
costUsd,
|
|
366
|
+
limitTokens: null,
|
|
367
|
+
withinLimit: true
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
const state = await this.quota.check(actorRef, day);
|
|
371
|
+
return {
|
|
372
|
+
usedTokens: state.usedTokens,
|
|
373
|
+
costUsd,
|
|
374
|
+
limitTokens: state.limitTokens,
|
|
375
|
+
withinLimit: state.withinLimit
|
|
376
|
+
};
|
|
327
377
|
}
|
|
328
378
|
/**
|
|
329
379
|
* Authorization seam for thread-scoped endpoints: the caller must own the thread. A missing
|
|
@@ -374,11 +424,13 @@ AgentService = _ts_decorate2([
|
|
|
374
424
|
_ts_param2(0, (0, import_common2.Inject)(import_nestjs_agent_core3.AGENT_RUNNER)),
|
|
375
425
|
_ts_param2(1, (0, import_common2.Inject)(import_nestjs_agent_core3.AGENT_STORE)),
|
|
376
426
|
_ts_param2(2, (0, import_common2.Inject)(import_nestjs_agent_core3.AGENT_DEPS_FACTORY)),
|
|
427
|
+
_ts_param2(3, (0, import_common2.Inject)(import_nestjs_agent_core3.AGENT_QUOTA_STORE)),
|
|
377
428
|
_ts_metadata2("design:type", Function),
|
|
378
429
|
_ts_metadata2("design:paramtypes", [
|
|
379
430
|
typeof AgentRunner === "undefined" ? Object : AgentRunner,
|
|
380
431
|
typeof AgentStore === "undefined" ? Object : AgentStore,
|
|
381
|
-
typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory
|
|
432
|
+
typeof AgentDepsFactory === "undefined" ? Object : AgentDepsFactory,
|
|
433
|
+
Object
|
|
382
434
|
])
|
|
383
435
|
], AgentService);
|
|
384
436
|
|
|
@@ -428,6 +480,9 @@ var ChatController = class {
|
|
|
428
480
|
} : {},
|
|
429
481
|
...body.regenerate === true ? {
|
|
430
482
|
regenerate: true
|
|
483
|
+
} : {},
|
|
484
|
+
...body.transient === true ? {
|
|
485
|
+
transient: true
|
|
431
486
|
} : {}
|
|
432
487
|
});
|
|
433
488
|
await this.pipe(res, runId, threadId);
|
|
@@ -631,6 +686,27 @@ var ThreadsController = class {
|
|
|
631
686
|
const actor = await this.actorResolver.resolve(req);
|
|
632
687
|
return this.agent.forkThread(actor, id, messageId);
|
|
633
688
|
}
|
|
689
|
+
async rename(req, id, body) {
|
|
690
|
+
const actor = await this.actorResolver.resolve(req);
|
|
691
|
+
await this.agent.renameThread(actor, id, body.title);
|
|
692
|
+
return {
|
|
693
|
+
ok: true
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
async promote(req, id) {
|
|
697
|
+
const actor = await this.actorResolver.resolve(req);
|
|
698
|
+
await this.agent.promoteThread(actor, id);
|
|
699
|
+
return {
|
|
700
|
+
ok: true
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
async truncateFrom(req, id, messageId) {
|
|
704
|
+
const actor = await this.actorResolver.resolve(req);
|
|
705
|
+
await this.agent.truncateThreadFrom(actor, id, messageId);
|
|
706
|
+
return {
|
|
707
|
+
ok: true
|
|
708
|
+
};
|
|
709
|
+
}
|
|
634
710
|
};
|
|
635
711
|
_ts_decorate5([
|
|
636
712
|
(0, import_common5.Get)(),
|
|
@@ -676,6 +752,43 @@ _ts_decorate5([
|
|
|
676
752
|
]),
|
|
677
753
|
_ts_metadata5("design:returntype", Promise)
|
|
678
754
|
], ThreadsController.prototype, "fork", null);
|
|
755
|
+
_ts_decorate5([
|
|
756
|
+
(0, import_common5.Patch)(":id"),
|
|
757
|
+
_ts_param5(0, (0, import_common5.Req)()),
|
|
758
|
+
_ts_param5(1, (0, import_common5.Param)("id")),
|
|
759
|
+
_ts_param5(2, (0, import_common5.Body)()),
|
|
760
|
+
_ts_metadata5("design:type", Function),
|
|
761
|
+
_ts_metadata5("design:paramtypes", [
|
|
762
|
+
typeof Request === "undefined" ? Object : Request,
|
|
763
|
+
String,
|
|
764
|
+
typeof RenameThreadBody === "undefined" ? Object : RenameThreadBody
|
|
765
|
+
]),
|
|
766
|
+
_ts_metadata5("design:returntype", Promise)
|
|
767
|
+
], ThreadsController.prototype, "rename", null);
|
|
768
|
+
_ts_decorate5([
|
|
769
|
+
(0, import_common5.Post)(":id/promote"),
|
|
770
|
+
_ts_param5(0, (0, import_common5.Req)()),
|
|
771
|
+
_ts_param5(1, (0, import_common5.Param)("id")),
|
|
772
|
+
_ts_metadata5("design:type", Function),
|
|
773
|
+
_ts_metadata5("design:paramtypes", [
|
|
774
|
+
typeof Request === "undefined" ? Object : Request,
|
|
775
|
+
String
|
|
776
|
+
]),
|
|
777
|
+
_ts_metadata5("design:returntype", Promise)
|
|
778
|
+
], ThreadsController.prototype, "promote", null);
|
|
779
|
+
_ts_decorate5([
|
|
780
|
+
(0, import_common5.Delete)(":id/from/:messageId"),
|
|
781
|
+
_ts_param5(0, (0, import_common5.Req)()),
|
|
782
|
+
_ts_param5(1, (0, import_common5.Param)("id")),
|
|
783
|
+
_ts_param5(2, (0, import_common5.Param)("messageId")),
|
|
784
|
+
_ts_metadata5("design:type", Function),
|
|
785
|
+
_ts_metadata5("design:paramtypes", [
|
|
786
|
+
typeof Request === "undefined" ? Object : Request,
|
|
787
|
+
String,
|
|
788
|
+
String
|
|
789
|
+
]),
|
|
790
|
+
_ts_metadata5("design:returntype", Promise)
|
|
791
|
+
], ThreadsController.prototype, "truncateFrom", null);
|
|
679
792
|
ThreadsController = _ts_decorate5([
|
|
680
793
|
(0, import_common5.Controller)("threads"),
|
|
681
794
|
_ts_param5(1, (0, import_common5.Inject)(import_nestjs_agent_core6.AGENT_ACTOR_RESOLVER)),
|