clientapi

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 8 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 clientapi is hopsesh's API for other programs on the same machine, run by the same user (a phone companion's host, an editor): what sessions there are, where, and moving them. It is versioned on its own, separately from the peer protocol hopsesh speaks to hopsesh on other machines, which stays internal.

A connection carries JSON objects, one per line. The client sends requests; hopsesh answers each one with a response carrying the request's id, and sends events (a watched catalog's changes, the steps of a move) that name the request they belong to. Requests run concurrently, so responses can come in any order. The first request is hello.

docs/client-api.md is the contract.

Index

Constants

View Source
const (
	API    = 1
	MinAPI = 1
)

API is the version this hopsesh speaks; MinAPI the oldest it still answers.

View Source
const (
	FeatureCatalogWatch = "catalog.watch"  // catalog changes as events
	FeaturePush         = "plan.push"      // plan and apply a move from here to another machine
	FeatureOwners       = "owners"         // reads other programs' owner records (owners/<name>/<pid>.json)
	FeatureTerminal     = "terminal.open"  // runs a session in a terminal hopsesh owns (needs hopsesh serve)
	FeatureNotes        = "plan.notes"     // plans carry notes: each warning and blocker with its fix and actions
	FeatureSetup        = "machines.setup" // sets hopsesh up on a machine reached over SSH
)

Features are what this hopsesh answers beyond API 1's required methods. A client checks for a feature instead of comparing versions.

View Source
const (
	MethodHello          = "hello"
	MethodCatalog        = "catalog"         // the sessions and machines, once
	MethodCatalogWatch   = "catalog.watch"   // the same, then catalog.changed events
	MethodCatalogUnwatch = "catalog.unwatch" // stops a watch
	MethodMachines       = "machines"        // the machines and what hopsesh can do on each
	MethodPlan           = "plan"            // plan a move or continuation; nothing changes
	MethodApply          = "apply"           // carry out a plan; step events, then a receipt
	MethodCancel         = "cancel"          // drop a plan that was not applied
	MethodUndo           = "undo"            // reverse an applied plan by its journal id
	MethodTerminalOpen   = "terminal.open"   // run a session here in a terminal hopsesh owns
	MethodSetup          = "machines.setup"  // set hopsesh up on a machine; step events, then a report
)

Methods.

View Source
const (
	EventCatalogChanged = "catalog.changed"
	EventStep           = "apply.step"
	EventSetupStep      = "setup.step"
)

Events.

View Source
const (
	CodeAPI         = "api"         // no API version both ends speak
	CodeHello       = "hello"       // a request before hello
	CodeUnknown     = "unknown"     // a method this hopsesh does not know
	CodeBadRequest  = "bad-request" // the request's params could not be read
	CodeNotFound    = "not-found"   // no such session, machine, plan or journal
	CodeAmbiguous   = "ambiguous"   // several sessions match
	CodeBlocked     = "blocked"     // the plan has blockers; it cannot be applied
	CodeOwned       = "owned"       // another program runs the session; it must stop it first
	CodeStale       = "stale"       // what the plan was made from changed; plan again
	CodeNoLogin     = "nologin"     // needs hopsesh serve in the login session
	CodeUnsupported = "unsupported" // this hopsesh cannot do it here
	CodeFailed      = "failed"      // anything else
)

Error codes.

View Source
const (
	AttentionOldCopyOpen = "old-copy-open" // a copy a move left behind is open: it may be continued twice
	AttentionOldCopyUsed = "old-copy-used" // a copy a move left behind was used after the move
	AttentionMachine     = "machine"       // a machine can't be read, or hopsesh there needs something
)

Attention kinds.

View Source
const (
	NoteBlocker = "blocker" // the plan cannot be applied while it stands
	NoteWarning = "warning" // the user should know before applying
)

Note levels.

View Source
const (
	// ActionPlan plans again with the plan's params changed by Params; Ask names params the
	// user fills in first (such as "dir").
	ActionPlan = "plan"
	// ActionSetup sets hopsesh up on Machine (machines.setup), then plans again.
	ActionSetup = "setup"
)

Action kinds.

Variables

This section is empty.

Functions

func Agree

func Agree(p HelloParams) int

Agree is the API both ends speak (0 when none).

func Serve

func Serve(ctx context.Context, r io.Reader, w io.Writer, h Handler) error

Serve answers requests on r until r ends or ctx is done. Before hello succeeds, only hello is answered.

Types

type ApplyParams

type ApplyParams struct {
	Plan string `json:"plan"`
	// OwnerStopped says the program in the plan's owner stopped the session (or it was
	// asked to and confirmed); without it, a plan with an owner is refused.
	OwnerStopped bool `json:"ownerStopped,omitempty"`
}

ApplyParams applies a plan made on this connection.

type Attention

type Attention struct {
	ID      string `json:"id"`
	Kind    string `json:"kind"`
	Machine string `json:"machine,omitempty"`
	Session string `json:"session,omitempty"` // the session's ref, when it is about one
	Title   string `json:"title"`
	Text    string `json:"text"`
	Fix     string `json:"fix,omitempty"`
}

Attention is one thing that needs the user. ID stays the same while it does.

type Catalog

type Catalog struct {
	Sessions []Session `json:"sessions"`
	Machines []Machine `json:"machines"`
	// Attention is what needs the user, from what hopsesh knows (an inbox's items).
	Attention []Attention `json:"attention"`
	// Complete is false while some machines are still being read (a watch sends the rest
	// when they are).
	Complete bool      `json:"complete"`
	At       time.Time `json:"at"`
}

Catalog is the sessions and machines hopsesh knows.

type CatalogParams

type CatalogParams struct {
	// Local leaves the other machines and the clouds out (fast: no network).
	Local bool `json:"local,omitempty"`
	// Machines limits it to these machines ("" or none: every allowed one).
	Machines []string `json:"machines,omitempty"`
	// Cached answers from hopsesh's last catalog at once, when it has one, instead of
	// reading the machines first (rows then say cached).
	Cached bool `json:"cached,omitempty"`
}

CatalogParams chooses what a catalog covers.

type Client

type Client struct {
	Name    string `json:"name"`
	Version string `json:"version,omitempty"`
}

Client names the program calling.

type Conn

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

Conn is a connection's sending side, safe for concurrent use.

func (*Conn) Event

func (c *Conn) Event(id json.RawMessage, name string, data any) error

Event sends an event for the request id.

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Fix     string `json:"fix,omitempty"`
	// Journal, on a failed apply, records what it did before it failed: undo reverses it.
	Journal string `json:"journal,omitempty"`
}

Error is a failed request. Fix, when there is one, is what the user can do about it.

func Errorf

func Errorf(code, format string, args ...any) *Error

Errorf makes an Error with a code.

func (*Error) Error

func (e *Error) Error() string

type Event

type Event struct {
	Event string          `json:"event"`
	For   json.RawMessage `json:"for,omitempty"`
	Data  any             `json:"data"`
}

Event is something hopsesh says on its own: For is the id of the request it belongs to.

type Handler

type Handler func(ctx context.Context, c *Conn, id json.RawMessage, method string, params json.RawMessage) (any, error)

Handler answers one request. It may send events on c until it returns; a long-lived subscription keeps its own goroutine and stops when ctx ends.

type HelloParams

type HelloParams struct {
	API    int    `json:"api"`
	MinAPI int    `json:"minApi,omitempty"`
	Client Client `json:"client"`
}

HelloParams is the client's hello.

type HelloResult

type HelloResult struct {
	API      int      `json:"api"`
	MinAPI   int      `json:"minApi"`
	Version  string   `json:"version"`
	Endpoint string   `json:"endpoint,omitempty"` // this machine's persistent identity
	Machine  string   `json:"machine"`
	OS       string   `json:"os"`
	Login    bool     `json:"login"` // hopsesh serve answers (the login session)
	Features []string `json:"features"`
}

HelloResult describes this hopsesh.

type Hop

type Hop struct {
	Operation string    `json:"operation"`
	Kind      string    `json:"kind"` // moved, continued, fetched, handoff, returned, continuation
	Machine   string    `json:"machine,omitempty"`
	Agent     string    `json:"agent,omitempty"`
	At        time.Time `json:"at,omitempty"`
}

Hop is one move or continuation in a session's lineage.

type Live

type Live struct {
	// State is "live", "ended" or "unknown".
	State  string `json:"state"`
	Status string `json:"status,omitempty"` // the agent's words: "busy", "waiting for input"
	PIDs   []int  `json:"pids,omitempty"`
	App    bool   `json:"app,omitempty"` // in the agent's desktop app
	// RemoteControl is "on", "off" or "" (the agent doesn't say); RemoteURL its page.
	RemoteControl string `json:"remoteControl,omitempty"`
	RemoteURL     string `json:"remoteUrl,omitempty"`
}

Live is whether the session is open now.

type Machine

type Machine struct {
	Name     string `json:"name"`
	Endpoint string `json:"endpoint,omitempty"`
	Local    bool   `json:"local,omitempty"`
	OS       string `json:"os,omitempty"`
	// Status is "ok", or why it can't be read now ("unreachable", "error", ...).
	Status  string `json:"status"`
	Problem string `json:"problem,omitempty"`
	Fix     string `json:"fix,omitempty"`
	Hopsesh string `json:"hopsesh,omitempty"` // the version there ("" when none was found)
	// Presence is how hopsesh works with it: "login" (hopsesh serve answers there),
	// "ssh" (over SSH only), or "none".
	Presence string    `json:"presence,omitempty"`
	Agents   []string  `json:"agents,omitempty"` // the agents found there
	Checked  time.Time `json:"checked,omitempty"`
}

Machine is a machine hopsesh reaches.

type Owner

type Owner struct {
	Name string `json:"name"`
	PID  int    `json:"pid"`
	// Stop says how to ask it to stop the session (the record's own words).
	Stop string `json:"stop,omitempty"`
}

Owner is another program that runs a session.

type Plan

type Plan struct {
	ID       string    `json:"id"` // apply or cancel it by this, on this connection
	Kind     string    `json:"kind"`
	Title    string    `json:"title"`
	Agent    string    `json:"agent"`
	From     PlanSide  `json:"from"`
	To       PlanSide  `json:"to"`
	Files    int       `json:"files"`
	Bytes    int64     `json:"bytes"`
	Repo     *PlanRepo `json:"repo,omitempty"`
	Conflict string    `json:"conflict,omitempty"`
	Warnings []string  `json:"warnings,omitempty"` // each note's text, for warnings
	Blockers []string  `json:"blockers,omitempty"` // each note's text, for blockers
	// Notes are the warnings and blockers with what the user can do about each.
	Notes     []PlanNote `json:"notes,omitempty"`
	NoWork    bool       `json:"noWork,omitempty"` // it is there already
	Protected bool       `json:"protected"`        // the original will be blocked from going on
	Owner     *Owner     `json:"owner,omitempty"`  // another program runs the original: it must stop it first
	Live      bool       `json:"live,omitempty"`   // the original is open now
}

Plan is what a move or continuation would do. Nothing has changed yet.

type PlanAction

type PlanAction struct {
	Kind  string `json:"kind"`
	Label string `json:"label"` // a button's words
	// Params change the plan's params, for an ActionPlan.
	Params map[string]any `json:"params,omitempty"`
	// Ask names the params the user chooses first, for an ActionPlan.
	Ask []string `json:"ask,omitempty"`
	// Machine is the machine to set hopsesh up on, for an ActionSetup.
	Machine string `json:"machine,omitempty"`
}

PlanAction is something a client can offer to resolve a note.

type PlanNote

type PlanNote struct {
	Level string `json:"level"`
	// Text says what is so, in words for anyone (no command-line flags).
	Text string `json:"text"`
	// Fix is what the user can do about it, in words, when there is something.
	Fix string `json:"fix,omitempty"`
	// Actions are what a client can offer for it, best first.
	Actions []PlanAction `json:"actions,omitempty"`
}

PlanNote is one warning or blocker of a plan.

type PlanParams

type PlanParams struct {
	// Session is a session's ref, from the catalog.
	Session string `json:"session"`
	// To is the machine it goes to: "" (or this machine's name) brings it here; another
	// machine's name sends it there, which only a session on this machine can be.
	To string `json:"to,omitempty"`
	// Agent continues it in another agent ("" keeps its own).
	Agent string `json:"agent,omitempty"`
	// Dir is the folder it goes to there ("" lets hopsesh choose: the same repository,
	// found or cloned).
	Dir string `json:"dir,omitempty"`
	// Fork keeps the original usable instead of blocking it.
	Fork bool `json:"fork,omitempty"`
	// Clone clones the repository there when it isn't (into hopsesh's repositories folder).
	Clone bool `json:"clone,omitempty"`
	// StopOpen lets hopsesh quit a copy of the session that is open where it goes.
	StopOpen bool `json:"stopOpen,omitempty"`
	// Conflict chooses what happens to a copy already there: "replace" (kept for undo) or
	// "keep-both" (this one comes in as a separate session).
	Conflict string `json:"conflict,omitempty"`
	// Worktree is "create" (work in a worktree on the session's branch), "main" (the main
	// checkout as it is) or "" (a worktree only when the session ran in one).
	Worktree string `json:"worktree,omitempty"`
	// CarryRules adds the user's instructions for every project of the session's agent to
	// the briefing, when it is continued in another agent.
	CarryRules bool `json:"carryRules,omitempty"`
}

PlanParams asks for a plan.

type PlanRepo

type PlanRepo struct {
	Action string `json:"action"` // use, clone, worktree, none ...
	Path   string `json:"path,omitempty"`
	Branch string `json:"branch,omitempty"`
	Remote string `json:"remote,omitempty"`
}

PlanRepo is what happens to the code there.

type PlanSide

type PlanSide struct {
	Machine string     `json:"machine"`
	Key     SessionKey `json:"key"`
	Dir     string     `json:"dir,omitempty"`
	Account string     `json:"account,omitempty"`
}

PlanSide is one end of a plan.

type Receipt

type Receipt struct {
	Plan string `json:"plan"`
	// Journal undoes it (MethodUndo); "" when nothing was changed.
	Journal string     `json:"journal,omitempty"`
	Machine string     `json:"machine"` // where the session is now
	Key     SessionKey `json:"key"`
	Ref     string     `json:"ref"`
	Dir     string     `json:"dir,omitempty"`
	// Command resumes it there, as the user would type it.
	Command  string   `json:"command,omitempty"`
	Files    int      `json:"files"`
	Bytes    int64    `json:"bytes"`
	Cloned   bool     `json:"cloned,omitempty"`
	Worktree string   `json:"worktree,omitempty"`
	Pushed   string   `json:"pushed,omitempty"`  // the branch pushed first
	Stopped  bool     `json:"stopped,omitempty"` // the original was stopped
	Warnings []string `json:"warnings,omitempty"`
}

Receipt is what an apply did.

type Request

type Request struct {
	ID     json.RawMessage `json:"id"`
	Method string          `json:"method"`
	Params json.RawMessage `json:"params,omitempty"`
}

Request is one call.

type Response

type Response struct {
	ID     json.RawMessage `json:"id"`
	Result any             `json:"result,omitempty"`
	Error  *Error          `json:"error,omitempty"`
}

Response answers a request: Result or Error.

type Session

type Session struct {
	// Ref names it in plan and terminal.open: "<machine>:<agent>[@profile]/<session>".
	Ref       string     `json:"ref"`
	Key       SessionKey `json:"key"`
	Machine   string     `json:"machine"`            // hopsesh's name for the machine, or the cloud
	Endpoint  string     `json:"endpoint,omitempty"` // the machine's identity ("" for a cloud, or not known yet)
	Cloud     bool       `json:"cloud,omitempty"`
	AgentName string     `json:"agentName"`
	Title     string     `json:"title"`
	Dir       string     `json:"dir,omitempty"` // its project folder there
	Branch    string     `json:"branch,omitempty"`
	Activity  time.Time  `json:"activity,omitempty"`
	Live      Live       `json:"live"`
	// Cached: from hopsesh's last catalog, not read just now. Plan reads it again.
	Cached bool `json:"cached,omitempty"`
	// Replica is this copy in the session's lineage: it follows the session across
	// moves, while Key can change.
	Replica string `json:"replica,omitempty"`
	// Hop is how this copy came to be here, when it came by a move or a continuation.
	Hop *Hop `json:"hop,omitempty"`
	// MovedTo is set on a copy a move left behind: the session goes on there. Continuing
	// this copy is refused unless the user insists.
	MovedTo *Hop `json:"movedTo,omitempty"`
	// Owner is the program running it, from its owner record (see docs/client-api.md).
	Owner *Owner `json:"owner,omitempty"`
}

Session is one copy of a session, where it is.

type SessionKey

type SessionKey struct {
	Agent   string `json:"agent"`
	Profile string `json:"profile,omitempty"` // the account folder there ("" its default)
	Session string `json:"session"`
}

SessionKey names a session in its agent: unique together with the machine's endpoint.

type Setup

type Setup struct {
	Machine string `json:"machine"`
	Version string `json:"version"`
	// Login: hopsesh answers from the login session there now.
	Login bool        `json:"login"`
	Steps []SetupStep `json:"steps"`
}

Setup reports a machines.setup.

type SetupParams

type SetupParams struct {
	Machine string `json:"machine"`
}

SetupParams sets hopsesh up on a machine hopsesh reaches over SSH: it installs this hopsesh there and has it start at each login there. Nothing needs administrator rights.

type SetupStep

type SetupStep struct {
	Machine string `json:"machine"`
	Name    string `json:"name"`
	State   string `json:"state"`
	Detail  string `json:"detail,omitempty"`
}

SetupStep is a machines.setup step event. State is done, skipped, waiting or failed.

type Step

type Step struct {
	Plan  string `json:"plan"`
	N     int    `json:"n"`
	Title string `json:"title"`
	State string `json:"state"`
}

Step is an apply.step event. Steps are numbered from 1 in the order apply takes them; which steps a move needs is known only as it goes. Each starts "running" and ends "done", or "failed" when apply fails in it.

type Terminal

type Terminal struct {
	ID      string `json:"id"`
	Title   string `json:"title"`
	Dir     string `json:"dir,omitempty"`
	State   string `json:"state"`
	Started string `json:"started,omitempty"`
}

Terminal is a terminal hopsesh runs (attach with hopsesh terminals attach <id>).

type TerminalParams

type TerminalParams struct {
	Session string `json:"session"`
	Cols    int    `json:"cols,omitempty"`
	Rows    int    `json:"rows,omitempty"`
	// Anyway continues a copy a move left behind; the user confirmed it.
	Anyway bool `json:"anyway,omitempty"`
}

TerminalParams runs a session on this machine in a terminal hopsesh owns.

type UndoParams

type UndoParams struct {
	Journal string `json:"journal"`
	// Force undoes it even when the session was used since (that work is lost).
	Force bool `json:"force,omitempty"`
}

UndoParams reverses an applied plan.

type Undone

type Undone struct {
	Journal string `json:"journal"`
	Title   string `json:"title,omitempty"`
}

Undone reports an undo.

type Unwatch

type Unwatch struct {
	Watch json.RawMessage `json:"watch"`
}

Unwatch stops the watch a catalog.watch request started.

type WatchParams

type WatchParams struct {
	CatalogParams
}

WatchParams starts a catalog watch.

Jump to

Keyboard shortcuts

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