feat(llm-parse): read a photo, as a booking or as a receipt in Costs

Whether the AI Parsing model reads images is an admin setting now, and it
lands with the two features that send a photo, so it has something to
control.

The setting is `vision: 'auto' | 'on' | 'off'` on the addon config. On
Automatic a local Ollama is asked what the model can do (`/api/show`), and a
cloud provider means no; Yes and No are applied as set. The old `multimodal`
flag is not read: the admin screens wrote it false on every save, so a
stored false meant nothing. The per-user toggle keeps its on/off meaning and
is read by the extraction now. The lookup is server-side only: capped read,
validated body, never throws, cached per server and model.
GET /api/llm/capabilities answers it for the signed-in caller, and both
photo pickers below ask it.

The booking import takes JPG, PNG and WEBP when the model reads images.
The photo goes as an image to every provider (image_url, an Anthropic image
block, Ollama's native chat with `images`), capped at 1600px, and skips KDE
Itinerary. A scanned PDF, which used to reach the model as empty text, has
its first two pages drawn by pdf-parse and sent the same way. The rest is
the import as it was: background job, review, the file attached to the
booking, the linked cost. On a real qwen3.5:4b a train ticket came back as
bare clock times until the router gave the model every type's field
meanings, and its price as an object inside a string, which read as 4900;
both are handled.

Costs gets Scan receipt: a dialog laid out like the booking import's takes
one photo, read on the import job queue, then reviewed in the existing
expense editor pre-filled with merchant, total, currency, day and lines,
the photo waiting in Receipts & Invoices to be uploaded on save. No table,
no migration, no category guess, no document-type detection.
This commit is contained in:
RemEx
2026-09-24 17:12:54 +00:00
committed by Maurice
parent 706c6c3959
commit b1c05bca83
183 changed files with 3098 additions and 218 deletions
+16 -6
View File
@@ -1,6 +1,6 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import type { BookingImportPreviewItem, BookingImportMode } from '@trek/shared'
import type { BookingImportPreviewItem, BookingImportMode, ReceiptRead } from '@trek/shared'
/**
* Tracks booking-import parses that run in the BACKGROUND (the async endpoint).
@@ -15,6 +15,8 @@ import type { BookingImportPreviewItem, BookingImportMode } from '@trek/shared'
* on mount. We deliberately persist neither the parsed `items` (re-fetched) nor the
* transient review flags (so a reload never auto-reopens the review flow).
*/
export type BackgroundTaskKind = 'transports' | 'bookings' | 'costs'
export interface BackgroundImportTask {
id: string // server job id
tripId: string
@@ -23,6 +25,8 @@ export interface BackgroundImportTask {
done: number
total: number
items?: BookingImportPreviewItem[]
/** A receipt scanned from Costs (`kind: 'costs'`): what the model read, or null when it read nothing. */
receipt?: ReceiptRead | null
warnings?: string[]
error?: string
reviewRequested?: boolean // user clicked "review" — the trip page consumes it
@@ -37,16 +41,17 @@ export interface BackgroundImportTask {
* when its type is guessed (#2076), and unlike the other transient flags it IS
* persisted: the parse deliberately outlives navigation and reload, and the
* review is triggered by the global widget on any page, so a component-local
* value is gone by the time it is needed.
* value is gone by the time it is needed. `costs` is a receipt scan, reviewed
* in the expense editor instead of the booking forms.
*/
kind?: 'transports' | 'bookings'
kind?: BackgroundTaskKind
}
interface BackgroundTasksState {
tasks: BackgroundImportTask[]
addTask: (task: { id: string; tripId: string; label: string; total: number; files?: File[]; mode?: BookingImportMode; kind?: 'transports' | 'bookings' }) => void
addTask: (task: { id: string; tripId: string; label: string; total: number; files?: File[]; mode?: BookingImportMode; kind?: BackgroundTaskKind }) => void
setProgress: (id: string, tripId: string, done: number, total: number) => void
setDone: (id: string, tripId: string, items: BookingImportPreviewItem[], warnings: string[]) => void
setDone: (id: string, tripId: string, items: BookingImportPreviewItem[], warnings: string[], receipt?: ReceiptRead | null) => void
setError: (id: string, tripId: string, error: string) => void
requestReview: (id: string) => void
markConsumed: (id: string) => void
@@ -73,7 +78,7 @@ export const useBackgroundTasksStore = create<BackgroundTasksState>()(
tasks: [],
addTask: ({ id, tripId, label, total, files, mode, kind }) => upsert(id, tripId, { label, total, status: 'running', done: 0, sourceFiles: files, mode, kind }),
setProgress: (id, tripId, done, total) => upsert(id, tripId, { done, total, status: 'running' }),
setDone: (id, tripId, items, warnings) => upsert(id, tripId, { status: 'done', items, warnings, done: items?.length ?? 0 }),
setDone: (id, tripId, items, warnings, receipt) => upsert(id, tripId, { status: 'done', items, warnings, receipt, done: items?.length ?? 0 }),
setError: (id, tripId, error) => upsert(id, tripId, { status: 'error', error }),
requestReview: (id) => set((s) => ({ tasks: s.tasks.map((t) => (t.id === id ? { ...t, reviewRequested: true } : t)) })),
markConsumed: (id) => set((s) => ({ tasks: s.tasks.map((t) => (t.id === id ? { ...t, consumed: true, reviewRequested: false } : t)) })),
@@ -96,3 +101,8 @@ export const useBackgroundTasksStore = create<BackgroundTasksState>()(
},
),
)
/** Whether a finished task has something to review: a read receipt, or at least one booking. */
export function taskFoundSomething(task: BackgroundImportTask): boolean {
return task.kind === 'costs' ? Boolean(task.receipt) : (task.items?.length ?? 0) > 0
}