@vite-hub/runtime 0.4.0 → 0.5.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/dist/core-BfUEli_0.d.ts +304 -0
- package/dist/core-DpLK9dQt.js +1105 -0
- package/dist/durability.d.ts +2 -2
- package/dist/durability.js +4 -1
- package/dist/hosted-console-ZfMo5MW4.d.ts +30 -0
- package/dist/index-DsisbCNa.d.ts +364 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +5 -4
- package/dist/internal/console.d.ts +7 -0
- package/dist/internal/console.js +2 -0
- package/dist/internal/context.d.ts +2 -0
- package/dist/internal/context.js +2 -0
- package/dist/internal/error-diagnostics.d.ts +2 -0
- package/dist/internal/error-diagnostics.js +2 -0
- package/dist/internal/execution-budget.d.ts +3 -2
- package/dist/internal/execution-budget.js +226 -1
- package/dist/internal/grant.js +1 -1
- package/dist/internal/h3.d.ts +2 -2
- package/dist/internal/h3.js +1 -7
- package/dist/internal/node.d.ts +1 -1
- package/dist/internal/public-url.d.ts +2 -2
- package/dist/internal/public-url.js +2 -2
- package/dist/internal/response.d.ts +2 -0
- package/dist/internal/response.js +2 -0
- package/dist/internal/run-failed.js +0 -1
- package/dist/internal/runtime-type.js +1 -1
- package/dist/internal/shutdown.js +17 -19
- package/dist/internal/test-mode-setup.js +0 -4
- package/dist/internal/trace.d.ts +6 -0
- package/dist/internal/trace.js +2 -0
- package/dist/operations.d.ts +8 -2
- package/dist/operations.js +6 -2
- package/dist/{public-url-1WeOw54Z.d.ts → public-url-BCFhTiTM.d.ts} +6 -1
- package/dist/{public-url-ByAmAjbP.js → public-url-DizC8CFs.js} +25 -17
- package/dist/request-context-Dmsliqan.js +687 -0
- package/dist/response-Be5M4WIn.js +432 -0
- package/dist/response-iONbTI0E.d.ts +33 -0
- package/dist/{runtime-type-BftydMAf.js → runtime-type-CHYQYwDx.js} +0 -7
- package/package.json +26 -1
- package/dist/index-C1DoYfCf.d.ts +0 -733
- package/dist/request-context-uRQpodgl.js +0 -2433
|
@@ -0,0 +1,1105 @@
|
|
|
1
|
+
import { t as runtimeErrorDiagnostics } from "./error-diagnostics-BM9W4pBg.js";
|
|
2
|
+
import { r as isRuntimeObject } from "./runtime-type-CHYQYwDx.js";
|
|
3
|
+
import { t as ViteHubError } from "./errors-BQ0ofIL4.js";
|
|
4
|
+
//#region src/internal/trace-attributes.ts
|
|
5
|
+
const contentAttributeKeys = /* @__PURE__ */ new Set([
|
|
6
|
+
"args",
|
|
7
|
+
"body",
|
|
8
|
+
"content",
|
|
9
|
+
"data",
|
|
10
|
+
"input",
|
|
11
|
+
"message",
|
|
12
|
+
"messages",
|
|
13
|
+
"output",
|
|
14
|
+
"payload",
|
|
15
|
+
"progress",
|
|
16
|
+
"prompt",
|
|
17
|
+
"raw",
|
|
18
|
+
"request",
|
|
19
|
+
"response",
|
|
20
|
+
"result",
|
|
21
|
+
"text",
|
|
22
|
+
"title"
|
|
23
|
+
]);
|
|
24
|
+
const canonicalAttributeKeys = /* @__PURE__ */ new Set([
|
|
25
|
+
"vitehub.activity.owner",
|
|
26
|
+
"vitehub.activity.phase",
|
|
27
|
+
"vitehub.payload.summary",
|
|
28
|
+
"vitehub.payload.value",
|
|
29
|
+
"vitehub.payload.visibility"
|
|
30
|
+
]);
|
|
31
|
+
function isTraceContentAttributeKey(key) {
|
|
32
|
+
if (key === "error.message") return false;
|
|
33
|
+
if (contentAttributeKeys.has(key)) return true;
|
|
34
|
+
return key.split(".").some((part, index) => index > 0 && contentAttributeKeys.has(part));
|
|
35
|
+
}
|
|
36
|
+
function ownProperties(value) {
|
|
37
|
+
if (!isRuntimeObject(value)) return [];
|
|
38
|
+
return Reflect.ownKeys(value).flatMap((key) => {
|
|
39
|
+
if (typeof key !== "string") return [];
|
|
40
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
41
|
+
return descriptor?.enumerable ? [[key, descriptor]] : [];
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function mapArray(value, map) {
|
|
45
|
+
const length = Object.getOwnPropertyDescriptor(value, "length").value;
|
|
46
|
+
const next = [];
|
|
47
|
+
next.length = length;
|
|
48
|
+
for (let index = 0; index < length; index += 1) {
|
|
49
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, index);
|
|
50
|
+
if (descriptor && "value" in descriptor) next[index] = map(descriptor.value);
|
|
51
|
+
}
|
|
52
|
+
return next;
|
|
53
|
+
}
|
|
54
|
+
function metadataValue(value, seen) {
|
|
55
|
+
if (!value || typeof value !== "object") return value;
|
|
56
|
+
if (seen.has(value)) return "[Circular]";
|
|
57
|
+
seen.add(value);
|
|
58
|
+
try {
|
|
59
|
+
if (Array.isArray(value)) return mapArray(value, (child) => metadataValue(child, seen));
|
|
60
|
+
return projectProperties(ownProperties(value), "metadata", seen);
|
|
61
|
+
} catch {
|
|
62
|
+
return;
|
|
63
|
+
} finally {
|
|
64
|
+
seen.delete(value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function projectProperties(properties, content, seen) {
|
|
68
|
+
const omitted = [];
|
|
69
|
+
const next = Object.fromEntries(properties.flatMap(([key, descriptor]) => {
|
|
70
|
+
if (content === "metadata" && isTraceContentAttributeKey(key)) {
|
|
71
|
+
omitted.push(key);
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
if (!("value" in descriptor)) return [];
|
|
75
|
+
return [[key, content === "metadata" ? metadataValue(descriptor.value, seen) : descriptor.value]];
|
|
76
|
+
}));
|
|
77
|
+
if (omitted.length) next["content.omitted"] = omitted;
|
|
78
|
+
return next;
|
|
79
|
+
}
|
|
80
|
+
/** Project ordinary attributes; canonical activity and payload fields belong to the event. */
|
|
81
|
+
function normalizeTraceAttributes(attributes, content) {
|
|
82
|
+
let properties;
|
|
83
|
+
try {
|
|
84
|
+
properties = ownProperties(attributes || {}).filter(([key]) => !canonicalAttributeKeys.has(key));
|
|
85
|
+
} catch {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const source = projectProperties(properties, "content", /* @__PURE__ */ new WeakSet());
|
|
89
|
+
try {
|
|
90
|
+
if (Array.isArray(source["content.omitted"])) {
|
|
91
|
+
const omitted = mapArray(source["content.omitted"], (value) => value).filter((key) => typeof key === "string" && !canonicalAttributeKeys.has(key));
|
|
92
|
+
if (omitted.length) source["content.omitted"] = omitted;
|
|
93
|
+
else delete source["content.omitted"];
|
|
94
|
+
}
|
|
95
|
+
} catch {
|
|
96
|
+
delete source["content.omitted"];
|
|
97
|
+
}
|
|
98
|
+
if (content === "metadata") {
|
|
99
|
+
const accessors = properties.filter(([key, descriptor]) => !("value" in descriptor) && isTraceContentAttributeKey(key));
|
|
100
|
+
if (accessors.length) {
|
|
101
|
+
const omitted = new Set(Array.isArray(source["content.omitted"]) ? source["content.omitted"] : []);
|
|
102
|
+
for (const [key] of accessors) omitted.add(key);
|
|
103
|
+
source["content.omitted"] = [...omitted];
|
|
104
|
+
}
|
|
105
|
+
const next = projectProperties(ownProperties(source), content, /* @__PURE__ */ new WeakSet());
|
|
106
|
+
return Object.keys(next).length ? next : void 0;
|
|
107
|
+
}
|
|
108
|
+
return Object.keys(source).length ? source : void 0;
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/core.ts
|
|
112
|
+
function captureWebAssemblyModule() {
|
|
113
|
+
const namespace = Object.getOwnPropertyDescriptor(globalThis, "WebAssembly")?.value;
|
|
114
|
+
const constructor = typeof namespace === "object" ? Object.getOwnPropertyDescriptor(namespace, "Module")?.value : void 0;
|
|
115
|
+
return {
|
|
116
|
+
constructor,
|
|
117
|
+
exports: typeof constructor === "function" ? Object.getOwnPropertyDescriptor(constructor, "exports")?.value : void 0
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const webAssemblyModule = /* @__PURE__ */ captureWebAssemblyModule();
|
|
121
|
+
const noExecutionAuthority = /* @__PURE__ */ Object.freeze({
|
|
122
|
+
credentials: "none",
|
|
123
|
+
environment: "none",
|
|
124
|
+
filesystem: /* @__PURE__ */ Object.freeze({
|
|
125
|
+
access: "none",
|
|
126
|
+
scope: "none"
|
|
127
|
+
}),
|
|
128
|
+
isolation: "none",
|
|
129
|
+
network: "none",
|
|
130
|
+
processes: "none"
|
|
131
|
+
});
|
|
132
|
+
const unknownExecutionAuthority = /* @__PURE__ */ Object.freeze({
|
|
133
|
+
credentials: "unknown",
|
|
134
|
+
environment: "unknown",
|
|
135
|
+
filesystem: /* @__PURE__ */ Object.freeze({
|
|
136
|
+
access: "unknown",
|
|
137
|
+
scope: "unknown"
|
|
138
|
+
}),
|
|
139
|
+
isolation: "unknown",
|
|
140
|
+
network: "unknown",
|
|
141
|
+
processes: "unknown"
|
|
142
|
+
});
|
|
143
|
+
function isExecutionAuthority(value) {
|
|
144
|
+
return inspectExecutionAuthority(value) !== void 0;
|
|
145
|
+
}
|
|
146
|
+
function inspectExecutionAuthority(value) {
|
|
147
|
+
try {
|
|
148
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
|
|
149
|
+
const authority = value;
|
|
150
|
+
const filesystem = authority.filesystem;
|
|
151
|
+
if (!filesystem || typeof filesystem !== "object" || Array.isArray(filesystem)) return void 0;
|
|
152
|
+
const files = filesystem;
|
|
153
|
+
const credentials = authority.credentials;
|
|
154
|
+
const environment = authority.environment;
|
|
155
|
+
const access = files.access;
|
|
156
|
+
const scope = files.scope;
|
|
157
|
+
const isolation = authority.isolation;
|
|
158
|
+
const network = authority.network;
|
|
159
|
+
const processes = authority.processes;
|
|
160
|
+
if (!(typeof credentials === "string" && [
|
|
161
|
+
"ambient",
|
|
162
|
+
"none",
|
|
163
|
+
"provisioned",
|
|
164
|
+
"unknown"
|
|
165
|
+
].includes(credentials) && typeof environment === "string" && [
|
|
166
|
+
"ambient",
|
|
167
|
+
"none",
|
|
168
|
+
"selected",
|
|
169
|
+
"unknown"
|
|
170
|
+
].includes(environment) && typeof access === "string" && [
|
|
171
|
+
"none",
|
|
172
|
+
"read-only",
|
|
173
|
+
"read-write",
|
|
174
|
+
"unknown"
|
|
175
|
+
].includes(access) && typeof scope === "string" && [
|
|
176
|
+
"host",
|
|
177
|
+
"none",
|
|
178
|
+
"sandbox",
|
|
179
|
+
"unknown",
|
|
180
|
+
"workspace"
|
|
181
|
+
].includes(scope) && typeof isolation === "string" && [
|
|
182
|
+
"container",
|
|
183
|
+
"microvm",
|
|
184
|
+
"none",
|
|
185
|
+
"process",
|
|
186
|
+
"unknown"
|
|
187
|
+
].includes(isolation) && typeof network === "string" && [
|
|
188
|
+
"none",
|
|
189
|
+
"restricted",
|
|
190
|
+
"unrestricted",
|
|
191
|
+
"unknown"
|
|
192
|
+
].includes(network) && typeof processes === "string" && [
|
|
193
|
+
"arbitrary",
|
|
194
|
+
"none",
|
|
195
|
+
"restricted",
|
|
196
|
+
"unknown"
|
|
197
|
+
].includes(processes))) return void 0;
|
|
198
|
+
return {
|
|
199
|
+
authority: {
|
|
200
|
+
credentials,
|
|
201
|
+
environment,
|
|
202
|
+
filesystem: {
|
|
203
|
+
access,
|
|
204
|
+
scope
|
|
205
|
+
},
|
|
206
|
+
isolation,
|
|
207
|
+
network,
|
|
208
|
+
processes
|
|
209
|
+
},
|
|
210
|
+
filesystem
|
|
211
|
+
};
|
|
212
|
+
} catch {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const canonicalExecutionAuthorities = /* @__PURE__ */ new WeakSet([noExecutionAuthority, unknownExecutionAuthority]);
|
|
217
|
+
function normalizeExecutionAuthority(value) {
|
|
218
|
+
if (isRuntimeObject(value) && canonicalExecutionAuthorities.has(value)) return value;
|
|
219
|
+
const inspected = inspectExecutionAuthority(value);
|
|
220
|
+
if (!inspected) throw runtimeErrorDiagnostics.RUNTIME_R0007({ message: "[vitehub] Invalid execution authority descriptor." });
|
|
221
|
+
const authority = Object.freeze({
|
|
222
|
+
...inspected.authority,
|
|
223
|
+
filesystem: Object.freeze({ ...inspected.authority.filesystem })
|
|
224
|
+
});
|
|
225
|
+
canonicalExecutionAuthorities.add(authority);
|
|
226
|
+
return authority;
|
|
227
|
+
}
|
|
228
|
+
function timestamp(value) {
|
|
229
|
+
if (value instanceof Date) return value.toISOString();
|
|
230
|
+
if (typeof value === "string") return value;
|
|
231
|
+
if (isRuntimeObject(value)) try {
|
|
232
|
+
return Reflect.apply(Date.prototype.toISOString, value, []);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (!(error instanceof TypeError)) throw error;
|
|
235
|
+
}
|
|
236
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
237
|
+
}
|
|
238
|
+
function normalizedTraceActivity(activity) {
|
|
239
|
+
if (!activity || typeof activity !== "object") return;
|
|
240
|
+
try {
|
|
241
|
+
if (Array.isArray(activity)) return;
|
|
242
|
+
const owner = Object.getOwnPropertyDescriptor(activity, "owner");
|
|
243
|
+
const phase = Object.getOwnPropertyDescriptor(activity, "phase");
|
|
244
|
+
if (!owner || !("value" in owner) || !phase || !("value" in phase)) return;
|
|
245
|
+
if (owner.value !== "agent" && owner.value !== "vitehub") return;
|
|
246
|
+
if (![
|
|
247
|
+
"delivery",
|
|
248
|
+
"execution",
|
|
249
|
+
"setup",
|
|
250
|
+
"teardown"
|
|
251
|
+
].includes(phase.value)) return;
|
|
252
|
+
return {
|
|
253
|
+
owner: owner.value,
|
|
254
|
+
phase: phase.value
|
|
255
|
+
};
|
|
256
|
+
} catch {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function reflectedMapEntries(value) {
|
|
261
|
+
if (!value || typeof value !== "object") return;
|
|
262
|
+
try {
|
|
263
|
+
return Array.from(Map.prototype.entries.call(value));
|
|
264
|
+
} catch {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function reflectedSetValues(value) {
|
|
269
|
+
if (!value || typeof value !== "object") return;
|
|
270
|
+
try {
|
|
271
|
+
return Array.from(Set.prototype.values.call(value));
|
|
272
|
+
} catch {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function isCryptoKey(value) {
|
|
277
|
+
if (!value || typeof value !== "object") return false;
|
|
278
|
+
const CryptoKeyConstructor = Object.getOwnPropertyDescriptor(globalThis, "CryptoKey")?.value;
|
|
279
|
+
return typeof CryptoKeyConstructor === "function" && value instanceof CryptoKeyConstructor;
|
|
280
|
+
}
|
|
281
|
+
function isWebAssemblyModule(value) {
|
|
282
|
+
if (!value || typeof value !== "object") return false;
|
|
283
|
+
const { constructor, exports } = webAssemblyModule;
|
|
284
|
+
if (typeof constructor !== "function") return false;
|
|
285
|
+
if (value instanceof constructor) return true;
|
|
286
|
+
try {
|
|
287
|
+
if (typeof exports !== "function") return false;
|
|
288
|
+
Reflect.apply(exports, constructor, [value]);
|
|
289
|
+
return true;
|
|
290
|
+
} catch {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function captureFunctionToString() {
|
|
295
|
+
return Function.prototype.toString;
|
|
296
|
+
}
|
|
297
|
+
const intrinsicFunctionToString = /* @__PURE__ */ captureFunctionToString();
|
|
298
|
+
function checkNativeFunctionToString() {
|
|
299
|
+
try {
|
|
300
|
+
if (Object.prototype.hasOwnProperty.call(intrinsicFunctionToString, "prototype")) return false;
|
|
301
|
+
if (intrinsicFunctionToString.name !== "toString" || intrinsicFunctionToString.length !== 1) return false;
|
|
302
|
+
if (intrinsicFunctionToString.call(intrinsicFunctionToString).replace(/\s+/g, " ") !== "function toString() { [native code] }") return false;
|
|
303
|
+
function probe() {
|
|
304
|
+
return 1;
|
|
305
|
+
}
|
|
306
|
+
if (intrinsicFunctionToString.call(probe) !== "function probe() { return 1 }") return false;
|
|
307
|
+
const namedProbes = [
|
|
308
|
+
function ArrayBuffer() {},
|
|
309
|
+
function SharedArrayBuffer() {},
|
|
310
|
+
function DataView() {},
|
|
311
|
+
function Memory() {}
|
|
312
|
+
];
|
|
313
|
+
for (const namedProbe of namedProbes) if (intrinsicFunctionToString.call(namedProbe).includes("[native code]")) return false;
|
|
314
|
+
const getterProbe = Object.getOwnPropertyDescriptor({ get byteLength() {
|
|
315
|
+
return 0;
|
|
316
|
+
} }, "byteLength")?.get;
|
|
317
|
+
if (!getterProbe || intrinsicFunctionToString.call(getterProbe).includes("[native code]")) return false;
|
|
318
|
+
try {
|
|
319
|
+
intrinsicFunctionToString.call(null);
|
|
320
|
+
return false;
|
|
321
|
+
} catch {
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
} catch {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const nativeFunctionToString = /* @__PURE__ */ checkNativeFunctionToString();
|
|
329
|
+
function isNativeBufferOperation(value, name) {
|
|
330
|
+
if (!nativeFunctionToString || typeof value !== "function") return false;
|
|
331
|
+
try {
|
|
332
|
+
return intrinsicFunctionToString.call(value).replace(/\s+/g, " ") === `function ${name}() { [native code] }`;
|
|
333
|
+
} catch {
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
function captureSharedBufferByteLength() {
|
|
338
|
+
try {
|
|
339
|
+
let sample;
|
|
340
|
+
const constructor = Object.getOwnPropertyDescriptor(globalThis, "SharedArrayBuffer")?.value;
|
|
341
|
+
if (isNativeBufferOperation(constructor, "SharedArrayBuffer")) sample = Reflect.construct(constructor, [0]);
|
|
342
|
+
else {
|
|
343
|
+
const namespace = Object.getOwnPropertyDescriptor(globalThis, "WebAssembly")?.value;
|
|
344
|
+
if (!isRuntimeObject(namespace)) return;
|
|
345
|
+
const memoryConstructor = Object.getOwnPropertyDescriptor(namespace, "Memory")?.value;
|
|
346
|
+
if (!isNativeBufferOperation(memoryConstructor, "Memory")) return;
|
|
347
|
+
const memory = Reflect.construct(memoryConstructor, [{
|
|
348
|
+
initial: 0,
|
|
349
|
+
maximum: 1,
|
|
350
|
+
shared: true
|
|
351
|
+
}]);
|
|
352
|
+
const memoryBuffer = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(memory), "buffer")?.get;
|
|
353
|
+
if (!isNativeBufferOperation(memoryBuffer, "get buffer")) return;
|
|
354
|
+
sample = memoryBuffer.call(memory);
|
|
355
|
+
}
|
|
356
|
+
if (!isRuntimeObject(sample)) return;
|
|
357
|
+
const getter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(sample), "byteLength")?.get;
|
|
358
|
+
if (!isNativeBufferOperation(getter, "get byteLength")) return;
|
|
359
|
+
if (getter.call(sample) !== 0) return;
|
|
360
|
+
const receiverProbes = [{}, Object.create(null)];
|
|
361
|
+
for (const receiver of receiverProbes) try {
|
|
362
|
+
getter.call(receiver);
|
|
363
|
+
return;
|
|
364
|
+
} catch {}
|
|
365
|
+
return getter;
|
|
366
|
+
} catch {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function captureOrdinaryBufferOperations() {
|
|
371
|
+
try {
|
|
372
|
+
const arrayBuffer = Object.getOwnPropertyDescriptor(globalThis, "ArrayBuffer")?.value;
|
|
373
|
+
const dataView = Object.getOwnPropertyDescriptor(globalThis, "DataView")?.value;
|
|
374
|
+
if (!isNativeBufferOperation(arrayBuffer, "ArrayBuffer") || !isNativeBufferOperation(dataView, "DataView")) return;
|
|
375
|
+
const sample = Reflect.construct(arrayBuffer, [0]);
|
|
376
|
+
const byteLength = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(sample), "byteLength")?.get;
|
|
377
|
+
if (!isNativeBufferOperation(byteLength, "get byteLength") || byteLength.call(sample) !== 0) return;
|
|
378
|
+
Reflect.construct(dataView, [
|
|
379
|
+
sample,
|
|
380
|
+
0,
|
|
381
|
+
0
|
|
382
|
+
]);
|
|
383
|
+
try {
|
|
384
|
+
Reflect.construct(dataView, [
|
|
385
|
+
{},
|
|
386
|
+
0,
|
|
387
|
+
0
|
|
388
|
+
]);
|
|
389
|
+
return;
|
|
390
|
+
} catch {
|
|
391
|
+
return {
|
|
392
|
+
byteLength,
|
|
393
|
+
dataView
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
} catch {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
const sharedBufferByteLength = /* @__PURE__ */ captureSharedBufferByteLength();
|
|
401
|
+
const ordinaryBufferOperations = /* @__PURE__ */ captureOrdinaryBufferOperations();
|
|
402
|
+
function isSharedArrayBuffer(value) {
|
|
403
|
+
if (!value || typeof value !== "object") return false;
|
|
404
|
+
if (sharedBufferByteLength) try {
|
|
405
|
+
if (ordinaryBufferOperations) Reflect.construct(ordinaryBufferOperations.dataView, [
|
|
406
|
+
value,
|
|
407
|
+
0,
|
|
408
|
+
0
|
|
409
|
+
]);
|
|
410
|
+
return typeof sharedBufferByteLength.call(value) === "number";
|
|
411
|
+
} catch {
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
if (!ordinaryBufferOperations) try {
|
|
415
|
+
const prototype = Object.getPrototypeOf(value);
|
|
416
|
+
if (prototype === Object.prototype) return false;
|
|
417
|
+
const tag = Object.getOwnPropertyDescriptor(prototype, Symbol.toStringTag)?.value;
|
|
418
|
+
const byteLength = Object.getOwnPropertyDescriptor(prototype, "byteLength")?.get;
|
|
419
|
+
return tag === "SharedArrayBuffer" && typeof byteLength === "function";
|
|
420
|
+
} catch {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
try {
|
|
424
|
+
Reflect.construct(ordinaryBufferOperations.dataView, [
|
|
425
|
+
value,
|
|
426
|
+
0,
|
|
427
|
+
0
|
|
428
|
+
]);
|
|
429
|
+
try {
|
|
430
|
+
ordinaryBufferOperations.byteLength.call(value);
|
|
431
|
+
return false;
|
|
432
|
+
} catch {
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
} catch {
|
|
436
|
+
return false;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
function containsSharedArrayBuffer(value, seen = /* @__PURE__ */ new Set()) {
|
|
440
|
+
if (!value || typeof value !== "object") return false;
|
|
441
|
+
if (isSharedArrayBuffer(value)) return true;
|
|
442
|
+
const WebAssemblyMemory = globalThis.WebAssembly?.Memory;
|
|
443
|
+
if (WebAssemblyMemory && value instanceof WebAssemblyMemory) return isSharedArrayBuffer(value.buffer);
|
|
444
|
+
if (seen.has(value)) return false;
|
|
445
|
+
seen.add(value);
|
|
446
|
+
if (ArrayBuffer.isView(value)) return containsSharedArrayBuffer(value.buffer, seen);
|
|
447
|
+
const mapEntries = reflectedMapEntries(value);
|
|
448
|
+
if (mapEntries) return mapEntries.some(([key, entry]) => containsSharedArrayBuffer(key, seen) || containsSharedArrayBuffer(entry, seen));
|
|
449
|
+
const setValues = reflectedSetValues(value);
|
|
450
|
+
if (setValues) return setValues.some((entry) => containsSharedArrayBuffer(entry, seen));
|
|
451
|
+
return Reflect.ownKeys(value).some((key) => {
|
|
452
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
453
|
+
return descriptor !== void 0 && "value" in descriptor && containsSharedArrayBuffer(descriptor.value, seen);
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
function reflectedRegExpState(value) {
|
|
457
|
+
if (!value || typeof value !== "object") return;
|
|
458
|
+
try {
|
|
459
|
+
const source = Object.getOwnPropertyDescriptor(RegExp.prototype, "source")?.get?.call(value);
|
|
460
|
+
const flags = Object.getOwnPropertyDescriptor(RegExp.prototype, "flags")?.get?.call(value);
|
|
461
|
+
const lastIndex = Reflect.get(value, "lastIndex");
|
|
462
|
+
if (typeof source !== "string" || typeof flags !== "string" || typeof lastIndex !== "number") return;
|
|
463
|
+
return {
|
|
464
|
+
flags,
|
|
465
|
+
lastIndex,
|
|
466
|
+
source
|
|
467
|
+
};
|
|
468
|
+
} catch {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function reflectedArrayBufferViewState(value) {
|
|
473
|
+
if (!ArrayBuffer.isView(value)) return;
|
|
474
|
+
try {
|
|
475
|
+
const bufferConstructor = Object.getOwnPropertyDescriptor(globalThis, "Buffer")?.value ?? globalThis.Buffer;
|
|
476
|
+
const isBuffer = typeof bufferConstructor === "function" ? Object.getOwnPropertyDescriptor(bufferConstructor, "isBuffer")?.value : void 0;
|
|
477
|
+
const isNodeBuffer = typeof isBuffer === "function" && Reflect.apply(isBuffer, bufferConstructor, [value]) === true;
|
|
478
|
+
const typedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype);
|
|
479
|
+
if (isNodeBuffer) {
|
|
480
|
+
const byteLength = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), "byteLength")?.get?.call(value);
|
|
481
|
+
return typeof byteLength === "number" ? {
|
|
482
|
+
byteLength,
|
|
483
|
+
kind: "Buffer"
|
|
484
|
+
} : void 0;
|
|
485
|
+
}
|
|
486
|
+
const typedArrayTag = Object.getOwnPropertyDescriptor(typedArrayPrototype, Symbol.toStringTag)?.get?.call(value);
|
|
487
|
+
if (typeof typedArrayTag === "string") {
|
|
488
|
+
const byteLength = Object.getOwnPropertyDescriptor(typedArrayPrototype, "byteLength")?.get?.call(value);
|
|
489
|
+
return typeof byteLength === "number" ? {
|
|
490
|
+
byteLength,
|
|
491
|
+
kind: typedArrayTag
|
|
492
|
+
} : void 0;
|
|
493
|
+
}
|
|
494
|
+
const dataViewByteLength = Object.getOwnPropertyDescriptor(DataView.prototype, "byteLength")?.get?.call(value);
|
|
495
|
+
return typeof dataViewByteLength === "number" ? {
|
|
496
|
+
byteLength: dataViewByteLength,
|
|
497
|
+
kind: "DataView"
|
|
498
|
+
} : void 0;
|
|
499
|
+
} catch {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function reflectedBlobState(value) {
|
|
504
|
+
const BlobConstructor = globalThis.Blob;
|
|
505
|
+
if (typeof BlobConstructor !== "function" || !(value instanceof BlobConstructor)) return;
|
|
506
|
+
try {
|
|
507
|
+
const FileConstructor = globalThis.File;
|
|
508
|
+
const file = typeof FileConstructor === "function" && value instanceof FileConstructor ? value : void 0;
|
|
509
|
+
const intrinsic = file ? new FileConstructor([], "") : new BlobConstructor([]);
|
|
510
|
+
const size = Object.getOwnPropertyDescriptor(BlobConstructor.prototype, "size")?.get?.call(value);
|
|
511
|
+
const type = Object.getOwnPropertyDescriptor(BlobConstructor.prototype, "type")?.get?.call(value);
|
|
512
|
+
if (typeof size !== "number" || typeof type !== "string") return;
|
|
513
|
+
const state = {
|
|
514
|
+
intrinsicSymbols: new Set(Reflect.ownKeys(intrinsic).filter((key) => typeof key === "symbol" && Object.getOwnPropertyDescriptor(intrinsic, key)?.enumerable === true)),
|
|
515
|
+
size,
|
|
516
|
+
type
|
|
517
|
+
};
|
|
518
|
+
if (file) {
|
|
519
|
+
const lastModified = Object.getOwnPropertyDescriptor(FileConstructor.prototype, "lastModified")?.get?.call(file);
|
|
520
|
+
const name = Object.getOwnPropertyDescriptor(FileConstructor.prototype, "name")?.get?.call(file);
|
|
521
|
+
if (typeof lastModified !== "number" || typeof name !== "string") return;
|
|
522
|
+
state.lastModified = lastModified;
|
|
523
|
+
state.name = name;
|
|
524
|
+
}
|
|
525
|
+
return state;
|
|
526
|
+
} catch {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
function containsUnsafeStructuredCloneState(value, seen = /* @__PURE__ */ new Set()) {
|
|
531
|
+
if (!value || typeof value !== "object" || seen.has(value)) return false;
|
|
532
|
+
if (isCryptoKey(value) || isWebAssemblyModule(value)) return true;
|
|
533
|
+
seen.add(value);
|
|
534
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
535
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
536
|
+
if (!descriptor?.enumerable) continue;
|
|
537
|
+
if (!("value" in descriptor) || containsUnsafeStructuredCloneState(descriptor.value, seen)) return true;
|
|
538
|
+
}
|
|
539
|
+
for (const key of ["cause", "errors"]) {
|
|
540
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
541
|
+
if (descriptor && (!("value" in descriptor) || containsUnsafeStructuredCloneState(descriptor.value, seen))) return true;
|
|
542
|
+
}
|
|
543
|
+
const mapEntries = reflectedMapEntries(value);
|
|
544
|
+
if (mapEntries) {
|
|
545
|
+
for (const [key, entry] of mapEntries) if (containsUnsafeStructuredCloneState(key, seen) || containsUnsafeStructuredCloneState(entry, seen)) return true;
|
|
546
|
+
} else {
|
|
547
|
+
const setValues = reflectedSetValues(value);
|
|
548
|
+
if (setValues) {
|
|
549
|
+
for (const entry of setValues) if (containsUnsafeStructuredCloneState(entry, seen)) return true;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return false;
|
|
553
|
+
}
|
|
554
|
+
function preservesEnumerableFields(source, snapshot, seen = /* @__PURE__ */ new Map()) {
|
|
555
|
+
if (!source || typeof source !== "object") return true;
|
|
556
|
+
if (!snapshot || typeof snapshot !== "object") return false;
|
|
557
|
+
if (seen.has(source)) return seen.get(source) === snapshot;
|
|
558
|
+
seen.set(source, snapshot);
|
|
559
|
+
const sourceView = reflectedArrayBufferViewState(source);
|
|
560
|
+
if (sourceView) {
|
|
561
|
+
const snapshotView = reflectedArrayBufferViewState(snapshot);
|
|
562
|
+
if (!snapshotView || sourceView.kind !== snapshotView.kind || sourceView.byteLength !== snapshotView.byteLength) return false;
|
|
563
|
+
}
|
|
564
|
+
if (source instanceof Date && (!(snapshot instanceof Date) || !Object.is(source.getTime(), snapshot.getTime()))) return false;
|
|
565
|
+
const sourceRegExp = reflectedRegExpState(source);
|
|
566
|
+
if (sourceRegExp) {
|
|
567
|
+
const snapshotRegExp = reflectedRegExpState(snapshot);
|
|
568
|
+
if (!snapshotRegExp || sourceRegExp.source !== snapshotRegExp.source || sourceRegExp.flags !== snapshotRegExp.flags || sourceRegExp.lastIndex !== snapshotRegExp.lastIndex) return false;
|
|
569
|
+
}
|
|
570
|
+
const sourceBlob = reflectedBlobState(source);
|
|
571
|
+
if (sourceBlob) {
|
|
572
|
+
const snapshotBlob = reflectedBlobState(snapshot);
|
|
573
|
+
if (!snapshotBlob || sourceBlob.size !== snapshotBlob.size || sourceBlob.type !== snapshotBlob.type || sourceBlob.name !== snapshotBlob.name || sourceBlob.lastModified !== snapshotBlob.lastModified) return false;
|
|
574
|
+
}
|
|
575
|
+
const sourceAggregateErrors = Object.getOwnPropertyDescriptor(source, "errors");
|
|
576
|
+
if (sourceAggregateErrors && "value" in sourceAggregateErrors) {
|
|
577
|
+
const snapshotAggregateErrors = Object.getOwnPropertyDescriptor(snapshot, "errors");
|
|
578
|
+
if (!snapshotAggregateErrors || !("value" in snapshotAggregateErrors) || !preservesEnumerableFields(sourceAggregateErrors.value, snapshotAggregateErrors.value, seen)) return false;
|
|
579
|
+
}
|
|
580
|
+
if (source instanceof AggregateError) {
|
|
581
|
+
if (!(snapshot instanceof AggregateError) || source.name !== snapshot.name || source.message !== snapshot.message || !preservesEnumerableFields(source.errors, snapshot.errors, seen) || !preservesEnumerableFields(source.cause, snapshot.cause, seen)) return false;
|
|
582
|
+
} else if (source instanceof DOMException) {
|
|
583
|
+
if (!(snapshot instanceof DOMException) || source.name !== snapshot.name || source.message !== snapshot.message || source.code !== snapshot.code) return false;
|
|
584
|
+
} else if (source instanceof Error) {
|
|
585
|
+
if (!(snapshot instanceof Error) || source.name !== snapshot.name || source.message !== snapshot.message || !preservesEnumerableFields(source.cause, snapshot.cause, seen)) return false;
|
|
586
|
+
}
|
|
587
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
588
|
+
const sourceDescriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
589
|
+
if (!sourceDescriptor?.enumerable) continue;
|
|
590
|
+
if (typeof key === "symbol") {
|
|
591
|
+
if (sourceBlob?.intrinsicSymbols.has(key)) continue;
|
|
592
|
+
return false;
|
|
593
|
+
}
|
|
594
|
+
if (!("value" in sourceDescriptor)) return false;
|
|
595
|
+
const snapshotDescriptor = Object.getOwnPropertyDescriptor(snapshot, key);
|
|
596
|
+
if (!snapshotDescriptor || !("value" in snapshotDescriptor)) return false;
|
|
597
|
+
if (!preservesEnumerableFields(sourceDescriptor.value, snapshotDescriptor.value, seen)) return false;
|
|
598
|
+
}
|
|
599
|
+
const sourceMapEntries = reflectedMapEntries(source);
|
|
600
|
+
if (sourceMapEntries) {
|
|
601
|
+
const snapshotEntries = reflectedMapEntries(snapshot);
|
|
602
|
+
if (!snapshotEntries || sourceMapEntries.length !== snapshotEntries.length) return false;
|
|
603
|
+
return sourceMapEntries.every(([key, value], index) => {
|
|
604
|
+
const snapshotEntry = snapshotEntries[index];
|
|
605
|
+
return snapshotEntry !== void 0 && preservesEnumerableFields(key, snapshotEntry[0], seen) && preservesEnumerableFields(value, snapshotEntry[1], seen);
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
const sourceSetValues = reflectedSetValues(source);
|
|
609
|
+
if (sourceSetValues) {
|
|
610
|
+
const snapshotValues = reflectedSetValues(snapshot);
|
|
611
|
+
if (!snapshotValues || sourceSetValues.length !== snapshotValues.length) return false;
|
|
612
|
+
return sourceSetValues.every((value, index) => preservesEnumerableFields(value, snapshotValues[index], seen));
|
|
613
|
+
}
|
|
614
|
+
return true;
|
|
615
|
+
}
|
|
616
|
+
function normalizedTracePayload(payload) {
|
|
617
|
+
if (payload === void 0) return;
|
|
618
|
+
if (!payload || typeof payload !== "object") return { visibility: "private" };
|
|
619
|
+
try {
|
|
620
|
+
if (Array.isArray(payload)) return { visibility: "private" };
|
|
621
|
+
const visibility = Object.getOwnPropertyDescriptor(payload, "visibility");
|
|
622
|
+
if (!visibility || !("value" in visibility)) return { visibility: "private" };
|
|
623
|
+
if (visibility.value === "public") {
|
|
624
|
+
const value = Object.getOwnPropertyDescriptor(payload, "value");
|
|
625
|
+
if (value && "value" in value && !containsUnsafeStructuredCloneState(value.value)) {
|
|
626
|
+
const snapshot = structuredClone(value.value);
|
|
627
|
+
if (preservesEnumerableFields(value.value, snapshot) && !containsSharedArrayBuffer(snapshot)) return {
|
|
628
|
+
value: snapshot,
|
|
629
|
+
visibility: "public"
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
if (visibility.value === "summary") {
|
|
634
|
+
const summary = Object.getOwnPropertyDescriptor(payload, "summary");
|
|
635
|
+
if (summary && "value" in summary && typeof summary.value === "string") return {
|
|
636
|
+
summary: summary.value,
|
|
637
|
+
visibility: "summary"
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
if (visibility.value === "redacted") return { visibility: "redacted" };
|
|
641
|
+
} catch {
|
|
642
|
+
return { visibility: "private" };
|
|
643
|
+
}
|
|
644
|
+
return { visibility: "private" };
|
|
645
|
+
}
|
|
646
|
+
function traceEventAttributes(event, content, normalized = false) {
|
|
647
|
+
const activity = normalized ? event.activity : normalizedTraceActivity(event.activity);
|
|
648
|
+
const payload = normalized ? event.payload : normalizedTracePayload(event.payload);
|
|
649
|
+
const next = { ...normalizeTraceAttributes(event.attributes, content) };
|
|
650
|
+
if (activity) {
|
|
651
|
+
next["vitehub.activity.owner"] = activity.owner;
|
|
652
|
+
next["vitehub.activity.phase"] = activity.phase;
|
|
653
|
+
}
|
|
654
|
+
if (payload) {
|
|
655
|
+
next["vitehub.payload.visibility"] = payload.visibility;
|
|
656
|
+
if (payload.visibility === "public") next["vitehub.payload.value"] = payload.value;
|
|
657
|
+
if (payload.visibility === "summary") next["vitehub.payload.summary"] = payload.summary;
|
|
658
|
+
}
|
|
659
|
+
return Object.keys(next).length ? next : void 0;
|
|
660
|
+
}
|
|
661
|
+
function aggregateTraceAttributes(attributes) {
|
|
662
|
+
if (!attributes) return void 0;
|
|
663
|
+
const next = { ...attributes };
|
|
664
|
+
delete next["vitehub.activity.owner"];
|
|
665
|
+
delete next["vitehub.activity.phase"];
|
|
666
|
+
delete next["vitehub.payload.summary"];
|
|
667
|
+
delete next["vitehub.payload.value"];
|
|
668
|
+
delete next["vitehub.payload.visibility"];
|
|
669
|
+
return Object.keys(next).length ? next : void 0;
|
|
670
|
+
}
|
|
671
|
+
function stepTraceAttributes(attributes) {
|
|
672
|
+
if (!attributes) return void 0;
|
|
673
|
+
const next = { ...attributes };
|
|
674
|
+
delete next["vitehub.activity.owner"];
|
|
675
|
+
delete next["vitehub.activity.phase"];
|
|
676
|
+
return Object.keys(next).length ? next : void 0;
|
|
677
|
+
}
|
|
678
|
+
function normalizeTraceEvent(event, sequence, content) {
|
|
679
|
+
const { activity: rawActivity, attributes: _attributes, payload: rawPayload, ...rest } = event;
|
|
680
|
+
const activity = normalizedTraceActivity(rawActivity);
|
|
681
|
+
const payload = normalizedTracePayload(rawPayload);
|
|
682
|
+
const attributes = traceEventAttributes({
|
|
683
|
+
activity,
|
|
684
|
+
attributes: event.attributes,
|
|
685
|
+
payload
|
|
686
|
+
}, content, true);
|
|
687
|
+
return {
|
|
688
|
+
...rest,
|
|
689
|
+
...activity ? { activity } : {},
|
|
690
|
+
...attributes ? { attributes } : {},
|
|
691
|
+
...payload ? { payload } : {},
|
|
692
|
+
sequence,
|
|
693
|
+
timestamp: timestamp(event.timestamp)
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
async function emitTraceEvent(context, event) {
|
|
697
|
+
return await context.traceLog?.append({
|
|
698
|
+
...event,
|
|
699
|
+
trace: event.trace || context.trace
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
function createTraceEventLog(options = {}) {
|
|
703
|
+
const content = options.content || "metadata";
|
|
704
|
+
const entries = [];
|
|
705
|
+
const cloneAttributes = (attributes) => {
|
|
706
|
+
if (!attributes) return void 0;
|
|
707
|
+
return { ...attributes };
|
|
708
|
+
};
|
|
709
|
+
const cloneEntry = (entry) => {
|
|
710
|
+
const publicValue = entry.attributes?.["vitehub.payload.value"];
|
|
711
|
+
const cloneable = structuredClone({
|
|
712
|
+
activity: entry.activity,
|
|
713
|
+
payload: entry.payload,
|
|
714
|
+
publicValue
|
|
715
|
+
});
|
|
716
|
+
const attributes = cloneAttributes(entry.attributes);
|
|
717
|
+
if (attributes && "vitehub.payload.value" in attributes) attributes["vitehub.payload.value"] = cloneable.publicValue;
|
|
718
|
+
return {
|
|
719
|
+
...entry,
|
|
720
|
+
...cloneable.activity ? { activity: cloneable.activity } : {},
|
|
721
|
+
...attributes ? { attributes } : {},
|
|
722
|
+
...cloneable.payload ? { payload: cloneable.payload } : {}
|
|
723
|
+
};
|
|
724
|
+
};
|
|
725
|
+
return {
|
|
726
|
+
async append(event) {
|
|
727
|
+
const entry = normalizeTraceEvent(event, entries.length + 1, content);
|
|
728
|
+
entries.push(entry);
|
|
729
|
+
await options.onEntry?.(cloneEntry(entry));
|
|
730
|
+
return cloneEntry(entry);
|
|
731
|
+
},
|
|
732
|
+
entries() {
|
|
733
|
+
return entries.map(cloneEntry);
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
function millis(value) {
|
|
738
|
+
const parsed = Date.parse(value);
|
|
739
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
740
|
+
}
|
|
741
|
+
function durationMs(startTime, endTime) {
|
|
742
|
+
if (!endTime) return void 0;
|
|
743
|
+
const duration = millis(endTime) - millis(startTime);
|
|
744
|
+
return Number.isFinite(duration) && duration >= 0 ? duration : void 0;
|
|
745
|
+
}
|
|
746
|
+
function firstString(...values) {
|
|
747
|
+
return values.find((value) => typeof value === "string" && value.length > 0);
|
|
748
|
+
}
|
|
749
|
+
function runId(event) {
|
|
750
|
+
return firstString(event.attributes?.["agent.run.id"], event.attributes?.["run.id"], event.attributes?.runId, event.trace?.id) || "default";
|
|
751
|
+
}
|
|
752
|
+
function stepId(event) {
|
|
753
|
+
return firstString(event.attributes?.["step.id"], event.attributes?.["tool.id"], event.attributes?.["approval.id"], event.attributes?.["model.call.id"]);
|
|
754
|
+
}
|
|
755
|
+
function stepStatus(event) {
|
|
756
|
+
return event.type === "error" || /\.(?:cancelled|error|failed)$/.test(event.name) ? "failed" : /\.(?:output|progress|request|start|started|summary|updated)$/.test(event.name) ? "running" : "completed";
|
|
757
|
+
}
|
|
758
|
+
function isTraceRunFinish(event) {
|
|
759
|
+
return event.name === "agent.invocation.cancelled" || event.name === "agent.invocation.finish" || event.name === "run.finish";
|
|
760
|
+
}
|
|
761
|
+
function isTraceRunError(event) {
|
|
762
|
+
return event.name === "agent.invocation.error" || event.name === "agent.stream.error" && event.attributes?.["error.recoverable"] !== true || event.name === "run.error";
|
|
763
|
+
}
|
|
764
|
+
function isTraceRunTerminal(event) {
|
|
765
|
+
return isTraceRunFinish(event) || isTraceRunError(event);
|
|
766
|
+
}
|
|
767
|
+
function isTraceRunFailureEvidence(event) {
|
|
768
|
+
return event.name === "run.error" || event.name === "agent.stream.error" && event.attributes?.["error.recoverable"] !== true;
|
|
769
|
+
}
|
|
770
|
+
function deriveTraceRuns(events) {
|
|
771
|
+
const groups = /* @__PURE__ */ new Map();
|
|
772
|
+
for (const event of events) {
|
|
773
|
+
const id = runId(event);
|
|
774
|
+
const group = groups.get(id);
|
|
775
|
+
if (group) group.push(event);
|
|
776
|
+
else groups.set(id, [event]);
|
|
777
|
+
}
|
|
778
|
+
return [...groups.entries()].map(([id, group]) => {
|
|
779
|
+
const sorted = group.sort((a, b) => a.sequence - b.sequence);
|
|
780
|
+
const first = sorted[0];
|
|
781
|
+
const steps = /* @__PURE__ */ new Map();
|
|
782
|
+
for (const event of sorted) {
|
|
783
|
+
const id = stepId(event);
|
|
784
|
+
if (!id) continue;
|
|
785
|
+
const existing = steps.get(id);
|
|
786
|
+
const status = stepStatus(event);
|
|
787
|
+
if (!existing) {
|
|
788
|
+
steps.set(id, {
|
|
789
|
+
attributes: stepTraceAttributes(event.attributes),
|
|
790
|
+
endTime: status === "running" ? void 0 : event.timestamp,
|
|
791
|
+
events: [event],
|
|
792
|
+
id,
|
|
793
|
+
name: event.name.replace(/\.(start|started|progress|finish|completed|failed|cancelled|end|error|request|decision|recorded)$/, ""),
|
|
794
|
+
startTime: event.timestamp,
|
|
795
|
+
status,
|
|
796
|
+
type: event.type
|
|
797
|
+
});
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
existing.events.push(event);
|
|
801
|
+
const attributes = { ...existing.attributes };
|
|
802
|
+
if (event.attributes?.["vitehub.payload.visibility"] !== void 0) {
|
|
803
|
+
delete attributes["vitehub.payload.summary"];
|
|
804
|
+
delete attributes["vitehub.payload.value"];
|
|
805
|
+
delete attributes["vitehub.payload.visibility"];
|
|
806
|
+
}
|
|
807
|
+
existing.attributes = stepTraceAttributes({
|
|
808
|
+
...attributes,
|
|
809
|
+
...event.attributes
|
|
810
|
+
});
|
|
811
|
+
if (status !== "running") {
|
|
812
|
+
existing.endTime = event.timestamp;
|
|
813
|
+
existing.status = status;
|
|
814
|
+
existing.durationMs = durationMs(existing.startTime, existing.endTime);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
const terminal = sorted.slice().reverse().find(isTraceRunTerminal);
|
|
818
|
+
const status = sorted.some(isTraceRunFailureEvidence) || terminal && isTraceRunError(terminal) ? "failed" : terminal?.name === "agent.invocation.cancelled" ? "cancelled" : terminal ? "completed" : "running";
|
|
819
|
+
const endTime = status === "running" ? void 0 : terminal?.timestamp;
|
|
820
|
+
return {
|
|
821
|
+
durationMs: durationMs(first.timestamp, endTime),
|
|
822
|
+
endTime,
|
|
823
|
+
events: sorted,
|
|
824
|
+
id,
|
|
825
|
+
startTime: first.timestamp,
|
|
826
|
+
status,
|
|
827
|
+
steps: [...steps.values()]
|
|
828
|
+
};
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
function traceRunTraceId(run) {
|
|
832
|
+
return firstString(...run.events.map((event) => event.trace?.id), run.id) || run.id;
|
|
833
|
+
}
|
|
834
|
+
function traceRunParentId(run) {
|
|
835
|
+
return firstString(...run.events.map((event) => event.trace?.parentId));
|
|
836
|
+
}
|
|
837
|
+
function traceRunSpanId(run) {
|
|
838
|
+
return firstString(...run.events.map((event) => event.attributes?.["agent.invocation.id"]), run.id) || run.id;
|
|
839
|
+
}
|
|
840
|
+
function isOpenTelemetryId(value, length) {
|
|
841
|
+
return value.length === length && /^[0-9a-f]+$/.test(value);
|
|
842
|
+
}
|
|
843
|
+
function openTelemetryId(value, length) {
|
|
844
|
+
const normalized = value.toLowerCase();
|
|
845
|
+
if (isOpenTelemetryId(normalized, length)) return normalized;
|
|
846
|
+
let output = "";
|
|
847
|
+
for (let seed = 0; output.length < length; seed += 1) {
|
|
848
|
+
let hash = 2166136261;
|
|
849
|
+
const input = `${value}:${seed}`;
|
|
850
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
851
|
+
hash ^= input.charCodeAt(index);
|
|
852
|
+
hash = Math.imul(hash, 16777619);
|
|
853
|
+
}
|
|
854
|
+
output += (hash >>> 0).toString(16).padStart(8, "0");
|
|
855
|
+
}
|
|
856
|
+
const id = output.slice(0, length);
|
|
857
|
+
return /^0+$/.test(id) ? `1${id.slice(1)}` : id;
|
|
858
|
+
}
|
|
859
|
+
function traceEventsToOpenTelemetryLogRecords(events, options = {}) {
|
|
860
|
+
return deriveTraceRuns([...events]).flatMap((run) => {
|
|
861
|
+
const spanId = openTelemetryId(traceRunSpanId(run), 16);
|
|
862
|
+
const traceId = openTelemetryId(traceRunTraceId(run), 32);
|
|
863
|
+
return run.events.map((event) => {
|
|
864
|
+
const id = stepId(event);
|
|
865
|
+
const attributes = traceEventAttributes(event, options.content === "metadata" ? "metadata" : "content");
|
|
866
|
+
const warning = event.name === "agent.stream.error" && event.attributes?.["error.recoverable"] === true;
|
|
867
|
+
const error = !warning && (event.type === "error" || /\.(?:cancelled|error|failed)$/.test(event.name));
|
|
868
|
+
return {
|
|
869
|
+
attributes: {
|
|
870
|
+
...attributes,
|
|
871
|
+
"vitehub.event.sequence": event.sequence,
|
|
872
|
+
"vitehub.event.type": event.type,
|
|
873
|
+
"vitehub.run.id": run.id,
|
|
874
|
+
...id ? { "vitehub.step.id": id } : {}
|
|
875
|
+
},
|
|
876
|
+
eventName: event.name,
|
|
877
|
+
...warning ? {
|
|
878
|
+
severityNumber: 13,
|
|
879
|
+
severityText: "WARN"
|
|
880
|
+
} : error ? {
|
|
881
|
+
severityNumber: 17,
|
|
882
|
+
severityText: "ERROR"
|
|
883
|
+
} : {},
|
|
884
|
+
spanId: id ? openTelemetryId(`${spanId}:${id}`, 16) : spanId,
|
|
885
|
+
time: event.timestamp,
|
|
886
|
+
traceId
|
|
887
|
+
};
|
|
888
|
+
});
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
function traceEventsToOpenTelemetrySpans(events, options = {}) {
|
|
892
|
+
const maxEntries = 1024;
|
|
893
|
+
const firstEntries = maxEntries / 2;
|
|
894
|
+
const tailEntries = maxEntries / 2;
|
|
895
|
+
const entriesByRun = /* @__PURE__ */ new Map();
|
|
896
|
+
for (const entry of events) {
|
|
897
|
+
const id = runId(entry);
|
|
898
|
+
let run = entriesByRun.get(id);
|
|
899
|
+
if (!run) {
|
|
900
|
+
run = {
|
|
901
|
+
count: 0,
|
|
902
|
+
first: [],
|
|
903
|
+
tail: Array.from({ length: tailEntries })
|
|
904
|
+
};
|
|
905
|
+
entriesByRun.set(id, run);
|
|
906
|
+
}
|
|
907
|
+
const indexed = {
|
|
908
|
+
entry,
|
|
909
|
+
index: run.count
|
|
910
|
+
};
|
|
911
|
+
if (run.count < firstEntries) run.first.push(indexed);
|
|
912
|
+
else run.tail[(run.count - firstEntries) % tailEntries] = indexed;
|
|
913
|
+
if (isTraceRunFailureEvidence(entry)) run.failure = indexed;
|
|
914
|
+
if (isTraceRunTerminal(entry)) run.terminal = indexed;
|
|
915
|
+
run.count += 1;
|
|
916
|
+
}
|
|
917
|
+
return deriveTraceRuns([...entriesByRun.values()].flatMap((run) => {
|
|
918
|
+
const tail = run.tail.filter((entry) => entry !== void 0).sort((left, right) => left.index - right.index);
|
|
919
|
+
const evidence = [run.failure, run.terminal].filter((entry) => entry !== void 0);
|
|
920
|
+
const indexed = run.count <= maxEntries ? [...run.first, ...tail] : [
|
|
921
|
+
...run.first,
|
|
922
|
+
...tail.slice(evidence.length),
|
|
923
|
+
...evidence
|
|
924
|
+
];
|
|
925
|
+
const entries = [...new Map(indexed.map((value) => [value.index, value])).values()].sort((left, right) => left.index - right.index).map((value) => value.entry);
|
|
926
|
+
if (run.count <= maxEntries) return entries;
|
|
927
|
+
return entries.map((entry, index) => index === 0 ? {
|
|
928
|
+
...entry,
|
|
929
|
+
attributes: {
|
|
930
|
+
...entry.attributes,
|
|
931
|
+
"vitehub.trace.originalEventCount": run.count,
|
|
932
|
+
"vitehub.trace.truncated": true
|
|
933
|
+
}
|
|
934
|
+
} : entry);
|
|
935
|
+
}).map((entry) => ({
|
|
936
|
+
...entry,
|
|
937
|
+
attributes: traceEventAttributes(entry, options.content === "metadata" ? "metadata" : "content")
|
|
938
|
+
}))).flatMap((run) => {
|
|
939
|
+
const rawParentSpanId = traceRunParentId(run);
|
|
940
|
+
const rawTraceId = traceRunTraceId(run);
|
|
941
|
+
const parentSpanId = rawParentSpanId ? openTelemetryId(rawParentSpanId, 16) : void 0;
|
|
942
|
+
const spanId = openTelemetryId(traceRunSpanId(run), 16);
|
|
943
|
+
const traceId = openTelemetryId(rawTraceId, 32);
|
|
944
|
+
const attributes = aggregateTraceAttributes(Object.assign({}, ...run.events.filter((event) => !stepId(event)).map((event) => event.attributes || {})));
|
|
945
|
+
const spanEvents = (events) => events.map((event) => ({
|
|
946
|
+
...event.attributes ? { attributes: event.attributes } : {},
|
|
947
|
+
name: event.name,
|
|
948
|
+
time: event.timestamp
|
|
949
|
+
}));
|
|
950
|
+
const errorMessage = firstString(...run.events.slice().reverse().map((event) => event.attributes?.["error.message"]));
|
|
951
|
+
return [{
|
|
952
|
+
attributes: {
|
|
953
|
+
...attributes,
|
|
954
|
+
"vitehub.run.id": run.id,
|
|
955
|
+
...rawParentSpanId ? { "vitehub.trace.parentId": rawParentSpanId } : {},
|
|
956
|
+
"vitehub.trace.id": rawTraceId
|
|
957
|
+
},
|
|
958
|
+
endTime: run.endTime,
|
|
959
|
+
events: spanEvents(run.events.filter((event) => !stepId(event))),
|
|
960
|
+
name: "vitehub.run",
|
|
961
|
+
...parentSpanId ? { parentSpanId } : {},
|
|
962
|
+
spanId,
|
|
963
|
+
startTime: run.startTime,
|
|
964
|
+
status: {
|
|
965
|
+
code: run.status === "failed" ? "ERROR" : run.status === "completed" ? "OK" : "UNSET",
|
|
966
|
+
...run.status === "failed" && errorMessage ? { message: errorMessage } : {}
|
|
967
|
+
},
|
|
968
|
+
traceId
|
|
969
|
+
}, ...run.steps.map((step) => ({
|
|
970
|
+
attributes: {
|
|
971
|
+
...aggregateTraceAttributes(step.attributes),
|
|
972
|
+
"vitehub.run.id": run.id,
|
|
973
|
+
"vitehub.step.id": step.id
|
|
974
|
+
},
|
|
975
|
+
endTime: step.endTime || run.endTime,
|
|
976
|
+
events: spanEvents(step.events),
|
|
977
|
+
name: step.name,
|
|
978
|
+
parentSpanId: spanId,
|
|
979
|
+
spanId: openTelemetryId(`${spanId}:${step.id}`, 16),
|
|
980
|
+
startTime: step.startTime,
|
|
981
|
+
status: {
|
|
982
|
+
code: step.status === "failed" || !step.endTime && run.status === "failed" ? "ERROR" : step.endTime || run.status === "completed" ? "OK" : "UNSET",
|
|
983
|
+
...typeof step.attributes?.["error.message"] === "string" ? { message: step.attributes["error.message"] } : {}
|
|
984
|
+
},
|
|
985
|
+
traceId
|
|
986
|
+
}))];
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
function createExecutionContext(context) {
|
|
990
|
+
const capabilities = context.capabilities ?? {};
|
|
991
|
+
const runtimeConfig = context.runtimeConfig ?? {};
|
|
992
|
+
return {
|
|
993
|
+
...context,
|
|
994
|
+
capabilities,
|
|
995
|
+
runtimeConfig
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
/** Create one operation's context. Without a host waitUntil, drain work before returning. */
|
|
999
|
+
function createRuntimeContext(input) {
|
|
1000
|
+
const values = /* @__PURE__ */ new Map();
|
|
1001
|
+
const memo = input.memo ?? ((key, create) => {
|
|
1002
|
+
if (!values.has(key)) values.set(key, create());
|
|
1003
|
+
return values.get(key);
|
|
1004
|
+
});
|
|
1005
|
+
const controller = createRuntimeWaitUntilController({ forward: input.waitUntil?.bind(input) });
|
|
1006
|
+
return createExecutionContext({
|
|
1007
|
+
...input,
|
|
1008
|
+
...controller,
|
|
1009
|
+
memo
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
function defineCapability(kind, value, options = {}) {
|
|
1013
|
+
const handle = {
|
|
1014
|
+
kind,
|
|
1015
|
+
name: options.name,
|
|
1016
|
+
value
|
|
1017
|
+
};
|
|
1018
|
+
capabilityHandles.add(handle);
|
|
1019
|
+
return handle;
|
|
1020
|
+
}
|
|
1021
|
+
function createRuntimeWaitUntilController(options = {}) {
|
|
1022
|
+
const pending = [];
|
|
1023
|
+
let flushing;
|
|
1024
|
+
return {
|
|
1025
|
+
flushWaitUntil() {
|
|
1026
|
+
flushing ??= Promise.resolve().then(async () => {
|
|
1027
|
+
try {
|
|
1028
|
+
let error;
|
|
1029
|
+
let failed = false;
|
|
1030
|
+
while (pending.length > 0) await Promise.all(pending.splice(0).map(async (task) => {
|
|
1031
|
+
try {
|
|
1032
|
+
await task;
|
|
1033
|
+
} catch (reason) {
|
|
1034
|
+
if (failed) return;
|
|
1035
|
+
error = reason;
|
|
1036
|
+
failed = true;
|
|
1037
|
+
}
|
|
1038
|
+
}));
|
|
1039
|
+
if (failed) throw error;
|
|
1040
|
+
} finally {
|
|
1041
|
+
flushing = void 0;
|
|
1042
|
+
}
|
|
1043
|
+
});
|
|
1044
|
+
return flushing;
|
|
1045
|
+
},
|
|
1046
|
+
waitUntil(task) {
|
|
1047
|
+
task.catch(() => {});
|
|
1048
|
+
pending.push(task);
|
|
1049
|
+
options.forward?.(task);
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
const capabilityHandles = /* @__PURE__ */ new WeakSet();
|
|
1054
|
+
function isProxy(value) {
|
|
1055
|
+
try {
|
|
1056
|
+
const getBuiltinModule = globalThis.process?.getBuiltinModule;
|
|
1057
|
+
const detector = getBuiltinModule?.("node:util")?.types?.isProxy;
|
|
1058
|
+
return detector ? detector(value) : void 0;
|
|
1059
|
+
} catch {
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
function isCapabilityHandle(value) {
|
|
1064
|
+
if (typeof value !== "object" || value === null) return false;
|
|
1065
|
+
if (capabilityHandles.has(value)) return true;
|
|
1066
|
+
if (isProxy(value) === true) return false;
|
|
1067
|
+
try {
|
|
1068
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
1069
|
+
const kind = descriptors.kind;
|
|
1070
|
+
const implementation = descriptors.value;
|
|
1071
|
+
if (!kind || !Object.hasOwn(kind, "value") || typeof kind.value !== "string" || !implementation || !Object.hasOwn(implementation, "value")) return false;
|
|
1072
|
+
if (Object.getPrototypeOf(value) === null) return false;
|
|
1073
|
+
let prototype = Object.getPrototypeOf(value);
|
|
1074
|
+
while (prototype !== null) {
|
|
1075
|
+
if (Object.getOwnPropertyDescriptor(prototype, "kind") || Object.getOwnPropertyDescriptor(prototype, "value")) return false;
|
|
1076
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
1077
|
+
}
|
|
1078
|
+
return true;
|
|
1079
|
+
} catch {
|
|
1080
|
+
return false;
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
function hasCapability(context, name) {
|
|
1084
|
+
return !!context.capabilities && Object.hasOwn(context.capabilities, name);
|
|
1085
|
+
}
|
|
1086
|
+
function getCapability(context, name) {
|
|
1087
|
+
const value = hasCapability(context, name) ? context.capabilities?.[name] : void 0;
|
|
1088
|
+
if (value === void 0) throw new ViteHubError("CAPABILITY_NOT_FOUND", `[vitehub:runtime] Capability "${name}" was not found.`, { details: { capability: name } });
|
|
1089
|
+
if (isCapabilityHandle(value)) return value;
|
|
1090
|
+
return defineCapability(name, value, { name });
|
|
1091
|
+
}
|
|
1092
|
+
function isResolvable(value) {
|
|
1093
|
+
return typeof value === "object" && value !== null && "resolve" in value && typeof value.resolve === "function";
|
|
1094
|
+
}
|
|
1095
|
+
async function resolveRuntimeValue(value, context) {
|
|
1096
|
+
if (isResolvable(value)) return await value.resolve(context);
|
|
1097
|
+
if (typeof value === "function") return await value(context);
|
|
1098
|
+
return value;
|
|
1099
|
+
}
|
|
1100
|
+
async function resolveCapabilityPolicy(policy, context) {
|
|
1101
|
+
if (!policy) return "allow";
|
|
1102
|
+
return typeof policy === "function" ? await policy(context) : policy;
|
|
1103
|
+
}
|
|
1104
|
+
//#endregion
|
|
1105
|
+
export { traceEventsToOpenTelemetrySpans as _, defineCapability as a, getCapability as c, isResolvable as d, noExecutionAuthority as f, traceEventsToOpenTelemetryLogRecords as g, resolveRuntimeValue as h, createTraceEventLog as i, hasCapability as l, resolveCapabilityPolicy as m, createRuntimeContext as n, deriveTraceRuns as o, normalizeExecutionAuthority as p, createRuntimeWaitUntilController as r, emitTraceEvent as s, createExecutionContext as t, isExecutionAuthority as u, unknownExecutionAuthority as v, isTraceContentAttributeKey as y };
|