@dudousxd/nestjs-agent-core 0.26.0 → 0.27.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 +679 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +208 -2
- package/dist/index.d.ts +208 -2
- package/dist/index.js +678 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2482,13 +2482,12 @@ var DefaultRolesPolicy = class {
|
|
|
2482
2482
|
__name(this, "DefaultRolesPolicy");
|
|
2483
2483
|
}
|
|
2484
2484
|
defaultRoles;
|
|
2485
|
-
constructor(defaultRoles = [
|
|
2486
|
-
"ADMIN"
|
|
2487
|
-
]) {
|
|
2485
|
+
constructor(defaultRoles = []) {
|
|
2488
2486
|
this.defaultRoles = defaultRoles;
|
|
2489
2487
|
}
|
|
2490
2488
|
can(actor, tool) {
|
|
2491
2489
|
const allowed = tool.roles ?? this.defaultRoles;
|
|
2490
|
+
if (allowed.length === 0) return true;
|
|
2492
2491
|
return (actor.roles ?? []).some((role) => allowed.includes(role));
|
|
2493
2492
|
}
|
|
2494
2493
|
};
|
|
@@ -4635,6 +4634,681 @@ ${block}`;
|
|
|
4635
4634
|
};
|
|
4636
4635
|
}
|
|
4637
4636
|
__name(runAgentLoop, "runAgentLoop");
|
|
4637
|
+
|
|
4638
|
+
// src/in-memory-store.ts
|
|
4639
|
+
var InMemoryAgentStore = class {
|
|
4640
|
+
static {
|
|
4641
|
+
__name(this, "InMemoryAgentStore");
|
|
4642
|
+
}
|
|
4643
|
+
threads = /* @__PURE__ */ new Map();
|
|
4644
|
+
toolCalls = /* @__PURE__ */ new Map();
|
|
4645
|
+
usage = [];
|
|
4646
|
+
runs = /* @__PURE__ */ new Map();
|
|
4647
|
+
now() {
|
|
4648
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
4649
|
+
}
|
|
4650
|
+
async createThread(input) {
|
|
4651
|
+
const id = crypto.randomUUID();
|
|
4652
|
+
const ts = this.now();
|
|
4653
|
+
const row = {
|
|
4654
|
+
id,
|
|
4655
|
+
actorRef: input.actor.id,
|
|
4656
|
+
title: input.title ?? "New chat",
|
|
4657
|
+
transient: input.transient ?? false,
|
|
4658
|
+
createdAt: ts,
|
|
4659
|
+
updatedAt: ts,
|
|
4660
|
+
messages: []
|
|
4661
|
+
};
|
|
4662
|
+
this.threads.set(id, row);
|
|
4663
|
+
return this.toSummary(row);
|
|
4664
|
+
}
|
|
4665
|
+
async getThread(threadId) {
|
|
4666
|
+
const row = this.threads.get(threadId);
|
|
4667
|
+
if (row === void 0) {
|
|
4668
|
+
return null;
|
|
4669
|
+
}
|
|
4670
|
+
return {
|
|
4671
|
+
...this.toSummary(row),
|
|
4672
|
+
messages: row.messages.map((message) => this.withApprovals(message)),
|
|
4673
|
+
...row.activeStreamId !== void 0 ? {
|
|
4674
|
+
activeStreamId: row.activeStreamId
|
|
4675
|
+
} : {}
|
|
4676
|
+
};
|
|
4677
|
+
}
|
|
4678
|
+
/** A message with the approval record of every call on it that was put to a person. */
|
|
4679
|
+
withApprovals(message) {
|
|
4680
|
+
const approvals = [];
|
|
4681
|
+
for (const call of this.toolCalls.values()) {
|
|
4682
|
+
if (call.messageId !== message.id) {
|
|
4683
|
+
continue;
|
|
4684
|
+
}
|
|
4685
|
+
const approval = toolCallApprovalFromRow({
|
|
4686
|
+
toolCallId: call.toolCallId,
|
|
4687
|
+
status: call.status,
|
|
4688
|
+
approver: call.approver,
|
|
4689
|
+
expiresAt: call.expiresAt,
|
|
4690
|
+
remember: call.remember,
|
|
4691
|
+
executedByRef: call.executedByRef,
|
|
4692
|
+
decidedVia: call.decidedVia,
|
|
4693
|
+
error: call.error
|
|
4694
|
+
});
|
|
4695
|
+
if (approval !== null) {
|
|
4696
|
+
approvals.push(approval);
|
|
4697
|
+
}
|
|
4698
|
+
}
|
|
4699
|
+
return approvals.length > 0 ? {
|
|
4700
|
+
...message,
|
|
4701
|
+
approvals
|
|
4702
|
+
} : message;
|
|
4703
|
+
}
|
|
4704
|
+
async rememberedApprovals(threadId) {
|
|
4705
|
+
const names = /* @__PURE__ */ new Set();
|
|
4706
|
+
for (const call of this.toolCalls.values()) {
|
|
4707
|
+
if (call.threadId === threadId && call.remember === true) {
|
|
4708
|
+
names.add(call.toolName);
|
|
4709
|
+
}
|
|
4710
|
+
}
|
|
4711
|
+
return [
|
|
4712
|
+
...names
|
|
4713
|
+
];
|
|
4714
|
+
}
|
|
4715
|
+
async toolCallInput(toolCallId) {
|
|
4716
|
+
return this.toolCalls.get(toolCallId)?.input ?? null;
|
|
4717
|
+
}
|
|
4718
|
+
async toolCallApproval(toolCallId) {
|
|
4719
|
+
const call = this.toolCalls.get(toolCallId);
|
|
4720
|
+
if (call === void 0) {
|
|
4721
|
+
return null;
|
|
4722
|
+
}
|
|
4723
|
+
return {
|
|
4724
|
+
status: call.status,
|
|
4725
|
+
approver: call.approver ?? null,
|
|
4726
|
+
expiresAt: call.expiresAt ?? null
|
|
4727
|
+
};
|
|
4728
|
+
}
|
|
4729
|
+
async listThreads(actorRef, limit = 50) {
|
|
4730
|
+
return [
|
|
4731
|
+
...this.threads.values()
|
|
4732
|
+
].filter((row) => row.actorRef === actorRef && !row.transient).sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)).slice(0, limit).map((row) => this.toSummary(row));
|
|
4733
|
+
}
|
|
4734
|
+
async softDeleteThread(threadId) {
|
|
4735
|
+
this.threads.delete(threadId);
|
|
4736
|
+
}
|
|
4737
|
+
async forkThread(threadId, fromMessageId) {
|
|
4738
|
+
const source = this.threads.get(threadId);
|
|
4739
|
+
if (source === void 0) {
|
|
4740
|
+
throw new Error(`thread ${threadId} not found`);
|
|
4741
|
+
}
|
|
4742
|
+
const cutoff = source.messages.findIndex((message) => message.id === fromMessageId);
|
|
4743
|
+
const kept = cutoff >= 0 ? source.messages.slice(0, cutoff + 1) : [
|
|
4744
|
+
...source.messages
|
|
4745
|
+
];
|
|
4746
|
+
const id = crypto.randomUUID();
|
|
4747
|
+
const ts = this.now();
|
|
4748
|
+
const row = {
|
|
4749
|
+
id,
|
|
4750
|
+
actorRef: source.actorRef,
|
|
4751
|
+
title: source.title,
|
|
4752
|
+
transient: false,
|
|
4753
|
+
createdAt: ts,
|
|
4754
|
+
updatedAt: ts,
|
|
4755
|
+
// Feedback rates a message in ITS thread; a fork starts unrated.
|
|
4756
|
+
messages: kept.map(({ feedback: _feedback, ...message }) => ({
|
|
4757
|
+
...message
|
|
4758
|
+
})),
|
|
4759
|
+
...source.defaultAgent != null ? {
|
|
4760
|
+
defaultAgent: source.defaultAgent
|
|
4761
|
+
} : {},
|
|
4762
|
+
...source.model != null ? {
|
|
4763
|
+
model: source.model
|
|
4764
|
+
} : {}
|
|
4765
|
+
};
|
|
4766
|
+
this.threads.set(id, row);
|
|
4767
|
+
return this.toSummary(row);
|
|
4768
|
+
}
|
|
4769
|
+
async ownerOfThread(threadId) {
|
|
4770
|
+
return this.threads.get(threadId)?.actorRef ?? null;
|
|
4771
|
+
}
|
|
4772
|
+
async ownerOfToolCall(toolCallId) {
|
|
4773
|
+
const call = this.toolCalls.get(toolCallId);
|
|
4774
|
+
if (call === void 0) {
|
|
4775
|
+
return null;
|
|
4776
|
+
}
|
|
4777
|
+
return this.threads.get(call.threadId)?.actorRef ?? null;
|
|
4778
|
+
}
|
|
4779
|
+
async runForToolCall(toolCallId) {
|
|
4780
|
+
const call = this.toolCalls.get(toolCallId);
|
|
4781
|
+
if (call === void 0) {
|
|
4782
|
+
return null;
|
|
4783
|
+
}
|
|
4784
|
+
return call.runId ?? this.threads.get(call.threadId)?.activeStreamId ?? null;
|
|
4785
|
+
}
|
|
4786
|
+
async ownerOfActiveStream(runId) {
|
|
4787
|
+
for (const row of this.threads.values()) {
|
|
4788
|
+
if (row.activeStreamId === runId) {
|
|
4789
|
+
return row.actorRef;
|
|
4790
|
+
}
|
|
4791
|
+
}
|
|
4792
|
+
return null;
|
|
4793
|
+
}
|
|
4794
|
+
async setTitle(threadId, title) {
|
|
4795
|
+
const row = this.threads.get(threadId);
|
|
4796
|
+
if (row !== void 0) {
|
|
4797
|
+
row.title = title;
|
|
4798
|
+
row.updatedAt = this.now();
|
|
4799
|
+
}
|
|
4800
|
+
}
|
|
4801
|
+
async updateThread(threadId, patch) {
|
|
4802
|
+
const row = this.threads.get(threadId);
|
|
4803
|
+
if (row === void 0) {
|
|
4804
|
+
return;
|
|
4805
|
+
}
|
|
4806
|
+
if (patch.title !== void 0) {
|
|
4807
|
+
row.title = patch.title;
|
|
4808
|
+
}
|
|
4809
|
+
if (patch.defaultAgent !== void 0) {
|
|
4810
|
+
row.defaultAgent = patch.defaultAgent;
|
|
4811
|
+
}
|
|
4812
|
+
if (patch.model !== void 0) {
|
|
4813
|
+
row.model = patch.model;
|
|
4814
|
+
}
|
|
4815
|
+
row.updatedAt = this.now();
|
|
4816
|
+
}
|
|
4817
|
+
async activeRunForThread(threadId) {
|
|
4818
|
+
return this.threads.get(threadId)?.activeStreamId ?? null;
|
|
4819
|
+
}
|
|
4820
|
+
/**
|
|
4821
|
+
* The thread's default agent, projected. A map lookup here, but the SQL adapters answer this with
|
|
4822
|
+
* a one-column read instead of materializing the whole transcript — so the caller that only needs
|
|
4823
|
+
* this scalar has a method to ask for it.
|
|
4824
|
+
*/
|
|
4825
|
+
async defaultAgentForThread(threadId) {
|
|
4826
|
+
return this.threads.get(threadId)?.defaultAgent ?? null;
|
|
4827
|
+
}
|
|
4828
|
+
/** The thread's pinned model, projected like {@link defaultAgentForThread}. */
|
|
4829
|
+
async modelForThread(threadId) {
|
|
4830
|
+
return this.threads.get(threadId)?.model ?? null;
|
|
4831
|
+
}
|
|
4832
|
+
/**
|
|
4833
|
+
* Persist the start of a run (turn). Replay-safe: called under a durable localStep.
|
|
4834
|
+
*
|
|
4835
|
+
* Takes the SPI's own {@link RecordRunStartInput} rather than a hand-copied shape: a field added
|
|
4836
|
+
* to the input is otherwise accepted and dropped, silently, by every adapter that re-declares it.
|
|
4837
|
+
*/
|
|
4838
|
+
async recordRunStart(run) {
|
|
4839
|
+
this.runs.set(run.runId, {
|
|
4840
|
+
runId: run.runId,
|
|
4841
|
+
threadId: run.threadId,
|
|
4842
|
+
actorRef: run.actorRef,
|
|
4843
|
+
status: "running",
|
|
4844
|
+
retries: 0,
|
|
4845
|
+
startedAt: this.now(),
|
|
4846
|
+
...run.agentName !== void 0 ? {
|
|
4847
|
+
agentName: run.agentName
|
|
4848
|
+
} : {},
|
|
4849
|
+
...run.parentRunId !== void 0 ? {
|
|
4850
|
+
parentRunId: run.parentRunId
|
|
4851
|
+
} : {},
|
|
4852
|
+
...run.promptHash !== void 0 ? {
|
|
4853
|
+
promptHash: run.promptHash
|
|
4854
|
+
} : {}
|
|
4855
|
+
});
|
|
4856
|
+
}
|
|
4857
|
+
/** Settle a run's outcome. A no-op when the run is unknown (mirrors `setTitle`/`updateThread`). */
|
|
4858
|
+
async recordRunEnd(end) {
|
|
4859
|
+
const row = this.runs.get(end.runId);
|
|
4860
|
+
if (row === void 0) {
|
|
4861
|
+
return;
|
|
4862
|
+
}
|
|
4863
|
+
row.status = end.status;
|
|
4864
|
+
row.settledAt = this.now();
|
|
4865
|
+
if (end.durationMs !== void 0) {
|
|
4866
|
+
row.durationMs = end.durationMs;
|
|
4867
|
+
}
|
|
4868
|
+
if (end.errorCode !== void 0) {
|
|
4869
|
+
row.errorCode = end.errorCode;
|
|
4870
|
+
}
|
|
4871
|
+
if (end.errorMessage !== void 0) {
|
|
4872
|
+
row.errorMessage = end.errorMessage;
|
|
4873
|
+
}
|
|
4874
|
+
}
|
|
4875
|
+
/** Bump the run's llm-step retry counter. */
|
|
4876
|
+
async bumpRunRetries(runId) {
|
|
4877
|
+
const row = this.runs.get(runId);
|
|
4878
|
+
if (row !== void 0) {
|
|
4879
|
+
row.retries += 1;
|
|
4880
|
+
}
|
|
4881
|
+
}
|
|
4882
|
+
async promoteThread(threadId) {
|
|
4883
|
+
const row = this.threads.get(threadId);
|
|
4884
|
+
if (row?.transient) {
|
|
4885
|
+
row.transient = false;
|
|
4886
|
+
row.updatedAt = this.now();
|
|
4887
|
+
}
|
|
4888
|
+
}
|
|
4889
|
+
async setActiveStream(threadId, runId) {
|
|
4890
|
+
const row = this.threads.get(threadId);
|
|
4891
|
+
if (row !== void 0) {
|
|
4892
|
+
if (runId === null) {
|
|
4893
|
+
delete row.activeStreamId;
|
|
4894
|
+
} else {
|
|
4895
|
+
row.activeStreamId = runId;
|
|
4896
|
+
}
|
|
4897
|
+
}
|
|
4898
|
+
}
|
|
4899
|
+
async appendMessage(input) {
|
|
4900
|
+
const row = this.threads.get(input.threadId);
|
|
4901
|
+
if (row === void 0) {
|
|
4902
|
+
throw new Error(`thread ${input.threadId} not found`);
|
|
4903
|
+
}
|
|
4904
|
+
const message = {
|
|
4905
|
+
id: crypto.randomUUID(),
|
|
4906
|
+
role: input.role,
|
|
4907
|
+
content: input.content,
|
|
4908
|
+
createdAt: this.now(),
|
|
4909
|
+
...input.toolCalls !== void 0 ? {
|
|
4910
|
+
toolCalls: input.toolCalls
|
|
4911
|
+
} : {},
|
|
4912
|
+
...input.toolResults !== void 0 ? {
|
|
4913
|
+
toolResults: input.toolResults
|
|
4914
|
+
} : {},
|
|
4915
|
+
...input.attachments !== void 0 ? {
|
|
4916
|
+
attachments: input.attachments
|
|
4917
|
+
} : {},
|
|
4918
|
+
...input.followUps !== void 0 ? {
|
|
4919
|
+
followUps: input.followUps
|
|
4920
|
+
} : {},
|
|
4921
|
+
...input.usage !== void 0 ? {
|
|
4922
|
+
usage: input.usage
|
|
4923
|
+
} : {},
|
|
4924
|
+
...input.agentName !== void 0 ? {
|
|
4925
|
+
agentName: input.agentName
|
|
4926
|
+
} : {},
|
|
4927
|
+
...input.runId !== void 0 ? {
|
|
4928
|
+
runId: input.runId
|
|
4929
|
+
} : {},
|
|
4930
|
+
...input.reasoning !== void 0 ? {
|
|
4931
|
+
reasoning: input.reasoning
|
|
4932
|
+
} : {},
|
|
4933
|
+
...input.reasoningMs !== void 0 ? {
|
|
4934
|
+
reasoningMs: input.reasoningMs
|
|
4935
|
+
} : {},
|
|
4936
|
+
...input.ui !== void 0 ? {
|
|
4937
|
+
ui: input.ui
|
|
4938
|
+
} : {}
|
|
4939
|
+
};
|
|
4940
|
+
row.messages.push(message);
|
|
4941
|
+
row.updatedAt = message.createdAt;
|
|
4942
|
+
return message;
|
|
4943
|
+
}
|
|
4944
|
+
async setMessageToolResults(messageId, results) {
|
|
4945
|
+
for (const row of this.threads.values()) {
|
|
4946
|
+
const message = row.messages.find((candidate) => candidate.id === messageId);
|
|
4947
|
+
if (message !== void 0) {
|
|
4948
|
+
message.toolResults = results;
|
|
4949
|
+
return;
|
|
4950
|
+
}
|
|
4951
|
+
}
|
|
4952
|
+
}
|
|
4953
|
+
async setMessageUi(messageId, ui) {
|
|
4954
|
+
for (const row of this.threads.values()) {
|
|
4955
|
+
const index = row.messages.findIndex((candidate) => candidate.id === messageId);
|
|
4956
|
+
const message = row.messages[index];
|
|
4957
|
+
if (message !== void 0) {
|
|
4958
|
+
const { ui: _previous, ...rest } = message;
|
|
4959
|
+
row.messages[index] = ui.length > 0 ? {
|
|
4960
|
+
...rest,
|
|
4961
|
+
ui
|
|
4962
|
+
} : rest;
|
|
4963
|
+
return;
|
|
4964
|
+
}
|
|
4965
|
+
}
|
|
4966
|
+
}
|
|
4967
|
+
async threadOfMessage(messageId) {
|
|
4968
|
+
return this.threadIdForMessage(messageId) ?? null;
|
|
4969
|
+
}
|
|
4970
|
+
async setMessageFeedback(messageId, feedback) {
|
|
4971
|
+
for (const row of this.threads.values()) {
|
|
4972
|
+
const index = row.messages.findIndex((candidate) => candidate.id === messageId);
|
|
4973
|
+
const message = row.messages[index];
|
|
4974
|
+
if (message !== void 0) {
|
|
4975
|
+
const { feedback: _previous, ...rest } = message;
|
|
4976
|
+
row.messages[index] = feedback !== null ? {
|
|
4977
|
+
...rest,
|
|
4978
|
+
feedback
|
|
4979
|
+
} : rest;
|
|
4980
|
+
return;
|
|
4981
|
+
}
|
|
4982
|
+
}
|
|
4983
|
+
}
|
|
4984
|
+
async truncateFrom(threadId, messageId) {
|
|
4985
|
+
const row = this.threads.get(threadId);
|
|
4986
|
+
if (row === void 0) {
|
|
4987
|
+
return;
|
|
4988
|
+
}
|
|
4989
|
+
const cutoff = row.messages.findIndex((message) => message.id === messageId);
|
|
4990
|
+
if (cutoff >= 0) {
|
|
4991
|
+
row.messages = row.messages.slice(0, cutoff);
|
|
4992
|
+
}
|
|
4993
|
+
}
|
|
4994
|
+
/**
|
|
4995
|
+
* Of `mediaIds`, the ones a surviving message in one of this actor's threads still carries.
|
|
4996
|
+
* Re-derived from the messages each call, so a media whose message was truncated away reads as
|
|
4997
|
+
* unreferenced again.
|
|
4998
|
+
*/
|
|
4999
|
+
async referencedMediaIds(actorRef, mediaIds) {
|
|
5000
|
+
if (mediaIds.length === 0) {
|
|
5001
|
+
return [];
|
|
5002
|
+
}
|
|
5003
|
+
const wanted = new Set(mediaIds);
|
|
5004
|
+
const found = /* @__PURE__ */ new Set();
|
|
5005
|
+
for (const thread of this.threads.values()) {
|
|
5006
|
+
if (thread.actorRef !== actorRef) {
|
|
5007
|
+
continue;
|
|
5008
|
+
}
|
|
5009
|
+
for (const message of thread.messages) {
|
|
5010
|
+
for (const attachment of message.attachments ?? []) {
|
|
5011
|
+
if (wanted.has(attachment.mediaId)) {
|
|
5012
|
+
found.add(attachment.mediaId);
|
|
5013
|
+
}
|
|
5014
|
+
}
|
|
5015
|
+
}
|
|
5016
|
+
}
|
|
5017
|
+
return [
|
|
5018
|
+
...wanted
|
|
5019
|
+
].filter((mediaId) => found.has(mediaId));
|
|
5020
|
+
}
|
|
5021
|
+
async recordToolCall(input) {
|
|
5022
|
+
this.toolCalls.set(input.toolCallId, {
|
|
5023
|
+
toolCallId: input.toolCallId,
|
|
5024
|
+
messageId: input.messageId,
|
|
5025
|
+
threadId: this.threadIdForMessage(input.messageId) ?? "",
|
|
5026
|
+
toolName: input.toolName,
|
|
5027
|
+
toolType: input.toolType,
|
|
5028
|
+
input: input.input,
|
|
5029
|
+
status: input.status,
|
|
5030
|
+
createdAt: this.now(),
|
|
5031
|
+
...input.runId !== void 0 ? {
|
|
5032
|
+
runId: input.runId
|
|
5033
|
+
} : {},
|
|
5034
|
+
...input.approver !== void 0 ? {
|
|
5035
|
+
approver: input.approver
|
|
5036
|
+
} : {},
|
|
5037
|
+
...input.expiresAt !== void 0 ? {
|
|
5038
|
+
expiresAt: input.expiresAt
|
|
5039
|
+
} : {}
|
|
5040
|
+
});
|
|
5041
|
+
}
|
|
5042
|
+
async updateToolCall(input) {
|
|
5043
|
+
const row = this.toolCalls.get(input.toolCallId);
|
|
5044
|
+
if (row === void 0) {
|
|
5045
|
+
return;
|
|
5046
|
+
}
|
|
5047
|
+
row.status = input.status;
|
|
5048
|
+
if (input.output !== void 0) {
|
|
5049
|
+
row.output = input.output;
|
|
5050
|
+
}
|
|
5051
|
+
if (input.error !== void 0) {
|
|
5052
|
+
row.error = input.error;
|
|
5053
|
+
}
|
|
5054
|
+
if (input.executionMs !== void 0) {
|
|
5055
|
+
row.executionMs = input.executionMs;
|
|
5056
|
+
}
|
|
5057
|
+
if (input.executedByRef !== void 0) {
|
|
5058
|
+
row.executedByRef = input.executedByRef;
|
|
5059
|
+
}
|
|
5060
|
+
if (input.remember !== void 0) {
|
|
5061
|
+
row.remember = input.remember;
|
|
5062
|
+
}
|
|
5063
|
+
if (input.decidedVia !== void 0) {
|
|
5064
|
+
row.decidedVia = input.decidedVia;
|
|
5065
|
+
}
|
|
5066
|
+
}
|
|
5067
|
+
async recordUsage(input) {
|
|
5068
|
+
const createdAt = this.now();
|
|
5069
|
+
this.usage.push({
|
|
5070
|
+
actorRef: input.actorRef,
|
|
5071
|
+
threadId: input.threadId,
|
|
5072
|
+
modelId: input.modelId,
|
|
5073
|
+
inputTokens: input.usage.inputTokens,
|
|
5074
|
+
outputTokens: input.usage.outputTokens,
|
|
5075
|
+
day: createdAt.slice(0, 10),
|
|
5076
|
+
createdAt,
|
|
5077
|
+
...input.usage.cacheWriteTokens !== void 0 ? {
|
|
5078
|
+
cacheWriteTokens: input.usage.cacheWriteTokens
|
|
5079
|
+
} : {},
|
|
5080
|
+
...input.usage.cacheReadTokens !== void 0 ? {
|
|
5081
|
+
cacheReadTokens: input.usage.cacheReadTokens
|
|
5082
|
+
} : {},
|
|
5083
|
+
...input.costUsd !== void 0 ? {
|
|
5084
|
+
costUsd: input.costUsd
|
|
5085
|
+
} : {}
|
|
5086
|
+
});
|
|
5087
|
+
}
|
|
5088
|
+
async quotaToday(actorRef, day) {
|
|
5089
|
+
return this.usageBetween(actorRef, day, day);
|
|
5090
|
+
}
|
|
5091
|
+
async usageBetween(actorRef, fromDay, toDay) {
|
|
5092
|
+
const rows = this.usage.filter((row) => row.actorRef === actorRef && row.day >= fromDay && row.day <= toDay);
|
|
5093
|
+
const usedTokens = rows.reduce((sum, row) => sum + row.inputTokens + row.outputTokens, 0);
|
|
5094
|
+
const costUsd = rows.reduce((sum, row) => sum + (row.costUsd ?? 0), 0);
|
|
5095
|
+
return {
|
|
5096
|
+
usedTokens,
|
|
5097
|
+
costUsd
|
|
5098
|
+
};
|
|
5099
|
+
}
|
|
5100
|
+
/** Test helper: read the recorded usage rows (modelId + token totals). */
|
|
5101
|
+
usageRows() {
|
|
5102
|
+
return this.usage.map((row) => ({
|
|
5103
|
+
actorRef: row.actorRef,
|
|
5104
|
+
tokens: row.inputTokens + row.outputTokens,
|
|
5105
|
+
modelId: row.modelId
|
|
5106
|
+
}));
|
|
5107
|
+
}
|
|
5108
|
+
/** Governance read-model feed: recorded usage rows with the input/output split + thread/day. */
|
|
5109
|
+
governanceUsage() {
|
|
5110
|
+
return this.usage.map((row) => ({
|
|
5111
|
+
actorRef: row.actorRef,
|
|
5112
|
+
threadId: row.threadId,
|
|
5113
|
+
modelId: row.modelId,
|
|
5114
|
+
inputTokens: row.inputTokens,
|
|
5115
|
+
outputTokens: row.outputTokens,
|
|
5116
|
+
day: row.day,
|
|
5117
|
+
createdAt: row.createdAt,
|
|
5118
|
+
...row.cacheWriteTokens !== void 0 ? {
|
|
5119
|
+
cacheWriteTokens: row.cacheWriteTokens
|
|
5120
|
+
} : {},
|
|
5121
|
+
...row.cacheReadTokens !== void 0 ? {
|
|
5122
|
+
cacheReadTokens: row.cacheReadTokens
|
|
5123
|
+
} : {},
|
|
5124
|
+
...row.costUsd !== void 0 ? {
|
|
5125
|
+
costUsd: row.costUsd
|
|
5126
|
+
} : {}
|
|
5127
|
+
}));
|
|
5128
|
+
}
|
|
5129
|
+
/** Governance read-model feed: recorded tool calls with the resolved thread + timestamp. */
|
|
5130
|
+
governanceToolCalls() {
|
|
5131
|
+
return [
|
|
5132
|
+
...this.toolCalls.values()
|
|
5133
|
+
].map((row) => ({
|
|
5134
|
+
toolCallId: row.toolCallId,
|
|
5135
|
+
toolName: row.toolName,
|
|
5136
|
+
toolType: row.toolType,
|
|
5137
|
+
status: row.status,
|
|
5138
|
+
threadId: row.threadId,
|
|
5139
|
+
messageId: row.messageId,
|
|
5140
|
+
createdAt: row.createdAt,
|
|
5141
|
+
...row.executionMs !== void 0 ? {
|
|
5142
|
+
executionMs: row.executionMs
|
|
5143
|
+
} : {},
|
|
5144
|
+
...row.error !== void 0 ? {
|
|
5145
|
+
error: row.error
|
|
5146
|
+
} : {},
|
|
5147
|
+
...row.runId !== void 0 ? {
|
|
5148
|
+
runId: row.runId
|
|
5149
|
+
} : {}
|
|
5150
|
+
}));
|
|
5151
|
+
}
|
|
5152
|
+
/** Governance read-model feed: every stored message, thread-resolved, for the thread drill-down. */
|
|
5153
|
+
governanceMessages() {
|
|
5154
|
+
const rows = [];
|
|
5155
|
+
for (const thread of this.threads.values()) {
|
|
5156
|
+
for (const message of thread.messages) {
|
|
5157
|
+
rows.push({
|
|
5158
|
+
messageId: message.id,
|
|
5159
|
+
threadId: thread.id,
|
|
5160
|
+
role: message.role,
|
|
5161
|
+
content: message.content,
|
|
5162
|
+
createdAt: message.createdAt,
|
|
5163
|
+
...message.agentName !== void 0 ? {
|
|
5164
|
+
agentName: message.agentName
|
|
5165
|
+
} : {}
|
|
5166
|
+
});
|
|
5167
|
+
}
|
|
5168
|
+
}
|
|
5169
|
+
return rows;
|
|
5170
|
+
}
|
|
5171
|
+
/**
|
|
5172
|
+
* Governance read-model feed: tool calls sitting `pending_approval`, joined to their thread
|
|
5173
|
+
* (title/actorRef) and the message that requested them (`agentName`, when the message has one).
|
|
5174
|
+
*/
|
|
5175
|
+
governancePendingApprovals() {
|
|
5176
|
+
const rows = [];
|
|
5177
|
+
for (const call of this.toolCalls.values()) {
|
|
5178
|
+
if (call.status !== "pending_approval") {
|
|
5179
|
+
continue;
|
|
5180
|
+
}
|
|
5181
|
+
const thread = this.threads.get(call.threadId);
|
|
5182
|
+
if (thread === void 0) {
|
|
5183
|
+
continue;
|
|
5184
|
+
}
|
|
5185
|
+
const message = thread.messages.find((candidate) => candidate.id === call.messageId);
|
|
5186
|
+
rows.push({
|
|
5187
|
+
toolCallId: call.toolCallId,
|
|
5188
|
+
toolName: call.toolName,
|
|
5189
|
+
input: call.input,
|
|
5190
|
+
threadId: call.threadId,
|
|
5191
|
+
threadTitle: thread.title,
|
|
5192
|
+
actorRef: thread.actorRef,
|
|
5193
|
+
requestedAt: call.createdAt,
|
|
5194
|
+
...message?.agentName !== void 0 ? {
|
|
5195
|
+
agentName: message.agentName
|
|
5196
|
+
} : {},
|
|
5197
|
+
...call.runId !== void 0 ? {
|
|
5198
|
+
runId: call.runId
|
|
5199
|
+
} : {}
|
|
5200
|
+
});
|
|
5201
|
+
}
|
|
5202
|
+
return rows;
|
|
5203
|
+
}
|
|
5204
|
+
/** Governance read-model feed: thread metadata (title/actor/message count/last activity). */
|
|
5205
|
+
governanceThreads() {
|
|
5206
|
+
return [
|
|
5207
|
+
...this.threads.values()
|
|
5208
|
+
].map((row) => ({
|
|
5209
|
+
threadId: row.id,
|
|
5210
|
+
title: row.title,
|
|
5211
|
+
actorRef: row.actorRef,
|
|
5212
|
+
messageCount: row.messages.length,
|
|
5213
|
+
updatedAt: row.updatedAt
|
|
5214
|
+
}));
|
|
5215
|
+
}
|
|
5216
|
+
/** Governance read-model feed: recorded run outcomes (the reliability surfaces). */
|
|
5217
|
+
governanceRuns() {
|
|
5218
|
+
return [
|
|
5219
|
+
...this.runs.values()
|
|
5220
|
+
].map((row) => ({
|
|
5221
|
+
runId: row.runId,
|
|
5222
|
+
threadId: row.threadId,
|
|
5223
|
+
actorRef: row.actorRef,
|
|
5224
|
+
status: row.status,
|
|
5225
|
+
retries: row.retries,
|
|
5226
|
+
startedAt: row.startedAt,
|
|
5227
|
+
...row.agentName !== void 0 ? {
|
|
5228
|
+
agentName: row.agentName
|
|
5229
|
+
} : {},
|
|
5230
|
+
...row.parentRunId !== void 0 ? {
|
|
5231
|
+
parentRunId: row.parentRunId
|
|
5232
|
+
} : {},
|
|
5233
|
+
...row.durationMs !== void 0 ? {
|
|
5234
|
+
durationMs: row.durationMs
|
|
5235
|
+
} : {},
|
|
5236
|
+
...row.errorCode !== void 0 ? {
|
|
5237
|
+
errorCode: row.errorCode
|
|
5238
|
+
} : {},
|
|
5239
|
+
...row.errorMessage !== void 0 ? {
|
|
5240
|
+
errorMessage: row.errorMessage
|
|
5241
|
+
} : {},
|
|
5242
|
+
...row.settledAt !== void 0 ? {
|
|
5243
|
+
settledAt: row.settledAt
|
|
5244
|
+
} : {},
|
|
5245
|
+
...row.promptHash !== void 0 ? {
|
|
5246
|
+
promptHash: row.promptHash
|
|
5247
|
+
} : {}
|
|
5248
|
+
}));
|
|
5249
|
+
}
|
|
5250
|
+
threadIdForMessage(messageId) {
|
|
5251
|
+
for (const [threadId, row] of this.threads) {
|
|
5252
|
+
if (row.messages.some((message) => message.id === messageId)) {
|
|
5253
|
+
return threadId;
|
|
5254
|
+
}
|
|
5255
|
+
}
|
|
5256
|
+
return void 0;
|
|
5257
|
+
}
|
|
5258
|
+
/** Test helper: read the recorded tool-call rows. */
|
|
5259
|
+
toolCallRows() {
|
|
5260
|
+
return [
|
|
5261
|
+
...this.toolCalls.values()
|
|
5262
|
+
].map((row) => ({
|
|
5263
|
+
toolCallId: row.toolCallId,
|
|
5264
|
+
toolName: row.toolName,
|
|
5265
|
+
toolType: row.toolType,
|
|
5266
|
+
status: row.status,
|
|
5267
|
+
input: row.input,
|
|
5268
|
+
...row.output !== void 0 ? {
|
|
5269
|
+
output: row.output
|
|
5270
|
+
} : {},
|
|
5271
|
+
...row.error !== void 0 ? {
|
|
5272
|
+
error: row.error
|
|
5273
|
+
} : {},
|
|
5274
|
+
...row.runId !== void 0 ? {
|
|
5275
|
+
runId: row.runId
|
|
5276
|
+
} : {},
|
|
5277
|
+
...row.executedByRef !== void 0 ? {
|
|
5278
|
+
executedByRef: row.executedByRef
|
|
5279
|
+
} : {},
|
|
5280
|
+
...row.approver !== void 0 ? {
|
|
5281
|
+
approver: row.approver
|
|
5282
|
+
} : {},
|
|
5283
|
+
...row.expiresAt !== void 0 ? {
|
|
5284
|
+
expiresAt: row.expiresAt
|
|
5285
|
+
} : {},
|
|
5286
|
+
...row.remember !== void 0 ? {
|
|
5287
|
+
remember: row.remember
|
|
5288
|
+
} : {},
|
|
5289
|
+
...row.decidedVia !== void 0 ? {
|
|
5290
|
+
decidedVia: row.decidedVia
|
|
5291
|
+
} : {}
|
|
5292
|
+
}));
|
|
5293
|
+
}
|
|
5294
|
+
toSummary(row) {
|
|
5295
|
+
const last = row.messages[row.messages.length - 1];
|
|
5296
|
+
return {
|
|
5297
|
+
id: row.id,
|
|
5298
|
+
title: row.title,
|
|
5299
|
+
transient: row.transient,
|
|
5300
|
+
createdAt: row.createdAt,
|
|
5301
|
+
updatedAt: row.updatedAt,
|
|
5302
|
+
...last !== void 0 ? {
|
|
5303
|
+
lastMessagePreview: last.content.slice(0, 120)
|
|
5304
|
+
} : {},
|
|
5305
|
+
defaultAgent: row.defaultAgent ?? null,
|
|
5306
|
+
...row.model != null ? {
|
|
5307
|
+
model: row.model
|
|
5308
|
+
} : {}
|
|
5309
|
+
};
|
|
5310
|
+
}
|
|
5311
|
+
};
|
|
4638
5312
|
export {
|
|
4639
5313
|
AGENT_ACTOR_DIRECTORY,
|
|
4640
5314
|
AGENT_ACTOR_RESOLVER,
|
|
@@ -4682,6 +5356,7 @@ export {
|
|
|
4682
5356
|
DefaultRolesPolicy,
|
|
4683
5357
|
ELICITATION_INPUT_TYPES,
|
|
4684
5358
|
GLOBAL_SCOPE,
|
|
5359
|
+
InMemoryAgentStore,
|
|
4685
5360
|
MAX_ASK_QUESTIONS,
|
|
4686
5361
|
OutputRejectedError,
|
|
4687
5362
|
ProcessorFailedError,
|