mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-02-12 18:13:05 -05:00
feat: Add todo tool (#286)
This commit is contained in:
360
tools/TodoTool.cpp
Normal file
360
tools/TodoTool.cpp
Normal file
@ -0,0 +1,360 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
*
|
||||
* QodeAssist is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QodeAssist is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "TodoTool.hpp"
|
||||
#include "ToolExceptions.hpp"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QMutexLocker>
|
||||
#include <QtConcurrent>
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
TodoTool::TodoTool(QObject *parent)
|
||||
: BaseTool(parent)
|
||||
{}
|
||||
|
||||
QString TodoTool::name() const
|
||||
{
|
||||
return "todo_tool";
|
||||
}
|
||||
|
||||
QString TodoTool::stringName() const
|
||||
{
|
||||
return "Managing TODO list for task tracking";
|
||||
}
|
||||
|
||||
QString TodoTool::description() const
|
||||
{
|
||||
return "Track and organize multi-step tasks during complex operations that require multiple "
|
||||
"sequential steps. "
|
||||
"**Use when planning 3+ step workflows.** "
|
||||
"Operations: 'add' - provide array of task descriptions to create full plan at once, "
|
||||
"'complete' - provide array of task IDs to mark finished steps, 'list' - review "
|
||||
"progress. "
|
||||
"Helpful for: large refactorings, feature implementations, debugging workflows. "
|
||||
"The list persists throughout the conversation.";
|
||||
}
|
||||
|
||||
QJsonObject TodoTool::getDefinition(LLMCore::ToolSchemaFormat format) const
|
||||
{
|
||||
QJsonObject definition;
|
||||
definition["type"] = "object";
|
||||
|
||||
QJsonObject properties;
|
||||
|
||||
QJsonObject operationProp;
|
||||
operationProp["type"] = "string";
|
||||
operationProp["description"] = "Operation: 'add' (create tasks), 'complete' (mark tasks as "
|
||||
"done), 'list' (show all tasks)";
|
||||
QJsonArray operationEnum;
|
||||
operationEnum.append("add");
|
||||
operationEnum.append("complete");
|
||||
operationEnum.append("list");
|
||||
operationProp["enum"] = operationEnum;
|
||||
properties["operation"] = operationProp;
|
||||
|
||||
QJsonObject tasksProp;
|
||||
tasksProp["type"] = "array";
|
||||
QJsonObject tasksItems;
|
||||
tasksItems["type"] = "string";
|
||||
tasksProp["items"] = tasksItems;
|
||||
tasksProp["description"]
|
||||
= "Array of task descriptions to create (required for 'add' operation). "
|
||||
"Create all subtasks at once, e.g.: ['Step 1: ...', 'Step 2: ...', 'Step 3: ...']";
|
||||
properties["tasks"] = tasksProp;
|
||||
|
||||
QJsonObject todoIdsProp;
|
||||
todoIdsProp["type"] = "array";
|
||||
QJsonObject todoIdsItems;
|
||||
todoIdsItems["type"] = "integer";
|
||||
todoIdsProp["items"] = todoIdsItems;
|
||||
todoIdsProp["description"]
|
||||
= "Array of todo item IDs to mark as completed (required for 'complete' operation). "
|
||||
"Example: [1, 2, 5] to complete tasks #1, #2, and #5";
|
||||
properties["todo_ids"] = todoIdsProp;
|
||||
|
||||
definition["properties"] = properties;
|
||||
|
||||
QJsonArray required;
|
||||
required.append("operation");
|
||||
definition["required"] = required;
|
||||
|
||||
switch (format) {
|
||||
case LLMCore::ToolSchemaFormat::OpenAI:
|
||||
return customizeForOpenAI(definition);
|
||||
case LLMCore::ToolSchemaFormat::Claude:
|
||||
return customizeForClaude(definition);
|
||||
case LLMCore::ToolSchemaFormat::Ollama:
|
||||
return customizeForOllama(definition);
|
||||
case LLMCore::ToolSchemaFormat::Google:
|
||||
return customizeForGoogle(definition);
|
||||
}
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
||||
LLMCore::ToolPermissions TodoTool::requiredPermissions() const
|
||||
{
|
||||
return LLMCore::ToolPermission::None;
|
||||
}
|
||||
|
||||
QFuture<QString> TodoTool::executeAsync(const QJsonObject &input)
|
||||
{
|
||||
return QtConcurrent::run([this, input]() -> QString {
|
||||
QString sessionId = input.value("session_id").toString();
|
||||
if (sessionId.isEmpty()) {
|
||||
sessionId = "current";
|
||||
}
|
||||
|
||||
const QString operation = input.value("operation").toString();
|
||||
|
||||
if (operation == "add") {
|
||||
if (!input.contains("tasks") || !input.value("tasks").isArray()) {
|
||||
throw ToolRuntimeError(
|
||||
tr("Error: 'tasks' parameter (array) is required for 'add' operation. "
|
||||
"Example: {\"operation\": \"add\", \"tasks\": [\"Task 1\", \"Task 2\"]}"));
|
||||
}
|
||||
|
||||
const QJsonArray tasksArray = input.value("tasks").toArray();
|
||||
if (tasksArray.isEmpty()) {
|
||||
throw ToolRuntimeError(
|
||||
tr("Error: 'tasks' array cannot be empty. Provide at least one task."));
|
||||
}
|
||||
|
||||
QStringList tasks;
|
||||
for (const QJsonValue &taskValue : tasksArray) {
|
||||
QString task = taskValue.toString().trimmed();
|
||||
if (!task.isEmpty()) {
|
||||
tasks.append(task);
|
||||
}
|
||||
}
|
||||
|
||||
if (tasks.isEmpty()) {
|
||||
throw ToolRuntimeError(
|
||||
tr("Error: All tasks in 'tasks' array are empty strings."));
|
||||
}
|
||||
|
||||
return addTodos(sessionId, tasks);
|
||||
|
||||
} else if (operation == "complete") {
|
||||
if (!input.contains("todo_ids") || !input.value("todo_ids").isArray()) {
|
||||
throw ToolRuntimeError(
|
||||
tr("Error: 'todo_ids' parameter (array) is required for 'complete' operation. "
|
||||
"Example: {\"operation\": \"complete\", \"todo_ids\": [1, 2, 3]}"));
|
||||
}
|
||||
|
||||
const QJsonArray idsArray = input.value("todo_ids").toArray();
|
||||
if (idsArray.isEmpty()) {
|
||||
throw ToolRuntimeError(
|
||||
tr("Error: 'todo_ids' array cannot be empty. Provide at least one ID."));
|
||||
}
|
||||
|
||||
QList<int> ids;
|
||||
for (const QJsonValue &idValue : idsArray) {
|
||||
int id = idValue.toInt(-1);
|
||||
if (id > 0) {
|
||||
ids.append(id);
|
||||
}
|
||||
}
|
||||
|
||||
if (ids.isEmpty()) {
|
||||
throw ToolRuntimeError(
|
||||
tr("Error: All IDs in 'todo_ids' array are invalid. IDs must be positive "
|
||||
"integers."));
|
||||
}
|
||||
|
||||
return completeTodos(sessionId, ids);
|
||||
|
||||
} else if (operation == "list") {
|
||||
return listTodos(sessionId);
|
||||
|
||||
} else {
|
||||
throw ToolRuntimeError(
|
||||
tr("Error: Unknown operation '%1'. Valid operations: 'add', 'complete', 'list'")
|
||||
.arg(operation));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TodoTool::clearSession(const QString &sessionId)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
m_sessionTodos.remove(sessionId);
|
||||
m_sessionNextId.remove(sessionId);
|
||||
}
|
||||
|
||||
QString TodoTool::addTodos(const QString &sessionId, const QStringList &tasks)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
if (!m_sessionTodos.contains(sessionId)) {
|
||||
m_sessionTodos[sessionId] = QHash<int, TodoItem>();
|
||||
m_sessionNextId[sessionId] = 1;
|
||||
}
|
||||
|
||||
for (const QString &task : tasks) {
|
||||
const int newId = m_sessionNextId[sessionId]++;
|
||||
m_sessionTodos[sessionId][newId] = {newId, task, false};
|
||||
}
|
||||
|
||||
const QString summary = (tasks.size() == 1) ? tr("✓ Added 1 new task")
|
||||
: tr("✓ Added %1 new tasks").arg(tasks.size());
|
||||
|
||||
return QString("%1\n\n%2").arg(summary, listTodosLocked(sessionId));
|
||||
}
|
||||
|
||||
QString TodoTool::completeTodos(const QString &sessionId, const QList<int> &todoIds)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
if (!m_sessionTodos.contains(sessionId)) {
|
||||
throw ToolRuntimeError(tr("Error: No todos found in this session"));
|
||||
}
|
||||
|
||||
auto &todos = m_sessionTodos[sessionId];
|
||||
int completedCount = 0;
|
||||
int alreadyCompletedCount = 0;
|
||||
QStringList notFound;
|
||||
|
||||
for (const int todoId : todoIds) {
|
||||
if (!todos.contains(todoId)) {
|
||||
notFound.append(QString("#%1").arg(todoId));
|
||||
continue;
|
||||
}
|
||||
|
||||
TodoItem &item = todos[todoId];
|
||||
if (item.completed) {
|
||||
alreadyCompletedCount++;
|
||||
} else {
|
||||
item.completed = true;
|
||||
completedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
QStringList messages;
|
||||
if (completedCount > 0) {
|
||||
messages.append((completedCount == 1) ? tr("✓ Marked 1 task as completed")
|
||||
: tr("✓ Marked %1 tasks as completed")
|
||||
.arg(completedCount));
|
||||
}
|
||||
|
||||
if (alreadyCompletedCount > 0) {
|
||||
messages.append(tr("⚠ %1 already completed").arg(alreadyCompletedCount));
|
||||
}
|
||||
|
||||
if (!notFound.isEmpty()) {
|
||||
messages.append(tr("❌ Not found: %1").arg(notFound.join(", ")));
|
||||
}
|
||||
|
||||
const QString summary = messages.join(", ");
|
||||
return QString("%1\n\n%2").arg(summary, listRemainingTodosLocked(sessionId));
|
||||
}
|
||||
|
||||
QString TodoTool::listTodos(const QString &sessionId) const
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
return listTodosLocked(sessionId);
|
||||
}
|
||||
|
||||
QString TodoTool::listTodosLocked(const QString &sessionId) const
|
||||
{
|
||||
const auto it = m_sessionTodos.constFind(sessionId);
|
||||
if (it == m_sessionTodos.constEnd() || it->isEmpty()) {
|
||||
return tr("📋 TODO List: (empty)");
|
||||
}
|
||||
|
||||
const auto &todos = *it;
|
||||
QList<int> ids = todos.keys();
|
||||
std::sort(ids.begin(), ids.end());
|
||||
|
||||
QStringList lines;
|
||||
lines.reserve(ids.size() + 4);
|
||||
lines.append(tr("📋 TODO List:"));
|
||||
lines.append("");
|
||||
|
||||
int completedCount = 0;
|
||||
for (const int id : ids) {
|
||||
const TodoItem &item = todos[id];
|
||||
const QString checkbox = item.completed ? "[x]" : "[ ]";
|
||||
const QString strikethrough = item.completed ? QString("~~") : QString("");
|
||||
|
||||
lines.append(QString("%1 **#%2** %3%4%5")
|
||||
.arg(checkbox)
|
||||
.arg(id)
|
||||
.arg(strikethrough, item.task, strikethrough));
|
||||
|
||||
if (item.completed) {
|
||||
completedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
lines.append("");
|
||||
const int totalCount = ids.size();
|
||||
const int percentage = totalCount > 0 ? (completedCount * 100) / totalCount : 0;
|
||||
lines.append(
|
||||
tr("Progress: %1/%2 completed (%3%)").arg(completedCount).arg(totalCount).arg(percentage));
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
QString TodoTool::listRemainingTodosLocked(const QString &sessionId) const
|
||||
{
|
||||
const auto it = m_sessionTodos.constFind(sessionId);
|
||||
if (it == m_sessionTodos.constEnd() || it->isEmpty()) {
|
||||
return tr("📋 All tasks completed! 🎉");
|
||||
}
|
||||
|
||||
const auto &todos = *it;
|
||||
QList<int> ids = todos.keys();
|
||||
std::sort(ids.begin(), ids.end());
|
||||
|
||||
int completedCount = 0;
|
||||
QStringList remainingLines;
|
||||
|
||||
for (const int id : ids) {
|
||||
const TodoItem &item = todos[id];
|
||||
if (item.completed) {
|
||||
completedCount++;
|
||||
} else {
|
||||
remainingLines.append(QString("[ ] **#%1** %2").arg(id).arg(item.task));
|
||||
}
|
||||
}
|
||||
|
||||
if (remainingLines.isEmpty()) {
|
||||
return tr("📋 All tasks completed! 🎉");
|
||||
}
|
||||
|
||||
QStringList lines;
|
||||
lines.reserve(remainingLines.size() + 4);
|
||||
lines.append(tr("📋 Remaining tasks:"));
|
||||
lines.append("");
|
||||
lines.append(remainingLines);
|
||||
lines.append("");
|
||||
|
||||
const int totalCount = ids.size();
|
||||
const int percentage = totalCount > 0 ? (completedCount * 100) / totalCount : 0;
|
||||
lines.append(
|
||||
tr("Progress: %1/%2 completed (%3%)").arg(completedCount).arg(totalCount).arg(percentage));
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::Tools
|
||||
66
tools/TodoTool.hpp
Normal file
66
tools/TodoTool.hpp
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2024-2025 Petr Mironychev
|
||||
*
|
||||
* This file is part of QodeAssist.
|
||||
*
|
||||
* QodeAssist is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QodeAssist is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <llmcore/BaseTool.hpp>
|
||||
|
||||
#include <QHash>
|
||||
#include <QMutex>
|
||||
#include <QString>
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
struct TodoItem
|
||||
{
|
||||
int id;
|
||||
QString task;
|
||||
bool completed;
|
||||
};
|
||||
|
||||
class TodoTool : public LLMCore::BaseTool
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit TodoTool(QObject *parent = nullptr);
|
||||
|
||||
QString name() const override;
|
||||
QString stringName() const override;
|
||||
QString description() const override;
|
||||
QJsonObject getDefinition(LLMCore::ToolSchemaFormat format) const override;
|
||||
LLMCore::ToolPermissions requiredPermissions() const override;
|
||||
|
||||
QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) override;
|
||||
|
||||
void clearSession(const QString &sessionId);
|
||||
|
||||
private:
|
||||
QString addTodos(const QString &sessionId, const QStringList &tasks);
|
||||
QString completeTodos(const QString &sessionId, const QList<int> &todoIds);
|
||||
QString listTodos(const QString &sessionId) const;
|
||||
QString listTodosLocked(const QString &sessionId) const;
|
||||
QString listRemainingTodosLocked(const QString &sessionId) const;
|
||||
|
||||
mutable QMutex m_mutex;
|
||||
QHash<QString, QHash<int, TodoItem>> m_sessionTodos;
|
||||
QHash<QString, int> m_sessionNextId;
|
||||
};
|
||||
|
||||
} // namespace QodeAssist::Tools
|
||||
@ -34,6 +34,7 @@
|
||||
#include "ListProjectFilesTool.hpp"
|
||||
#include "ProjectSearchTool.hpp"
|
||||
#include "ReadVisibleFilesTool.hpp"
|
||||
#include "TodoTool.hpp"
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
@ -54,6 +55,7 @@ void ToolsFactory::registerTools()
|
||||
registerTool(new ExecuteTerminalCommandTool(this));
|
||||
registerTool(new ProjectSearchTool(this));
|
||||
registerTool(new FindAndReadFileTool(this));
|
||||
registerTool(new TodoTool(this));
|
||||
|
||||
LOG_MESSAGE(QString("Registered %1 tools").arg(m_tools.size()));
|
||||
}
|
||||
@ -107,6 +109,10 @@ QJsonArray ToolsFactory::getToolsDefinitions(
|
||||
continue;
|
||||
}
|
||||
|
||||
if (it.value()->name() == "todo_tool" && !settings.enableTodoTool()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto requiredPerms = it.value()->requiredPermissions();
|
||||
|
||||
if (filter != LLMCore::RunToolsFilter::ALL) {
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
#include "ToolsManager.hpp"
|
||||
#include "TodoTool.hpp"
|
||||
#include "logger/Logger.hpp"
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
@ -74,6 +75,10 @@ void ToolsManager::executeToolCall(
|
||||
|
||||
QJsonObject modifiedInput = input;
|
||||
modifiedInput["_request_id"] = requestId;
|
||||
|
||||
if (!m_currentSessionId.isEmpty()) {
|
||||
modifiedInput["session_id"] = m_currentSessionId;
|
||||
}
|
||||
|
||||
PendingTool pendingTool{toolId, toolName, modifiedInput, "", false};
|
||||
queue.queue.append(pendingTool);
|
||||
@ -140,27 +145,21 @@ QJsonArray ToolsManager::getToolsDefinitions(
|
||||
void ToolsManager::cleanupRequest(const QString &requestId)
|
||||
{
|
||||
if (m_toolQueues.contains(requestId)) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Canceling pending tools for request %1").arg(requestId));
|
||||
m_toolHandler->cleanupRequest(requestId);
|
||||
m_toolQueues.remove(requestId);
|
||||
}
|
||||
|
||||
LOG_MESSAGE(QString("ToolsManager: Cleaned up request %1").arg(requestId));
|
||||
}
|
||||
|
||||
void ToolsManager::onToolFinished(
|
||||
const QString &requestId, const QString &toolId, const QString &result, bool success)
|
||||
{
|
||||
if (!m_toolQueues.contains(requestId)) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool result for unknown request %1").arg(requestId));
|
||||
return;
|
||||
}
|
||||
|
||||
auto &queue = m_toolQueues[requestId];
|
||||
|
||||
if (!queue.completed.contains(toolId)) {
|
||||
LOG_MESSAGE(QString("ToolsManager: Tool result for unknown tool %1 in request %2")
|
||||
.arg(toolId, requestId));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -197,4 +196,17 @@ QHash<QString, QString> ToolsManager::getToolResults(const QString &requestId) c
|
||||
return results;
|
||||
}
|
||||
|
||||
void ToolsManager::clearTodoSession(const QString &sessionId)
|
||||
{
|
||||
auto *todoTool = qobject_cast<TodoTool *>(m_toolsFactory->getToolByName("todo_tool"));
|
||||
if (todoTool) {
|
||||
todoTool->clearSession(sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
void ToolsManager::setCurrentSessionId(const QString &sessionId)
|
||||
{
|
||||
m_currentSessionId = sessionId;
|
||||
}
|
||||
|
||||
} // namespace QodeAssist::Tools
|
||||
|
||||
@ -27,6 +27,7 @@
|
||||
#include "ToolHandler.hpp"
|
||||
#include "ToolsFactory.hpp"
|
||||
#include <llmcore/BaseTool.hpp>
|
||||
#include <llmcore/IToolsManager.hpp>
|
||||
|
||||
namespace QodeAssist::Tools {
|
||||
|
||||
@ -46,7 +47,7 @@ struct ToolQueue
|
||||
bool isExecuting = false;
|
||||
};
|
||||
|
||||
class ToolsManager : public QObject
|
||||
class ToolsManager : public QObject, public LLMCore::IToolsManager
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@ -57,12 +58,15 @@ public:
|
||||
const QString &requestId,
|
||||
const QString &toolId,
|
||||
const QString &toolName,
|
||||
const QJsonObject &input);
|
||||
const QJsonObject &input) override;
|
||||
|
||||
QJsonArray getToolsDefinitions(
|
||||
LLMCore::ToolSchemaFormat format,
|
||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL) const;
|
||||
void cleanupRequest(const QString &requestId);
|
||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL) const override;
|
||||
|
||||
void cleanupRequest(const QString &requestId) override;
|
||||
void setCurrentSessionId(const QString &sessionId) override;
|
||||
void clearTodoSession(const QString &sessionId) override;
|
||||
|
||||
ToolsFactory *toolsFactory() const;
|
||||
|
||||
@ -77,6 +81,7 @@ private:
|
||||
ToolsFactory *m_toolsFactory;
|
||||
ToolHandler *m_toolHandler;
|
||||
QHash<QString, ToolQueue> m_toolQueues;
|
||||
QString m_currentSessionId;
|
||||
|
||||
void executeNextTool(const QString &requestId);
|
||||
QHash<QString, QString> getToolResults(const QString &requestId) const;
|
||||
|
||||
Reference in New Issue
Block a user