/**
* 更新任务状态工具
*/
import { ToolResult } from '../types';
import { ZenTaoClient } from '../client';
import { ITask } from '../types';
import { LRUCache } from '../utils/cache';
export async function updateTaskStatus(
client: ZenTaoClient,
cache: LRUCache<string, any>,
taskId: number,
status: string
): Promise<ToolResult> {
try {
// 验证输入参数
if (!taskId || isNaN(taskId)) {
return {
content: [
{
type: 'text',
text: '错误:任务 ID 必须是一个有效的数字',
},
],
isError: true,
};
}
if (!status) {
return {
content: [
{
type: 'text',
text: '错误:任务状态不能为空',
},
],
isError: true,
};
}
// 验证状态值
const validStatuses = [
'wait',
'doing',
'done',
'blocked',
'cancel',
'closed',
];
if (!validStatuses.includes(status)) {
return {
content: [
{
type: 'text',
text: `错误:不支持的任务状态 "${status}"\n\n有效状态包括:\n - wait: 未开始\n - doing: 进行中\n - done: 已完成\n - blocked: 已阻塞\n - cancel: 已取消\n - closed: 已关闭`,
},
],
isError: true,
};
}
// 调用 API 更新任务状态
const updatedTask = await client.put<ITask>(`/api.php/v1/tasks/${taskId}`, {
status,
});
// 清理相关缓存
cache.delete(`tasks:${JSON.stringify({ projectId: updatedTask.project })}`);
cache.delete(`task:${taskId}`);
// 格式化输出
const statusDescriptions: Record<string, string> = {
wait: '未开始',
doing: '进行中',
done: '已完成',
blocked: '已阻塞',
cancel: '已取消',
closed: '已关闭',
};
const output = `任务状态更新成功!\n\n任务信息:\n ID: ${updatedTask.id}\n 名称: ${updatedTask.name}\n 项目: ${updatedTask.project}\n 旧状态: ${status}\n 新状态: ${statusDescriptions[status]}\n 更新时间: ${new Date().toLocaleString('zh-CN')}\n\n任务已成功更新为 "${statusDescriptions[status]}" 状态。`;
return {
content: [
{
type: 'text',
text: output,
},
],
};
} catch (error: any) {
if (error.code === 404) {
return {
content: [
{
type: 'text',
text: `未找到 ID 为 ${taskId} 的任务`,
},
],
isError: true,
};
}
let errorMessage = `更新任务状态失败: ${error.message || '未知错误'}`;
if (error.code === 403) {
errorMessage += '\n\n可能原因:权限不足,无法更新任务状态';
}
return {
content: [
{
type: 'text',
text: errorMessage,
},
],
isError: true,
};
}
}