refactor(agent): 移除未使用的工具功能
删除天气、时间、文件列表和笑话工具及相关代码,仅保留计算器和商品查询功能 更新代理描述和测试路由以反映功能变更
This commit is contained in:
parent
726befe2fa
commit
eada12cbaf
|
|
@ -1,10 +1,6 @@
|
|||
import { Agent } from "@mastra/core/agent";
|
||||
import {
|
||||
weatherTool,
|
||||
timeTool,
|
||||
calculatorTool,
|
||||
filelistTool,
|
||||
jokeTool,
|
||||
goodsTool,
|
||||
} from "../tools";
|
||||
import { createDeepSeek } from '@ai-sdk/deepseek';
|
||||
|
|
@ -43,12 +39,8 @@ export const multiFunctionAgent = new Agent({
|
|||
content: `
|
||||
你是一个具有内存功能的多功能助手,具备以下能力:
|
||||
|
||||
1. **天气信息**:使用天气工具提供任何地点的当前天气。
|
||||
2. **时间查询**:告知当前时间和时区。
|
||||
3. **计算器**:执行基本算术运算(加、减、乘、除)。
|
||||
4. **文件列表**:列出目录中的文件(默认当前目录)。
|
||||
5. **笑话**:讲随机笑话调节气氛。
|
||||
6. **商品查询**:使用商品工具查询商品信息,支持分页查询商品列表或根据商品ID获取单个商品详情。
|
||||
1. **计算器**:执行基本算术运算(加、减、乘、除)。
|
||||
2. **商品查询**:使用商品工具查询商品信息,支持分页查询商品列表或根据商品ID获取单个商品详情。
|
||||
|
||||
请始终礼貌、乐于助人。如果用户询问超出你能力范围的事情,请礼貌解释你能做什么。
|
||||
你可以记住之前的对话和用户偏好。
|
||||
|
|
@ -63,11 +55,7 @@ export const multiFunctionAgent = new Agent({
|
|||
model: deepseek.chat('mimo-v2-flash'),
|
||||
memory: memory,
|
||||
tools: {
|
||||
weatherTool,
|
||||
timeTool,
|
||||
calculatorTool,
|
||||
filelistTool,
|
||||
jokeTool,
|
||||
goodsTool,
|
||||
},
|
||||
});
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
import { createTool } from "@mastra/core/tools";
|
||||
import { z } from "zod";
|
||||
import fs from "fs/promises";
|
||||
import path from "path";
|
||||
|
||||
export const filelistTool = createTool({
|
||||
id: "list-files",
|
||||
description: "列出当前目录中的文件",
|
||||
inputSchema: z.object({
|
||||
directory: z.string().optional().describe("目录路径(默认:当前目录)"),
|
||||
}),
|
||||
outputSchema: z.object({
|
||||
files: z.array(z.string()),
|
||||
directory: z.string(),
|
||||
}),
|
||||
execute: async ({ context }) => {
|
||||
const targetDir = context.directory ? path.resolve(context.directory) : process.cwd();
|
||||
const files = await fs.readdir(targetDir);
|
||||
return {
|
||||
files,
|
||||
directory: targetDir,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
|
@ -1,6 +1,2 @@
|
|||
export { weatherTool } from "./weather-tool";
|
||||
export { timeTool } from "./time-tool";
|
||||
export { calculatorTool } from "./calculator-tool";
|
||||
export { filelistTool } from "./filelist-tool";
|
||||
export { jokeTool } from "./joke-tool";
|
||||
export { goodsTool } from "./goods-tool";
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
import { createTool } from "@mastra/core/tools";
|
||||
import { z } from "zod";
|
||||
|
||||
const jokes = [
|
||||
"Why don't scientists trust atoms? Because they make up everything!",
|
||||
"Why did the scarecrow win an award? Because he was outstanding in his field!",
|
||||
"What do you call a fake noodle? An impasta!",
|
||||
"Why don't eggs tell jokes? They'd crack each other up!",
|
||||
"How does a penguin build its house? Igloos it together!",
|
||||
"Why did the math book look so sad? Because it had too many problems.",
|
||||
"What do you call a bear with no teeth? A gummy bear!",
|
||||
"Why did the bicycle fall over? Because it was two-tired!",
|
||||
"What do you call a fish wearing a bowtie? Sofishticated!",
|
||||
"Why can't you give Elsa a balloon? Because she will let it go!",
|
||||
];
|
||||
|
||||
export const jokeTool = createTool({
|
||||
id: "tell-joke",
|
||||
description: "讲一个随机笑话",
|
||||
inputSchema: z.object({}),
|
||||
outputSchema: z.object({
|
||||
joke: z.string(),
|
||||
}),
|
||||
execute: async () => {
|
||||
const randomIndex = Math.floor(Math.random() * jokes.length);
|
||||
return {
|
||||
joke: jokes[randomIndex],
|
||||
};
|
||||
},
|
||||
});
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
import { createTool } from "@mastra/core/tools";
|
||||
import { z } from "zod";
|
||||
|
||||
export const timeTool = createTool({
|
||||
id: "get-time",
|
||||
description: "获取当前时间",
|
||||
inputSchema: z.object({}),
|
||||
outputSchema: z.object({
|
||||
currentTime: z.string(),
|
||||
timezone: z.string(),
|
||||
}),
|
||||
execute: async () => {
|
||||
const now = new Date();
|
||||
return {
|
||||
currentTime: now.toISOString(),
|
||||
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
import { createTool } from "@mastra/core/tools";
|
||||
import { z } from "zod";
|
||||
|
||||
interface WeatherResponse {
|
||||
current: {
|
||||
time: string;
|
||||
temperature_2m: number;
|
||||
apparent_temperature: number;
|
||||
relative_humidity_2m: number;
|
||||
wind_speed_10m: number;
|
||||
wind_gusts_10m: number;
|
||||
weather_code: number;
|
||||
};
|
||||
}
|
||||
|
||||
export const weatherTool = createTool({
|
||||
id: "get-weather",
|
||||
description: "获取指定地点的当前天气",
|
||||
inputSchema: z.object({
|
||||
location: z.string().describe("城市名称"),
|
||||
}),
|
||||
outputSchema: z.object({
|
||||
temperature: z.number(),
|
||||
feelsLike: z.number(),
|
||||
humidity: z.number(),
|
||||
windSpeed: z.number(),
|
||||
windGust: z.number(),
|
||||
conditions: z.string(),
|
||||
location: z.string(),
|
||||
}),
|
||||
execute: async ({ context }) => {
|
||||
return await getWeather(context.location);
|
||||
},
|
||||
});
|
||||
|
||||
const getWeather = async (location: string) => {
|
||||
const geocodingUrl = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(location)}&count=1`;
|
||||
const geocodingResponse = await fetch(geocodingUrl);
|
||||
const geocodingData = await geocodingResponse.json();
|
||||
|
||||
if (!geocodingData.results?.[0]) {
|
||||
throw new Error(`Location '${location}' not found`);
|
||||
}
|
||||
|
||||
const { latitude, longitude, name } = geocodingData.results[0];
|
||||
|
||||
const weatherUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m,apparent_temperature,relative_humidity_2m,wind_speed_10m,wind_gusts_10m,weather_code`;
|
||||
|
||||
const response = await fetch(weatherUrl);
|
||||
const data: WeatherResponse = await response.json();
|
||||
|
||||
return {
|
||||
temperature: data.current.temperature_2m,
|
||||
feelsLike: data.current.apparent_temperature,
|
||||
humidity: data.current.relative_humidity_2m,
|
||||
windSpeed: data.current.wind_speed_10m,
|
||||
windGust: data.current.wind_gusts_10m,
|
||||
conditions: getWeatherCondition(data.current.weather_code),
|
||||
location: name,
|
||||
};
|
||||
};
|
||||
|
||||
function getWeatherCondition(code: number): string {
|
||||
const conditions: Record<number, string> = {
|
||||
0: "晴天",
|
||||
1: "基本晴朗",
|
||||
2: "局部多云",
|
||||
3: "阴天",
|
||||
45: "有雾",
|
||||
48: "结霜雾",
|
||||
51: "小雨",
|
||||
53: "中雨",
|
||||
55: "大雨",
|
||||
56: "轻微冻雨",
|
||||
57: "严重冻雨",
|
||||
61: "小雨",
|
||||
63: "中雨",
|
||||
65: "大雨",
|
||||
66: "轻微冻雨",
|
||||
67: "严重冻雨",
|
||||
71: "小雪",
|
||||
73: "中雪",
|
||||
75: "大雪",
|
||||
77: "雪粒",
|
||||
80: "小雨阵雨",
|
||||
81: "中雨阵雨",
|
||||
82: "暴雨阵雨",
|
||||
85: "小雪阵雨",
|
||||
86: "大雪阵雨",
|
||||
90: "雷暴",
|
||||
96: "雷暴伴轻微冰雹",
|
||||
99: "雷暴伴严重冰雹",
|
||||
};
|
||||
return conditions[code] || "未知";
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@ router.get('/', (req, res) => {
|
|||
|
||||
res.json({
|
||||
name: agent.name,
|
||||
description: 'Multi-function agent with weather, time, calculator, file listing, and joke capabilities',
|
||||
description: 'Multi-function agent with calculator and goods query capabilities',
|
||||
tools: Object.keys(agent.tools),
|
||||
});
|
||||
});
|
||||
|
|
@ -85,38 +85,6 @@ router.post('/test-tools', async (req, res) => {
|
|||
|
||||
const testResults = [];
|
||||
|
||||
// Test weather tool
|
||||
try {
|
||||
const weatherResponse = await agent.generate('What is the weather in Tokyo?');
|
||||
testResults.push({
|
||||
tool: 'weatherTool',
|
||||
success: true,
|
||||
response: weatherResponse.text.substring(0, 100) + '...',
|
||||
});
|
||||
} catch (error) {
|
||||
testResults.push({
|
||||
tool: 'weatherTool',
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
});
|
||||
}
|
||||
|
||||
// Test time tool
|
||||
try {
|
||||
const timeResponse = await agent.generate('What time is it?');
|
||||
testResults.push({
|
||||
tool: 'timeTool',
|
||||
success: true,
|
||||
response: timeResponse.text.substring(0, 100) + '...',
|
||||
});
|
||||
} catch (error) {
|
||||
testResults.push({
|
||||
tool: 'timeTool',
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
});
|
||||
}
|
||||
|
||||
// Test calculator tool
|
||||
try {
|
||||
const calcResponse = await agent.generate('Calculate 15 * 3');
|
||||
|
|
@ -133,33 +101,17 @@ router.post('/test-tools', async (req, res) => {
|
|||
});
|
||||
}
|
||||
|
||||
// Test filelist tool
|
||||
// Test goods tool
|
||||
try {
|
||||
const fileResponse = await agent.generate('List files in current directory');
|
||||
const goodsResponse = await agent.generate('Query product list with page 1');
|
||||
testResults.push({
|
||||
tool: 'filelistTool',
|
||||
tool: 'goodsTool',
|
||||
success: true,
|
||||
response: fileResponse.text.substring(0, 100) + '...',
|
||||
response: goodsResponse.text.substring(0, 100) + '...',
|
||||
});
|
||||
} catch (error) {
|
||||
testResults.push({
|
||||
tool: 'filelistTool',
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
});
|
||||
}
|
||||
|
||||
// Test joke tool
|
||||
try {
|
||||
const jokeResponse = await agent.generate('Tell me a joke');
|
||||
testResults.push({
|
||||
tool: 'jokeTool',
|
||||
success: true,
|
||||
response: jokeResponse.text.substring(0, 100) + '...',
|
||||
});
|
||||
} catch (error) {
|
||||
testResults.push({
|
||||
tool: 'jokeTool',
|
||||
tool: 'goodsTool',
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error',
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue