|
| 1 | +import path from "path" |
| 2 | +import os from "os" |
| 3 | +import { Global } from "../global" |
| 4 | +import { Filesystem } from "../util/filesystem" |
| 5 | +import { Config } from "../config/config" |
| 6 | +import { Instance } from "../project/instance" |
| 7 | +import { Flag } from "@/flag/flag" |
| 8 | +import { Log } from "../util/log" |
| 9 | +import type { MessageV2 } from "./message-v2" |
| 10 | + |
| 11 | +const log = Log.create({ service: "instruction" }) |
| 12 | + |
| 13 | +const FILES = [ |
| 14 | + "AGENTS.md", |
| 15 | + "CLAUDE.md", |
| 16 | + "CONTEXT.md", // deprecated |
| 17 | +] |
| 18 | + |
| 19 | +function globalFiles() { |
| 20 | + const files = [path.join(Global.Path.config, "AGENTS.md")] |
| 21 | + if (!Flag.OPENCODE_DISABLE_CLAUDE_CODE_PROMPT) { |
| 22 | + files.push(path.join(os.homedir(), ".claude", "CLAUDE.md")) |
| 23 | + } |
| 24 | + if (Flag.OPENCODE_CONFIG_DIR) { |
| 25 | + files.push(path.join(Flag.OPENCODE_CONFIG_DIR, "AGENTS.md")) |
| 26 | + } |
| 27 | + return files |
| 28 | +} |
| 29 | + |
| 30 | +async function resolveRelative(instruction: string): Promise<string[]> { |
| 31 | + if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) { |
| 32 | + return Filesystem.globUp(instruction, Instance.directory, Instance.worktree).catch(() => []) |
| 33 | + } |
| 34 | + if (!Flag.OPENCODE_CONFIG_DIR) { |
| 35 | + log.warn( |
| 36 | + `Skipping relative instruction "${instruction}" - no OPENCODE_CONFIG_DIR set while project config is disabled`, |
| 37 | + ) |
| 38 | + return [] |
| 39 | + } |
| 40 | + return Filesystem.globUp(instruction, Flag.OPENCODE_CONFIG_DIR, Flag.OPENCODE_CONFIG_DIR).catch(() => []) |
| 41 | +} |
| 42 | + |
| 43 | +export namespace InstructionPrompt { |
| 44 | + export async function systemPaths() { |
| 45 | + const config = await Config.get() |
| 46 | + const paths = new Set<string>() |
| 47 | + |
| 48 | + if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) { |
| 49 | + for (const file of FILES) { |
| 50 | + const matches = await Filesystem.findUp(file, Instance.directory, Instance.worktree) |
| 51 | + if (matches.length > 0) { |
| 52 | + matches.forEach((p) => paths.add(path.resolve(p))) |
| 53 | + break |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + for (const file of globalFiles()) { |
| 59 | + if (await Bun.file(file).exists()) { |
| 60 | + paths.add(path.resolve(file)) |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (config.instructions) { |
| 66 | + for (let instruction of config.instructions) { |
| 67 | + if (instruction.startsWith("https://") || instruction.startsWith("http://")) continue |
| 68 | + if (instruction.startsWith("~/")) { |
| 69 | + instruction = path.join(os.homedir(), instruction.slice(2)) |
| 70 | + } |
| 71 | + const matches = path.isAbsolute(instruction) |
| 72 | + ? await Array.fromAsync( |
| 73 | + new Bun.Glob(path.basename(instruction)).scan({ |
| 74 | + cwd: path.dirname(instruction), |
| 75 | + absolute: true, |
| 76 | + onlyFiles: true, |
| 77 | + }), |
| 78 | + ).catch(() => []) |
| 79 | + : await resolveRelative(instruction) |
| 80 | + matches.forEach((p) => paths.add(path.resolve(p))) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return paths |
| 85 | + } |
| 86 | + |
| 87 | + export async function system() { |
| 88 | + const config = await Config.get() |
| 89 | + const paths = await systemPaths() |
| 90 | + |
| 91 | + const files = Array.from(paths).map(async (p) => { |
| 92 | + const content = await Bun.file(p) |
| 93 | + .text() |
| 94 | + .catch(() => "") |
| 95 | + return content ? "Instructions from: " + p + "\n" + content : "" |
| 96 | + }) |
| 97 | + |
| 98 | + const urls: string[] = [] |
| 99 | + if (config.instructions) { |
| 100 | + for (const instruction of config.instructions) { |
| 101 | + if (instruction.startsWith("https://") || instruction.startsWith("http://")) { |
| 102 | + urls.push(instruction) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + const fetches = urls.map((url) => |
| 107 | + fetch(url, { signal: AbortSignal.timeout(5000) }) |
| 108 | + .then((res) => (res.ok ? res.text() : "")) |
| 109 | + .catch(() => "") |
| 110 | + .then((x) => (x ? "Instructions from: " + url + "\n" + x : "")), |
| 111 | + ) |
| 112 | + |
| 113 | + return Promise.all([...files, ...fetches]).then((result) => result.filter(Boolean)) |
| 114 | + } |
| 115 | + |
| 116 | + export function loaded(messages: MessageV2.WithParts[]) { |
| 117 | + const paths = new Set<string>() |
| 118 | + for (const msg of messages) { |
| 119 | + for (const part of msg.parts) { |
| 120 | + if (part.type === "tool" && part.tool === "read" && part.state.status === "completed") { |
| 121 | + if (part.state.time.compacted) continue |
| 122 | + const loaded = part.state.metadata?.loaded |
| 123 | + if (!loaded || !Array.isArray(loaded)) continue |
| 124 | + for (const p of loaded) { |
| 125 | + if (typeof p === "string") paths.add(p) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return paths |
| 131 | + } |
| 132 | + |
| 133 | + export async function find(dir: string) { |
| 134 | + for (const file of FILES) { |
| 135 | + const filepath = path.resolve(path.join(dir, file)) |
| 136 | + if (await Bun.file(filepath).exists()) return filepath |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + export async function resolve(messages: MessageV2.WithParts[], filepath: string) { |
| 141 | + const system = await systemPaths() |
| 142 | + const already = loaded(messages) |
| 143 | + const results: { filepath: string; content: string }[] = [] |
| 144 | + |
| 145 | + let current = path.dirname(path.resolve(filepath)) |
| 146 | + const root = path.resolve(Instance.directory) |
| 147 | + |
| 148 | + while (current.startsWith(root)) { |
| 149 | + const found = await find(current) |
| 150 | + if (found && !system.has(found) && !already.has(found)) { |
| 151 | + const content = await Bun.file(found) |
| 152 | + .text() |
| 153 | + .catch(() => undefined) |
| 154 | + if (content) { |
| 155 | + results.push({ filepath: found, content: "Instructions from: " + found + "\n" + content }) |
| 156 | + } |
| 157 | + } |
| 158 | + if (current === root) break |
| 159 | + current = path.dirname(current) |
| 160 | + } |
| 161 | + |
| 162 | + return results |
| 163 | + } |
| 164 | +} |
0 commit comments