P1822分钟
多模态与 GUI Agent
你到目前为止构建的每个 Agent 都在纯文本世界中运行——读取字符串、调用函数、产出文本。 但很多真实界面并不以文本或 API 暴露。本课教 Agent 去"看":理解图像、导航 GUI、 通过感知-决策-动作循环自动化视觉工作流。
SDK Focusimage content blockstool usestructured outputmulti-step loops
为什么 Agent 需要视觉
文本工具在你控制接口时工作良好——API、CLI、结构化数据。但许多现实系统只暴露视觉界面:
text
文本工具胜任:
数据库 → SQL 查询 → 结构化结果
API → HTTP 请求 → JSON 响应
CLI → 命令 → 文本输出
文本工具失败:
遗留企业应用 → 只有 GUI,没有 API
带图表的 PDF → OCR 遗漏的视觉数据
视频内容 → 时序视觉信息
Web 应用 → 随交互变化的动态 UI多模态 Agent 将 P2 的工具使用模式扩展到视觉领域。不再是 function_call → text_result,循环变为 screenshot → visual_understanding → action。
多模态输入
图像理解
现代 LLM 接受图像作为输入,与文本并列。Agent 可以分析截图、图表、文档和照片:
typescript
import { query, type SDKUserMessage } from "@anthropic-ai/claude-agent-sdk";
// `query()` 接受 `prompt: string | AsyncIterable<SDKUserMessage>`。
// 要发送图像内容块,需要将其包装成流式 user message。
async function* userInput(screenshotBase64: string): AsyncIterable<SDKUserMessage> {
yield {
type: "user",
session_id: "",
parent_tool_use_id: null,
message: {
role: "user",
content: [
{
type: "image",
source: { type: "base64", media_type: "image/png", data: screenshotBase64 },
},
{
type: "text",
text:
"Describe the UI elements visible in this screenshot. " +
"List all buttons, input fields, and their current states.",
},
],
},
};
}
const response = query({ prompt: userInput(screenshotBase64) });
// query() 返回 AsyncIterable——流式处理消息并取最终结果。
for await (const message of response) {
if (message.type === "result" && message.subtype === "success") {
console.log(message.result);
}
}