mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-29 06:13:46 +00:00
feat(hub,web): serve chat image originals as cached attachments
Pasted images were stored inline in the message JSON as full-size base64 data URLs. Every conversation load (open, paginate, mobile reconnect) had to download and parse those megabytes before anything rendered, with no lazy loading and no per-image caching. Measured on this hub: 115 messages holding 138MB of base64, the largest single message 18.3MB. New messages now carry only a small thumbnail (browser-generated, 768px webp/jpeg data URL); the original is uploaded as before and the hub keeps a durable copy under ~/.hapi/attachments (table chat_attachments, schema v27). The chat renders the thumbnail instantly and lazily swaps in the original from GET /api/sessions/:id/attachments/:id when the viewer opens; that endpoint answers sha256 ETag + immutable Cache-Control so repeated opens are free. Sessions scope the URL, deletes remove the copy, and hub/scripts/attachments.ts covers stats/verify/gc/export. Old messages keep their inline previews (migration deliberately deferred). Verified end-to-end against the local hub: upload returns the attachment id/url, original 200 with ETag, revalidation 304, cross-session 404, delete removes file+row (404 after). Typecheck clean; suite status matches the pre-change baseline; fixtures unchanged.
This commit is contained in:
@@ -716,13 +716,16 @@ export type CursorMigrateRefusalReason =
|
||||
export const UploadFileRequestSchema = z.object({
|
||||
filename: z.string().min(1).max(255),
|
||||
content: z.string().min(1),
|
||||
mimeType: z.string().min(1).max(255)
|
||||
mimeType: z.string().min(1).max(255),
|
||||
/** Client attachment id; lets the hub keep a durable copy addressable by id. */
|
||||
attachmentId: z.string().min(1).max(64).regex(/^[A-Za-z0-9._-]+$/).optional()
|
||||
})
|
||||
|
||||
export type UploadFileRequest = z.infer<typeof UploadFileRequestSchema>
|
||||
|
||||
export const DeleteUploadRequestSchema = z.object({
|
||||
path: z.string().min(1)
|
||||
path: z.string().min(1),
|
||||
attachmentId: z.string().min(1).max(64).regex(/^[A-Za-z0-9._-]+$/).optional()
|
||||
})
|
||||
|
||||
export type DeleteUploadRequest = z.infer<typeof DeleteUploadRequestSchema>
|
||||
@@ -955,6 +958,10 @@ export type GeneratedImageResponse = {
|
||||
export type UploadFileResponse = {
|
||||
success: boolean
|
||||
path?: string
|
||||
/** Set when the hub kept a durable image copy for web display. */
|
||||
attachmentId?: string
|
||||
/** Session-scoped URL for the hub copy, e.g. /api/sessions/<id>/attachments/<attachmentId>. */
|
||||
attachmentUrl?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user