|
6 | 6 | * print the model and a curl line. It never calls a paid model; `--test` sends one minimal request |
7 | 7 | * through the gateway, and only for loopback endpoints. |
8 | 8 | */ |
9 | | -import { lstat, readFile, writeFile } from 'node:fs/promises'; |
| 9 | +import { lstat, readFile } from 'node:fs/promises'; |
| 10 | +import { replaceProfileConfig } from '../config/profile-update.js'; |
10 | 11 | import path from 'node:path'; |
11 | 12 | import { loadConfig, providerSchema, type GatewayConfig } from '../config/config.js'; |
12 | 13 | import { bi, pick, processLocale, type Locale } from '../core/locale.js'; |
@@ -280,15 +281,14 @@ export async function connectModel(o: ModelConnectOptions): Promise<void> { |
280 | 281 | const { preset, name, agentId, baseUrl } = resolveTarget(o, t); |
281 | 282 | const keyFile = o.keyFile ? await checkKeyFile(o.keyFile) : undefined; |
282 | 283 | const file = path.join(path.resolve(o.dir), 'config.json'); |
283 | | - const raw = JSON.parse( |
284 | | - await readFile(file, 'utf8').catch(() => { |
285 | | - throw new Fault( |
286 | | - 'not_running', |
287 | | - bi(`没有 ${file}。先运行 oaa quickstart。`, `No ${file}. Run oaa quickstart first.`), |
288 | | - 404, |
289 | | - ); |
290 | | - }), |
291 | | - ) as RawConfig; |
| 284 | + const original = await readFile(file, 'utf8').catch(() => { |
| 285 | + throw new Fault( |
| 286 | + 'not_running', |
| 287 | + bi(`没有 ${file}。先运行 oaa quickstart。`, `No ${file}. Run oaa quickstart first.`), |
| 288 | + 404, |
| 289 | + ); |
| 290 | + }); |
| 291 | + const raw = JSON.parse(original) as RawConfig; |
292 | 292 |
|
293 | 293 | // A conflicting provider of the same name is refused before anything is probed or written. |
294 | 294 | const entry = presetProviderEntry(preset, { baseUrl, keyEnv: o.keyEnv, keyFile }); |
@@ -327,7 +327,9 @@ export async function connectModel(o: ModelConnectOptions): Promise<void> { |
327 | 327 | ); |
328 | 328 | else { |
329 | 329 | providers[name] = entry; |
330 | | - await writeFile(file, JSON.stringify(raw, null, 2) + '\n', { mode: 0o600 }); |
| 330 | + await replaceProfileConfig(file, original, JSON.stringify(raw, null, 2) + '\n', { |
| 331 | + allowSymlink: true, |
| 332 | + }); |
331 | 333 | changed = true; |
332 | 334 | out( |
333 | 335 | t( |
|
0 commit comments