外观
Message
结构
typescript
interface Message {
messageId?: string // 自动生成
timestamp?: number // 自动生成
type: string // 消息类型
text: string // 人类可读文本
data?: any // 结构化数据
from?: AgentCard // 来源 Agent(自动注入)
}消息类型
| 类型 | 方向 | 用途 |
|---|---|---|
call | Client → Server | 调用技能 |
cancel | Client → Server | 取消执行 |
answer | Client → Server | 回答问题 |
progress | Server → Client | 进度更新 |
question | Server → Client | 询问用户 |
done | Server → Client | 执行完成 |
error | Server → Client | 错误信息 |
发送消息
Server 端
typescript
// 发送进度
ctx.stream.send({
type: 'progress',
text: '处理中...',
data: { percent: 50 }
})
// 发送问题
ctx.stream.send({
type: 'question',
text: '选择选项',
data: { questionId: 'q1', options: ['A', 'B'] }
})
// 返回结果(自动转为 done)
return { result: 'success' }Client 端
typescript
// 回答问题
stream.send({
type: 'answer',
text: '用户选择',
data: { questionId: 'q1', answer: 'A' }
})
// 取消
stream.send({ type: 'cancel', text: '用户取消' })
// 或
stream.cancel('用户取消')接收消息
typescript
for await (const msg of stream) {
switch (msg.type) {
case 'progress':
console.log(msg.text)
break
case 'question':
stream.send({
type: 'answer',
data: { questionId: msg.data.questionId, answer: 'yes' }
})
break
case 'done':
return msg.data
case 'error':
throw new Error(msg.text)
}
}data 结构示例
typescript
// call
{ skill: 'execute', params: { code: '...' } }
// progress
{ current: 50, total: 100, percent: 50 }
// question
{ questionId: 'env', options: ['dev', 'prod'] }
// answer
{ questionId: 'env', answer: 'prod' }
// done
{ result: { ... } }
// error
{ code: 'FILE_NOT_FOUND', path: '/...' }