Clear stale provider env for ChatGPT Official activation

Switching from a saved provider to the built-in ChatGPT provider must remove previously managed Anthropic env without writing any OpenAI runtime env yet. This keeps Task 1 metadata-only while preventing stale provider settings from surviving the switch.

Constraint: Task 1 still must not write ChatGPT OAuth runtime env
Rejected: Return without touching settings.json | stale ANTHROPIC_* env survives from the previous provider
Confidence: high
Scope-risk: narrow
Tested: bun test src/server/__tests__/providers.test.ts --test-name-pattern "ChatGPT Official"
Tested: bun test src/server/__tests__/providers.test.ts
Tested: git diff --check
This commit is contained in:
程序员阿江(Relakkes) 2026-05-18 22:49:26 +08:00
parent 072b913fd2
commit 238ab65cf3
2 changed files with 24 additions and 3 deletions

View File

@ -320,10 +320,30 @@ describe('ProviderService', () => {
await svc.activateProvider('openai-official')
const config = await readProvidersConfig()
const settings = await readSettings()
expect(config.activeId).toBe('openai-official')
await expect(
fs.readFile(path.join(tmpDir, 'cc-haha', 'settings.json'), 'utf-8'),
).rejects.toThrow()
expect(settings.env).toBeUndefined()
})
test('activating ChatGPT Official clears stale managed provider env', async () => {
const svc = new ProviderService()
const provider = await svc.addProvider(sampleInput({
apiFormat: 'openai_responses',
baseUrl: 'https://api.example.com/openai',
models: {
main: 'provider-main',
haiku: 'provider-haiku',
sonnet: 'provider-sonnet',
opus: 'provider-opus',
},
}))
await svc.activateProvider(provider.id)
expect(((await readSettings()).env as Record<string, string>).ANTHROPIC_BASE_URL).toContain('/proxy')
await svc.activateProvider('openai-official')
const settings = await readSettings()
expect(settings.env).toBeUndefined()
})
})

View File

@ -359,6 +359,7 @@ export class ProviderService {
await this.writeIndex(index)
if (isOpenAIOfficialProviderId(id)) {
await this.clearProviderFromSettings()
return
}