meshagent-agents 0.0.36__py3-none-any.whl → 0.0.38__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of meshagent-agents might be problematic. Click here for more details.
- meshagent/agents/__init__.py +27 -2
- meshagent/agents/adapter.py +18 -9
- meshagent/agents/agent.py +317 -214
- meshagent/agents/chat.py +390 -267
- meshagent/agents/context.py +58 -30
- meshagent/agents/development.py +11 -13
- meshagent/agents/hosting.py +109 -46
- meshagent/agents/indexer.py +241 -224
- meshagent/agents/listener.py +55 -52
- meshagent/agents/mail.py +145 -109
- meshagent/agents/planning.py +294 -199
- meshagent/agents/prompt.py +14 -12
- meshagent/agents/pydantic.py +98 -61
- meshagent/agents/schemas/__init__.py +11 -0
- meshagent/agents/schemas/document.py +32 -21
- meshagent/agents/schemas/gallery.py +23 -14
- meshagent/agents/schemas/presentation.py +33 -17
- meshagent/agents/schemas/schema.py +99 -45
- meshagent/agents/schemas/super_editor_document.py +52 -46
- meshagent/agents/single_shot_writer.py +37 -31
- meshagent/agents/thread_schema.py +74 -32
- meshagent/agents/utils.py +20 -12
- meshagent/agents/version.py +1 -1
- meshagent/agents/worker.py +48 -28
- meshagent/agents/writer.py +36 -23
- meshagent_agents-0.0.38.dist-info/METADATA +64 -0
- meshagent_agents-0.0.38.dist-info/RECORD +30 -0
- meshagent_agents-0.0.36.dist-info/METADATA +0 -36
- meshagent_agents-0.0.36.dist-info/RECORD +0 -30
- {meshagent_agents-0.0.36.dist-info → meshagent_agents-0.0.38.dist-info}/WHEEL +0 -0
- {meshagent_agents-0.0.36.dist-info → meshagent_agents-0.0.38.dist-info}/licenses/LICENSE +0 -0
- {meshagent_agents-0.0.36.dist-info → meshagent_agents-0.0.38.dist-info}/top_level.txt +0 -0
meshagent/agents/context.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
from typing import Optional
|
|
3
2
|
from copy import deepcopy
|
|
4
3
|
from meshagent.api import RoomClient
|
|
@@ -7,22 +6,30 @@ from meshagent.api.participant import Participant
|
|
|
7
6
|
|
|
8
7
|
import uuid
|
|
9
8
|
|
|
9
|
+
|
|
10
10
|
class AgentChatContext:
|
|
11
|
-
def __init__(
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
*,
|
|
14
|
+
messages: Optional[list[dict]] = None,
|
|
15
|
+
system_role: Optional[str] = None,
|
|
16
|
+
previous_messages: Optional[list[dict]] = None,
|
|
17
|
+
previous_response_id: Optional[str] = None,
|
|
18
|
+
):
|
|
12
19
|
self.id = str(uuid.uuid4())
|
|
13
|
-
if messages
|
|
20
|
+
if messages is None:
|
|
14
21
|
messages = list[dict]()
|
|
15
22
|
self._messages = messages.copy()
|
|
16
|
-
if system_role
|
|
23
|
+
if system_role is None:
|
|
17
24
|
system_role = "system"
|
|
18
25
|
self._system_role = system_role
|
|
19
26
|
|
|
20
|
-
if previous_messages
|
|
27
|
+
if previous_messages is None:
|
|
21
28
|
previous_messages = list[dict]()
|
|
22
29
|
|
|
23
30
|
self._previous_response_id = previous_response_id
|
|
24
31
|
self._previous_messages = previous_messages
|
|
25
|
-
|
|
32
|
+
|
|
26
33
|
@property
|
|
27
34
|
def messages(self):
|
|
28
35
|
return self._messages
|
|
@@ -30,65 +37,86 @@ class AgentChatContext:
|
|
|
30
37
|
@property
|
|
31
38
|
def system_role(self):
|
|
32
39
|
return self._system_role
|
|
33
|
-
|
|
40
|
+
|
|
34
41
|
@property
|
|
35
42
|
def previous_messages(self):
|
|
36
43
|
return self._previous_messages
|
|
37
|
-
|
|
44
|
+
|
|
38
45
|
@property
|
|
39
46
|
def previous_response_id(self):
|
|
40
47
|
return self._previous_response_id
|
|
41
|
-
|
|
48
|
+
|
|
42
49
|
def track_response(self, id: str):
|
|
43
50
|
self._previous_response_id = id
|
|
44
51
|
self._previous_messages.extend(self.messages)
|
|
45
52
|
self.messages.clear()
|
|
46
53
|
|
|
47
54
|
def append_rules(self, rules: list[str]):
|
|
48
|
-
|
|
49
55
|
system_message = None
|
|
50
|
-
|
|
56
|
+
|
|
51
57
|
for m in self.messages:
|
|
52
58
|
if m["role"] == self.system_role:
|
|
53
59
|
system_message = m
|
|
54
60
|
break
|
|
55
61
|
|
|
56
|
-
if system_message
|
|
57
|
-
system_message = {"role": self.system_role, "content"
|
|
62
|
+
if system_message is None:
|
|
63
|
+
system_message = {"role": self.system_role, "content": ""}
|
|
58
64
|
self.messages.insert(0, system_message)
|
|
59
65
|
|
|
60
66
|
plan = f"Rules:\n-{'\n-'.join(rules)}\n"
|
|
61
67
|
system_message["content"] = system_message["content"] + plan
|
|
62
68
|
|
|
63
69
|
def append_assistant_message(self, message: str) -> None:
|
|
64
|
-
self.messages.append({
|
|
65
|
-
|
|
70
|
+
self.messages.append({"role": "assistant", "content": message})
|
|
71
|
+
|
|
66
72
|
def append_user_message(self, message: str) -> None:
|
|
67
|
-
self.messages.append({
|
|
73
|
+
self.messages.append({"role": "user", "content": message})
|
|
68
74
|
|
|
69
75
|
def append_user_image(self, url: str) -> None:
|
|
70
|
-
self.messages.append(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
self.messages.append(
|
|
77
|
+
{
|
|
78
|
+
"role": "user",
|
|
79
|
+
"content": [
|
|
80
|
+
{"type": "image_url", "image_url": {"url": url, "detail": "auto"}}
|
|
81
|
+
],
|
|
82
|
+
}
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
def copy(self) -> "AgentChatContext":
|
|
86
|
+
return AgentChatContext(
|
|
87
|
+
messages=deepcopy(self.messages), system_role=self._system_role
|
|
88
|
+
)
|
|
75
89
|
|
|
76
90
|
def to_json(self) -> dict:
|
|
77
91
|
return {
|
|
78
|
-
"messages"
|
|
79
|
-
"system_role"
|
|
80
|
-
"previous_messages"
|
|
81
|
-
"previous_response_id"
|
|
92
|
+
"messages": self.messages,
|
|
93
|
+
"system_role": self.system_role,
|
|
94
|
+
"previous_messages": self.previous_messages,
|
|
95
|
+
"previous_response_id": self.previous_response_id,
|
|
82
96
|
}
|
|
83
|
-
|
|
97
|
+
|
|
84
98
|
@staticmethod
|
|
85
99
|
def from_json(json: dict):
|
|
86
|
-
return AgentChatContext(
|
|
100
|
+
return AgentChatContext(
|
|
101
|
+
messages=json["messages"],
|
|
102
|
+
system_role=json.get("system_role", None),
|
|
103
|
+
previous_messages=json.get("previous_messages", None),
|
|
104
|
+
previous_response_id=json.get("previous_response_id", None),
|
|
105
|
+
)
|
|
106
|
+
|
|
87
107
|
|
|
88
108
|
class AgentCallContext:
|
|
89
|
-
def __init__(
|
|
109
|
+
def __init__(
|
|
110
|
+
self,
|
|
111
|
+
*,
|
|
112
|
+
chat: AgentChatContext,
|
|
113
|
+
room: RoomClient,
|
|
114
|
+
toolkits: Optional[list[Toolkit]] = None,
|
|
115
|
+
caller: Optional[Participant] = None,
|
|
116
|
+
on_behalf_of: Optional[Participant] = None,
|
|
117
|
+
):
|
|
90
118
|
self._room = room
|
|
91
|
-
if toolkits
|
|
119
|
+
if toolkits is None:
|
|
92
120
|
toolkits = list[Toolkit]()
|
|
93
121
|
self._toolkits = toolkits
|
|
94
122
|
self._chat = chat
|
|
@@ -110,7 +138,7 @@ class AgentCallContext:
|
|
|
110
138
|
@property
|
|
111
139
|
def on_behalf_of(self):
|
|
112
140
|
return self._on_behalf_of
|
|
113
|
-
|
|
141
|
+
|
|
114
142
|
@property
|
|
115
143
|
def room(self):
|
|
116
144
|
return self._room
|
meshagent/agents/development.py
CHANGED
|
@@ -1,32 +1,30 @@
|
|
|
1
1
|
from .agent import SingleRoomAgent
|
|
2
|
-
from meshagent.api import websocket_protocol, RoomClient
|
|
2
|
+
from meshagent.api import websocket_protocol, RoomClient
|
|
3
3
|
import asyncio
|
|
4
4
|
import signal
|
|
5
|
-
import json
|
|
6
5
|
|
|
7
|
-
from aiohttp import web
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
async def connect_development_agent(*, room_name:str, agent: SingleRoomAgent):
|
|
11
|
-
|
|
7
|
+
async def connect_development_agent(*, room_name: str, agent: SingleRoomAgent):
|
|
12
8
|
async with RoomClient(
|
|
13
|
-
protocol
|
|
14
|
-
|
|
9
|
+
protocol=websocket_protocol(
|
|
10
|
+
participant_name=agent.name, room_name=room_name, role="agent"
|
|
11
|
+
)
|
|
12
|
+
) as room:
|
|
15
13
|
await agent.start(room=room)
|
|
16
14
|
|
|
17
15
|
try:
|
|
18
|
-
|
|
19
16
|
term = asyncio.Future()
|
|
20
17
|
|
|
21
18
|
def clean_termination(signal, frame):
|
|
22
19
|
term.set_result(True)
|
|
23
|
-
|
|
20
|
+
|
|
24
21
|
signal.signal(signal.SIGTERM, clean_termination)
|
|
25
22
|
signal.signal(signal.SIGABRT, clean_termination)
|
|
26
23
|
|
|
27
|
-
await asyncio.wait(
|
|
24
|
+
await asyncio.wait(
|
|
25
|
+
[asyncio.ensure_future(room.protocol.wait_for_close()), term],
|
|
26
|
+
return_when=asyncio.FIRST_COMPLETED,
|
|
27
|
+
)
|
|
28
28
|
|
|
29
29
|
finally:
|
|
30
|
-
|
|
31
30
|
await agent.stop()
|
|
32
|
-
|
meshagent/agents/hosting.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
|
|
3
|
-
from meshagent.api import
|
|
4
|
-
from meshagent.api.webhooks import WebhookServer,
|
|
3
|
+
from meshagent.api import RoomMessage
|
|
4
|
+
from meshagent.api.webhooks import WebhookServer, CallEvent
|
|
5
5
|
from meshagent.api import WebSocketClientProtocol
|
|
6
6
|
from meshagent.api.room_server_client import RoomClient
|
|
7
7
|
from meshagent.agents import SingleRoomAgent
|
|
8
8
|
from aiohttp import web
|
|
9
9
|
import asyncio
|
|
10
10
|
|
|
11
|
-
import logging
|
|
12
11
|
from typing import Callable, Optional
|
|
13
12
|
|
|
14
13
|
from .agent import TaskRunner
|
|
@@ -16,77 +15,137 @@ from .agent import TaskRunner
|
|
|
16
15
|
logger = logging.getLogger("hosting")
|
|
17
16
|
|
|
18
17
|
|
|
18
|
+
class RemoteTaskRunnerServer[T: TaskRunner](WebhookServer):
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
*,
|
|
22
|
+
cls: Optional[T] = None,
|
|
23
|
+
path: Optional[str] = None,
|
|
24
|
+
app: Optional[web.Application] = None,
|
|
25
|
+
host=None,
|
|
26
|
+
port=None,
|
|
27
|
+
webhook_secret=None,
|
|
28
|
+
create_agent: Optional[Callable[[dict], TaskRunner]] = None,
|
|
29
|
+
validate_webhook_secret: Optional[bool] = None,
|
|
30
|
+
):
|
|
31
|
+
super().__init__(
|
|
32
|
+
path=path,
|
|
33
|
+
app=app,
|
|
34
|
+
host=host,
|
|
35
|
+
port=port,
|
|
36
|
+
webhook_secret=webhook_secret,
|
|
37
|
+
validate_webhook_secret=validate_webhook_secret,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
if create_agent is None:
|
|
19
41
|
|
|
20
|
-
class RemoteTaskRunnerServer[T:TaskRunner](WebhookServer):
|
|
21
|
-
def __init__(self, *, cls: Optional[T] = None, path: Optional[str] = None, app: Optional[web.Application] = None, host = None, port = None, webhook_secret = None, create_agent: Optional[Callable[[dict],TaskRunner]] = None, validate_webhook_secret: Optional[bool] = None):
|
|
22
|
-
super().__init__(path=path, app=app, host=host, port=port, webhook_secret=webhook_secret, validate_webhook_secret=validate_webhook_secret)
|
|
23
|
-
|
|
24
|
-
if create_agent == None:
|
|
25
42
|
def default_create_agent(arguments: dict) -> TaskRunner:
|
|
26
43
|
return cls(**arguments)
|
|
27
|
-
|
|
44
|
+
|
|
28
45
|
create_agent = default_create_agent
|
|
29
46
|
|
|
30
47
|
self._create_agent = create_agent
|
|
31
48
|
|
|
32
|
-
async def _spawn(
|
|
49
|
+
async def _spawn(
|
|
50
|
+
self,
|
|
51
|
+
*,
|
|
52
|
+
room_name: str,
|
|
53
|
+
room_url: str,
|
|
54
|
+
token: str,
|
|
55
|
+
arguments: Optional[dict] = None,
|
|
56
|
+
):
|
|
33
57
|
agent = self._create_agent(arguments=arguments)
|
|
34
|
-
|
|
58
|
+
|
|
35
59
|
async def run():
|
|
36
|
-
async with RoomClient(
|
|
37
|
-
|
|
60
|
+
async with RoomClient(
|
|
61
|
+
protocol=WebSocketClientProtocol(url=room_url, token=token)
|
|
62
|
+
) as room:
|
|
38
63
|
dismissed = asyncio.Future()
|
|
39
64
|
|
|
40
65
|
def on_message(message: RoomMessage):
|
|
41
66
|
if message.type == "dismiss":
|
|
42
|
-
logger.info(
|
|
67
|
+
logger.info(
|
|
68
|
+
f"dismissed task runner by {message.from_participant_id}"
|
|
69
|
+
)
|
|
43
70
|
dismissed.set_result(True)
|
|
44
71
|
|
|
45
72
|
room.messaging.on("message", on_message)
|
|
46
|
-
|
|
73
|
+
|
|
47
74
|
await agent.start(room=room)
|
|
48
75
|
|
|
49
|
-
done, pending = await asyncio.wait(
|
|
50
|
-
dismissed,
|
|
51
|
-
asyncio.
|
|
52
|
-
|
|
53
|
-
|
|
76
|
+
done, pending = await asyncio.wait(
|
|
77
|
+
[dismissed, asyncio.ensure_future(room.protocol.wait_for_close())],
|
|
78
|
+
return_when=asyncio.FIRST_COMPLETED,
|
|
79
|
+
)
|
|
54
80
|
|
|
55
81
|
await agent.stop()
|
|
56
82
|
|
|
57
83
|
def on_done(task: asyncio.Task):
|
|
58
84
|
try:
|
|
59
|
-
|
|
85
|
+
task.result()
|
|
60
86
|
except Exception as e:
|
|
61
87
|
logger.error("agent encountered an error", exc_info=e)
|
|
62
|
-
|
|
88
|
+
|
|
63
89
|
task = asyncio.create_task(run())
|
|
64
90
|
task.add_done_callback(on_done)
|
|
65
|
-
|
|
66
|
-
async def on_call(self, event: CallEvent):
|
|
67
|
-
await self._spawn(room_name=event.room_name, room_url=event.room_url, token=event.token, arguments=event.arguments)
|
|
68
91
|
|
|
92
|
+
async def on_call(self, event: CallEvent):
|
|
93
|
+
await self._spawn(
|
|
94
|
+
room_name=event.room_name,
|
|
95
|
+
room_url=event.room_url,
|
|
96
|
+
token=event.token,
|
|
97
|
+
arguments=event.arguments,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class RemoteAgentServer[T: SingleRoomAgent](WebhookServer):
|
|
102
|
+
def __init__(
|
|
103
|
+
self,
|
|
104
|
+
*,
|
|
105
|
+
cls: Optional[T] = None,
|
|
106
|
+
path: Optional[str] = None,
|
|
107
|
+
app: Optional[web.Application] = None,
|
|
108
|
+
host=None,
|
|
109
|
+
port=None,
|
|
110
|
+
webhook_secret=None,
|
|
111
|
+
create_agent: Optional[Callable[[dict], SingleRoomAgent]] = None,
|
|
112
|
+
validate_webhook_secret: Optional[bool] = None,
|
|
113
|
+
):
|
|
114
|
+
super().__init__(
|
|
115
|
+
path=path,
|
|
116
|
+
app=app,
|
|
117
|
+
host=host,
|
|
118
|
+
port=port,
|
|
119
|
+
webhook_secret=webhook_secret,
|
|
120
|
+
validate_webhook_secret=validate_webhook_secret,
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
if create_agent is None:
|
|
69
124
|
|
|
70
|
-
class RemoteAgentServer[T:SingleRoomAgent](WebhookServer):
|
|
71
|
-
def __init__(self, *, cls: Optional[T] = None, path: Optional[str] = None, app: Optional[web.Application] = None, host = None, port = None, webhook_secret = None, create_agent: Optional[Callable[[dict], SingleRoomAgent]] = None, validate_webhook_secret: Optional[bool] = None):
|
|
72
|
-
super().__init__(path=path, app=app, host=host, port=port, webhook_secret=webhook_secret, validate_webhook_secret=validate_webhook_secret)
|
|
73
|
-
|
|
74
|
-
if create_agent == None:
|
|
75
125
|
def default_create_agent(arguments: dict) -> SingleRoomAgent:
|
|
76
126
|
return cls(**arguments)
|
|
77
|
-
|
|
127
|
+
|
|
78
128
|
create_agent = default_create_agent
|
|
79
129
|
|
|
80
130
|
self._create_agent = create_agent
|
|
81
|
-
|
|
82
|
-
async def _spawn(self, *, room_name: str, room_url: str, token: str, arguments: Optional[dict] = None):
|
|
83
131
|
|
|
84
|
-
|
|
132
|
+
async def _spawn(
|
|
133
|
+
self,
|
|
134
|
+
*,
|
|
135
|
+
room_name: str,
|
|
136
|
+
room_url: str,
|
|
137
|
+
token: str,
|
|
138
|
+
arguments: Optional[dict] = None,
|
|
139
|
+
):
|
|
140
|
+
logger.info(
|
|
141
|
+
f"spawning agent on room: {room_name} url: {room_url} arguments: {arguments}"
|
|
142
|
+
)
|
|
85
143
|
agent = self._create_agent(arguments=arguments)
|
|
86
144
|
|
|
87
145
|
async def run():
|
|
88
|
-
async with RoomClient(
|
|
89
|
-
|
|
146
|
+
async with RoomClient(
|
|
147
|
+
protocol=WebSocketClientProtocol(url=room_url, token=token)
|
|
148
|
+
) as room:
|
|
90
149
|
dismissed = asyncio.Future()
|
|
91
150
|
|
|
92
151
|
def on_message(message: RoomMessage):
|
|
@@ -95,25 +154,29 @@ class RemoteAgentServer[T:SingleRoomAgent](WebhookServer):
|
|
|
95
154
|
dismissed.set_result(True)
|
|
96
155
|
|
|
97
156
|
room.messaging.on("message", on_message)
|
|
98
|
-
|
|
157
|
+
|
|
99
158
|
await agent.start(room=room)
|
|
100
159
|
|
|
101
|
-
done, pending = await asyncio.wait(
|
|
102
|
-
dismissed,
|
|
103
|
-
asyncio.
|
|
104
|
-
|
|
105
|
-
|
|
160
|
+
done, pending = await asyncio.wait(
|
|
161
|
+
[dismissed, asyncio.ensure_future(room.protocol.wait_for_close())],
|
|
162
|
+
return_when=asyncio.FIRST_COMPLETED,
|
|
163
|
+
)
|
|
164
|
+
|
|
106
165
|
await agent.stop()
|
|
107
166
|
|
|
108
167
|
def on_done(task: asyncio.Task):
|
|
109
168
|
try:
|
|
110
|
-
|
|
169
|
+
task.result()
|
|
111
170
|
except Exception as e:
|
|
112
171
|
logger.error("agent encountered an error", exc_info=e)
|
|
113
172
|
|
|
114
173
|
task = asyncio.create_task(run())
|
|
115
174
|
task.add_done_callback(on_done)
|
|
116
|
-
|
|
117
|
-
async def on_call(self, event: CallEvent):
|
|
118
|
-
await self._spawn(room_name=event.room_name, room_url=event.room_url, token=event.token, arguments=event.arguments)
|
|
119
175
|
|
|
176
|
+
async def on_call(self, event: CallEvent):
|
|
177
|
+
await self._spawn(
|
|
178
|
+
room_name=event.room_name,
|
|
179
|
+
room_url=event.room_url,
|
|
180
|
+
token=event.token,
|
|
181
|
+
arguments=event.arguments,
|
|
182
|
+
)
|