213 lines
5.4 KiB
Go
213 lines
5.4 KiB
Go
// compat.go - Compatibility types during 9P migration.
|
|
// These stub out the old JSON-RPC interface while we transition to 9P.
|
|
// TODO: Remove once 9P client is fully implemented.
|
|
package toolsrv
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"syscall"
|
|
)
|
|
|
|
// Process represents a spawned toolsrv subprocess.
|
|
// TODO: Update for 9P model.
|
|
type Process struct {
|
|
Socket string
|
|
Info HostInfo
|
|
cleanup func()
|
|
}
|
|
|
|
// Close shuts down the process.
|
|
func (p *Process) Close() {
|
|
if p.cleanup != nil {
|
|
p.cleanup()
|
|
}
|
|
}
|
|
|
|
// ProcessKeeper manages toolsrv process lifecycle and reconnection.
|
|
// TODO: Update for 9P model.
|
|
type ProcessKeeper struct {
|
|
proc *Process
|
|
conn *Conn
|
|
ctx context.Context
|
|
respawn func(ctx context.Context) (*Process, error)
|
|
}
|
|
|
|
// NewProcessKeeper creates a new ProcessKeeper.
|
|
func NewProcessKeeper(ctx context.Context, proc *Process, respawn func(ctx context.Context) (*Process, error)) *ProcessKeeper {
|
|
return &ProcessKeeper{proc: proc, ctx: ctx, respawn: respawn}
|
|
}
|
|
|
|
// Dial returns a connection to the toolsrv.
|
|
func (pk *ProcessKeeper) Dial() (*Conn, error) {
|
|
// TODO: Implement actual 9P connection
|
|
if pk.conn == nil {
|
|
pk.conn = &Conn{}
|
|
}
|
|
return pk.conn, nil
|
|
}
|
|
|
|
// SetContext updates the context.
|
|
func (pk *ProcessKeeper) SetContext(ctx context.Context) {
|
|
pk.ctx = ctx
|
|
}
|
|
|
|
// Close shuts down the keeper.
|
|
func (pk *ProcessKeeper) Close() {
|
|
// TODO: Implement
|
|
}
|
|
|
|
// Spawn starts a local toolsrv subprocess.
|
|
// TODO: Update to spawn 9P server.
|
|
func Spawn(ctx context.Context, cwd string, opts ...Option) (*Process, error) {
|
|
// TODO: Implement
|
|
return &Process{}, nil
|
|
}
|
|
|
|
// SpawnRemote starts a toolsrv on a remote host via SSH.
|
|
// TODO: Update to spawn 9P server.
|
|
func SpawnRemote(ctx context.Context, cfg RemoteConfig) (*Process, error) {
|
|
// TODO: Implement
|
|
return &Process{}, nil
|
|
}
|
|
|
|
// RemoteConfig holds configuration for remote toolsrv.
|
|
type RemoteConfig struct {
|
|
Host string
|
|
Port int
|
|
User string
|
|
CWD string
|
|
SSHTarget string
|
|
}
|
|
|
|
// Option is a functional option for toolsrv.
|
|
type Option func(*optionState)
|
|
|
|
type optionState struct {
|
|
yolo bool
|
|
}
|
|
|
|
// WithYolo returns an option to skip sandbox enforcement.
|
|
func WithYolo() Option {
|
|
return func(o *optionState) {
|
|
o.yolo = true
|
|
}
|
|
}
|
|
|
|
// HostInfo contains information about the host running toolsrv.
|
|
type HostInfo struct {
|
|
Platform string
|
|
IsGitRepo bool
|
|
}
|
|
|
|
// FetchHostInfo retrieves host information from toolsrv.
|
|
func FetchHostInfo(conn *Conn) (HostInfo, error) {
|
|
// TODO: Implement via 9P read to /info
|
|
return HostInfo{}, nil
|
|
}
|
|
|
|
// Conn is a stub for the old RPC connection.
|
|
// TODO: Replace with 9P client using lib9p.
|
|
type Conn struct {
|
|
// Placeholder - will be replaced with 9P fsys
|
|
}
|
|
|
|
// Runner interface for tool execution.
|
|
type Runner interface {
|
|
ListTools() ([]ToolInfo, error)
|
|
CallTool(ctx context.Context, name string, args json.RawMessage) (json.RawMessage, error)
|
|
}
|
|
|
|
// ListTools returns the list of available tools.
|
|
func (c *Conn) ListTools() ([]ToolInfo, error) {
|
|
// TODO: Implement via 9P read to /tools
|
|
return nil, nil
|
|
}
|
|
|
|
// CallTool calls a tool and returns the result.
|
|
func (c *Conn) CallTool(ctx context.Context, name string, args json.RawMessage) (json.RawMessage, error) {
|
|
// TODO: Implement via 9P rdwr to /proc/new
|
|
return nil, nil
|
|
}
|
|
|
|
// SetEnv sets an environment variable on the remote server.
|
|
func (c *Conn) SetEnv(key, value string) {
|
|
// TODO: Implement via 9P (or remove - may not be needed with 9P model)
|
|
}
|
|
|
|
// SetCWD sets the working directory on the remote server.
|
|
func (c *Conn) SetCWD(dir string) {
|
|
// TODO: Implement via 9P (or remove - may not be needed with 9P model)
|
|
}
|
|
|
|
// SetOnToolsChanged sets a callback for when tools change.
|
|
func (c *Conn) SetOnToolsChanged(fn func()) {
|
|
// TODO: Implement or remove
|
|
}
|
|
|
|
// ToolRegistryRevision returns the current revision of the tool registry.
|
|
func (c *Conn) ToolRegistryRevision() uint64 {
|
|
// TODO: Implement or remove
|
|
return 0
|
|
}
|
|
|
|
// Ping checks if the connection is alive.
|
|
func (c *Conn) Ping() error {
|
|
// TODO: Implement via 9P
|
|
return nil
|
|
}
|
|
|
|
// Close closes the connection.
|
|
func (c *Conn) Close() {
|
|
// TODO: Implement
|
|
}
|
|
|
|
// Detach detaches a running process.
|
|
func (c *Conn) Detach() bool {
|
|
// TODO: Implement via 9P
|
|
return false
|
|
}
|
|
|
|
// ListDetachedRaw returns raw info about detached processes.
|
|
func (c *Conn) ListDetachedRaw() []any {
|
|
// TODO: Implement via 9P read to /proc
|
|
return nil
|
|
}
|
|
|
|
// SignalDetached sends a signal to a detached process.
|
|
func (c *Conn) SignalDetached(pid int, sig syscall.Signal) error {
|
|
// TODO: Implement via 9P write to /proc/{pid}/ctl
|
|
return nil
|
|
}
|
|
|
|
// GetDetachedOutput gets output from a detached process.
|
|
func (c *Conn) GetDetachedOutput(pid int) (string, error) {
|
|
// TODO: Implement via 9P read to /proc/{pid}/out
|
|
return "", nil
|
|
}
|
|
|
|
// DismissDetached dismisses a detached process.
|
|
func (c *Conn) DismissDetached(pid int) bool {
|
|
// TODO: Implement via 9P write to /proc/{pid}/ctl
|
|
return false
|
|
}
|
|
|
|
// WithOutputStream returns a context with streaming output function attached.
|
|
func WithOutputStream(ctx context.Context, fn func(string)) context.Context {
|
|
return WithStreamFunc(ctx, fn)
|
|
}
|
|
|
|
// RateLimitedError indicates a rate limit was hit.
|
|
type RateLimitedError struct {
|
|
Err error
|
|
Remaining int
|
|
}
|
|
|
|
func (e RateLimitedError) Error() string {
|
|
return e.Err.Error()
|
|
}
|
|
|
|
// Add MediaType and Data fields to ToolResultContent if needed
|
|
// by extending the existing type (already defined in exec9p.go).
|
|
// Note: These may need to be in the actual ToolResultContent struct.
|