11from fastapi import APIRouter , Depends , Header
2- from ApiClient import verify_api_key , get_api_client
2+ from ApiClient import verify_api_key
33from Models import ResponseMessage
44from pydantic import BaseModel
55from Globals import getenv
6+ from Task import Task
7+ import datetime
68import logging
7- import json
89
910
1011app = APIRouter ()
@@ -60,17 +61,39 @@ async def new_task(
6061 user = Depends (verify_api_key ),
6162 authorization : str = Header (None ),
6263) -> ResponseMessage :
63- agixt = get_api_client (authorization = authorization )
64- response = agixt .execute_command (
64+ try :
65+ days = int (days )
66+ except :
67+ days = 0
68+ try :
69+ hours = int (hours )
70+ except :
71+ hours = 0
72+ try :
73+ minutes = int (minutes )
74+ except :
75+ minutes = 0
76+ # Calculate the due date
77+ due_date = datetime .datetime .now () + datetime .timedelta (
78+ days = days , hours = hours , minutes = minutes
79+ )
80+
81+ # Initialize task manager with the current token
82+ task_manager = Task (token = authorization )
83+ # Create a descriptive title from the purpose of the follow-up
84+ title_preview = task .title .split ("\n " )[0 ][:50 ] + (
85+ "..." if len (task .title ) > 50 else ""
86+ )
87+
88+ # Create the follow-up task
89+ task_id = await task_manager .create_task (
90+ title = title_preview ,
91+ description = task .task_description ,
92+ category_name = "Follow-ups" ,
6593 agent_name = task .agent_name ,
66- command_name = "Schedule Task" ,
67- command_args = {
68- "task_description" : task .task_description ,
69- "days" : task .days ,
70- "hours" : task .hours ,
71- "minutes" : task .minutes ,
72- },
73- conversation_name = task .conversation_id ,
94+ due_date = due_date ,
95+ priority = 1 , # High priority for follow-ups
96+ memory_collection = task .conversation_id , # This ensures context preservation
7497 )
7598 return ResponseMessage (message = f"Task created for agent '{ task .agent_name } '." )
7699
@@ -88,17 +111,20 @@ async def new_reoccurring_task(
88111 user = Depends (verify_api_key ),
89112 authorization : str = Header (None ),
90113) -> ResponseMessage :
91- agixt = get_api_client (authorization = authorization )
92- response = agixt .execute_command (
114+ task_manager = Task (token = authorization )
115+ title_preview = task .title .split ("\n " )[0 ][:50 ] + (
116+ "..." if len (task .title ) > 50 else ""
117+ )
118+ task_ids = await task_manager .create_reoccurring_task (
119+ title = title_preview ,
120+ description = task .task_description ,
121+ category_name = "Follow-ups" ,
93122 agent_name = task .agent_name ,
94- command_name = "Schedule Reoccurring Task" ,
95- command_args = {
96- "task_description" : task .task_description ,
97- "start_date" : task .start_date ,
98- "end_date" : task .end_date ,
99- "frequency" : task .frequency ,
100- },
101- conversation_name = task .conversation_id ,
123+ start_date = task .start_date ,
124+ end_date = task .end_date ,
125+ frequency = task .frequency ,
126+ priority = 1 , # High priority for follow-ups
127+ memory_collection = task .conversation_id , # This ensures context preservation
102128 )
103129 return ResponseMessage (
104130 message = f"Reoccurring task created for agent '{ task .agent_name } '."
@@ -118,22 +144,20 @@ async def modify_task(
118144 user = Depends (verify_api_key ),
119145 authorization : str = Header (None ),
120146) -> ResponseMessage :
121- agixt = get_api_client (authorization = authorization )
122- response = agixt .execute_command (
123- agent_name = task .agent_name ,
124- command_name = "Modify Scheduled Task" ,
125- command_args = {
126- "task_id" : task .task_id ,
127- "title" : task .title ,
128- "description" : task .description ,
129- "due_date" : task .due_date ,
130- "estimated_hours" : task .estimated_hours ,
131- "priority" : task .priority ,
132- "cancel_task" : task .cancel_task ,
133- },
134- conversation_name = task .conversation_id ,
135- )
136- return ResponseMessage (message = f"Task modified for agent '{ task .agent_name } '." )
147+ task_manager = Task (token = authorization )
148+ if str (task .cancel_task ).lower () == "true" :
149+ response = await task_manager .delete_task (task .task_id )
150+ else :
151+ # Update the task
152+ response = await task_manager .update_task (
153+ task_id = task .task_id ,
154+ title = task .title ,
155+ description = task .description ,
156+ due_date = task .due_date ,
157+ estimated_hours = task .estimated_hours ,
158+ priority = task .priority ,
159+ )
160+ return ResponseMessage (message = response )
137161
138162
139163@app .get (
@@ -146,11 +170,6 @@ async def modify_task(
146170async def get_scheduled_tasks (
147171 user = Depends (verify_api_key ), authorization : str = Header (None )
148172):
149- agixt = get_api_client (authorization = authorization )
150- response = agixt .execute_command (
151- agent_name = "" ,
152- command_name = "Get Scheduled Tasks" ,
153- command_args = {},
154- conversation_name = "" ,
155- )
156- return json .loads (response )
173+ task_manager = Task (token = authorization )
174+ tasks = await task_manager .get_pending_tasks ()
175+ return {"tasks" : tasks }
0 commit comments