@segmentstream/ontology 0.196.0 → 0.197.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/dist/internal/diagnostics.js +1 -0
- package/dist/server.js +12 -0
- package/dist/service/chat/providers/telegram.d.ts +4 -3
- package/dist/service/chat/providers/telegram.js +21 -4
- package/dist/service/workflow-deployment.js +5 -0
- package/dist/service/workflow-output.d.ts +40 -0
- package/dist/service/workflow-output.js +166 -0
- package/dist/service/workflows.d.ts +24 -3
- package/dist/service/workflows.js +60 -10
- package/dist/workflow-runtime.d.ts +5 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.197.0
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
## 0.196.1
|
|
8
|
+
|
|
9
|
+
A Telegram rich message the ontology sends shows a table written on the line right after text as a
|
|
10
|
+
table. Markdown reads such a table as a block of its own, while Telegram took it as more of the
|
|
11
|
+
text before it and showed its rows as one line of pipes; the rich message now has the Markdown's
|
|
12
|
+
blocks a blank line apart, each as written.
|
|
13
|
+
|
|
3
14
|
## 0.196.0
|
|
4
15
|
|
|
5
16
|
Allow chat persona rules to match a trusted registration time. A workspace can change the initial persona for new groups while preserving inherited settings in established groups.
|
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. */
|
|
@@ -12,9 +12,10 @@ export { TELEGRAM_MESSAGE_LIMIT };
|
|
|
12
12
|
export declare function renderTelegram(markdown: string, attribution: string, separator?: string, links?: ReadonlyMap<string, string>): string;
|
|
13
13
|
/**
|
|
14
14
|
* The message as one Telegram rich message, for Markdown with a table, which MarkdownV2 shows only
|
|
15
|
-
* as code: the member's Markdown, its
|
|
16
|
-
* attribution. Undefined without a table, beyond what one
|
|
17
|
-
* Markdown leaves open, such as a code fence it never
|
|
15
|
+
* as code: the member's Markdown, its blocks a blank line apart and its links to the app as
|
|
16
|
+
* `renderTelegram` points them, then the attribution. Undefined without a table, beyond what one
|
|
17
|
+
* rich message takes, or when a block the Markdown leaves open, such as a code fence it never
|
|
18
|
+
* closes, would take the attribution in.
|
|
18
19
|
*/
|
|
19
20
|
export declare function telegramRichMarkdown(markdown: string, attribution: string, separator?: string, links?: ReadonlyMap<string, string>): string | undefined;
|
|
20
21
|
/** Buttons as Telegram shows them: an inline keyboard, a workspace button's data `ws:<token>`. */
|
|
@@ -75,18 +75,35 @@ function richShape(tree) {
|
|
|
75
75
|
}
|
|
76
76
|
return shape;
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* The Markdown's blocks, each as written, a blank line apart. Markdown takes a table on the line
|
|
80
|
+
* after a paragraph's text as a block of its own; Telegram's rich message takes it as more of the
|
|
81
|
+
* paragraph's text, so a table is sent apart from the text before it, as it was read here.
|
|
82
|
+
*/
|
|
83
|
+
function markdownBlocksApart(markdown) {
|
|
84
|
+
const blocks = converter.toAst(markdown).children.map((node) => {
|
|
85
|
+
const start = node.position?.start.offset;
|
|
86
|
+
const end = node.position?.end.offset;
|
|
87
|
+
return start === undefined || end === undefined ? undefined : markdown.slice(start, end);
|
|
88
|
+
});
|
|
89
|
+
return blocks.every((block) => block !== undefined) ? blocks.join("\n\n") : markdown;
|
|
90
|
+
}
|
|
78
91
|
/**
|
|
79
92
|
* The message as one Telegram rich message, for Markdown with a table, which MarkdownV2 shows only
|
|
80
|
-
* as code: the member's Markdown, its
|
|
81
|
-
* attribution. Undefined without a table, beyond what one
|
|
82
|
-
* Markdown leaves open, such as a code fence it never
|
|
93
|
+
* as code: the member's Markdown, its blocks a blank line apart and its links to the app as
|
|
94
|
+
* `renderTelegram` points them, then the attribution. Undefined without a table, beyond what one
|
|
95
|
+
* rich message takes, or when a block the Markdown leaves open, such as a code fence it never
|
|
96
|
+
* closes, would take the attribution in.
|
|
83
97
|
*/
|
|
84
98
|
export function telegramRichMarkdown(markdown, attribution, separator = "\n\n", links = new Map()) {
|
|
85
99
|
// A table has pipes: Markdown without one is not parsed again.
|
|
86
100
|
if (!markdown.includes("|")) {
|
|
87
101
|
return undefined;
|
|
88
102
|
}
|
|
89
|
-
|
|
103
|
+
// Written back from its tree, Markdown with links to the app has its blocks apart already.
|
|
104
|
+
const body = links.size === 0
|
|
105
|
+
? markdownBlocksApart(markdown)
|
|
106
|
+
: stringifyMarkdown(telegramMarkdownWithLinks(markdown, links));
|
|
90
107
|
const signed = attribution === "" ? "" : markdownAttribution(attribution, separator);
|
|
91
108
|
const rich = signed === "" ? body : `${body}\n\n${signed}`;
|
|
92
109
|
const tree = converter.toAst(rich);
|
|
@@ -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.
|
|
@@ -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;
|
|
@@ -41,6 +43,8 @@ export interface WorkflowStepRecord {
|
|
|
41
43
|
readonly completedAt?: Date | string | undefined;
|
|
42
44
|
readonly error?: unknown;
|
|
43
45
|
readonly attempts?: ReadonlyArray<WorkflowAttemptRecord>;
|
|
46
|
+
/** What the step returned, when the step page was listed with its outputs. */
|
|
47
|
+
readonly output?: WorkflowOutputRead | undefined;
|
|
44
48
|
}
|
|
45
49
|
export interface WorkflowAttemptRecord {
|
|
46
50
|
readonly attempt: number;
|
|
@@ -96,10 +100,20 @@ export interface WorkflowEngine {
|
|
|
96
100
|
* has no such run. Without it, failed runs cannot be retried.
|
|
97
101
|
*/
|
|
98
102
|
readonly getRunArguments?: (runId: string) => Promise<ReadonlyArray<unknown> | undefined>;
|
|
103
|
+
/**
|
|
104
|
+
* A page of a run's steps. With `outputs`, each completed step carries what it returned, read
|
|
105
|
+
* with the page; otherwise no step's data is read.
|
|
106
|
+
*/
|
|
99
107
|
readonly listSteps: (runId: string, page?: {
|
|
100
108
|
readonly limit?: number;
|
|
101
109
|
readonly cursor?: string;
|
|
110
|
+
readonly outputs?: boolean;
|
|
102
111
|
}) => Promise<WorkflowPage<WorkflowStepRecord>>;
|
|
112
|
+
/**
|
|
113
|
+
* What a completed run returned; `undefined` for a run that has not completed or returned
|
|
114
|
+
* nothing. Without it, run views carry no output.
|
|
115
|
+
*/
|
|
116
|
+
readonly getRunOutput?: (run: WorkflowRunRecord) => Promise<WorkflowOutputRead | undefined>;
|
|
103
117
|
readonly diagnostics?: (run: WorkflowRunRecord, steps?: ReadonlyArray<WorkflowStepRecord>) => Promise<WorkflowDiagnostics>;
|
|
104
118
|
/**
|
|
105
119
|
* Resumes the hook a workflow waits on with `payload`: `not_found` when no run waits on the token
|
|
@@ -107,6 +121,11 @@ export interface WorkflowEngine {
|
|
|
107
121
|
*/
|
|
108
122
|
readonly resumeHook?: (token: string, payload: unknown) => Promise<"resumed" | "not_found">;
|
|
109
123
|
}
|
|
124
|
+
/** A step as the world lists it: its stored input and output only when its data was resolved. */
|
|
125
|
+
type WorldStepRecord = Omit<WorkflowStepRecord, "output"> & {
|
|
126
|
+
readonly input?: unknown;
|
|
127
|
+
readonly output?: unknown;
|
|
128
|
+
};
|
|
110
129
|
export interface WorldLike {
|
|
111
130
|
readonly hooks?: {
|
|
112
131
|
readonly getByToken: (token: string) => Promise<{
|
|
@@ -115,10 +134,12 @@ export interface WorldLike {
|
|
|
115
134
|
};
|
|
116
135
|
readonly runs: {
|
|
117
136
|
readonly list: (params: unknown) => Promise<WorkflowPage<WorkflowRunRecord>>;
|
|
118
|
-
readonly get: (id: string, params?: unknown) => Promise<WorkflowRunRecord
|
|
137
|
+
readonly get: (id: string, params?: unknown) => Promise<WorkflowRunRecord & {
|
|
138
|
+
readonly output?: unknown;
|
|
139
|
+
}>;
|
|
119
140
|
};
|
|
120
141
|
readonly steps: {
|
|
121
|
-
readonly list: (params: unknown) => Promise<WorkflowPage<
|
|
142
|
+
readonly list: (params: unknown) => Promise<WorkflowPage<WorldStepRecord>>;
|
|
122
143
|
};
|
|
123
144
|
readonly events?: {
|
|
124
145
|
readonly list: (params: unknown) => Promise<WorkflowPage<WorkflowEventRecord>>;
|
|
@@ -134,7 +155,7 @@ export interface WorldLike {
|
|
|
134
155
|
* The Workflow SDK's engine over a world: `start` from `workflow/api` on the ontology's queue, and
|
|
135
156
|
* the world's `runs.list`, `runs.get` and `steps.list`, paged within what the Vercel world accepts.
|
|
136
157
|
*/
|
|
137
|
-
export declare function worldWorkflowEngine(world: () => Promise<WorldLike>, begin?: WorkflowSdk["start"], hydrate?: NonNullable<WorkflowSdk["hydrateError"]>, resume?: WorkflowSdk["resumeHook"]): WorkflowEngine;
|
|
158
|
+
export declare function worldWorkflowEngine(world: () => Promise<WorldLike>, begin?: WorkflowSdk["start"], hydrate?: NonNullable<WorkflowSdk["hydrateError"]>, resume?: WorkflowSdk["resumeHook"], hydrateOutput?: HydrateOutput): WorkflowEngine;
|
|
138
159
|
/**
|
|
139
160
|
* The Vercel Workflow SDK as `installWorkflowRuntime()` registered it. The service imports none of
|
|
140
161
|
* the SDK itself: a process that bundles the service without serving workflows, such as the
|
|
@@ -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,
|
|
@@ -501,6 +543,7 @@ function runView(workflow, record) {
|
|
|
501
543
|
completedAt: iso(record.completedAt),
|
|
502
544
|
...(startedBy === undefined ? {} : { startedBy }),
|
|
503
545
|
error: workflowErrorView(record.error),
|
|
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.0",
|
|
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",
|