@uru-intelligence/app 1.0.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/LICENSE +58 -0
- package/README.md +146 -0
- package/dist/dampers.d.ts +145 -0
- package/dist/dampers.js +200 -0
- package/dist/datasets.d.ts +11 -0
- package/dist/datasets.js +119 -0
- package/dist/errorResponse.d.ts +2 -0
- package/dist/errorResponse.js +11 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.js +20 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +118 -0
- package/dist/runtime.d.ts +32 -0
- package/dist/runtime.js +71 -0
- package/dist/transport.d.ts +58 -0
- package/dist/transport.js +625 -0
- package/dist/types.d.ts +211 -0
- package/dist/types.js +15 -0
- package/package.json +62 -0
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Low-level HTTP transport for the Uru app runtime client — the capability
|
|
3
|
+
* invocation (`invokeAppRuntime`), job-status poll, and job-cancel calls, plus
|
|
4
|
+
* their private response/state normalizers. Split out of `index.ts` so that file
|
|
5
|
+
* stays the public API + factory layer and this stays the wire layer (and so
|
|
6
|
+
* neither exceeds the repo's 1000-line clean-code budget).
|
|
7
|
+
*
|
|
8
|
+
* The P2 runaway dampers (default idempotency key, 429 backoff, forceRefresh
|
|
9
|
+
* throttle — `docs/plans/gem-runtime-cost-safety-audit.md`) live inline in
|
|
10
|
+
* `invokeAppRuntime` here, composing the pure helpers from `./dampers`.
|
|
11
|
+
*
|
|
12
|
+
* Imports the shared URL/error primitives from `./runtime.js` and the
|
|
13
|
+
* option/event TYPES from `./types.js`. It does NOT import `./index.js`: the
|
|
14
|
+
* barrel is the published API, and the old `index ↔ transport` cycle was paid
|
|
15
|
+
* for by exporting four internal helpers from it. Every edge here points at a
|
|
16
|
+
* module that imports nothing back.
|
|
17
|
+
*/
|
|
18
|
+
import { computeRateLimitBackoffMs, defaultIdempotencyKey, parseRetryAfterSeconds, } from './dampers.js';
|
|
19
|
+
import { runtimeErrorMessage } from './errorResponse.js';
|
|
20
|
+
import { UruAppRuntimeError } from './errors.js';
|
|
21
|
+
import { resolveHeaders, runtimeBasePath, trimTrailingSlash } from './runtime.js';
|
|
22
|
+
function isRecord(value) {
|
|
23
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
function httpRecord(value, label) {
|
|
26
|
+
if (!isRecord(value)) {
|
|
27
|
+
throw new UruAppRuntimeError(`Invalid ${label} response.`, {
|
|
28
|
+
status: 0,
|
|
29
|
+
body: value,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
function invalidOwnedField(label, body, field) {
|
|
35
|
+
throw new UruAppRuntimeError(`Invalid ${label} response field: ${field}.`, {
|
|
36
|
+
status: 0,
|
|
37
|
+
body,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function requiredString(body, field, label) {
|
|
41
|
+
const value = body[field];
|
|
42
|
+
return typeof value === 'string' ? value : invalidOwnedField(label, body, field);
|
|
43
|
+
}
|
|
44
|
+
function requiredBoolean(body, field, label) {
|
|
45
|
+
const value = body[field];
|
|
46
|
+
return typeof value === 'boolean' ? value : invalidOwnedField(label, body, field);
|
|
47
|
+
}
|
|
48
|
+
function optionalString(value) {
|
|
49
|
+
return typeof value === 'string' ? value : undefined;
|
|
50
|
+
}
|
|
51
|
+
function optionalNullableString(value) {
|
|
52
|
+
return value === null || typeof value === 'string' ? value : undefined;
|
|
53
|
+
}
|
|
54
|
+
function optionalNullableNumber(value) {
|
|
55
|
+
return value === null || (typeof value === 'number' && Number.isFinite(value))
|
|
56
|
+
? value
|
|
57
|
+
: undefined;
|
|
58
|
+
}
|
|
59
|
+
function optionalBoolean(value) {
|
|
60
|
+
return typeof value === 'boolean' ? value : undefined;
|
|
61
|
+
}
|
|
62
|
+
function optionalRecord(value) {
|
|
63
|
+
return isRecord(value) ? value : undefined;
|
|
64
|
+
}
|
|
65
|
+
/** Map the owned camelCase HTTP envelope without copying unknown fields. */
|
|
66
|
+
export function appRuntimeInvocationResponseFromHttp(value) {
|
|
67
|
+
const label = 'app runtime invocation';
|
|
68
|
+
const body = httpRecord(value, label);
|
|
69
|
+
const output = {
|
|
70
|
+
ok: requiredBoolean(body, 'ok', label),
|
|
71
|
+
status: requiredString(body, 'status', label),
|
|
72
|
+
state: requiredString(body, 'state', label),
|
|
73
|
+
deploymentId: requiredString(body, 'deploymentId', label),
|
|
74
|
+
capabilityType: requiredString(body, 'capabilityType', label),
|
|
75
|
+
capabilityName: requiredString(body, 'capabilityName', label),
|
|
76
|
+
pending: requiredBoolean(body, 'pending', label),
|
|
77
|
+
};
|
|
78
|
+
if (Object.prototype.hasOwnProperty.call(body, 'result')) {
|
|
79
|
+
output.result = body['result'];
|
|
80
|
+
}
|
|
81
|
+
const executionId = optionalNullableString(body['executionId']);
|
|
82
|
+
if (executionId !== undefined)
|
|
83
|
+
output.executionId = executionId;
|
|
84
|
+
const diagnostics = optionalRecord(body['diagnostics']);
|
|
85
|
+
if (diagnostics !== undefined)
|
|
86
|
+
output.diagnostics = diagnostics;
|
|
87
|
+
return output;
|
|
88
|
+
}
|
|
89
|
+
/** Map the owned camelCase job-status envelope without copying unknown fields. */
|
|
90
|
+
export function appRuntimeExecutionStatusFromHttp(value) {
|
|
91
|
+
const label = 'app runtime job status';
|
|
92
|
+
const body = httpRecord(value, label);
|
|
93
|
+
const output = {
|
|
94
|
+
success: requiredBoolean(body, 'success', label),
|
|
95
|
+
executionId: requiredString(body, 'executionId', label),
|
|
96
|
+
};
|
|
97
|
+
const pending = optionalBoolean(body['pending']);
|
|
98
|
+
if (pending !== undefined)
|
|
99
|
+
output.pending = pending;
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(body, 'result')) {
|
|
101
|
+
output.result = body['result'];
|
|
102
|
+
}
|
|
103
|
+
const error = optionalNullableString(body['error']);
|
|
104
|
+
if (error !== undefined)
|
|
105
|
+
output.error = error;
|
|
106
|
+
const state = optionalString(body['state']);
|
|
107
|
+
if (state !== undefined)
|
|
108
|
+
output.state = state;
|
|
109
|
+
const status = optionalNullableString(body['status']);
|
|
110
|
+
if (status !== undefined)
|
|
111
|
+
output.status = status;
|
|
112
|
+
const statusUrl = optionalNullableString(body['statusUrl']);
|
|
113
|
+
if (statusUrl !== undefined)
|
|
114
|
+
output.statusUrl = statusUrl;
|
|
115
|
+
const retryAfterSeconds = optionalNullableNumber(body['retryAfterSeconds']);
|
|
116
|
+
if (retryAfterSeconds !== undefined)
|
|
117
|
+
output.retryAfterSeconds = retryAfterSeconds;
|
|
118
|
+
const cacheStatus = optionalNullableString(body['cacheStatus']);
|
|
119
|
+
if (cacheStatus !== undefined)
|
|
120
|
+
output.cacheStatus = cacheStatus;
|
|
121
|
+
const executor = optionalNullableString(body['executor']);
|
|
122
|
+
if (executor !== undefined)
|
|
123
|
+
output.executor = executor;
|
|
124
|
+
const executorKind = optionalNullableString(body['executorKind']);
|
|
125
|
+
if (executorKind !== undefined)
|
|
126
|
+
output.executorKind = executorKind;
|
|
127
|
+
return output;
|
|
128
|
+
}
|
|
129
|
+
function routeSegmentForCapabilityType(capabilityType) {
|
|
130
|
+
if (capabilityType === 'server_function') {
|
|
131
|
+
return 'server-functions';
|
|
132
|
+
}
|
|
133
|
+
if (capabilityType === 'endpoint') {
|
|
134
|
+
return 'endpoints';
|
|
135
|
+
}
|
|
136
|
+
if (capabilityType === 'job') {
|
|
137
|
+
return 'jobs';
|
|
138
|
+
}
|
|
139
|
+
return capabilityType;
|
|
140
|
+
}
|
|
141
|
+
function encodeCapabilityPath(capabilityType, capabilityName) {
|
|
142
|
+
if (capabilityType !== 'endpoint') {
|
|
143
|
+
return encodeURIComponent(capabilityName);
|
|
144
|
+
}
|
|
145
|
+
return capabilityName
|
|
146
|
+
.split('/')
|
|
147
|
+
.filter(segment => segment.trim().length > 0)
|
|
148
|
+
.map(segment => encodeURIComponent(segment))
|
|
149
|
+
.join('/');
|
|
150
|
+
}
|
|
151
|
+
function makeRuntimeRequestId() {
|
|
152
|
+
return `uru_app_${Math.random().toString(36).slice(2)}`;
|
|
153
|
+
}
|
|
154
|
+
function runtimeHttpRequestOptions(options) {
|
|
155
|
+
const transport = {};
|
|
156
|
+
if (options?.idempotencyKey) {
|
|
157
|
+
transport.idempotencyKey = options.idempotencyKey;
|
|
158
|
+
}
|
|
159
|
+
if (typeof options?.timeoutMs === 'number') {
|
|
160
|
+
transport.timeoutMs = options.timeoutMs;
|
|
161
|
+
}
|
|
162
|
+
if (options?.forceRefresh === true) {
|
|
163
|
+
transport.forceRefresh = true;
|
|
164
|
+
}
|
|
165
|
+
return transport;
|
|
166
|
+
}
|
|
167
|
+
function normalizeRuntimeState(input) {
|
|
168
|
+
const explicitState = String(input.state ?? '')
|
|
169
|
+
.trim()
|
|
170
|
+
.toLowerCase();
|
|
171
|
+
if (explicitState) {
|
|
172
|
+
return explicitState;
|
|
173
|
+
}
|
|
174
|
+
const cacheStatus = String(input.cacheStatus ?? '')
|
|
175
|
+
.trim()
|
|
176
|
+
.toLowerCase();
|
|
177
|
+
if (cacheStatus === 'refreshing') {
|
|
178
|
+
return 'refreshing';
|
|
179
|
+
}
|
|
180
|
+
if (cacheStatus === 'stale') {
|
|
181
|
+
return 'stale';
|
|
182
|
+
}
|
|
183
|
+
if (cacheStatus === 'fresh' || cacheStatus === 'coalesced') {
|
|
184
|
+
return 'cached';
|
|
185
|
+
}
|
|
186
|
+
if (cacheStatus === 'refreshed') {
|
|
187
|
+
return 'refreshed';
|
|
188
|
+
}
|
|
189
|
+
const status = String(input.status ?? '')
|
|
190
|
+
.trim()
|
|
191
|
+
.toLowerCase();
|
|
192
|
+
if (status === 'queued') {
|
|
193
|
+
return 'queued';
|
|
194
|
+
}
|
|
195
|
+
if (status === 'running') {
|
|
196
|
+
return input.forceRefresh ? 'refreshing' : 'running';
|
|
197
|
+
}
|
|
198
|
+
if (status === 'polling') {
|
|
199
|
+
return 'polling';
|
|
200
|
+
}
|
|
201
|
+
if (status === 'canceled' || status === 'cancelled') {
|
|
202
|
+
return 'canceled';
|
|
203
|
+
}
|
|
204
|
+
if (status === 'rate_limited') {
|
|
205
|
+
return 'rate_limited';
|
|
206
|
+
}
|
|
207
|
+
if (status === 'budget_blocked') {
|
|
208
|
+
return 'budget_blocked';
|
|
209
|
+
}
|
|
210
|
+
if (status === 'service_unavailable') {
|
|
211
|
+
return 'service_unavailable';
|
|
212
|
+
}
|
|
213
|
+
if (status === 'failed' || status === 'error') {
|
|
214
|
+
return 'failed';
|
|
215
|
+
}
|
|
216
|
+
if (status === 'succeeded' || status === 'success' || status === 'completed') {
|
|
217
|
+
return 'succeeded';
|
|
218
|
+
}
|
|
219
|
+
if (input.httpStatus === 429) {
|
|
220
|
+
return 'rate_limited';
|
|
221
|
+
}
|
|
222
|
+
if (input.httpStatus === 402) {
|
|
223
|
+
return 'budget_blocked';
|
|
224
|
+
}
|
|
225
|
+
if (input.httpStatus === 503) {
|
|
226
|
+
return 'service_unavailable';
|
|
227
|
+
}
|
|
228
|
+
if (input.pending === true) {
|
|
229
|
+
return input.forceRefresh ? 'refreshing' : 'running';
|
|
230
|
+
}
|
|
231
|
+
if (input.ok === true || input.success === true) {
|
|
232
|
+
return 'succeeded';
|
|
233
|
+
}
|
|
234
|
+
if (input.ok === false || input.success === false || input.error) {
|
|
235
|
+
return 'failed';
|
|
236
|
+
}
|
|
237
|
+
return 'idle';
|
|
238
|
+
}
|
|
239
|
+
function runtimeEventTypeForState(state, pending) {
|
|
240
|
+
if (state === 'canceled') {
|
|
241
|
+
return 'canceled';
|
|
242
|
+
}
|
|
243
|
+
if (state === 'failed' ||
|
|
244
|
+
state === 'rate_limited' ||
|
|
245
|
+
state === 'budget_blocked' ||
|
|
246
|
+
state === 'service_unavailable') {
|
|
247
|
+
return 'failed';
|
|
248
|
+
}
|
|
249
|
+
if (pending ||
|
|
250
|
+
state === 'queued' ||
|
|
251
|
+
state === 'running' ||
|
|
252
|
+
state === 'refreshing') {
|
|
253
|
+
return 'status';
|
|
254
|
+
}
|
|
255
|
+
return 'completed';
|
|
256
|
+
}
|
|
257
|
+
function emitRuntimeEvent(options, event) {
|
|
258
|
+
if (typeof options?.onEvent === 'function') {
|
|
259
|
+
options.onEvent(event);
|
|
260
|
+
}
|
|
261
|
+
if (typeof options?.onStatus === 'function') {
|
|
262
|
+
options.onStatus(event);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
export async function invokeAppRuntime(options, damper, capabilityType, name, input, invocationOptions) {
|
|
266
|
+
const runtimeFetch = options.fetch ?? globalThis.fetch;
|
|
267
|
+
if (!runtimeFetch) {
|
|
268
|
+
throw new UruAppRuntimeError('Uru app runtime fetch is not available.', {
|
|
269
|
+
status: 0,
|
|
270
|
+
body: null,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
const deploymentId = options.deploymentId.trim();
|
|
274
|
+
const capabilityName = name.trim();
|
|
275
|
+
if (!deploymentId) {
|
|
276
|
+
throw new UruAppRuntimeError('Uru app runtime deployment id is required.', {
|
|
277
|
+
status: 0,
|
|
278
|
+
body: null,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
if (!capabilityName) {
|
|
282
|
+
throw new UruAppRuntimeError('Uru app runtime capability name is required.', {
|
|
283
|
+
status: 0,
|
|
284
|
+
body: null,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
// P2 damper: a requested forceRefresh is honored only if it clears the
|
|
288
|
+
// per-capability throttle; otherwise the call is sent WITHOUT force_refresh so
|
|
289
|
+
// it can be served from cache instead of hammering past it.
|
|
290
|
+
const requestedForce = invocationOptions?.forceRefresh === true;
|
|
291
|
+
const effectiveForce = damper.throttle.shouldForceRefresh({
|
|
292
|
+
key: `${deploymentId}:${capabilityType}:${capabilityName}`,
|
|
293
|
+
requested: requestedForce,
|
|
294
|
+
nowMs: damper.config.now(),
|
|
295
|
+
minIntervalMs: damper.config.forceRefreshMinIntervalMs,
|
|
296
|
+
});
|
|
297
|
+
const requestId = invocationOptions?.requestId ?? makeRuntimeRequestId();
|
|
298
|
+
emitRuntimeEvent(invocationOptions, {
|
|
299
|
+
type: 'started',
|
|
300
|
+
state: effectiveForce ? 'refreshing' : 'queued',
|
|
301
|
+
deploymentId,
|
|
302
|
+
capabilityType,
|
|
303
|
+
capabilityName,
|
|
304
|
+
requestId,
|
|
305
|
+
});
|
|
306
|
+
const baseUrl = options.baseUrl ? trimTrailingSlash(options.baseUrl) : '';
|
|
307
|
+
const basePath = runtimeBasePath(options);
|
|
308
|
+
const routeSegment = routeSegmentForCapabilityType(capabilityType);
|
|
309
|
+
const encodedCapabilityName = encodeCapabilityPath(capabilityType, capabilityName);
|
|
310
|
+
const url = `${baseUrl}${basePath}/apps/${encodeURIComponent(deploymentId)}/${routeSegment}/${encodedCapabilityName}`;
|
|
311
|
+
const headers = new Headers(await resolveHeaders(options.headers));
|
|
312
|
+
headers.set('content-type', 'application/json');
|
|
313
|
+
// Build the transport envelope: the caller's normalized options, but with the
|
|
314
|
+
// throttled forceRefresh decision and — when the caller supplied none — a
|
|
315
|
+
// default idempotency_key so identical accidental re-fires collapse to one
|
|
316
|
+
// Temporal workflow (REJECT_DUPLICATE) instead of thousands.
|
|
317
|
+
const transport = runtimeHttpRequestOptions(invocationOptions);
|
|
318
|
+
if (effectiveForce) {
|
|
319
|
+
transport.forceRefresh = true;
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
delete transport.forceRefresh;
|
|
323
|
+
}
|
|
324
|
+
if (transport.idempotencyKey === undefined &&
|
|
325
|
+
damper.config.defaultIdempotencyKeys) {
|
|
326
|
+
transport.idempotencyKey = defaultIdempotencyKey({
|
|
327
|
+
deploymentId,
|
|
328
|
+
capabilityType,
|
|
329
|
+
capabilityName,
|
|
330
|
+
input: input ?? {},
|
|
331
|
+
nowMs: damper.config.now(),
|
|
332
|
+
windowMs: damper.config.idempotencyWindowMs,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
const requestBody = JSON.stringify({ input: input ?? {}, ...transport });
|
|
336
|
+
// P2 damper: bounded backoff-and-retry on 429 / rate_limited. `attempt` counts
|
|
337
|
+
// retries beyond the first send; the loop performs at most
|
|
338
|
+
// `maxRateLimitRetries` extra sends, honoring Retry-After / retry_after_seconds
|
|
339
|
+
// (capped) before falling through to the normal failure path.
|
|
340
|
+
let response;
|
|
341
|
+
let body;
|
|
342
|
+
for (let attempt = 0;; attempt += 1) {
|
|
343
|
+
response = await runtimeFetch(url, {
|
|
344
|
+
method: 'POST',
|
|
345
|
+
headers,
|
|
346
|
+
body: requestBody,
|
|
347
|
+
});
|
|
348
|
+
body = (await response.json().catch(() => null));
|
|
349
|
+
if (response.status !== 429 || attempt >= damper.config.maxRateLimitRetries) {
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
const delayMs = computeRateLimitBackoffMs({
|
|
353
|
+
attempt,
|
|
354
|
+
retryAfterSeconds: parseRetryAfterSeconds({
|
|
355
|
+
header: response.headers.get('retry-after'),
|
|
356
|
+
body,
|
|
357
|
+
}),
|
|
358
|
+
baseDelayMs: damper.config.rateLimitBaseDelayMs,
|
|
359
|
+
maxDelayMs: damper.config.rateLimitMaxDelayMs,
|
|
360
|
+
});
|
|
361
|
+
emitRuntimeEvent(invocationOptions, {
|
|
362
|
+
type: 'status',
|
|
363
|
+
state: 'rate_limited',
|
|
364
|
+
deploymentId,
|
|
365
|
+
capabilityType,
|
|
366
|
+
capabilityName,
|
|
367
|
+
requestId,
|
|
368
|
+
retryAfterSeconds: Math.ceil(delayMs / 1000),
|
|
369
|
+
});
|
|
370
|
+
await damper.config.sleep(delayMs);
|
|
371
|
+
}
|
|
372
|
+
if (!response.ok) {
|
|
373
|
+
const message = runtimeErrorMessage(body, `Uru app runtime call failed with HTTP ${response.status}`);
|
|
374
|
+
emitRuntimeEvent(invocationOptions, {
|
|
375
|
+
type: 'failed',
|
|
376
|
+
state: normalizeRuntimeState({
|
|
377
|
+
httpStatus: response.status,
|
|
378
|
+
error: message,
|
|
379
|
+
}),
|
|
380
|
+
deploymentId,
|
|
381
|
+
capabilityType,
|
|
382
|
+
capabilityName,
|
|
383
|
+
requestId,
|
|
384
|
+
error: message,
|
|
385
|
+
diagnostics: typeof body === 'object' && body !== null
|
|
386
|
+
? body
|
|
387
|
+
: {},
|
|
388
|
+
});
|
|
389
|
+
throw new UruAppRuntimeError(message, {
|
|
390
|
+
status: response.status,
|
|
391
|
+
body,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
const runtimeResponse = appRuntimeInvocationResponseFromHttp(body);
|
|
395
|
+
const diagnostics = runtimeResponse.diagnostics ?? {};
|
|
396
|
+
const cacheStatus = typeof diagnostics['cache_status'] === 'string'
|
|
397
|
+
? diagnostics['cache_status']
|
|
398
|
+
: null;
|
|
399
|
+
const state = normalizeRuntimeState({
|
|
400
|
+
ok: runtimeResponse.ok,
|
|
401
|
+
pending: runtimeResponse.pending,
|
|
402
|
+
status: runtimeResponse.status,
|
|
403
|
+
state: runtimeResponse.state,
|
|
404
|
+
cacheStatus,
|
|
405
|
+
forceRefresh: effectiveForce,
|
|
406
|
+
});
|
|
407
|
+
const normalizedResponse = {
|
|
408
|
+
ok: runtimeResponse.ok,
|
|
409
|
+
status: runtimeResponse.status,
|
|
410
|
+
state,
|
|
411
|
+
deploymentId: runtimeResponse.deploymentId || deploymentId,
|
|
412
|
+
capabilityType: runtimeResponse.capabilityType || capabilityType,
|
|
413
|
+
capabilityName: runtimeResponse.capabilityName || capabilityName,
|
|
414
|
+
...(Object.prototype.hasOwnProperty.call(runtimeResponse, 'result')
|
|
415
|
+
? { result: runtimeResponse.result }
|
|
416
|
+
: {}),
|
|
417
|
+
...(runtimeResponse.executionId === undefined
|
|
418
|
+
? {}
|
|
419
|
+
: { executionId: runtimeResponse.executionId }),
|
|
420
|
+
pending: runtimeResponse.pending,
|
|
421
|
+
diagnostics,
|
|
422
|
+
};
|
|
423
|
+
emitRuntimeEvent(invocationOptions, {
|
|
424
|
+
type: runtimeEventTypeForState(state, normalizedResponse.pending),
|
|
425
|
+
state,
|
|
426
|
+
deploymentId: normalizedResponse.deploymentId,
|
|
427
|
+
capabilityType: normalizedResponse.capabilityType,
|
|
428
|
+
capabilityName: normalizedResponse.capabilityName,
|
|
429
|
+
executionId: normalizedResponse.executionId,
|
|
430
|
+
requestId,
|
|
431
|
+
result: normalizedResponse.result,
|
|
432
|
+
cacheStatus,
|
|
433
|
+
diagnostics,
|
|
434
|
+
});
|
|
435
|
+
return normalizedResponse;
|
|
436
|
+
}
|
|
437
|
+
export async function getAppRuntimeJobStatus(options, executionId, invocationOptions) {
|
|
438
|
+
const runtimeFetch = options.fetch ?? globalThis.fetch;
|
|
439
|
+
if (!runtimeFetch) {
|
|
440
|
+
throw new UruAppRuntimeError('Uru app runtime fetch is not available.', {
|
|
441
|
+
status: 0,
|
|
442
|
+
body: null,
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
const deploymentId = options.deploymentId.trim();
|
|
446
|
+
const normalizedExecutionId = executionId.trim();
|
|
447
|
+
if (!deploymentId || !normalizedExecutionId) {
|
|
448
|
+
throw new UruAppRuntimeError('Uru app runtime deployment id and execution id are required.', {
|
|
449
|
+
status: 0,
|
|
450
|
+
body: null,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
const requestId = invocationOptions?.requestId ?? makeRuntimeRequestId();
|
|
454
|
+
emitRuntimeEvent(invocationOptions, {
|
|
455
|
+
type: 'status',
|
|
456
|
+
state: 'polling',
|
|
457
|
+
deploymentId,
|
|
458
|
+
capabilityType: 'job',
|
|
459
|
+
capabilityName: '',
|
|
460
|
+
executionId: normalizedExecutionId,
|
|
461
|
+
requestId,
|
|
462
|
+
});
|
|
463
|
+
const baseUrl = options.baseUrl ? trimTrailingSlash(options.baseUrl) : '';
|
|
464
|
+
const basePath = runtimeBasePath(options);
|
|
465
|
+
const url = `${baseUrl}${basePath}/apps/${encodeURIComponent(deploymentId)}/jobs/executions/${encodeURIComponent(normalizedExecutionId)}`;
|
|
466
|
+
const headers = new Headers(await resolveHeaders(options.headers));
|
|
467
|
+
const response = await runtimeFetch(url, { method: 'GET', headers });
|
|
468
|
+
const body = (await response.json().catch(() => null));
|
|
469
|
+
if (!response.ok && response.status !== 202) {
|
|
470
|
+
const message = runtimeErrorMessage(body, `Uru app runtime job status failed with HTTP ${response.status}`);
|
|
471
|
+
emitRuntimeEvent(invocationOptions, {
|
|
472
|
+
type: 'failed',
|
|
473
|
+
state: normalizeRuntimeState({
|
|
474
|
+
httpStatus: response.status,
|
|
475
|
+
error: message,
|
|
476
|
+
}),
|
|
477
|
+
deploymentId,
|
|
478
|
+
capabilityType: 'job',
|
|
479
|
+
capabilityName: '',
|
|
480
|
+
executionId: normalizedExecutionId,
|
|
481
|
+
requestId,
|
|
482
|
+
error: message,
|
|
483
|
+
diagnostics: typeof body === 'object' && body !== null
|
|
484
|
+
? body
|
|
485
|
+
: {},
|
|
486
|
+
});
|
|
487
|
+
throw new UruAppRuntimeError(message, {
|
|
488
|
+
status: response.status,
|
|
489
|
+
body,
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
const statusBody = appRuntimeExecutionStatusFromHttp(body);
|
|
493
|
+
const state = normalizeRuntimeState({
|
|
494
|
+
success: statusBody.success,
|
|
495
|
+
pending: statusBody.pending,
|
|
496
|
+
status: statusBody.status,
|
|
497
|
+
state: statusBody.state,
|
|
498
|
+
cacheStatus: statusBody.cacheStatus,
|
|
499
|
+
error: statusBody.error,
|
|
500
|
+
});
|
|
501
|
+
const normalizedStatus = {
|
|
502
|
+
...(statusBody.success === undefined ? {} : { success: statusBody.success }),
|
|
503
|
+
...(statusBody.pending === undefined ? {} : { pending: statusBody.pending }),
|
|
504
|
+
...(Object.prototype.hasOwnProperty.call(statusBody, 'result')
|
|
505
|
+
? { result: statusBody.result }
|
|
506
|
+
: {}),
|
|
507
|
+
...(statusBody.error === undefined ? {} : { error: statusBody.error }),
|
|
508
|
+
state,
|
|
509
|
+
...(statusBody.status === undefined ? {} : { status: statusBody.status }),
|
|
510
|
+
...(statusBody.executionId === undefined
|
|
511
|
+
? {}
|
|
512
|
+
: { executionId: statusBody.executionId }),
|
|
513
|
+
...(statusBody.statusUrl === undefined
|
|
514
|
+
? {}
|
|
515
|
+
: { statusUrl: statusBody.statusUrl }),
|
|
516
|
+
...(statusBody.retryAfterSeconds === undefined
|
|
517
|
+
? {}
|
|
518
|
+
: { retryAfterSeconds: statusBody.retryAfterSeconds }),
|
|
519
|
+
...(statusBody.cacheStatus === undefined
|
|
520
|
+
? {}
|
|
521
|
+
: { cacheStatus: statusBody.cacheStatus }),
|
|
522
|
+
...(statusBody.executor === undefined ? {} : { executor: statusBody.executor }),
|
|
523
|
+
...(statusBody.executorKind === undefined
|
|
524
|
+
? {}
|
|
525
|
+
: { executorKind: statusBody.executorKind }),
|
|
526
|
+
};
|
|
527
|
+
emitRuntimeEvent(invocationOptions, {
|
|
528
|
+
type: runtimeEventTypeForState(state, normalizedStatus.pending),
|
|
529
|
+
state,
|
|
530
|
+
deploymentId,
|
|
531
|
+
capabilityType: 'job',
|
|
532
|
+
capabilityName: '',
|
|
533
|
+
executionId: normalizedExecutionId,
|
|
534
|
+
requestId,
|
|
535
|
+
result: normalizedStatus.result,
|
|
536
|
+
error: normalizedStatus.error,
|
|
537
|
+
retryAfterSeconds: normalizedStatus.retryAfterSeconds,
|
|
538
|
+
cacheStatus: normalizedStatus.cacheStatus,
|
|
539
|
+
});
|
|
540
|
+
return normalizedStatus;
|
|
541
|
+
}
|
|
542
|
+
export async function cancelAppRuntimeJob(options, executionId, invocationOptions) {
|
|
543
|
+
const runtimeFetch = options.fetch ?? globalThis.fetch;
|
|
544
|
+
if (!runtimeFetch) {
|
|
545
|
+
throw new UruAppRuntimeError('Uru app runtime fetch is not available.', {
|
|
546
|
+
status: 0,
|
|
547
|
+
body: null,
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
const deploymentId = options.deploymentId.trim();
|
|
551
|
+
const normalizedExecutionId = executionId.trim();
|
|
552
|
+
if (!deploymentId || !normalizedExecutionId) {
|
|
553
|
+
throw new UruAppRuntimeError('Uru app runtime deployment id and execution id are required.', {
|
|
554
|
+
status: 0,
|
|
555
|
+
body: null,
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
const requestId = invocationOptions?.requestId ?? makeRuntimeRequestId();
|
|
559
|
+
const baseUrl = options.baseUrl ? trimTrailingSlash(options.baseUrl) : '';
|
|
560
|
+
const basePath = runtimeBasePath(options);
|
|
561
|
+
const url = `${baseUrl}${basePath}/apps/${encodeURIComponent(deploymentId)}/jobs/executions/${encodeURIComponent(normalizedExecutionId)}/cancel`;
|
|
562
|
+
const headers = new Headers(await resolveHeaders(options.headers));
|
|
563
|
+
const response = await runtimeFetch(url, { method: 'POST', headers });
|
|
564
|
+
const body = (await response.json().catch(() => null));
|
|
565
|
+
if (!response.ok) {
|
|
566
|
+
const message = runtimeErrorMessage(body, `Uru app runtime job cancel failed with HTTP ${response.status}`);
|
|
567
|
+
emitRuntimeEvent(invocationOptions, {
|
|
568
|
+
type: 'failed',
|
|
569
|
+
state: normalizeRuntimeState({
|
|
570
|
+
httpStatus: response.status,
|
|
571
|
+
error: message,
|
|
572
|
+
}),
|
|
573
|
+
deploymentId,
|
|
574
|
+
capabilityType: 'job',
|
|
575
|
+
capabilityName: '',
|
|
576
|
+
executionId: normalizedExecutionId,
|
|
577
|
+
requestId,
|
|
578
|
+
error: message,
|
|
579
|
+
diagnostics: typeof body === 'object' && body !== null
|
|
580
|
+
? body
|
|
581
|
+
: {},
|
|
582
|
+
});
|
|
583
|
+
throw new UruAppRuntimeError(message, {
|
|
584
|
+
status: response.status,
|
|
585
|
+
body,
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
const runtimeResponse = appRuntimeInvocationResponseFromHttp(body);
|
|
589
|
+
const diagnostics = runtimeResponse.diagnostics ?? {};
|
|
590
|
+
const normalizedResponse = {
|
|
591
|
+
ok: runtimeResponse.ok,
|
|
592
|
+
status: runtimeResponse.status,
|
|
593
|
+
state: normalizeRuntimeState({
|
|
594
|
+
ok: runtimeResponse.ok,
|
|
595
|
+
pending: runtimeResponse.pending,
|
|
596
|
+
status: runtimeResponse.status,
|
|
597
|
+
state: runtimeResponse.state,
|
|
598
|
+
cacheStatus: typeof diagnostics['cache_status'] === 'string'
|
|
599
|
+
? diagnostics['cache_status']
|
|
600
|
+
: null,
|
|
601
|
+
}),
|
|
602
|
+
deploymentId: runtimeResponse.deploymentId || deploymentId,
|
|
603
|
+
capabilityType: runtimeResponse.capabilityType || 'job',
|
|
604
|
+
capabilityName: runtimeResponse.capabilityName,
|
|
605
|
+
...(Object.prototype.hasOwnProperty.call(runtimeResponse, 'result')
|
|
606
|
+
? { result: runtimeResponse.result }
|
|
607
|
+
: {}),
|
|
608
|
+
...(runtimeResponse.executionId === undefined
|
|
609
|
+
? {}
|
|
610
|
+
: { executionId: runtimeResponse.executionId }),
|
|
611
|
+
pending: runtimeResponse.pending,
|
|
612
|
+
diagnostics,
|
|
613
|
+
};
|
|
614
|
+
emitRuntimeEvent(invocationOptions, {
|
|
615
|
+
type: 'canceled',
|
|
616
|
+
state: 'canceled',
|
|
617
|
+
deploymentId: normalizedResponse.deploymentId,
|
|
618
|
+
capabilityType: 'job',
|
|
619
|
+
capabilityName: normalizedResponse.capabilityName,
|
|
620
|
+
executionId: normalizedExecutionId,
|
|
621
|
+
requestId,
|
|
622
|
+
diagnostics,
|
|
623
|
+
});
|
|
624
|
+
return normalizedResponse;
|
|
625
|
+
}
|