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 });
+9 -9
View File
@@ -51,15 +51,6 @@ public static class ChatEndpoint
var userId = req.HttpContext.GetUserId();
// API-Key des Users auflösen
var userApiKey = await db.GetUserSelectedApiKey(userId);
if (userApiKey == null)
{
res.StatusCode = 400;
await res.WriteAsJsonAsync(new { error = "Kein API-Key ausgewählt. Bitte wähle einen Key aus." });
return;
}
var maxIterations = settings.GetInt("MaxIterations", 100);
var hasRemote = remoteMgr.GetBySessionId(sessionId) != null;
@@ -71,6 +62,15 @@ public static class ChatEndpoint
return;
}
// API-Key des Users für diesen Agent auflösen
var userApiKey = await db.GetAgentApiKeyRecord(userId, ctx.AgentId);
if (userApiKey == null)
{
res.StatusCode = 400;
await res.WriteAsJsonAsync(new { error = "Kein API-Key für diesen Agent zugewiesen. Bitte weise einen Key zu." });
return;
}
if (isChoiceResponse)
{
// Choice-Antwort: tool_result in die VORHERIGE Chain speichern (wo der tool_use liegt)