# Story 2.1: Infrastructure IA & Abstraction Provider

Status: done

## Story

As an administrator,
I want to configure my AI provider (OpenAI or Ollama) centrally,
so that the application can use artificial intelligence securely.

## Acceptance Criteria

1. **Given** an `AIProvider` interface and the `Vercel AI SDK` installed.
2. **When** I provide my API key or Ollama instance URL in environment variables.
3. **Then** the system initializes the appropriate driver.
4. **And** no API keys are exposed to the client-side.

## Tasks / Subtasks

- [x] Installation du Vercel AI SDK (AC: 1)
  - [x] `npm install ai @ai-sdk/openai ollama-ai-provider`
- [x] Création de l'interface d'abstraction `AIProvider` (AC: 1, 3)
  - [x] Définir les méthodes standard (ex: `generateTags(content: string)`, `getEmbeddings(text: string)`)
- [x] Implémentation des drivers (AC: 3)
  - [x] `OpenAIProvider` utilisant le SDK officiel
  - [x] `OllamaProvider` pour le support local
- [x] Configuration via variables d'environnement (AC: 2, 4)
  - [x] Gérer `AI_PROVIDER`, `OPENAI_API_KEY`, `OLLAMA_BASE_URL` dans `.env`
  - [x] Créer une factory pour initialiser le bon provider au démarrage du serveur
- [x] Test de connexion (AC: 3)
  - [x] Créer un endpoint de santé/test pour vérifier la communication avec le provider configuré

## Senior Developer Review (AI)
- **Review Date:** 2026-01-08
- **Status:** Approved with auto-fixes
- **Fixes Applied:**
  - Switched to `generateObject` with Zod for robust parsing.
  - Added strict error handling and timeouts.
  - Improved prompts and system messages.

## Dev Agent Record

### Agent Model Used
BMad Master (Gemini 2.0 Flash)

### Debug Log References
- Infrastructure created in keep-notes/lib/ai
- Packages: ai, @ai-sdk/openai, ollama-ai-provider
- Test endpoint: /api/ai/test

### Completion Notes List
- [x] Abstraction interface defined
- [x] Factory pattern implemented
- [x] OpenAI and Ollama drivers ready
- [x] API test route created

### File List
- keep-notes/lib/ai/types.ts
- keep-notes/lib/ai/factory.ts
- keep-notes/lib/ai/providers/openai.ts
- keep-notes/lib/ai/providers/ollama.ts
- keep-notes/app/api/ai/test/route.ts

Status: review

