pkg/ezagentapi/startup.go

package ezagentapi

import (
	"encoding/json"
	"fmt"

	"git.cafe/ezterry/ez-agent/pkg/jsonlpipe"
	"git.cafe/ezterry/ez-agent/pkg/protocol"
)

const protocolVersion = 1

func clientOptionsToProtocolHello(opts ClientOptions) protocol.Hello {
	return normalizeHello(protocol.Hello{Name: opts.Name, LeafID: opts.LeafID, Cwd: opts.Cwd})
}

func protocolHelloToClientHello(h protocol.Hello) ClientHello {
	return ClientHello{Itr: h.Itr, Name: h.Name, LeafID: h.LeafID, Cwd: h.Cwd}
}

func helloAckToProtocol(ack HelloAck) protocol.HelloAck {
	return normalizeHelloAck(protocol.HelloAck{ServerID: ack.ServerID, StateDir: ack.StateDir, ConnectionID: ack.ConnectionID, ConfigError: ack.ConfigError, Ready: ack.Ready, Message: ack.Message})
}

func protocolHelloAckToAPI(ack protocol.HelloAck) HelloAck {
	return HelloAck{Itr: ack.Itr, ServerID: ack.ServerID, StateDir: ack.StateDir, ConnectionID: ack.ConnectionID, ConfigError: ack.ConfigError, Ready: ack.Ready, Message: ack.Message}
}

func normalizeHello(h protocol.Hello) protocol.Hello {
	h.Type = "hello"
	if h.Protocol == 0 {
		h.Protocol = protocolVersion
	}
	return h
}

func normalizeHelloAck(ack protocol.HelloAck) protocol.HelloAck {
	ack.Type = "hello_ack"
	if ack.Protocol == 0 {
		ack.Protocol = protocolVersion
	}
	return ack
}

type StartupError struct{ Frame protocol.Error }

type StartupReplyError struct{ StartupError }

func (e StartupError) Error() string       { return e.Frame.Message }
func (e StartupReplyError) InitReply() any { return e.Frame }

func decodeHello(f jsonlpipe.Frame) (protocol.Hello, error) {
	if f.Type != "hello" {
		return protocol.Hello{}, StartupReplyError{StartupError{Frame: protocol.Error{Type: "error", Code: "not_attached", Message: "hello required before commands"}}}
	}
	var hello protocol.Hello
	if err := json.Unmarshal(f.Raw, &hello); err != nil {
		return protocol.Hello{}, fmt.Errorf("ezagentapi: decode hello: %w", err)
	}
	if hello.Type != "hello" {
		return protocol.Hello{}, StartupReplyError{StartupError{Frame: protocol.Error{Type: "error", Code: "parse_error", Message: fmt.Sprintf("invalid hello type %q", hello.Type)}}}
	}
	if hello.Protocol != protocolVersion {
		return protocol.Hello{}, StartupReplyError{StartupError{Frame: protocol.Error{Type: "error", Code: "protocol_mismatch", Message: fmt.Sprintf("unsupported protocol %d", hello.Protocol)}}}
	}
	return hello, nil
}

func decodeHelloAck(f jsonlpipe.Frame) (protocol.HelloAck, error) {
	if f.Type != "hello_ack" {
		return protocol.HelloAck{}, fmt.Errorf("ezagentapi: expected hello_ack, got %q", f.Type)
	}
	// Ready is a pointer here rather than in protocol.HelloAck so an omitted
	// field from an older daemon remains distinguishable from ready:false.
	// The public API intentionally exposes the normalized plain bool.
	var wire struct {
		Type         string `json:"type"`
		Itr          int64  `json:"itr"`
		Protocol     int    `json:"protocol"`
		ServerID     string `json:"server_id"`
		StateDir     string `json:"state_dir"`
		ConnectionID string `json:"connection_id,omitempty"`
		ConfigError  string `json:"config_error,omitempty"`
		Ready        *bool  `json:"ready"`
		Message      string `json:"message"`
	}
	if err := json.Unmarshal(f.Raw, &wire); err != nil {
		return protocol.HelloAck{}, fmt.Errorf("ezagentapi: decode hello_ack: %w", err)
	}
	ack := protocol.HelloAck{Type: wire.Type, Itr: wire.Itr, Protocol: wire.Protocol, ServerID: wire.ServerID, StateDir: wire.StateDir, ConnectionID: wire.ConnectionID, ConfigError: wire.ConfigError, Message: wire.Message}
	if wire.Ready == nil {
		ack.Ready = true
		if ack.Message == "" {
			ack.Message = "READY"
		}
	} else {
		ack.Ready = *wire.Ready
	}
	if ack.Type != "hello_ack" {
		return protocol.HelloAck{}, fmt.Errorf("ezagentapi: invalid hello_ack type %q", ack.Type)
	}
	if ack.Protocol != protocolVersion {
		return protocol.HelloAck{}, fmt.Errorf("ezagentapi: unsupported protocol %d", ack.Protocol)
	}
	return ack, nil
}