@bli-cockpit/telemetry-core 0.1.70 → 0.1.71
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 +133 -0
- package/dist/image-descriptions.js +132 -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 +28 -0
- package/dist/transcript-images.js +79 -0
- package/dist/work-context.d.ts +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,133 @@
|
|
|
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.
|
|
10
|
+
*
|
|
11
|
+
* This file is shared by the collector (builds the marker, builds the request), the dashboard
|
|
12
|
+
* (validates the request, answers) and anything that reads a marker back. Pure: no I/O, no env.
|
|
13
|
+
*/
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
/**
|
|
16
|
+
* `describe_and_keep` every distinct image is described, the transcript still carries the image.
|
|
17
|
+
* `describe_and_drop` every image that has a stored description is replaced by its marker in the
|
|
18
|
+
* uploaded transcript; an image that could not be described is KEPT, never
|
|
19
|
+
* dropped without a description.
|
|
20
|
+
*/
|
|
21
|
+
export type ImageHandlingMode = "describe_and_keep" | "describe_and_drop";
|
|
22
|
+
/**
|
|
23
|
+
* The one line Edward changes to start dropping images. Default is keep. The collector reads it at
|
|
24
|
+
* run time, so flipping it needs a CLI release (`gh workflow run release.yml -f tag=latest`) and
|
|
25
|
+
* then `cockpit settings cli-floor <version>`; no other file changes.
|
|
26
|
+
*/
|
|
27
|
+
export declare const IMAGE_HANDLING_MODE: ImageHandlingMode;
|
|
28
|
+
/**
|
|
29
|
+
* Largest decoded image the describe door takes. The door is a serverless function with a 4.5 MB
|
|
30
|
+
* request body ceiling and base64 inflates by a third, so 3 MB is the biggest image that fits with
|
|
31
|
+
* room for the JSON around it. A larger image is kept as it is and counted (`image_too_large`).
|
|
32
|
+
*/
|
|
33
|
+
export declare const IMAGE_DESCRIBE_MAX_BYTES = 3000000;
|
|
34
|
+
/** A base64 run shorter than this is not a screenshot (an icon, a hash, a token). */
|
|
35
|
+
export declare const IMAGE_MIN_BASE64_CHARS = 500;
|
|
36
|
+
/** The longest description the door stores. About 300 tokens is 1,200 characters. */
|
|
37
|
+
export declare const IMAGE_DESCRIPTION_MAX_CHARS = 1600;
|
|
38
|
+
export declare const IMAGE_MEDIA_TYPES: readonly ["image/png", "image/jpeg", "image/gif", "image/webp"];
|
|
39
|
+
export type ImageMediaType = (typeof IMAGE_MEDIA_TYPES)[number];
|
|
40
|
+
/** What the bytes are, from their first bytes. A declared type in the transcript is never trusted. */
|
|
41
|
+
export declare function sniffImageMediaType(bytes: Uint8Array): ImageMediaType | null;
|
|
42
|
+
/** Hex characters of the sha256 a marker carries. 64 bits is far past any one person's image count. */
|
|
43
|
+
export declare const IMAGE_MARKER_HASH_CHARS = 16;
|
|
44
|
+
export declare function imageSizeLabel(byteSize: number): string;
|
|
45
|
+
/** `[image sha256:ab12cd34ef567890… png 412KB]`: what replaces the base64 in an uploaded transcript. */
|
|
46
|
+
export declare function formatImageMarker(image: {
|
|
47
|
+
sha256: string;
|
|
48
|
+
mediaType: ImageMediaType;
|
|
49
|
+
byteSize: number;
|
|
50
|
+
}): string;
|
|
51
|
+
export interface ParsedImageMarker {
|
|
52
|
+
sha256Prefix: string;
|
|
53
|
+
mediaType: ImageMediaType;
|
|
54
|
+
sizeLabel: string;
|
|
55
|
+
}
|
|
56
|
+
/** Every marker in a piece of text, in order. A fresh regex per call, so no shared `lastIndex`. */
|
|
57
|
+
export declare function parseImageMarkers(text: string): ParsedImageMarker[];
|
|
58
|
+
export declare const ImageDescribeRequestSchema: z.ZodObject<{
|
|
59
|
+
schema_version: z.ZodLiteral<"image-describe.v1">;
|
|
60
|
+
sha256: z.ZodString;
|
|
61
|
+
media_type: z.ZodEnum<{
|
|
62
|
+
"image/png": "image/png";
|
|
63
|
+
"image/jpeg": "image/jpeg";
|
|
64
|
+
"image/gif": "image/gif";
|
|
65
|
+
"image/webp": "image/webp";
|
|
66
|
+
}>;
|
|
67
|
+
byte_size: z.ZodNumber;
|
|
68
|
+
content_base64: z.ZodString;
|
|
69
|
+
source: z.ZodOptional<z.ZodEnum<{
|
|
70
|
+
codex: "codex";
|
|
71
|
+
claude_code: "claude_code";
|
|
72
|
+
manual: "manual";
|
|
73
|
+
}>>;
|
|
74
|
+
session_id: z.ZodOptional<z.ZodString>;
|
|
75
|
+
}, z.core.$strict>;
|
|
76
|
+
export type ImageDescribeRequest = z.infer<typeof ImageDescribeRequestSchema>;
|
|
77
|
+
export declare const ImageDescriptionSchema: z.ZodObject<{
|
|
78
|
+
sha256: z.ZodString;
|
|
79
|
+
media_type: z.ZodEnum<{
|
|
80
|
+
"image/png": "image/png";
|
|
81
|
+
"image/jpeg": "image/jpeg";
|
|
82
|
+
"image/gif": "image/gif";
|
|
83
|
+
"image/webp": "image/webp";
|
|
84
|
+
}>;
|
|
85
|
+
byte_size: z.ZodNumber;
|
|
86
|
+
description: z.ZodString;
|
|
87
|
+
secrets_masked: z.ZodBoolean;
|
|
88
|
+
model: z.ZodString;
|
|
89
|
+
created_at: z.ZodString;
|
|
90
|
+
}, z.core.$strict>;
|
|
91
|
+
export type ImageDescription = z.infer<typeof ImageDescriptionSchema>;
|
|
92
|
+
export declare const ImageDescribeResponseSchema: z.ZodObject<{
|
|
93
|
+
ok: z.ZodLiteral<true>;
|
|
94
|
+
status: z.ZodEnum<{
|
|
95
|
+
described: "described";
|
|
96
|
+
known: "known";
|
|
97
|
+
}>;
|
|
98
|
+
description: z.ZodObject<{
|
|
99
|
+
sha256: z.ZodString;
|
|
100
|
+
media_type: z.ZodEnum<{
|
|
101
|
+
"image/png": "image/png";
|
|
102
|
+
"image/jpeg": "image/jpeg";
|
|
103
|
+
"image/gif": "image/gif";
|
|
104
|
+
"image/webp": "image/webp";
|
|
105
|
+
}>;
|
|
106
|
+
byte_size: z.ZodNumber;
|
|
107
|
+
description: z.ZodString;
|
|
108
|
+
secrets_masked: z.ZodBoolean;
|
|
109
|
+
model: z.ZodString;
|
|
110
|
+
created_at: z.ZodString;
|
|
111
|
+
}, z.core.$strict>;
|
|
112
|
+
}, z.core.$strict>;
|
|
113
|
+
export type ImageDescribeResponse = z.infer<typeof ImageDescribeResponseSchema>;
|
|
114
|
+
export declare const ImageReadResponseSchema: z.ZodObject<{
|
|
115
|
+
ok: z.ZodLiteral<true>;
|
|
116
|
+
descriptions: z.ZodArray<z.ZodObject<{
|
|
117
|
+
sha256: z.ZodString;
|
|
118
|
+
media_type: z.ZodEnum<{
|
|
119
|
+
"image/png": "image/png";
|
|
120
|
+
"image/jpeg": "image/jpeg";
|
|
121
|
+
"image/gif": "image/gif";
|
|
122
|
+
"image/webp": "image/webp";
|
|
123
|
+
}>;
|
|
124
|
+
byte_size: z.ZodNumber;
|
|
125
|
+
description: z.ZodString;
|
|
126
|
+
secrets_masked: z.ZodBoolean;
|
|
127
|
+
model: z.ZodString;
|
|
128
|
+
created_at: z.ZodString;
|
|
129
|
+
}, z.core.$strict>>;
|
|
130
|
+
}, z.core.$strict>;
|
|
131
|
+
export type ImageReadResponse = z.infer<typeof ImageReadResponseSchema>;
|
|
132
|
+
/** A sha256 or a prefix of one (the 16 characters a marker carries). */
|
|
133
|
+
export declare const ImageHashQuerySchema: z.ZodString;
|
|
@@ -0,0 +1,132 @@
|
|
|
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.
|
|
10
|
+
*
|
|
11
|
+
* This file is shared by the collector (builds the marker, builds the request), the dashboard
|
|
12
|
+
* (validates the request, answers) and anything that reads a marker back. Pure: no I/O, no env.
|
|
13
|
+
*/
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
/**
|
|
16
|
+
* The one line Edward changes to start dropping images. Default is keep. The collector reads it at
|
|
17
|
+
* run time, so flipping it needs a CLI release (`gh workflow run release.yml -f tag=latest`) and
|
|
18
|
+
* then `cockpit settings cli-floor <version>`; no other file changes.
|
|
19
|
+
*/
|
|
20
|
+
export const IMAGE_HANDLING_MODE = "describe_and_keep";
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// Limits
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
/**
|
|
25
|
+
* Largest decoded image the describe door takes. The door is a serverless function with a 4.5 MB
|
|
26
|
+
* request body ceiling and base64 inflates by a third, so 3 MB is the biggest image that fits with
|
|
27
|
+
* room for the JSON around it. A larger image is kept as it is and counted (`image_too_large`).
|
|
28
|
+
*/
|
|
29
|
+
export const IMAGE_DESCRIBE_MAX_BYTES = 3_000_000;
|
|
30
|
+
/** A base64 run shorter than this is not a screenshot (an icon, a hash, a token). */
|
|
31
|
+
export const IMAGE_MIN_BASE64_CHARS = 500;
|
|
32
|
+
/** The longest description the door stores. About 300 tokens is 1,200 characters. */
|
|
33
|
+
export const IMAGE_DESCRIPTION_MAX_CHARS = 1_600;
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Media types
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
export const IMAGE_MEDIA_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"];
|
|
38
|
+
const EXTENSION_BY_TYPE = {
|
|
39
|
+
"image/png": "png",
|
|
40
|
+
"image/jpeg": "jpg",
|
|
41
|
+
"image/gif": "gif",
|
|
42
|
+
"image/webp": "webp",
|
|
43
|
+
};
|
|
44
|
+
const TYPE_BY_EXTENSION = new Map(Object.entries(EXTENSION_BY_TYPE).map(([type, ext]) => [ext, type]));
|
|
45
|
+
/** What the bytes are, from their first bytes. A declared type in the transcript is never trusted. */
|
|
46
|
+
export function sniffImageMediaType(bytes) {
|
|
47
|
+
if (bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47)
|
|
48
|
+
return "image/png";
|
|
49
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff)
|
|
50
|
+
return "image/jpeg";
|
|
51
|
+
if (bytes.length >= 6 && bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38)
|
|
52
|
+
return "image/gif";
|
|
53
|
+
if (bytes.length >= 12 &&
|
|
54
|
+
bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 &&
|
|
55
|
+
bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) {
|
|
56
|
+
return "image/webp";
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// The marker
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
/** Hex characters of the sha256 a marker carries. 64 bits is far past any one person's image count. */
|
|
64
|
+
export const IMAGE_MARKER_HASH_CHARS = 16;
|
|
65
|
+
export function imageSizeLabel(byteSize) {
|
|
66
|
+
if (byteSize >= 1_000_000)
|
|
67
|
+
return `${(byteSize / 1_000_000).toFixed(1)}MB`;
|
|
68
|
+
if (byteSize >= 1_000)
|
|
69
|
+
return `${Math.round(byteSize / 1_000)}KB`;
|
|
70
|
+
return `${byteSize}B`;
|
|
71
|
+
}
|
|
72
|
+
/** `[image sha256:ab12cd34ef567890… png 412KB]`: what replaces the base64 in an uploaded transcript. */
|
|
73
|
+
export function formatImageMarker(image) {
|
|
74
|
+
const hash = image.sha256.slice(0, IMAGE_MARKER_HASH_CHARS);
|
|
75
|
+
return `[image sha256:${hash}… ${EXTENSION_BY_TYPE[image.mediaType]} ${imageSizeLabel(image.byteSize)}]`;
|
|
76
|
+
}
|
|
77
|
+
const MARKER_SOURCE = String.raw `\[image sha256:([0-9a-f]{${IMAGE_MARKER_HASH_CHARS}})… (png|jpg|gif|webp) (\d+(?:\.\d)?(?:B|KB|MB))\]`;
|
|
78
|
+
/** Every marker in a piece of text, in order. A fresh regex per call, so no shared `lastIndex`. */
|
|
79
|
+
export function parseImageMarkers(text) {
|
|
80
|
+
const found = [];
|
|
81
|
+
for (const match of text.matchAll(new RegExp(MARKER_SOURCE, "g"))) {
|
|
82
|
+
const mediaType = TYPE_BY_EXTENSION.get(match[2]);
|
|
83
|
+
if (mediaType)
|
|
84
|
+
found.push({ sha256Prefix: match[1], mediaType, sizeLabel: match[3] });
|
|
85
|
+
}
|
|
86
|
+
return found;
|
|
87
|
+
}
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
// The door: POST /api/ambient/images (describe) and GET /api/ambient/images (read)
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
const Sha256Schema = z.string().regex(/^[0-9a-f]{64}$/, "sha256 must be 64 lowercase hex characters");
|
|
92
|
+
export const ImageDescribeRequestSchema = z
|
|
93
|
+
.object({
|
|
94
|
+
schema_version: z.literal("image-describe.v1"),
|
|
95
|
+
sha256: Sha256Schema,
|
|
96
|
+
media_type: z.enum(IMAGE_MEDIA_TYPES),
|
|
97
|
+
byte_size: z.number().int().min(1).max(IMAGE_DESCRIBE_MAX_BYTES),
|
|
98
|
+
content_base64: z.string().min(8).max(Math.ceil((IMAGE_DESCRIBE_MAX_BYTES * 4) / 3) + 16),
|
|
99
|
+
/** Which agent wrote the transcript the image came from. */
|
|
100
|
+
source: z.enum(["codex", "claude_code", "manual"]).optional(),
|
|
101
|
+
/** The agent's session id, an id only, for "where did this image appear". */
|
|
102
|
+
session_id: z.string().min(1).max(200).optional(),
|
|
103
|
+
})
|
|
104
|
+
.strict();
|
|
105
|
+
export const ImageDescriptionSchema = z
|
|
106
|
+
.object({
|
|
107
|
+
sha256: Sha256Schema,
|
|
108
|
+
media_type: z.enum(IMAGE_MEDIA_TYPES),
|
|
109
|
+
byte_size: z.number().int().nonnegative(),
|
|
110
|
+
description: z.string().max(IMAGE_DESCRIPTION_MAX_CHARS),
|
|
111
|
+
/** True when the secret guards masked something in the description. */
|
|
112
|
+
secrets_masked: z.boolean(),
|
|
113
|
+
model: z.string(),
|
|
114
|
+
created_at: z.string(),
|
|
115
|
+
})
|
|
116
|
+
.strict();
|
|
117
|
+
export const ImageDescribeResponseSchema = z
|
|
118
|
+
.object({
|
|
119
|
+
ok: z.literal(true),
|
|
120
|
+
/** `described`: the model ran now. `known`: this person already had a description for these bytes. */
|
|
121
|
+
status: z.enum(["described", "known"]),
|
|
122
|
+
description: ImageDescriptionSchema,
|
|
123
|
+
})
|
|
124
|
+
.strict();
|
|
125
|
+
export const ImageReadResponseSchema = z
|
|
126
|
+
.object({
|
|
127
|
+
ok: z.literal(true),
|
|
128
|
+
descriptions: z.array(ImageDescriptionSchema),
|
|
129
|
+
})
|
|
130
|
+
.strict();
|
|
131
|
+
/** A sha256 or a prefix of one (the 16 characters a marker carries). */
|
|
132
|
+
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,28 @@
|
|
|
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
|
+
/** Cheap check on raw bytes before anyone decodes a whole transcript to text. */
|
|
15
|
+
export declare function mightHoldEmbeddedImages(bytes: Buffer): boolean;
|
|
16
|
+
/** Every embedded image in `text`, in order. Runs that are not images are skipped. */
|
|
17
|
+
export declare function findEmbeddedImages(text: string): EmbeddedImage[];
|
|
18
|
+
/** The base64 text of one found image, sliced from the transcript only when it is about to be sent. */
|
|
19
|
+
export declare function base64Of(text: string, image: EmbeddedImage): string;
|
|
20
|
+
/**
|
|
21
|
+
* `text` with the chosen images swapped for their markers. `replace` says which images; the rest
|
|
22
|
+
* stay byte for byte. Returns how many characters came out, so the caller can log bytes saved.
|
|
23
|
+
*/
|
|
24
|
+
export declare function replaceImagesWithMarkers(text: string, images: readonly EmbeddedImage[], replace: (image: EmbeddedImage) => boolean): {
|
|
25
|
+
text: string;
|
|
26
|
+
replacedCount: number;
|
|
27
|
+
charsSaved: number;
|
|
28
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find the base64 images inside a transcript's text, and swap them for short markers (BLI-16977).
|
|
3
|
+
*
|
|
4
|
+
* A Claude Code line stores a pasted screenshot as `"source":{"type":"base64","media_type":"image/png","data":"<base64>"}`;
|
|
5
|
+
* a Codex line stores it as `"image_url":"data:image/png;base64,<base64>"`. Either way the bytes
|
|
6
|
+
* are one long run of base64 characters. This file finds those runs, decodes each one far enough
|
|
7
|
+
* to know it IS an image (magic bytes, never the transcript's own claim) and what its sha256 is,
|
|
8
|
+
* and can rewrite the text with a marker in place of chosen images.
|
|
9
|
+
*
|
|
10
|
+
* Pure: text in, text out. No network, no disk, no environment. A run that does not decode to a
|
|
11
|
+
* PNG, JPEG, GIF or WebP is left exactly as it is (a PDF, a long token, a hex dump).
|
|
12
|
+
*
|
|
13
|
+
* The rewrite keeps the JSON line valid: in the `"data":"..."` form only the base64 is replaced,
|
|
14
|
+
* inside its quotes; in the `data:image/..;base64,...` form the whole data URL is replaced, inside
|
|
15
|
+
* the string it sat in. So every parser that read the line before still reads it.
|
|
16
|
+
*/
|
|
17
|
+
import { createHash } from "node:crypto";
|
|
18
|
+
import { IMAGE_MIN_BASE64_CHARS, formatImageMarker, sniffImageMediaType, } from "./image-descriptions.js";
|
|
19
|
+
/** `"data":"<base64>"` (Claude) or `data:image/png;base64,<base64>` (Codex, and any data URL). */
|
|
20
|
+
const BASE64_RUN = new RegExp(`("data"\\s*:\\s*")([A-Za-z0-9+/=]{${IMAGE_MIN_BASE64_CHARS},})|(data:image/[a-z+]+;base64,)([A-Za-z0-9+/=]{${IMAGE_MIN_BASE64_CHARS},})`, "g");
|
|
21
|
+
/** Cheap check on raw bytes before anyone decodes a whole transcript to text. */
|
|
22
|
+
export function mightHoldEmbeddedImages(bytes) {
|
|
23
|
+
return bytes.includes("base64") || bytes.includes('"data"');
|
|
24
|
+
}
|
|
25
|
+
/** Every embedded image in `text`, in order. Runs that are not images are skipped. */
|
|
26
|
+
export function findEmbeddedImages(text) {
|
|
27
|
+
const found = [];
|
|
28
|
+
for (const match of text.matchAll(BASE64_RUN)) {
|
|
29
|
+
const isDataField = match[1] !== undefined;
|
|
30
|
+
const run = (isDataField ? match[2] : match[4]) ?? "";
|
|
31
|
+
const matchStart = match.index ?? 0;
|
|
32
|
+
const base64Start = matchStart + (isDataField ? match[1].length : match[3].length);
|
|
33
|
+
const bytes = Buffer.from(run, "base64");
|
|
34
|
+
const mediaType = sniffImageMediaType(bytes);
|
|
35
|
+
if (!mediaType)
|
|
36
|
+
continue;
|
|
37
|
+
found.push({
|
|
38
|
+
replaceStart: isDataField ? base64Start : matchStart,
|
|
39
|
+
replaceEnd: base64Start + run.length,
|
|
40
|
+
base64Start,
|
|
41
|
+
base64End: base64Start + run.length,
|
|
42
|
+
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
43
|
+
mediaType,
|
|
44
|
+
byteSize: bytes.byteLength,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return found;
|
|
48
|
+
}
|
|
49
|
+
/** The base64 text of one found image, sliced from the transcript only when it is about to be sent. */
|
|
50
|
+
export function base64Of(text, image) {
|
|
51
|
+
return text.slice(image.base64Start, image.base64End);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* `text` with the chosen images swapped for their markers. `replace` says which images; the rest
|
|
55
|
+
* stay byte for byte. Returns how many characters came out, so the caller can log bytes saved.
|
|
56
|
+
*/
|
|
57
|
+
export function replaceImagesWithMarkers(text, images, replace) {
|
|
58
|
+
const pieces = [];
|
|
59
|
+
let cursor = 0;
|
|
60
|
+
let replacedCount = 0;
|
|
61
|
+
let charsSaved = 0;
|
|
62
|
+
for (const image of images) {
|
|
63
|
+
if (!replace(image))
|
|
64
|
+
continue;
|
|
65
|
+
const marker = formatImageMarker({
|
|
66
|
+
sha256: image.sha256,
|
|
67
|
+
mediaType: image.mediaType,
|
|
68
|
+
byteSize: image.byteSize,
|
|
69
|
+
});
|
|
70
|
+
pieces.push(text.slice(cursor, image.replaceStart), marker);
|
|
71
|
+
cursor = image.replaceEnd;
|
|
72
|
+
replacedCount += 1;
|
|
73
|
+
charsSaved += image.replaceEnd - image.replaceStart - marker.length;
|
|
74
|
+
}
|
|
75
|
+
if (replacedCount === 0)
|
|
76
|
+
return { text, replacedCount: 0, charsSaved: 0 };
|
|
77
|
+
pieces.push(text.slice(cursor));
|
|
78
|
+
return { text: pieces.join(""), replacedCount, charsSaved };
|
|
79
|
+
}
|
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>;
|