Include Luna AI Assistant design docs covering channels, configuration, core architecture, memory, scheduler, and skills. Add reference docs from OpenClaw and ZeroClaw projects, plus Mistral and OpenAI API specs.
4.4 KiB
Channels
The Channels module provides a unified abstraction for communication platforms like the CLI and Telegram. It handles message transport, while Core manages AI context and session state.
Luna.Channels.Abstractions
This project defines the contracts and event models for all channel implementations.
IChannel
The primary interface for any communication transport. There is no abstract base class; IChannel is the complete contract.
public interface IChannel : IDisposable
{
string ChannelId { get; }
string ChannelType { get; }
string DisplayName { get; }
bool IsConnected { get; }
event ChannelMessageReceivedEventHandler? MessageReceived;
event EventHandler<ChannelConnectionEventArgs>? ConnectionStateChanged;
Task SendMessageAsync(ChannelMessage message, CancellationToken ct = default);
Task SendStreamingMessageAsync(IAsyncEnumerable<ChatStreamUpdate> messageStream, CancellationToken ct = default);
}
IChannelManager
Coordinates registered channels and handles message routing.
public interface IChannelManager
{
void RegisterChannel(IChannel channel);
void UnregisterChannel(string channelId);
IChannel? GetChannel(string channelId);
IReadOnlyList<IChannel> GetAllChannels();
event EventHandler<ChannelMessageReceivedEventArgs>? MessageRouted;
}
Supporting Types
- ChannelType: A static class providing constants like
cliandtelegram. - ChannelMessageReceivedEventHandler: Delegate for handling incoming messages.
Task ChannelMessageReceivedEventHandler(object? sender, ChannelMessageReceivedEventArgs e) - ChannelMessageReceivedEventArgs: Contains the
Channel,Message, and anIsHandledflag. - ChannelConnectionEventArgs: Contains the
ChannelandIsConnectedstatus.
Luna.Channels
This project contains the concrete management logic and specific channel implementations.
ChannelManager
The ChannelManager implements IChannelManager and acts as the central hub for message traffic. It injects ISessionManager from Core and ILogger<ChannelManager>.
When a channel is registered via RegisterChannel, the manager subscribes to its MessageReceived event. The OnMessageReceivedAsync handler performs the following:
- Calls
sessionManager.RouteMessagesAsync(message.Content, message.ConversationId, channel.ChannelId). - Pipes the resulting
IAsyncEnumerable<ChatStreamUpdate>back to the channel viachannel.SendStreamingMessageAsync.
Channel Implementations
- CLI Channel — SignalR-based adapter for the command-line interface.
- Telegram Channel — Telegram Bot API adapter with message splitting and streaming.
- Web Interface Channel — Browser-based chat UI (not yet implemented).
Routing Flow
Channel.MessageReceivedevent triggers.ChannelManagercatches the event and identifies the sender.- Manager calls
SessionManager.RouteMessagesAsync. SessionManagerreturns anIAsyncEnumerable<ChatStreamUpdate>.ChannelManagerpasses this stream toChannel.SendStreamingMessageAsync.- The channel implementation handles the physical transport of the stream.
Architecture Decisions
- Separation of Concerns:
ChannelManagerhandles transport and routing.SessionManagerhandles AI context and conversation logic. - Conversation Scoping: Sessions are scoped to conversations, not specific channels. This allows for potential cross-channel persistence.
- Unified DTOs: All message data uses the
ChannelMessageDTO fromLuna.Shared. - Registration: Channels are registered via the
AddChannels()DI extension method, following the Configuration patterns. - Options Pattern: Implementations use
IOptions<TOptions>(e.g.,TelegramOptions) for configuration.
Dependencies
Project References
Luna.Channels.AbstractionsLuna.ConfigurationLuna.Core.AbstractionsLuna.Shared
NuGet Packages
Telegram.BotMicrosoft.Extensions.Hosting.AbstractionsMicrosoft.Extensions.Logging.Abstractions
Adding a New Channel
To implement a new channel:
- Create a class implementing
IChannel. - Ensure it handles both
SendMessageAsyncandSendStreamingMessageAsync. - Raise
MessageReceivedwhen the external platform sends a message. - Register the channel with
IChannelManagerduring startup or via a background adapter (likeTelegramAdapter).