summaryrefslogtreecommitdiffhomepage
path: root/packages/core/tests/models/catalog.test.ts
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-02 22:50:11 +0900
committerAdam Malczewski <[email protected]>2026-06-02 22:50:11 +0900
commit66e5d3b105bfd2b34c6f35876bf33dbb3cb9dcae (patch)
treec3e039e09c89231f84dfd16f7bbbf8aedcc2dc7d /packages/core/tests/models/catalog.test.ts
parent4b45d33c256cf580a53054078be6fd7148fa6302 (diff)
downloaddispatch-66e5d3b105bfd2b34c6f35876bf33dbb3cb9dcae.tar.gz
dispatch-66e5d3b105bfd2b34c6f35876bf33dbb3cb9dcae.zip
feat(chat): paste-to-attach images/PDFs with model capability check
Add multimodal image/PDF input to the chat box via clipboard paste, gated by a graceful per-model capability check. UX: a pasted image/PDF inserts an inline token (【image:…】 / 【pdf:…】) into the draft, so attachments have ORDER relative to typed text and can be referenced positionally. The token is the only handle — deleting it (atomic Backspace/ Delete, or selection overlap) detaches the file; an input-reconciliation safety net detaches any attachment whose token is no longer intact. No preview strip. Capability check: resolveModelCapabilities reads models.dev modalities.input (new GET /models/capabilities, mirrors /context-limit). The input blocks Send (no tokens spent) only on a definitive 'no'; unknown capability (catalog offline / unmapped provider) stays permissive. Attachments require a fresh turn — Send is blocked while generating and /chat rejects content mid-turn (409). Attachments are EPHEMERAL: forwarded to the model for the turn via ordered AI SDK ImagePart/FilePart content, but never persisted (history keeps the text with [image]/[pdf] markers). Text-only turns serialize byte-identically to before. Limits (Anthropic-aligned, enforced at paste + re-validated server-side): PNG/JPEG/WebP/GIF/PDF; image ≤5MB, PDF ≤32MB, ≤20 attachments, ≤32MB total. core: UserContentPart types, models/attachments validator, capability resolver, agent.run+toModelMessages thread ordered content. api: /chat content validation + passthrough. frontend: attachment-tokens helper, ChatInput paste/token/gating, per-tab staged attachments, App.svelte capability fetch. +44 tests.
Diffstat (limited to 'packages/core/tests/models/catalog.test.ts')
-rw-r--r--packages/core/tests/models/catalog.test.ts75
1 files changed, 72 insertions, 3 deletions
diff --git a/packages/core/tests/models/catalog.test.ts b/packages/core/tests/models/catalog.test.ts
index 51043e6..f4bddc2 100644
--- a/packages/core/tests/models/catalog.test.ts
+++ b/packages/core/tests/models/catalog.test.ts
@@ -4,6 +4,7 @@ import {
__resetCatalogCacheForTests,
getModelsCatalog,
resolveContextLimit,
+ resolveModelCapabilities,
} from "../../src/models/catalog.js";
const CACHE_PATH = "/tmp/dispatch/models-dev.json";
@@ -13,14 +14,30 @@ const CATALOG = {
anthropic: {
id: "anthropic",
models: {
- "claude-sonnet-4-5": { limit: { context: 200000, output: 64000 } },
- "claude-sonnet-4-6": { limit: { context: 1000000, output: 64000 } },
+ "claude-sonnet-4-5": {
+ limit: { context: 200000, output: 64000 },
+ modalities: { input: ["text", "image", "pdf"], output: ["text"] },
+ },
+ "claude-sonnet-4-6": {
+ limit: { context: 1000000, output: 64000 },
+ modalities: { input: ["text", "image", "pdf"], output: ["text"] },
+ },
+ // A text-only model: definitively no image/pdf input.
+ "text-only-model": {
+ limit: { context: 100000, output: 8192 },
+ modalities: { input: ["text"], output: ["text"] },
+ },
+ // An entry predating the modalities field → capability unknown.
+ "legacy-model": { limit: { context: 100000, output: 8192 } },
},
},
opencode: {
id: "opencode",
models: {
- "glm-4-6": { limit: { context: 131072, output: 8192 } },
+ "glm-4-6": {
+ limit: { context: 131072, output: 8192 },
+ modalities: { input: ["text", "image"], output: ["text"] },
+ },
},
},
};
@@ -156,3 +173,55 @@ describe("getModelsCatalog caching", () => {
warn.mockRestore();
});
});
+
+describe("resolveModelCapabilities", () => {
+ it("reports image + pdf for a vision model", async () => {
+ mockFetchOnce(CATALOG);
+ expect(await resolveModelCapabilities("anthropic", "claude-sonnet-4-5")).toEqual({
+ image: true,
+ pdf: true,
+ });
+ });
+
+ it("reports image-only for a model whose modalities omit pdf", async () => {
+ mockFetchOnce(CATALOG);
+ // glm-4-6 lists image but not pdf (resolved via the opencode fallback).
+ expect(await resolveModelCapabilities("opencode-anthropic", "glm-4-6")).toEqual({
+ image: true,
+ pdf: false,
+ });
+ });
+
+ it("reports a definitive no for a text-only model", async () => {
+ mockFetchOnce(CATALOG);
+ expect(await resolveModelCapabilities("anthropic", "text-only-model")).toEqual({
+ image: false,
+ pdf: false,
+ });
+ });
+
+ it("returns null (unknown) for an entry without modalities", async () => {
+ mockFetchOnce(CATALOG);
+ expect(await resolveModelCapabilities("anthropic", "legacy-model")).toBeNull();
+ });
+
+ it("returns null (unknown) for an unknown model id", async () => {
+ mockFetchOnce(CATALOG);
+ expect(await resolveModelCapabilities("anthropic", "no-such-model")).toBeNull();
+ });
+
+ it("returns null for an unsupported provider without hitting the network", async () => {
+ const fetchFn = mockFetchOnce(CATALOG);
+ expect(await resolveModelCapabilities("google", "gemini-2.5-pro")).toBeNull();
+ expect(await resolveModelCapabilities("anthropic", "")).toBeNull();
+ expect(fetchFn).not.toHaveBeenCalled();
+ });
+
+ it("returns null (unknown) when the catalog is offline with no cache", async () => {
+ const fetchFn = vi.fn(() => Promise.reject(new Error("offline")));
+ vi.stubGlobal("fetch", fetchFn);
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ expect(await resolveModelCapabilities("anthropic", "claude-sonnet-4-5")).toBeNull();
+ warn.mockRestore();
+ });
+});