attachments.mdx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ---
  2. title: "Attachments"
  3. ---
  4. Attachments add local context to a prompt as text or image media. Regardless
  5. of how a prompt is submitted, current V2 sessions make these attachment types
  6. 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. A client accepting a file does not mean its contents are visible to
  15. the model.
  16. <Callout type="warning">
  17. Use a model that supports image input before attaching an image. OpenCode passes supported image media to the selected
  18. provider, but the provider and model still enforce their own formats, dimensions, file counts, and size limits. A
  19. text-only model may reject the request.
  20. </Callout>
  21. ## Add attachments
  22. Desktop and web clients provide **Attach file**, paste, and drag-and-drop controls for supported text and image files. The
  23. desktop file picker limits one selection to 20 MiB in total; the server also applies the per-attachment limit below.
  24. Attachment controls and client-side limits depend on the interface. For programmatic submission, see the generated
  25. [API reference](/api).
  26. V2 prompt and command inputs represent each attachment with a `uri` and
  27. optional `name` and `description`. Use an absolute `file:` URL for a file or
  28. directory available to the server, or an inline `data:` URL. For a text `file:`
  29. URL, optional positive `start` and `end` query parameters select one-based
  30. lines:
  31. ```text
  32. file:///home/me/project/src/server.ts?start=20&end=60
  33. ```
  34. HTTP and HTTPS attachment URLs are not supported. OpenCode materializes each
  35. attachment before admitting the prompt and rejects invalid URLs, unreadable
  36. paths, non-files other than directories, and attachments over 20 MiB decoded.
  37. The server infers the media type from the bytes. A supplied filename or data
  38. URL media type does not make an unsupported binary format model-visible.
  39. ## Configure image processing
  40. Configure image normalization in `opencode.json` or `opencode.jsonc`:
  41. ```jsonc title="opencode.jsonc"
  42. {
  43. "$schema": "https://opencode.ai/config.json",
  44. "media": {
  45. "image": {
  46. "auto_resize": true,
  47. "max_width": 2000,
  48. "max_height": 2000,
  49. "max_base64_bytes": 5242880,
  50. },
  51. },
  52. }
  53. ```
  54. All fields are optional:
  55. | Field | Default | Behavior |
  56. | ------------------ | --------: | ----------------------------------------------------------------------------------- |
  57. | `auto_resize` | `true` | Resize an image that exceeds any configured limit. If `false`, reject it. |
  58. | `max_width` | `2000` | Maximum width in pixels. Must be a positive integer. |
  59. | `max_height` | `2000` | Maximum height in pixels. Must be a positive integer. |
  60. | `max_base64_bytes` | `5242880` | Maximum byte length of the Base64-encoded image string. Must be a positive integer. |
  61. <Callout type="note">
  62. These settings apply to supported image media attached directly to prompts and images produced by the built-in `read`
  63. tool. If the image resizer is unavailable, OpenCode passes the original image through unchanged.
  64. </Callout>
  65. The `read` tool recognizes PNG, JPEG, GIF, and WebP by their contents and will
  66. ingest at most 20 MiB of source image bytes. It decodes the image and compares
  67. its width, height, and encoded Base64 length with all three configured limits.
  68. When `auto_resize` is `true`, OpenCode preserves the aspect ratio, scales the
  69. image down to the dimension limits, and tries progressively smaller PNG and
  70. JPEG encodings until the Base64 limit is met. The resulting media type can
  71. therefore change to PNG or JPEG. If no encoding fits, the tool call fails.
  72. When `auto_resize` is `false`, exceeding any limit fails the tool call without
  73. modifying the image. An image that cannot be decoded also fails. If the image resizer cannot be loaded, OpenCode uses the
  74. original image instead, so these settings are processing limits rather than an upload or security boundary.
  75. ## Limits and provider behavior
  76. - Direct prompt attachments are limited to 20 MiB decoded per item by the V2
  77. server. Client-specific limits can be lower.
  78. - `max_base64_bytes` counts the encoded Base64 characters in bytes, not the
  79. decoded file size and not the complete `data:` URL.
  80. - Text attachments are inserted into the prompt as text and do not require a
  81. multimodal model. Large text read through the `read` tool has separate
  82. paging and truncation limits.
  83. - Image attachments use provider-native image input. Provider errors can still
  84. occur when OpenCode's limits pass but the selected model's limits do not.
  85. - PDFs and other unsupported binary prompt attachments should be converted to
  86. text or supported images before attaching them.