|
| 1 | +import path from "path" |
| 2 | +import fs from "fs/promises" |
| 3 | +import { existsSync } from "fs" |
| 4 | +import * as Effect from "effect/Effect" |
| 5 | +import { Flag, env as flagEnv } from "@opencode-ai/core/flag/flag" |
| 6 | +import { FSUtil } from "@opencode-ai/core/fs-util" |
| 7 | +import { Global } from "../../global" |
| 8 | +import { ConfigPaths } from "@/config/paths" |
| 9 | +import { ConfigManaged } from "@/config/managed" |
| 10 | + |
| 11 | +/** |
| 12 | + * Whether the user config turns telemetry off, read without an Instance. |
| 13 | + * |
| 14 | + * `Config.get()` needs an Instance, and the CLI's main thread initialises telemetry in middleware |
| 15 | + * before one exists, so there it throws. Treating that as "enabled" let an opted-out user's main |
| 16 | + * thread clear the opt-out marker and resolve their identity. This reads the same files `Config` |
| 17 | + * merges, in the same order (a later source overrides an earlier one), straight from disk: |
| 18 | + * the global config files, `OPENCODE_CONFIG`, the project files and `.altimate-code`/`.opencode` |
| 19 | + * directories found from `directory`, the inline `OPENCODE_CONFIG_CONTENT`, the managed config |
| 20 | + * directory and macOS managed preferences. Remote and account configs are not read (they need |
| 21 | + * the network). |
| 22 | + * |
| 23 | + * - `disabled`: the last source that sets `telemetry.disabled` sets it truthy. |
| 24 | + * - `enabled`: no source turns it off. |
| 25 | + * - `unreadable`: a source that could not be read or parsed comes after the last one that set |
| 26 | + * the value, so the answer is unknown. Callers fail closed for anything an opt-out forbids. |
| 27 | + */ |
| 28 | +export type State = "disabled" | "enabled" | "unreadable" |
| 29 | + |
| 30 | +const DIR_FILES = ["altimate-code.json", "altimate-code.jsonc", "opencode.json", "opencode.jsonc"] |
| 31 | +const GLOBAL_FILES = ["config.json", "opencode.json", "opencode.jsonc", "altimate-code.json", "altimate-code.jsonc"] |
| 32 | + |
| 33 | +/** The git root above `directory`, which `Config` uses as the stop for its upward search. */ |
| 34 | +function worktreeOf(directory: string): string | undefined { |
| 35 | + let dir = path.resolve(directory) |
| 36 | + while (true) { |
| 37 | + if (existsSync(path.join(dir, ".git"))) return dir |
| 38 | + const parent = path.dirname(dir) |
| 39 | + if (parent === dir) return undefined |
| 40 | + dir = parent |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export async function disabledState(directory: string = process.cwd()): Promise<State> { |
| 45 | + try { |
| 46 | + let value: boolean | undefined |
| 47 | + let unknown = false |
| 48 | + const apply = (data: unknown) => { |
| 49 | + const disabled = (data as { telemetry?: { disabled?: unknown } } | undefined)?.telemetry?.disabled |
| 50 | + if (disabled === undefined) return |
| 51 | + value = Boolean(disabled) |
| 52 | + unknown = false |
| 53 | + } |
| 54 | + const readFile = async (file: string) => { |
| 55 | + let text: string |
| 56 | + try { |
| 57 | + text = await fs.readFile(file, "utf-8") |
| 58 | + } catch (err) { |
| 59 | + if ((err as NodeJS.ErrnoException)?.code !== "ENOENT") unknown = true |
| 60 | + return |
| 61 | + } |
| 62 | + try { |
| 63 | + apply(await ConfigPaths.parseText(text, file, "empty")) |
| 64 | + } catch { |
| 65 | + unknown = true |
| 66 | + } |
| 67 | + } |
| 68 | + const readText = async (text: string, source: string, dir: string) => { |
| 69 | + try { |
| 70 | + apply(await ConfigPaths.parseText(text, { source, dir }, "empty")) |
| 71 | + } catch { |
| 72 | + unknown = true |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (const file of GLOBAL_FILES) await readFile(path.join(Global.Path.config, file)) |
| 77 | + if (Flag.OPENCODE_CONFIG) await readFile(Flag.OPENCODE_CONFIG) |
| 78 | + |
| 79 | + const worktree = worktreeOf(directory) |
| 80 | + const located = await Effect.runPromise( |
| 81 | + Effect.all([ |
| 82 | + Flag.OPENCODE_DISABLE_PROJECT_CONFIG |
| 83 | + ? Effect.succeed([] as string[]) |
| 84 | + : ConfigPaths.files("opencode", directory, worktree), |
| 85 | + ConfigPaths.directories(directory, worktree), |
| 86 | + ]).pipe(Effect.provide(FSUtil.defaultLayer)), |
| 87 | + ) |
| 88 | + const [projectFiles, directories] = located |
| 89 | + for (const file of projectFiles) await readFile(file) |
| 90 | + for (const dir of directories) { |
| 91 | + if (dir.endsWith(".altimate-code") || dir.endsWith(".opencode") || dir === Flag.OPENCODE_CONFIG_DIR) { |
| 92 | + for (const file of DIR_FILES) await readFile(path.join(dir, file)) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const inline = flagEnv("OPENCODE_CONFIG_CONTENT") |
| 97 | + if (inline) await readText(inline, "OPENCODE_CONFIG_CONTENT", directory) |
| 98 | + |
| 99 | + const managedDir = ConfigManaged.managedConfigDir() |
| 100 | + if (existsSync(managedDir)) for (const file of DIR_FILES) await readFile(path.join(managedDir, file)) |
| 101 | + const managed = await ConfigManaged.readManagedPreferences().catch(() => { |
| 102 | + unknown = true |
| 103 | + return undefined |
| 104 | + }) |
| 105 | + if (managed) await readText(managed.text, managed.source, path.dirname(managed.source)) |
| 106 | + |
| 107 | + if (unknown) return "unreadable" |
| 108 | + return value ? "disabled" : "enabled" |
| 109 | + } catch { |
| 110 | + return "unreadable" |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +export * as TelemetryConfig from "./config-state" |
0 commit comments