@protobase/workflows 0.71.0 → 0.71.2
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/dist/definition/schema.js +2 -1
- package/dist/definition/types.d.ts +5 -0
- package/dist/index.d.ts +3 -1
- package/dist/limits.d.ts +7 -0
- package/dist/limits.js +8 -1
- package/dist/plugin/cache-plan.js +49 -0
- package/dist/plugin/cache-types.d.ts +45 -0
- package/dist/plugin/cached-run.js +128 -0
- package/dist/plugin/context.d.ts +5 -0
- package/dist/plugin/define-plugin.js +1 -0
- package/dist/plugin/step-handlers.js +16 -3
- package/dist/plugin/type-list.d.ts +7 -0
- package/dist/plugin/type-list.js +7 -1
- package/dist/plugin/types.d.ts +3 -0
- package/dist/run/cache/cache-key.d.ts +11 -0
- package/dist/run/cache/cache-key.js +40 -0
- package/dist/run/cache/cache-port.d.ts +51 -0
- package/dist/run/cache/cache-ttl.js +21 -0
- package/dist/run/cache/canonical.js +14 -0
- package/dist/run/cache/read-tracker.d.ts +4 -0
- package/dist/run/cache/read-tracker.js +58 -0
- package/dist/run/cache/secret-names.d.ts +12 -0
- package/dist/run/cache/secret-names.js +29 -0
- package/dist/run/port.d.ts +3 -0
- package/dist/run/runner.d.ts +4 -1
- package/dist/run/runner.js +2 -1
- package/dist/run/segment.d.ts +4 -1
- package/dist/run/segment.js +6 -1
- package/dist/run/task.js +6 -1
- package/package.json +1 -1
|
@@ -27,11 +27,16 @@ type Flow = {
|
|
|
27
27
|
export type PassState = Common & Flow & {
|
|
28
28
|
Type: 'Pass';
|
|
29
29
|
};
|
|
30
|
+
/**
|
|
31
|
+
* `Cache` turns its step type's result cache on (`true`, forced when the type does not offer it) or off (`false`);
|
|
32
|
+
* without it, the type decides.
|
|
33
|
+
*/
|
|
30
34
|
export type TaskState = Common & Flow & {
|
|
31
35
|
Type: 'Task';
|
|
32
36
|
Resource: string;
|
|
33
37
|
Arguments?: Template;
|
|
34
38
|
TimeoutSeconds?: number;
|
|
39
|
+
Cache?: boolean;
|
|
35
40
|
Retry?: Retrier[];
|
|
36
41
|
Catch?: Catcher[];
|
|
37
42
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { limits } from "./limits.js";
|
|
|
8
8
|
import { nestingDepth } from "./nesting.js";
|
|
9
9
|
import { ErrorOutput, StepError } from "./run/errors.js";
|
|
10
10
|
import { Redactor, redacted, secretRedactor, shortestSecret } from "./run/redact.js";
|
|
11
|
+
import { KeyShape } from "./run/cache/cache-key.js";
|
|
12
|
+
import { CacheCall, CacheEntry, CacheIdentity, StepCacheStore } from "./run/cache/cache-port.js";
|
|
11
13
|
import { LoadedRun, RunChange, RunStatus, RunStore, SavedStep, SegmentQueue, StepLog, StepRecord, StepStatus, TaskContext, TaskHandler, TaskHandlers } from "./run/port.js";
|
|
12
14
|
import { EarlierStep, rerunSteps } from "./run/rerun.js";
|
|
13
15
|
import { Runner, RunnerPort, createRunner } from "./run/runner.js";
|
|
@@ -18,4 +20,4 @@ import { triggerChecks } from "./plugin/trigger-checks.js";
|
|
|
18
20
|
import { TypeEntry, Unavailable, typeList } from "./plugin/type-list.js";
|
|
19
21
|
import { ConnectionSupport, connectionUnavailable } from "./plugin/connection-availability.js";
|
|
20
22
|
import { CatalogEntry, TypeCatalog, typeCatalog } from "./plugin/type-catalog.js";
|
|
21
|
-
export { type CatalogEntry, type Catcher, type CheckOptions, type ChoiceRule, type ChoiceState, type ConnectionSupport, type Definition, type EarlierStep, type ErrorOutput, type LoadedPlugin, type LoadedRun, type Machine, type MapState, type Problem, type Redactor, type Retrier, type RunChange, type RunStatus, type RunStore, type Runner, type RunnerPort, type SavedStep, type SegmentInput, type SegmentQueue, type State, type StateMachine, StepError, type StepLog, type StepRecord, type StepStatus, type StepWiring, type TaskContext, type TaskHandler, type TaskHandlers, type TaskState, type Trigger, type TriggerCheck, type TypeCatalog, type TypeEntry, type Unavailable, checkDefinition, connectionUnavailable, createRunner, definitionSchema, limits, loadPlugins, nestingDepth, problemText, redacted, rerunSteps, runSegment, secretRedactor, shortestSecret, stepHandlers, triggerChecks, typeCatalog, typeList, usedConnections };
|
|
23
|
+
export { type CacheCall, type CacheEntry, type CacheIdentity, type CatalogEntry, type Catcher, type CheckOptions, type ChoiceRule, type ChoiceState, type ConnectionSupport, type Definition, type EarlierStep, type ErrorOutput, type KeyShape, type LoadedPlugin, type LoadedRun, type Machine, type MapState, type Problem, type Redactor, type Retrier, type RunChange, type RunStatus, type RunStore, type Runner, type RunnerPort, type SavedStep, type SegmentInput, type SegmentQueue, type State, type StateMachine, type StepCacheStore, StepError, type StepLog, type StepRecord, type StepStatus, type StepWiring, type TaskContext, type TaskHandler, type TaskHandlers, type TaskState, type Trigger, type TriggerCheck, type TypeCatalog, type TypeEntry, type Unavailable, checkDefinition, connectionUnavailable, createRunner, definitionSchema, limits, loadPlugins, nestingDepth, problemText, redacted, rerunSteps, runSegment, secretRedactor, shortestSecret, stepHandlers, triggerChecks, typeCatalog, typeList, usedConnections };
|
package/dist/limits.d.ts
CHANGED
|
@@ -35,5 +35,12 @@ export declare const limits: {
|
|
|
35
35
|
/** A run's input: its size as JSON and how deeply its arrays and objects nest. */
|
|
36
36
|
readonly inputBytes: number;
|
|
37
37
|
readonly inputDepth: 64;
|
|
38
|
+
/** A cached step result: the longest it serves, how long one a workflow forced on serves, its size as JSON, and the entries one step type keeps per tenant. */
|
|
39
|
+
readonly cacheMaxSeconds: number;
|
|
40
|
+
readonly cacheForcedSeconds: 3600;
|
|
41
|
+
readonly cacheValueBytes: number;
|
|
42
|
+
readonly cacheEntries: 1000;
|
|
43
|
+
/** How long a cache lookup may take before the step runs without it. */
|
|
44
|
+
readonly cacheLookupMs: 2000;
|
|
38
45
|
};
|
|
39
46
|
//#endregion
|
package/dist/limits.js
CHANGED
|
@@ -34,7 +34,14 @@ const limits = {
|
|
|
34
34
|
segmentMs: 6e4,
|
|
35
35
|
/** A run's input: its size as JSON and how deeply its arrays and objects nest. */
|
|
36
36
|
inputBytes: 1048576,
|
|
37
|
-
inputDepth: 64
|
|
37
|
+
inputDepth: 64,
|
|
38
|
+
/** A cached step result: the longest it serves, how long one a workflow forced on serves, its size as JSON, and the entries one step type keeps per tenant. */
|
|
39
|
+
cacheMaxSeconds: 2592e3,
|
|
40
|
+
cacheForcedSeconds: 3600,
|
|
41
|
+
cacheValueBytes: 262144,
|
|
42
|
+
cacheEntries: 1e3,
|
|
43
|
+
/** How long a cache lookup may take before the step runs without it. */
|
|
44
|
+
cacheLookupMs: 2e3
|
|
38
45
|
};
|
|
39
46
|
//#endregion
|
|
40
47
|
export { limits };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { limits } from "../limits.js";
|
|
2
|
+
import { cacheSeconds } from "../run/cache/cache-ttl.js";
|
|
3
|
+
//#region src/plugin/cache-plan.ts
|
|
4
|
+
const isOn = (declared, setting, input) => {
|
|
5
|
+
if (setting === false) return false;
|
|
6
|
+
if (setting === true && declared?.choice !== "switch") return true;
|
|
7
|
+
if (!declared) return false;
|
|
8
|
+
if (setting === void 0 && declared.choice === "switch" && declared.default !== true) return false;
|
|
9
|
+
return declared.when?.(input) ?? true;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The plan of one call of the type `id` with its checked input, or undefined when the cache is off for it, as it is for
|
|
13
|
+
* every call of a type without `cache` that no workflow forces on. A type forced on without `cache` keys on its whole
|
|
14
|
+
* input, returns a hit, keeps it `limits.cacheForcedSeconds`, and keys on every person a step acts as.
|
|
15
|
+
*/
|
|
16
|
+
const cachePlan = (id, type, input, cache) => {
|
|
17
|
+
if (!cache || !isOn(type.cache, cache.setting, input)) return;
|
|
18
|
+
const connection = type.connection === void 0 ? {} : { connection: `${id.split(":")[0]}:${type.connection}` };
|
|
19
|
+
const declared = type.cache;
|
|
20
|
+
if (!declared) return {
|
|
21
|
+
cache,
|
|
22
|
+
use: "return",
|
|
23
|
+
key: "input",
|
|
24
|
+
seconds: limits.cacheForcedSeconds,
|
|
25
|
+
identity: {
|
|
26
|
+
...connection,
|
|
27
|
+
saver: true,
|
|
28
|
+
enabler: true
|
|
29
|
+
},
|
|
30
|
+
actsAs: ["records", "mailbox"]
|
|
31
|
+
};
|
|
32
|
+
const seconds = cacheSeconds(typeof declared.ttl === "function" ? declared.ttl(input) : declared.ttl);
|
|
33
|
+
if (seconds === void 0) return { invalidTtl: true };
|
|
34
|
+
const actsAs = declared.actsAs ?? [];
|
|
35
|
+
return {
|
|
36
|
+
cache,
|
|
37
|
+
use: declared.use ?? "return",
|
|
38
|
+
key: declared.key ?? "reads",
|
|
39
|
+
seconds,
|
|
40
|
+
identity: {
|
|
41
|
+
...connection,
|
|
42
|
+
saver: actsAs.includes("records"),
|
|
43
|
+
enabler: actsAs.includes("mailbox")
|
|
44
|
+
},
|
|
45
|
+
actsAs
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
export { cachePlan };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/plugin/cache-types.d.ts
|
|
2
|
+
/** How much of a step type's cache its workflow's author decides. */
|
|
3
|
+
export type CacheChoice =
|
|
4
|
+
/** Nothing: the type decides alone, or caching makes no sense for it. */
|
|
5
|
+
'hidden' |
|
|
6
|
+
/** An on and off switch, on unless the workflow turns it off when `default` is true. */
|
|
7
|
+
'switch' |
|
|
8
|
+
/** The type decides, and the author may force it on. */
|
|
9
|
+
'force' |
|
|
10
|
+
/** The type decides, and advises against forcing it on with its `disclaimer`. */
|
|
11
|
+
'discouraged';
|
|
12
|
+
/** A duration such as `10 minutes`, or a number of seconds; at most 30 days. */
|
|
13
|
+
export type CacheTtl = string | number;
|
|
14
|
+
/**
|
|
15
|
+
* A step type's result cache. When it is on for a call, the engine looks up an earlier call of the type with the same
|
|
16
|
+
* key, from any run or workflow, made as the same tenant and, where the type acts as someone, the same person or account.
|
|
17
|
+
*/
|
|
18
|
+
export type StepCache = {
|
|
19
|
+
choice: CacheChoice;
|
|
20
|
+
/** What the editor shows before a workflow forces it on; `discouraged` needs one. */
|
|
21
|
+
disclaimer?: string;
|
|
22
|
+
/** For `switch`: on unless the workflow turns it off. Default false. */
|
|
23
|
+
default?: boolean;
|
|
24
|
+
/** `return` (default): a hit is the step's result, and `run` is not called. `offer`: `run` gets it as `earlier`. */
|
|
25
|
+
use?: 'return' | 'offer';
|
|
26
|
+
/** Whether this call may use the cache at all, from its input; false looks up and keeps nothing. Default true. */
|
|
27
|
+
when?: (input: Record<string, unknown>) => boolean;
|
|
28
|
+
/** How long a result serves, or per input. */
|
|
29
|
+
ttl: CacheTtl | ((input: Record<string, unknown>) => CacheTtl);
|
|
30
|
+
/**
|
|
31
|
+
* What the key covers: `reads` (default) the input values `run` read, `input` the whole input, or the JSON value a
|
|
32
|
+
* function makes of the input; a function that throws or gives no JSON value leaves the call uncached.
|
|
33
|
+
*/
|
|
34
|
+
key?: 'reads' | 'input' | ((input: Record<string, unknown>) => unknown);
|
|
35
|
+
/** The services a cached call may act through as a person: `records` (as the saver) and `mailbox` (as the enabler). A call that uses another is not kept. */
|
|
36
|
+
actsAs?: ('records' | 'mailbox')[];
|
|
37
|
+
};
|
|
38
|
+
/** What an `offer` step gets of the earlier call it may use: its result, when it was kept and until when, and the secrets that changed since. */
|
|
39
|
+
export type EarlierResult = {
|
|
40
|
+
output: unknown;
|
|
41
|
+
cached_at: string;
|
|
42
|
+
expires_at: string;
|
|
43
|
+
secrets_changed: string[];
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { limits } from "../limits.js";
|
|
2
|
+
import { jsonBytes } from "../run/json-bytes.js";
|
|
3
|
+
import { cacheKey } from "../run/cache/cache-key.js";
|
|
4
|
+
import { trackReads } from "../run/cache/read-tracker.js";
|
|
5
|
+
//#region src/plugin/cached-run.ts
|
|
6
|
+
/** A result a step took from `earlier`: the cache keeps its entry as it is. */
|
|
7
|
+
var Reused = class {
|
|
8
|
+
output;
|
|
9
|
+
constructor(output) {
|
|
10
|
+
this.output = output;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const reuse = (output) => new Reused(output);
|
|
14
|
+
const late = Symbol("late");
|
|
15
|
+
const tolerant = async (task, what, call) => {
|
|
16
|
+
let timer;
|
|
17
|
+
const timeout = new Promise((resolve) => timer = setTimeout(resolve, limits.cacheLookupMs, late));
|
|
18
|
+
try {
|
|
19
|
+
const result = await Promise.race([call(), timeout]);
|
|
20
|
+
if (result !== late) return result;
|
|
21
|
+
task.log(`The cache took too long to ${what}; the step ran without it`);
|
|
22
|
+
return;
|
|
23
|
+
} catch (error) {
|
|
24
|
+
if (task.signal.aborted) throw error;
|
|
25
|
+
task.log(`The cache could not ${what}; the step ran without it`);
|
|
26
|
+
return;
|
|
27
|
+
} finally {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const watched = (services, mailbox, actsAs, used) => {
|
|
32
|
+
const mark = (name, target) => {
|
|
33
|
+
if (actsAs.includes(name)) return target;
|
|
34
|
+
return new Proxy(target, { get: (object, key, receiver) => (used.add(name), Reflect.get(object, key, receiver)) });
|
|
35
|
+
};
|
|
36
|
+
const fetch = (url, init) => (JSON.stringify([url, init ?? null]), services.fetch(url, init));
|
|
37
|
+
return {
|
|
38
|
+
services: {
|
|
39
|
+
...services,
|
|
40
|
+
fetch,
|
|
41
|
+
records: mark("records", services.records),
|
|
42
|
+
files: mark("files", services.files)
|
|
43
|
+
},
|
|
44
|
+
mailbox: mailbox && mark("mailbox", mailbox)
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const shapesOf = async (plan, type, task) => {
|
|
48
|
+
if (plan.key === "input") return [{ input: true }];
|
|
49
|
+
if (typeof plan.key === "function") return [{ custom: true }];
|
|
50
|
+
return await tolerant(task, "be read", () => plan.cache.store.shapes(type)) ?? [];
|
|
51
|
+
};
|
|
52
|
+
const customValue = (plan, input, task) => {
|
|
53
|
+
if (typeof plan.key !== "function") return input;
|
|
54
|
+
try {
|
|
55
|
+
return plan.key(input) ?? void 0;
|
|
56
|
+
} catch {
|
|
57
|
+
task.log("The step type's cache key failed; the step ran without the cache");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
const earlierOf = (entry) => ({
|
|
62
|
+
output: entry.output,
|
|
63
|
+
cached_at: entry.cachedAt.toISOString(),
|
|
64
|
+
expires_at: entry.expiresAt.toISOString(),
|
|
65
|
+
secrets_changed: entry.secretsChanged
|
|
66
|
+
});
|
|
67
|
+
const logHit = (task, entry) => {
|
|
68
|
+
task.log(`An earlier result from ${entry.cachedAt.toISOString()} was in the cache`);
|
|
69
|
+
for (const name of entry.secretsChanged) task.log(`The secret ${name} changed since that result was kept`);
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* One call of a step type with its cache on: the result of an earlier call with the same key and identity when the
|
|
73
|
+
* type returns hits, else the step's own run, offered the earlier result when it asks for one, whose fresh result is
|
|
74
|
+
* kept. A failure, a reused result, a result over `limits.cacheValueBytes` and a call that acted as someone its key
|
|
75
|
+
* does not hold are not kept.
|
|
76
|
+
*/
|
|
77
|
+
const cachedRun = async (plan, type, parts, task, run) => {
|
|
78
|
+
const { cache } = plan;
|
|
79
|
+
const keyValue = customValue(plan, parts.input, task);
|
|
80
|
+
if (keyValue === void 0) return run(parts);
|
|
81
|
+
const call = {
|
|
82
|
+
runId: task.runId,
|
|
83
|
+
type,
|
|
84
|
+
identity: plan.identity
|
|
85
|
+
};
|
|
86
|
+
const keys = (await Promise.all((await shapesOf(plan, type, task)).map((shape) => cacheKey(type, shape, keyValue, cache.nameSecrets)))).flatMap((key) => key ? [key.key] : []);
|
|
87
|
+
const entry = keys.length === 0 ? void 0 : await tolerant(task, "be read", () => cache.store.lookup({
|
|
88
|
+
...call,
|
|
89
|
+
keys
|
|
90
|
+
}));
|
|
91
|
+
if (entry) logHit(task, entry);
|
|
92
|
+
if (entry && plan.use === "return") return entry.output;
|
|
93
|
+
const used = /* @__PURE__ */ new Set();
|
|
94
|
+
const tracked = plan.key === "reads" ? trackReads(structuredClone(parts.input)) : void 0;
|
|
95
|
+
const returned = await run({
|
|
96
|
+
...watched(parts.services, parts.mailbox, plan.actsAs, used),
|
|
97
|
+
input: tracked?.input ?? parts.input,
|
|
98
|
+
...entry && plan.use === "offer" && { earlier: earlierOf(entry) }
|
|
99
|
+
});
|
|
100
|
+
if (returned instanceof Reused) return returned.output;
|
|
101
|
+
const result = returned === void 0 ? void 0 : JSON.parse(JSON.stringify(returned));
|
|
102
|
+
await keep(cache, task, {
|
|
103
|
+
...call,
|
|
104
|
+
plan,
|
|
105
|
+
keyValue,
|
|
106
|
+
used,
|
|
107
|
+
shape: tracked ? { reads: tracked.paths() } : plan.key === "input" ? { input: true } : { custom: true },
|
|
108
|
+
result
|
|
109
|
+
});
|
|
110
|
+
return result;
|
|
111
|
+
};
|
|
112
|
+
const keep = async (cache, task, { plan, keyValue, used, shape, result, ...call }) => {
|
|
113
|
+
if (used.size > 0) return task.log(`The result was not cached: the step used ${[...used].sort().join(", ")}`);
|
|
114
|
+
const output = cache.redact(result);
|
|
115
|
+
if (jsonBytes(output) > limits.cacheValueBytes) return task.log(`The result was not cached: it is larger than ${limits.cacheValueBytes / 1024} KiB`);
|
|
116
|
+
const key = await cacheKey(call.type, shape, keyValue, cache.nameSecrets);
|
|
117
|
+
if (!key) return;
|
|
118
|
+
await tolerant(task, "be written", () => cache.store.write({
|
|
119
|
+
...call,
|
|
120
|
+
key: key.key,
|
|
121
|
+
shape,
|
|
122
|
+
output,
|
|
123
|
+
seconds: plan.seconds,
|
|
124
|
+
secrets: key.secrets
|
|
125
|
+
}));
|
|
126
|
+
};
|
|
127
|
+
//#endregion
|
|
128
|
+
export { cachedRun, reuse };
|
package/dist/plugin/context.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { EarlierResult } from "./cache-types.js";
|
|
1
2
|
//#region src/plugin/context.d.ts
|
|
2
3
|
/** A JSON Schema (draft-07), as plain data: what a step or trigger takes and gives. */
|
|
3
4
|
export type JsonSchema = Record<string, unknown>;
|
|
@@ -100,5 +101,9 @@ export type StepContext<Input = Record<string, unknown>> = PluginServices & {
|
|
|
100
101
|
* they may still change the workflow; undefined when the app links no mailboxes (`auth.personalMail`).
|
|
101
102
|
*/
|
|
102
103
|
mailbox: PluginMail | undefined;
|
|
104
|
+
/** For a step type whose cache offers it (`use: 'offer'`): the earlier call's result with the same key, when one serves. */
|
|
105
|
+
earlier?: EarlierResult;
|
|
106
|
+
/** Wraps the result a step takes from `earlier`, cleaned or as it was, so the cache keeps the entry as it is. */
|
|
107
|
+
reuse: (output: unknown) => unknown;
|
|
103
108
|
};
|
|
104
109
|
//#endregion
|
|
@@ -34,6 +34,7 @@ const checkPlugin = (plugin, where = `Workflow plugin "${plugin.name}"`) => {
|
|
|
34
34
|
const connections = Object.keys(plugin.connections ?? {});
|
|
35
35
|
for (const [name, connection] of Object.entries(plugin.connections ?? {})) checkConnection(where, name, connection);
|
|
36
36
|
for (const [key, type] of [...Object.entries(plugin.triggers ?? {}), ...Object.entries(plugin.steps ?? {})]) if (type.connection !== void 0 && !connections.includes(type.connection)) throw new Error(`${where}: ${key} uses the connection "${type.connection}", which the plugin does not declare`);
|
|
37
|
+
for (const [key, type] of Object.entries(plugin.steps ?? {})) if (type.cache?.choice === "discouraged" && !type.cache.disclaimer?.trim()) throw new Error(`${where}: ${key} advises against forcing its cache on; give the cache a \`disclaimer\` that says why`);
|
|
37
38
|
};
|
|
38
39
|
/** Declares a workflow plugin, checking its names: a type is `<name>:<key>`, such as `app:http` or `acme:new_order`. */
|
|
39
40
|
const definePlugin = (plugin) => {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { StepError } from "../run/errors.js";
|
|
2
|
+
import { cachePlan } from "./cache-plan.js";
|
|
3
|
+
import { cachedRun, reuse } from "./cached-run.js";
|
|
2
4
|
import { Ajv } from "ajv";
|
|
3
5
|
//#region src/plugin/step-handlers.ts
|
|
4
6
|
const ajv = new Ajv({
|
|
@@ -15,8 +17,8 @@ const handler = (id, type, wiring) => {
|
|
|
15
17
|
}
|
|
16
18
|
const connection = type.connection === void 0 ? void 0 : wiring.connection(id, type.connection, task);
|
|
17
19
|
const { runId, idempotencyKey, signal, log, redact } = task;
|
|
18
|
-
|
|
19
|
-
...
|
|
20
|
+
const run = ({ services, input, mailbox, earlier }) => type.run({
|
|
21
|
+
...services,
|
|
20
22
|
input,
|
|
21
23
|
runId,
|
|
22
24
|
idempotencyKey,
|
|
@@ -24,8 +26,19 @@ const handler = (id, type, wiring) => {
|
|
|
24
26
|
log,
|
|
25
27
|
redact,
|
|
26
28
|
connection,
|
|
27
|
-
mailbox
|
|
29
|
+
mailbox,
|
|
30
|
+
reuse,
|
|
31
|
+
...earlier && { earlier }
|
|
28
32
|
});
|
|
33
|
+
const parts = {
|
|
34
|
+
services: wiring.services(task),
|
|
35
|
+
input,
|
|
36
|
+
mailbox: wiring.mailbox?.(task)
|
|
37
|
+
};
|
|
38
|
+
const plan = cachePlan(id, type, parts.input, task.cache);
|
|
39
|
+
if (plan && "invalidTtl" in plan) log("The step type's cache gave no valid time to live; the step ran without the cache");
|
|
40
|
+
if (!plan || "invalidTtl" in plan) return run(parts);
|
|
41
|
+
return cachedRun(plan, id, parts, task, run);
|
|
29
42
|
};
|
|
30
43
|
};
|
|
31
44
|
/** The runner's step types from the plugins' steps, each checking its input against its schema before it runs. */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CacheChoice } from "./cache-types.js";
|
|
1
2
|
import { JsonSchema } from "./context.js";
|
|
2
3
|
import { ConnectionDeclaration, Plugin, TriggerType } from "./types.js";
|
|
3
4
|
//#region src/plugin/type-list.d.ts
|
|
@@ -15,6 +16,12 @@ export type TypeEntry = {
|
|
|
15
16
|
connection?: {
|
|
16
17
|
name: string;
|
|
17
18
|
} & ConnectionDeclaration;
|
|
19
|
+
/** A step type's cache advice: how much the workflow's author decides, the switch's default, and the warning before forcing it on. */
|
|
20
|
+
cache?: {
|
|
21
|
+
choice: CacheChoice;
|
|
22
|
+
default?: boolean;
|
|
23
|
+
disclaimer?: string;
|
|
24
|
+
};
|
|
18
25
|
/** False when this app cannot run it, with the reason; a definition that uses it is not saved. */
|
|
19
26
|
available: boolean;
|
|
20
27
|
reason?: string;
|
package/dist/plugin/type-list.js
CHANGED
|
@@ -11,7 +11,13 @@ const entry = (plugin, key, type) => ({
|
|
|
11
11
|
...type.connection !== void 0 && { connection: {
|
|
12
12
|
name: type.connection,
|
|
13
13
|
...plugin.connections[type.connection]
|
|
14
|
-
} }
|
|
14
|
+
} },
|
|
15
|
+
..."cache" in type && type.cache && { cache: advice(type.cache) }
|
|
16
|
+
});
|
|
17
|
+
const advice = ({ choice, default: on, disclaimer }) => ({
|
|
18
|
+
choice,
|
|
19
|
+
...on !== void 0 && { default: on },
|
|
20
|
+
...disclaimer !== void 0 && { disclaimer }
|
|
15
21
|
});
|
|
16
22
|
/** Every type of `plugins`, steps first, each marked available or not by `unavailable`. */
|
|
17
23
|
const typeList = (plugins, unavailable) => plugins.flatMap((plugin) => [...Object.entries(plugin.steps ?? {}), ...Object.entries(plugin.triggers ?? {})].map(([key, type]) => entry(plugin, key, type))).map((type) => {
|
package/dist/plugin/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { StepCache } from "./cache-types.js";
|
|
1
2
|
import { JsonSchema, PluginConnection, PluginFetch, StepContext } from "./context.js";
|
|
2
3
|
//#region src/plugin/types.d.ts
|
|
3
4
|
/** What a type shows in the type list the MCP tools and the editor read. */
|
|
@@ -17,6 +18,8 @@ export type StepType = Described & {
|
|
|
17
18
|
errors?: Record<string, string>;
|
|
18
19
|
/** The prefix of its error names, such as `Http`; an input that does not fit fails as `<prefix>.InvalidArguments`. Default `Step`. */
|
|
19
20
|
errorPrefix?: string;
|
|
21
|
+
/** Its result cache; without it the engine keeps and looks up nothing for it unless a workflow forces it on. */
|
|
22
|
+
cache?: StepCache;
|
|
20
23
|
run: (context: StepContext) => Promise<unknown>;
|
|
21
24
|
};
|
|
22
25
|
/** What every trigger type has: its description, and a check of its input beyond its schema, such as a cron's fields. */
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { InputPath } from "./read-tracker.js";
|
|
2
|
+
//#region src/run/cache/cache-key.d.ts
|
|
3
|
+
/** Which part of the input a cache key covers: the values a step read, its whole input, or the value of a key function. */
|
|
4
|
+
export type KeyShape = {
|
|
5
|
+
reads: InputPath[];
|
|
6
|
+
} | {
|
|
7
|
+
input: true;
|
|
8
|
+
} | {
|
|
9
|
+
custom: true;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { canonicalJson } from "./canonical.js";
|
|
2
|
+
//#region src/run/cache/cache-key.ts
|
|
3
|
+
/** Whether a path is in the input, and its value there, through own properties only. */
|
|
4
|
+
const at = (input, path) => {
|
|
5
|
+
let value = input;
|
|
6
|
+
for (const key of path) {
|
|
7
|
+
if (typeof value !== "object" || value === null || !Object.hasOwn(value, key)) return [false, null];
|
|
8
|
+
value = value[key];
|
|
9
|
+
}
|
|
10
|
+
return [true, value];
|
|
11
|
+
};
|
|
12
|
+
const hex = (bytes) => [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
13
|
+
/**
|
|
14
|
+
* The cache key of one call of a step type for one shape: the SHA-256 of the type, the shape and the values it covers,
|
|
15
|
+
* as canonical JSON with each secret written as its name, and the names of the secrets it held. `value` is the input,
|
|
16
|
+
* or for a `custom` shape the key function's value. Undefined when the value has no JSON form.
|
|
17
|
+
*/
|
|
18
|
+
const cacheKey = async (type, shape, value, nameSecrets) => {
|
|
19
|
+
const covered = "reads" in shape ? shape.reads.map((path) => [path, ...at(value, path)]) : value;
|
|
20
|
+
let named;
|
|
21
|
+
let text;
|
|
22
|
+
try {
|
|
23
|
+
named = nameSecrets(covered);
|
|
24
|
+
text = canonicalJson({
|
|
25
|
+
v: 1,
|
|
26
|
+
type,
|
|
27
|
+
shape,
|
|
28
|
+
value: named.value
|
|
29
|
+
});
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error instanceof TypeError || error instanceof RangeError) return;
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
key: hex(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text))),
|
|
36
|
+
secrets: named.secrets
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
export { cacheKey };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { SecretNamer } from "./secret-names.js";
|
|
2
|
+
import { KeyShape } from "./cache-key.js";
|
|
3
|
+
//#region src/run/cache/cache-port.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Who a cached result was made as, beside the run's tenant, which every entry is kept under: the account the run's
|
|
6
|
+
* version binds to the type's `connection` (`<plugin>:<name>`), the person who saved it (`saver`, as records act), and
|
|
7
|
+
* the person who turned it on (`enabler`, as the mailbox sends). The store reads them from the run, never from the step.
|
|
8
|
+
*/
|
|
9
|
+
export type CacheIdentity = {
|
|
10
|
+
connection?: string;
|
|
11
|
+
saver: boolean;
|
|
12
|
+
enabler: boolean;
|
|
13
|
+
};
|
|
14
|
+
/** An earlier result a store found, with the secrets whose value changed since it was kept, by name. */
|
|
15
|
+
export type CacheEntry = {
|
|
16
|
+
output: unknown;
|
|
17
|
+
cachedAt: Date;
|
|
18
|
+
expiresAt: Date;
|
|
19
|
+
secretsChanged: string[];
|
|
20
|
+
};
|
|
21
|
+
export type CacheCall = {
|
|
22
|
+
runId: string;
|
|
23
|
+
type: string;
|
|
24
|
+
identity: CacheIdentity;
|
|
25
|
+
};
|
|
26
|
+
/** Where step results are kept for later calls of the same step type, from any run or workflow. */
|
|
27
|
+
export type StepCacheStore = {
|
|
28
|
+
/** The key shapes the type's entries were kept under. */
|
|
29
|
+
shapes(type: string): Promise<KeyShape[]>;
|
|
30
|
+
/** The newest entry that has not expired under one of `keys`, made under the run's identity. */
|
|
31
|
+
lookup(call: CacheCall & {
|
|
32
|
+
keys: string[];
|
|
33
|
+
}): Promise<CacheEntry | undefined>;
|
|
34
|
+
/** Keeps a result for `seconds`; `secrets` names the secrets its key held, whose values the store fingerprints. */
|
|
35
|
+
write(call: CacheCall & {
|
|
36
|
+
key: string;
|
|
37
|
+
shape: KeyShape;
|
|
38
|
+
output: unknown;
|
|
39
|
+
seconds: number;
|
|
40
|
+
secrets: string[];
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
};
|
|
43
|
+
/** What a step type's handler gets of the cache: the Task's `Cache`, the store, and what keys and kept results need. */
|
|
44
|
+
export type TaskCache = {
|
|
45
|
+
setting: boolean | undefined;
|
|
46
|
+
store: StepCacheStore;
|
|
47
|
+
nameSecrets: SecretNamer;
|
|
48
|
+
/** The value with the app's secrets blanked out, as the step's record keeps it. */
|
|
49
|
+
redact: <T>(value: T) => T;
|
|
50
|
+
};
|
|
51
|
+
//#endregion
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { limits } from "../../limits.js";
|
|
2
|
+
//#region src/run/cache/cache-ttl.ts
|
|
3
|
+
const units = {
|
|
4
|
+
second: 1,
|
|
5
|
+
minute: 60,
|
|
6
|
+
hour: 3600,
|
|
7
|
+
day: 86400
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* The seconds a cache entry serves, from a number of seconds or text such as `10 minutes`, at most
|
|
11
|
+
* `limits.cacheMaxSeconds`; undefined for anything else, zero and below included, so such an entry is never written.
|
|
12
|
+
*/
|
|
13
|
+
const cacheSeconds = (ttl) => {
|
|
14
|
+
if (typeof ttl === "number") return Number.isFinite(ttl) && ttl >= 1 ? Math.min(Math.floor(ttl), limits.cacheMaxSeconds) : void 0;
|
|
15
|
+
if (typeof ttl !== "string") return;
|
|
16
|
+
const match = /^(\d+) (second|minute|hour|day)s?$/.exec(ttl.trim());
|
|
17
|
+
if (!match) return;
|
|
18
|
+
return cacheSeconds(Number(match[1]) * units[match[2]]);
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
export { cacheSeconds };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
//#region src/run/cache/canonical.ts
|
|
2
|
+
/**
|
|
3
|
+
* The JSON text of a value with every object's keys in code-unit order, so two equal values always give the same text.
|
|
4
|
+
* Strings, keys and structure are written as JSON writes them, so no value can pass for another's text.
|
|
5
|
+
*/
|
|
6
|
+
const canonicalJson = (value) => {
|
|
7
|
+
if (Array.isArray(value)) return `[${value.map((item) => canonicalJson(item ?? null)).join(",")}]`;
|
|
8
|
+
if (typeof value === "object" && value !== null) return `{${Object.keys(value).filter((key) => value[key] !== void 0).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`).join(",")}}`;
|
|
9
|
+
const text = JSON.stringify(value);
|
|
10
|
+
if (text === void 0) throw new TypeError(`A ${typeof value} has no JSON form`);
|
|
11
|
+
return text;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { canonicalJson };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//#region src/run/cache/read-tracker.ts
|
|
2
|
+
const pathKey = (path) => JSON.stringify(path);
|
|
3
|
+
/**
|
|
4
|
+
* A view of `input` that records which of its values a step reads: a key read records that key's path, its value
|
|
5
|
+
* when it is a primitive; an array, and an object whose keys are listed (as `JSON.stringify`, `Object.keys`, a spread or
|
|
6
|
+
* `for…in` list them), records its whole path. `paths()` gives the paths read, each one no other path read contains.
|
|
7
|
+
*/
|
|
8
|
+
const trackReads = (input) => {
|
|
9
|
+
const read = /* @__PURE__ */ new Map();
|
|
10
|
+
const record = (path) => void read.set(pathKey(path), path);
|
|
11
|
+
const views = /* @__PURE__ */ new Map();
|
|
12
|
+
const view = (target, path) => {
|
|
13
|
+
const known = views.get(pathKey(path));
|
|
14
|
+
if (known) return known;
|
|
15
|
+
const proxy = new Proxy(target, {
|
|
16
|
+
get(object, key, receiver) {
|
|
17
|
+
if (typeof key === "symbol") return Reflect.get(object, key, receiver);
|
|
18
|
+
const at = [...path, key];
|
|
19
|
+
if (!Object.hasOwn(object, key)) {
|
|
20
|
+
record(at);
|
|
21
|
+
return Reflect.get(object, key, receiver);
|
|
22
|
+
}
|
|
23
|
+
const value = object[key];
|
|
24
|
+
if (Array.isArray(value) || typeof value !== "object" || value === null) {
|
|
25
|
+
record(at);
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
return view(value, at);
|
|
29
|
+
},
|
|
30
|
+
has(object, key) {
|
|
31
|
+
if (typeof key === "string") record([...path, key]);
|
|
32
|
+
return Reflect.has(object, key);
|
|
33
|
+
},
|
|
34
|
+
ownKeys(object) {
|
|
35
|
+
record(path);
|
|
36
|
+
return Reflect.ownKeys(object);
|
|
37
|
+
},
|
|
38
|
+
getOwnPropertyDescriptor(object, key) {
|
|
39
|
+
record(path);
|
|
40
|
+
return Reflect.getOwnPropertyDescriptor(object, key);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
views.set(pathKey(path), proxy);
|
|
44
|
+
return proxy;
|
|
45
|
+
};
|
|
46
|
+
const paths = () => {
|
|
47
|
+
const all = [...read.values()].sort((a, b) => a.length - b.length);
|
|
48
|
+
const kept = [];
|
|
49
|
+
for (const path of all) if (!kept.some((outer) => outer.every((key, i) => path[i] === key))) kept.push(path);
|
|
50
|
+
return kept.sort((a, b) => pathKey(a) < pathKey(b) ? -1 : 1);
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
input: view(input, []),
|
|
54
|
+
paths
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
//#endregion
|
|
58
|
+
export { trackReads };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/run/cache/secret-names.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Replaces each secret's value inside the texts of a value, its keys too, by `[secret:<NAME>]`, the longest value first,
|
|
4
|
+
* and says which secrets it found: a cache key holds a secret's name, never its value. The secrets are at least
|
|
5
|
+
* `shortestSecret` characters long, which the runner checks at startup.
|
|
6
|
+
*/
|
|
7
|
+
export declare const secretNamer: (secrets: Readonly<Record<string, string>>) => <T>(value: T) => {
|
|
8
|
+
value: T;
|
|
9
|
+
secrets: string[];
|
|
10
|
+
};
|
|
11
|
+
export type SecretNamer = ReturnType<typeof secretNamer>;
|
|
12
|
+
//#endregion
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/run/cache/secret-names.ts
|
|
2
|
+
/**
|
|
3
|
+
* Replaces each secret's value inside the texts of a value, its keys too, by `[secret:<NAME>]`, the longest value first,
|
|
4
|
+
* and says which secrets it found: a cache key holds a secret's name, never its value. The secrets are at least
|
|
5
|
+
* `shortestSecret` characters long, which the runner checks at startup.
|
|
6
|
+
*/
|
|
7
|
+
const secretNamer = (secrets) => {
|
|
8
|
+
const ordered = Object.entries(secrets).sort(([, a], [, b]) => b.length - a.length);
|
|
9
|
+
return (value) => {
|
|
10
|
+
const found = /* @__PURE__ */ new Set();
|
|
11
|
+
const text = (input) => ordered.reduce((result, [name, secret]) => {
|
|
12
|
+
if (!result.includes(secret)) return result;
|
|
13
|
+
found.add(name);
|
|
14
|
+
return result.replaceAll(secret, `[secret:${name}]`);
|
|
15
|
+
}, input);
|
|
16
|
+
const walk = (input) => {
|
|
17
|
+
if (typeof input === "string") return text(input);
|
|
18
|
+
if (Array.isArray(input)) return input.map(walk);
|
|
19
|
+
if (typeof input !== "object" || input === null) return input;
|
|
20
|
+
return Object.fromEntries(Object.entries(input).map(([key, item]) => [text(key), walk(item)]));
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
value: walk(value),
|
|
24
|
+
secrets: [...found].sort()
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
export { secretNamer };
|
package/dist/run/port.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Definition } from "../definition/types.js";
|
|
2
|
+
import { TaskCache } from "./cache/cache-port.js";
|
|
2
3
|
//#region src/run/port.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* How a step ended: `succeeded`, `retrying` (it failed and a retrier tries it again), `caught` (it failed and a
|
|
@@ -53,6 +54,8 @@ export type TaskContext = {
|
|
|
53
54
|
redact: (text: string) => string;
|
|
54
55
|
/** Blanks `value` out of all this step and the rest of its segment record, such as an access token it was given. */
|
|
55
56
|
secret: (value: string) => void;
|
|
57
|
+
/** The step cache, when the app keeps one; a step type that caches nothing never touches it. */
|
|
58
|
+
cache?: TaskCache;
|
|
56
59
|
};
|
|
57
60
|
/**
|
|
58
61
|
* A step type, which a Task names as its `Resource`: it takes the Task's evaluated `Arguments` and returns its result.
|
package/dist/run/runner.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { StepCacheStore } from "./cache/cache-port.js";
|
|
1
2
|
import { RunStore, SegmentQueue, TaskHandlers } from "./port.js";
|
|
2
3
|
//#region src/run/runner.d.ts
|
|
3
4
|
/**
|
|
@@ -8,6 +9,8 @@ export type RunnerPort<Db> = {
|
|
|
8
9
|
store: RunStore<Db>;
|
|
9
10
|
queue: SegmentQueue<Db>;
|
|
10
11
|
tasks: TaskHandlers;
|
|
12
|
+
/** Where step types that cache keep their results. */
|
|
13
|
+
cache?: StepCacheStore;
|
|
11
14
|
secrets?: Readonly<Record<string, string>>;
|
|
12
15
|
now?: () => Date;
|
|
13
16
|
budgetMs?: number;
|
|
@@ -17,7 +20,7 @@ export type RunnerPort<Db> = {
|
|
|
17
20
|
* The workflow runner over a port. `start` queues a run's first segment in `db`, which may be the caller's
|
|
18
21
|
* transaction, so the run starts when it commits; `segment` is what the queue's job runs.
|
|
19
22
|
*/
|
|
20
|
-
export declare const createRunner: <Db>({ store, queue, tasks, secrets, now, budgetMs, maxSteps }: RunnerPort<Db>) => {
|
|
23
|
+
export declare const createRunner: <Db>({ store, queue, tasks, cache, secrets, now, budgetMs, maxSteps }: RunnerPort<Db>) => {
|
|
21
24
|
start: (db: Db, runId: string) => Promise<void>;
|
|
22
25
|
/**
|
|
23
26
|
* Runs the run from where it stands to its next wait, then queues the next segment or ends the run. Run again after
|
package/dist/run/runner.js
CHANGED
|
@@ -7,7 +7,7 @@ const ended = /* @__PURE__ */ new Set(["succeeded", "failed"]);
|
|
|
7
7
|
* The workflow runner over a port. `start` queues a run's first segment in `db`, which may be the caller's
|
|
8
8
|
* transaction, so the run starts when it commits; `segment` is what the queue's job runs.
|
|
9
9
|
*/
|
|
10
|
-
const createRunner = ({ store, queue, tasks, secrets = {}, now = () => /* @__PURE__ */ new Date(), budgetMs = limits.segmentMs, maxSteps = limits.runSteps }) => {
|
|
10
|
+
const createRunner = ({ store, queue, tasks, cache, secrets = {}, now = () => /* @__PURE__ */ new Date(), budgetMs = limits.segmentMs, maxSteps = limits.runSteps }) => {
|
|
11
11
|
secretRedactor(Object.values(secrets));
|
|
12
12
|
return {
|
|
13
13
|
start: (db, runId) => queue.schedule(db, runId),
|
|
@@ -28,6 +28,7 @@ const createRunner = ({ store, queue, tasks, secrets = {}, now = () => /* @__PUR
|
|
|
28
28
|
secrets,
|
|
29
29
|
redactor,
|
|
30
30
|
tasks,
|
|
31
|
+
...cache && { cache },
|
|
31
32
|
signal,
|
|
32
33
|
now,
|
|
33
34
|
budgetMs,
|
package/dist/run/segment.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Definition } from "../definition/types.js";
|
|
2
2
|
import { Redactor } from "./redact.js";
|
|
3
|
+
import { StepCacheStore } from "./cache/cache-port.js";
|
|
3
4
|
import { SavedStep, StepRecord, TaskHandlers } from "./port.js";
|
|
4
5
|
import { ScopeOutcome } from "./context.js";
|
|
5
6
|
//#region src/run/segment.d.ts
|
|
@@ -18,6 +19,8 @@ export type SegmentInput = {
|
|
|
18
19
|
/** Saves steps in one guarded statement: false when any of their scopes and seqs is taken. */
|
|
19
20
|
save: (steps: readonly StepRecord[]) => Promise<boolean>;
|
|
20
21
|
tasks: TaskHandlers;
|
|
22
|
+
/** Where step types that cache keep their results; none keeps nothing. */
|
|
23
|
+
cache?: StepCacheStore;
|
|
21
24
|
signal: AbortSignal;
|
|
22
25
|
now?: () => Date;
|
|
23
26
|
/** How long the segment takes new steps. Default `limits.segmentMs`. */
|
|
@@ -29,7 +32,7 @@ export type SegmentInput = {
|
|
|
29
32
|
* Runs a run's steps from where its saved steps leave it to its next wait: it ends or fails, waits until a time
|
|
30
33
|
* (a Wait, a retry's delay), or yields once its time is up. `superseded` when another segment saved a step first.
|
|
31
34
|
*/
|
|
32
|
-
export declare const runSegment: ({ runId, definition, input, startedAt, secrets, redactor, saved, save, tasks, signal, now, budgetMs, maxSteps }: SegmentInput) => Promise<ScopeOutcome | {
|
|
35
|
+
export declare const runSegment: ({ runId, definition, input, startedAt, secrets, redactor, saved, save, tasks, cache, signal, now, budgetMs, maxSteps }: SegmentInput) => Promise<ScopeOutcome | {
|
|
33
36
|
kind: "superseded";
|
|
34
37
|
}>;
|
|
35
38
|
//#endregion
|
package/dist/run/segment.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { limits } from "../limits.js";
|
|
2
2
|
import { SupersededError } from "./errors.js";
|
|
3
|
+
import { secretNamer } from "./cache/secret-names.js";
|
|
3
4
|
import { runScope } from "./scope.js";
|
|
4
5
|
//#region src/run/segment.ts
|
|
5
6
|
/**
|
|
6
7
|
* Runs a run's steps from where its saved steps leave it to its next wait: it ends or fails, waits until a time
|
|
7
8
|
* (a Wait, a retry's delay), or yields once its time is up. `superseded` when another segment saved a step first.
|
|
8
9
|
*/
|
|
9
|
-
const runSegment = async ({ runId, definition, input, startedAt, secrets, redactor, saved, save, tasks, signal, now = () => /* @__PURE__ */ new Date(), budgetMs = limits.segmentMs, maxSteps = limits.runSteps }) => {
|
|
10
|
+
const runSegment = async ({ runId, definition, input, startedAt, secrets, redactor, saved, save, tasks, cache, signal, now = () => /* @__PURE__ */ new Date(), budgetMs = limits.segmentMs, maxSteps = limits.runSteps }) => {
|
|
10
11
|
const byScope = /* @__PURE__ */ new Map();
|
|
11
12
|
for (const step of [...saved].sort((a, b) => a.seq - b.seq)) {
|
|
12
13
|
const steps = byScope.get(step.scope);
|
|
@@ -33,6 +34,10 @@ const runSegment = async ({ runId, definition, input, startedAt, secrets, redact
|
|
|
33
34
|
},
|
|
34
35
|
redactor,
|
|
35
36
|
tasks,
|
|
37
|
+
...cache && { cache: {
|
|
38
|
+
store: cache,
|
|
39
|
+
nameSecrets: secretNamer(secrets)
|
|
40
|
+
} },
|
|
36
41
|
signal,
|
|
37
42
|
now,
|
|
38
43
|
deadline: Date.now() + budgetMs,
|
package/dist/run/task.js
CHANGED
|
@@ -27,7 +27,12 @@ const runTask = async (context, state, args, idempotencyKey, logs) => {
|
|
|
27
27
|
idempotencyKey,
|
|
28
28
|
log,
|
|
29
29
|
redact: context.redactor.text,
|
|
30
|
-
secret: context.redactor.add
|
|
30
|
+
secret: context.redactor.add,
|
|
31
|
+
...context.cache && { cache: {
|
|
32
|
+
...context.cache,
|
|
33
|
+
setting: state.Cache,
|
|
34
|
+
redact: context.redactor.value
|
|
35
|
+
} }
|
|
31
36
|
}), aborted(signal)]);
|
|
32
37
|
} catch (error) {
|
|
33
38
|
if (context.signal.aborted || error instanceof SupersededError) throw error;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/workflows",
|
|
3
|
-
"version": "0.71.
|
|
3
|
+
"version": "0.71.2",
|
|
4
4
|
"description": "Workflows as state machines with JSONata expressions: their check, a runner that saves every step, and the plugin interface for trigger and step types.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|