agent.ts 11 KB

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