forked from vhaudiquet/rhema-together
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:
1 parent
acc545ede7
commit
2e17e7463c
18 files changed
+624
-46
No files matched your search
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
Reference in new issue
Block a user