Skip to content

Commit 168daf6

Browse files
committed
use task module
1 parent f94cb6b commit 168daf6

File tree

2 files changed

+65
-69
lines changed

2 files changed

+65
-69
lines changed

agixt/endpoints/Tasks.py

Lines changed: 65 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from fastapi import APIRouter, Depends, Header
2-
from ApiClient import verify_api_key, get_api_client
2+
from ApiClient import verify_api_key
33
from Models import ResponseMessage
44
from pydantic import BaseModel
55
from Globals import getenv
6+
from Task import Task
7+
import datetime
68
import logging
7-
import json
89

910

1011
app = 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(
146170
async 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}

agixt/extensions/scheduled_tasks.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import os
21
from Extensions import Extensions
32
from Task import Task
43
import datetime
5-
from agixtsdk import AGiXTSDK
6-
from Globals import getenv
74

85

96
class scheduled_tasks(Extensions):
@@ -21,31 +18,11 @@ def __init__(self, **kwargs):
2118
self.command_name = (
2219
kwargs["command_name"] if "command_name" in kwargs else "Smart Prompt"
2320
)
24-
self.user = kwargs["user"] if "user" in kwargs else ""
2521
self.agent_name = kwargs["agent_name"] if "agent_name" in kwargs else "gpt4free"
26-
self.conversation_name = (
27-
kwargs["conversation_name"] if "conversation_name" in kwargs else ""
28-
)
29-
self.WORKING_DIRECTORY = (
30-
kwargs["conversation_directory"]
31-
if "conversation_directory" in kwargs
32-
else os.path.join(os.getcwd(), "WORKSPACE")
33-
)
34-
os.makedirs(self.WORKING_DIRECTORY, exist_ok=True)
3522
self.conversation_id = (
3623
kwargs["conversation_id"] if "conversation_id" in kwargs else ""
3724
)
38-
39-
self.ApiClient = (
40-
kwargs["ApiClient"]
41-
if "ApiClient" in kwargs
42-
else AGiXTSDK(
43-
base_uri=getenv("AGIXT_URI"),
44-
api_key=kwargs["api_key"] if "api_key" in kwargs else "",
45-
)
46-
)
4725
self.api_key = kwargs["api_key"] if "api_key" in kwargs else ""
48-
self.failures = 0
4926

5027
async def schedule_task(
5128
self,

0 commit comments

Comments
 (0)