@swirls/looms 0.11.2 → 0.12.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 +24 -4
- package/dist/actor/routing.d.ts +2 -2
- package/dist/actor/routing.js +9 -3
- package/dist/client/client.d.ts +352 -1
- package/dist/client/client.js +28 -12
- package/dist/cloudflare/durable-object.js +16 -8
- package/dist/debugger/app/assets/{index-D13zm4x1.js → index-B_KJqkH1.js} +2 -2
- package/dist/debugger/app/assets/{routes-BrDGfYv0.js → routes-oKOihm_o.js} +2 -2
- package/dist/debugger/app/index.html +1 -1
- package/dist/projectors/delivery.js +12 -2
- package/dist/projectors/event-log.d.ts +7 -1
- package/dist/projectors/event-log.js +26 -12
- package/dist/projectors/postgres.d.ts +9 -1
- package/dist/projectors/postgres.js +25 -2
- package/dist/projectors/replica/migrations.d.ts +14 -1
- package/dist/projectors/replica/migrations.js +25 -3
- package/dist/projectors/replica/postgres.d.ts +4 -2
- package/dist/projectors/replica/postgres.js +9 -6
- package/dist/runtime/looms.d.ts +9 -1
- package/dist/runtime/looms.js +13 -0
- package/dist/runtime/server/fetch-handler.js +2 -1
- package/dist/runtime/server/routes/runs.js +38 -18
- package/package.json +1 -1
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta charset="UTF-8" />
|
|
5
5
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
6
|
<title>Looms debugger</title>
|
|
7
|
-
<script type="module" crossorigin src="./assets/index-
|
|
7
|
+
<script type="module" crossorigin src="./assets/index-B_KJqkH1.js"></script>
|
|
8
8
|
<link rel="modulepreload" crossorigin href="./assets/rolldown-runtime-CbXtAM7H.js">
|
|
9
9
|
<link rel="stylesheet" crossorigin href="./assets/index-CO5gIvV8.css">
|
|
10
10
|
</head>
|
|
@@ -90,6 +90,16 @@ export function createProjectorDelivery(source, projectors, cursors, options = {
|
|
|
90
90
|
(!force && status.nextRetryAt !== null && status.nextRetryAt > currentTime)) {
|
|
91
91
|
return;
|
|
92
92
|
}
|
|
93
|
+
// Skip projector.init (often Hyperdrive/Postgres DDL) when there is nothing
|
|
94
|
+
// to project. Brand-new runs and caught-up deliveries stay off the critical
|
|
95
|
+
// path; projectors with `position` still need init to align the cursor.
|
|
96
|
+
const sourceTail = yield* source.tail(runId).pipe(Effect.orElseSucceed(() => 0));
|
|
97
|
+
if (sourceTail === 0) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (sourceTail <= status.cursor && projector.position === undefined) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
93
103
|
const initialization = yield* Effect.tryPromise({
|
|
94
104
|
try: () => initialize(projector),
|
|
95
105
|
catch: normalizeError,
|
|
@@ -139,14 +149,14 @@ export function createProjectorDelivery(source, projectors, cursors, options = {
|
|
|
139
149
|
nextRetryAt: null,
|
|
140
150
|
lastError: null,
|
|
141
151
|
};
|
|
142
|
-
const
|
|
152
|
+
const lagTail = yield* source.tail(runId).pipe(Effect.orElseSucceed(() => last.seq));
|
|
143
153
|
observe({
|
|
144
154
|
type: 'projector.lag',
|
|
145
155
|
runId,
|
|
146
156
|
projector: projectorIdentity(projector),
|
|
147
157
|
at: advancedAt,
|
|
148
158
|
cursor: last.seq,
|
|
149
|
-
lag: Math.max(0,
|
|
159
|
+
lag: Math.max(0, lagTail - last.seq),
|
|
150
160
|
attempts: 0,
|
|
151
161
|
});
|
|
152
162
|
}
|
|
@@ -3,7 +3,7 @@ import { type EventEnvelope, type EventStore, type RunAttributeFields } from '@s
|
|
|
3
3
|
import { eventLogOps, type EventLogEntry, type EventLogOp, type RunLogRow } from './event-log-ops';
|
|
4
4
|
import type { Projector } from './projector';
|
|
5
5
|
import { type ResolvedReplicaColumn } from './replica/columns';
|
|
6
|
-
import { POSTGRES_EVENT_LOG_SCHEMA_SQL } from './replica/migrations';
|
|
6
|
+
import { POSTGRES_EVENT_LOG_SCHEMA_SQL, type PostgresReplicaMigrate } from './replica/migrations';
|
|
7
7
|
export { POSTGRES_EVENT_LOG_SCHEMA_SQL, eventLogOps };
|
|
8
8
|
export type { EventLogEntry, EventLogOp, RunLogRow };
|
|
9
9
|
export interface EventLogProjector extends Projector {
|
|
@@ -25,6 +25,12 @@ export type PostgresEventLogOptions = ({
|
|
|
25
25
|
}) & {
|
|
26
26
|
/** Declared run attributes. Each one is a `looms_run_index` column. */
|
|
27
27
|
readonly additionalFields?: RunAttributeFields;
|
|
28
|
+
/**
|
|
29
|
+
* Schema ownership on init. Default `auto` applies pending versioned migrations
|
|
30
|
+
* (lock-free when nothing is pending). `verify` refuses to start when a migration
|
|
31
|
+
* is missing. `skip` leaves schema ownership to the host.
|
|
32
|
+
*/
|
|
33
|
+
readonly migrate?: PostgresReplicaMigrate;
|
|
28
34
|
};
|
|
29
35
|
export declare function postgresEventLog(options: PostgresEventLogOptions): EventLogProjector;
|
|
30
36
|
export declare function applySqlOps(sql: TransactionSql, ops: readonly EventLogOp[], columns?: readonly ResolvedReplicaColumn[]): Promise<void>;
|
|
@@ -4,7 +4,7 @@ import {} from '@swirls/looms/core';
|
|
|
4
4
|
import { eventLogOps } from './event-log-ops.js';
|
|
5
5
|
import { runProjectorPromise } from './projector.js';
|
|
6
6
|
import { columnValues, resolveReplicaColumns } from './replica/columns.js';
|
|
7
|
-
import { applyPostgresMigrations, POSTGRES_EVENT_LOG_SCHEMA_SQL,
|
|
7
|
+
import { applyPostgresMigrations, POSTGRES_EVENT_LOG_SCHEMA_SQL, postgresReplicaMigrations, verifyPostgresMigrations, } from './replica/migrations.js';
|
|
8
8
|
export { POSTGRES_EVENT_LOG_SCHEMA_SQL, eventLogOps };
|
|
9
9
|
const terminal = new Set(['completed', 'failed', 'cancelled']);
|
|
10
10
|
export function memoryEventLog() {
|
|
@@ -35,7 +35,7 @@ export function memoryEventLog() {
|
|
|
35
35
|
export function postgresEventLog(options) {
|
|
36
36
|
const created = 'url' in options;
|
|
37
37
|
const connected = created ? createPostgresClient(options.url) : options.sql;
|
|
38
|
-
return createEventLogProjector('postgres-event-log', postgresStore(connected, created, options.additionalFields));
|
|
38
|
+
return createEventLogProjector('postgres-event-log', postgresStore(connected, created, options.additionalFields, options.migrate ?? 'auto'));
|
|
39
39
|
}
|
|
40
40
|
function createEventLogProjector(name, store) {
|
|
41
41
|
return {
|
|
@@ -134,18 +134,32 @@ function mergeFinish(existing, op) {
|
|
|
134
134
|
function utcDate(ms) {
|
|
135
135
|
return DateTime.toDateUtc(DateTime.makeUnsafe(ms));
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
const POSTGRES_EVENT_LOG_INIT_SQL = [
|
|
139
|
-
POSTGRES_EVENT_LOG_SCHEMA_SQL,
|
|
140
|
-
POSTGRES_SCHEMA_MIGRATIONS_SQL,
|
|
141
|
-
].join('\n\n');
|
|
142
|
-
function postgresStore(sql, ownsClient, additionalFields) {
|
|
137
|
+
function postgresStore(sql, ownsClient, additionalFields, migrate) {
|
|
143
138
|
const columns = resolveReplicaColumns(additionalFields);
|
|
144
|
-
const
|
|
139
|
+
const migrations = postgresReplicaMigrations(additionalFields);
|
|
140
|
+
let ready;
|
|
141
|
+
const initSchema = () => {
|
|
142
|
+
switch (migrate) {
|
|
143
|
+
case 'skip':
|
|
144
|
+
return Promise.resolve();
|
|
145
|
+
case 'verify':
|
|
146
|
+
return verifyPostgresMigrations(sql, migrations);
|
|
147
|
+
case 'auto':
|
|
148
|
+
return applyPostgresMigrations(sql, migrations);
|
|
149
|
+
default: {
|
|
150
|
+
const exhaustive = migrate;
|
|
151
|
+
return exhaustive;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
145
155
|
return {
|
|
146
|
-
init: () =>
|
|
147
|
-
.
|
|
148
|
-
|
|
156
|
+
init: () => {
|
|
157
|
+
ready ??= runProjectorPromise('postgres-event-log', initSchema).catch((cause) => {
|
|
158
|
+
ready = undefined;
|
|
159
|
+
throw cause;
|
|
160
|
+
});
|
|
161
|
+
return ready;
|
|
162
|
+
},
|
|
149
163
|
apply: (events) => {
|
|
150
164
|
const ops = events.flatMap(eventLogOps);
|
|
151
165
|
if (ops.length === 0) {
|
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import { type Sql } from 'postgres';
|
|
2
2
|
import { type IndexOp, type IndexProjector } from './index-model';
|
|
3
|
+
import { type PostgresReplicaMigrate } from './replica/migrations';
|
|
3
4
|
export { POSTGRES_SCHEMA_SQL } from './schema';
|
|
4
5
|
export { memoryEventLog, postgresEventLog, POSTGRES_EVENT_LOG_SCHEMA_SQL, type EventLogEntry, type EventLogProjector, type MemoryEventLog, type RunLogRow, } from './event-log';
|
|
5
|
-
export type PostgresProjectorOptions = {
|
|
6
|
+
export type PostgresProjectorOptions = ({
|
|
6
7
|
url: string;
|
|
7
8
|
} | {
|
|
8
9
|
sql: Sql;
|
|
10
|
+
}) & {
|
|
11
|
+
/**
|
|
12
|
+
* Schema ownership on init. Default `auto` applies pending versioned migrations
|
|
13
|
+
* (lock-free when nothing is pending). `verify` refuses to start when a migration
|
|
14
|
+
* is missing. `skip` leaves schema ownership to the host.
|
|
15
|
+
*/
|
|
16
|
+
readonly migrate?: PostgresReplicaMigrate;
|
|
9
17
|
};
|
|
10
18
|
type SqlTag = (strings: TemplateStringsArray, ...values: readonly any[]) => PromiseLike<void>;
|
|
11
19
|
/** Apply actor and review index operations on an existing connection or transaction. */
|
|
@@ -2,7 +2,7 @@ import { DateTime } from 'effect';
|
|
|
2
2
|
import createPostgresClient, {} from 'postgres';
|
|
3
3
|
import { createIndexProjector, } from './index-model.js';
|
|
4
4
|
import { runProjectorPromise } from './projector.js';
|
|
5
|
-
import {
|
|
5
|
+
import { applyPostgresMigrations, postgresReplicaMigrations, verifyPostgresMigrations, } from './replica/migrations.js';
|
|
6
6
|
export { POSTGRES_SCHEMA_SQL } from './schema.js';
|
|
7
7
|
export { memoryEventLog, postgresEventLog, POSTGRES_EVENT_LOG_SCHEMA_SQL, } from './event-log.js';
|
|
8
8
|
function ownsClient(options) {
|
|
@@ -79,8 +79,31 @@ export function applyIndexOps(sql, ops) {
|
|
|
79
79
|
export function postgres(options) {
|
|
80
80
|
const createdClient = ownsClient(options);
|
|
81
81
|
const sql = createdClient ? createPostgresClient(options.url) : options.sql;
|
|
82
|
+
const migrate = options.migrate ?? 'auto';
|
|
83
|
+
const migrations = postgresReplicaMigrations();
|
|
84
|
+
let ready;
|
|
85
|
+
const initSchema = () => {
|
|
86
|
+
switch (migrate) {
|
|
87
|
+
case 'skip':
|
|
88
|
+
return Promise.resolve();
|
|
89
|
+
case 'verify':
|
|
90
|
+
return verifyPostgresMigrations(sql, migrations);
|
|
91
|
+
case 'auto':
|
|
92
|
+
return applyPostgresMigrations(sql, migrations);
|
|
93
|
+
default: {
|
|
94
|
+
const exhaustive = migrate;
|
|
95
|
+
return exhaustive;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
82
99
|
const backend = {
|
|
83
|
-
init: () =>
|
|
100
|
+
init: () => {
|
|
101
|
+
ready ??= runProjectorPromise('postgres', initSchema).catch((cause) => {
|
|
102
|
+
ready = undefined;
|
|
103
|
+
throw cause;
|
|
104
|
+
});
|
|
105
|
+
return ready;
|
|
106
|
+
},
|
|
84
107
|
applyOps: (ops) => runProjectorPromise('postgres', () => sql.begin((tx) => applyIndexOps(tx, ops))).then(() => undefined),
|
|
85
108
|
getActor: (runId, actorId) => runProjectorPromise('postgres', () => sql `
|
|
86
109
|
SELECT * FROM looms_run_actors WHERE run_id = ${runId} AND actor_id = ${actorId}
|
|
@@ -2,6 +2,13 @@ import type { Sql, TransactionSql } from 'postgres';
|
|
|
2
2
|
import type { RunAttributeFields } from '@swirls/looms/core';
|
|
3
3
|
import { type ReplicaMigration, type ResolvedReplicaColumn } from './columns';
|
|
4
4
|
export type { ReplicaMigration };
|
|
5
|
+
/**
|
|
6
|
+
* Schema ownership for Postgres projectors and the replica.
|
|
7
|
+
* `auto` applies pending migrations on init (lock-free when nothing is pending).
|
|
8
|
+
* `verify` refuses to start when a migration has not been applied.
|
|
9
|
+
* `skip` leaves schema ownership to the host.
|
|
10
|
+
*/
|
|
11
|
+
export type PostgresReplicaMigrate = 'auto' | 'verify' | 'skip';
|
|
5
12
|
/**
|
|
6
13
|
* Actors and reviews. Same statements the Postgres index projector has always
|
|
7
14
|
* applied, so a database created by an older Looms keeps working.
|
|
@@ -28,8 +35,14 @@ export declare function postgresColumnIndexSql(column: ResolvedReplicaColumn): s
|
|
|
28
35
|
export declare function postgresAttributeMigrations(additionalFields?: RunAttributeFields): readonly ReplicaMigration[];
|
|
29
36
|
/** Ids recorded in `looms_schema_migrations`, or none when the table does not exist yet. */
|
|
30
37
|
export declare function appliedPostgresMigrationIds(sql: Sql | TransactionSql): Promise<ReadonlySet<string>>;
|
|
31
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* Applies each migration that is not recorded yet, in order, under one advisory lock.
|
|
40
|
+
* When every migration is already recorded, returns after a lock-free read so cold
|
|
41
|
+
* Durable Object inits do not contend on DDL or the global migration lock.
|
|
42
|
+
*/
|
|
32
43
|
export declare function applyPostgresMigrations(sql: Sql, migrations: readonly ReplicaMigration[]): Promise<void>;
|
|
44
|
+
/** Throws when any migration has not been recorded yet. */
|
|
45
|
+
export declare function verifyPostgresMigrations(sql: Sql, migrations: readonly ReplicaMigration[]): Promise<void>;
|
|
33
46
|
/** Versioned DDL for the Postgres replica with no attribute columns. Looms only creates and alters `looms_*` objects. */
|
|
34
47
|
export declare const POSTGRES_REPLICA_MIGRATIONS: readonly ReplicaMigration[];
|
|
35
48
|
export declare function postgresReplicaMigrations(additionalFields?: RunAttributeFields): readonly ReplicaMigration[];
|
|
@@ -143,18 +143,40 @@ export async function appliedPostgresMigrationIds(sql) {
|
|
|
143
143
|
const rows = await sql `SELECT id FROM looms_schema_migrations`;
|
|
144
144
|
return new Set(rows.map((row) => row.id));
|
|
145
145
|
}
|
|
146
|
-
/**
|
|
146
|
+
/**
|
|
147
|
+
* Applies each migration that is not recorded yet, in order, under one advisory lock.
|
|
148
|
+
* When every migration is already recorded, returns after a lock-free read so cold
|
|
149
|
+
* Durable Object inits do not contend on DDL or the global migration lock.
|
|
150
|
+
*/
|
|
147
151
|
export async function applyPostgresMigrations(sql, migrations) {
|
|
152
|
+
if (migrations.length === 0) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
const applied = await appliedPostgresMigrationIds(sql);
|
|
156
|
+
const pending = migrations.some((migration) => !applied.has(migration.id));
|
|
157
|
+
if (!pending) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
148
160
|
await sql.begin(async (tx) => {
|
|
149
161
|
await tx `SELECT pg_advisory_xact_lock(${MIGRATION_LOCK_KEY})`;
|
|
150
|
-
const
|
|
162
|
+
const appliedUnderLock = await appliedPostgresMigrationIds(tx);
|
|
151
163
|
for (const migration of migrations) {
|
|
152
|
-
if (!
|
|
164
|
+
if (!appliedUnderLock.has(migration.id)) {
|
|
153
165
|
await tx.unsafe(migration.sql);
|
|
154
166
|
}
|
|
155
167
|
}
|
|
156
168
|
});
|
|
157
169
|
}
|
|
170
|
+
/** Throws when any migration has not been recorded yet. */
|
|
171
|
+
export async function verifyPostgresMigrations(sql, migrations) {
|
|
172
|
+
const applied = await appliedPostgresMigrationIds(sql);
|
|
173
|
+
const pending = migrations
|
|
174
|
+
.filter((migration) => !applied.has(migration.id))
|
|
175
|
+
.map((migration) => migration.id);
|
|
176
|
+
if (pending.length > 0) {
|
|
177
|
+
throw new Error(`Looms replica is missing migrations: ${pending.join(', ')}. Generate them with \`looms replica generate\` and apply the files before starting.`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
158
180
|
/** Versioned DDL for the Postgres replica with no attribute columns. Looms only creates and alters `looms_*` objects. */
|
|
159
181
|
export const POSTGRES_REPLICA_MIGRATIONS = [
|
|
160
182
|
replicaMigration('0001_replica', [
|
|
@@ -2,6 +2,8 @@ import { type Sql } from 'postgres';
|
|
|
2
2
|
import { type EventStore, type FoldRegistry, type RunAttributeFields, type RunState } from '@swirls/looms/core';
|
|
3
3
|
import { ReplicaGapError, type Projector } from '../projector';
|
|
4
4
|
import { type NoAdditionalFields, type ReplicaPurgeOptions } from './columns';
|
|
5
|
+
import { type PostgresReplicaMigrate } from './migrations';
|
|
6
|
+
export type { PostgresReplicaMigrate };
|
|
5
7
|
export type { NoAdditionalFields, ReplicaPurgeOptions } from './columns';
|
|
6
8
|
export interface ReplicaStatus {
|
|
7
9
|
/** Highest sequence copied with no hole in front of it. */
|
|
@@ -16,16 +18,16 @@ export interface ReplicaStatus {
|
|
|
16
18
|
export interface ReplicaPurgeResult {
|
|
17
19
|
readonly runs: number;
|
|
18
20
|
}
|
|
19
|
-
export type PostgresReplicaMigrate = 'auto' | 'verify';
|
|
20
21
|
export type PostgresReplicaOptions<Fields extends RunAttributeFields = NoAdditionalFields> = ({
|
|
21
22
|
readonly url: string;
|
|
22
23
|
} | {
|
|
23
24
|
readonly sql: Sql;
|
|
24
25
|
}) & {
|
|
25
26
|
/**
|
|
26
|
-
* `auto` applies pending migrations on init.
|
|
27
|
+
* `auto` applies pending migrations on init (lock-free when nothing is pending).
|
|
27
28
|
* `verify` refuses to start when a migration has not been applied, and names
|
|
28
29
|
* `looms replica generate` so the host can commit the SQL.
|
|
30
|
+
* `skip` leaves schema ownership to the host (for example drizzle migrations).
|
|
29
31
|
*/
|
|
30
32
|
readonly migrate: PostgresReplicaMigrate;
|
|
31
33
|
/**
|
|
@@ -115,19 +115,22 @@ export function postgresReplica(options) {
|
|
|
115
115
|
.map((migration) => migration.id);
|
|
116
116
|
};
|
|
117
117
|
const migrate = () => applyPostgresMigrations(sql, migrations);
|
|
118
|
+
const verify = async () => {
|
|
119
|
+
const pending = await pendingMigrations();
|
|
120
|
+
if (pending.length > 0) {
|
|
121
|
+
throw new Error(`Looms replica is missing migrations: ${pending.join(', ')}. Generate them with \`looms replica generate\` and apply the files before starting.`);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
118
124
|
const init = () => {
|
|
125
|
+
if (options.migrate === 'skip') {
|
|
126
|
+
return Promise.resolve();
|
|
127
|
+
}
|
|
119
128
|
ready ??= (options.migrate === 'verify' ? verify() : migrate()).catch((cause) => {
|
|
120
129
|
ready = undefined;
|
|
121
130
|
throw cause;
|
|
122
131
|
});
|
|
123
132
|
return ready;
|
|
124
133
|
};
|
|
125
|
-
const verify = async () => {
|
|
126
|
-
const pending = await pendingMigrations();
|
|
127
|
-
if (pending.length > 0) {
|
|
128
|
-
throw new Error(`Looms replica is missing migrations: ${pending.join(', ')}. Generate them with \`looms replica generate\` and apply the files before starting.`);
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
134
|
const project = async (events) => {
|
|
132
135
|
await init();
|
|
133
136
|
if (events.length === 0) {
|
package/dist/runtime/looms.d.ts
CHANGED
|
@@ -93,9 +93,17 @@ export interface Looms<Fields extends RunAttributeFields = RunAttributeFields> {
|
|
|
93
93
|
}>;
|
|
94
94
|
/** Start a run from a definition object; the input type follows the definition's schema. */
|
|
95
95
|
start<TDef extends DefinitionRef>(definition: TDef, ...args: LoomsStartArgs<TDef, Fields>): Promise<StartResult>;
|
|
96
|
+
/**
|
|
97
|
+
* Like `start`, but answers once the start is committed and leaves the first wake to the
|
|
98
|
+
* scheduler.
|
|
99
|
+
*/
|
|
100
|
+
launch<TDef extends DefinitionRef>(definition: TDef, ...args: LoomsStartArgs<TDef, Fields>): Promise<StartResult>;
|
|
96
101
|
/** Start a run by `{ kind, definitionName }` when you only have names (HTTP bodies, CLIs). */
|
|
97
102
|
startRun(args: LoomsStartRunArgs<Fields>): Promise<StartResult>;
|
|
98
|
-
/**
|
|
103
|
+
/**
|
|
104
|
+
* Like `startRun`, but answers once the start is committed and leaves the first wake to the
|
|
105
|
+
* scheduler.
|
|
106
|
+
*/
|
|
99
107
|
launchRun(args: LoomsStartRunArgs<Fields>): Promise<StartResult>;
|
|
100
108
|
getRun(runId: string): Promise<RunState>;
|
|
101
109
|
getEvents(runId: string, options?: {
|
package/dist/runtime/looms.js
CHANGED
|
@@ -101,6 +101,19 @@ export function createLooms(options = {}) {
|
|
|
101
101
|
secrets: startOpts?.secrets,
|
|
102
102
|
trace: startOpts?.trace,
|
|
103
103
|
})),
|
|
104
|
+
launch: (definition, input, startOpts) => runEffect((i) => i.runtime.launchRun({
|
|
105
|
+
kind: definition.kind,
|
|
106
|
+
definitionName: definition.name,
|
|
107
|
+
definitionVersion: definition.version,
|
|
108
|
+
// SAFETY: input is validated against the definition's schema inside launchRun.
|
|
109
|
+
input: input,
|
|
110
|
+
runId: startOpts?.runId,
|
|
111
|
+
threadId: startOpts?.threadId,
|
|
112
|
+
idempotencyKey: startOpts?.idempotencyKey,
|
|
113
|
+
attributes: startOpts?.attributes,
|
|
114
|
+
secrets: startOpts?.secrets,
|
|
115
|
+
trace: startOpts?.trace,
|
|
116
|
+
})),
|
|
104
117
|
getRun: (runId) => runEffect((i) => i.runtime.getRun(runId)),
|
|
105
118
|
getEvents: (runId, opts) => runEffect((i) => i.runtime.getEvents(runId, opts)),
|
|
106
119
|
signal: (runId, events, signalOpts) => runEffect((i) => i.runtime.signal(runId, events, signalOpts)),
|
|
@@ -23,6 +23,7 @@ const StartAttributesSchema = Schema.Struct({
|
|
|
23
23
|
class UnreadableStartBody extends Data.TaggedError('UnreadableStartBody') {
|
|
24
24
|
}
|
|
25
25
|
const noAttributes = {};
|
|
26
|
+
const START_ROUTES = new Set(['runs.start', 'runs.launch']);
|
|
26
27
|
function readRequestAttributes(req) {
|
|
27
28
|
return Effect.tryPromise({
|
|
28
29
|
try: () => req.clone().json(),
|
|
@@ -42,7 +43,7 @@ function authorizeRoute(req, matched, authorize, runtime, store) {
|
|
|
42
43
|
const stored = Predicate.isString(runId)
|
|
43
44
|
? yield* readStoredAttributes(runtime, store, runId)
|
|
44
45
|
: noAttributes;
|
|
45
|
-
const requested =
|
|
46
|
+
const requested = START_ROUTES.has(name) ? yield* readRequestAttributes(req) : noAttributes;
|
|
46
47
|
const attributes = hasEntries(stored) ? stored : requested;
|
|
47
48
|
const route = Predicate.isString(runId)
|
|
48
49
|
? hasEntries(attributes)
|
|
@@ -32,6 +32,27 @@ const CancelBodySchema = Schema.Struct({
|
|
|
32
32
|
const ForkBodySchema = Schema.Struct({
|
|
33
33
|
runId: Schema.optional(Schema.String),
|
|
34
34
|
});
|
|
35
|
+
function readStartRunArgs(req) {
|
|
36
|
+
return Effect.gen(function* () {
|
|
37
|
+
const body = yield* readJson(req).pipe(Effect.flatMap(Schema.decodeUnknownEffect(StartRunBodySchema)));
|
|
38
|
+
const definitionName = body.definitionName ?? body.name;
|
|
39
|
+
const kind = body.kind;
|
|
40
|
+
if (!definitionName || !kind) {
|
|
41
|
+
return Response.json({ error: 'kind and definitionName required' }, { status: 400 });
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
kind,
|
|
45
|
+
definitionName,
|
|
46
|
+
definitionVersion: body.definitionVersion,
|
|
47
|
+
input: body.input ?? null,
|
|
48
|
+
runId: body.runId ?? createRunId(),
|
|
49
|
+
idempotencyKey: extractIdempotencyKey(req, body.idempotencyKey),
|
|
50
|
+
attributes: body.attributes,
|
|
51
|
+
secrets: body.secrets,
|
|
52
|
+
trace: readTraceHeaders(req.headers),
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
}
|
|
35
56
|
function wantsRunStream(req, url) {
|
|
36
57
|
return url.searchParams.get('stream') === 'true' || acceptsEventStream(req);
|
|
37
58
|
}
|
|
@@ -94,25 +115,10 @@ export function runRoutes({ runtime, store }) {
|
|
|
94
115
|
name: 'runs.start',
|
|
95
116
|
access: 'write',
|
|
96
117
|
handle: ({ req, url }) => Effect.gen(function* () {
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (!definitionName || !kind) {
|
|
101
|
-
return Response.json({ error: 'kind and definitionName required' }, { status: 400 });
|
|
118
|
+
const args = yield* readStartRunArgs(req);
|
|
119
|
+
if (args instanceof Response) {
|
|
120
|
+
return args;
|
|
102
121
|
}
|
|
103
|
-
const runId = body.runId ?? createRunId();
|
|
104
|
-
const idempotencyKey = extractIdempotencyKey(req, body.idempotencyKey);
|
|
105
|
-
const args = {
|
|
106
|
-
kind,
|
|
107
|
-
definitionName,
|
|
108
|
-
definitionVersion: body.definitionVersion,
|
|
109
|
-
input: body.input ?? null,
|
|
110
|
-
runId,
|
|
111
|
-
idempotencyKey,
|
|
112
|
-
attributes: body.attributes,
|
|
113
|
-
secrets: body.secrets,
|
|
114
|
-
trace: readTraceHeaders(req.headers),
|
|
115
|
-
};
|
|
116
122
|
if (wantsRunStream(req, url)) {
|
|
117
123
|
return streamStartRun(req, runtime, store, args);
|
|
118
124
|
}
|
|
@@ -120,6 +126,20 @@ export function runRoutes({ runtime, store }) {
|
|
|
120
126
|
return Response.json(result);
|
|
121
127
|
}),
|
|
122
128
|
}),
|
|
129
|
+
route({
|
|
130
|
+
method: 'POST',
|
|
131
|
+
path: '/runs/launch',
|
|
132
|
+
name: 'runs.launch',
|
|
133
|
+
access: 'write',
|
|
134
|
+
handle: ({ req }) => Effect.gen(function* () {
|
|
135
|
+
const args = yield* readStartRunArgs(req);
|
|
136
|
+
if (args instanceof Response) {
|
|
137
|
+
return args;
|
|
138
|
+
}
|
|
139
|
+
const result = yield* provideStore(runtime.launchRun(args), store);
|
|
140
|
+
return Response.json(result);
|
|
141
|
+
}),
|
|
142
|
+
}),
|
|
123
143
|
route({
|
|
124
144
|
method: 'GET',
|
|
125
145
|
path: '/runs/:runId/replay',
|