a2a

package
v0.31.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package a2a 实现 Agent-to-Agent 通信协议 基于 JSON-RPC 2.0 和 A2A 标准

Index

Constants

View Source
const (
	ErrorCodeParseError     = -32700
	ErrorCodeInvalidRequest = -32600
	ErrorCodeMethodNotFound = -32601
	ErrorCodeInvalidParams  = -32602
	ErrorCodeInternalError  = -32603
)

标准 JSON-RPC 错误码

View Source
const (
	ErrorCodeTaskNotFound                 = -32001
	ErrorCodeTaskNotCancelable            = -32002
	ErrorCodePushNotificationNotSupported = -32003
	ErrorCodeUnsupportedOperation         = -32004
)

A2A 特定错误码

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentCard

type AgentCard struct {
	Name               string       `json:"name"`
	Description        string       `json:"description"`
	URL                string       `json:"url"`
	Provider           Provider     `json:"provider"`
	Version            string       `json:"version"`
	Capabilities       Capabilities `json:"capabilities"`
	DefaultInputModes  []string     `json:"defaultInputModes"`
	DefaultOutputModes []string     `json:"defaultOutputModes"`
	Skills             []Skill      `json:"skills,omitempty"`
}

AgentCard Agent 元数据卡片 用于 Agent 发现和能力声明

type Artifact

type Artifact struct {
	Name  string `json:"name"`
	Parts []Part `json:"parts"`
}

Artifact 任务产出物

type Capabilities

type Capabilities struct {
	Streaming              bool `json:"streaming"`
	PushNotifications      bool `json:"pushNotifications"`
	StateTransitionHistory bool `json:"stateTransitionHistory"`
}

Capabilities Agent 能力声明

type Handler

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

Handler HTTP 处理器,实现 A2A 协议的 HTTP 端点

func NewHandler

func NewHandler(server *Server) *Handler

NewHandler 创建新的 HTTP 处理器

func (*Handler) GetAgentCard

func (h *Handler) GetAgentCard(c *gin.Context)

GetAgentCard 处理 Agent Card 请求 GET /.well-known/{agentId}/agent-card.json

func (*Handler) GetTaskStatus

func (h *Handler) GetTaskStatus(c *gin.Context)

GetTaskStatus 获取任务状态的便捷端点 GET /a2a/{agentId}/tasks/{taskId}

func (*Handler) HandleBatchJSONRPC

func (h *Handler) HandleBatchJSONRPC(c *gin.Context)

HandleBatchJSONRPC 处理批量 JSON-RPC 2.0 请求 POST /a2a/{agentId}/batch

func (*Handler) HandleJSONRPC

func (h *Handler) HandleJSONRPC(c *gin.Context)

HandleJSONRPC 处理 JSON-RPC 2.0 请求 POST /a2a/{agentId}

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(rg *gin.RouterGroup)

RegisterRoutes 注册 A2A 路由到 Gin RouterGroup

type InMemoryTaskStore

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

InMemoryTaskStore 内存任务存储 使用 Map 存储,支持并发访问

func NewInMemoryTaskStore

func NewInMemoryTaskStore() *InMemoryTaskStore

NewInMemoryTaskStore 创建内存任务存储

func (*InMemoryTaskStore) AddCancellation

func (s *InMemoryTaskStore) AddCancellation(taskID string)

AddCancellation 添加取消信号

func (*InMemoryTaskStore) Delete

func (s *InMemoryTaskStore) Delete(agentID, taskID string) error

Delete 删除任务

func (*InMemoryTaskStore) IsCanceled

func (s *InMemoryTaskStore) IsCanceled(taskID string) bool

IsCanceled 检查是否已取消

func (*InMemoryTaskStore) List

func (s *InMemoryTaskStore) List(agentID string) ([]*Task, error)

List 列出 Agent 的所有任务

func (*InMemoryTaskStore) Load

func (s *InMemoryTaskStore) Load(agentID, taskID string) (*Task, error)

Load 加载任务

func (*InMemoryTaskStore) RemoveCancellation

func (s *InMemoryTaskStore) RemoveCancellation(taskID string)

RemoveCancellation 移除取消信号

func (*InMemoryTaskStore) Save

func (s *InMemoryTaskStore) Save(agentID string, task *Task) error

Save 保存任务

type JSONRPCRequest

type JSONRPCRequest struct {
	JSONRPC string `json:"jsonrpc"` // 固定为 "2.0"
	ID      any    `json:"id"`      // 字符串或数字
	Method  string `json:"method"`
	Params  any    `json:"params,omitempty"`
}

JSONRPCRequest JSON-RPC 2.0 请求

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string    `json:"jsonrpc"` // 固定为 "2.0"
	ID      any       `json:"id"`
	Result  any       `json:"result,omitempty"`
	Error   *RPCError `json:"error,omitempty"`
}

JSONRPCResponse JSON-RPC 2.0 响应

func NewErrorResponse

func NewErrorResponse(id any, code int, message string, data any) *JSONRPCResponse

NewErrorResponse 创建错误响应

func NewSuccessResponse

func NewSuccessResponse(id any, result any) *JSONRPCResponse

NewSuccessResponse 创建成功响应

type Message

type Message struct {
	MessageID        string   `json:"messageId"`
	Role             string   `json:"role"` // "user" 或 "agent"
	Parts            []Part   `json:"parts"`
	Kind             string   `json:"kind"` // 固定为 "message"
	ContextID        string   `json:"contextId,omitempty"`
	TaskID           string   `json:"taskId,omitempty"`
	ReferenceTaskIDs []string `json:"referenceTaskIds,omitempty"`
}

Message 消息对象

func NewTextMessage

func NewTextMessage(messageID, role, text string) Message

NewTextMessage 创建文本消息

type MessageSendParams

type MessageSendParams struct {
	Message   Message  `json:"message"`
	ContextID string   `json:"contextId,omitempty"`
	Metadata  Metadata `json:"metadata,omitempty"`
}

MessageSendParams message/send 方法参数

type MessageSendResult

type MessageSendResult struct {
	TaskID string `json:"taskId"`
}

MessageSendResult message/send 方法返回结果

type MessageStreamParams

type MessageStreamParams struct {
	Message   Message  `json:"message"`
	ContextID string   `json:"contextId,omitempty"`
	Metadata  Metadata `json:"metadata,omitempty"`
}

MessageStreamParams message/stream 方法参数

type MessageStreamResult

type MessageStreamResult struct {
	TaskID string `json:"taskId"`
}

MessageStreamResult message/stream 方法返回结果

type Metadata

type Metadata map[string]any

Metadata 任务元数据

type Part

type Part struct {
	Kind string `json:"kind"` // "text", "file", "data"
	Text string `json:"text,omitempty"`
	Data any    `json:"data,omitempty"`
	// File 相关字段
	Name     string `json:"name,omitempty"`
	MimeType string `json:"mimeType,omitempty"`
	URL      string `json:"url,omitempty"`
}

Part 消息部分 支持 text、file、data 三种类型

type Provider

type Provider struct {
	Organization string `json:"organization"`
	URL          string `json:"url"`
}

Provider Agent 提供者信息

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

RPCError JSON-RPC 错误对象

type Server

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

Server A2A 服务器 将 Actor 系统暴露为 A2A 协议端点

func NewServer

func NewServer(actorSystem *actor.System, taskStore TaskStore) *Server

NewServer 创建 A2A 服务器

func (*Server) GetAgentCard

func (s *Server) GetAgentCard(agentID string) (*AgentCard, error)

GetAgentCard 获取 Agent Card

func (*Server) HandleRequest

func (s *Server) HandleRequest(ctx context.Context, agentID string, req *JSONRPCRequest) *JSONRPCResponse

HandleRequest 处理 JSON-RPC 请求

type Skill

type Skill struct {
	ID          string   `json:"id"`
	Name        string   `json:"name"`
	Description string   `json:"description"`
	Tags        []string `json:"tags,omitempty"`
}

Skill Agent 技能定义

type Task

type Task struct {
	ID        string     `json:"id"`
	ContextID string     `json:"contextId"`
	Status    TaskStatus `json:"status"`
	Artifacts []Artifact `json:"artifacts,omitempty"`
	History   []Message  `json:"history,omitempty"`
	Metadata  Metadata   `json:"metadata,omitempty"`
	Kind      string     `json:"kind"` // 固定为 "task"
}

Task 任务对象 跟踪 Agent 交互的完整生命周期

func NewTask

func NewTask(id, contextID string) *Task

NewTask 创建新任务

func (*Task) AddMessage

func (t *Task) AddMessage(msg Message)

AddMessage 添加消息到历史

func (*Task) IsFinalState

func (t *Task) IsFinalState() bool

IsFinalState 检查是否为最终状态

func (*Task) UpdateStatus

func (t *Task) UpdateStatus(state TaskState, message *Message)

UpdateStatus 更新任务状态

type TaskState

type TaskState string

TaskState 任务状态枚举

const (
	TaskStateSubmitted     TaskState = "submitted"
	TaskStateWorking       TaskState = "working"
	TaskStateCompleted     TaskState = "completed"
	TaskStateFailed        TaskState = "failed"
	TaskStateCanceled      TaskState = "canceled"
	TaskStateInputRequired TaskState = "input-required"
)

type TaskStatus

type TaskStatus struct {
	State     TaskState `json:"state"`
	Timestamp string    `json:"timestamp"` // ISO 8601 格式
	Message   *Message  `json:"message,omitempty"`
}

TaskStatus 任务状态

type TaskStore

type TaskStore interface {
	// Load 加载任务
	Load(agentID, taskID string) (*Task, error)

	// Save 保存任务
	Save(agentID string, task *Task) error

	// Delete 删除任务
	Delete(agentID, taskID string) error

	// List 列出 Agent 的所有任务
	List(agentID string) ([]*Task, error)

	// AddCancellation 添加取消信号
	AddCancellation(taskID string)

	// RemoveCancellation 移除取消信号
	RemoveCancellation(taskID string)

	// IsCanceled 检查是否已取消
	IsCanceled(taskID string) bool
}

TaskStore Task 存储接口

type TasksCancelParams

type TasksCancelParams struct {
	TaskID string `json:"taskId"`
}

TasksCancelParams tasks/cancel 方法参数

type TasksCancelResult

type TasksCancelResult struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
}

TasksCancelResult tasks/cancel 方法返回结果

type TasksGetParams

type TasksGetParams struct {
	TaskID string `json:"taskId"`
}

TasksGetParams tasks/get 方法参数

type TasksGetResult

type TasksGetResult struct {
	Task *Task `json:"task"`
}

TasksGetResult tasks/get 方法返回结果

Jump to

Keyboard shortcuts

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