@botbotgo/agent-harness 0.0.475 → 0.0.476
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 +3 -1234
- package/README.zh.md +3 -1191
- package/dist/acp.js +1 -1
- package/dist/api.js +1 -404
- package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
- package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
- package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
- package/dist/cli/chat-interactive.js +25 -244
- package/dist/cli/chat-rendering.js +6 -100
- package/dist/cli/chat-stream.js +23 -512
- package/dist/cli/chat-ui.js +21 -199
- package/dist/cli/chat-workspace.js +2 -210
- package/dist/cli/main.js +21 -428
- package/dist/cli/managed-service-commands.js +9 -63
- package/dist/cli/managed-service.js +2 -137
- package/dist/cli/options-init-chat.js +1 -108
- package/dist/cli/options-runtime.js +1 -158
- package/dist/cli/options-serve.js +1 -282
- package/dist/cli/options.js +2 -19
- package/dist/cli/process-guards.js +1 -139
- package/dist/cli/request-tree.js +7 -296
- package/dist/cli/runtime-commands.js +12 -258
- package/dist/cli/runtime-output.js +16 -155
- package/dist/cli/server-commands.js +16 -270
- package/dist/cli/workspace.js +1 -67
- package/dist/cli.js +1 -7
- package/dist/client/acp.js +1 -1
- package/dist/client/in-process.js +1 -67
- package/dist/client/index.js +1 -2
- package/dist/client/types.js +0 -1
- package/dist/client.js +1 -1
- package/dist/contracts/core.js +1 -1
- package/dist/contracts/runtime-evaluation.js +0 -1
- package/dist/contracts/runtime-memory.js +0 -1
- package/dist/contracts/runtime-observability.js +0 -1
- package/dist/contracts/runtime-requests.js +0 -1
- package/dist/contracts/runtime-scheduling.js +0 -1
- package/dist/contracts/runtime.js +1 -27
- package/dist/contracts/types.js +1 -3
- package/dist/contracts/workspace.js +0 -1
- package/dist/flow/build-flow-graph.js +1 -50
- package/dist/flow/export-mermaid.js +2 -464
- package/dist/flow/export-sequence-mermaid.js +2 -325
- package/dist/flow/flow-graph-normalization.js +1 -214
- package/dist/flow/flow-graph-runtime.js +1 -107
- package/dist/flow/flow-graph-upstream.js +1 -494
- package/dist/flow/index.js +1 -3
- package/dist/flow/types.js +0 -1
- package/dist/index.js +1 -5
- package/dist/init-project.js +1 -1
- package/dist/knowledge/config.js +1 -32
- package/dist/knowledge/contracts.js +0 -1
- package/dist/knowledge/index.js +1 -2
- package/dist/knowledge/module.js +12 -909
- package/dist/knowledge/procedural/config.js +1 -125
- package/dist/knowledge/procedural/index.js +1 -2
- package/dist/knowledge/procedural/manager.js +9 -345
- package/dist/mcp.js +1 -2
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -2
- package/dist/persistence/file-store.js +3 -758
- package/dist/persistence/sqlite-request-context-store.js +5 -54
- package/dist/persistence/sqlite-request-queue-store.js +10 -108
- package/dist/persistence/sqlite-runtime.js +1 -86
- package/dist/persistence/sqlite-store.js +62 -810
- package/dist/persistence/types.js +0 -1
- package/dist/projections/presentation.js +37 -206
- package/dist/projections/request-events.js +2 -502
- package/dist/projections/upstream-events.js +1 -201
- package/dist/protocol/a2a/http-discovery.js +1 -178
- package/dist/protocol/a2a/http-rpc.js +6 -622
- package/dist/protocol/a2a/http.js +1 -138
- package/dist/protocol/a2a/task-state.js +3 -317
- package/dist/protocol/acp/client.js +8 -294
- package/dist/protocol/acp/harness-client.js +1 -218
- package/dist/protocol/acp/http.js +5 -130
- package/dist/protocol/acp/server.js +1 -310
- package/dist/protocol/acp/stdio.js +2 -69
- package/dist/protocol/ag-ui/http.js +3 -378
- package/dist/protocol/mcp/server.js +1 -428
- package/dist/resource/backend/workspace-scoped-backend.js +1 -319
- package/dist/resource/isolation.js +1 -237
- package/dist/resource/mcp/tool-support.js +3 -296
- package/dist/resource/mcp-tool-support.js +1 -2
- package/dist/resource/providers/resource-provider.js +1 -215
- package/dist/resource/resource-impl.js +1 -3
- package/dist/resource/resource-types.js +0 -1
- package/dist/resource/resource.js +1 -1
- package/dist/resource/sources.js +1 -247
- package/dist/resource/tools/function-tool-resolver.js +2 -272
- package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
- package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
- package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
- package/dist/runtime/adapter/flow/execution-context.js +1 -71
- package/dist/runtime/adapter/flow/invocation-flow.js +8 -425
- package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
- package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
- package/dist/runtime/adapter/invocation-result.js +2 -473
- package/dist/runtime/adapter/local-tool-invocation.js +6 -638
- package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
- package/dist/runtime/adapter/middleware-assembly.js +5 -477
- package/dist/runtime/adapter/model/invocation-request.js +3 -183
- package/dist/runtime/adapter/model/message-assembly.js +1 -28
- package/dist/runtime/adapter/model/model-providers.js +23 -1115
- package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
- package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
- package/dist/runtime/adapter/resilience.js +1 -104
- package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
- package/dist/runtime/adapter/runtime-shell.js +5 -166
- package/dist/runtime/adapter/stream-event-projection.js +2 -622
- package/dist/runtime/adapter/stream-text-consumption.js +1 -18
- package/dist/runtime/adapter/terminal-status.js +2 -67
- package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
- package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
- package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
- package/dist/runtime/adapter/tool/provider-tool.js +1 -25
- package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
- package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
- package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
- package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
- package/dist/runtime/adapter/tool/tool-replay.js +1 -37
- package/dist/runtime/adapter/tool-resolution.js +1 -86
- package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
- package/dist/runtime/agent-runtime-adapter.js +60 -2338
- package/dist/runtime/agent-runtime-assembly.js +7 -249
- package/dist/runtime/env/runtime-env.js +1 -62
- package/dist/runtime/harness/background-runtime.js +1 -8
- package/dist/runtime/harness/bindings.js +1 -58
- package/dist/runtime/harness/events/event-bus.js +1 -16
- package/dist/runtime/harness/events/event-sink.js +1 -61
- package/dist/runtime/harness/events/events.js +1 -80
- package/dist/runtime/harness/events/listener-runtime.js +1 -13
- package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
- package/dist/runtime/harness/events/streaming.js +1 -100
- package/dist/runtime/harness/events/timeline.js +1 -52
- package/dist/runtime/harness/public-shapes.js +1 -186
- package/dist/runtime/harness/run/artifact-paths.js +1 -15
- package/dist/runtime/harness/run/governance.js +1 -295
- package/dist/runtime/harness/run/helpers.js +1 -71
- package/dist/runtime/harness/run/inspection.js +1 -409
- package/dist/runtime/harness/run/operator-overview.js +1 -80
- package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
- package/dist/runtime/harness/run/recovery.js +1 -162
- package/dist/runtime/harness/run/resources.js +1 -60
- package/dist/runtime/harness/run/resume.js +1 -56
- package/dist/runtime/harness/run/routing.js +1 -48
- package/dist/runtime/harness/run/run-lifecycle.js +1 -66
- package/dist/runtime/harness/run/run-operations.js +1 -217
- package/dist/runtime/harness/run/run-queue.js +1 -43
- package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
- package/dist/runtime/harness/run/session-records.js +1 -97
- package/dist/runtime/harness/run/start-run.js +1 -120
- package/dist/runtime/harness/run/startup-runtime.js +1 -69
- package/dist/runtime/harness/run/stream-run.js +8 -1418
- package/dist/runtime/harness/run/surface-semantics.js +1 -79
- package/dist/runtime/harness/runtime-defaults.js +1 -39
- package/dist/runtime/harness/system/boundary-analysis.js +1 -234
- package/dist/runtime/harness/system/health-monitor.js +1 -258
- package/dist/runtime/harness/system/inventory.js +1 -129
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
- package/dist/runtime/harness/system/policy-engine.js +1 -175
- package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
- package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
- package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
- package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
- package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
- package/dist/runtime/harness/system/session-memory-sync.js +3 -113
- package/dist/runtime/harness/system/skill-requirements.js +1 -112
- package/dist/runtime/harness/system/store.js +9 -365
- package/dist/runtime/harness/tool-gateway/index.js +1 -2
- package/dist/runtime/harness/tool-gateway/policy.js +1 -45
- package/dist/runtime/harness/tool-gateway/validation.js +1 -176
- package/dist/runtime/harness/tool-schema.js +1 -3
- package/dist/runtime/harness.js +3 -1490
- package/dist/runtime/index.js +1 -3
- package/dist/runtime/layout/runtime-layout.js +1 -31
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
- package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
- package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
- package/dist/runtime/parsing/output-content.js +10 -550
- package/dist/runtime/parsing/output-parsing.js +1 -4
- package/dist/runtime/parsing/output-recovery.js +3 -213
- package/dist/runtime/parsing/output-tool-args.js +7 -663
- package/dist/runtime/parsing/stream-event-parsing.js +3 -362
- package/dist/runtime/prompts/runtime-prompts.js +4 -73
- package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
- package/dist/runtime/skills/skill-metadata.js +1 -197
- package/dist/runtime/startup-tracing.js +2 -37
- package/dist/runtime/support/compiled-binding.js +1 -290
- package/dist/runtime/support/embedding-models.js +1 -118
- package/dist/runtime/support/harness-support.js +5 -137
- package/dist/runtime/support/llamaindex.js +1 -108
- package/dist/runtime/support/runtime-adapter-options.js +1 -29
- package/dist/runtime/support/runtime-factories.js +1 -51
- package/dist/runtime/support/vector-stores.js +9 -270
- package/dist/scaffold/init-project.js +54 -233
- package/dist/tooling/extensions.js +1 -311
- package/dist/tooling/module-loader.js +1 -55
- package/dist/tools.js +1 -176
- package/dist/utils/agent-display.js +1 -18
- package/dist/utils/bundled-text.js +4 -39
- package/dist/utils/compiled-binding.js +1 -33
- package/dist/utils/fs.js +2 -45
- package/dist/utils/id.js +1 -9
- package/dist/utils/message-content.js +1 -30
- package/dist/utils/object.js +1 -6
- package/dist/workspace/agent-binding-compiler.js +3 -613
- package/dist/workspace/compile.js +1 -472
- package/dist/workspace/framework-contract-validation.js +2 -322
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/object-loader-paths.js +1 -71
- package/dist/workspace/object-loader-readers.js +1 -187
- package/dist/workspace/object-loader.js +1 -754
- package/dist/workspace/resource-compilers.js +1 -374
- package/dist/workspace/support/agent-capabilities.js +1 -37
- package/dist/workspace/support/agent-execution-config.js +1 -44
- package/dist/workspace/support/discovery.js +1 -147
- package/dist/workspace/support/source-collectors.js +1 -30
- package/dist/workspace/support/source-protocols.js +2 -192
- package/dist/workspace/support/workspace-ref-utils.js +1 -362
- package/dist/workspace/tool-hydration.js +1 -280
- package/dist/workspace/validate.js +1 -99
- package/dist/workspace/yaml-object-reader.js +1 -285
- package/package.json +7 -3
|
@@ -1,296 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { MemorySaver } from "@langchain/langgraph";
|
|
4
|
-
import { createClient } from "@libsql/client";
|
|
5
|
-
import { UPSTREAM_SESSION_CONFIG_KEY } from "../adapter/upstream-configurable-keys.js";
|
|
6
|
-
const CHECKPOINT_TABLE = "checkpoint_state";
|
|
7
|
-
const DEFAULT_STATE_KEY = "default";
|
|
8
|
-
const SQLITE_BUSY_RETRY_ATTEMPTS = 30;
|
|
9
|
-
const SQLITE_BUSY_RETRY_DELAY_MS = 100;
|
|
10
|
-
function normalizeCheckpointConfig(config) {
|
|
11
|
-
if (!config?.configurable) {
|
|
12
|
-
return config;
|
|
13
|
-
}
|
|
14
|
-
if (UPSTREAM_SESSION_CONFIG_KEY in config.configurable) {
|
|
15
|
-
return config;
|
|
16
|
-
}
|
|
17
|
-
const sessionId = config.configurable.session_id;
|
|
18
|
-
if (typeof sessionId !== "string" || sessionId.length === 0) {
|
|
19
|
-
return config;
|
|
20
|
-
}
|
|
21
|
-
return {
|
|
22
|
-
...config,
|
|
23
|
-
configurable: {
|
|
24
|
-
...config.configurable,
|
|
25
|
-
[UPSTREAM_SESSION_CONFIG_KEY]: sessionId,
|
|
26
|
-
},
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
function encodeBinary(value) {
|
|
30
|
-
if (value instanceof Uint8Array) {
|
|
31
|
-
return {
|
|
32
|
-
__type: "Uint8Array",
|
|
33
|
-
data: Array.from(value),
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
if (Array.isArray(value)) {
|
|
37
|
-
return value.map((item) => encodeBinary(item));
|
|
38
|
-
}
|
|
39
|
-
if (typeof value === "object" && value) {
|
|
40
|
-
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, encodeBinary(entry)]));
|
|
41
|
-
}
|
|
42
|
-
return value;
|
|
43
|
-
}
|
|
44
|
-
function decodeBinary(value) {
|
|
45
|
-
if (Array.isArray(value)) {
|
|
46
|
-
return value.map((item) => decodeBinary(item));
|
|
47
|
-
}
|
|
48
|
-
if (typeof value === "object" && value) {
|
|
49
|
-
const typed = value;
|
|
50
|
-
if (typed.__type === "Uint8Array" && Array.isArray(typed.data)) {
|
|
51
|
-
return new Uint8Array(typed.data.map((item) => Number(item)));
|
|
52
|
-
}
|
|
53
|
-
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, decodeBinary(entry)]));
|
|
54
|
-
}
|
|
55
|
-
return value;
|
|
56
|
-
}
|
|
57
|
-
function parseWriteScopeKey(key) {
|
|
58
|
-
try {
|
|
59
|
-
const parsed = JSON.parse(key);
|
|
60
|
-
return Array.isArray(parsed) && typeof parsed[0] === "string" ? parsed[0] : undefined;
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function extractScopedStorage(storage, stateKey) {
|
|
67
|
-
if (stateKey === DEFAULT_STATE_KEY) {
|
|
68
|
-
return structuredClone(storage);
|
|
69
|
-
}
|
|
70
|
-
const scoped = storage[stateKey];
|
|
71
|
-
return scoped === undefined ? {} : { [stateKey]: structuredClone(scoped) };
|
|
72
|
-
}
|
|
73
|
-
function extractScopedWrites(writes, stateKey) {
|
|
74
|
-
if (stateKey === DEFAULT_STATE_KEY) {
|
|
75
|
-
return structuredClone(writes);
|
|
76
|
-
}
|
|
77
|
-
return Object.fromEntries(Object.entries(writes)
|
|
78
|
-
.filter(([key]) => parseWriteScopeKey(key) === stateKey)
|
|
79
|
-
.map(([key, value]) => [key, structuredClone(value)]));
|
|
80
|
-
}
|
|
81
|
-
function createIsolatedMemorySaver(storage, writes) {
|
|
82
|
-
const saver = new MemorySaver();
|
|
83
|
-
const typed = saver;
|
|
84
|
-
typed.storage = storage;
|
|
85
|
-
typed.writes = writes;
|
|
86
|
-
return saver;
|
|
87
|
-
}
|
|
88
|
-
function serializeSaverState(saver) {
|
|
89
|
-
const typed = saver;
|
|
90
|
-
return {
|
|
91
|
-
storage: structuredClone(typed.storage ?? {}),
|
|
92
|
-
writes: structuredClone(typed.writes ?? {}),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
function collectErrorMessages(error) {
|
|
96
|
-
const messages = [];
|
|
97
|
-
let current = error;
|
|
98
|
-
while (current instanceof Error) {
|
|
99
|
-
messages.push(current.message);
|
|
100
|
-
current = current.cause;
|
|
101
|
-
}
|
|
102
|
-
if (typeof current === "string" && current.length > 0) {
|
|
103
|
-
messages.push(current);
|
|
104
|
-
}
|
|
105
|
-
return messages;
|
|
106
|
-
}
|
|
107
|
-
function isSqliteBusyError(error) {
|
|
108
|
-
return collectErrorMessages(error).some((message) => /SQLITE_BUSY|database is locked/i.test(message));
|
|
109
|
-
}
|
|
110
|
-
function sleep(ms) {
|
|
111
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
112
|
-
}
|
|
113
|
-
export class SqliteCheckpointSaver extends MemorySaver {
|
|
114
|
-
filePath;
|
|
115
|
-
client = null;
|
|
116
|
-
savers = new Map();
|
|
117
|
-
initialized = false;
|
|
118
|
-
initialization = null;
|
|
119
|
-
operationChain = Promise.resolve();
|
|
120
|
-
legacyDefaultRow;
|
|
121
|
-
constructor(filePath) {
|
|
122
|
-
super();
|
|
123
|
-
this.filePath = filePath;
|
|
124
|
-
mkdirSync(path.dirname(filePath), { recursive: true });
|
|
125
|
-
}
|
|
126
|
-
async runSerialized(operation) {
|
|
127
|
-
const pending = this.operationChain.then(operation, operation);
|
|
128
|
-
this.operationChain = pending.then(() => undefined, () => undefined);
|
|
129
|
-
return pending;
|
|
130
|
-
}
|
|
131
|
-
async getClient() {
|
|
132
|
-
if (!this.client) {
|
|
133
|
-
this.client = createClient({ url: `file:${this.filePath}` });
|
|
134
|
-
}
|
|
135
|
-
return this.client;
|
|
136
|
-
}
|
|
137
|
-
async executeWithBusyRetry(operation) {
|
|
138
|
-
for (let attempt = 0;; attempt += 1) {
|
|
139
|
-
try {
|
|
140
|
-
return await operation();
|
|
141
|
-
}
|
|
142
|
-
catch (error) {
|
|
143
|
-
if (!isSqliteBusyError(error) || attempt >= SQLITE_BUSY_RETRY_ATTEMPTS) {
|
|
144
|
-
throw error;
|
|
145
|
-
}
|
|
146
|
-
await sleep(Math.min(SQLITE_BUSY_RETRY_DELAY_MS * (attempt + 1), 1_000));
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
async ensureInitialized() {
|
|
151
|
-
if (this.initialized) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
if (this.initialization) {
|
|
155
|
-
await this.initialization;
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
this.initialization = (async () => {
|
|
159
|
-
const client = await this.getClient();
|
|
160
|
-
await this.executeWithBusyRetry(() => client.execute("PRAGMA journal_mode=WAL"));
|
|
161
|
-
await this.executeWithBusyRetry(() => client.execute("PRAGMA foreign_keys=ON"));
|
|
162
|
-
await this.executeWithBusyRetry(() => client.execute("PRAGMA busy_timeout=5000"));
|
|
163
|
-
await this.executeWithBusyRetry(() => client.execute(`
|
|
164
|
-
CREATE TABLE IF NOT EXISTS ${CHECKPOINT_TABLE} (
|
|
1
|
+
import{mkdirSync as E}from"node:fs";import _ from"node:path";import{MemorySaver as f}from"@langchain/langgraph";import{createClient as m}from"@libsql/client";import{UPSTREAM_SESSION_CONFIG_KEY as d}from"../adapter/upstream-configurable-keys.js";const h="checkpoint_state",o="default",T=30,R=100;function c(r){if(!r?.configurable||d in r.configurable)return r;const t=r.configurable.session_id;return typeof t!="string"||t.length===0?r:{...r,configurable:{...r.configurable,[d]:t}}}function y(r){return r instanceof Uint8Array?{__type:"Uint8Array",data:Array.from(r)}:Array.isArray(r)?r.map(t=>y(t)):typeof r=="object"&&r?Object.fromEntries(Object.entries(r).map(([t,e])=>[t,y(e)])):r}function u(r){if(Array.isArray(r))return r.map(t=>u(t));if(typeof r=="object"&&r){const t=r;return t.__type==="Uint8Array"&&Array.isArray(t.data)?new Uint8Array(t.data.map(e=>Number(e))):Object.fromEntries(Object.entries(r).map(([e,i])=>[e,u(i)]))}return r}function A(r){try{const t=JSON.parse(r);return Array.isArray(t)&&typeof t[0]=="string"?t[0]:void 0}catch{return}}function p(r,t){if(t===o)return structuredClone(r);const e=r[t];return e===void 0?{}:{[t]:structuredClone(e)}}function S(r,t){return t===o?structuredClone(r):Object.fromEntries(Object.entries(r).filter(([e])=>A(e)===t).map(([e,i])=>[e,structuredClone(i)]))}function O(r,t){const e=new f,i=e;return i.storage=r,i.writes=t,e}function C(r){const t=r;return{storage:structuredClone(t.storage??{}),writes:structuredClone(t.writes??{})}}function L(r){const t=[];let e=r;for(;e instanceof Error;)t.push(e.message),e=e.cause;return typeof e=="string"&&e.length>0&&t.push(e),t}function x(r){return L(r).some(t=>/SQLITE_BUSY|database is locked/i.test(t))}function z(r){return new Promise(t=>setTimeout(t,r))}class b extends f{filePath;client=null;savers=new Map;initialized=!1;initialization=null;operationChain=Promise.resolve();legacyDefaultRow;constructor(t){super(),this.filePath=t,E(_.dirname(t),{recursive:!0})}async runSerialized(t){const e=this.operationChain.then(t,t);return this.operationChain=e.then(()=>{},()=>{}),e}async getClient(){return this.client||(this.client=m({url:`file:${this.filePath}`})),this.client}async executeWithBusyRetry(t){for(let e=0;;e+=1)try{return await t()}catch(i){if(!x(i)||e>=T)throw i;await z(Math.min(R*(e+1),1e3))}}async ensureInitialized(){if(!this.initialized){if(this.initialization){await this.initialization;return}this.initialization=(async()=>{const t=await this.getClient();await this.executeWithBusyRetry(()=>t.execute("PRAGMA journal_mode=WAL")),await this.executeWithBusyRetry(()=>t.execute("PRAGMA foreign_keys=ON")),await this.executeWithBusyRetry(()=>t.execute("PRAGMA busy_timeout=5000")),await this.executeWithBusyRetry(()=>t.execute(`
|
|
2
|
+
CREATE TABLE IF NOT EXISTS ${h} (
|
|
165
3
|
state_key TEXT PRIMARY KEY,
|
|
166
4
|
state_json TEXT NOT NULL,
|
|
167
5
|
updated_at TEXT NOT NULL
|
|
168
6
|
)
|
|
169
|
-
`));
|
|
170
|
-
this.initialized = true;
|
|
171
|
-
this.initialization = null;
|
|
172
|
-
})();
|
|
173
|
-
await this.initialization;
|
|
174
|
-
}
|
|
175
|
-
getStateKey(config) {
|
|
176
|
-
const normalized = normalizeCheckpointConfig(config);
|
|
177
|
-
const sessionId = normalized?.configurable?.[UPSTREAM_SESSION_CONFIG_KEY];
|
|
178
|
-
return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : DEFAULT_STATE_KEY;
|
|
179
|
-
}
|
|
180
|
-
async loadStateRow(stateKey) {
|
|
181
|
-
await this.ensureInitialized();
|
|
182
|
-
const client = await this.getClient();
|
|
183
|
-
const result = await this.executeWithBusyRetry(() => client.execute({
|
|
184
|
-
sql: `SELECT state_json, updated_at FROM ${CHECKPOINT_TABLE} WHERE state_key = ?`,
|
|
185
|
-
args: [stateKey],
|
|
186
|
-
}));
|
|
187
|
-
return result.rows[0] ?? null;
|
|
188
|
-
}
|
|
189
|
-
async loadLegacyDefaultRow() {
|
|
190
|
-
if (this.legacyDefaultRow !== undefined) {
|
|
191
|
-
return this.legacyDefaultRow;
|
|
192
|
-
}
|
|
193
|
-
this.legacyDefaultRow = await this.loadStateRow(DEFAULT_STATE_KEY);
|
|
194
|
-
return this.legacyDefaultRow;
|
|
195
|
-
}
|
|
196
|
-
async loadSaver(stateKey) {
|
|
197
|
-
if (this.savers.has(stateKey)) {
|
|
198
|
-
return this.savers.get(stateKey);
|
|
199
|
-
}
|
|
200
|
-
let storage = {};
|
|
201
|
-
let writes = {};
|
|
202
|
-
const row = await this.loadStateRow(stateKey);
|
|
203
|
-
if (row) {
|
|
204
|
-
const parsed = JSON.parse(row.state_json);
|
|
205
|
-
storage = extractScopedStorage(decodeBinary(parsed.storage ?? {}), stateKey);
|
|
206
|
-
writes = extractScopedWrites(decodeBinary(parsed.writes ?? {}), stateKey);
|
|
207
|
-
}
|
|
208
|
-
else if (stateKey !== DEFAULT_STATE_KEY) {
|
|
209
|
-
const legacyRow = await this.loadLegacyDefaultRow();
|
|
210
|
-
if (legacyRow) {
|
|
211
|
-
const parsed = JSON.parse(legacyRow.state_json);
|
|
212
|
-
storage = extractScopedStorage(decodeBinary(parsed.storage ?? {}), stateKey);
|
|
213
|
-
writes = extractScopedWrites(decodeBinary(parsed.writes ?? {}), stateKey);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
const saver = createIsolatedMemorySaver(storage, writes);
|
|
217
|
-
this.savers.set(stateKey, saver);
|
|
218
|
-
return saver;
|
|
219
|
-
}
|
|
220
|
-
async ensureLoadedState(config) {
|
|
221
|
-
const stateKey = this.getStateKey(config);
|
|
222
|
-
return { stateKey, saver: await this.loadSaver(stateKey) };
|
|
223
|
-
}
|
|
224
|
-
async persistState(stateKey, saver) {
|
|
225
|
-
await this.ensureInitialized();
|
|
226
|
-
const client = await this.getClient();
|
|
227
|
-
const snapshot = serializeSaverState(saver);
|
|
228
|
-
const scopedStorage = snapshot.storage;
|
|
229
|
-
const scopedWrites = snapshot.writes;
|
|
230
|
-
if (stateKey !== DEFAULT_STATE_KEY &&
|
|
231
|
-
Object.keys(scopedStorage).length === 0 &&
|
|
232
|
-
Object.keys(scopedWrites).length === 0) {
|
|
233
|
-
await this.executeWithBusyRetry(() => client.execute({
|
|
234
|
-
sql: `DELETE FROM ${CHECKPOINT_TABLE} WHERE state_key = ?`,
|
|
235
|
-
args: [stateKey],
|
|
236
|
-
}));
|
|
237
|
-
this.savers.delete(stateKey);
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
const now = new Date().toISOString();
|
|
241
|
-
const payload = JSON.stringify({
|
|
242
|
-
storage: scopedStorage,
|
|
243
|
-
writes: scopedWrites,
|
|
244
|
-
}, (_key, value) => encodeBinary(value));
|
|
245
|
-
await this.executeWithBusyRetry(() => client.execute({
|
|
246
|
-
sql: `INSERT INTO ${CHECKPOINT_TABLE} (state_key, state_json, updated_at)
|
|
7
|
+
`)),this.initialized=!0,this.initialization=null})(),await this.initialization}}getStateKey(t){const i=c(t)?.configurable?.[d];return typeof i=="string"&&i.length>0?i:o}async loadStateRow(t){await this.ensureInitialized();const e=await this.getClient();return(await this.executeWithBusyRetry(()=>e.execute({sql:`SELECT state_json, updated_at FROM ${h} WHERE state_key = ?`,args:[t]}))).rows[0]??null}async loadLegacyDefaultRow(){return this.legacyDefaultRow!==void 0?this.legacyDefaultRow:(this.legacyDefaultRow=await this.loadStateRow(o),this.legacyDefaultRow)}async loadSaver(t){if(this.savers.has(t))return this.savers.get(t);let e={},i={};const a=await this.loadStateRow(t);if(a){const s=JSON.parse(a.state_json);e=p(u(s.storage??{}),t),i=S(u(s.writes??{}),t)}else if(t!==o){const s=await this.loadLegacyDefaultRow();if(s){const l=JSON.parse(s.state_json);e=p(u(l.storage??{}),t),i=S(u(l.writes??{}),t)}}const n=O(e,i);return this.savers.set(t,n),n}async ensureLoadedState(t){const e=this.getStateKey(t);return{stateKey:e,saver:await this.loadSaver(e)}}async persistState(t,e){await this.ensureInitialized();const i=await this.getClient(),a=C(e),n=a.storage,s=a.writes;if(t!==o&&Object.keys(n).length===0&&Object.keys(s).length===0){await this.executeWithBusyRetry(()=>i.execute({sql:`DELETE FROM ${h} WHERE state_key = ?`,args:[t]})),this.savers.delete(t);return}const l=new Date().toISOString(),w=JSON.stringify({storage:n,writes:s},(j,g)=>y(g));await this.executeWithBusyRetry(()=>i.execute({sql:`INSERT INTO ${h} (state_key, state_json, updated_at)
|
|
247
8
|
VALUES (?, ?, ?)
|
|
248
9
|
ON CONFLICT(state_key) DO UPDATE SET
|
|
249
10
|
state_json = excluded.state_json,
|
|
250
|
-
updated_at = excluded.updated_at`,
|
|
251
|
-
args: [stateKey, payload, now],
|
|
252
|
-
}));
|
|
253
|
-
}
|
|
254
|
-
async getTuple(config) {
|
|
255
|
-
return this.runSerialized(async () => {
|
|
256
|
-
const { saver } = await this.ensureLoadedState(config);
|
|
257
|
-
return saver.getTuple(normalizeCheckpointConfig(config));
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
async *list(config, options) {
|
|
261
|
-
const { saver } = await this.ensureLoadedState(config);
|
|
262
|
-
for await (const item of saver.list(normalizeCheckpointConfig(config), options)) {
|
|
263
|
-
yield item;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
async put(config, checkpoint, metadata) {
|
|
267
|
-
return this.runSerialized(async () => {
|
|
268
|
-
const { stateKey, saver } = await this.ensureLoadedState(config);
|
|
269
|
-
const result = await saver.put(normalizeCheckpointConfig(config), checkpoint, metadata);
|
|
270
|
-
await this.persistState(stateKey, saver);
|
|
271
|
-
return result;
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
async putWrites(config, writes, taskId) {
|
|
275
|
-
return this.runSerialized(async () => {
|
|
276
|
-
const { stateKey, saver } = await this.ensureLoadedState(config);
|
|
277
|
-
const result = await saver.putWrites(normalizeCheckpointConfig(config), writes, taskId);
|
|
278
|
-
await this.persistState(stateKey, saver);
|
|
279
|
-
return result;
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
async deleteSession(sessionId) {
|
|
283
|
-
return this.runSerialized(async () => {
|
|
284
|
-
const { saver } = await this.ensureLoadedState({
|
|
285
|
-
configurable: {
|
|
286
|
-
[UPSTREAM_SESSION_CONFIG_KEY]: sessionId,
|
|
287
|
-
},
|
|
288
|
-
});
|
|
289
|
-
const typed = saver;
|
|
290
|
-
typed.storage = {};
|
|
291
|
-
typed.writes = {};
|
|
292
|
-
await this.persistState(sessionId, saver);
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
export { SqliteCheckpointSaver as SqliteSaver };
|
|
11
|
+
updated_at = excluded.updated_at`,args:[t,w,l]}))}async getTuple(t){return this.runSerialized(async()=>{const{saver:e}=await this.ensureLoadedState(t);return e.getTuple(c(t))})}async*list(t,e){const{saver:i}=await this.ensureLoadedState(t);for await(const a of i.list(c(t),e))yield a}async put(t,e,i){return this.runSerialized(async()=>{const{stateKey:a,saver:n}=await this.ensureLoadedState(t),s=await n.put(c(t),e,i);return await this.persistState(a,n),s})}async putWrites(t,e,i){return this.runSerialized(async()=>{const{stateKey:a,saver:n}=await this.ensureLoadedState(t),s=await n.putWrites(c(t),e,i);return await this.persistState(a,n),s})}async deleteSession(t){return this.runSerialized(async()=>{const{saver:e}=await this.ensureLoadedState({configurable:{[d]:t}}),i=e;i.storage={},i.writes={},await this.persistState(t,e)})}}export{b as SqliteCheckpointSaver,b as SqliteSaver};
|