Skip to content

feat: add experimental config to disable automatic file attachment on paste in TUI #38105

Description

@victcity

Pasting a local file path (e.g., /home/user/document.pdf) into the TUI prompt input automatically reads the entire file content and creates a file attachment ([PDF 1]) with the file embedded as a base64 data URL — instead of inserting the path as plain text.

This makes it impossible to use file paths as text input in prompts, and for large files it easily exceeds the model's context window (the entire file content is base64-encoded and sent as a FilePart).

Root causepackages/tui/src/component/prompt/index.tsx:

  1. pasteInputTextpastedFilepath() detects if the pasted text is a local file path
  2. readLocalAttachment(filepath) reads the file content from disk and checks mime type
  3. For PDFs and images, pasteAttachment() base64-encodes the entire file and creates a [PDF N] / [Image N] FilePart with data:...;base64,... as the URL
  4. The original text path is discarded — only the file attachment is sent to the model

Verification — A 316-byte test PDF produces a 452-byte base64 data URL (13.7× inflation). A 100 KB PDF would produce ~137 KB of base64 data (5938× inflation versus the 23-byte path string). This is consistent with the context overflow the reporter experienced with two PDF paths.

Proposed solution: Add a new experimental config option:

{
  "experimental": {
    "disable_paste_attachment": true
  }
}

When set to true, pasteInputText would skip the file-path detection logic (pastedFilepath()readLocalAttachment()) and treat the pasted content as plain text. Users who want to attach files can still use the @ file reference or the dedicated attachment UI.

Expected behavior with disable_paste_attachment: true:

  • Pasting /home/user/document.pdf → inserts the text literal /home/user/document.pdf
  • File attachments only via explicit @ reference or attach button
  • No context wasted on unintended file base64 blobs

Related issues: #32014, #19892


在 TUI 中粘贴本地文件路径(例如 /home/user/document.pdf)时,opencode 会自动读取整个文件内容并将其转为文件附件 ([PDF 1]),以 base64 data URL 形式嵌入,而非将路径作为纯文本插入。

这导致:

  1. 无法在提示词中将文件路径作为文本使用
  2. 大文件会轻易超出模型上下文窗口(整个文件被 base64 编码后作为 FilePart 发送)

根因定位packages/tui/src/component/prompt/index.tsx

  1. pasteInputTextpastedFilepath() 检测粘贴内容是否为本地文件路径
  2. readLocalAttachment(filepath) 从磁盘读取文件内容并检测 MIME 类型
  3. 对于 PDF 和图片,pasteAttachment() 将整个文件 base64 编码,生成 [PDF N] / [Image N] 的 FilePart,URL 为 data:...;base64,...
  4. 原始文本路径被丢弃,仅文件附件被发送给模型

验证数据:一个 316 字节的测试 PDF 会产生 452 字节的 base64 data URL(膨胀 13.7 倍)。100KB 的 PDF 会产生约 137KB 的 base64(相较于 23 字节的路径字符串膨胀约 5938 倍)。这与报告者粘贴两个 PDF 路径即超出上下文的体验一致。

提议方案:新增实验性配置项:

{
  "experimental": {
    "disable_paste_attachment": true
  }
}

设为 true 时,pasteInputText 跳过文件路径检测逻辑(pastedFilepath()readLocalAttachment()),粘贴内容直接作为纯文本插入。需要附件的用户仍可使用 @ 文件引用或附件按钮。

期望行为

  • 粘贴 /home/user/document.pdf → 插入文本 /home/user/document.pdf
  • 仅通过显式 @ 引用或附件按钮附加文件
  • 不因误触文件路径检测浪费上下文

关联 Issues#32014#19892

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions