spool-sdk 0.0.0-stage → 0.2.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.
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Errors the SDK raises inside a run. Each has the engine's stable code, and `retryable`
3
+ * says whether sending the same request again can succeed.
4
+ */
5
+ export declare abstract class SpoolError extends Error {
6
+ abstract readonly code: string;
7
+ abstract readonly retryable: boolean;
8
+ }
9
+ /**
10
+ * This worker no longer holds the run's lease: it expired, or another worker took the run
11
+ * over. The run stops here; whoever holds the lease now replays it from the journal.
12
+ */
13
+ export declare class LeaseLostError extends SpoolError {
14
+ readonly afterResend: boolean;
15
+ readonly name = "LeaseLostError";
16
+ readonly code = "LEASE_LOST";
17
+ readonly retryable = false;
18
+ /**
19
+ * `afterResend`: the request had been sent before, on a connection that dropped before
20
+ * the reply came. That first copy may have been applied, so for a completion this can
21
+ * mean the run did finish.
22
+ */
23
+ constructor(afterResend?: boolean);
24
+ }
25
+ /**
26
+ * A step's name differs from the one the journal holds at its position: the workflow's
27
+ * code changed while the run was in flight. Replaying further would hand stored outputs
28
+ * to the wrong steps, so the run stops and needs an operator.
29
+ */
30
+ export declare class StepMismatchError extends SpoolError {
31
+ readonly name = "StepMismatchError";
32
+ readonly code = "STEP_MISMATCH";
33
+ readonly retryable = false;
34
+ static at(seq: number, recorded: string, called: string): StepMismatchError;
35
+ }
36
+ /** The worker was stopped before the run finished; its lease expires and it runs again. */
37
+ export declare class WorkerStoppedError extends SpoolError {
38
+ readonly name = "WorkerStoppedError";
39
+ readonly code = "WORKER_STOPPED";
40
+ readonly retryable = false;
41
+ constructor();
42
+ }
43
+ /** The connection to the engine dropped before the reply arrived. */
44
+ export declare class DisconnectedError extends SpoolError {
45
+ readonly name = "DisconnectedError";
46
+ readonly code = "DISCONNECTED";
47
+ readonly retryable = true;
48
+ constructor();
49
+ }
50
+ /** The engine rejected a request with a code the SDK has no class for. */
51
+ export declare class RequestFailedError extends SpoolError {
52
+ readonly code: string;
53
+ readonly retryable: boolean;
54
+ readonly name = "RequestFailedError";
55
+ constructor(code: string, message: string, retryable: boolean);
56
+ }
57
+ /**
58
+ * Throw this (or a subclass) from a step for an error that running the step again cannot
59
+ * fix, such as invalid input or a 4xx answer: the run fails at once instead of retrying.
60
+ */
61
+ export declare class NonRetryableError extends Error {
62
+ name: string;
63
+ }
64
+ /**
65
+ * A step threw; the failure was recorded and the run stopped on this worker. The engine
66
+ * runs it again later from that step, or has failed the run. Workflow code that catches
67
+ * this should let it propagate: the run can no longer make progress on this attempt.
68
+ */
69
+ export declare class StepFailedError extends SpoolError {
70
+ readonly step: string;
71
+ readonly retryable: boolean;
72
+ readonly name = "StepFailedError";
73
+ readonly code = "STEP_FAILED";
74
+ constructor(step: string, retryable: boolean, options: {
75
+ readonly cause: unknown;
76
+ });
77
+ }
78
+ /**
79
+ * The run went to sleep (ctx.sleep): the engine recorded the sleep and took the run back, and
80
+ * queues it again for any worker once the time has passed. Thrown out of ctx.sleep to stop
81
+ * the workflow on this worker; let it propagate.
82
+ */
83
+ /**
84
+ * Stops a run whose step must wait for its rate limit (StepOptions.rateLimit): its token is
85
+ * reserved and the run resumes on a worker once the token is due, without holding this one.
86
+ */
87
+ export declare class RunThrottledError extends SpoolError {
88
+ readonly key: string;
89
+ readonly waitMs: number;
90
+ readonly name = "RunThrottledError";
91
+ readonly code = "RUN_THROTTLED";
92
+ readonly retryable = false;
93
+ constructor(key: string, waitMs: number);
94
+ }
95
+ export declare class RunSleepingError extends SpoolError {
96
+ readonly durationMs: number;
97
+ readonly name = "RunSleepingError";
98
+ readonly code = "RUN_SLEEPING";
99
+ readonly retryable = false;
100
+ constructor(durationMs: number);
101
+ }
102
+ /**
103
+ * The run is waiting for an event (ctx.waitForEvent): the engine took it back and queues it
104
+ * again once the event arrives or the wait times out. Thrown out of ctx.waitForEvent to
105
+ * stop the workflow on this worker; let it propagate.
106
+ */
107
+ export declare class RunWaitingError extends SpoolError {
108
+ readonly event: string;
109
+ readonly name = "RunWaitingError";
110
+ readonly code = "RUN_WAITING";
111
+ readonly retryable = false;
112
+ constructor(event: string);
113
+ }
114
+ /** Ordinary flow when a run stops: logged, not treated as a workflow failure. */
115
+ export declare function isRunStop(error: unknown): error is LeaseLostError | WorkerStoppedError;
package/dist/errors.js ADDED
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Errors the SDK raises inside a run. Each has the engine's stable code, and `retryable`
3
+ * says whether sending the same request again can succeed.
4
+ */
5
+ export class SpoolError extends Error {
6
+ }
7
+ /**
8
+ * This worker no longer holds the run's lease: it expired, or another worker took the run
9
+ * over. The run stops here; whoever holds the lease now replays it from the journal.
10
+ */
11
+ export class LeaseLostError extends SpoolError {
12
+ afterResend;
13
+ name = "LeaseLostError";
14
+ code = "LEASE_LOST";
15
+ retryable = false;
16
+ /**
17
+ * `afterResend`: the request had been sent before, on a connection that dropped before
18
+ * the reply came. That first copy may have been applied, so for a completion this can
19
+ * mean the run did finish.
20
+ */
21
+ constructor(afterResend = false) {
22
+ super("the lease on this run was lost; the run continues on another attempt");
23
+ this.afterResend = afterResend;
24
+ }
25
+ }
26
+ /**
27
+ * A step's name differs from the one the journal holds at its position: the workflow's
28
+ * code changed while the run was in flight. Replaying further would hand stored outputs
29
+ * to the wrong steps, so the run stops and needs an operator.
30
+ */
31
+ export class StepMismatchError extends SpoolError {
32
+ name = "StepMismatchError";
33
+ code = "STEP_MISMATCH";
34
+ retryable = false;
35
+ static at(seq, recorded, called) {
36
+ return new StepMismatchError(`step ${String(seq)} is "${called}" in this code but was recorded as "${recorded}"; the workflow changed while the run was in flight`);
37
+ }
38
+ }
39
+ /** The worker was stopped before the run finished; its lease expires and it runs again. */
40
+ export class WorkerStoppedError extends SpoolError {
41
+ name = "WorkerStoppedError";
42
+ code = "WORKER_STOPPED";
43
+ retryable = false;
44
+ constructor() {
45
+ super("the worker stopped before the run finished");
46
+ }
47
+ }
48
+ /** The connection to the engine dropped before the reply arrived. */
49
+ export class DisconnectedError extends SpoolError {
50
+ name = "DisconnectedError";
51
+ code = "DISCONNECTED";
52
+ retryable = true;
53
+ constructor() {
54
+ super("the connection to the engine closed before the reply arrived");
55
+ }
56
+ }
57
+ /** The engine rejected a request with a code the SDK has no class for. */
58
+ export class RequestFailedError extends SpoolError {
59
+ code;
60
+ retryable;
61
+ name = "RequestFailedError";
62
+ constructor(code, message, retryable) {
63
+ super(message);
64
+ this.code = code;
65
+ this.retryable = retryable;
66
+ }
67
+ }
68
+ /**
69
+ * Throw this (or a subclass) from a step for an error that running the step again cannot
70
+ * fix, such as invalid input or a 4xx answer: the run fails at once instead of retrying.
71
+ */
72
+ export class NonRetryableError extends Error {
73
+ name = "NonRetryableError";
74
+ }
75
+ /**
76
+ * A step threw; the failure was recorded and the run stopped on this worker. The engine
77
+ * runs it again later from that step, or has failed the run. Workflow code that catches
78
+ * this should let it propagate: the run can no longer make progress on this attempt.
79
+ */
80
+ export class StepFailedError extends SpoolError {
81
+ step;
82
+ retryable;
83
+ name = "StepFailedError";
84
+ code = "STEP_FAILED";
85
+ constructor(step, retryable, options) {
86
+ super(retryable
87
+ ? `step "${step}" failed; the run will be retried from this step`
88
+ : `step "${step}" failed with an error that is not retryable; the run fails`, options);
89
+ this.step = step;
90
+ this.retryable = retryable;
91
+ }
92
+ }
93
+ /**
94
+ * The run went to sleep (ctx.sleep): the engine recorded the sleep and took the run back, and
95
+ * queues it again for any worker once the time has passed. Thrown out of ctx.sleep to stop
96
+ * the workflow on this worker; let it propagate.
97
+ */
98
+ /**
99
+ * Stops a run whose step must wait for its rate limit (StepOptions.rateLimit): its token is
100
+ * reserved and the run resumes on a worker once the token is due, without holding this one.
101
+ */
102
+ export class RunThrottledError extends SpoolError {
103
+ key;
104
+ waitMs;
105
+ name = "RunThrottledError";
106
+ code = "RUN_THROTTLED";
107
+ retryable = false;
108
+ constructor(key, waitMs) {
109
+ super(`the run waits ${String(waitMs)} ms for rate limit "${key}"; it resumes on a worker after that`);
110
+ this.key = key;
111
+ this.waitMs = waitMs;
112
+ }
113
+ }
114
+ export class RunSleepingError extends SpoolError {
115
+ durationMs;
116
+ name = "RunSleepingError";
117
+ code = "RUN_SLEEPING";
118
+ retryable = false;
119
+ constructor(durationMs) {
120
+ super(`the run is sleeping for ${String(durationMs)} ms; it resumes on a worker after that`);
121
+ this.durationMs = durationMs;
122
+ }
123
+ }
124
+ /**
125
+ * The run is waiting for an event (ctx.waitForEvent): the engine took it back and queues it
126
+ * again once the event arrives or the wait times out. Thrown out of ctx.waitForEvent to
127
+ * stop the workflow on this worker; let it propagate.
128
+ */
129
+ export class RunWaitingError extends SpoolError {
130
+ event;
131
+ name = "RunWaitingError";
132
+ code = "RUN_WAITING";
133
+ retryable = false;
134
+ constructor(event) {
135
+ super(`the run is waiting for the event "${event}"; it resumes on a worker after that`);
136
+ this.event = event;
137
+ }
138
+ }
139
+ /** Ordinary flow when a run stops: logged, not treated as a workflow failure. */
140
+ export function isRunStop(error) {
141
+ return error instanceof LeaseLostError || error instanceof WorkerStoppedError;
142
+ }
143
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAgB,UAAW,SAAQ,KAAK;CAG7C;AAED;;;GAGG;AACH,MAAM,OAAO,cAAe,SAAQ,UAAU;IAUvB;IATH,IAAI,GAAG,gBAAgB,CAAC;IACxB,IAAI,GAAG,YAAY,CAAC;IACpB,SAAS,GAAG,KAAK,CAAC;IAEpC;;;;OAIG;IACH,YAAqB,cAAc,KAAK;QACtC,KAAK,CAAC,sEAAsE,CAAC,CAAC;QAD3D,gBAAW,GAAX,WAAW,CAAQ;IAExC,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,OAAO,iBAAkB,SAAQ,UAAU;IAC7B,IAAI,GAAG,mBAAmB,CAAC;IAC3B,IAAI,GAAG,eAAe,CAAC;IACvB,SAAS,GAAG,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Duration } from "./duration.js";
3
+ export type { LlmCall, LlmStream, LlmUsage, StepOptions, WaitOptions, WorkflowContext, } from "./context.js";
4
+ export { DisconnectedError, LeaseLostError, NonRetryableError, RequestFailedError, RunSleepingError, RunThrottledError, RunWaitingError, SpoolError, StepFailedError, StepMismatchError, WorkerStoppedError, } from "./errors.js";
5
+ export { consoleLogger, type Logger } from "./logger.js";
6
+ export { Worker, type WorkerOptions } from "./worker.js";
7
+ export { workflow, type Workflow } from "./workflow.js";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { parseDuration } from "./duration.js";
2
+ export { DisconnectedError, LeaseLostError, NonRetryableError, RequestFailedError, RunSleepingError, RunThrottledError, RunWaitingError, SpoolError, StepFailedError, StepMismatchError, WorkerStoppedError, } from "./errors.js";
3
+ export { consoleLogger } from "./logger.js";
4
+ export { Worker } from "./worker.js";
5
+ export { workflow } from "./workflow.js";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAiB,MAAM,eAAe,CAAC;AAS7D,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,UAAU,EACV,eAAe,EACf,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,aAAa,EAAe,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,MAAM,EAAsB,MAAM,aAAa,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAiB,MAAM,eAAe,CAAC"}
package/dist/json.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { JsonValue } from "@bufbuild/protobuf";
2
+ /**
3
+ * The value as the journal will store it: what JSON.stringify keeps, with `undefined`
4
+ * becoming null. A step returns this form on its first run too, so the code after it
5
+ * sees the same value whether the step ran or was replayed (a Date is a string both
6
+ * times, not a Date once and a string after a crash).
7
+ */
8
+ export declare function toStoredJson(value: unknown, what: string): JsonValue;
package/dist/json.js ADDED
@@ -0,0 +1,24 @@
1
+ import { NonRetryableError } from "./errors.js";
2
+ /**
3
+ * The value as the journal will store it: what JSON.stringify keeps, with `undefined`
4
+ * becoming null. A step returns this form on its first run too, so the code after it
5
+ * sees the same value whether the step ran or was replayed (a Date is a string both
6
+ * times, not a Date once and a string after a crash).
7
+ */
8
+ export function toStoredJson(value, what) {
9
+ let text;
10
+ try {
11
+ // The standard typings say string, but undefined, functions, symbols and objects whose
12
+ // toJSON returns undefined all stringify to undefined.
13
+ text = JSON.stringify(value);
14
+ }
15
+ catch (error) {
16
+ // A bug in the step, such as a BigInt or a cycle: running it again would not help.
17
+ throw new NonRetryableError(`${what} cannot be stored as JSON`, { cause: error });
18
+ }
19
+ if (text === undefined)
20
+ return null;
21
+ // JSON.parse returns `any`; the text was produced by JSON.stringify, so it is JSON.
22
+ return JSON.parse(text);
23
+ }
24
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc,EAAE,IAAY;IACvD,IAAI,IAAwB,CAAC;IAC7B,IAAI,CAAC;QACH,uFAAuF;QACvF,uDAAuD;QACvD,IAAI,GAAI,IAAI,CAAC,SAAoD,CAAC,KAAK,CAAC,CAAC;IAC3E,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,mFAAmF;QACnF,MAAM,IAAI,iBAAiB,CAAC,GAAG,IAAI,2BAA2B,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACpF,CAAC;IACD,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACpC,oFAAoF;IACpF,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAc,CAAC;AACvC,CAAC"}
@@ -0,0 +1,9 @@
1
+ /** The logging the SDK needs; a pino logger fits it as is. */
2
+ export interface Logger {
3
+ debug(fields: object, message: string): void;
4
+ info(fields: object, message: string): void;
5
+ warn(fields: object, message: string): void;
6
+ error(fields: object, message: string): void;
7
+ }
8
+ /** The default: warnings and errors to the console, nothing else. */
9
+ export declare const consoleLogger: Logger;
package/dist/logger.js ADDED
@@ -0,0 +1,12 @@
1
+ /** The default: warnings and errors to the console, nothing else. */
2
+ export const consoleLogger = {
3
+ debug: () => undefined,
4
+ info: () => undefined,
5
+ warn: (fields, message) => {
6
+ console.warn(`[spool] ${message}`, fields);
7
+ },
8
+ error: (fields, message) => {
9
+ console.error(`[spool] ${message}`, fields);
10
+ },
11
+ };
12
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAQA,qEAAqE;AACrE,MAAM,CAAC,MAAM,aAAa,GAAW;IACnC,KAAK,EAAE,GAAG,EAAE,CAAC,SAAS;IACtB,IAAI,EAAE,GAAG,EAAE,CAAC,SAAS;IACrB,IAAI,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE;QACxB,OAAO,CAAC,IAAI,CAAC,WAAW,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC;IAC7C,CAAC;IACD,KAAK,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,EAAE;QACzB,OAAO,CAAC,KAAK,CAAC,WAAW,OAAO,EAAE,EAAE,MAAM,CAAC,CAAC;IAC9C,CAAC;CACF,CAAC"}