File size: 9,525 Bytes
b7bf9e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""Agent tools for task CRUD operations."""

import json
import uuid
from datetime import datetime

from agents import RunContextWrapper, function_tool
from sqlalchemy import select

from src.api.agent.context import ChatContext
from src.api.database import get_session_factory
from src.api.models.task import Task


def _get_tool_session_factory():
    """Get session factory — separated for test monkeypatching."""
    return get_session_factory()


def _error(code: str, message: str, suggestion: str) -> str:
    # Return user-friendly error message instead of JSON
    return f"{message} {suggestion}"


async def _add_task_impl(
    ctx: RunContextWrapper[ChatContext], title: str, description: str | None
) -> str:
    if not title or not title.strip():
        return _error("VALIDATION_ERROR", "Title cannot be empty.", "Provide a task title.")
    if len(title) > 200:
        return _error(
            "VALIDATION_ERROR",
            "Title too long (max 200 characters).",
            "Shorten the task title.",
        )

    user_id = uuid.UUID(ctx.context.user_id)
    factory = _get_tool_session_factory()
    async with factory() as session:
        task = Task(
            title=title.strip(),
            description=description.strip() if description else None,
            user_id=user_id,
        )
        session.add(task)
        await session.commit()
        await session.refresh(task)

        ctx.context.tasks_modified = True
        ctx.context.modified_tasks.append(("created", str(task.id), task.title))

        return f"Added task '{task.title}' successfully!"


@function_tool
async def add_task(
    ctx: RunContextWrapper[ChatContext], title: str, description: str | None = None
) -> str:
    """Create a new task for the user.

    Args:
        title: Task title, 1-200 characters.
        description: Optional task description, max 2000 characters.
    """
    return await _add_task_impl(ctx, title, description)


async def _list_tasks_impl(
    ctx: RunContextWrapper[ChatContext], completed: bool | None
) -> str:
    user_id = uuid.UUID(ctx.context.user_id)
    factory = _get_tool_session_factory()
    async with factory() as session:
        conditions = [
            Task.user_id == user_id,
            Task.is_deleted == False,  # noqa: E712
        ]
        if completed is not None:
            conditions.append(Task.completed == completed)

        result = await session.execute(
            select(Task).where(*conditions).order_by(Task.created_at.desc())
        )
        tasks = result.scalars().all()

        if not tasks:
            return "You have no tasks right now. Would you like to add one?"

        task_list = []
        for i, task in enumerate(tasks, 1):
            status = "✓" if task.completed else "○"
            task_list.append(f"{i}. [{status}] {task.title} (id: {task.id})")

        return f"Here are your tasks:\n" + "\n".join(task_list)


@function_tool
async def list_tasks(
    ctx: RunContextWrapper[ChatContext], completed: bool | None = None
) -> str:
    """List the user's tasks with optional filtering.

    Args:
        completed: Filter by completion status. True=completed only, False=pending only, omit=all tasks.
    """
    return await _list_tasks_impl(ctx, completed)


async def _complete_task_impl(
    ctx: RunContextWrapper[ChatContext], task_id: str
) -> str:
    try:
        task_uuid = uuid.UUID(task_id)
    except ValueError:
        return _error("INVALID_ID", "Invalid task ID format.", "Use list_tasks to find valid task IDs.")

    user_id = uuid.UUID(ctx.context.user_id)
    factory = _get_tool_session_factory()
    async with factory() as session:
        result = await session.execute(
            select(Task).where(
                Task.id == task_uuid,
                Task.user_id == user_id,
                Task.is_deleted == False,  # noqa: E712
            )
        )
        task = result.scalar_one_or_none()
        if task is None:
            return _error(
                "NOT_FOUND",
                "No task found with that ID.",
                "Use list_tasks to find valid task IDs.",
            )

        task.completed = True
        task.updated_at = datetime.utcnow()
        await session.commit()
        await session.refresh(task)

        ctx.context.tasks_modified = True
        ctx.context.modified_tasks.append(("completed", str(task.id), task.title))

        return f"Great job! I've marked '{task.title}' as completed. 🎉"


@function_tool
async def complete_task(ctx: RunContextWrapper[ChatContext], task_id: str) -> str:
    """Mark a task as completed.

    Args:
        task_id: UUID of the task to complete.
    """
    return await _complete_task_impl(ctx, task_id)


async def _update_task_impl(
    ctx: RunContextWrapper[ChatContext],
    task_id: str,
    title: str | None,
    description: str | None,
) -> str:
    try:
        task_uuid = uuid.UUID(task_id)
    except ValueError:
        return _error("INVALID_ID", "Invalid task ID format.", "Use list_tasks to find valid task IDs.")

    if title is not None and not title.strip():
        return _error("VALIDATION_ERROR", "Title cannot be empty.", "Provide a non-empty title.")
    if title is not None and len(title) > 200:
        return _error("VALIDATION_ERROR", "Title too long (max 200 characters).", "Shorten the title.")
    if description is not None and len(description) > 2000:
        return _error(
            "VALIDATION_ERROR",
            "Description too long (max 2000 characters).",
            "Shorten the description.",
        )

    user_id = uuid.UUID(ctx.context.user_id)
    factory = _get_tool_session_factory()
    async with factory() as session:
        result = await session.execute(
            select(Task).where(
                Task.id == task_uuid,
                Task.user_id == user_id,
                Task.is_deleted == False,  # noqa: E712
            )
        )
        task = result.scalar_one_or_none()
        if task is None:
            return _error(
                "NOT_FOUND",
                "No task found with that ID.",
                "Use list_tasks to find valid task IDs.",
            )

        if title is not None:
            task.title = title.strip()
        if description is not None:
            task.description = description.strip()
        task.updated_at = datetime.utcnow()
        await session.commit()
        await session.refresh(task)

        ctx.context.tasks_modified = True
        ctx.context.modified_tasks.append(("updated", str(task.id), task.title))

        return f"Task updated successfully! '{task.title}' is now up to date."


@function_tool
async def update_task(
    ctx: RunContextWrapper[ChatContext],
    task_id: str,
    title: str | None = None,
    description: str | None = None,
) -> str:
    """Update a task's title or description.

    Args:
        task_id: UUID of the task to update.
        title: New title, 1-200 characters.
        description: New description, max 2000 characters.
    """
    return await _update_task_impl(ctx, task_id, title, description)


async def _delete_task_impl(
    ctx: RunContextWrapper[ChatContext], task_id: str
) -> str:
    try:
        task_uuid = uuid.UUID(task_id)
    except ValueError:
        return _error("INVALID_ID", "Invalid task ID format.", "Use list_tasks to find valid task IDs.")

    user_id = uuid.UUID(ctx.context.user_id)
    factory = _get_tool_session_factory()
    async with factory() as session:
        result = await session.execute(
            select(Task).where(
                Task.id == task_uuid,
                Task.user_id == user_id,
                Task.is_deleted == False,  # noqa: E712
            )
        )
        task = result.scalar_one_or_none()
        if task is None:
            return _error(
                "NOT_FOUND",
                "No task found with that ID.",
                "The task may have already been deleted. Try listing your tasks.",
            )

        task.is_deleted = True
        task.updated_at = datetime.utcnow()
        await session.commit()

        ctx.context.tasks_modified = True
        ctx.context.modified_tasks.append(("deleted", str(task.id), task.title))

        return f"Task '{task.title}' has been removed from your list."


@function_tool
async def delete_task(ctx: RunContextWrapper[ChatContext], task_id: str) -> str:
    """Soft-delete a task.

    Args:
        task_id: UUID of the task to delete.
    """
    return await _delete_task_impl(ctx, task_id)


@function_tool
async def delete_all_tasks(ctx: RunContextWrapper[ChatContext]) -> str:
    """Delete ALL of the user's tasks at once. Use when the user asks to clear or delete everything."""
    user_id = uuid.UUID(ctx.context.user_id)
    factory = _get_tool_session_factory()
    async with factory() as session:
        result = await session.execute(
            select(Task).where(
                Task.user_id == user_id,
                Task.is_deleted == False,  # noqa: E712
            )
        )
        tasks = result.scalars().all()

        if not tasks:
            return "You don't have any tasks to delete."

        for task in tasks:
            task.is_deleted = True
            task.updated_at = datetime.utcnow()
            ctx.context.modified_tasks.append(("deleted", str(task.id), task.title))

        ctx.context.tasks_modified = True
        await session.commit()

        return f"All {len(tasks)} tasks have been deleted."