56 lines
1.9 KiB
Markdown
56 lines
1.9 KiB
Markdown
# Plan: API Key pro Agent
|
|
|
|
## Übersicht
|
|
Statt einem globalen API-Key pro User wird der Key **pro Agent** zugewiesen.
|
|
Kein Key = kein Chat. Saubere Trennung.
|
|
|
|
## Datenbank
|
|
|
|
### Neue Tabelle `agent_api_keys`
|
|
```sql
|
|
CREATE TABLE agent_api_keys (
|
|
user_id TEXT NOT NULL,
|
|
agent_id TEXT NOT NULL,
|
|
key_id TEXT NOT NULL,
|
|
UNIQUE(user_id, agent_id)
|
|
);
|
|
```
|
|
|
|
### Neue DB-Methoden
|
|
- `SetAgentApiKey(string userId, string agentId, string keyId)`
|
|
- `GetAgentApiKey(string userId, string agentId)` → key_id oder null
|
|
- `GetAgentApiKeyRecord(string userId, string agentId)` → vollständiges ApiKey-Objekt
|
|
|
|
### Migration
|
|
- Bestehende `users.selected_api_key_id` Werte → in `agent_api_keys` für alle Agents des Users übernehmen (einmalig)
|
|
- `users.selected_api_key_id` bleibt vorerst in der Tabelle (kein DROP), wird aber nicht mehr aktiv genutzt
|
|
|
|
## Backend (Endpoints)
|
|
|
|
### Angepasste Endpoints
|
|
- **`PUT /api/api-keys/select/{keyId}`** → neuer Query-Parameter `?agentId=xxx`
|
|
- **`GET /api/api-keys/selected`** → neuer Query-Parameter `?agentId=xxx`
|
|
|
|
### Chat/Services
|
|
- **`ChatEndpoint`** → `GetAgentApiKeyRecord(userId, agentId)` statt `GetUserSelectedApiKey(userId)`
|
|
- **`TelegramService`** → `GetAgentApiKeyRecord(userId, agentId)` statt `GetUserSelectedApiKey(userId)`
|
|
- **`CronjobService`** → `GetAgentApiKeyRecord(userId, agentId)` statt `GetUserSelectedApiKey(userId)`
|
|
|
|
### Fehler-Handling
|
|
- Kein Key für Agent zugewiesen → Fehlermeldung: "Kein API Key für diesen Agent zugewiesen"
|
|
- Kein Fallback auf globalen Key
|
|
|
|
## Frontend
|
|
|
|
### Key-Switch-Modal
|
|
- Bekommt `agentId` als Parameter (aus aktuellem Chat/Session)
|
|
- Select-Call sendet `agentId` mit
|
|
- Zeigt aktuell zugewiesenen Key für diesen Agent
|
|
|
|
### Usage-Badge
|
|
- Zeigt den Key des aktuellen Agents (nicht den globalen)
|
|
|
|
### API-Calls
|
|
- `selectApiKey(keyId, agentId)` → `PUT /api/api-keys/select/{keyId}?agentId={agentId}`
|
|
- `getSelectedApiKey(agentId)` → `GET /api/api-keys/selected?agentId={agentId}`
|