system.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { Ripgrep } from "../file/ripgrep"
  2. import { Global } from "../global"
  3. import { Filesystem } from "../util/filesystem"
  4. import { Config } from "../config/config"
  5. import { Instance } from "../project/instance"
  6. import path from "path"
  7. import os from "os"
  8. import PROMPT_ANTHROPIC from "./prompt/anthropic.txt"
  9. import PROMPT_ANTHROPIC_WITHOUT_TODO from "./prompt/qwen.txt"
  10. import PROMPT_BEAST from "./prompt/beast.txt"
  11. import PROMPT_GEMINI from "./prompt/gemini.txt"
  12. import PROMPT_ANTHROPIC_SPOOF from "./prompt/anthropic_spoof.txt"
  13. import PROMPT_CODEX from "./prompt/codex.txt"
  14. import PROMPT_CODEX_INSTRUCTIONS from "./prompt/codex_header.txt"
  15. import type { Provider } from "@/provider/provider"
  16. import { Flag } from "@/flag/flag"
  17. export namespace SystemPrompt {
  18. export function header(providerID: string) {
  19. if (providerID.includes("anthropic")) return [PROMPT_ANTHROPIC_SPOOF.trim()]
  20. return []
  21. }
  22. export function instructions() {
  23. return PROMPT_CODEX_INSTRUCTIONS.trim()
  24. }
  25. export function provider(model: Provider.Model) {
  26. if (model.api.id.includes("gpt-5")) return [PROMPT_CODEX]
  27. if (model.api.id.includes("gpt-") || model.api.id.includes("o1") || model.api.id.includes("o3"))
  28. return [PROMPT_BEAST]
  29. if (model.api.id.includes("gemini-")) return [PROMPT_GEMINI]
  30. if (model.api.id.includes("claude")) return [PROMPT_ANTHROPIC]
  31. return [PROMPT_ANTHROPIC_WITHOUT_TODO]
  32. }
  33. export async function environment() {
  34. const project = Instance.project
  35. return [
  36. [
  37. `Here is some useful information about the environment you are running in:`,
  38. `<env>`,
  39. ` Working directory: ${Instance.directory}`,
  40. ` Is directory a git repo: ${project.vcs === "git" ? "yes" : "no"}`,
  41. ` Platform: ${process.platform}`,
  42. ` Today's date: ${new Date().toDateString()}`,
  43. `</env>`,
  44. `<files>`,
  45. ` ${
  46. project.vcs === "git" && false
  47. ? await Ripgrep.tree({
  48. cwd: Instance.directory,
  49. limit: 200,
  50. })
  51. : ""
  52. }`,
  53. `</files>`,
  54. ].join("\n"),
  55. ]
  56. }
  57. const LOCAL_RULE_FILES = [
  58. "AGENTS.md",
  59. "CLAUDE.md",
  60. "CONTEXT.md", // deprecated
  61. ]
  62. const GLOBAL_RULE_FILES = [path.join(Global.Path.config, "AGENTS.md")]
  63. if (!Flag.OPENCODE_DISABLE_CLAUDE_CODE_PROMPT) {
  64. GLOBAL_RULE_FILES.push(path.join(os.homedir(), ".claude", "CLAUDE.md"))
  65. }
  66. if (Flag.OPENCODE_CONFIG_DIR) {
  67. GLOBAL_RULE_FILES.push(path.join(Flag.OPENCODE_CONFIG_DIR, "AGENTS.md"))
  68. }
  69. export async function custom() {
  70. const config = await Config.get()
  71. const paths = new Set<string>()
  72. for (const localRuleFile of LOCAL_RULE_FILES) {
  73. const matches = await Filesystem.findUp(localRuleFile, Instance.directory, Instance.worktree)
  74. if (matches.length > 0) {
  75. matches.forEach((path) => paths.add(path))
  76. break
  77. }
  78. }
  79. for (const globalRuleFile of GLOBAL_RULE_FILES) {
  80. if (await Bun.file(globalRuleFile).exists()) {
  81. paths.add(globalRuleFile)
  82. break
  83. }
  84. }
  85. const urls: string[] = []
  86. if (config.instructions) {
  87. for (let instruction of config.instructions) {
  88. if (instruction.startsWith("https://") || instruction.startsWith("http://")) {
  89. urls.push(instruction)
  90. continue
  91. }
  92. if (instruction.startsWith("~/")) {
  93. instruction = path.join(os.homedir(), instruction.slice(2))
  94. }
  95. let matches: string[] = []
  96. if (path.isAbsolute(instruction)) {
  97. matches = await Array.fromAsync(
  98. new Bun.Glob(path.basename(instruction)).scan({
  99. cwd: path.dirname(instruction),
  100. absolute: true,
  101. onlyFiles: true,
  102. }),
  103. ).catch(() => [])
  104. } else {
  105. matches = await Filesystem.globUp(instruction, Instance.directory, Instance.worktree).catch(() => [])
  106. }
  107. matches.forEach((path) => paths.add(path))
  108. }
  109. }
  110. const foundFiles = Array.from(paths).map((p) =>
  111. Bun.file(p)
  112. .text()
  113. .catch(() => "")
  114. .then((x) => "Instructions from: " + p + "\n" + x),
  115. )
  116. const foundUrls = urls.map((url) =>
  117. fetch(url, { signal: AbortSignal.timeout(5000) })
  118. .then((res) => (res.ok ? res.text() : ""))
  119. .catch(() => "")
  120. .then((x) => (x ? "Instructions from: " + url + "\n" + x : "")),
  121. )
  122. return Promise.all([...foundFiles, ...foundUrls]).then((result) => result.filter(Boolean))
  123. }
  124. }