@swirls/looms 0.6.1 → 0.8.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 +26 -0
- package/dist/actor/cell.d.ts +1 -1
- package/dist/actor/routing.d.ts +5 -1
- package/dist/actor/routing.js +14 -3
- package/dist/agent/effects.d.ts +20 -2
- package/dist/agent/effects.js +2 -0
- package/dist/agent/module.d.ts +40 -4
- package/dist/approval/effects.d.ts +10 -1
- package/dist/approval/effects.js +1 -0
- package/dist/approval/module.d.ts +20 -2
- package/dist/cli/commands.js +65 -1
- package/dist/client/client.d.ts +38 -10
- package/dist/client/client.js +2 -1
- package/dist/cloudflare/durable-object.d.ts +1 -1
- package/dist/cloudflare/index.d.ts +1 -1
- package/dist/cloudflare/index.js +1 -1
- package/dist/cloudflare/queue-consumer.d.ts +17 -10
- package/dist/cloudflare/queue-consumer.js +63 -27
- package/dist/cloudflare/queue-message.d.ts +4 -1
- package/dist/cloudflare/queue-message.js +4 -1
- package/dist/cloudflare/queue-worker.d.ts +7 -3
- package/dist/cloudflare/queue-worker.js +7 -2
- package/dist/cloudflare/queues.d.ts +1 -1
- package/dist/cloudflare/queues.js +1 -1
- package/dist/core/compose.d.ts +2 -1
- package/dist/core/compose.js +5 -0
- package/dist/core/effects.d.ts +84 -39
- package/dist/core/effects.js +29 -8
- package/dist/core/fold.js +34 -0
- package/dist/core/module-scope.d.ts +22 -8
- package/dist/core/module-scope.js +3 -0
- package/dist/core/module.d.ts +40 -9
- package/dist/core/module.js +25 -6
- package/dist/core/protocol.d.ts +12 -1
- package/dist/core/protocol.js +12 -0
- package/dist/core/published.d.ts +52 -0
- package/dist/core/published.js +45 -0
- package/dist/core/replay.d.ts +4 -2
- package/dist/core/state.d.ts +8 -2
- package/dist/core/state.js +2 -0
- package/dist/debugger/app/assets/{index-CUO1xyYW.js → index-CMRl_aWQ.js} +2 -2
- package/dist/debugger/app/assets/routes-CfXxKG45.js +47 -0
- package/dist/debugger/app/index.html +1 -1
- package/dist/debugger/events/protocol-summarize.js +2 -0
- package/dist/evaluator/effects.d.ts +10 -1
- package/dist/evaluator/effects.js +1 -0
- package/dist/evaluator/module.d.ts +20 -2
- package/dist/projectors/delivery.d.ts +8 -3
- package/dist/projectors/delivery.js +29 -2
- package/dist/projectors/event-log.d.ts +8 -2
- package/dist/projectors/event-log.js +47 -61
- package/dist/projectors/index.d.ts +1 -0
- package/dist/projectors/index.js +1 -0
- package/dist/projectors/postgres.d.ts +4 -1
- package/dist/projectors/postgres.js +6 -4
- package/dist/projectors/projector.d.ts +16 -0
- package/dist/projectors/projector.js +3 -0
- package/dist/projectors/replica/drizzle.d.ts +1192 -0
- package/dist/projectors/replica/drizzle.js +119 -0
- package/dist/projectors/replica/migrations.d.ts +35 -0
- package/dist/projectors/replica/migrations.js +230 -0
- package/dist/projectors/replica/postgres.d.ts +57 -0
- package/dist/projectors/replica/postgres.js +356 -0
- package/dist/projectors/schema.d.ts +2 -1
- package/dist/projectors/schema.js +2 -31
- package/dist/projectors/sqlite-delivery.js +9 -0
- package/dist/runtime/cancellation.js +1 -1
- package/dist/runtime/dispatch-effect.d.ts +2 -2
- package/dist/runtime/dispatch-effect.js +12 -4
- package/dist/runtime/effect-admission.d.ts +45 -0
- package/dist/runtime/effect-admission.js +70 -0
- package/dist/runtime/helpers.d.ts +6 -1
- package/dist/runtime/helpers.js +14 -5
- package/dist/runtime/index.d.ts +1 -0
- package/dist/runtime/index.js +1 -0
- package/dist/runtime/local-effect-executor.js +25 -3
- package/dist/runtime/retry-policy.js +6 -1
- package/dist/runtime/runtime.js +7 -2
- package/dist/runtime/server/routes/definitions.js +8 -1
- package/dist/runtime/types.d.ts +20 -5
- package/dist/runtime/wake-engine.js +20 -0
- package/dist/runtime/worker-callbacks.js +2 -2
- package/dist/runtime/worker-effect-dispatch.d.ts +35 -9
- package/dist/runtime/worker-effect-dispatch.js +105 -26
- package/dist/schedule/effects.d.ts +20 -2
- package/dist/schedule/effects.js +2 -0
- package/dist/schedule/module.d.ts +76 -40
- package/dist/testing/corpus-module.d.ts +2 -2
- package/dist/testing/corpus-module.js +27 -19
- package/dist/testing/index.js +12 -23
- package/dist/workflow/effects.d.ts +20 -2
- package/dist/workflow/effects.js +2 -0
- package/dist/workflow/module.d.ts +40 -4
- package/package.json +29 -1
- package/dist/debugger/app/assets/routes-lt_6J025.js +0 -47
package/dist/cloudflare/index.js
CHANGED
|
@@ -4,7 +4,7 @@ export { durableObjectLauncher, durableObjectScheduleHost, LoomsDurableObject, }
|
|
|
4
4
|
export { r2BlobStore, } from './r2-blob-store.js';
|
|
5
5
|
export { routeToDurableObject, } from './router.js';
|
|
6
6
|
export { createMemoryQueue } from './memory-queue.js';
|
|
7
|
-
export { processQueueMessage, queueConsumer, QueueTaskError, } from './queue-consumer.js';
|
|
7
|
+
export { processQueueMessage, queueConsumer, QueueTaskError, workerHandlers, } from './queue-consumer.js';
|
|
8
8
|
export { readQueueEffectMessage, } from './queue-message.js';
|
|
9
9
|
export { cloudflareQueuePullSource, createQueuePullConsumer, QueuePullError, } from './queue-pull.js';
|
|
10
10
|
export { cloudflareQueueWorker, DEFAULT_MAX_QUEUE_MESSAGE_BYTES, QueueDispatchError, } from './queue-worker.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Effect } from 'effect';
|
|
2
|
-
import { type BlobStore, type EventInput, type JsonValue, type
|
|
2
|
+
import { type BlobStore, type EventInput, type JsonValue, type WorkerCallback, type WorkerCallbackInput, type WorkerCallbackResult, type WorkerEffectContext, type WorkerEffectDefinition, type WorkerSecretsResult } from '@swirls/looms/core';
|
|
3
3
|
import { type QueueEffectMessage, type QueueMessageLike } from './queue-message';
|
|
4
4
|
/** The worker half of the Looms HTTP API. `createLoomsClient({ baseUrl, workerCallbackToken })` is one. */
|
|
5
5
|
export interface WorkerCallbackClient {
|
|
@@ -10,15 +10,22 @@ export interface WorkerCallbackClient {
|
|
|
10
10
|
export interface QueueTask extends Omit<QueueEffectMessage, 'v' | 'input' | 'inputRef'> {
|
|
11
11
|
readonly input: JsonValue;
|
|
12
12
|
}
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
export type QueueTaskHandler = (task: QueueTask, ctx: WorkerEffectContext) => Promise<ReadonlyArray<EventInput>> | ReadonlyArray<EventInput>;
|
|
14
|
+
type WorkerHandlerFn<TInput> = (input: TInput, ctx: WorkerEffectContext) => Promise<ReadonlyArray<EventInput>> | ReadonlyArray<EventInput>;
|
|
15
|
+
type WorkerHandlerBinding<TInput> = readonly [
|
|
16
|
+
WorkerEffectDefinition<TInput>,
|
|
17
|
+
WorkerHandlerFn<TInput>
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Build a `handlers` map from **worker-placed** effect definitions. Input is validated with each
|
|
21
|
+
* definition's schema before the typed handler runs. Passing a local definition is a type error.
|
|
22
|
+
*/
|
|
23
|
+
export declare function workerHandlers<const Entries extends ReadonlyArray<WorkerHandlerBinding<any>>>(entries: {
|
|
24
|
+
readonly [K in keyof Entries]: Entries[K] extends readonly [
|
|
25
|
+
WorkerEffectDefinition<infer TInput>,
|
|
26
|
+
unknown
|
|
27
|
+
] ? WorkerHandlerBinding<TInput> : Entries[K];
|
|
28
|
+
}): Readonly<Record<string, QueueTaskHandler>>;
|
|
22
29
|
declare const QueueTaskError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
23
30
|
readonly _tag: "QueueTaskError";
|
|
24
31
|
} & Readonly<A>;
|
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
import { Data, Effect } from 'effect';
|
|
2
|
-
import { resolveJson, } from '@swirls/looms/core';
|
|
2
|
+
import { resolveJson, validateInput, } from '@swirls/looms/core';
|
|
3
3
|
import { readQueueEffectMessage, } from './queue-message.js';
|
|
4
|
+
/**
|
|
5
|
+
* Build a `handlers` map from **worker-placed** effect definitions. Input is validated with each
|
|
6
|
+
* definition's schema before the typed handler runs. Passing a local definition is a type error.
|
|
7
|
+
*/
|
|
8
|
+
export function workerHandlers(entries) {
|
|
9
|
+
// SAFETY: keys are effect types; values are QueueTaskHandler wrappers around typed handlers.
|
|
10
|
+
const handlers = Object.create(null);
|
|
11
|
+
for (const [definition, handler] of entries) {
|
|
12
|
+
handlers[definition.type] = (task, ctx) => {
|
|
13
|
+
if (definition.input) {
|
|
14
|
+
return validateInput(definition.input, task.input).then((input) => {
|
|
15
|
+
// SAFETY: validateInput returns InferSchemaOutput for the definition's schema; TInput is that type.
|
|
16
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion
|
|
17
|
+
return handler(input, ctx);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return Promise.resolve(handler(task.input, ctx));
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return handlers;
|
|
24
|
+
}
|
|
4
25
|
/** Throw from a handler to control how Looms treats the failed attempt. */
|
|
5
26
|
export class QueueTaskError extends Data.TaggedError('QueueTaskError') {
|
|
6
27
|
}
|
|
@@ -45,32 +66,47 @@ function runAttempt(task, attempt, handler, cancelRequested, options) {
|
|
|
45
66
|
: Effect.sleep(50 * (5 - remaining)).pipe(Effect.andThen(send(remaining - 1)))));
|
|
46
67
|
return send(4);
|
|
47
68
|
};
|
|
48
|
-
const
|
|
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
|
-
|
|
69
|
+
const emitted = [];
|
|
70
|
+
const loadSecrets = () => callback(() => options.callbacks.workerSecrets(task.runId, attempt)).pipe(Effect.flatMap((result) => {
|
|
71
|
+
if (result.status === 'accepted') {
|
|
72
|
+
return Effect.succeed(result.secrets);
|
|
73
|
+
}
|
|
74
|
+
controller.abort();
|
|
75
|
+
return Effect.fail(new CallbackError({ message: `the attempt is ${result.status}` }));
|
|
76
|
+
}));
|
|
77
|
+
return taskInput(task, options.blobs).pipe(Effect.flatMap((input) => loadSecrets().pipe(Effect.mapError((error) => new QueueTaskError({ message: error.message, nonRetryable: true })), Effect.flatMap((secrets) => {
|
|
78
|
+
const ctx = {
|
|
79
|
+
placement: 'worker',
|
|
80
|
+
effectId: task.effectId,
|
|
81
|
+
runId: task.runId,
|
|
82
|
+
threadId: task.threadId,
|
|
83
|
+
causingEventId: task.causingEventId,
|
|
84
|
+
signal: controller.signal,
|
|
85
|
+
attributes: task.attributes ?? {},
|
|
86
|
+
secrets,
|
|
87
|
+
trace: task.trace ?? null,
|
|
88
|
+
heartbeat: (details) => Effect.runPromise(callback(() => options.callbacks.workerCallback(task.runId, details === undefined
|
|
89
|
+
? { ...attempt, kind: 'heartbeat' }
|
|
90
|
+
: { ...attempt, kind: 'heartbeat', details })).pipe(Effect.map((result) => {
|
|
91
|
+
if (result.status !== 'accepted' || result.cancelRequested) {
|
|
92
|
+
controller.abort();
|
|
93
|
+
}
|
|
94
|
+
}))),
|
|
95
|
+
emit: (event) => {
|
|
96
|
+
emitted.push(event);
|
|
97
|
+
return Promise.resolve();
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
return Effect.tryPromise({
|
|
101
|
+
try: () => Promise.resolve(handler({ ...taskFields(task), input }, ctx)),
|
|
102
|
+
catch: (cause) => cause instanceof QueueTaskError
|
|
103
|
+
? cause
|
|
104
|
+
: new QueueTaskError({
|
|
105
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
106
|
+
}),
|
|
107
|
+
});
|
|
108
|
+
}))), Effect.matchEffect({
|
|
109
|
+
onSuccess: (events) => report({ ...attempt, kind: 'complete', events: [...emitted, ...events] }),
|
|
74
110
|
onFailure: (error) => report(controller.signal.aborted
|
|
75
111
|
? { ...attempt, kind: 'cancelled' }
|
|
76
112
|
: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type BlobRef, type JsonValue, type TraceContext } from '@swirls/looms/core';
|
|
1
|
+
import { type BlobRef, type JsonValue, type RunAttributes, type TraceContext } from '@swirls/looms/core';
|
|
2
2
|
import type { EffectWorkerDeadlines } from '@swirls/looms/runtime';
|
|
3
3
|
/**
|
|
4
4
|
* One effect attempt on a Cloudflare Queue. It never carries the run's secrets; the consumer reads
|
|
@@ -13,6 +13,9 @@ export interface QueueEffectMessage {
|
|
|
13
13
|
readonly input?: JsonValue;
|
|
14
14
|
/** Set instead of `input` when the input would push the message over the queue's size limit. */
|
|
15
15
|
readonly inputRef?: BlobRef;
|
|
16
|
+
readonly threadId: string;
|
|
17
|
+
readonly causingEventId: string;
|
|
18
|
+
readonly attributes?: RunAttributes;
|
|
16
19
|
/** Progress from the previous attempt's last heartbeat. */
|
|
17
20
|
readonly details?: JsonValue;
|
|
18
21
|
readonly trace?: TraceContext;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Option, Schema } from 'effect';
|
|
2
|
-
import { isBlobRef, TraceContextSchema, } from '@swirls/looms/core';
|
|
2
|
+
import { isBlobRef, RunAttributesSchema, TraceContextSchema, } from '@swirls/looms/core';
|
|
3
3
|
const QueueEffectMessageSchema = Schema.Struct({
|
|
4
4
|
v: Schema.Literal(1),
|
|
5
5
|
runId: Schema.String,
|
|
@@ -8,6 +8,9 @@ const QueueEffectMessageSchema = Schema.Struct({
|
|
|
8
8
|
type: Schema.String,
|
|
9
9
|
input: Schema.optional(Schema.Json),
|
|
10
10
|
inputRef: Schema.optional(Schema.Json),
|
|
11
|
+
threadId: Schema.String,
|
|
12
|
+
causingEventId: Schema.String,
|
|
13
|
+
attributes: Schema.optional(RunAttributesSchema),
|
|
11
14
|
details: Schema.optional(Schema.Json),
|
|
12
15
|
trace: Schema.optional(TraceContextSchema),
|
|
13
16
|
deadlines: Schema.optional(Schema.Struct({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type Types } from 'effect';
|
|
2
|
-
import { type BlobStore } from '@swirls/looms/core';
|
|
2
|
+
import { type BlobStore, type WorkerEffectDefinition } from '@swirls/looms/core';
|
|
3
3
|
import type { EffectWorker } from '@swirls/looms/runtime';
|
|
4
4
|
import type { QueueEffectMessage } from './queue-message';
|
|
5
5
|
/** Leaves room under Cloudflare's 128 KB message limit. */
|
|
@@ -10,12 +10,16 @@ export interface QueueProducer {
|
|
|
10
10
|
readonly contentType?: 'json';
|
|
11
11
|
}): Promise<void>;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Route worker-placed effect definitions to a queue. Passing a local-placed definition is a
|
|
15
|
+
* type error — only {@link WorkerEffectDefinition} values are accepted.
|
|
16
|
+
*/
|
|
13
17
|
export interface QueueRoute {
|
|
14
|
-
readonly
|
|
18
|
+
readonly effects: ReadonlyArray<WorkerEffectDefinition>;
|
|
15
19
|
readonly queue: QueueProducer;
|
|
16
20
|
}
|
|
17
21
|
export interface CloudflareQueueWorkerOptions {
|
|
18
|
-
/** The first route that
|
|
22
|
+
/** The first route that lists an effect type gets its attempts. */
|
|
19
23
|
readonly routes: ReadonlyArray<QueueRoute>;
|
|
20
24
|
/** Holds an input that would push a message past `maxMessageBytes`. Without it, that dispatch fails. */
|
|
21
25
|
readonly blobs?: BlobStore;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Data, Effect
|
|
1
|
+
import { Data, Effect } from 'effect';
|
|
2
2
|
import { claimJson, utf8JsonBytes } from '@swirls/looms/core';
|
|
3
3
|
/** Leaves room under Cloudflare's 128 KB message limit. */
|
|
4
4
|
export const DEFAULT_MAX_QUEUE_MESSAGE_BYTES = 100_000;
|
|
@@ -12,7 +12,7 @@ export class QueueDispatchError extends Data.TaggedError('QueueDispatchError') {
|
|
|
12
12
|
* queued message cannot be recalled.
|
|
13
13
|
*/
|
|
14
14
|
export function cloudflareQueueWorker(options) {
|
|
15
|
-
const routeFor = (type) => options.routes.find((route) =>
|
|
15
|
+
const routeFor = (type) => options.routes.find((route) => route.effects.some((effect) => effect.type === type));
|
|
16
16
|
return {
|
|
17
17
|
dispatchSemantics: 'acknowledged',
|
|
18
18
|
handles: (type) => routeFor(type) !== undefined,
|
|
@@ -37,7 +37,12 @@ function queueMessage(task, options) {
|
|
|
37
37
|
effectId: task.effectId,
|
|
38
38
|
attempt: task.attempt,
|
|
39
39
|
type: task.type,
|
|
40
|
+
threadId: task.threadId,
|
|
41
|
+
causingEventId: task.causingEventId,
|
|
40
42
|
};
|
|
43
|
+
if (task.attributes !== undefined) {
|
|
44
|
+
message.attributes = task.attributes;
|
|
45
|
+
}
|
|
41
46
|
if (task.details !== undefined) {
|
|
42
47
|
message.details = task.details;
|
|
43
48
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* only inside the Workers runtime that hosts Durable Objects.
|
|
5
5
|
*/
|
|
6
6
|
export { createMemoryQueue, type MemoryQueue, type MemoryQueueOptions } from './memory-queue';
|
|
7
|
-
export { processQueueMessage, queueConsumer, QueueTaskError, type QueueBatchLike, type QueueConsumerOptions, type QueueTask, type
|
|
7
|
+
export { processQueueMessage, queueConsumer, QueueTaskError, workerHandlers, type QueueBatchLike, type QueueConsumerOptions, type QueueTask, type QueueTaskHandler, type WorkerCallbackClient, } from './queue-consumer';
|
|
8
8
|
export { readQueueEffectMessage, type QueueEffectMessage, type QueueMessageLike, } from './queue-message';
|
|
9
9
|
export { cloudflareQueuePullSource, createQueuePullConsumer, QueuePullError, type CloudflareQueuePullSourceOptions, type PulledQueueMessage, type QueueLeaseRetry, type QueuePullConsumer, type QueuePullConsumerOptions, type QueuePullResult, type QueuePullSource, } from './queue-pull';
|
|
10
10
|
export { cloudflareQueueWorker, DEFAULT_MAX_QUEUE_MESSAGE_BYTES, QueueDispatchError, type CloudflareQueueWorkerOptions, type QueueProducer, type QueueRoute, } from './queue-worker';
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* only inside the Workers runtime that hosts Durable Objects.
|
|
5
5
|
*/
|
|
6
6
|
export { createMemoryQueue } from './memory-queue.js';
|
|
7
|
-
export { processQueueMessage, queueConsumer, QueueTaskError, } from './queue-consumer.js';
|
|
7
|
+
export { processQueueMessage, queueConsumer, QueueTaskError, workerHandlers, } from './queue-consumer.js';
|
|
8
8
|
export { readQueueEffectMessage, } from './queue-message.js';
|
|
9
9
|
export { cloudflareQueuePullSource, createQueuePullConsumer, QueuePullError, } from './queue-pull.js';
|
|
10
10
|
export { cloudflareQueueWorker, DEFAULT_MAX_QUEUE_MESSAGE_BYTES, QueueDispatchError, } from './queue-worker.js';
|
package/dist/core/compose.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { CatalogEvent, EventCatalog } from './catalog';
|
|
|
3
3
|
import type { EffectDefinition } from './effects';
|
|
4
4
|
import type { EventEnvelope } from './envelope';
|
|
5
5
|
import type { FoldRegistry } from './fold';
|
|
6
|
-
import type { AnyRuntimeModule, EffectMiddleware, RuntimeModule, RegisteredDefinition, SignalInterruptRule } from './module';
|
|
6
|
+
import type { AnyRuntimeModule, EffectAdmission, EffectMiddleware, RuntimeModule, RegisteredDefinition, SignalInterruptRule } from './module';
|
|
7
7
|
import type { ProjectionDefinition } from './projection';
|
|
8
8
|
import { protocolCatalog } from './protocol';
|
|
9
9
|
import type { ThreadDefinition } from './thread';
|
|
@@ -22,6 +22,7 @@ export interface ComposedRegistry extends FoldRegistry {
|
|
|
22
22
|
readonly projections: ReadonlyMap<string, ProjectionDefinition>;
|
|
23
23
|
readonly catalogs: readonly EventCatalog[];
|
|
24
24
|
readonly middleware: readonly EffectMiddleware[];
|
|
25
|
+
readonly admission: readonly EffectAdmission[];
|
|
25
26
|
readonly interruptRules: readonly SignalInterruptRule[];
|
|
26
27
|
}
|
|
27
28
|
export declare function composeModules(modules: readonly AnyRuntimeModule[]): ComposedRegistry;
|
package/dist/core/compose.js
CHANGED
|
@@ -15,6 +15,7 @@ export function composeModules(modules) {
|
|
|
15
15
|
const effects = new Map();
|
|
16
16
|
const projections = new Map();
|
|
17
17
|
const middleware = [];
|
|
18
|
+
const admission = [];
|
|
18
19
|
const interruptRules = [];
|
|
19
20
|
const catalogs = [protocolCatalog];
|
|
20
21
|
for (const module of modules) {
|
|
@@ -81,6 +82,9 @@ export function composeModules(modules) {
|
|
|
81
82
|
if (module.middleware) {
|
|
82
83
|
middleware.push(...module.middleware);
|
|
83
84
|
}
|
|
85
|
+
if (module.admission) {
|
|
86
|
+
admission.push(...module.admission);
|
|
87
|
+
}
|
|
84
88
|
if (module.interruptsOnSignal) {
|
|
85
89
|
interruptRules.push(module.interruptsOnSignal);
|
|
86
90
|
}
|
|
@@ -101,6 +105,7 @@ export function composeModules(modules) {
|
|
|
101
105
|
projections,
|
|
102
106
|
catalogs,
|
|
103
107
|
middleware,
|
|
108
|
+
admission,
|
|
104
109
|
interruptRules,
|
|
105
110
|
};
|
|
106
111
|
}
|
package/dist/core/effects.d.ts
CHANGED
|
@@ -167,7 +167,8 @@ export declare function deferredUntil(effect: RuntimeEffect): number | undefined
|
|
|
167
167
|
export declare const WITHDRAWN_ERROR_PREFIX = "withdrawn:";
|
|
168
168
|
export declare function withdrawnError(siblingEffectId: string): string;
|
|
169
169
|
export declare function isWithdrawnError(error: string): boolean;
|
|
170
|
-
|
|
170
|
+
/** Shared identity and credentials for an effect attempt. */
|
|
171
|
+
export interface EffectContextBase {
|
|
171
172
|
readonly effectId: string;
|
|
172
173
|
readonly runId: string;
|
|
173
174
|
readonly threadId: string;
|
|
@@ -175,23 +176,43 @@ export interface EffectContext {
|
|
|
175
176
|
readonly signal: AbortSignal;
|
|
176
177
|
/** Labels safe for every reader of the run. */
|
|
177
178
|
readonly attributes: RunAttributes;
|
|
178
|
-
/**
|
|
179
|
+
/**
|
|
180
|
+
* Bearer material for this run. Never copy it into an emitted event.
|
|
181
|
+
* On a durable worker transport this is loaded before the handler runs (same shape as local).
|
|
182
|
+
*/
|
|
179
183
|
readonly secrets: SecretContext;
|
|
180
184
|
/** The W3C trace context of the caller that started the run, to parent spans the handler starts. */
|
|
181
185
|
readonly trace: TraceContext | null;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Context for an effect that runs in the host actor cell (for example a Durable Object).
|
|
189
|
+
* `emit` appends live so subscribers see events as they happen.
|
|
190
|
+
*/
|
|
191
|
+
export interface LocalEffectContext extends EffectContextBase {
|
|
192
|
+
readonly placement: 'local';
|
|
193
|
+
/** No-op locally; present so local handlers can call the same progress API as workers. */
|
|
194
|
+
heartbeat(details?: JsonValue): Promise<void>;
|
|
195
|
+
/** Appended to the run log immediately. */
|
|
182
196
|
emit(event: EventInput): Promise<void>;
|
|
183
197
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
readonly
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
198
|
+
/**
|
|
199
|
+
* Context for an effect that runs on an external effect worker (for example a Cloudflare Queue consumer).
|
|
200
|
+
* `emit` is buffered until the attempt completes — not a live stream.
|
|
201
|
+
*/
|
|
202
|
+
export interface WorkerEffectContext extends EffectContextBase {
|
|
203
|
+
readonly placement: 'worker';
|
|
204
|
+
/** Extends the heartbeat deadline on the host. */
|
|
205
|
+
heartbeat(details?: JsonValue): Promise<void>;
|
|
206
|
+
/** Buffered until `complete`; subscribers do not see the event until then. */
|
|
207
|
+
emit(event: EventInput): Promise<void>;
|
|
208
|
+
}
|
|
209
|
+
/** Either placement's context. Prefer {@link LocalEffectContext} or {@link WorkerEffectContext}. */
|
|
210
|
+
export type EffectContext = LocalEffectContext | WorkerEffectContext;
|
|
211
|
+
export interface ScopedLocalEffectContext<E extends EventInput = EventInput> extends Omit<LocalEffectContext, 'emit'> {
|
|
193
212
|
emit(event: E): Promise<void>;
|
|
194
213
|
}
|
|
214
|
+
/** @deprecated Use {@link ScopedLocalEffectContext}. */
|
|
215
|
+
export type ScopedEffectContext<E extends EventInput = EventInput> = ScopedLocalEffectContext<E>;
|
|
195
216
|
export type EffectInputSchema = SchemaInput;
|
|
196
217
|
declare const EffectHandlerError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
|
|
197
218
|
readonly _tag: "EffectHandlerError";
|
|
@@ -226,47 +247,71 @@ export declare function failEffect(options: EffectFailureOptions): Effect.Effect
|
|
|
226
247
|
export declare function readEffectFailure(cause: unknown): EffectFailure | null;
|
|
227
248
|
export type EffectHandlerResult<R = never, E extends EventInput = EventInput, HandlerError = never> = ReadonlyArray<E> | Promise<ReadonlyArray<E>> | Effect.Effect<ReadonlyArray<E>, HandlerError, R>;
|
|
228
249
|
declare const effectInputType: unique symbol;
|
|
229
|
-
|
|
230
|
-
|
|
250
|
+
declare const effectEventType: unique symbol;
|
|
251
|
+
/** Where an effect attempt runs. Declared with `placement` on `m.effect` / {@link defineEffect}. */
|
|
252
|
+
export type EffectPlacement = 'local' | 'worker';
|
|
253
|
+
/** @deprecated Use {@link EffectPlacement}. */
|
|
254
|
+
export type EffectExecution = EffectPlacement;
|
|
255
|
+
interface EffectDefinitionFields<TInput> {
|
|
231
256
|
readonly type: string;
|
|
232
257
|
readonly input?: SchemaInput;
|
|
233
258
|
readonly retry?: RetryPolicy;
|
|
234
|
-
/** Forces where the effect runs. When omitted, the runtime decides from `hasHandler` and the configured worker. */
|
|
235
|
-
readonly execution?: EffectExecution;
|
|
236
|
-
/** Whether `execute` runs a user handler. Without one, the effect can only complete through a worker. */
|
|
237
|
-
readonly hasHandler: boolean;
|
|
238
259
|
/**
|
|
239
260
|
* The outcome follows from the input alone, like scheduling from state already folded. Forking
|
|
240
261
|
* a failed run retries the effect that produced the failure, not a deterministic one passing it on.
|
|
241
262
|
*/
|
|
242
263
|
readonly deterministic?: boolean;
|
|
243
264
|
readonly [effectInputType]?: (input: TInput) => TInput;
|
|
244
|
-
execute(input: JsonValue, ctx: EffectContext): Effect.Effect<ReadonlyArray<E>, InvalidInputError | EffectHandlerError, R>;
|
|
245
265
|
}
|
|
246
266
|
/**
|
|
247
|
-
*
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
267
|
+
* An effect that runs in the host actor cell. Requires `execute`. Live `emit` / local middleware apply.
|
|
268
|
+
*/
|
|
269
|
+
export interface LocalEffectDefinition<TInput = JsonValue, R = Context.Service.Any, E extends EventInput = EventInput> extends EffectDefinitionFields<TInput> {
|
|
270
|
+
readonly placement: 'local';
|
|
271
|
+
execute(input: JsonValue, ctx: LocalEffectContext): Effect.Effect<ReadonlyArray<E>, InvalidInputError | EffectHandlerError, R>;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* An effect that only an external effect worker completes. Declare it with `placement: 'worker'`
|
|
275
|
+
* and implement the handler via `workerHandlers` (or an equivalent consumer). The host never runs it.
|
|
276
|
+
*/
|
|
277
|
+
export interface WorkerEffectDefinition<TInput = JsonValue, E extends EventInput = EventInput> extends EffectDefinitionFields<TInput> {
|
|
278
|
+
readonly placement: 'worker';
|
|
279
|
+
readonly [effectEventType]?: E;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Either placement's definition. `R` is the host services a local handler needs; worker
|
|
283
|
+
* definitions never run on the host, so they contribute none.
|
|
284
|
+
*/
|
|
285
|
+
export type EffectDefinition<TInput = JsonValue, R = Context.Service.Any, E extends EventInput = EventInput> = LocalEffectDefinition<TInput, R, E> | WorkerEffectDefinition<TInput, E>;
|
|
286
|
+
export declare function isLocalEffectDefinition(definition: EffectDefinition): definition is LocalEffectDefinition;
|
|
287
|
+
export declare function isWorkerEffectDefinition(definition: EffectDefinition): definition is WorkerEffectDefinition;
|
|
288
|
+
/**
|
|
289
|
+
* `execute` is required for `placement: 'local'` and rejected for `placement: 'worker'`. A worker
|
|
290
|
+
* handler lives with the worker (`workerHandlers`), so the host bundle never imports its
|
|
291
|
+
* dependencies.
|
|
292
|
+
*/
|
|
293
|
+
export type PlacementHandler<P extends EffectPlacement, THandler> = P extends 'local' ? {
|
|
294
|
+
readonly execute: THandler;
|
|
295
|
+
} : {
|
|
296
|
+
readonly execute?: 'Worker effects have no execute; implement the handler with workerHandlers';
|
|
297
|
+
};
|
|
298
|
+
/** The definition type `defineEffect` / `m.effect` return for a given placement. */
|
|
299
|
+
export type PlacedEffectDefinition<P extends EffectPlacement, TInput, R, E extends EventInput> = P extends 'local' ? LocalEffectDefinition<TInput, R, E> : WorkerEffectDefinition<TInput, E>;
|
|
300
|
+
interface EffectDefinitionInput<TPlacement extends EffectPlacement, TSchema> {
|
|
301
|
+
readonly type: string;
|
|
302
|
+
readonly placement: TPlacement;
|
|
303
|
+
readonly input?: TSchema;
|
|
304
|
+
readonly retry?: RetryPolicy;
|
|
305
|
+
readonly deterministic?: boolean;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Low-level effect constructor without a module namespace. Inside a module, prefer the scoped
|
|
309
|
+
* `m.effect` / `scope.effect`, which also checks the type prefix and the catalog.
|
|
251
310
|
*
|
|
252
|
-
*
|
|
311
|
+
* `placement: 'local'` runs `execute` in the host actor cell with live `emit`.
|
|
312
|
+
* `placement: 'worker'` has no `execute`; implement it on the worker with `workerHandlers`.
|
|
253
313
|
*/
|
|
254
|
-
export declare function defineEffect<TSchema extends SchemaInput, R = never, E extends EventInput = EventInput, HandlerError = never>(def:
|
|
255
|
-
type: string;
|
|
256
|
-
input: TSchema;
|
|
257
|
-
retry?: RetryPolicy;
|
|
258
|
-
execution?: EffectExecution;
|
|
259
|
-
deterministic?: boolean;
|
|
260
|
-
execute?: (input: InferDefinedSchema<TSchema>, ctx: EffectContext) => EffectHandlerResult<R, E, HandlerError>;
|
|
261
|
-
}): EffectDefinition<InferDefinedSchema<TSchema>, R, E>;
|
|
262
|
-
export declare function defineEffect<R = never, E extends EventInput = EventInput, HandlerError = never>(def: {
|
|
263
|
-
type: string;
|
|
264
|
-
input?: undefined;
|
|
265
|
-
retry?: RetryPolicy;
|
|
266
|
-
execution?: EffectExecution;
|
|
267
|
-
deterministic?: boolean;
|
|
268
|
-
execute?: (input: JsonValue, ctx: EffectContext) => EffectHandlerResult<R, E, HandlerError>;
|
|
269
|
-
}): EffectDefinition<JsonValue, R, E>;
|
|
314
|
+
export declare function defineEffect<const P extends EffectPlacement, TSchema extends SchemaInput | undefined = undefined, R = never, E extends EventInput = EventInput, HandlerError = never>(def: EffectDefinitionInput<P, TSchema> & PlacementHandler<P, (input: InferDefinedSchema<TSchema>, ctx: LocalEffectContext) => EffectHandlerResult<R, E, HandlerError>>): PlacedEffectDefinition<P, InferDefinedSchema<TSchema>, R, E>;
|
|
270
315
|
export declare function spawn(args: {
|
|
271
316
|
childThreadId?: string;
|
|
272
317
|
kind: string;
|
package/dist/core/effects.js
CHANGED
|
@@ -137,6 +137,12 @@ export function readEffectFailure(cause) {
|
|
|
137
137
|
}
|
|
138
138
|
return null;
|
|
139
139
|
}
|
|
140
|
+
export function isLocalEffectDefinition(definition) {
|
|
141
|
+
return definition.placement === 'local';
|
|
142
|
+
}
|
|
143
|
+
export function isWorkerEffectDefinition(definition) {
|
|
144
|
+
return definition.placement === 'worker';
|
|
145
|
+
}
|
|
140
146
|
function liftHandlerResult(result) {
|
|
141
147
|
if (Effect.isEffect(result)) {
|
|
142
148
|
return result.pipe(Effect.mapError((cause) => new EffectHandlerError(cause)));
|
|
@@ -157,27 +163,42 @@ function invokeHandler(handler, input, ctx) {
|
|
|
157
163
|
return Effect.fail(new EffectHandlerError(cause));
|
|
158
164
|
}
|
|
159
165
|
}
|
|
160
|
-
|
|
166
|
+
function defineLocalEffect(def) {
|
|
161
167
|
const schema = def.input;
|
|
162
168
|
const handler = def.execute;
|
|
163
|
-
const noOutput = [];
|
|
164
169
|
return {
|
|
165
170
|
type: def.type,
|
|
166
171
|
input: schema,
|
|
167
172
|
retry: def.retry,
|
|
168
|
-
|
|
169
|
-
hasHandler: handler !== undefined,
|
|
173
|
+
placement: 'local',
|
|
170
174
|
deterministic: def.deterministic,
|
|
171
175
|
execute: (raw, ctx) => Effect.gen(function* () {
|
|
172
176
|
const input = schema ? yield* validateInputEffect(schema, raw) : raw;
|
|
173
|
-
if (!handler) {
|
|
174
|
-
return noOutput;
|
|
175
|
-
}
|
|
176
|
-
// invokeHandler turns a synchronous throw into EffectHandlerError.
|
|
177
177
|
return yield* invokeHandler(handler, input, ctx);
|
|
178
178
|
}),
|
|
179
179
|
};
|
|
180
180
|
}
|
|
181
|
+
function defineWorkerEffect(def) {
|
|
182
|
+
return {
|
|
183
|
+
type: def.type,
|
|
184
|
+
input: def.input,
|
|
185
|
+
retry: def.retry,
|
|
186
|
+
placement: 'worker',
|
|
187
|
+
deterministic: def.deterministic,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
export function defineEffect(def) {
|
|
191
|
+
switch (def.placement) {
|
|
192
|
+
case 'local':
|
|
193
|
+
return defineLocalEffect(def);
|
|
194
|
+
case 'worker':
|
|
195
|
+
return defineWorkerEffect(def);
|
|
196
|
+
default: {
|
|
197
|
+
const exhaustive = def;
|
|
198
|
+
return exhaustive;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
181
202
|
export function spawn(args) {
|
|
182
203
|
const childThreadId = args.childThreadId ?? '';
|
|
183
204
|
return {
|
package/dist/core/fold.js
CHANGED
|
@@ -249,6 +249,7 @@ function applyProtocol(state, event, registry) {
|
|
|
249
249
|
lastHeartbeatAt: null,
|
|
250
250
|
heartbeatDetails: priorHeartbeatDetails(state, effectId),
|
|
251
251
|
lastError: null,
|
|
252
|
+
dispatchTry: 0,
|
|
252
253
|
},
|
|
253
254
|
},
|
|
254
255
|
};
|
|
@@ -304,6 +305,38 @@ function applyProtocol(state, event, registry) {
|
|
|
304
305
|
: null,
|
|
305
306
|
heartbeatDetails: nextHeartbeatDetails(state, effectId, event, payload),
|
|
306
307
|
lastError: readString(payload, 'error') ?? null,
|
|
308
|
+
dispatchTry: sameAttempt ? previous.dispatchTry : 0,
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
case 'runtime.effect.dispatch.deferred': {
|
|
314
|
+
const effectId = readString(payload, 'effectId');
|
|
315
|
+
const attempt = payload.attempt;
|
|
316
|
+
const retryAt = payload.retryAt;
|
|
317
|
+
const dispatchTry = payload.try;
|
|
318
|
+
if (!effectId ||
|
|
319
|
+
!Predicate.isNumber(attempt) ||
|
|
320
|
+
!Predicate.isNumber(retryAt) ||
|
|
321
|
+
!Predicate.isNumber(dispatchTry)) {
|
|
322
|
+
return state;
|
|
323
|
+
}
|
|
324
|
+
const previous = state.effectExecutions[effectId];
|
|
325
|
+
return {
|
|
326
|
+
...state,
|
|
327
|
+
effectExecutions: {
|
|
328
|
+
...state.effectExecutions,
|
|
329
|
+
[effectId]: {
|
|
330
|
+
effectId,
|
|
331
|
+
attempt,
|
|
332
|
+
status: 'dispatch_wait',
|
|
333
|
+
nextAttemptAt: retryAt,
|
|
334
|
+
deadlineAt: retryAt,
|
|
335
|
+
startedAt: null,
|
|
336
|
+
lastHeartbeatAt: null,
|
|
337
|
+
heartbeatDetails: previous?.heartbeatDetails ?? null,
|
|
338
|
+
lastError: readString(payload, 'error') ?? 'worker dispatch deferred',
|
|
339
|
+
dispatchTry,
|
|
307
340
|
},
|
|
308
341
|
},
|
|
309
342
|
};
|
|
@@ -333,6 +366,7 @@ function applyProtocol(state, event, registry) {
|
|
|
333
366
|
lastHeartbeatAt: null,
|
|
334
367
|
heartbeatDetails: priorHeartbeatDetails(state, effectId),
|
|
335
368
|
lastError: readString(payload, 'error') ?? 'effect failed',
|
|
369
|
+
dispatchTry: 0,
|
|
336
370
|
},
|
|
337
371
|
},
|
|
338
372
|
};
|