pi-muse-tether 0.2.3 → 0.2.4
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/CHANGELOG.md +4 -0
- package/package.json +1 -1
- package/src/tool-result-receipt.ts +79 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.4 — 2026-11-24
|
|
4
|
+
|
|
5
|
+
- Tool results containing images now work with Muse Code 1.4.25, which includes image dimensions in its placeholder text.
|
|
6
|
+
|
|
3
7
|
## 0.2.3 — 2026-11-23
|
|
4
8
|
|
|
5
9
|
- Tests now refuse to write to the real request ledger, even when a test path points to it indirectly.
|
package/package.json
CHANGED
|
@@ -10,7 +10,8 @@ const imageMagic: Record<string, (b: Buffer) => boolean> = {
|
|
|
10
10
|
"image/gif": b => ["GIF87a", "GIF89a"].includes(b.subarray(0,6).toString("ascii")),
|
|
11
11
|
"image/webp": b => b.subarray(0,4).toString("ascii") === "RIFF" && b.subarray(8,12).toString("ascii") === "WEBP",
|
|
12
12
|
};
|
|
13
|
-
|
|
13
|
+
type ImageDimensions = { width: number; height: number };
|
|
14
|
+
function imageDeclarations(sent: BridgeResult, dimensions?: Map<number, ImageDimensions>): UnverifiableImage[] {
|
|
14
15
|
const images: UnverifiableImage[] = [];
|
|
15
16
|
for (const [partIndex, part] of sent.content.entries()) {
|
|
16
17
|
if (part.type === "text" && typeof part.text === "string") continue;
|
|
@@ -19,6 +20,8 @@ function imageDeclarations(sent: BridgeResult): UnverifiableImage[] {
|
|
|
19
20
|
const bytes = Buffer.from(part.data, "base64");
|
|
20
21
|
if (bytes.toString("base64") !== part.data || !imageMagic[part.mimeType](bytes))
|
|
21
22
|
throw new Error("Muse tool result cannot verify MCP parts: malformed image encoding or MIME signature");
|
|
23
|
+
const size = dimensions && parseImageDimensions(bytes, part.mimeType);
|
|
24
|
+
if (size) dimensions!.set(partIndex, size);
|
|
22
25
|
images.push({ partIndex, sha256: createHash("sha256").update(bytes).digest("hex"), byteLen: bytes.length, mimeType: part.mimeType,
|
|
23
26
|
...(sent.isError === true ? {modelVisibility: "omitted-error-result" as const} : {}) });
|
|
24
27
|
}
|
|
@@ -27,6 +30,71 @@ function imageDeclarations(sent: BridgeResult): UnverifiableImage[] {
|
|
|
27
30
|
export function assertToolResultVerifiable(sent: BridgeResult): void {
|
|
28
31
|
imageDeclarations(sent);
|
|
29
32
|
}
|
|
33
|
+
|
|
34
|
+
// Header facts only; signatures have already been checked. Replace this
|
|
35
|
+
// local reader with shared result-parts image identities when available.
|
|
36
|
+
function parseImageDimensions(bytes: Buffer, mimeType: string): ImageDimensions | undefined {
|
|
37
|
+
let width = 0, height = 0;
|
|
38
|
+
if (mimeType === "image/png" && bytes.length >= 24 && bytes.readUInt32BE(8) === 13 &&
|
|
39
|
+
bytes.toString("latin1", 12, 16) === "IHDR") {
|
|
40
|
+
width = bytes.readUInt32BE(16);
|
|
41
|
+
height = bytes.readUInt32BE(20);
|
|
42
|
+
} else if (mimeType === "image/gif" && bytes.length >= 10) {
|
|
43
|
+
width = bytes.readUInt16LE(6);
|
|
44
|
+
height = bytes.readUInt16LE(8);
|
|
45
|
+
} else if (mimeType === "image/webp" && bytes.length >= 20) {
|
|
46
|
+
const riffEnd = bytes.readUInt32LE(4) + 8;
|
|
47
|
+
const chunkLength = bytes.readUInt32LE(16);
|
|
48
|
+
if (riffEnd > bytes.length || 20 + chunkLength + (chunkLength % 2) > riffEnd) return;
|
|
49
|
+
const kind = bytes.toString("latin1", 12, 16);
|
|
50
|
+
if (kind === "VP8X" && chunkLength >= 10) {
|
|
51
|
+
width = 1 + bytes.readUIntLE(24, 3);
|
|
52
|
+
height = 1 + bytes.readUIntLE(27, 3);
|
|
53
|
+
} else if (kind === "VP8L" && chunkLength >= 5 && bytes[20] === 0x2f) {
|
|
54
|
+
const bits = bytes.readUInt32LE(21);
|
|
55
|
+
width = 1 + (bits & 0x3fff);
|
|
56
|
+
height = 1 + ((bits >>> 14) & 0x3fff);
|
|
57
|
+
} else if (kind === "VP8 " && chunkLength >= 10 && (bytes[20] & 1) === 0 &&
|
|
58
|
+
bytes[23] === 0x9d && bytes[24] === 0x01 && bytes[25] === 0x2a) {
|
|
59
|
+
width = bytes.readUInt16LE(26) & 0x3fff;
|
|
60
|
+
height = bytes.readUInt16LE(28) & 0x3fff;
|
|
61
|
+
}
|
|
62
|
+
} else if (mimeType === "image/jpeg") {
|
|
63
|
+
let i = 2;
|
|
64
|
+
while (i < bytes.length && bytes[i++] === 0xff) {
|
|
65
|
+
while (bytes[i] === 0xff) i++;
|
|
66
|
+
if (i >= bytes.length) break;
|
|
67
|
+
const marker = bytes[i++];
|
|
68
|
+
if (marker === 0xd9 || marker === 0xda || marker === 0x00) break;
|
|
69
|
+
if (marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) continue;
|
|
70
|
+
if (i + 2 > bytes.length) break;
|
|
71
|
+
const length = bytes.readUInt16BE(i);
|
|
72
|
+
if (length < 2 || i + length > bytes.length) break;
|
|
73
|
+
if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)) {
|
|
74
|
+
if (length >= 8) {
|
|
75
|
+
height = bytes.readUInt16BE(i + 3);
|
|
76
|
+
width = bytes.readUInt16BE(i + 5);
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
i += length;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (width && height) return { width, height };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const imagePlaceholder = /^\[MCP image content: media_type=(image\/(?:png|jpeg|webp|gif)), bytes=([1-9]\d*)(?:, dimensions=([1-9]\d*x[1-9]\d*)(?:, source_dimensions=([1-9]\d*x[1-9]\d*))?)?\]$/;
|
|
87
|
+
function matchesMuseImagePlaceholder(line: string, image: UnverifiableImage, size?: ImageDimensions): boolean {
|
|
88
|
+
const match = imagePlaceholder.exec(line);
|
|
89
|
+
if (!match || match[0] !== line) return false;
|
|
90
|
+
const [, mime, bytes, dimensions, sourceDimensions] = match;
|
|
91
|
+
if (!dimensions) return mime === image.mimeType && bytes === String(image.byteLen);
|
|
92
|
+
if (!size) return false;
|
|
93
|
+
const source = `${size.width}x${size.height}`;
|
|
94
|
+
// Muse can re-encode an image with or without changing its dimensions.
|
|
95
|
+
// The normalized MIME and size are descriptive, not evidence of source bytes.
|
|
96
|
+
return sourceDimensions ? sourceDimensions === source && dimensions !== source : dimensions === source;
|
|
97
|
+
}
|
|
30
98
|
/** visibleBytes includes any displayed ellipsis; candidate offsets address the original projection. */
|
|
31
99
|
export type TruncatedView = { visibleBytes: number; projectionBytes: number; omittedOffsetBytes: number; omittedByteLen: number; sha256: string; ellipsisIdentity: "not-present" | "generated" | "ambiguous"; candidateOmittedOffsetsBytes: number[]; sources: {partIndex:number;partOffsetBytes:number;byteLen:number}[] };
|
|
32
100
|
/** MSP 1.4.14 live capture: 65,536 view bytes ended with U+2026, after an
|
|
@@ -36,7 +104,8 @@ function viewPrefixes(visible: string, full: string): string[] {
|
|
|
36
104
|
return candidates.filter(prefix => !(prefix.length && /[\uD800-\uDBFF]/.test(prefix.at(-1)!)) && full.startsWith(prefix));
|
|
37
105
|
}
|
|
38
106
|
export function toolResultReceipt(item: any, sent: BridgeResult, stored?: string, onRedactions?: (spans: VendorRedaction[]) => void, onImages?: (images: UnverifiableImage[]) => void, onTruncatedView?: (view: TruncatedView) => void): BridgeResult {
|
|
39
|
-
const
|
|
107
|
+
const dimensions = new Map<number, ImageDimensions>();
|
|
108
|
+
const images = imageDeclarations(sent, sent.isError === true ? undefined : dimensions);
|
|
40
109
|
if (images.length && !onImages) throw new Error("Muse image receipt requires declared-unverifiable reporting");
|
|
41
110
|
if (typeof item.visibleOutput !== "string") throw new Error("Muse tool result missing visibleOutput");
|
|
42
111
|
if (item.truncated !== undefined && typeof item.truncated !== "boolean")
|
|
@@ -60,25 +129,29 @@ export function toolResultReceipt(item: any, sent: BridgeResult, stored?: string
|
|
|
60
129
|
}
|
|
61
130
|
// Construct the ordered native projection from Pi parts. Never search/remove
|
|
62
131
|
// placeholder-shaped strings: literal tool text and leading LF remain bytes.
|
|
63
|
-
let expected = "";
|
|
132
|
+
let expected = "", expectedLine = 0;
|
|
133
|
+
const actualLines = images.length && sent.isError !== true ? actual.split("\n") : [];
|
|
64
134
|
const protectedImages: { start: number; end: number }[] = [];
|
|
65
135
|
const textSources: {partIndex:number; start:number; end:number}[] = [];
|
|
66
136
|
for (const [index, part] of sent.content.entries()) {
|
|
67
137
|
if (part.type === "text") {
|
|
68
138
|
// Native string folding ignores leading empty chunks but preserves a
|
|
69
139
|
// separator before an empty chunk after nonempty output (captured live).
|
|
70
|
-
if (expected) expected += "\n";
|
|
140
|
+
if (expected) { expected += "\n"; expectedLine++; }
|
|
71
141
|
const start = Buffer.byteLength(expected);
|
|
72
142
|
expected += part.text;
|
|
143
|
+
if (actualLines.length) expectedLine += part.text.split("\n").length - 1;
|
|
73
144
|
textSources.push({partIndex:index,start,end:Buffer.byteLength(expected)});
|
|
74
145
|
} else {
|
|
75
146
|
// The official client's failed MCP path projects text only. Images were
|
|
76
147
|
// delivered to MCP, NOT exposed to the model; declare that known loss.
|
|
77
148
|
if (sent.isError === true) continue;
|
|
78
149
|
const image = images.find(image => image.partIndex === index)!;
|
|
79
|
-
if (expected) expected += "\n";
|
|
150
|
+
if (expected) { expected += "\n"; expectedLine++; }
|
|
80
151
|
const start = expected.length;
|
|
81
|
-
|
|
152
|
+
const line = actualLines[expectedLine];
|
|
153
|
+
expected += line !== undefined && matchesMuseImagePlaceholder(line, image, dimensions.get(index)) ? line :
|
|
154
|
+
`[MCP image content: media_type=${image.mimeType}, bytes=${image.byteLen}]`;
|
|
82
155
|
protectedImages.push({ start, end: expected.length });
|
|
83
156
|
}
|
|
84
157
|
}
|