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

+115 -2
View File
@@ -104,6 +104,119 @@ describe("NanobotTui composer", () => {
expect(setup.captureCharFrame()).toContain("Ask nanobot anything")
})
test("turns a confirmed pasted video path into a removable reference and retains failed sends", async () => {
setup = await createRenderer({ width: 80, height: 24, screenMode: "alternate-screen" })
const transport = client()
transport.inspectAttachment = async (path) => {
if (path.includes("missing")) throw new Error("file not found")
return { path, name: "my clip.mp4", size: 6, kind: "video", mime: "video/mp4", url: "/api/media/sig/path" }
}
const sentOptions: MessageOptions[] = []
transport.sendAttachments = async (_content, options) => {
sentOptions.push(options)
if (sentOptions.length === 1) throw new Error("source unavailable")
return "accepted"
}
const app = NanobotTui.mount(setup.renderer, options, transport, new MockTreeSitterClient({ autoResolveTimeout: 0 }))
app.accept({ event: "attached", chat_id: "chat" })
const ui = app as unknown as { ready: boolean; composer: TextareaRenderable; status: { plainText: string } }
await waitUntil(() => ui.ready)
await setup.mockInput.pasteBracketedText('"/project/my clip.mp4"')
await waitUntil(() => ui.composer.plainText.includes("[Video #1: my clip.mp4]"))
ui.composer.submit()
await waitUntil(() => ui.status.plainText.includes("source unavailable"))
expect(ui.composer.plainText).toContain("[Video #1: my clip.mp4]")
expect(sentOptions[0]?.media).toEqual([{ path: "/project/my clip.mp4", name: "my clip.mp4" }])
ui.composer.submit()
await waitUntil(() => ui.composer.plainText === "")
await setup.mockInput.pasteBracketedText("./missing.mp4")
await waitUntil(() => ui.status.plainText.includes("file not found"))
expect(ui.composer.plainText).toBe("./missing.mp4")
})
test.each([false, true])("preserves text %s while a native pasted path is checked", async (prepend) => {
setup = await createRenderer({ width: 100, height: 24, screenMode: "alternate-screen" })
const transport = client()
let complete!: (file: Awaited<ReturnType<typeof transport.inspectAttachment>>) => void
transport.inspectAttachment = () => new Promise((resolve) => { complete = resolve })
const sent: Array<{ content: string; options: MessageOptions }> = []
transport.sendAttachments = async (content, options) => { sent.push({ content, options }); return "accepted" }
const app = NanobotTui.mount(setup.renderer, options, transport, new MockTreeSitterClient({ autoResolveTimeout: 0 }))
app.accept({ event: "attached", chat_id: "chat" })
const ui = app as unknown as { ready: boolean; composer: TextareaRenderable; pathInspectionPending: boolean }
await waitUntil(() => ui.ready)
const path = "/project/image.png"
await setup.mockInput.pasteBracketedText(path)
if (prepend) ui.composer.cursorOffset = 0
await setup.mockInput.typeText(prepend ? "请看 " : " describe it")
complete({ path, name: "image.png", size: 6, kind: "image", mime: "image/png", url: "/api/media/sig/path" })
await waitUntil(() => !ui.pathInspectionPending)
expect(ui.composer.plainText).toContain("[Image #1: image.png]")
ui.composer.submit()
await waitUntil(() => sent.length === 1)
expect(sent[0]?.content).toBe(prepend ? "请看" : "describe it")
expect(sent[0]?.options.media).toEqual([{ path, name: "image.png" }])
})
test("cancels an edited native path and clears checking status without losing the text", async () => {
setup = await createRenderer({ width: 100, height: 24, screenMode: "alternate-screen" })
const transport = client()
let complete!: (file: Awaited<ReturnType<typeof transport.inspectAttachment>>) => void
transport.inspectAttachment = () => new Promise((resolve) => { complete = resolve })
const app = NanobotTui.mount(setup.renderer, options, transport, new MockTreeSitterClient({ autoResolveTimeout: 0 }))
app.accept({ event: "attached", chat_id: "chat" })
const ui = app as unknown as { ready: boolean; composer: TextareaRenderable; pathInspectionPending: boolean; status: { plainText: string }; draft: { imageCount: number } }
await waitUntil(() => ui.ready)
await setup.mockInput.pasteBracketedText("/project/image.png")
ui.composer.setText("/project/other.png")
complete({ path: "/project/image.png", name: "image.png", size: 6, kind: "image", mime: "image/png", url: "/api/media/sig/path" })
await waitUntil(() => !ui.pathInspectionPending)
expect(ui.draft.imageCount).toBe(0)
expect(ui.composer.plainText).toBe("/project/other.png")
expect(ui.status.plainText).not.toContain("Checking local attachment")
})
test("allows one referenced video plus four non-video attachments including a clipboard image", async () => {
setup = await createRenderer({ width: 100, height: 24, screenMode: "alternate-screen" })
const transport = client()
transport.inspectAttachment = async (path) => ({
path, name: path.split("/").at(-1)!, size: 6, url: "/api/media/sig/path",
kind: path.endsWith("mp4") ? "video" : "file",
mime: path.endsWith("mp4") ? "video/mp4" : "text/plain",
})
const sentOptions: MessageOptions[] = []
transport.sendAttachments = async (_content, options) => { sentOptions.push(options); return "accepted" }
const app = NanobotTui.mount(setup.renderer, options, transport,
new MockTreeSitterClient({ autoResolveTimeout: 0 }), undefined,
{ read: async () => ({ mimeType: "image/png", dataUrl: "data:image/png;base64,eA==" }), dispose: async () => {} })
app.accept({ event: "attached", chat_id: "chat" })
const ui = app as unknown as { ready: boolean; pathInspectionPending: boolean; composer: TextareaRenderable; draft: { imageCount: number }; status: { plainText: string } }
await waitUntil(() => ui.ready)
for (const [index, name] of ["clip.mp4", "one.txt", "two.txt", "three.txt"].entries()) {
await setup.mockInput.pasteBracketedText(`/project/${name}`)
await waitUntil(() => !ui.pathInspectionPending)
expect(ui.draft.imageCount, `${name}: ${ui.composer.plainText}; ${ui.status.plainText}`).toBe(index + 1)
}
const beforeResize = ui.composer.plainText
for (const width of [140, 40, 100]) {
setup.resize(width, 30)
await setup.flush()
expect(ui.composer.plainText).toBe(beforeResize)
expect(ui.draft.imageCount).toBe(4)
}
setup.mockInput.pressKey("v", { ctrl: true })
await waitUntil(() => ui.draft.imageCount === 5)
setup.mockInput.pressKey("v", { ctrl: true })
await waitUntil(() => ui.status.plainText.includes("4 images or files"))
expect(ui.draft.imageCount).toBe(5)
ui.composer.submit()
await waitUntil(() => sentOptions.length === 1)
expect(sentOptions[0]?.media).toHaveLength(5)
expect(sentOptions[0]?.media?.filter((item) => item.path)).toHaveLength(4)
expect(sentOptions[0]?.media?.at(-1)?.data_url).toBe("data:image/png;base64,eA==")
await waitUntil(() => ui.composer.plainText === "")
})
test("compacts large pastes in the composer without changing the sent text", async () => {
const sent: string[] = []
setup = await createRenderer({ width: 72, height: 20, screenMode: "alternate-screen" })
@@ -248,7 +361,7 @@ describe("NanobotTui composer", () => {
setup.mockInput.pressKey("v", { ctrl: true })
await waitUntil(() => ui.composer.plainText === "这是什么? [Image #1] ")
setup.mockInput.pressTab()
expect(ui.status.plainText).toContain("Images cannot be queued")
expect(ui.status.plainText).toContain("Attachments cannot be queued")
expect(ui.composer.plainText).toBe("这是什么? [Image #1] ")
ui.composer.submit()
await waitUntil(() => sent.length === 2)
@@ -405,7 +518,7 @@ describe("NanobotTui composer", () => {
setup.mockInput.pressKey("v", { ctrl: true })
await waitUntil(() => ui.composer.plainText === "/model [Image #1] ")
ui.composer.submit()
await waitUntil(() => ui.status.plainText.includes("Remove the image before running a command."))
await waitUntil(() => ui.status.plainText.includes("Remove attachments before running a command."))
expect(sent).toEqual([])
expect(ui.composer.plainText).toBe("/model [Image #1] ")