/**
* 根据 ID 获取项目详情工具
*/
import { ToolResult } from '../types';
import { ZenTaoClient } from '../client';
import { IProject } from '../types';
import { LRUCache } from '../utils/cache';
export async function getProjectByIdHandler(
client: ZenTaoClient,
cache: LRUCache<string, any>,
projectId: number
): Promise<ToolResult> {
try {
// 验证输入
if (!projectId || isNaN(projectId)) {
return {
content: [
{
type: 'text',
text: '错误:项目 ID 必须是一个有效的数字',
},
],
isError: true,
};
}
// 生成缓存键
const cacheKey = `project:${projectId}`;
// 尝试从缓存获取
const cached = cache.get(cacheKey);
if (cached) {
const output = `从缓存获取项目详情:\n\n项目信息:\n ID: ${cached.id}\n 名称: ${cached.name}\n 代码: ${cached.code}\n 状态: ${cached.status}\n 开始时间: ${cached.begin}\n 结束时间: ${cached.end || '未设置'}\n 团队: ${cached.team || '未设置'}\n 权限控制: ${cached.acl || '默认'}`;
return {
content: [
{
type: 'text',
text: output,
},
],
};
}
// 调用 API
const project = await client.get<IProject>(`/api.php/v1/projects/${projectId}`);
// 缓存结果 (30分钟 TTL)
cache.set(cacheKey, project, 30 * 60 * 1000);
// 格式化输出
const output = `项目详情:\n\n基本信息:\n ID: ${project.id}\n 名称: ${project.name}\n 代码: ${project.code}\n 状态: ${project.status}\n 开始时间: ${project.begin}\n 结束时间: ${project.end || '未设置'}\n\n详细信息:\n 团队: ${project.team || '未设置'}\n 权限控制: ${project.acl || '默认'}\n 产品关联: ${project.hasProduct || '未设置'}`;
return {
content: [
{
type: 'text',
text: output,
},
],
};
} catch (error: any) {
if (error.code === 404) {
return {
content: [
{
type: 'text',
text: `未找到 ID 为 ${projectId} 的项目`,
},
],
isError: true,
};
}
return {
content: [
{
type: 'text',
text: `获取项目详情失败: ${error.message || '未知错误'}`,
},
],
isError: true,
};
}
}