The module path claimed a nonexistent GitHub repo. Point it (and the
agent User-Agent contact URL, README/RUNNING clone URLs) at the real
Gitea repository. Pure identifier rename: no runtime behavior change.
Verified: build, vet, unit + full DB integration (-race), db-converge,
web lint/SSR/build, browser regressions, e2e against rebuilt stack.
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.
The A+B slice: one conversation surface for questions AND automations.
Backend: POST /api/ai/chat — ephemeral multi-turn chat through the same
per-user AI resolution as everything else (BYO key -> plan-gated server
key -> 428). The server is stateless per request: the browser owns
history, the handler caps it anyway (40 messages accepted, last 20 used,
8k chars each, last turn must be the user's). Per-user rate limit
(30/hour, keyed on the account not the IP) and a 50-second budget inside
the proxy's 55s window.
The chat system prompt embeds agent.AutomationSchema — the SAME contract
text the compiler uses — so chat proposals and compiled tasks can never
drift. The model answers in a {reply, automation|null} JSON envelope; a
proposal reaches the client only if its spec passes agent.ValidateSpec
(invalid ones are dropped silently, the reply survives), and model-
invented channelIds are scrubbed so the UI always picks from the user's
real channels. A model that ignores the envelope degrades gracefully: raw
text becomes the reply, no proposal.
Web: components/assistant.tsx — floating launcher on every signed-in
screen (mounted once in AppShellWrapper so conversations survive in-app
navigation), Sheet drawer with markdown rendering (house ReactMarkdown +
remarkGfm pattern), proposal cards (channel picker for notify, editable
recipient for email, Create automation -> the regular tasks endpoint),
and Save to page (Markdown transcript into Pages — the only persistence,
by design). Enter sends, Shift+Enter newline.
Tests: envelope parsing, valid proposal + channelId scrub, invalid-spec
drop with reply intact, non-JSON graceful fallback, request validation
(empty/assistant-last/system-role), multi-turn passthrough, 428 path.
Full suite green (-race); web lint/SSR-eslint/build clean.
Docs: API.md chat endpoint, RUNNING.md 'two doors' framing (drawer +
Settings), README feature bullet, plan/notes docs updated.
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).