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 ¶
- func AllTabs(layout *jsvalue.Object) []*jsvalue.Object
- func FormatTabTitle(raw, panelType string) string
- func MergeMeta(current, patch *jsvalue.Object) (next *jsvalue.Object, changed bool)
- func Panes(layout *jsvalue.Object) []*jsvalue.Object
- func Tabs(pane *jsvalue.Object) []*jsvalue.Object
- func ValidWorkspaceID(id string) bool
- func Value(v any) *any
- type AgentState
- type Broadcaster
- type Config
- type Error
- type LayoutPatch
- type Prompts
- type Provider
- type RatioUpdate
- type Reconciler
- type ReorderItem
- type Store
- func (s *Store) ActiveWorkspaceID() string
- func (s *Store) AddTab(wsID, paneID string, o TabOptions) (*jsvalue.Object, error)
- func (s *Store) AdoptAgentSession(sessionName string, p Provider, sessionID, jsonlPath string) (adopted bool, err error)
- func (s *Store) ClosePane(wsID, paneID string) (*jsvalue.Object, error)
- func (s *Store) CreateGroup(name string) (*jsvalue.Object, error)
- func (s *Store) CreatePane(wsID, cwd string) (paneID string, tab *jsvalue.Object, err error)
- func (s *Store) CreateWorkspace(directory, name, panelType string) (*jsvalue.Object, error)
- func (s *Store) DeleteWorkspace(id string) error
- func (s *Store) FirstPaneTabs(wsID string) (tabs []*jsvalue.Object, activeTabID any)
- func (s *Store) GetLayout(wsID, defaultCwd string) (*jsvalue.Object, error)
- func (s *Store) Init() error
- func (s *Store) InitLayouts() error
- func (s *Store) KillPaneSessions(sessions []string)
- func (s *Store) Layout(wsID string) *jsvalue.Object
- func (s *Store) LayoutChangedByCluster(wsID string)
- func (s *Store) MetaKey() string
- func (s *Store) MoveTab(wsID, tabID, fromPaneID, toPaneID string, toIndex float64) (*jsvalue.Object, error)
- func (s *Store) NewTabID() string
- func (s *Store) PatchLayout(wsID string, p LayoutPatch) (*jsvalue.Object, error)
- func (s *Store) PatchPane(wsID, paneID string, activeTabID *string) (*jsvalue.Object, error)
- func (s *Store) PatchTab(wsID, paneID, tabID string, p TabPatch) (*jsvalue.Object, error)
- func (s *Store) ReconcileTabCwd(sessionName string) error
- func (s *Store) RemoveLayout(wsID string) error
- func (s *Store) RemoveTab(wsID, paneID, tabID string) error
- func (s *Store) RenameGroup(id, name string) (*jsvalue.Object, error)
- func (s *Store) RenameWorkspace(id, name string) (*jsvalue.Object, error)
- func (s *Store) ReorderGroups(ids []string) error
- func (s *Store) ReorderTabs(wsID, paneID string, tabIDs []string) (*jsvalue.Object, error)
- func (s *Store) ReorderWorkspaces(items []ReorderItem) error
- func (s *Store) RestartTab(wsID, paneID, tabID, command string) error
- func (s *Store) RestoreSessions() error
- func (s *Store) SetGroupCollapsed(id string, collapsed bool) error
- func (s *Store) SetTabLaunchApps(sessionName string, ids []string) error
- func (s *Store) SetTabLaunchModel(sessionName, model, effort string) error
- func (s *Store) SetTabLaunchModes(sessionName string, fast bool, permission string) error
- func (s *Store) SetWorkspaceGroup(id string, groupID *string) error
- func (s *Store) SplitPane(wsID, sourcePaneID, orientation, cwd, panelType string) (*jsvalue.Object, error)
- func (s *Store) StateChanged(name string) error
- func (s *Store) StoreRecoveredAgentSession(sessionName string, p Provider, sessionID, jsonlPath string) error
- func (s *Store) TabLaunch(tab *jsvalue.Object) *jsvalue.Object
- func (s *Store) TabLaunchFast(sessionName string) (fast, ok bool)
- func (s *Store) UngroupGroup(id string) error
- func (s *Store) UpdateActive(body *jsvalue.Object) error
- func (s *Store) UpdateTabAgentSessionID(sessionName string, p Provider, sessionID any) error
- func (s *Store) UpdateTabAgentState(sessionName string, p Provider, st AgentState) error
- func (s *Store) UpdateTabAgentSummary(sessionName string, p Provider, summary any) error
- func (s *Store) UpdateTabCliStatus(sessionName string, cliState string, dismissedAt *any, lastEventName any) error
- func (s *Store) UpdateTabLastUserMessage(sessionName string, message any) error
- func (s *Store) UpdateTabMeta(sessionName string, patch *jsvalue.Object) (*jsvalue.Object, error)
- func (s *Store) UpdateWorkspaceDirectories(id string, directories []string) error
- func (s *Store) Workspace(id string) *jsvalue.Object
- func (s *Store) WorkspaceTabs() []WorkspaceTab
- func (s *Store) Workspaces() *jsvalue.Object
- func (s *Store) WorkspacesChangedByCluster()
- type TabOptions
- type TabPatch
- type Tmux
- type WorkspaceTab
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatTabTitle ¶
FormatTabTitle is a tab's display name from its "<command>|<cwd>" title; panelType "" when the tab has none.
func MergeMeta ¶
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 ValidWorkspaceID ¶
ValidWorkspaceID says whether id can name a workspace folder; anything else could leave the data directory.
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 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 ¶
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.
func ProviderOfPanel ¶
ProviderOfPanel is the provider of a tab's panel type, "" for a panel that runs no agent.
type RatioUpdate ¶
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 ¶
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 (*Store) ActiveWorkspaceID ¶
ActiveWorkspaceID is the active workspace, or the first one when the active one is gone; "" when there is none.
func (*Store) AddTab ¶
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 ¶
ClosePane removes a pane (never the last one); focus goes to its neighbour and its sessions end after the write.
func (*Store) CreateGroup ¶
CreateGroup adds a group; a blank name becomes "Group <n>".
func (*Store) CreatePane ¶
CreatePane starts a session for a pane the page is about to add; the layout is not touched.
func (*Store) CreateWorkspace ¶
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 ¶
DeleteWorkspace ends every session of the workspace, removes its layout and takes it off the list.
func (*Store) FirstPaneTabs ¶
FirstPaneTabs are the tabs of the first pane in their order, and its active tab.
func (*Store) GetLayout ¶
GetLayout is a workspace's layout; a workspace without one gets a default layout and its tmux session first.
func (*Store) Init ¶
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 ¶
InitLayouts is Init's first half, the files alone: the workspace list is there, made or migrated.
func (*Store) KillPaneSessions ¶
KillPaneSessions ends the sessions of a pane the page closed outside any workspace; only the runtime's own session names are touched.
func (*Store) LayoutChangedByCluster ¶
LayoutChangedByCluster: the cluster wrote this layout; the next write starts from it, and the reconciler and the pages follow it.
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
NewTabID is an id for a tab yet to be added (TabOptions.ID).
func (*Store) PatchLayout ¶
PatchLayout sets the focus, a split's ratio, equal ratios or the diff settings.
func (*Store) PatchTab ¶
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 ¶
ReconcileTabCwd corrects the stored folder of a tab whose session ended in a folder that no longer exists.
func (*Store) RemoveLayout ¶
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 ¶
RemoveTab ends the tab's session, then removes the tab; an emptied pane goes too unless it is the last one.
func (*Store) RenameGroup ¶
RenameGroup names a group; a blank name changes nothing.
func (*Store) RenameWorkspace ¶
RenameWorkspace names a workspace (the route trims and checks the name).
func (*Store) ReorderGroups ¶
ReorderGroups sets the groups' order; the ids must name every group once.
func (*Store) ReorderTabs ¶
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 ¶
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 ¶
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 ¶
SetGroupCollapsed folds or unfolds a group in the sidebar.
func (*Store) SetTabLaunchApps ¶ added in v0.16.42
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
UngroupGroup removes a group; its workspaces stay, outside any group.
func (*Store) UpdateActive ¶
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 ¶
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 ¶
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 ¶
UpdateTabLastUserMessage stores the tab's last prompt (nil stores null).
func (*Store) UpdateTabMeta ¶
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 ¶
UpdateWorkspaceDirectories replaces a workspace's folders when they changed.
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 ¶
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 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.