system.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 { Skill } from "../skill"
  6. import { Instance } from "../project/instance"
  7. import path from "path"
  8. import os from "os"
  9. import PROMPT_ANTHROPIC from "./prompt/anthropic.txt"
  10. import PROMPT_ANTHROPIC_WITHOUT_TODO from "./prompt/qwen.txt"
  11. import PROMPT_POLARIS from "./prompt/polaris.txt"
  12. import PROMPT_BEAST from "./prompt/beast.txt"
  13. import PROMPT_GEMINI from "./prompt/gemini.txt"
  14. import PROMPT_ANTHROPIC_SPOOF from "./prompt/anthropic_spoof.txt"
  15. import PROMPT_CODEX from "./prompt/codex.txt"
  16. import type { Provider } from "@/provider/provider"
  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 provider(model: Provider.Model) {
  23. if (model.api.id.includes("gpt-5")) return [PROMPT_CODEX]
  24. if (model.api.id.includes("gpt-") || model.api.id.includes("o1") || model.api.id.includes("o3"))
  25. return [PROMPT_BEAST]
  26. if (model.api.id.includes("gemini-")) return [PROMPT_GEMINI]
  27. if (model.api.id.includes("claude")) return [PROMPT_ANTHROPIC]
  28. if (model.api.id.includes("polaris-alpha")) return [PROMPT_POLARIS]
  29. return [PROMPT_ANTHROPIC_WITHOUT_TODO]
  30. }
  31. export async function environment() {
  32. const project = Instance.project
  33. return [
  34. [
  35. `Here is some useful information about the environment you are running in:`,
  36. `<env>`,
  37. ` Working directory: ${Instance.directory}`,
  38. ` Is directory a git repo: ${project.vcs === "git" ? "yes" : "no"}`,
  39. ` Platform: ${process.platform}`,
  40. ` Today's date: ${new Date().toDateString()}`,
  41. `</env>`,
  42. `<files>`,
  43. ` ${
  44. project.vcs === "git"
  45. ? await Ripgrep.tree({
  46. cwd: Instance.directory,
  47. limit: 200,
  48. })
  49. : ""
  50. }`,
  51. `</files>`,
  52. ].join("\n"),
  53. ]
  54. }
  55. const LOCAL_RULE_FILES = [
  56. "AGENTS.md",
  57. "CLAUDE.md",
  58. "CONTEXT.md", // deprecated
  59. ]
  60. const GLOBAL_RULE_FILES = [
  61. path.join(Global.Path.config, "AGENTS.md"),
  62. path.join(os.homedir(), ".claude", "CLAUDE.md"),
  63. ]
  64. export async function custom() {
  65. const config = await Config.get()
  66. const paths = new Set<string>()
  67. for (const localRuleFile of LOCAL_RULE_FILES) {
  68. const matches = await Filesystem.findUp(localRuleFile, Instance.directory, Instance.worktree)
  69. if (matches.length > 0) {
  70. matches.forEach((path) => paths.add(path))
  71. break
  72. }
  73. }
  74. for (const globalRuleFile of GLOBAL_RULE_FILES) {
  75. if (await Bun.file(globalRuleFile).exists()) {
  76. paths.add(globalRuleFile)
  77. break
  78. }
  79. }
  80. if (config.instructions) {
  81. for (let instruction of config.instructions) {
  82. if (instruction.startsWith("~/")) {
  83. instruction = path.join(os.homedir(), instruction.slice(2))
  84. }
  85. let matches: string[] = []
  86. if (path.isAbsolute(instruction)) {
  87. matches = await Array.fromAsync(
  88. new Bun.Glob(path.basename(instruction)).scan({
  89. cwd: path.dirname(instruction),
  90. absolute: true,
  91. onlyFiles: true,
  92. }),
  93. ).catch(() => [])
  94. } else {
  95. matches = await Filesystem.globUp(instruction, Instance.directory, Instance.worktree).catch(() => [])
  96. }
  97. matches.forEach((path) => paths.add(path))
  98. }
  99. }
  100. const found = Array.from(paths).map((p) =>
  101. Bun.file(p)
  102. .text()
  103. .catch(() => "")
  104. .then((x) => "Instructions from: " + p + "\n" + x),
  105. )
  106. return Promise.all(found).then((result) => result.filter(Boolean))
  107. }
  108. }