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.
5.3 KiB
Core Module
The Core module serves as the central orchestration engine for Luna, managing sessions, message routing, context compaction, and tool integration. It is split into Luna.Core.Abstractions for interfaces and Luna.Core for the primary implementation.
Luna.Core.Abstractions
This project defines the contracts used by the Core and other modules.
ISessionManager
The primary interface for managing chat sessions and routing messages.
public interface ISessionManager
{
IAsyncEnumerable<ChatStreamUpdate> RouteMessagesAsync(string content, string conversationId, string connectionId, CancellationToken ct);
Task ClientDisconnectedAsync(string connectionId);
}
IChatHubContext
Interface for sending responses back to clients via SignalR.
public interface IChatHubContext
{
Task SendResponseAsync(string connectionId, ChannelMessage message, CancellationToken ct);
Task SendStreamingResponseAsync(string connectionId, IAsyncEnumerable<ChatStreamUpdate> messageStream, CancellationToken ct);
}
Luna.Core
The main implementation project containing the session logic and SignalR hubs.
Session
The Session class manages the state of an active conversation.
public class Session
{
public string SessionId { get; set; }
public List<ChatMessage> Messages { get; } = new();
public int TokenAmount => TokenEstimator.EstimateTokens(Messages);
}
SessionManager
Implements ISessionManager. It coordinates between agents, memory, and the core LLM processing.
- Injected Services:
[FromKeyedServices("Core")] IAgent coreAgent[FromKeyedServices("Librarian")] IAgent librarianAgentIOptions<SessionOptions> optionsIChatStreamUpdateBuilder streamUpdateBuilderIMemoryStore memoryStore
- State Management: Maintains an in-memory
Dictionary<string, Session>for sessions and aConcurrentDictionary<string, string>for mapping connection IDs to session IDs.
Message Routing Flow
- Session Initialization: Creates a new session if one does not exist and loads existing Memory via
memoryStore.GetMemoriesAsync(). - Token Check: Evaluates current token usage against
MaxContextTokens * ContextTokenThreshold. - Compaction: If the threshold is exceeded, triggers
CompactSessionAsync. - Processing: Appends the user message, streams the response from the
coreAgentviaProcessStreamingAsync, and converts AI content toChatStreamUpdateusing theIChatStreamUpdateBuilder. - Persistence: Appends the assistant response to the session history.
Compaction Strategy
When a session exceeds the token threshold, the system:
- Retains the last
Nmessages (defined bySessionOptions.RetainedMessagesAfterCompacting). - Sends all older messages to the
librarianAgentfor summarization. - Wraps the resulting summary in
<---- MEMORY BEGIN ---->and<---- MEMORY END ---->markers and inserts it at the beginning of the message list.
Disconnect Flow
When ClientDisconnectedAsync is called:
- The conversation log is saved to the
IMemoryStore. - The session and connection mappings are cleaned up.
Hubs and Contexts
ChatHub
A SignalR hub that serves as the entry point for real-time communication.
- OnConnected: Creates a
CliChanneland registers it with theIChannelManager. - OnDisconnected: Unregisters the channel.
- OnMessageReceived: Delegates message handling to the
CliChannel.RaiseMessageReceived.
ChatHubContext
Implements IChatHubContext using IHubContext<ChatHub>. It handles the actual transmission of data to SignalR clients, supporting both discrete and streaming responses.
Tools System
IToolbox
Provides a mechanism for discovering and exposing tools to the AI.
- Implementation: Uses reflection to find methods decorated with
[ToolAttribute]. - Function Creation: Generates
AIToolinstances usingAIFunctionFactory.Create.
IToolsProvider
Exposes the collection of discovered tools.
public interface IToolsProvider
{
IEnumerable<AITool> GetTools();
}
Token Estimation
The TokenEstimator provides a heuristic-based token count:
- Calculation: Number of characters divided by 4.
Architecture Flow
The following flow describes how a message moves through the Core module:
Channel.MessageReceived → ChannelManager → SessionManager.RouteMessagesAsync
→ Token Check → Compaction if needed (LibrarianAgent)
→ CoreAgent.ProcessStreamingAsync → IChatClient Streaming
→ ChatStreamUpdateBuilder → IAsyncEnumerable<ChatStreamUpdate>
→ Channel.SendStreamingMessageAsync → Client
Cross-References
- Channels: Management of communication pathways.
- Memory: Long-term and short-term state persistence.
- Configuration:
SessionOptionsand system settings. - Skills: Integration of specialized capabilities.
- Scheduler: Task timing and execution.
Dependencies
Project References
- Luna.Agents.Abstractions
- Luna.Channels
- Luna.Channels.Abstractions
- Configuration (Luna.Configuration)
- Luna.Core.Abstractions
- Memory (Luna.Memory)
- Luna.Providers
- Luna.Providers.Abstractions
- Luna.Shared
NuGet Packages
- Microsoft.AspNetCore.OpenApi
- Microsoft.Extensions.AI