-
-
Notifications
You must be signed in to change notification settings - Fork 895
Expand file tree
/
Copy pathgateway.py
More file actions
232 lines (187 loc) · 7.35 KB
/
gateway.py
File metadata and controls
232 lines (187 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""
Gateway command group for PraisonAI CLI.
Provides commands for managing the WebSocket gateway with multi-bot support.
"""
from typing import Optional
import typer
app = typer.Typer(
help="Manage the PraisonAI Gateway server",
no_args_is_help=True,
)
@app.command("start")
def gateway_start(
host: str = typer.Option("127.0.0.1", "--host", help="Host to bind to"),
port: int = typer.Option(8765, "--port", help="Port to listen on"),
agents: Optional[str] = typer.Option(None, "--agents", help="Path to agent configuration file"),
config: Optional[str] = typer.Option(None, "--config", help="Path to gateway.yaml for multi-bot mode"),
):
"""Start the gateway server.
Examples:
praisonai gateway start
praisonai gateway start --config gateway.yaml
praisonai gateway start --agents agents.yaml --port 9000
"""
from ..features.gateway import GatewayHandler
handler = GatewayHandler()
handler.start(host=host, port=port, agent_file=agents, config_file=config)
@app.command("status")
def gateway_status(
host: str = typer.Option("127.0.0.1", "--host", help="Gateway host"),
port: int = typer.Option(8765, "--port", help="Gateway port"),
):
"""Check gateway status.
Examples:
praisonai gateway status
praisonai gateway status --port 9000
"""
from ..features.gateway import GatewayHandler
handler = GatewayHandler()
handler.status(host=host, port=port)
@app.command("channels")
def gateway_channels(
config: str = typer.Option("gateway.yaml", "--config", "-c", help="Path to gateway.yaml"),
json_output: bool = typer.Option(False, "--json", help="Output JSON"),
):
"""List channels configured in a gateway.yaml file.
Examples:
praisonai gateway channels
praisonai gateway channels --config my-gateway.yaml --json
"""
import os
import yaml
if not os.path.exists(config):
print(f"Error: Config file not found: {config}")
raise typer.Exit(1)
with open(config) as f:
cfg = yaml.safe_load(f) or {}
channels = cfg.get("channels", {})
if not channels:
print("No channels configured.")
raise typer.Exit(0)
if json_output:
import json
print(json.dumps(channels, indent=2))
raise typer.Exit(0)
try:
from rich.table import Table
from rich.console import Console
console = Console()
table = Table(title="Configured Channels")
table.add_column("Name", style="cyan")
table.add_column("Platform", style="green")
table.add_column("Token", style="yellow")
table.add_column("Config Keys", style="dim")
for name, ch_cfg in channels.items():
platform = ch_cfg.get("platform", "unknown")
token_val = ch_cfg.get("token", "")
has_token = "✅ set" if token_val else "❌ missing"
keys = ", ".join(
k for k in ch_cfg.keys() if k not in ("platform", "token")
)
table.add_row(name, platform, has_token, keys or "—")
console.print(table)
except ImportError:
print(f"{'Name':<20} {'Platform':<12} {'Token':<12}")
print("-" * 44)
for name, ch_cfg in channels.items():
platform = ch_cfg.get("platform", "unknown")
has_token = "set" if ch_cfg.get("token") else "missing"
print(f"{name:<20} {platform:<12} {has_token:<12}")
@app.command("send")
def gateway_send(
config: str = typer.Option("gateway.yaml", "--config", "-c", help="Path to gateway.yaml"),
channel: str = typer.Option(..., "--channel", help="Channel name from config (e.g. 'telegram')"),
channel_id: str = typer.Option(..., "--channel-id", help="Target chat/channel ID"),
message: str = typer.Option(..., "--message", "-m", help="Message text to send"),
thread_id: Optional[str] = typer.Option(None, "--thread-id", help="Optional thread ID"),
):
"""Send a one-shot test message to a channel bot.
Instantiates the bot from gateway.yaml config, sends the message, then exits.
Useful for testing scheduled delivery targets.
Examples:
praisonai gateway send --config gateway.yaml --channel telegram --channel-id 12345 -m "Hello"
"""
import os
import asyncio
import yaml
if not os.path.exists(config):
print(f"Error: Config file not found: {config}")
raise typer.Exit(1)
with open(config) as f:
cfg = yaml.safe_load(f) or {}
channels_cfg = cfg.get("channels", {})
ch_cfg = channels_cfg.get(channel)
if not ch_cfg:
available = ", ".join(channels_cfg.keys()) if channels_cfg else "(none)"
print(f"Error: Channel '{channel}' not found in config. Available: {available}")
raise typer.Exit(1)
platform = ch_cfg.get("platform", channel)
token = ch_cfg.get("token", "")
# Resolve env vars in token
if token and token.startswith("${") and token.endswith("}"):
env_var = token[2:-1]
token = os.environ.get(env_var, "")
if not token:
print(f"Error: Environment variable {env_var} not set")
raise typer.Exit(1)
async def _send():
try:
from praisonai.gateway.server import WebSocketGateway
bot = WebSocketGateway._create_bot(channel, ch_cfg)
except Exception as e:
print(f"Error creating bot: {e}")
raise typer.Exit(1)
try:
await bot.start()
await asyncio.sleep(1) # Let bot initialise
result = await bot.send_message(
channel_id, message, thread_id=thread_id,
)
print(f"✅ Message sent to {channel}:{channel_id}")
if hasattr(result, "message_id"):
print(f" Message ID: {result.message_id}")
except Exception as e:
print(f"❌ Send failed: {e}")
raise typer.Exit(1)
finally:
try:
await bot.stop()
except Exception:
pass
try:
asyncio.run(_send())
except typer.Exit:
raise
except Exception as e:
print(f"Error: {e}")
raise typer.Exit(1)
@app.callback(invoke_without_command=True)
def gateway_callback(ctx: typer.Context):
"""Show gateway help if no subcommand provided."""
if ctx.invoked_subcommand is None:
help_text = """
[bold cyan]PraisonAI Gateway - Multi-Bot WebSocket Server[/bold cyan]
Manage the gateway server: praisonai gateway <command>
[bold]Commands:[/bold]
[green]start[/green] Start the gateway server
[green]status[/green] Check gateway status
[green]channels[/green] List channels from gateway.yaml
[green]send[/green] Send a test message to a channel
[bold]Multi-Bot Mode:[/bold]
praisonai gateway start --config gateway.yaml
[bold]Standard Mode:[/bold]
praisonai gateway start
praisonai gateway start --agents agents.yaml --port 9000
[bold]Check Status:[/bold]
praisonai gateway status
[bold]Channel Management:[/bold]
praisonai gateway channels --config gateway.yaml
praisonai gateway send --config gateway.yaml --channel telegram --channel-id 12345 -m "test"
"""
try:
from rich import print as rprint
rprint(help_text)
except ImportError:
import re
plain = re.sub(r'\\[/?[^\\]]+\\]', '', help_text)
print(plain)