File size: 8,040 Bytes
f500658 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import type {
CommitResult,
Context,
Entry,
EntryScan,
EntryStructure,
ForkOptions,
ListElement,
ListReadOptions,
SessionStats,
Storage,
StorageBranchScan,
StoredValue,
UsageRow,
UsageScan,
Value,
ValueList,
Write,
} from "@earendil-works/pi-agent-core";
import { branchTip, prepareStorageCommit } from "@earendil-works/pi-agent-core";
import { appendEntryToBranchIndex, scanBranchEntries, scanBranchEntryStructures } from "./session/branch-entries.ts";
import { decodeEntryRow, EntryRowWriter, readAllEntryRows, readEntryRows, scanEntryRows } from "./session/entries.ts";
import { advanceNextSeq, readNextSeq } from "./session/session-sequences.ts";
import { addUsageToSessionStats, incrementMessageCount, readSessionStats } from "./session/session-stats.ts";
import { decodeUsageLedgerRow, scanUsageLedgerRows, UsageLedgerRowWriter } from "./session/usage-ledger.ts";
import {
appendListValueRow,
deleteListValueRows,
deleteScalarValueRow,
readAllScalarValueRows,
readListValueRows,
readScalarValueRow,
scanScalarValueRows,
setScalarValueRow,
} from "./session/values.ts";
import type { SqliteDatabase } from "./types.ts";
export interface SqliteStorageOptions {
sessionId: string;
now?: () => number;
}
export interface SqliteStorageSnapshot {
entries: Entry[];
scalarValues: StoredValue<unknown>[];
entriesComplete: boolean;
}
export class SqliteStorage implements Storage {
private readonly db: SqliteDatabase;
private readonly sessionId: string;
private readonly now: () => number;
private readonly entryWriter: EntryRowWriter;
private readonly usageWriter: UsageLedgerRowWriter;
private commitQueue: Promise<void> = Promise.resolve();
private state: "open" | "closing" | "closed" = "open";
private closePromise: Promise<void> | undefined;
constructor(db: SqliteDatabase, options: SqliteStorageOptions) {
this.db = db;
this.sessionId = options.sessionId;
this.now = options.now ?? Date.now;
this.entryWriter = new EntryRowWriter(db, this.sessionId);
this.usageWriter = new UsageLedgerRowWriter(db, this.sessionId);
}
async commit(writes: Write[], _context: Context): Promise<CommitResult> {
if (this.state !== "open") throw new Error("SqliteStorage is closed");
const result = this.commitQueue.then(() => this.applyCommit(writes));
this.commitQueue = result.then(
() => undefined,
() => undefined,
);
return result;
}
getEntries(ids: string[], _context: Context): Promise<Map<string, Entry>> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
const rowsById = new Map(readEntryRows(this.db, this.sessionId, ids).map((row) => [row.id, row]));
const entries = new Map<string, Entry>();
for (const id of ids) {
const row = rowsById.get(id);
if (row !== undefined) entries.set(id, decodeEntryRow(row));
}
return Promise.resolve(entries);
}
getValue<T>(address: Value<T>, _context: Context): Promise<StoredValue<T> | undefined> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
return Promise.resolve(readScalarValueRow(this.db, this.sessionId, address));
}
scanValues<T>(prefix: Value<T>, _context: Context): Promise<StoredValue<T>[]> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
return Promise.resolve(scanScalarValueRows(this.db, this.sessionId, prefix));
}
async readList<T>(
address: ValueList<T>,
options: ListReadOptions | undefined,
_context: Context,
): Promise<ListElement<T>[]> {
if (this.state !== "open") throw new Error("SqliteStorage is closed");
return readListValueRows(this.db, this.sessionId, address, options);
}
scanBranch(query: StorageBranchScan, _context: Context): Promise<Entry[]> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
return Promise.resolve().then(() => scanBranchEntries(this.db, this.sessionId, query));
}
scanBranchStructure(query: StorageBranchScan, _context: Context): Promise<EntryStructure[]> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
return Promise.resolve().then(() => scanBranchEntryStructures(this.db, this.sessionId, query));
}
scanEntries(query: EntryScan, _context: Context): Promise<Entry[]> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
return Promise.resolve(scanEntryRows(this.db, this.sessionId, query).map(decodeEntryRow));
}
scanUsage(query: UsageScan, _context: Context): Promise<UsageRow[]> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
return Promise.resolve(scanUsageLedgerRows(this.db, this.sessionId, query).map(decodeUsageLedgerRow));
}
getStats(_context: Context): Promise<SessionStats> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
return Promise.resolve(readSessionStats(this.db, this.sessionId));
}
snapshot(options: ForkOptions, _context: Context): Promise<SqliteStorageSnapshot> {
if (this.state !== "open") return Promise.reject(new Error("SqliteStorage is closed"));
const result = this.commitQueue.then(() => this.readSnapshot(options));
this.commitQueue = result.then(
() => undefined,
() => undefined,
);
return result;
}
private readSnapshot(options: ForkOptions): SqliteStorageSnapshot {
const scalarValues = readAllScalarValueRows(this.db, this.sessionId);
return {
entries: this.readSnapshotEntries(options, scalarValues),
scalarValues,
entriesComplete: options.scope === "tree",
};
}
private readSnapshotEntries(options: ForkOptions, scalarValues: readonly StoredValue<unknown>[]): Entry[] {
if (options.scope === "tree") return readAllEntryRows(this.db, this.sessionId).map(decodeEntryRow);
const sourceAddress = branchTip(options.branch);
const sourceTip = scalarValues.find(
(stored) => stored.address.namespace === sourceAddress.namespace && stored.address.key === sourceAddress.key,
) as StoredValue<string | null> | undefined;
if (sourceTip === undefined) throw new Error(`Unknown source branch: ${options.branch}`);
return sourceTip.value === null
? []
: scanBranchEntries(this.db, this.sessionId, { start: sourceTip.value, order: "oldestFirst" });
}
private applyCommit(writes: Write[]): CommitResult {
return this.db.transaction(() => {
const firstSeq = readNextSeq(this.db, this.sessionId);
const prepared = prepareStorageCommit(writes, firstSeq, this.now());
for (const write of prepared.writes) {
switch (write.kind) {
case "entry": {
const { kind: _kind, ...entry } = write;
this.entryWriter.insert(entry);
appendEntryToBranchIndex(this.db, this.sessionId, entry);
if (entry.type === "message") incrementMessageCount(this.db, this.sessionId);
break;
}
case "usage": {
const { kind: _kind, ...row } = write;
this.usageWriter.insert(row);
addUsageToSessionStats(this.db, this.sessionId, row.usage);
break;
}
case "value":
if (write.op === "delete") {
deleteScalarValueRow(this.db, this.sessionId, write.namespace, write.key);
} else {
setScalarValueRow(this.db, this.sessionId, write.namespace, write.key, write.seq, write.value);
}
break;
case "list":
if (write.op === "delete") {
deleteListValueRows(this.db, this.sessionId, write.namespace, write.key);
} else {
appendListValueRow(this.db, this.sessionId, write.namespace, write.key, write.seq, write.value);
}
break;
}
}
advanceNextSeq(this.db, this.sessionId, firstSeq + prepared.writes.length);
return { ...prepared.result, stats: readSessionStats(this.db, this.sessionId) };
});
}
close(_context: Context): Promise<void> {
if (this.closePromise !== undefined) return this.closePromise;
this.state = "closing";
this.closePromise = this.commitQueue.then(() => {
this.state = "closed";
});
return this.closePromise;
}
}
|