feat: add image attachments for vision models

A chat line can carry up to four images, each capped at 5 MB and
sniffed from its bytes. Uploads go to a room route first, the message
references the ids, and the bytes are served back to members; an
upload no message claims within an hour is swept on the next send.

Images only make sense when the model can look at them, and the
catalog's answer is tri-state, so the gate is a stored fact, not a
guess: the provider listing remembers supports_vision per kind and
model, the upload and send routes refuse when the room's effective
model is not a known vision model, and the composer hides the attach
button on the same fact, which rides the room payload. At turn time
the fact goes to the host agent, which sends the blocks on a vision
model and leaves a note where each image was on the rest.
This commit is contained in:
vhaudiquet committed 2026-12-01 21:59:53 +01:00
1 parent acc545ede7
commit 2e17e7463c
18 files changed
+624 -46

No files matched your search

+63
View File
@@ -1213,6 +1213,27 @@ button {
margin-bottom: 0;
}
.msg-attachments {
display: flex;
flex-wrap: wrap;
gap: var(--sp-2);
margin-top: var(--sp-1);
}
.msg-attachment {
display: block;
border: 1px solid var(--border);
border-radius: var(--r-card);
overflow: hidden;
line-height: 0;
}
.msg-attachment img {
display: block;
max-width: 240px;
max-height: 180px;
}
.msg-text p,
.msg-text ul,
.msg-text ol,
@@ -1577,6 +1598,48 @@ button {
padding-left: var(--sp-2);
}
.composer-attachments {
display: flex;
flex-wrap: wrap;
gap: var(--sp-2);
padding: var(--sp-1) var(--sp-2) var(--sp-2);
}
.composer-attachment {
position: relative;
}
.composer-attachment-thumb {
display: block;
width: 56px;
height: 56px;
object-fit: cover;
border: 1px solid var(--border);
border-radius: var(--r-control);
}
.composer-attachment-remove {
position: absolute;
top: -6px;
right: -6px;
display: grid;
place-items: center;
width: 18px;
height: 18px;
padding: 0;
border: 1px solid var(--border-strong);
border-radius: var(--r-avatar);
background: var(--surface);
color: var(--muted);
cursor: pointer;
line-height: 0;
}
.composer-attachment-remove:hover {
color: var(--danger);
border-color: var(--danger);
}
.composer-buttons {
display: flex;
align-items: center;
+124 -7
View File
@@ -1,16 +1,28 @@
<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import { AtSign, Send } from 'lucide-vue-next'
import type { Message, SendMessageResponse } from '~/types'
import { AtSign, ImagePlus, Send, X } from 'lucide-vue-next'
import type {
Message,
SendMessageResponse,
UploadedAttachment,
UploadAttachmentResponse,
} from '~/types'
import { apiErrorMessage } from '~/utils/errors'
const props = defineProps<{ roomId: string }>()
const props = defineProps<{ roomId: string; canAttach: boolean }>()
const emit = defineEmits<{ sent: [message: Message]; typing: [active: boolean]; resized: [] }>()
const MAX_ATTACHMENTS = 4
const MAX_ATTACHMENT_BYTES = 5 * 1024 * 1024
const ATTACHMENT_TYPES = ['image/png', 'image/jpeg', 'image/gif', 'image/webp']
const api = useApi()
const text = ref('')
const sending = ref(false)
const uploading = ref(false)
const error = ref('')
const attachments = ref<UploadedAttachment[]>([])
const fileInput = ref<HTMLInputElement | null>(null)
const input = ref<HTMLTextAreaElement | null>(null)
const sizer = ref<HTMLTextAreaElement | null>(null)
let typingTimer: number | null = null
@@ -79,14 +91,83 @@ function onKeydown(event: KeyboardEvent) {
void send()
}
function clearAttachments() {
for (const att of attachments.value) URL.revokeObjectURL(att.previewUrl)
attachments.value = []
}
function rejectFile(file: File): string | null {
if (!ATTACHMENT_TYPES.includes(file.type)) return `${file.name} is not a PNG, JPEG, GIF, or WebP image`
if (file.size > MAX_ATTACHMENT_BYTES) return `${file.name} is over the 5 MB cap`
return null
}
async function addFiles(files: Iterable<File>) {
const picked = [...files]
if (picked.length === 0) return
const reason = picked.map(rejectFile).find((value) => value !== null)
if (reason) {
error.value = reason
return
}
const capacity = Math.max(0, MAX_ATTACHMENTS - attachments.value.length)
if (picked.length > capacity) {
error.value = `A message carries at most ${MAX_ATTACHMENTS} images.`
return
}
uploading.value = true
error.value = ''
try {
for (const file of picked) {
const response = await api.upload<UploadAttachmentResponse>(
`/rooms/${props.roomId}/attachments`,
file,
)
attachments.value.push({ ...response.attachment, previewUrl: URL.createObjectURL(file) })
}
} catch (err) {
error.value = apiErrorMessage(err)
} finally {
uploading.value = false
}
}
function pickFiles() {
fileInput.value?.click()
}
function onFilesChosen(event: Event) {
const element = event.target as HTMLInputElement
void addFiles(element.files ?? [])
element.value = ''
}
function onPaste(event: ClipboardEvent) {
const files = [...(event.clipboardData?.files ?? [])].filter((file) => file.type.startsWith('image/'))
if (files.length === 0) return
event.preventDefault()
void addFiles(files)
}
function removeAttachment(at: number) {
const att = attachments.value[at]
if (!att) return
URL.revokeObjectURL(att.previewUrl)
attachments.value.splice(at, 1)
}
async function send() {
const value = text.value.trim()
if (!value || sending.value) return
if ((!value && attachments.value.length === 0) || sending.value || uploading.value) return
sending.value = true
error.value = ''
try {
const response = await api.post<SendMessageResponse>(`/rooms/${props.roomId}/messages`, { text: value })
const response = await api.post<SendMessageResponse>(`/rooms/${props.roomId}/messages`, {
text: value,
attachments: attachments.value.map((att) => att.id),
})
text.value = ''
clearAttachments()
if (typingTimer !== null) {
window.clearTimeout(typingTimer)
typingTimer = null
@@ -120,6 +201,7 @@ onBeforeUnmount(() => {
if (typingTimer !== null) window.clearTimeout(typingTimer)
widthObserver?.disconnect()
widthObserver = null
clearAttachments()
})
</script>
@@ -136,7 +218,22 @@ onBeforeUnmount(() => {
placeholder="Message the room. Use @rhema to ask the agent."
@input="onInput"
@keydown="onKeydown"
@paste="onPaste"
/>
<div v-if="attachments.length > 0" class="composer-attachments">
<div v-for="(att, index) in attachments" :key="att.id" class="composer-attachment">
<img :src="att.previewUrl" alt="" class="composer-attachment-thumb">
<button
type="button"
class="composer-attachment-remove"
:aria-label="`Remove image ${index + 1}`"
title="Remove"
@click="removeAttachment(index)"
>
<X :size="12" aria-hidden="true" />
</button>
</div>
</div>
<textarea
ref="sizer"
class="composer-sizer"
@@ -150,6 +247,26 @@ onBeforeUnmount(() => {
<div class="composer-actions">
<span class="composer-hint">Enter sends, Shift+Enter adds a line.</span>
<div class="composer-buttons">
<input
ref="fileInput"
type="file"
class="sr-only"
accept="image/png,image/jpeg,image/gif,image/webp"
multiple
tabindex="-1"
aria-hidden="true"
@change="onFilesChosen"
>
<button
v-if="canAttach"
type="button"
class="icon-btn"
aria-label="Attach images"
title="Attach images, or paste one into the message"
@click="pickFiles"
>
<ImagePlus :size="16" aria-hidden="true" />
</button>
<button
type="button"
class="icon-btn"
@@ -162,13 +279,13 @@ onBeforeUnmount(() => {
<button
type="button"
class="btn btn-primary btn-sm"
:disabled="!text.trim() || sending"
:disabled="(!text.trim() && attachments.length === 0) || sending || uploading"
aria-label="Send message"
title="Send"
@click="send"
>
<Send :size="14" aria-hidden="true" />
{{ sending ? 'Sending' : 'Send' }}
{{ sending ? 'Sending' : uploading ? 'Uploading' : 'Send' }}
</button>
</div>
</div>
+19 -1
View File
@@ -5,7 +5,12 @@ import { fullTime, relativeTime } from '~/utils/format'
import { memberColorVar } from '~/utils/member'
import { renderMarkdown } from '~/utils/markdown'
const props = defineProps<{ message: Message; color?: string | null; avatar?: string | null }>()
const props = defineProps<{
message: Message
roomId: string
color?: string | null
avatar?: string | null
}>()
const html = computed(() => renderMarkdown(props.message.text))
const style = computed(() => ({ '--member': memberColorVar(props.color) }))
@@ -32,6 +37,19 @@ const style = computed(() => ({ '--member': memberColorVar(props.color) }))
<span class="msg-time" :title="fullTime(message.created_at)">{{ relativeTime(message.created_at) }}</span>
</div>
<div class="msg-text" v-html="html"></div>
<div v-if="message.attachments?.length" class="msg-attachments">
<a
v-for="att in message.attachments"
:key="att.id"
class="msg-attachment"
:href="`/api/rooms/${roomId}/attachments/${att.id}`"
target="_blank"
rel="noopener"
:aria-label="`Open attached image, ${att.mime.replace('image/', '')}, ${Math.ceil(att.size / 1024)} KB`"
>
<img :src="`/api/rooms/${roomId}/attachments/${att.id}`" alt="" loading="lazy">
</a>
</div>
</div>
</div>
</template>
+2
View File
@@ -837,6 +837,7 @@ onBeforeUnmount(() => {
<ChatMessage
v-if="entry.kind === 'message'"
:message="entry.message"
:room-id="roomId"
:color="colorFor(entry.message)"
:avatar="avatarFor(entry.message)"
/>
@@ -873,6 +874,7 @@ onBeforeUnmount(() => {
<ChatComposer
:room-id="roomId"
:can-attach="detail?.room.model_vision === true"
@sent="onSent"
@typing="onTyping"
@resized="onComposerResized"
+16
View File
@@ -14,6 +14,12 @@ export interface BootstrapResponse {
preview_origin: string
}
export interface MessageAttachment {
id: string
mime: string
size: number
}
export interface Message {
seq: number
id: string
@@ -23,6 +29,15 @@ export interface Message {
text: string
mentions: boolean
created_at: number
attachments?: MessageAttachment[]
}
export interface UploadedAttachment extends MessageAttachment {
previewUrl: string
}
export interface UploadAttachmentResponse {
attachment: MessageAttachment
}
export interface MessagePage {
@@ -56,6 +71,7 @@ export interface Room {
workspace: string
repo_url: string | null
model: string | null
model_vision: boolean | null
max_turn_steps: number
created_at: number
archived_at: number | null