effect-cf 0.40.0 → 0.40.1
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 +8 -0
- package/dist/AiGateway.d.mts +86 -0
- package/dist/AiGateway.mjs +178 -0
- package/dist/AnalyticsEngine.d.mts +170 -0
- package/dist/AnalyticsEngine.mjs +275 -0
- package/dist/{Artifacts-B3r3RuTB.d.mts → Artifacts.d.mts} +8 -5
- package/dist/{Artifacts-CBxWGeZB.mjs → Artifacts.mjs} +9 -5
- package/dist/Binding.d.mts +45 -0
- package/dist/{Binding-C82jRhdr.mjs → Binding.mjs} +8 -4
- package/dist/BrowserRendering.d.mts +85 -0
- package/dist/BrowserRendering.mjs +81 -0
- package/dist/Cache.d.mts +40 -0
- package/dist/Cache.mjs +51 -0
- package/dist/CloudflareOtlp.d.mts +95 -0
- package/dist/CloudflareOtlp.mjs +125 -0
- package/dist/CloudflareTracer.d.mts +20 -0
- package/dist/CloudflareTracer.mjs +69 -0
- package/dist/ComputerArtifacts.d.mts +2 -2
- package/dist/ComputerArtifacts.mjs +3 -2
- package/dist/ComputerWorkspace.d.mts +36 -31
- package/dist/ComputerWorkspace.mjs +8 -3
- package/dist/ComputerWorkspaceHost.d.mts +1 -1
- package/dist/ComputerWorkspaceHost.mjs +1 -1
- package/dist/{ContainerNamespace-DbLaeFhN.d.mts → ContainerNamespace.d.mts} +16 -12
- package/dist/ContainerNamespace.mjs +165 -0
- package/dist/D1.d.mts +33 -0
- package/dist/D1.mjs +32 -0
- package/dist/DurableObject.d.mts +103 -0
- package/dist/DurableObject.mjs +77 -0
- package/dist/DurableObjectAlarm.d.mts +187 -0
- package/dist/DurableObjectAlarm.mjs +344 -0
- package/dist/DurableObjectDefinition.d.mts +87 -0
- package/dist/DurableObjectDefinition.mjs +125 -0
- package/dist/DurableObjectNamespace.d.mts +154 -0
- package/dist/DurableObjectNamespace.mjs +144 -0
- package/dist/DurableObjectRpcWebSocket.d.mts +112 -0
- package/dist/DurableObjectRpcWebSocket.mjs +547 -0
- package/dist/DurableObjectSqlite.d.mts +11 -0
- package/dist/DurableObjectSqlite.mjs +16 -0
- package/dist/DurableObjectState.d.mts +65 -0
- package/dist/DurableObjectState.mjs +47 -0
- package/dist/DurableObjectStorage.d.mts +112 -0
- package/dist/{DurableObjectState-DLL0YgBd.mjs → DurableObjectStorage.mjs} +19 -157
- package/dist/DurableObjectWebSocket.d.mts +83 -0
- package/dist/DurableObjectWebSocket.mjs +120 -0
- package/dist/Email.d.mts +106 -0
- package/dist/Email.mjs +247 -0
- package/dist/Environment.d.mts +59 -0
- package/dist/{ErrorMessage-Byx2OgNd.mjs → Environment.mjs} +7 -18
- package/dist/{Hyperdrive-HrwFlZ6x.d.mts → Hyperdrive.d.mts} +8 -5
- package/dist/Hyperdrive.mjs +32 -0
- package/dist/HyperdrivePg.d.mts +4 -3
- package/dist/HyperdrivePg.mjs +3 -1
- package/dist/Images.d.mts +191 -0
- package/dist/Images.mjs +112 -0
- package/dist/Kv.d.mts +84 -0
- package/dist/Kv.mjs +132 -0
- package/dist/Mcp.d.mts +5 -3
- package/dist/Mcp.mjs +13 -4
- package/dist/Queue.d.mts +68 -0
- package/dist/Queue.mjs +54 -0
- package/dist/QueueBinding.d.mts +42 -0
- package/dist/QueueBinding.mjs +69 -0
- package/dist/QueueDefinition.d.mts +48 -0
- package/dist/QueueDefinition.mjs +63 -0
- package/dist/R2.d.mts +89 -0
- package/dist/R2.mjs +117 -0
- package/dist/Rpc.d.mts +29 -0
- package/dist/Rpc.mjs +28 -0
- package/dist/RpcDefinition.d.mts +116 -0
- package/dist/RpcDefinition.mjs +184 -0
- package/dist/RpcTracing.d.mts +52 -0
- package/dist/RpcTracing.mjs +77 -0
- package/dist/Sandbox.d.mts +10 -5
- package/dist/Sandbox.mjs +9 -3
- package/dist/ServiceBinding.d.mts +112 -0
- package/dist/ServiceBinding.mjs +145 -0
- package/dist/Vectorize.d.mts +68 -0
- package/dist/Vectorize.mjs +76 -0
- package/dist/Vitest.d.mts +6 -4
- package/dist/Vitest.mjs +8 -4
- package/dist/WebTransport.d.mts +66 -0
- package/dist/WebTransport.mjs +63 -0
- package/dist/Worker.d.mts +139 -0
- package/dist/Worker.mjs +141 -0
- package/dist/WorkerDefinition.d.mts +100 -0
- package/dist/WorkerDefinition.mjs +103 -0
- package/dist/WorkersAi.d.mts +84 -0
- package/dist/WorkersAi.mjs +84 -0
- package/dist/Workflow.d.mts +91 -0
- package/dist/Workflow.mjs +139 -0
- package/dist/{WorkflowBinding-DtVykUCz.d.mts → WorkflowBinding.d.mts} +14 -10
- package/dist/WorkflowBinding.mjs +100 -0
- package/dist/WorkflowDefinition.d.mts +51 -0
- package/dist/WorkflowDefinition.mjs +69 -0
- package/dist/{rolldown-runtime-D7D4PA-g.mjs → _virtual/_rolldown/runtime.mjs} +1 -1
- package/dist/index.d.mts +40 -1839
- package/dist/index.mjs +40 -3161
- package/dist/{ComputerWorkspaceHostRegistry-CY5Fv4Ng.mjs → internal/ComputerWorkspaceHostRegistry.mjs} +1 -1
- package/dist/internal/Entrypoint.mjs +37 -0
- package/dist/internal/ErrorMessage.mjs +17 -0
- package/dist/internal/NativeCallback.mjs +33 -0
- package/dist/internal/RpcInvocation.d.mts +13 -0
- package/dist/internal/RpcInvocation.mjs +34 -0
- package/dist/internal/Runtime.mjs +31 -0
- package/dist/internal/Telemetry.mjs +20 -0
- package/dist/{WorkerContext-Yobnfw-K.mjs → internal/WorkerContext.mjs} +4 -31
- package/package.json +1 -1
- package/src/AiGateway.ts +5 -1
- package/src/AnalyticsEngine.ts +14 -19
- package/src/Artifacts.ts +6 -1
- package/src/Binding.ts +5 -1
- package/src/BrowserRendering.ts +5 -1
- package/src/Cache.ts +5 -1
- package/src/CloudflareOtlp.ts +12 -10
- package/src/CloudflareTracer.ts +7 -1
- package/src/ComputerArtifacts.ts +2 -1
- package/src/ComputerWorkspace.ts +7 -1
- package/src/ContainerNamespace.ts +6 -1
- package/src/D1.ts +4 -1
- package/src/DurableObject.ts +4 -1
- package/src/DurableObjectAlarm.ts +10 -12
- package/src/DurableObjectDefinition.ts +3 -1
- package/src/DurableObjectNamespace.ts +4 -1
- package/src/DurableObjectRpcWebSocket.ts +7 -1
- package/src/DurableObjectSqlite.ts +2 -1
- package/src/DurableObjectState.ts +4 -1
- package/src/DurableObjectStorage.ts +11 -13
- package/src/DurableObjectWebSocket.ts +5 -1
- package/src/Email.ts +5 -1
- package/src/Environment.ts +6 -1
- package/src/Hyperdrive.ts +4 -1
- package/src/HyperdrivePg.ts +3 -1
- package/src/Images.ts +7 -1
- package/src/Kv.ts +7 -1
- package/src/Mcp.ts +13 -13
- package/src/Queue.ts +3 -1
- package/src/QueueBinding.ts +5 -1
- package/src/QueueDefinition.ts +4 -1
- package/src/R2.ts +7 -1
- package/src/Rpc.ts +4 -1
- package/src/RpcDefinition.ts +4 -1
- package/src/RpcTracing.ts +9 -1
- package/src/Sandbox.ts +7 -1
- package/src/ServiceBinding.ts +6 -1
- package/src/Vectorize.ts +7 -1
- package/src/Vitest.ts +5 -1
- package/src/WebTransport.ts +6 -1
- package/src/Worker.ts +10 -16
- package/src/WorkerDefinition.ts +3 -1
- package/src/WorkersAi.ts +7 -1
- package/src/Workflow.ts +8 -11
- package/src/WorkflowBinding.ts +6 -1
- package/src/WorkflowDefinition.ts +4 -1
- package/src/internal/Entrypoint.ts +3 -1
- package/src/internal/ErrorMessage.ts +1 -1
- package/src/internal/NativeCallback.ts +3 -1
- package/src/internal/RpcInvocation.ts +5 -1
- package/src/internal/Runtime.ts +5 -1
- package/src/internal/Telemetry.ts +4 -3
- package/src/internal/WorkerContext.ts +3 -1
- package/dist/Binding-DKcLrQCM.d.mts +0 -94
- package/dist/DurableObjectState-DJ0K-zyd.d.mts +0 -244
- package/dist/Worker-XsGJsCJX.mjs +0 -885
- package/dist/Worker-gV4mDqIs.d.mts +0 -641
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { WorkerEnvironment } from "./Environment.mjs";
|
|
2
|
+
import { BindingNotFoundError, BindingValidationError } from "./Binding.mjs";
|
|
3
|
+
import * as Context$1 from "effect/Context";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as S from "effect/Schema";
|
|
6
|
+
import { Layer } from "effect";
|
|
7
|
+
declare namespace WorkersAi_d_exports {
|
|
8
|
+
export { LayerOptions, Tag, TagClass, WorkersAiAsyncBatchResponse, WorkersAiBinding, WorkersAiClient, WorkersAiDefinition, WorkersAiEmbeddingInput, WorkersAiEmbeddingResponse, WorkersAiModelsSearchObject, WorkersAiModelsSearchParams, WorkersAiOperationError, WorkersAiOptions, WorkersAiService, embeddingResponse, isWorkersAiBinding, layer, make, makeClient };
|
|
9
|
+
}
|
|
10
|
+
declare const WorkersAiOperationError_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 & {
|
|
11
|
+
readonly _tag: "WorkersAiOperationError";
|
|
12
|
+
} & Readonly<A>;
|
|
13
|
+
declare class WorkersAiOperationError extends WorkersAiOperationError_base<{
|
|
14
|
+
readonly binding: string;
|
|
15
|
+
readonly operation: string;
|
|
16
|
+
readonly cause: unknown;
|
|
17
|
+
}> {
|
|
18
|
+
get message(): string;
|
|
19
|
+
}
|
|
20
|
+
interface WorkersAiDefinition {
|
|
21
|
+
readonly binding: string;
|
|
22
|
+
}
|
|
23
|
+
type WorkersAiBinding<ModelList extends AiModelListType = AiModels> = CloudflareAi<ModelList>;
|
|
24
|
+
type WorkersAiOptions = AiOptions;
|
|
25
|
+
type WorkersAiModelsSearchParams = AiModelsSearchParams;
|
|
26
|
+
type WorkersAiModelsSearchObject = AiModelsSearchObject;
|
|
27
|
+
type WorkersAiAsyncBatchResponse = AiAsyncBatchResponse;
|
|
28
|
+
type WorkersAiEmbeddingInput = AiTextEmbeddingsInput | Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Input | Parameters<WorkersAiBinding["run"]>[1];
|
|
29
|
+
declare const embeddingDimensionsKey = "shape";
|
|
30
|
+
interface WorkersAiEmbeddingResponse {
|
|
31
|
+
readonly data: ReadonlyArray<ReadonlyArray<number>>;
|
|
32
|
+
readonly [embeddingDimensionsKey]: ReadonlyArray<number>;
|
|
33
|
+
}
|
|
34
|
+
interface WorkersAiClient<ModelList extends AiModelListType = AiModels> {
|
|
35
|
+
readonly aiGatewayLogId: Effect.Effect<string | null, WorkersAiOperationError>;
|
|
36
|
+
readonly run: {
|
|
37
|
+
<Name extends keyof ModelList>(model: Name, input: {
|
|
38
|
+
readonly requests: ReadonlyArray<ModelList[Name]["inputs"]>;
|
|
39
|
+
}, options: WorkersAiOptions & {
|
|
40
|
+
readonly queueRequest: true;
|
|
41
|
+
}): Effect.Effect<WorkersAiAsyncBatchResponse, WorkersAiOperationError>;
|
|
42
|
+
<Name extends keyof ModelList>(model: Name, input: ModelList[Name]["inputs"], options: WorkersAiOptions & {
|
|
43
|
+
readonly returnRawResponse: true;
|
|
44
|
+
}): Effect.Effect<Response, WorkersAiOperationError>;
|
|
45
|
+
<Name extends keyof ModelList>(model: Name, input: ModelList[Name]["inputs"], options: WorkersAiOptions & {
|
|
46
|
+
readonly websocket: true;
|
|
47
|
+
}): Effect.Effect<Response, WorkersAiOperationError>;
|
|
48
|
+
<Name extends keyof ModelList>(model: Name, input: ModelList[Name]["inputs"] & {
|
|
49
|
+
readonly stream: true;
|
|
50
|
+
}, options?: WorkersAiOptions): Effect.Effect<ReadableStream, WorkersAiOperationError>;
|
|
51
|
+
<Name extends keyof ModelList>(model: Name, input: ModelList[Name]["inputs"], options?: WorkersAiOptions): Effect.Effect<ModelList[Name]["postProcessedOutputs"], WorkersAiOperationError>;
|
|
52
|
+
<Input extends Parameters<WorkersAiBinding["run"]>[1], Output = Awaited<ReturnType<WorkersAiBinding["run"]>>>(model: string, input: Input, options?: WorkersAiOptions): Effect.Effect<Output, WorkersAiOperationError>;
|
|
53
|
+
};
|
|
54
|
+
readonly runEmbedding: <Input extends WorkersAiEmbeddingInput>(model: string, input: Input, options?: WorkersAiOptions) => Effect.Effect<WorkersAiEmbeddingResponse, WorkersAiOperationError>;
|
|
55
|
+
readonly models: (params?: WorkersAiModelsSearchParams) => Effect.Effect<ReadonlyArray<WorkersAiModelsSearchObject>, WorkersAiOperationError>;
|
|
56
|
+
readonly gateway: (gatewayId: string) => Effect.Effect<CloudflareAiGateway, WorkersAiOperationError>;
|
|
57
|
+
readonly rawUnsafe: Effect.Effect<WorkersAiBinding<ModelList>>;
|
|
58
|
+
readonly definition: WorkersAiDefinition;
|
|
59
|
+
}
|
|
60
|
+
declare const WorkersAiServiceTypeId: unique symbol;
|
|
61
|
+
interface WorkersAiService<Id extends string> {
|
|
62
|
+
readonly [WorkersAiServiceTypeId]: {
|
|
63
|
+
readonly id: Id;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
type LayerOptions = {
|
|
67
|
+
readonly binding: string;
|
|
68
|
+
};
|
|
69
|
+
interface TagClass<Self, Id extends string, ModelList extends AiModelListType = AiModels> extends Context$1.ServiceClass<Self, Id, WorkersAiClient<ModelList>> {
|
|
70
|
+
readonly id: Id;
|
|
71
|
+
readonly layer: (options: LayerOptions) => Layer.Layer<Self, BindingNotFoundError | BindingValidationError, WorkerEnvironment>;
|
|
72
|
+
}
|
|
73
|
+
type WorkersAiBindingValue = S.Schema.Type<typeof S.Unknown>;
|
|
74
|
+
declare const isWorkersAiBinding: (value: WorkersAiBindingValue) => value is WorkersAiBinding;
|
|
75
|
+
declare const embeddingResponse: (value: {
|
|
76
|
+
readonly data?: ReadonlyArray<ReadonlyArray<number>>;
|
|
77
|
+
readonly [embeddingDimensionsKey]?: ReadonlyArray<number>;
|
|
78
|
+
}) => WorkersAiEmbeddingResponse;
|
|
79
|
+
declare const makeClient: <ModelList extends AiModelListType = AiModels>(definition: WorkersAiDefinition) => (ai: WorkersAiBinding<ModelList>) => WorkersAiClient<ModelList>;
|
|
80
|
+
declare const layer: <Self, ModelList extends AiModelListType = AiModels>(tag: Context$1.Service<Self, WorkersAiClient<ModelList>>, definition: WorkersAiDefinition) => Layer.Layer<Self, BindingNotFoundError | BindingValidationError, WorkerEnvironment>;
|
|
81
|
+
declare const make: <Id extends string>(id: Id) => TagClass<WorkersAiService<Id>, Id, AiModels>;
|
|
82
|
+
declare const Tag: <Self>() => <Id extends string, ModelList extends AiModelListType = AiModels>(id: Id) => TagClass<Self, Id, ModelList>;
|
|
83
|
+
//#endregion
|
|
84
|
+
export { LayerOptions, Tag, TagClass, WorkersAiAsyncBatchResponse, WorkersAiBinding, WorkersAiClient, WorkersAiDefinition, WorkersAiEmbeddingInput, WorkersAiEmbeddingResponse, WorkersAiModelsSearchObject, WorkersAiModelsSearchParams, WorkersAiOperationError, WorkersAiOptions, WorkersAiService, WorkersAi_d_exports, embeddingResponse, isWorkersAiBinding, layer, make, makeClient };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { layer as layer$1 } from "./Binding.mjs";
|
|
3
|
+
import { causeMessage } from "./internal/ErrorMessage.mjs";
|
|
4
|
+
import * as Context from "effect/Context";
|
|
5
|
+
import * as Data from "effect/Data";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import * as S from "effect/Schema";
|
|
8
|
+
import * as Predicate from "effect/Predicate";
|
|
9
|
+
import * as Option from "effect/Option";
|
|
10
|
+
//#region src/WorkersAi.ts
|
|
11
|
+
var WorkersAi_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
Tag: () => Tag,
|
|
13
|
+
WorkersAiOperationError: () => WorkersAiOperationError,
|
|
14
|
+
embeddingResponse: () => embeddingResponse,
|
|
15
|
+
isWorkersAiBinding: () => isWorkersAiBinding,
|
|
16
|
+
layer: () => layer,
|
|
17
|
+
make: () => make,
|
|
18
|
+
makeClient: () => makeClient
|
|
19
|
+
});
|
|
20
|
+
const expectedWorkersAiBinding = "Workers AI binding with run(), gateway(), and models()";
|
|
21
|
+
var WorkersAiOperationError = class extends Data.TaggedError("WorkersAiOperationError") {
|
|
22
|
+
get message() {
|
|
23
|
+
return `Workers AI ${this.operation} failed for binding "${this.binding}": ${causeMessage(this.cause)}`;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const embeddingDimensionsKey = "shape";
|
|
27
|
+
const workersAiError = (binding, operation, cause) => new WorkersAiOperationError({
|
|
28
|
+
binding,
|
|
29
|
+
operation,
|
|
30
|
+
cause
|
|
31
|
+
});
|
|
32
|
+
const tryWorkersAiPromise = (binding, operation, evaluate) => Effect.tryPromise({
|
|
33
|
+
try: evaluate,
|
|
34
|
+
catch: (cause) => workersAiError(binding, operation, cause)
|
|
35
|
+
});
|
|
36
|
+
const tryWorkersAiSync = (binding, operation, evaluate) => Effect.try({
|
|
37
|
+
try: evaluate,
|
|
38
|
+
catch: (cause) => workersAiError(binding, operation, cause)
|
|
39
|
+
});
|
|
40
|
+
const WorkersAiBindingSchema = S.declare((value) => Predicate.hasProperty(value, "run") && Predicate.isFunction(value.run) && Predicate.hasProperty(value, "gateway") && Predicate.isFunction(value.gateway) && Predicate.hasProperty(value, "models") && Predicate.isFunction(value.models));
|
|
41
|
+
const decodeWorkersAiBinding = S.decodeUnknownOption(WorkersAiBindingSchema);
|
|
42
|
+
const isWorkersAiBinding = (value) => Option.isSome(decodeWorkersAiBinding(value));
|
|
43
|
+
const embeddingResponse = (value) => ({
|
|
44
|
+
data: value.data ?? [],
|
|
45
|
+
[embeddingDimensionsKey]: value[embeddingDimensionsKey] ?? []
|
|
46
|
+
});
|
|
47
|
+
const decodeEmbeddingResponse = S.decodeUnknownEffect(S.Struct({
|
|
48
|
+
data: S.optional(S.Array(S.Array(S.Number))),
|
|
49
|
+
[embeddingDimensionsKey]: S.optional(S.Array(S.Number))
|
|
50
|
+
}));
|
|
51
|
+
const makeClient = (definition) => (ai) => {
|
|
52
|
+
const run = Effect.fn("WorkersAi.run")((model, input, options) => tryWorkersAiPromise(definition.binding, "run", () => {
|
|
53
|
+
return ai.run(model, input, options);
|
|
54
|
+
}));
|
|
55
|
+
return {
|
|
56
|
+
definition,
|
|
57
|
+
aiGatewayLogId: tryWorkersAiSync(definition.binding, "aiGatewayLogId", () => ai.aiGatewayLogId),
|
|
58
|
+
run,
|
|
59
|
+
runEmbedding: Effect.fn("WorkersAi.runEmbedding")(function* (model, input, options) {
|
|
60
|
+
const response = yield* run(model, input, options);
|
|
61
|
+
const decoded = yield* decodeEmbeddingResponse(response).pipe(Effect.mapError((cause) => new WorkersAiOperationError({
|
|
62
|
+
binding: definition.binding,
|
|
63
|
+
operation: "runEmbedding",
|
|
64
|
+
cause
|
|
65
|
+
})));
|
|
66
|
+
return embeddingResponse(decoded);
|
|
67
|
+
}),
|
|
68
|
+
models: Effect.fn("WorkersAi.models")((params) => tryWorkersAiPromise(definition.binding, "models", () => ai.models(params))),
|
|
69
|
+
gateway: (gatewayId) => tryWorkersAiSync(definition.binding, "gateway", () => ai.gateway(gatewayId)),
|
|
70
|
+
rawUnsafe: Effect.succeed(ai)
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const layer = (tag, definition) => layer$1(tag, definition.binding, (value) => isWorkersAiBinding(value), makeClient(definition), { expected: expectedWorkersAiBinding });
|
|
74
|
+
const make = (id) => Tag()(id);
|
|
75
|
+
const Tag = () => (id) => {
|
|
76
|
+
const tag = Context.Service()(id);
|
|
77
|
+
const makeLayer = (definition) => layer(tag, definition);
|
|
78
|
+
return Object.assign(tag, {
|
|
79
|
+
id,
|
|
80
|
+
layer: makeLayer
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
export { Tag, WorkersAiOperationError, WorkersAi_exports, embeddingResponse, isWorkersAiBinding, layer, make, makeClient };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { WorkerEnv, WorkerEnvironment } from "./Environment.mjs";
|
|
2
|
+
import { ExecutionContext, WorkerContext } from "./Worker.mjs";
|
|
3
|
+
import { Definition as Definition$1, Handler as Handler$1, LayerOptions as LayerOptions$1, Options as Options$1, TagClass as TagClass$1, implement as implement$1 } from "./WorkflowDefinition.mjs";
|
|
4
|
+
import * as Context$1 from "effect/Context";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Layer$1 from "effect/Layer";
|
|
7
|
+
import * as Cause$1 from "effect/Cause";
|
|
8
|
+
import { WorkflowEntrypoint, WorkflowEvent, WorkflowSleepDuration, WorkflowStep, WorkflowStepConfig, WorkflowStepContext, WorkflowStepEvent, WorkflowTimeoutDuration } from "cloudflare:workers";
|
|
9
|
+
import { Scope } from "effect";
|
|
10
|
+
declare namespace Workflow_d_exports {
|
|
11
|
+
export { Definition, Handler, LayerOptions, Options, Tag, TagClass, WorkflowClass, WorkflowEvent$1 as WorkflowEvent, WorkflowEventService, WorkflowHandler, WorkflowOptions, WorkflowRunContext, WorkflowStep$1 as WorkflowStep, WorkflowStepContext$1 as WorkflowStepContext, WorkflowStepError, WorkflowStepNonRetryableError, WorkflowStepService, implement, make, sleep, sleepUntil, step, waitForEvent };
|
|
12
|
+
}
|
|
13
|
+
interface WorkflowEventService<Payload = unknown> {
|
|
14
|
+
readonly raw: WorkflowEvent<unknown>;
|
|
15
|
+
readonly payload: Payload;
|
|
16
|
+
readonly timestamp: Date;
|
|
17
|
+
readonly instanceId: string;
|
|
18
|
+
readonly workflowName: string;
|
|
19
|
+
}
|
|
20
|
+
declare const WorkflowEvent_base: Context$1.ServiceClass<WorkflowEvent$1, "effect-cf/WorkflowEvent", WorkflowEventService<unknown>>;
|
|
21
|
+
declare class WorkflowEvent$1 extends WorkflowEvent_base {}
|
|
22
|
+
declare const WorkflowStepError_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]; }>) => Cause$1.YieldableError & {
|
|
23
|
+
readonly _tag: "WorkflowStepError";
|
|
24
|
+
} & Readonly<A>;
|
|
25
|
+
declare class WorkflowStepError extends WorkflowStepError_base<{
|
|
26
|
+
readonly step?: string;
|
|
27
|
+
readonly operation: string;
|
|
28
|
+
readonly cause: unknown;
|
|
29
|
+
}> {
|
|
30
|
+
get message(): string;
|
|
31
|
+
}
|
|
32
|
+
declare const WorkflowStepNonRetryableError_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]; }>) => Cause$1.YieldableError & {
|
|
33
|
+
readonly _tag: "WorkflowStepNonRetryableError";
|
|
34
|
+
} & Readonly<A>;
|
|
35
|
+
/**
|
|
36
|
+
* Typed step failure that Cloudflare must treat as terminal instead of retrying.
|
|
37
|
+
*
|
|
38
|
+
* The workflow boundary converts this value into Cloudflare's native
|
|
39
|
+
* `NonRetryableError` while preserving the original cause and its message.
|
|
40
|
+
*/
|
|
41
|
+
declare class WorkflowStepNonRetryableError extends WorkflowStepNonRetryableError_base<{
|
|
42
|
+
readonly cause: unknown;
|
|
43
|
+
}> {
|
|
44
|
+
get message(): string;
|
|
45
|
+
}
|
|
46
|
+
interface WorkflowStepService {
|
|
47
|
+
readonly raw: WorkflowStep;
|
|
48
|
+
do<A, E, R>(name: string, effect: Effect.Effect<A, E, R>, config?: WorkflowStepConfig): Effect.Effect<A, WorkflowStepError, Exclude<R, WorkflowStepContext$1>>;
|
|
49
|
+
readonly sleep: (name: string, duration: WorkflowSleepDuration) => Effect.Effect<void, WorkflowStepError>;
|
|
50
|
+
readonly sleepUntil: (name: string, timestamp: Date | number) => Effect.Effect<void, WorkflowStepError>;
|
|
51
|
+
readonly waitForEvent: <Payload>(name: string, options: {
|
|
52
|
+
readonly type: string;
|
|
53
|
+
readonly timeout?: WorkflowTimeoutDuration | number;
|
|
54
|
+
}) => Effect.Effect<WorkflowStepEvent<Payload>, WorkflowStepError>;
|
|
55
|
+
}
|
|
56
|
+
declare const WorkflowStep_base: Context$1.ServiceClass<WorkflowStep$1, "effect-cf/WorkflowStep", WorkflowStepService>;
|
|
57
|
+
declare class WorkflowStep$1 extends WorkflowStep_base {}
|
|
58
|
+
declare const WorkflowStepContext_base: Context$1.ServiceClass<WorkflowStepContext$1, "effect-cf/WorkflowStepContext", WorkflowStepContext>;
|
|
59
|
+
declare class WorkflowStepContext$1 extends WorkflowStepContext_base {}
|
|
60
|
+
type RuntimeContext<ROut> = ExecutionContext | WorkerContext | WorkerEnvironment | ROut;
|
|
61
|
+
type WorkflowRunContext<ROut> = RuntimeContext<ROut> | WorkflowEvent$1 | WorkflowStep$1 | Scope.Scope;
|
|
62
|
+
type WorkflowHandler<ROut, Payload = unknown, Result = unknown> = (payload: Payload) => Effect.Effect<Result, unknown, WorkflowRunContext<ROut>>;
|
|
63
|
+
interface WorkflowOptions<ROut, Payload = unknown, Result = unknown> {
|
|
64
|
+
readonly run: WorkflowHandler<ROut, Payload, Result>;
|
|
65
|
+
}
|
|
66
|
+
type WorkflowClass<Payload, Result, _ROut> = new (ctx: globalThis.ExecutionContext, env: WorkerEnv) => WorkflowEntrypoint<WorkerEnv, Payload> & {
|
|
67
|
+
run(event: Readonly<WorkflowEvent<Payload>>, step: WorkflowStep): Promise<Result>;
|
|
68
|
+
};
|
|
69
|
+
declare const make: <ROut, LayerError, Payload = unknown, Result = unknown>(layer: Layer$1.Layer<ROut, LayerError, ExecutionContext | WorkerContext | WorkerEnvironment>, options: WorkflowOptions<ROut, Payload, Result>) => WorkflowClass<Payload, Result, ROut>;
|
|
70
|
+
declare const step: <A, E, R>(name: string, effect: Effect.Effect<A, E, R>, config?: WorkflowStepConfig) => Effect.Effect<A, WorkflowStepError, WorkflowStep$1 | Exclude<R, WorkflowStepContext$1>>;
|
|
71
|
+
declare const sleep: (name: string, duration: WorkflowSleepDuration) => Effect.Effect<void, WorkflowStepError, WorkflowStep$1>;
|
|
72
|
+
declare const sleepUntil: (name: string, timestamp: Date | number) => Effect.Effect<void, WorkflowStepError, WorkflowStep$1>;
|
|
73
|
+
declare const waitForEvent: <Payload>(name: string, options: {
|
|
74
|
+
readonly type: string;
|
|
75
|
+
readonly timeout?: WorkflowTimeoutDuration | number;
|
|
76
|
+
}) => Effect.Effect<WorkflowStepEvent<Payload>, WorkflowStepError, WorkflowStep$1>;
|
|
77
|
+
type Definition<Id extends string = string, Payload extends Definition$1.Any["payload"] = Definition$1.Any["payload"], Result extends Definition$1.Any["result"] = Definition$1.Any["result"]> = Definition$1<Id, Payload, Result>;
|
|
78
|
+
declare namespace Definition {
|
|
79
|
+
type Any = Definition$1.Any;
|
|
80
|
+
}
|
|
81
|
+
type LayerOptions = LayerOptions$1;
|
|
82
|
+
type TagClass<Self, Id extends string, Payload extends Definition$1.Any["payload"], Result extends Definition$1.Any["result"]> = TagClass$1<Self, Id, Payload, Result>;
|
|
83
|
+
declare const Tag: <Self>() => <Id extends string, Payload extends Definition$1.Any["payload"], Result extends Definition$1.Any["result"]>(id: Id, definition: {
|
|
84
|
+
readonly payload: Payload;
|
|
85
|
+
readonly result: Result;
|
|
86
|
+
}) => TagClass<Self, Id, Payload, Result>;
|
|
87
|
+
declare const implement: typeof implement$1;
|
|
88
|
+
type Handler<ROut, Self extends Definition$1.Any> = Handler$1<ROut, Self>;
|
|
89
|
+
type Options<ROut, Self extends Definition$1.Any> = Options$1<ROut, Self>;
|
|
90
|
+
//#endregion
|
|
91
|
+
export { Definition, Handler, LayerOptions, Options, Tag, TagClass, WorkflowClass, WorkflowEvent$1 as WorkflowEvent, WorkflowEventService, WorkflowHandler, WorkflowOptions, WorkflowRunContext, WorkflowStep$1 as WorkflowStep, WorkflowStepContext$1 as WorkflowStepContext, WorkflowStepError, WorkflowStepNonRetryableError, WorkflowStepService, Workflow_d_exports, implement, make, sleep, sleepUntil, step, waitForEvent };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { causeMessage } from "./internal/ErrorMessage.mjs";
|
|
3
|
+
import { fromExecutionContext } from "./internal/WorkerContext.mjs";
|
|
4
|
+
import { assumeEntrypointClass } from "./internal/Entrypoint.mjs";
|
|
5
|
+
import { makeEntrypointRuntime, runEventPromise } from "./internal/Runtime.mjs";
|
|
6
|
+
import { ExecutionContext, WorkerContext } from "./Worker.mjs";
|
|
7
|
+
import { Tag as Tag$1, implement as implement$1 } from "./WorkflowDefinition.mjs";
|
|
8
|
+
import * as Context from "effect/Context";
|
|
9
|
+
import * as Data from "effect/Data";
|
|
10
|
+
import * as Effect from "effect/Effect";
|
|
11
|
+
import * as Layer from "effect/Layer";
|
|
12
|
+
import * as Option from "effect/Option";
|
|
13
|
+
import * as Cause from "effect/Cause";
|
|
14
|
+
import * as FiberSet from "effect/FiberSet";
|
|
15
|
+
import { WorkflowEntrypoint } from "cloudflare:workers";
|
|
16
|
+
import { NonRetryableError } from "cloudflare:workflows";
|
|
17
|
+
//#region src/Workflow.ts
|
|
18
|
+
var Workflow_exports = /* @__PURE__ */ __exportAll({
|
|
19
|
+
Tag: () => Tag,
|
|
20
|
+
WorkflowEvent: () => WorkflowEvent$1,
|
|
21
|
+
WorkflowStep: () => WorkflowStep$1,
|
|
22
|
+
WorkflowStepContext: () => WorkflowStepContext$1,
|
|
23
|
+
WorkflowStepError: () => WorkflowStepError,
|
|
24
|
+
WorkflowStepNonRetryableError: () => WorkflowStepNonRetryableError,
|
|
25
|
+
implement: () => implement,
|
|
26
|
+
make: () => make,
|
|
27
|
+
sleep: () => sleep,
|
|
28
|
+
sleepUntil: () => sleepUntil,
|
|
29
|
+
step: () => step,
|
|
30
|
+
waitForEvent: () => waitForEvent
|
|
31
|
+
});
|
|
32
|
+
var WorkflowEvent$1 = class extends Context.Service()("effect-cf/WorkflowEvent") {};
|
|
33
|
+
var WorkflowStepError = class extends Data.TaggedError("WorkflowStepError") {
|
|
34
|
+
get message() {
|
|
35
|
+
const step = this.step === void 0 ? "" : ` in step "${this.step}"`;
|
|
36
|
+
return `Workflow ${this.operation} failed${step}: ${causeMessage(this.cause)}`;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Typed step failure that Cloudflare must treat as terminal instead of retrying.
|
|
41
|
+
*
|
|
42
|
+
* The workflow boundary converts this value into Cloudflare's native
|
|
43
|
+
* `NonRetryableError` while preserving the original cause and its message.
|
|
44
|
+
*/
|
|
45
|
+
var WorkflowStepNonRetryableError = class extends Data.TaggedError("WorkflowStepNonRetryableError") {
|
|
46
|
+
get message() {
|
|
47
|
+
return `Workflow step failed without retry: ${causeMessage(this.cause)}`;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var WorkflowStep$1 = class extends Context.Service()("effect-cf/WorkflowStep") {};
|
|
51
|
+
var WorkflowStepContext$1 = class extends Context.Service()("effect-cf/WorkflowStepContext") {};
|
|
52
|
+
const fromWorkflowEvent = (event) => ({
|
|
53
|
+
raw: event,
|
|
54
|
+
payload: event.payload,
|
|
55
|
+
timestamp: event.timestamp,
|
|
56
|
+
instanceId: event.instanceId,
|
|
57
|
+
workflowName: event.workflowName
|
|
58
|
+
});
|
|
59
|
+
const toCloudflareNonRetryableError = (error) => {
|
|
60
|
+
const cloudflareError = new NonRetryableError(error.message);
|
|
61
|
+
cloudflareError.cause = error.cause;
|
|
62
|
+
return cloudflareError;
|
|
63
|
+
};
|
|
64
|
+
const exposeCloudflareNonRetryableError = (effect) => Effect.catchCause(effect, (cause) => {
|
|
65
|
+
const error = Cause.findErrorOption(cause);
|
|
66
|
+
return Option.isSome(error) && error.value instanceof WorkflowStepNonRetryableError ? Effect.die(toCloudflareNonRetryableError(error.value)) : Effect.failCause(cause);
|
|
67
|
+
});
|
|
68
|
+
const fromWorkflowStep = (step) => ({
|
|
69
|
+
raw: step,
|
|
70
|
+
do: (name, effect, config) => Effect.gen(function* () {
|
|
71
|
+
const context = yield* Effect.context();
|
|
72
|
+
return yield* Effect.scoped(Effect.gen(function* () {
|
|
73
|
+
const runPromise = yield* FiberSet.makeRuntimePromise();
|
|
74
|
+
return yield* Effect.tryPromise({
|
|
75
|
+
try: () => {
|
|
76
|
+
const run = (stepContext) => {
|
|
77
|
+
const stepEffect = Effect.scoped(Effect.provideService(effect, WorkflowStepContext$1, stepContext));
|
|
78
|
+
return runPromise(exposeCloudflareNonRetryableError(Effect.provideContext(stepEffect, context)));
|
|
79
|
+
};
|
|
80
|
+
const rawStep = step;
|
|
81
|
+
return config === void 0 ? rawStep.do(name, run) : rawStep.do(name, config, run);
|
|
82
|
+
},
|
|
83
|
+
catch: (cause) => new WorkflowStepError({
|
|
84
|
+
step: name,
|
|
85
|
+
operation: "do",
|
|
86
|
+
cause
|
|
87
|
+
})
|
|
88
|
+
});
|
|
89
|
+
}));
|
|
90
|
+
}),
|
|
91
|
+
sleep: (name, duration) => Effect.tryPromise({
|
|
92
|
+
try: () => step.sleep(name, duration),
|
|
93
|
+
catch: (cause) => new WorkflowStepError({
|
|
94
|
+
step: name,
|
|
95
|
+
operation: "sleep",
|
|
96
|
+
cause
|
|
97
|
+
})
|
|
98
|
+
}),
|
|
99
|
+
sleepUntil: (name, timestamp) => Effect.tryPromise({
|
|
100
|
+
try: () => step.sleepUntil(name, timestamp),
|
|
101
|
+
catch: (cause) => new WorkflowStepError({
|
|
102
|
+
step: name,
|
|
103
|
+
operation: "sleepUntil",
|
|
104
|
+
cause
|
|
105
|
+
})
|
|
106
|
+
}),
|
|
107
|
+
waitForEvent: (name, options) => Effect.tryPromise({
|
|
108
|
+
try: () => {
|
|
109
|
+
return step.waitForEvent(name, options);
|
|
110
|
+
},
|
|
111
|
+
catch: (cause) => new WorkflowStepError({
|
|
112
|
+
step: name,
|
|
113
|
+
operation: "waitForEvent",
|
|
114
|
+
cause
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
});
|
|
118
|
+
const make = (layer, options) => {
|
|
119
|
+
class EffectWorkflow extends WorkflowEntrypoint {
|
|
120
|
+
runtime;
|
|
121
|
+
constructor(ctx, env) {
|
|
122
|
+
super(ctx, env);
|
|
123
|
+
this.runtime = makeEntrypointRuntime(layer, env, Layer.mergeAll(Layer.succeed(ExecutionContext, ctx), Layer.succeed(WorkerContext, fromExecutionContext(ctx))));
|
|
124
|
+
}
|
|
125
|
+
run(event, step) {
|
|
126
|
+
const workflowServices = Layer.mergeAll(Layer.succeed(WorkflowEvent$1, fromWorkflowEvent(event)), Layer.succeed(WorkflowStep$1, fromWorkflowStep(step)));
|
|
127
|
+
return runEventPromise(this.runtime, options.run(event.payload), workflowServices);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return assumeEntrypointClass(EffectWorkflow);
|
|
131
|
+
};
|
|
132
|
+
const step = (name, effect, config) => Effect.flatMap(WorkflowStep$1, (workflowStep) => workflowStep.do(name, effect, config));
|
|
133
|
+
const sleep = (name, duration) => Effect.flatMap(WorkflowStep$1, (workflowStep) => workflowStep.sleep(name, duration));
|
|
134
|
+
const sleepUntil = (name, timestamp) => Effect.flatMap(WorkflowStep$1, (workflowStep) => workflowStep.sleepUntil(name, timestamp));
|
|
135
|
+
const waitForEvent = (name, options) => Effect.flatMap(WorkflowStep$1, (workflowStep) => workflowStep.waitForEvent(name, options));
|
|
136
|
+
const Tag = Tag$1;
|
|
137
|
+
const implement = implement$1;
|
|
138
|
+
//#endregion
|
|
139
|
+
export { Tag, WorkflowEvent$1 as WorkflowEvent, WorkflowStep$1 as WorkflowStep, WorkflowStepContext$1 as WorkflowStepContext, WorkflowStepError, WorkflowStepNonRetryableError, Workflow_exports, implement, make, sleep, sleepUntil, step, waitForEvent };
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { WorkerEnvironment } from "./Environment.mjs";
|
|
2
|
+
import { BindingNotFoundError, BindingValidationError } from "./Binding.mjs";
|
|
3
|
+
import { ServiceFreeSchema } from "./RpcDefinition.mjs";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as S from "effect/Schema";
|
|
6
|
+
import * as Option from "effect/Option";
|
|
7
|
+
import { Context } from "effect";
|
|
4
8
|
declare namespace WorkflowBinding_d_exports {
|
|
5
9
|
export { WorkflowBindingClient, WorkflowBindingDefinition, WorkflowInstance, WorkflowInstanceCreateBatchOptions, WorkflowInstanceCreateOptions, WorkflowInstanceEvent, WorkflowInstanceLocationHint, WorkflowInstanceRestartOptions, WorkflowInstanceStatus, WorkflowInstanceStatusName, WorkflowInstanceTerminateOptions, WorkflowOperationError, WorkflowResultDecodeError, isWorkflow, layer, makeClient };
|
|
6
10
|
}
|
|
@@ -41,10 +45,10 @@ interface WorkflowBindingDefinition<Payload extends ServiceFreeSchema, Result ex
|
|
|
41
45
|
readonly result: Result;
|
|
42
46
|
}
|
|
43
47
|
interface WorkflowBindingClient<Payload extends ServiceFreeSchema, Result extends ServiceFreeSchema> {
|
|
44
|
-
readonly create: (payload:
|
|
45
|
-
readonly createBatch: (batch: WorkflowInstanceCreateBatchOptions<
|
|
46
|
-
readonly get: (instanceId: string) => Effect.Effect<WorkflowInstance<
|
|
47
|
-
readonly rawUnsafe: Effect.Effect<CloudflareWorkflow<
|
|
48
|
+
readonly create: (payload: S.Schema.Type<Payload>, options?: WorkflowInstanceCreateOptions<S.Codec.Encoded<Payload>>) => Effect.Effect<WorkflowInstance<S.Schema.Type<Result>>, WorkflowOperationError | S.SchemaError>;
|
|
49
|
+
readonly createBatch: (batch: WorkflowInstanceCreateBatchOptions<S.Schema.Type<Payload>, S.Codec.Encoded<Payload>>) => Effect.Effect<ReadonlyArray<WorkflowInstance<S.Schema.Type<Result>>>, WorkflowOperationError | S.SchemaError>;
|
|
50
|
+
readonly get: (instanceId: string) => Effect.Effect<WorkflowInstance<S.Schema.Type<Result>>, WorkflowOperationError>;
|
|
51
|
+
readonly rawUnsafe: Effect.Effect<CloudflareWorkflow<S.Codec.Encoded<Payload>>>;
|
|
48
52
|
}
|
|
49
53
|
declare const WorkflowOperationError_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 & {
|
|
50
54
|
readonly _tag: "WorkflowOperationError";
|
|
@@ -66,9 +70,9 @@ declare class WorkflowResultDecodeError extends WorkflowResultDecodeError_base<{
|
|
|
66
70
|
}> {
|
|
67
71
|
get message(): string;
|
|
68
72
|
}
|
|
69
|
-
type WorkflowBindingValue =
|
|
73
|
+
type WorkflowBindingValue = S.Schema.Type<typeof S.Unknown>;
|
|
70
74
|
declare const isWorkflow: <Payload>(value: WorkflowBindingValue) => value is CloudflareWorkflow<Payload>;
|
|
71
|
-
declare const makeClient: <Payload extends ServiceFreeSchema, Result extends ServiceFreeSchema>(definition: WorkflowBindingDefinition<Payload, Result>) => ((workflow: CloudflareWorkflow<
|
|
75
|
+
declare const makeClient: <Payload extends ServiceFreeSchema, Result extends ServiceFreeSchema>(definition: WorkflowBindingDefinition<Payload, Result>) => ((workflow: CloudflareWorkflow<S.Codec.Encoded<Payload>>) => WorkflowBindingClient<Payload, Result>);
|
|
72
76
|
declare const layer: <Self, Payload extends ServiceFreeSchema, Result extends ServiceFreeSchema>(tag: Context.Service<Self, WorkflowBindingClient<Payload, Result>>, definition: WorkflowBindingDefinition<Payload, Result>) => import("effect/Layer").Layer<Self, BindingNotFoundError | BindingValidationError, WorkerEnvironment>;
|
|
73
77
|
//#endregion
|
|
74
|
-
export {
|
|
78
|
+
export { WorkflowBindingClient, WorkflowBindingDefinition, WorkflowBinding_d_exports, WorkflowInstance, WorkflowInstanceCreateBatchOptions, WorkflowInstanceCreateOptions, WorkflowInstanceEvent, WorkflowInstanceLocationHint, WorkflowInstanceRestartOptions, WorkflowInstanceStatus, WorkflowInstanceStatusName, WorkflowInstanceTerminateOptions, WorkflowOperationError, WorkflowResultDecodeError, isWorkflow, layer, makeClient };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
|
|
2
|
+
import { layer as layer$1 } from "./Binding.mjs";
|
|
3
|
+
import { causeMessage } from "./internal/ErrorMessage.mjs";
|
|
4
|
+
import * as Data from "effect/Data";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as S from "effect/Schema";
|
|
7
|
+
import * as Predicate from "effect/Predicate";
|
|
8
|
+
import * as Option from "effect/Option";
|
|
9
|
+
//#region src/WorkflowBinding.ts
|
|
10
|
+
var WorkflowBinding_exports = /* @__PURE__ */ __exportAll({
|
|
11
|
+
WorkflowOperationError: () => WorkflowOperationError,
|
|
12
|
+
WorkflowResultDecodeError: () => WorkflowResultDecodeError,
|
|
13
|
+
isWorkflow: () => isWorkflow,
|
|
14
|
+
layer: () => layer,
|
|
15
|
+
makeClient: () => makeClient
|
|
16
|
+
});
|
|
17
|
+
const expectedWorkflow = "Workflow binding with create(), createBatch(), and get()";
|
|
18
|
+
var WorkflowOperationError = class extends Data.TaggedError("WorkflowOperationError") {
|
|
19
|
+
get message() {
|
|
20
|
+
return `Workflow ${this.operation} failed for binding "${this.binding}": ${causeMessage(this.cause)}`;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var WorkflowResultDecodeError = class extends Data.TaggedError("WorkflowResultDecodeError") {
|
|
24
|
+
get message() {
|
|
25
|
+
return `Workflow result decode failed for binding "${this.binding}" instance "${this.instanceId}": ${causeMessage(this.cause)}`;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const workflowError = (binding, operation, cause) => new WorkflowOperationError({
|
|
29
|
+
binding,
|
|
30
|
+
operation,
|
|
31
|
+
cause
|
|
32
|
+
});
|
|
33
|
+
const tryWorkflowPromise = (binding, operation, evaluate) => Effect.tryPromise({
|
|
34
|
+
try: evaluate,
|
|
35
|
+
catch: (cause) => workflowError(binding, operation, cause)
|
|
36
|
+
});
|
|
37
|
+
const WorkflowBindingSchema = S.declare((value) => Predicate.hasProperty(value, "create") && Predicate.isFunction(value.create) && Predicate.hasProperty(value, "createBatch") && Predicate.isFunction(value.createBatch) && Predicate.hasProperty(value, "get") && Predicate.isFunction(value.get));
|
|
38
|
+
const decodeWorkflowBinding = S.decodeUnknownOption(WorkflowBindingSchema);
|
|
39
|
+
const isWorkflow = (value) => Option.isSome(decodeWorkflowBinding(value));
|
|
40
|
+
const makeClient = (definition) => {
|
|
41
|
+
const encodePayload = S.encodeEffect(definition.payload);
|
|
42
|
+
const decodeResult = S.decodeUnknownEffect(definition.result);
|
|
43
|
+
const wrapInstance = (raw) => {
|
|
44
|
+
const operation = (name, evaluate) => tryWorkflowPromise(definition.binding, name, evaluate);
|
|
45
|
+
return {
|
|
46
|
+
raw,
|
|
47
|
+
id: raw.id,
|
|
48
|
+
pause: operation("pause", () => raw.pause()),
|
|
49
|
+
resume: operation("resume", () => raw.resume()),
|
|
50
|
+
terminate: (options) => operation("terminate", () => raw.terminate(options)),
|
|
51
|
+
restart: (options) => operation("restart", () => raw.restart(options)),
|
|
52
|
+
delete: operation("delete", () => raw.delete()),
|
|
53
|
+
status: operation("status", () => raw.status()).pipe(Effect.flatMap((status) => Effect.gen(function* () {
|
|
54
|
+
const output = status.output === void 0 || status.output === null && status.status !== "complete" ? Option.none() : Option.some(yield* decodeResult(status.output).pipe(Effect.mapError((cause) => new WorkflowResultDecodeError({
|
|
55
|
+
binding: definition.binding,
|
|
56
|
+
instanceId: raw.id,
|
|
57
|
+
cause
|
|
58
|
+
}))));
|
|
59
|
+
return {
|
|
60
|
+
status: status.status,
|
|
61
|
+
output,
|
|
62
|
+
error: status.error === null || status.error === void 0 ? Option.none() : Option.some(status.error)
|
|
63
|
+
};
|
|
64
|
+
}))),
|
|
65
|
+
sendEvent: (event) => operation("sendEvent", () => raw.sendEvent(event))
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
return (workflow) => ({
|
|
69
|
+
create: Effect.fn("WorkflowBinding.create", { attributes: {
|
|
70
|
+
binding: definition.binding,
|
|
71
|
+
operation: "create"
|
|
72
|
+
} })(function* (payload, options) {
|
|
73
|
+
const encoded = yield* encodePayload(payload);
|
|
74
|
+
const raw = yield* tryWorkflowPromise(definition.binding, "create", () => workflow.create({
|
|
75
|
+
...options,
|
|
76
|
+
params: encoded
|
|
77
|
+
}));
|
|
78
|
+
return wrapInstance(raw);
|
|
79
|
+
}),
|
|
80
|
+
createBatch: Effect.fn("WorkflowBinding.createBatch", { attributes: {
|
|
81
|
+
binding: definition.binding,
|
|
82
|
+
operation: "createBatch"
|
|
83
|
+
} })(function* (batch) {
|
|
84
|
+
const encodedBatch = [];
|
|
85
|
+
for (const item of batch) {
|
|
86
|
+
const { payload, ...options } = item;
|
|
87
|
+
encodedBatch.push({
|
|
88
|
+
...options,
|
|
89
|
+
params: yield* encodePayload(payload)
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return (yield* tryWorkflowPromise(definition.binding, "createBatch", () => workflow.createBatch(encodedBatch))).map(wrapInstance);
|
|
93
|
+
}),
|
|
94
|
+
get: (instanceId) => tryWorkflowPromise(definition.binding, "get", () => workflow.get(instanceId)).pipe(Effect.map(wrapInstance)),
|
|
95
|
+
rawUnsafe: Effect.succeed(workflow)
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const layer = (tag, definition) => layer$1(tag, definition.binding, (value) => isWorkflow(value), makeClient(definition), { expected: expectedWorkflow });
|
|
99
|
+
//#endregion
|
|
100
|
+
export { WorkflowBinding_exports, WorkflowOperationError, WorkflowResultDecodeError, isWorkflow, layer, makeClient };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { WorkerEnvironment } from "./Environment.mjs";
|
|
2
|
+
import { BindingNotFoundError, BindingValidationError } from "./Binding.mjs";
|
|
3
|
+
import { ServiceFreeSchema } from "./RpcDefinition.mjs";
|
|
4
|
+
import { ExecutionContext, WorkerContext } from "./Worker.mjs";
|
|
5
|
+
import { WorkflowBindingClient, WorkflowInstance, WorkflowInstanceCreateBatchOptions, WorkflowInstanceCreateOptions, WorkflowOperationError } from "./WorkflowBinding.mjs";
|
|
6
|
+
import { WorkflowClass, WorkflowRunContext } from "./Workflow.mjs";
|
|
7
|
+
import * as Context$1 from "effect/Context";
|
|
8
|
+
import * as Effect from "effect/Effect";
|
|
9
|
+
import * as S from "effect/Schema";
|
|
10
|
+
import { Layer } from "effect";
|
|
11
|
+
declare namespace WorkflowDefinition_d_exports {
|
|
12
|
+
export { Definition, Handler, LayerOptions, Options, Tag, TagClass, Workflow, implement, make };
|
|
13
|
+
}
|
|
14
|
+
interface Definition<Id extends string = string, Payload extends ServiceFreeSchema = ServiceFreeSchema, Result extends ServiceFreeSchema = ServiceFreeSchema> {
|
|
15
|
+
readonly id: Id;
|
|
16
|
+
readonly payload: Payload;
|
|
17
|
+
readonly result: Result;
|
|
18
|
+
}
|
|
19
|
+
declare namespace Definition {
|
|
20
|
+
type Any = Definition<string, ServiceFreeSchema, ServiceFreeSchema>;
|
|
21
|
+
}
|
|
22
|
+
type Handler<ROut, Self extends Definition.Any> = (payload: S.Schema.Type<Self["payload"]>) => Effect.Effect<S.Schema.Type<Self["result"]>, unknown, WorkflowRunContext<ROut>>;
|
|
23
|
+
interface Options<ROut, Self extends Definition.Any> {
|
|
24
|
+
readonly run: Handler<ROut, Self>;
|
|
25
|
+
}
|
|
26
|
+
type LayerOptions = {
|
|
27
|
+
readonly binding: string;
|
|
28
|
+
};
|
|
29
|
+
interface TagClass<Self, Id extends string, Payload extends ServiceFreeSchema, Result extends ServiceFreeSchema> extends Context$1.ServiceClass<Self, `effect-cf/Workflow/${Id}`, WorkflowBindingClient<Payload, Result>> {
|
|
30
|
+
readonly id: Id;
|
|
31
|
+
readonly payload: Payload;
|
|
32
|
+
readonly result: Result;
|
|
33
|
+
readonly make: <ROut, LayerError>(layer: Layer.Layer<ROut, LayerError, ExecutionContext | WorkerContext | WorkerEnvironment>, options: Options<ROut, Definition<Id, Payload, Result>>) => WorkflowClass<S.Codec.Encoded<Payload>, S.Codec.Encoded<Result>, ROut>;
|
|
34
|
+
readonly layer: (options: LayerOptions) => Layer.Layer<Self, BindingNotFoundError | BindingValidationError, WorkerEnvironment>;
|
|
35
|
+
readonly create: (payload: S.Schema.Type<Payload>, options?: WorkflowInstanceCreateOptions<S.Codec.Encoded<Payload>>) => Effect.Effect<WorkflowInstance<S.Schema.Type<Result>>, WorkflowOperationError | S.SchemaError, Self>;
|
|
36
|
+
readonly createBatch: (batch: WorkflowInstanceCreateBatchOptions<S.Schema.Type<Payload>, S.Codec.Encoded<Payload>>) => Effect.Effect<ReadonlyArray<WorkflowInstance<S.Schema.Type<Result>>>, WorkflowOperationError | S.SchemaError, Self>;
|
|
37
|
+
readonly get: (instanceId: string) => Effect.Effect<WorkflowInstance<S.Schema.Type<Result>>, WorkflowOperationError, Self>;
|
|
38
|
+
readonly rawUnsafe: Effect.Effect<globalThis.Workflow<S.Codec.Encoded<Payload>>, never, Self>;
|
|
39
|
+
}
|
|
40
|
+
declare const make: <Id extends string, Payload extends ServiceFreeSchema, Result extends ServiceFreeSchema>(id: Id, definition: {
|
|
41
|
+
readonly payload: Payload;
|
|
42
|
+
readonly result: Result;
|
|
43
|
+
}) => TagClass<Definition<Id, Payload, Result>, Id, Payload, Result>;
|
|
44
|
+
declare const Tag: <Self>() => <Id extends string, Payload extends ServiceFreeSchema, Result extends ServiceFreeSchema>(id: Id, definition: {
|
|
45
|
+
readonly payload: Payload;
|
|
46
|
+
readonly result: Result;
|
|
47
|
+
}) => TagClass<Self, Id, Payload, Result>;
|
|
48
|
+
declare const Workflow: typeof Tag;
|
|
49
|
+
declare const implement: <ROut, const Self extends Definition.Any>(_definition: Self, handler: Handler<ROut, Self>) => Handler<ROut, Self>;
|
|
50
|
+
//#endregion
|
|
51
|
+
export { Definition, Handler, LayerOptions, Options, Tag, TagClass, Workflow, WorkflowDefinition_d_exports, implement, make };
|