attachments.mdx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ---
  2. title: "Attachments"
  3. description: ""
  4. ---
  5. OpenCode can add local context to a prompt as text or image media. Current V2
  6. sessions make these attachment types visible to the model:
  7. | Input | Model receives |
  8. | --- | --- |
  9. | UTF-8 text file | The filename and decoded text |
  10. | Directory | A non-recursive listing of its immediate files and directories |
  11. | PNG, JPEG, GIF, or WebP | Image media |
  12. SVG files are treated as text, not image media. PDF, AVIF, BMP, audio, video,
  13. and other binary prompt attachments are not currently included in the model
  14. request. Some clients may let you select a PDF, but V2 does not yet make that
  15. PDF visible to the model.
  16. <Warning>
  17. Use a model that supports image input before attaching an image. OpenCode
  18. passes supported image media to the selected provider, but the provider and
  19. model still enforce their own formats, dimensions, file counts, and size
  20. limits. A text-only model may reject the request.
  21. </Warning>
  22. ## Add attachments
  23. ### TUI
  24. Type `@` followed by a filename and select the result to attach a project file.
  25. This is the preferred way to add source code and other text files:
  26. ```text
  27. Explain the error handling in @src/server.ts
  28. ```
  29. Paste an image from the clipboard with the configured paste key, `Ctrl+V` by
  30. default. You can also drag a supported image into a terminal that exposes the
  31. dropped file path to the TUI. The TUI reads PNG, JPEG, GIF, and WebP as image
  32. attachments; a dropped SVG is inserted as text.
  33. ### Desktop and web
  34. Use **Attach file**, paste, or drag and drop. Attach UTF-8 text or a PNG, JPEG,
  35. GIF, or WebP image. The desktop file picker limits one selection to 20 MiB in
  36. total; the server also applies the per-attachment limit described below.
  37. ### CLI
  38. Pass `--file` or `-f` to `opencode2 run`. Repeat the flag for multiple files:
  39. ```bash
  40. opencode2 run -f src/server.ts -f screenshot.png "Explain the failure"
  41. ```
  42. The run command accepts at most 100 file flags and reads at most 10 MiB per
  43. file. Use it for text files and the four supported image formats; other binary
  44. files do not become model context.
  45. ### API
  46. The V2 prompt and command payloads accept a `files` array. Each item requires a
  47. `uri` and can include `name` and `description`:
  48. ```bash
  49. opencode2 api post /api/session/ses_example/prompt --data '{
  50. "text": "Review this file",
  51. "files": [
  52. {
  53. "uri": "file:///home/me/project/src/server.ts",
  54. "name": "server.ts",
  55. "description": "Request handler"
  56. }
  57. ]
  58. }'
  59. ```
  60. Use an absolute `file:` URL for a file available to the server, or an inline
  61. data URL:
  62. ```json
  63. {
  64. "text": "What is wrong with this layout?",
  65. "files": [
  66. {
  67. "uri": "data:image/png;base64,<base64-data>",
  68. "name": "layout.png"
  69. }
  70. ]
  71. }
  72. ```
  73. HTTP and HTTPS attachment URLs are not supported. OpenCode materializes each
  74. attachment before admitting the prompt and rejects invalid URLs, unreadable
  75. paths, non-files other than directories, and attachments over 20 MiB decoded.
  76. For a text `file:` URL, optional positive `start` and `end` query parameters
  77. select one-based lines:
  78. ```text
  79. file:///home/me/project/src/server.ts?start=20&end=60
  80. ```
  81. The server infers the media type from the bytes. A supplied filename or data
  82. URL media type does not make an unsupported binary format model-visible.
  83. ## Configure image processing
  84. Configure image normalization in `opencode.json` or `opencode.jsonc`:
  85. ```jsonc title="opencode.jsonc"
  86. {
  87. "$schema": "https://opencode.ai/config.json",
  88. "attachments": {
  89. "image": {
  90. "auto_resize": true,
  91. "max_width": 2000,
  92. "max_height": 2000,
  93. "max_base64_bytes": 5242880
  94. }
  95. }
  96. }
  97. ```
  98. All fields are optional:
  99. | Field | Default | Behavior |
  100. | --- | ---: | --- |
  101. | `auto_resize` | `true` | Resize an image that exceeds any configured limit. If `false`, reject it. |
  102. | `max_width` | `2000` | Maximum width in pixels. Must be a positive integer. |
  103. | `max_height` | `2000` | Maximum height in pixels. Must be a positive integer. |
  104. | `max_base64_bytes` | `5242880` | Maximum byte length of the Base64-encoded image string. Must be a positive integer. |
  105. <Note>
  106. In the current V2 runtime, these settings apply to image media produced by
  107. the built-in `read` tool. Images attached directly through the TUI, desktop,
  108. web, CLI, or API bypass this normalization. Resize direct attachments before
  109. adding them if the provider requires smaller media.
  110. </Note>
  111. The `read` tool recognizes PNG, JPEG, GIF, and WebP by their contents and will
  112. ingest at most 20 MiB of source image bytes. It decodes the image and compares
  113. its width, height, and encoded Base64 length with all three configured limits.
  114. When `auto_resize` is `true`, OpenCode preserves the aspect ratio, scales the
  115. image down to the dimension limits, and tries progressively smaller PNG and
  116. JPEG encodings until the Base64 limit is met. The resulting media type can
  117. therefore change to PNG or JPEG. If no encoding fits, the tool call fails.
  118. When `auto_resize` is `false`, exceeding any limit fails the tool call without
  119. modifying the image. An image that cannot be decoded also fails. If the image
  120. resizer cannot be loaded, the `read` tool returns the
  121. original image instead, so these settings are processing limits rather than an
  122. upload or security boundary.
  123. ## Limits and provider behavior
  124. - Direct prompt attachments are limited to 20 MiB decoded per item by the V2
  125. server. Client-specific limits can be lower.
  126. - `max_base64_bytes` counts the encoded Base64 characters in bytes, not the
  127. decoded file size and not the complete `data:` URL.
  128. - Text attachments are inserted into the prompt as text and do not require a
  129. multimodal model. Large text read through the `read` tool has separate
  130. paging and truncation limits.
  131. - Image attachments use provider-native image input. Provider errors can still
  132. occur when OpenCode's limits pass but the selected model's limits do not.
  133. - PDFs and other unsupported binary prompt attachments should be converted to
  134. text or supported images before attaching them.