agent

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTaskTopic   = "betar/tasks"
	DefaultResultTopic = "betar/tasks/results"
)

Variables

This section is empty.

Functions

func GenerateDID

func GenerateDID(privKey p2pcrypto.PrivKey, appName, name string) string

GenerateDID generates a deterministic did:key from a libp2p private key and a name. Uses SHA256(appName/name/pubBytes) as an Ed25519 seed to derive the DID. Returns "" on failure.

Types

type ADKConfig

type ADKConfig struct {
	AppName   string
	ModelName string
	APIKey    string            // Google API key
	PrivKey   p2pcrypto.PrivKey // libp2p private key for DID generation

	// Provider fields
	Provider      string // "google", "openai", or "" for auto-detect
	OpenAIAPIKey  string // OpenAI-compatible API key
	OpenAIBaseURL string // OpenAI-compatible base URL (empty = api.openai.com)
}

ADKConfig configures the runtime bridge.

type ADKRuntime

type ADKRuntime struct {
	// contains filtered or unexported fields
}

ADKRuntime is the runtime wrapper used by the manager and worker.

func NewADKRuntime

func NewADKRuntime(cfg ADKConfig) (*ADKRuntime, error)

NewADKRuntime creates a runtime wrapper.

func (*ADKRuntime) CreateAgent

func (r *ADKRuntime) CreateAgent(ctx context.Context, spec AgentSpec) (string, error)

CreateAgent creates a runtime agent and returns runtime agent ID.

func (*ADKRuntime) DeleteAgent

func (r *ADKRuntime) DeleteAgent(ctx context.Context, runtimeAgentID string) error

DeleteAgent removes runtime agent.

func (*ADKRuntime) RunTask

func (r *ADKRuntime) RunTask(ctx context.Context, req types.TaskRequest) (*types.TaskResult, error)

RunTask executes a task via runtime.

type AgentSpec

type AgentSpec struct {
	Name        string
	Description string
	Image       string
	Price       float64
	Model       string
	APIKey      string // per-agent Google API key; empty = use global default
	Services    []types.Service
	X402Support bool
	OnChain     bool // if true, register on-chain via EIP-8004 (default: false)

	// Provider fields
	Provider      string // "google", "openai", or "" for auto-detect
	OpenAIAPIKey  string // OpenAI-compatible API key
	OpenAIBaseURL string // OpenAI-compatible base URL

	// CustomHandler skips ADK runtime creation. Use this when the agent
	// will be served via sdk.Serve() with a custom TaskHandler.
	CustomHandler bool
}

AgentSpec represents agent specification for registration

type LocalAgent

type LocalAgent struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	Price       float64   `json:"price"`
	MetadataCID string    `json:"metadataCID"`
	AgentID     string    `json:"agentID"` // ADK runtime agent ID
	Status      string    `json:"status"`
	CreatedAt   time.Time `json:"createdAt"`
	TokenID     *big.Int  `json:"tokenId,omitempty"` // EIP-8004 on-chain token ID
}

LocalAgent represents a local agent managed by this node

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages agent lifecycle

func NewManager

func NewManager(runtimeCfg ADKConfig, ipfsClient *ipfs.Client, p2pHost *p2p.Host, listingService *marketplace.AgentListingService, privKey crypto.PrivKey, paymentSvc *marketplace.PaymentService, walletAddr string, sessionStore SessionStorer, eip8004Client ...*eip8004.Client) (*Manager, error)

NewManager creates a new agent manager

func (*Manager) ConnectToAgent

func (m *Manager) ConnectToAgent(ctx context.Context, peerID peer.ID) error

ConnectToAgent connects to a remote agent via P2P

func (*Manager) DeregisterAgent

func (m *Manager) DeregisterAgent(ctx context.Context, agentID string) error

DeregisterAgent removes an agent from local registry and runtime.

func (*Manager) DiscoverAgents

func (m *Manager) DiscoverAgents(ctx context.Context) ([]types.AgentListing, error)

DiscoverAgents discovers agents from the marketplace

func (*Manager) ExecuteTask

func (m *Manager) ExecuteTask(ctx context.Context, agentID, input string) (string, error)

ExecuteTask executes a task on a local agent or a remote agent from CRDT

func (*Manager) FindListingByAgentID

func (m *Manager) FindListingByAgentID(agentID string) (*types.AgentListing, string)

FindListingByAgentID is the exported version of findListingByAgentID.

func (*Manager) GetAgent

func (m *Manager) GetAgent(agentID string) (*LocalAgent, error)

GetAgent gets a local agent by ID

func (*Manager) ListAgents

func (m *Manager) ListAgents() []*LocalAgent

ListAgents lists all local agents

func (*Manager) RegisterAgent

func (m *Manager) RegisterAgent(ctx context.Context, spec AgentSpec) (*LocalAgent, error)

RegisterAgent registers a new agent locally and publishes to marketplace

func (*Manager) RegisterStreamHandlers

func (m *Manager) RegisterStreamHandlers(sh *p2p.StreamHandler)

RegisterStreamHandlers registers marketplace stream handlers on the basic StreamHandler. Currently registers an "info" handler that returns all known local listings as JSON.

func (*Manager) RegisterX402Handlers

func (m *Manager) RegisterX402Handlers(sh *p2p.X402StreamHandler)

RegisterX402Handlers wires up the x402 stream handler and stores a reference for client use.

func (*Manager) RemoteExecuteX402

func (m *Manager) RemoteExecuteX402(ctx context.Context, peerID peer.ID, agentID, input string) (string, error)

RemoteExecuteX402 executes a task on a remote agent using the /x402/libp2p/1.0.0 protocol. It performs the standard 2-trip flow: send x402.request → receive x402.payment_required → sign with challenge nonce → send x402.paid_request → receive x402.response.

func (*Manager) SetCustomHandler

func (m *Manager) SetCustomHandler(h func(ctx context.Context, agentID, input string) (string, error))

SetCustomHandler registers a custom task handler that is used instead of the ADK runtime for agents registered with CustomHandler: true.

func (*Manager) SetTokenStore

func (m *Manager) SetTokenStore(ts *eip8004.TokenStore)

SetTokenStore sets the EIP-8004 token store for persisting on-chain tokenIDs.

type Runtime

type Runtime interface {
	CreateAgent(ctx context.Context, spec AgentSpec) (string, error)
	DeleteAgent(ctx context.Context, runtimeAgentID string) error
	RunTask(ctx context.Context, req types.TaskRequest) (*types.TaskResult, error)
}

Runtime defines required agent runtime capabilities.

type SessionStorer

type SessionStorer interface {
	AddExchange(ctx context.Context, agentID, callerID string, ex types.Exchange) error
}

SessionStorer is the interface Manager uses to persist session data. Implemented by *session.Store.

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker consumes task requests from pubsub and publishes task results.

func NewWorker

func NewWorker(ps *p2p.PubSub, runtime Runtime, taskTopic, resultTopic string) *Worker

NewWorker creates a pubsub worker.

func (*Worker) HandleMessage

func (w *Worker) HandleMessage(ctx context.Context, msg *pubsub.Message) error

HandleMessage handles a single pubsub task message.

func (*Worker) Start

func (w *Worker) Start(ctx context.Context) error

Start subscribes and processes incoming task messages until context cancellation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址