Documentation
¶
Overview ¶
Package bgmodel is the model behind the background features: the small jobs harnessly does beside a chat (the summary of a notification, «what is going on now», the next-step hints, the background memory). A person picks a provider and a model for each (Settings → Background tasks), or leaves it «as now», where the chat's own agent decides and nothing here is called.
The client is small on purpose: one chat completion, in the OpenAI-compatible or the Anthropic-compatible form, with a timeout and a cap on the answer's tokens. A feature that cannot get its answer falls back to what it did before.
Index ¶
- Constants
- Variables
- func FolderFromAnswer(answer string, folders []FolderInfo) string
- func IsAgent(provider string) bool
- func ReasonOf(err error) string
- func ValidFeature(id string) bool
- type AgentCall
- type Call
- type Choice
- type Client
- type Error
- type Feature
- type FolderInfo
- type Lister
- type Outcome
- type Providers
- type Request
- type Runner
- func (r *Runner) Forget(provider string) []string
- func (r *Runner) Last() map[string]Outcome
- func (r *Runner) Move(from, to string, hasModel func(model string) bool) []string
- func (r *Runner) NotifySummary(ctx context.Context, answer string) (summary string, used bool, err error)
- func (r *Runner) Pick(feature string, c *Choice) (before *Choice, err error)
- func (r *Runner) Picked(feature string) bool
- func (r *Runner) Run(ctx context.Context, feature string, call Call) (text string, used bool, err error)
- func (r *Runner) RunStream(ctx context.Context, feature string, call Call, onDelta func(string)) (text string, used bool, err error)
- func (r *Runner) SearchAnswer(ctx context.Context, question string, sources []SearchSource) (text string, cited []int, used bool, err error)
- func (r *Runner) SearchAnswerStream(ctx context.Context, question string, sources []SearchSource, ...) (text string, cited []int, used bool, err error)
- func (r *Runner) SuggestFolder(ctx context.Context, prompts []string, folders []FolderInfo) (id string, used bool, err error)
- func (r *Runner) TurnSummary(ctx context.Context, prompt string) (text string, used bool, err error)
- func (r *Runner) UIAgentStream(ctx context.Context, question, page, lang string, places []UIPlace, ...) (text string, act *UIAction, used bool, err error)
- func (r *Runner) Usable(feature string) bool
- func (r *Runner) Widget(ctx context.Context, prompt, data string) (text string, used bool, err error)
- type SearchSource
- type Store
- type UIAction
- type UIOption
- type UIPlace
- type UISetting
Constants ¶
const ( DefaultTimeout = 12 * time.Second MaxTimeout = 60 * time.Second DefaultMaxTokens = 256 MaxMaxTokens = 4096 // MaxPrompt is the most text a call sends (bytes); a longer prompt keeps its end, where a turn's outcome is. MaxPrompt = 24 << 10 )
Bounds of one call.
const ( ReasonUnreachable = "unreachable" // the address did not answer ReasonTimeout = "timeout" // no answer in time ReasonRateLimited = "rate-limited" // 429 ReasonRejected = "rejected" // another error of the provider ReasonEmpty = "empty" // an answer with no text ReasonNoProvider = "no-provider" // the provider picked for the feature is gone )
Reasons a call failed.
const ( // FeatureNotifySummary: the text of a finished turn's notification. FeatureNotifySummary = "notify-summary" // FeatureChatNow: «what is going on now» in a chat. FeatureChatNow = "chat-now" // FeatureNextStep: the hints of the next step. FeatureNextStep = "next-step" // FeatureMemory: the background memory. FeatureMemory = "memory" // FeatureSearchAnswer: the answer the search gives to a question in words. FeatureSearchAnswer = "search-answer" // FeatureUIAgent: the agent of the window, which answers a request in words and takes the person where they asked. FeatureUIAgent = "ui-agent" // FeatureWidget: the short line a smart widget of the chat's right column asks for. FeatureWidget = "widget" // FeatureFolder: the folder the app offers for a chat that has none, after a few prompts. FeatureFolder = "folder-suggest" // FeatureInboxTriage classifies incoming items, without starting a chat. FeatureInboxTriage = "inbox-triage" // InboxGPTProfile is the managed GPT profile, not a connected API account or an agent CLI. InboxGPTProfile = "@gpt" )
The background features a provider can be picked for.
const ( AgentClaude = "claude" AgentCodex = "codex" )
The agents of the machine a feature may also run on: a model of Claude Code or Codex through its CLI and the machine's own sign-in (a plan), with no key of a provider.
const ( FolderTitleMax = 80 FolderNameMax = 80 )
FolderTitleMax and FolderNameMax cut a title and a name.
const ( SearchSourcesMax = 12 SearchSourceTextMax = 1200 SearchQuestionMax = 400 )
The bounds of one answer: how many sources the model is shown and how much of each.
const ( UIActGo = "go" UIActSet = "set" )
The kinds of a UIAction.
const ( UIPlacesMax = 160 UISettingsMax = 80 UIQuestionMax = 400 )
The bounds of one request.
const ( WidgetPromptMax = 2000 WidgetDataMax = 8000 WidgetAnswerMax = 400 )
The bounds of a smart widget's call: the question the widget's manifest asks, the data the page gives it (what the widget shows, never a secret or a chat), and a short answer.
const FolderMax = 40
FolderMax is how many folders it is told of.
const FolderPromptMax = 600
The bounds of a folder suggestion: the person's last prompts of the chat, the folders with a few of the chats each holds (their titles say what the folder is for), and a one-word answer.
const FolderPrompts = 6
FolderPrompts is how many of the chat's last prompts the model reads.
const FolderTitlesMax = 5
FolderTitlesMax is how many chat titles of one folder it is told.
const WidgetDefaultModel = "haiku"
WidgetDefaultModel is the machine's Claude model a smart widget runs on when nothing is picked for FeatureWidget.
Variables ¶
var ( ErrUnknownFeature = errors.New("bgmodel: no such feature") ErrUnknownProvider = errors.New("bgmodel: no such provider") ErrBadModel = errors.New("bgmodel: no model named") ErrBadEffort = errors.New("bgmodel: no such effort") )
Reasons a choice is refused.
Functions ¶
func FolderFromAnswer ¶
func FolderFromAnswer(answer string, folders []FolderInfo) string
FolderFromAnswer reads the model's answer: the id of a folder it names, "" for none or for a word that is no folder of the list (a made-up id never reaches the page).
func ValidFeature ¶
ValidFeature says whether id is a background feature.
Types ¶
type AgentCall ¶
type AgentCall func(ctx context.Context, agent, model string, call Call, onDelta func(string)) (string, error)
AgentCall asks a model of an agent of the machine: the call's system and prompt to the agent's CLI, its text handed to onDelta as it comes when onDelta is not nil. It answers the whole text.
type Call ¶
type Call struct {
System string
Prompt string
MaxTokens int
Timeout time.Duration
// Quick: an answer of a line, wanted at once (Request.Quick).
Quick bool
}
Call is what a feature asks its model.
type Choice ¶
type Choice struct {
Provider string `json:"provider"`
Model string `json:"model"`
// Effort is the reasoning effort the model is asked for (extproviders.ModelEfforts); "" is the model's own.
Effort string `json:"effort,omitempty"`
}
Choice is the provider and the model picked for a feature.
func ParseChoice ¶
ParseChoice reads "<provider>/<model>" (a model's name may have slashes of its own).
type Client ¶
type Client struct {
// HTTP sends them; nil is a client that follows no redirect (a redirect would carry the key to the host it points
// at).
HTTP *http.Client
}
Client makes the calls.
type Error ¶
Error is a failed call: why, and the provider's status when it answered. It holds neither the key nor the body.
type Feature ¶
type Feature struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
// Live: the feature already goes through the picked provider. One that is not live keeps the choice for the day
// it does.
Live bool `json:"live"`
}
Feature is one background feature as Settings lists it.
type FolderInfo ¶
type FolderInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Titles []string `json:"titles"`
}
FolderInfo is one folder of the person: its id (what the answer names), its name and some of its chats' titles.
type Lister ¶
type Lister interface {
List() ([]extproviders.View, error)
}
Lister is what a registry of providers may add to Providers: its accounts, for a feature that has a default.
type Outcome ¶
type Outcome struct {
At int64 `json:"at"`
OK bool `json:"ok"`
// Reason and Message say why it failed (Message in words for the person); empty when it went well.
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
}
Outcome is how a feature's last call went.
type Providers ¶
type Providers interface {
Get(id string) (extproviders.Provider, error)
Key(p extproviders.Provider) (string, error)
}
Providers is the part of the providers' registry the features use.
type Request ¶
type Request struct {
BaseURL string
API extproviders.API
Key string
Model string
// System is the instruction, Prompt the text it is about.
System string
Prompt string
// MaxTokens caps the answer (DefaultMaxTokens when 0), Timeout the whole call (DefaultTimeout when 0).
MaxTokens int
Timeout time.Duration
// Quick asks the model to answer without thinking first (an Anthropic-form API; the other form has no common way).
Quick bool
// Effort is the reasoning effort the person gave the model (extproviders.ModelEfforts); "" sends none. A quick call
// does not think whatever it is.
Effort string
}
Request is one completion.
type Runner ¶
type Runner struct {
Store *Store
Providers Providers
Client *Client
// Agent asks the machine's agents; nil: a feature may be given a provider's model only.
Agent AgentCall
// InboxGPT is the selected GPT profile model for Inbox only; it never uses an agent CLI.
InboxGPT func(context.Context, Choice, Call) (string, error)
Now func() time.Time
// Log is told of a call that failed. The caller decides whether to fall back.
Log func(feature string, c Choice, err error)
// contains filtered or unexported fields
}
Runner runs the features on the providers picked for them.
func (*Runner) Forget ¶
Forget puts every feature that runs on a provider back to «as now»: the provider was removed. It answers the features it changed.
func (*Runner) Last ¶
Last is how each feature's last call went, by feature; a feature that was never called is not there.
func (*Runner) Move ¶
Move puts every feature that runs on one account onto another of the same provider, where that one has the feature's model. It answers the features it moved; the rest stay for Forget.
func (*Runner) NotifySummary ¶
func (r *Runner) NotifySummary(ctx context.Context, answer string) (summary string, used bool, err error)
NotifySummary is the summary of a finished turn for its notification: one short line of what the agent did, from the text of its answer. used is false for «as now».
func (*Runner) Pick ¶
Pick keeps the choice of a feature after checking it: the provider is one of the registry and a model is named; nil puts the feature back to «as now». It answers what the choice was before.
func (*Runner) Picked ¶
Picked says whether a provider is picked for the feature: false is «as now», and the feature does what it always did without calling Run.
func (*Runner) Run ¶
func (r *Runner) Run(ctx context.Context, feature string, call Call) (text string, used bool, err error)
Run asks the model picked for the feature. used is false when none is picked («as now»): the caller does what it did before, and nothing was called. An error means the same for the caller, and is recorded (Last, Log).
func (*Runner) RunStream ¶
func (r *Runner) RunStream(ctx context.Context, feature string, call Call, onDelta func(string)) (text string, used bool, err error)
RunStream is Run with the text handed to onDelta as it comes.
func (*Runner) SearchAnswer ¶
func (r *Runner) SearchAnswer(ctx context.Context, question string, sources []SearchSource) (text string, cited []int, used bool, err error)
SearchAnswer answers a question from what the search found. cited are the sources the answer rests on, by their place in sources, in the order the answer names them. used is false when no model can answer (none is picked and the registry has none to fall back on): the caller says a model has to be picked.
func (*Runner) SearchAnswerStream ¶
func (r *Runner) SearchAnswerStream(ctx context.Context, question string, sources []SearchSource, onDelta func(string)) (text string, cited []int, used bool, err error)
SearchAnswerStream is SearchAnswer that hands the raw pieces of the answer to onDelta as they come (they may hold the [2] marks); text and cited are what SearchAnswer gives.
func (*Runner) SuggestFolder ¶
func (r *Runner) SuggestFolder(ctx context.Context, prompts []string, folders []FolderInfo) (id string, used bool, err error)
SuggestFolder picks a folder for a chat from its prompts: the folder's id, or "" when none fits. used is false when no model can answer.
func (*Runner) TurnSummary ¶
func (r *Runner) TurnSummary(ctx context.Context, prompt string) (text string, used bool, err error)
TurnSummary is the few lines on a finished turn that stand on the chat as its recap, from a prompt that carries the turn and says what to write. used is false for «as now».
func (*Runner) UIAgentStream ¶
func (r *Runner) UIAgentStream(ctx context.Context, question, page, lang string, places []UIPlace, settings []UISetting, onAction func(UIAction), onDelta func(string)) (text string, act *UIAction, used bool, err error)
UIAgentStream answers a request to the window. onAction is called once, as soon as an action's mark is read, and only for an action the request allows; onDelta gets the words of the answer as they come, without the mark. lang is the interface's language ("en", "ru"; else the machine's): the answer is in it. used is false when no model can answer.
type SearchSource ¶
type SearchSource struct {
// Kind says what it is in a word (a chat, a document, a setting); Title is its name; Text is what was found in it.
Kind string `json:"kind"`
Title string `json:"title"`
Text string `json:"text"`
}
SearchSource is one thing the search found, as the model is shown it.
type Store ¶
type Store struct {
Path string
// contains filtered or unexported fields
}
Store keeps the choices in a file: {"features": {"<id>": {"provider", "model"}}}. A feature that is not there is «as now». A file that is missing or does not read is no choices.
type UIAction ¶
UIAction is what the agent does besides answering: Place to go to, or Key to change to Value (Label in words).
type UIPlace ¶
type UIPlace struct {
ID string `json:"id"`
Title string `json:"title"`
Words string `json:"words,omitempty"`
}
UIPlace is a place of the window the agent can take the person to: a section of Settings, a screen, a setting as its page names it. ID is the page's own address of it; Words are other words it is found by.
func PlacesOfMap ¶
PlacesOfMap reads the window's map: each line «- <name>: /<path>» is a place whose id is its path; other lines (the map's heading) are left out.