feat: API Key pro Agent statt pro User

- Neue Tabelle agent_api_keys (user_id, agent_id, key_id)
- Migration: bestehende selected_api_key_id auf alle Agents verteilt
- Backend: GetAgentApiKeyRecord(userId, agentId) in Chat, Telegram, Cronjobs
- Endpoints: select/selected mit ?agentId Query-Parameter
- Frontend: currentAgentId aus Session, alle Key-Calls agent-aware
- DeleteApiKey bereinigt agent_api_keys statt users.selected_api_key_id
This commit is contained in:
Meleki committed 2026-03-14 21:41:27 +01:00
1 parent b04b1a2cf9
commit 1effc6d00f
10 files changed
+128 -43

No files matched your search

+18 -3
View File
@@ -540,8 +540,16 @@ public static class ApiKeysEndpoint
return;
}
await db.SetUserSelectedApiKey(userId, keyId);
await ctx.Response.WriteAsJsonAsync(new { ok = true, keyId, name = key.Name });
var agentId = ctx.Request.Query["agentId"].FirstOrDefault();
if (string.IsNullOrEmpty(agentId))
{
ctx.Response.StatusCode = 400;
await ctx.Response.WriteAsJsonAsync(new { error = "agentId required" });
return;
}
await db.SetAgentApiKey(userId, agentId, keyId);
await ctx.Response.WriteAsJsonAsync(new { ok = true, keyId, agentId, name = key.Name });
}
private static async Task HandleGetSelected(HttpContext ctx, DatabaseService db)
@@ -549,7 +557,14 @@ public static class ApiKeysEndpoint
var userId = ctx.GetUserId();
if (userId == null) { ctx.Response.StatusCode = 401; return; }
var key = await db.GetUserSelectedApiKey(userId);
var agentId = ctx.Request.Query["agentId"].FirstOrDefault();
if (string.IsNullOrEmpty(agentId))
{
await ctx.Response.WriteAsJsonAsync(new { selected = false });
return;
}
var key = await db.GetAgentApiKeyRecord(userId, agentId);
if (key == null)
{
await ctx.Response.WriteAsJsonAsync(new { selected = false });