forked from vhaudiquet/rhema-together
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.
296 lines
8.7 KiB
Vue
296 lines
8.7 KiB
Vue
<script setup lang="ts">
|
|
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
|
|
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; 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
|
|
let typingActive = false
|
|
let widthObserver: ResizeObserver | null = null
|
|
let inputWidth = 0
|
|
|
|
function resize() {
|
|
const element = input.value
|
|
const mirror = sizer.value
|
|
if (!element) return
|
|
const previous = element.getBoundingClientRect().height
|
|
if (mirror) {
|
|
// Measure a detached copy. Resetting the real textarea to `auto`
|
|
// collapses it to one row for a frame, which resizes the chat
|
|
// viewport and moves the reader off the bottom.
|
|
mirror.style.width = `${element.clientWidth}px`
|
|
mirror.value = text.value
|
|
mirror.style.height = '0px'
|
|
element.style.height = `${mirror.scrollHeight}px`
|
|
} else {
|
|
element.style.height = 'auto'
|
|
element.style.height = `${element.scrollHeight}px`
|
|
}
|
|
const current = element.getBoundingClientRect().height
|
|
if (Math.abs(current - previous) > 0.5) emit('resized')
|
|
}
|
|
|
|
function notifyTyping(active: boolean) {
|
|
if (typingActive === active) return
|
|
typingActive = active
|
|
emit('typing', active)
|
|
}
|
|
|
|
function onInput() {
|
|
resize()
|
|
notifyTyping(true)
|
|
if (typingTimer !== null) window.clearTimeout(typingTimer)
|
|
typingTimer = window.setTimeout(() => {
|
|
typingTimer = null
|
|
notifyTyping(false)
|
|
}, 2000)
|
|
}
|
|
|
|
function insertMention() {
|
|
const element = input.value
|
|
const current = text.value
|
|
const start = element?.selectionStart ?? current.length
|
|
const end = element?.selectionEnd ?? current.length
|
|
const before = current.slice(0, start)
|
|
const after = current.slice(end)
|
|
const spacer = before.length > 0 && !/\s$/.test(before) ? ' ' : ''
|
|
const insertion = `${spacer}@rhema `
|
|
text.value = `${before}${insertion}${after}`
|
|
const cursor = before.length + insertion.length
|
|
void nextTick(() => {
|
|
element?.focus()
|
|
element?.setSelectionRange(cursor, cursor)
|
|
resize()
|
|
})
|
|
}
|
|
|
|
function onKeydown(event: KeyboardEvent) {
|
|
if (event.key !== 'Enter' || event.shiftKey || event.isComposing) return
|
|
event.preventDefault()
|
|
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 && 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,
|
|
attachments: attachments.value.map((att) => att.id),
|
|
})
|
|
text.value = ''
|
|
clearAttachments()
|
|
if (typingTimer !== null) {
|
|
window.clearTimeout(typingTimer)
|
|
typingTimer = null
|
|
}
|
|
notifyTyping(false)
|
|
emit('sent', response.message)
|
|
await nextTick()
|
|
resize()
|
|
input.value?.focus()
|
|
} catch (err) {
|
|
error.value = apiErrorMessage(err)
|
|
} finally {
|
|
sending.value = false
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
const element = input.value
|
|
if (!element) return
|
|
inputWidth = element.clientWidth
|
|
widthObserver = new ResizeObserver(() => {
|
|
const width = input.value?.clientWidth ?? 0
|
|
if (width === inputWidth) return
|
|
inputWidth = width
|
|
resize()
|
|
})
|
|
widthObserver.observe(element)
|
|
})
|
|
|
|
onBeforeUnmount(() => {
|
|
if (typingTimer !== null) window.clearTimeout(typingTimer)
|
|
widthObserver?.disconnect()
|
|
widthObserver = null
|
|
clearAttachments()
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<div class="composer">
|
|
<div class="composer-inner">
|
|
<label class="sr-only" for="room-composer">Message the room</label>
|
|
<textarea
|
|
id="room-composer"
|
|
ref="input"
|
|
v-model="text"
|
|
class="composer-input"
|
|
rows="1"
|
|
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"
|
|
rows="1"
|
|
readonly
|
|
tabindex="-1"
|
|
aria-hidden="true"
|
|
autocomplete="off"
|
|
spellcheck="false"
|
|
></textarea>
|
|
<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"
|
|
aria-label="Insert @rhema mention"
|
|
title="Insert @rhema mention"
|
|
@click="insertMention"
|
|
>
|
|
<AtSign :size="16" aria-hidden="true" />
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="btn btn-primary btn-sm"
|
|
:disabled="(!text.trim() && attachments.length === 0) || sending || uploading"
|
|
aria-label="Send message"
|
|
title="Send"
|
|
@click="send"
|
|
>
|
|
<Send :size="14" aria-hidden="true" />
|
|
{{ sending ? 'Sending' : uploading ? 'Uploading' : 'Send' }}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<p v-if="error" class="form-error composer-error" role="alert">{{ error }}</p>
|
|
</div>
|
|
</template>
|