Separate AI config from implementation: BYO provider, model and key per user
release gate / check (push) Failing after 6m42s
release gate / check (push) Failing after 6m42s
The plan matrix's 'use your own AI' rows, made real. internal/ai is now pure implementation: three provider adapters — openai-compatible (OpenRouter, OpenAI, Grok, Groq, Ollama, any house gateway), native Anthropic (/v1/messages, x-api-key) and native Gemini (generateContent; the key travels in the x-goog-api-key HEADER, never the URL, because Go's HTTP error strings include URLs and errors get logged). One netguard-dialed client, per-source outbound budgets (ai:user:<id> vs ai:server). Configuration is data: UserAiConfig (migration 015) — one row per user with provider, base URL, model and an API key encrypted at rest via internal/crypto (the IMAP-password format), masked in every response; a PUT echoing the mask or leaving the key blank keeps the stored value; version contract on updates. New endpoints: GET/PUT/DELETE /api/ai/config, POST /api/ai/test (one probe completion through the resolved config), GET /api/ai/status (plan-gated availability, no secrets). Resolution chain (ai.ResolveForUser): the user's enabled BYO config first on EVERY plan; else the server env key (AI_API_KEY/AI_MODEL/AI_BASE_URL/ AI_PROVIDER) only when the tenant's plan may spend it — new entitlements.AllowServerAI: premium (credits), core (house gateway) and selfhosted (operator IS the user); free/standard are BYO-only, exactly the matrix. No AI available => 428 with a pointer to Settings -> AI (was a global 503): the precondition is per-user now. Agent compile and url_watch extraction resolve per owner, so different users' tasks can run on different providers. Web: Settings -> AI tab (provider picker with per-provider defaults and model hints, write-only key input, Test button, effective-source badge with plan context, enable switch, remove). The Automations tab reads /api/ai/status for a proactive banner and treats 428 as 'not configured'. Preparatory refactors (behavior-preserving): agent.AutomationSchema extracted as the single spec text shared by the compiler prompt (and the upcoming chat assistant), and ai.CompleteMessages for multi-turn conversations with per-provider role mapping (assistant -> model on Gemini). Tests: all three adapters against one loopback fake (asserting each provider's auth-header shape), masked-key lifecycle incl. mask-echo preservation across a provider switch, encrypted-at-rest verification, plan gate both directions (free refuses the server key, premium/core/ selfhosted allow it), BYO precedence over the server key, validation rejects, 428 paths, and compile resolution through a BYO config. Full suite green (-race), converge green (015), web lint/SSR/build clean. Docs: API.md AI section, RUNNING.md setup rewritten around BYO-first + plan-gated server fallback, .env.example (AI_PROVIDER + the precedence story), compose passthrough, AGENTS.md invariant (config vs implementation, Gemini key-in-header rule).
This commit is contained in:
@@ -280,6 +280,25 @@ through netguard + outbound budgets.
|
||||
| DELETE | `/api/agent/tasks/{id}` | `{ok:true}` |
|
||||
| POST | `/api/agent/tasks/{id}/run` | Immediate evaluation → `{outcome}` ∈ checked/fired/failed/skipped |
|
||||
| GET | `/api/agent/runs?taskId=` | Last 50 runs `[{id,taskId,outcome,detail,createdAt}]` |
|
||||
|
||||
### AI configuration (per user)
|
||||
|
||||
Provider adapters are implementation (`internal/ai`: OpenAI-compatible,
|
||||
Anthropic, Gemini); configuration is data: one encrypted `UserAiConfig`
|
||||
row per user (BYO key, masked in every response, blank/mask on PUT keeps
|
||||
the stored key). Resolution per request: the user's enabled config →
|
||||
else the server env key (`AI_*`) **only when the plan allows it**
|
||||
(`entitlements.AllowServerAI`: premium/core/selfhosted; free/standard are
|
||||
BYO-only) → else 428. Budgets are per user (`ai:user:<id>`) or shared
|
||||
(`ai:server`).
|
||||
|
||||
| Method | Path | Body / Returns |
|
||||
|---|---|---|
|
||||
| GET | `/api/ai/config` | The caller's masked config or `null` |
|
||||
| PUT | `/api/ai/config` | `{provider,baseUrl?,model,apiKey,enabled?,version?}` → 201/200 masked; `version` required once a row exists (409 stale) |
|
||||
| DELETE | `/api/ai/config` | `{ok:true}` |
|
||||
| POST | `/api/ai/test` | One probe completion through the resolved config → `{ok,source,model,reply}`; 428 when nothing is available; 502 with a truncated provider error |
|
||||
| GET | `/api/ai/status` | `{userConfigured,userEnabled,serverAvailable,effective,plan}` — `serverAvailable` is already plan-gated; no secrets |
|
||||
| GET | `/api/alerts/check` | Undismissed bookmarks due within 15 minutes, soonest first |
|
||||
| DELETE | `/api/alerts/{bookmarkId}` | `{ok:true}`; sets `alertSent` |
|
||||
| GET | `/api/contacts` | Synced contacts with `_count.bookmarks` |
|
||||
|
||||
Reference in New Issue
Block a user