用 JavaScript navigator.modelContext.registerTool() 動態註冊工具。
適合需要複雜邏輯、動態資料或非表單互動的情境。
navigator.modelContext.registerTool() 方法,你可以註冊任意數量的工具。
每個工具包含 name(名稱)、description(描述)、inputSchema(JSON Schema 格式的參數定義)和
execute(async callback,Agent 呼叫時執行)。
registerTool(tool) 新增工具 |
unregisterTool(name) 移除工具 |
clearContext() 清除全部 |
provideContext(options) 批次設定
navigator.modelContext.registerTool({
name: "add_todo",
description: "Add a new todo item to the list",
inputSchema: {
type: "object",
properties: {
text: {
type: "string",
description: "The todo item text"
}
},
required: ["text"]
},
execute: async ({ text }) => {
// 你的實作邏輯
todos.push({ text, done: false });
return {
content: [{
type: "text",
text: JSON.stringify({ success: true })
}]
};
}
});
Agent 可以透過上述 4 個工具管理待辦事項。試試在 DevTools Console 呼叫它們。
Agent 可以透過 calculate 工具進行數學運算。
支援基本運算(+, -, *, /)和括號。
模擬天氣 API。預設城市:Taipei、Tokyo、New York、London、San Francisco。 其他城市會隨機產生天氣資料。