- 人工智能
- AI 应用
- 交互助手
- AI Agent
【免费下载链接】ironclaw
IronClaw is an Agent OS focused on privacy, security and extensibility
本篇文章聚焦 IronClaw 开源仓库中 Google Sheets 扩展包(google-sheets)的核心读取操作get_spreadsheet,围绕其官方工具提示词文档(get_spreadsheet.md),结合同包内的输入 Schema、扩展清单manifest.toml以及 WASM 客端源码,完整剖析该操作"由谁调用、传什么参数、如何解析 ID、返回什么结构、如何鉴权"的端到端实现。读完本文,你将掌握在 IronClaw 的沙箱化扩展体系下安全获取 Google Sheets 表格元数据的调用契约与底层原理,并能在开发或调试扩展时快速定位关键代码。
一、操作定位:基于 capability 的工具提示词
在 IronClaw 的扩展体系里,每个可执行操作都由三件套定义:manifest.toml中的工具声明(含 capability id)、prompts/下的模型提示词(说明何时使用、如何使用)、schemas/下的输入 JSON Schema(约束参数结构)。get_spreadsheet正是其中的只读元数据操作,对应 capability idgoogle-sheets.get_spreadsheet。
其提示词文档原文仅用三句话完整定义了该操作的调用约定:
- 通过 spreadsheet ID 获取 spreadsheet 元数据(Get spreadsheet metadata by spreadsheet ID)。
- 如果用户只提供了 spreadsheet 的名称/标题,应先用 Google Drive 的
google-drive.list_files查找该文件的 ID。- host 根据 capability id 选择此操作;只提供 input schema 描述的参数,不要包含 action 字段。
这三点分别回答了"调什么""ID 从哪来""参数怎么传"三个关键问题。其中"由 host 根据 capability id 选择操作"体现了 IronClaw 的分派机制——模型侧无需自行拼装 action 名,从而避免调用方伪造操作。
二、输入契约:spreadsheet_id 唯一必填
get_spreadsheet的输入由 get_spreadsheet.input.v1.json 约束,采用 JSON Schema draft-07 格式:
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "Google Sheets get_spreadsheet", "description": "Get spreadsheet metadata.", "type": "object", "required": ["spreadsheet_id"], "properties": { "spreadsheet_id": { "type": "string", "description": "The spreadsheet ID." } }, "additionalProperties": false }要点解析:
spreadsheet_id(必填):Google Sheets 的表格 ID,其本质与 Google Drive 的文件 ID 相同。该 ID 出现在表格 URL 中(https://docs.google.com/spreadsheets/d/<SPREADSHEET_ID>/edit)。additionalProperties: false:禁止传入任何额外字段,参数必须严格符合 schema。- 无
action字段:与提示词文档一致——操作类型由 host 依据 capability id 注入,调用方传 action 会被拒绝(详见下文源码分析)。
这是google-sheets包中参数最精简的操作之一,因为元数据读取不需要 range、values 等数据操作参数,仅凭 ID 即可命中资源。
三、ID 解析路径:名称 → Drive 搜索 → ID
提示词文档明确要求:当用户只给出"文件名/标题"而非 ID 时,不能直接猜测 ID,而应调用同属 Google 产品族的google-drive.list_files工具先行搜索。
该工具在 google-drive/manifest.toml 中声明(id = "google-drive.list_files",配有自己的 input schema 与 prompt doc),其定位在 google-sheets 包 README 中也有呼应——"Use Google Drive list_files to find existing spreadsheets by name/title"。
推荐的解析流程:
- 判断用户输入:若为形如
1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms的长字符串,视为 ID 直接使用; - 若为自然语言名称/标题(如"Q1 销售报表"),调用
google-drive.list_files(可按名称检索),从返回结果中提取目标文件的id; - 将取得的 ID 作为
spreadsheet_id调用get_spreadsheet。
从源码结构看,google-drive与google-sheets同属vendor.google管理组,共享google_oauth_client_id/google_oauth_client_secret部署级凭据(见 manifest.toml 的[admin_configuration]),因此两者的凭据与账号体系天然互通,跨工具协作无需额外授权。
四、底层实现:WASM 客端的元数据拉取
google-sheets是一个data-only 包(无 Rust crate),可执行逻辑以 WASM 客端形式打包(产物wasm/google_sheets_tool.wasm),源码位于 wasm-src/src。get_spreadsheet的实现位于 api.rs:
/// Get spreadsheet metadata. pub fn get_spreadsheet(spreadsheet_id: &str) -> Result<SpreadsheetMetadata, GuestFailure> { let path = format!( "{}?fields=spreadsheetId,properties.title,spreadsheetUrl,sheets.properties,namedRanges", url_encode(spreadsheet_id) ); let response = api_call("GET", &path, None)?; let parsed: serde_json::Value = serde_json::from_str(&response).map_err(|e| serialization_failure(&e))?; Ok(SpreadsheetMetadata { spreadsheet_id: parsed["spreadsheetId"].as_str().unwrap_or("").to_string(), title: parsed["properties"]["title"].as_str().unwrap_or("").to_string(), url: parsed["spreadsheetUrl"].as_str().unwrap_or("").to_string(), sheets: parsed["sheets"] .as_array() .map(|arr| arr.iter().map(parse_sheet_info).collect()) .unwrap_or_default(), named_ranges: parsed["namedRanges"] .as_array() .map(|arr| arr.iter().map(parse_named_range).collect()) .unwrap_or_default(), }) }4.1 请求构造
- 方法:
GET; - 基础地址:常量
SHEETS_API_BASE = "https://sheets.googleapis.com/v4/spreadsheets"(api.rs),路径为{SHEETS_API_BASE}/{url_encode(spreadsheet_id)}; - 字段裁剪:通过
fields参数只请求spreadsheetId, properties.title, spreadsheetUrl, sheets.properties, namedRanges,避免拉取整表数据,显著降低响应体积与带宽开销; - ID 编码:
spreadsheet_id经url_encode处理,保证特殊字符安全。
4.2 统一出站通道与凭据隔离
所有 API 调用统一走api_call→host::http_request(api.rs),由 host 侧负责凭据注入与限流。文件头部注释点明安全设计关键(api.rs):
All API calls go through the host's HTTP capability, which handles credential injection and rate limiting.The WASM tool never sees the actual OAuth token.
即:WASM 客端永远接触不到真实的 OAuth Token,凭据由宿主注入,这是 IronClaw 隐私/安全定位在扩展层的直接体现。
4.3 响应解析与返回结构
响应按SpreadsheetMetadata结构(定义于 types.rs)序列化返回:
pub struct SpreadsheetMetadata { pub spreadsheet_id: String, pub title: String, pub url: String, pub sheets: Vec<SheetInfo>, // 空时省略 named_ranges 字段 #[serde(skip_serializing_if = "Vec::is_empty")] pub named_ranges: Vec<NamedRange>, }其中嵌套结构:
| 结构 | 字段 | 说明 |
|---|---|---|
SheetInfo(每个 sheet/tab) | sheet_id、title、index、row_count、column_count | 由parse_sheet_info从properties/properties.gridProperties解析(api.rs),数值缺失时回退为 0 |
NamedRange(命名区域) | named_range_id、name、range | range由format_grid_range渲染为sheetId=…, rows a:b, cols c:d人类可读形式(api.rs) |
sheet_id是数字型ID(非 sheet 名称),这是后续format_cells、delete_sheet、rename_sheet等操作必需的输入——因此get_spreadsheet也是这些表格管理操作的前置步骤(见 lib.rs 的 Tips:"Sheet IDs (numeric) are different from sheet names. Get them via get_spreadsheet.")。
4.4 分派与参数防注入
execute_inner通过action_from_context从 host 注入的调用上下文(ToolContext.capability_id)解析出本次动作名,再经params_with_action将动作注入参数(lib.rs):
- 若调用方参数中已包含
action字段,直接返回invalid_parameters输入错误(测试params_with_action_rejects_caller_supplied_action验证了此行为,lib.rs); - 若
capability_id不在白名单,返回unsupported_google_sheets_capability。
这从机制上落实了提示词中"只提供 schema 描述的参数,不要包含 action 字段"的约定。
五、权限模型:只读 Scope 与门控
manifest.toml 中google-sheets.get_spreadsheet的完整声明:
[[tools]] origin_gate_matrix = { loop_run = "gated_unless_granted", product = "forbidden", automation = "forbidden" } id = "google-sheets.get_spreadsheet" description = "Get spreadsheet metadata by spreadsheet ID. If the user only provided a spreadsheet name/title, search Google Drive first." effects = ["network", "use_secret"] default_permission = "ask" visibility = "model" input_schema_ref = "schemas/google-sheets/get_spreadsheet.input.v1.json" prompt_doc_ref = "prompts/google-sheets/get_spreadsheet.md" [[tools.credentials]] handle = "google_runtime_token" vendor = "google" scopes = ["https://www.googleapis.com/auth/spreadsheets.readonly"] audience = { scheme = "https", host = "sheets.googleapis.com" } injection = { type = "header", name = "authorization", prefix = "Bearer " }安全设计要点:
- 最小权限 Scope:
get_spreadsheet仅申请spreadsheets.readonly,而write_values、append_values、format_cells等写操作申请的是spreadsheets(manifest.toml)。按操作粒度拆分 scope,读取不持有写权限。 - 效果声明:
effects = ["network", "use_secret"]——需要出网且使用密钥,但不含external_write,属于纯读取操作。 - 默认许可:
default_permission = "ask",即每次调用需经用户/审批门控确认。 - 来源门控矩阵:
loop_run = "gated_unless_granted"(循环运行场景需显式授权),product与automation场景直接forbidden——限制了该工具的暴露面。 - 凭据注入:OAuth token 以
Authorization: Bearer <token>头注入,目标 audience 限定为sheets.googleapis.com;同时[auth.google]段配置了 OAuth2 授权码流程与 PKCE(S256)等参数(manifest.toml)。
六、错误处理:可预期的失败语义
get_spreadsheet的错误均映射为带kind+code的GuestFailure,便于 host 与模型侧做稳定分支(api.rs):
| HTTP 状态 | kind | code | 说明 |
|---|---|---|---|
| 401 | AuthRequired | google_api_error_status_401 | 凭据失效/未授权,触发重新授权流程 |
| 其他非 2xx | Client | api_status_{status} | 如 429 限流、404 表格不存在等,消息附带 API 原文 |
| 传输层失败 | 依HttpErrorKind映射 | google_api_transport_error等 | 网络拒绝、执行器失败等 |
WASM 客端内置单测验证了关键映射(api.rs):401 →AuthRequired、429 →Client且保留错误正文片段。所有错误消息经bounded_message截断至 512 字符,防止不受控的超长字符串流入下游(api.rs)。
七、与表格操作族的协作
get_spreadsheet在google-sheets包的 11 个工具中扮演"目录与入口"角色(完整清单见 README):
- ID 确认:
create_spreadsheet返回新建表格的spreadsheet_id与url;之后可用get_spreadsheet复核元数据。 - Sheet 定位:
read_values/write_values/append_values需要 A1 记法 range(如Sheet1!A1:D10);format_cells、delete_sheet、rename_sheet需要数字 sheet_id——两者都可通过get_spreadsheet返回的sheets数组获得(lib.rs 的 Tips 对此有明确说明)。 - 命名区域发现:返回的
named_ranges可让模型直接以名称引用预定义区域,避免硬编码行列号。
在模型侧,一个典型的多步调用序列为:google-drive.list_files(按标题搜 ID)→google-sheets.get_spreadsheet(拿元数据与 sheet_id)→google-sheets.read_values(按 A1 区域读数据)→ 视需要format_cells等写操作。每一步都遵循各自的 schema 与权限声明。
八、测试与验证方式
该包的可验证性保障包括:
- 清单投影测试:
cargo test -p ironclaw_extension_registry会校验manifest.toml的工具声明、schema 引用与凭据配置的一致性(见 README 的 Tests / checks 一节); - WASM 产物新鲜度检查:
python3 scripts/ci/check-wasm-artifact-freshness.py确保wasm/google_sheets_tool.wasm与wasm-src/源码同步,防止发布过期二进制; - 客端单元测试:
api.rs与lib.rs内置的#[cfg(test)]覆盖了错误映射与参数防注入等关键行为。
这些测试共同保证了"提示词 → schema → manifest → WASM 实现"四层契约的一致性。
九、小结
google-sheets.get_spreadsheet是 IronClaw 扩展体系中"只读元数据操作"的典型样本:提示词文档定义了模型侧的最小调用约定(凭 ID 读取、名称先走 Drive 搜索、不传 action);输入 schema 将契约收敛为单一必填参数;WASM 客端通过 host 统一出站通道完成字段裁剪的 GET 请求并返回结构化的表格/Sheet/命名区域元数据;manifest 则从 scope、effects、门控矩阵与凭据注入四个维度落实了最小权限与安全隔离。理解这一操作,即可触类旁通地掌握整个google-sheets包乃至 Google 产品族扩展的调用与实现模式。
- 人工智能
- AI 应用
- 交互助手
- AI Agent
【免费下载链接】ironclaw
IronClaw is an Agent OS focused on privacy, security and extensibility
相关推荐
如何在Obsidian中无缝管理电子表格?终极Excel插件完整指南
如何在Obsidian中无缝管理电子表格?终极Excel插件完整指南 你是否曾为在笔记软件中处理表格数据而烦恼?当需要在Obsidian中创建预算表、项目进度表
人工智能AI 应用交互助手AI AgentCherry Studio gh-pr-review 代码评审清单:A/B/C 三级检查体系与项目规则落地
Cherry Studio gh pr review 代码评审清单:A/B/C 三级检查体系与项目规则落地 本文以 Cherry Studio 仓库中的自动化代
人工智能AI 应用交互助手AI AgentIronClaw google-docs 扩展完全指南:语义化文档工作流与 WASM 工具实现解析
IronClaw google docs 扩展完全指南:语义化文档工作流与 WASM 工具实现解析 本文以 IronClaw 开源仓库中 google docs
人工智能AI 应用交互助手AI Agent
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考