Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(mistral): let Mistral bots use the Local VM (computerUse) and send images to vision models

Ooi Pin Juna28186704e20
diff --git a/server/drivers/mistral.test.ts b/server/drivers/mistral.test.ts
index c2fba04297..df7e0dfb2c 100644
--- a/server/drivers/mistral.test.ts
+++ b/server/drivers/mistral.test.ts
@@ -1,3 +1,6 @@
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { ASK_USER_TOOL_DEFINITION } from "../../shared/ask-question.ts";
import { recordEvents } from "../testing/events.ts";
@@ -11,6 +14,14 @@ const create = (config = {}) => MistralDriver.create({
});
describe("Mistral provider", () => {
+ it.each([undefined, true, false])("advertises computer capabilities according to tools=%s", async (tools) => {
+ vi.stubGlobal("fetch", vi.fn(async () => Response.json({ data: [] })));
+ const instance = await create({ tools });
+ try {
+ expect(instance.adapter.capabilities).toMatchObject({ computerMcp: tools !== false, localComputerMcp: tools !== false });
+ } finally { await instance.dispose(); }
+ });
+
it("rejects invalid tools flags and non-TLS remote endpoints", () => {
expect(() => MistralDriver.decodeConfig({ tools: "false" })).toThrow();
expect(() => MistralDriver.decodeConfig({ url: "http://example.com/v1" })).toThrow("HTTPS");
@@ -60,6 +71,57 @@ describe("Mistral provider", () => {
await instance.dispose();
});
+ it("sends images on loopback only for catalog vision models and aliases, retaining flags after failed refreshes", async () => {
+ const directory = mkdtempSync(join(tmpdir(), "mistral-images-"));
+ const path = join(directory, "input.png");
+ const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jBv0AAAAASUVORK5CYII=";
+ writeFileSync(path, Buffer.from(png, "base64"));
+ const catalog = { data: [
+ { id: "vision", aliases: ["vision-latest"], capabilities: { completion_chat: true, vision: true } },
+ { id: "text", capabilities: { vision: false } }, { id: "unspecified" },
+ ] };
+ let data: unknown = catalog;
+ let request: RequestInit | undefined;
+ vi.stubGlobal("fetch", vi.fn(async (url, init) => {
+ if (String(url).endsWith("/models")) return Response.json(data);
+ request = init;
+ return new Response('data: {"choices":[{"delta":{"content":"Seen"},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n',
+ { headers: { "content-type": "text/event-stream" } });
+ }));
+ const instance = await create({ url: "http://127.0.0.1:1234/v1", model: "custom" });
+ try {
+ const cases = [
+ { data: catalog, model: "vision", sees: true },
+ { data: catalog, model: "vision-latest", sees: true },
+ { data: catalog, model: "text", sees: false },
+ { data: catalog, model: "unspecified", sees: false },
+ { data: catalog, model: "custom", sees: false },
+ { data: { error: "offline" }, model: "vision-latest", sees: true },
+ { data: { data: [] }, model: "vision", sees: true },
+ { data: { data: [{ id: "vision", aliases: ["vision-latest"], capabilities: { vision: false } }] }, model: "vision-latest", sees: false },
+ ];
+ for (const [index, entry] of cases.entries()) {
+ data = entry.data;
+ await instance.refreshModels?.();
+ const recorder = recordEvents(instance.adapter);
+ try {
+ await instance.adapter.sendTurn({ threadId: `image-${index}`, text: "Describe", model: entry.model,
+ images: [{ path, mime: "image/png", bytes: Buffer.from(png, "base64").length }] });
+ expect(await recorder.until(event => event.type === "turn.completed")).toMatchObject({ ok: true });
+ const body = JSON.parse(String(request?.body));
+ expect(body.model).toBe(entry.model);
+ expect(body.messages[0].content).toEqual(entry.sees ? [
+ { type: "text", text: "Describe" }, { type: "image_url", image_url: { url: `data:image/png;base64,${png}` } },
+ ] : expect.stringContaining("Describe\n\n"));
+ if (entry.sees) expect(request?.redirect).toBe("error");
+ } finally { recorder.stop(); }
+ }
+ } finally {
+ await instance.dispose();
+ rmSync(directory, { recursive: true, force: true });
+ }
+ });
+
it.each([undefined, true, false])("streams text and usage with only supported request fields (tools=%s)", async (tools) => {
let request: RequestInit | undefined;
vi.stubGlobal("fetch", vi.fn(async (url, init) => {
diff --git a/server/drivers/mistral.ts b/server/drivers/mistral.ts
index 1c04738402..e1fc4db0a1 100644
--- a/server/drivers/mistral.ts
+++ b/server/drivers/mistral.ts
@@ -33,7 +33,7 @@ const modelCard = z.object({
name: z.string().nullable().optional(),
aliases: z.array(z.string()).optional(),
archived: z.boolean().optional(),
- capabilities: z.object({ completion_chat: z.boolean().optional() }).optional(),
+ capabilities: z.object({ completion_chat: z.boolean().optional(), vision: z.boolean().optional() }).optional(),
max_context_length: z.number().int().positive().optional(),
});
@@ -66,6 +66,7 @@ export const MistralDriver: ProviderDriver<MistralConfig> = {
return { default: options.some((option) => option.id === preferred) ? preferred : options[0].id, options };
};
let catalog = withConfiguredModel(DEFAULT_MODELS.options);
+ let imageModels = new Set<string>();
const refreshModels = async () => {
if (!apiKey) return;
try {
@@ -79,6 +80,7 @@ export const MistralDriver: ProviderDriver<MistralConfig> = {
const rows = Array.isArray(json) ? json : z.object({ data: z.array(z.unknown()) }).parse(json).data;
const options: ModelCatalog["options"] = [];
const seen = new Set<string>();
+ const images = new Set<string>();
for (const row of rows) {
const parsed = modelCard.safeParse(row);
if (!parsed.success || parsed.data.archived || parsed.data.capabilities?.completion_chat === false) continue;
@@ -86,17 +88,22 @@ export const MistralDriver: ProviderDriver<MistralConfig> = {
for (const id of [card.id, ...(card.aliases ?? [])]) {
if (!id.trim() || seen.has(id)) continue;
seen.add(id);
+ if (card.capabilities?.vision) images.add(id);
options.push({ id, label: id === card.id ? card.name?.trim() || id : id,
...(card.max_context_length ? { contextWindow: card.max_context_length } : {}) });
}
}
- if (options.length) catalog = withConfiguredModel(options);
+ if (options.length) {
+ catalog = withConfiguredModel(options);
+ imageModels = images;
+ }
} catch {}
};
if (apiKey) void refreshModels();
return createOpenAIChatRuntime({
input, driverKind: "mistral", apiKey, apiUrl: config.url,
tools: config.tools, models: () => catalog, refreshModels, contentText,
+ computerUse: true, imageInput: (model) => imageModels.has(model),
requestBody: (model, messages, stream) => ({ model, messages, stream }),
httpErrorLabel: "Mistral",
missingKeyError: "Save a Mistral API key in Settings → API keys, or set MISTRAL_API_KEY.",