-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da689ab
commit f1ff4ad
Showing
283 changed files
with
1,998 additions
and
871 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import List, Literal | ||
|
||
from abstra_internals.controllers.sdk_context import SDKContextStore | ||
from abstra_internals.entities.agents import ConnectionModel | ||
|
||
|
||
def get_connections(role: Literal["client", "agent"]) -> List[ConnectionModel]: | ||
if role == "client": | ||
return ( | ||
SDKContextStore.get_by_thread().repositories.role_clients.get_connections() | ||
) | ||
else: | ||
return ( | ||
SDKContextStore.get_by_thread().repositories.role_agents.get_connections() | ||
) | ||
|
||
|
||
__all__ = ["get_connections"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from typing import Optional | ||
|
||
import requests | ||
|
||
from abstra_internals.email_templates import task_waiting_template | ||
from abstra_internals.entities.execution import Execution, PreExecution | ||
from abstra_internals.entities.execution_context import ScriptContext | ||
from abstra_internals.environment import IS_PRODUCTION | ||
from abstra_internals.repositories.factory import Repositories | ||
from abstra_internals.repositories.project.project import ( | ||
AgentStage, | ||
ClientStage, | ||
FormStage, | ||
ProjectRepository, | ||
ScriptStage, | ||
Stage, | ||
) | ||
from abstra_internals.repositories.tasks import TaskDTO, TaskPayload | ||
|
||
|
||
class TaskExecutor: | ||
def __init__(self, repos: Repositories) -> None: | ||
self.project = ProjectRepository.load() | ||
self.repos = repos | ||
|
||
def send_task( | ||
self, | ||
type: str, | ||
current_stage: Stage, | ||
payload: TaskPayload, | ||
execution: Optional[Execution] = None, | ||
) -> None: | ||
project = ProjectRepository.load() | ||
next_stages = [ | ||
project.get_stage_raises(t.target_id) | ||
for t in current_stage.workflow_transitions | ||
if t.matches(type) | ||
] | ||
|
||
for stage in next_stages: | ||
task = self.repos.tasks.send_task( | ||
type=type, | ||
payload=payload, | ||
source_stage_id=current_stage.id, | ||
target_stage_id=stage.id, | ||
execution_id=execution.id if execution else None, | ||
) | ||
self._send_waiting_thread_notification(task) | ||
if execution: | ||
execution.context.sent_tasks.append(task.id) | ||
if isinstance(stage, ScriptStage): | ||
self.repos.producer.submit( | ||
PreExecution( | ||
context=ScriptContext(task_id=task.id), | ||
stage_id=stage.id, | ||
) | ||
) | ||
elif ( | ||
isinstance(stage, AgentStage) | ||
and stage.project_id is not None | ||
and stage.client_stage_id is not None | ||
): | ||
agent = self.repos.role_clients.get_agent(stage.project_id) | ||
conn = next( | ||
c | ||
for c in self.repos.role_clients.get_connections() | ||
if c.agent_project_id == agent.project_id | ||
and c.client_stage_id == stage.id | ||
) | ||
|
||
assert conn is not None, "Connection for agent not found" | ||
|
||
requests.post( | ||
agent.tasks_url + "/agent", | ||
json={ | ||
"task_data": { | ||
"type": type, | ||
"payload": { | ||
**payload, | ||
"connection_token": conn.token, | ||
}, | ||
}, | ||
"target_stage_id": (stage.client_stage_id), | ||
"execution_id": ( | ||
execution.id if IS_PRODUCTION and execution else None | ||
), | ||
}, | ||
headers={"authorization": conn.token}, | ||
).raise_for_status() | ||
|
||
elif isinstance(stage, ClientStage): | ||
assert isinstance(payload["connection_token"], str) | ||
conn = self.repos.role_agents.get_connection_by_token( | ||
payload["connection_token"] | ||
) | ||
|
||
requests.post( | ||
conn.client_task_url, | ||
json=task.model_dump(), | ||
headers={"Authorization": conn.token}, | ||
).raise_for_status() | ||
|
||
def _send_waiting_thread_notification(self, task: TaskDTO): | ||
stage = self.project.get_stage(task.target_stage_id) | ||
if not stage: | ||
raise Exception(f"Stage {task.target_stage_id} not found") | ||
|
||
if not (isinstance(stage, FormStage) and stage.notification_trigger.enabled): | ||
return | ||
|
||
recipient_emails = stage.notification_trigger.get_recipients(task.payload) | ||
if not recipient_emails: | ||
return | ||
|
||
self.repos.email.send( | ||
task_waiting_template.generate_email( | ||
recipient_emails=recipient_emails, form=stage | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.