@segmentstream/ontology 0.196.1 → 0.197.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 +14 -0
- package/dist/internal/diagnostics.js +1 -0
- package/dist/server.js +12 -0
- package/dist/service/workflow-deployment.js +5 -0
- package/dist/service/workflow-errors.d.ts +6 -0
- package/dist/service/workflow-errors.js +36 -0
- package/dist/service/workflow-output.d.ts +40 -0
- package/dist/service/workflow-output.js +166 -0
- package/dist/service/workflows.d.ts +26 -3
- package/dist/service/workflows.js +62 -12
- package/dist/workflow-runtime.d.ts +5 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.197.1
|
|
4
|
+
|
|
5
|
+
A workflow run that the runtime fails for reaching its event limit, its replay limit or its
|
|
6
|
+
queue deliveries now says so. Run detail, in `segmentstream workflow run get` and Cloud, shows
|
|
7
|
+
the runtime's classification as the error code (`MAX_EVENTS_EXCEEDED`, `REPLAY_TIMEOUT` or
|
|
8
|
+
`MAX_DELIVERIES_EXCEEDED`) and the runtime's statement of the limit as the message, such as
|
|
9
|
+
`Workflow exceeded the maximum of 25000 events per run`; the runs list carries the code. Before,
|
|
10
|
+
such a run showed only `MaxEventsExceededError` or `FatalError` with no message. Redeploy the
|
|
11
|
+
workspace after upgrading.
|
|
12
|
+
|
|
13
|
+
## 0.197.0
|
|
14
|
+
|
|
15
|
+
A workflow run's detail now carries what the run returned and what each completed step returned, as `output`: the value as JSON, redacted by the same rules as logs. A value whose JSON text is longer than 4 KB is cut to that length and marked `outputTruncated: true`; a value the service cannot decrypt or deserialize is answered with `output: null` and `outputUnavailable`, with the reference of its operator report. `segmentstream workflow run get`, its `--output json` and the ontology MCP tool show them. Upgrade and redeploy the workspace to serve them.
|
|
16
|
+
|
|
3
17
|
## 0.196.1
|
|
4
18
|
|
|
5
19
|
A Telegram rich message the ontology sends shows a table written on the line right after text as a
|
package/dist/server.js
CHANGED
|
@@ -92,6 +92,18 @@ export function installWorkflowRuntime() {
|
|
|
92
92
|
? hydrateStepError(value, run.runId, key)
|
|
93
93
|
: hydrateRunError(value, run.runId, key);
|
|
94
94
|
},
|
|
95
|
+
hydrateOutput: async (value, run, kind) => {
|
|
96
|
+
const { hydrateStepReturnValue, hydrateWorkflowReturnValue } = await import("@workflow/core/serialization");
|
|
97
|
+
const key = await runEncryptionKey(run);
|
|
98
|
+
// A run's returned streams are revived inert: reading the run never opens their data.
|
|
99
|
+
const inert = {
|
|
100
|
+
ReadableStream: () => Object.create(ReadableStream.prototype),
|
|
101
|
+
WritableStream: () => Object.create(WritableStream.prototype),
|
|
102
|
+
};
|
|
103
|
+
return kind === "step"
|
|
104
|
+
? hydrateStepReturnValue(value, run.runId, key)
|
|
105
|
+
: hydrateWorkflowReturnValue(value, run.runId, key, [], globalThis, inert);
|
|
106
|
+
},
|
|
95
107
|
});
|
|
96
108
|
}
|
|
97
109
|
/** The key the world encrypts a run's data with, or `undefined` when it does not encrypt. */
|
|
@@ -95,6 +95,11 @@ export function deploymentWorkflowEngine(engine, observe) {
|
|
|
95
95
|
await requireRun(runId);
|
|
96
96
|
return engine.listSteps(runId, options);
|
|
97
97
|
},
|
|
98
|
+
...(engine.getRunOutput
|
|
99
|
+
? {
|
|
100
|
+
getRunOutput: async (run) => engine.getRunOutput(await requireRun(run.runId)),
|
|
101
|
+
}
|
|
102
|
+
: {}),
|
|
98
103
|
...(engine.getRunArguments
|
|
99
104
|
? {
|
|
100
105
|
// Only a run of this deployment is read, as getRun answers.
|
|
@@ -24,3 +24,9 @@ export declare class WorkflowStepFailed extends Error {
|
|
|
24
24
|
export declare function stepFailure(cause: Cause.Cause<unknown>, requestId: string, step: WorkflowStepContext, workflow: string, credentials: readonly (string | undefined)[]): WorkflowStepFailed;
|
|
25
25
|
/** Find our envelope through retry-exhaustion/run wrappers, with bounded cycle-safe traversal. */
|
|
26
26
|
export declare function workflowErrorView(value: unknown): WorkflowErrorView | null;
|
|
27
|
+
/**
|
|
28
|
+
* A run's error, with the runtime's own classification when it failed the run for outgrowing a
|
|
29
|
+
* per-run limit: the code (`MAX_EVENTS_EXCEEDED`, `REPLAY_TIMEOUT`, `MAX_DELIVERIES_EXCEEDED`) and
|
|
30
|
+
* the first line of the runtime's message, which names the limit. Other failures are unchanged.
|
|
31
|
+
*/
|
|
32
|
+
export declare function workflowRunErrorView(value: unknown, errorCode: unknown): WorkflowErrorView | null;
|
|
@@ -23,6 +23,18 @@ const DETERMINISTIC_FAILURES = new Set([
|
|
|
23
23
|
"AgentTaskRefused",
|
|
24
24
|
"DeploymentInterrupted",
|
|
25
25
|
]);
|
|
26
|
+
/**
|
|
27
|
+
* Run failures the Workflow runtime classifies itself when a run outgrows a per-run limit. The
|
|
28
|
+
* runtime builds these errors itself (`MaxEventsExceededError`, and the `FatalError`s of its replay
|
|
29
|
+
* budget and run delivery count), so their first line states the limit and carries no workspace
|
|
30
|
+
* data; the hint below it names the runtime's environment, which workspaces do not set.
|
|
31
|
+
*/
|
|
32
|
+
const RUNTIME_LIMIT_FAILURES = new Set([
|
|
33
|
+
"MAX_EVENTS_EXCEEDED",
|
|
34
|
+
"REPLAY_TIMEOUT",
|
|
35
|
+
"MAX_DELIVERIES_EXCEEDED",
|
|
36
|
+
]);
|
|
37
|
+
const MAX_RUNTIME_MESSAGE_CHARS = 300;
|
|
26
38
|
const TAG = /^[A-Za-z][A-Za-z0-9_]{0,99}$/;
|
|
27
39
|
const ID = /^[A-Za-z0-9_.:-]{1,200}$/;
|
|
28
40
|
function record(value) {
|
|
@@ -179,3 +191,27 @@ export function workflowErrorView(value) {
|
|
|
179
191
|
const tag = typeof name === "string" && TAG.test(name) ? name : null;
|
|
180
192
|
return publicError(item ?? {}, tag);
|
|
181
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* A run's error, with the runtime's own classification when it failed the run for outgrowing a
|
|
196
|
+
* per-run limit: the code (`MAX_EVENTS_EXCEEDED`, `REPLAY_TIMEOUT`, `MAX_DELIVERIES_EXCEEDED`) and
|
|
197
|
+
* the first line of the runtime's message, which names the limit. Other failures are unchanged.
|
|
198
|
+
*/
|
|
199
|
+
export function workflowRunErrorView(value, errorCode) {
|
|
200
|
+
const view = workflowErrorView(value);
|
|
201
|
+
if (typeof errorCode !== "string" || !RUNTIME_LIMIT_FAILURES.has(errorCode)) {
|
|
202
|
+
return view;
|
|
203
|
+
}
|
|
204
|
+
const item = record(value);
|
|
205
|
+
const firstLine = value instanceof Error || typeof item?.message === "string"
|
|
206
|
+
? String(item.message)
|
|
207
|
+
.split("\n", 1)[0]
|
|
208
|
+
.trim()
|
|
209
|
+
: "";
|
|
210
|
+
return {
|
|
211
|
+
...(view ?? { tag: null, message: null, code: errorCode }),
|
|
212
|
+
code: errorCode,
|
|
213
|
+
message: firstLine === ""
|
|
214
|
+
? (view?.message ?? null)
|
|
215
|
+
: makeRedactor(environmentSecrets(process.env))(firstLine).slice(0, MAX_RUNTIME_MESSAGE_CHARS),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** The most bytes of a value's JSON text a run or step view carries. */
|
|
2
|
+
export declare const MAX_WORKFLOW_OUTPUT_BYTES = 4096;
|
|
3
|
+
/**
|
|
4
|
+
* What a run or a step returned, as the engine read it: the hydrated value, or that it could not be
|
|
5
|
+
* read, with the reference of the operator report that says why. Absent when nothing was recorded:
|
|
6
|
+
* the run or step has not completed, or it returned nothing.
|
|
7
|
+
*/
|
|
8
|
+
export type WorkflowOutputRead = {
|
|
9
|
+
readonly value: unknown;
|
|
10
|
+
} | {
|
|
11
|
+
readonly unavailable: true;
|
|
12
|
+
readonly reference?: string;
|
|
13
|
+
};
|
|
14
|
+
/** Hydrates only a returned value: the Workflow SDK's step or workflow return-value reader. */
|
|
15
|
+
export type HydrateOutput = (value: unknown, run: {
|
|
16
|
+
readonly runId: string;
|
|
17
|
+
readonly deploymentId?: string;
|
|
18
|
+
}, kind: "run" | "step") => Promise<unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* Reads what a run or step returned from its stored payload. A payload that cannot be decrypted or
|
|
21
|
+
* deserialized is reported to the operator logs and answered as unavailable with that reference.
|
|
22
|
+
*/
|
|
23
|
+
export declare function readWorkflowOutput(stored: unknown, run: {
|
|
24
|
+
readonly runId: string;
|
|
25
|
+
readonly deploymentId?: string;
|
|
26
|
+
}, kind: "run" | "step", hydrate: HydrateOutput): Promise<WorkflowOutputRead | undefined>;
|
|
27
|
+
/** A returned value that could not be read: the failure goes to the operator logs. */
|
|
28
|
+
export declare function workflowOutputUnavailable(error: unknown, runId: string, operation: string): WorkflowOutputRead;
|
|
29
|
+
/**
|
|
30
|
+
* The view's fields for a returned value: `output`, the value as JSON, redacted like the logs; and,
|
|
31
|
+
* when its JSON text is longer than {@link MAX_WORKFLOW_OUTPUT_BYTES}, `outputTruncated: true` with
|
|
32
|
+
* `output` that text cut to the limit. `outputUnavailable` says the value could not be read.
|
|
33
|
+
*/
|
|
34
|
+
export declare function workflowOutputView(read: WorkflowOutputRead | undefined): {
|
|
35
|
+
readonly output: unknown;
|
|
36
|
+
readonly outputTruncated?: true;
|
|
37
|
+
readonly outputUnavailable?: {
|
|
38
|
+
readonly reference?: string;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { captureRuntimeFailure, environmentSecrets, isSensitiveField, makeRedactor, } from "../internal/diagnostics.js";
|
|
2
|
+
/** The most bytes of a value's JSON text a run or step view carries. */
|
|
3
|
+
export const MAX_WORKFLOW_OUTPUT_BYTES = 4096;
|
|
4
|
+
// Values nested deeper are cut: a returned value is read for what it says, not for its shape.
|
|
5
|
+
const MAX_DEPTH = 20;
|
|
6
|
+
/**
|
|
7
|
+
* Reads what a run or step returned from its stored payload. A payload that cannot be decrypted or
|
|
8
|
+
* deserialized is reported to the operator logs and answered as unavailable with that reference.
|
|
9
|
+
*/
|
|
10
|
+
export async function readWorkflowOutput(stored, run, kind, hydrate) {
|
|
11
|
+
if (stored === undefined || stored === null) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
return { value: await hydrate(stored, run, kind) };
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
return workflowOutputUnavailable(error, run.runId, `hydrate_${kind}_output`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** A returned value that could not be read: the failure goes to the operator logs. */
|
|
22
|
+
export function workflowOutputUnavailable(error, runId, operation) {
|
|
23
|
+
return {
|
|
24
|
+
unavailable: true,
|
|
25
|
+
reference: captureRuntimeFailure(error, {
|
|
26
|
+
service: "ontology",
|
|
27
|
+
boundary: "ontology.workflow.output.failed",
|
|
28
|
+
workflowRunId: runId,
|
|
29
|
+
operation,
|
|
30
|
+
}).errorId,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The view's fields for a returned value: `output`, the value as JSON, redacted like the logs; and,
|
|
35
|
+
* when its JSON text is longer than {@link MAX_WORKFLOW_OUTPUT_BYTES}, `outputTruncated: true` with
|
|
36
|
+
* `output` that text cut to the limit. `outputUnavailable` says the value could not be read.
|
|
37
|
+
*/
|
|
38
|
+
export function workflowOutputView(read) {
|
|
39
|
+
if (read === undefined) {
|
|
40
|
+
return { output: null };
|
|
41
|
+
}
|
|
42
|
+
if ("unavailable" in read) {
|
|
43
|
+
return {
|
|
44
|
+
output: null,
|
|
45
|
+
outputUnavailable: read.reference === undefined ? {} : { reference: read.reference },
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const redact = makeRedactor(environmentSecrets(process.env));
|
|
49
|
+
const value = jsonValue(read.value, redact, new Set(), 0);
|
|
50
|
+
if (value === undefined) {
|
|
51
|
+
return { output: null };
|
|
52
|
+
}
|
|
53
|
+
const text = JSON.stringify(value);
|
|
54
|
+
const bytes = new TextEncoder().encode(text);
|
|
55
|
+
if (bytes.length <= MAX_WORKFLOW_OUTPUT_BYTES) {
|
|
56
|
+
return { output: value };
|
|
57
|
+
}
|
|
58
|
+
// Cut on a character boundary: a partial UTF-8 sequence at the end is dropped.
|
|
59
|
+
const cut = new TextDecoder("utf-8", { fatal: false })
|
|
60
|
+
.decode(bytes.subarray(0, MAX_WORKFLOW_OUTPUT_BYTES))
|
|
61
|
+
.replace(/�$/u, "");
|
|
62
|
+
return { output: cut, outputTruncated: true };
|
|
63
|
+
}
|
|
64
|
+
const REDACTED = "[REDACTED]";
|
|
65
|
+
/**
|
|
66
|
+
* A value under a credential-like key is withheld whole, whatever its shape, as the operator
|
|
67
|
+
* reports withhold it; numbers and booleans stay, so a count such as `totalTokens` reads.
|
|
68
|
+
*/
|
|
69
|
+
function sensitive(key, value) {
|
|
70
|
+
return (value !== null &&
|
|
71
|
+
value !== undefined &&
|
|
72
|
+
typeof value !== "number" &&
|
|
73
|
+
typeof value !== "boolean" &&
|
|
74
|
+
isSensitiveField(key));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A hydrated value as JSON: strings redacted, a value under a credential-like key withheld, and
|
|
78
|
+
* what JSON cannot hold described: dates as ISO text, big integers as decimal text, maps as
|
|
79
|
+
* objects or entry lists, sets as arrays, bytes and streams by kind. `undefined` for nothing.
|
|
80
|
+
*/
|
|
81
|
+
function jsonValue(value, redact, seen, depth) {
|
|
82
|
+
switch (typeof value) {
|
|
83
|
+
case "undefined":
|
|
84
|
+
case "function":
|
|
85
|
+
case "symbol":
|
|
86
|
+
return undefined;
|
|
87
|
+
case "string":
|
|
88
|
+
return redact(value);
|
|
89
|
+
case "number":
|
|
90
|
+
return Number.isFinite(value) ? value : String(value);
|
|
91
|
+
case "bigint":
|
|
92
|
+
return value.toString();
|
|
93
|
+
case "boolean":
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
if (value === null) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
const item = value;
|
|
100
|
+
if (seen.has(item)) {
|
|
101
|
+
return "[Circular]";
|
|
102
|
+
}
|
|
103
|
+
if (depth >= MAX_DEPTH) {
|
|
104
|
+
return "[Nested too deep]";
|
|
105
|
+
}
|
|
106
|
+
if (item instanceof Date) {
|
|
107
|
+
return Number.isNaN(item.getTime()) ? null : item.toISOString();
|
|
108
|
+
}
|
|
109
|
+
if (item instanceof URL || item instanceof RegExp) {
|
|
110
|
+
return redact(String(item));
|
|
111
|
+
}
|
|
112
|
+
if (item instanceof ArrayBuffer || ArrayBuffer.isView(item)) {
|
|
113
|
+
return `[${item.byteLength} bytes]`;
|
|
114
|
+
}
|
|
115
|
+
if (typeof Blob !== "undefined" && item instanceof Blob) {
|
|
116
|
+
return `[${item.size} bytes]`;
|
|
117
|
+
}
|
|
118
|
+
if ((typeof ReadableStream !== "undefined" && item instanceof ReadableStream) ||
|
|
119
|
+
(typeof WritableStream !== "undefined" && item instanceof WritableStream)) {
|
|
120
|
+
return "[Stream]";
|
|
121
|
+
}
|
|
122
|
+
seen.add(item);
|
|
123
|
+
try {
|
|
124
|
+
const nested = (entry) => {
|
|
125
|
+
const converted = jsonValue(entry, redact, seen, depth + 1);
|
|
126
|
+
return converted === undefined ? null : converted;
|
|
127
|
+
};
|
|
128
|
+
if (item instanceof Error) {
|
|
129
|
+
return object([
|
|
130
|
+
["name", item.name],
|
|
131
|
+
["message", item.message],
|
|
132
|
+
]);
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(item)) {
|
|
135
|
+
return item.map(nested);
|
|
136
|
+
}
|
|
137
|
+
if (item instanceof Set) {
|
|
138
|
+
return [...item].map(nested);
|
|
139
|
+
}
|
|
140
|
+
if (item instanceof Map) {
|
|
141
|
+
const entries = [...item.entries()];
|
|
142
|
+
return entries.every(([key]) => typeof key === "string")
|
|
143
|
+
? object(entries)
|
|
144
|
+
: entries.map(([key, entry]) => [
|
|
145
|
+
nested(key),
|
|
146
|
+
sensitive(String(key), entry) ? REDACTED : nested(entry),
|
|
147
|
+
]);
|
|
148
|
+
}
|
|
149
|
+
return object(Object.entries(item));
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
seen.delete(item);
|
|
153
|
+
}
|
|
154
|
+
function object(entries) {
|
|
155
|
+
const result = {};
|
|
156
|
+
for (const [key, entry] of entries) {
|
|
157
|
+
const converted = sensitive(key, entry)
|
|
158
|
+
? REDACTED
|
|
159
|
+
: jsonValue(entry, redact, seen, depth + 1);
|
|
160
|
+
if (converted !== undefined) {
|
|
161
|
+
result[redact(key)] = converted;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -18,6 +18,8 @@ import { OntologyBackend } from "./runtime-contract.js";
|
|
|
18
18
|
export { WorkflowStepFailed } from "./workflow-errors.js";
|
|
19
19
|
import { type RuntimeTokenVerifier } from "./runtime-identity.js";
|
|
20
20
|
export { MAX_LISTED_WORKFLOW_EVENTS, WORLD_PAGE_SIZE } from "./workflow-diagnostics.js";
|
|
21
|
+
import { type HydrateOutput, type WorkflowOutputRead } from "./workflow-output.js";
|
|
22
|
+
export { MAX_WORKFLOW_OUTPUT_BYTES, type WorkflowOutputRead } from "./workflow-output.js";
|
|
21
23
|
/** A run as the workflow routes answer it, from the Workflow SDK's run record. */
|
|
22
24
|
export interface WorkflowRunRecord {
|
|
23
25
|
readonly runId: string;
|
|
@@ -30,6 +32,8 @@ export interface WorkflowRunRecord {
|
|
|
30
32
|
readonly attributes?: Readonly<Record<string, string>> | undefined;
|
|
31
33
|
readonly deploymentId?: string;
|
|
32
34
|
readonly error?: unknown;
|
|
35
|
+
/** The runtime's classification of a failed run, such as `MAX_EVENTS_EXCEEDED`. */
|
|
36
|
+
readonly errorCode?: string | undefined;
|
|
33
37
|
}
|
|
34
38
|
/** A step of a run, from the Workflow SDK's step record. */
|
|
35
39
|
export interface WorkflowStepRecord {
|
|
@@ -41,6 +45,8 @@ export interface WorkflowStepRecord {
|
|
|
41
45
|
readonly completedAt?: Date | string | undefined;
|
|
42
46
|
readonly error?: unknown;
|
|
43
47
|
readonly attempts?: ReadonlyArray<WorkflowAttemptRecord>;
|
|
48
|
+
/** What the step returned, when the step page was listed with its outputs. */
|
|
49
|
+
readonly output?: WorkflowOutputRead | undefined;
|
|
44
50
|
}
|
|
45
51
|
export interface WorkflowAttemptRecord {
|
|
46
52
|
readonly attempt: number;
|
|
@@ -96,10 +102,20 @@ export interface WorkflowEngine {
|
|
|
96
102
|
* has no such run. Without it, failed runs cannot be retried.
|
|
97
103
|
*/
|
|
98
104
|
readonly getRunArguments?: (runId: string) => Promise<ReadonlyArray<unknown> | undefined>;
|
|
105
|
+
/**
|
|
106
|
+
* A page of a run's steps. With `outputs`, each completed step carries what it returned, read
|
|
107
|
+
* with the page; otherwise no step's data is read.
|
|
108
|
+
*/
|
|
99
109
|
readonly listSteps: (runId: string, page?: {
|
|
100
110
|
readonly limit?: number;
|
|
101
111
|
readonly cursor?: string;
|
|
112
|
+
readonly outputs?: boolean;
|
|
102
113
|
}) => Promise<WorkflowPage<WorkflowStepRecord>>;
|
|
114
|
+
/**
|
|
115
|
+
* What a completed run returned; `undefined` for a run that has not completed or returned
|
|
116
|
+
* nothing. Without it, run views carry no output.
|
|
117
|
+
*/
|
|
118
|
+
readonly getRunOutput?: (run: WorkflowRunRecord) => Promise<WorkflowOutputRead | undefined>;
|
|
103
119
|
readonly diagnostics?: (run: WorkflowRunRecord, steps?: ReadonlyArray<WorkflowStepRecord>) => Promise<WorkflowDiagnostics>;
|
|
104
120
|
/**
|
|
105
121
|
* Resumes the hook a workflow waits on with `payload`: `not_found` when no run waits on the token
|
|
@@ -107,6 +123,11 @@ export interface WorkflowEngine {
|
|
|
107
123
|
*/
|
|
108
124
|
readonly resumeHook?: (token: string, payload: unknown) => Promise<"resumed" | "not_found">;
|
|
109
125
|
}
|
|
126
|
+
/** A step as the world lists it: its stored input and output only when its data was resolved. */
|
|
127
|
+
type WorldStepRecord = Omit<WorkflowStepRecord, "output"> & {
|
|
128
|
+
readonly input?: unknown;
|
|
129
|
+
readonly output?: unknown;
|
|
130
|
+
};
|
|
110
131
|
export interface WorldLike {
|
|
111
132
|
readonly hooks?: {
|
|
112
133
|
readonly getByToken: (token: string) => Promise<{
|
|
@@ -115,10 +136,12 @@ export interface WorldLike {
|
|
|
115
136
|
};
|
|
116
137
|
readonly runs: {
|
|
117
138
|
readonly list: (params: unknown) => Promise<WorkflowPage<WorkflowRunRecord>>;
|
|
118
|
-
readonly get: (id: string, params?: unknown) => Promise<WorkflowRunRecord
|
|
139
|
+
readonly get: (id: string, params?: unknown) => Promise<WorkflowRunRecord & {
|
|
140
|
+
readonly output?: unknown;
|
|
141
|
+
}>;
|
|
119
142
|
};
|
|
120
143
|
readonly steps: {
|
|
121
|
-
readonly list: (params: unknown) => Promise<WorkflowPage<
|
|
144
|
+
readonly list: (params: unknown) => Promise<WorkflowPage<WorldStepRecord>>;
|
|
122
145
|
};
|
|
123
146
|
readonly events?: {
|
|
124
147
|
readonly list: (params: unknown) => Promise<WorkflowPage<WorkflowEventRecord>>;
|
|
@@ -134,7 +157,7 @@ export interface WorldLike {
|
|
|
134
157
|
* The Workflow SDK's engine over a world: `start` from `workflow/api` on the ontology's queue, and
|
|
135
158
|
* the world's `runs.list`, `runs.get` and `steps.list`, paged within what the Vercel world accepts.
|
|
136
159
|
*/
|
|
137
|
-
export declare function worldWorkflowEngine(world: () => Promise<WorldLike>, begin?: WorkflowSdk["start"], hydrate?: NonNullable<WorkflowSdk["hydrateError"]>, resume?: WorkflowSdk["resumeHook"]): WorkflowEngine;
|
|
160
|
+
export declare function worldWorkflowEngine(world: () => Promise<WorldLike>, begin?: WorkflowSdk["start"], hydrate?: NonNullable<WorkflowSdk["hydrateError"]>, resume?: WorkflowSdk["resumeHook"], hydrateOutput?: HydrateOutput): WorkflowEngine;
|
|
138
161
|
/**
|
|
139
162
|
* The Vercel Workflow SDK as `installWorkflowRuntime()` registered it. The service imports none of
|
|
140
163
|
* the SDK itself: a process that bundles the service without serving workflows, such as the
|
|
@@ -34,7 +34,7 @@ import { CurrentCaller } from "./caller-context.js";
|
|
|
34
34
|
import { InvalidActionInput, InvalidRequestBody, RequestId, failureResponse, forbidden, internalError, notFound, toServerResponse, unauthorized, unavailable, } from "./errors.js";
|
|
35
35
|
import { identityToken as requestIdentityToken, makeOperations, } from "./operations.js";
|
|
36
36
|
import { OntologyBackend } from "./runtime-contract.js";
|
|
37
|
-
import { stepFailure, WorkflowStepFailed, workflowErrorView } from "./workflow-errors.js";
|
|
37
|
+
import { stepFailure, WorkflowStepFailed, workflowErrorView, workflowRunErrorView, } from "./workflow-errors.js";
|
|
38
38
|
export { WorkflowStepFailed } from "./workflow-errors.js";
|
|
39
39
|
import { runtimeCredential } from "./runtime-identity.js";
|
|
40
40
|
import { deploymentWorkflowEngine } from "./workflow-deployment.js";
|
|
@@ -42,6 +42,8 @@ import { decodeWorkflowArguments, encodeWorkflowArguments } from "../runtime/wor
|
|
|
42
42
|
import { TriggerInputInvalid, WorkflowRecoveryUnavailable } from "../runtime/errors.js";
|
|
43
43
|
import { hasWorkflowError, hydrateWorkflowStepPage, readWorkflowDiagnostics, WORLD_PAGE_SIZE, } from "./workflow-diagnostics.js";
|
|
44
44
|
export { MAX_LISTED_WORKFLOW_EVENTS, WORLD_PAGE_SIZE } from "./workflow-diagnostics.js";
|
|
45
|
+
import { readWorkflowOutput, workflowOutputUnavailable, workflowOutputView, } from "./workflow-output.js";
|
|
46
|
+
export { MAX_WORKFLOW_OUTPUT_BYTES } from "./workflow-output.js";
|
|
45
47
|
/**
|
|
46
48
|
* The world has no such run: the Workflow SDK's `WorkflowRunNotFoundError`, told apart by its name
|
|
47
49
|
* as its own `is` guard does (this module imports none of the SDK), or a world's bare 404.
|
|
@@ -62,7 +64,13 @@ function isHookGone(error) {
|
|
|
62
64
|
* The Workflow SDK's engine over a world: `start` from `workflow/api` on the ontology's queue, and
|
|
63
65
|
* the world's `runs.list`, `runs.get` and `steps.list`, paged within what the Vercel world accepts.
|
|
64
66
|
*/
|
|
65
|
-
export function worldWorkflowEngine(world, begin = (workflow, args, options) => installedWorkflowSdk().start(workflow, args, options), hydrate = (value, run, kind) => installedWorkflowSdk().hydrateError?.(value, run, kind) ?? Promise.resolve(value), resume = (token, payload) => installedWorkflowSdk().resumeHook(token, payload)) {
|
|
67
|
+
export function worldWorkflowEngine(world, begin = (workflow, args, options) => installedWorkflowSdk().start(workflow, args, options), hydrate = (value, run, kind) => installedWorkflowSdk().hydrateError?.(value, run, kind) ?? Promise.resolve(value), resume = (token, payload) => installedWorkflowSdk().resumeHook(token, payload), hydrateOutput = (value, run, kind) => {
|
|
68
|
+
const read = installedWorkflowSdk().hydrateOutput;
|
|
69
|
+
if (read === undefined) {
|
|
70
|
+
throw new Error("The Workflow runtime cannot read what a run returned.");
|
|
71
|
+
}
|
|
72
|
+
return read(value, run, kind);
|
|
73
|
+
}) {
|
|
66
74
|
const engine = {
|
|
67
75
|
allocateRun: () => {
|
|
68
76
|
const allocate = installedWorkflowSdk().allocateRun;
|
|
@@ -147,16 +155,50 @@ export function worldWorkflowEngine(world, begin = (workflow, args, options) =>
|
|
|
147
155
|
},
|
|
148
156
|
listSteps: async (runId, options = {}) => {
|
|
149
157
|
const current = await world();
|
|
150
|
-
const
|
|
158
|
+
const listed = await current.steps.list({
|
|
151
159
|
runId,
|
|
152
160
|
pagination: {
|
|
153
161
|
limit: Math.min(Math.max(options.limit ?? WORLD_PAGE_SIZE, 1), WORLD_PAGE_SIZE),
|
|
154
162
|
sortOrder: "desc",
|
|
155
163
|
...(options.cursor === undefined ? {} : { cursor: options.cursor }),
|
|
156
164
|
},
|
|
157
|
-
|
|
165
|
+
// A step's data, its input and output, is read only for its output.
|
|
166
|
+
resolveData: options.outputs ? "all" : "none",
|
|
158
167
|
});
|
|
159
|
-
|
|
168
|
+
const stored = new Map(listed.data.map((step) => [step.stepId, step.status === "completed" ? step.output : null]));
|
|
169
|
+
const page = await hydrateWorkflowStepPage(current, runId, {
|
|
170
|
+
...listed,
|
|
171
|
+
data: listed.data.map(({ input: _input, output: _output, ...step }) => step),
|
|
172
|
+
}, hydrate);
|
|
173
|
+
if (!options.outputs || ![...stored.values()].some((value) => value != null)) {
|
|
174
|
+
return page;
|
|
175
|
+
}
|
|
176
|
+
let run;
|
|
177
|
+
try {
|
|
178
|
+
// The creating deployment's key decrypts the run's data.
|
|
179
|
+
run = await current.runs.get(runId, { resolveData: "none" });
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
const unavailable = workflowOutputUnavailable(error, runId, "read_output_encryption_context");
|
|
183
|
+
return {
|
|
184
|
+
...page,
|
|
185
|
+
data: page.data.map((step) => stored.get(step.stepId) == null ? step : { ...step, output: unavailable }),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
...page,
|
|
190
|
+
data: await Promise.all(page.data.map(async (step) => {
|
|
191
|
+
const output = await readWorkflowOutput(stored.get(step.stepId), run, "step", hydrateOutput);
|
|
192
|
+
return output === undefined ? step : { ...step, output };
|
|
193
|
+
})),
|
|
194
|
+
};
|
|
195
|
+
},
|
|
196
|
+
getRunOutput: async (record) => {
|
|
197
|
+
if (record.status !== "completed") {
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
const run = await (await world()).runs.get(record.runId, { resolveData: "all" });
|
|
201
|
+
return readWorkflowOutput(run.output, run, "run", hydrateOutput);
|
|
160
202
|
},
|
|
161
203
|
diagnostics: async (run, steps) => readWorkflowDiagnostics(await world(), run, steps, hydrate),
|
|
162
204
|
};
|
|
@@ -489,7 +531,7 @@ function runStartedBy(attributes) {
|
|
|
489
531
|
}
|
|
490
532
|
return undefined;
|
|
491
533
|
}
|
|
492
|
-
function runView(workflow, record) {
|
|
534
|
+
function runView(workflow, record, output) {
|
|
493
535
|
const startedBy = runStartedBy(record.attributes);
|
|
494
536
|
return {
|
|
495
537
|
runId: record.runId,
|
|
@@ -500,7 +542,8 @@ function runView(workflow, record) {
|
|
|
500
542
|
startedAt: iso(record.startedAt),
|
|
501
543
|
completedAt: iso(record.completedAt),
|
|
502
544
|
...(startedBy === undefined ? {} : { startedBy }),
|
|
503
|
-
error:
|
|
545
|
+
error: workflowRunErrorView(record.error, record.errorCode),
|
|
546
|
+
...output,
|
|
504
547
|
};
|
|
505
548
|
}
|
|
506
549
|
function stepView(record) {
|
|
@@ -512,6 +555,7 @@ function stepView(record) {
|
|
|
512
555
|
startedAt: iso(record.startedAt),
|
|
513
556
|
completedAt: iso(record.completedAt),
|
|
514
557
|
error: workflowErrorView(record.error),
|
|
558
|
+
...workflowOutputView(record.output),
|
|
515
559
|
...(record.attempts === undefined
|
|
516
560
|
? {}
|
|
517
561
|
: {
|
|
@@ -1699,7 +1743,7 @@ export function workflowsLayer(options) {
|
|
|
1699
1743
|
const query = new URL(request.url, "https://ontology.invalid").searchParams;
|
|
1700
1744
|
const limit = Math.min(Math.max(Math.trunc(Number(query.get("limit")) || WORLD_PAGE_SIZE), 1), WORLD_PAGE_SIZE);
|
|
1701
1745
|
const cursor = query.get("after") ?? undefined;
|
|
1702
|
-
const steps = yield* Effect.tryPromise(() => engine.listSteps(runId, { limit, ...(cursor ? { cursor } : {}) })).pipe(Effect.result);
|
|
1746
|
+
const steps = yield* Effect.tryPromise(() => engine.listSteps(runId, { limit, ...(cursor ? { cursor } : {}), outputs: true })).pipe(Effect.result);
|
|
1703
1747
|
if (steps._tag === "Failure") {
|
|
1704
1748
|
yield* Effect.logError("ontology.workflow.steps.failed", steps.failure).pipe(Effect.annotateLogs({ requestId, workflow: found.workflow.manifest.name, runId }));
|
|
1705
1749
|
}
|
|
@@ -1707,14 +1751,20 @@ export function workflowsLayer(options) {
|
|
|
1707
1751
|
? undefined
|
|
1708
1752
|
: yield* Effect.tryPromise(() => engine.diagnostics(run.value, steps._tag === "Success" ? steps.success.data : [])).pipe(Effect.result);
|
|
1709
1753
|
const observed = diagnostics?._tag === "Success" ? diagnostics.success : undefined;
|
|
1754
|
+
// The run is answered without its output when the world cannot read it; the
|
|
1755
|
+
// failure is reported to the operator logs with the reference the view carries.
|
|
1756
|
+
const output = engine.getRunOutput === undefined
|
|
1757
|
+
? undefined
|
|
1758
|
+
: yield* Effect.tryPromise(() => engine.getRunOutput(run.value)).pipe(Effect.result);
|
|
1710
1759
|
if (diagnostics?._tag === "Failure") {
|
|
1711
1760
|
yield* Effect.logError("ontology.workflow.diagnostics.failed", diagnostics.failure).pipe(Effect.annotateLogs({ requestId, runId }));
|
|
1712
1761
|
}
|
|
1713
1762
|
return json({
|
|
1714
|
-
...runView(found.workflow.manifest.name, {
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1763
|
+
...runView(found.workflow.manifest.name, { ...run.value, error: observed?.error ?? run.value.error }, workflowOutputView(output === undefined
|
|
1764
|
+
? undefined
|
|
1765
|
+
: output._tag === "Success"
|
|
1766
|
+
? output.success
|
|
1767
|
+
: workflowOutputUnavailable(output.failure, runId, "read_run_output"))),
|
|
1718
1768
|
steps: steps._tag === "Success"
|
|
1719
1769
|
? steps.success.data.map((step) => {
|
|
1720
1770
|
const attempts = observed?.attempts.get(step.stepId);
|
|
@@ -91,6 +91,11 @@ export interface WorkflowSdk {
|
|
|
91
91
|
readonly runId: string;
|
|
92
92
|
readonly deploymentId?: string;
|
|
93
93
|
}, kind: "step" | "run") => Promise<unknown>;
|
|
94
|
+
/** Hydrate only what a run or step returned, for the run's view; arguments stay unresolved. */
|
|
95
|
+
readonly hydrateOutput?: (value: unknown, run: {
|
|
96
|
+
readonly runId: string;
|
|
97
|
+
readonly deploymentId?: string;
|
|
98
|
+
}, kind: "step" | "run") => Promise<unknown>;
|
|
94
99
|
/** `resumeHook` from `workflow/api`: it rejects with `HookNotFoundError` for a gone hook. */
|
|
95
100
|
readonly resumeHook: (token: string, payload: unknown) => Promise<unknown>;
|
|
96
101
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@segmentstream/ontology",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.197.1",
|
|
4
4
|
"description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "SegmentStream",
|