agent.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import { Config } from "../config/config"
  2. import z from "zod"
  3. import { Provider } from "../provider/provider"
  4. import { generateObject, streamObject, type ModelMessage } from "ai"
  5. import { SystemPrompt } from "../session/system"
  6. import { Instance } from "../project/instance"
  7. import { Truncate } from "../tool/truncation"
  8. import { Auth } from "../auth"
  9. import { ProviderTransform } from "../provider/transform"
  10. import PROMPT_GENERATE from "./generate.txt"
  11. import PROMPT_COMPACTION from "./prompt/compaction.txt"
  12. import PROMPT_EXPLORE from "./prompt/explore.txt"
  13. import PROMPT_SUMMARY from "./prompt/summary.txt"
  14. import PROMPT_TITLE from "./prompt/title.txt"
  15. import { PermissionNext } from "@/permission/next"
  16. import { mergeDeep, pipe, sortBy, values } from "remeda"
  17. import { Global } from "@/global"
  18. import path from "path"
  19. import { Plugin } from "@/plugin"
  20. export namespace Agent {
  21. export const Info = z
  22. .object({
  23. name: z.string(),
  24. description: z.string().optional(),
  25. mode: z.enum(["subagent", "primary", "all"]),
  26. native: z.boolean().optional(),
  27. hidden: z.boolean().optional(),
  28. topP: z.number().optional(),
  29. temperature: z.number().optional(),
  30. color: z.string().optional(),
  31. permission: PermissionNext.Ruleset,
  32. model: z
  33. .object({
  34. modelID: z.string(),
  35. providerID: z.string(),
  36. })
  37. .optional(),
  38. variant: z.string().optional(),
  39. prompt: z.string().optional(),
  40. options: z.record(z.string(), z.any()),
  41. steps: z.number().int().positive().optional(),
  42. })
  43. .meta({
  44. ref: "Agent",
  45. })
  46. export type Info = z.infer<typeof Info>
  47. const state = Instance.state(async () => {
  48. const cfg = await Config.get()
  49. const defaults = PermissionNext.fromConfig({
  50. "*": "allow",
  51. doom_loop: "ask",
  52. external_directory: {
  53. "*": "ask",
  54. [Truncate.DIR]: "allow",
  55. [Truncate.GLOB]: "allow",
  56. },
  57. question: "deny",
  58. plan_enter: "deny",
  59. plan_exit: "deny",
  60. // mirrors github.com/github/gitignore Node.gitignore pattern for .env files
  61. read: {
  62. "*": "allow",
  63. "*.env": "ask",
  64. "*.env.*": "ask",
  65. "*.env.example": "allow",
  66. },
  67. })
  68. const user = PermissionNext.fromConfig(cfg.permission ?? {})
  69. const result: Record<string, Info> = {
  70. build: {
  71. name: "build",
  72. description: "The default agent. Executes tools based on configured permissions.",
  73. options: {},
  74. permission: PermissionNext.merge(
  75. defaults,
  76. PermissionNext.fromConfig({
  77. question: "allow",
  78. plan_enter: "allow",
  79. }),
  80. user,
  81. ),
  82. mode: "primary",
  83. native: true,
  84. },
  85. plan: {
  86. name: "plan",
  87. description: "Plan mode. Disallows all edit tools.",
  88. options: {},
  89. permission: PermissionNext.merge(
  90. defaults,
  91. PermissionNext.fromConfig({
  92. question: "allow",
  93. plan_exit: "allow",
  94. external_directory: {
  95. [path.join(Global.Path.data, "plans", "*")]: "allow",
  96. },
  97. edit: {
  98. "*": "deny",
  99. [path.join(".opencode", "plans", "*.md")]: "allow",
  100. [path.relative(Instance.worktree, path.join(Global.Path.data, path.join("plans", "*.md")))]: "allow",
  101. },
  102. }),
  103. user,
  104. ),
  105. mode: "primary",
  106. native: true,
  107. },
  108. general: {
  109. name: "general",
  110. description: `General-purpose agent for researching complex questions and executing multi-step tasks. Use this agent to execute multiple units of work in parallel.`,
  111. permission: PermissionNext.merge(
  112. defaults,
  113. PermissionNext.fromConfig({
  114. todoread: "deny",
  115. todowrite: "deny",
  116. }),
  117. user,
  118. ),
  119. options: {},
  120. mode: "subagent",
  121. native: true,
  122. },
  123. explore: {
  124. name: "explore",
  125. permission: PermissionNext.merge(
  126. defaults,
  127. PermissionNext.fromConfig({
  128. "*": "deny",
  129. grep: "allow",
  130. glob: "allow",
  131. list: "allow",
  132. bash: "allow",
  133. webfetch: "allow",
  134. websearch: "allow",
  135. codesearch: "allow",
  136. read: "allow",
  137. external_directory: {
  138. [Truncate.DIR]: "allow",
  139. [Truncate.GLOB]: "allow",
  140. },
  141. }),
  142. user,
  143. ),
  144. description: `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.`,
  145. prompt: PROMPT_EXPLORE,
  146. options: {},
  147. mode: "subagent",
  148. native: true,
  149. },
  150. compaction: {
  151. name: "compaction",
  152. mode: "primary",
  153. native: true,
  154. hidden: true,
  155. prompt: PROMPT_COMPACTION,
  156. permission: PermissionNext.merge(
  157. defaults,
  158. PermissionNext.fromConfig({
  159. "*": "deny",
  160. }),
  161. user,
  162. ),
  163. options: {},
  164. },
  165. title: {
  166. name: "title",
  167. mode: "primary",
  168. options: {},
  169. native: true,
  170. hidden: true,
  171. temperature: 0.5,
  172. permission: PermissionNext.merge(
  173. defaults,
  174. PermissionNext.fromConfig({
  175. "*": "deny",
  176. }),
  177. user,
  178. ),
  179. prompt: PROMPT_TITLE,
  180. },
  181. summary: {
  182. name: "summary",
  183. mode: "primary",
  184. options: {},
  185. native: true,
  186. hidden: true,
  187. permission: PermissionNext.merge(
  188. defaults,
  189. PermissionNext.fromConfig({
  190. "*": "deny",
  191. }),
  192. user,
  193. ),
  194. prompt: PROMPT_SUMMARY,
  195. },
  196. }
  197. for (const [key, value] of Object.entries(cfg.agent ?? {})) {
  198. if (value.disable) {
  199. delete result[key]
  200. continue
  201. }
  202. let item = result[key]
  203. if (!item)
  204. item = result[key] = {
  205. name: key,
  206. mode: "all",
  207. permission: PermissionNext.merge(defaults, user),
  208. options: {},
  209. native: false,
  210. }
  211. if (value.model) item.model = Provider.parseModel(value.model)
  212. item.variant = value.variant ?? item.variant
  213. item.prompt = value.prompt ?? item.prompt
  214. item.description = value.description ?? item.description
  215. item.temperature = value.temperature ?? item.temperature
  216. item.topP = value.top_p ?? item.topP
  217. item.mode = value.mode ?? item.mode
  218. item.color = value.color ?? item.color
  219. item.hidden = value.hidden ?? item.hidden
  220. item.name = value.name ?? item.name
  221. item.steps = value.steps ?? item.steps
  222. item.options = mergeDeep(item.options, value.options ?? {})
  223. item.permission = PermissionNext.merge(item.permission, PermissionNext.fromConfig(value.permission ?? {}))
  224. }
  225. // Ensure Truncate.DIR is allowed unless explicitly configured
  226. for (const name in result) {
  227. const agent = result[name]
  228. const explicit = agent.permission.some((r) => {
  229. if (r.permission !== "external_directory") return false
  230. if (r.action !== "deny") return false
  231. return r.pattern === Truncate.DIR || r.pattern === Truncate.GLOB
  232. })
  233. if (explicit) continue
  234. result[name].permission = PermissionNext.merge(
  235. result[name].permission,
  236. PermissionNext.fromConfig({ external_directory: { [Truncate.DIR]: "allow", [Truncate.GLOB]: "allow" } }),
  237. )
  238. }
  239. return result
  240. })
  241. export async function get(agent: string) {
  242. return state().then((x) => x[agent])
  243. }
  244. export async function list() {
  245. const cfg = await Config.get()
  246. return pipe(
  247. await state(),
  248. values(),
  249. sortBy([(x) => (cfg.default_agent ? x.name === cfg.default_agent : x.name === "build"), "desc"]),
  250. )
  251. }
  252. export async function defaultAgent() {
  253. const cfg = await Config.get()
  254. const agents = await state()
  255. if (cfg.default_agent) {
  256. const agent = agents[cfg.default_agent]
  257. if (!agent) throw new Error(`default agent "${cfg.default_agent}" not found`)
  258. if (agent.mode === "subagent") throw new Error(`default agent "${cfg.default_agent}" is a subagent`)
  259. if (agent.hidden === true) throw new Error(`default agent "${cfg.default_agent}" is hidden`)
  260. return agent.name
  261. }
  262. const primaryVisible = Object.values(agents).find((a) => a.mode !== "subagent" && a.hidden !== true)
  263. if (!primaryVisible) throw new Error("no primary visible agent found")
  264. return primaryVisible.name
  265. }
  266. export async function generate(input: { description: string; model?: { providerID: string; modelID: string } }) {
  267. const cfg = await Config.get()
  268. const defaultModel = input.model ?? (await Provider.defaultModel())
  269. const model = await Provider.getModel(defaultModel.providerID, defaultModel.modelID)
  270. const language = await Provider.getLanguage(model)
  271. const system = [PROMPT_GENERATE]
  272. await Plugin.trigger("experimental.chat.system.transform", { model }, { system })
  273. const existing = await list()
  274. const params = {
  275. experimental_telemetry: {
  276. isEnabled: cfg.experimental?.openTelemetry,
  277. metadata: {
  278. userId: cfg.username ?? "unknown",
  279. },
  280. },
  281. temperature: 0.3,
  282. messages: [
  283. ...system.map(
  284. (item): ModelMessage => ({
  285. role: "system",
  286. content: item,
  287. }),
  288. ),
  289. {
  290. role: "user",
  291. content: `Create an agent configuration based on this request: \"${input.description}\".\n\nIMPORTANT: The following identifiers already exist and must NOT be used: ${existing.map((i) => i.name).join(", ")}\n Return ONLY the JSON object, no other text, do not wrap in backticks`,
  292. },
  293. ],
  294. model: language,
  295. schema: z.object({
  296. identifier: z.string(),
  297. whenToUse: z.string(),
  298. systemPrompt: z.string(),
  299. }),
  300. } satisfies Parameters<typeof generateObject>[0]
  301. if (defaultModel.providerID === "openai" && (await Auth.get(defaultModel.providerID))?.type === "oauth") {
  302. const result = streamObject({
  303. ...params,
  304. providerOptions: ProviderTransform.providerOptions(model, {
  305. instructions: SystemPrompt.instructions(),
  306. store: false,
  307. }),
  308. onError: () => {},
  309. })
  310. for await (const part of result.fullStream) {
  311. if (part.type === "error") throw part.error
  312. }
  313. return result.object
  314. }
  315. const result = await generateObject(params)
  316. return result.object
  317. }
  318. }