mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
* feat(hub,shared): scratchlist v2.2 hub attachment storage foundation (#921) Hub stores scratchlist attachment bytes on filesystem; SQLite holds AttachmentMetadata[] JSON via session_scratchlist.attachments (v11→v12). Upstream ladder: v10→v11 text-only scratchlist table (#896), v11→v12 attachments column. Configurable limits via HAPI_SCRATCHLIST_* env vars. Upload, serve, and limits REST routes; delete entry cleans hub files. Web promote/rehydrate still TODO. Soup renumber branch follows. Co-authored-by: Cursor <cursoragent@cursor.com> * feat(web): scratchlist v2.2 attachment UX (#921) Route scratchlist-mode composer submits with attachments to hub storage, show image thumbnails in the drawer, and rehydrate attachments on promote to composer or queue (hub fetch → CLI upload for send). Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): scratchlist attach submit, float thumbs, copy tooltip (#921) Hub upload adapter now sets path on ready attachments so the composer send button unlocks in scratchlist mode; routing label matches attachments too. Entry thumbnails float left with text wrap; copy tooltip clarifies text-only. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): adapt scratchlist update tests to patch API (#921) update() now takes { text?, attachments? }; v12 CRUD tests still passed a string. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): harden scratchlist attachment ownership and orphan cleanup Resolve claimed hub paths against the current session before persist, count on-disk session bytes for upload caps, delete blobs dropped on entry update, and DELETE pending uploads when composer remove runs. Co-authored-by: Cursor <cursoragent@cursor.com> * chore: drop accidental .cursor files from attachment PR Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): exit scratchlist mode before rehydrate; delete raced uploads Promote-to-composer flushes mode exit so attachments use the chat adapter. Cancel-during-upload deletes the hub blob once upload returns. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): exact UUID delete match; stage hub paths on chat send Reject partial attachment ids on disk delete, and restage scratchlist hub attachments through uploadFile when sending after leaving scratchlist mode. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): skip text-only PUT resolve; cleanup session attachment dirs Text-only edits keep existing attachment metadata after session-id transfer. Require full UUID on resolve. Delete scratchlist attachment files when a session is deleted. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): scratchlist attach route, PUT bytes, orphan deletes Park only hub-resident attachments; subtract removed blobs from the PUT session cap; delete attachment files only when no other entry still references them. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): canonicalize scratchlist attachment filenames Resolve stores the on-disk sanitized name (not claimed.filename) and hardens Content-Disposition against CR/LF/quote injection. Co-authored-by: Cursor <cursoragent@cursor.com> * test(hub): cover toxic filename canonicalize on resolve Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub,web): serialize scratchlist uploads; drop hub blobs after chat stage Per-session upload lock keeps disk byte caps honest under concurrency. After a successful toggle-off chat send, delete the staged hub copies so they no longer count against the session attachment budget. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(shared,web): allow clearing scratchlist attachments; cleanup staged uploads PUT may send attachments:[] without a text change. Staging to chat rolls back partial normal-upload copies on failure. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(hub): re-key scratchlist attachment files on session merge Move hub blobs when scratchlist rows transfer between session ids so quota and path ownership stay correct. Reject PUT that would leave an empty textless entry after clearing attachments. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(web): reuse restored scratchlist hub attachments without re-upload Composer draft remount was re-uploading blobs that already had a hapi-hub:scratchlist path, orphaning the originals against session quota. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+29
-4
@@ -271,6 +271,8 @@ export const SCRATCHLIST_MAX_TEXT_LENGTH = 10_000
|
||||
*/
|
||||
export const SCRATCHLIST_MAX_ENTRY_ID_LENGTH = 128
|
||||
|
||||
import { ScratchlistAttachmentsArraySchema } from './scratchlistAttachments'
|
||||
|
||||
export const ScratchlistEntryCreateRequestSchema = z.object({
|
||||
/**
|
||||
* Optional client-supplied entry id. Lets the web client preserve its
|
||||
@@ -280,20 +282,43 @@ export const ScratchlistEntryCreateRequestSchema = z.object({
|
||||
* generate one.
|
||||
*/
|
||||
entryId: z.string().min(1).max(SCRATCHLIST_MAX_ENTRY_ID_LENGTH).optional(),
|
||||
text: z.string().min(1).max(SCRATCHLIST_MAX_TEXT_LENGTH),
|
||||
text: z.string().max(SCRATCHLIST_MAX_TEXT_LENGTH).default(''),
|
||||
attachments: ScratchlistAttachmentsArraySchema.optional().default([]),
|
||||
/**
|
||||
* Optional client-supplied createdAt. Used by the migration path to
|
||||
* preserve the original timestamps from localStorage. New entries
|
||||
* omit this and let the hub stamp `Date.now()`.
|
||||
*/
|
||||
createdAt: z.number().int().nonnegative().optional()
|
||||
})
|
||||
}).refine(
|
||||
(data) => data.text.trim().length > 0 || data.attachments.length > 0,
|
||||
{ message: 'Scratchlist entry requires text or attachments', path: ['text'] }
|
||||
)
|
||||
|
||||
export type ScratchlistEntryCreateRequest = z.infer<typeof ScratchlistEntryCreateRequestSchema>
|
||||
|
||||
export const ScratchlistEntryUpdateRequestSchema = z.object({
|
||||
text: z.string().min(1).max(SCRATCHLIST_MAX_TEXT_LENGTH)
|
||||
})
|
||||
text: z.string().max(SCRATCHLIST_MAX_TEXT_LENGTH).optional(),
|
||||
attachments: ScratchlistAttachmentsArraySchema.optional(),
|
||||
}).refine(
|
||||
(data) => data.text !== undefined || data.attachments !== undefined,
|
||||
{ message: 'Update requires text and/or attachments', path: ['text'] }
|
||||
).refine(
|
||||
(data) => {
|
||||
// Attachments-only patch may clear the list (`[]`) while keeping text.
|
||||
if (data.text === undefined && data.attachments !== undefined) {
|
||||
return true
|
||||
}
|
||||
if (data.text !== undefined && data.attachments === undefined) {
|
||||
return data.text.trim().length > 0
|
||||
}
|
||||
if (data.text !== undefined && data.attachments !== undefined) {
|
||||
return data.text.trim().length > 0 || data.attachments.length > 0
|
||||
}
|
||||
return true
|
||||
},
|
||||
{ message: 'Scratchlist entry requires non-empty text or attachments', path: ['text'] }
|
||||
)
|
||||
|
||||
export type ScratchlistEntryUpdateRequest = z.infer<typeof ScratchlistEntryUpdateRequestSchema>
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './scratchlistAttachments'
|
||||
export * from './apiTypes'
|
||||
export * from './cursorCliSku'
|
||||
export * from './messages'
|
||||
|
||||
@@ -268,7 +268,14 @@ export const ScratchlistEntrySchema = z.object({
|
||||
entryId: z.string().min(1),
|
||||
text: z.string(),
|
||||
createdAt: z.number(),
|
||||
updatedAt: z.number()
|
||||
updatedAt: z.number(),
|
||||
attachments: z.array(z.object({
|
||||
id: z.string(),
|
||||
filename: z.string(),
|
||||
mimeType: z.string(),
|
||||
size: z.number(),
|
||||
path: z.string(),
|
||||
})).optional().default([])
|
||||
})
|
||||
|
||||
export type ScratchlistEntry = z.infer<typeof ScratchlistEntrySchema>
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
/** Hub-resident scratchlist files use this path prefix in AttachmentMetadata.path */
|
||||
export const HUB_SCRATCHLIST_ATTACHMENT_PATH_PREFIX = 'hapi-hub:scratchlist/'
|
||||
|
||||
export function isHubScratchlistAttachmentPath(path: string): boolean {
|
||||
return path.startsWith(HUB_SCRATCHLIST_ATTACHMENT_PATH_PREFIX)
|
||||
}
|
||||
|
||||
export function toHubScratchlistAttachmentPath(storageKey: string): string {
|
||||
return `${HUB_SCRATCHLIST_ATTACHMENT_PATH_PREFIX}${storageKey}`
|
||||
}
|
||||
|
||||
export function parseHubScratchlistAttachmentPath(path: string): string | null {
|
||||
if (!isHubScratchlistAttachmentPath(path)) {
|
||||
return null
|
||||
}
|
||||
const key = path.slice(HUB_SCRATCHLIST_ATTACHMENT_PATH_PREFIX.length).trim()
|
||||
return key.length > 0 ? key : null
|
||||
}
|
||||
|
||||
export const SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_BYTES_PER_FILE = 10 * 1024 * 1024
|
||||
export const SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_PER_ENTRY = 4
|
||||
export const SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_BYTES_PER_ENTRY = 20 * 1024 * 1024
|
||||
export const SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_BYTES_PER_SESSION = 100 * 1024 * 1024
|
||||
|
||||
export const SCRATCHLIST_ATTACHMENT_DEFAULT_ALLOWED_MIMES = [
|
||||
'image/jpeg',
|
||||
'image/png',
|
||||
'image/gif',
|
||||
'image/webp',
|
||||
'image/svg+xml',
|
||||
'application/pdf',
|
||||
'text/plain',
|
||||
] as const
|
||||
|
||||
export const ScratchlistAttachmentLimitsSchema = z.object({
|
||||
maxBytesPerFile: z.number().int().positive(),
|
||||
maxAttachmentsPerEntry: z.number().int().positive(),
|
||||
maxBytesPerEntry: z.number().int().positive(),
|
||||
maxBytesPerSession: z.number().int().positive(),
|
||||
allowedMimeTypes: z.array(z.string().min(1)),
|
||||
})
|
||||
|
||||
export type ScratchlistAttachmentLimits = z.infer<typeof ScratchlistAttachmentLimitsSchema>
|
||||
|
||||
export const SCRATCHLIST_ATTACHMENT_DEFAULT_LIMITS: ScratchlistAttachmentLimits = {
|
||||
maxBytesPerFile: SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_BYTES_PER_FILE,
|
||||
maxAttachmentsPerEntry: SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_PER_ENTRY,
|
||||
maxBytesPerEntry: SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_BYTES_PER_ENTRY,
|
||||
maxBytesPerSession: SCRATCHLIST_ATTACHMENT_DEFAULT_MAX_BYTES_PER_SESSION,
|
||||
allowedMimeTypes: [...SCRATCHLIST_ATTACHMENT_DEFAULT_ALLOWED_MIMES],
|
||||
}
|
||||
|
||||
/** Persisted attachment metadata — previewUrl is client-only and stripped at hub write */
|
||||
export const ScratchlistAttachmentMetadataSchema = z.object({
|
||||
id: z.string(),
|
||||
filename: z.string(),
|
||||
mimeType: z.string(),
|
||||
size: z.number(),
|
||||
path: z.string(),
|
||||
})
|
||||
|
||||
export type ScratchlistAttachmentMetadata = z.infer<typeof ScratchlistAttachmentMetadataSchema>
|
||||
|
||||
export const ScratchlistAttachmentsArraySchema = z.array(ScratchlistAttachmentMetadataSchema)
|
||||
|
||||
export function parseScratchlistAttachmentsJson(raw: string | null | undefined): ScratchlistAttachmentMetadata[] {
|
||||
if (!raw) return []
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(raw)
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
const result = ScratchlistAttachmentsArraySchema.safeParse(parsed)
|
||||
return result.success ? result.data : []
|
||||
}
|
||||
|
||||
export function serializeScratchlistAttachments(attachments: ScratchlistAttachmentMetadata[]): string | null {
|
||||
if (attachments.length === 0) return null
|
||||
return JSON.stringify(attachments)
|
||||
}
|
||||
|
||||
export function stripPreviewUrls(
|
||||
attachments: Array<{ previewUrl?: string } & ScratchlistAttachmentMetadata>
|
||||
): ScratchlistAttachmentMetadata[] {
|
||||
return attachments.map(({ previewUrl: _preview, ...rest }) => rest)
|
||||
}
|
||||
Reference in New Issue
Block a user