|
1 | 1 | import { readFile } from "node:fs/promises" |
2 | 2 | import path from "node:path" |
| 3 | +import { Effect } from "effect" |
3 | 4 | import { parse } from "jsonc-parser" |
| 5 | +import { Flag, env } from "@opencode-ai/core/flag/flag" |
| 6 | +import { FSUtil } from "@opencode-ai/core/fs-util" |
4 | 7 | import { Global } from "@/global" |
| 8 | +import { ConfigPaths } from "@/config/paths" |
| 9 | +import { git } from "@/util/git" |
5 | 10 | import { MAX_LINES, MAX_BYTES } from "@/tool/truncate-core" |
6 | 11 |
|
7 | | -// Read only output settings: loading the full agent config can install dependencies |
8 | | -// or fetch remote configuration, neither of which belongs in this standalone server. |
| 12 | +/** The project's git worktree root, as the agent's Instance resolves it (`Project.fromDirectory`). */ |
| 13 | +async function projectWorktree(directory: string): Promise<string> { |
| 14 | + const dotgit = await Effect.runPromise( |
| 15 | + Effect.gen(function* () { |
| 16 | + return yield* (yield* FSUtil.Service).up({ targets: [".git"], start: directory }) |
| 17 | + }).pipe( |
| 18 | + Effect.provide(FSUtil.defaultLayer), |
| 19 | + Effect.orElseSucceed(() => [] as string[]), |
| 20 | + ), |
| 21 | + ) |
| 22 | + if (!dotgit[0]) return "/" |
| 23 | + const sandbox = path.dirname(dotgit[0]) |
| 24 | + const result = await git(["rev-parse", "--path-format=absolute", "--git-common-dir"], { cwd: sandbox }) |
| 25 | + const common = result.exitCode === 0 ? result.text().replace(/[\r\n]+$/, "") : "" |
| 26 | + if (!common) return sandbox |
| 27 | + const resolved = path.resolve(sandbox, common) |
| 28 | + return resolved === sandbox ? sandbox : path.dirname(resolved) |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Read only output settings: loading the full agent config can install dependencies or fetch remote |
| 33 | + * configuration, neither of which belongs in this standalone server. The files, their order and the |
| 34 | + * `ALTIMATE_CLI_*` / `OPENCODE_*` variables are the agent loader's (`config/config.ts`), through the |
| 35 | + * same `ConfigPaths` and `Flag` helpers, so one config resolves to the same limits in both. |
| 36 | + */ |
9 | 37 | export async function outputLimits() { |
| 38 | + const directory = process.cwd() |
10 | 39 | const limits = { maxLines: MAX_LINES, maxBytes: MAX_BYTES } |
11 | 40 | const apply = (text: string) => { |
12 | 41 | const output = parse(text)?.tool_output |
13 | 42 | if (Number.isInteger(output?.max_lines) && output.max_lines > 0) limits.maxLines = output.max_lines |
14 | 43 | if (Number.isInteger(output?.max_bytes) && output.max_bytes > 0) limits.maxBytes = output.max_bytes |
15 | 44 | } |
| 45 | + // A missing or unreadable file contributes nothing; output limits never stop the server from starting. |
16 | 46 | const load = async (file: string) => { |
17 | | - const text = await readFile(file, "utf8").catch((error) => { |
18 | | - if (error.code === "ENOENT") return undefined |
19 | | - throw error |
20 | | - }) |
| 47 | + const text = await readFile(file, "utf8").catch(() => undefined) |
21 | 48 | if (text) apply(text) |
22 | 49 | } |
| 50 | + const worktree = await projectWorktree(directory) |
| 51 | + const discover = <A>(effect: Effect.Effect<A, unknown, FSUtil.Service>, fallback: A) => |
| 52 | + Effect.runPromise(effect.pipe(Effect.provide(FSUtil.defaultLayer), Effect.orElseSucceed(() => fallback))) |
| 53 | + |
23 | 54 | for (const name of ["config.json", "opencode.json", "opencode.jsonc", "altimate-code.json", "altimate-code.jsonc"]) { |
24 | 55 | await load(path.join(Global.Path.config, name)) |
25 | 56 | } |
26 | | - if (process.env.OPENCODE_CONFIG) await load(process.env.OPENCODE_CONFIG) |
27 | | - if (process.env.OPENCODE_DISABLE_PROJECT_CONFIG !== "1" && process.env.OPENCODE_DISABLE_PROJECT_CONFIG !== "true") { |
28 | | - const ancestors = [] |
29 | | - for (let directory = process.cwd(); ; directory = path.dirname(directory)) { |
30 | | - ancestors.unshift(directory) |
31 | | - if (directory === path.dirname(directory)) break |
32 | | - } |
33 | | - for (const directory of ancestors) { |
34 | | - for (const base of [directory, path.join(directory, ".opencode"), path.join(directory, ".altimate-code")]) { |
35 | | - for (const name of ["opencode.jsonc", "opencode.json", "altimate-code.jsonc", "altimate-code.json"]) { |
36 | | - await load(path.join(base, name)) |
37 | | - } |
38 | | - } |
| 57 | + const custom = env("OPENCODE_CONFIG") |
| 58 | + if (custom) await load(custom) |
| 59 | + if (!Flag.OPENCODE_DISABLE_PROJECT_CONFIG) { |
| 60 | + for (const file of await discover(ConfigPaths.files("opencode", directory, worktree), [] as string[])) { |
| 61 | + await load(file) |
39 | 62 | } |
40 | 63 | } |
41 | | - if (process.env.OPENCODE_CONFIG_DIR) { |
42 | | - for (const name of ["opencode.jsonc", "opencode.json", "altimate-code.jsonc", "altimate-code.json"]) { |
43 | | - await load(path.join(process.env.OPENCODE_CONFIG_DIR, name)) |
| 64 | + const configDir = Flag.OPENCODE_CONFIG_DIR |
| 65 | + for (const dir of await discover(ConfigPaths.directories(directory, worktree), [] as string[])) { |
| 66 | + if (!dir.endsWith(".altimate-code") && !dir.endsWith(".opencode") && dir !== configDir) continue |
| 67 | + for (const name of ["altimate-code.json", "altimate-code.jsonc", "opencode.json", "opencode.jsonc"]) { |
| 68 | + await load(path.join(dir, name)) |
44 | 69 | } |
45 | 70 | } |
46 | | - if (process.env.OPENCODE_CONFIG_CONTENT) apply(process.env.OPENCODE_CONFIG_CONTENT) |
| 71 | + const content = env("OPENCODE_CONFIG_CONTENT") |
| 72 | + if (content) apply(content) |
47 | 73 | return limits |
48 | 74 | } |
0 commit comments