@relayflows/sdk 2.0.31 → 2.0.32
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/authored-preflight.d.ts +4 -0
- package/dist/authored-preflight.d.ts.map +1 -0
- package/dist/authored-preflight.js +50 -0
- package/dist/authored-preflight.js.map +1 -0
- package/dist/authored-worker-step.d.ts.map +1 -1
- package/dist/authored-worker-step.js +3 -2
- package/dist/authored-worker-step.js.map +1 -1
- package/dist/cli/check.d.ts +10 -2
- package/dist/cli/check.d.ts.map +1 -1
- package/dist/cli/check.js +16 -137
- package/dist/cli/check.js.map +1 -1
- package/dist/cli/cli-probe.d.ts +20 -0
- package/dist/cli/cli-probe.d.ts.map +1 -0
- package/dist/cli/cli-probe.js +166 -0
- package/dist/cli/cli-probe.js.map +1 -0
- package/dist/cli/cloud-mirror-session.d.ts +97 -0
- package/dist/cli/cloud-mirror-session.d.ts.map +1 -0
- package/dist/cli/cloud-mirror-session.js +264 -0
- package/dist/cli/cloud-mirror-session.js.map +1 -0
- package/dist/cli-commands.d.ts +6 -0
- package/dist/cli-commands.d.ts.map +1 -1
- package/dist/cli-commands.js +1 -0
- package/dist/cli-commands.js.map +1 -1
- package/dist/cli.d.ts +7 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +84 -18
- package/dist/cli.js.map +1 -1
- package/dist/cloud-deploy.d.ts +1 -1
- package/dist/cloud-deploy.d.ts.map +1 -1
- package/dist/cloud-deploy.js +24 -8
- package/dist/cloud-deploy.js.map +1 -1
- package/dist/cloud-mirror-ledger.d.ts +40 -0
- package/dist/cloud-mirror-ledger.d.ts.map +1 -0
- package/dist/cloud-mirror-ledger.js +142 -0
- package/dist/cloud-mirror-ledger.js.map +1 -0
- package/dist/cloud-mirror-step.d.ts +168 -0
- package/dist/cloud-mirror-step.d.ts.map +1 -0
- package/dist/cloud-mirror-step.js +504 -0
- package/dist/cloud-mirror-step.js.map +1 -0
- package/dist/cloud-mirror-transport.d.ts +94 -0
- package/dist/cloud-mirror-transport.d.ts.map +1 -0
- package/dist/cloud-mirror-transport.js +163 -0
- package/dist/cloud-mirror-transport.js.map +1 -0
- package/dist/cloud-mirror.d.ts +132 -0
- package/dist/cloud-mirror.d.ts.map +1 -0
- package/dist/cloud-mirror.js +556 -0
- package/dist/cloud-mirror.js.map +1 -0
- package/dist/hosted-extension-runtime.js +1 -1
- package/dist/package-version.d.ts +10 -0
- package/dist/package-version.d.ts.map +1 -0
- package/dist/package-version.js +22 -0
- package/dist/package-version.js.map +1 -0
- package/dist/preflight.d.ts +14 -0
- package/dist/preflight.d.ts.map +1 -1
- package/dist/preflight.js +18 -3
- package/dist/preflight.js.map +1 -1
- package/dist/relay-cli.d.ts.map +1 -1
- package/dist/relay-cli.js +1 -21
- package/dist/relay-cli.js.map +1 -1
- package/package.json +2 -2
- package/src/authored-preflight.ts +46 -0
- package/src/authored-worker-step.ts +4 -3
- package/src/cli/check.ts +25 -163
- package/src/cli/cli-probe.ts +194 -0
- package/src/cli/cloud-mirror-session.ts +329 -0
- package/src/cli-commands.ts +1 -0
- package/src/cli.ts +94 -19
- package/src/cloud-deploy.ts +26 -8
- package/src/cloud-mirror-ledger.ts +161 -0
- package/src/cloud-mirror-step.ts +618 -0
- package/src/cloud-mirror-transport.ts +261 -0
- package/src/cloud-mirror.ts +633 -0
- package/src/hosted-extension-runtime.ts +1 -1
- package/src/package-version.ts +23 -0
- package/src/preflight.ts +31 -7
- package/src/relay-cli.ts +1 -24
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// The Cloud calls a mirrored local run makes, and nothing else.
|
|
2
|
+
//
|
|
3
|
+
// A hosted run reports through five endpoints. A local run reports through the
|
|
4
|
+
// same five, with the same bodies, using a credential Cloud issued for that
|
|
5
|
+
// one run — so the control plane cannot tell a mirrored run from a sandboxed
|
|
6
|
+
// one, and no route needed a new case to admit it.
|
|
7
|
+
//
|
|
8
|
+
// POST /api/v1/workflows/local-run register; returns the credential
|
|
9
|
+
// POST /api/v1/workflows/runs/<id>/events lifecycle events
|
|
10
|
+
// POST /api/v1/workflows/runs/<id>/steps/snapshot the live view, while it runs
|
|
11
|
+
// POST /api/v1/workflows/runs/<id>/steps the final report
|
|
12
|
+
// PUT /api/v1/workflows/runs/<id>/storage/<k> runner log and step transcripts
|
|
13
|
+
// POST /api/v1/workflows/callback the terminal status
|
|
14
|
+
//
|
|
15
|
+
// ## Two credentials, never confused
|
|
16
|
+
//
|
|
17
|
+
// Registration authenticates as the *operator*: the `agent-relay cloud login`
|
|
18
|
+
// token or `FLOWS_CLOUD_TOKEN`, the same credential `flows run --cloud` uses.
|
|
19
|
+
// Everything after it authenticates as the *run*, with the run-bound token the
|
|
20
|
+
// registration returned. That token can write this run's steps and read this
|
|
21
|
+
// run, and can do nothing else in the workspace — so a mirror that is somehow
|
|
22
|
+
// coerced into pushing elsewhere has nothing to push with.
|
|
23
|
+
//
|
|
24
|
+
// ## Everything here is best-effort by construction
|
|
25
|
+
//
|
|
26
|
+
// Except `register`, which the caller needs an answer from, every function
|
|
27
|
+
// resolves to a boolean and throws nothing. A mirror is an observer: a run's
|
|
28
|
+
// correctness cannot depend on whether a report landed, and a Cloud outage
|
|
29
|
+
// must cost a local run nothing but its dashboard page. The caller decides
|
|
30
|
+
// what to retry; this module decides nothing.
|
|
31
|
+
|
|
32
|
+
import { cloudFetch, CloudFlowError, cloudConnection, cloudRunId, isCloudRecord, type CloudConnectionOptions } from './cloud-http.js';
|
|
33
|
+
import type { FinalStep, SnapshotStep } from './cloud-mirror-step.js';
|
|
34
|
+
|
|
35
|
+
/** What registration returns: the run, and the credential that may report on it. */
|
|
36
|
+
export interface MirrorRegistration {
|
|
37
|
+
runId: string;
|
|
38
|
+
/** The run-bound token every later call authenticates with. */
|
|
39
|
+
token: string;
|
|
40
|
+
/** Proves the terminal callback came from this run's executor. */
|
|
41
|
+
callbackToken: string;
|
|
42
|
+
/** The dashboard page for this run, as Cloud named it. */
|
|
43
|
+
runUrl: string;
|
|
44
|
+
/**
|
|
45
|
+
* The deployment that issued the credential, pinned.
|
|
46
|
+
*
|
|
47
|
+
* Load-bearing, not bookkeeping. `cloudConnection` resolves the base URL
|
|
48
|
+
* from the *login store* only while no explicit token is given — and every
|
|
49
|
+
* call after registration gives one. Without pinning it here, a CLI signed
|
|
50
|
+
* in to a staging deployment would register there and then send that
|
|
51
|
+
* deployment's run token to the production default.
|
|
52
|
+
*/
|
|
53
|
+
apiUrl: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface MirrorRunSource {
|
|
57
|
+
/** The exact source bytes that ran: YAML/JSON text, or authored `.flow.ts`. */
|
|
58
|
+
workflow: string;
|
|
59
|
+
fileType: 'yaml' | 'ts';
|
|
60
|
+
/** Authored runs only: the input the flow was invoked with. */
|
|
61
|
+
inputs?: unknown;
|
|
62
|
+
/**
|
|
63
|
+
* The Cloud run of the attempt this one continues, for a `flows resume`.
|
|
64
|
+
*
|
|
65
|
+
* Cloud models a resume as a new attempt row — its own hosted resume mints a
|
|
66
|
+
* fresh run id too — so one parked-then-resumed flow is two rows either way.
|
|
67
|
+
* Naming the predecessor is what stops those two rows reading as two
|
|
68
|
+
* unrelated pieces of work: the run page links them, and a reader of the
|
|
69
|
+
* red "Needs review" row can see it was answered.
|
|
70
|
+
*/
|
|
71
|
+
resumedFromRunId?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** One lifecycle event, in the vocabulary Cloud's session event stream uses. */
|
|
75
|
+
export interface MirrorEvent {
|
|
76
|
+
eventType: string;
|
|
77
|
+
stepName?: string;
|
|
78
|
+
payload?: Record<string, unknown>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const REGISTER_PATH = '/api/v1/workflows/local-run';
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Register the run and take its credential.
|
|
85
|
+
*
|
|
86
|
+
* The only call here that throws. Everything the mirror does afterwards
|
|
87
|
+
* depends on this receipt, so a caller has to be able to tell "Cloud refused"
|
|
88
|
+
* from "Cloud accepted" — and the CLI turns that difference into one line on
|
|
89
|
+
* stderr rather than a failed run.
|
|
90
|
+
*/
|
|
91
|
+
export async function registerLocalRun(
|
|
92
|
+
source: MirrorRunSource,
|
|
93
|
+
options: CloudConnectionOptions = {},
|
|
94
|
+
): Promise<MirrorRegistration> {
|
|
95
|
+
const result = await cloudFetch(REGISTER_PATH, options, {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
detail: true,
|
|
98
|
+
body: JSON.stringify({
|
|
99
|
+
workflow: source.workflow,
|
|
100
|
+
fileType: source.fileType,
|
|
101
|
+
relayflowVersion: 'v2',
|
|
102
|
+
...(source.inputs === undefined ? {} : { inputs: source.inputs }),
|
|
103
|
+
...(source.resumedFromRunId === undefined ? {} : { resumedFromRunId: source.resumedFromRunId }),
|
|
104
|
+
}),
|
|
105
|
+
});
|
|
106
|
+
if (!isCloudRecord(result)) {
|
|
107
|
+
throw new CloudFlowError('invalid_response', 'Cloud did not return a registered local run.');
|
|
108
|
+
}
|
|
109
|
+
const runId = cloudRunId(result['runId']);
|
|
110
|
+
const token = result['accessToken'];
|
|
111
|
+
const callbackToken = result['callbackToken'];
|
|
112
|
+
if (typeof token !== 'string' || token.trim().length === 0
|
|
113
|
+
|| typeof callbackToken !== 'string' || callbackToken.length === 0) {
|
|
114
|
+
throw new CloudFlowError('invalid_response', 'Cloud registered the run without a usable credential.');
|
|
115
|
+
}
|
|
116
|
+
const { baseUrl } = cloudConnection(options);
|
|
117
|
+
const runUrl = typeof result['runUrl'] === 'string' && /^https:\/\//u.test(result['runUrl'])
|
|
118
|
+
? result['runUrl']
|
|
119
|
+
: `${baseUrl}/dashboard/workflow/${encodeURIComponent(runId)}/runner`;
|
|
120
|
+
return { runId, token, callbackToken, runUrl, apiUrl: baseUrl };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A mirror's authenticated view of one run. Built once, from the registration. */
|
|
124
|
+
export class MirrorClient {
|
|
125
|
+
/** Connection options with the run's credential and its deployment pinned. */
|
|
126
|
+
private readonly bound: CloudConnectionOptions;
|
|
127
|
+
|
|
128
|
+
constructor(
|
|
129
|
+
private readonly registration: MirrorRegistration,
|
|
130
|
+
options: CloudConnectionOptions = {},
|
|
131
|
+
) {
|
|
132
|
+
this.bound = { ...options, token: registration.token, apiUrl: registration.apiUrl };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
get runId(): string {
|
|
136
|
+
return this.registration.runId;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
get runUrl(): string {
|
|
140
|
+
return this.registration.runUrl;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Every call but registration goes through here. The run token replaces the
|
|
145
|
+
* operator credential, the outcome collapses to a boolean, and nothing —
|
|
146
|
+
* not a refusal, not a timeout, not a thrown transport error — escapes.
|
|
147
|
+
*/
|
|
148
|
+
private async post(
|
|
149
|
+
path: string,
|
|
150
|
+
body: string,
|
|
151
|
+
init: { signal?: AbortSignal; timeoutMs?: number } = {},
|
|
152
|
+
): Promise<boolean> {
|
|
153
|
+
try {
|
|
154
|
+
await cloudFetch(path, {
|
|
155
|
+
...this.bound,
|
|
156
|
+
...(init.signal === undefined ? {} : { signal: init.signal }),
|
|
157
|
+
...(init.timeoutMs === undefined ? {} : { requestTimeoutMs: init.timeoutMs }),
|
|
158
|
+
}, { method: 'POST', body });
|
|
159
|
+
return true;
|
|
160
|
+
} catch {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async publishEvent(event: MirrorEvent, signal?: AbortSignal): Promise<boolean> {
|
|
166
|
+
return this.post(
|
|
167
|
+
`/api/v1/workflows/runs/${this.registration.runId}/events`,
|
|
168
|
+
JSON.stringify({
|
|
169
|
+
eventType: event.eventType,
|
|
170
|
+
...(event.stepName === undefined ? {} : { stepName: event.stepName }),
|
|
171
|
+
payload: event.payload ?? {},
|
|
172
|
+
}),
|
|
173
|
+
{ ...(signal === undefined ? {} : { signal }) },
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async publishSnapshot(
|
|
178
|
+
snapshot: { sequence: number; capturedAt: string; truncated?: true; omittedStepCount?: number; steps: SnapshotStep[] },
|
|
179
|
+
limits: { signal?: AbortSignal; timeoutMs?: number } = {},
|
|
180
|
+
): Promise<boolean> {
|
|
181
|
+
return this.post(
|
|
182
|
+
`/api/v1/workflows/runs/${this.registration.runId}/steps/snapshot`,
|
|
183
|
+
JSON.stringify(snapshot),
|
|
184
|
+
limits,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async publishSteps(
|
|
189
|
+
steps: readonly FinalStep[],
|
|
190
|
+
omittedStepCount: number,
|
|
191
|
+
signal?: AbortSignal,
|
|
192
|
+
timeoutMs?: number,
|
|
193
|
+
): Promise<boolean> {
|
|
194
|
+
return this.post(
|
|
195
|
+
`/api/v1/workflows/runs/${this.registration.runId}/steps`,
|
|
196
|
+
JSON.stringify({ steps, ...(omittedStepCount > 0 ? { omittedStepCount } : {}) }),
|
|
197
|
+
{ ...(signal === undefined ? {} : { signal }), ...(timeoutMs === undefined ? {} : { timeoutMs }) },
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Write one object under the run's storage prefix. The keys are the ones the
|
|
203
|
+
* `/logs` route already reads: `runner.log` for the run, and
|
|
204
|
+
* `<stepName>/agent.log` for a step's assembled transcript.
|
|
205
|
+
*/
|
|
206
|
+
async putObject(
|
|
207
|
+
key: string,
|
|
208
|
+
bytes: Uint8Array,
|
|
209
|
+
signal?: AbortSignal,
|
|
210
|
+
timeoutMs?: number,
|
|
211
|
+
): Promise<boolean> {
|
|
212
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._/-]{0,511}$/u.test(key) || key.includes('..')) return false;
|
|
213
|
+
try {
|
|
214
|
+
await cloudFetch(
|
|
215
|
+
`/api/v1/workflows/runs/${this.registration.runId}/storage/${key}`,
|
|
216
|
+
{
|
|
217
|
+
...this.bound,
|
|
218
|
+
...(signal === undefined ? {} : { signal }),
|
|
219
|
+
...(timeoutMs === undefined ? {} : { requestTimeoutMs: timeoutMs }),
|
|
220
|
+
},
|
|
221
|
+
{ method: 'PUT', body: bytes, contentType: 'text/plain' },
|
|
222
|
+
);
|
|
223
|
+
return true;
|
|
224
|
+
} catch {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The terminal status. Authenticated by the callback token rather than the
|
|
231
|
+
* run token, because that is the credential this route checks — and because
|
|
232
|
+
* the run token is revoked by the transition this very call performs.
|
|
233
|
+
*/
|
|
234
|
+
async reportTerminal(
|
|
235
|
+
status: 'completed' | 'failed' | 'cancelled',
|
|
236
|
+
result: Record<string, unknown>,
|
|
237
|
+
error?: string,
|
|
238
|
+
signal?: AbortSignal,
|
|
239
|
+
timeoutMs?: number,
|
|
240
|
+
): Promise<boolean> {
|
|
241
|
+
try {
|
|
242
|
+
await cloudFetch('/api/v1/workflows/callback', {
|
|
243
|
+
...this.bound,
|
|
244
|
+
...(signal === undefined ? {} : { signal }),
|
|
245
|
+
...(timeoutMs === undefined ? {} : { requestTimeoutMs: timeoutMs }),
|
|
246
|
+
}, {
|
|
247
|
+
method: 'POST',
|
|
248
|
+
body: JSON.stringify({
|
|
249
|
+
runId: this.registration.runId,
|
|
250
|
+
callbackToken: this.registration.callbackToken,
|
|
251
|
+
status,
|
|
252
|
+
result,
|
|
253
|
+
...(error === undefined ? {} : { error }),
|
|
254
|
+
}),
|
|
255
|
+
});
|
|
256
|
+
return true;
|
|
257
|
+
} catch {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|