BYO AI settings: custom instructions, model picker, key show/hide
Mirrors the BYOK dialog suggested in issue #2: - UserAiConfig gains an optional instructions column (schema.sql + migration 016); PUT validates (<=4000 chars) and full-replaces it. - Instructions ride along on the assistant chat system prompt only (chatSystemWith); the automation compiler and page-extraction prompts stay server-owned contracts. - Settings -> AI: labeled fields, per-provider suggested-model dropdown with a custom-model escape hatch, API key show/hide toggle, and the instructions textarea. - Tests: projection echo, omission clears, cap validation, and the system-prompt assertion via a recorded fake provider body.
This commit is contained in:
@@ -80,14 +80,22 @@ func TestAiConfig(t *testing.T) {
|
||||
// Compile inherits the same precondition.
|
||||
alice.request("POST", "/api/agent/compile", map[string]any{"text": "x"}, 428)
|
||||
|
||||
// --- PUT create: openai-compatible with a BYO key ---
|
||||
// --- PUT create: openai-compatible with a BYO key + instructions ---
|
||||
created := alice.request("PUT", "/api/ai/config", map[string]any{
|
||||
"provider": "openai-compatible", "baseUrl": base + "/v1", "model": "test-model",
|
||||
"apiKey": "sk-byo-secret-123",
|
||||
"apiKey": "sk-byo-secret-123", "instructions": "Answer like a pirate.",
|
||||
}, 201)
|
||||
if created["apiKey"] != "••••••••" {
|
||||
t.Fatalf("key leaked in response: %v", created["apiKey"])
|
||||
}
|
||||
if created["instructions"] != "Answer like a pirate." {
|
||||
t.Fatalf("instructions not echoed: %v", created["instructions"])
|
||||
}
|
||||
// GET returns the same projection (instructions are not a secret).
|
||||
gotCfg := alice.request("GET", "/api/ai/config", nil, 200)
|
||||
if gotCfg["instructions"] != "Answer like a pirate." || gotCfg["apiKey"] != "••••••••" {
|
||||
t.Fatalf("GET projection wrong: %v", gotCfg)
|
||||
}
|
||||
// Encrypted at rest, decryptable with the server secret.
|
||||
var storedKey string
|
||||
if e := d.QueryRow(`SELECT "apiKey" FROM "UserAiConfig" WHERE "ownerId"=(SELECT id FROM "User" WHERE email='alice@superbrain.test')`).Scan(&storedKey); e != nil {
|
||||
@@ -116,6 +124,10 @@ func TestAiConfig(t *testing.T) {
|
||||
"provider": "anthropic", "baseUrl": base, "model": "claude-x",
|
||||
"apiKey": "••••••••", "version": int(created["version"].(float64)),
|
||||
}, 200)
|
||||
// Full-replacement semantics: omitting instructions clears them.
|
||||
if updated["instructions"] != "" {
|
||||
t.Fatalf("instructions must clear when omitted: %v", updated["instructions"])
|
||||
}
|
||||
alice.request("POST", "/api/ai/test", nil, 200)
|
||||
if got := fake.lastAuth["anthropic"]; got != "sk-byo-secret-123|2023-06-01" {
|
||||
t.Fatalf("anthropic adapter headers = %q (mask must preserve the stored key)", got)
|
||||
@@ -136,6 +148,7 @@ func TestAiConfig(t *testing.T) {
|
||||
alice.request("PUT", "/api/ai/config", map[string]any{"provider": "skynet", "model": "x", "apiKey": "k", "version": 3}, 400)
|
||||
alice.request("PUT", "/api/ai/config", map[string]any{"provider": "anthropic", "model": "", "apiKey": "k", "version": 3}, 400)
|
||||
alice.request("PUT", "/api/ai/config", map[string]any{"provider": "anthropic", "model": "m", "baseUrl": "ftp://x", "apiKey": "k", "version": 3}, 400)
|
||||
alice.request("PUT", "/api/ai/config", map[string]any{"provider": "anthropic", "model": "m", "apiKey": "k", "instructions": strings.Repeat("x", 4001), "version": 3}, 400)
|
||||
|
||||
// --- Plan gate on the SERVER key ---
|
||||
// Selfhosted (the harness default) may spend the operator's key.
|
||||
|
||||
Reference in New Issue
Block a user