refactor(agent): 移除未使用的工具功能

删除天气、时间、文件列表和笑话工具及相关代码,仅保留计算器和商品查询功能
更新代理描述和测试路由以反映功能变更
This commit is contained in:
dzq 2026-01-04 11:45:33 +08:00
parent 726befe2fa
commit eada12cbaf
7 changed files with 9 additions and 241 deletions

View File

@ -1,10 +1,6 @@
import { Agent } from "@mastra/core/agent"; import { Agent } from "@mastra/core/agent";
import { import {
weatherTool,
timeTool,
calculatorTool, calculatorTool,
filelistTool,
jokeTool,
goodsTool, goodsTool,
} from "../tools"; } from "../tools";
import { createDeepSeek } from '@ai-sdk/deepseek'; import { createDeepSeek } from '@ai-sdk/deepseek';
@ -43,12 +39,8 @@ export const multiFunctionAgent = new Agent({
content: ` content: `
1. ****使 1. ****
2. **** 2. ****使ID获取单个商品详情
3. ****
4. ****
5. ****
6. ****使ID获取单个商品详情
@ -63,11 +55,7 @@ export const multiFunctionAgent = new Agent({
model: deepseek.chat('mimo-v2-flash'), model: deepseek.chat('mimo-v2-flash'),
memory: memory, memory: memory,
tools: { tools: {
weatherTool,
timeTool,
calculatorTool, calculatorTool,
filelistTool,
jokeTool,
goodsTool, goodsTool,
}, },
}); });

View File

@ -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,
};
},
});

View File

@ -1,6 +1,2 @@
export { weatherTool } from "./weather-tool";
export { timeTool } from "./time-tool";
export { calculatorTool } from "./calculator-tool"; export { calculatorTool } from "./calculator-tool";
export { filelistTool } from "./filelist-tool"; export { goodsTool } from "./goods-tool";
export { jokeTool } from "./joke-tool";
export { goodsTool } from "./goods-tool";

View File

@ -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],
};
},
});

View File

@ -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,
};
},
});

View File

@ -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}&current=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] || "未知";
}

View File

@ -14,7 +14,7 @@ router.get('/', (req, res) => {
res.json({ res.json({
name: agent.name, 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), tools: Object.keys(agent.tools),
}); });
}); });
@ -85,38 +85,6 @@ router.post('/test-tools', async (req, res) => {
const testResults = []; 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 // Test calculator tool
try { try {
const calcResponse = await agent.generate('Calculate 15 * 3'); 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 { try {
const fileResponse = await agent.generate('List files in current directory'); const goodsResponse = await agent.generate('Query product list with page 1');
testResults.push({ testResults.push({
tool: 'filelistTool', tool: 'goodsTool',
success: true, success: true,
response: fileResponse.text.substring(0, 100) + '...', response: goodsResponse.text.substring(0, 100) + '...',
}); });
} catch (error) { } catch (error) {
testResults.push({ testResults.push({
tool: 'filelistTool', tool: 'goodsTool',
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',
success: false, success: false,
error: error instanceof Error ? error.message : 'Unknown error', error: error instanceof Error ? error.message : 'Unknown error',
}); });