system.ts 4.0 KB

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