ez-agent-ruby/lib/ez_agent/api.rb
# frozen_string_literal: true
require_relative "api/errors"
require_relative "api/error_classification"
require_relative "api/incidents"
require_relative "api/event"
require_relative "api/pending_requests"
require_relative "api/events"
require_relative "api/completion"
require_relative "api/outbound_work"
require_relative "api/values"
require_relative "api/tool_invocations"
require_relative "api/sessions"
require_relative "api/tool_approvals"
require_relative "api/leaf_sync_view"
require_relative "api/models"
require_relative "api/capabilities"
require_relative "api/discovery"
require_relative "api/management"
require_relative "api/macros"
require_relative "api/daemon"
require_relative "api/extras"
require_relative "api/client"
module EzAgent
module API
VERSION = "0.4.0".freeze
module_function
def resolve_endpoint(socket_path: nil, env: ENV)
Endpoint.resolve(socket_path: socket_path, env: env)
end
# Connect directly to the local AF_UNIX service. No CLI subprocess or
# daemon autolaunch is performed. This is only an endpoint/dialing adapter;
# protocol startup and lifecycle belong to Connection.open. With a block,
# connection shutdown and joining are guaranteed without sending the
# daemon-wide shutdown frame.
def connect(socket_path: nil, name: DEFAULT_CONNECTION_NAME, cwd: Dir.pwd,
leaf_id: nil, startup_timeout: DEFAULT_STARTUP_TIMEOUT,
extras: [],
provider_workers: DEFAULT_PROVIDER_WORKERS,
provider_heartbeat_interval: DEFAULT_PROVIDER_HEARTBEAT_INTERVAL)
begin
endpoint = Endpoint.resolve(socket_path: socket_path)
socket = begin
UNIXSocket.new(endpoint.socket_path)
rescue SystemCallError, IOError => cause
raise TransportError.new("connecting to #{endpoint.socket_path}: #{cause.message}", cause: cause)
end
connection = Connection.open(
reader: socket, writer: socket, close: -> { socket.close unless socket.closed? },
name: name, cwd: cwd, leaf_id: leaf_id, startup_timeout: startup_timeout,
extras: extras,
provider_workers: provider_workers,
provider_heartbeat_interval: provider_heartbeat_interval
)
connection.__send__(:set_endpoint, endpoint)
rescue Exception => error
Incidents.record(:connect, error)
raise
end
return connection unless block_given?
# Caller failures are not connection setup incidents. Cleanup still runs,
# but no secondary exception may replace an exception from the block.
primary_error = nil
begin
yield connection
rescue Exception => error
primary_error = error
raise
ensure
cleanup_error = nil
begin
connection.close
rescue Exception => error
cleanup_error = error
connection.__send__(:retain_cleanup_error, error, boundary: :connect_close)
end
begin
connection.join(raise_on_error: false)
rescue Exception => error
cleanup_error ||= error
connection.__send__(:retain_cleanup_error, error, boundary: :connect_join)
end
raise cleanup_error if cleanup_error && primary_error.nil?
end
end
end
end