agent.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. export * as AgentPlugin from "./agent"
  2. import path from "path"
  3. import { Effect } from "effect"
  4. import { AgentV2 } from "../agent"
  5. import { Global } from "../global"
  6. import { Location } from "../location"
  7. import { PermissionV2 } from "../permission"
  8. import { PluginV2 } from "../plugin"
  9. const TRUNCATION_GLOB = path.join(Global.Path.data, "tool-output", "*")
  10. const PROMPT_EXPLORE = `You are a file search specialist. You excel at thoroughly navigating and exploring codebases.
  11. Your strengths:
  12. - Rapidly finding files using glob patterns
  13. - Searching code and text with powerful regex patterns
  14. - Reading and analyzing file contents
  15. Guidelines:
  16. - Use Glob for broad file pattern matching
  17. - Use Grep for searching file contents with regex
  18. - Use Read when you know the specific file path you need to read
  19. - Adapt your search approach based on the thoroughness level specified by the caller
  20. - Return file paths as absolute paths in your final response
  21. - For clear communication, avoid using emojis
  22. - Do not create any files, or run bash commands that modify the user's system state in any way
  23. Complete the user's search request efficiently and report your findings clearly.`
  24. const PROMPT_COMPACTION = `You are an anchored context summarization assistant for coding sessions.
  25. Summarize only the conversation history you are given. The newest turns may be kept verbatim outside your summary, so focus on the older context that still matters for continuing the work.
  26. If the prompt includes a <previous-summary> block, treat it as the current anchored summary. Update it with the new history by preserving still-true details, removing stale details, and merging in new facts.
  27. Always follow the exact output structure requested by the user prompt. Keep every section, preserve exact file paths and identifiers when known, and prefer terse bullets over paragraphs.
  28. Do not answer the conversation itself. Do not mention that you are summarizing, compacting, or merging context. Respond in the same language as the conversation.`
  29. const PROMPT_TITLE = `You are a title generator. You output ONLY a thread title. Nothing else.
  30. <task>
  31. Generate a brief title that would help the user find this conversation later.
  32. Follow all rules in <rules>
  33. Use the <examples> so you know what a good title looks like.
  34. Your output must be:
  35. - A single line
  36. - <=50 characters
  37. - No explanations
  38. </task>
  39. <rules>
  40. - you MUST use the same language as the user message you are summarizing
  41. - Title must be grammatically correct and read naturally - no word salad
  42. - Never include tool names in the title (e.g. "read tool", "bash tool", "edit tool")
  43. - Focus on the main topic or question the user needs to retrieve
  44. - Vary your phrasing - avoid repetitive patterns like always starting with "Analyzing"
  45. - When a file is mentioned, focus on WHAT the user wants to do WITH the file, not just that they shared it
  46. - Keep exact: technical terms, numbers, filenames, HTTP codes
  47. - Remove: the, this, my, a, an
  48. - Never assume tech stack
  49. - Never use tools
  50. - NEVER respond to questions, just generate a title for the conversation
  51. - The title should NEVER include "summarizing" or "generating" when generating a title
  52. - DO NOT SAY YOU CANNOT GENERATE A TITLE OR COMPLAIN ABOUT THE INPUT
  53. - Always output something meaningful, even if the input is minimal.
  54. - If the user message is short or conversational (e.g. "hello", "lol", "what's up", "hey"):
  55. -> create a title that reflects the user's tone or intent (such as Greeting, Quick check-in, Light chat, Intro message, etc.)
  56. </rules>
  57. <examples>
  58. "debug 500 errors in production" -> Debugging production 500 errors
  59. "refactor user service" -> Refactoring user service
  60. "why is app.js failing" -> app.js failure investigation
  61. "implement rate limiting" -> Rate limiting implementation
  62. "how do I connect postgres to my API" -> Postgres API connection
  63. "best practices for React hooks" -> React hooks best practices
  64. "@src/auth.ts can you add refresh token support" -> Auth refresh token support
  65. "@utils/parser.ts this is broken" -> Parser bug fix
  66. "look at @config.json" -> Config review
  67. "@App.tsx add dark mode toggle" -> Dark mode toggle in App
  68. </examples>`
  69. const PROMPT_SUMMARY = `Summarize what was done in this conversation. Write like a pull request description.
  70. Rules:
  71. - 2-3 sentences max
  72. - Describe the changes made, not the process
  73. - Do not mention running tests, builds, or other validation steps
  74. - Do not explain what the user asked for
  75. - Write in first person (I added..., I fixed...)
  76. - Never ask questions or add new questions
  77. - If the conversation ends with an unanswered question to the user, preserve that exact question
  78. - If the conversation ends with an imperative statement or request to the user (e.g. "Now please run the command and paste the console output"), always include that exact request in the summary`
  79. export const Plugin = PluginV2.define({
  80. id: PluginV2.ID.make("agent"),
  81. effect: Effect.gen(function* () {
  82. const agent = yield* AgentV2.Service
  83. const location = yield* Location.Service
  84. const worktree = location.directory
  85. const whitelistedDirs = [TRUNCATION_GLOB, path.join(Global.Path.tmp, "*")]
  86. const readonlyExternalDirectory: PermissionV2.Ruleset = [
  87. { action: "external_directory", resource: "*", effect: "ask" },
  88. ...whitelistedDirs.map(
  89. (resource): PermissionV2.Rule => ({ action: "external_directory", resource, effect: "allow" }),
  90. ),
  91. ]
  92. const defaults: PermissionV2.Ruleset = [
  93. { action: "*", resource: "*", effect: "allow" },
  94. ...readonlyExternalDirectory,
  95. { action: "question", resource: "*", effect: "deny" },
  96. { action: "plan_enter", resource: "*", effect: "deny" },
  97. { action: "plan_exit", resource: "*", effect: "deny" },
  98. { action: "read", resource: "*", effect: "allow" },
  99. { action: "read", resource: "*.env", effect: "ask" },
  100. { action: "read", resource: "*.env.*", effect: "ask" },
  101. { action: "read", resource: "*.env.example", effect: "allow" },
  102. ]
  103. yield* agent.update((editor) => {
  104. editor.update(AgentV2.ID.make("build"), (item) => {
  105. item.description = "The default agent. Executes tools based on configured permissions."
  106. item.mode = "primary"
  107. item.permissions.push(
  108. ...PermissionV2.merge(defaults, [
  109. { action: "question", resource: "*", effect: "allow" },
  110. { action: "plan_enter", resource: "*", effect: "allow" },
  111. ]),
  112. )
  113. })
  114. editor.update(AgentV2.ID.make("plan"), (item) => {
  115. item.description = "Plan mode. Disallows all edit tools."
  116. item.mode = "primary"
  117. item.permissions.push(
  118. ...PermissionV2.merge(defaults, [
  119. { action: "question", resource: "*", effect: "allow" },
  120. { action: "plan_exit", resource: "*", effect: "allow" },
  121. { action: "external_directory", resource: path.join(Global.Path.data, "plans", "*"), effect: "allow" },
  122. { action: "edit", resource: "*", effect: "deny" },
  123. { action: "edit", resource: path.join(".opencode", "plans", "*.md"), effect: "allow" },
  124. {
  125. action: "edit",
  126. resource: path.relative(worktree, path.join(Global.Path.data, "plans", "*.md")),
  127. effect: "allow",
  128. },
  129. ]),
  130. )
  131. })
  132. editor.update(AgentV2.ID.make("general"), (item) => {
  133. item.description =
  134. "General-purpose agent for researching complex questions and executing multi-step tasks. Use this agent to execute multiple units of work in parallel."
  135. item.mode = "subagent"
  136. item.permissions.push(...PermissionV2.merge(defaults, [{ action: "todowrite", resource: "*", effect: "deny" }]))
  137. })
  138. editor.update(AgentV2.ID.make("explore"), (item) => {
  139. item.description =
  140. 'Fast agent specialized for exploring codebases. Use this when you need to quickly find files by patterns (eg. "src/components/**/*.tsx"), search code for keywords (eg. "API endpoints"), or answer questions about the codebase (eg. "how do API endpoints work?"). When calling this agent, specify the desired thoroughness level: "quick" for basic searches, "medium" for moderate exploration, or "very thorough" for comprehensive analysis across multiple locations and naming conventions.'
  141. item.system = PROMPT_EXPLORE
  142. item.mode = "subagent"
  143. item.permissions.push(
  144. ...PermissionV2.merge(
  145. defaults,
  146. [
  147. { action: "*", resource: "*", effect: "deny" },
  148. { action: "grep", resource: "*", effect: "allow" },
  149. { action: "glob", resource: "*", effect: "allow" },
  150. { action: "webfetch", resource: "*", effect: "allow" },
  151. { action: "websearch", resource: "*", effect: "allow" },
  152. { action: "read", resource: "*", effect: "allow" },
  153. ],
  154. readonlyExternalDirectory,
  155. ),
  156. )
  157. })
  158. editor.update(AgentV2.ID.make("compaction"), (item) => {
  159. item.mode = "primary"
  160. item.hidden = true
  161. item.system = PROMPT_COMPACTION
  162. item.permissions.push(...PermissionV2.merge(defaults, [{ action: "*", resource: "*", effect: "deny" }]))
  163. })
  164. editor.update(AgentV2.ID.make("title"), (item) => {
  165. item.mode = "primary"
  166. item.hidden = true
  167. item.system = PROMPT_TITLE
  168. item.permissions.push(...PermissionV2.merge(defaults, [{ action: "*", resource: "*", effect: "deny" }]))
  169. })
  170. editor.update(AgentV2.ID.make("summary"), (item) => {
  171. item.mode = "primary"
  172. item.hidden = true
  173. item.system = PROMPT_SUMMARY
  174. item.permissions.push(...PermissionV2.merge(defaults, [{ action: "*", resource: "*", effect: "deny" }]))
  175. })
  176. })
  177. }),
  178. })