state

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: 23 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 state keeps the runtime's workspace list (`workspaces.json`) and each workspace's layout (`workspaces/<id>/layout.json`): reads with their normalisation, atomic writes in the bytes the previous runtime wrote, the commit of every change to the cluster when the cluster owns the pair's state, the changes the cluster makes, and the tree and tab operations behind the layout and workspace routes.

Documents are jsvalue objects, so fields this package does not model survive every write in their place.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllTabs

func AllTabs(layout *jsvalue.Object) []*jsvalue.Object

AllTabs are a layout's tabs in tree order.

func FormatTabTitle

func FormatTabTitle(raw, panelType string) string

FormatTabTitle is a tab's display name from its "<command>|<cwd>" title; panelType "" when the tab has none.

func MergeMeta

func MergeMeta(current, patch *jsvalue.Object) (next *jsvalue.Object, changed bool)

MergeMeta merges a patch into a tab's metadata: each patch key with a value replaces the stored one, keys outside the known ones are ignored and stored keys the patch does not name stay. changed is false when nothing differs.

func Panes

func Panes(layout *jsvalue.Object) []*jsvalue.Object

Panes are the panes of a layout in tree order.

func Tabs

func Tabs(pane *jsvalue.Object) []*jsvalue.Object

Tabs are a pane's tabs.

func ValidWorkspaceID

func ValidWorkspaceID(id string) bool

ValidWorkspaceID says whether id can name a workspace folder; anything else could leave the data directory.

func Value

func Value(v any) *any

Value is a field of AgentState: a string, or nil for null.

Types

type AgentState

type AgentState struct {
	SessionID, JsonlPath, Summary, LastUserMessage *any
}

AgentState is a conversation update; a nil field is left alone, a pointer to nil stores null.

type Broadcaster

type Broadcaster interface {
	Layout(wsID string)
	Workspaces()
}

Broadcaster tells the pages that a layout or the workspace list changed (sync events `layout` and `workspace`).

type Config

type Config struct {
	DataDir    string
	Tmux       Tmux
	Broadcast  Broadcaster
	Reconciler Reconciler
	Prompts    Prompts
	// AgentTokenHeader carries the token of agent.json on the machine agent's loopback state routes.
	AgentTokenHeader string
	// MetaKey is the tab field holding the platform's own metadata (launch choice, mode).
	MetaKey string
	// Home is the account's home: a new workspace's folder when none is given, the folder of the migrated layout.
	Home string
	// Now and NewID are the clock and the six-character id source; tests make them predictable.
	Now   func() time.Time
	NewID func() string
	// SessionOwned says the tmux session is a chat's, whose owner makes it again when the chat needs it: the restore
	// of sessions at a start makes none for such a tab. nil: no session is.
	SessionOwned func(session string) bool
	// HTTP calls the machine agent; its own timeout is the commit timeout.
	HTTP *http.Client
}

Config is where the stores live and what they tell.

type Error

type Error struct {
	Status  int
	Message string
}

Error is a refusal with the status and message the route answers.

func (*Error) Error

func (e *Error) Error() string

type LayoutPatch

type LayoutPatch struct {
	// ActivePaneID, when set, must name a pane.
	ActivePaneID *string
	// RatioUpdate sets the ratio of the split at Path (child indexes from the root).
	RatioUpdate *RatioUpdate
	Equalize    bool
	// DiffSettings are merged over the stored ones.
	DiffSettings *jsvalue.Object
}

LayoutPatch is the body of PATCH /api/layout.

func ParseLayoutPatch

func ParseLayoutPatch(body *jsvalue.Object) LayoutPatch

ParseLayoutPatch reads the body the way the route does: present fields are applied, truthiness decides.

type Prompts

type Prompts interface {
	WriteWorkspacePrompts(ws *jsvalue.Object)
}

Prompts writes the agent CLIs' per-workspace prompt files after a workspace is created, renamed or changes folders.

type Provider

type Provider string

Provider names the agent CLI whose conversation a tab holds.

const (
	Claude      Provider = "claude"
	Codex       Provider = "codex"
	Antigravity Provider = "antigravity"
)

func ProviderOfPanel

func ProviderOfPanel(panelType any) Provider

ProviderOfPanel is the provider of a tab's panel type, "" for a panel that runs no agent.

func (Provider) SessionID

func (p Provider) SessionID(tab *jsvalue.Object) any

SessionID is the conversation id a tab of this provider holds, nil when none.

func (Provider) WriteSessionID

func (p Provider) WriteSessionID(tab *jsvalue.Object, sessionID any)

WriteSessionID stores a conversation id in a tab object the caller holds (not on disk).

type RatioUpdate

type RatioUpdate struct {
	Path  []int
	Ratio any
}

type Reconciler

type Reconciler interface {
	ReconcileWorkspaceTabs(wsID string, tabIDs []string)
	RemoveWorkspaceTabs(wsID string)
}

Reconciler keeps the tab statuses in step with the layouts: after a write, the tabs a workspace still has; after a layout is removed, none.

type ReorderItem

type ReorderItem struct {
	ID       string
	GroupID  *string
	HasGroup bool
}

ReorderItem is one workspace of a reorder and, when HasGroup, the group it moves to (nil: none).

type Store

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

Store holds the locks and caches of both stores. Use one per data directory.

func New

func New(cfg Config) *Store

New opens the stores of cfg.DataDir.

func (*Store) ActiveWorkspaceID

func (s *Store) ActiveWorkspaceID() string

ActiveWorkspaceID is the active workspace, or the first one when the active one is gone; "" when there is none.

func (*Store) AddTab

func (s *Store) AddTab(wsID, paneID string, o TabOptions) (*jsvalue.Object, error)

AddTab appends a tab to a pane, starts its session (not for a web browser) and makes it the pane's active tab.

func (*Store) AdoptAgentSession

func (s *Store) AdoptAgentSession(sessionName string, p Provider, sessionID, jsonlPath string) (adopted bool, err error)

AdoptAgentSession gives a tab that keeps no session of its agent the one its chat's record holds. A resume reads the tab, and a tab whose agent was cut before a turn of it ended has neither a transcript path nor a history entry to recover the id from, so its chat could not be resumed. A tab that keeps a session keeps it; adopted says the tab was written.

func (*Store) ClosePane

func (s *Store) ClosePane(wsID, paneID string) (*jsvalue.Object, error)

ClosePane removes a pane (never the last one); focus goes to its neighbour and its sessions end after the write.

func (*Store) CreateGroup

func (s *Store) CreateGroup(name string) (*jsvalue.Object, error)

CreateGroup adds a group; a blank name becomes "Group <n>".

func (*Store) CreatePane

func (s *Store) CreatePane(wsID, cwd string) (paneID string, tab *jsvalue.Object, err error)

CreatePane starts a session for a pane the page is about to add; the layout is not touched.

func (*Store) CreateWorkspace

func (s *Store) CreateWorkspace(directory, name, panelType string) (*jsvalue.Object, error)

CreateWorkspace adds a workspace for a folder with a default layout (its first tab of panelType, if given, and that tab's session).

func (*Store) DeleteWorkspace

func (s *Store) DeleteWorkspace(id string) error

DeleteWorkspace ends every session of the workspace, removes its layout and takes it off the list.

func (*Store) FirstPaneTabs

func (s *Store) FirstPaneTabs(wsID string) (tabs []*jsvalue.Object, activeTabID any)

FirstPaneTabs are the tabs of the first pane in their order, and its active tab.

func (*Store) GetLayout

func (s *Store) GetLayout(wsID, defaultCwd string) (*jsvalue.Object, error)

GetLayout is a workspace's layout; a workspace without one gets a default layout and its tmux session first.

func (*Store) Init

func (s *Store) Init() error

Init prepares the stores at start and after a tmux reset: the workspace list (migrated from the oldest single-layout files when there is none), a default layout for a workspace without one, and each layout checked against tmux: the missing session of an agent tab whose agent was up is started again and a session of the workspace that no tab names becomes a tab.

func (*Store) InitLayouts

func (s *Store) InitLayouts() error

InitLayouts is Init's first half, the files alone: the workspace list is there, made or migrated.

func (*Store) KillPaneSessions

func (s *Store) KillPaneSessions(sessions []string)

KillPaneSessions ends the sessions of a pane the page closed outside any workspace; only the runtime's own session names are touched.

func (*Store) Layout

func (s *Store) Layout(wsID string) *jsvalue.Object

Layout is a workspace's layout as stored, nil when there is none.

func (*Store) LayoutChangedByCluster

func (s *Store) LayoutChangedByCluster(wsID string)

LayoutChangedByCluster: the cluster wrote this layout; the next write starts from it, and the reconciler and the pages follow it.

func (*Store) MetaKey

func (s *Store) MetaKey() string

MetaKey is the tab field holding the platform's metadata.

func (*Store) MoveTab

func (s *Store) MoveTab(wsID, tabID, fromPaneID, toPaneID string, toIndex float64) (*jsvalue.Object, error)

MoveTab moves a tab to another pane (or another place in its own) and focuses it there.

func (*Store) NewTabID added in v0.16.154

func (s *Store) NewTabID() string

NewTabID is an id for a tab yet to be added (TabOptions.ID).

func (*Store) PatchLayout

func (s *Store) PatchLayout(wsID string, p LayoutPatch) (*jsvalue.Object, error)

PatchLayout sets the focus, a split's ratio, equal ratios or the diff settings.

func (*Store) PatchPane

func (s *Store) PatchPane(wsID, paneID string, activeTabID *string) (*jsvalue.Object, error)

PatchPane sets a pane's active tab.

func (*Store) PatchTab

func (s *Store) PatchTab(wsID, paneID, tabID string, p TabPatch) (*jsvalue.Object, error)

PatchTab sets the page's fields of a tab. A folder is taken from the session itself when it can be read, and a title is made from the session's program and folder, whatever the page sent.

func (*Store) ReconcileTabCwd

func (s *Store) ReconcileTabCwd(sessionName string) error

ReconcileTabCwd corrects the stored folder of a tab whose session ended in a folder that no longer exists.

func (*Store) RemoveLayout

func (s *Store) RemoveLayout(wsID string) error

RemoveLayout deletes a workspace's layout folder (in the cluster first when it owns the state) and tells the reconciler and the pages.

func (*Store) RemoveTab

func (s *Store) RemoveTab(wsID, paneID, tabID string) error

RemoveTab ends the tab's session, then removes the tab; an emptied pane goes too unless it is the last one.

func (*Store) RenameGroup

func (s *Store) RenameGroup(id, name string) (*jsvalue.Object, error)

RenameGroup names a group; a blank name changes nothing.

func (*Store) RenameWorkspace

func (s *Store) RenameWorkspace(id, name string) (*jsvalue.Object, error)

RenameWorkspace names a workspace (the route trims and checks the name).

func (*Store) ReorderGroups

func (s *Store) ReorderGroups(ids []string) error

ReorderGroups sets the groups' order; the ids must name every group once.

func (*Store) ReorderTabs

func (s *Store) ReorderTabs(wsID, paneID string, tabIDs []string) (*jsvalue.Object, error)

ReorderTabs puts a pane's tabs in the given order.

func (*Store) ReorderWorkspaces

func (s *Store) ReorderWorkspaces(items []ReorderItem) error

ReorderWorkspaces sets the workspaces' order (and groups); the items must name every workspace once.

func (*Store) RestartTab

func (s *Store) RestartTab(wsID, paneID, tabID, command string) error

RestartTab starts a tab's session again when it is gone, in its folder or the nearest one that still exists; with the folder lost the stored folder follows and the remembered command is dropped instead of run.

func (*Store) RestoreSessions

func (s *Store) RestoreSessions() error

RestoreSessions is Init's second half: each layout checked against tmux. After a reboot it starts again the sessions of the agent tabs whose agents were up (see agentWasUp); the runtime's start does the rest of its work first.

func (*Store) SetGroupCollapsed

func (s *Store) SetGroupCollapsed(id string, collapsed bool) error

SetGroupCollapsed folds or unfolds a group in the sidebar.

func (*Store) SetTabLaunchApps added in v0.16.42

func (s *Store) SetTabLaunchApps(sessionName string, ids []string) error

SetTabLaunchApps writes the apps a tab's next launch turns on (launch.apps of its metadata), keeping the rest of the launch; an empty list removes them. A tab with no launch choice gets one of no model and no effort.

func (*Store) SetTabLaunchModel

func (s *Store) SetTabLaunchModel(sessionName, model, effort string) error

SetTabLaunchModel writes the model and the effort a tab's next launch starts with (launch.model and launch.effort of its metadata), keeping the rest of the launch; "" leaves the CLI's own.

func (*Store) SetTabLaunchModes

func (s *Store) SetTabLaunchModes(sessionName string, fast bool, permission string) error

SetTabLaunchModes writes the fast and permission modes a tab's next launch starts in (launch.fast and launch.permission of its metadata), keeping the rest of the launch; off and "" remove them. A tab with no launch choice gets one of no model and no effort.

func (*Store) SetWorkspaceGroup

func (s *Store) SetWorkspaceGroup(id string, groupID *string) error

SetWorkspaceGroup puts a workspace in a group; nil, or a group that does not exist, takes it out of any.

func (*Store) SplitPane

func (s *Store) SplitPane(wsID, sourcePaneID, orientation, cwd, panelType string) (*jsvalue.Object, error)

SplitPane splits a pane in two: the new pane (with a new tab and its session, none for a web browser) goes after it, takes the focus, and the ratios stay equal if they were.

func (*Store) StateChanged

func (s *Store) StateChanged(name string) error

StateChanged takes the agent's notice that the cluster wrote a state file (the state-changed route).

func (*Store) StoreRecoveredAgentSession

func (s *Store) StoreRecoveredAgentSession(sessionName string, p Provider, sessionID, jsonlPath string) error

StoreRecoveredAgentSession stores a session found on disk. The stored id itself is compared: a Claude tab reads its id back from the transcript path, so a tab whose id is null would look up to date.

func (*Store) TabLaunch

func (s *Store) TabLaunch(tab *jsvalue.Object) *jsvalue.Object

TabLaunch is the launch choice a tab's metadata remembers: an object of a nullable-string model and effort (and the backend profile's id, when the chat runs on one), nil for none (or a malformed one).

func (*Store) TabLaunchFast

func (s *Store) TabLaunchFast(sessionName string) (fast, ok bool)

TabLaunchFast says whether the tab of a session starts in fast mode (launch.fast of its metadata): what a GPT chat's relay reads on each request. ok is false when no tab has the session.

func (*Store) UngroupGroup

func (s *Store) UngroupGroup(id string) error

UngroupGroup removes a group; its workspaces stay, outside any group.

func (*Store) UpdateActive

func (s *Store) UpdateActive(body *jsvalue.Object) error

UpdateActive stores the active workspace and the sidebar's state from the body of PATCH /api/workspace/active, each field present as given.

func (*Store) UpdateTabAgentSessionID

func (s *Store) UpdateTabAgentSessionID(sessionName string, p Provider, sessionID any) error

UpdateTabAgentSessionID stores a tab's conversation id (nil clears it).

func (*Store) UpdateTabAgentState

func (s *Store) UpdateTabAgentState(sessionName string, p Provider, st AgentState) error

UpdateTabAgentState stores what changed of a tab's conversation and its last prompt.

func (*Store) UpdateTabAgentSummary

func (s *Store) UpdateTabAgentSummary(sessionName string, p Provider, summary any) error

UpdateTabAgentSummary stores a tab's conversation summary.

func (*Store) UpdateTabCliStatus

func (s *Store) UpdateTabCliStatus(sessionName string, cliState string, dismissedAt *any, lastEventName any) error

UpdateTabCliStatus persists the tab's agent state, when it was dismissed (nil: absent) and its last hook event (stored as null when absent).

func (*Store) UpdateTabLastUserMessage

func (s *Store) UpdateTabLastUserMessage(sessionName string, message any) error

UpdateTabLastUserMessage stores the tab's last prompt (nil stores null).

func (*Store) UpdateTabMeta

func (s *Store) UpdateTabMeta(sessionName string, patch *jsvalue.Object) (*jsvalue.Object, error)

UpdateTabMeta merges a patch into the platform metadata of the tab running sessionName and answers the stored metadata; nil when that tab does not exist.

func (*Store) UpdateWorkspaceDirectories

func (s *Store) UpdateWorkspaceDirectories(id string, directories []string) error

UpdateWorkspaceDirectories replaces a workspace's folders when they changed.

func (*Store) Workspace

func (s *Store) Workspace(id string) *jsvalue.Object

Workspace is one workspace, nil when there is no such one.

func (*Store) WorkspaceTabs

func (s *Store) WorkspaceTabs() []WorkspaceTab

WorkspaceTabs is every tab of every workspace, in list and tree order (read without locks, as a lookup).

func (*Store) Workspaces

func (s *Store) Workspaces() *jsvalue.Object

Workspaces is GET /api/workspace: the list, groups, active workspace and sidebar.

func (*Store) WorkspacesChangedByCluster

func (s *Store) WorkspacesChangedByCluster()

WorkspacesChangedByCluster: the cluster wrote the workspace list.

type TabOptions

type TabOptions struct {
	Name, Cwd, PanelType, Command string
	// ID is the tab's id when the caller chose it ahead (NewTabID): a launch line that names its chat needs it before the
	// tab exists. "" makes a new one.
	ID string
	// Meta is the tab's metadata from its first write (the keys MergeMeta knows), so that no reader of the layout sees
	// the tab without it.
	Meta *jsvalue.Object
}

TabOptions are a new tab's name, folder, panel type and the command typed into its shell.

type TabPatch

type TabPatch map[string]any

TabPatch is the body of PATCH …/tabs/<id>: the fields present, by name.

func ParseTabPatch

func ParseTabPatch(body *jsvalue.Object) (TabPatch, error)

ParseTabPatch checks the body as the route does.

type Tmux

type Tmux interface {
	CreateSession(name string, cols, rows int, cwd string) error
	KillSession(name string)
	HasSession(name string) bool
	SendKeys(name, line string) error
	ListSessions() []string
	Names() tmux.Names
	SessionName(wsID, paneID, tabID string) string
	ParseSessionName(name string) (wsID, paneID, tabID string, ok bool)
	ResolveExistingDir(preferred string) string
	// SessionCwd is the folder of the session's shell, else tmux's idea of the pane, "" when neither is known.
	SessionCwd(name string) string
	PaneCurrentCommand(name string) string
}

Tmux is what the stores ask of tmux. FromTmux adapts the runtime's own.

func FromTmux

func FromTmux(t *tmux.Tmux) Tmux

FromTmux is the runtime's tmux layer as the stores use it.

type WorkspaceTab

type WorkspaceTab struct {
	Workspace *jsvalue.Object
	Tab       *jsvalue.Object
}

WorkspaceTab is a tab with the workspace it belongs to.

Jump to

Keyboard shortcuts

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