Skip to content

refactor(ai): import AIProvider instead of redeclaring it - #80

Merged
Vrun-design merged 1 commit into
Vrun-design:mainfrom
Mr-Macharia:refactor/ai-provider-type
Sep 20, 2026
Merged

Vrun-design merged 1 commit into
Vrun-design:mainfrom
Mr-Macharia:refactor/ai-provider-type

Conversation

@Mr-Macharia

Copy link
Copy Markdown
Contributor

Summary

AIProvider is declared twice, independently:

  • src/store/types.ts:41 — the canonical union, imported by the store, settings UI, hooks and zod schemas
  • src/services/aiService.ts:17 — a standalone re-declaration of the same ten string literals

Neither imports the other, so they're unrelated types that happen to match today. Add a provider to one and not the other and TypeScript stays silent — both remain valid string-literal unions. The failure surfaces at runtime instead: getEnvApiKey() falls through to undefined, or resolveOpenAICompatibleBaseUrl() throws Unknown provider.

Latent right now (both list the same ten providers), but it gets more likely with every provider added.

Changes

One file, two lines — src/services/aiService.ts imports the canonical type and re-exports it:

import type { AIProvider } from '@/store/types';

export type { AIProvider };

The re-export keeps the module's public surface unchanged. Nothing currently imports AIProvider from aiService (consumers all use @/store or ./types; only ChatMessage comes from aiService), so a plain import would also compile — happy to drop the re-export if you'd prefer the smaller surface.

Test plan

  • npx tsc --noEmit — clean
  • npm run lint — clean
  • npm test — 295 files, 1445 tests passing

Drift regression check — temporarily added a fake 'testprovider' to the canonical union:

  • Before: aiService.ts reported nothing; a provider missing from its switch compiled fine
  • After: tsc reports 7 errors across aiProviders.ts (all five Record<AIProvider, …> maps), aiSettings.ts and aiSettingsSchemas.ts

Temporary edit reverted; no behavior change, pure type-level fix.

Notes for reviewers

The two runtime provider lists were already protected and need no change — AI_PROVIDERS in aiSettings.ts uses satisfies readonly AIProvider[], and aiProviderSchema in aiSettingsSchemas.ts uses satisfies [AIProvider, ...AIProvider[]]. aiService.ts was the only unguarded copy.

One gap this doesn't close: getEnvApiKey's switch has default: return undefined, so it isn't exhaustiveness-checked and won't itself error on an unknown provider. The fix works because the shared type propagates into the guarded maps. Tightening that switch would be a separate change — happy to include it if you'd like.

`AIProvider` was declared twice: the canonical union in
`src/store/types.ts` and an independent copy in `aiService.ts`. Neither
imported the other, so adding a provider to one and not the other
compiled cleanly and failed at runtime instead — `getEnvApiKey` would
return undefined, or `resolveOpenAICompatibleBaseUrl` would throw
`Unknown provider`.

Import the canonical type and re-export it to keep the module surface
unchanged. Verified by temporarily adding a fake provider to the union:
tsc now reports errors across aiProviders.ts, aiSettings.ts and
aiSettingsSchemas.ts, where previously aiService.ts stayed silent.
Copilot AI lite review requested due to automatic review settings September 11, 2026 14:01

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Vrun-design
Vrun-design merged commit 9367906 into Vrun-design:main Sep 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants