Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.
feat(vision): a declared capability beats the name-based guess (#232)
tournierjc2a098d372860
diff --git a/src/core/agent.ts b/src/core/agent.tsindex 05ce1159..44216440 100644--- a/src/core/agent.ts+++ b/src/core/agent.ts@@ -3415,8 +3415,15 @@ export class Agent {// passed back with assistant turns (issue #24); other providers// get plain text.// Photos and PDFs from the chat go to models that can take them.- setPluginImagesAllowed(modelSeesImages(provider.name, provider.getModel()));- const providerMessages = withAttachments(deepseekProviderOptions ? withReasoningParts(messages) : messages, msg, provider.name, provider.getModel());+ const seesImages = modelSeesImages(provider.name, provider.getModel(), provider.seesImages);+ setPluginImagesAllowed(seesImages);+ const providerMessages = withAttachments(+ deepseekProviderOptions ? withReasoningParts(messages) : messages,+ msg,+ provider.name,+ provider.getModel(),+ provider.seesImages,+ );// Per-provider output ceiling: a 32K request is rejected outright by// providers with smaller limits and burned a halve-and-retry round.const providerMaxOutputTokens = Math.min(effectiveMaxOutputTokens, provider.getMaxOutputTokens() ?? Infinity);diff --git a/src/core/attachments.test.ts b/src/core/attachments.test.tsindex 5ce53a20..d8795209 100644--- a/src/core/attachments.test.ts+++ b/src/core/attachments.test.ts@@ -56,3 +56,30 @@ describe('chat attachments for the model', () => {expect(modelReadsPdf('openai', 'gpt-3.5-turbo')).toBe(false);});});++describe('a declared capability beats the name-based guess', () => {+ it('a self-hosted model the owner declared as seeing images gets the pixels', () => {+ // The name says nothing (no "vl", no vendor): only the declaration can know.+ expect(modelSeesImages('openaiCompat', 'qwen3.8-flash-next')).toBe(false);+ expect(modelSeesImages('openaiCompat', 'qwen3.8-flash-next', true)).toBe(true);++ const out = withAttachments(history, msg([{ kind: 'image', path: img }]), 'openaiCompat', 'qwen3.8-flash-next', true);+ const parts = out[2].content as unknown as Array<{ type: string }>;+ expect(parts.some(p => p.type === 'image')).toBe(true);+ });++ it('a declaration of false is honoured even for a model whose name matches', () => {+ expect(modelSeesImages('openai', 'gpt-4.1-mini')).toBe(true);+ expect(modelSeesImages('openai', 'gpt-4.1-mini', false)).toBe(false);++ const out = withAttachments(history, msg([{ kind: 'image', path: img }]), 'openai', 'gpt-4.1-mini', false);+ const parts = out[2].content as unknown as Array<{ type: string; text?: string }>;+ expect(parts.some(p => p.type === 'image')).toBe(false);+ expect(JSON.stringify(parts)).toContain("can't view images");+ });++ it('no declaration keeps the old behaviour', () => {+ expect(modelSeesImages('deepseek', 'deepseek-flash')).toBe(false);+ expect(modelSeesImages('anthropic', 'claude-sonnet-4-5')).toBe(true);+ });+});diff --git a/src/core/attachments.ts b/src/core/attachments.tsindex db21d7d9..893563ab 100644--- a/src/core/attachments.ts+++ b/src/core/attachments.ts@@ -25,7 +25,16 @@ interface Attachment {const IMAGE_MIME: Record<string, string> = { '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.png': 'image/png', '.gif': 'image/gif', '.webp': 'image/webp' };/** Models that accept image input. Conservative: unknown models get the note instead. */-export function modelSeesImages(provider: string, model: string): boolean {+/**+ * Does the current model read images?+ *+ * `declared` wins when the owner configured a capability (`providers.<name>.vision`):+ * a self-hosted model's name is no evidence — a local Qwen that sees images was+ * called `qwen3.8-flash-next` and refused. Without a declaration, the name is+ * matched against the providers known to ship vision.+ */+export function modelSeesImages(provider: string, model: string, declared?: boolean): boolean {+ if (declared !== undefined) return declared;const m = model.toLowerCase();if (provider === 'deepseek') return false;if (/gpt-4o|gpt-4\.1|gpt-5|chatgpt-4o|\bo[134](-|$)|claude|gemini|pixtral|llava|qwen.*vl|vision|grok-(2-vision|4)|llama-?4|mistral-(medium|small)-3|gemma-?3/.test(m)) return true;@@ -58,7 +67,14 @@ function isPdf(a: Attachment): boolean {* this provider and model. Returns the messages unchanged when there is* nothing to attach or the model can't take it.*/-export function withAttachments<T extends { role: string; content: unknown }>(messages: T[], msg: ChannelMessage, provider: string, model: string): T[] {+export function withAttachments<T extends { role: string; content: unknown }>(+ messages: T[],+ msg: ChannelMessage,+ provider: string,+ model: string,+ /** The owner's declaration, when there is one (see `modelSeesImages`). */+ declaredVision?: boolean,+): T[] {const files = attachmentsOf(msg);if (!files.length) return messages;const parts: Array<Record<string, unknown>> = [];@@ -68,7 +84,7 @@ export function withAttachments<T extends { role: string; content: unknown }>(meconst size = statSync(a.path).size;if (a.kind === 'image') {const mediaType = a.mime?.startsWith('image/') ? a.mime : IMAGE_MIME[extname(a.path).toLowerCase()] ?? 'image/jpeg';- if (!modelSeesImages(provider, model)) notes.push(`The current model (${model}) can't view images; say so, and offer to describe it with a vision model if one is configured.`);+ if (!modelSeesImages(provider, model, declaredVision)) notes.push(`The current model (${model}) can't view images; say so, and offer to describe it with a vision model if one is configured.`);else if (size > MAX_IMAGE_BYTES) notes.push(`An image is too large to view (${Math.round(size / 1048576)} MB).`);else parts.push({ type: 'image', image: readFileSync(a.path), mediaType });} else if (isPdf(a)) {diff --git a/src/providers/base.ts b/src/providers/base.tsindex 6ad188a1..440f2fa4 100644--- a/src/providers/base.ts+++ b/src/providers/base.ts@@ -32,6 +32,14 @@ export abstract class BaseProvider {return this.config.model;}+ /**+ * What the owner declared about this endpoint's capability, or undefined when+ * nothing was declared — in which case the name-based guess applies.+ */+ get seesImages(): boolean | undefined {+ return this.config.vision;+ }+/*** Largest `maxOutputTokens` this provider/model accepts, or undefined for* "no known ceiling". The agent loop clamps its request to this so adiff --git a/src/utils/config-vision.test.ts b/src/utils/config-vision.test.tsnew file mode 100644index 00000000..52928b65--- /dev/null+++ b/src/utils/config-vision.test.ts@@ -0,0 +1,77 @@+import { afterEach, describe, expect, it } from 'vitest';+import { getDefaultConfig } from './config.js';+import { modelSeesImages } from '../core/attachments.js';++/**+ * « Not declared » is a third state, not `false`.+ *+ * The three self-hosted endpoints (`openaiCompat`, `lmStudio`, `litellm`) used to default their+ * capability flag to `false` through `getEnvBool(…, false)`. A `false` is a declaration: it turned+ * images off for every model whose name the guesser would have let through (`gpt-4o` behind a+ * gateway), which is the opposite of what a missing variable should mean. Undefined keeps the guess.+ */+const KEYS = ['OPENAI_COMPAT_VISION', 'LM_STUDIO_VISION', 'LITELLM_VISION'] as const;+const saved = new Map<string, string | undefined>();++function unsetAll(): void {+ for (const key of KEYS) {+ saved.set(key, process.env[key]);+ delete process.env[key];+ }+}++afterEach(() => {+ for (const [key, value] of saved) {+ if (value === undefined) delete process.env[key];+ else process.env[key] = value;+ }+ saved.clear();+});++describe('a capability flag nobody set', () => {+ it('stays undefined, so the name guess still runs for openaiCompat', () => {+ unsetAll();+ const config = getDefaultConfig();+ expect(config.providers.openaiCompat.vision).toBeUndefined();+ // Today's behaviour, pinned: a gateway serving gpt-4o still gets the photos.+ expect(modelSeesImages('openaiCompat', 'gpt-4o', config.providers.openaiCompat.vision)).toBe(true);+ });++ it('stays undefined for lmStudio and litellm too', () => {+ unsetAll();+ const config = getDefaultConfig();+ expect(config.providers.lmStudio.vision).toBeUndefined();+ expect(config.providers.litellm.vision).toBeUndefined();+ });++ it('a model the guesser does not recognise still gets the note, not the image', () => {+ unsetAll();+ const config = getDefaultConfig();+ expect(modelSeesImages('openaiCompat', 'qwen3.8-flash-next', config.providers.openaiCompat.vision)).toBe(false);+ });+});++describe('a capability flag the owner set', () => {+ it('false is a declaration and wins over the name guess', () => {+ unsetAll();+ process.env.OPENAI_COMPAT_VISION = 'false';+ const config = getDefaultConfig();+ expect(config.providers.openaiCompat.vision).toBe(false);+ expect(modelSeesImages('openaiCompat', 'gpt-4o', config.providers.openaiCompat.vision)).toBe(false);+ });++ it('true is a declaration and wins over an unrecognised name', () => {+ unsetAll();+ process.env.OPENAI_COMPAT_VISION = 'true';+ const config = getDefaultConfig();+ expect(config.providers.openaiCompat.vision).toBe(true);+ expect(modelSeesImages('openaiCompat', 'qwen3.8-flash-next', config.providers.openaiCompat.vision)).toBe(true);+ });++ it('an unreadable value is the same as not declared', () => {+ unsetAll();+ process.env.LITELLM_VISION = 'maybe';+ const config = getDefaultConfig();+ expect(config.providers.litellm.vision).toBeUndefined();+ });+});diff --git a/src/utils/config.ts b/src/utils/config.tsindex 62d3b6ff..52be5691 100644--- a/src/utils/config.ts+++ b/src/utils/config.ts@@ -37,6 +37,12 @@ export interface ProviderConfig {enabled: boolean;/** Optional per-provider cap on output tokens per request. */maxOutputTokens?: number;+ /**+ * The owner says this endpoint's model reads images. `modelSeesImages` otherwise+ * guesses from the model name, which cannot know about a self-hosted model — a+ * local Qwen-VL called `qwen3.8-flash-next` was refused for that reason.+ */+ vision?: boolean;}export interface TelegramAccessUser {@@ -301,6 +307,21 @@ function getEnvBool(key: string, fallback: boolean): boolean {return fallback;}+/**+ * A capability the owner declares, where "not set" is a third state.+ *+ * `getEnvBool(key, false)` would answer `false` for a variable nobody set, and a `false` is a+ * declaration: it switches images off for a model whose name the guesser would have accepted+ * (`gpt-4o` behind a gateway). Undefined means "nothing declared", so the name-based guess in+ * `modelSeesImages` still applies.+ */+function getEnvOptionalBool(key: string): boolean | undefined {+ const val = process.env[key]?.toLowerCase();+ if (val === 'true') return true;+ if (val === 'false') return false;+ return undefined;+}+export function getDefaultConfig(): MercuryConfig {const home = getMercuryHome();const cloudApiUrl = getEnv('MERCURY_CLOUD_API_URL', MERCURY_CLOUD_API_URL);@@ -402,6 +423,7 @@ export function getDefaultConfig(): MercuryConfig {baseUrl: getEnv('OPENAI_COMPAT_BASE_URL', ''),model: getEnv('OPENAI_COMPAT_MODEL', ''),enabled: getEnvBool('OPENAI_COMPAT_ENABLED', false),+ vision: getEnvOptionalBool('OPENAI_COMPAT_VISION'),},mimo: {name: 'mimo',@@ -420,6 +442,7 @@ export function getDefaultConfig(): MercuryConfig {lmStudio: {name: 'lmStudio',apiKey: getEnv('LM_STUDIO_API_KEY', ''),+ vision: getEnvOptionalBool('LM_STUDIO_VISION'),baseUrl: getEnv('LM_STUDIO_BASE_URL', 'http://127.0.0.1:1234/v1'),model: getEnv('LM_STUDIO_MODEL', ''),enabled: getEnvBool('LM_STUDIO_ENABLED', false),@@ -444,6 +467,7 @@ export function getDefaultConfig(): MercuryConfig {baseUrl: getEnv('LITELLM_BASE_URL', 'http://localhost:4000/v1'),model: getEnv('LITELLM_MODEL', 'gpt-4o-mini'),enabled: getEnvBool('LITELLM_ENABLED', false),+ vision: getEnvOptionalBool('LITELLM_VISION'),},},channels: {