mobileagent

package
v1.5.122 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 14 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 mobileagent is the UI-neutral session boundary for native mobile hosts. Android (Kotlin) and a future iOS (Swift) host supply the workspace, provider stream, tool registry and authorization gate; this package owns one conversation, one run at a time, cancellation, and an ordered JSON-encodable event queue.

It deliberately imports neither gui/appsession nor any desktop browser, terminal or Computer Use package. A host that wants the Android tool catalog builds it with internal/androidagent and passes the resulting Batch config.

Index

Constants

View Source
const (
	EventRunStart        = "run_start"
	EventText            = "text"
	EventThinking        = "thinking"
	EventToolStart       = "tool_start"
	EventToolUpdate      = "tool_update"
	EventToolEnd         = "tool_end"
	EventContextUsage    = "context_usage"
	EventCompactionStart = "compaction_start"
	EventCompaction      = "compaction"
	// EventQueued says a prompt arrived while a run was active and will be
	// delivered at the next turn boundary (TUI-style steering).
	EventQueued = "queued"
	EventRunEnd = "run_end"
)

Event types carried across the host boundary.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Workspace is the host-selected project directory. Tools receive their own
	// roots from the host; this path is validated and recorded for the host.
	Workspace string
	Model     string
	Provider  string

	// Stream produces the provider stream. A mobile host normally wires the
	// real provider; tests use a scripted one.
	Stream provider.StreamFn
	// APIKey is the static fallback. GetAPIKey, when set, is consulted per
	// request and wins over APIKey, which is how a host refreshes short-lived
	// credentials without rebuilding the session.
	APIKey    string
	GetAPIKey func(context.Context, string) string

	SystemPrompt string
	// History restores an earlier conversation. The slice is copied.
	History agentcore.MessageList

	// Tools is the model-visible catalog. When Batch.Registry is set, the
	// registry's tools replace this field.
	Tools []agentcore.AgentTool
	Batch agenttool.BatchConfig
	// Permissions is the live approval mode shared with the tool set. Nil
	// selects auto mode for hosts that inspect it; the gate is still
	// Batch.BeforeToolCall.
	Permissions *permissions.State

	ThinkingLevel agentcore.ThinkingLevel
	ContextWindow int
	Compaction    compaction.CompactionSettings
	// Checkpoint is the loop's mid-run persistence seam. It runs on the loop
	// goroutine and must not block on user input.
	Checkpoint func(context.Context, *agentcore.AgentContext, bool)
	// ObserveEvent is optional host telemetry. It runs on the loop goroutine
	// and must not block; it receives every loop event in order.
	ObserveEvent func(agentcore.AgentEvent)
	// OnRunEnd is optional persistence/telemetry for the final conversation.
	// It runs on the session goroutine after the loop has stopped and before
	// run_end is published, so a host can save the completed turn before the
	// next run may start.
	OnRunEnd    func(agentcore.MessageList)
	SessionID   string
	EventBuffer int
}

Config is supplied by the mobile host, never by the model. Workspace, Stream and Model are required. Tools and Batch must describe the same catalog; when Registry is set, Tools is replaced with the registry's list so the model cannot be offered a tool the executor does not know. A non-empty tool catalog requires an authorization gate (Batch.BeforeToolCall): the session never manufactures an unconfined-execution grant.

type Event

type Event struct {
	Seq   uint64 `json:"seq"`
	RunID string `json:"runId,omitempty"`
	Type  string `json:"type"`
	Text  string `json:"text,omitempty"`
	// Image and ImageMime carry the first image block of a tool result, e.g.
	// view_image. The tool already bounds the encoded size.
	Image         string          `json:"image,omitempty"`
	ImageMime     string          `json:"imageMime,omitempty"`
	ToolCallID    string          `json:"toolCallId,omitempty"`
	ToolName      string          `json:"toolName,omitempty"`
	Args          json.RawMessage `json:"args,omitempty"`
	ContextTokens int             `json:"contextTokens,omitempty"`
	ContextWindow int             `json:"contextWindow,omitempty"`
	Details       json.RawMessage `json:"details,omitempty"`
	IsError       bool            `json:"isError,omitempty"`
	StopReason    string          `json:"stopReason,omitempty"`
	Error         string          `json:"error,omitempty"`
	Cancelled     bool            `json:"cancelled,omitempty"`
}

Event is one ordered notification. Seq is assigned by the session and increases monotonically for its lifetime, so a host can detect a missed event. Type selects which of the remaining fields are meaningful. All fields are JSON-safe so a JNI/Objective-C bridge can forward the marshaled value without inspecting Go types.

type Session

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

Session owns one mobile conversation. It is safe for concurrent use; at most one run is active at a time. Poll (or PollJSON) is the intended bridge: a host drains events on its own thread rather than blocking a UI thread inside a JNI callback.

func New

func New(cfg Config) (*Session, error)

New validates cfg and builds a session. It does not open the network or run the model.

func (*Session) Cancel

func (s *Session) Cancel()

Cancel asks the active run to stop. It is a no-op when idle. The run_end event reports Cancelled once the loop has drained.

func (*Session) Close

func (s *Session) Close() error

Close cancels any active run, waits for it to drain, and makes the session unusable. It does not close host-owned tools; the host remains responsible for the tool set it built.

func (*Session) PauseForTakeover added in v1.5.104

func (s *Session) PauseForTakeover() error

PauseForTakeover stops admission immediately. TakeoverReady becomes true after every previously admitted tool call has finished.

func (*Session) Permissions

func (s *Session) Permissions() *permissions.State

Permissions returns the live approval state the host shares with its tools.

func (*Session) Poll

func (s *Session) Poll(max int) []Event

Poll removes and returns up to max queued events. max <= 0 returns all.

func (*Session) PollJSON

func (s *Session) PollJSON(max int) (string, error)

PollJSON is Poll for a bridge that forwards JSON instead of translating Go values. An empty queue is "[]", never "".

func (*Session) ResumeAfterTakeover added in v1.5.104

func (s *Session) ResumeAfterTakeover()

ResumeAfterTakeover is a host action, not a model-visible tool.

func (*Session) Running

func (s *Session) Running() bool

Running reports whether a run is active (including its final event not yet polled).

func (*Session) Snapshot

func (s *Session) Snapshot() Snapshot

Snapshot returns a consistent view for a view rebuild.

func (*Session) SnapshotJSON

func (s *Session) SnapshotJSON() (string, error)

SnapshotJSON is Snapshot for a JSON bridge.

func (*Session) Start

func (s *Session) Start(prompt string) error

Start begins one run. It returns immediately; the result arrives as events. A prompt that arrives while a run is active is queued and delivered at the next turn boundary, the same way the TUI steers a running turn.

func (*Session) TakeoverReady added in v1.5.104

func (s *Session) TakeoverReady() bool

TakeoverReady reports whether the pause is latched and no tool is in flight.

func (*Session) Workspace

func (s *Session) Workspace() string

Workspace returns the validated project directory.

type Snapshot

type Snapshot struct {
	Running  bool                  `json:"running"`
	RunID    string                `json:"runId,omitempty"`
	Seq      uint64                `json:"seq"`
	Pending  int                   `json:"pending"`
	Tools    int                   `json:"tools"`
	Messages agentcore.MessageList `json:"messages"`
}

Snapshot is the state a host reads after a missed event or a view rebuild. Messages is a shallow copy of the conversation; message values are immutable once appended by the loop. Tools is the size of the catalog attached to every request: a host can use it to prove the model was offered tools instead of inferring that from a reply which claims otherwise.

Jump to

Keyboard shortcuts

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