@uru-intelligence/app 2.1.2 → 2.1.4
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/README.md +7 -0
- package/dist/dampers.d.ts +3 -3
- package/dist/dampers.js +24 -6
- package/dist/index.d.ts +1 -1
- package/dist/transport-deadline.d.ts +8 -0
- package/dist/transport-deadline.js +107 -0
- package/dist/transport-invocation.d.ts +21 -0
- package/dist/transport-invocation.js +48 -0
- package/dist/transport.d.ts +3 -1
- package/dist/transport.js +199 -53
- package/dist/types.d.ts +11 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -155,6 +155,8 @@ const response = await app.query('summary', {}, { onStatus: event => render(even
|
|
|
155
155
|
on HTTP 429 and throttles `forceRefresh` per capability. Override these
|
|
156
156
|
controls through `dampers`; see `UruRunawayDamperOptions`. Supply an explicit
|
|
157
157
|
`idempotencyKey` when your application retries the same logical operation.
|
|
158
|
+
A custom retry wait can accept the optional `AbortSignal` to stop its own
|
|
159
|
+
work. The SDK still bounds the call when that wait ignores the signal.
|
|
158
160
|
|
|
159
161
|
## Transport values
|
|
160
162
|
|
|
@@ -169,6 +171,11 @@ Input that exceeds the JavaScript runtime's serialization limit also fails
|
|
|
169
171
|
before either action. `timeoutMs`, when set, must be an integer from 1,000 to
|
|
170
172
|
60,000, as required by the runtime HTTP contract.
|
|
171
173
|
|
|
174
|
+
Pass an `AbortSignal` as `signal` to cancel a capability call, job status read,
|
|
175
|
+
or job cancel request. The signal is local to the SDK and is never sent in the
|
|
176
|
+
JSON body. When `timeoutMs` is set, it also bounds the network request, response
|
|
177
|
+
body read, and any 429 retry wait.
|
|
178
|
+
|
|
172
179
|
Response diagnostics use the same JSON rules. Invalid diagnostics in a success
|
|
173
180
|
response cause `UruAppRuntimeError`. An HTTP error keeps its original status and
|
|
174
181
|
body. Diagnostic keys remain opaque; the client does not change their casing.
|
package/dist/dampers.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export interface ResolvedDamperConfig {
|
|
|
7
7
|
readonly rateLimitMaxDelayMs: number;
|
|
8
8
|
readonly forceRefreshMinIntervalMs: number;
|
|
9
9
|
readonly now: () => number;
|
|
10
|
-
readonly sleep: (ms: number) => Promise<void>;
|
|
10
|
+
readonly sleep: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
11
11
|
}
|
|
12
12
|
/** Caller-supplied damper overrides; every field defaults (see {@link resolveDamperConfig}). */
|
|
13
13
|
export interface UruRunawayDamperOptions {
|
|
@@ -23,8 +23,8 @@ export interface UruRunawayDamperOptions {
|
|
|
23
23
|
forceRefreshMinIntervalMs?: number;
|
|
24
24
|
/** Clock (ms); injectable for tests. Defaults to `Date.now`. */
|
|
25
25
|
now?: () => number;
|
|
26
|
-
/** Async delay; injectable for tests. Defaults to `setTimeout`. */
|
|
27
|
-
sleep?: (ms: number) => Promise<void>;
|
|
26
|
+
/** Async delay; injectable for tests. Defaults to an abortable `setTimeout`. */
|
|
27
|
+
sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
28
28
|
}
|
|
29
29
|
/** Fold caller overrides into a fully-defaulted {@link ResolvedDamperConfig}. */
|
|
30
30
|
export declare const resolveDamperConfig: (options: UruRunawayDamperOptions | undefined) => ResolvedDamperConfig;
|
package/dist/dampers.js
CHANGED
|
@@ -10,13 +10,31 @@ const positiveOr = (value, fallback) => typeof value === "number" && Number.isFi
|
|
|
10
10
|
const nonNegativeOr = (value, fallback) => typeof value === "number" && Number.isFinite(value) && value >= 0
|
|
11
11
|
? value
|
|
12
12
|
: fallback;
|
|
13
|
-
const defaultSleep =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const defaultSleep = (ms, signal) => new Promise((resolve, reject) => {
|
|
14
|
+
let timeout;
|
|
15
|
+
const abort = () => {
|
|
16
|
+
cleanup();
|
|
17
|
+
reject(signal?.reason ??
|
|
18
|
+
new DOMException("The operation was aborted.", "AbortError"));
|
|
19
|
+
};
|
|
20
|
+
const cleanup = () => {
|
|
21
|
+
clearTimeout(timeout);
|
|
22
|
+
signal?.removeEventListener("abort", abort);
|
|
23
|
+
};
|
|
24
|
+
timeout = setTimeout(() => {
|
|
25
|
+
cleanup();
|
|
26
|
+
resolve();
|
|
17
27
|
}, Math.max(0, ms));
|
|
18
|
-
|
|
19
|
-
|
|
28
|
+
if (signal === undefined)
|
|
29
|
+
return;
|
|
30
|
+
if (signal.aborted) {
|
|
31
|
+
abort();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
35
|
+
if (signal.aborted)
|
|
36
|
+
abort();
|
|
37
|
+
});
|
|
20
38
|
/** Fold caller overrides into a fully-defaulted {@link ResolvedDamperConfig}. */
|
|
21
39
|
export const resolveDamperConfig = (options) => ({
|
|
22
40
|
defaultIdempotencyKeys: options?.defaultIdempotencyKeys ?? true,
|
package/dist/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ import type { CreateUruAppRuntimeClientOptions, CreateUruServerContextOptions, U
|
|
|
13
13
|
export { UruAppRuntimeError } from "./errors.js";
|
|
14
14
|
export { URU_UPLOAD_MAX_BYTES, createUploadsContext as createUruUploadsContext } from "./uploads.js";
|
|
15
15
|
export type { UruRunawayDamperOptions } from "./dampers.js";
|
|
16
|
-
export type { CreateUruAppRuntimeClientOptions, CreateUruServerContextOptions, UruAppRuntimeCapabilityType, UruAppRuntimeClient, UruAppRuntimeEvent, UruAppRuntimeEventType, UruAppRuntimeExecutionStatus, UruAppRuntimeInvocationOptions, UruAppRuntimeInvocationResponse, UruAppRuntimeState, UruDecodedInvocationOptions, UruResultDecoder, UruDatasetMutationResult, UruDatasetQuery, UruDatasetRow, UruDatasetRowsResult, UruDatasetSortClause, UruDatasetSqlWriteOptions, UruDatasetWhereClause, UruDatasetWhereOperator, UruDatasetsContext, UruFilesContext, UruFileMetadata, UruFileContent, UruFileOutput, UruFileOutputSummary, UruJobProgress, UruJobCheckpoint, UruProviderRuntimeEnvironment, UruServerContext, UruUploadGrant, UruUploadOutcome, UruUploadResult, UruUploadsContext, } from "./types.js";
|
|
16
|
+
export type { CreateUruAppRuntimeClientOptions, CreateUruServerContextOptions, UruAppRuntimeCapabilityType, UruAppRuntimeClient, UruAppRuntimeEvent, UruAppRuntimeEventType, UruAppRuntimeExecutionDiagnostics, UruAppRuntimeExecutionStatus, UruAppRuntimeInvocationOptions, UruAppRuntimeInvocationResponse, UruAppRuntimeState, UruDecodedInvocationOptions, UruResultDecoder, UruDatasetMutationResult, UruDatasetQuery, UruDatasetRow, UruDatasetRowsResult, UruDatasetSortClause, UruDatasetSqlWriteOptions, UruDatasetWhereClause, UruDatasetWhereOperator, UruDatasetsContext, UruFilesContext, UruFileMetadata, UruFileContent, UruFileOutput, UruFileOutputSummary, UruJobProgress, UruJobCheckpoint, UruProviderRuntimeEnvironment, UruServerContext, UruUploadGrant, UruUploadOutcome, UruUploadResult, UruUploadsContext, } from "./types.js";
|
|
17
17
|
/**
|
|
18
18
|
* Build a capability-invocation client against a Uru app runtime.
|
|
19
19
|
*
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { UruAppRuntimeInvocationOptions } from "./types.js";
|
|
2
|
+
export declare const assertRuntimeSignal: (value: unknown) => void;
|
|
3
|
+
export declare const validateRuntimeInvocationOptions: (options?: UruAppRuntimeInvocationOptions) => void;
|
|
4
|
+
export declare const withRuntimeRequestSignal: <T>(options: UruAppRuntimeInvocationOptions | undefined, action: (signal: AbortSignal | undefined) => Promise<T>) => Promise<T>;
|
|
5
|
+
export declare const abortReason: (signal: AbortSignal) => unknown;
|
|
6
|
+
/** Race a fetch, body read, or retry wait against the same request signal. */
|
|
7
|
+
export declare const awaitWithAbort: <T>(promise: Promise<T>, signal: AbortSignal | undefined) => Promise<T>;
|
|
8
|
+
export declare const readRuntimeResponseBody: (response: Response, signal?: AbortSignal) => Promise<unknown>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { UruAppRuntimeError } from "./errors.js";
|
|
2
|
+
export const assertRuntimeSignal = (value) => {
|
|
3
|
+
if (value !== undefined &&
|
|
4
|
+
(typeof AbortSignal === "undefined" || !(value instanceof AbortSignal))) {
|
|
5
|
+
throw new UruAppRuntimeError("Invalid app runtime signal.", {
|
|
6
|
+
body: null,
|
|
7
|
+
status: 0,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
const assertRuntimeTimeout = (value) => {
|
|
12
|
+
if (!Number.isInteger(value) || value < 1000 || value > 60_000) {
|
|
13
|
+
throw new UruAppRuntimeError("Invalid app runtime timeoutMs.", {
|
|
14
|
+
body: value,
|
|
15
|
+
status: 0,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export const validateRuntimeInvocationOptions = (options) => {
|
|
20
|
+
assertRuntimeSignal(options?.signal);
|
|
21
|
+
if (options?.timeoutMs !== undefined) {
|
|
22
|
+
assertRuntimeTimeout(options.timeoutMs);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const runtimeSignalIsAborted = (signal) => signal?.aborted === true;
|
|
26
|
+
/**
|
|
27
|
+
* Make one deadline for the whole network operation. The regular timer keeps
|
|
28
|
+
* the deadline live even when the caller has no other active event-loop work.
|
|
29
|
+
* The owner clears it after the response body or aborted request settles.
|
|
30
|
+
*/
|
|
31
|
+
const runtimeRequestSignal = (options) => {
|
|
32
|
+
validateRuntimeInvocationOptions(options);
|
|
33
|
+
const timeoutMs = options?.timeoutMs;
|
|
34
|
+
const callerSignal = options?.signal;
|
|
35
|
+
if (timeoutMs === undefined) {
|
|
36
|
+
return callerSignal === undefined
|
|
37
|
+
? { dispose: () => undefined }
|
|
38
|
+
: { signal: callerSignal, dispose: () => undefined };
|
|
39
|
+
}
|
|
40
|
+
const timeoutController = new AbortController();
|
|
41
|
+
const timeout = setTimeout(() => {
|
|
42
|
+
timeoutController.abort(new DOMException("The operation timed out.", "TimeoutError"));
|
|
43
|
+
}, timeoutMs);
|
|
44
|
+
return {
|
|
45
|
+
signal: callerSignal === undefined
|
|
46
|
+
? timeoutController.signal
|
|
47
|
+
: AbortSignal.any([callerSignal, timeoutController.signal]),
|
|
48
|
+
dispose: () => clearTimeout(timeout),
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export const withRuntimeRequestSignal = async (options, action) => {
|
|
52
|
+
const request = runtimeRequestSignal(options);
|
|
53
|
+
try {
|
|
54
|
+
return await action(request.signal);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
request.dispose();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
export const abortReason = (signal) => signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
61
|
+
/** Race a fetch, body read, or retry wait against the same request signal. */
|
|
62
|
+
export const awaitWithAbort = async (promise, signal) => {
|
|
63
|
+
if (signal === undefined)
|
|
64
|
+
return await promise;
|
|
65
|
+
if (signal.aborted)
|
|
66
|
+
throw abortReason(signal);
|
|
67
|
+
return await new Promise((resolve, reject) => {
|
|
68
|
+
const onAbort = () => {
|
|
69
|
+
signal.removeEventListener("abort", onAbort);
|
|
70
|
+
reject(abortReason(signal));
|
|
71
|
+
};
|
|
72
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
73
|
+
void promise.then(value => {
|
|
74
|
+
signal.removeEventListener("abort", onAbort);
|
|
75
|
+
resolve(value);
|
|
76
|
+
}, error => {
|
|
77
|
+
signal.removeEventListener("abort", onAbort);
|
|
78
|
+
reject(error);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
export const readRuntimeResponseBody = async (response, signal) => {
|
|
83
|
+
if (runtimeSignalIsAborted(signal)) {
|
|
84
|
+
void response.body?.cancel().catch(() => undefined);
|
|
85
|
+
throw abortReason(signal);
|
|
86
|
+
}
|
|
87
|
+
const parsed = Promise.resolve()
|
|
88
|
+
.then(() => {
|
|
89
|
+
if (runtimeSignalIsAborted(signal))
|
|
90
|
+
throw abortReason(signal);
|
|
91
|
+
return response.json();
|
|
92
|
+
})
|
|
93
|
+
.catch(() => {
|
|
94
|
+
if (runtimeSignalIsAborted(signal))
|
|
95
|
+
throw abortReason(signal);
|
|
96
|
+
return null;
|
|
97
|
+
});
|
|
98
|
+
try {
|
|
99
|
+
return await awaitWithAbort(parsed, signal);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (runtimeSignalIsAborted(signal)) {
|
|
103
|
+
void response.body?.cancel().catch(() => undefined);
|
|
104
|
+
}
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DamperRuntime } from "./transport.js";
|
|
2
|
+
import type { UruAppRuntimeCapabilityType, UruAppRuntimeEvent } from "./types.js";
|
|
3
|
+
import type { RuntimeFetch } from "./runtime.js";
|
|
4
|
+
export interface InvocationFetchArguments {
|
|
5
|
+
capabilityName: string;
|
|
6
|
+
capabilityType: UruAppRuntimeCapabilityType;
|
|
7
|
+
damper: DamperRuntime;
|
|
8
|
+
deploymentId: string;
|
|
9
|
+
emit: (event: UruAppRuntimeEvent) => void;
|
|
10
|
+
headers: Headers;
|
|
11
|
+
requestBody: string;
|
|
12
|
+
requestId: string;
|
|
13
|
+
runtimeFetch: RuntimeFetch;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
url: string;
|
|
16
|
+
}
|
|
17
|
+
export interface RuntimeFetchResult {
|
|
18
|
+
body: unknown;
|
|
19
|
+
response: Response;
|
|
20
|
+
}
|
|
21
|
+
export declare const fetchInvocation: (args: InvocationFetchArguments, attempt?: number) => Promise<RuntimeFetchResult>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { computeRateLimitBackoffMs, parseRetryAfterSeconds, } from "./dampers.js";
|
|
2
|
+
import { awaitWithAbort, readRuntimeResponseBody, } from "./transport-deadline.js";
|
|
3
|
+
export const fetchInvocation = async (args, attempt = 0) => {
|
|
4
|
+
// Read out of `args` first. `args.runtimeFetch(...)` is a METHOD call, so it
|
|
5
|
+
// would hand the fetch `args` as its receiver — the same class of mistake
|
|
6
|
+
// that broke the global one.
|
|
7
|
+
const { runtimeFetch } = args;
|
|
8
|
+
args.signal?.throwIfAborted();
|
|
9
|
+
const response = await awaitWithAbort(Promise.resolve().then(() => {
|
|
10
|
+
args.signal?.throwIfAborted();
|
|
11
|
+
return runtimeFetch(args.url, {
|
|
12
|
+
body: args.requestBody,
|
|
13
|
+
headers: args.headers,
|
|
14
|
+
method: "POST",
|
|
15
|
+
...(args.signal === undefined ? {} : { signal: args.signal }),
|
|
16
|
+
});
|
|
17
|
+
}), args.signal);
|
|
18
|
+
const body = await readRuntimeResponseBody(response, args.signal);
|
|
19
|
+
const shouldRetry = response.status === 429 && attempt < args.damper.config.maxRateLimitRetries;
|
|
20
|
+
if (!shouldRetry) {
|
|
21
|
+
return { body, response };
|
|
22
|
+
}
|
|
23
|
+
const delayMs = computeRateLimitBackoffMs({
|
|
24
|
+
attempt,
|
|
25
|
+
baseDelayMs: args.damper.config.rateLimitBaseDelayMs,
|
|
26
|
+
maxDelayMs: args.damper.config.rateLimitMaxDelayMs,
|
|
27
|
+
retryAfterSeconds: parseRetryAfterSeconds({
|
|
28
|
+
body,
|
|
29
|
+
header: response.headers.get("retry-after"),
|
|
30
|
+
}),
|
|
31
|
+
});
|
|
32
|
+
args.signal?.throwIfAborted();
|
|
33
|
+
args.emit({
|
|
34
|
+
capabilityName: args.capabilityName,
|
|
35
|
+
capabilityType: args.capabilityType,
|
|
36
|
+
deploymentId: args.deploymentId,
|
|
37
|
+
requestId: args.requestId,
|
|
38
|
+
retryAfterSeconds: Math.ceil(delayMs / 1000),
|
|
39
|
+
state: "rate_limited",
|
|
40
|
+
type: "status",
|
|
41
|
+
});
|
|
42
|
+
args.signal?.throwIfAborted();
|
|
43
|
+
const sleepPromise = args.signal === undefined
|
|
44
|
+
? args.damper.config.sleep(delayMs)
|
|
45
|
+
: args.damper.config.sleep(delayMs, args.signal);
|
|
46
|
+
await awaitWithAbort(sleepPromise, args.signal);
|
|
47
|
+
return await fetchInvocation(args, attempt + 1);
|
|
48
|
+
};
|
package/dist/transport.d.ts
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import type { ForceRefreshThrottle, ResolvedDamperConfig } from "./dampers.js";
|
|
19
19
|
import type { RuntimeJsonObject } from "./transport-json.js";
|
|
20
|
-
import type { CreateUruAppRuntimeClientOptions, UruAppRuntimeCapabilityType, UruAppRuntimeExecutionStatus, UruAppRuntimeInvocationOptions, UruAppRuntimeInvocationResponse, UruResultDecoder } from "./types.js";
|
|
20
|
+
import type { CreateUruAppRuntimeClientOptions, UruAppRuntimeCapabilityType, UruAppRuntimeExecutionDiagnostics, UruAppRuntimeExecutionStatus, UruAppRuntimeInvocationOptions, UruAppRuntimeInvocationResponse, UruResultDecoder } from "./types.js";
|
|
21
21
|
/** Per-client damper state: the resolved config plus the forceRefresh throttle. */
|
|
22
22
|
export interface DamperRuntime {
|
|
23
23
|
readonly config: ResolvedDamperConfig;
|
|
@@ -48,6 +48,8 @@ interface AppRuntimeExecutionHttpResponse {
|
|
|
48
48
|
cacheStatus?: string | null;
|
|
49
49
|
executor?: string | null;
|
|
50
50
|
executorKind?: string | null;
|
|
51
|
+
code?: string | null;
|
|
52
|
+
diagnostics?: UruAppRuntimeExecutionDiagnostics;
|
|
51
53
|
}
|
|
52
54
|
/** Map the owned camelCase HTTP envelope without copying unknown fields. */
|
|
53
55
|
export declare const appRuntimeInvocationResponseFromHttp: (value: unknown, decodeResult?: UruResultDecoder<unknown>) => AppRuntimeInvocationHttpResponse;
|
package/dist/transport.js
CHANGED
|
@@ -15,11 +15,95 @@
|
|
|
15
15
|
* for by exporting four internal helpers from it. Every edge here points at a
|
|
16
16
|
* module that imports nothing back.
|
|
17
17
|
*/
|
|
18
|
-
import { computeRateLimitBackoffMs, parseRetryAfterSeconds, } from "./dampers.js";
|
|
19
18
|
import { runtimeErrorMessage } from "./error-response.js";
|
|
20
19
|
import { UruAppRuntimeError } from "./errors.js";
|
|
21
20
|
import { resolveHeaders, resolveRuntimeFetch, runtimeBasePath, trimTrailingSlash, } from "./runtime.js";
|
|
21
|
+
import { abortReason, awaitWithAbort, readRuntimeResponseBody, validateRuntimeInvocationOptions, withRuntimeRequestSignal, } from "./transport-deadline.js";
|
|
22
|
+
import { fetchInvocation } from "./transport-invocation.js";
|
|
22
23
|
import { isRuntimeJsonObject } from "./transport-json.js";
|
|
24
|
+
/**
|
|
25
|
+
* Only platform failure codes from the public Gem failure contract may cross
|
|
26
|
+
* this boundary. Provider-defined details stay inside the HTTP response.
|
|
27
|
+
*/
|
|
28
|
+
const SAFE_RUNTIME_FAILURE_CODES = new Set([
|
|
29
|
+
"artifact_tool_caller_identity_required",
|
|
30
|
+
"invalid_wrapper_tool_name",
|
|
31
|
+
"runtime_identity_missing",
|
|
32
|
+
"policy_receipt_handoff_invalid",
|
|
33
|
+
"mcp_proxy_not_configured",
|
|
34
|
+
"missing_api_key",
|
|
35
|
+
"dataset_frozen_gem_release_unverified",
|
|
36
|
+
"dataset_frozen_gem_compatibility_disabled",
|
|
37
|
+
"timeout",
|
|
38
|
+
"transport_error",
|
|
39
|
+
"connection_unavailable",
|
|
40
|
+
"artifact_tool_receipt_unavailable",
|
|
41
|
+
"artifact_tool_attempt_unavailable",
|
|
42
|
+
"tool_result_content_unavailable",
|
|
43
|
+
"tool_parameters_content_unavailable",
|
|
44
|
+
"artifact_tool_refresh_failed",
|
|
45
|
+
"tool_admission_unavailable",
|
|
46
|
+
"tool_gateway_denied",
|
|
47
|
+
"app_runtime_failed",
|
|
48
|
+
"artifact_proxy_failed",
|
|
49
|
+
"artifact_rate_limited",
|
|
50
|
+
"artifact_tool_access_invalid",
|
|
51
|
+
"artifact_tool_admission_identity_invalid",
|
|
52
|
+
"artifact_tool_admission_limited",
|
|
53
|
+
"artifact_tool_admission_unavailable",
|
|
54
|
+
"artifact_tool_authority_identity_invalid",
|
|
55
|
+
"artifact_tool_binding_identity_mismatch",
|
|
56
|
+
"artifact_tool_binding_not_visible",
|
|
57
|
+
"artifact_tool_cache_refresh_in_progress",
|
|
58
|
+
"artifact_tool_circuit_open",
|
|
59
|
+
"artifact_tool_execution_cancelled",
|
|
60
|
+
"artifact_tool_execution_failed",
|
|
61
|
+
"artifact_tool_execution_pending",
|
|
62
|
+
"artifact_tool_pre_dispatch_failed",
|
|
63
|
+
"artifact_tool_provider_outcome_unknown",
|
|
64
|
+
"artifact_tool_terminalization_unavailable",
|
|
65
|
+
"execution_failed",
|
|
66
|
+
"execution_outcome_unknown",
|
|
67
|
+
"historical_result_unavailable",
|
|
68
|
+
"job_canceled",
|
|
69
|
+
"job_cleanup_failed",
|
|
70
|
+
"job_completion_not_accepted",
|
|
71
|
+
"job_handler_failed",
|
|
72
|
+
"job_hard_deadline",
|
|
73
|
+
"job_machine_start_failed",
|
|
74
|
+
"job_machine_stopped",
|
|
75
|
+
"job_output_invalid",
|
|
76
|
+
"job_prepare_failed",
|
|
77
|
+
"job_report_failed",
|
|
78
|
+
"job_revoked",
|
|
79
|
+
"job_runtime_failed",
|
|
80
|
+
"legacy_tool_contract_disabled",
|
|
81
|
+
"program_job_attach_rejected",
|
|
82
|
+
"program_job_capacity_exceeded",
|
|
83
|
+
"program_job_owner_missing",
|
|
84
|
+
"program_job_spending_check_failed",
|
|
85
|
+
"program_job_spending_limit",
|
|
86
|
+
"provider_credential_rejected",
|
|
87
|
+
"provider_failed",
|
|
88
|
+
"provider_invalid",
|
|
89
|
+
"provider_not_connected",
|
|
90
|
+
"provider_rate_limited",
|
|
91
|
+
"provider_rejected",
|
|
92
|
+
"provider_required",
|
|
93
|
+
"provider_timeout",
|
|
94
|
+
"provider_transport_failed",
|
|
95
|
+
"provider_unavailable",
|
|
96
|
+
"public_tool_budget_limited",
|
|
97
|
+
"public_tool_budget_unavailable",
|
|
98
|
+
"runtime_capability_unsupported",
|
|
99
|
+
"runtime_unavailable",
|
|
100
|
+
"tool_execution_failed",
|
|
101
|
+
"tool_timeout",
|
|
102
|
+
"tool_transport_failed",
|
|
103
|
+
"usage_limit_reached",
|
|
104
|
+
"usage_limit_unavailable",
|
|
105
|
+
]);
|
|
106
|
+
const RUNTIME_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
|
|
23
107
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
108
|
const httpRecord = (value, label) => {
|
|
25
109
|
if (!isRecord(value)) {
|
|
@@ -54,6 +138,64 @@ const optionalNullableNumber = (value) => value === null || (typeof value === "n
|
|
|
54
138
|
? value
|
|
55
139
|
: undefined;
|
|
56
140
|
const optionalBoolean = (value) => typeof value === "boolean" ? value : undefined;
|
|
141
|
+
const safeRecordValue = (record, field) => {
|
|
142
|
+
if (record === null)
|
|
143
|
+
return undefined;
|
|
144
|
+
try {
|
|
145
|
+
return record[field];
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const optionalApprovedFailureCode = (value) => {
|
|
152
|
+
if (value === null)
|
|
153
|
+
return null;
|
|
154
|
+
return typeof value === "string" && SAFE_RUNTIME_FAILURE_CODES.has(value)
|
|
155
|
+
? value
|
|
156
|
+
: undefined;
|
|
157
|
+
};
|
|
158
|
+
const optionalRuntimeUuid = (value) => {
|
|
159
|
+
if (typeof value !== "string")
|
|
160
|
+
return undefined;
|
|
161
|
+
const normalized = value.trim();
|
|
162
|
+
return RUNTIME_UUID.test(normalized) ? normalized : undefined;
|
|
163
|
+
};
|
|
164
|
+
const safeExecutionDiagnostics = (value) => {
|
|
165
|
+
if (!isRecord(value))
|
|
166
|
+
return undefined;
|
|
167
|
+
const childRuntimeRunId = optionalRuntimeUuid(safeRecordValue(value, "childRuntimeRunId"));
|
|
168
|
+
const platformCode = optionalApprovedFailureCode(safeRecordValue(value, "platformCode")) ??
|
|
169
|
+
optionalApprovedFailureCode(safeRecordValue(value, "code")) ?? undefined;
|
|
170
|
+
if (childRuntimeRunId === undefined && platformCode === undefined) {
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
...(childRuntimeRunId === undefined ? {} : { childRuntimeRunId }),
|
|
175
|
+
...(platformCode === undefined ? {} : { platformCode }),
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
const safeExecutionDiagnosticsFromHttp = (body) => {
|
|
179
|
+
const details = safeRecordValue(body, "details");
|
|
180
|
+
const nested = safeExecutionDiagnostics(safeRecordValue(isRecord(details) ? details : null, "diagnostics")) ?? safeExecutionDiagnostics(details);
|
|
181
|
+
const direct = safeExecutionDiagnostics(safeRecordValue(body, "diagnostics"));
|
|
182
|
+
if (nested === undefined)
|
|
183
|
+
return direct;
|
|
184
|
+
if (direct === undefined)
|
|
185
|
+
return nested;
|
|
186
|
+
return {
|
|
187
|
+
...(nested.childRuntimeRunId === undefined
|
|
188
|
+
? direct.childRuntimeRunId === undefined
|
|
189
|
+
? {}
|
|
190
|
+
: { childRuntimeRunId: direct.childRuntimeRunId }
|
|
191
|
+
: { childRuntimeRunId: nested.childRuntimeRunId }),
|
|
192
|
+
...(nested.platformCode === undefined
|
|
193
|
+
? direct.platformCode === undefined
|
|
194
|
+
? {}
|
|
195
|
+
: { platformCode: direct.platformCode }
|
|
196
|
+
: { platformCode: nested.platformCode }),
|
|
197
|
+
};
|
|
198
|
+
};
|
|
57
199
|
/** Map the owned camelCase HTTP envelope without copying unknown fields. */
|
|
58
200
|
export const appRuntimeInvocationResponseFromHttp = (value, decodeResult) => {
|
|
59
201
|
const label = "app runtime invocation";
|
|
@@ -140,6 +282,14 @@ export const appRuntimeExecutionStatusFromHttp = (value, decodeResult) => {
|
|
|
140
282
|
if (executorKind !== undefined) {
|
|
141
283
|
output.executorKind = executorKind;
|
|
142
284
|
}
|
|
285
|
+
const code = optionalApprovedFailureCode(body.code);
|
|
286
|
+
if (code !== undefined) {
|
|
287
|
+
output.code = code;
|
|
288
|
+
}
|
|
289
|
+
const diagnostics = safeExecutionDiagnosticsFromHttp(body);
|
|
290
|
+
if (diagnostics !== undefined) {
|
|
291
|
+
output.diagnostics = diagnostics;
|
|
292
|
+
}
|
|
143
293
|
return output;
|
|
144
294
|
};
|
|
145
295
|
const routeSegmentForCapabilityType = (capabilityType) => {
|
|
@@ -167,20 +317,13 @@ const encodeCapabilityPath = (capabilityType, capabilityName) => {
|
|
|
167
317
|
const makeRuntimeRequestId = () => `uru_app_${Math.random().toString(36).slice(2)}`;
|
|
168
318
|
const runtimeHttpRequestOptions = (options) => {
|
|
169
319
|
const transport = {};
|
|
320
|
+
validateRuntimeInvocationOptions(options);
|
|
170
321
|
const idempotencyKey = options?.idempotencyKey;
|
|
171
322
|
if (idempotencyKey !== undefined && idempotencyKey !== "") {
|
|
172
323
|
transport.idempotencyKey = idempotencyKey;
|
|
173
324
|
}
|
|
174
325
|
const timeoutMs = options?.timeoutMs;
|
|
175
326
|
if (timeoutMs !== undefined) {
|
|
176
|
-
if (!Number.isInteger(timeoutMs) ||
|
|
177
|
-
timeoutMs < 1000 ||
|
|
178
|
-
timeoutMs > 60_000) {
|
|
179
|
-
throw new UruAppRuntimeError("Invalid app runtime timeoutMs.", {
|
|
180
|
-
body: timeoutMs,
|
|
181
|
-
status: 0,
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
327
|
transport.timeoutMs = timeoutMs;
|
|
185
328
|
}
|
|
186
329
|
if (options?.forceRefresh === true) {
|
|
@@ -350,43 +493,6 @@ const invocationRequestBody = (transport, runtimeInput, effectiveForce, damper,
|
|
|
350
493
|
});
|
|
351
494
|
}
|
|
352
495
|
};
|
|
353
|
-
const responseBody = async (response) => await response.json().catch(() => null);
|
|
354
|
-
const fetchInvocation = async (args, attempt = 0) => {
|
|
355
|
-
// Read out of `args` first. `args.runtimeFetch(...)` is a METHOD call, so it
|
|
356
|
-
// would hand the fetch `args` as its receiver — the same class of mistake
|
|
357
|
-
// that broke the global one.
|
|
358
|
-
const { runtimeFetch } = args;
|
|
359
|
-
const response = await runtimeFetch(args.url, {
|
|
360
|
-
body: args.requestBody,
|
|
361
|
-
headers: args.headers,
|
|
362
|
-
method: "POST",
|
|
363
|
-
});
|
|
364
|
-
const body = await responseBody(response);
|
|
365
|
-
const shouldRetry = response.status === 429 && attempt < args.damper.config.maxRateLimitRetries;
|
|
366
|
-
if (!shouldRetry) {
|
|
367
|
-
return { body, response };
|
|
368
|
-
}
|
|
369
|
-
const delayMs = computeRateLimitBackoffMs({
|
|
370
|
-
attempt,
|
|
371
|
-
baseDelayMs: args.damper.config.rateLimitBaseDelayMs,
|
|
372
|
-
maxDelayMs: args.damper.config.rateLimitMaxDelayMs,
|
|
373
|
-
retryAfterSeconds: parseRetryAfterSeconds({
|
|
374
|
-
body,
|
|
375
|
-
header: response.headers.get("retry-after"),
|
|
376
|
-
}),
|
|
377
|
-
});
|
|
378
|
-
emitRuntimeEvent(args.invocationOptions, {
|
|
379
|
-
capabilityName: args.capabilityName,
|
|
380
|
-
capabilityType: args.capabilityType,
|
|
381
|
-
deploymentId: args.deploymentId,
|
|
382
|
-
requestId: args.requestId,
|
|
383
|
-
retryAfterSeconds: Math.ceil(delayMs / 1000),
|
|
384
|
-
state: "rate_limited",
|
|
385
|
-
type: "status",
|
|
386
|
-
});
|
|
387
|
-
await args.damper.config.sleep(delayMs);
|
|
388
|
-
return await fetchInvocation(args, attempt + 1);
|
|
389
|
-
};
|
|
390
496
|
const normalizedInvocationResponse = (runtimeResponse, fallbackIdentity, fallbackCapabilityType, state, diagnostics) => ({
|
|
391
497
|
capabilityName: runtimeResponse.capabilityName === ""
|
|
392
498
|
? fallbackIdentity.capabilityName
|
|
@@ -415,6 +521,9 @@ export const invokeAppRuntime = async (options, damper, capabilityType, name, in
|
|
|
415
521
|
const { capabilityName, deploymentId } = identity;
|
|
416
522
|
const runtimeInput = invocationInput(input);
|
|
417
523
|
const transport = runtimeHttpRequestOptions(invocationOptions);
|
|
524
|
+
if (invocationOptions?.signal?.aborted === true) {
|
|
525
|
+
throw abortReason(invocationOptions.signal);
|
|
526
|
+
}
|
|
418
527
|
// P2 damper: a requested forceRefresh is honored only if it clears the
|
|
419
528
|
// per-capability throttle; otherwise the call is sent WITHOUT force_refresh so
|
|
420
529
|
// it can be served from cache instead of hammering past it.
|
|
@@ -444,18 +553,19 @@ export const invokeAppRuntime = async (options, damper, capabilityType, name, in
|
|
|
444
553
|
// retries beyond the first send; the loop performs at most
|
|
445
554
|
// `maxRateLimitRetries` extra sends, honoring Retry-After / retry_after_seconds
|
|
446
555
|
// (capped) before falling through to the normal failure path.
|
|
447
|
-
const { body, response } = await fetchInvocation({
|
|
556
|
+
const { body, response } = await withRuntimeRequestSignal(invocationOptions, async (requestSignal) => await fetchInvocation({
|
|
448
557
|
capabilityName,
|
|
449
558
|
capabilityType,
|
|
450
559
|
damper,
|
|
451
560
|
deploymentId,
|
|
561
|
+
emit: event => emitRuntimeEvent(invocationOptions, event),
|
|
452
562
|
headers,
|
|
453
|
-
...(invocationOptions === undefined ? {} : { invocationOptions }),
|
|
454
563
|
requestBody,
|
|
455
564
|
requestId,
|
|
456
565
|
runtimeFetch,
|
|
566
|
+
...(requestSignal === undefined ? {} : { signal: requestSignal }),
|
|
457
567
|
url,
|
|
458
|
-
});
|
|
568
|
+
}));
|
|
459
569
|
if (!response.ok) {
|
|
460
570
|
const message = runtimeErrorMessage(body, `Uru app runtime call failed with HTTP ${response.status}`);
|
|
461
571
|
emitRuntimeEvent(invocationOptions, {
|
|
@@ -544,6 +654,10 @@ const normalizedExecutionStatus = (status, state) => ({
|
|
|
544
654
|
...(status.executorKind === undefined
|
|
545
655
|
? {}
|
|
546
656
|
: { executorKind: status.executorKind }),
|
|
657
|
+
...(status.code === undefined ? {} : { code: status.code }),
|
|
658
|
+
...(status.diagnostics === undefined
|
|
659
|
+
? {}
|
|
660
|
+
: { diagnostics: status.diagnostics }),
|
|
547
661
|
...(status.pending === undefined ? {} : { pending: status.pending }),
|
|
548
662
|
...(Object.hasOwn(status, "result") ? { result: status.result } : {}),
|
|
549
663
|
...(status.retryAfterSeconds === undefined
|
|
@@ -558,6 +672,8 @@ export const getAppRuntimeJobStatus = async (options, executionId, invocationOpt
|
|
|
558
672
|
const runtimeFetch = resolveRuntimeFetch(options);
|
|
559
673
|
const identity = jobIdentity(options, executionId);
|
|
560
674
|
const { deploymentId, executionId: normalizedExecutionId } = identity;
|
|
675
|
+
validateRuntimeInvocationOptions(invocationOptions);
|
|
676
|
+
invocationOptions?.signal?.throwIfAborted();
|
|
561
677
|
const requestId = invocationOptions?.requestId ?? makeRuntimeRequestId();
|
|
562
678
|
emitRuntimeEvent(invocationOptions, {
|
|
563
679
|
capabilityName: "",
|
|
@@ -570,8 +686,22 @@ export const getAppRuntimeJobStatus = async (options, executionId, invocationOpt
|
|
|
570
686
|
});
|
|
571
687
|
const url = jobExecutionUrl(options, identity);
|
|
572
688
|
const headers = new Headers(await resolveHeaders(options.headers));
|
|
573
|
-
const response = await
|
|
574
|
-
|
|
689
|
+
const { response, body } = await withRuntimeRequestSignal(invocationOptions, async (signal) => {
|
|
690
|
+
if (signal?.aborted === true) {
|
|
691
|
+
throw abortReason(signal);
|
|
692
|
+
}
|
|
693
|
+
const response = await awaitWithAbort(Promise.resolve().then(() => {
|
|
694
|
+
if (signal?.aborted === true) {
|
|
695
|
+
throw abortReason(signal);
|
|
696
|
+
}
|
|
697
|
+
return runtimeFetch(url, {
|
|
698
|
+
headers,
|
|
699
|
+
method: "GET",
|
|
700
|
+
...(signal === undefined ? {} : { signal }),
|
|
701
|
+
});
|
|
702
|
+
}), signal);
|
|
703
|
+
return { body: await readRuntimeResponseBody(response, signal), response };
|
|
704
|
+
});
|
|
575
705
|
if (!response.ok && response.status !== 202) {
|
|
576
706
|
const message = runtimeErrorMessage(body, `Uru app runtime job status failed with HTTP ${response.status}`);
|
|
577
707
|
emitRuntimeEvent(invocationOptions, {
|
|
@@ -615,11 +745,27 @@ export const cancelAppRuntimeJob = async (options, executionId, invocationOption
|
|
|
615
745
|
const runtimeFetch = resolveRuntimeFetch(options);
|
|
616
746
|
const identity = jobIdentity(options, executionId);
|
|
617
747
|
const { deploymentId, executionId: normalizedExecutionId } = identity;
|
|
748
|
+
validateRuntimeInvocationOptions(invocationOptions);
|
|
749
|
+
invocationOptions?.signal?.throwIfAborted();
|
|
618
750
|
const requestId = invocationOptions?.requestId ?? makeRuntimeRequestId();
|
|
619
751
|
const url = jobExecutionUrl(options, identity, true);
|
|
620
752
|
const headers = new Headers(await resolveHeaders(options.headers));
|
|
621
|
-
const response = await
|
|
622
|
-
|
|
753
|
+
const { response, body } = await withRuntimeRequestSignal(invocationOptions, async (signal) => {
|
|
754
|
+
if (signal?.aborted === true) {
|
|
755
|
+
throw abortReason(signal);
|
|
756
|
+
}
|
|
757
|
+
const response = await awaitWithAbort(Promise.resolve().then(() => {
|
|
758
|
+
if (signal?.aborted === true) {
|
|
759
|
+
throw abortReason(signal);
|
|
760
|
+
}
|
|
761
|
+
return runtimeFetch(url, {
|
|
762
|
+
headers,
|
|
763
|
+
method: "POST",
|
|
764
|
+
...(signal === undefined ? {} : { signal }),
|
|
765
|
+
});
|
|
766
|
+
}), signal);
|
|
767
|
+
return { body: await readRuntimeResponseBody(response, signal), response };
|
|
768
|
+
});
|
|
623
769
|
if (!response.ok) {
|
|
624
770
|
const message = runtimeErrorMessage(body, `Uru app runtime job cancel failed with HTTP ${response.status}`);
|
|
625
771
|
emitRuntimeEvent(invocationOptions, {
|
package/dist/types.d.ts
CHANGED
|
@@ -48,6 +48,8 @@ export interface UruAppRuntimeEvent<TResult = unknown> {
|
|
|
48
48
|
/** Per-call options accepted by every capability method. */
|
|
49
49
|
export interface UruAppRuntimeInvocationOptions {
|
|
50
50
|
idempotencyKey?: string;
|
|
51
|
+
/** Abort the network call, response-body read, or retry wait. */
|
|
52
|
+
signal?: AbortSignal;
|
|
51
53
|
timeoutMs?: number;
|
|
52
54
|
forceRefresh?: boolean;
|
|
53
55
|
requestId?: string;
|
|
@@ -72,6 +74,11 @@ export interface UruAppRuntimeInvocationResponse<TResult = unknown> {
|
|
|
72
74
|
pending: boolean;
|
|
73
75
|
diagnostics: RuntimeJsonObject;
|
|
74
76
|
}
|
|
77
|
+
/** Safe fields retained from a failed job's platform-owned diagnostics. */
|
|
78
|
+
export interface UruAppRuntimeExecutionDiagnostics {
|
|
79
|
+
childRuntimeRunId?: string;
|
|
80
|
+
platformCode?: string;
|
|
81
|
+
}
|
|
75
82
|
/** The normalized envelope a job-status poll resolves to. */
|
|
76
83
|
export interface UruAppRuntimeExecutionStatus<TResult = unknown> {
|
|
77
84
|
success?: boolean;
|
|
@@ -86,6 +93,10 @@ export interface UruAppRuntimeExecutionStatus<TResult = unknown> {
|
|
|
86
93
|
cacheStatus?: string | null;
|
|
87
94
|
executor?: string | null;
|
|
88
95
|
executorKind?: string | null;
|
|
96
|
+
/** A platform-owned failure code, when the server approved it. */
|
|
97
|
+
code?: string | null;
|
|
98
|
+
/** Safe child-run diagnostics; provider details are never copied here. */
|
|
99
|
+
diagnostics?: UruAppRuntimeExecutionDiagnostics;
|
|
89
100
|
}
|
|
90
101
|
/** Input is untrusted until the transport validates it as a JSON object. */
|
|
91
102
|
export interface UruCapabilityInvoker {
|