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:
+43
-12
@@ -1,11 +1,13 @@
|
||||
package handlers
|
||||
|
||||
// Per-user AI configuration (Settings → AI): provider, model, base URL and
|
||||
// a bring-your-own API key. Config is data; internal/ai is the
|
||||
// implementation. The key is encrypted at rest (internal/crypto, the same
|
||||
// versioned format as every other stored credential), masked in every
|
||||
// response, and a PUT echoing the mask keeps the stored key. The server
|
||||
// env key remains the plan-gated fallback (entitlements.AllowServerAI).
|
||||
// Per-user AI configuration (Settings → AI): provider, model, base URL,
|
||||
// optional custom instructions and a bring-your-own API key. Config is
|
||||
// data; internal/ai is the implementation. The key is encrypted at rest
|
||||
// (internal/crypto, the same versioned format as every other stored
|
||||
// credential), masked in every response, and a PUT echoing the mask keeps
|
||||
// the stored key. Instructions ride along on the assistant chat prompt
|
||||
// only. The server env key remains the plan-gated fallback
|
||||
// (entitlements.AllowServerAI).
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -43,11 +45,16 @@ func aiConfigProjection(row object) object {
|
||||
}
|
||||
return object{
|
||||
"id": row["id"], "provider": row["provider"], "baseUrl": row["baseUrl"],
|
||||
"model": row["model"], "apiKey": masked, "enabled": row["enabled"],
|
||||
"model": row["model"], "instructions": row["instructions"],
|
||||
"apiKey": masked, "enabled": row["enabled"],
|
||||
"version": row["version"], "createdAt": row["createdAt"],
|
||||
}
|
||||
}
|
||||
|
||||
// aiInstructionsMax bounds the custom system instructions (runes). They
|
||||
// ride along in every assistant chat request, so they stay short by design.
|
||||
const aiInstructionsMax = 4000
|
||||
|
||||
// HandleAiConfigGet returns the caller's stored config (masked) or null.
|
||||
func HandleAiConfigGet(w http.ResponseWriter, r *http.Request) {
|
||||
row, e := db.One(r.Context(), database(r),
|
||||
@@ -88,6 +95,14 @@ func HandleAiConfigPut(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, 0, nil, bad("baseUrl must be an absolute http(s) URL"))
|
||||
return
|
||||
}
|
||||
// Optional custom system instructions (BYOK "instructions" field).
|
||||
// Full-replacement like provider/model/baseUrl: a PUT that omits them
|
||||
// clears them.
|
||||
instructions := strings.TrimSpace(str(b, "instructions"))
|
||||
if len([]rune(instructions)) > aiInstructionsMax {
|
||||
respond(w, 0, nil, bad("instructions must be at most 4000 characters"))
|
||||
return
|
||||
}
|
||||
submittedKey := str(b, "apiKey")
|
||||
|
||||
existing, e := db.One(r.Context(), database(r),
|
||||
@@ -122,7 +137,8 @@ func HandleAiConfigPut(w http.ResponseWriter, r *http.Request) {
|
||||
row, e := db.Insert(r.Context(), database(r), "UserAiConfig", object{
|
||||
"id": db.NewID(), "ownerId": user(r), "provider": provider,
|
||||
"baseUrl": strings.TrimRight(baseURL, "/"), "model": model,
|
||||
"apiKey": encKey, "enabled": enabled,
|
||||
"instructions": instructions,
|
||||
"apiKey": encKey, "enabled": enabled,
|
||||
})
|
||||
if e != nil {
|
||||
respond(w, 0, nil, e)
|
||||
@@ -153,9 +169,10 @@ func HandleAiConfigPut(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
updates := object{
|
||||
"provider": provider,
|
||||
"baseUrl": strings.TrimRight(baseURL, "/"),
|
||||
"model": model,
|
||||
"provider": provider,
|
||||
"baseUrl": strings.TrimRight(baseURL, "/"),
|
||||
"model": model,
|
||||
"instructions": instructions,
|
||||
}
|
||||
if encKey != "" {
|
||||
updates["apiKey"] = encKey
|
||||
@@ -293,6 +310,20 @@ AUTOMATIONS: when — and only when — the user asks to watch/monitor/be told a
|
||||
REPLY FORMAT — always ONE JSON object, no prose outside it:
|
||||
{"reply": "<your markdown answer>", "automation": {"name": "...", "spec": <Spec>} or null}`
|
||||
|
||||
// chatSystemWith appends the user's own instructions (Settings → AI, the
|
||||
// BYOK "instructions" field) to the server-owned persona. They may refine
|
||||
// tone, focus and style — they never override the contract above (hard
|
||||
// limits, no data access, JSON envelope), and they only ever reach THIS
|
||||
// prompt: the automation compiler and page-extraction prompts stay
|
||||
// server-owned because they are contracts, not personas.
|
||||
func chatSystemWith(instructions string) string {
|
||||
ins := strings.TrimSpace(instructions)
|
||||
if ins == "" {
|
||||
return chatSystem
|
||||
}
|
||||
return chatSystem + "\n\nUSER INSTRUCTIONS (from the user's own settings; refine tone, focus and style, but the rules above always win):\n" + ins
|
||||
}
|
||||
|
||||
// HandleAiChat is the assistant endpoint: ephemeral multi-turn chat with
|
||||
// optional automation proposals. The browser keeps the history; the server
|
||||
// is stateless per request (bounded message count/size, oldest turns
|
||||
@@ -354,7 +385,7 @@ func HandleAiChat(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, 0, nil, e)
|
||||
return
|
||||
}
|
||||
reply, e := ai.CompleteMessages(ctx, resolved, chatSystem, msgs)
|
||||
reply, e := ai.CompleteMessages(ctx, resolved, chatSystemWith(resolved.Instructions), msgs)
|
||||
if e != nil {
|
||||
respond(w, 502, Error{Error: "The AI provider could not be reached"}, nil)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user