Documentation
¶
Overview ¶
Package ai is Mail's assistant: a summary of a thread and a draft reply. It asks the provider the user picked in Mail's settings (provider.go): their own logged-in coding CLI — Claude Code, Codex or Gemini CLI — headless and without tools (cli.go), an API with their key (Anthropic's Messages API, anthropic.go; OpenAI, OpenRouter or DeepSeek through the OpenAI-compatible API, below), or a model server on this computer (Ollama, LM Studio, llama.cpp — the same API). Mail text goes to that provider only while AI is on — when the user runs an AI action, and for inbox triage (triage.go) — and nowhere else.
Index ¶
- Constants
- Variables
- func Argv(provider, model, system, user string, batch bool, files map[string]string) ([]string, string, error)
- func CleanLine(s string, max int) string
- func DefaultModel(id string) string
- func DraftReply(ctx context.Context, c Chatter, thread []Mail, me, instruction string) (string, error)
- func IsBatch(p string) bool
- func LanguageName(code string) string
- func LanguageRule(code, what string) string
- func LocalSendsTo(raw string) string
- func Summarize(ctx context.Context, c Chatter, thread []Mail) (string, error)
- func SummarizeIn(ctx context.Context, c Chatter, thread []Mail, lang string) (string, error)
- func ValidCategory(c string) bool
- type Anthropic
- type CLI
- type Chatter
- type Client
- type Conversation
- type Info
- type Mail
- type Verdict
Constants ¶
const ( ProviderClaude = "claude" // the user's Claude Code login, through `claude -p` ProviderCodex = "codex" // the user's Codex login, through `codex exec` ProviderGemini = "gemini" // the user's Gemini CLI login, through `gemini -p` ProviderAnthropic = "anthropic" // Anthropic's Messages API with the user's key ProviderOpenAI = "openai" // OpenAI's API with the user's key ProviderOpenRouter = "openrouter" // OpenRouter with the user's key ProviderDeepSeek = "deepseek" // DeepSeek's API with the user's key ProviderLocal = "local" // an OpenAI-compatible server the user runs (Ollama by default) )
The providers Mail's assistant can ask.
const ( KindCLI = "cli" // a coding CLI the user is logged in to KindAPI = "api" // a cloud API that takes the user's key KindLocal = "local" // a server at an address the user gives )
Kinds of provider.
const ( CatImportant = "important" // a person wrote and it needs attention CatUpdates = "updates" // notifications, receipts, bookings, CI CatNewsletter = "newsletter" // marketing, promotions, digests CatSpam = "spam" // likely spam or phishing )
Triage categories: a small fixed set. Whatever the model answers is checked against it; anything else is dropped.
const AnthropicURL = "https://api.anthropic.com/v1"
AnthropicURL is the Messages API's base.
const DefaultURL = "http://127.0.0.1:11434/v1"
DefaultURL is Ollama's OpenAI-compatible endpoint.
const SummaryMax = 140
SummaryMax is the longest one-line summary kept, in runes.
Variables ¶
var AutoOrder = []string{ProviderClaude, ProviderCodex, ProviderGemini, ProviderLocal}
AutoOrder is the order "Automatic" tries: the user's CLIs first, so a machine with claude works with no setup, then a local server.
var Categories = []string{CatImportant, CatUpdates, CatNewsletter, CatSpam}
Categories is every category, in the order the inbox filters show them.
ErrUnavailable means no model server answered.
var Languages = map[string]string{"en": "English", "ru": "Russian", "es": "Spanish", "de": "German"}
Languages are the languages the user can ask Mail's AI to write in (setting ai_language); "auto" is the email's own language.
var Providers = []Info{ {ID: ProviderClaude, Label: "Claude Code", Kind: KindCLI, Command: "claude", ModelHint: "haiku, sonnet or opus; empty is your Claude Code default", SendsTo: "Anthropic, through your Claude Code login (the claude command on this computer)"}, {ID: ProviderCodex, Label: "Codex", Kind: KindCLI, Command: "codex", ModelHint: "empty is your Codex default", SendsTo: "OpenAI, through your Codex login (the codex command on this computer)"}, {ID: ProviderGemini, Label: "Gemini CLI", Kind: KindCLI, Command: "gemini", ModelHint: "empty is your Gemini CLI default", SendsTo: "Google, through your Gemini CLI login (the gemini command on this computer)"}, {ID: ProviderAnthropic, Label: "Anthropic API", Kind: KindAPI, URL: AnthropicURL, Model: "claude-haiku-5-5", ModelHint: "claude-haiku-5-5", SendsTo: "api.anthropic.com, with your API key"}, {ID: ProviderOpenAI, Label: "OpenAI API", Kind: KindAPI, URL: "https://api.openai.com/v1", NeedsModel: true, ModelHint: "a model id from your OpenAI account", SendsTo: "api.openai.com, with your API key"}, {ID: ProviderOpenRouter, Label: "OpenRouter", Kind: KindAPI, URL: "https://openrouter.ai/api/v1", NeedsModel: true, ModelHint: "vendor/model, as openrouter.ai/models lists it", SendsTo: "openrouter.ai with your API key, which passes it on to the model's vendor"}, {ID: ProviderDeepSeek, Label: "DeepSeek API", Kind: KindAPI, URL: "https://api.deepseek.com/v1", Model: "deepseek-chat", ModelHint: "deepseek-chat", SendsTo: "api.deepseek.com, with your API key"}, {ID: ProviderLocal, Label: "Local model", Kind: KindLocal, SendsTo: "the model server on this computer"}, }
Providers is every provider, in the order the picker lists them.
Functions ¶
func Argv ¶ added in v0.119.0
func Argv(provider, model, system, user string, batch bool, files map[string]string) ([]string, string, error)
Argv is the command line and the stdin for one call. files are the paths the call uses inside Dir: the Claude settings file, the Codex answer file.
claude: `-p --output-format json` prints one JSON result; `--tools ""` gives the model no tool at all; `--no-session-persistence` writes no transcript, so Caprock's own ingest never sees the call; `--setting-sources local` from an empty folder and `--settings` holding `disableAllHooks` keep the user's hooks, plugins and memory files out. `--system-prompt` carries the instructions, except through a Windows .cmd shim, where they go to stdin with the rest (as Second Brain does).
codex: `exec` is the non-interactive mode, the prompt read from stdin (`-`); `--ephemeral` writes no session file; `--ignore-user-config` and `--ignore-rules` leave the user's config.toml (MCP servers, hooks, profiles) and execpolicy rules out — the login is still read from CODEX_HOME; `--sandbox read-only` and the features above leave nothing that can act; `-c web_search="disabled"` turns the web search tool off; `--skip-git-repo-check` because Caprock's folder is no repository; `-o` writes the final answer to a file.
gemini: `-p` is the non-interactive mode, appended to stdin; `-o json` prints one JSON object with `response`; `--approval-mode plan` is its read-only mode; `--skip-trust` trusts Caprock's empty folder for the call; `-e none` loads no extension and `--allowed-mcp-server-names` with a name no server has loads no MCP server; the system settings file (GEMINI_CLI_SYSTEM_SETTINGS_PATH, see Chat) removes hooks and tools.
func CleanLine ¶ added in v0.125.0
CleanLine makes untrusted model text one plain line of at most max runes: control characters and line breaks become spaces, runs of space collapse.
func DefaultModel ¶ added in v0.119.0
DefaultModel is the model a provider uses when none is set.
func DraftReply ¶ added in v0.119.0
func DraftReply(ctx context.Context, c Chatter, thread []Mail, me, instruction string) (string, error)
DraftReply writes a reply to the last message of a thread. The user's own instruction is outside the fence: it is the one thing that is theirs.
func LanguageName ¶ added in v0.125.0
LanguageName is the name of a language code, "" for auto or anything unknown.
func LanguageRule ¶ added in v0.125.0
LanguageRule is the prompt's sentence on which language to write what in.
func LocalSendsTo ¶ added in v0.119.0
LocalSendsTo says where a local-kind server is: on this computer when its host is loopback, else the address itself, so a URL pointed at another machine is never described as local.
func Summarize ¶ added in v0.119.0
Summarize writes a short summary of a thread with what is asked of the reader, in the language of the email.
func SummarizeIn ¶ added in v0.125.0
SummarizeIn is Summarize in the language the user chose in Mail's settings (ai_language; "" or "auto" is the email's own language).
func ValidCategory ¶ added in v0.125.0
ValidCategory reports whether c is one of Categories.
Types ¶
type Anthropic ¶ added in v0.119.0
type Anthropic struct {
URL string // "" is AnthropicURL; tests point it at a fake
Key string
Model string
HTTP *http.Client
}
Anthropic calls the Messages API with the user's API key. Raw net/http, like the OpenAI-compatible client beside it: one POST does not earn the SDK's weight in a binary whose lightness is the point.
type CLI ¶ added in v0.119.0
type CLI struct {
Provider string // ProviderClaude, ProviderCodex or ProviderGemini
Binary string // the CLI's absolute path
Env []string
Dir string // an empty working folder of Caprock's
Model string // "" is the CLI's default
Timeout time.Duration
}
CLI asks the user's own logged-in coding CLI, headless, with no tool it can use, from an empty folder of Caprock's. The login is the CLI's: Caprock never sees a token. Every flag below was read from the real `--help` of Claude Code 2.1.300, codex-cli 0.161.0 and Gemini CLI 0.62.0 on 2026-10-15; the comments on Argv say what each one is for.
The prompt always goes on stdin, never on the command line: a long thread is not cut by a command-line limit, and a Windows .cmd shim (npm installs codex and gemini as one) cannot carry it there intact.
type Chatter ¶ added in v0.119.0
Chatter answers one system and one user message. Every provider is one.
type Client ¶
type Client struct {
URL string
Model string
Key string
HTTP *http.Client
// Remote is set for a cloud endpoint: errors then name the service, not
// "the model server", and an unreachable host is not "no local server".
Remote bool
}
Client is a chat-completions client for any OpenAI-compatible server: a local one (no key) or OpenAI, OpenRouter and DeepSeek (Key is sent as a bearer token).
type Conversation ¶ added in v0.125.0
Conversation is one inbox conversation given to Triage: its newest messages, the newest last.
type Info ¶ added in v0.119.0
type Info struct {
ID string `json:"id"`
Label string `json:"label"`
Kind string `json:"kind"`
// Command is the CLI's command name (KindCLI).
Command string `json:"command,omitempty"`
// URL is the API base (KindAPI).
URL string `json:"-"`
// Model is the model used when the user leaves the field empty; "" for a
// CLI means the CLI's own default.
Model string `json:"default_model,omitempty"`
// NeedsModel: no call is made until a model is typed.
NeedsModel bool `json:"needs_model,omitempty"`
// ModelHint is the model field's placeholder.
ModelHint string `json:"model_hint,omitempty"`
// SendsTo says where the mail text goes when an AI action runs (ADR-047).
SendsTo string `json:"sends_to"`
}
Info describes one provider for the settings panel.
type Mail ¶
type Mail struct {
From, Date, Subject, Body string
}
Mail is the text of one message given to the model.
type Verdict ¶ added in v0.125.0
type Verdict struct {
Key string
Category string
Reply bool // the last message waits on the reader's answer
Summary string // one line
}
Verdict is the model's answer for one conversation, validated.
func ParseTriage ¶ added in v0.125.0
func ParseTriage(out string, convs []Conversation) ([]Verdict, error)
ParseTriage reads the model's answer for a batch: a JSON object with "items", or a bare array, possibly wrapped in prose or a code fence. Only valid items for conversations of the batch are returned, once each.
func Triage ¶ added in v0.125.0
Triage sorts a batch of conversations in one call. The model sees them as c1…cN; an answer for an id not in the batch, or with a category outside Categories, is dropped, so a conversation missing from the result was not classified. An error means nothing came back that could be read.