Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package session keeps native observations alive while actions hold leases.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lease added in v0.3.0

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

Lease keeps a snapshot and its AX handles alive for an action. Obtain one through Acquire or Take; its zero value is not usable. Do not copy a Lease. A lease guarantees lifetime, not UI freshness or permission to act.

func (*Lease) Close added in v0.3.0

func (l *Lease) Close() error

Close releases the lease. Only the last owner closes the snapshot and receives its close error. Repeated calls do nothing and return nil. Do not overlap Close with use of a handle previously returned by Resolve.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/axuiautomation"
	"github.com/tmc/axmcp/internal/computeruse"
	"github.com/tmc/axmcp/internal/computeruse/session"
)

// exampleSnapshot supplies metadata without opening a native application.
type exampleSnapshot struct{}

func (exampleSnapshot) State() computeruse.AppState {
	return computeruse.AppState{App: computeruse.AppInfo{BundleID: "com.example.app"}}
}
func (exampleSnapshot) Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error) {
	return nil, computeruse.ElementNode{Index: index, Title: "Save"}, nil
}
func (exampleSnapshot) Close() error { return nil }

func main() {
	store := session.NewStore()
	defer store.Close()
	state, _ := store.Bind(exampleSnapshot{})
	lease, _ := store.Take(state.StateID)
	fmt.Println(lease.Close())
	fmt.Println(lease.Close())
}
Output:
<nil>
<nil>

func (*Lease) Resolve added in v0.3.0

func (l *Lease) Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error)

Resolve returns a handle owned by the lease. The caller must finish all use of the handle before closing the lease. Resolve after Close returns an error.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/axuiautomation"
	"github.com/tmc/axmcp/internal/computeruse"
	"github.com/tmc/axmcp/internal/computeruse/session"
)

// exampleSnapshot supplies metadata without opening a native application.
type exampleSnapshot struct{}

func (exampleSnapshot) State() computeruse.AppState {
	return computeruse.AppState{App: computeruse.AppInfo{BundleID: "com.example.app"}}
}
func (exampleSnapshot) Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error) {
	return nil, computeruse.ElementNode{Index: index, Title: "Save"}, nil
}
func (exampleSnapshot) Close() error { return nil }

func main() {
	store := session.NewStore()
	defer store.Close()
	state, _ := store.Bind(exampleSnapshot{})
	lease, _ := store.Acquire(state.StateID)
	defer lease.Close()
	_, node, _ := lease.Resolve(1)
	fmt.Println(node.Title)
}
Output:
Save

func (*Lease) Retain added in v0.3.0

func (l *Lease) Retain() (*Lease, error)

Retain returns an independently owned lease for the same snapshot. It does not restore a consumed state token or establish freshness. Close the returned lease after its last use. Retaining a closed lease or a lease from a closed store returns an error. Retained handles must not be used concurrently unless the snapshot implementation permits it.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/axuiautomation"
	"github.com/tmc/axmcp/internal/computeruse"
	"github.com/tmc/axmcp/internal/computeruse/session"
)

// exampleSnapshot supplies metadata without opening a native application.
type exampleSnapshot struct{}

func (exampleSnapshot) State() computeruse.AppState {
	return computeruse.AppState{App: computeruse.AppInfo{BundleID: "com.example.app"}}
}
func (exampleSnapshot) Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error) {
	return nil, computeruse.ElementNode{Index: index, Title: "Save"}, nil
}
func (exampleSnapshot) Close() error { return nil }

func main() {
	store := session.NewStore()
	defer store.Close()
	state, _ := store.Bind(exampleSnapshot{})
	original, _ := store.Take(state.StateID)
	retained, _ := original.Retain()
	original.Close()
	defer retained.Close()
	fmt.Println(retained.State().App.BundleID)
}
Output:
com.example.app

func (*Lease) State added in v0.3.0

func (l *Lease) State() computeruse.AppState

State returns the captured metadata. It remains available after Close.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/axuiautomation"
	"github.com/tmc/axmcp/internal/computeruse"
	"github.com/tmc/axmcp/internal/computeruse/session"
)

// exampleSnapshot supplies metadata without opening a native application.
type exampleSnapshot struct{}

func (exampleSnapshot) State() computeruse.AppState {
	return computeruse.AppState{App: computeruse.AppInfo{BundleID: "com.example.app"}}
}
func (exampleSnapshot) Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error) {
	return nil, computeruse.ElementNode{Index: index, Title: "Save"}, nil
}
func (exampleSnapshot) Close() error { return nil }

func main() {
	store := session.NewStore()
	defer store.Close()
	state, _ := store.Bind(exampleSnapshot{})
	lease, _ := store.Acquire(state.StateID)
	defer lease.Close()
	fmt.Println(lease.State().SessionID)
}
Output:
bundle:com.example.app

type Snapshot

type Snapshot interface {
	State() computeruse.AppState
	Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error)
	Close() error
}

Snapshot is the live state handle stored for later actions.

type Store

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

Store keeps the latest live state per app session.

func NewStore

func NewStore() *Store

NewStore returns an empty store.

func (*Store) Acquire added in v0.3.0

func (s *Store) Acquire(stateID string) (*Lease, error)

Acquire retains a snapshot until the returned lease is closed. It does not consume the state token or establish that the observed UI is still current.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/axuiautomation"
	"github.com/tmc/axmcp/internal/computeruse"
	"github.com/tmc/axmcp/internal/computeruse/session"
)

// exampleSnapshot supplies metadata without opening a native application.
type exampleSnapshot struct{}

func (exampleSnapshot) State() computeruse.AppState {
	return computeruse.AppState{App: computeruse.AppInfo{BundleID: "com.example.app"}}
}
func (exampleSnapshot) Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error) {
	return nil, computeruse.ElementNode{Index: index, Title: "Save"}, nil
}
func (exampleSnapshot) Close() error { return nil }

func main() {
	store := session.NewStore()
	defer store.Close()
	state, _ := store.Bind(exampleSnapshot{})
	lease, _ := store.Acquire(state.StateID)
	defer lease.Close()
	fmt.Println(lease.State().App.BundleID)
}
Output:
com.example.app

func (*Store) Bind

func (s *Store) Bind(snapshot Snapshot) (computeruse.AppState, error)

Bind owns snapshot, including when it returns an error. It replaces the previous observation for the app; active leases keep the old snapshot alive. Errors closing a replaced snapshot are discarded.

func (*Store) Close

func (s *Store) Close() error

Close invalidates all tokens and permanently closes the store. It does not wait for active leases. Their final Close reports deferred snapshot errors.

func (*Store) Get

func (s *Store) Get(stateID string) (computeruse.AppState, bool)

func (*Store) GetForApp

func (s *Store) GetForApp(selector string) (computeruse.AppState, bool)

func (*Store) InvalidateSession

func (s *Store) InvalidateSession(sessionID string) error

InvalidateSession removes its token. Existing leases remain alive until closed.

func (*Store) Take added in v0.3.0

func (s *Store) Take(stateID string) (*Lease, error)

Take atomically consumes a state token and retains its snapshot until the returned lease is closed. At most one Take can succeed for a token.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/axuiautomation"
	"github.com/tmc/axmcp/internal/computeruse"
	"github.com/tmc/axmcp/internal/computeruse/session"
)

// exampleSnapshot supplies metadata without opening a native application.
type exampleSnapshot struct{}

func (exampleSnapshot) State() computeruse.AppState {
	return computeruse.AppState{App: computeruse.AppInfo{BundleID: "com.example.app"}}
}
func (exampleSnapshot) Resolve(index int) (*axuiautomation.Element, computeruse.ElementNode, error) {
	return nil, computeruse.ElementNode{Index: index, Title: "Save"}, nil
}
func (exampleSnapshot) Close() error { return nil }

func main() {
	store := session.NewStore()
	defer store.Close()
	state, _ := store.Bind(exampleSnapshot{})
	lease, _ := store.Take(state.StateID)
	defer lease.Close()
	_, err := store.Take(state.StateID)
	fmt.Println(err != nil)
}
Output:
true

Jump to

Keyboard shortcuts

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