Skip to content

The agent that grows with you

feat: add canonical group attachment controls

Teknium4673f17c9aa5
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
new file mode 100644
index 00000000000..ab1fe0c89ce
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
@@ -0,0 +1,68 @@
+import { Button } from '@hermes/plugin-sdk'
+import { useRef, useState } from 'react'
+
+import { useCanonicalGroupLabels } from './canonical-group-labels'
+import { canonicalGroupRequest, type CanonicalGroupBinding } from './canonical-groups'
+
+interface Attachment { attachment_id?: string; event_id?: string; kind: string; name: string; mime: string; size?: number }
+interface DownloadedAttachment extends Attachment { data_base64: string }
+
+function kindFor(file: File): string {
+ if (file.type.startsWith('image/')) return 'image'
+ if (file.type === 'application/pdf') return 'pdf'
+ return 'file'
+}
+
+function extension(mime: string, name: string): string {
+ const dot = name.lastIndexOf('.')
+ if (dot > 0) return name.slice(dot)
+ return mime === 'application/pdf' ? '.pdf' : mime.split('/')[1] ? `.${mime.split('/')[1]}` : '.bin'
+}
+
+export function CanonicalGroupAttachments({ binding, attachments, onChange, disabled }: {
+ binding: CanonicalGroupBinding
+ attachments: Attachment[]
+ onChange: (attachments: Attachment[]) => void
+ disabled: boolean
+}) {
+ const labels = useCanonicalGroupLabels()
+ const input = useRef<HTMLInputElement>(null)
+ const [error, setError] = useState('')
+ const [busy, setBusy] = useState(false)
+
+ async function upload(file: File) {
+ setBusy(true); setError('')
+ try {
+ const data = await file.arrayBuffer()
+ const result = await canonicalGroupRequest<Attachment>(binding, 'bot.group.attachment.upload', {
+ room_id: binding.roomId, upload_id: crypto.randomUUID(), kind: kindFor(file), name: file.name,
+ mime: file.type || 'application/octet-stream', data_base64: btoa(String.fromCharCode(...new Uint8Array(data)))
+ })
+ onChange([...attachments, result])
+ } catch (e) { setError(e instanceof Error ? e.message : String(e)) }
+ finally { setBusy(false) }
+ }
+
+ async function download(attachment: Attachment) {
+ if (!attachment.attachment_id || !attachment.event_id || !window.hermesDesktop?.saveImageBuffer) return
+ setBusy(true); setError('')
+ try {
+ const result = await canonicalGroupRequest<DownloadedAttachment>(binding, 'bot.group.attachment.download', {
+ room_id: binding.roomId, event_id: attachment.event_id, attachment_id: attachment.attachment_id
+ })
+ const bytes = Uint8Array.from(atob(result.data_base64), char => char.charCodeAt(0))
+ await window.hermesDesktop.saveImageBuffer(bytes, extension(result.mime, result.name), result.name)
+ } catch (e) { setError(e instanceof Error ? e.message : String(e)) }
+ finally { setBusy(false) }
+ }
+
+ return <div className="flex flex-wrap items-center gap-2">
+ <input ref={input} hidden type="file" onChange={e => { const file = e.target.files?.[0]; if (file) void upload(file); e.currentTarget.value = '' }} />
+ <Button disabled={disabled || busy} onClick={() => input.current?.click()}>{labels.attachFiles}</Button>
+ {attachments.map(a => <span key={a.attachment_id ?? a.name} className="flex items-center gap-1">
+ <span>{a.name}</span><Button disabled={disabled || busy} onClick={() => void download(a)}>{labels.download}</Button>
+ <Button disabled={disabled || busy} onClick={() => onChange(attachments.filter(item => item !== a))}>{labels.removeAttachment}</Button>
+ </span>)}
+ {error && <span role="alert">{labels.uploadFailed}: {error}</span>}
+ </div>
+}
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
index 66087f74e6c..6f951e0b37e 100644
--- a/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
@@ -3,11 +3,13 @@ import { useEffect, useRef, useState } from 'react'
import { useCanonicalGroupLabels } from './canonical-group-labels'
import { prepareCanonicalGroupSend, readCanonicalGroupSend, retireCanonicalGroupSend } from './canonical-group-send'
+import { CanonicalGroupAttachments } from './canonical-group-attachments'
import type { PreparedCanonicalGroupSend } from './canonical-group-send'
import { actCanonicalGroup, canonicalGroupRequest } from './canonical-groups'
import type { CanonicalGroupBinding, CanonicalPendingAction } from './canonical-groups'
interface RoomEvent { seq: number; kind: string; payload: { text?: string; content?: string }; actor?: { member_id?: string } }
+interface Attachment { attachment_id?: string; event_id?: string; kind: string; name: string; mime: string; size?: number }
interface RoomState { room: { name: string }; driver_status?: { pending_actions?: CanonicalPendingAction[] } }
export function CanonicalGroupWorkspace({ binding, visible = true, onBack }: {
@@ -27,7 +29,7 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
const [error, setError] = useState('')
const [readError, setReadError] = useState('')
const [draft, setDraft] = useState('')
- const [attachments, setAttachments] = useState<Record<string, unknown>[]>([])
+ const [attachments, setAttachments] = useState<Attachment[]>([])
const [restored, setRestored] = useState(false)
const [pending, setPending] = useState<PreparedCanonicalGroupSend | null>(null)
const [busy, setBusy] = useState(false)
@@ -46,7 +48,7 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
if (entry) {
setPending(entry)
setDraft(String(entry.params.payload.text ?? ''))
- setAttachments((entry.params.payload.attachments as Record<string, unknown>[] | undefined) ?? [])
+ setAttachments((entry.params.payload.attachments as Attachment[] | undefined) ?? [])
}
setRestored(true)
@@ -122,7 +124,7 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
if (!alive.current) {return}
setPending(exact)
setDraft(String(exact.params.payload.text ?? ''))
- setAttachments((exact.params.payload.attachments as Record<string, unknown>[] | undefined) ?? [])
+ setAttachments((exact.params.payload.attachments as Attachment[] | undefined) ?? [])
await canonicalGroupRequest(exact.binding, 'groups.send', exact.params)
await retireCanonicalGroupSend(exact.binding, exact.params.event_id)
@@ -158,6 +160,7 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
</div>}
{pending && <p role="status">{labels.restoredPendingSend}</p>}
<form className="flex gap-2" onSubmit={event => { event.preventDefault(); send() }}>
+ <CanonicalGroupAttachments binding={binding} attachments={attachments} onChange={setAttachments} disabled={!restored || busy || !!pending} />
<textarea aria-label={labels.groupMessage} className="min-w-0 flex-1" disabled={!restored || busy || !!pending} onChange={e => setDraft(e.target.value)} value={draft} />
<Button disabled={!restored || busy || (!pending && !draft.trim() && !attachments.length) || !state?.driver_status} type="submit">{pending ? labels.retry : labels.send}</Button>
</form>