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:
+16
-10
@@ -68,13 +68,18 @@ var ErrNoAI = errors.New("ai: no configuration available for this user")
|
||||
|
||||
// Resolved is one usable endpoint. Source is "user" (BYO) or "server"
|
||||
// (env); it feeds budget keys and the status endpoint, never prompts.
|
||||
// Instructions carries the user's optional custom system instructions
|
||||
// (BYO configs only; the server key has none). They apply to the in-app
|
||||
// assistant persona — server-owned contracts (automation compiler, page
|
||||
// extraction) must never include them.
|
||||
type Resolved struct {
|
||||
Provider string
|
||||
BaseURL string
|
||||
Model string
|
||||
APIKey string
|
||||
Source string
|
||||
UserID string
|
||||
Provider string
|
||||
BaseURL string
|
||||
Model string
|
||||
APIKey string
|
||||
Instructions string
|
||||
Source string
|
||||
UserID string
|
||||
}
|
||||
|
||||
// client dials through netguard for every provider (public ranges +
|
||||
@@ -128,11 +133,11 @@ func serverConfig() (Resolved, bool) {
|
||||
// ResolveForUser applies the resolution chain: the user's enabled BYO
|
||||
// config first, then the server key iff the user's plan may spend it.
|
||||
func ResolveForUser(ctx context.Context, d *db.DB, userID string) (Resolved, error) {
|
||||
var id, provider, baseURL, model, encKey string
|
||||
var id, provider, baseURL, model, encKey, instructions string
|
||||
var enabled bool
|
||||
err := d.QueryRowContext(ctx,
|
||||
`SELECT id, provider, "baseUrl", model, "apiKey", enabled FROM "UserAiConfig" WHERE "ownerId"=$1`,
|
||||
userID).Scan(&id, &provider, &baseURL, &model, &encKey, &enabled)
|
||||
`SELECT id, provider, "baseUrl", model, "apiKey", instructions, enabled FROM "UserAiConfig" WHERE "ownerId"=$1`,
|
||||
userID).Scan(&id, &provider, &baseURL, &model, &encKey, &instructions, &enabled)
|
||||
if err == nil && enabled && encKey != "" {
|
||||
key, decErr := crypt.Decrypt(encKey, config.Load().EncryptionSecret())
|
||||
if decErr != nil {
|
||||
@@ -140,7 +145,8 @@ func ResolveForUser(ctx context.Context, d *db.DB, userID string) (Resolved, err
|
||||
}
|
||||
return Resolved{
|
||||
Provider: provider, BaseURL: strings.TrimRight(baseURL, "/"),
|
||||
Model: model, APIKey: key, Source: "user", UserID: userID,
|
||||
Model: model, APIKey: key, Instructions: instructions,
|
||||
Source: "user", UserID: userID,
|
||||
}, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
|
||||
Reference in New Issue
Block a user