agentbridge

package
v0.1.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 18 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidKey

func ValidKey(s string) bool

ValidKey reports whether s looks like an API key (no whitespace or control characters, plausible length). It does not check the key with the server.

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

func (b *Backend) Act(ctx context.Context, steps []agent.Step) (agent.Result, error)

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) CloseTab

func (b *Backend) CloseTab(id string) error

CloseTab closes a tab.

func (*Backend) Confirm

func (b *Backend) Confirm(ctx context.Context, req agent.ConfirmRequest) (bool, error)

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) OpenTab

func (b *Backend) OpenTab(raw string) (agent.TabInfo, error)

OpenTab opens a new foreground tab.

func (*Backend) Screenshot

func (b *Backend) Screenshot(tabID string) ([]byte, error)

Screenshot returns a PNG of a readable tab.

func (*Backend) Snapshot

func (b *Backend) Snapshot(tabID string, delta bool) (string, error)

Snapshot returns the accessibility snapshot of a readable tab.

func (*Backend) SwitchTab

func (b *Backend) SwitchTab(id string) error

SwitchTab brings a tab to the front (the user sees it).

func (*Backend) Tabs

func (b *Backend) Tabs() []agent.TabInfo

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.

func (*Backend) Title

func (b *Backend) Title(tabID string) string

Title returns the title of a readable tab ("" otherwise).

func (*Backend) URL

func (b *Backend) URL(tabID string) string

URL returns the tab's real URL (the agent core needs it to decide what it may show the model; it syncs the user's rule for that origin first).

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host is the app.AgentHost over internal/agent.

func New

func New(o Options) *Host

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

func (h *Host) AttachApp(a *app.App)

AttachApp gives the host the app whose tabs it will drive (called by app.New).

func (*Host) ClearKey

func (h *Host) ClearKey()

ClearKey forgets the stored key.

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) Confirm

func (h *Host) Confirm(runID, id string, approved bool)

Confirm delivers the user's answer to a confirm card.

func (*Host) Info

func (h *Host) Info() app.AgentInfo

Info feeds state.agent (called on every state push: cheap, no I/O).

func (*Host) KeySource

func (h *Host) KeySource() string

KeySource reports where the key comes from ("session", "file", "env" or "").

func (*Host) NewChat

func (h *Host) NewChat()

NewChat starts a new conversation.

func (*Host) OpenChat

func (h *Host) OpenChat(id string) (string, []map[string]any, bool)

OpenChat shows an old transcript. The model starts without it.

func (*Host) Prewarm

func (h *Host) Prewarm(ctx context.Context) error

Prewarm exposes warm for tests: it reports the error of the HEAD request.

func (*Host) Resume

func (h *Host) Resume()

Resume continues after TakeControl.

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) SetKey

func (h *Host) SetKey(key string)

SetKey stores the key (see KeyStore) and rebuilds the agent.

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) Stop

func (h *Host) Stop()

Stop cancels the active run.

func (*Host) StoreKey

func (h *Host) StoreKey(key string) error

StoreKey is SetKey with a result: junk is refused (and a toast says so).

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

func NewKeyStore(dir string, env func(string) string, useEnv bool) *KeyStore

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.

func (*KeyStore) Get

func (k *KeyStore) Get() (key, source string)

Get returns the key and where it came from ("session", "file", "env", "").

func (*KeyStore) Save

func (k *KeyStore) Save(key string) error

Save stores the key (file if a directory is known, always in memory).

func (*KeyStore) SetDir

func (k *KeyStore) SetDir(dir string)

SetDir changes the directory (used once the profile is known). A key saved in memory before that is persisted.

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.

Jump to

Keyboard shortcuts

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