@vite-hub/runtime 0.0.4 → 0.3.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/README.md +278 -41
- package/dist/durability.d.ts +107 -0
- package/dist/durability.js +168 -0
- package/dist/error-diagnostics-BKICpwZp.js +85 -0
- package/dist/error-diagnostics-BsJr9VdW.d.ts +15 -0
- package/dist/errors-BEomN3fX.js +190 -0
- package/dist/execution-budget-BPXbISEt.d.ts +56 -0
- package/dist/index-Bcl-Hr-a.d.ts +678 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -1159
- package/dist/internal/drain.d.ts +17 -0
- package/dist/internal/drain.js +75 -0
- package/dist/internal/execution-budget.d.ts +10 -0
- package/dist/internal/execution-budget.js +17 -0
- package/dist/internal/grant.d.ts +36 -0
- package/dist/internal/grant.js +44 -0
- package/dist/internal/h3.d.ts +42 -0
- package/dist/internal/h3.js +181 -0
- package/dist/internal/node.d.ts +33 -0
- package/dist/{node.js → internal/node.js} +12 -9
- package/dist/internal/public-url.d.ts +2 -0
- package/dist/internal/public-url.js +2 -0
- package/dist/internal/run-scope.d.ts +11 -0
- package/dist/internal/run-scope.js +30 -0
- package/dist/internal/runtime-type.d.ts +26 -0
- package/dist/internal/runtime-type.js +3 -0
- package/dist/internal/shutdown.d.ts +84 -0
- package/dist/internal/shutdown.js +202 -0
- package/dist/internal/test-mode-setup.d.ts +7 -0
- package/dist/internal/test-mode-setup.js +114 -0
- package/dist/internal/test-mode.d.ts +32 -0
- package/dist/internal/test-mode.js +9 -0
- package/dist/operations.d.ts +167 -0
- package/dist/operations.js +406 -0
- package/dist/public-url-1WeOw54Z.d.ts +34 -0
- package/dist/public-url-B6x0HMgY.js +140 -0
- package/dist/request-context-CP9NhgLL.js +2274 -0
- package/dist/runtime-type-BftydMAf.js +88 -0
- package/package.json +92 -17
- package/dist/drain.d.ts +0 -1
- package/dist/drain.js +0 -52
- package/dist/index-tBKyUP1T.d.ts +0 -314
- package/dist/node.d.ts +0 -35
- package/dist/runtime-type-ovvOd6m5.js +0 -37
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { defineDiagnostics } from "nostics";
|
|
2
|
+
//#region src/durability.ts
|
|
3
|
+
/**
|
|
4
|
+
* Runs every reporter once, in order. A reporter that calls `context.find()` runs the reporter of that primitive first,
|
|
5
|
+
* so the order of the entries does not matter.
|
|
6
|
+
*/
|
|
7
|
+
function collectDurability(entries, env) {
|
|
8
|
+
const results = /* @__PURE__ */ new Map();
|
|
9
|
+
const running = /* @__PURE__ */ new Set();
|
|
10
|
+
const run = (entry) => {
|
|
11
|
+
const done = results.get(entry);
|
|
12
|
+
if (done) return done;
|
|
13
|
+
if (running.has(entry)) return [];
|
|
14
|
+
running.add(entry);
|
|
15
|
+
const reports = entry.report(context);
|
|
16
|
+
running.delete(entry);
|
|
17
|
+
results.set(entry, reports);
|
|
18
|
+
return reports;
|
|
19
|
+
};
|
|
20
|
+
const context = {
|
|
21
|
+
env,
|
|
22
|
+
find: (primitive, name = "default") => entries.filter((entry) => entry.primitive === primitive).flatMap(run).find((report) => (report.name ?? "default") === name)
|
|
23
|
+
};
|
|
24
|
+
return entries.flatMap((entry) => [...run(entry)]);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Where a SQLite or libSQL URL keeps its data: `:memory:` in this process, `libsql:`, `http(s):`, or `ws(s):` on a
|
|
28
|
+
* remote server, and anything else, such as `file:`, on this host.
|
|
29
|
+
*/
|
|
30
|
+
function classifySqliteUrl(url) {
|
|
31
|
+
if (url === ":memory:" || url === "file::memory:") return {
|
|
32
|
+
store: "sqlite",
|
|
33
|
+
scope: "process",
|
|
34
|
+
reason: "SQLite in the memory of this process."
|
|
35
|
+
};
|
|
36
|
+
if (url === "remote" || /^(?:libsql|https?|wss?):/i.test(url)) return {
|
|
37
|
+
store: "libsql",
|
|
38
|
+
scope: "shared",
|
|
39
|
+
reason: "A remote libSQL server."
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
store: "sqlite",
|
|
43
|
+
scope: "host",
|
|
44
|
+
reason: `The SQLite file ${url.replace(/^file:(?:\/\/)?/i, "")} on this host.`
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Hides a remote URL before the build writes it into the server: a `file:` or `:memory:` URL stays, and any other URL
|
|
49
|
+
* becomes `remote`, which `classifySqliteUrl()` reads as a remote server.
|
|
50
|
+
*/
|
|
51
|
+
function sqliteUrlInput(url) {
|
|
52
|
+
return /^file:/i.test(url) || url === ":memory:" ? url : "remote";
|
|
53
|
+
}
|
|
54
|
+
/** Reads a configuration value the same way the runtime does: the first set environment variable, then the default. */
|
|
55
|
+
function resolveDurabilityValue(value, env) {
|
|
56
|
+
if (typeof value !== "object") return value?.trim() || void 0;
|
|
57
|
+
for (const name of value.source?.names ?? (value.source?.name ? [value.source.name] : [])) {
|
|
58
|
+
const resolved = env[name]?.trim();
|
|
59
|
+
if (resolved) return resolved;
|
|
60
|
+
}
|
|
61
|
+
return typeof value.default === "string" ? value.default.trim() || void 0 : void 0;
|
|
62
|
+
}
|
|
63
|
+
const instancesEnvName = "VITEHUB_INSTANCES";
|
|
64
|
+
const durabilityDiagnostics = defineDiagnostics({
|
|
65
|
+
docsBase: "https://vitehub.dev/docs/development/diagnostics",
|
|
66
|
+
codes: {
|
|
67
|
+
RUNTIME_R0017: {
|
|
68
|
+
why: ({ instances, lines }) => [`[vitehub] ${instancesEnvName} is ${instances}, but these primitives keep their state in one process or on one host. Each instance would see different data:`, ...lines].join("\n"),
|
|
69
|
+
fix: `Move each listed primitive to a shared store, or set ${instancesEnvName}=1 and run one instance.`,
|
|
70
|
+
data: (params) => params
|
|
71
|
+
},
|
|
72
|
+
RUNTIME_R0018: {
|
|
73
|
+
why: ({ source, value }) => `${source} must be a positive integer. It is ${JSON.stringify(value)}.`,
|
|
74
|
+
fix: `Set ${instancesEnvName} or \`node.instances\` to the number of server instances that share this deployment, such as 1 or 3.`,
|
|
75
|
+
data: (params) => params
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
/**
|
|
80
|
+
* The number of server instances that run this build. `VITEHUB_INSTANCES` wins over `node.instances`, because a
|
|
81
|
+
* deployment often decides the count after the build. The default is 1.
|
|
82
|
+
*/
|
|
83
|
+
function readInstanceCount(env = process.env, configured) {
|
|
84
|
+
const value = env[instancesEnvName]?.trim();
|
|
85
|
+
if (value) {
|
|
86
|
+
if (!/^\d+$/.test(value) || Number(value) < 1) throw durabilityDiagnostics.RUNTIME_R0018({
|
|
87
|
+
source: instancesEnvName,
|
|
88
|
+
value
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
instances: Number(value),
|
|
92
|
+
instancesSource: "env"
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (configured === void 0) return {
|
|
96
|
+
instances: 1,
|
|
97
|
+
instancesSource: "default"
|
|
98
|
+
};
|
|
99
|
+
if (!Number.isInteger(configured) || configured < 1) throw durabilityDiagnostics.RUNTIME_R0018({
|
|
100
|
+
source: "node.instances",
|
|
101
|
+
value: String(configured)
|
|
102
|
+
});
|
|
103
|
+
return {
|
|
104
|
+
instances: configured,
|
|
105
|
+
instancesSource: "option"
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function label(durability) {
|
|
109
|
+
return durability.name ? `${durability.primitive} (${durability.name})` : durability.primitive;
|
|
110
|
+
}
|
|
111
|
+
/** One line per primitive: name, store, scope, and reason, in aligned columns. */
|
|
112
|
+
function formatDurabilityTable(primitives) {
|
|
113
|
+
const rows = primitives.map((durability) => [
|
|
114
|
+
label(durability),
|
|
115
|
+
durability.store,
|
|
116
|
+
durability.scope,
|
|
117
|
+
durability.reason
|
|
118
|
+
]);
|
|
119
|
+
const widths = [
|
|
120
|
+
0,
|
|
121
|
+
1,
|
|
122
|
+
2
|
|
123
|
+
].map((column) => Math.max(...rows.map((row) => row[column].length)));
|
|
124
|
+
return rows.map((row) => ` ${row.map((cell, column) => column < 3 ? cell.padEnd(widths[column]) : cell).join(" ")}`).join("\n");
|
|
125
|
+
}
|
|
126
|
+
function instancesLabel(report) {
|
|
127
|
+
const count = `${report.instances} instance${report.instances === 1 ? "" : "s"}`;
|
|
128
|
+
if (report.instancesSource === "env") return `${count}, from ${instancesEnvName}`;
|
|
129
|
+
if (report.instancesSource === "option") return `${count}, from node.instances`;
|
|
130
|
+
return `${count}. Set ${instancesEnvName} when you run more`;
|
|
131
|
+
}
|
|
132
|
+
/** The primitives that would split their state across `instances` server instances. */
|
|
133
|
+
function durabilityProblems(primitives, instances) {
|
|
134
|
+
return instances > 1 ? primitives.filter((durability) => durability.scope !== "shared") : [];
|
|
135
|
+
}
|
|
136
|
+
/** Builds the error that a server with several instances and a `host` or `process` store fails with. */
|
|
137
|
+
function durabilityError(report) {
|
|
138
|
+
return durabilityDiagnostics.RUNTIME_R0017({
|
|
139
|
+
instances: report.instances,
|
|
140
|
+
lines: report.problems.map((durability) => ` - ${label(durability)}: ${durability.store} (${durability.scope}). ${durability.reason}${durability.fix ? ` Fix: ${durability.fix}` : ""}`),
|
|
141
|
+
primitives: report.problems.map(label)
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
const reportKey = Symbol.for("vitehub.durability.report");
|
|
145
|
+
const scope = globalThis;
|
|
146
|
+
/** The report that the server recorded at boot, or `undefined` before the boot check runs or outside a Node server. */
|
|
147
|
+
function getDurabilityReport() {
|
|
148
|
+
return scope[reportKey];
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Records the durability of each primitive, prints one line per primitive, and fails when several instances would
|
|
152
|
+
* split state. A Node server runs it once at boot, from a Nitro plugin that ViteHub generates.
|
|
153
|
+
*/
|
|
154
|
+
function checkDurability(primitives, options = {}) {
|
|
155
|
+
const count = readInstanceCount(options.env ?? process.env, options.instances);
|
|
156
|
+
const report = {
|
|
157
|
+
...count,
|
|
158
|
+
primitives: [...primitives],
|
|
159
|
+
problems: durabilityProblems(primitives, count.instances)
|
|
160
|
+
};
|
|
161
|
+
scope[reportKey] = report;
|
|
162
|
+
const log = options.log === void 0 ? (message) => console.info(message) : options.log;
|
|
163
|
+
if (log && primitives.length) log(`[vitehub] Durability (${instancesLabel(report)}):\n${formatDurabilityTable(primitives)}`);
|
|
164
|
+
if (options.enforce !== false && report.problems.length) throw durabilityError(report);
|
|
165
|
+
return report;
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
export { checkDurability, classifySqliteUrl, collectDurability, durabilityError, durabilityProblems, formatDurabilityTable, getDurabilityReport, instancesEnvName, readInstanceCount, resolveDurabilityValue, sqliteUrlInput };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { defineDiagnostics } from "nostics";
|
|
2
|
+
//#region src/error-diagnostics.ts
|
|
3
|
+
const dynamicError = { why: ({ message }) => message === void 0 ? "" : String(message) };
|
|
4
|
+
const codes = {
|
|
5
|
+
RUNTIME_R0001: {
|
|
6
|
+
...dynamicError,
|
|
7
|
+
fix: "Pass `maxDepth` to `normalizeRuntimeDiagnosticError()` as an integer of 0 or more, or remove it to use the default."
|
|
8
|
+
},
|
|
9
|
+
RUNTIME_R0002: {
|
|
10
|
+
...dynamicError,
|
|
11
|
+
fix: "Pass `maxErrors` to `normalizeRuntimeDiagnosticError()` as an integer of 1 or more, or remove it to use the default."
|
|
12
|
+
},
|
|
13
|
+
RUNTIME_R0003: {
|
|
14
|
+
...dynamicError,
|
|
15
|
+
fix: "Pass `maxStringLength` to `normalizeRuntimeDiagnosticError()` as an integer of 64 or more, or remove it to use the default."
|
|
16
|
+
},
|
|
17
|
+
RUNTIME_R0004: {
|
|
18
|
+
...dynamicError,
|
|
19
|
+
fix: "Read the logs of the process that `vitehub drain` waited for to find why its drain failed, then fix that failure and run `vitehub drain` again."
|
|
20
|
+
},
|
|
21
|
+
RUNTIME_R0005: {
|
|
22
|
+
...dynamicError,
|
|
23
|
+
fix: "Read the logs of the process that the message names to find why it stopped, then start it again before you run `vitehub drain`."
|
|
24
|
+
},
|
|
25
|
+
RUNTIME_R0006: {
|
|
26
|
+
...dynamicError,
|
|
27
|
+
fix: "Pass `new ViteHubError()` a nonempty `code` and `message` string within the length limits, and plain JSON `details`."
|
|
28
|
+
},
|
|
29
|
+
RUNTIME_R0007: {
|
|
30
|
+
...dynamicError,
|
|
31
|
+
fix: "Give the execution authority a valid value for each of `credentials`, `environment`, `filesystem.access`, `filesystem.scope`, `isolation`, `network`, and `processes`."
|
|
32
|
+
},
|
|
33
|
+
RUNTIME_R0009: {
|
|
34
|
+
...dynamicError,
|
|
35
|
+
fix: "Set `intervalMs` of the process reconciler to a number of milliseconds from 1 to 2147483647."
|
|
36
|
+
},
|
|
37
|
+
RUNTIME_R0010: {
|
|
38
|
+
...dynamicError,
|
|
39
|
+
fix: "Give the process reconciler a `signal` other than `SIGKILL` or `SIGSTOP`, or set `signal: false`."
|
|
40
|
+
},
|
|
41
|
+
RUNTIME_R0011: {
|
|
42
|
+
...dynamicError,
|
|
43
|
+
fix: "Call `drain()` of the process reconciler after its callback returns, not from inside the callback."
|
|
44
|
+
},
|
|
45
|
+
RUNTIME_R0015: {
|
|
46
|
+
...dynamicError,
|
|
47
|
+
fix: "Pass the `Request` of a Fetchable handler, or the event of an H3 or Nitro route, to `getStandardContext()`."
|
|
48
|
+
},
|
|
49
|
+
RUNTIME_R0016: {
|
|
50
|
+
...dynamicError,
|
|
51
|
+
fix: "Return the body or a `Response` from the handler, in place of setting `~standard.res.body`."
|
|
52
|
+
},
|
|
53
|
+
RUNTIME_R0017: dynamicError,
|
|
54
|
+
RUNTIME_R0018: dynamicError,
|
|
55
|
+
RUNTIME_CONSOLE_PATH_INVALID: {
|
|
56
|
+
why: ({ value }) => `${value} is not a Console path. Use a path that starts with one /, such as /agents/<agent>.`,
|
|
57
|
+
fix: "Pass `hostedConsoleUrl()` a Console path that starts with one `/`, such as `/kv` or `/agents/<agent>/invocations/<id>`."
|
|
58
|
+
},
|
|
59
|
+
RUNTIME_CONSOLE_PROJECT_URL_INVALID: {
|
|
60
|
+
why: ({ value }) => `${value} is not a project URL. Use the https:// origin of the app, with its base path if it has one.`,
|
|
61
|
+
fix: "Pass `hostedConsoleUrl()` the `https://` origin of the app, or `http://localhost` in development, with its base path and without a user name or password."
|
|
62
|
+
},
|
|
63
|
+
RUNTIME_EXECUTION_BUDGET_AMOUNT_INVALID: {
|
|
64
|
+
why: ({ counter }) => `[vitehub] Execution budget ${counter} amount must be a finite non-negative number.`,
|
|
65
|
+
fix: "Pass `consume()` and the `record*()` methods of the execution budget a finite number of 0 or more."
|
|
66
|
+
},
|
|
67
|
+
RUNTIME_EXECUTION_BUDGET_EXCEEDED: {
|
|
68
|
+
why: ({ counter }) => `[vitehub] Execution budget exhausted for ${counter}.`,
|
|
69
|
+
fix: "Raise the `executionBudget` limit that `error.counter` names, such as `maxToolCalls`, `maxRetries`, `maxInputBytes`, or `maxOutputBytes`, or give the run less work."
|
|
70
|
+
},
|
|
71
|
+
RUNTIME_EXECUTION_BUDGET_LIMIT_INVALID: {
|
|
72
|
+
why: () => "[vitehub] Execution budget limits must be finite non-negative numbers.",
|
|
73
|
+
fix: "Set each limit of `executionBudget`, such as `maxToolCalls` or `maxOutputBytes`, to a finite number of 0 or more, or remove it."
|
|
74
|
+
},
|
|
75
|
+
RUNTIME_SERIALIZED_RESPONSE_INVALID: {
|
|
76
|
+
why: () => "Invalid serialized Response",
|
|
77
|
+
fix: "Pass `deserializeResponse()` the record that `serializeResponse()` returned, unchanged. Check it first with `isSerializedResponse()`."
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const runtimeErrorDiagnostics = /*#__PURE__*/ defineDiagnostics({
|
|
81
|
+
docsBase: "https://vitehub.dev/docs/development/diagnostics",
|
|
82
|
+
codes
|
|
83
|
+
});
|
|
84
|
+
//#endregion
|
|
85
|
+
export { runtimeErrorDiagnostics as t };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Diagnostics } from "nostics";
|
|
2
|
+
//#region src/error-diagnostics.d.ts
|
|
3
|
+
interface RuntimeCodeDefinition {
|
|
4
|
+
why: (params: {
|
|
5
|
+
counter?: string;
|
|
6
|
+
message?: unknown;
|
|
7
|
+
value?: string;
|
|
8
|
+
}) => string;
|
|
9
|
+
fix?: string;
|
|
10
|
+
}
|
|
11
|
+
type RuntimeErrorCode = "RUNTIME_R0001" | "RUNTIME_R0002" | "RUNTIME_R0003" | "RUNTIME_R0004" | "RUNTIME_R0005" | "RUNTIME_R0006" | "RUNTIME_R0007" | "RUNTIME_R0009" | "RUNTIME_R0010" | "RUNTIME_R0011" | "RUNTIME_R0015" | "RUNTIME_R0016" | "RUNTIME_R0017" | "RUNTIME_R0018" | "RUNTIME_CONSOLE_PATH_INVALID" | "RUNTIME_CONSOLE_PROJECT_URL_INVALID" | "RUNTIME_EXECUTION_BUDGET_AMOUNT_INVALID" | "RUNTIME_EXECUTION_BUDGET_EXCEEDED" | "RUNTIME_EXECUTION_BUDGET_LIMIT_INVALID" | "RUNTIME_SERIALIZED_RESPONSE_INVALID";
|
|
12
|
+
declare const codes: Record<RuntimeErrorCode, RuntimeCodeDefinition>;
|
|
13
|
+
declare const runtimeErrorDiagnostics: Diagnostics<typeof codes, []>;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { runtimeErrorDiagnostics as t };
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { t as runtimeErrorDiagnostics } from "./error-diagnostics-BKICpwZp.js";
|
|
2
|
+
//#region src/errors.ts
|
|
3
|
+
const MAX_DETAIL_DEPTH = 8;
|
|
4
|
+
const MAX_DETAIL_ENTRIES = 128;
|
|
5
|
+
const MAX_DETAIL_KEY_LENGTH = 128;
|
|
6
|
+
const MAX_DETAIL_NODES = 1024;
|
|
7
|
+
const MAX_DETAIL_STRING_LENGTH = 16384;
|
|
8
|
+
const MAX_ERROR_CODE_LENGTH = 128;
|
|
9
|
+
const MAX_ERROR_MESSAGE_LENGTH = 16384;
|
|
10
|
+
const MAX_REQUEST_ID_LENGTH = 256;
|
|
11
|
+
const publicShapes = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
function invalidPublicError() {
|
|
13
|
+
throw runtimeErrorDiagnostics.RUNTIME_R0006({ message: "[vitehub] ViteHubError requires a valid public error contract." });
|
|
14
|
+
}
|
|
15
|
+
function isArray(value) {
|
|
16
|
+
try {
|
|
17
|
+
return Array.isArray(value);
|
|
18
|
+
} catch {
|
|
19
|
+
invalidPublicError();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function readOwnDataProperty(value, key, requireEnumerable = false) {
|
|
23
|
+
try {
|
|
24
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
25
|
+
if (descriptor === void 0) return void 0;
|
|
26
|
+
if (!("value" in descriptor) || requireEnumerable && !descriptor.enumerable) invalidPublicError();
|
|
27
|
+
return descriptor.value;
|
|
28
|
+
} catch {
|
|
29
|
+
invalidPublicError();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function clonePublicDetail(value, state, depth, allowUndefined) {
|
|
33
|
+
state.nodes += 1;
|
|
34
|
+
if (state.nodes > MAX_DETAIL_NODES) invalidPublicError();
|
|
35
|
+
if (value === void 0 && allowUndefined) return void 0;
|
|
36
|
+
if (value === null || typeof value === "boolean") return value;
|
|
37
|
+
if (typeof value === "string" && value.length <= MAX_DETAIL_STRING_LENGTH) return value;
|
|
38
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
39
|
+
if (typeof value !== "object" || depth > MAX_DETAIL_DEPTH || state.ancestors.has(value)) invalidPublicError();
|
|
40
|
+
let prototype;
|
|
41
|
+
let keys;
|
|
42
|
+
try {
|
|
43
|
+
prototype = Object.getPrototypeOf(value);
|
|
44
|
+
keys = Reflect.ownKeys(value);
|
|
45
|
+
} catch {
|
|
46
|
+
invalidPublicError();
|
|
47
|
+
}
|
|
48
|
+
state.ancestors.add(value);
|
|
49
|
+
try {
|
|
50
|
+
if (isArray(value)) {
|
|
51
|
+
const length = readOwnDataProperty(value, "length");
|
|
52
|
+
if (!Number.isSafeInteger(length) || length < 0 || length > MAX_DETAIL_ENTRIES || keys.length !== length + 1 || !keys.includes("length")) invalidPublicError();
|
|
53
|
+
const result = [];
|
|
54
|
+
for (let index = 0; index < length; index += 1) {
|
|
55
|
+
const key = String(index);
|
|
56
|
+
if (!keys.includes(key)) invalidPublicError();
|
|
57
|
+
result.push(clonePublicDetail(readOwnDataProperty(value, key, true), state, depth + 1, false));
|
|
58
|
+
}
|
|
59
|
+
return Object.freeze(result);
|
|
60
|
+
}
|
|
61
|
+
if (prototype !== Object.prototype && prototype !== null) invalidPublicError();
|
|
62
|
+
if (keys.length > MAX_DETAIL_ENTRIES || keys.some((key) => typeof key !== "string")) invalidPublicError();
|
|
63
|
+
const result = {};
|
|
64
|
+
for (const key of keys) {
|
|
65
|
+
if (key.length === 0 || key.length > MAX_DETAIL_KEY_LENGTH) invalidPublicError();
|
|
66
|
+
Object.defineProperty(result, key, {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
value: clonePublicDetail(readOwnDataProperty(value, key, true), state, depth + 1, true),
|
|
69
|
+
writable: true
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return Object.freeze(result);
|
|
73
|
+
} finally {
|
|
74
|
+
state.ancestors.delete(value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function clonePublicDetails(value) {
|
|
78
|
+
if (value === void 0) return void 0;
|
|
79
|
+
if (typeof value !== "object" || value === null || isArray(value)) invalidPublicError();
|
|
80
|
+
return clonePublicDetail(value, {
|
|
81
|
+
ancestors: /* @__PURE__ */ new Set(),
|
|
82
|
+
nodes: 0
|
|
83
|
+
}, 0, false);
|
|
84
|
+
}
|
|
85
|
+
function normalizePublicError(code, message, options) {
|
|
86
|
+
if (typeof code !== "string" || code.length === 0 || code.length > MAX_ERROR_CODE_LENGTH) invalidPublicError();
|
|
87
|
+
if (typeof message !== "string" || message.length === 0 || message.length > MAX_ERROR_MESSAGE_LENGTH) invalidPublicError();
|
|
88
|
+
if (typeof options !== "object" || options === null || isArray(options)) invalidPublicError();
|
|
89
|
+
const cause = readOwnDataProperty(options, "cause");
|
|
90
|
+
const details = clonePublicDetails(readOwnDataProperty(options, "details"));
|
|
91
|
+
const requestId = readOwnDataProperty(options, "requestId");
|
|
92
|
+
if (requestId !== void 0 && (typeof requestId !== "string" || requestId.length === 0 || requestId.length > MAX_REQUEST_ID_LENGTH)) invalidPublicError();
|
|
93
|
+
return {
|
|
94
|
+
cause,
|
|
95
|
+
code,
|
|
96
|
+
details,
|
|
97
|
+
message,
|
|
98
|
+
name: "ViteHubError",
|
|
99
|
+
requestId,
|
|
100
|
+
shape: Object.freeze({
|
|
101
|
+
code,
|
|
102
|
+
...details === void 0 ? {} : { details },
|
|
103
|
+
message,
|
|
104
|
+
name: "ViteHubError",
|
|
105
|
+
...requestId === void 0 ? {} : { requestId }
|
|
106
|
+
})
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
var ViteHubError = class extends Error {
|
|
110
|
+
code;
|
|
111
|
+
details;
|
|
112
|
+
requestId;
|
|
113
|
+
constructor(code, message, options = {}) {
|
|
114
|
+
const normalized = normalizePublicError(code, message, options);
|
|
115
|
+
super(normalized.message, normalized.cause === void 0 ? void 0 : { cause: normalized.cause });
|
|
116
|
+
this.name = "ViteHubError";
|
|
117
|
+
this.code = normalized.code;
|
|
118
|
+
this.details = normalized.details;
|
|
119
|
+
this.requestId = normalized.requestId;
|
|
120
|
+
const toJSON = this.toJSON.bind(this);
|
|
121
|
+
Object.defineProperties(this, {
|
|
122
|
+
code: {
|
|
123
|
+
configurable: true,
|
|
124
|
+
enumerable: true,
|
|
125
|
+
value: normalized.code,
|
|
126
|
+
writable: false
|
|
127
|
+
},
|
|
128
|
+
details: {
|
|
129
|
+
configurable: true,
|
|
130
|
+
enumerable: true,
|
|
131
|
+
value: normalized.details,
|
|
132
|
+
writable: false
|
|
133
|
+
},
|
|
134
|
+
message: {
|
|
135
|
+
configurable: true,
|
|
136
|
+
enumerable: false,
|
|
137
|
+
value: normalized.message,
|
|
138
|
+
writable: false
|
|
139
|
+
},
|
|
140
|
+
name: {
|
|
141
|
+
configurable: false,
|
|
142
|
+
enumerable: false,
|
|
143
|
+
value: normalized.name,
|
|
144
|
+
writable: false
|
|
145
|
+
},
|
|
146
|
+
requestId: {
|
|
147
|
+
configurable: true,
|
|
148
|
+
enumerable: true,
|
|
149
|
+
value: normalized.requestId,
|
|
150
|
+
writable: false
|
|
151
|
+
},
|
|
152
|
+
toJSON: {
|
|
153
|
+
configurable: false,
|
|
154
|
+
enumerable: false,
|
|
155
|
+
value: toJSON,
|
|
156
|
+
writable: false
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
publicShapes.set(this, normalized.shape);
|
|
160
|
+
}
|
|
161
|
+
toJSON() {
|
|
162
|
+
const existing = publicShapes.get(this);
|
|
163
|
+
if (existing) return existing;
|
|
164
|
+
const normalized = normalizePublicError(readOwnDataProperty(this, "code"), readOwnDataProperty(this, "message"), {
|
|
165
|
+
details: readOwnDataProperty(this, "details"),
|
|
166
|
+
requestId: readOwnDataProperty(this, "requestId")
|
|
167
|
+
});
|
|
168
|
+
publicShapes.set(this, normalized.shape);
|
|
169
|
+
return normalized.shape;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function getViteHubErrorShape(value) {
|
|
173
|
+
if (typeof value !== "object" || value === null) return;
|
|
174
|
+
try {
|
|
175
|
+
const existing = publicShapes.get(value);
|
|
176
|
+
if (existing) return existing;
|
|
177
|
+
if (readOwnDataProperty(value, "name") !== "ViteHubError") return;
|
|
178
|
+
return normalizePublicError(readOwnDataProperty(value, "code"), readOwnDataProperty(value, "message"), {
|
|
179
|
+
details: readOwnDataProperty(value, "details"),
|
|
180
|
+
requestId: readOwnDataProperty(value, "requestId")
|
|
181
|
+
}).shape;
|
|
182
|
+
} catch {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function isViteHubError(value) {
|
|
187
|
+
return getViteHubErrorShape(value) !== void 0;
|
|
188
|
+
}
|
|
189
|
+
//#endregion
|
|
190
|
+
export { getViteHubErrorShape as n, isViteHubError as r, ViteHubError as t };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Diagnostic } from "nostics";
|
|
2
|
+
//#region src/execution-budget.d.ts
|
|
3
|
+
/** Counters that an Invocation can spend while it runs. */
|
|
4
|
+
type ExecutionBudgetCounter = "inputBytes" | "outputBytes" | "retries" | "toolCalls";
|
|
5
|
+
interface ExecutionBudgetLimits {
|
|
6
|
+
deadlineAt?: number;
|
|
7
|
+
maxInputBytes?: number;
|
|
8
|
+
maxOutputBytes?: number;
|
|
9
|
+
maxRetries?: number;
|
|
10
|
+
maxToolCalls?: number;
|
|
11
|
+
}
|
|
12
|
+
interface ExecutionBudgetUsage {
|
|
13
|
+
inputBytes: number;
|
|
14
|
+
outputBytes: number;
|
|
15
|
+
retries: number;
|
|
16
|
+
toolCalls: number;
|
|
17
|
+
}
|
|
18
|
+
interface ExecutionBudgetSnapshot {
|
|
19
|
+
cancelled: boolean;
|
|
20
|
+
deadlineAt?: number;
|
|
21
|
+
limits: Readonly<ExecutionBudgetLimits>;
|
|
22
|
+
remaining: {
|
|
23
|
+
inputBytes?: number;
|
|
24
|
+
outputBytes?: number;
|
|
25
|
+
retries?: number;
|
|
26
|
+
toolCalls?: number;
|
|
27
|
+
timeMs?: number;
|
|
28
|
+
};
|
|
29
|
+
usage: Readonly<ExecutionBudgetUsage>;
|
|
30
|
+
}
|
|
31
|
+
interface ExecutionBudgetOptions extends ExecutionBudgetLimits {
|
|
32
|
+
signal?: AbortSignal;
|
|
33
|
+
}
|
|
34
|
+
/** `RUNTIME_EXECUTION_BUDGET_EXCEEDED`, with the counter and the limit that ran out. Agents read them. */
|
|
35
|
+
type ExecutionBudgetExceededError = Diagnostic & {
|
|
36
|
+
counter: ExecutionBudgetCounter;
|
|
37
|
+
limit?: number;
|
|
38
|
+
};
|
|
39
|
+
/** Whether `error` is the error of an execution budget that ran out. */
|
|
40
|
+
declare function isExecutionBudgetExceededError(error: unknown): error is ExecutionBudgetExceededError;
|
|
41
|
+
interface ExecutionBudget {
|
|
42
|
+
readonly signal: AbortSignal;
|
|
43
|
+
cancel(reason?: unknown): void;
|
|
44
|
+
child(options?: ExecutionBudgetOptions): ExecutionBudget;
|
|
45
|
+
consume(counter: ExecutionBudgetCounter, amount?: number): void;
|
|
46
|
+
/** Release timers and abort listeners owned by this budget and its children. */
|
|
47
|
+
dispose(): void;
|
|
48
|
+
recordInputBytes(amount: number): void;
|
|
49
|
+
recordOutputBytes(amount: number): void;
|
|
50
|
+
recordRetry(): void;
|
|
51
|
+
recordToolCall(inputBytes?: number): void;
|
|
52
|
+
snapshot(): ExecutionBudgetSnapshot;
|
|
53
|
+
}
|
|
54
|
+
declare function createExecutionBudget(options?: ExecutionBudgetOptions): ExecutionBudget;
|
|
55
|
+
//#endregion
|
|
56
|
+
export { ExecutionBudgetOptions as a, createExecutionBudget as c, ExecutionBudgetLimits as i, isExecutionBudgetExceededError as l, ExecutionBudgetCounter as n, ExecutionBudgetSnapshot as o, ExecutionBudgetExceededError as r, ExecutionBudgetUsage as s, ExecutionBudget as t };
|