42 lines
1.3 KiB
Markdown
42 lines
1.3 KiB
Markdown
|
|
# 创建 getTransactions 工具
|
||
|
|
|
||
|
|
让我们创建一个从 Google 表格获取交易数据的工具。我们将创建一个名为 `tools/get-transactions-tool.ts` 的新文件。
|
||
|
|
|
||
|
|
首先,在 src/mastra/tools/get-transactions-tool.ts 创建新的工具文件
|
||
|
|
|
||
|
|
现在,添加必要的导入:
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
import { createTool } from "@mastra/core/tools";
|
||
|
|
import { z } from "zod";
|
||
|
|
```
|
||
|
|
|
||
|
|
现在,让我们创建我们的工具:
|
||
|
|
|
||
|
|
```typescript
|
||
|
|
export const getTransactionsTool = createTool({
|
||
|
|
id: "get-transactions",
|
||
|
|
description: "从 Google 表格获取交易数据",
|
||
|
|
inputSchema: z.object({}), // 不需要输入参数
|
||
|
|
outputSchema: z.object({
|
||
|
|
csvData: z.string(),
|
||
|
|
}),
|
||
|
|
execute: async () => {
|
||
|
|
return await getTransactions();
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
const getTransactions = async () => {
|
||
|
|
// 此 URL 指向包含交易数据的公共 Google 表格
|
||
|
|
const url =
|
||
|
|
"https://docs.google.com/spreadsheets/d/e/2PACX-1vTQWaCzJAFsF4owWRHQRLo4G0-ERv31c74OOZFnqLiTLaP7NweoiX7IXvzQud2H6bdUPnIqZEA485Ux/pub?gid=0&single=true&output=csv";
|
||
|
|
const response = await fetch(url);
|
||
|
|
const data = await response.text();
|
||
|
|
return {
|
||
|
|
csvData: data,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
```
|
||
|
|
|
||
|
|
此工具从公共 Google 表格获取交易数据并将其作为字符串返回。Mastra 的 `createTool` 函数让您可以轻松定义工具的 ID、描述、输入和输出模式以及执行逻辑。
|