pkg/ezagentapi/types.go

package ezagentapi

import (
	"context"
	"encoding/json"

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

// UnsupportedMessage is an inbound message type not currently modeled by ezagentapi.
type UnsupportedMessage struct {
	Type string
	Raw  json.RawMessage
}

// ContentBlock is a model/provider message content block.
type ContentBlock = protocol.ContentBlock

// PendingTurn is the canonical semantic pre-persistence turn.
type PendingTurn = protocol.PendingTurn

// PendingTurnAdvanced carries optional advanced turn metadata.
type PendingTurnAdvanced = protocol.PendingTurnAdvanced

// AttachmentBlobSpec carries attachment payloads and display metadata.
type AttachmentBlobSpec = protocol.AttachmentBlobSpec

// FileElement is one encoded attachment payload.
type FileElement = protocol.FileElement

// FileMeta is attachment metadata safe for display.
type FileMeta = protocol.FileMeta

// ToolCall is an inbound tool invocation.
type ToolCall = protocol.ToolCallIPC

// ProgressUpdate is a semantic transient progress mutation. Wire-only type
// and enabled fields are supplied by UpdateProgress.
type ProgressUpdate struct {
	LeafID          string
	TurnID          string
	UpdateID        string
	Title           string
	Lines           []string
	VisualizerMacro string
	VisualizerType  string
}

// TurnSummary is one typed entry in a history snapshot.
type TurnSummary = protocol.TurnSummary

// HistorySnapshot is the complete response to list_history. TipTurnID is the
// canonical history cut and may identify a draft turn omitted from Turns.
type HistorySnapshot = protocol.HistoryList

// PendingQuestion is one typed daemon-owned question in a leaf snapshot.
type PendingQuestion = protocol.PendingQuestion

// ProjectConfigTrustQuestion identifies project configuration awaiting trust.
type ProjectConfigTrustQuestion = protocol.ProjectConfigTrustQuestion

// QuestionReplyAck is the correlated acknowledgement for QuestionReply.
type QuestionReplyAck = protocol.QuestionReplyAck

// ToolAbort is an inbound tool abort request.
type ToolAbort = protocol.ToolAbort

// ToolInfo is tool metadata provided by a peer.
type ToolInfo = protocol.ToolInfo

// ToolInfoRequest is an inbound request for tool metadata.
type ToolInfoRequest = protocol.ToolInfoRequest

// PluginCall is an inbound plugin invocation.
type PluginCall = protocol.PluginCallIPC

// PluginInfo is plugin metadata provided by a peer.
type PluginInfo = protocol.PluginInfo

// PluginInfoRequest is an inbound request for plugin metadata.
type PluginInfoRequest = protocol.PluginInfoRequest

// MacroCall is an inbound macro invocation.
type MacroCall = protocol.MacroCallIPC

// MacroInfo is macro metadata provided by a peer.
type MacroInfo = protocol.MacroInfo

// MacroInfoRequest is an inbound request for macro metadata.
type MacroInfoRequest = protocol.MacroInfoRequest

// Shutdown is an inbound shutdown request.
type Shutdown = protocol.Shutdown

// ClientOptions describes an ez-agent client/module peer to connect.
type ClientOptions struct {
	Name     string
	LeafID   string
	Cwd      string
	Handlers Handlers
}

// ClientHello is the API-level startup hello received by a service peer.
type ClientHello struct {
	Itr    int64
	Name   string
	LeafID string
	Cwd    string
}

// HelloAck is the API-level startup acknowledgement received by a client peer.
type HelloAck struct {
	Itr          int64
	ServerID     string
	StateDir     string
	ConnectionID string
	ConfigError  string
	// Ready reports whether the daemon can accept normal protocol traffic.
	// A bootstrap daemon sends Ready false and then closes the connection.
	Ready   bool
	Message string
}

// ServiceOptions describes an ez-agent service peer to accept.
type ServiceOptions struct {
	ServerID    string
	StateDir    string
	ConfigError string
	BuildAck    func(ClientHello) (HelloAck, error)
	OnHello     func(context.Context, ServiceHello) (HelloAck, error)
	Handlers    Handlers
}

// ServiceHello is passed to a service-side startup hook after wire hello
// validation and before hello_ack is sent.
type ServiceHello struct {
	Hello ClientHello
}

// Handlers contains typed ez-agent callbacks for inbound protocol events.
// Callback bodies are serialized per connection, but RPC response correlation
// runs independently before callback serialization. A callback may therefore
// make a blocking RPC on the same Conn without preventing its response from
// reaching the waiter. Long-running or mutually dependent user work should
// still be handed to application workers; callback ordering is not a protocol
// or acknowledgement barrier.
type Handlers struct {
	// PrepareInbound is called in wire order on the reader. It must not block.
	// Its returned function runs in the callback goroutine before dispatch and
	// may attach request-local context. It must honor cancellation.
	PrepareInbound func(string, json.RawMessage) func(context.Context) context.Context
	// ConcurrentRPCs lets independent request handlers use their own admission
	// gates. Control requests must not queue behind a healthy long-running RPC.
	ConcurrentRPCs         bool
	OnBackgroundTaskAdd    func(context.Context, protocol.BackgroundTaskAdd) error
	OnBackgroundTaskRemove func(context.Context, protocol.BackgroundTaskRemove) error
	OnBackgroundTaskKill   func(context.Context, protocol.BackgroundTaskKill) error
	// OnRequestQueued observes selected decoded request IDs before callback
	// serialization. It must be quick and nonblocking. The optional returned
	// function runs after the callback lane is acquired, before handler start.
	// This observation neither changes dispatch ordering nor cancels requests.
	OnRequestQueued         func(RequestReceipt) func()
	OnAnnounce              func(context.Context, protocol.Announce) error
	OnPrompt                func(context.Context, protocol.Prompt) error
	OnSteer                 func(context.Context, protocol.Steer) error
	OnAbort                 func(context.Context, protocol.Abort) error
	OnCreateLeaf            func(context.Context, protocol.CreateLeaf) error
	OnDeleteLeaf            func(context.Context, protocol.DeleteLeaf) error
	OnRewindLeaf            func(context.Context, protocol.RewindLeaf) error
	OnRenameLeaf            func(context.Context, protocol.RenameLeaf) error
	OnUpdateLeafMeta        func(context.Context, protocol.UpdateLeafMeta) error
	OnNotifyLeaf            func(context.Context, protocol.NotifyLeaf) error
	OnSwitchLeaf            func(context.Context, protocol.SwitchLeaf) error
	OnDropFeed              func(context.Context, protocol.DropFeed) error
	OnListLeaves            func(context.Context, protocol.ListLeaves) error
	OnListRoots             func(context.Context, protocol.ListRoots) error
	OnListCwd               func(context.Context, protocol.ListCwd) error
	OnUpdateCwd             func(context.Context, protocol.UpdateCwd) error
	OnListHistory           func(context.Context, protocol.ListHistory) error
	OnListTurnTree          func(context.Context, protocol.ListTurnTree) error
	OnListQueued            func(context.Context, protocol.ListQueued) error
	OnListConnections       func(context.Context, protocol.ListConnections) error
	OnGetTurn               func(context.Context, protocol.GetTurn) error
	OnGetAttachmentChunk    func(context.Context, protocol.GetAttachmentChunk) error
	OnLeafStats             func(context.Context, protocol.LeafStats) error
	OnCreateVirtualRoot     func(context.Context, protocol.CreateVirtualRoot) error
	OnVacuum                func(context.Context, protocol.Vacuum) error
	OnGetLeafConfig         func(context.Context, protocol.GetLeafConfig) error
	OnLeafFiles             func(context.Context, protocol.LeafFiles) error
	OnSetLeafConfig         func(context.Context, protocol.SetLeafConfig) error
	OnListLeafConfig        func(context.Context, protocol.ListLeafConfig) error
	OnDeleteLeafConfig      func(context.Context, protocol.DeleteLeafConfig) error
	OnGetEffectiveConfig    func(context.Context, protocol.GetEffectiveConfig) error
	OnGetSupportedFiletypes func(context.Context, protocol.GetSupportedFiletypes) error
	OnAddTool               func(context.Context, protocol.AddTool) error
	OnRemoveTool            func(context.Context, protocol.RemoveTool) error
	OnListTools             func(context.Context, protocol.ListTools) error
	OnGetToolInfo           func(context.Context, protocol.GetToolInfo) error
	OnListPlugins           func(context.Context, protocol.ListPlugins) error
	OnListMacros            func(context.Context, protocol.ListMacros) error
	OnMacroCallRequest      func(context.Context, protocol.MacroCall) error
	OnListModels            func(context.Context, protocol.ListModels) error
	OnListProviders         func(context.Context, protocol.ListProviders) error
	OnResolveModels         func(context.Context, protocol.ResolveModels) error
	OnSetModel              func(context.Context, protocol.SetModel) error
	OnSetThinkingLevel      func(context.Context, protocol.SetThinkingLevel) error
	OnReportProgress        func(context.Context, protocol.ReportProgress) error
	OnQuestionReply         func(context.Context, protocol.QuestionReply) error
	OnToolInvoke            func(context.Context, protocol.ToolInvoke) error

	OnToolCall           func(context.Context, ToolCall) error
	OnToolAbort          func(context.Context, ToolAbort) error
	OnToolInfoRequest    func(context.Context, ToolInfoRequest) error
	OnToolResult         func(context.Context, protocol.ToolResult) error
	OnToolInvokeDone     func(context.Context, protocol.ToolInvokeDone) error
	OnToolAck            func(context.Context, protocol.ToolAck) error
	OnToolInfoResponse   func(context.Context, protocol.ToolInfoResponse) error
	OnHeartbeat          func(context.Context, protocol.OpHeartbeat) error
	OnToolAbortAck       func(context.Context, protocol.ToolAbortAck) error
	OnPluginCall         func(context.Context, PluginCall) error
	OnPluginInfoRequest  func(context.Context, PluginInfoRequest) error
	OnPluginResult       func(context.Context, protocol.PluginResultIPC) error
	OnPluginInfoResponse func(context.Context, protocol.PluginInfoResponse) error
	OnMacroCall          func(context.Context, MacroCall) error
	OnMacroInfoRequest   func(context.Context, MacroInfoRequest) error
	OnMacroResultIPC     func(context.Context, protocol.MacroResultIPC) error
	OnMacroInfoResponse  func(context.Context, protocol.MacroInfoResponse) error
	OnShutdown           func(context.Context, Shutdown) error
	OnPromptCompleted    func(context.Context, protocol.PromptCompleted) error
	OnPromptAccepted     func(context.Context, protocol.PromptAccepted) error
	OnSteerAccepted      func(context.Context, protocol.SteerAccepted) error
	OnAgentStart         func(context.Context, protocol.AgentStart) error
	OnAgentEnd           func(context.Context, protocol.AgentEnd) error
	OnTurnStart          func(context.Context, protocol.TurnStart) error
	OnTurnEnd            func(context.Context, protocol.TurnEnd) error
	OnMessageStart       func(context.Context, protocol.MessageStart) error
	OnMessageUpdate      func(context.Context, protocol.MessageUpdate) error
	OnMessageEnd         func(context.Context, protocol.MessageEnd) error
	OnTurnsSent          func(context.Context, protocol.TurnsSent) error
	OnTurnContent        func(context.Context, protocol.TurnContent) error
	OnLeafCreated        func(context.Context, protocol.LeafCreated) error
	OnLeafUpdated        func(context.Context, protocol.LeafUpdated) error
	OnLeafDeleted        func(context.Context, protocol.LeafDeleted) error
	OnLeafRewound        func(context.Context, protocol.LeafRewound) error
	OnLeafSwitched       func(context.Context, protocol.LeafSwitched) error
	OnFeedDropped        func(context.Context, protocol.FeedDropped) error
	OnAbortReceived      func(context.Context, protocol.AbortReceived) error
	OnInformational      func(context.Context, protocol.Informational) error
	OnProtocolError      func(context.Context, protocol.Error) error
	OnUnsupportedMessage func(context.Context, UnsupportedMessage) error
	OnError              func(context.Context, error)
}

// Capabilities describes the tools, plugins, and macros this peer provides.
type Capabilities struct {
	Tools   []string
	Plugins []string
	Macros  []string
}

// ToolResult is the canonical tool-result wire envelope. Its Turn is the sole
// semantic result payload.
type ToolResult = protocol.ToolResult

// ToolResultMetadata is encoded in PendingTurn.Advanced.OtherMeta.
type ToolResultMetadata struct {
	IsError      bool   `json:"is_error,omitempty"`
	FriendlyCall string `json:"friendly_call,omitempty"`
}

// ToolResultOptions configures NewToolResult.
type ToolResultOptions struct {
	ToolName string
	Metadata ToolResultMetadata
	Files    []FileElement
}

// PluginResult describes the result of one plugin invocation.
type PluginResult struct {
	PluginCallID string
	PendingTurns []PendingTurn
	IsError      bool
}

// MacroResult describes the result of one macro invocation.
type MacroResult struct {
	MacroCallID string
	Result      string
	IsError     bool
}