import { ServerConfig } from "../../configuration";
import { TrellisObjectStatus } from "../../models";
import { Repository } from "../../repositories";
import { autoCompleteParentHierarchy } from "../../utils";
import { appendAffectedFiles } from "./appendAffectedFiles.js";
export async function completeTask(
repository: Repository,
serverConfig: ServerConfig,
taskId: string,
summary: string,
filesChanged: Record<string, string>,
): Promise<{ content: Array<{ type: string; text: string }> }> {
// Get the task object from repository
const task = await repository.getObjectById(taskId);
if (!task) {
throw new Error(`Task with ID "${taskId}" not found`);
}
// Check if task is in progress
if (task.status !== TrellisObjectStatus.IN_PROGRESS) {
throw new Error(
`Task "${taskId}" is not in progress (current status: ${task.status})`,
);
}
// Update task status to done
task.status = TrellisObjectStatus.DONE;
// Append to affected files map
await appendAffectedFiles(repository, task, filesChanged);
// Append summary to log
task.log.push(summary);
// Save the updated task
await repository.saveObject(task);
// If auto-complete parent is enabled, check if we should complete parent objects
if (serverConfig.autoCompleteParent) {
await autoCompleteParentHierarchy(repository, task);
}
return {
content: [
{
type: "text",
text: `Task "${taskId}" completed successfully. Updated ${Object.keys(filesChanged).length} affected files.`,
},
],
};
}