project-nomad/admin/app/utils/model_capabilities.ts
Gabe Graves edbfe1ad78 feat(ai): support local vision attachments
Closes #1176. Salvages @gabegraves's work from #1178, rebased onto the
RagPipelineService refactor that landed after it was written.

Adds JPEG/PNG/WebP attachments to chat over multipart, leaving text-only JSON
requests on their existing path. Images are normalized server-side to bounded
JPEG with metadata stripped, capped on count/size/pixels/output, and the
temporary uploads are cleaned up on every validation path. They are attached
only to the latest user message, are excluded from RAG, titles, logs and
persisted history, and are never written to storage.

Vision support is resolved per model from what the backend actually reports --
OpenAI-compatible /v1/models metadata, then Ollama /api/show, then llama.cpp
/props -- never inferred from a model name. A model that reports no vision
support has attachments disabled with a visible explanation; a backend that
reports nothing is allowed with a warning and its rejection is translated into
plain compatibility guidance rather than a raw error.

Changes from the original:

- Prompt assembly moved into RagPipelineService in the meantime, so the
  controller half is rebuilt against the trace and the multipart-aware
  collection filter is now the only one (dev's request.input() version would
  have silently dropped the collection on every image request).
- Added a native-path converter. dev split chat into native and OpenAI-compat
  paths after this was written; Ollama's native /api/chat does not take OpenAI
  content parts, it takes a sibling `images` array of bare base64. Without this
  every image request on a native Ollama backend would fail.
- Dropped the model-recommendation change (selectRecommendedModels and the
  Qwen3-VL fallback entry). Displacing a recommended model at first-time setup
  is a product decision and is being taken separately. The "Supports images"
  label in Easy Setup is kept: it describes what is already on offer rather
  than changing it.
- Dropped the DEFAULT_QUERY_REWRITE_MODEL download prompt his branch carried,
  which dev deleted in d022c2d.
- Removed a duplicate checkModelHasThinking; kept dev's memoized getModelInfo
  path, since thinking detection is not part of this feature.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F8Mgg1vd2eKSs8StZiuyMV
2026-09-08 04:50:04 -07:00

87 lines
2.7 KiB
TypeScript

import type { ModelVisionCapability } from '../../types/ollama.js'
export type ModelCapabilities = {
thinking: boolean
vision: ModelVisionCapability
}
export type ModelCapabilityProbes = {
ollamaShow: () => Promise<unknown>
llamaProps: () => Promise<unknown>
}
export type InstalledModelMetadata = {
name: string
size: number
capabilities?: string[]
}
export function capabilitiesFromOllamaShow(value: unknown): ModelCapabilities | null {
if (!value || typeof value !== 'object') return null
const capabilities = (value as { capabilities?: unknown }).capabilities
if (!Array.isArray(capabilities) || !capabilities.every((item) => typeof item === 'string')) {
return null
}
return {
thinking: capabilities.includes('thinking'),
vision: capabilities.includes('vision') ? 'supported' : 'unsupported',
}
}
export function visionCapabilityFromLlamaProps(value: unknown): ModelVisionCapability | null {
if (!value || typeof value !== 'object') return null
const modalities = (value as { modalities?: unknown }).modalities
if (modalities && typeof modalities === 'object' && !Array.isArray(modalities)) {
const vision = (modalities as { vision?: unknown }).vision
return typeof vision === 'boolean' ? (vision ? 'supported' : 'unsupported') : null
}
if (Array.isArray(modalities) && modalities.every((item) => typeof item === 'string')) {
return modalities.includes('image') || modalities.includes('vision')
? 'supported'
: 'unsupported'
}
return null
}
export async function resolveModelCapabilities(
advertisedMetadata: unknown,
probes: ModelCapabilityProbes
): Promise<ModelCapabilities | null> {
const advertised = capabilitiesFromOllamaShow(advertisedMetadata)
if (advertised) return advertised
try {
const ollama = capabilitiesFromOllamaShow(await probes.ollamaShow())
if (ollama) return ollama
} catch {}
try {
const vision = visionCapabilityFromLlamaProps(await probes.llamaProps())
if (vision) return { thinking: false, vision }
} catch {}
return null
}
export function installedModelsFromOpenAIResponse(value: unknown): InstalledModelMetadata[] {
if (!value || typeof value !== 'object') return []
const data = (value as { data?: unknown }).data
if (!Array.isArray(data)) return []
return data.flatMap((entry) => {
if (!entry || typeof entry !== 'object') return []
const { id, capabilities } = entry as { id?: unknown; capabilities?: unknown }
if (typeof id !== 'string') return []
const model: InstalledModelMetadata = { name: id, size: 0 }
if (
Array.isArray(capabilities) &&
capabilities.every((capability) => typeof capability === 'string')
) {
model.capabilities = capabilities
}
return [model]
})
}