clawchain/x/agent/keeper/query_live_agents.go
caelum0x db831658ab feat(agent): expand gRPC surface, tasks, intents, negotiations, IBC discovery
Adds full QueryServer/MsgServer gRPC services, task lifecycle handlers
(accept, checkpoint, complete, delegate), intent protocol, negotiation
system, IBC agent discovery, rate limiting, reputation adapter, and
comprehensive test coverage.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 22:13:45 +03:00

76 lines
1.8 KiB
Go

package keeper
import (
"context"
"clawchain/x/agent/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
)
func (q queryServer) LiveAgents(ctx context.Context, req *types.QueryLiveAgentsRequest) (*types.QueryLiveAgentsResponse, error) {
if req == nil {
return nil, errorsmod.Wrap(types.ErrInvalidAddress, "empty request")
}
params, err := q.k.Params.Get(ctx)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to get params")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
currentHeight := sdkCtx.BlockHeight()
maxGap := params.MaxHeartbeatGapBlocks
cutoff := int64(0)
if maxGap > 0 {
cutoff = currentHeight - maxGap
if cutoff < 0 {
cutoff = 0
}
}
// Paginate over the AgentLiveness map, filtering for live agents.
liveAgents, pageRes, err := query.CollectionFilteredPaginate(
ctx,
q.k.AgentLiveness,
req.Pagination,
func(_ string, liveness types.AgentLiveness) (bool, error) {
// If maxGap is 0 (disabled), include all agents with any heartbeat.
if maxGap > 0 && liveness.LastHeartbeatHeight < cutoff {
return false, nil
}
return true, nil
},
func(_ string, liveness types.AgentLiveness) (types.LiveAgentEntry, error) {
entry := types.LiveAgentEntry{
Address: liveness.AgentAddress,
Liveness: liveness,
}
// Enrich with agent info if available.
agent, err := q.k.Agents.Get(ctx, liveness.AgentAddress)
if err == nil {
entry.Name = agent.Name
entry.Endpoint = agent.Endpoint
}
return entry, nil
},
)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to paginate live agents")
}
if liveAgents == nil {
liveAgents = []types.LiveAgentEntry{}
}
return &types.QueryLiveAgentsResponse{
Agents: liveAgents,
Pagination: pageRes,
}, nil
}