forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.ts
More file actions
164 lines (140 loc) · 4.54 KB
/
Copy pathloader.ts
File metadata and controls
164 lines (140 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Configuration loader for opencode-antigravity-auth plugin.
*
* Loads config from files.
* Priority (lowest to highest):
* 1. Schema defaults
* 2. User config file
* 3. Project config file
*/
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import { AntigravityConfigSchema, DEFAULT_CONFIG, type AntigravityConfig } from "./schema";
import { createLogger } from "../logger";
const log = createLogger("config");
// =============================================================================
// Path Utilities
// =============================================================================
/**
* Get the config directory path, with the following precedence:
* 1. OPENCODE_CONFIG_DIR env var (if set)
* 2. ~/.config/opencode (all platforms, including Windows)
*/
function getConfigDir(): string {
// 1. Check for explicit override via env var
if (process.env.OPENCODE_CONFIG_DIR) {
return process.env.OPENCODE_CONFIG_DIR;
}
// 2. Use ~/.config/opencode on all platforms (including Windows)
const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
return join(xdgConfig, "opencode");
}
/**
* Get the user-level config file path.
*/
export function getUserConfigPath(): string {
return join(getConfigDir(), "antigravity.json");
}
/**
* Get the project-level config file path.
*/
export function getProjectConfigPath(directory: string): string {
return join(directory, ".opencode", "antigravity.json");
}
// =============================================================================
// Config Loading
// =============================================================================
/**
* Load and parse a config file, returning null if not found or invalid.
*/
function loadConfigFile(path: string): Partial<AntigravityConfig> | null {
try {
if (!existsSync(path)) {
return null;
}
const content = readFileSync(path, "utf-8");
const rawConfig = JSON.parse(content);
// Validate with Zod (partial - we'll merge with defaults later)
const result = AntigravityConfigSchema.partial().safeParse(rawConfig);
if (!result.success) {
log.warn("Config validation error", {
path,
issues: result.error.issues.map(i => `${i.path.join(".")}: ${i.message}`).join(", "),
});
return null;
}
return result.data;
} catch (error) {
if (error instanceof SyntaxError) {
log.warn("Invalid JSON in config file", { path, error: error.message });
} else {
log.warn("Failed to load config file", { path, error: String(error) });
}
return null;
}
}
/**
* Deep merge two config objects, with override taking precedence.
*/
function mergeConfigs(
base: AntigravityConfig,
override: Partial<AntigravityConfig>
): AntigravityConfig {
return {
...base,
...override,
// Deep merge signature_cache if both exist
signature_cache: override.signature_cache
? {
...base.signature_cache,
...override.signature_cache,
}
: base.signature_cache,
};
}
// =============================================================================
// Main Loader
// =============================================================================
/**
* Load the complete configuration.
*
* @param directory - The project directory (for project-level config)
* @returns Fully resolved configuration
*/
export function loadConfig(directory: string): AntigravityConfig {
// Start with defaults
let config: AntigravityConfig = { ...DEFAULT_CONFIG };
// Load user config file (if exists)
const userConfigPath = getUserConfigPath();
const userConfig = loadConfigFile(userConfigPath);
if (userConfig) {
config = mergeConfigs(config, userConfig);
}
// Load project config file (if exists) - overrides user config
const projectConfigPath = getProjectConfigPath(directory);
const projectConfig = loadConfigFile(projectConfigPath);
if (projectConfig) {
config = mergeConfigs(config, projectConfig);
}
return config;
}
/**
* Check if a config file exists at the given path.
*/
export function configExists(path: string): boolean {
return existsSync(path);
}
/**
* Get the default logs directory.
*/
export function getDefaultLogsDir(): string {
return join(getConfigDir(), "antigravity-logs");
}
let runtimeConfig: AntigravityConfig | null = null;
export function initRuntimeConfig(config: AntigravityConfig): void {
runtimeConfig = config;
}
export function getKeepThinking(): boolean {
return runtimeConfig?.keep_thinking ?? false;
}