外观
Agent Client
创建 Client
typescript
import { createAgentClient } from '@multi-agent/a2a'
const client = createAgentClient({
agentId: 'target-agent',
address: 'a2a://localhost:50061',
})调用技能
typescript
const stream = await client.call('skillName', { param1: 'value' })
for await (const msg of stream) {
if (msg.type === 'progress') console.log('进度:', msg.text)
if (msg.type === 'done') console.log('结果:', msg.data)
if (msg.type === 'error') console.error('错误:', msg.text)
}流结束机制
Server Handler 执行完毕后,框架自动结束流,for await 循环自动退出,无需手动 break。
传递 Metadata
typescript
const stream = await client.call('task', params, {
metadata: {
'x-user-id': 'user-123',
'x-trace-id': 'trace-abc',
},
})取消请求
typescript
const controller = new AbortController()
const stream = await client.call('longTask', params, {
signal: controller.signal,
})
// 5 秒后取消
setTimeout(() => controller.abort(), 5000)
for await (const msg of stream) {
if (msg.type === 'progress') console.log(msg.text)
if (msg.type === 'done') console.log(msg.data)
}
// 取消后循环正常退出回答问题
当 Server 发送 question 消息时:
typescript
for await (const msg of stream) {
if (msg.type === 'question') {
stream.send({
type: 'answer',
data: { questionId: msg.data.questionId, answer: 'yes' },
})
}
if (msg.type === 'done') console.log('完成:', msg.data)
}获取 Agent 信息
typescript
const card = await client.getAgentCard()
console.log(card.name) // 名称
console.log(card.version) // 版本
console.log(card.skills) // 技能列表
console.log(card.defaultSkill) // 默认技能健康检查
typescript
const healthy = await client.checkHealth()
if (!healthy) {
console.log('Agent 不可用')
}关闭连接
typescript
await client.close()使用插件
typescript
import { createAgentClient, createTracingPlugin } from '@multi-agent/a2a'
const client = createAgentClient(config)
.use(createTracingPlugin({ provider }))API 速查
| 方法 | 说明 |
|---|---|
call(skill, params, options?) | 调用技能,返回双向流 |
getAgentCard() | 获取 Agent 信息 |
checkHealth() | 健康检查 |
close() | 关闭连接 |
use(plugin) | 注册插件 |