remotev1

import remotev1 "pocketone.dev/ext/contracts/remote/v1"

Package remotev1 is the remote/v1 contract: remote control of an app. It's the base every client can rely on (who you are, the app's status, restart, logs); contracts that add an area, like deploy/v1, require it.

A wire contract at /api/_c/remote/v1; whoami at its root is the one fixed path a client starts from. A standalone app gets it from ext/remotes; a multisite host provides it for its instances (so it works for stock images too).

Source: ext/contracts/remote/v1.

Variables

var Contract = extkit.NewContract[Remote]("remote/v1", extkit.One).HostProvidable().JS(extkit.JSHooks, nil).JSTypes(jsTypes)

Contract is remote/v1.

var W = extkit.NewWire(Contract, "remote", func(inner Remote, g *extkit.Guard[Remote]) Remote {
	return &guardRemote{inner, g}
},
	extkit.Op[Remote]{
		Name:   "whoami",
		Desc:   "Says who the credential is, the app's name and URL, and the contracts it offers.",
		Method: "GET",
		Path:   "",
		Scope:  "public",
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Remote, a extkit.Auth, req extkit.OpRequest) (any, error) {
			return impl.Whoami(ctx, a)
		},
	},
	extkit.Op[Remote]{
		Name:   "status",
		Desc:   "Reports whether the app is running, its versions, uptime and mode.",
		Method: "GET",
		Path:   "status",
		Scope:  "remote:read",
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Remote, a extkit.Auth, req extkit.OpRequest) (any, error) {
			return impl.Status(ctx, a)
		},
	},
	extkit.Op[Remote]{
		Name:   "restart",
		Desc:   "Restarts the app; requests in flight finish first.",
		Method: "POST",
		Path:   "restart",
		Scope:  "remote",
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Remote, a extkit.Auth, req extkit.OpRequest) (any, error) {
			return impl.Restart(ctx, a)
		},
	},
	extkit.Op[Remote]{
		Name:   "logs",
		Desc:   "Streams the app's log: the last entries, then new ones as they're written.",
		Method: "GET",
		Path:   "logs",
		Scope:  "remote:read",
		In:     reflect.TypeOf(LogsInput{}),
		Out:    extkit.OutStream,
		Invoke: func(ctx context.Context, impl Remote, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in LogsInput
			if err := extkit.DecodeQuery(req.Query, &in); err != nil {
				return nil, err
			}
			return impl.Logs(ctx, a, in)
		},
	},
)

W is remote/v1's wire side: routes at /api/_c/remote/v1/<op>, action hooks, MCP tools and the client.

Functions

OnLogs

func OnLogs(owner any, fn func(e *extkit.CallEvent, in *LogsInput) error)

OnLogs binds fn to logs calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.

OnRestart

func OnRestart(owner any, fn func(e *extkit.CallEvent) error)

OnRestart binds fn to restart calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse.

OnStatus

func OnStatus(owner any, fn func(e *extkit.CallEvent) error)

OnStatus binds fn to status calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse.

OnWhoami

func OnWhoami(owner any, fn func(e *extkit.CallEvent) error)

OnWhoami binds fn to whoami calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse.

Types

App

type App struct {
	Name string `json:"name"`
	URL  string `json:"url"`
}

App names the app.

Client

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

Client is remote/v1 over HTTP, for the CLI and other apps. It implements Remote; the token it was made with is the credential, so the extkit.Auth argument is ignored.

NewClient

func NewClient(baseURL, token string, hc *http.Client) *Client

NewClient calls remote/v1 on the app at baseURL with token (a PocketBase auth token or an API token).

Client.Logs

func (c *Client) Logs(ctx context.Context, _ extkit.Auth, in LogsInput) (extkit.Stream, error)

Client.Restart

func (c *Client) Restart(ctx context.Context, _ extkit.Auth) (RestartResult, error)

Client.Status

func (c *Client) Status(ctx context.Context, _ extkit.Auth) (Status, error)

Client.Whoami

func (c *Client) Whoami(ctx context.Context, _ extkit.Auth) (Whoami, error)

Identity

type Identity struct {
	Collection string `json:"collection"`
	ID         string `json:"id"`
	Email      string `json:"email,omitempty"`
}

Identity is the record a credential acts as.

LogEntry

type LogEntry struct {
	Time    string         `json:"time"`
	Level   string         `json:"level"`
	Stream  string         `json:"stream,omitempty"` // stdout or stderr, for process output
	Message string         `json:"message"`
	Data    map[string]any `json:"data,omitempty"`
}

LogEntry is one line of an app's log.

LogsInput

type LogsInput struct {
	N int `json:"n"` // entries to replay first (default 100, at most 1000)
}

LogsInput picks how much history a log stream starts with.

Remote

type Remote interface {
	// Whoami says who the credential is, the app's name and URL, and the contracts it offers.
	//
	//op: GET / scope=public
	Whoami(ctx context.Context, a extkit.Auth) (Whoami, error)

	// Status reports whether the app is running, its versions, uptime and mode.
	//
	//op: GET status scope=remote:read
	Status(ctx context.Context, a extkit.Auth) (Status, error)

	// Restart restarts the app; requests in flight finish first.
	//
	//op: POST restart scope=remote
	Restart(ctx context.Context, a extkit.Auth) (RestartResult, error)

	// Logs streams the app's log: the last entries, then new ones as they're written.
	//
	//op: GET logs scope=remote:read
	Logs(ctx context.Context, a extkit.Auth, in LogsInput) (extkit.Stream, error)
}

Remote controls an app from afar.

RestartResult

type RestartResult struct {
	Restarting bool   `json:"restarting"`
	Message    string `json:"message,omitempty"`
}

RestartResult says what a restart did.

Status

type Status struct {
	Running    bool   `json:"running"`
	Version    string `json:"version"`    // the PocketOne build ("0.0.2", "dev+87f81fd2")
	PocketBase string `json:"pocketbase"` // the PocketBase version it's based on
	Uptime     string `json:"uptime"`     // since this app last started ("3h2m5s")
	Dev        bool   `json:"dev"`
}

Status is how the app is doing.

Whoami

type Whoami struct {
	Auth *Identity `json:"auth"` // nil for an anonymous caller
	// Via: "token" (API token), "jwt", or "" (anonymous).
	Via     string   `json:"via"`
	Scopes  []string `json:"scopes"` // a token's scopes; empty: the owner's full rights
	TokenID string   `json:"tokenId,omitempty"`
	App     App      `json:"app"`
	// Contracts are the wire contracts the app offers ("deploy/v1"): each at /api/_c/<id>/.
	Contracts []string `json:"contracts"`
	// Extensions are the extensions running on the app; only for a signed-in caller.
	Extensions []string `json:"extensions,omitempty"`
}

Whoami is who a credential is and what the app offers.