Files
EvoScientist-Multi/EvoScientist/commands/implementation/channel.py
T
Wiktor Cupiał 1d117ff277 feat: completion enchancements (#302)
* feat: completion enchancements

* fix: handle exception

* fix: duplicate view

* fix: remove deadcode

* fix tab
2026-07-05 05:14:42 +00:00

196 lines
7.5 KiB
Python

from __future__ import annotations
from typing import ClassVar
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
from ..base import Command, CommandContext, SubCommand
from ..manager import manager
class ChannelCommand(Command):
"""Configure messaging channels."""
name = "/channel"
description = "Configure messaging channels"
category = "Channels"
subcommands: ClassVar[list[SubCommand]] = [
SubCommand("status", "Show channel status"),
SubCommand("stop", "Stop running channels"),
SubCommand("telegram", "Start Telegram channel"),
SubCommand("discord", "Start Discord channel"),
SubCommand("slack", "Start Slack channel"),
SubCommand("feishu", "Start Feishu channel"),
SubCommand("dingtalk", "Start DingTalk channel"),
SubCommand("wechat", "Start WeChat channel"),
SubCommand("email", "Start Email channel"),
SubCommand("imessage", "Start iMessage channel"),
]
def needs_agent(self, args: list[str]) -> bool:
# ``status`` and ``stop`` are introspection / teardown; they
# must work even when the agent load is still in flight or has
# failed. Only start/add flows feed ``ctx.agent`` into
# ``_start_channels_bus_mode``.
subcmd = args[0].lower() if args else ""
return subcmd not in {"status", "stop"}
async def execute(self, ctx: CommandContext, args: list[str]) -> None:
import EvoScientist.cli.channel as _ch_mod
from ...cli.channel import (
_channels_is_running,
_channels_running_list,
_channels_stop,
_start_channels_bus_mode,
)
from ...config import load_config
subcmd = args[0].lower() if args else ""
if subcmd == "status" or (not subcmd and _channels_is_running()):
running = _channels_running_list()
if running and _ch_mod._manager:
detailed = _ch_mod._manager.get_detailed_status()
table = Table(
title="Channel Status",
show_header=True,
header_style="bold cyan",
)
table.add_column("Channel")
table.add_column("Status")
table.add_column("Details", style="dim")
for name, info in detailed.items():
ok = info.get("running", False)
detail = (
f"sent: {info.get('sent', 0)}, recv: {info.get('received', 0)}"
)
status = (
"[green]\u25cf online[/green]"
if ok
else "[red]\u25cb offline[/red]"
)
table.add_row(name, status, detail)
ctx.ui.mount_renderable(table)
else:
ctx.ui.append_system(
"No messaging channels are currently active.", style="yellow"
)
return
if subcmd == "stop":
target = args[1] if len(args) > 1 else ""
if not _channels_is_running():
ctx.ui.append_system("No channels are running.", style="dim")
else:
if target:
_channels_stop(target, runtime=ctx.channel_runtime)
ctx.ui.append_system(f"Channel '{target}' stopped.", style="green")
else:
_channels_stop(runtime=ctx.channel_runtime)
ctx.ui.append_system("All channels stopped.", style="green")
return
config = load_config()
send_thinking = config.channel_send_thinking
# If args specifies channels, use them, otherwise use config
# Handle both space-separated and comma-separated inputs
requested = []
for a in args:
requested.extend([t.strip() for t in a.split(",") if t.strip()])
if _channels_is_running():
if not requested:
ctx.ui.append_system(
"Channels already running. Specify type to add: /channel <type>",
style="yellow",
)
return
ctx.ui.append_system(f"Adding channel(s): {', '.join(requested)}...")
from ...cli.channel import _add_channel_to_running_bus
# Bind the runtime up-front so partial-success states (one
# channel attached, next one raises) still leave the bus
# observing the latest agent/thread refs.
if ctx.channel_runtime is not None:
ctx.channel_runtime.bind(ctx.agent, ctx.thread_id)
try:
for ct in requested:
_add_channel_to_running_bus(ct, config, send_thinking=send_thinking)
ctx.ui.append_system("Channel(s) added successfully.", style="green")
except Exception as e:
ctx.ui.append_system(f"Failed to add channels: {e}", style="red")
return
ctx.ui.append_system("Starting channels...")
original = config.channel_enabled
if requested:
config.channel_enabled = ",".join(requested)
if not config.channel_enabled:
ctx.ui.append_system(
"No channels enabled in config. Use /channel <type> to start one.",
style="yellow",
)
ctx.ui.append_system(
"Types: telegram, discord, slack, feishu, dingtalk, wechat, email, imessage",
style="dim",
)
return
try:
# We need to make sure we don't block the UI.
# _start_channels_bus_mode might start threads/loops.
_start_channels_bus_mode(
config,
ctx.agent,
ctx.thread_id,
send_thinking=send_thinking,
)
if ctx.channel_runtime is not None:
ctx.channel_runtime.bind(ctx.agent, ctx.thread_id)
# Show status panel
if _ch_mod._manager:
detailed = _ch_mod._manager.get_detailed_status()
all_ok = all(info.get("running", False) for info in detailed.values())
lines = []
for name, info in detailed.items():
ok = info.get("running", False)
line = Text()
if ok:
line.append("\u25cf ", style="green")
line.append(name, style="bold")
else:
line.append("\u25cb ", style="red")
line.append(name, style="bold red")
detail = (
f"sent: {info.get('sent', 0)}, recv: {info.get('received', 0)}"
)
line.append(f" {detail}", style="dim")
lines.append(line)
body = Text("\n").join(lines)
border = "green" if all_ok else "yellow"
ctx.ui.mount_renderable(
Panel(
body,
title="[bold]Channels[/bold]",
border_style=border,
expand=False,
)
)
except Exception as e:
ctx.ui.append_system(f"Failed to start channels: {e}", style="red")
finally:
config.channel_enabled = original
# Register Channel command
manager.register(ChannelCommand())