@vellumai/assistant 0.10.7-dev.202607082203.bf32263 → 0.10.7-dev.202607082226.4246ddb
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/package.json +1 -1
- package/src/__tests__/conversation-history-web-search.test.ts +6 -0
- package/src/__tests__/persist-media-references.test.ts +117 -0
- package/src/__tests__/server-history-render.test.ts +30 -0
- package/src/daemon/conversation-agent-loop-handlers.ts +18 -5
- package/src/daemon/handlers/shared.ts +9 -9
- package/src/daemon/persist-media-references.ts +151 -0
package/package.json
CHANGED
|
@@ -681,6 +681,12 @@ describe("web_search_tool_result structural guard", () => {
|
|
|
681
681
|
// reasoning as caption-blocks.ts above.
|
|
682
682
|
"providers/media-resolve.ts",
|
|
683
683
|
|
|
684
|
+
// Walks a tool_result's rich `contentBlocks` to materialize nested base64
|
|
685
|
+
// media into workspace references at persist time. web_search_tool_result
|
|
686
|
+
// blocks carry no contentBlocks, so only tool_result is relevant. Same
|
|
687
|
+
// reasoning as media-resolve.ts above.
|
|
688
|
+
"daemon/persist-media-references.ts",
|
|
689
|
+
|
|
684
690
|
// Detects turn boundaries by checking whether a user message carries any
|
|
685
691
|
// tool_result block (internal continuation) vs. none (genuine user prompt).
|
|
686
692
|
// A web_search_tool_result-only message is also internal, but treating one
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, mock, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
mock.module("../util/logger.js", () => ({
|
|
4
|
+
getLogger: () =>
|
|
5
|
+
new Proxy({} as Record<string, unknown>, { get: () => () => {} }),
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
import { referenceMediaBlocksForPersist } from "../daemon/persist-media-references.js";
|
|
9
|
+
import { getAttachmentsForMessage } from "../persistence/attachments-store.js";
|
|
10
|
+
import {
|
|
11
|
+
addMessage,
|
|
12
|
+
createConversation,
|
|
13
|
+
} from "../persistence/conversation-crud.js";
|
|
14
|
+
import { getDb } from "../persistence/db-connection.js";
|
|
15
|
+
import { initializeDb } from "../persistence/db-init.js";
|
|
16
|
+
import { mediaSourceBytes } from "../providers/media-resolve.js";
|
|
17
|
+
import type { ContentBlock } from "../providers/types.js";
|
|
18
|
+
|
|
19
|
+
await initializeDb();
|
|
20
|
+
|
|
21
|
+
function resetTables() {
|
|
22
|
+
const db = getDb();
|
|
23
|
+
db.run("DELETE FROM message_attachments");
|
|
24
|
+
db.run("DELETE FROM attachments");
|
|
25
|
+
db.run("DELETE FROM messages");
|
|
26
|
+
db.run("DELETE FROM conversations");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// "aGVsbG8=" = "hello"
|
|
30
|
+
const IMAGE_B64 = Buffer.from("hello").toString("base64");
|
|
31
|
+
|
|
32
|
+
describe("referenceMediaBlocksForPersist", () => {
|
|
33
|
+
beforeEach(resetTables);
|
|
34
|
+
|
|
35
|
+
test("materializes tool_result base64 media into a linked workspace_ref", async () => {
|
|
36
|
+
const conv = createConversation();
|
|
37
|
+
const msg = await addMessage(conv.id, "user", "tool results");
|
|
38
|
+
|
|
39
|
+
const blocks: ContentBlock[] = [
|
|
40
|
+
{
|
|
41
|
+
type: "tool_result",
|
|
42
|
+
tool_use_id: "tool-1",
|
|
43
|
+
content: "screenshot captured",
|
|
44
|
+
contentBlocks: [
|
|
45
|
+
{
|
|
46
|
+
type: "image",
|
|
47
|
+
source: {
|
|
48
|
+
type: "base64",
|
|
49
|
+
media_type: "image/png",
|
|
50
|
+
data: IMAGE_B64,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
const referenced = referenceMediaBlocksForPersist(
|
|
58
|
+
conv.id,
|
|
59
|
+
conv.createdAt,
|
|
60
|
+
msg.id,
|
|
61
|
+
blocks,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
// The nested image is now a workspace_ref, not inline base64.
|
|
65
|
+
const toolResult = referenced[0] as Extract<
|
|
66
|
+
ContentBlock,
|
|
67
|
+
{ type: "tool_result" }
|
|
68
|
+
>;
|
|
69
|
+
const nested = toolResult.contentBlocks![0] as Extract<
|
|
70
|
+
ContentBlock,
|
|
71
|
+
{ type: "image" }
|
|
72
|
+
>;
|
|
73
|
+
expect(nested.source.type).toBe("workspace_ref");
|
|
74
|
+
expect(JSON.stringify(referenced)).not.toContain(IMAGE_B64);
|
|
75
|
+
|
|
76
|
+
// The attachment row is linked to the message (GC anchor) and its bytes
|
|
77
|
+
// resolve back to the original image.
|
|
78
|
+
if (nested.source.type !== "workspace_ref") throw new Error("expected ref");
|
|
79
|
+
const linked = getAttachmentsForMessage(msg.id);
|
|
80
|
+
expect(linked.map((a) => a.id)).toEqual([nested.source.attachmentId]);
|
|
81
|
+
expect(mediaSourceBytes(nested.source)?.toString()).toBe("hello");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("leaves an already-referenced block untouched and creates no row", async () => {
|
|
85
|
+
const conv = createConversation();
|
|
86
|
+
const msg = await addMessage(conv.id, "user", "tool results");
|
|
87
|
+
|
|
88
|
+
const blocks: ContentBlock[] = [
|
|
89
|
+
{
|
|
90
|
+
type: "tool_result",
|
|
91
|
+
tool_use_id: "tool-1",
|
|
92
|
+
content: "ok",
|
|
93
|
+
contentBlocks: [
|
|
94
|
+
{
|
|
95
|
+
type: "image",
|
|
96
|
+
source: {
|
|
97
|
+
type: "workspace_ref",
|
|
98
|
+
media_type: "image/png",
|
|
99
|
+
attachmentId: "att-existing",
|
|
100
|
+
sizeBytes: 5,
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
},
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
const referenced = referenceMediaBlocksForPersist(
|
|
108
|
+
conv.id,
|
|
109
|
+
conv.createdAt,
|
|
110
|
+
msg.id,
|
|
111
|
+
blocks,
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
expect(referenced).toEqual(blocks);
|
|
115
|
+
expect(getAttachmentsForMessage(msg.id)).toHaveLength(0);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -257,6 +257,36 @@ describe("renderHistoryContent", () => {
|
|
|
257
257
|
]);
|
|
258
258
|
});
|
|
259
259
|
|
|
260
|
+
test("resolves a workspace_ref tool-result image back to base64", () => {
|
|
261
|
+
// "aGVsbG8=" = "hello" — a stand-in for screenshot bytes.
|
|
262
|
+
const stored = uploadAttachment("shot.png", "image/png", "aGVsbG8=");
|
|
263
|
+
const output = renderHistoryContent([
|
|
264
|
+
{ type: "tool_use", id: "tu_1", name: "browser_screenshot", input: {} },
|
|
265
|
+
{
|
|
266
|
+
type: "tool_result",
|
|
267
|
+
tool_use_id: "tu_1",
|
|
268
|
+
content: "captured",
|
|
269
|
+
is_error: false,
|
|
270
|
+
contentBlocks: [
|
|
271
|
+
{
|
|
272
|
+
type: "image",
|
|
273
|
+
source: {
|
|
274
|
+
type: "workspace_ref",
|
|
275
|
+
media_type: "image/png",
|
|
276
|
+
attachmentId: stored.id,
|
|
277
|
+
sizeBytes: 5,
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
},
|
|
282
|
+
]);
|
|
283
|
+
|
|
284
|
+
// The persisted reference is resolved to base64 so the wire contract
|
|
285
|
+
// (imageDataList base64) is unchanged for the client.
|
|
286
|
+
expect(output.toolCalls[0].imageDataList).toEqual(["aGVsbG8="]);
|
|
287
|
+
expect(output.toolCalls[0].imageData).toBe("aGVsbG8=");
|
|
288
|
+
});
|
|
289
|
+
|
|
260
290
|
test("marks error tool results", () => {
|
|
261
291
|
const output = renderHistoryContent([
|
|
262
292
|
{ type: "tool_use", id: "tu_1", name: "bash", input: { command: "bad" } },
|
|
@@ -101,6 +101,7 @@ import type {
|
|
|
101
101
|
WebSearchMetadata,
|
|
102
102
|
WebSearchResultItem,
|
|
103
103
|
} from "./message-types/web-activity.js";
|
|
104
|
+
import { referenceMediaBlocksForPersist } from "./persist-media-references.js";
|
|
104
105
|
import type { TurnLatencyTracker } from "./turn-latency-tracker.js";
|
|
105
106
|
|
|
106
107
|
const log = getLogger("agent-loop-handlers");
|
|
@@ -1287,8 +1288,24 @@ export async function finalizePendingToolResultRow(
|
|
|
1287
1288
|
conversationId,
|
|
1288
1289
|
metadata,
|
|
1289
1290
|
);
|
|
1291
|
+
// `getConversation` returns `ConversationRow | null`, so `!= null` gates on a
|
|
1292
|
+
// real row (skipping media referencing / disk sync when the conversation was
|
|
1293
|
+
// not found rather than asking those helpers to resolve a missing id).
|
|
1294
|
+
const conv = getConversation(conversationId);
|
|
1295
|
+
// Swap any base64 media the tools produced (screenshots, generated images)
|
|
1296
|
+
// for workspace references so the blob stays in the attachment store, out of
|
|
1297
|
+
// this row and the lexical index. Runs once, here at finalize (on-arrival
|
|
1298
|
+
// writes keep base64 for durability); the send boundary re-inflates the refs.
|
|
1299
|
+
const blocks = buildToolResultBlocks(state.pendingToolResults);
|
|
1290
1300
|
const contentJson = JSON.stringify(
|
|
1291
|
-
|
|
1301
|
+
conv != null
|
|
1302
|
+
? referenceMediaBlocksForPersist(
|
|
1303
|
+
conversationId,
|
|
1304
|
+
conv.createdAt,
|
|
1305
|
+
rowId,
|
|
1306
|
+
blocks as ContentBlock[],
|
|
1307
|
+
)
|
|
1308
|
+
: blocks,
|
|
1292
1309
|
);
|
|
1293
1310
|
await persistLoopMessageContent(
|
|
1294
1311
|
rowId,
|
|
@@ -1297,10 +1314,6 @@ export async function finalizePendingToolResultRow(
|
|
|
1297
1314
|
rlog,
|
|
1298
1315
|
);
|
|
1299
1316
|
// Sync the row to the JSONL disk view so it stays in lockstep with the DB.
|
|
1300
|
-
// `getConversation` returns `ConversationRow | null`, so `!= null` gates on a
|
|
1301
|
-
// real row (skipping the sync when the conversation was not found rather than
|
|
1302
|
-
// asking the disk-view to resolve a missing id).
|
|
1303
|
-
const conv = getConversation(conversationId);
|
|
1304
1317
|
if (conv != null) {
|
|
1305
1318
|
syncMessageToDisk(conversationId, rowId, conv.createdAt);
|
|
1306
1319
|
}
|
|
@@ -11,7 +11,9 @@ import { getConfig } from "../../config/loader.js";
|
|
|
11
11
|
import type { LLMCallSite, Speed } from "../../config/schemas/llm.js";
|
|
12
12
|
import { ipcCall as gatewayIpcCall } from "../../ipc/gateway-client.js";
|
|
13
13
|
import type { SecretPromptResult } from "../../permissions/secret-prompt-types.js";
|
|
14
|
+
import { resolveMediaSourceData } from "../../providers/media-resolve.js";
|
|
14
15
|
import { isPlaceholderSentinelText } from "../../providers/placeholder-sentinels.js";
|
|
16
|
+
import type { MediaSource } from "../../providers/types.js";
|
|
15
17
|
import { broadcastMessage } from "../../runtime/assistant-event-hub.js";
|
|
16
18
|
import type { AuthContext } from "../../runtime/auth/types.js";
|
|
17
19
|
import * as pendingInteractions from "../../runtime/pending-interactions.js";
|
|
@@ -656,19 +658,17 @@ export function renderHistoryContent(
|
|
|
656
658
|
const resultContent =
|
|
657
659
|
typeof block.content === "string" ? block.content : "";
|
|
658
660
|
const isError = block.is_error === true;
|
|
659
|
-
// Extract
|
|
661
|
+
// Extract image data from persisted contentBlocks (e.g. browser_screenshot,
|
|
662
|
+
// image generation) as base64. The source may be inline base64 or a
|
|
663
|
+
// workspace reference; resolveMediaSourceData reads either back to base64.
|
|
660
664
|
const imageDataList: string[] = [];
|
|
661
665
|
if (Array.isArray(block.contentBlocks)) {
|
|
662
666
|
for (const cb of block.contentBlocks) {
|
|
663
|
-
if (
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
isRecord(cb.source) &&
|
|
667
|
-
typeof (cb.source as Record<string, unknown>).data === "string"
|
|
668
|
-
) {
|
|
669
|
-
imageDataList.push(
|
|
670
|
-
(cb.source as Record<string, unknown>).data as string,
|
|
667
|
+
if (isRecord(cb) && cb.type === "image" && isRecord(cb.source)) {
|
|
668
|
+
const resolved = resolveMediaSourceData(
|
|
669
|
+
cb.source as unknown as MediaSource,
|
|
671
670
|
);
|
|
671
|
+
if (resolved) imageDataList.push(resolved.data);
|
|
672
672
|
}
|
|
673
673
|
}
|
|
674
674
|
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert inline base64 media blocks to workspace references at persist time —
|
|
3
|
+
* the inverse of `providers/media-resolve.ts` (`resolveMediaReferences`, which
|
|
4
|
+
* re-inflates references to base64 at the provider send boundary).
|
|
5
|
+
*
|
|
6
|
+
* Tool results carry generated media (browser screenshots, image generation)
|
|
7
|
+
* as base64 `image`/`file` blocks nested in a `tool_result`'s `contentBlocks`.
|
|
8
|
+
* Persisting that base64 into `messages.content` bloats the row and the lexical
|
|
9
|
+
* index. Just before the finalized tool-result row is written,
|
|
10
|
+
* {@link referenceMediaBlocksForPersist} materializes each base64 media block
|
|
11
|
+
* into an attachment-store row and swaps its `source` for a
|
|
12
|
+
* {@link WorkspaceRefMediaSource}, keeping the blob in the attachment store.
|
|
13
|
+
*
|
|
14
|
+
* A block whose materialization fails is left as inline base64 so media is
|
|
15
|
+
* never lost. The walk descends into `tool_result.contentBlocks`; every other
|
|
16
|
+
* block passes through untouched.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { optimizeImageForTransport } from "../agent/image-optimize.js";
|
|
20
|
+
import { parseImageDimensions } from "../context/image-dimensions.js";
|
|
21
|
+
import {
|
|
22
|
+
createInlineAttachment,
|
|
23
|
+
linkAttachmentToMessage,
|
|
24
|
+
} from "../persistence/attachments-store.js";
|
|
25
|
+
import type {
|
|
26
|
+
ContentBlock,
|
|
27
|
+
FileContent,
|
|
28
|
+
ImageContent,
|
|
29
|
+
WorkspaceRefMediaSource,
|
|
30
|
+
} from "../providers/types.js";
|
|
31
|
+
import { getLogger } from "../util/logger.js";
|
|
32
|
+
|
|
33
|
+
const log = getLogger("persist-media-references");
|
|
34
|
+
|
|
35
|
+
/** A filename for an unnamed image block, derived from its MIME subtype. */
|
|
36
|
+
function imageFilename(mediaType: string): string {
|
|
37
|
+
const subtype = mediaType.split("/")[1]?.replace(/[^a-z0-9]/gi, "") || "png";
|
|
38
|
+
return `image.${subtype}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Dimensions to hint on an image reference. The model receives the
|
|
43
|
+
* transport-optimized image (`resolveMediaReferences` applies the same
|
|
44
|
+
* optimization at send time), so hint the optimized dimensions.
|
|
45
|
+
*/
|
|
46
|
+
function optimizedImageDimensions(
|
|
47
|
+
dataBase64: string,
|
|
48
|
+
mediaType: string,
|
|
49
|
+
): { width: number; height: number } | null {
|
|
50
|
+
const optimized = optimizeImageForTransport(dataBase64, mediaType);
|
|
51
|
+
return parseImageDimensions(optimized.data, optimized.mediaType);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Materialize one base64 media block into an attachment row linked to
|
|
56
|
+
* `messageId`, returning the block with a `workspace_ref` source — or null when
|
|
57
|
+
* the store write fails (caller keeps the inline base64 block).
|
|
58
|
+
*/
|
|
59
|
+
function referenceMediaBlock(
|
|
60
|
+
conversationId: string,
|
|
61
|
+
conversationCreatedAt: number,
|
|
62
|
+
messageId: string,
|
|
63
|
+
block: ImageContent | FileContent,
|
|
64
|
+
position: number,
|
|
65
|
+
): ContentBlock | null {
|
|
66
|
+
if (block.source.type !== "base64") return block;
|
|
67
|
+
const { data, media_type } = block.source;
|
|
68
|
+
const filename =
|
|
69
|
+
block.source.filename ??
|
|
70
|
+
(block.type === "image" ? imageFilename(media_type) : "attachment");
|
|
71
|
+
|
|
72
|
+
let stored: { id: string; sizeBytes: number };
|
|
73
|
+
try {
|
|
74
|
+
stored = createInlineAttachment(
|
|
75
|
+
conversationId,
|
|
76
|
+
conversationCreatedAt,
|
|
77
|
+
filename,
|
|
78
|
+
media_type,
|
|
79
|
+
data,
|
|
80
|
+
);
|
|
81
|
+
linkAttachmentToMessage(messageId, stored.id, position);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
log.warn(
|
|
84
|
+
{ err, mediaType: media_type, messageId },
|
|
85
|
+
"Failed to store tool-result media; persisting inline",
|
|
86
|
+
);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const source: WorkspaceRefMediaSource = {
|
|
91
|
+
type: "workspace_ref",
|
|
92
|
+
media_type,
|
|
93
|
+
attachmentId: stored.id,
|
|
94
|
+
sizeBytes: stored.sizeBytes,
|
|
95
|
+
...(block.source.filename !== undefined
|
|
96
|
+
? { filename: block.source.filename }
|
|
97
|
+
: {}),
|
|
98
|
+
};
|
|
99
|
+
if (block.type === "image") {
|
|
100
|
+
const dims = optimizedImageDimensions(data, media_type);
|
|
101
|
+
if (dims) {
|
|
102
|
+
source.width = dims.width;
|
|
103
|
+
source.height = dims.height;
|
|
104
|
+
}
|
|
105
|
+
return { type: "image", source };
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
type: "file",
|
|
109
|
+
source,
|
|
110
|
+
...(block.extracted_text !== undefined
|
|
111
|
+
? { extracted_text: block.extracted_text }
|
|
112
|
+
: {}),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Return a copy of `blocks` with every inline base64 `image`/`file` media block
|
|
118
|
+
* (including those nested in a `tool_result`'s `contentBlocks`) materialized
|
|
119
|
+
* into an attachment row linked to `messageId` and swapped for a
|
|
120
|
+
* `workspace_ref`. Blocks that are already references, carry no media, or fail
|
|
121
|
+
* to store are returned unchanged.
|
|
122
|
+
*/
|
|
123
|
+
export function referenceMediaBlocksForPersist(
|
|
124
|
+
conversationId: string,
|
|
125
|
+
conversationCreatedAt: number,
|
|
126
|
+
messageId: string,
|
|
127
|
+
blocks: ContentBlock[],
|
|
128
|
+
): ContentBlock[] {
|
|
129
|
+
// A single link position counter across all attachments in the message so
|
|
130
|
+
// their `message_attachments` rows keep content order.
|
|
131
|
+
let position = 0;
|
|
132
|
+
const convert = (block: ContentBlock): ContentBlock => {
|
|
133
|
+
if (block.type === "image" || block.type === "file") {
|
|
134
|
+
if (block.source.type !== "base64") return block;
|
|
135
|
+
return (
|
|
136
|
+
referenceMediaBlock(
|
|
137
|
+
conversationId,
|
|
138
|
+
conversationCreatedAt,
|
|
139
|
+
messageId,
|
|
140
|
+
block,
|
|
141
|
+
position++,
|
|
142
|
+
) ?? block
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
if (block.type === "tool_result" && block.contentBlocks?.length) {
|
|
146
|
+
return { ...block, contentBlocks: block.contentBlocks.map(convert) };
|
|
147
|
+
}
|
|
148
|
+
return block;
|
|
149
|
+
};
|
|
150
|
+
return blocks.map(convert);
|
|
151
|
+
}
|