1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
import type {
AgentEvent,
ChatMessage,
ModelInfo,
ProviderContract,
ProviderEvent,
ToolContract,
} from "@dispatch/kernel";
import { describe, expect, it, vi } from "vitest";
import { createVisionHandoffService, type VisionHandoffDeps } from "./service.js";
// ── Test doubles (outermost-edge fakes — NOT @dispatch/* mocks) ──────────────
function makeVisionProvider(
describe: (imageUrl: string) => string,
id = "umans",
): ProviderContract {
return {
id,
stream: vi.fn(
(
messages: readonly ChatMessage[],
_tools: readonly ToolContract[],
): AsyncIterable<ProviderEvent> => {
const img = messages.flatMap((m) => m.chunks).find((c) => c.type === "image");
const url = img && img.type === "image" ? img.url : "";
const text = describe(url);
async function* gen(): AsyncIterable<ProviderEvent> {
yield { type: "text-delta", delta: text };
yield { type: "finish", reason: "stop" };
}
return gen();
},
),
};
}
function makeDeps(overrides: Partial<VisionHandoffDeps> = {}): VisionHandoffDeps {
const visionProvider = makeVisionProvider((url) => `DESCRIPTION of ${url}`);
const catalog = ["umans/umans-kimi-k2.7", "umans/umans-glm-5.2"];
const infoMap: Record<string, ModelInfo> = {
"umans/umans-kimi-k2.7": { id: "umans-kimi-k2.7", vision: true },
"umans/umans-glm-5.2": { id: "umans-glm-5.2" },
};
return {
credentialStore: {
listCatalog: vi.fn(async () => catalog),
getModelInfo: vi.fn(async (name: string) => infoMap[name]),
resolve: vi.fn((name: string) => {
if (name === "umans/umans-kimi-k2.7")
return { providerId: "umans", model: "umans-kimi-k2.7" };
if (name === "umans/umans-glm-5.2") return { providerId: "umans", model: "umans-glm-5.2" };
return undefined;
}),
},
resolveModel: vi.fn((name: string) =>
name === "umans/umans-kimi-k2.7" || name === "umans/umans-glm-5.2"
? { provider: visionProvider, model: name.split("/")[1] }
: undefined,
),
readFileAsDataUrl: vi.fn(async (path: string) => `data:image/png;base64,FILE(${path})`),
setConversationTitle: vi.fn(async (_conversationId: string, _title: string) => {}),
...overrides,
};
}
describe("VisionHandoffService.isVisionCapable", () => {
it("returns true for kimi (via ModelInfo)", async () => {
const svc = createVisionHandoffService(makeDeps());
expect(await svc.isVisionCapable("umans/umans-kimi-k2.7")).toBe(true);
});
it("returns false for glm-5.2", async () => {
const svc = createVisionHandoffService(makeDeps());
expect(await svc.isVisionCapable("umans/umans-glm-5.2")).toBe(false);
});
it("returns false for undefined model name", async () => {
const svc = createVisionHandoffService(makeDeps());
expect(await svc.isVisionCapable(undefined)).toBe(false);
});
});
describe("VisionHandoffService.resolveVisionModel", () => {
it("resolves the kimi model from the catalog", async () => {
const svc = createVisionHandoffService(makeDeps());
const vision = await svc.resolveVisionModel();
expect(vision?.modelName).toBe("umans/umans-kimi-k2.7");
expect(vision?.model).toBe("umans-kimi-k2.7");
});
it("excludes the given model", async () => {
const svc = createVisionHandoffService(makeDeps());
const vision = await svc.resolveVisionModel("umans/umans-kimi-k2.7");
expect(vision).toBeUndefined();
});
});
describe("VisionHandoffService.prepareForProvider", () => {
it("passes messages through unchanged when the model is vision-capable", async () => {
const deps = makeDeps();
const svc = createVisionHandoffService(deps);
const messages: ChatMessage[] = [
{
role: "user",
chunks: [
{ type: "text", text: "What's this?" },
{ type: "image", url: "data:image/png;base64,abc" },
],
},
];
const result = await svc.prepareForProvider(messages, "umans/umans-kimi-k2.7");
expect(result).toBe(messages); // same reference — no copy, no change
});
it("passes messages through unchanged when there are no images", async () => {
const deps = makeDeps();
const svc = createVisionHandoffService(deps);
const messages: ChatMessage[] = [{ role: "user", chunks: [{ type: "text", text: "hi" }] }];
const result = await svc.prepareForProvider(messages, "umans/umans-glm-5.2");
expect(result).toBe(messages);
});
it("replaces image chunks with numbered placeholders for a non-vision model", async () => {
const deps = makeDeps();
const svc = createVisionHandoffService(deps);
const messages: ChatMessage[] = [
{
role: "user",
chunks: [
{ type: "text", text: "Describe this" },
{ type: "image", url: "data:image/png;base64,img1" },
],
},
];
const result = await svc.prepareForProvider(messages, "umans/umans-glm-5.2", {
conversationId: "conv-1",
});
expect(result).toHaveLength(1);
const chunks = result[0]?.chunks;
expect(chunks).toHaveLength(2);
// Text chunk unchanged.
expect(chunks?.[0]).toEqual({ type: "text", text: "Describe this" });
// Image chunk → placeholder text.
expect(chunks?.[1]?.type).toBe("text");
const placeholder = (chunks?.[1] as { text: string }).text;
expect(placeholder).toContain("Image 1");
expect(placeholder).toContain("consult_vision");
});
it("assigns sequential image IDs across multiple messages", async () => {
const deps = makeDeps();
const svc = createVisionHandoffService(deps);
const messages: ChatMessage[] = [
{ role: "user", chunks: [{ type: "image", url: "data:image/png;base64,a" }] },
{ role: "assistant", chunks: [{ type: "text", text: "ok" }] },
{ role: "user", chunks: [{ type: "image", url: "data:image/png;base64,b" }] },
];
const result = await svc.prepareForProvider(messages, "umans/umans-glm-5.2", {
conversationId: "conv-1",
});
// First image → Image 1, second → Image 2.
expect((result[0]?.chunks[0] as { text: string }).text).toContain("Image 1");
// Assistant message unchanged.
expect(result[1]?.chunks[0]?.type).toBe("text");
expect((result[2]?.chunks[0] as { text: string }).text).toContain("Image 2");
});
it("registers images so getRegisteredImage can look them up", async () => {
const deps = makeDeps();
const svc = createVisionHandoffService(deps);
const messages: ChatMessage[] = [
{
role: "user",
chunks: [{ type: "image", url: "data:image/png;base64,registered" }],
},
];
await svc.prepareForProvider(messages, "umans/umans-glm-5.2", { conversationId: "conv-42" });
const img = svc.getRegisteredImage("conv-42", 1);
expect(img?.url).toBe("data:image/png;base64,registered");
});
it("uses no-vision placeholder when no vision model is available", async () => {
const deps = makeDeps();
(deps.credentialStore.listCatalog as ReturnType<typeof vi.fn>).mockResolvedValue([]);
const svc = createVisionHandoffService(deps);
const messages: ChatMessage[] = [
{ role: "user", chunks: [{ type: "image", url: "data:image/png;base64,abc" }] },
];
const result = await svc.prepareForProvider(messages, "umans/umans-glm-5.2", {
conversationId: "conv-1",
});
const text = (result[0]?.chunks[0] as { text: string }).text;
expect(text).toContain("no vision-capable model");
expect(text).not.toContain("consult_vision");
});
});
describe("VisionHandoffService.consultVision", () => {
function makeOrchestratorDouble(response: string): {
orchestrator: NonNullable<
VisionHandoffDeps["resolveOrchestrator"] extends () => infer T ? T : never
>;
handleMessage: ReturnType<typeof vi.fn>;
} {
const handleMessage = vi.fn(
async (input: {
conversationId: string;
text: string;
onEvent: (event: AgentEvent) => void;
}): Promise<void> => {
input.onEvent({
type: "text-delta",
conversationId: input.conversationId,
turnId: "t1",
delta: response,
});
input.onEvent({
type: "done",
conversationId: input.conversationId,
turnId: "t1",
reason: "stop",
});
},
);
return { orchestrator: { handleMessage }, handleMessage };
}
it("opens a new consultation with a pasted image and returns convId + response", async () => {
const deps = makeDeps();
const { orchestrator, handleMessage } = makeOrchestratorDouble("The error is on line 12.");
deps.resolveOrchestrator = () => orchestrator;
const svc = createVisionHandoffService(deps);
// Register an image first (as prepareForProvider would).
const messages: ChatMessage[] = [
{ role: "user", chunks: [{ type: "image", url: "data:image/png;base64,img1" }] },
];
await svc.prepareForProvider(messages, "umans/umans-glm-5.2", { conversationId: "conv-1" });
const result = await svc.consultVision("What error is shown?", {
conversationId: "conv-1",
imageIds: [1],
});
expect("error" in result).toBe(false);
if (!("error" in result)) {
expect(result.conversationId).toBeTruthy();
expect(result.response).toContain("line 12");
expect(result.response).toContain(result.conversationId);
expect(result.response).toContain("dispatch CLI");
}
// The orchestrator was called with the vision model + the image.
expect(handleMessage).toHaveBeenCalledOnce();
const call = handleMessage.mock.calls[0]?.[0];
expect(call.modelName).toBe("umans/umans-kimi-k2.7");
expect(call.images).toHaveLength(1);
expect(call.images?.[0]?.url).toBe("data:image/png;base64,img1");
});
it("labels the consultation tab with an 'IMAGE - ' prefixed title", async () => {
const deps = makeDeps();
const { orchestrator } = makeOrchestratorDouble("The error is on line 12.");
deps.resolveOrchestrator = () => orchestrator;
const svc = createVisionHandoffService(deps);
// Register an image first (as prepareForProvider would).
const messages: ChatMessage[] = [
{ role: "user", chunks: [{ type: "image", url: "data:image/png;base64,img1" }] },
];
await svc.prepareForProvider(messages, "umans/umans-glm-5.2", { conversationId: "conv-1" });
const result = await svc.consultVision("What error is shown?", {
conversationId: "conv-1",
imageIds: [1],
});
expect("error" in result).toBe(false);
// The title was set with the IMAGE - prefix + the question.
expect(deps.setConversationTitle).toHaveBeenCalledOnce();
const [titleConvId, title] = (deps.setConversationTitle as ReturnType<typeof vi.fn>).mock
.calls[0];
expect(titleConvId).toBe((result as { conversationId: string }).conversationId);
expect(title).toBe("IMAGE - What error is shown?");
});
it("does not call setConversationTitle when it is not provided", async () => {
const deps = makeDeps({ setConversationTitle: undefined });
const { orchestrator } = makeOrchestratorDouble("response");
deps.resolveOrchestrator = () => orchestrator;
const svc = createVisionHandoffService(deps);
const messages: ChatMessage[] = [
{ role: "user", chunks: [{ type: "image", url: "data:image/png;base64,img1" }] },
];
await svc.prepareForProvider(messages, "umans/umans-glm-5.2", { conversationId: "conv-1" });
// Should NOT throw — setConversationTitle is optional.
const result = await svc.consultVision("What?", {
conversationId: "conv-1",
imageIds: [1],
});
expect("error" in result).toBe(false);
});
it("opens a consultation with a file path image", async () => {
const deps = makeDeps();
const { orchestrator } = makeOrchestratorDouble("It's a diagram.");
deps.resolveOrchestrator = () => orchestrator;
const svc = createVisionHandoffService(deps);
const result = await svc.consultVision("What is this diagram?", {
conversationId: "conv-1",
path: "diagram.png",
cwd: "/work",
});
expect("error" in result).toBe(false);
expect(deps.readFileAsDataUrl).toHaveBeenCalledWith("diagram.png", "/work");
});
it("returns an error when imageId is not registered", async () => {
const deps = makeDeps();
const { orchestrator } = makeOrchestratorDouble("response");
deps.resolveOrchestrator = () => orchestrator;
const svc = createVisionHandoffService(deps);
const result = await svc.consultVision("What?", {
conversationId: "conv-1",
imageIds: [99], // not registered
});
expect("error" in result).toBe(true);
if ("error" in result) {
expect(result.error).toContain("Image 99");
}
});
it("returns an error when no orchestrator is available", async () => {
const deps = makeDeps();
// No resolveOrchestrator provided.
const svc = createVisionHandoffService(deps);
const result = await svc.consultVision("What?", {
conversationId: "conv-1",
imageIds: [1],
});
expect("error" in result).toBe(true);
});
it("returns an error when no vision model is available", async () => {
const deps = makeDeps();
(deps.credentialStore.listCatalog as ReturnType<typeof vi.fn>).mockResolvedValue([]);
const { orchestrator } = makeOrchestratorDouble("response");
deps.resolveOrchestrator = () => orchestrator;
const svc = createVisionHandoffService(deps);
const result = await svc.consultVision("What?", {
conversationId: "conv-1",
imageIds: [1],
});
expect("error" in result).toBe(true);
if ("error" in result) {
expect(result.error).toContain("No vision-capable model");
}
});
it("returns an error when no image source is provided", async () => {
const deps = makeDeps();
const { orchestrator } = makeOrchestratorDouble("response");
deps.resolveOrchestrator = () => orchestrator;
const svc = createVisionHandoffService(deps);
const result = await svc.consultVision("What?", {
conversationId: "conv-1",
});
expect("error" in result).toBe(true);
});
});
|