forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdater.ts
More file actions
177 lines (153 loc) · 4.9 KB
/
Copy pathupdater.ts
File metadata and controls
177 lines (153 loc) · 4.9 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
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* OpenCode configuration file updater.
*
* Updates ~/.config/opencode/opencode.json(c) with plugin models.
*/
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
import { join, dirname } from "node:path";
import { homedir } from "node:os";
import { OPENCODE_MODEL_DEFINITIONS } from "./models";
// =============================================================================
// Types
// =============================================================================
export interface UpdateConfigResult {
success: boolean;
configPath: string;
error?: string;
}
export interface OpencodeConfig {
$schema?: string;
plugin?: string[];
provider?: {
google?: {
models?: Record<string, unknown>;
[key: string]: unknown;
};
[key: string]: unknown;
};
[key: string]: unknown;
}
export interface UpdateConfigOptions {
/** Override the config file path (for testing) */
configPath?: string;
}
// =============================================================================
// Constants
// =============================================================================
const PLUGIN_NAME = "opencode-antigravity-auth@latest";
const SCHEMA_URL = "https://opencode.ai/config.json";
const OPENCODE_JSON_FILENAME = "opencode.json";
const OPENCODE_JSONC_FILENAME = "opencode.jsonc";
function stripJsonCommentsAndTrailingCommas(json: string): string {
return json
.replace(
/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
(match: string, group: string | undefined) => (group ? "" : match)
)
.replace(/,(\s*[}\]])/g, "$1");
}
/**
* Get the opencode config directory path.
*/
export function getOpencodeConfigDir(): string {
const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
return join(xdgConfig, "opencode");
}
/**
* Get the opencode config file path.
*
* Prefers opencode.jsonc when present so we update the active config file
* instead of creating a new opencode.json.
*/
export function getOpencodeConfigPath(): string {
const configDir = getOpencodeConfigDir();
const jsoncPath = join(configDir, OPENCODE_JSONC_FILENAME);
const jsonPath = join(configDir, OPENCODE_JSON_FILENAME);
if (existsSync(jsoncPath)) {
return jsoncPath;
}
if (existsSync(jsonPath)) {
return jsonPath;
}
return jsonPath;
}
// =============================================================================
// Main Function
// =============================================================================
/**
* Updates the opencode configuration file with plugin models.
*
* This function:
* 1. Reads existing opencode.json/opencode.jsonc (or creates default structure)
* 2. Replaces `provider.google.models` with plugin models
* 3. Writes back to disk with proper formatting
*
* Preserves:
* - $schema and other top-level config keys
* - Non-google provider sections
* - Other settings within google provider (except models)
*
* @param options - Optional configuration (e.g., custom configPath for testing)
* @returns UpdateConfigResult with success status and path
*/
export async function updateOpencodeConfig(
options: UpdateConfigOptions = {}
): Promise<UpdateConfigResult> {
const configPath = options.configPath ?? getOpencodeConfigPath();
try {
let config: OpencodeConfig;
// Read existing config or create default
if (existsSync(configPath)) {
const content = readFileSync(configPath, "utf-8");
config = JSON.parse(stripJsonCommentsAndTrailingCommas(content)) as OpencodeConfig;
} else {
// Create default config structure
config = {
$schema: SCHEMA_URL,
plugin: [],
provider: {},
};
}
// Ensure $schema is set
if (!config.$schema) {
config.$schema = SCHEMA_URL;
}
// Ensure plugin array exists and contains our plugin
if (!Array.isArray(config.plugin)) {
config.plugin = [];
}
// Check if plugin is already in the list (any version)
const hasPlugin = config.plugin.some((p) =>
p.includes("opencode-antigravity-auth")
);
if (!hasPlugin) {
config.plugin.push(PLUGIN_NAME);
}
// Ensure provider.google structure exists
if (!config.provider) {
config.provider = {};
}
if (!config.provider.google) {
config.provider.google = {};
}
// Replace google models with plugin models
config.provider.google.models = { ...OPENCODE_MODEL_DEFINITIONS };
// Ensure config directory exists
const configDir = dirname(configPath);
if (!existsSync(configDir)) {
mkdirSync(configDir, { recursive: true });
}
// Write config with proper formatting (2-space indent)
writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
return {
success: true,
configPath,
};
} catch (error) {
return {
success: false,
configPath,
error: error instanceof Error ? error.message : String(error),
};
}
}