references.mdx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ---
  2. title: "References"
  3. description: ""
  4. ---
  5. References give OpenCode named access to directories outside the current
  6. project. Use them for documentation, shared libraries, examples, or source from
  7. another repository.
  8. Configure references by alias in `opencode.json` or `opencode.jsonc`:
  9. ```jsonc title="opencode.jsonc"
  10. {
  11. "$schema": "https://opencode.ai/config.json",
  12. "references": {
  13. "docs": {
  14. "path": "../product-docs",
  15. "description": "Use for product behavior and terminology"
  16. },
  17. "effect": {
  18. "repository": "Effect-TS/effect",
  19. "branch": "main",
  20. "description": "Use for Effect implementation details"
  21. }
  22. }
  23. }
  24. ```
  25. ## Local directories
  26. Use `path` for a local directory:
  27. ```jsonc
  28. {
  29. "references": {
  30. "design-system": {
  31. "path": "../design-system",
  32. "description": "Use when working with components or design tokens"
  33. }
  34. }
  35. }
  36. ```
  37. Relative paths resolve from the directory containing the config file that
  38. defines them. Absolute paths and home-relative paths such as `~/docs` are also
  39. supported.
  40. The string shorthand is useful when no other fields are needed:
  41. ```jsonc
  42. {
  43. "references": {
  44. "docs": "../docs",
  45. "shared": "~/work/shared"
  46. }
  47. }
  48. ```
  49. <Note>
  50. A shorthand string is treated as a local path only when it starts with `.`,
  51. `/`, or `~`. Use `./docs`, not `docs`; a bare `docs` value is interpreted as
  52. a Git repository.
  53. </Note>
  54. ## Git repositories
  55. Use `repository` for a remote Git repository. GitHub `owner/repo` shorthand,
  56. Git URLs, host/path forms, and SCP-style remotes are supported.
  57. ```jsonc
  58. {
  59. "references": {
  60. "effect": {
  61. "repository": "Effect-TS/effect",
  62. "branch": "main"
  63. },
  64. "internal-sdk": {
  65. "repository": "git@gitlab.example.com:platform/sdk.git",
  66. "branch": "release/v2"
  67. }
  68. }
  69. }
  70. ```
  71. Without `branch`, OpenCode checks out and refreshes the remote's default
  72. branch. Branch names may contain letters, numbers, `/`, `_`, `.`, and `-`, but
  73. cannot start with `-` or contain `..`. Local `file:` repositories are not
  74. supported.
  75. Git references also support shorthand:
  76. ```jsonc
  77. {
  78. "references": {
  79. "effect": "Effect-TS/effect",
  80. "sdk": "gitlab.com/platform/sdk"
  81. }
  82. }
  83. ```
  84. ### Cloning and storage
  85. OpenCode normalizes a remote and stores one checkout under its global data
  86. directory at `opencode/repos/<host>/<repository-path>`. On a typical Linux
  87. installation, for example, `Effect-TS/effect` is stored at:
  88. ```text
  89. ~/.local/share/opencode/repos/github.com/Effect-TS/effect
  90. ```
  91. Missing repositories are cloned. Existing checkouts are fetched and reset to
  92. the requested branch, or to the remote default branch when `branch` is omitted.
  93. Materialization runs asynchronously when references load or reload, so a new
  94. reference can appear before its checkout is ready. Clone and refresh failures
  95. are logged and do not stop other references from loading.
  96. <Warning>
  97. The cache has one checkout per normalized remote, not one per branch. Do not
  98. configure the same repository at multiple branches; only one branch can be
  99. exposed. Avoid editing cached checkouts because a refresh resets them.
  100. </Warning>
  101. ## Description and visibility
  102. `description` tells agents when a reference is relevant. References with a
  103. description are included in agent instructions with their alias and resolved
  104. path. References without one remain available in `@` autocomplete but are not
  105. advertised automatically.
  106. Set `hidden` to `true` to remove a reference from TUI `@` autocomplete:
  107. ```jsonc
  108. {
  109. "references": {
  110. "internal": {
  111. "path": "../internal",
  112. "description": "Use for internal service behavior",
  113. "hidden": true
  114. }
  115. }
  116. }
  117. ```
  118. `hidden` controls only autocomplete visibility. It does not remove the
  119. reference from the reference API or agent instructions when a description is
  120. present.
  121. ## Use references
  122. Type `@` in the TUI and select a reference alias to attach its root directory:
  123. ```text
  124. Compare the current implementation with @effect
  125. ```
  126. The attachment provides a non-recursive listing of the root's immediate files
  127. and directories. V2 currently attaches references by root alias;
  128. `@alias/path` is not a reference-specific file browser. Ask the agent to
  129. inspect a particular path when more detail is needed.
  130. References do not grant extra tool permissions. Access outside the active
  131. Location remains subject to the agent's normal tool rules and the
  132. `external_directory` permission. Editing a reference additionally requires the
  133. applicable edit permission.
  134. ## Fields
  135. | Field | Local | Git | Description |
  136. | --- | --- | --- | --- |
  137. | `path` | Required | No | Local directory path |
  138. | `repository` | No | Required | Remote Git repository |
  139. | `branch` | No | Optional | Branch to fetch and check out |
  140. | `description` | Optional | Optional | Guidance describing when agents should use it |
  141. | `hidden` | Optional | Optional | Hide it from TUI `@` autocomplete |
  142. An alias cannot be empty or contain `/`, `\`, whitespace, a backtick, or a
  143. comma.