Documentation
¶
Overview ¶
Package agentbridge connects the Go agent loop (internal/agent) to the GUI (internal/app): Host implements app.AgentHost and its optional capabilities, and Backend implements agent.Backend over the app's live tabs.
Threads: the UI loop calls Host.Send/Stop/TakeControl/... and must never be blocked; Send starts a goroutine per run. The agent goroutines call Backend, which reaches the tabs only through app.Agent* methods (each executes on the tab's worker or the UI loop). Events travel agent -> translator goroutine -> the channel returned by Send -> the app's reader -> UI loop -> chrome page.
Index ¶
- func ValidKey(s string) bool
- type Backend
- func (b *Backend) Act(ctx context.Context, steps []agent.Step) (agent.Result, error)
- func (b *Backend) CloseTab(id string) error
- func (b *Backend) Confirm(ctx context.Context, req agent.ConfirmRequest) (bool, error)
- func (b *Backend) OpenTab(raw string) (agent.TabInfo, error)
- func (b *Backend) Screenshot(tabID string) ([]byte, error)
- func (b *Backend) Snapshot(tabID string, delta bool) (string, error)
- func (b *Backend) SwitchTab(id string) error
- func (b *Backend) Tabs() []agent.TabInfo
- func (b *Backend) Title(tabID string) string
- func (b *Backend) URL(tabID string) string
- type Host
- func (h *Host) AttachApp(a *app.App)
- func (h *Host) ClearKey()
- func (h *Host) Close()
- func (h *Host) Confirm(runID, id string, approved bool)
- func (h *Host) Info() app.AgentInfo
- func (h *Host) KeySource() string
- func (h *Host) NewChat()
- func (h *Host) OpenChat(id string) (string, []map[string]any, bool)
- func (h *Host) Prewarm(ctx context.Context) error
- func (h *Host) Resume()
- func (h *Host) Send(text, mode string, pc app.PageContext) <-chan app.AgentEvent
- func (h *Host) SetKey(key string)
- func (h *Host) SidebarOpened()
- func (h *Host) Stop()
- func (h *Host) StoreKey(key string) error
- func (h *Host) TakeControl()
- type KeyStore
- type Options
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
Backend implements agent.Backend over the live tabs of an app.App. Actions run on the engine tab the user is looking at (through the tab's worker), so the compositor repaints after every step and the user watches the agent work. Every read and action is checked against the user's per-origin visibility rule at the moment it happens (not only when the run started), and incognito tabs are invisible to the agent unless the whole window is private.
The browser verbs themselves (click, type, wait, expect, snapshot ...) are agent.RPCBackend's: this type adds tab-id mapping, the visibility gate, app-level navigation (HTTPS upgrade, tracker shield, history), visible scrolling, the cursor overlay and the confirm cards.
func (*Backend) Act ¶
Act runs steps one at a time. Each step is its own worker operation, so the page repaints between steps. It stops at the first failing non-optional step.
func (*Backend) Confirm ¶
Confirm blocks until the user answers the confirm card (the agent core has already emitted the event), the run is stopped, or the timeout passes.
func (*Backend) Screenshot ¶
Screenshot returns a PNG of a readable tab.
func (*Backend) Tabs ¶
Tabs lists the tabs the agent may know about. Pages it may not read are listed by id only; incognito tabs of a normal window are omitted.
type Host ¶
type Host struct {
// contains filtered or unexported fields
}
Host is the app.AgentHost over internal/agent.
func New ¶
New returns a host. It does not touch the network or the disk until the app attaches and the user (or ANTHROPIC_API_KEY) supplies a key.
func (*Host) AttachApp ¶
AttachApp gives the host the app whose tabs it will drive (called by app.New).
func (*Host) Close ¶
func (h *Host) Close()
Close cancels the run in flight and releases the connections. Safe to call more than once; the app calls it as the window closes.
func (*Host) KeySource ¶
KeySource reports where the key comes from ("session", "file", "env" or "").
func (*Host) Send ¶
func (h *Host) Send(text, mode string, pc app.PageContext) <-chan app.AgentEvent
Send starts a run. It never blocks: every failure comes back as an error event on the returned channel.
func (*Host) SidebarOpened ¶
func (h *Host) SidebarOpened()
SidebarOpened warms the API connection (dial + TLS + HTTP/2) so the first message does not pay for it.
func (*Host) TakeControl ¶
func (h *Host) TakeControl()
TakeControl pauses the agent; Resume hands the browser back.
type KeyStore ¶
type KeyStore struct {
// contains filtered or unexported fields
}
KeyStore keeps the user's Anthropic API key.
Where it lives:
- Windows: a DPAPI blob (CryptProtectData, bound to the user account) in a 0600 file under the profile directory.
- macOS and Linux: a 0600 file under the profile directory (the directory itself is 0700). There is deliberately no `security add-generic-password -w` call, because that puts the key in the process arguments. A native Keychain / Secret Service backend is future work; see docs/agent.md.
- No profile directory (in-memory / incognito / headless): memory only.
The ANTHROPIC_API_KEY environment variable is a fallback for development; it is never written anywhere. The key is never logged, never sent to the chrome page and is redacted from every error that leaves this package.
func NewKeyStore ¶
NewKeyStore returns a store rooted at dir ("" = memory only). env is the environment lookup (nil = os.Getenv); useEnv enables the env fallback.
func (*KeyStore) Clear ¶
func (k *KeyStore) Clear()
Clear forgets the stored key (memory and file). An env key stays.
type Options ¶
type Options struct {
// BaseURL overrides the API origin (default $SWEEF_AGENT_BASE_URL, then
// https://api.anthropic.com). For a future rate-limited proxy.
BaseURL string
// Model overrides agent.DefaultModel.
Model string
// Thinking defaults to disabled: the sidebar favours latency.
Thinking agent.ThinkingMode
// KeyDir is where the API key file lives when the profile has no
// directory of its own (private window). "" = memory only.
KeyDir string
// NoEnvKey ignores ANTHROPIC_API_KEY (tests).
NoEnvKey bool
// Env overrides os.Getenv (tests).
Env func(string) string
// ConfirmTimeout is how long a confirm card waits (default 2 min).
ConfirmTimeout time.Duration
// Tune lets the caller adjust the agent config before the agent is built
// (tests point the HTTP client at a fake server, shorten retries ...).
Tune func(*agent.Config)
// Logf receives diagnostics. It never sees the key.
Logf func(string, ...any)
}
Options configures a Host. The zero value is a production host.