Skip to content

Commit ab2cea1

Browse files
committed
Update Runtime.register()
1 parent 4cc6b6e commit ab2cea1

File tree

17 files changed

+72
-40
lines changed

17 files changed

+72
-40
lines changed

coagent/core/runtime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async def stop(self) -> None:
3535
await self.deregister()
3636
await self._channel.close()
3737

38-
async def register_spec(self, spec: AgentSpec) -> None:
38+
async def register(self, spec: AgentSpec) -> None:
3939
spec.register(self)
4040

4141
if self._discovery:

coagent/core/types.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,8 @@ async def start(self) -> None:
299299
async def stop(self) -> None:
300300
pass
301301

302-
async def register(
303-
self, name: str, constructor: Constructor, description: str = ""
304-
) -> None:
305-
await self.register_spec(
306-
AgentSpec(name=name, constructor=constructor, description=description)
307-
)
308-
309302
@abc.abstractmethod
310-
async def register_spec(self, spec: AgentSpec) -> None:
303+
async def register(self, spec: AgentSpec) -> None:
311304
pass
312305

313306
@abc.abstractmethod

examples/app-builder/auto_team.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22

3-
from coagent.core import idle_loop, new, set_stderr_logger
43
from coagent.agents import StreamChatAgent, tool
4+
from coagent.core import AgentSpec, idle_loop, new, set_stderr_logger
55
from coagent.runtimes import NATSRuntime
66

77

@@ -27,9 +27,12 @@ async def transfer_to_qa(self):
2727
yield chunk
2828

2929

30+
auto_team = AgentSpec("auto_team", new(AutoTeam))
31+
32+
3033
async def main():
3134
async with NATSRuntime.from_servers() as runtime:
32-
await runtime.register("auto_team", new(AutoTeam))
35+
await runtime.register(auto_team)
3336
await idle_loop()
3437

3538

examples/app-builder/dev.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import asyncio
22
from typing import AsyncIterator
33

4-
from coagent.agents.chat_agent import ChatMessage, chat
4+
from coagent.agents.chat_agent import ChatMessage
5+
from coagent.agents.util import chat_stream
56
from coagent.core import (
7+
AgentSpec,
68
BaseAgent,
79
Context,
810
handler,
@@ -48,17 +50,20 @@ async def handle(
4850
]
4951

5052
reply = ""
51-
async for chunk in chat(msgs):
53+
async for chunk in chat_stream(msgs):
5254
yield ChatMessage(role="assistant", content=chunk.content)
5355
reply += chunk.content
5456

5557
pretty_trace_agent_output("DevEngineer", reply)
5658
msg.messages.append(ChatMessage(role="user", content=reply))
5759

5860

61+
dev = AgentSpec("dev", new(DevEngineer))
62+
63+
5964
async def main():
6065
async with NATSRuntime.from_servers() as runtime:
61-
await runtime.register("dev", new(DevEngineer))
66+
await runtime.register(dev)
6267
await idle_loop()
6368

6469

examples/app-builder/qa.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import asyncio
22
from typing import AsyncIterator
33

4-
from coagent.agents.chat_agent import ChatMessage, chat
4+
from coagent.agents.chat_agent import ChatMessage
5+
from coagent.agents.util import chat_stream
56
from coagent.core import (
7+
AgentSpec,
68
BaseAgent,
79
Context,
810
handler,
@@ -67,17 +69,20 @@ async def handle(
6769
]
6870

6971
reply = ""
70-
async for chunk in chat(msgs):
72+
async for chunk in chat_stream(msgs):
7173
yield ChatMessage(role="assistant", content=chunk.content)
7274
reply += chunk.content
7375

7476
pretty_trace_agent_output("QaEngineer", reply)
7577
msg.messages.append(ChatMessage(role="user", content=reply))
7678

7779

80+
qa = AgentSpec("qa", new(QaEngineer))
81+
82+
7883
async def main():
7984
async with NATSRuntime.from_servers() as runtime:
80-
await runtime.register("qa", new(QaEngineer))
85+
await runtime.register(qa)
8186
await idle_loop()
8287

8388

examples/app-builder/team.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import asyncio
22

33
from coagent.agents import Sequential
4-
from coagent.core import new, set_stderr_logger
4+
from coagent.core import AgentSpec, new, set_stderr_logger
55
from coagent.core.util import idle_loop
66
from coagent.runtimes import NATSRuntime
77

88

9+
team = AgentSpec("team", new(Sequential, "dev", "qa"))
10+
11+
912
async def main():
1013
async with NATSRuntime.from_servers() as runtime:
11-
await runtime.register("team", new(Sequential, "dev", "qa"))
14+
await runtime.register(team)
1215
await idle_loop()
1316

1417

examples/autogen/autogen.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from coagent.agents.chat_agent import ChatHistory, ChatMessage
55
from coagent.core import (
6+
AgentSpec,
67
BaseAgent,
78
Context,
89
handler,
@@ -60,9 +61,12 @@ async def handle(self, msg: ChatHistory, ctx: Context) -> ChatHistory:
6061
return msg
6162

6263

64+
autogen = AgentSpec("autogen", new(AutoGenWeatherAgent))
65+
66+
6367
async def main():
6468
async with NATSRuntime.from_servers() as runtime:
65-
await runtime.register("autogen", new(AutoGenWeatherAgent))
69+
await runtime.register(autogen)
6670
await idle_loop()
6771

6872

examples/discovery/server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import asyncio
33

44
from coagent.core import (
5+
AgentSpec,
56
BaseAgent,
67
Context,
78
GenericMessage,
@@ -27,8 +28,10 @@ async def main(name: str, description: str, server: str):
2728
else:
2829
raise ValueError(f"Unsupported server: {server}")
2930

31+
employee = AgentSpec(name, new(Employee), description=description)
32+
3033
async with runtime:
31-
await runtime.register(name, new(Employee), description=description)
34+
await runtime.register(employee)
3235
await idle_loop()
3336

3437

examples/opencsg/csghub.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from coagent.agents import StreamChatAgent, confirm, RunContext, tool
66
from coagent.agents.messages import ChatMessage
7-
from coagent.core import idle_loop, new, set_stderr_logger
7+
from coagent.core import AgentSpec, idle_loop, new, set_stderr_logger
88
from coagent.runtimes import NATSRuntime
99

1010
import httpx
@@ -58,8 +58,9 @@ async def search_model(
5858

5959

6060
async def main(name: str):
61+
csghub = AgentSpec(name, new(CSGHub))
6162
async with NATSRuntime.from_servers() as runtime:
62-
await runtime.register(name, new(CSGHub))
63+
await runtime.register(csghub)
6364
await idle_loop()
6465

6566

examples/opencsg/dataflow.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import asyncio
33

44
from coagent.agents import StreamChatAgent, RunContext, submit, tool
5-
from coagent.core import idle_loop, new, set_stderr_logger
5+
from coagent.core import AgentSpec, idle_loop, new, set_stderr_logger
66
from coagent.runtimes import NATSRuntime
77

88
import httpx
@@ -34,8 +34,9 @@ async def search_dataset(
3434

3535

3636
async def main(name: str):
37+
dataflow = AgentSpec(name, new(DataFlow))
3738
async with NATSRuntime.from_servers() as runtime:
38-
await runtime.register(name, new(DataFlow))
39+
await runtime.register(dataflow)
3940
await idle_loop()
4041

4142

0 commit comments

Comments
 (0)