|
| 1 | +from typing import Optional, List, Dict, Any, Iterator |
| 2 | +from pydantic import BaseModel, Field |
| 3 | +import requests |
| 4 | +import os |
| 5 | +import json |
| 6 | +import logging |
| 7 | +from agentx.util import get_headers |
| 8 | +from agentx.resources.agent import Agent |
| 9 | +from agentx.resources.conversation import Conversation, ChatResponse |
| 10 | + |
| 11 | + |
| 12 | +class User(BaseModel): |
| 13 | + id: str = Field(alias="_id") |
| 14 | + name: str |
| 15 | + email: str |
| 16 | + deleted: bool |
| 17 | + createdAt: str |
| 18 | + updatedAt: str |
| 19 | + avatar: str |
| 20 | + status: int |
| 21 | + customer: str |
| 22 | + resetPwdToken: Optional[str] = None |
| 23 | + defaultWorkspace: str |
| 24 | + workspaces: List[str] |
| 25 | + |
| 26 | + class Config: |
| 27 | + populate_by_name = True |
| 28 | + extra = "ignore" |
| 29 | + |
| 30 | + |
| 31 | +class Workforce(BaseModel): |
| 32 | + id: str = Field(alias="_id") |
| 33 | + agents: List[Agent] |
| 34 | + name: str |
| 35 | + image: str |
| 36 | + description: str |
| 37 | + manager: Agent |
| 38 | + creator: User |
| 39 | + context: int |
| 40 | + references: bool |
| 41 | + workspace: str |
| 42 | + createdAt: str |
| 43 | + updatedAt: str |
| 44 | + |
| 45 | + class Config: |
| 46 | + populate_by_name = True |
| 47 | + extra = "ignore" |
| 48 | + |
| 49 | + def new_conversation(self) -> Conversation: |
| 50 | + """Create a new conversation for this workforce.""" |
| 51 | + url = f"https://api.agentx.so/api/v1/access/teams/{self.id}/conversations/new" |
| 52 | + response = requests.post( |
| 53 | + url, |
| 54 | + headers=get_headers(), |
| 55 | + json={"type": "chat"}, |
| 56 | + ) |
| 57 | + if response.status_code == 200: |
| 58 | + conv_data = response.json() |
| 59 | + # Set the agent_id to the manager's ID since this is a workforce conversation |
| 60 | + conv_data["agent_id"] = self.manager.id |
| 61 | + return Conversation(**conv_data) |
| 62 | + else: |
| 63 | + raise Exception( |
| 64 | + f"Failed to create new conversation: {response.status_code} - {response.reason}" |
| 65 | + ) |
| 66 | + |
| 67 | + def list_conversations(self) -> List[Conversation]: |
| 68 | + """List all conversations for this workforce.""" |
| 69 | + url = f"https://api.agentx.so/api/v1/access/teams/{self.id}/conversations" |
| 70 | + response = requests.get(url, headers=get_headers()) |
| 71 | + if response.status_code == 200: |
| 72 | + conversations = [] |
| 73 | + for conv_data in response.json(): |
| 74 | + # Set the agent_id to the manager's ID since this is a workforce conversation |
| 75 | + conv_data["agent_id"] = self.manager.id |
| 76 | + conversations.append(Conversation(**conv_data)) |
| 77 | + return conversations |
| 78 | + else: |
| 79 | + raise Exception( |
| 80 | + f"Failed to list conversations: {response.status_code} - {response.reason}" |
| 81 | + ) |
| 82 | + |
| 83 | + def chat_stream( |
| 84 | + self, conversation_id: str, message: str, context: int = -1 |
| 85 | + ) -> Iterator[ChatResponse]: |
| 86 | + """Send a message to a team conversation and stream the response.""" |
| 87 | + url = f"https://api.agentx.so/api/v1/access/teams/conversations/{conversation_id}/jsonmessagesse" |
| 88 | + response = requests.post( |
| 89 | + url, headers=get_headers(), json={"message": message, "context": context} |
| 90 | + ) |
| 91 | + result = "" |
| 92 | + if response.status_code == 200: |
| 93 | + buf = b"" |
| 94 | + for chunk in response.iter_content(): |
| 95 | + buf += chunk |
| 96 | + try: |
| 97 | + chunk = buf.decode("utf-8") |
| 98 | + except UnicodeDecodeError: |
| 99 | + continue |
| 100 | + result += chunk |
| 101 | + buf = b"" |
| 102 | + try: |
| 103 | + if result.count("{") == result.count("}"): |
| 104 | + catch_json = json.loads(result) |
| 105 | + if catch_json: |
| 106 | + result = "" |
| 107 | + yield ChatResponse( |
| 108 | + text=catch_json.get("text"), |
| 109 | + cot=catch_json.get("cot"), |
| 110 | + botId=catch_json.get("botId"), |
| 111 | + reference=catch_json.get("reference"), |
| 112 | + tasks=catch_json.get("tasks"), |
| 113 | + ) |
| 114 | + except json.JSONDecodeError: |
| 115 | + continue |
| 116 | + else: |
| 117 | + raise Exception( |
| 118 | + f"Failed to send message: {response.status_code} - {response.reason}" |
| 119 | + ) |
0 commit comments