Documentation
¶
Index ¶
- Constants
- type Agent
- func (a *Agent) AddContact(agentID string) error
- func (a *Agent) BlockAgent(agentID string) error
- func (a *Agent) Broadcast(ctx context.Context, env *envelope.Envelope, destinations []string) map[string]error
- func (a *Agent) Capabilities() []string
- func (a *Agent) DecryptEnvelope(env *envelope.Envelope) (*envelope.Envelope, error)
- func (a *Agent) Discover(ctx context.Context, capabilities []string) ([]*discovery.DiscoverResult, error)
- func (a *Agent) EstablishSession(peerID, peerX25519PubKeyStr string) error
- func (a *Agent) GetTask(traceID string) (*Task, bool)
- func (a *Agent) Handle(capability string, handler HandlerFunc)
- func (a *Agent) HandleIncomingEnvelope(ctx context.Context, env *envelope.Envelope)
- func (a *Agent) ID() string
- func (a *Agent) ImportContacts(agentIDs []string) error
- func (a *Agent) ListContacts() []security.TrustEntry
- func (a *Agent) ListTasks() []*Task
- func (a *Agent) OnConnectionRequest(handler ConnectionRequestHandler)
- func (a *Agent) OnMessage(handler MessageHandler)
- func (a *Agent) PublicKey() string
- func (a *Agent) RemoveContact(agentID string)
- func (a *Agent) Send(ctx context.Context, env *envelope.Envelope) error
- func (a *Agent) SendRequest(ctx context.Context, env *envelope.Envelope, timeout time.Duration) (*envelope.Envelope, error)
- func (a *Agent) Start(ctx context.Context) error
- func (a *Agent) Stop(ctx context.Context) error
- func (a *Agent) Use(mw ...Middleware)
- func (a *Agent) X25519PublicKeyString() (string, error)
- type ConnectionRequest
- type ConnectionRequestHandler
- type HandlerFunc
- type MessageHandler
- type Middleware
- type Options
- type Router
- type Task
- type TaskState
- type TaskTracker
Constants ¶
const ( // MetadataKeyCapability is the envelope metadata key used for routing. MetadataKeyCapability = "capability" // MetadataKeyAction is the envelope metadata key for sub-action routing. MetadataKeyAction = "action" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent is the top-level API that assembles all P2P SDK components: identity, peer management, discovery, signaling, and security.
func NewSimple ¶
NewSimple creates an Agent with minimal configuration for enterprise intranet deployments. It uses server-based discovery and signaling only (no Nostr, no DHT, no STUN/TURN).
func (*Agent) AddContact ¶
AddContact adds an agent to the whitelist with TrustVerified level.
func (*Agent) BlockAgent ¶
BlockAgent explicitly blocks an agent.
func (*Agent) Broadcast ¶
func (a *Agent) Broadcast(ctx context.Context, env *envelope.Envelope, destinations []string) map[string]error
Broadcast sends an envelope to multiple destinations concurrently. Each destination gets a copy with a new ID. Returns a map of destination to error (nil for success).
func (*Agent) Capabilities ¶
Capabilities returns the deduplicated union of opts.Capabilities and router-registered capabilities.
func (*Agent) DecryptEnvelope ¶
func (*Agent) Discover ¶
func (a *Agent) Discover(ctx context.Context, capabilities []string) ([]*discovery.DiscoverResult, error)
Discover finds agents by capabilities on the platform.
func (*Agent) EstablishSession ¶
EstablishSession derives a session key with a peer using their X25519 public key. This is called during signaling when X25519 public keys are exchanged.
func (*Agent) Handle ¶
func (a *Agent) Handle(capability string, handler HandlerFunc)
Handle registers a capability handler on the router.
func (*Agent) HandleIncomingEnvelope ¶
HandleIncomingEnvelope validates the signature (pre-authentication), decrypts if needed, and dispatches to handlers.
func (*Agent) ImportContacts ¶
ImportContacts bulk-imports agent IDs as verified contacts.
func (*Agent) ListContacts ¶
func (a *Agent) ListContacts() []security.TrustEntry
ListContacts returns all trust store entries.
func (*Agent) OnConnectionRequest ¶
func (a *Agent) OnConnectionRequest(handler ConnectionRequestHandler)
OnConnectionRequest registers a handler called when a non-whitelisted peer requests a connection. The handler returns true to allow, false to deny.
func (*Agent) OnMessage ¶
func (a *Agent) OnMessage(handler MessageHandler)
OnMessage registers a handler for incoming messages.
func (*Agent) RemoveContact ¶
RemoveContact removes an agent from the whitelist.
func (*Agent) Send ¶
Send sends an envelope to a peer using P2P (preferred) or signaling relay (fallback). The payload is encrypted (if a session key exists), then the envelope is signed covering the ciphertext + headers (encrypt-then-sign for pre-authentication).
func (*Agent) SendRequest ¶
func (a *Agent) SendRequest(ctx context.Context, env *envelope.Envelope, timeout time.Duration) (*envelope.Envelope, error)
SendRequest sends an envelope and waits for a response with the same TraceID. It returns the response envelope or an error on timeout/context cancellation.
func (*Agent) X25519PublicKeyString ¶
X25519PublicKeyString returns the agent's X25519 public key for key exchange.
type ConnectionRequest ¶
ConnectionRequest describes a pending connection from an unknown peer.
type ConnectionRequestHandler ¶
type ConnectionRequestHandler func(ctx context.Context, req *ConnectionRequest) bool
ConnectionRequestHandler is called when a non-whitelisted peer requests a connection. Return true to allow, false to deny.
type HandlerFunc ¶
HandlerFunc processes an inbound envelope and optionally returns a response envelope.
func Chain ¶
func Chain(handler HandlerFunc, mws ...Middleware) HandlerFunc
Chain wraps a handler with the given middlewares. Middlewares are applied in order: the first middleware is the outermost wrapper.
type MessageHandler ¶
MessageHandler is called when an incoming envelope is received.
type Middleware ¶
type Middleware func(HandlerFunc) HandlerFunc
Middleware wraps a HandlerFunc to add cross-cutting behavior.
func LoggingMiddleware ¶
func LoggingMiddleware(logger *slog.Logger) Middleware
LoggingMiddleware logs capability, source, and duration for each dispatched request.
func RecoveryMiddleware ¶
func RecoveryMiddleware(logger *slog.Logger) Middleware
RecoveryMiddleware recovers from panics in handlers and converts them to errors.
type Options ¶
type Options struct {
// Name is the agent's display name.
Name string
// ServerURL is the peerclaw-server base URL (e.g., "http://localhost:8080").
ServerURL string
// Capabilities lists what this agent can do (e.g., "chat", "search").
Capabilities []string
// Protocols lists supported communication protocols (e.g., "a2a", "mcp").
Protocols []string
// KeypairPath is the path to the Ed25519 keypair seed file.
// If empty or not found, a new keypair will be generated.
KeypairPath string
// TrustStorePath is the path to the trust store file.
TrustStorePath string
// NostrRelays is a list of Nostr relay WebSocket URLs for fallback transport.
// If non-empty, a transport Selector will be created wrapping WebRTC + Nostr.
NostrRelays []string
// Discovery is an optional custom discovery implementation.
// If nil, a RegistryClient is created using ServerURL.
Discovery discovery.Discovery
// Signaling is an optional custom signaling client implementation.
// If nil, a WebSocket Client is created using ServerURL.
Signaling pcsignaling.SignalingClient
// MessageCachePath is the file path for persisting offline message cache.
MessageCachePath string
// ClaimToken is a one-time pairing code (e.g., "PCW-XXXX-XXXX") obtained from
// the platform. When set, the agent uses the claim flow instead of direct
// registration, binding itself to the user who generated the token.
ClaimToken string
// InboxRelays is a list of Nostr relay URLs for the offline mailbox.
// When non-empty, a Mailbox will be created for offline message delivery.
InboxRelays []string
// MailboxTTL is how long mailbox messages are retained (default 7 days).
MailboxTTL time.Duration
// InboxSyncInterval is how often the inbox is polled for new messages (default 5 minutes).
InboxSyncInterval time.Duration
// OutboxStatePath is the file path for persisting the outbox queue.
OutboxStatePath string
// LastSyncPath is the file path for persisting the last inbox sync timestamp.
LastSyncPath string
// Logger is the structured logger. Uses slog.Default() if nil.
Logger *slog.Logger
}
Options configures an Agent.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router dispatches inbound envelopes to capability-specific handlers.
func (*Router) Capabilities ¶
Capabilities returns the list of registered capability names.
func (*Router) Dispatch ¶
func (r *Router) Dispatch(ctx context.Context, env *envelope.Envelope) (matched bool, resp *envelope.Envelope, err error)
Dispatch routes an envelope to the matching capability handler. Returns (false, nil, nil) if no capability metadata or no matching handler (fallthrough). Returns (true, resp, err) when a handler is matched and executed.
func (*Router) Handle ¶
func (r *Router) Handle(capability string, handler HandlerFunc)
Handle registers a handler for the given capability.
func (*Router) Use ¶
func (r *Router) Use(mw ...Middleware)
Use appends global middlewares that wrap every handler on dispatch.
type Task ¶
type Task struct {
ID string
TraceID string
AgentID string // destination agent
State TaskState
Request *envelope.Envelope
Response *envelope.Envelope // terminal response (nil until completed/failed)
CreatedAt time.Time
UpdatedAt time.Time
}
Task maps an Envelope request-response exchange to an A2A task lifecycle.
type TaskTracker ¶
type TaskTracker struct {
// contains filtered or unexported fields
}
TaskTracker manages the lifecycle of tasks keyed by TraceID.
func NewTaskTracker ¶
func NewTaskTracker() *TaskTracker
NewTaskTracker creates an empty TaskTracker.
func (*TaskTracker) Get ¶
func (tt *TaskTracker) Get(traceID string) (*Task, bool)
Get returns a task by its TraceID.
func (*TaskTracker) Remove ¶
func (tt *TaskTracker) Remove(traceID string)
Remove deletes a task by its TraceID.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
peerclaw-trust
command
|
|
|
examples
|
|
|
capability-handler
command
|
|
|
echo
command
|
|
|
enterprise
command
Enterprise example: minimal intranet agent setup using NewSimple + ImportContacts.
|
Enterprise example: minimal intranet agent setup using NewSimple + ImportContacts. |
|
llm-tools
command
Package main demonstrates how to expose PeerClaw agent capabilities as MCP-compatible tools for LLM-driven agents.
|
Package main demonstrates how to expose PeerClaw agent capabilities as MCP-compatible tools for LLM-driven agents. |
|
p2p-chat
command
Package main demonstrates a minimal P2P chat between two agents.
|
Package main demonstrates a minimal P2P chat between two agents. |