bgmodel

package
v0.16.521 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const (
	ReasonUnauthorized = "unauthorized" // the provider refused the key
	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.

View Source
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.

View Source
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.

View Source
const (
	FolderTitleMax = 80
	FolderNameMax  = 80
)

FolderTitleMax and FolderNameMax cut a title and a name.

View Source
const (
	SearchSourcesMax    = 12
	SearchSourceTextMax = 1200
	SearchQuestionMax   = 400
)

The bounds of one answer: how many sources the model is shown and how much of each.

View Source
const (
	UIActGo  = "go"
	UIActSet = "set"
)

The kinds of a UIAction.

View Source
const (
	UIPlacesMax   = 160
	UISettingsMax = 80
	UIQuestionMax = 400
)

The bounds of one request.

View Source
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.

View Source
const FolderMax = 40

FolderMax is how many folders it is told of.

View Source
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.

View Source
const FolderPrompts = 6

FolderPrompts is how many of the chat's last prompts the model reads.

View Source
const FolderTitlesMax = 5

FolderTitlesMax is how many chat titles of one folder it is told.

View Source
const WidgetDefaultModel = "haiku"

WidgetDefaultModel is the machine's Claude model a smart widget runs on when nothing is picked for FeatureWidget.

Variables

View Source
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 IsAgent

func IsAgent(provider string) bool

IsAgent says whether a provider's name is an agent of the machine.

func ReasonOf

func ReasonOf(err error) string

ReasonOf is the reason of any error of a call.

func ValidFeature

func ValidFeature(id string) bool

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

func ParseChoice(s string) (Choice, bool)

ParseChoice reads "<provider>/<model>" (a model's name may have slashes of its own).

func (Choice) String

func (c Choice) String() string

String is the choice as one value: "<provider>/<model>".

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.

func (*Client) Complete

func (c *Client) Complete(ctx context.Context, r Request) (string, error)

Complete asks the model once and gives the text of its answer.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, r Request, onDelta func(string)) (string, error)

Stream asks the model once and hands over the text as it comes: onDelta gets every non-empty piece, and the whole text, trimmed, is the answer. A provider that ignores «stream» and answers in one body gives one piece.

type Error

type Error struct {
	Reason string
	Status int
}

Error is a failed call: why, and the provider's status when it answered. It holds neither the key nor the body.

func (*Error) Error

func (e *Error) Error() string

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.

func Features

func Features() []Feature

Features are the background features, in the order Settings lists them.

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

func (r *Runner) Forget(provider string) []string

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

func (r *Runner) Last() map[string]Outcome

Last is how each feature's last call went, by feature; a feature that was never called is not there.

func (*Runner) Move

func (r *Runner) Move(from, to string, hasModel func(model string) bool) []string

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

func (r *Runner) Pick(feature string, c *Choice) (before *Choice, err error)

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

func (r *Runner) Picked(feature string) bool

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.

func (*Runner) Usable

func (r *Runner) Usable(feature string) bool

Usable says whether Run would call a model for the feature: one is picked, or the feature has a default to fall back on.

func (*Runner) Widget

func (r *Runner) Widget(ctx context.Context, prompt, data string) (text string, used bool, err error)

Widget answers a smart widget's request over its data: a few short lines. 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.

func (*Store) All

func (s *Store) All() map[string]Choice

All is every choice made, by feature.

func (*Store) Get

func (s *Store) Get(feature string) (Choice, bool)

Get is the choice of a feature; ok is false for «as now».

func (*Store) Set

func (s *Store) Set(feature string, c *Choice) (before *Choice, err error)

Set keeps the choice of a feature; nil puts it back to «as now». It answers what the choice was before.

type UIAction

type UIAction struct {
	Kind  string
	Place string
	Key   string
	Value string
	Label string
}

UIAction is what the agent does besides answering: Place to go to, or Key to change to Value (Label in words).

type UIOption

type UIOption struct {
	Value string
	Label string
}

UIOption is one value a setting takes.

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

func PlacesOfMap(text string) []UIPlace

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.

type UISetting

type UISetting struct {
	Key        string
	Section    string
	Title      string
	Kind       string
	Options    []UIOption
	Min, Max   *int
	Value      string
	ValueLabel string
}

UISetting is a setting of the catalog as the agent is shown it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL