feat(attachments): support local files and video attachments (#6321)

* feat(attachments): support local files and video attachments

Share gateway inspection and send validation for borrowed local paths while preserving binary uploads, original-file ownership, and existing limits.

Related to NAN-284. Native provider video input remains outside this change.

* test: make cross-platform verification fixtures self-contained

* test(webui): align typed fixtures with runtime contracts

* fix(webui): preserve uploaded video playback during history sync

* test(tui): preserve local attachment drafts across resizes

* perf(attachments): preserve upload caches and release video previews

* docs(webui): keep attachment guidance user-focused

* chore: remove redundant dependency comment

* test(providers): make stream idle renewal timing deterministic

* fix(attachments): keep upload byte limits out of local references

Validate common format/count rules separately from upload byte budgets. Keep local inspection and send-time checks metadata-only, and exclude local references from composer and pending-upload totals. Explain upload size rejections with actual size and maximum in all ten locales. Related to NAN-284.

* fix(attachments): retain image references and draft order

* fix(attachments): close local reference UX and authorization gaps

* fix(attachments): preserve focus during asynchronous path confirmation
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-22 18:32:18 +08:00
1 parent 7dfeff9ddf
commit 254f94b068
65 files changed
+1816 -292

No files matched your search

+53 -12
View File
@@ -1,3 +1,4 @@
import { pastedFilePath, PastedPathRange } from "../../../packages/client-events/attachments"
import {
BoxRenderable,
CliRenderEvents,
@@ -221,6 +222,8 @@ export class NanobotTui {
private apiRefreshPromise: Promise<GatewayApiConnection> | null = null
private skillLoadId = 0
private clipboardImagePending = false
private pathInspection: PastedPathRange | null = null
private get pathInspectionPending(): boolean { return this.pathInspection !== null }
private clipboardPasteGeneration = 0
private composerValue = ""
private composerCursor = 0
@@ -508,6 +511,10 @@ export class NanobotTui {
private submit(): void {
if (this.quitting || this.composer.isDestroyed) return
const visibleContent = this.composer.plainText.trim()
if (this.pathInspectionPending) {
this.status.content = "Checking local attachment…"
return
}
if (this.sessionLoading) {
this.status.content = "Loading sessions…"
return
@@ -564,7 +571,7 @@ export class NanobotTui {
}
const command = this.commandMenu.resolve(visibleContent)
if ((command || visibleContent.startsWith("!")) && this.draft.media(visibleContent).length) {
this.status.content = "Remove the image before running a command."
this.status.content = "Remove attachments before running a command."
return
}
if (command?.source === "tui") {
@@ -607,7 +614,7 @@ export class NanobotTui {
this.attachmentSendPending = true
const originalText = this.composer.plainText
const chatId = this.client.activeChatId
this.status.content = "Uploading attachments…"
this.status.content = "Sending attachments…"
void this.client.sendAttachments(prompt.content, prompt.options).then((turnId) => {
if (this.client.activeChatId !== chatId || this.composer.plainText !== originalText) return
this.sendPrompt(prompt, steering, turnId)
@@ -1246,10 +1253,7 @@ export class NanobotTui {
this.status.content = "Duplicate image placeholder. Rename or remove it before sending."
return false
}
if (!this.hasPrompt(prompt)) return false
if ((prompt.options.media?.length || 0) <= MAX_DRAFT_IMAGES) return true
this.status.content = `Remove images until ${MAX_DRAFT_IMAGES} or fewer remain`
return false
return this.hasPrompt(prompt)
}
private restoreQueuedPrompts(): void {
@@ -1264,7 +1268,7 @@ export class NanobotTui {
if (!this.activeTurn || !this.ready) return
const visibleContent = this.composer.plainText.trim()
if (this.draft.media(visibleContent).length) {
this.status.content = "Images cannot be queued. Press Enter to send now."
this.status.content = "Attachments cannot be queued. Press Enter to send now."
return
}
if (this.commandMenu.resolve(visibleContent)?.source === "tui") {
@@ -1874,6 +1878,11 @@ export class NanobotTui {
value = edit.value
cursor = edit.cursor
}
if (this.pathInspection && !this.pathInspection.update(value)) {
this.pathInspection = null
if (this.activeTurn) this.renderActiveStatus()
else this.status.content = this.ready ? this.readyStatus() : this.connectionMessage
}
this.composerValue = value
this.composerCursor = cursor
this.draft.prune(value)
@@ -1933,6 +1942,7 @@ export class NanobotTui {
private setComposer(content: string): void {
this.clipboardPasteGeneration += 1
this.pathInspection = null
this.draft.clear()
this.composer.setText(content)
this.composer.cursorOffset = content.length
@@ -1945,10 +1955,6 @@ export class NanobotTui {
private async pasteClipboardImage(): Promise<void> {
if (this.clipboardImagePending) return
if (this.draft.imageCount >= MAX_DRAFT_IMAGES) {
this.status.content = `A message can include up to ${MAX_DRAFT_IMAGES} images`
return
}
const generation = this.clipboardPasteGeneration
this.clipboardImagePending = true
this.status.content = "Reading clipboard image…"
@@ -1957,7 +1963,7 @@ export class NanobotTui {
if (this.quitting || generation !== this.clipboardPasteGeneration) return
const insertion = this.draft.image(image, this.composer.plainText)
if (!insertion) {
this.status.content = `A message can include up to ${MAX_DRAFT_IMAGES} images`
this.status.content = `A message can include up to ${MAX_DRAFT_IMAGES} images or files`
return
}
this.composer.insertText(insertion.text)
@@ -1981,6 +1987,41 @@ export class NanobotTui {
private handlePaste(event: PasteEvent): void {
event.preventDefault()
const value = stripAnsiSequences(decodePasteBytes(event.bytes))
const path = pastedFilePath(value)
if (path && !this.pathInspectionPending) {
this.composer.insertText(value)
const pasted = this.composer.plainText
const cursor = this.composerStringCursor()
const generation = this.clipboardPasteGeneration
const chatId = this.client.activeChatId
const range = new PastedPathRange(pasted, cursor - value.length, cursor)
this.pathInspection = range
this.status.content = "Checking local attachment…"
void this.client.inspectAttachment(path).then((file) => {
if (this.quitting || this.composer.isDestroyed || generation !== this.clipboardPasteGeneration
|| chatId !== this.client.activeChatId || this.pathInspection !== range) return
if (!range.update(this.composer.plainText)) return
const insertion = this.draft.localFile(file, this.composer.plainText)
if (!insertion) throw new Error("Too many attachments; remove one and paste the path again")
const edited = range.replace(insertion.text, this.composerStringCursor())!
this.pathInspection = null
this.composer.setText(edited.value)
this.setComposerStringCursor(edited.value, edited.cursor)
this.status.content = `Attached ${file.name}. Review before sending.`
}).catch((error: unknown) => {
if (generation !== this.clipboardPasteGeneration || this.quitting || this.pathInspection !== range) return
this.pathInspection = null
this.status.content = error instanceof Error ? error.message : "Attachment unavailable; path retained"
}).finally(() => {
if (this.pathInspection !== range) return
this.pathInspection = null
if (!this.quitting && !this.composer.isDestroyed) {
if (this.activeTurn) this.renderActiveStatus()
else this.status.content = this.ready ? this.readyStatus() : this.connectionMessage
}
})
return
}
const insertion = this.draft.paste(value)
if (!insertion.text) return
this.composer.insertText(insertion.text)