Skip to content

Commit b40fad7

Browse files
committed
Improve the CoS server
1 parent 77a57de commit b40fad7

File tree

3 files changed

+87
-37
lines changed

3 files changed

+87
-37
lines changed

coagent/core/util.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,3 @@ def pretty_trace_tool_call(name: str, args: dict[str, any]):
9191
logger.opt(colors=True).trace(
9292
f"<green>{name}</green><magenta>({args_str})</magenta>"
9393
)
94-
95-
96-
def get_nats_servers():
97-
NATS_SERVERS = os.environ.get("NATS_SERVERS", None)
98-
if NATS_SERVERS and len(NATS_SERVERS) > 0:
99-
NATS_SERVERS = NATS_SERVERS.split(",")
100-
return NATS_SERVERS

coagent/cos/app.py

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,88 @@
1+
import argparse
2+
import asyncio
3+
from hypercorn.asyncio import serve
4+
from hypercorn.config import Config
15
from starlette.applications import Starlette
26
from starlette.routing import Route
37

48
from coagent.cos.runtime import CosRuntime
5-
from coagent.runtimes import NATSRuntime, LocalRuntime
6-
from coagent.core.util import get_nats_servers
9+
from coagent.core import set_stderr_logger
10+
from coagent.runtimes import NATSRuntime, HTTPRuntime
711

8-
runtime = CosRuntime(NATSRuntime.from_servers(servers=get_nats_servers()))
912

13+
class Application:
14+
def __init__(self, server: str, auth: str):
15+
if server.startswith("nats://"):
16+
runtime = NATSRuntime.from_servers(server)
17+
elif server.startswith(("http://", "https://")):
18+
runtime = HTTPRuntime.from_server(server, auth)
19+
else:
20+
raise ValueError(f"Unsupported server: {server}")
21+
self.runtime = CosRuntime(runtime)
1022

11-
async def startup():
12-
await runtime.start()
23+
async def startup(self):
24+
await self.runtime.start()
1325

26+
async def shutdown(self):
27+
await self.runtime.stop()
1428

15-
async def shutdown():
16-
await runtime.stop()
17-
18-
19-
routes = [
20-
Route("/runtime/register", runtime.register, methods=["POST"]),
21-
Route("/runtime/channel/subscribe", runtime.subscribe, methods=["POST"]),
22-
Route("/runtime/channel/publish", runtime.publish, methods=["POST"]),
23-
Route("/runtime/channel/publish_multi", runtime.publish_multi, methods=["POST"]),
24-
]
25-
26-
27-
app = Starlette(debug=True, routes=routes, on_startup=[startup], on_shutdown=[shutdown])
29+
@property
30+
def starlette(self) -> Starlette:
31+
routes = [
32+
Route(
33+
"/runtime/register",
34+
self.runtime.register,
35+
methods=["POST"],
36+
),
37+
Route(
38+
"/runtime/channel/subscribe",
39+
self.runtime.subscribe,
40+
methods=["POST"],
41+
),
42+
Route(
43+
"/runtime/channel/publish",
44+
self.runtime.publish,
45+
methods=["POST"],
46+
),
47+
Route(
48+
"/runtime/channel/publish_multi",
49+
self.runtime.publish_multi,
50+
methods=["POST"],
51+
),
52+
]
53+
return Starlette(
54+
debug=True,
55+
routes=routes,
56+
on_startup=[self.startup],
57+
on_shutdown=[self.shutdown],
58+
)
2859

2960

3061
if __name__ == "__main__":
31-
import asyncio
32-
from hypercorn.asyncio import serve
33-
from hypercorn.config import Config
62+
parser = argparse.ArgumentParser()
63+
parser.add_argument(
64+
"--listen",
65+
type=str,
66+
default="127.0.0.1:8000",
67+
help="The listen address of the CoS server. (Defaults to %(default)r)",
68+
)
69+
parser.add_argument(
70+
"--server",
71+
type=str,
72+
default="nats://localhost:4222",
73+
help="The runtime server address. (Defaults to %(default)r)",
74+
)
75+
parser.add_argument(
76+
"--auth",
77+
type=str,
78+
default="",
79+
help="The runtime server authentication token. (Defaults to %(default)r)",
80+
)
81+
args = parser.parse_args()
82+
83+
set_stderr_logger()
84+
app = Application(args.server, args.auth).starlette
3485

3586
config = Config()
36-
config.bind = ["127.0.0.1:8000"]
87+
config.bind = [args.listen]
3788
asyncio.run(serve(app, config))

examples/cos/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ node examples/cos/cos.js
2323
Finally, start a ping-pong client in the third terminal:
2424

2525
```bash
26-
python examples/ping-pong/client.py
26+
coagent server -H type:Ping
2727
```
2828

2929
or start a stream-ping-pong client:
3030

3131
```bash
32-
python examples/stream-ping-pong/client.py
32+
coagent stream_server -H type:Ping --chat
3333
```
3434

3535

@@ -50,27 +50,33 @@ python examples/cos/cos.py
5050
Finally, start a ping-pong client in the third terminal:
5151

5252
```bash
53-
python examples/ping-pong/client.py
53+
coagent server -H type:Ping
5454
```
5555

5656
or start a stream-ping-pong client:
5757

5858
```bash
59-
python examples/stream-ping-pong/client.py
59+
coagent stream_server -H type:Ping --chat
6060
```
6161

6262
### Run Go Agent
6363

64-
Build Go agent:
64+
Start the CoS server in one terminal:
6565

6666
```bash
67-
cd examples/cos/goagent
68-
go build
67+
python coagent/cos/app.py
6968
```
7069

71-
Run a Go agent in one terminal:
70+
Then build and run the Go agent:
7271

7372
```bash
7473
cd examples/cos/goagent
74+
go build
7575
./goagent
7676
```
77+
78+
Finally, start a ping-pong client in the third terminal:
79+
80+
```bash
81+
coagent server -H type:Ping
82+
```

0 commit comments

Comments
 (0)