Documentation
¶
Index ¶
- type LLM
- type MCPClient
- type Main
- func (m Main) HandleChats(w http.ResponseWriter, r *http.Request)
- func (m Main) HandleHome(w http.ResponseWriter, r *http.Request)
- func (m Main) HandleRefreshTitle(w http.ResponseWriter, r *http.Request)
- func (m Main) HandleSSE(w http.ResponseWriter, r *http.Request)
- func (m Main) Shutdown(ctx context.Context) error
- type Store
- type TitleGenerator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LLM ¶
type LLM interface {
Chat(ctx context.Context, messages []models.Message, tools []mcp.Tool) iter.Seq2[models.Content, error]
}
LLM represents a large language model interface that provides chat functionality. It accepts a context and a sequence of messages, returning an iterator that yields response chunks and potential errors.
type MCPClient ¶ added in v0.2.0
type MCPClient interface { ServerInfo() mcp.Info ToolServerSupported() bool ResourceServerSupported() bool PromptServerSupported() bool ListTools(ctx context.Context, params mcp.ListToolsParams) (mcp.ListToolsResult, error) ListResources(ctx context.Context, params mcp.ListResourcesParams) (mcp.ListResourcesResult, error) ReadResource(ctx context.Context, params mcp.ReadResourceParams) (mcp.ReadResourceResult, error) ListPrompts(ctx context.Context, params mcp.ListPromptsParams) (mcp.ListPromptResult, error) GetPrompt(ctx context.Context, params mcp.GetPromptParams) (mcp.GetPromptResult, error) CallTool(ctx context.Context, params mcp.CallToolParams) (mcp.CallToolResult, error) }
MCPClient defines the interface for interacting with an MCP server. This allows for mocking in tests.
type Main ¶
type Main struct {
// contains filtered or unexported fields
}
Main handles the core functionality of the chat application, managing server-sent events, HTML templates, and interactions between the LLM and Store components.
func NewMain ¶
func NewMain( llm LLM, titleGen TitleGenerator, store Store, mcpClients []MCPClient, logger *slog.Logger, ) (Main, error)
NewMain creates a new Main instance with the provided LLM and Store implementations. It initializes the SSE server with default configurations and parses the required HTML templates from the embedded filesystem. The SSE server is configured to handle both default events and chat-specific topics.
func (Main) HandleChats ¶
func (m Main) HandleChats(w http.ResponseWriter, r *http.Request)
HandleChats processes chat interactions through HTTP POST requests, managing both new chat creation and message handling. It supports three input methods: 1. Regular messages via the "message" form field 2. Predefined prompts via "prompt_name" and "prompt_args" form fields 3. Attached resources via the "attached_resources" JSON array of resource URIs
When resources are attached, they're processed and appended to the latest user message. Resources are retrieved from registered MCP clients based on their URIs.
The handler expects an optional "chat_id" field. If no chat_id is provided, it creates a new chat session. For new chats, it asynchronously generates a title based on the first message or prompt.
The function handles different rendering strategies based on whether it's a new chat (complete chatbox template) or an existing chat (individual message templates). For all chats, it adds messages to the database and initiates asynchronous AI response generation that will be streamed via Server-Sent Events (SSE).
The function returns appropriate HTTP error responses for invalid methods, missing required fields, resource processing failures, or internal processing errors. For successful requests, it renders the appropriate templates with messages marked with correct streaming states.
func (Main) HandleHome ¶
func (m Main) HandleHome(w http.ResponseWriter, r *http.Request)
HandleHome renders the home page template with chat and message data. It displays a list of available chats and, if a chat_id query parameter is provided, shows the messages for the selected chat. The handler retrieves chat and message data from the store and prepares it for template rendering.
func (Main) HandleRefreshTitle ¶ added in v0.2.0
func (m Main) HandleRefreshTitle(w http.ResponseWriter, r *http.Request)
HandleRefreshTitle handles requests to regenerate a chat title. It accepts POST requests with a chat_id parameter, retrieves the first user message from the chat history, and uses the title generator to create a new title. The handler updates the chat title in the database and returns the new title to be displayed.
The function expects a "chat_id" form field identifying which chat's title should be refreshed. After updating the database, it asynchronously notifies all connected clients through Server-Sent Events (SSE) to maintain UI consistency across sessions while immediately returning the new title text to the requesting client.
The function returns appropriate HTTP error responses for invalid methods, missing required fields, or when no messages are found for title generation. On success, it returns just the title text to be inserted into the targeted span element via HTMX.
func (Main) HandleSSE ¶
func (m Main) HandleSSE(w http.ResponseWriter, r *http.Request)
HandleSSE serves Server-Sent Events (SSE) requests by delegating to the underlying SSE server. This endpoint enables real-time updates for the client.
type Store ¶
type Store interface { Chats(ctx context.Context) ([]models.Chat, error) AddChat(ctx context.Context, chat models.Chat) (string, error) UpdateChat(ctx context.Context, chat models.Chat) error Messages(ctx context.Context, chatID string) ([]models.Message, error) AddMessage(ctx context.Context, chatID string, message models.Message) (string, error) UpdateMessage(ctx context.Context, chatID string, message models.Message) error }
Store defines the interface for managing chat and message persistence. It provides methods for creating, reading, and updating chats and their associated messages. The interface supports both atomic operations and bulk retrieval of chats and messages.