@bli-cockpit/telemetry-core 0.1.70 → 0.1.72
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/image-descriptions.d.ts +140 -0
- package/dist/image-descriptions.js +134 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/ingest-dto.d.ts +1 -1
- package/dist/transcript-images.d.ts +31 -0
- package/dist/transcript-images.js +102 -0
- package/dist/work-context.d.ts +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Images inside collected transcripts: the marker, the one switch, and the door's wire shapes
|
|
3
|
+
* (BLI-16977).
|
|
4
|
+
*
|
|
5
|
+
* A Claude or Codex transcript stores each pasted screenshot as a base64 string inside the JSONL
|
|
6
|
+
* line. Measured on one laptop on 2026-10-09 that was 17% of Claude bytes and 59% of Codex bytes.
|
|
7
|
+
* The collector finds every such image, sends each distinct one ONCE to the describe door (a
|
|
8
|
+
* short text description, kept against the image's sha256), and, only when the switch below says
|
|
9
|
+
* so, replaces the base64 in the uploaded transcript with the short marker built here and strips
|
|
10
|
+
* the transcript's duplicate copies and bookkeeping (the slim transcript).
|
|
11
|
+
*
|
|
12
|
+
* This file is shared by the collector (builds the marker, builds the request), the dashboard
|
|
13
|
+
* (validates the request, answers) and anything that reads a marker back. Pure: no I/O, no env.
|
|
14
|
+
*/
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
/**
|
|
17
|
+
* `describe_and_keep` every distinct image is described, the transcript still carries the image and
|
|
18
|
+
* is uploaded exactly as it is on disk. The slim transcript is NOT computed
|
|
19
|
+
* and NOT uploaded in this mode.
|
|
20
|
+
* `describe_and_drop` every image that has a stored description is replaced by its marker (an image
|
|
21
|
+
* that could not be described is KEPT, never dropped without a description),
|
|
22
|
+
* and the uploaded transcript is the SLIM transcript: the duplicate copies and
|
|
23
|
+
* the bookkeeping are stripped too, every line stays. What is stripped and
|
|
24
|
+
* kept: docs/architecture/image-descriptions.md, code in
|
|
25
|
+
* packages/cockpit-local-collector/src/slim/.
|
|
26
|
+
*/
|
|
27
|
+
export type ImageHandlingMode = "describe_and_keep" | "describe_and_drop";
|
|
28
|
+
/**
|
|
29
|
+
* The one line Edward changes to start uploading slim transcripts (images described and dropped,
|
|
30
|
+
* duplicate copies and bookkeeping stripped). Default is keep. The collector reads it at run time,
|
|
31
|
+
* so flipping it needs a CLI release (`gh workflow run release.yml -f tag=latest`) and then
|
|
32
|
+
* `cockpit settings cli-floor <version>`; no other file changes.
|
|
33
|
+
*/
|
|
34
|
+
export declare const IMAGE_HANDLING_MODE: ImageHandlingMode;
|
|
35
|
+
/**
|
|
36
|
+
* Largest decoded image the describe door takes. The door is a serverless function with a 4.5 MB
|
|
37
|
+
* request body ceiling and base64 inflates by a third, so 3 MB is the biggest image that fits with
|
|
38
|
+
* room for the JSON around it. A larger image is kept as it is and counted (`image_too_large`).
|
|
39
|
+
*/
|
|
40
|
+
export declare const IMAGE_DESCRIBE_MAX_BYTES = 3000000;
|
|
41
|
+
/** A base64 run shorter than this is not a screenshot (an icon, a hash, a token). */
|
|
42
|
+
export declare const IMAGE_MIN_BASE64_CHARS = 500;
|
|
43
|
+
/** The longest description the door stores. About 300 tokens is 1,200 characters. */
|
|
44
|
+
export declare const IMAGE_DESCRIPTION_MAX_CHARS = 1600;
|
|
45
|
+
export declare const IMAGE_MEDIA_TYPES: readonly ["image/png", "image/jpeg", "image/gif", "image/webp"];
|
|
46
|
+
export type ImageMediaType = (typeof IMAGE_MEDIA_TYPES)[number];
|
|
47
|
+
/** What the bytes are, from their first bytes. A declared type in the transcript is never trusted. */
|
|
48
|
+
export declare function sniffImageMediaType(bytes: Uint8Array): ImageMediaType | null;
|
|
49
|
+
/** Hex characters of the sha256 a marker carries. 64 bits is far past any one person's image count. */
|
|
50
|
+
export declare const IMAGE_MARKER_HASH_CHARS = 16;
|
|
51
|
+
export declare function imageSizeLabel(byteSize: number): string;
|
|
52
|
+
/** `[image sha256:ab12cd34ef567890… png 412KB]`: what replaces the base64 in an uploaded transcript. */
|
|
53
|
+
export declare function formatImageMarker(image: {
|
|
54
|
+
sha256: string;
|
|
55
|
+
mediaType: ImageMediaType;
|
|
56
|
+
byteSize: number;
|
|
57
|
+
}): string;
|
|
58
|
+
export interface ParsedImageMarker {
|
|
59
|
+
sha256Prefix: string;
|
|
60
|
+
mediaType: ImageMediaType;
|
|
61
|
+
sizeLabel: string;
|
|
62
|
+
}
|
|
63
|
+
/** Every marker in a piece of text, in order. A fresh regex per call, so no shared `lastIndex`. */
|
|
64
|
+
export declare function parseImageMarkers(text: string): ParsedImageMarker[];
|
|
65
|
+
export declare const ImageDescribeRequestSchema: z.ZodObject<{
|
|
66
|
+
schema_version: z.ZodLiteral<"image-describe.v1">;
|
|
67
|
+
sha256: z.ZodString;
|
|
68
|
+
media_type: z.ZodEnum<{
|
|
69
|
+
"image/png": "image/png";
|
|
70
|
+
"image/jpeg": "image/jpeg";
|
|
71
|
+
"image/gif": "image/gif";
|
|
72
|
+
"image/webp": "image/webp";
|
|
73
|
+
}>;
|
|
74
|
+
byte_size: z.ZodNumber;
|
|
75
|
+
content_base64: z.ZodString;
|
|
76
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
77
|
+
codex: "codex";
|
|
78
|
+
claude_code: "claude_code";
|
|
79
|
+
manual: "manual";
|
|
80
|
+
}>>;
|
|
81
|
+
session_id: z.ZodOptional<z.ZodString>;
|
|
82
|
+
}, z.core.$strict>;
|
|
83
|
+
export type ImageDescribeRequest = z.infer<typeof ImageDescribeRequestSchema>;
|
|
84
|
+
export declare const ImageDescriptionSchema: z.ZodObject<{
|
|
85
|
+
sha256: z.ZodString;
|
|
86
|
+
media_type: z.ZodEnum<{
|
|
87
|
+
"image/png": "image/png";
|
|
88
|
+
"image/jpeg": "image/jpeg";
|
|
89
|
+
"image/gif": "image/gif";
|
|
90
|
+
"image/webp": "image/webp";
|
|
91
|
+
}>;
|
|
92
|
+
byte_size: z.ZodNumber;
|
|
93
|
+
description: z.ZodString;
|
|
94
|
+
secrets_masked: z.ZodBoolean;
|
|
95
|
+
model: z.ZodString;
|
|
96
|
+
created_at: z.ZodString;
|
|
97
|
+
}, z.core.$strict>;
|
|
98
|
+
export type ImageDescription = z.infer<typeof ImageDescriptionSchema>;
|
|
99
|
+
export declare const ImageDescribeResponseSchema: z.ZodObject<{
|
|
100
|
+
ok: z.ZodLiteral<true>;
|
|
101
|
+
status: z.ZodEnum<{
|
|
102
|
+
described: "described";
|
|
103
|
+
known: "known";
|
|
104
|
+
}>;
|
|
105
|
+
description: z.ZodObject<{
|
|
106
|
+
sha256: z.ZodString;
|
|
107
|
+
media_type: z.ZodEnum<{
|
|
108
|
+
"image/png": "image/png";
|
|
109
|
+
"image/jpeg": "image/jpeg";
|
|
110
|
+
"image/gif": "image/gif";
|
|
111
|
+
"image/webp": "image/webp";
|
|
112
|
+
}>;
|
|
113
|
+
byte_size: z.ZodNumber;
|
|
114
|
+
description: z.ZodString;
|
|
115
|
+
secrets_masked: z.ZodBoolean;
|
|
116
|
+
model: z.ZodString;
|
|
117
|
+
created_at: z.ZodString;
|
|
118
|
+
}, z.core.$strict>;
|
|
119
|
+
}, z.core.$strict>;
|
|
120
|
+
export type ImageDescribeResponse = z.infer<typeof ImageDescribeResponseSchema>;
|
|
121
|
+
export declare const ImageReadResponseSchema: z.ZodObject<{
|
|
122
|
+
ok: z.ZodLiteral<true>;
|
|
123
|
+
descriptions: z.ZodArray<z.ZodObject<{
|
|
124
|
+
sha256: z.ZodString;
|
|
125
|
+
media_type: z.ZodEnum<{
|
|
126
|
+
"image/png": "image/png";
|
|
127
|
+
"image/jpeg": "image/jpeg";
|
|
128
|
+
"image/gif": "image/gif";
|
|
129
|
+
"image/webp": "image/webp";
|
|
130
|
+
}>;
|
|
131
|
+
byte_size: z.ZodNumber;
|
|
132
|
+
description: z.ZodString;
|
|
133
|
+
secrets_masked: z.ZodBoolean;
|
|
134
|
+
model: z.ZodString;
|
|
135
|
+
created_at: z.ZodString;
|
|
136
|
+
}, z.core.$strict>>;
|
|
137
|
+
}, z.core.$strict>;
|
|
138
|
+
export type ImageReadResponse = z.infer<typeof ImageReadResponseSchema>;
|
|
139
|
+
/** A sha256 or a prefix of one (the 16 characters a marker carries). */
|
|
140
|
+
export declare const ImageHashQuerySchema: z.ZodString;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Images inside collected transcripts: the marker, the one switch, and the door's wire shapes
|
|
3
|
+
* (BLI-16977).
|
|
4
|
+
*
|
|
5
|
+
* A Claude or Codex transcript stores each pasted screenshot as a base64 string inside the JSONL
|
|
6
|
+
* line. Measured on one laptop on 2026-10-09 that was 17% of Claude bytes and 59% of Codex bytes.
|
|
7
|
+
* The collector finds every such image, sends each distinct one ONCE to the describe door (a
|
|
8
|
+
* short text description, kept against the image's sha256), and, only when the switch below says
|
|
9
|
+
* so, replaces the base64 in the uploaded transcript with the short marker built here and strips
|
|
10
|
+
* the transcript's duplicate copies and bookkeeping (the slim transcript).
|
|
11
|
+
*
|
|
12
|
+
* This file is shared by the collector (builds the marker, builds the request), the dashboard
|
|
13
|
+
* (validates the request, answers) and anything that reads a marker back. Pure: no I/O, no env.
|
|
14
|
+
*/
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
/**
|
|
17
|
+
* The one line Edward changes to start uploading slim transcripts (images described and dropped,
|
|
18
|
+
* duplicate copies and bookkeeping stripped). Default is keep. The collector reads it at run time,
|
|
19
|
+
* so flipping it needs a CLI release (`gh workflow run release.yml -f tag=latest`) and then
|
|
20
|
+
* `cockpit settings cli-floor <version>`; no other file changes.
|
|
21
|
+
*/
|
|
22
|
+
export const IMAGE_HANDLING_MODE = "describe_and_keep";
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Limits
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
/**
|
|
27
|
+
* Largest decoded image the describe door takes. The door is a serverless function with a 4.5 MB
|
|
28
|
+
* request body ceiling and base64 inflates by a third, so 3 MB is the biggest image that fits with
|
|
29
|
+
* room for the JSON around it. A larger image is kept as it is and counted (`image_too_large`).
|
|
30
|
+
*/
|
|
31
|
+
export const IMAGE_DESCRIBE_MAX_BYTES = 3_000_000;
|
|
32
|
+
/** A base64 run shorter than this is not a screenshot (an icon, a hash, a token). */
|
|
33
|
+
export const IMAGE_MIN_BASE64_CHARS = 500;
|
|
34
|
+
/** The longest description the door stores. About 300 tokens is 1,200 characters. */
|
|
35
|
+
export const IMAGE_DESCRIPTION_MAX_CHARS = 1_600;
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Media types
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
export const IMAGE_MEDIA_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"];
|
|
40
|
+
const EXTENSION_BY_TYPE = {
|
|
41
|
+
"image/png": "png",
|
|
42
|
+
"image/jpeg": "jpg",
|
|
43
|
+
"image/gif": "gif",
|
|
44
|
+
"image/webp": "webp",
|
|
45
|
+
};
|
|
46
|
+
const TYPE_BY_EXTENSION = new Map(Object.entries(EXTENSION_BY_TYPE).map(([type, ext]) => [ext, type]));
|
|
47
|
+
/** What the bytes are, from their first bytes. A declared type in the transcript is never trusted. */
|
|
48
|
+
export function sniffImageMediaType(bytes) {
|
|
49
|
+
if (bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47)
|
|
50
|
+
return "image/png";
|
|
51
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff)
|
|
52
|
+
return "image/jpeg";
|
|
53
|
+
if (bytes.length >= 6 && bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38)
|
|
54
|
+
return "image/gif";
|
|
55
|
+
if (bytes.length >= 12 &&
|
|
56
|
+
bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 &&
|
|
57
|
+
bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) {
|
|
58
|
+
return "image/webp";
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
// The marker
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
/** Hex characters of the sha256 a marker carries. 64 bits is far past any one person's image count. */
|
|
66
|
+
export const IMAGE_MARKER_HASH_CHARS = 16;
|
|
67
|
+
export function imageSizeLabel(byteSize) {
|
|
68
|
+
if (byteSize >= 1_000_000)
|
|
69
|
+
return `${(byteSize / 1_000_000).toFixed(1)}MB`;
|
|
70
|
+
if (byteSize >= 1_000)
|
|
71
|
+
return `${Math.round(byteSize / 1_000)}KB`;
|
|
72
|
+
return `${byteSize}B`;
|
|
73
|
+
}
|
|
74
|
+
/** `[image sha256:ab12cd34ef567890… png 412KB]`: what replaces the base64 in an uploaded transcript. */
|
|
75
|
+
export function formatImageMarker(image) {
|
|
76
|
+
const hash = image.sha256.slice(0, IMAGE_MARKER_HASH_CHARS);
|
|
77
|
+
return `[image sha256:${hash}… ${EXTENSION_BY_TYPE[image.mediaType]} ${imageSizeLabel(image.byteSize)}]`;
|
|
78
|
+
}
|
|
79
|
+
const MARKER_SOURCE = String.raw `\[image sha256:([0-9a-f]{${IMAGE_MARKER_HASH_CHARS}})… (png|jpg|gif|webp) (\d+(?:\.\d)?(?:B|KB|MB))\]`;
|
|
80
|
+
/** Every marker in a piece of text, in order. A fresh regex per call, so no shared `lastIndex`. */
|
|
81
|
+
export function parseImageMarkers(text) {
|
|
82
|
+
const found = [];
|
|
83
|
+
for (const match of text.matchAll(new RegExp(MARKER_SOURCE, "g"))) {
|
|
84
|
+
const mediaType = TYPE_BY_EXTENSION.get(match[2]);
|
|
85
|
+
if (mediaType)
|
|
86
|
+
found.push({ sha256Prefix: match[1], mediaType, sizeLabel: match[3] });
|
|
87
|
+
}
|
|
88
|
+
return found;
|
|
89
|
+
}
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
// The door: POST /api/ambient/images (describe) and GET /api/ambient/images (read)
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
const Sha256Schema = z.string().regex(/^[0-9a-f]{64}$/, "sha256 must be 64 lowercase hex characters");
|
|
94
|
+
export const ImageDescribeRequestSchema = z
|
|
95
|
+
.object({
|
|
96
|
+
schema_version: z.literal("image-describe.v1"),
|
|
97
|
+
sha256: Sha256Schema,
|
|
98
|
+
media_type: z.enum(IMAGE_MEDIA_TYPES),
|
|
99
|
+
byte_size: z.number().int().min(1).max(IMAGE_DESCRIBE_MAX_BYTES),
|
|
100
|
+
content_base64: z.string().min(8).max(Math.ceil((IMAGE_DESCRIBE_MAX_BYTES * 4) / 3) + 16),
|
|
101
|
+
/** Which agent wrote the transcript the image came from. */
|
|
102
|
+
source: z.enum(["codex", "claude_code", "manual"]).optional(),
|
|
103
|
+
/** The agent's session id, an id only, for "where did this image appear". */
|
|
104
|
+
session_id: z.string().min(1).max(200).optional(),
|
|
105
|
+
})
|
|
106
|
+
.strict();
|
|
107
|
+
export const ImageDescriptionSchema = z
|
|
108
|
+
.object({
|
|
109
|
+
sha256: Sha256Schema,
|
|
110
|
+
media_type: z.enum(IMAGE_MEDIA_TYPES),
|
|
111
|
+
byte_size: z.number().int().nonnegative(),
|
|
112
|
+
description: z.string().max(IMAGE_DESCRIPTION_MAX_CHARS),
|
|
113
|
+
/** True when the secret guards masked something in the description. */
|
|
114
|
+
secrets_masked: z.boolean(),
|
|
115
|
+
model: z.string(),
|
|
116
|
+
created_at: z.string(),
|
|
117
|
+
})
|
|
118
|
+
.strict();
|
|
119
|
+
export const ImageDescribeResponseSchema = z
|
|
120
|
+
.object({
|
|
121
|
+
ok: z.literal(true),
|
|
122
|
+
/** `described`: the model ran now. `known`: this person already had a description for these bytes. */
|
|
123
|
+
status: z.enum(["described", "known"]),
|
|
124
|
+
description: ImageDescriptionSchema,
|
|
125
|
+
})
|
|
126
|
+
.strict();
|
|
127
|
+
export const ImageReadResponseSchema = z
|
|
128
|
+
.object({
|
|
129
|
+
ok: z.literal(true),
|
|
130
|
+
descriptions: z.array(ImageDescriptionSchema),
|
|
131
|
+
})
|
|
132
|
+
.strict();
|
|
133
|
+
/** A sha256 or a prefix of one (the 16 characters a marker carries). */
|
|
134
|
+
export const ImageHashQuerySchema = z.string().regex(/^[0-9a-f]{12,64}$/, "give 12 to 64 lowercase hex characters of the sha256");
|
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ export * from "./evidence-reconcile.js";
|
|
|
10
10
|
export * from "./evidence-stubs.js";
|
|
11
11
|
export * from "./evidence-transcripts.js";
|
|
12
12
|
export * from "./evidence-upload.js";
|
|
13
|
+
export * from "./image-descriptions.js";
|
|
14
|
+
export * from "./transcript-images.js";
|
|
13
15
|
export * from "./ingest-dto.js";
|
|
14
16
|
export * from "./local-config.js";
|
|
15
17
|
export * from "./memory-daemon-endpoint.js";
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,8 @@ export * from "./evidence-reconcile.js";
|
|
|
10
10
|
export * from "./evidence-stubs.js";
|
|
11
11
|
export * from "./evidence-transcripts.js";
|
|
12
12
|
export * from "./evidence-upload.js";
|
|
13
|
+
export * from "./image-descriptions.js";
|
|
14
|
+
export * from "./transcript-images.js";
|
|
13
15
|
export * from "./ingest-dto.js";
|
|
14
16
|
export * from "./local-config.js";
|
|
15
17
|
export * from "./memory-daemon-endpoint.js";
|
package/dist/ingest-dto.d.ts
CHANGED
|
@@ -458,13 +458,13 @@ export declare const TelemetryIngestEnvelopeSchema: z.ZodObject<{
|
|
|
458
458
|
}>>;
|
|
459
459
|
intent_source: z.ZodOptional<z.ZodEnum<{
|
|
460
460
|
unknown: "unknown";
|
|
461
|
+
manual: "manual";
|
|
461
462
|
explicit_user: "explicit_user";
|
|
462
463
|
linear_ticket: "linear_ticket";
|
|
463
464
|
git_branch: "git_branch";
|
|
464
465
|
collector_heuristic: "collector_heuristic";
|
|
465
466
|
agent_heuristic: "agent_heuristic";
|
|
466
467
|
llm_inference: "llm_inference";
|
|
467
|
-
manual: "manual";
|
|
468
468
|
}>>;
|
|
469
469
|
intent_confidence: z.ZodOptional<z.ZodNumber>;
|
|
470
470
|
pull_request_url: z.ZodOptional<z.ZodString>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type ImageMediaType } from "./image-descriptions.js";
|
|
2
|
+
export interface EmbeddedImage {
|
|
3
|
+
/** Where the replaced text begins and ends in the transcript string. */
|
|
4
|
+
replaceStart: number;
|
|
5
|
+
replaceEnd: number;
|
|
6
|
+
/** Where the base64 itself begins and ends (inside the replaced range). */
|
|
7
|
+
base64Start: number;
|
|
8
|
+
base64End: number;
|
|
9
|
+
sha256: string;
|
|
10
|
+
mediaType: ImageMediaType;
|
|
11
|
+
/** Decoded size in bytes. */
|
|
12
|
+
byteSize: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Cheap check on raw bytes before anyone decodes a whole transcript to text: does it hold a data
|
|
16
|
+
* URL, or a string that starts the way a PNG, JPEG, GIF or WebP does in base64?
|
|
17
|
+
*/
|
|
18
|
+
export declare function mightHoldEmbeddedImages(bytes: Buffer): boolean;
|
|
19
|
+
/** Every embedded image in `text`, in order. Runs that are not images are skipped. */
|
|
20
|
+
export declare function findEmbeddedImages(text: string): EmbeddedImage[];
|
|
21
|
+
/** The base64 text of one found image, sliced from the transcript only when it is about to be sent. */
|
|
22
|
+
export declare function base64Of(text: string, image: EmbeddedImage): string;
|
|
23
|
+
/**
|
|
24
|
+
* `text` with the chosen images swapped for their markers. `replace` says which images; the rest
|
|
25
|
+
* stay byte for byte. Returns how many characters came out, so the caller can log bytes saved.
|
|
26
|
+
*/
|
|
27
|
+
export declare function replaceImagesWithMarkers(text: string, images: readonly EmbeddedImage[], replace: (image: EmbeddedImage) => boolean): {
|
|
28
|
+
text: string;
|
|
29
|
+
replacedCount: number;
|
|
30
|
+
charsSaved: number;
|
|
31
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find the base64 images inside a transcript's text, and swap them for short markers (BLI-16977).
|
|
3
|
+
*
|
|
4
|
+
* A transcript stores a picture as one long run of base64 characters, in one of two shapes:
|
|
5
|
+
*
|
|
6
|
+
* - a JSON string that is NOTHING BUT the run (`"data":"<base64>"` in a Claude image block, a
|
|
7
|
+
* Codex `image_gen` result, an MCP image, a Codex `toolSurface.screenshot` value, ...). Any key
|
|
8
|
+
* name, any depth: the finder looks at the string, not at what it is called.
|
|
9
|
+
* - a data URL (`data:image/png;base64,<base64>`) anywhere inside a string (a Codex `image_url`).
|
|
10
|
+
*
|
|
11
|
+
* Either way this file decodes each run far enough to know it IS an image (magic bytes, never the
|
|
12
|
+
* transcript's own claim) and what its sha256 is, and can rewrite the text with a marker in place
|
|
13
|
+
* of chosen images.
|
|
14
|
+
*
|
|
15
|
+
* Pure: text in, text out. No network, no disk, no environment. A run that does not decode to a
|
|
16
|
+
* PNG, JPEG, GIF or WebP is left exactly as it is (a PDF, a long token, a hex dump).
|
|
17
|
+
*
|
|
18
|
+
* The rewrite keeps the JSON line valid: in the whole-string form only the base64 is replaced,
|
|
19
|
+
* inside its quotes; in the data-URL form the whole data URL is replaced, inside the string it sat
|
|
20
|
+
* in. So every parser that read the line before still reads it.
|
|
21
|
+
*/
|
|
22
|
+
import { createHash } from "node:crypto";
|
|
23
|
+
import { IMAGE_MIN_BASE64_CHARS, formatImageMarker, sniffImageMediaType, } from "./image-descriptions.js";
|
|
24
|
+
const BASE64_RUN = `[A-Za-z0-9+/=]{${IMAGE_MIN_BASE64_CHARS},}`;
|
|
25
|
+
/**
|
|
26
|
+
* What the first characters of a base64 run look like for each type we accept: PNG `iVBOR`, JPEG
|
|
27
|
+
* `/9j/`, GIF `R0lGO`, WebP (RIFF) `UklGR`. Anchoring the whole-string form on these keeps the scan
|
|
28
|
+
* fast (no 500-character attempt after every quote); the decoded bytes are still sniffed after.
|
|
29
|
+
*/
|
|
30
|
+
const IMAGE_BASE64_STARTS = String.raw `(?:iVBOR|\/9j\/|R0lGO|UklGR)`;
|
|
31
|
+
/**
|
|
32
|
+
* 1: a run that starts like an image and sits between two quotes (plain `"` or the `\"` of a JSON
|
|
33
|
+
* string that was itself stored inside a string), so it is the whole string.
|
|
34
|
+
* 2: a data URL prefix, then the run.
|
|
35
|
+
*/
|
|
36
|
+
const IMAGE_STRING = new RegExp(`(?<=")(${IMAGE_BASE64_STARTS}[A-Za-z0-9+/=]{${IMAGE_MIN_BASE64_CHARS - 5},})(?=\\\\?")|(data:image/[a-z0-9.+-]+;base64,)(${BASE64_RUN})`, "g");
|
|
37
|
+
/**
|
|
38
|
+
* Cheap check on raw bytes before anyone decodes a whole transcript to text: does it hold a data
|
|
39
|
+
* URL, or a string that starts the way a PNG, JPEG, GIF or WebP does in base64?
|
|
40
|
+
*/
|
|
41
|
+
export function mightHoldEmbeddedImages(bytes) {
|
|
42
|
+
return (bytes.includes("base64") ||
|
|
43
|
+
bytes.includes("iVBOR") ||
|
|
44
|
+
bytes.includes("/9j/") ||
|
|
45
|
+
bytes.includes("R0lGO") ||
|
|
46
|
+
bytes.includes("UklGR"));
|
|
47
|
+
}
|
|
48
|
+
/** Every embedded image in `text`, in order. Runs that are not images are skipped. */
|
|
49
|
+
export function findEmbeddedImages(text) {
|
|
50
|
+
const found = [];
|
|
51
|
+
for (const match of text.matchAll(IMAGE_STRING)) {
|
|
52
|
+
const isWholeString = match[1] !== undefined;
|
|
53
|
+
const run = (isWholeString ? match[1] : match[3]) ?? "";
|
|
54
|
+
const matchStart = match.index ?? 0;
|
|
55
|
+
const base64Start = matchStart + (isWholeString ? 0 : match[2].length);
|
|
56
|
+
const bytes = Buffer.from(run, "base64");
|
|
57
|
+
const mediaType = sniffImageMediaType(bytes);
|
|
58
|
+
if (!mediaType)
|
|
59
|
+
continue;
|
|
60
|
+
found.push({
|
|
61
|
+
replaceStart: isWholeString ? base64Start : matchStart,
|
|
62
|
+
replaceEnd: base64Start + run.length,
|
|
63
|
+
base64Start,
|
|
64
|
+
base64End: base64Start + run.length,
|
|
65
|
+
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
66
|
+
mediaType,
|
|
67
|
+
byteSize: bytes.byteLength,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return found;
|
|
71
|
+
}
|
|
72
|
+
/** The base64 text of one found image, sliced from the transcript only when it is about to be sent. */
|
|
73
|
+
export function base64Of(text, image) {
|
|
74
|
+
return text.slice(image.base64Start, image.base64End);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `text` with the chosen images swapped for their markers. `replace` says which images; the rest
|
|
78
|
+
* stay byte for byte. Returns how many characters came out, so the caller can log bytes saved.
|
|
79
|
+
*/
|
|
80
|
+
export function replaceImagesWithMarkers(text, images, replace) {
|
|
81
|
+
const pieces = [];
|
|
82
|
+
let cursor = 0;
|
|
83
|
+
let replacedCount = 0;
|
|
84
|
+
let charsSaved = 0;
|
|
85
|
+
for (const image of images) {
|
|
86
|
+
if (!replace(image))
|
|
87
|
+
continue;
|
|
88
|
+
const marker = formatImageMarker({
|
|
89
|
+
sha256: image.sha256,
|
|
90
|
+
mediaType: image.mediaType,
|
|
91
|
+
byteSize: image.byteSize,
|
|
92
|
+
});
|
|
93
|
+
pieces.push(text.slice(cursor, image.replaceStart), marker);
|
|
94
|
+
cursor = image.replaceEnd;
|
|
95
|
+
replacedCount += 1;
|
|
96
|
+
charsSaved += image.replaceEnd - image.replaceStart - marker.length;
|
|
97
|
+
}
|
|
98
|
+
if (replacedCount === 0)
|
|
99
|
+
return { text, replacedCount: 0, charsSaved: 0 };
|
|
100
|
+
pieces.push(text.slice(cursor));
|
|
101
|
+
return { text: pieces.join(""), replacedCount, charsSaved };
|
|
102
|
+
}
|
package/dist/work-context.d.ts
CHANGED
|
@@ -60,13 +60,13 @@ export declare const WorkPhaseSchema: z.ZodEnum<{
|
|
|
60
60
|
export type WorkPhase = z.infer<typeof WorkPhaseSchema>;
|
|
61
61
|
export declare const IntentSourceSchema: z.ZodEnum<{
|
|
62
62
|
unknown: "unknown";
|
|
63
|
+
manual: "manual";
|
|
63
64
|
explicit_user: "explicit_user";
|
|
64
65
|
linear_ticket: "linear_ticket";
|
|
65
66
|
git_branch: "git_branch";
|
|
66
67
|
collector_heuristic: "collector_heuristic";
|
|
67
68
|
agent_heuristic: "agent_heuristic";
|
|
68
69
|
llm_inference: "llm_inference";
|
|
69
|
-
manual: "manual";
|
|
70
70
|
}>;
|
|
71
71
|
export type IntentSource = z.infer<typeof IntentSourceSchema>;
|
|
72
72
|
export declare const LocalWorkContextSchema: z.ZodObject<{
|
|
@@ -137,13 +137,13 @@ export declare const LocalWorkContextSchema: z.ZodObject<{
|
|
|
137
137
|
}>>;
|
|
138
138
|
intent_source: z.ZodOptional<z.ZodEnum<{
|
|
139
139
|
unknown: "unknown";
|
|
140
|
+
manual: "manual";
|
|
140
141
|
explicit_user: "explicit_user";
|
|
141
142
|
linear_ticket: "linear_ticket";
|
|
142
143
|
git_branch: "git_branch";
|
|
143
144
|
collector_heuristic: "collector_heuristic";
|
|
144
145
|
agent_heuristic: "agent_heuristic";
|
|
145
146
|
llm_inference: "llm_inference";
|
|
146
|
-
manual: "manual";
|
|
147
147
|
}>>;
|
|
148
148
|
intent_confidence: z.ZodOptional<z.ZodNumber>;
|
|
149
149
|
pull_request_url: z.ZodOptional<z.ZodString>;
|