diff --git a/packages/opencode/src/app/app.ts b/packages/opencode/src/app/app.ts index 2d0bcb4141b..9d7920870a4 100644 --- a/packages/opencode/src/app/app.ts +++ b/packages/opencode/src/app/app.ts @@ -37,7 +37,7 @@ export namespace App { cwd: input.cwd, version: input.version, }) - const git = await Filesystem.findUp(".git", input.cwd).then((x) => + const git = await Filesystem.findUp(".git", input.cwd).then(([x]) => x ? path.dirname(x) : undefined, ) log.info("git", { git }) diff --git a/packages/opencode/src/config/config.ts b/packages/opencode/src/config/config.ts index 33d565eecf8..9efa837e3aa 100644 --- a/packages/opencode/src/config/config.ts +++ b/packages/opencode/src/config/config.ts @@ -9,7 +9,7 @@ export namespace Config { export const state = App.state("config", async (app) => { let result: Info = {} for (const file of ["opencode.jsonc", "opencode.json"]) { - const resolved = await Filesystem.findUp( + const [resolved] = await Filesystem.findUp( file, app.path.cwd, app.path.root, diff --git a/packages/opencode/src/session/context.ts b/packages/opencode/src/session/context.ts index 2bd1acd5d5d..345eb9ea76b 100644 --- a/packages/opencode/src/session/context.ts +++ b/packages/opencode/src/session/context.ts @@ -1,5 +1,5 @@ import { App } from "../app/app" -import path from "path" +import { Filesystem } from "../util/filesystem" export namespace SessionContext { const FILES = [ @@ -9,20 +9,10 @@ export namespace SessionContext { ] export async function find() { const { cwd, root } = App.info().path - let current = cwd const found = [] - while (true) { - for (const item of FILES) { - const file = Bun.file(path.join(current, item)) - if (await file.exists()) { - found.push(file.text()) - } - } - - if (current === root) break - const parent = path.dirname(current) - if (parent === current) break - current = parent + for (const item of FILES) { + const matches = await Filesystem.findUp(item, cwd, root) + found.push(...matches.map((x) => Bun.file(x).text())) } return Promise.all(found).then((parts) => parts.join("\n\n")) } diff --git a/packages/opencode/src/session/index.ts b/packages/opencode/src/session/index.ts index 46260bc114a..4754e986d37 100644 --- a/packages/opencode/src/session/index.ts +++ b/packages/opencode/src/session/index.ts @@ -683,6 +683,7 @@ export namespace Session { modelID: string providerID: string }) { + const app = App.info() await Session.chat({ sessionID: input.sessionID, providerID: input.providerID, @@ -690,7 +691,7 @@ export namespace Session { parts: [ { type: "text", - text: PROMPT_INITIALIZE, + text: PROMPT_INITIALIZE.replace("${path}", app.path.root), }, ], }) diff --git a/packages/opencode/src/session/prompt/initialize.txt b/packages/opencode/src/session/prompt/initialize.txt index 27886b40428..4e45b4c784f 100644 --- a/packages/opencode/src/session/prompt/initialize.txt +++ b/packages/opencode/src/session/prompt/initialize.txt @@ -3,5 +3,6 @@ Please analyze this codebase and create an AGENTS.md file containing: 2. Code style guidelines including imports, formatting, types, naming conventions, error handling, etc. The file you create will be given to agentic coding agents (such as yourself) that operate in this repository. Make it about 20 lines long. -If there's already an AGENTS.md, improve it. If there are Cursor rules (in .cursor/rules/ or .cursorrules) or Copilot rules (in .github/copilot-instructions.md), make sure to include them. + +If there's already an AGENTS.md, improve it if it's located in ${path} diff --git a/packages/opencode/src/util/filesystem.ts b/packages/opencode/src/util/filesystem.ts index ac85ca35e54..bddc4025bba 100644 --- a/packages/opencode/src/util/filesystem.ts +++ b/packages/opencode/src/util/filesystem.ts @@ -3,16 +3,16 @@ import { dirname, join } from "path" export namespace Filesystem { export async function findUp(target: string, start: string, stop?: string) { - let currentDir = start + let current = start + const result = [] while (true) { - const targetPath = join(currentDir, target) - if (await exists(targetPath)) return targetPath - if (stop === currentDir) return - const parentDir = dirname(currentDir) - if (parentDir === currentDir) { - return - } - currentDir = parentDir + const search = join(current, target) + if (await exists(search)) result.push(search) + if (stop === current) break + const parent = dirname(current) + if (parent === current) break + current = parent } + return result } }