/**
* 获取任务列表工具
*/
import { ToolResult } from '../types';
import { ZenTaoClient } from '../client';
import { ITask, ITaskQueryParams } from '../types';
import { LRUCache } from '../utils/cache';
export async function getTasks(
client: ZenTaoClient,
cache: LRUCache<string, any>,
params?: ITaskQueryParams
): Promise<ToolResult> {
try {
// 构建查询参数
const queryParams: any = {
page: params?.page || 1,
limit: params?.limit || 50,
};
if (params?.projectId) {
queryParams.projectId = params.projectId;
}
if (params?.productId) {
queryParams.productId = params.productId;
}
if (params?.storyId) {
queryParams.storyId = params.storyId;
}
if (params?.status) {
queryParams.status = params.status;
}
if (params?.assignedTo) {
queryParams.assignedTo = params.assignedTo;
}
if (params?.pri) {
queryParams.pri = params.pri;
}
if (params?.orderBy) {
queryParams.orderBy = params.orderBy;
}
// 生成缓存键
const cacheKey = `tasks:${JSON.stringify(queryParams)}`;
// 尝试从缓存获取
const cached = cache.get(cacheKey);
if (cached) {
const output = `从缓存获取的任务列表(共 ${cached.length} 个):\n\n${cached.map((t: ITask) =>
`• [${t.id}] ${t.name} - 状态: ${t.status} - 负责人: ${t.assignedTo || '未分配'}`
).join('\n')}`;
return {
content: [
{
type: 'text',
text: output,
},
],
};
}
// 调用 API
const tasks = await client.get<ITask[]>('/api.php/v1/tasks', queryParams);
// 缓存结果 (5分钟 TTL)
cache.set(cacheKey, tasks, 5 * 60 * 1000);
// 格式化输出
const output = `获取到 ${tasks.length} 个任务:\n\n${tasks.map((t: ITask) =>
`• [${t.id}] ${t.name}\n 状态: ${t.status}\n 负责人: ${t.assignedTo || '未分配'}\n 优先级: ${t.pri || '未设置'}\n 截止时间: ${t.deadline || '未设置'}\n`
).join('\n')}`;
return {
content: [
{
type: 'text',
text: output,
},
],
};
} catch (error: any) {
return {
content: [
{
type: 'text',
text: `获取任务列表失败: ${error.message || '未知错误'}`,
},
],
isError: true,
};
}
}