/**
* 创建项目工具
*/
import { ToolResult } from '../types';
import { ZenTaoClient } from '../client';
import { IProject, ICreateProjectParams } from '../types';
import { LRUCache } from '../utils/cache';
export async function createProject(
client: ZenTaoClient,
cache: LRUCache<string, any>,
params: ICreateProjectParams
): Promise<ToolResult> {
try {
// 验证输入参数
if (!params.name || !params.code || !params.begin) {
return {
content: [
{
type: 'text',
text: '错误:项目名称、代码和开始时间都是必填项',
},
],
isError: true,
};
}
// 调用 API 创建项目
const newProject = await client.post<IProject>('/api.php/v1/projects', params);
// 清理相关缓存
cache.delete('projects:{}'); // 项目列表缓存
cache.delete(`project:${newProject.id}`); // 项目详情缓存
// 格式化输出
const output = `项目创建成功!\n\n项目信息:\n ID: ${newProject.id}\n 名称: ${newProject.name}\n 代码: ${newProject.code}\n 状态: ${newProject.status}\n 开始时间: ${newProject.begin}\n 结束时间: ${newProject.end || '未设置'}\n\n您现在可以开始为该项目创建任务、添加团队成员等操作。`;
return {
content: [
{
type: 'text',
text: output,
},
],
};
} catch (error: any) {
let errorMessage = `创建项目失败: ${error.message || '未知错误'}`;
// 提供详细的错误信息
if (error.code === 409) {
errorMessage += '\n\n可能原因:项目代码已存在,请使用唯一的项目代码';
} else if (error.code === 403) {
errorMessage += '\n\n可能原因:权限不足,无法创建项目';
}
return {
content: [
{
type: 'text',
text: errorMessage,
},
],
isError: true,
};
}
}