@hue-run/sdk 0.17.0 → 0.18.1
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/CLI.md +130 -8
- package/EVALUATIONS.md +56 -2
- package/README.md +299 -37
- package/dist/attachment.d.ts +82 -0
- package/dist/attachment.js +225 -0
- package/dist/blobs.d.ts +41 -3
- package/dist/blobs.js +126 -41
- package/dist/cli/import.d.ts +35 -0
- package/dist/cli/import.js +692 -0
- package/dist/cli/langfuse.d.ts +309 -0
- package/dist/cli/langfuse.js +1075 -0
- package/dist/client.d.ts +18 -0
- package/dist/client.js +148 -13
- package/dist/evals/client.d.ts +19 -1
- package/dist/evals/client.js +91 -11
- package/dist/evals/files.d.ts +11 -1
- package/dist/evals/files.js +248 -20
- package/dist/evals/media.d.ts +221 -0
- package/dist/evals/media.js +919 -0
- package/dist/evals/runner.js +130 -106
- package/dist/evals/types.d.ts +49 -0
- package/dist/evals.d.ts +2 -0
- package/dist/evals.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/inline-files.d.ts +140 -8
- package/dist/inline-files.js +572 -51
- package/dist/json-edits.d.ts +24 -0
- package/dist/json-edits.js +187 -0
- package/dist/privacy.d.ts +13 -0
- package/dist/privacy.js +84 -17
- package/dist/safety.d.ts +6 -2
- package/dist/safety.js +15 -3
- package/dist/setup/cli.js +7 -3
- package/dist/snapshot.d.ts +1 -1
- package/dist/snapshot.js +123 -29
- package/dist/transport.d.ts +10 -0
- package/dist/transport.js +296 -56
- package/dist/types.d.ts +25 -6
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/inline-files.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { MAX_BODY_BYTES } from "./config.js";
|
|
3
|
+
import { applyJsonEdits } from "./json-edits.js";
|
|
3
4
|
/** Text media above this decoded byte limit is offloaded; binary media always uses a digest. */
|
|
4
5
|
export const INLINE_FILE_LIMIT = 64 * 1024;
|
|
5
6
|
/** The longest message text, in UTF-16 code units, inspected for inline files. */
|
|
@@ -16,6 +17,78 @@ const messageKeys = new Set([
|
|
|
16
17
|
export function isMessageKey(key) {
|
|
17
18
|
return messageKeys.has(key);
|
|
18
19
|
}
|
|
20
|
+
/** Attributes whose value is JSON text by its convention, read for files anywhere in it: message
|
|
21
|
+
* sources (GenAI, AI SDK, OpenInference `input.value`/`output.value`, OpenLLMetry entities,
|
|
22
|
+
* Langfuse observations and traces) and tool arguments and results. */
|
|
23
|
+
const jsonMediaKeys = new Set([
|
|
24
|
+
"gen_ai.input.messages",
|
|
25
|
+
"gen_ai.output.messages",
|
|
26
|
+
"ai.prompt.messages",
|
|
27
|
+
"ai.prompt",
|
|
28
|
+
"input.value",
|
|
29
|
+
"output.value",
|
|
30
|
+
"traceloop.entity.input",
|
|
31
|
+
"traceloop.entity.output",
|
|
32
|
+
"langfuse.observation.input",
|
|
33
|
+
"langfuse.observation.output",
|
|
34
|
+
"langfuse.trace.input",
|
|
35
|
+
"langfuse.trace.output",
|
|
36
|
+
"gen_ai.tool.call.arguments",
|
|
37
|
+
"gen_ai.tool.call.result",
|
|
38
|
+
"ai.toolCall.args",
|
|
39
|
+
"ai.toolCall.result",
|
|
40
|
+
"tool.parameters",
|
|
41
|
+
"tool_arguments",
|
|
42
|
+
"tool_response",
|
|
43
|
+
]);
|
|
44
|
+
/** Recorded metadata: JSON text when it reads as an object or array, otherwise the text itself. */
|
|
45
|
+
const metadataKeys = new Set([
|
|
46
|
+
"metadata",
|
|
47
|
+
"langfuse.observation.metadata",
|
|
48
|
+
"langfuse.trace.metadata",
|
|
49
|
+
]);
|
|
50
|
+
const metadataPrefixes = [
|
|
51
|
+
"langfuse.observation.metadata.",
|
|
52
|
+
"langfuse.trace.metadata.",
|
|
53
|
+
"ai.telemetry.metadata.",
|
|
54
|
+
];
|
|
55
|
+
/** OpenLLMetry's indexed message content: the JSON text of a parts list. */
|
|
56
|
+
const indexedContentKey = /^gen_ai\.(?:prompt|completion)\.\d{1,9}\.content$/;
|
|
57
|
+
/** OpenInference's flattened image content: the image's URL. */
|
|
58
|
+
const openInferenceImageKey = /^llm\.(?:input|output)_messages\.\d{1,9}\.message\.contents\.\d{1,9}\.message_content\.image\.image\.url$/;
|
|
59
|
+
/** How an attribute's text is read for files, when it is read at all. */
|
|
60
|
+
export function readingOf(key) {
|
|
61
|
+
if (jsonMediaKeys.has(key))
|
|
62
|
+
return "json";
|
|
63
|
+
if (metadataKeys.has(key) || metadataPrefixes.some((prefix) => key.startsWith(prefix)))
|
|
64
|
+
return "metadata";
|
|
65
|
+
if (indexedContentKey.test(key))
|
|
66
|
+
return "parts";
|
|
67
|
+
if (openInferenceImageKey.test(key))
|
|
68
|
+
return "url";
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The attributes of a record that hold literal text: OpenInference's `input.value` and
|
|
73
|
+
* `output.value` when the record declares `input.mime_type` / `output.mime_type` as `text/plain`.
|
|
74
|
+
* Hue reads no JSON, and so no file, in them; neither does the SDK.
|
|
75
|
+
*/
|
|
76
|
+
export function literalKeys(attributes) {
|
|
77
|
+
const keys = new Set();
|
|
78
|
+
if (attributes === null || typeof attributes !== "object")
|
|
79
|
+
return keys;
|
|
80
|
+
for (const side of ["input", "output"]) {
|
|
81
|
+
const declared = Object.getOwnPropertyDescriptor(attributes, `${side}.mime_type`);
|
|
82
|
+
if (declared && "value" in declared && declared.value === "text/plain")
|
|
83
|
+
keys.add(`${side}.value`);
|
|
84
|
+
}
|
|
85
|
+
return keys;
|
|
86
|
+
}
|
|
87
|
+
/** Whether a span attribute is one whose value the SDK inspects for inline media: the attributes
|
|
88
|
+
* Hue's reader reads files from. */
|
|
89
|
+
export function isMediaKey(key) {
|
|
90
|
+
return readingOf(key) !== undefined;
|
|
91
|
+
}
|
|
19
92
|
/** Strict base64: alphabet characters only, padded to a multiple of four. */
|
|
20
93
|
const base64 = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
21
94
|
/** An RFC 2397 `data:` URL's header, up to the comma before its data. Its `;` parameters are read
|
|
@@ -67,6 +140,7 @@ export function inlineFileBytes(content) {
|
|
|
67
140
|
bytes: decoded ?? Buffer.from(content, "utf8"),
|
|
68
141
|
decoded: decoded !== undefined,
|
|
69
142
|
...(mediaType ? { urlMediaType: mediaType } : {}),
|
|
143
|
+
...(decoded !== undefined && !url ? { bareBase64: true } : {}),
|
|
70
144
|
};
|
|
71
145
|
}
|
|
72
146
|
const audioTypes = {
|
|
@@ -126,10 +200,13 @@ export function isTextMediaType(contentType) {
|
|
|
126
200
|
type === "application/x-javascript" ||
|
|
127
201
|
type === "application/x-www-form-urlencoded");
|
|
128
202
|
}
|
|
129
|
-
/** Cheap shared prefilter before bounded parsing of
|
|
203
|
+
/** Cheap shared prefilter before bounded parsing of the attributes inspected for media. */
|
|
130
204
|
export function mayInlineFiles(key, value) {
|
|
131
|
-
return (
|
|
132
|
-
|
|
205
|
+
return isMediaKey(key) && mayHoldMedia(value);
|
|
206
|
+
}
|
|
207
|
+
/** Whether text may hold inline media at all: cheap, before it is parsed or walked. */
|
|
208
|
+
export function mayHoldMedia(value) {
|
|
209
|
+
return (typeof value === "string" &&
|
|
133
210
|
[
|
|
134
211
|
"blob",
|
|
135
212
|
"file",
|
|
@@ -138,7 +215,8 @@ export function mayInlineFiles(key, value) {
|
|
|
138
215
|
"media",
|
|
139
216
|
"inlineData",
|
|
140
217
|
"inline_data",
|
|
141
|
-
"
|
|
218
|
+
"audio",
|
|
219
|
+
"data:",
|
|
142
220
|
"\\u",
|
|
143
221
|
].some((marker) => value.includes(marker)));
|
|
144
222
|
}
|
|
@@ -218,7 +296,7 @@ export function contentPath(part, pointer = "") {
|
|
|
218
296
|
name = undefined;
|
|
219
297
|
dataUrlOnly = true;
|
|
220
298
|
}
|
|
221
|
-
else if (part.type === "input_image") {
|
|
299
|
+
else if (part.type === "input_image" || part.type === "computer_screenshot") {
|
|
222
300
|
key = "image_url";
|
|
223
301
|
contentType = undefined;
|
|
224
302
|
name = undefined;
|
|
@@ -262,14 +340,254 @@ export function contentPath(part, pointer = "") {
|
|
|
262
340
|
...(encoded ? { base64: true } : {}),
|
|
263
341
|
};
|
|
264
342
|
}
|
|
265
|
-
/**
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
343
|
+
/** A media type as a `data:` URL declares one: `type/subtype`, no parameters. */
|
|
344
|
+
const declaredMediaType = /^[A-Za-z0-9][A-Za-z0-9!#$&^_.+-]*\/[A-Za-z0-9!#$&^_.+-]+$/;
|
|
345
|
+
/** The media type a whole string that is a `data:` URL declares, when it declares one: such a
|
|
346
|
+
* string is a file wherever it sits. A web URL, or any other text, is not. */
|
|
347
|
+
function dataUrlType(text) {
|
|
348
|
+
if (!text.startsWith("data:"))
|
|
349
|
+
return undefined;
|
|
350
|
+
const header = dataUrl.exec(text);
|
|
351
|
+
if (!header)
|
|
352
|
+
return undefined;
|
|
353
|
+
const type = header[1].split(";")[0].trim();
|
|
354
|
+
return declaredMediaType.test(type) && isMediaContentType(type) ? type : undefined;
|
|
355
|
+
}
|
|
356
|
+
/** The modality a media type implies, as Hue's reader names it. */
|
|
357
|
+
export function modalityOf(mediaType) {
|
|
358
|
+
const type = mediaType.split(";")[0].trim().toLowerCase();
|
|
359
|
+
if (type.startsWith("image/"))
|
|
360
|
+
return "image";
|
|
361
|
+
if (type.startsWith("audio/"))
|
|
362
|
+
return "audio";
|
|
363
|
+
if (type.startsWith("video/"))
|
|
364
|
+
return "video";
|
|
365
|
+
if (type.startsWith("text/") ||
|
|
366
|
+
type === "application/pdf" ||
|
|
367
|
+
type === "application/rtf" ||
|
|
368
|
+
type === "application/msword" ||
|
|
369
|
+
type === "application/vnd.ms-excel" ||
|
|
370
|
+
type === "application/vnd.ms-powerpoint" ||
|
|
371
|
+
type.startsWith("application/vnd.openxmlformats-officedocument."))
|
|
372
|
+
return "document";
|
|
373
|
+
return "file";
|
|
374
|
+
}
|
|
375
|
+
/** The media types OpenAI's audio `format` names; any other valid name is `audio/<name>`. */
|
|
376
|
+
const audioFormats = {
|
|
377
|
+
mp3: "audio/mpeg",
|
|
378
|
+
mpeg: "audio/mpeg",
|
|
379
|
+
mpga: "audio/mpeg",
|
|
380
|
+
wav: "audio/wav",
|
|
381
|
+
wave: "audio/wav",
|
|
382
|
+
ogg: "audio/ogg",
|
|
383
|
+
oga: "audio/ogg",
|
|
384
|
+
opus: "audio/opus",
|
|
385
|
+
flac: "audio/flac",
|
|
386
|
+
aac: "audio/aac",
|
|
387
|
+
m4a: "audio/mp4",
|
|
388
|
+
mp4: "audio/mp4",
|
|
389
|
+
webm: "audio/webm",
|
|
390
|
+
pcm16: "audio/pcm",
|
|
391
|
+
pcm: "audio/pcm",
|
|
392
|
+
};
|
|
393
|
+
function audioFormatMediaType(format) {
|
|
394
|
+
const name = format.trim().toLowerCase();
|
|
395
|
+
if (!/^[a-z0-9.+-]{1,32}$/.test(name))
|
|
396
|
+
return undefined;
|
|
397
|
+
return Object.hasOwn(audioFormats, name) ? audioFormats[name] : `audio/${name}`;
|
|
398
|
+
}
|
|
399
|
+
/** The media type the first bytes of base64 audio name by their container's signature (WAV,
|
|
400
|
+
* MP3, AAC, FLAC, Ogg): OpenAI's assistant audio keeps only the bytes, in the format the request
|
|
401
|
+
* asked for. Raw PCM has no signature and no type. */
|
|
402
|
+
function audioSignatureMediaType(base64) {
|
|
403
|
+
const head = Buffer.from(base64.slice(0, 16), "base64");
|
|
404
|
+
const text = (start, end) => head.subarray(start, end).toString("latin1");
|
|
405
|
+
if (text(0, 4) === "RIFF" && text(8, 12) === "WAVE")
|
|
406
|
+
return "audio/wav";
|
|
407
|
+
if (text(0, 3) === "ID3")
|
|
408
|
+
return "audio/mpeg";
|
|
409
|
+
if (text(0, 4) === "fLaC")
|
|
410
|
+
return "audio/flac";
|
|
411
|
+
if (text(0, 4) === "OggS")
|
|
412
|
+
return "audio/ogg";
|
|
413
|
+
// An MPEG frame's sync word: layer bits 00 are an AAC ADTS frame, any other an MP3 frame.
|
|
414
|
+
if (head.length >= 2 && head[0] === 0xff && (head[1] & 0xf0) === 0xf0)
|
|
415
|
+
return (head[1] & 0x06) === 0 ? "audio/aac" : "audio/mpeg";
|
|
416
|
+
if (head.length >= 2 && head[0] === 0xff && (head[1] & 0xe0) === 0xe0)
|
|
417
|
+
return "audio/mpeg";
|
|
418
|
+
return undefined;
|
|
419
|
+
}
|
|
420
|
+
/** A label as Hue's reader takes one: nonempty once trimmed, at most 256 code units. */
|
|
421
|
+
function readerLabel(value) {
|
|
422
|
+
if (typeof value !== "string" || value.length === 0)
|
|
423
|
+
return false;
|
|
424
|
+
const text = value.trim();
|
|
425
|
+
return text.length > 0 && text.length <= 256;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* The keys of a value Hue's reader reads as a file by its shape, where the SDK found no inline
|
|
429
|
+
* bytes to upload in it (a remote URL, a provider file id, an empty or malformed field): nothing
|
|
430
|
+
* inside them is read as another file, so nothing inside them is replaced either. Undefined for a
|
|
431
|
+
* value that is no file. Mirrors the reader's shapes (`mediaPartOf`).
|
|
432
|
+
*/
|
|
433
|
+
function readerConsumed(part) {
|
|
434
|
+
const type = typeof part.type === "string" ? part.type.toLowerCase().replaceAll("-", "_") : undefined;
|
|
435
|
+
const size = part.size ?? part.byte_size;
|
|
436
|
+
const digest = (typeof part.sha256 === "string" && /^[0-9a-f]{64}$/.test(part.sha256)) ||
|
|
437
|
+
(typeof size === "number" && Number.isSafeInteger(size) && size >= 0);
|
|
438
|
+
const identifies = (content, fileId) => (typeof content === "string" && content.length > 0) || readerLabel(fileId) || digest;
|
|
439
|
+
const anthropic = () => {
|
|
440
|
+
const source = object(part.source);
|
|
441
|
+
return source && source.type !== "content"
|
|
442
|
+
? ["type", "source", "title", "name", "filename", "cache_control"]
|
|
443
|
+
: undefined;
|
|
444
|
+
};
|
|
445
|
+
switch (type) {
|
|
446
|
+
case "blob":
|
|
447
|
+
return ["type", "content", "mime_type", "modality", "name", "filename", "transcript"];
|
|
448
|
+
case "uri":
|
|
449
|
+
return ["type", "uri", "mime_type", "modality", "name", "filename", "transcript"];
|
|
450
|
+
case "file": {
|
|
451
|
+
const file = object(part.file);
|
|
452
|
+
if (file)
|
|
453
|
+
return identifies(file.file_data, file.file_id) ? ["type", "file"] : undefined;
|
|
454
|
+
if (typeof part.file_id === "string" || typeof part.modality === "string")
|
|
455
|
+
return identifies(undefined, part.file_id)
|
|
456
|
+
? ["type", "file_id", "mime_type", "modality", "name", "filename"]
|
|
457
|
+
: undefined;
|
|
458
|
+
return identifies(part.data)
|
|
459
|
+
? ["type", "data", "mediaType", "mimeType", "filename", "name", "providerOptions"]
|
|
460
|
+
: undefined;
|
|
461
|
+
}
|
|
462
|
+
case "image":
|
|
463
|
+
if (object(part.source))
|
|
464
|
+
return anthropic();
|
|
465
|
+
return typeof part.image === "string" || digest
|
|
466
|
+
? ["type", "image", "mediaType", "mimeType", "filename", "name", "providerOptions"]
|
|
467
|
+
: undefined;
|
|
468
|
+
case "document":
|
|
469
|
+
return anthropic();
|
|
470
|
+
case "image_url":
|
|
471
|
+
return ["type", "image_url"];
|
|
472
|
+
case "input_image":
|
|
473
|
+
return ["type", "image_url", "file_id", "detail"];
|
|
474
|
+
case "input_audio":
|
|
475
|
+
return ["type", "input_audio", "data", "format", "transcript"];
|
|
476
|
+
case "input_file":
|
|
477
|
+
return ["type", "file_data", "file_url", "file_id", "filename", "name"];
|
|
478
|
+
case "computer_screenshot":
|
|
479
|
+
return ["type", "image_url", "file_id"];
|
|
480
|
+
case "media":
|
|
481
|
+
return ["type", "data", "mime_type", "file_uri", "fileUri", "name", "filename"];
|
|
482
|
+
case undefined: {
|
|
483
|
+
for (const key of ["inlineData", "inline_data", "fileData", "file_data"])
|
|
484
|
+
if (object(part[key]))
|
|
485
|
+
return [key];
|
|
486
|
+
for (const key of ["image", "document"])
|
|
487
|
+
if (object(object(part[key])?.source))
|
|
488
|
+
return [key];
|
|
489
|
+
const audio = object(part.audio);
|
|
490
|
+
if (audio &&
|
|
491
|
+
typeof audio.id === "string" &&
|
|
492
|
+
(typeof audio.data === "string" ||
|
|
493
|
+
typeof audio.transcript === "string" ||
|
|
494
|
+
typeof audio.expires_at === "number"))
|
|
495
|
+
return ["audio"];
|
|
496
|
+
return undefined;
|
|
497
|
+
}
|
|
498
|
+
default:
|
|
499
|
+
return undefined;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/** The keys of a part whose string value is JSON by its convention, read as the value it encodes:
|
|
503
|
+
* a tool call's `arguments`, a tool result, and a `tool` or `function` message's `content`. A
|
|
504
|
+
* message's own text is never read as JSON. */
|
|
505
|
+
function encodedJsonKeys(item) {
|
|
506
|
+
if (item.role === "tool" || item.role === "function")
|
|
507
|
+
return ["content"];
|
|
508
|
+
const type = typeof item.type === "string" ? item.type.toLowerCase().replaceAll("-", "_") : undefined;
|
|
509
|
+
if (type === "tool_call" || type === "function_call")
|
|
510
|
+
return ["arguments"];
|
|
511
|
+
if (type === "tool_result" || type === "function_call_output" || type === "tool_call_response")
|
|
512
|
+
return ["output", "result", "content", "response"];
|
|
513
|
+
if (typeof item.name === "string" && Object.hasOwn(item, "arguments"))
|
|
514
|
+
return ["arguments"];
|
|
515
|
+
return [];
|
|
516
|
+
}
|
|
517
|
+
/** The object or array a JSON-encoded string holds; undefined for any other string. */
|
|
518
|
+
function encodedJsonValue(text) {
|
|
519
|
+
if (!/^\s*[[{]/.test(text))
|
|
520
|
+
return undefined;
|
|
521
|
+
try {
|
|
522
|
+
const parsed = JSON.parse(text);
|
|
523
|
+
return parsed !== null && typeof parsed === "object" ? parsed : undefined;
|
|
524
|
+
}
|
|
525
|
+
catch {
|
|
526
|
+
return undefined;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
/** How deep Hue's reader looks for files in a recorded value: a part more than ten levels below
|
|
530
|
+
* the value's root (an encoded string's JSON counting as a level) is not read as a file, so it is
|
|
531
|
+
* not inspected either. */
|
|
532
|
+
export const MAX_MEDIA_DEPTH = 10;
|
|
533
|
+
/**
|
|
534
|
+
* Whether Hue's reader reads JSON text as the value it encodes, and so reads files in it: nested
|
|
535
|
+
* at most 32 levels, with no number JavaScript cannot hold exactly (an integer past 2^53, or one
|
|
536
|
+
* past the double range). Hue keeps any other text as it is, so the SDK leaves it as recorded.
|
|
537
|
+
*/
|
|
538
|
+
export function readerParses(value) {
|
|
539
|
+
const stack = [[value, 0]];
|
|
540
|
+
while (stack.length) {
|
|
541
|
+
const [item, depth] = stack.pop();
|
|
542
|
+
if (depth > 32)
|
|
543
|
+
return false;
|
|
544
|
+
if (typeof item === "number" &&
|
|
545
|
+
(!Number.isFinite(item) || (Number.isInteger(item) && !Number.isSafeInteger(item))))
|
|
546
|
+
return false;
|
|
547
|
+
if (item !== null && typeof item === "object")
|
|
548
|
+
for (const child of Object.values(item))
|
|
549
|
+
stack.push([child, depth + 1]);
|
|
550
|
+
}
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Every file a recorded value carries for upload, in reading order, as Hue's reader finds them
|
|
555
|
+
* (`forEachMediaPart`): a provider part with inline bytes, an OpenAI assistant message's audio,
|
|
556
|
+
* a whole string that is a typed `data:` URL, and the files inside a tool call's or tool result's
|
|
557
|
+
* JSON-encoded string. A part whose bytes are not inline (a remote URL, a file id) is left whole.
|
|
558
|
+
* The caller applies the decoded text threshold.
|
|
559
|
+
*/
|
|
560
|
+
export function mediaIn(value, source) {
|
|
561
|
+
const box = { value };
|
|
562
|
+
const files = [];
|
|
563
|
+
const encoded = [];
|
|
564
|
+
const document = { at: "", edits: [] };
|
|
565
|
+
const visit = (node, pointer, depth, owner, key, within) => {
|
|
566
|
+
if (depth > MAX_MEDIA_DEPTH)
|
|
567
|
+
return;
|
|
568
|
+
const add = (file) => files.push({
|
|
569
|
+
...file,
|
|
570
|
+
...(within.length ? { encoded: within } : {}),
|
|
571
|
+
document: within.at(-1) ?? document,
|
|
572
|
+
});
|
|
573
|
+
if (typeof node === "string") {
|
|
574
|
+
const type = dataUrlType(node);
|
|
575
|
+
if (type !== undefined)
|
|
576
|
+
add({
|
|
577
|
+
part: {},
|
|
578
|
+
owner,
|
|
579
|
+
key,
|
|
580
|
+
content: node,
|
|
581
|
+
pointer,
|
|
582
|
+
partPointer: pointer,
|
|
583
|
+
contentType: type,
|
|
584
|
+
whole: true,
|
|
585
|
+
});
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
271
588
|
if (Array.isArray(node)) {
|
|
272
|
-
|
|
589
|
+
for (let index = 0; index < node.length; index++)
|
|
590
|
+
visit(node[index], `${pointer}/${index}`, depth + 1, node, index, within);
|
|
273
591
|
return;
|
|
274
592
|
}
|
|
275
593
|
const part = object(node);
|
|
@@ -277,22 +595,137 @@ export function largeInlineFileParts(value) {
|
|
|
277
595
|
return;
|
|
278
596
|
const file = contentPath(part, pointer);
|
|
279
597
|
if (file) {
|
|
280
|
-
|
|
598
|
+
add(file);
|
|
281
599
|
return;
|
|
282
600
|
}
|
|
283
|
-
|
|
284
|
-
|
|
601
|
+
const consumed = readerConsumed(part);
|
|
602
|
+
// An OpenAI Chat assistant message's spoken reply: the message is the part.
|
|
603
|
+
const audio = consumed?.length === 1 && consumed[0] === "audio" ? object(part.audio) : undefined;
|
|
604
|
+
if (audio &&
|
|
605
|
+
typeof audio.data === "string" &&
|
|
606
|
+
audio.data.length % 4 === 0 &&
|
|
607
|
+
base64.test(audio.data)) {
|
|
608
|
+
const format = typeof audio.format === "string" && audio.format ? audio.format : undefined;
|
|
609
|
+
const contentType = format
|
|
610
|
+
? audioFormatMediaType(format)
|
|
611
|
+
: audioSignatureMediaType(audio.data);
|
|
612
|
+
add({
|
|
613
|
+
part,
|
|
614
|
+
owner: audio,
|
|
615
|
+
key: "data",
|
|
616
|
+
content: audio.data,
|
|
617
|
+
pointer: `${pointer}/audio/data`,
|
|
618
|
+
partPointer: pointer,
|
|
619
|
+
...(contentType ? { contentType } : {}),
|
|
620
|
+
base64: true,
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
const encodedKeys = encodedJsonKeys(part);
|
|
624
|
+
for (const [name, item] of Object.entries(part)) {
|
|
625
|
+
if (consumed?.includes(name))
|
|
626
|
+
continue;
|
|
627
|
+
const at = `${pointer}/${pointerSegment(name)}`;
|
|
628
|
+
const inner = typeof item === "string" && encodedKeys.includes(name) ? encodedJsonValue(item) : undefined;
|
|
629
|
+
if (inner !== undefined) {
|
|
630
|
+
const holder = {
|
|
631
|
+
owner: part,
|
|
632
|
+
key: name,
|
|
633
|
+
value: inner,
|
|
634
|
+
dirty: false,
|
|
635
|
+
text: item,
|
|
636
|
+
at,
|
|
637
|
+
edits: [],
|
|
638
|
+
parent: within.at(-1) ?? document,
|
|
639
|
+
};
|
|
640
|
+
const before = files.length;
|
|
641
|
+
visit(inner, at, depth + 1, holder, "value", [
|
|
642
|
+
...within,
|
|
643
|
+
holder,
|
|
644
|
+
]);
|
|
645
|
+
if (files.length > before)
|
|
646
|
+
encoded.push(holder);
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
visit(item, at, depth + 1, part, name, within);
|
|
650
|
+
}
|
|
285
651
|
};
|
|
286
|
-
visit(value, "", 0);
|
|
287
|
-
return
|
|
652
|
+
visit(value, "", 0, box, "value", []);
|
|
653
|
+
return { box, files, encoded, document, ...(source !== undefined ? { source } : {}) };
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* An attribute's recorded text read for files as Hue's reader reads that attribute: JSON by its
|
|
657
|
+
* convention; metadata as JSON when it reads as an object or array, otherwise as the text itself;
|
|
658
|
+
* an OpenLLMetry content as the parts list it encodes (any other content stays text); an
|
|
659
|
+
* OpenInference image URL as the text itself. Undefined for text that reading finds nothing in.
|
|
660
|
+
* Throws for JSON that does not parse or nests too deep.
|
|
661
|
+
*/
|
|
662
|
+
export function readMediaText(key, text) {
|
|
663
|
+
/** JSON text read as Hue's reader reads it: text it keeps as text holds no file. */
|
|
664
|
+
const json = (parsed) => (readerParses(parsed) ? mediaIn(parsed, text) : undefined);
|
|
665
|
+
switch (readingOf(key)) {
|
|
666
|
+
case "json":
|
|
667
|
+
return json(JSON.parse(text));
|
|
668
|
+
case "metadata":
|
|
669
|
+
return /^\s*[[{]/.test(text) ? json(JSON.parse(text)) : mediaIn(text);
|
|
670
|
+
case "parts": {
|
|
671
|
+
if (!/^\s*\[/.test(text))
|
|
672
|
+
return undefined;
|
|
673
|
+
const parsed = JSON.parse(text);
|
|
674
|
+
return Array.isArray(parsed) &&
|
|
675
|
+
parsed.length > 0 &&
|
|
676
|
+
parsed.every((part) => typeof object(part)?.type === "string")
|
|
677
|
+
? json(parsed)
|
|
678
|
+
: undefined;
|
|
679
|
+
}
|
|
680
|
+
case "url":
|
|
681
|
+
return mediaIn(text);
|
|
682
|
+
default:
|
|
683
|
+
return undefined;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
/** The recorded value's text once its files are placed: its JSON text as recorded with only the
|
|
687
|
+
* placed files' values (or their parts' members) replaced, each JSON-encoded string a placed file
|
|
688
|
+
* sits in written again the same way, innermost first. */
|
|
689
|
+
export function placedMediaText(media) {
|
|
690
|
+
reencodeMedia(media);
|
|
691
|
+
return media.source !== undefined
|
|
692
|
+
? applyJsonEdits(media.source, media.document.edits)
|
|
693
|
+
: JSON.stringify(media.box.value);
|
|
694
|
+
}
|
|
695
|
+
/** Each JSON-encoded string a placed file sits in, written again, innermost first: the string as
|
|
696
|
+
* recorded with only its placed files changed. */
|
|
697
|
+
export function reencodeMedia(media) {
|
|
698
|
+
for (const holder of media.encoded) {
|
|
699
|
+
if (!holder.dirty)
|
|
700
|
+
continue;
|
|
701
|
+
const text = applyJsonEdits(holder.text, holder.edits);
|
|
702
|
+
holder.owner[holder.key] = text;
|
|
703
|
+
holder.dirty = false;
|
|
704
|
+
holder.parent.edits.push({
|
|
705
|
+
pointer: holder.at.slice(holder.parent.at.length),
|
|
706
|
+
value: JSON.stringify(text),
|
|
707
|
+
});
|
|
708
|
+
}
|
|
288
709
|
}
|
|
710
|
+
/** Whether a found file has bytes to place: nonempty, and a whole string's `data:` URL decodes. */
|
|
711
|
+
export function placeable(file, decoded) {
|
|
712
|
+
return decoded.bytes.byteLength > 0 && (!file.whole || decoded.decoded);
|
|
713
|
+
}
|
|
714
|
+
/** Whether an uploaded file keeps a 16 KiB prefix of its content in place (text media in a
|
|
715
|
+
* provider part, whose entry names the content) rather than its digest. A whole string, a file
|
|
716
|
+
* inside a JSON-encoded string, and a text file a part holds as bare base64 (a GenAI `blob`,
|
|
717
|
+
* Vertex `media` or AI SDK part, whose content is not the text itself) take the digest form. */
|
|
718
|
+
export function keepsPrefix(file, textual, decoded) {
|
|
719
|
+
return textual && !file.whole && !file.encoded?.length && !decoded.bareBase64;
|
|
720
|
+
}
|
|
721
|
+
/** A file's bytes: a field its convention declares base64 decoded, any other content read as
|
|
722
|
+
* Hue's reader reads it, whatever the part's media type (`inlineFileBytes`): a `data:` URL by its
|
|
723
|
+
* own encoding, text that reads as base64 decoded (what the GenAI conventions, and
|
|
724
|
+
* `client.media.resolve`, write in a `blob` or `media` part for every media type), anything else
|
|
725
|
+
* as its UTF-8. */
|
|
289
726
|
export function mediaBytes(file) {
|
|
290
727
|
if (file.base64)
|
|
291
728
|
return { bytes: Buffer.from(file.content, "base64"), decoded: true };
|
|
292
|
-
if (isMediaContentType(file.contentType) &&
|
|
293
|
-
isTextMediaType(file.contentType) &&
|
|
294
|
-
!file.content.startsWith("data:"))
|
|
295
|
-
return { bytes: Buffer.from(file.content, "utf8"), decoded: false };
|
|
296
729
|
return inlineFileBytes(file.content);
|
|
297
730
|
}
|
|
298
731
|
export function mediaContentType(file, decoded) {
|
|
@@ -333,18 +766,124 @@ export class MediaDigestMemo {
|
|
|
333
766
|
return digest;
|
|
334
767
|
}
|
|
335
768
|
}
|
|
336
|
-
/**
|
|
769
|
+
/** The GenAI `blob` part in digest form a whole-string file is replaced by. */
|
|
770
|
+
function blobPart(contentType, digest) {
|
|
771
|
+
return {
|
|
772
|
+
type: "blob",
|
|
773
|
+
mime_type: contentType,
|
|
774
|
+
modality: modalityOf(contentType),
|
|
775
|
+
sha256: digest.sha256,
|
|
776
|
+
size: digest.size,
|
|
777
|
+
};
|
|
778
|
+
}
|
|
779
|
+
/** Digest placement removes only the leaf and adds identity to the provider part (the message,
|
|
780
|
+
* for OpenAI assistant audio); a whole string becomes a GenAI `blob` part in digest form. */
|
|
337
781
|
export function digestPart(file, digest) {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
782
|
+
const owner = file.owner;
|
|
783
|
+
const document = file.document;
|
|
784
|
+
const at = (pointer) => pointer.slice(document.at.length);
|
|
785
|
+
if (file.whole) {
|
|
786
|
+
const part = blobPart(file.contentType, digest);
|
|
787
|
+
owner[file.key] = part;
|
|
788
|
+
document?.edits.push({ pointer: at(file.pointer), value: JSON.stringify(part) });
|
|
789
|
+
}
|
|
790
|
+
else {
|
|
791
|
+
delete owner[file.key];
|
|
792
|
+
file.part.sha256 = digest.sha256;
|
|
793
|
+
file.part.size = digest.size;
|
|
794
|
+
if (document) {
|
|
795
|
+
const set = [
|
|
796
|
+
["sha256", JSON.stringify(digest.sha256)],
|
|
797
|
+
["size", String(digest.size)],
|
|
798
|
+
];
|
|
799
|
+
const ownerPointer = file.pointer.slice(0, file.pointer.lastIndexOf("/"));
|
|
800
|
+
if (ownerPointer === file.partPointer)
|
|
801
|
+
document.edits.push({ pointer: at(ownerPointer), remove: String(file.key), set });
|
|
802
|
+
else {
|
|
803
|
+
document.edits.push({ pointer: at(ownerPointer), remove: String(file.key) });
|
|
804
|
+
document.edits.push({ pointer: at(file.partPointer), set });
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
for (const holder of file.encoded ?? [])
|
|
809
|
+
holder.dirty = true;
|
|
341
810
|
}
|
|
342
|
-
|
|
343
|
-
|
|
811
|
+
/** Prefix placement keeps the first 16 KiB of a text file's content in place. */
|
|
812
|
+
export function prefixPart(file, prefix) {
|
|
813
|
+
file.owner[file.key] = prefix;
|
|
814
|
+
file.document?.edits.push({
|
|
815
|
+
pointer: file.pointer.slice(file.document.at.length),
|
|
816
|
+
value: JSON.stringify(prefix),
|
|
817
|
+
});
|
|
818
|
+
for (const holder of file.encoded ?? [])
|
|
819
|
+
holder.dirty = true;
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* A span's own attribute with its inline media replaced by their digest, as export places media
|
|
823
|
+
* it does not upload. Only the recorded message attributes (`isMessageKey`) shrink without an
|
|
824
|
+
* upload; every other inspected attribute keeps its bytes inline unless they upload, so `anyKey`
|
|
825
|
+
* asks for the digest form of any inspected attribute (what the queue is charged for a value it
|
|
826
|
+
* holds whole for upload).
|
|
827
|
+
*/
|
|
828
|
+
export function hashInlineFiles(key, value, count, memo, traceId = "", anyKey = false) {
|
|
829
|
+
return anyKey || isMessageKey(key) ? hashFiles(key, value, false, count, memo, traceId) : value;
|
|
344
830
|
}
|
|
345
831
|
/** Nested message attributes retain small media and digest larger media without upload state. */
|
|
346
|
-
export function hashNestedInlineFiles(key, value) {
|
|
347
|
-
return hashFiles(key, value, true);
|
|
832
|
+
export function hashNestedInlineFiles(key, value, count) {
|
|
833
|
+
return isMessageKey(key) ? hashFiles(key, value, true, count) : value;
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* Each file a recorded value holds replaced by its digest, as export places a file it does not
|
|
837
|
+
* upload: binary media of any size (`nested`: over the inline limit) and text media over the
|
|
838
|
+
* inline limit. Whether any was replaced; `count` counts them.
|
|
839
|
+
*/
|
|
840
|
+
export function digestMedia(media, nested, count, memo, traceId = "") {
|
|
841
|
+
let changed = false;
|
|
842
|
+
for (const file of media.files) {
|
|
843
|
+
const decoded = mediaBytes(file);
|
|
844
|
+
if (!placeable(file, decoded))
|
|
845
|
+
continue;
|
|
846
|
+
if ((nested || isTextMediaType(mediaContentType(file, decoded))) &&
|
|
847
|
+
decoded.bytes.byteLength <= INLINE_FILE_LIMIT)
|
|
848
|
+
continue;
|
|
849
|
+
const digest = memo
|
|
850
|
+
? memo.digest(file, decoded, traceId)
|
|
851
|
+
: {
|
|
852
|
+
sha256: createHash("sha256").update(decoded.bytes).digest("hex"),
|
|
853
|
+
size: decoded.bytes.byteLength,
|
|
854
|
+
};
|
|
855
|
+
digestPart(file, digest);
|
|
856
|
+
changed = true;
|
|
857
|
+
if (count)
|
|
858
|
+
count.files++;
|
|
859
|
+
}
|
|
860
|
+
if (changed)
|
|
861
|
+
reencodeMedia(media);
|
|
862
|
+
return changed;
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* The UTF-8 size of a structured value's JSON once each file the SDK may upload from it, within
|
|
866
|
+
* the inspection limits, is placed in its digest form: whether a log body over the value cap fits
|
|
867
|
+
* it once its media upload. Mutates `value`, a copy the caller owns.
|
|
868
|
+
*/
|
|
869
|
+
export function digestFormBytes(value) {
|
|
870
|
+
const media = mediaIn(value);
|
|
871
|
+
let inspected = 0;
|
|
872
|
+
for (const file of media.files) {
|
|
873
|
+
const units = file.content.length;
|
|
874
|
+
if (units > MAX_INLINE_FILE_TEXT || inspected + units > INLINE_FILE_TEXT_PER_RECORD)
|
|
875
|
+
continue;
|
|
876
|
+
inspected += units;
|
|
877
|
+
const decoded = mediaBytes(file);
|
|
878
|
+
if (!placeable(file, decoded))
|
|
879
|
+
continue;
|
|
880
|
+
if (isTextMediaType(mediaContentType(file, decoded)) &&
|
|
881
|
+
decoded.bytes.byteLength <= INLINE_FILE_LIMIT)
|
|
882
|
+
continue;
|
|
883
|
+
digestPart(file, { sha256: "0".repeat(64), size: decoded.bytes.byteLength });
|
|
884
|
+
}
|
|
885
|
+
reencodeMedia(media);
|
|
886
|
+
return Buffer.byteLength(JSON.stringify(media.box.value));
|
|
348
887
|
}
|
|
349
888
|
function hashFiles(key, value, nested, count, memo, traceId = "") {
|
|
350
889
|
if (typeof value !== "string" ||
|
|
@@ -352,28 +891,10 @@ function hashFiles(key, value, nested, count, memo, traceId = "") {
|
|
|
352
891
|
value.length > MAX_INLINE_FILE_TEXT)
|
|
353
892
|
return value;
|
|
354
893
|
try {
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
const decoded = mediaBytes(file);
|
|
360
|
-
if (!decoded.bytes.byteLength)
|
|
361
|
-
continue;
|
|
362
|
-
if ((nested || isTextMediaType(mediaContentType(file, decoded))) &&
|
|
363
|
-
decoded.bytes.byteLength <= INLINE_FILE_LIMIT)
|
|
364
|
-
continue;
|
|
365
|
-
const digest = memo
|
|
366
|
-
? memo.digest(file, decoded, traceId)
|
|
367
|
-
: {
|
|
368
|
-
sha256: createHash("sha256").update(decoded.bytes).digest("hex"),
|
|
369
|
-
size: decoded.bytes.byteLength,
|
|
370
|
-
};
|
|
371
|
-
digestPart(file, digest);
|
|
372
|
-
changed = true;
|
|
373
|
-
if (count)
|
|
374
|
-
count.files++;
|
|
375
|
-
}
|
|
376
|
-
return changed ? JSON.stringify(parsed) : value;
|
|
894
|
+
const media = readMediaText(key, value);
|
|
895
|
+
if (!media || !digestMedia(media, nested, count, memo, traceId))
|
|
896
|
+
return value;
|
|
897
|
+
return placedMediaText(media);
|
|
377
898
|
}
|
|
378
899
|
catch {
|
|
379
900
|
return value;
|