@zhivex-ai/core 1.18.0 → 1.20.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/README.md +4 -0
- package/dist/agent/approvals.d.ts +7 -0
- package/dist/agent/approvals.d.ts.map +1 -0
- package/dist/agent/approvals.js +97 -0
- package/dist/agent/approvals.js.map +1 -0
- package/dist/agent/cancellation.d.ts +4 -0
- package/dist/agent/cancellation.d.ts.map +1 -0
- package/dist/agent/cancellation.js +69 -0
- package/dist/agent/cancellation.js.map +1 -0
- package/dist/agent/common.d.ts +20 -0
- package/dist/agent/common.d.ts.map +1 -0
- package/dist/agent/common.js +150 -0
- package/dist/agent/common.js.map +1 -0
- package/dist/agent/compaction.d.ts +6 -0
- package/dist/agent/compaction.d.ts.map +1 -0
- package/dist/agent/compaction.js +156 -0
- package/dist/agent/compaction.js.map +1 -0
- package/dist/agent/context.d.ts +58 -0
- package/dist/agent/context.d.ts.map +1 -0
- package/dist/agent/context.js +267 -0
- package/dist/agent/context.js.map +1 -0
- package/dist/agent/definition.d.ts +4 -0
- package/dist/agent/definition.d.ts.map +1 -0
- package/dist/agent/definition.js +35 -0
- package/dist/agent/definition.js.map +1 -0
- package/dist/agent/execution.d.ts +14 -0
- package/dist/agent/execution.d.ts.map +1 -0
- package/dist/agent/execution.js +1062 -0
- package/dist/agent/execution.js.map +1 -0
- package/dist/agent/groups.d.ts +3 -0
- package/dist/agent/groups.d.ts.map +1 -0
- package/dist/agent/groups.js +130 -0
- package/dist/agent/groups.js.map +1 -0
- package/dist/agent/guardrails.d.ts +4 -0
- package/dist/agent/guardrails.d.ts.map +1 -0
- package/dist/agent/guardrails.js +29 -0
- package/dist/agent/guardrails.js.map +1 -0
- package/dist/agent/instance.d.ts +42 -0
- package/dist/agent/instance.d.ts.map +1 -0
- package/dist/agent/instance.js +82 -0
- package/dist/agent/instance.js.map +1 -0
- package/dist/agent/lifecycle.d.ts +31 -0
- package/dist/agent/lifecycle.d.ts.map +1 -0
- package/dist/agent/lifecycle.js +184 -0
- package/dist/agent/lifecycle.js.map +1 -0
- package/dist/agent/state.d.ts +6 -0
- package/dist/agent/state.d.ts.map +1 -0
- package/dist/agent/state.js +119 -0
- package/dist/agent/state.js.map +1 -0
- package/dist/agent/telemetry.d.ts +12 -0
- package/dist/agent/telemetry.d.ts.map +1 -0
- package/dist/agent/telemetry.js +176 -0
- package/dist/agent/telemetry.js.map +1 -0
- package/dist/agent/tool-execution.d.ts +6 -0
- package/dist/agent/tool-execution.d.ts.map +1 -0
- package/dist/agent/tool-execution.js +176 -0
- package/dist/agent/tool-execution.js.map +1 -0
- package/dist/agent-run-view.d.ts +9 -0
- package/dist/agent-run-view.d.ts.map +1 -0
- package/dist/agent-run-view.js +42 -0
- package/dist/agent-run-view.js.map +1 -0
- package/dist/agent-store/file.d.ts +11 -0
- package/dist/agent-store/file.d.ts.map +1 -0
- package/dist/agent-store/file.js +534 -0
- package/dist/agent-store/file.js.map +1 -0
- package/dist/agent-store/memory.d.ts +9 -0
- package/dist/agent-store/memory.d.ts.map +1 -0
- package/dist/agent-store/memory.js +198 -0
- package/dist/agent-store/memory.js.map +1 -0
- package/dist/agent-store/postgres.d.ts +4 -0
- package/dist/agent-store/postgres.d.ts.map +1 -0
- package/dist/agent-store/postgres.js +374 -0
- package/dist/agent-store/postgres.js.map +1 -0
- package/dist/agent-store/shared.d.ts +19 -0
- package/dist/agent-store/shared.d.ts.map +1 -0
- package/dist/agent-store/shared.js +137 -0
- package/dist/agent-store/shared.js.map +1 -0
- package/dist/agent-store/sqlite.d.ts +4 -0
- package/dist/agent-store/sqlite.d.ts.map +1 -0
- package/dist/agent-store/sqlite.js +362 -0
- package/dist/agent-store/sqlite.js.map +1 -0
- package/dist/agent-store.d.ts +5 -21
- package/dist/agent-store.d.ts.map +1 -1
- package/dist/agent-store.js +5 -1588
- package/dist/agent-store.js.map +1 -1
- package/dist/agent.d.ts +6 -57
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +6 -2631
- package/dist/agent.js.map +1 -1
- package/dist/agents-entry.d.ts +19 -0
- package/dist/agents-entry.d.ts.map +1 -0
- package/dist/agents-entry.js +14 -0
- package/dist/agents-entry.js.map +1 -0
- package/dist/api-stability.d.ts.map +1 -1
- package/dist/api-stability.js +1 -0
- package/dist/api-stability.js.map +1 -1
- package/dist/artifact/file.d.ts +6 -0
- package/dist/artifact/file.d.ts.map +1 -0
- package/dist/artifact/file.js +380 -0
- package/dist/artifact/file.js.map +1 -0
- package/dist/artifact/memory.d.ts +3 -0
- package/dist/artifact/memory.d.ts.map +1 -0
- package/dist/artifact/memory.js +80 -0
- package/dist/artifact/memory.js.map +1 -0
- package/dist/artifact/postgres.d.ts +3 -0
- package/dist/artifact/postgres.d.ts.map +1 -0
- package/dist/artifact/postgres.js +283 -0
- package/dist/artifact/postgres.js.map +1 -0
- package/dist/artifact/shared.d.ts +208 -0
- package/dist/artifact/shared.d.ts.map +1 -0
- package/dist/artifact/shared.js +523 -0
- package/dist/artifact/shared.js.map +1 -0
- package/dist/artifact/sqlite.d.ts +3 -0
- package/dist/artifact/sqlite.d.ts.map +1 -0
- package/dist/artifact/sqlite.js +283 -0
- package/dist/artifact/sqlite.js.map +1 -0
- package/dist/artifact.d.ts +6 -186
- package/dist/artifact.d.ts.map +1 -1
- package/dist/artifact.js +6 -1536
- package/dist/artifact.js.map +1 -1
- package/dist/catalog-contracts.d.ts +52 -0
- package/dist/catalog-contracts.d.ts.map +1 -0
- package/dist/catalog-contracts.js +250 -0
- package/dist/catalog-contracts.js.map +1 -0
- package/dist/catalog.d.ts +3 -52
- package/dist/catalog.d.ts.map +1 -1
- package/dist/catalog.js +3 -249
- package/dist/catalog.js.map +1 -1
- package/dist/generate-cache-file.d.ts +11 -0
- package/dist/generate-cache-file.d.ts.map +1 -0
- package/dist/generate-cache-file.js +124 -0
- package/dist/generate-cache-file.js.map +1 -0
- package/dist/generate-text.d.ts +2 -5
- package/dist/generate-text.d.ts.map +1 -1
- package/dist/generate-text.js +6 -18
- package/dist/generate-text.js.map +1 -1
- package/dist/generation-entry.d.ts +6 -0
- package/dist/generation-entry.d.ts.map +1 -0
- package/dist/generation-entry.js +6 -0
- package/dist/generation-entry.js.map +1 -0
- package/dist/image-input.d.ts +4 -0
- package/dist/image-input.d.ts.map +1 -0
- package/dist/image-input.js +26 -0
- package/dist/image-input.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/middleware-runtime.d.ts +35 -0
- package/dist/middleware-runtime.d.ts.map +1 -0
- package/dist/middleware-runtime.js +532 -0
- package/dist/middleware-runtime.js.map +1 -0
- package/dist/middleware.d.ts +3 -43
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +3 -653
- package/dist/middleware.js.map +1 -1
- package/dist/normalize-messages.d.ts +3 -0
- package/dist/normalize-messages.d.ts.map +1 -0
- package/dist/normalize-messages.js +16 -0
- package/dist/normalize-messages.js.map +1 -0
- package/dist/provider-entry.d.ts +18 -0
- package/dist/provider-entry.d.ts.map +1 -0
- package/dist/provider-entry.js +14 -0
- package/dist/provider-entry.js.map +1 -0
- package/dist/realtime.d.ts +2 -0
- package/dist/realtime.d.ts.map +1 -1
- package/dist/realtime.js +6 -0
- package/dist/realtime.js.map +1 -1
- package/dist/store-key.d.ts +3 -0
- package/dist/store-key.d.ts.map +1 -0
- package/dist/store-key.js +5 -0
- package/dist/store-key.js.map +1 -0
- package/dist/store-security.d.ts +1 -2
- package/dist/store-security.d.ts.map +1 -1
- package/dist/store-security.js +2 -4
- package/dist/store-security.js.map +1 -1
- package/dist/types/agent-state.d.ts +236 -0
- package/dist/types/agent-state.d.ts.map +1 -0
- package/dist/types/agent-state.js +2 -0
- package/dist/types/agent-state.js.map +1 -0
- package/dist/types/agents.d.ts +429 -0
- package/dist/types/agents.d.ts.map +1 -0
- package/dist/types/agents.js +2 -0
- package/dist/types/agents.js.map +1 -0
- package/dist/types/common.d.ts +179 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/common.js +2 -0
- package/dist/types/common.js.map +1 -0
- package/dist/types/generation.d.ts +109 -0
- package/dist/types/generation.d.ts.map +1 -0
- package/dist/types/generation.js +2 -0
- package/dist/types/generation.js.map +1 -0
- package/dist/types/media-data.d.ts +65 -0
- package/dist/types/media-data.d.ts.map +1 -0
- package/dist/types/media-data.js +2 -0
- package/dist/types/media-data.js.map +1 -0
- package/dist/types/media.d.ts +141 -0
- package/dist/types/media.d.ts.map +1 -0
- package/dist/types/media.js +2 -0
- package/dist/types/media.js.map +1 -0
- package/dist/types/messages.d.ts +84 -0
- package/dist/types/messages.d.ts.map +1 -0
- package/dist/types/messages.js +2 -0
- package/dist/types/messages.js.map +1 -0
- package/dist/types/middleware.d.ts +130 -0
- package/dist/types/middleware.d.ts.map +1 -0
- package/dist/types/middleware.js +2 -0
- package/dist/types/middleware.js.map +1 -0
- package/dist/types/model-tools.d.ts +409 -0
- package/dist/types/model-tools.d.ts.map +1 -0
- package/dist/types/model-tools.js +2 -0
- package/dist/types/model-tools.js.map +1 -0
- package/dist/types/persistence.d.ts +168 -0
- package/dist/types/persistence.d.ts.map +1 -0
- package/dist/types/persistence.js +2 -0
- package/dist/types/persistence.js.map +1 -0
- package/dist/types/provider-resources.d.ts +336 -0
- package/dist/types/provider-resources.d.ts.map +1 -0
- package/dist/types/provider-resources.js +2 -0
- package/dist/types/provider-resources.js.map +1 -0
- package/dist/types/provider.d.ts +23 -0
- package/dist/types/provider.d.ts.map +1 -0
- package/dist/types/provider.js +2 -0
- package/dist/types/provider.js.map +1 -0
- package/dist/types/resource-options.d.ts +88 -0
- package/dist/types/resource-options.d.ts.map +1 -0
- package/dist/types/resource-options.js +2 -0
- package/dist/types/resource-options.js.map +1 -0
- package/dist/types/stream.d.ts +70 -0
- package/dist/types/stream.d.ts.map +1 -0
- package/dist/types/stream.js +2 -0
- package/dist/types/stream.js.map +1 -0
- package/dist/types/ui.d.ts +110 -0
- package/dist/types/ui.d.ts.map +1 -0
- package/dist/types/ui.js +2 -0
- package/dist/types/ui.js.map +1 -0
- package/dist/types.d.ts +16 -2480
- package/dist/types.d.ts.map +1 -1
- package/dist/ui.d.ts.map +1 -1
- package/dist/ui.js +2 -0
- package/dist/ui.js.map +1 -1
- package/dist/workflow-state-service/file.d.ts +5 -0
- package/dist/workflow-state-service/file.d.ts.map +1 -0
- package/dist/workflow-state-service/file.js +142 -0
- package/dist/workflow-state-service/file.js.map +1 -0
- package/dist/workflow-state-service/memory.d.ts +3 -0
- package/dist/workflow-state-service/memory.d.ts.map +1 -0
- package/dist/workflow-state-service/memory.js +27 -0
- package/dist/workflow-state-service/memory.js.map +1 -0
- package/dist/workflow-state-service/postgres.d.ts +4 -0
- package/dist/workflow-state-service/postgres.d.ts.map +1 -0
- package/dist/workflow-state-service/postgres.js +179 -0
- package/dist/workflow-state-service/postgres.js.map +1 -0
- package/dist/workflow-state-service/shared.d.ts +43 -0
- package/dist/workflow-state-service/shared.d.ts.map +1 -0
- package/dist/workflow-state-service/shared.js +107 -0
- package/dist/workflow-state-service/shared.js.map +1 -0
- package/dist/workflow-state-service/sqlite.d.ts +4 -0
- package/dist/workflow-state-service/sqlite.d.ts.map +1 -0
- package/dist/workflow-state-service/sqlite.js +183 -0
- package/dist/workflow-state-service/sqlite.js.map +1 -0
- package/dist/workflow-state-service.d.ts +6 -33
- package/dist/workflow-state-service.d.ts.map +1 -1
- package/dist/workflow-state-service.js +6 -626
- package/dist/workflow-state-service.js.map +1 -1
- package/package.json +17 -1
package/dist/agent-store.js
CHANGED
|
@@ -1,1589 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { assertPostgresClient } from "./postgres-client.js";
|
|
7
|
-
import { ensurePrivateDirectory, writePrivateFile } from "./store-security.js";
|
|
8
|
-
const cloneState = (state) => JSON.parse(JSON.stringify(normalizeAgentRunState(state)));
|
|
9
|
-
const cloneMessages = (messages) => JSON.parse(JSON.stringify(messages));
|
|
10
|
-
const cloneJournalEntry = (entry) => JSON.parse(JSON.stringify(entry));
|
|
11
|
-
const scopePrefix = (scope) => scope
|
|
12
|
-
? `${encodeURIComponent(scope.namespace ?? "default")}:${encodeURIComponent(scope.tenantId)}:${encodeURIComponent(scope.userId ?? "*")}:`
|
|
13
|
-
: "";
|
|
14
|
-
const scopedKey = (scope, value) => `${scopePrefix(scope)}${value}`;
|
|
15
|
-
const defaultMemoryKey = (context) => context.scope
|
|
16
|
-
? scopedKey(context.scope, context.agentId ?? context.runId)
|
|
17
|
-
: context.runId;
|
|
18
|
-
const sameScope = (left, right) => left.tenantId === right.tenantId && left.userId === right.userId && left.namespace === right.namespace;
|
|
19
|
-
const validateScope = (value) => {
|
|
20
|
-
if (!value)
|
|
21
|
-
return undefined;
|
|
22
|
-
if (typeof value.tenantId !== "string" || value.tenantId.length === 0) {
|
|
23
|
-
throw new ValidationError('Agent store scope "tenantId" must be a non-empty string.');
|
|
24
|
-
}
|
|
25
|
-
for (const field of ["userId", "namespace"]) {
|
|
26
|
-
if (value[field] !== undefined && (typeof value[field] !== "string" || value[field].length === 0)) {
|
|
27
|
-
throw new ValidationError(`Agent store scope "${field}" must be a non-empty string when provided.`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return value;
|
|
31
|
-
};
|
|
32
|
-
const resolveScope = (configured, operation) => {
|
|
33
|
-
configured = validateScope(configured);
|
|
34
|
-
operation = validateScope(operation);
|
|
35
|
-
if (configured && operation && !sameScope(configured, operation)) {
|
|
36
|
-
throw new ValidationError("The operation scope does not match the store scope.");
|
|
37
|
-
}
|
|
38
|
-
return operation ?? configured;
|
|
39
|
-
};
|
|
40
|
-
const fileNameForAgentStoreKey = (key) => `${encodeURIComponent(key)}.json`;
|
|
41
|
-
const fileNameForIdempotencyKey = (key) => `.idempotency-${createHash("sha256").update(key).digest("hex")}.json`;
|
|
42
|
-
const identifierPattern = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
43
|
-
const FILE_RUN_LOCK_TIMEOUT_MS = 2_000;
|
|
44
|
-
const FILE_RUN_LOCK_RETRY_MS = 10;
|
|
45
|
-
const FILE_RUN_LOCK_STALE_MS = 30_000;
|
|
46
|
-
const waitForFileRunLock = () => new Promise((resolve) => {
|
|
47
|
-
setTimeout(resolve, FILE_RUN_LOCK_RETRY_MS);
|
|
48
|
-
});
|
|
49
|
-
const parseFileRunStoreLock = (value) => {
|
|
50
|
-
try {
|
|
51
|
-
const lock = JSON.parse(value);
|
|
52
|
-
return typeof lock.ownerId === "string" &&
|
|
53
|
-
Number.isSafeInteger(lock.pid) &&
|
|
54
|
-
lock.pid > 0 &&
|
|
55
|
-
Number.isFinite(lock.createdAt)
|
|
56
|
-
? lock
|
|
57
|
-
: undefined;
|
|
58
|
-
}
|
|
59
|
-
catch {
|
|
60
|
-
return undefined;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
const isProcessAlive = (pid) => {
|
|
64
|
-
try {
|
|
65
|
-
process.kill(pid, 0);
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
return error.code !== "ESRCH";
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
const readFileRunStoreLock = async (lockPath) => {
|
|
73
|
-
let handle;
|
|
74
|
-
try {
|
|
75
|
-
handle = await fs.open(lockPath, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
76
|
-
const stats = await handle.stat();
|
|
77
|
-
return {
|
|
78
|
-
lock: parseFileRunStoreLock(await handle.readFile("utf8")),
|
|
79
|
-
modifiedAt: stats.mtimeMs
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
catch (error) {
|
|
83
|
-
if (error.code === "ENOENT") {
|
|
84
|
-
return undefined;
|
|
85
|
-
}
|
|
86
|
-
if (error.code === "ELOOP") {
|
|
87
|
-
throw new ValidationError("Refusing to follow a file AgentRunStore lock symlink.");
|
|
88
|
-
}
|
|
89
|
-
throw error;
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
await handle?.close().catch(() => undefined);
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
const removeFileRunStoreLock = async (lockPath, ownerId) => {
|
|
96
|
-
const current = await readFileRunStoreLock(lockPath);
|
|
97
|
-
if (current?.lock?.ownerId !== ownerId) {
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
try {
|
|
101
|
-
await fs.unlink(lockPath);
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
if (error.code === "ENOENT") {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
const recoverFileRunStoreLock = async (lockPath) => {
|
|
112
|
-
const current = await readFileRunStoreLock(lockPath);
|
|
113
|
-
if (!current) {
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
const malformedAndStale = !current.lock && Date.now() - current.modifiedAt >= FILE_RUN_LOCK_STALE_MS;
|
|
117
|
-
const ownerExited = current.lock ? !isProcessAlive(current.lock.pid) : false;
|
|
118
|
-
// Never steal an old lock from a live owner: a large local store can make a
|
|
119
|
-
// legitimate CAS operation exceed the stale threshold. The age fallback is
|
|
120
|
-
// reserved for malformed locks that have no verifiable owner.
|
|
121
|
-
if (!malformedAndStale && !ownerExited) {
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
const recoveryId = createHash("sha256")
|
|
125
|
-
.update(current.lock?.ownerId ?? "malformed")
|
|
126
|
-
.digest("hex");
|
|
127
|
-
const recoveryPath = `${lockPath}.${recoveryId}.recovery`;
|
|
128
|
-
try {
|
|
129
|
-
await fs.link(lockPath, recoveryPath);
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
if (error.code === "ENOENT") {
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
|
-
if (error.code === "EEXIST") {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
throw error;
|
|
139
|
-
}
|
|
140
|
-
try {
|
|
141
|
-
const [lockStats, recoveryStats] = await Promise.all([
|
|
142
|
-
fs.lstat(lockPath),
|
|
143
|
-
fs.lstat(recoveryPath)
|
|
144
|
-
]);
|
|
145
|
-
if (lockStats.dev !== recoveryStats.dev || lockStats.ino !== recoveryStats.ino) {
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
const verified = await readFileRunStoreLock(lockPath);
|
|
149
|
-
if (current.lock?.ownerId !== verified?.lock?.ownerId ||
|
|
150
|
-
(!current.lock && verified?.lock)) {
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
await fs.unlink(lockPath);
|
|
154
|
-
return true;
|
|
155
|
-
}
|
|
156
|
-
catch (error) {
|
|
157
|
-
if (error.code === "ENOENT") {
|
|
158
|
-
return true;
|
|
159
|
-
}
|
|
160
|
-
throw error;
|
|
161
|
-
}
|
|
162
|
-
finally {
|
|
163
|
-
await fs.unlink(recoveryPath).catch((error) => {
|
|
164
|
-
if (error.code !== "ENOENT")
|
|
165
|
-
throw error;
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
const acquireFileRunStoreLock = async (lockPath) => {
|
|
170
|
-
const ownerId = randomUUID();
|
|
171
|
-
const startedAt = Date.now();
|
|
172
|
-
const lock = { ownerId, pid: process.pid, createdAt: startedAt };
|
|
173
|
-
while (true) {
|
|
174
|
-
try {
|
|
175
|
-
await writePrivateFile(lockPath, JSON.stringify(lock), { flag: "wx" });
|
|
176
|
-
return async () => {
|
|
177
|
-
await removeFileRunStoreLock(lockPath, ownerId);
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
catch (error) {
|
|
181
|
-
if (error.code !== "EEXIST") {
|
|
182
|
-
throw error;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
if (await recoverFileRunStoreLock(lockPath)) {
|
|
186
|
-
continue;
|
|
187
|
-
}
|
|
188
|
-
if (Date.now() - startedAt >= FILE_RUN_LOCK_TIMEOUT_MS) {
|
|
189
|
-
throw new ConflictError("Timed out acquiring the file AgentRunStore revision lock.");
|
|
190
|
-
}
|
|
191
|
-
await waitForFileRunLock();
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
const normalizeLimit = (value, fallback = 50) => {
|
|
195
|
-
if (value === undefined)
|
|
196
|
-
return fallback;
|
|
197
|
-
if (!Number.isSafeInteger(value) || value < 1 || value > 1_000) {
|
|
198
|
-
throw new ValidationError('The "limit" option must be an integer between 1 and 1000.');
|
|
199
|
-
}
|
|
200
|
-
return value;
|
|
201
|
-
};
|
|
202
|
-
const validateLeaseOptions = (options) => {
|
|
203
|
-
if (!options.ownerId.trim())
|
|
204
|
-
throw new ValidationError('The lease "ownerId" must not be empty.');
|
|
205
|
-
if (!Number.isSafeInteger(options.ttlMs) || options.ttlMs < 1 || options.ttlMs > 86_400_000) {
|
|
206
|
-
throw new ValidationError('The lease "ttlMs" must be an integer between 1 and 86400000.');
|
|
207
|
-
}
|
|
208
|
-
if (options.now !== undefined && (!Number.isSafeInteger(options.now) || options.now < 0)) {
|
|
209
|
-
throw new ValidationError('The lease "now" value must be a non-negative integer.');
|
|
210
|
-
}
|
|
211
|
-
};
|
|
212
|
-
const encodeCursor = (state) => Buffer.from(JSON.stringify([state.updatedAt ?? state.startedAt ?? 0, state.runId]), "utf8").toString("base64url");
|
|
213
|
-
const decodeCursor = (cursor) => {
|
|
214
|
-
if (!cursor)
|
|
215
|
-
return undefined;
|
|
216
|
-
try {
|
|
217
|
-
const value = JSON.parse(Buffer.from(cursor, "base64url").toString("utf8"));
|
|
218
|
-
if (!Array.isArray(value) || value.length !== 2 || typeof value[0] !== "number" || typeof value[1] !== "string") {
|
|
219
|
-
throw new Error("invalid");
|
|
220
|
-
}
|
|
221
|
-
return [value[0], value[1]];
|
|
222
|
-
}
|
|
223
|
-
catch {
|
|
224
|
-
throw new ValidationError('The "cursor" option is invalid.');
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
const listStates = (states, options = {}) => {
|
|
228
|
-
const limit = normalizeLimit(options.limit);
|
|
229
|
-
const cursor = decodeCursor(options.cursor);
|
|
230
|
-
const filtered = [...states]
|
|
231
|
-
.filter((state) => options.agentId === undefined || state.agentId === options.agentId)
|
|
232
|
-
.filter((state) => options.parentRunId === undefined || state.parentRunId === options.parentRunId)
|
|
233
|
-
.filter((state) => !options.statuses?.length || options.statuses.includes(state.status))
|
|
234
|
-
.filter((state) => options.updatedAfter === undefined || (state.updatedAt ?? 0) > options.updatedAfter)
|
|
235
|
-
.filter((state) => options.updatedBefore === undefined || (state.updatedAt ?? 0) < options.updatedBefore)
|
|
236
|
-
.sort((left, right) => (right.updatedAt ?? right.startedAt ?? 0) - (left.updatedAt ?? left.startedAt ?? 0) || right.runId.localeCompare(left.runId))
|
|
237
|
-
.filter((state) => !cursor || (state.updatedAt ?? state.startedAt ?? 0) < cursor[0] || ((state.updatedAt ?? state.startedAt ?? 0) === cursor[0] && state.runId < cursor[1]));
|
|
238
|
-
const page = filtered.slice(0, limit);
|
|
239
|
-
return {
|
|
240
|
-
items: page.map(cloneState),
|
|
241
|
-
...(filtered.length > limit && page.at(-1) ? { nextCursor: encodeCursor(page.at(-1)) } : {})
|
|
242
|
-
};
|
|
243
|
-
};
|
|
244
|
-
const assertJournalRevision = (current, expectedRevision) => {
|
|
245
|
-
if (expectedRevision !== undefined && (!current || current.revision !== expectedRevision)) {
|
|
246
|
-
throw new ConflictError("Agent tool-call journal revision conflict.");
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
const assertLeaseOwner = (lease, ownerId) => {
|
|
250
|
-
if (ownerId !== undefined && (!lease || lease.ownerId !== ownerId || lease.expiresAt <= Date.now())) {
|
|
251
|
-
throw new ConflictError("Reconciliation lost execution ownership.");
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
const nextJournalEntry = (entry, options) => {
|
|
255
|
-
if (!entry.runId || !entry.toolCallId || !entry.toolName || !entry.idempotencyKey) {
|
|
256
|
-
throw new ValidationError("Tool-call journal entries require runId, toolCallId, toolName, and idempotencyKey.");
|
|
257
|
-
}
|
|
258
|
-
return cloneJournalEntry({
|
|
259
|
-
...entry,
|
|
260
|
-
revision: options?.expectedRevision === undefined ? entry.revision : options.expectedRevision + 1
|
|
261
|
-
});
|
|
262
|
-
};
|
|
263
|
-
const defaultMemoryMessages = (state) => {
|
|
264
|
-
const lastAssistantMessage = [...state.messages].reverse().find((message) => message.role === "assistant");
|
|
265
|
-
return lastAssistantMessage ? [lastAssistantMessage] : [];
|
|
266
|
-
};
|
|
267
|
-
const validateIdentifier = (value, fieldName) => {
|
|
268
|
-
if (!identifierPattern.test(value)) {
|
|
269
|
-
throw new ValidationError(`The "${fieldName}" option must match the SQL identifier pattern [A-Za-z_][A-Za-z0-9_]*.`);
|
|
270
|
-
}
|
|
271
|
-
return value;
|
|
272
|
-
};
|
|
273
|
-
const getRecordField = (value, candidates) => {
|
|
274
|
-
if (!value || typeof value !== "object") {
|
|
275
|
-
return undefined;
|
|
276
|
-
}
|
|
277
|
-
const record = value;
|
|
278
|
-
for (const candidate of candidates) {
|
|
279
|
-
if (candidate in record) {
|
|
280
|
-
return record[candidate];
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return undefined;
|
|
284
|
-
};
|
|
285
|
-
const prepareSqliteStatement = (db, sql) => {
|
|
286
|
-
if (typeof db.prepare === "function") {
|
|
287
|
-
return db.prepare(sql);
|
|
288
|
-
}
|
|
289
|
-
if (typeof db.query === "function") {
|
|
290
|
-
return db.query(sql);
|
|
291
|
-
}
|
|
292
|
-
throw new ValidationError('The "db" option must expose either a "prepare()" or "query()" method.');
|
|
293
|
-
};
|
|
294
|
-
const initializeSqliteTable = (db, sql) => {
|
|
295
|
-
db.exec(sql);
|
|
296
|
-
};
|
|
297
|
-
const assertExpectedRevision = (current, expectedRevision) => {
|
|
298
|
-
if (expectedRevision !== undefined && (current?.revision ?? 0) !== expectedRevision) {
|
|
299
|
-
throw new ConflictError("AgentRunState revision conflict.");
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
const nextStoredState = (state, options) => {
|
|
303
|
-
const normalized = normalizeAgentRunState(state);
|
|
304
|
-
return options?.expectedRevision === undefined
|
|
305
|
-
? normalized
|
|
306
|
-
: { ...normalized, revision: options.expectedRevision + 1 };
|
|
307
|
-
};
|
|
308
|
-
const isConcurrentPostgresTableCreationConflict = (error) => {
|
|
309
|
-
if (!error || typeof error !== "object")
|
|
310
|
-
return false;
|
|
311
|
-
const record = error;
|
|
312
|
-
return record.code === "23505" &&
|
|
313
|
-
(record.constraint === "pg_type_typname_nsp_index" ||
|
|
314
|
-
record.constraint_name === "pg_type_typname_nsp_index");
|
|
315
|
-
};
|
|
316
|
-
const ensurePostgresTable = (() => {
|
|
317
|
-
const initializedTables = new WeakMap();
|
|
318
|
-
return async (client, tableName, createSql) => {
|
|
319
|
-
let tables = initializedTables.get(client);
|
|
320
|
-
if (!tables) {
|
|
321
|
-
tables = new Map();
|
|
322
|
-
initializedTables.set(client, tables);
|
|
323
|
-
}
|
|
324
|
-
let initialization = tables.get(tableName);
|
|
325
|
-
if (!initialization) {
|
|
326
|
-
initialization = (async () => {
|
|
327
|
-
for (let attempt = 0;; attempt += 1) {
|
|
328
|
-
try {
|
|
329
|
-
await client.query(createSql, []);
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
catch (error) {
|
|
333
|
-
if (!isConcurrentPostgresTableCreationConflict(error) || attempt >= 2) {
|
|
334
|
-
throw error;
|
|
335
|
-
}
|
|
336
|
-
await new Promise((resolve) => setTimeout(resolve, 10 * (attempt + 1)));
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
})();
|
|
340
|
-
tables.set(tableName, initialization);
|
|
341
|
-
}
|
|
342
|
-
try {
|
|
343
|
-
await initialization;
|
|
344
|
-
}
|
|
345
|
-
catch (error) {
|
|
346
|
-
if (tables.get(tableName) === initialization) {
|
|
347
|
-
tables.delete(tableName);
|
|
348
|
-
}
|
|
349
|
-
throw error;
|
|
350
|
-
}
|
|
351
|
-
};
|
|
352
|
-
})();
|
|
353
|
-
export const createInMemoryAgentRunStore = (options = {}) => {
|
|
354
|
-
const states = new Map();
|
|
355
|
-
const idempotencyKeys = new Map();
|
|
356
|
-
const parentRunIds = new Map();
|
|
357
|
-
const leases = new Map();
|
|
358
|
-
const journal = new Map();
|
|
359
|
-
const runKey = (runId, scope) => scopedKey(resolveScope(options.scope, scope), runId);
|
|
360
|
-
const idempotencyKey = (key, scope) => scopedKey(resolveScope(options.scope, scope), key);
|
|
361
|
-
const journalKey = (runId, toolCallId, scope) => `${runKey(runId, scope)}:${toolCallId}`;
|
|
362
|
-
const removeParentIndex = (state) => {
|
|
363
|
-
if (!state?.parentRunId) {
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
const parentKey = scopedKey(resolveScope(options.scope, state.scope), state.parentRunId);
|
|
367
|
-
const children = parentRunIds.get(parentKey);
|
|
368
|
-
children?.delete(runKey(state.runId, state.scope));
|
|
369
|
-
if (children?.size === 0) {
|
|
370
|
-
parentRunIds.delete(parentKey);
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
return {
|
|
374
|
-
reconciliationFencing: true,
|
|
375
|
-
load(runId, scope) {
|
|
376
|
-
const state = states.get(runKey(runId, scope));
|
|
377
|
-
return state ? cloneState(normalizeAgentRunState(state)) : undefined;
|
|
378
|
-
},
|
|
379
|
-
findByIdempotencyKey(idempotencyKeyValue, scope) {
|
|
380
|
-
const runId = idempotencyKeys.get(idempotencyKey(idempotencyKeyValue, scope));
|
|
381
|
-
if (!runId) {
|
|
382
|
-
return undefined;
|
|
383
|
-
}
|
|
384
|
-
const state = states.get(runId);
|
|
385
|
-
return state ? cloneState(normalizeAgentRunState(state)) : undefined;
|
|
386
|
-
},
|
|
387
|
-
findByParentRunId(parentRunId, scope) {
|
|
388
|
-
return [...(parentRunIds.get(scopedKey(resolveScope(options.scope, scope), parentRunId)) ?? [])].flatMap((runId) => {
|
|
389
|
-
const state = states.get(runId);
|
|
390
|
-
return state ? [cloneState(normalizeAgentRunState(state))] : [];
|
|
391
|
-
});
|
|
392
|
-
},
|
|
393
|
-
claimIdempotencyKey(state) {
|
|
394
|
-
const scope = resolveScope(options.scope, state.scope);
|
|
395
|
-
const existingRunId = idempotencyKeys.get(idempotencyKey(state.idempotencyKey, scope));
|
|
396
|
-
const existing = existingRunId ? states.get(existingRunId) : undefined;
|
|
397
|
-
if (existing) {
|
|
398
|
-
return { claimed: false, state: cloneState(existing) };
|
|
399
|
-
}
|
|
400
|
-
const normalized = normalizeAgentRunState(state);
|
|
401
|
-
removeParentIndex(states.get(runKey(normalized.runId, scope)));
|
|
402
|
-
const stored = cloneState({ ...normalized, ...(scope ? { scope } : {}) });
|
|
403
|
-
states.set(runKey(normalized.runId, scope), stored);
|
|
404
|
-
idempotencyKeys.set(idempotencyKey(state.idempotencyKey, scope), runKey(normalized.runId, scope));
|
|
405
|
-
if (normalized.parentRunId) {
|
|
406
|
-
const parentKey = scopedKey(scope, normalized.parentRunId);
|
|
407
|
-
const children = parentRunIds.get(parentKey) ?? new Set();
|
|
408
|
-
children.add(runKey(normalized.runId, scope));
|
|
409
|
-
parentRunIds.set(parentKey, children);
|
|
410
|
-
}
|
|
411
|
-
return { claimed: true, state: cloneState(stored) };
|
|
412
|
-
},
|
|
413
|
-
save(state, saveOptions) {
|
|
414
|
-
const scope = resolveScope(options.scope, state.scope);
|
|
415
|
-
assertLeaseOwner(leases.get(runKey(state.runId, scope)), saveOptions?.leaseOwnerId);
|
|
416
|
-
const current = states.get(runKey(state.runId, scope));
|
|
417
|
-
assertExpectedRevision(current, saveOptions?.expectedRevision);
|
|
418
|
-
const normalized = nextStoredState(state, saveOptions);
|
|
419
|
-
removeParentIndex(current);
|
|
420
|
-
states.set(runKey(normalized.runId, scope), cloneState({ ...normalized, ...(scope ? { scope } : {}) }));
|
|
421
|
-
if (normalized.idempotencyKey) {
|
|
422
|
-
const owner = idempotencyKeys.get(idempotencyKey(normalized.idempotencyKey, scope));
|
|
423
|
-
if (owner && owner !== runKey(normalized.runId, scope)) {
|
|
424
|
-
throw new ConflictError("AgentRunState idempotency key conflict.");
|
|
425
|
-
}
|
|
426
|
-
idempotencyKeys.set(idempotencyKey(normalized.idempotencyKey, scope), runKey(normalized.runId, scope));
|
|
427
|
-
}
|
|
428
|
-
if (normalized.parentRunId) {
|
|
429
|
-
const parentKey = scopedKey(scope, normalized.parentRunId);
|
|
430
|
-
const children = parentRunIds.get(parentKey) ?? new Set();
|
|
431
|
-
children.add(runKey(normalized.runId, scope));
|
|
432
|
-
parentRunIds.set(parentKey, children);
|
|
433
|
-
}
|
|
434
|
-
},
|
|
435
|
-
delete(runId, scope) {
|
|
436
|
-
const key = runKey(runId, scope);
|
|
437
|
-
const state = states.get(key);
|
|
438
|
-
if (state?.idempotencyKey) {
|
|
439
|
-
idempotencyKeys.delete(idempotencyKey(state.idempotencyKey, state.scope));
|
|
440
|
-
}
|
|
441
|
-
removeParentIndex(state);
|
|
442
|
-
states.delete(key);
|
|
443
|
-
leases.delete(key);
|
|
444
|
-
for (const journalEntryKey of journal.keys()) {
|
|
445
|
-
if (journalEntryKey.startsWith(`${key}:`))
|
|
446
|
-
journal.delete(journalEntryKey);
|
|
447
|
-
}
|
|
448
|
-
},
|
|
449
|
-
list(listOptions, scope) {
|
|
450
|
-
const prefix = scopePrefix(resolveScope(options.scope, scope));
|
|
451
|
-
return listStates([...states.entries()].filter(([key]) => key.startsWith(prefix)).map(([, state]) => state), listOptions);
|
|
452
|
-
},
|
|
453
|
-
deleteExpired(retention, scope) {
|
|
454
|
-
const prefix = scopePrefix(resolveScope(options.scope, scope));
|
|
455
|
-
const candidates = listStates([...states.entries()].filter(([key]) => key.startsWith(prefix)).map(([, state]) => state), {
|
|
456
|
-
statuses: retention.statuses,
|
|
457
|
-
updatedBefore: retention.before,
|
|
458
|
-
limit: retention.limit ?? 1_000
|
|
459
|
-
}).items;
|
|
460
|
-
for (const state of candidates)
|
|
461
|
-
this.delete?.(state.runId, state.scope);
|
|
462
|
-
return candidates.length;
|
|
463
|
-
},
|
|
464
|
-
acquireLease(runId, leaseOptions, scope) {
|
|
465
|
-
validateLeaseOptions(leaseOptions);
|
|
466
|
-
const key = runKey(runId, scope);
|
|
467
|
-
if (!states.has(key))
|
|
468
|
-
return undefined;
|
|
469
|
-
const now = leaseOptions.now ?? Date.now();
|
|
470
|
-
const current = leases.get(key);
|
|
471
|
-
if (current && current.expiresAt > now && current.ownerId !== leaseOptions.ownerId)
|
|
472
|
-
return undefined;
|
|
473
|
-
const lease = { runId, ownerId: leaseOptions.ownerId, expiresAt: now + leaseOptions.ttlMs };
|
|
474
|
-
leases.set(key, lease);
|
|
475
|
-
return { ...lease };
|
|
476
|
-
},
|
|
477
|
-
renewLease(runId, leaseOptions, scope) {
|
|
478
|
-
validateLeaseOptions(leaseOptions);
|
|
479
|
-
const key = runKey(runId, scope);
|
|
480
|
-
const now = leaseOptions.now ?? Date.now();
|
|
481
|
-
const current = leases.get(key);
|
|
482
|
-
if (!current || current.ownerId !== leaseOptions.ownerId || current.expiresAt <= now)
|
|
483
|
-
return undefined;
|
|
484
|
-
const lease = { runId, ownerId: leaseOptions.ownerId, expiresAt: now + leaseOptions.ttlMs };
|
|
485
|
-
leases.set(key, lease);
|
|
486
|
-
return { ...lease };
|
|
487
|
-
},
|
|
488
|
-
releaseLease(runId, ownerId, scope) {
|
|
489
|
-
const key = runKey(runId, scope);
|
|
490
|
-
if (leases.get(key)?.ownerId !== ownerId)
|
|
491
|
-
return false;
|
|
492
|
-
return leases.delete(key);
|
|
493
|
-
},
|
|
494
|
-
loadToolCall(runId, toolCallId, scope) {
|
|
495
|
-
const entry = journal.get(journalKey(runId, toolCallId, scope));
|
|
496
|
-
return entry ? cloneJournalEntry(entry) : undefined;
|
|
497
|
-
},
|
|
498
|
-
loadToolExecution(runId, toolCallId, scope) {
|
|
499
|
-
return this.loadToolCall?.(runId, toolCallId, scope);
|
|
500
|
-
},
|
|
501
|
-
listToolCalls(runId, scope) {
|
|
502
|
-
const prefix = `${runKey(runId, scope)}:`;
|
|
503
|
-
return [...journal.entries()]
|
|
504
|
-
.filter(([key]) => key.startsWith(prefix))
|
|
505
|
-
.map(([, entry]) => cloneJournalEntry(entry))
|
|
506
|
-
.sort((left, right) => left.updatedAt - right.updatedAt || left.toolCallId.localeCompare(right.toolCallId));
|
|
507
|
-
},
|
|
508
|
-
saveToolCall(entry, journalOptions) {
|
|
509
|
-
const scope = resolveScope(options.scope, entry.scope);
|
|
510
|
-
if (!states.has(runKey(entry.runId, scope)))
|
|
511
|
-
throw new ValidationError("Cannot journal a tool call for an unknown run.");
|
|
512
|
-
assertLeaseOwner(leases.get(runKey(entry.runId, scope)), journalOptions?.leaseOwnerId);
|
|
513
|
-
const key = journalKey(entry.runId, entry.toolCallId, scope);
|
|
514
|
-
const current = journal.get(key);
|
|
515
|
-
assertJournalRevision(current, journalOptions?.expectedRevision);
|
|
516
|
-
const next = nextJournalEntry(entry, journalOptions);
|
|
517
|
-
journal.set(key, next);
|
|
518
|
-
return cloneJournalEntry(next);
|
|
519
|
-
},
|
|
520
|
-
claimToolExecution(entry) {
|
|
521
|
-
const key = journalKey(entry.runId, entry.toolCallId, entry.scope);
|
|
522
|
-
const existing = journal.get(key);
|
|
523
|
-
if (existing)
|
|
524
|
-
return { claimed: false, entry: cloneJournalEntry(existing) };
|
|
525
|
-
const claimed = this.saveToolCall?.({ ...entry, status: "running", revision: 0 });
|
|
526
|
-
return { claimed: true, entry: claimed };
|
|
527
|
-
},
|
|
528
|
-
completeToolExecution(entry, journalOptions) {
|
|
529
|
-
return this.saveToolCall?.({ ...entry, status: entry.status === "failed" ? "failed" : "completed" }, journalOptions);
|
|
530
|
-
}
|
|
531
|
-
};
|
|
532
|
-
};
|
|
533
|
-
export const createFileAgentRunStore = (options) => {
|
|
534
|
-
const effectiveScope = (scope) => resolveScope(options.scope, scope);
|
|
535
|
-
const runPath = (runId, scope) => path.join(options.directory, fileNameForAgentStoreKey(scopedKey(effectiveScope(scope), runId)));
|
|
536
|
-
const idempotencyPath = (key, scope) => path.join(options.directory, fileNameForIdempotencyKey(scopedKey(effectiveScope(scope), key)));
|
|
537
|
-
const leasePath = (runId, scope) => path.join(options.directory, `.lease-${createHash("sha256").update(scopedKey(effectiveScope(scope), runId)).digest("hex")}.json`);
|
|
538
|
-
const toolPath = (runId, toolCallId, scope) => path.join(options.directory, `.tool-${createHash("sha256").update(`${scopedKey(effectiveScope(scope), runId)}:${toolCallId}`).digest("hex")}.json`);
|
|
539
|
-
const revisionLockPath = (runId, scope) => path.join(options.directory, `.run-lock-${createHash("sha256").update(scopedKey(effectiveScope(scope), runId)).digest("hex")}.lock`);
|
|
540
|
-
const load = async (runId, scope) => {
|
|
541
|
-
try {
|
|
542
|
-
const content = await fs.readFile(runPath(runId, scope), "utf8");
|
|
543
|
-
return normalizeAgentRunState(JSON.parse(content));
|
|
544
|
-
}
|
|
545
|
-
catch (error) {
|
|
546
|
-
if (error.code === "ENOENT") {
|
|
547
|
-
return undefined;
|
|
548
|
-
}
|
|
549
|
-
throw error;
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
const findByIdempotencyKey = async (idempotencyKey, scope) => {
|
|
553
|
-
const targetScope = effectiveScope(scope);
|
|
554
|
-
try {
|
|
555
|
-
const marker = await fs.readFile(idempotencyPath(idempotencyKey, scope), "utf8");
|
|
556
|
-
const claimed = normalizeAgentRunState(JSON.parse(marker));
|
|
557
|
-
return (await load(claimed.runId, scope)) ?? claimed;
|
|
558
|
-
}
|
|
559
|
-
catch (error) {
|
|
560
|
-
if (error.code !== "ENOENT") {
|
|
561
|
-
throw error;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
let entries;
|
|
565
|
-
try {
|
|
566
|
-
entries = await fs.readdir(options.directory);
|
|
567
|
-
}
|
|
568
|
-
catch (error) {
|
|
569
|
-
return error.code === "ENOENT" ? undefined : Promise.reject(error);
|
|
570
|
-
}
|
|
571
|
-
for (const entry of entries) {
|
|
572
|
-
if (!entry.endsWith(".json") || entry.startsWith(".")) {
|
|
573
|
-
continue;
|
|
574
|
-
}
|
|
575
|
-
const content = await fs.readFile(path.join(options.directory, entry), "utf8");
|
|
576
|
-
const state = normalizeAgentRunState(JSON.parse(content));
|
|
577
|
-
if (state.idempotencyKey === idempotencyKey && (!targetScope || (state.scope && sameScope(state.scope, targetScope)))) {
|
|
578
|
-
return state;
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
return undefined;
|
|
582
|
-
};
|
|
583
|
-
return {
|
|
584
|
-
reconciliationFencing: true,
|
|
585
|
-
load,
|
|
586
|
-
findByIdempotencyKey,
|
|
587
|
-
async findByParentRunId(parentRunId, scope) {
|
|
588
|
-
const targetScope = effectiveScope(scope);
|
|
589
|
-
let entries;
|
|
590
|
-
try {
|
|
591
|
-
entries = await fs.readdir(options.directory);
|
|
592
|
-
}
|
|
593
|
-
catch (error) {
|
|
594
|
-
if (error.code === "ENOENT") {
|
|
595
|
-
return [];
|
|
596
|
-
}
|
|
597
|
-
throw error;
|
|
598
|
-
}
|
|
599
|
-
const states = [];
|
|
600
|
-
for (const entry of entries) {
|
|
601
|
-
if (!entry.endsWith(".json") || entry.startsWith(".")) {
|
|
602
|
-
continue;
|
|
603
|
-
}
|
|
604
|
-
const content = await fs.readFile(path.join(options.directory, entry), "utf8");
|
|
605
|
-
const state = normalizeAgentRunState(JSON.parse(content));
|
|
606
|
-
if (state.parentRunId === parentRunId && (!targetScope || (state.scope && sameScope(state.scope, targetScope)))) {
|
|
607
|
-
states.push(state);
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
return states;
|
|
611
|
-
},
|
|
612
|
-
async claimIdempotencyKey(state) {
|
|
613
|
-
await ensurePrivateDirectory(options.directory);
|
|
614
|
-
const scope = effectiveScope(state.scope);
|
|
615
|
-
const normalized = normalizeAgentRunState({ ...state, ...(scope ? { scope } : {}) });
|
|
616
|
-
try {
|
|
617
|
-
await writePrivateFile(idempotencyPath(state.idempotencyKey, scope), JSON.stringify(normalized, null, 2), { flag: "wx" });
|
|
618
|
-
await writePrivateFile(runPath(normalized.runId, scope), JSON.stringify(normalized, null, 2));
|
|
619
|
-
return { claimed: true, state: normalized };
|
|
620
|
-
}
|
|
621
|
-
catch (error) {
|
|
622
|
-
if (error.code !== "EEXIST") {
|
|
623
|
-
throw error;
|
|
624
|
-
}
|
|
625
|
-
const existing = await findByIdempotencyKey(state.idempotencyKey, scope);
|
|
626
|
-
if (!existing) {
|
|
627
|
-
throw new ConflictError("AgentRunState idempotency claim could not be loaded.");
|
|
628
|
-
}
|
|
629
|
-
return { claimed: false, state: existing };
|
|
630
|
-
}
|
|
631
|
-
},
|
|
632
|
-
async save(state, saveOptions) {
|
|
633
|
-
await ensurePrivateDirectory(options.directory);
|
|
634
|
-
const scope = effectiveScope(state.scope);
|
|
635
|
-
const releaseLock = await acquireFileRunStoreLock(revisionLockPath(state.runId, scope));
|
|
636
|
-
try {
|
|
637
|
-
if (saveOptions?.leaseOwnerId)
|
|
638
|
-
assertLeaseOwner(JSON.parse(await fs.readFile(leasePath(state.runId, scope), "utf8")), saveOptions.leaseOwnerId);
|
|
639
|
-
const current = await load(state.runId, scope);
|
|
640
|
-
assertExpectedRevision(current, saveOptions?.expectedRevision);
|
|
641
|
-
const normalized = nextStoredState(state, saveOptions);
|
|
642
|
-
if (normalized.idempotencyKey) {
|
|
643
|
-
const owner = await findByIdempotencyKey(normalized.idempotencyKey, scope);
|
|
644
|
-
if (owner && owner.runId !== normalized.runId) {
|
|
645
|
-
throw new ConflictError("AgentRunState idempotency key conflict.");
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
const stored = { ...normalized, ...(scope ? { scope } : {}) };
|
|
649
|
-
await writePrivateFile(runPath(normalized.runId, scope), JSON.stringify(stored, null, 2));
|
|
650
|
-
if (normalized.idempotencyKey) {
|
|
651
|
-
await writePrivateFile(idempotencyPath(normalized.idempotencyKey, scope), JSON.stringify(stored, null, 2));
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
finally {
|
|
655
|
-
await releaseLock();
|
|
656
|
-
}
|
|
657
|
-
},
|
|
658
|
-
async delete(runId, scope) {
|
|
659
|
-
const current = await load(runId, scope);
|
|
660
|
-
const targetScope = effectiveScope(scope);
|
|
661
|
-
try {
|
|
662
|
-
await fs.unlink(runPath(runId, scope));
|
|
663
|
-
}
|
|
664
|
-
catch (error) {
|
|
665
|
-
if (error.code !== "ENOENT") {
|
|
666
|
-
throw error;
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
if (current?.idempotencyKey) {
|
|
670
|
-
try {
|
|
671
|
-
await fs.unlink(idempotencyPath(current.idempotencyKey, scope));
|
|
672
|
-
}
|
|
673
|
-
catch (error) {
|
|
674
|
-
if (error.code !== "ENOENT") {
|
|
675
|
-
throw error;
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
await fs.unlink(leasePath(runId, scope)).catch((error) => {
|
|
680
|
-
if (error.code !== "ENOENT")
|
|
681
|
-
throw error;
|
|
682
|
-
});
|
|
683
|
-
const entries = await fs.readdir(options.directory).catch((error) => {
|
|
684
|
-
if (error.code === "ENOENT")
|
|
685
|
-
return [];
|
|
686
|
-
throw error;
|
|
687
|
-
});
|
|
688
|
-
for (const entryName of entries.filter((name) => name.startsWith(".tool-") && name.endsWith(".json"))) {
|
|
689
|
-
const entryPath = path.join(options.directory, entryName);
|
|
690
|
-
try {
|
|
691
|
-
const journalEntry = JSON.parse(await fs.readFile(entryPath, "utf8"));
|
|
692
|
-
const sameEntryScope = targetScope
|
|
693
|
-
? Boolean(journalEntry.scope && sameScope(journalEntry.scope, targetScope))
|
|
694
|
-
: journalEntry.scope === undefined;
|
|
695
|
-
if (journalEntry.runId === runId && sameEntryScope) {
|
|
696
|
-
await fs.unlink(entryPath);
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
catch (error) {
|
|
700
|
-
if (error.code !== "ENOENT")
|
|
701
|
-
throw error;
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
},
|
|
705
|
-
async list(listOptions, scope) {
|
|
706
|
-
const targetScope = effectiveScope(scope);
|
|
707
|
-
const entries = await fs.readdir(options.directory).catch((error) => {
|
|
708
|
-
if (error.code === "ENOENT")
|
|
709
|
-
return [];
|
|
710
|
-
throw error;
|
|
711
|
-
});
|
|
712
|
-
const states = [];
|
|
713
|
-
for (const entry of entries) {
|
|
714
|
-
if (!entry.endsWith(".json") || entry.startsWith("."))
|
|
715
|
-
continue;
|
|
716
|
-
const state = normalizeAgentRunState(JSON.parse(await fs.readFile(path.join(options.directory, entry), "utf8")));
|
|
717
|
-
if (!targetScope || (state.scope && sameScope(state.scope, targetScope)))
|
|
718
|
-
states.push(state);
|
|
719
|
-
}
|
|
720
|
-
return listStates(states, listOptions);
|
|
721
|
-
},
|
|
722
|
-
async deleteExpired(retention, scope) {
|
|
723
|
-
const page = await this.list?.({ statuses: retention.statuses, updatedBefore: retention.before, limit: retention.limit ?? 1_000 }, scope);
|
|
724
|
-
const items = page.items;
|
|
725
|
-
for (const state of items)
|
|
726
|
-
await this.delete?.(state.runId, state.scope);
|
|
727
|
-
return items.length;
|
|
728
|
-
},
|
|
729
|
-
async acquireLease(runId, leaseOptions, scope) {
|
|
730
|
-
await ensurePrivateDirectory(options.directory);
|
|
731
|
-
const unlock = await acquireFileRunStoreLock(revisionLockPath(runId, scope));
|
|
732
|
-
try {
|
|
733
|
-
validateLeaseOptions(leaseOptions);
|
|
734
|
-
if (!await load(runId, scope))
|
|
735
|
-
return undefined;
|
|
736
|
-
await ensurePrivateDirectory(options.directory);
|
|
737
|
-
const file = leasePath(runId, scope);
|
|
738
|
-
const now = leaseOptions.now ?? Date.now();
|
|
739
|
-
const lease = { runId, ownerId: leaseOptions.ownerId, expiresAt: now + leaseOptions.ttlMs };
|
|
740
|
-
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
741
|
-
try {
|
|
742
|
-
await writePrivateFile(file, JSON.stringify(lease), { flag: "wx" });
|
|
743
|
-
return lease;
|
|
744
|
-
}
|
|
745
|
-
catch (error) {
|
|
746
|
-
if (error.code !== "EEXIST")
|
|
747
|
-
throw error;
|
|
748
|
-
const current = JSON.parse(await fs.readFile(file, "utf8"));
|
|
749
|
-
if (current.ownerId === leaseOptions.ownerId) {
|
|
750
|
-
await writePrivateFile(file, JSON.stringify(lease));
|
|
751
|
-
return lease;
|
|
752
|
-
}
|
|
753
|
-
if (current.expiresAt > now)
|
|
754
|
-
return undefined;
|
|
755
|
-
await fs.unlink(file).catch(() => undefined);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
return undefined;
|
|
759
|
-
}
|
|
760
|
-
finally {
|
|
761
|
-
await unlock();
|
|
762
|
-
}
|
|
763
|
-
},
|
|
764
|
-
async renewLease(runId, leaseOptions, scope) {
|
|
765
|
-
await ensurePrivateDirectory(options.directory);
|
|
766
|
-
const unlock = await acquireFileRunStoreLock(revisionLockPath(runId, scope));
|
|
767
|
-
try {
|
|
768
|
-
validateLeaseOptions(leaseOptions);
|
|
769
|
-
const file = leasePath(runId, scope);
|
|
770
|
-
const now = leaseOptions.now ?? Date.now();
|
|
771
|
-
try {
|
|
772
|
-
const current = JSON.parse(await fs.readFile(file, "utf8"));
|
|
773
|
-
if (current.ownerId !== leaseOptions.ownerId || current.expiresAt <= now)
|
|
774
|
-
return undefined;
|
|
775
|
-
const lease = { runId, ownerId: leaseOptions.ownerId, expiresAt: now + leaseOptions.ttlMs };
|
|
776
|
-
await writePrivateFile(file, JSON.stringify(lease));
|
|
777
|
-
return lease;
|
|
778
|
-
}
|
|
779
|
-
catch (error) {
|
|
780
|
-
if (error.code === "ENOENT")
|
|
781
|
-
return undefined;
|
|
782
|
-
throw error;
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
finally {
|
|
786
|
-
await unlock();
|
|
787
|
-
}
|
|
788
|
-
},
|
|
789
|
-
async releaseLease(runId, ownerId, scope) {
|
|
790
|
-
await ensurePrivateDirectory(options.directory);
|
|
791
|
-
const unlock = await acquireFileRunStoreLock(revisionLockPath(runId, scope));
|
|
792
|
-
try {
|
|
793
|
-
const file = leasePath(runId, scope);
|
|
794
|
-
try {
|
|
795
|
-
const current = JSON.parse(await fs.readFile(file, "utf8"));
|
|
796
|
-
if (current.ownerId !== ownerId)
|
|
797
|
-
return false;
|
|
798
|
-
await fs.unlink(file);
|
|
799
|
-
return true;
|
|
800
|
-
}
|
|
801
|
-
catch (error) {
|
|
802
|
-
if (error.code === "ENOENT")
|
|
803
|
-
return false;
|
|
804
|
-
throw error;
|
|
805
|
-
}
|
|
806
|
-
}
|
|
807
|
-
finally {
|
|
808
|
-
await unlock();
|
|
809
|
-
}
|
|
810
|
-
},
|
|
811
|
-
async loadToolCall(runId, toolCallId, scope) {
|
|
812
|
-
try {
|
|
813
|
-
return JSON.parse(await fs.readFile(toolPath(runId, toolCallId, scope), "utf8"));
|
|
814
|
-
}
|
|
815
|
-
catch (error) {
|
|
816
|
-
if (error.code === "ENOENT")
|
|
817
|
-
return undefined;
|
|
818
|
-
throw error;
|
|
819
|
-
}
|
|
820
|
-
},
|
|
821
|
-
async loadToolExecution(runId, toolCallId, scope) {
|
|
822
|
-
return this.loadToolCall?.(runId, toolCallId, scope);
|
|
823
|
-
},
|
|
824
|
-
async listToolCalls(runId, scope) {
|
|
825
|
-
const targetScope = effectiveScope(scope);
|
|
826
|
-
const entries = await fs.readdir(options.directory).catch(() => []);
|
|
827
|
-
const results = [];
|
|
828
|
-
for (const entry of entries) {
|
|
829
|
-
if (!entry.startsWith(".tool-") || !entry.endsWith(".json"))
|
|
830
|
-
continue;
|
|
831
|
-
const value = JSON.parse(await fs.readFile(path.join(options.directory, entry), "utf8"));
|
|
832
|
-
if (value.runId === runId && (!targetScope || (value.scope && sameScope(value.scope, targetScope))))
|
|
833
|
-
results.push(value);
|
|
834
|
-
}
|
|
835
|
-
return results.sort((a, b) => a.updatedAt - b.updatedAt || a.toolCallId.localeCompare(b.toolCallId));
|
|
836
|
-
},
|
|
837
|
-
async saveToolCall(entry, journalOptions) {
|
|
838
|
-
await ensurePrivateDirectory(options.directory);
|
|
839
|
-
const unlock = await acquireFileRunStoreLock(revisionLockPath(entry.runId, entry.scope));
|
|
840
|
-
try {
|
|
841
|
-
if (journalOptions?.leaseOwnerId)
|
|
842
|
-
assertLeaseOwner(JSON.parse(await fs.readFile(leasePath(entry.runId, entry.scope), "utf8")), journalOptions.leaseOwnerId);
|
|
843
|
-
const file = toolPath(entry.runId, entry.toolCallId, entry.scope);
|
|
844
|
-
const current = await this.loadToolCall?.(entry.runId, entry.toolCallId, entry.scope);
|
|
845
|
-
assertJournalRevision(current, journalOptions?.expectedRevision);
|
|
846
|
-
const next = nextJournalEntry(entry, journalOptions);
|
|
847
|
-
await writePrivateFile(file, JSON.stringify(next, null, 2));
|
|
848
|
-
return next;
|
|
849
|
-
}
|
|
850
|
-
finally {
|
|
851
|
-
await unlock();
|
|
852
|
-
}
|
|
853
|
-
},
|
|
854
|
-
async claimToolExecution(entry) {
|
|
855
|
-
await ensurePrivateDirectory(options.directory);
|
|
856
|
-
const unlock = await acquireFileRunStoreLock(revisionLockPath(entry.runId, entry.scope));
|
|
857
|
-
try {
|
|
858
|
-
await ensurePrivateDirectory(options.directory);
|
|
859
|
-
if (!await load(entry.runId, entry.scope))
|
|
860
|
-
throw new ValidationError("Cannot journal a tool call for an unknown run.");
|
|
861
|
-
const next = nextJournalEntry({ ...entry, status: "running", revision: 0 });
|
|
862
|
-
try {
|
|
863
|
-
await writePrivateFile(toolPath(entry.runId, entry.toolCallId, entry.scope), JSON.stringify(next, null, 2), { flag: "wx" });
|
|
864
|
-
return { claimed: true, entry: next };
|
|
865
|
-
}
|
|
866
|
-
catch (error) {
|
|
867
|
-
if (error.code !== "EEXIST")
|
|
868
|
-
throw error;
|
|
869
|
-
const existing = await this.loadToolCall?.(entry.runId, entry.toolCallId, entry.scope);
|
|
870
|
-
if (!existing)
|
|
871
|
-
throw new ConflictError("Tool execution claim could not be loaded.");
|
|
872
|
-
return { claimed: false, entry: existing };
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
finally {
|
|
876
|
-
await unlock();
|
|
877
|
-
}
|
|
878
|
-
},
|
|
879
|
-
async completeToolExecution(entry, journalOptions) {
|
|
880
|
-
return this.saveToolCall?.({ ...entry, status: entry.status === "failed" ? "failed" : "completed" }, journalOptions);
|
|
881
|
-
}
|
|
882
|
-
};
|
|
883
|
-
};
|
|
884
|
-
export const createInMemoryAgentMemoryStore = (options = {}) => {
|
|
885
|
-
const keyFor = (context) => (options.key ?? defaultMemoryKey)({ ...context, scope: resolveScope(options.scope, context.scope) });
|
|
886
|
-
const selectMessages = options.selectMessages ?? defaultMemoryMessages;
|
|
887
|
-
const memories = new Map(Object.entries(options.initialMessages ?? {}).map(([key, messages]) => [key, cloneMessages(messages)]));
|
|
888
|
-
return {
|
|
889
|
-
load(context) {
|
|
890
|
-
return cloneMessages(memories.get(keyFor(context)) ?? (context.agentId ? options.initialMessages?.[context.agentId] : undefined) ?? []);
|
|
891
|
-
},
|
|
892
|
-
save(context) {
|
|
893
|
-
memories.set(keyFor(context), cloneMessages(selectMessages(context.state)));
|
|
894
|
-
}
|
|
895
|
-
};
|
|
896
|
-
};
|
|
897
|
-
export const createFileAgentMemoryStore = (options) => {
|
|
898
|
-
const keyFor = (context) => (options.key ?? defaultMemoryKey)({ ...context, scope: resolveScope(options.scope, context.scope) });
|
|
899
|
-
const selectMessages = options.selectMessages ?? defaultMemoryMessages;
|
|
900
|
-
return {
|
|
901
|
-
async load(context) {
|
|
902
|
-
try {
|
|
903
|
-
const file = await fs.readFile(path.join(options.directory, fileNameForAgentStoreKey(keyFor(context))), "utf8");
|
|
904
|
-
return JSON.parse(file);
|
|
905
|
-
}
|
|
906
|
-
catch (error) {
|
|
907
|
-
if (error.code === "ENOENT") {
|
|
908
|
-
return [];
|
|
909
|
-
}
|
|
910
|
-
throw error;
|
|
911
|
-
}
|
|
912
|
-
},
|
|
913
|
-
async save(context) {
|
|
914
|
-
await ensurePrivateDirectory(options.directory);
|
|
915
|
-
await writePrivateFile(path.join(options.directory, fileNameForAgentStoreKey(keyFor(context))), JSON.stringify(selectMessages(context.state), null, 2));
|
|
916
|
-
}
|
|
917
|
-
};
|
|
918
|
-
};
|
|
919
|
-
export const createSqliteAgentRunStore = (options) => {
|
|
920
|
-
const tableName = validateIdentifier(options.tableName ?? "zhivex_agent_runs", "tableName");
|
|
921
|
-
const idempotencyTableName = `${tableName}_idempotency`;
|
|
922
|
-
const parentTableName = `${tableName}_parents`;
|
|
923
|
-
const leaseTableName = `${tableName}_leases`;
|
|
924
|
-
const journalTableName = `${tableName}_tool_journal`;
|
|
925
|
-
const dbKey = (value, scope) => scopedKey(resolveScope(options.scope, scope), value);
|
|
926
|
-
initializeSqliteTable(options.db, `CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
927
|
-
run_id TEXT PRIMARY KEY,
|
|
928
|
-
state_json TEXT NOT NULL,
|
|
929
|
-
updated_at_ms INTEGER NOT NULL
|
|
930
|
-
)`);
|
|
931
|
-
initializeSqliteTable(options.db, `CREATE INDEX IF NOT EXISTS ${tableName}_updated_idx ON ${tableName} (updated_at_ms, run_id)`);
|
|
932
|
-
initializeSqliteTable(options.db, `CREATE TABLE IF NOT EXISTS ${leaseTableName} (
|
|
933
|
-
run_key TEXT PRIMARY KEY,
|
|
934
|
-
run_id TEXT NOT NULL,
|
|
935
|
-
owner_id TEXT NOT NULL,
|
|
936
|
-
expires_at_ms INTEGER NOT NULL
|
|
937
|
-
)`);
|
|
938
|
-
initializeSqliteTable(options.db, `CREATE INDEX IF NOT EXISTS ${leaseTableName}_expiry_idx ON ${leaseTableName} (expires_at_ms)`);
|
|
939
|
-
initializeSqliteTable(options.db, `CREATE TABLE IF NOT EXISTS ${journalTableName} (
|
|
940
|
-
run_key TEXT NOT NULL,
|
|
941
|
-
tool_call_id TEXT NOT NULL,
|
|
942
|
-
entry_json TEXT NOT NULL,
|
|
943
|
-
revision INTEGER NOT NULL,
|
|
944
|
-
updated_at_ms INTEGER NOT NULL,
|
|
945
|
-
PRIMARY KEY (run_key, tool_call_id)
|
|
946
|
-
)`);
|
|
947
|
-
initializeSqliteTable(options.db, `CREATE INDEX IF NOT EXISTS ${journalTableName}_run_idx ON ${journalTableName} (run_key, updated_at_ms)`);
|
|
948
|
-
initializeSqliteTable(options.db, `CREATE TABLE IF NOT EXISTS ${idempotencyTableName} (
|
|
949
|
-
idempotency_key TEXT PRIMARY KEY,
|
|
950
|
-
run_id TEXT NOT NULL,
|
|
951
|
-
updated_at_ms INTEGER NOT NULL
|
|
952
|
-
)`);
|
|
953
|
-
initializeSqliteTable(options.db, `CREATE TABLE IF NOT EXISTS ${parentTableName} (
|
|
954
|
-
run_id TEXT PRIMARY KEY,
|
|
955
|
-
parent_run_id TEXT NOT NULL,
|
|
956
|
-
updated_at_ms INTEGER NOT NULL
|
|
957
|
-
)`);
|
|
958
|
-
initializeSqliteTable(options.db, `CREATE INDEX IF NOT EXISTS ${parentTableName}_parent_idx ON ${parentTableName} (parent_run_id, updated_at_ms)`);
|
|
959
|
-
const loadStatement = prepareSqliteStatement(options.db, `SELECT state_json FROM ${tableName} WHERE run_id = ?`);
|
|
960
|
-
const saveStatement = prepareSqliteStatement(options.db, `
|
|
961
|
-
INSERT INTO ${tableName} (run_id, state_json, updated_at_ms)
|
|
962
|
-
VALUES (?, ?, ?)
|
|
963
|
-
ON CONFLICT(run_id) DO UPDATE SET
|
|
964
|
-
state_json = excluded.state_json,
|
|
965
|
-
updated_at_ms = excluded.updated_at_ms
|
|
966
|
-
`);
|
|
967
|
-
const deleteStatement = prepareSqliteStatement(options.db, `DELETE FROM ${tableName} WHERE run_id = ?`);
|
|
968
|
-
const findIdempotencyStatement = prepareSqliteStatement(options.db, `SELECT runs.state_json
|
|
969
|
-
FROM ${tableName} runs
|
|
970
|
-
INNER JOIN ${idempotencyTableName} keys ON keys.run_id = runs.run_id
|
|
971
|
-
WHERE keys.idempotency_key = ?`);
|
|
972
|
-
const saveIdempotencyStatement = prepareSqliteStatement(options.db, `
|
|
973
|
-
INSERT INTO ${idempotencyTableName} (idempotency_key, run_id, updated_at_ms)
|
|
974
|
-
VALUES (?, ?, ?)
|
|
975
|
-
ON CONFLICT(idempotency_key) DO NOTHING
|
|
976
|
-
`);
|
|
977
|
-
const deleteIdempotencyStatement = prepareSqliteStatement(options.db, `DELETE FROM ${idempotencyTableName} WHERE run_id = ?`);
|
|
978
|
-
const findParentStatement = prepareSqliteStatement(options.db, `SELECT runs.state_json
|
|
979
|
-
FROM ${tableName} runs
|
|
980
|
-
INNER JOIN ${parentTableName} parents ON parents.run_id = runs.run_id
|
|
981
|
-
WHERE parents.parent_run_id = ?`);
|
|
982
|
-
const saveParentStatement = prepareSqliteStatement(options.db, `
|
|
983
|
-
INSERT INTO ${parentTableName} (run_id, parent_run_id, updated_at_ms)
|
|
984
|
-
VALUES (?, ?, ?)
|
|
985
|
-
ON CONFLICT(run_id) DO UPDATE SET
|
|
986
|
-
parent_run_id = excluded.parent_run_id,
|
|
987
|
-
updated_at_ms = excluded.updated_at_ms
|
|
988
|
-
`);
|
|
989
|
-
const deleteParentStatement = prepareSqliteStatement(options.db, `DELETE FROM ${parentTableName} WHERE run_id = ?`);
|
|
990
|
-
const listStatement = prepareSqliteStatement(options.db, `SELECT state_json FROM ${tableName}`);
|
|
991
|
-
const loadLeaseStatement = prepareSqliteStatement(options.db, `SELECT owner_id, expires_at_ms FROM ${leaseTableName} WHERE run_key = ?`);
|
|
992
|
-
const saveLeaseStatement = prepareSqliteStatement(options.db, `INSERT INTO ${leaseTableName} (run_key, run_id, owner_id, expires_at_ms) VALUES (?, ?, ?, ?) ON CONFLICT(run_key) DO UPDATE SET owner_id = excluded.owner_id, expires_at_ms = excluded.expires_at_ms`);
|
|
993
|
-
const deleteLeaseStatement = prepareSqliteStatement(options.db, `DELETE FROM ${leaseTableName} WHERE run_key = ? AND owner_id = ?`);
|
|
994
|
-
const deleteRunLeaseStatement = prepareSqliteStatement(options.db, `DELETE FROM ${leaseTableName} WHERE run_key = ?`);
|
|
995
|
-
const deleteRunJournalStatement = prepareSqliteStatement(options.db, `DELETE FROM ${journalTableName} WHERE run_key = ?`);
|
|
996
|
-
const loadJournalStatement = prepareSqliteStatement(options.db, `SELECT entry_json FROM ${journalTableName} WHERE run_key = ? AND tool_call_id = ?`);
|
|
997
|
-
const listJournalStatement = prepareSqliteStatement(options.db, `SELECT entry_json FROM ${journalTableName} WHERE run_key = ? ORDER BY updated_at_ms, tool_call_id`);
|
|
998
|
-
const insertJournalStatement = prepareSqliteStatement(options.db, `INSERT INTO ${journalTableName} (run_key, tool_call_id, entry_json, revision, updated_at_ms) VALUES (?, ?, ?, ?, ?) ON CONFLICT(run_key, tool_call_id) DO NOTHING`);
|
|
999
|
-
const saveJournalStatement = prepareSqliteStatement(options.db, `INSERT INTO ${journalTableName} (run_key, tool_call_id, entry_json, revision, updated_at_ms) VALUES (?, ?, ?, ?, ?) ON CONFLICT(run_key, tool_call_id) DO UPDATE SET entry_json = excluded.entry_json, revision = excluded.revision, updated_at_ms = excluded.updated_at_ms`);
|
|
1000
|
-
return {
|
|
1001
|
-
load(runId, scope) {
|
|
1002
|
-
const row = loadStatement.get([dbKey(runId, scope)]);
|
|
1003
|
-
const stateJson = getRecordField(row, ["state_json", "stateJson"]);
|
|
1004
|
-
return typeof stateJson === "string" ? normalizeAgentRunState(JSON.parse(stateJson)) : undefined;
|
|
1005
|
-
},
|
|
1006
|
-
findByIdempotencyKey(idempotencyKey, scope) {
|
|
1007
|
-
const row = findIdempotencyStatement.get([dbKey(idempotencyKey, scope)]);
|
|
1008
|
-
const stateJson = getRecordField(row, ["state_json", "stateJson"]);
|
|
1009
|
-
return typeof stateJson === "string" ? normalizeAgentRunState(JSON.parse(stateJson)) : undefined;
|
|
1010
|
-
},
|
|
1011
|
-
findByParentRunId(parentRunId, scope) {
|
|
1012
|
-
const rows = findParentStatement.all?.([dbKey(parentRunId, scope)]);
|
|
1013
|
-
if (Array.isArray(rows)) {
|
|
1014
|
-
return rows.flatMap((row) => {
|
|
1015
|
-
const stateJson = getRecordField(row, ["state_json", "stateJson"]);
|
|
1016
|
-
return typeof stateJson === "string" ? [normalizeAgentRunState(JSON.parse(stateJson))] : [];
|
|
1017
|
-
});
|
|
1018
|
-
}
|
|
1019
|
-
const row = findParentStatement.get([dbKey(parentRunId, scope)]);
|
|
1020
|
-
const stateJson = getRecordField(row, ["state_json", "stateJson"]);
|
|
1021
|
-
return typeof stateJson === "string" ? [normalizeAgentRunState(JSON.parse(stateJson))] : [];
|
|
1022
|
-
},
|
|
1023
|
-
claimIdempotencyKey(state) {
|
|
1024
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1025
|
-
try {
|
|
1026
|
-
const scope = resolveScope(options.scope, state.scope);
|
|
1027
|
-
const existingRow = findIdempotencyStatement.get([dbKey(state.idempotencyKey, scope)]);
|
|
1028
|
-
const existingJson = getRecordField(existingRow, ["state_json", "stateJson"]);
|
|
1029
|
-
if (typeof existingJson === "string") {
|
|
1030
|
-
options.db.exec("COMMIT");
|
|
1031
|
-
return { claimed: false, state: normalizeAgentRunState(JSON.parse(existingJson)) };
|
|
1032
|
-
}
|
|
1033
|
-
const normalized = normalizeAgentRunState({ ...state, ...(scope ? { scope } : {}) });
|
|
1034
|
-
const updatedAt = Date.now();
|
|
1035
|
-
saveStatement.run([dbKey(normalized.runId, scope), JSON.stringify(normalized), updatedAt]);
|
|
1036
|
-
saveIdempotencyStatement.run([dbKey(state.idempotencyKey, scope), dbKey(normalized.runId, scope), updatedAt]);
|
|
1037
|
-
if (normalized.parentRunId) {
|
|
1038
|
-
saveParentStatement.run([dbKey(normalized.runId, scope), dbKey(normalized.parentRunId, scope), updatedAt]);
|
|
1039
|
-
}
|
|
1040
|
-
options.db.exec("COMMIT");
|
|
1041
|
-
return { claimed: true, state: normalized };
|
|
1042
|
-
}
|
|
1043
|
-
catch (error) {
|
|
1044
|
-
options.db.exec("ROLLBACK");
|
|
1045
|
-
throw error;
|
|
1046
|
-
}
|
|
1047
|
-
},
|
|
1048
|
-
save(state, saveOptions) {
|
|
1049
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1050
|
-
try {
|
|
1051
|
-
const scope = resolveScope(options.scope, state.scope);
|
|
1052
|
-
const currentRow = loadStatement.get([dbKey(state.runId, scope)]);
|
|
1053
|
-
const currentJson = getRecordField(currentRow, ["state_json", "stateJson"]);
|
|
1054
|
-
const current = typeof currentJson === "string"
|
|
1055
|
-
? normalizeAgentRunState(JSON.parse(currentJson))
|
|
1056
|
-
: undefined;
|
|
1057
|
-
assertExpectedRevision(current, saveOptions?.expectedRevision);
|
|
1058
|
-
const normalized = nextStoredState(state, saveOptions);
|
|
1059
|
-
if (normalized.idempotencyKey) {
|
|
1060
|
-
const ownerRow = findIdempotencyStatement.get([dbKey(normalized.idempotencyKey, scope)]);
|
|
1061
|
-
const ownerJson = getRecordField(ownerRow, ["state_json", "stateJson"]);
|
|
1062
|
-
const owner = typeof ownerJson === "string"
|
|
1063
|
-
? normalizeAgentRunState(JSON.parse(ownerJson))
|
|
1064
|
-
: undefined;
|
|
1065
|
-
if (owner && owner.runId !== normalized.runId) {
|
|
1066
|
-
throw new ConflictError("AgentRunState idempotency key conflict.");
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
const updatedAt = Date.now();
|
|
1070
|
-
const stored = { ...normalized, ...(scope ? { scope } : {}) };
|
|
1071
|
-
saveStatement.run([dbKey(normalized.runId, scope), JSON.stringify(stored), updatedAt]);
|
|
1072
|
-
if (normalized.idempotencyKey) {
|
|
1073
|
-
saveIdempotencyStatement.run([dbKey(normalized.idempotencyKey, scope), dbKey(normalized.runId, scope), updatedAt]);
|
|
1074
|
-
}
|
|
1075
|
-
deleteParentStatement.run([dbKey(normalized.runId, scope)]);
|
|
1076
|
-
if (normalized.parentRunId) {
|
|
1077
|
-
saveParentStatement.run([dbKey(normalized.runId, scope), dbKey(normalized.parentRunId, scope), updatedAt]);
|
|
1078
|
-
}
|
|
1079
|
-
options.db.exec("COMMIT");
|
|
1080
|
-
}
|
|
1081
|
-
catch (error) {
|
|
1082
|
-
options.db.exec("ROLLBACK");
|
|
1083
|
-
throw error;
|
|
1084
|
-
}
|
|
1085
|
-
},
|
|
1086
|
-
delete(runId, scope) {
|
|
1087
|
-
const key = dbKey(runId, scope);
|
|
1088
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1089
|
-
try {
|
|
1090
|
-
deleteStatement.run([key]);
|
|
1091
|
-
deleteIdempotencyStatement.run([key]);
|
|
1092
|
-
deleteParentStatement.run([key]);
|
|
1093
|
-
deleteRunLeaseStatement.run([key]);
|
|
1094
|
-
deleteRunJournalStatement.run([key]);
|
|
1095
|
-
options.db.exec("COMMIT");
|
|
1096
|
-
}
|
|
1097
|
-
catch (error) {
|
|
1098
|
-
options.db.exec("ROLLBACK");
|
|
1099
|
-
throw error;
|
|
1100
|
-
}
|
|
1101
|
-
},
|
|
1102
|
-
list(listOptions, scope) {
|
|
1103
|
-
const rows = listStatement.all?.([]) ?? [];
|
|
1104
|
-
const prefix = scopePrefix(resolveScope(options.scope, scope));
|
|
1105
|
-
const states = rows.flatMap((row) => {
|
|
1106
|
-
const value = getRecordField(row, ["state_json", "stateJson"]);
|
|
1107
|
-
if (typeof value !== "string")
|
|
1108
|
-
return [];
|
|
1109
|
-
const state = normalizeAgentRunState(JSON.parse(value));
|
|
1110
|
-
return scopedKey(state.scope, state.runId).startsWith(prefix) ? [state] : [];
|
|
1111
|
-
});
|
|
1112
|
-
return listStates(states, listOptions);
|
|
1113
|
-
},
|
|
1114
|
-
deleteExpired(retention, scope) {
|
|
1115
|
-
const page = this.list?.({ statuses: retention.statuses, updatedBefore: retention.before, limit: retention.limit ?? 1_000 }, scope);
|
|
1116
|
-
for (const state of page.items)
|
|
1117
|
-
this.delete?.(state.runId, state.scope);
|
|
1118
|
-
return page.items.length;
|
|
1119
|
-
},
|
|
1120
|
-
acquireLease(runId, leaseOptions, scope) {
|
|
1121
|
-
validateLeaseOptions(leaseOptions);
|
|
1122
|
-
const now = leaseOptions.now ?? Date.now();
|
|
1123
|
-
const key = dbKey(runId, scope);
|
|
1124
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1125
|
-
try {
|
|
1126
|
-
const row = loadLeaseStatement.get([key]);
|
|
1127
|
-
const owner = getRecordField(row, ["owner_id", "ownerId"]);
|
|
1128
|
-
const expiry = getRecordField(row, ["expires_at_ms", "expiresAtMs"]);
|
|
1129
|
-
if (typeof owner === "string" && owner !== leaseOptions.ownerId && typeof expiry === "number" && expiry > now) {
|
|
1130
|
-
options.db.exec("COMMIT");
|
|
1131
|
-
return undefined;
|
|
1132
|
-
}
|
|
1133
|
-
const lease = { runId, ownerId: leaseOptions.ownerId, expiresAt: now + leaseOptions.ttlMs };
|
|
1134
|
-
saveLeaseStatement.run([key, runId, lease.ownerId, lease.expiresAt]);
|
|
1135
|
-
options.db.exec("COMMIT");
|
|
1136
|
-
return lease;
|
|
1137
|
-
}
|
|
1138
|
-
catch (error) {
|
|
1139
|
-
options.db.exec("ROLLBACK");
|
|
1140
|
-
throw error;
|
|
1141
|
-
}
|
|
1142
|
-
},
|
|
1143
|
-
renewLease(runId, leaseOptions, scope) {
|
|
1144
|
-
validateLeaseOptions(leaseOptions);
|
|
1145
|
-
const now = leaseOptions.now ?? Date.now();
|
|
1146
|
-
const key = dbKey(runId, scope);
|
|
1147
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1148
|
-
try {
|
|
1149
|
-
const row = loadLeaseStatement.get([key]);
|
|
1150
|
-
const owner = getRecordField(row, ["owner_id", "ownerId"]);
|
|
1151
|
-
const expiry = getRecordField(row, ["expires_at_ms", "expiresAtMs"]);
|
|
1152
|
-
if (owner !== leaseOptions.ownerId || typeof expiry !== "number" || expiry <= now) {
|
|
1153
|
-
options.db.exec("COMMIT");
|
|
1154
|
-
return undefined;
|
|
1155
|
-
}
|
|
1156
|
-
const lease = { runId, ownerId: leaseOptions.ownerId, expiresAt: now + leaseOptions.ttlMs };
|
|
1157
|
-
saveLeaseStatement.run([key, runId, lease.ownerId, lease.expiresAt]);
|
|
1158
|
-
options.db.exec("COMMIT");
|
|
1159
|
-
return lease;
|
|
1160
|
-
}
|
|
1161
|
-
catch (error) {
|
|
1162
|
-
options.db.exec("ROLLBACK");
|
|
1163
|
-
throw error;
|
|
1164
|
-
}
|
|
1165
|
-
},
|
|
1166
|
-
releaseLease(runId, ownerId, scope) {
|
|
1167
|
-
const key = dbKey(runId, scope);
|
|
1168
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1169
|
-
try {
|
|
1170
|
-
const row = loadLeaseStatement.get([key]);
|
|
1171
|
-
if (getRecordField(row, ["owner_id", "ownerId"]) !== ownerId) {
|
|
1172
|
-
options.db.exec("COMMIT");
|
|
1173
|
-
return false;
|
|
1174
|
-
}
|
|
1175
|
-
deleteLeaseStatement.run([key, ownerId]);
|
|
1176
|
-
options.db.exec("COMMIT");
|
|
1177
|
-
return true;
|
|
1178
|
-
}
|
|
1179
|
-
catch (error) {
|
|
1180
|
-
options.db.exec("ROLLBACK");
|
|
1181
|
-
throw error;
|
|
1182
|
-
}
|
|
1183
|
-
},
|
|
1184
|
-
loadToolCall(runId, toolCallId, scope) {
|
|
1185
|
-
const row = loadJournalStatement.get([dbKey(runId, scope), toolCallId]);
|
|
1186
|
-
const value = getRecordField(row, ["entry_json", "entryJson"]);
|
|
1187
|
-
return typeof value === "string" ? JSON.parse(value) : undefined;
|
|
1188
|
-
},
|
|
1189
|
-
loadToolExecution(runId, toolCallId, scope) {
|
|
1190
|
-
return this.loadToolCall?.(runId, toolCallId, scope);
|
|
1191
|
-
},
|
|
1192
|
-
listToolCalls(runId, scope) {
|
|
1193
|
-
const rows = listJournalStatement.all?.([dbKey(runId, scope)]) ?? [];
|
|
1194
|
-
return rows.flatMap((row) => {
|
|
1195
|
-
const value = getRecordField(row, ["entry_json", "entryJson"]);
|
|
1196
|
-
return typeof value === "string" ? [JSON.parse(value)] : [];
|
|
1197
|
-
});
|
|
1198
|
-
},
|
|
1199
|
-
saveToolCall(entry, journalOptions) {
|
|
1200
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1201
|
-
try {
|
|
1202
|
-
const current = this.loadToolCall?.(entry.runId, entry.toolCallId, entry.scope);
|
|
1203
|
-
assertJournalRevision(current, journalOptions?.expectedRevision);
|
|
1204
|
-
const next = nextJournalEntry(entry, journalOptions);
|
|
1205
|
-
saveJournalStatement.run([dbKey(entry.runId, entry.scope), entry.toolCallId, JSON.stringify(next), next.revision, next.updatedAt]);
|
|
1206
|
-
options.db.exec("COMMIT");
|
|
1207
|
-
return next;
|
|
1208
|
-
}
|
|
1209
|
-
catch (error) {
|
|
1210
|
-
options.db.exec("ROLLBACK");
|
|
1211
|
-
throw error;
|
|
1212
|
-
}
|
|
1213
|
-
},
|
|
1214
|
-
claimToolExecution(entry) {
|
|
1215
|
-
options.db.exec("BEGIN IMMEDIATE");
|
|
1216
|
-
try {
|
|
1217
|
-
const current = this.loadToolCall?.(entry.runId, entry.toolCallId, entry.scope);
|
|
1218
|
-
if (current) {
|
|
1219
|
-
options.db.exec("COMMIT");
|
|
1220
|
-
return { claimed: false, entry: current };
|
|
1221
|
-
}
|
|
1222
|
-
const next = nextJournalEntry({ ...entry, status: "running", revision: 0 });
|
|
1223
|
-
insertJournalStatement.run([dbKey(entry.runId, entry.scope), entry.toolCallId, JSON.stringify(next), 0, next.updatedAt]);
|
|
1224
|
-
options.db.exec("COMMIT");
|
|
1225
|
-
return { claimed: true, entry: next };
|
|
1226
|
-
}
|
|
1227
|
-
catch (error) {
|
|
1228
|
-
options.db.exec("ROLLBACK");
|
|
1229
|
-
throw error;
|
|
1230
|
-
}
|
|
1231
|
-
},
|
|
1232
|
-
completeToolExecution(entry, journalOptions) {
|
|
1233
|
-
return this.saveToolCall?.({ ...entry, status: entry.status === "failed" ? "failed" : "completed" }, journalOptions);
|
|
1234
|
-
}
|
|
1235
|
-
};
|
|
1236
|
-
};
|
|
1237
|
-
export const createSqliteAgentMemoryStore = (options) => {
|
|
1238
|
-
const tableName = validateIdentifier(options.tableName ?? "zhivex_agent_memory", "tableName");
|
|
1239
|
-
const keyFor = (context) => (options.key ?? defaultMemoryKey)({ ...context, scope: resolveScope(options.scope, context.scope) });
|
|
1240
|
-
const selectMessages = options.selectMessages ?? defaultMemoryMessages;
|
|
1241
|
-
initializeSqliteTable(options.db, `CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
1242
|
-
memory_key TEXT PRIMARY KEY,
|
|
1243
|
-
messages_json TEXT NOT NULL,
|
|
1244
|
-
updated_at_ms INTEGER NOT NULL
|
|
1245
|
-
)`);
|
|
1246
|
-
const loadStatement = prepareSqliteStatement(options.db, `SELECT messages_json FROM ${tableName} WHERE memory_key = ?`);
|
|
1247
|
-
const saveStatement = prepareSqliteStatement(options.db, `
|
|
1248
|
-
INSERT INTO ${tableName} (memory_key, messages_json, updated_at_ms)
|
|
1249
|
-
VALUES (?, ?, ?)
|
|
1250
|
-
ON CONFLICT(memory_key) DO UPDATE SET
|
|
1251
|
-
messages_json = excluded.messages_json,
|
|
1252
|
-
updated_at_ms = excluded.updated_at_ms
|
|
1253
|
-
`);
|
|
1254
|
-
return {
|
|
1255
|
-
load(context) {
|
|
1256
|
-
const row = loadStatement.get([keyFor(context)]);
|
|
1257
|
-
const messagesJson = getRecordField(row, ["messages_json", "messagesJson"]);
|
|
1258
|
-
return typeof messagesJson === "string" ? JSON.parse(messagesJson) : [];
|
|
1259
|
-
},
|
|
1260
|
-
save(context) {
|
|
1261
|
-
saveStatement.run([keyFor(context), JSON.stringify(selectMessages(context.state)), Date.now()]);
|
|
1262
|
-
}
|
|
1263
|
-
};
|
|
1264
|
-
};
|
|
1265
|
-
export const createPostgresAgentRunStore = (options) => {
|
|
1266
|
-
assertPostgresClient(options.client);
|
|
1267
|
-
const tableName = validateIdentifier(options.tableName ?? "zhivex_agent_runs", "tableName");
|
|
1268
|
-
const idempotencyTableName = `${tableName}_idempotency`;
|
|
1269
|
-
const parentTableName = `${tableName}_parents`;
|
|
1270
|
-
const leaseTableName = `${tableName}_leases`;
|
|
1271
|
-
const journalTableName = `${tableName}_tool_journal`;
|
|
1272
|
-
const dbKey = (value, scope) => scopedKey(resolveScope(options.scope, scope), value);
|
|
1273
|
-
const createSql = `
|
|
1274
|
-
CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
1275
|
-
run_id TEXT PRIMARY KEY,
|
|
1276
|
-
state_json JSONB NOT NULL,
|
|
1277
|
-
updated_at_ms BIGINT NOT NULL
|
|
1278
|
-
)
|
|
1279
|
-
`;
|
|
1280
|
-
const createIdempotencySql = `
|
|
1281
|
-
CREATE TABLE IF NOT EXISTS ${idempotencyTableName} (
|
|
1282
|
-
idempotency_key TEXT PRIMARY KEY,
|
|
1283
|
-
run_id TEXT NOT NULL,
|
|
1284
|
-
updated_at_ms BIGINT NOT NULL
|
|
1285
|
-
)
|
|
1286
|
-
`;
|
|
1287
|
-
const createParentSql = `
|
|
1288
|
-
CREATE TABLE IF NOT EXISTS ${parentTableName} (
|
|
1289
|
-
run_id TEXT PRIMARY KEY,
|
|
1290
|
-
parent_run_id TEXT NOT NULL,
|
|
1291
|
-
updated_at_ms BIGINT NOT NULL
|
|
1292
|
-
)
|
|
1293
|
-
`;
|
|
1294
|
-
const createLeaseSql = `CREATE TABLE IF NOT EXISTS ${leaseTableName} (
|
|
1295
|
-
run_key TEXT PRIMARY KEY,
|
|
1296
|
-
run_id TEXT NOT NULL,
|
|
1297
|
-
owner_id TEXT NOT NULL,
|
|
1298
|
-
expires_at_ms BIGINT NOT NULL
|
|
1299
|
-
)`;
|
|
1300
|
-
const createJournalSql = `CREATE TABLE IF NOT EXISTS ${journalTableName} (
|
|
1301
|
-
run_key TEXT NOT NULL,
|
|
1302
|
-
tool_call_id TEXT NOT NULL,
|
|
1303
|
-
entry_json JSONB NOT NULL,
|
|
1304
|
-
revision BIGINT NOT NULL,
|
|
1305
|
-
updated_at_ms BIGINT NOT NULL,
|
|
1306
|
-
PRIMARY KEY (run_key, tool_call_id)
|
|
1307
|
-
)`;
|
|
1308
|
-
const createIndexesSql = `
|
|
1309
|
-
CREATE INDEX IF NOT EXISTS ${tableName}_updated_idx ON ${tableName} (updated_at_ms DESC, run_id);
|
|
1310
|
-
CREATE INDEX IF NOT EXISTS ${parentTableName}_parent_idx ON ${parentTableName} (parent_run_id, updated_at_ms DESC);
|
|
1311
|
-
CREATE INDEX IF NOT EXISTS ${leaseTableName}_expiry_idx ON ${leaseTableName} (expires_at_ms);
|
|
1312
|
-
CREATE INDEX IF NOT EXISTS ${journalTableName}_run_idx ON ${journalTableName} (run_key, updated_at_ms, tool_call_id)
|
|
1313
|
-
`;
|
|
1314
|
-
const ensureAllTables = async () => {
|
|
1315
|
-
await ensurePostgresTable(options.client, tableName, createSql);
|
|
1316
|
-
await ensurePostgresTable(options.client, idempotencyTableName, createIdempotencySql);
|
|
1317
|
-
await ensurePostgresTable(options.client, parentTableName, createParentSql);
|
|
1318
|
-
await ensurePostgresTable(options.client, leaseTableName, createLeaseSql);
|
|
1319
|
-
await ensurePostgresTable(options.client, journalTableName, createJournalSql);
|
|
1320
|
-
await ensurePostgresTable(options.client, `${tableName}:indexes`, createIndexesSql);
|
|
1321
|
-
};
|
|
1322
|
-
return {
|
|
1323
|
-
async load(runId, scope) {
|
|
1324
|
-
await ensureAllTables();
|
|
1325
|
-
const result = await options.client.query(`SELECT state_json FROM ${tableName} WHERE run_id = $1`, [dbKey(runId, scope)]);
|
|
1326
|
-
const state = result.rows[0] ? (getRecordField(result.rows[0], ["state_json", "stateJson"]) ?? undefined) : undefined;
|
|
1327
|
-
return state ? normalizeAgentRunState(state) : undefined;
|
|
1328
|
-
},
|
|
1329
|
-
async findByIdempotencyKey(idempotencyKey, scope) {
|
|
1330
|
-
await ensureAllTables();
|
|
1331
|
-
const result = await options.client.query(`SELECT runs.state_json
|
|
1332
|
-
FROM ${tableName} runs
|
|
1333
|
-
INNER JOIN ${idempotencyTableName} keys ON keys.run_id = runs.run_id
|
|
1334
|
-
WHERE keys.idempotency_key = $1`, [dbKey(idempotencyKey, scope)]);
|
|
1335
|
-
const state = result.rows[0] ? (getRecordField(result.rows[0], ["state_json", "stateJson"]) ?? undefined) : undefined;
|
|
1336
|
-
return state ? normalizeAgentRunState(state) : undefined;
|
|
1337
|
-
},
|
|
1338
|
-
async findByParentRunId(parentRunId, scope) {
|
|
1339
|
-
await ensureAllTables();
|
|
1340
|
-
const result = await options.client.query(`SELECT runs.state_json
|
|
1341
|
-
FROM ${tableName} runs
|
|
1342
|
-
INNER JOIN ${parentTableName} parents ON parents.run_id = runs.run_id
|
|
1343
|
-
WHERE parents.parent_run_id = $1`, [dbKey(parentRunId, scope)]);
|
|
1344
|
-
return result.rows.flatMap((row) => {
|
|
1345
|
-
const state = getRecordField(row, ["state_json", "stateJson"]) ?? undefined;
|
|
1346
|
-
return state ? [normalizeAgentRunState(state)] : [];
|
|
1347
|
-
});
|
|
1348
|
-
},
|
|
1349
|
-
async claimIdempotencyKey(state) {
|
|
1350
|
-
await ensureAllTables();
|
|
1351
|
-
const scope = resolveScope(options.scope, state.scope);
|
|
1352
|
-
const normalized = normalizeAgentRunState({ ...state, ...(scope ? { scope } : {}) });
|
|
1353
|
-
const updatedAt = Date.now();
|
|
1354
|
-
await options.client.query(`INSERT INTO ${tableName} (run_id, state_json, updated_at_ms)
|
|
1355
|
-
VALUES ($1, $2::jsonb, $3)
|
|
1356
|
-
ON CONFLICT(run_id) DO NOTHING`, [dbKey(normalized.runId, scope), normalized, updatedAt]);
|
|
1357
|
-
const claim = await options.client.query(`INSERT INTO ${idempotencyTableName} (idempotency_key, run_id, updated_at_ms)
|
|
1358
|
-
VALUES ($1, $2, $3)
|
|
1359
|
-
ON CONFLICT(idempotency_key) DO NOTHING
|
|
1360
|
-
RETURNING run_id`, [dbKey(state.idempotencyKey, scope), dbKey(normalized.runId, scope), updatedAt]);
|
|
1361
|
-
const claimedRunId = getRecordField(claim.rows[0], ["run_id", "runId"]);
|
|
1362
|
-
if (claimedRunId === dbKey(normalized.runId, scope)) {
|
|
1363
|
-
if (normalized.parentRunId) {
|
|
1364
|
-
await options.client.query(`INSERT INTO ${parentTableName} (run_id, parent_run_id, updated_at_ms)
|
|
1365
|
-
VALUES ($1, $2, $3)
|
|
1366
|
-
ON CONFLICT(run_id) DO UPDATE SET
|
|
1367
|
-
parent_run_id = EXCLUDED.parent_run_id,
|
|
1368
|
-
updated_at_ms = EXCLUDED.updated_at_ms`, [dbKey(normalized.runId, scope), dbKey(normalized.parentRunId, scope), updatedAt]);
|
|
1369
|
-
}
|
|
1370
|
-
return { claimed: true, state: normalized };
|
|
1371
|
-
}
|
|
1372
|
-
const existing = await options.client.query(`SELECT runs.state_json
|
|
1373
|
-
FROM ${tableName} runs
|
|
1374
|
-
INNER JOIN ${idempotencyTableName} keys ON keys.run_id = runs.run_id
|
|
1375
|
-
WHERE keys.idempotency_key = $1`, [dbKey(state.idempotencyKey, scope)]);
|
|
1376
|
-
const existingState = existing.rows[0]
|
|
1377
|
-
? getRecordField(existing.rows[0], ["state_json", "stateJson"])
|
|
1378
|
-
: undefined;
|
|
1379
|
-
if (!existingState) {
|
|
1380
|
-
throw new ConflictError("AgentRunState idempotency claim could not be loaded.");
|
|
1381
|
-
}
|
|
1382
|
-
if (existingState.runId !== normalized.runId) {
|
|
1383
|
-
await options.client.query(`DELETE FROM ${tableName} WHERE run_id = $1`, [dbKey(normalized.runId, scope)]);
|
|
1384
|
-
}
|
|
1385
|
-
return { claimed: false, state: normalizeAgentRunState(existingState) };
|
|
1386
|
-
},
|
|
1387
|
-
async save(state, saveOptions) {
|
|
1388
|
-
await ensureAllTables();
|
|
1389
|
-
const scope = resolveScope(options.scope, state.scope);
|
|
1390
|
-
const normalized = nextStoredState(state, saveOptions);
|
|
1391
|
-
const stored = { ...normalized, ...(scope ? { scope } : {}) };
|
|
1392
|
-
const updatedAt = Date.now();
|
|
1393
|
-
if (normalized.idempotencyKey) {
|
|
1394
|
-
const owner = await options.client.query(`SELECT run_id FROM ${idempotencyTableName} WHERE idempotency_key = $1`, [dbKey(normalized.idempotencyKey, scope)]);
|
|
1395
|
-
const ownerRunId = getRecordField(owner.rows[0], ["run_id", "runId"]);
|
|
1396
|
-
if (typeof ownerRunId === "string" && ownerRunId !== dbKey(normalized.runId, scope)) {
|
|
1397
|
-
throw new ConflictError("AgentRunState idempotency key conflict.");
|
|
1398
|
-
}
|
|
1399
|
-
}
|
|
1400
|
-
if (saveOptions?.expectedRevision === undefined) {
|
|
1401
|
-
await options.client.query(`INSERT INTO ${tableName} (run_id, state_json, updated_at_ms)
|
|
1402
|
-
VALUES ($1, $2::jsonb, $3)
|
|
1403
|
-
ON CONFLICT(run_id) DO UPDATE SET
|
|
1404
|
-
state_json = EXCLUDED.state_json,
|
|
1405
|
-
updated_at_ms = EXCLUDED.updated_at_ms`, [dbKey(normalized.runId, scope), stored, updatedAt]);
|
|
1406
|
-
}
|
|
1407
|
-
else {
|
|
1408
|
-
const saved = await options.client.query(`INSERT INTO ${tableName} (run_id, state_json, updated_at_ms)
|
|
1409
|
-
VALUES ($1, $2::jsonb, $3)
|
|
1410
|
-
ON CONFLICT(run_id) DO UPDATE SET
|
|
1411
|
-
state_json = EXCLUDED.state_json,
|
|
1412
|
-
updated_at_ms = EXCLUDED.updated_at_ms
|
|
1413
|
-
WHERE COALESCE((${tableName}.state_json->>'revision')::bigint, 0) = $4
|
|
1414
|
-
RETURNING run_id`, [dbKey(normalized.runId, scope), stored, updatedAt, saveOptions.expectedRevision]);
|
|
1415
|
-
if (getRecordField(saved.rows[0], ["run_id", "runId"]) !== dbKey(normalized.runId, scope)) {
|
|
1416
|
-
throw new ConflictError("AgentRunState revision conflict.");
|
|
1417
|
-
}
|
|
1418
|
-
}
|
|
1419
|
-
if (normalized.idempotencyKey) {
|
|
1420
|
-
await options.client.query(`INSERT INTO ${idempotencyTableName} (idempotency_key, run_id, updated_at_ms)
|
|
1421
|
-
VALUES ($1, $2, $3)
|
|
1422
|
-
ON CONFLICT(idempotency_key) DO NOTHING`, [dbKey(normalized.idempotencyKey, scope), dbKey(normalized.runId, scope), updatedAt]);
|
|
1423
|
-
}
|
|
1424
|
-
await options.client.query(`DELETE FROM ${parentTableName} WHERE run_id = $1`, [dbKey(normalized.runId, scope)]);
|
|
1425
|
-
if (normalized.parentRunId) {
|
|
1426
|
-
await options.client.query(`INSERT INTO ${parentTableName} (run_id, parent_run_id, updated_at_ms)
|
|
1427
|
-
VALUES ($1, $2, $3)
|
|
1428
|
-
ON CONFLICT(run_id) DO UPDATE SET
|
|
1429
|
-
parent_run_id = EXCLUDED.parent_run_id,
|
|
1430
|
-
updated_at_ms = EXCLUDED.updated_at_ms`, [dbKey(normalized.runId, scope), dbKey(normalized.parentRunId, scope), updatedAt]);
|
|
1431
|
-
}
|
|
1432
|
-
},
|
|
1433
|
-
async delete(runId, scope) {
|
|
1434
|
-
await ensureAllTables();
|
|
1435
|
-
const key = dbKey(runId, scope);
|
|
1436
|
-
await options.client.query(`WITH deleted_run AS (
|
|
1437
|
-
DELETE FROM ${tableName} WHERE run_id = $1 RETURNING run_id
|
|
1438
|
-
), deleted_idempotency AS (
|
|
1439
|
-
DELETE FROM ${idempotencyTableName} WHERE run_id = $1 RETURNING run_id
|
|
1440
|
-
), deleted_parent AS (
|
|
1441
|
-
DELETE FROM ${parentTableName} WHERE run_id = $1 RETURNING run_id
|
|
1442
|
-
), deleted_lease AS (
|
|
1443
|
-
DELETE FROM ${leaseTableName} WHERE run_key = $1 RETURNING run_key
|
|
1444
|
-
)
|
|
1445
|
-
DELETE FROM ${journalTableName} WHERE run_key = $1`, [key]);
|
|
1446
|
-
},
|
|
1447
|
-
async list(listOptions, scope) {
|
|
1448
|
-
await ensureAllTables();
|
|
1449
|
-
const prefix = scopePrefix(resolveScope(options.scope, scope));
|
|
1450
|
-
const result = await options.client.query(`SELECT state_json FROM ${tableName} WHERE run_id >= $1 AND run_id < $2`, [prefix, `${prefix}\uffff`]);
|
|
1451
|
-
const states = result.rows.flatMap((row) => {
|
|
1452
|
-
const state = getRecordField(row, ["state_json", "stateJson"]);
|
|
1453
|
-
return state ? [normalizeAgentRunState(state)] : [];
|
|
1454
|
-
});
|
|
1455
|
-
return listStates(states, listOptions);
|
|
1456
|
-
},
|
|
1457
|
-
async deleteExpired(retention, scope) {
|
|
1458
|
-
const page = await this.list?.({ statuses: retention.statuses, updatedBefore: retention.before, limit: retention.limit ?? 1_000 }, scope);
|
|
1459
|
-
for (const state of page.items)
|
|
1460
|
-
await this.delete?.(state.runId, state.scope);
|
|
1461
|
-
return page.items.length;
|
|
1462
|
-
},
|
|
1463
|
-
async acquireLease(runId, leaseOptions, scope) {
|
|
1464
|
-
validateLeaseOptions(leaseOptions);
|
|
1465
|
-
await ensureAllTables();
|
|
1466
|
-
const now = leaseOptions.now ?? Date.now();
|
|
1467
|
-
const key = dbKey(runId, scope);
|
|
1468
|
-
const expiresAt = now + leaseOptions.ttlMs;
|
|
1469
|
-
const result = await options.client.query(`INSERT INTO ${leaseTableName} (run_key, run_id, owner_id, expires_at_ms)
|
|
1470
|
-
SELECT $1, $2, $3, $4
|
|
1471
|
-
WHERE EXISTS (SELECT 1 FROM ${tableName} WHERE run_id = $1)
|
|
1472
|
-
ON CONFLICT(run_key) DO UPDATE SET
|
|
1473
|
-
owner_id = EXCLUDED.owner_id,
|
|
1474
|
-
expires_at_ms = EXCLUDED.expires_at_ms
|
|
1475
|
-
WHERE ${leaseTableName}.owner_id = EXCLUDED.owner_id OR ${leaseTableName}.expires_at_ms <= $5
|
|
1476
|
-
RETURNING owner_id, expires_at_ms`, [key, runId, leaseOptions.ownerId, expiresAt, now]);
|
|
1477
|
-
const owner = getRecordField(result.rows[0], ["owner_id", "ownerId"]);
|
|
1478
|
-
return owner === leaseOptions.ownerId ? { runId, ownerId: leaseOptions.ownerId, expiresAt } : undefined;
|
|
1479
|
-
},
|
|
1480
|
-
async renewLease(runId, leaseOptions, scope) {
|
|
1481
|
-
validateLeaseOptions(leaseOptions);
|
|
1482
|
-
await ensureAllTables();
|
|
1483
|
-
const now = leaseOptions.now ?? Date.now();
|
|
1484
|
-
const expiresAt = now + leaseOptions.ttlMs;
|
|
1485
|
-
const result = await options.client.query(`UPDATE ${leaseTableName}
|
|
1486
|
-
SET expires_at_ms = $3
|
|
1487
|
-
WHERE run_key = $1 AND owner_id = $2 AND expires_at_ms > $4
|
|
1488
|
-
RETURNING owner_id`, [dbKey(runId, scope), leaseOptions.ownerId, expiresAt, now]);
|
|
1489
|
-
return getRecordField(result.rows[0], ["owner_id", "ownerId"]) === leaseOptions.ownerId
|
|
1490
|
-
? { runId, ownerId: leaseOptions.ownerId, expiresAt }
|
|
1491
|
-
: undefined;
|
|
1492
|
-
},
|
|
1493
|
-
async releaseLease(runId, ownerId, scope) {
|
|
1494
|
-
await ensureAllTables();
|
|
1495
|
-
const result = await options.client.query(`DELETE FROM ${leaseTableName} WHERE run_key = $1 AND owner_id = $2 RETURNING owner_id`, [dbKey(runId, scope), ownerId]);
|
|
1496
|
-
return getRecordField(result.rows[0], ["owner_id", "ownerId"]) === ownerId;
|
|
1497
|
-
},
|
|
1498
|
-
async loadToolCall(runId, toolCallId, scope) {
|
|
1499
|
-
await ensureAllTables();
|
|
1500
|
-
const result = await options.client.query(`SELECT entry_json FROM ${journalTableName} WHERE run_key = $1 AND tool_call_id = $2`, [dbKey(runId, scope), toolCallId]);
|
|
1501
|
-
const entry = getRecordField(result.rows[0], ["entry_json", "entryJson"]);
|
|
1502
|
-
return entry && typeof entry === "object" ? cloneJournalEntry(entry) : undefined;
|
|
1503
|
-
},
|
|
1504
|
-
async loadToolExecution(runId, toolCallId, scope) {
|
|
1505
|
-
return this.loadToolCall?.(runId, toolCallId, scope);
|
|
1506
|
-
},
|
|
1507
|
-
async listToolCalls(runId, scope) {
|
|
1508
|
-
await ensureAllTables();
|
|
1509
|
-
const result = await options.client.query(`SELECT entry_json FROM ${journalTableName} WHERE run_key = $1 ORDER BY updated_at_ms, tool_call_id`, [dbKey(runId, scope)]);
|
|
1510
|
-
return result.rows.flatMap((row) => {
|
|
1511
|
-
const entry = getRecordField(row, ["entry_json", "entryJson"]);
|
|
1512
|
-
return entry && typeof entry === "object" ? [cloneJournalEntry(entry)] : [];
|
|
1513
|
-
});
|
|
1514
|
-
},
|
|
1515
|
-
async saveToolCall(entry, journalOptions) {
|
|
1516
|
-
await ensureAllTables();
|
|
1517
|
-
const next = nextJournalEntry(entry, journalOptions);
|
|
1518
|
-
const key = dbKey(entry.runId, entry.scope);
|
|
1519
|
-
const result = journalOptions?.expectedRevision === undefined
|
|
1520
|
-
? await options.client.query(`INSERT INTO ${journalTableName} (run_key, tool_call_id, entry_json, revision, updated_at_ms)
|
|
1521
|
-
VALUES ($1, $2, $3::jsonb, $4, $5)
|
|
1522
|
-
ON CONFLICT(run_key, tool_call_id) DO UPDATE SET
|
|
1523
|
-
entry_json = EXCLUDED.entry_json,
|
|
1524
|
-
revision = EXCLUDED.revision,
|
|
1525
|
-
updated_at_ms = EXCLUDED.updated_at_ms
|
|
1526
|
-
RETURNING revision`, [key, entry.toolCallId, next, next.revision, next.updatedAt])
|
|
1527
|
-
: await options.client.query(`UPDATE ${journalTableName}
|
|
1528
|
-
SET entry_json = $3::jsonb,
|
|
1529
|
-
revision = $4,
|
|
1530
|
-
updated_at_ms = $5
|
|
1531
|
-
WHERE run_key = $1 AND tool_call_id = $2 AND revision = $6
|
|
1532
|
-
RETURNING revision`, [key, entry.toolCallId, next, next.revision, next.updatedAt, journalOptions.expectedRevision]);
|
|
1533
|
-
if (getRecordField(result.rows[0], ["revision"]) === undefined) {
|
|
1534
|
-
throw new ConflictError("Agent tool-call journal revision conflict.");
|
|
1535
|
-
}
|
|
1536
|
-
return next;
|
|
1537
|
-
},
|
|
1538
|
-
async claimToolExecution(entry) {
|
|
1539
|
-
await ensureAllTables();
|
|
1540
|
-
const next = nextJournalEntry({ ...entry, status: "running", revision: 0 });
|
|
1541
|
-
const result = await options.client.query(`INSERT INTO ${journalTableName} (run_key, tool_call_id, entry_json, revision, updated_at_ms)
|
|
1542
|
-
SELECT $1, $2, $3::jsonb, 0, $4
|
|
1543
|
-
WHERE EXISTS (SELECT 1 FROM ${tableName} WHERE run_id = $1)
|
|
1544
|
-
ON CONFLICT(run_key, tool_call_id) DO NOTHING
|
|
1545
|
-
RETURNING entry_json`, [dbKey(entry.runId, entry.scope), entry.toolCallId, next, next.updatedAt]);
|
|
1546
|
-
const claimed = getRecordField(result.rows[0], ["entry_json", "entryJson"]);
|
|
1547
|
-
if (claimed && typeof claimed === "object")
|
|
1548
|
-
return { claimed: true, entry: next };
|
|
1549
|
-
const existing = await this.loadToolCall?.(entry.runId, entry.toolCallId, entry.scope);
|
|
1550
|
-
if (!existing)
|
|
1551
|
-
throw new ValidationError("Cannot journal a tool call for an unknown run.");
|
|
1552
|
-
return { claimed: false, entry: existing };
|
|
1553
|
-
},
|
|
1554
|
-
async completeToolExecution(entry, journalOptions) {
|
|
1555
|
-
return this.saveToolCall?.({ ...entry, status: entry.status === "failed" ? "failed" : "completed" }, journalOptions);
|
|
1556
|
-
}
|
|
1557
|
-
};
|
|
1558
|
-
};
|
|
1559
|
-
export const createPostgresAgentMemoryStore = (options) => {
|
|
1560
|
-
assertPostgresClient(options.client);
|
|
1561
|
-
const tableName = validateIdentifier(options.tableName ?? "zhivex_agent_memory", "tableName");
|
|
1562
|
-
const keyFor = (context) => (options.key ?? defaultMemoryKey)({ ...context, scope: resolveScope(options.scope, context.scope) });
|
|
1563
|
-
const selectMessages = options.selectMessages ?? defaultMemoryMessages;
|
|
1564
|
-
const createSql = `
|
|
1565
|
-
CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
1566
|
-
memory_key TEXT PRIMARY KEY,
|
|
1567
|
-
messages_json JSONB NOT NULL,
|
|
1568
|
-
updated_at_ms BIGINT NOT NULL
|
|
1569
|
-
)
|
|
1570
|
-
`;
|
|
1571
|
-
return {
|
|
1572
|
-
async load(context) {
|
|
1573
|
-
await ensurePostgresTable(options.client, tableName, createSql);
|
|
1574
|
-
const result = await options.client.query(`SELECT messages_json FROM ${tableName} WHERE memory_key = $1`, [keyFor(context)]);
|
|
1575
|
-
return result.rows[0]
|
|
1576
|
-
? (getRecordField(result.rows[0], ["messages_json", "messagesJson"]) ?? [])
|
|
1577
|
-
: [];
|
|
1578
|
-
},
|
|
1579
|
-
async save(context) {
|
|
1580
|
-
await ensurePostgresTable(options.client, tableName, createSql);
|
|
1581
|
-
await options.client.query(`INSERT INTO ${tableName} (memory_key, messages_json, updated_at_ms)
|
|
1582
|
-
VALUES ($1, $2::jsonb, $3)
|
|
1583
|
-
ON CONFLICT(memory_key) DO UPDATE SET
|
|
1584
|
-
messages_json = EXCLUDED.messages_json,
|
|
1585
|
-
updated_at_ms = EXCLUDED.updated_at_ms`, [keyFor(context), selectMessages(context.state), Date.now()]);
|
|
1586
|
-
}
|
|
1587
|
-
};
|
|
1588
|
-
};
|
|
1
|
+
/** Compatibility facade. Backend implementations live in dedicated internal modules. */
|
|
2
|
+
export { createInMemoryAgentRunStore, createInMemoryAgentMemoryStore } from "./agent-store/memory.js";
|
|
3
|
+
export { createFileAgentRunStore, createFileAgentMemoryStore } from "./agent-store/file.js";
|
|
4
|
+
export { createSqliteAgentRunStore, createSqliteAgentMemoryStore } from "./agent-store/sqlite.js";
|
|
5
|
+
export { createPostgresAgentRunStore, createPostgresAgentMemoryStore } from "./agent-store/postgres.js";
|
|
1589
6
|
//# sourceMappingURL=agent-store.js.map
|