@swirls/looms 0.8.0 → 0.10.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/CHANGELOG.md +12 -0
- package/dist/actor/routing.js +2 -1
- package/dist/agent/debugger/agent-chat.js +6 -5
- package/dist/agent/debugger/index.js +2 -1
- package/dist/agent/module.d.ts +799 -593
- package/dist/agent/module.js +31 -0
- package/dist/agent/scope.js +1 -0
- package/dist/agent/signals.d.ts +108 -15
- package/dist/agent/signals.js +70 -47
- package/dist/approval/debugger/approvals-view.js +1 -1
- package/dist/approval/index.d.ts +1 -0
- package/dist/approval/index.js +1 -0
- package/dist/approval/module.d.ts +42 -1
- package/dist/approval/module.js +53 -0
- package/dist/approval/scope.js +1 -0
- package/dist/approval/signals.d.ts +19 -13
- package/dist/approval/signals.js +25 -45
- package/dist/approval/steps.d.ts +11 -0
- package/dist/approval/steps.js +39 -0
- package/dist/cli/commands.js +87 -16
- package/dist/client/client.d.ts +211 -0
- package/dist/client/client.js +18 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/module-scope.d.ts +50 -5
- package/dist/core/module-scope.js +22 -2
- package/dist/core/module.d.ts +29 -2
- package/dist/core/projection.d.ts +1 -0
- package/dist/core/published.d.ts +59 -1
- package/dist/core/published.js +97 -1
- package/dist/core/signals.d.ts +56 -0
- package/dist/core/signals.js +93 -0
- package/dist/core/validate-event.d.ts +12 -1
- package/dist/core/validate-event.js +34 -11
- package/dist/debugger/app/assets/index-CO5gIvV8.css +2 -0
- package/dist/debugger/app/assets/{index-CMRl_aWQ.js → index-D-2Q7ecG.js} +2 -2
- package/dist/debugger/app/assets/routes-lvmllT60.js +47 -0
- package/dist/debugger/app/index.html +2 -2
- package/dist/debugger/index.d.ts +1 -1
- package/dist/debugger/index.js +1 -1
- package/dist/debugger/lib/cn.d.ts +1 -1
- package/dist/debugger/lib/cn.js +12 -1
- package/dist/debugger/plugin.d.ts +11 -1
- package/dist/debugger/plugin.js +41 -0
- package/dist/debugger/shell/debugger-shell.js +1 -1
- package/dist/debugger/shell/form-fields.d.ts +17 -0
- package/dist/debugger/shell/form-fields.js +38 -0
- package/dist/debugger/shell/run-action.d.ts +6 -0
- package/dist/debugger/shell/run-action.js +15 -0
- package/dist/debugger/shell/run-controls.js +3 -55
- package/dist/debugger/shell/schema-form.d.ts +10 -1
- package/dist/debugger/shell/schema-form.js +108 -10
- package/dist/debugger/shell/signal-form.d.ts +18 -0
- package/dist/debugger/shell/signal-form.js +185 -0
- package/dist/debugger/shell/start-form.js +2 -32
- package/dist/debugger/shell/use-debounced-preview.d.ts +22 -0
- package/dist/debugger/shell/use-debounced-preview.js +68 -0
- package/dist/evaluator/index.d.ts +1 -1
- package/dist/evaluator/index.js +1 -1
- package/dist/evaluator/module.d.ts +8 -1
- package/dist/evaluator/module.js +33 -0
- package/dist/evaluator/scope.js +1 -0
- package/dist/projectors/event-log-ops.d.ts +68 -0
- package/dist/projectors/event-log-ops.js +79 -0
- package/dist/projectors/event-log.d.ts +4 -68
- package/dist/projectors/event-log.js +4 -79
- package/dist/projectors/replica/migrations.d.ts +4 -6
- package/dist/projectors/replica/migrations.js +18 -1
- package/dist/projectors/replica/sqlite-bun.d.ts +5 -0
- package/dist/projectors/replica/sqlite-bun.js +39 -0
- package/dist/projectors/replica/sqlite-migrations.d.ts +5 -0
- package/dist/projectors/replica/sqlite-migrations.js +99 -0
- package/dist/projectors/replica/sqlite.d.ts +95 -0
- package/dist/projectors/replica/sqlite.js +517 -0
- package/dist/runtime/server/routes/definitions.d.ts +1 -1
- package/dist/runtime/server/routes/definitions.js +58 -2
- package/dist/runtime/server/routes/runs.js +25 -1
- package/dist/schedule/module.d.ts +117 -1
- package/dist/schedule/module.js +30 -0
- package/dist/schedule/scope.js +1 -0
- package/dist/schedule/signals.d.ts +59 -7
- package/dist/schedule/signals.js +35 -17
- package/dist/testing/corpus-module.d.ts +1 -1
- package/dist/testing/index.js +2 -1
- package/dist/testing/module-examples.d.ts +6 -0
- package/dist/testing/module-examples.js +66 -0
- package/dist/workflow/module.d.ts +3 -1
- package/dist/workflow/module.js +13 -0
- package/dist/workflow/scope.js +1 -0
- package/package.json +17 -1
- package/dist/debugger/app/assets/index-B7GdT1UA.css +0 -2
- package/dist/debugger/app/assets/routes-CfXxKG45.js +0 -47
- /package/dist/evaluator/{signals.d.ts → steps.d.ts} +0 -0
- /package/dist/evaluator/{signals.js → steps.js} +0 -0
|
@@ -1,56 +1,11 @@
|
|
|
1
|
-
import { DateTime, Effect
|
|
1
|
+
import { DateTime, Effect } from 'effect';
|
|
2
2
|
import createPostgresClient, {} from 'postgres';
|
|
3
|
-
import {
|
|
3
|
+
import {} from '@swirls/looms/core';
|
|
4
|
+
import { eventLogOps } from './event-log-ops.js';
|
|
4
5
|
import { runProjectorPromise } from './projector.js';
|
|
5
6
|
import { POSTGRES_EVENT_LOG_SCHEMA_SQL } from './replica/migrations.js';
|
|
6
|
-
export { POSTGRES_EVENT_LOG_SCHEMA_SQL };
|
|
7
|
-
const decodeAttributes = Schema.decodeUnknownOption(Schema.Record(Schema.String, Schema.String));
|
|
7
|
+
export { POSTGRES_EVENT_LOG_SCHEMA_SQL, eventLogOps };
|
|
8
8
|
const terminal = new Set(['completed', 'failed', 'cancelled']);
|
|
9
|
-
export function eventLogOps(event) {
|
|
10
|
-
if (event.ephemeral) {
|
|
11
|
-
return [];
|
|
12
|
-
}
|
|
13
|
-
const entry = {
|
|
14
|
-
runId: event.runId,
|
|
15
|
-
seq: event.seq,
|
|
16
|
-
eventId: event.id,
|
|
17
|
-
ts: event.ts,
|
|
18
|
-
type: event.type,
|
|
19
|
-
payload: event.payload,
|
|
20
|
-
threadId: event.threadId,
|
|
21
|
-
parentThreadId: event.parentThreadId ?? null,
|
|
22
|
-
causationId: event.causationId ?? null,
|
|
23
|
-
correlationId: event.correlationId ?? null,
|
|
24
|
-
effectId: event.effectId ?? null,
|
|
25
|
-
origin: event.origin,
|
|
26
|
-
idempotencyKey: event.idempotencyKey ?? null,
|
|
27
|
-
};
|
|
28
|
-
const payload = isJsonObject(event.payload) ? event.payload : {};
|
|
29
|
-
switch (event.type) {
|
|
30
|
-
case 'runtime.run.started':
|
|
31
|
-
return [{ type: 'insert', entry }, startOp(event, payload)];
|
|
32
|
-
case 'runtime.run.completed':
|
|
33
|
-
return [
|
|
34
|
-
{ type: 'insert', entry },
|
|
35
|
-
{
|
|
36
|
-
type: 'finish',
|
|
37
|
-
runId: event.runId,
|
|
38
|
-
status: completedRunStatus(event.payload),
|
|
39
|
-
at: event.ts,
|
|
40
|
-
},
|
|
41
|
-
];
|
|
42
|
-
case 'runtime.run.cancelled':
|
|
43
|
-
return [
|
|
44
|
-
{ type: 'insert', entry },
|
|
45
|
-
{ type: 'finish', runId: event.runId, status: 'cancelled', at: event.ts },
|
|
46
|
-
];
|
|
47
|
-
default:
|
|
48
|
-
return [
|
|
49
|
-
{ type: 'insert', entry },
|
|
50
|
-
{ type: 'touch', runId: event.runId, at: event.ts },
|
|
51
|
-
];
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
9
|
export function memoryEventLog() {
|
|
55
10
|
const events = new Map();
|
|
56
11
|
const runs = new Map();
|
|
@@ -184,36 +139,6 @@ function mergeFinish(existing, op) {
|
|
|
184
139
|
function utcDate(ms) {
|
|
185
140
|
return DateTime.toDateUtc(DateTime.makeUnsafe(ms));
|
|
186
141
|
}
|
|
187
|
-
function startOp(event, payload) {
|
|
188
|
-
const launchedBy = readRunLaunchSource(payload.launchedBy === undefined ? undefined : payload.launchedBy);
|
|
189
|
-
const attributes = readAttributes(payload.attributes);
|
|
190
|
-
return {
|
|
191
|
-
type: 'start',
|
|
192
|
-
runId: event.runId,
|
|
193
|
-
kind: readString(payload, 'kind'),
|
|
194
|
-
definitionName: readString(payload, 'definitionName'),
|
|
195
|
-
definitionVersion: readString(payload, 'definitionVersion'),
|
|
196
|
-
rootThreadId: readString(payload, 'rootThreadId'),
|
|
197
|
-
parentRunId: launchedBy?.runId ?? null,
|
|
198
|
-
launchedByEffectId: launchedBy?.effectId ?? null,
|
|
199
|
-
correlationId: event.correlationId ?? null,
|
|
200
|
-
attributes,
|
|
201
|
-
tenant: attributes.tenant ?? null,
|
|
202
|
-
project: attributes.project ?? null,
|
|
203
|
-
org: attributes.org ?? null,
|
|
204
|
-
at: event.ts,
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
function readString(payload, key) {
|
|
208
|
-
const value = payload[key];
|
|
209
|
-
return Predicate.isString(value) ? value : null;
|
|
210
|
-
}
|
|
211
|
-
function readAttributes(value) {
|
|
212
|
-
if (value === undefined) {
|
|
213
|
-
return {};
|
|
214
|
-
}
|
|
215
|
-
return Option.getOrElse(decodeAttributes(value), () => ({}));
|
|
216
|
-
}
|
|
217
142
|
function postgresStore(sql, ownsClient) {
|
|
218
143
|
return {
|
|
219
144
|
init: () => runProjectorPromise('postgres-event-log', () => sql.unsafe(POSTGRES_EVENT_LOG_SCHEMA_SQL)).then(() => undefined),
|
|
@@ -15,21 +15,19 @@ export interface ReplicaMigration {
|
|
|
15
15
|
}
|
|
16
16
|
/** Versioned DDL for the Postgres replica. Looms only creates and alters `looms_*` objects. */
|
|
17
17
|
export declare const POSTGRES_REPLICA_MIGRATIONS: readonly ReplicaMigration[];
|
|
18
|
+
export type ReplicaDialect = 'postgres' | 'sqlite';
|
|
18
19
|
export interface GenerateReplicaMigrationsOptions {
|
|
19
20
|
/** Directory of SQL migrations the host already applies. */
|
|
20
21
|
readonly out: string;
|
|
21
|
-
/** Append a Drizzle `_journal.json` entry for each file written. */
|
|
22
|
+
/** Append a Drizzle `_journal.json` entry for each file written. Postgres only. */
|
|
22
23
|
readonly journal?: 'drizzle';
|
|
23
24
|
/** Report pending migrations and write nothing. */
|
|
24
25
|
readonly check?: boolean;
|
|
26
|
+
/** Which replica DDL to write. Defaults to Postgres. */
|
|
27
|
+
readonly dialect?: ReplicaDialect;
|
|
25
28
|
}
|
|
26
29
|
export interface GenerateReplicaMigrationsResult {
|
|
27
30
|
readonly written: readonly string[];
|
|
28
31
|
readonly pending: readonly string[];
|
|
29
32
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Writes each migration the host does not already have.
|
|
32
|
-
* A file counts as applied when it contains `-- looms-migration: <id>`.
|
|
33
|
-
* The numeric prefix continues after the highest `NNNN_` file already in `out`.
|
|
34
|
-
*/
|
|
35
33
|
export declare function generateReplicaMigrations(options: GenerateReplicaMigrationsOptions): Promise<GenerateReplicaMigrationsResult>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readdir, readFile, writeFile, mkdir } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { Predicate } from 'effect';
|
|
4
|
+
import { SQLITE_REPLICA_MIGRATIONS } from './sqlite-migrations.js';
|
|
4
5
|
/**
|
|
5
6
|
* Actors and reviews. Same statements the Postgres index projector has always
|
|
6
7
|
* applied, so a database created by an older Looms keeps working.
|
|
@@ -200,9 +201,25 @@ async function appendDrizzleJournal(directory, tags) {
|
|
|
200
201
|
* A file counts as applied when it contains `-- looms-migration: <id>`.
|
|
201
202
|
* The numeric prefix continues after the highest `NNNN_` file already in `out`.
|
|
202
203
|
*/
|
|
204
|
+
function migrationsFor(dialect) {
|
|
205
|
+
switch (dialect) {
|
|
206
|
+
case 'postgres':
|
|
207
|
+
return POSTGRES_REPLICA_MIGRATIONS;
|
|
208
|
+
case 'sqlite':
|
|
209
|
+
return SQLITE_REPLICA_MIGRATIONS;
|
|
210
|
+
default: {
|
|
211
|
+
const unexpected = dialect;
|
|
212
|
+
return unexpected;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
203
216
|
export async function generateReplicaMigrations(options) {
|
|
217
|
+
const dialect = options.dialect ?? 'postgres';
|
|
218
|
+
if (dialect === 'sqlite' && options.journal === 'drizzle') {
|
|
219
|
+
throw new Error('Drizzle journal output is only supported for the Postgres replica.');
|
|
220
|
+
}
|
|
204
221
|
const recorded = await recordedMigrationIds(options.out);
|
|
205
|
-
const pending =
|
|
222
|
+
const pending = migrationsFor(dialect).filter((migration) => !recorded.has(migration.id));
|
|
206
223
|
if (options.check || pending.length === 0) {
|
|
207
224
|
return { written: [], pending: pending.map((migration) => migration.id) };
|
|
208
225
|
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Database } from 'bun:sqlite';
|
|
2
|
+
import { type SqliteReplica, type SqliteReplicaDatabase, type SqliteReplicaMigrate } from './sqlite';
|
|
3
|
+
/** Opens a SQLite file. The replica closes it on `dispose`. */
|
|
4
|
+
export declare function openSqliteReplicaFile(filename: string, migrate: SqliteReplicaMigrate): SqliteReplica;
|
|
5
|
+
export declare function bunSqliteReplicaDatabase(db: Database): SqliteReplicaDatabase;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Database } from 'bun:sqlite';
|
|
2
|
+
import { sqliteReplica, } from './sqlite.js';
|
|
3
|
+
/** Opens a SQLite file. The replica closes it on `dispose`. */
|
|
4
|
+
export function openSqliteReplicaFile(filename, migrate) {
|
|
5
|
+
const db = new Database(filename);
|
|
6
|
+
const replica = sqliteReplica({ database: bunSqliteReplicaDatabase(db), migrate });
|
|
7
|
+
return {
|
|
8
|
+
...replica,
|
|
9
|
+
dispose: () => {
|
|
10
|
+
db.close();
|
|
11
|
+
return Promise.resolve();
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function bunSqliteReplicaDatabase(db) {
|
|
16
|
+
return {
|
|
17
|
+
exec: (sql) => {
|
|
18
|
+
db.exec(sql);
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
},
|
|
21
|
+
get: (sql, params = []) => {
|
|
22
|
+
const row = db.query(sql).get(...params);
|
|
23
|
+
return Promise.resolve(row ?? null);
|
|
24
|
+
},
|
|
25
|
+
all: (sql, params = []) => Promise.resolve(db.query(sql).all(...params)),
|
|
26
|
+
batch: (statements) => {
|
|
27
|
+
if (statements.length === 0) {
|
|
28
|
+
return Promise.resolve();
|
|
29
|
+
}
|
|
30
|
+
const apply = db.transaction((batch) => {
|
|
31
|
+
for (const entry of batch) {
|
|
32
|
+
db.run(entry.sql, [...entry.params]);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
apply(statements);
|
|
36
|
+
return Promise.resolve();
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Same tables as the Postgres replica, in SQLite types.
|
|
3
|
+
* Timestamps are epoch milliseconds. JSON is text. Booleans are 0 or 1.
|
|
4
|
+
* A fresh database gets every column from `CREATE TABLE`. SQLite has no
|
|
5
|
+
* `ADD COLUMN IF NOT EXISTS` we want to depend on, so later migrations should
|
|
6
|
+
* add columns the way the index projector does: read `PRAGMA table_info` first.
|
|
7
|
+
*/
|
|
8
|
+
const SQLITE_REPLICA_SCHEMA_SQL = `
|
|
9
|
+
CREATE TABLE IF NOT EXISTS looms_run_actors (
|
|
10
|
+
run_id TEXT NOT NULL,
|
|
11
|
+
actor_id TEXT NOT NULL,
|
|
12
|
+
kind TEXT,
|
|
13
|
+
status TEXT,
|
|
14
|
+
definition_name TEXT,
|
|
15
|
+
parent_actor_id TEXT,
|
|
16
|
+
launched_by_run_id TEXT,
|
|
17
|
+
launched_by_effect_id TEXT,
|
|
18
|
+
correlation_id TEXT,
|
|
19
|
+
updated_at INTEGER NOT NULL,
|
|
20
|
+
PRIMARY KEY (run_id, actor_id)
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
CREATE INDEX IF NOT EXISTS looms_run_actors_launched_by ON looms_run_actors (launched_by_run_id);
|
|
24
|
+
CREATE INDEX IF NOT EXISTS looms_run_actors_correlation ON looms_run_actors (correlation_id);
|
|
25
|
+
|
|
26
|
+
CREATE TABLE IF NOT EXISTS looms_run_reviews (
|
|
27
|
+
run_id TEXT NOT NULL,
|
|
28
|
+
review_id TEXT NOT NULL,
|
|
29
|
+
status TEXT NOT NULL,
|
|
30
|
+
title TEXT NOT NULL,
|
|
31
|
+
updated_at INTEGER NOT NULL,
|
|
32
|
+
PRIMARY KEY (run_id, review_id)
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
CREATE TABLE IF NOT EXISTS looms_run_events (
|
|
36
|
+
run_id TEXT NOT NULL,
|
|
37
|
+
seq INTEGER NOT NULL,
|
|
38
|
+
event_id TEXT NOT NULL,
|
|
39
|
+
ts INTEGER NOT NULL,
|
|
40
|
+
type TEXT NOT NULL,
|
|
41
|
+
payload TEXT NOT NULL,
|
|
42
|
+
thread_id TEXT,
|
|
43
|
+
parent_thread_id TEXT,
|
|
44
|
+
causation_id TEXT,
|
|
45
|
+
correlation_id TEXT,
|
|
46
|
+
effect_id TEXT,
|
|
47
|
+
origin TEXT NOT NULL,
|
|
48
|
+
idempotency_key TEXT,
|
|
49
|
+
ephemeral INTEGER NOT NULL DEFAULT 0,
|
|
50
|
+
PRIMARY KEY (run_id, seq)
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
CREATE INDEX IF NOT EXISTS looms_run_events_type_ts ON looms_run_events (type, ts);
|
|
54
|
+
|
|
55
|
+
CREATE TABLE IF NOT EXISTS looms_run_index (
|
|
56
|
+
run_id TEXT PRIMARY KEY,
|
|
57
|
+
status TEXT NOT NULL,
|
|
58
|
+
kind TEXT,
|
|
59
|
+
definition_name TEXT,
|
|
60
|
+
definition_version TEXT,
|
|
61
|
+
root_thread_id TEXT,
|
|
62
|
+
parent_run_id TEXT,
|
|
63
|
+
launched_by_effect_id TEXT,
|
|
64
|
+
correlation_id TEXT,
|
|
65
|
+
attributes TEXT NOT NULL DEFAULT '{}',
|
|
66
|
+
tenant TEXT,
|
|
67
|
+
project TEXT,
|
|
68
|
+
org TEXT,
|
|
69
|
+
started_at INTEGER,
|
|
70
|
+
updated_at INTEGER NOT NULL,
|
|
71
|
+
completed_at INTEGER,
|
|
72
|
+
replicated_seq INTEGER NOT NULL DEFAULT 0,
|
|
73
|
+
purged_at INTEGER
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
CREATE INDEX IF NOT EXISTS looms_run_index_tenant ON looms_run_index (tenant);
|
|
77
|
+
CREATE INDEX IF NOT EXISTS looms_run_index_project ON looms_run_index (project);
|
|
78
|
+
CREATE INDEX IF NOT EXISTS looms_run_index_org ON looms_run_index (org);
|
|
79
|
+
CREATE INDEX IF NOT EXISTS looms_run_index_parent ON looms_run_index (parent_run_id);
|
|
80
|
+
CREATE INDEX IF NOT EXISTS looms_run_index_status ON looms_run_index (status);
|
|
81
|
+
|
|
82
|
+
CREATE TABLE IF NOT EXISTS looms_schema_migrations (
|
|
83
|
+
id TEXT PRIMARY KEY,
|
|
84
|
+
applied_at INTEGER NOT NULL
|
|
85
|
+
);
|
|
86
|
+
`.trim();
|
|
87
|
+
function sqliteReplicaMigration(id, sql) {
|
|
88
|
+
return {
|
|
89
|
+
id,
|
|
90
|
+
sql: [
|
|
91
|
+
sql,
|
|
92
|
+
`INSERT INTO looms_schema_migrations (id, applied_at) VALUES ('${id}', unixepoch() * 1000) ON CONFLICT (id) DO NOTHING;`,
|
|
93
|
+
].join('\n\n'),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** Versioned DDL for the SQLite replica, including D1. Looms only creates `looms_*` objects. */
|
|
97
|
+
export const SQLITE_REPLICA_MIGRATIONS = [
|
|
98
|
+
sqliteReplicaMigration('0001_replica', SQLITE_REPLICA_SCHEMA_SQL),
|
|
99
|
+
];
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { type EventStore, type FoldRegistry, type RunState } from '@swirls/looms/core';
|
|
2
|
+
import { ReplicaGapError, type Projector } from '../projector';
|
|
3
|
+
export interface ReplicaStatus {
|
|
4
|
+
/** Highest sequence copied with no hole in front of it. */
|
|
5
|
+
readonly replicatedSeq: number;
|
|
6
|
+
/** The run summary says the run has finished. */
|
|
7
|
+
readonly finished: boolean;
|
|
8
|
+
/** Finished, and every event through that finish has been copied. */
|
|
9
|
+
readonly complete: boolean;
|
|
10
|
+
/** Set once retention has deleted the event copy. */
|
|
11
|
+
readonly purgedAt: number | null;
|
|
12
|
+
}
|
|
13
|
+
export interface ReplicaPurgeOptions {
|
|
14
|
+
/** Delete event copies for runs that finished before this epoch millisecond. */
|
|
15
|
+
readonly finishedBefore: number;
|
|
16
|
+
/** Keep the run summary row. Defaults to true. */
|
|
17
|
+
readonly keepIndex?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface ReplicaPurgeResult {
|
|
20
|
+
readonly runs: number;
|
|
21
|
+
}
|
|
22
|
+
export type SqliteReplicaMigrate = 'auto' | 'verify';
|
|
23
|
+
export type SqlValue = string | number | null;
|
|
24
|
+
export interface SqlStatement {
|
|
25
|
+
readonly sql: string;
|
|
26
|
+
readonly params: readonly SqlValue[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Async SQL used by the replica.
|
|
30
|
+
* `bun:sqlite` and Cloudflare D1 both adapt to this. One writer per run is assumed:
|
|
31
|
+
* D1 cannot interleave a read and the following writes in one interactive transaction.
|
|
32
|
+
*/
|
|
33
|
+
export interface SqliteReplicaDatabase {
|
|
34
|
+
exec(sql: string): Promise<void>;
|
|
35
|
+
get<T>(sql: string, params?: readonly SqlValue[]): Promise<T | null>;
|
|
36
|
+
all<T>(sql: string, params?: readonly SqlValue[]): Promise<readonly T[]>;
|
|
37
|
+
batch(statements: readonly SqlStatement[]): Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Enough of a prepared statement for the replica.
|
|
41
|
+
* Cloudflare's `D1PreparedStatement` satisfies this.
|
|
42
|
+
*/
|
|
43
|
+
export interface D1BoundStatement {
|
|
44
|
+
all<T>(): Promise<{
|
|
45
|
+
readonly results?: readonly T[];
|
|
46
|
+
}>;
|
|
47
|
+
first<T>(): Promise<T | null>;
|
|
48
|
+
}
|
|
49
|
+
export type SqliteReplicaOptions = {
|
|
50
|
+
readonly database: SqliteReplicaDatabase;
|
|
51
|
+
/**
|
|
52
|
+
* `auto` applies pending migrations on init.
|
|
53
|
+
* `verify` refuses to start when a migration has not been applied, and names
|
|
54
|
+
* `looms replica generate --dialect sqlite` so the host can commit the SQL.
|
|
55
|
+
*/
|
|
56
|
+
readonly migrate: SqliteReplicaMigrate;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Adapts Cloudflare D1.
|
|
60
|
+
* The statement type is inferred from `prepare().bind()` so `D1Database` itself is accepted:
|
|
61
|
+
* D1's `batch` only takes the statements its own `prepare` returns.
|
|
62
|
+
*/
|
|
63
|
+
export declare function d1ReplicaDatabase<Statement extends D1BoundStatement>(d1: {
|
|
64
|
+
exec(query: string): Promise<{
|
|
65
|
+
readonly count: number;
|
|
66
|
+
readonly duration: number;
|
|
67
|
+
}>;
|
|
68
|
+
prepare(query: string): {
|
|
69
|
+
bind(...values: readonly SqlValue[]): Statement;
|
|
70
|
+
};
|
|
71
|
+
batch(statements: readonly Statement[]): Promise<readonly object[]>;
|
|
72
|
+
}): SqliteReplicaDatabase;
|
|
73
|
+
/**
|
|
74
|
+
* A complete, gap-checked copy of every run, plus the run summary folded in the same batch.
|
|
75
|
+
* Reading an event back returns the envelope the source returned.
|
|
76
|
+
* Targets a shared SQLite database or D1, not the per-run actor store.
|
|
77
|
+
*/
|
|
78
|
+
export interface SqliteReplica extends Projector {
|
|
79
|
+
readonly position: (runId: string) => Promise<number | null>;
|
|
80
|
+
/** Apply pending migrations. `init` does this only when `migrate` is `auto`. */
|
|
81
|
+
readonly migrate: () => Promise<void>;
|
|
82
|
+
readonly pendingMigrations: () => Promise<readonly string[]>;
|
|
83
|
+
/** Read-only view of the event copy. Folds and replay run against it unchanged. */
|
|
84
|
+
readonly eventStore: () => EventStore;
|
|
85
|
+
/** Fold the copied events with the same registry the runtime used. */
|
|
86
|
+
readonly loadRun: (runId: string, registry: FoldRegistry) => Promise<RunState>;
|
|
87
|
+
/** How far the copy has got, and whether it is safe to treat as complete. */
|
|
88
|
+
readonly status: (runId: string) => Promise<ReplicaStatus | null>;
|
|
89
|
+
/** Delete event copies for finished runs. Later deliveries for those runs are skipped. */
|
|
90
|
+
readonly purge: (options: ReplicaPurgeOptions) => Promise<ReplicaPurgeResult>;
|
|
91
|
+
/** Copy every run in `source`. Purged runs are left alone. */
|
|
92
|
+
readonly rebuild: (source: EventStore) => Promise<number>;
|
|
93
|
+
}
|
|
94
|
+
export declare function sqliteReplica(options: SqliteReplicaOptions): SqliteReplica;
|
|
95
|
+
export { ReplicaGapError };
|