Documentation
¶
Overview ¶
Package a2a 实现 Agent-to-Agent 通信协议 基于 JSON-RPC 2.0 和 A2A 标准
Index ¶
- Constants
- type AgentCard
- type Artifact
- type Capabilities
- type Handler
- type InMemoryTaskStore
- func (s *InMemoryTaskStore) AddCancellation(taskID string)
- func (s *InMemoryTaskStore) Delete(agentID, taskID string) error
- func (s *InMemoryTaskStore) IsCanceled(taskID string) bool
- func (s *InMemoryTaskStore) List(agentID string) ([]*Task, error)
- func (s *InMemoryTaskStore) Load(agentID, taskID string) (*Task, error)
- func (s *InMemoryTaskStore) RemoveCancellation(taskID string)
- func (s *InMemoryTaskStore) Save(agentID string, task *Task) error
- type JSONRPCRequest
- type JSONRPCResponse
- type Message
- type MessageSendParams
- type MessageSendResult
- type MessageStreamParams
- type MessageStreamResult
- type Metadata
- type Part
- type Provider
- type RPCError
- type Server
- type Skill
- type Task
- type TaskState
- type TaskStatus
- type TaskStore
- type TasksCancelParams
- type TasksCancelResult
- type TasksGetParams
- type TasksGetResult
Constants ¶
const ( ErrorCodeParseError = -32700 ErrorCodeInvalidRequest = -32600 ErrorCodeMethodNotFound = -32601 ErrorCodeInvalidParams = -32602 ErrorCodeInternalError = -32603 )
标准 JSON-RPC 错误码
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 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 (*Handler) GetAgentCard ¶
GetAgentCard 处理 Agent Card 请求 GET /.well-known/{agentId}/agent-card.json
func (*Handler) GetTaskStatus ¶
GetTaskStatus 获取任务状态的便捷端点 GET /a2a/{agentId}/tasks/{taskId}
func (*Handler) HandleBatchJSONRPC ¶
HandleBatchJSONRPC 处理批量 JSON-RPC 2.0 请求 POST /a2a/{agentId}/batch
func (*Handler) HandleJSONRPC ¶
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 移除取消信号
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 ¶
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 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 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 (*Server) GetAgentCard ¶
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 (*Task) UpdateStatus ¶
UpdateStatus 更新任务状态
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 方法返回结果