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:
@@ -15,11 +15,12 @@ import (
|
||||
// fakeOpenAI is a loopback OpenAI-compatible chat-completions server with a
|
||||
// programmable reply, standing in for OpenRouter in tests.
|
||||
type fakeOpenAI struct {
|
||||
ln net.Listener
|
||||
srv *http.Server
|
||||
mu sync.Mutex
|
||||
next string // canned assistant message content
|
||||
hits int
|
||||
ln net.Listener
|
||||
srv *http.Server
|
||||
mu sync.Mutex
|
||||
next string // canned assistant message content
|
||||
lastBody string // raw JSON of the most recent request body
|
||||
hits int
|
||||
}
|
||||
|
||||
func newFakeOpenAI(t *testing.T) *fakeOpenAI {
|
||||
@@ -38,6 +39,7 @@ func newFakeOpenAI(t *testing.T) *fakeOpenAI {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
f.mu.Lock()
|
||||
f.hits++
|
||||
f.lastBody = string(body)
|
||||
reply := f.next
|
||||
f.mu.Unlock()
|
||||
if strings.Contains(string(body), "extract one fact") || strings.Contains(string(body), "You extract one fact") {
|
||||
@@ -65,6 +67,14 @@ func (f *fakeOpenAI) setReply(s string) {
|
||||
f.mu.Unlock()
|
||||
}
|
||||
|
||||
// lastRequest returns the raw JSON body of the most recent completion
|
||||
// request (system-prompt assertions read it).
|
||||
func (f *fakeOpenAI) lastRequest() string {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.lastBody
|
||||
}
|
||||
|
||||
// fakeWatchTarget serves a fixed "flights" page for url_watch evaluation.
|
||||
func fakeWatchTarget(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -90,6 +91,26 @@ func TestAiChat(t *testing.T) {
|
||||
t.Setenv("AI_API_KEY", "")
|
||||
alice.request("POST", "/api/ai/chat", msgs("anything"), 428)
|
||||
|
||||
// --- user instructions ride along on the assistant system prompt ---
|
||||
// A BYO config resolves even with the server key unset. Instructions
|
||||
// apply to THIS prompt only — never to the compiler/extraction contracts.
|
||||
alice.request("PUT", "/api/ai/config", map[string]any{
|
||||
"provider": "openai-compatible", "baseUrl": "http://" + host + ":" + port + "/v1",
|
||||
"model": "byo-model", "apiKey": "sk-byo-chat", "instructions": "Answer like a pirate.",
|
||||
}, 201)
|
||||
fake.setReply(`{"reply":"Arr.","automation":null}`)
|
||||
out = alice.request("POST", "/api/ai/chat", msgs("hello"), 200)
|
||||
if out["reply"] != "Arr." {
|
||||
t.Fatalf("byo chat reply = %v", out["reply"])
|
||||
}
|
||||
body := fake.lastRequest()
|
||||
if !strings.Contains(body, "Answer like a pirate.") {
|
||||
t.Fatal("user instructions missing from the assistant system prompt")
|
||||
}
|
||||
if !strings.Contains(body, "USER INSTRUCTIONS") {
|
||||
t.Fatal("instructions must be appended as a delimited user section")
|
||||
}
|
||||
|
||||
// The chat route is session-authenticated like every /api/ai route.
|
||||
anon, _ := cookiejar.New(nil)
|
||||
stranger := apiClient{t, &http.Client{Jar: anon}, srv.URL}
|
||||
|
||||
@@ -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