Files
aethera/backend/internal/api/handlers.go
2026-01-17 10:07:21 -05:00

550 lines
16 KiB
Go

package api
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"github.com/google/uuid"
"github.com/openai/openai-go/v3"
"github.com/sirupsen/logrus"
"reichard.io/aethera/internal/client"
"reichard.io/aethera/internal/store"
"reichard.io/aethera/pkg/slices"
)
type API struct {
logger *logrus.Entry
store store.Store
client *client.Client
dataDir string
}
func New(s store.Store, dataDir string, logger *logrus.Logger) *API {
return &API{
store: s,
dataDir: dataDir,
logger: logger.WithField("service", "api"),
}
}
func (a *API) GetSettings(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "GetSettingsHandler")
settings, err := a.store.GetSettings()
if err != nil {
log.WithError(err).Error("failed to retrieve settings")
http.Error(w, "Failed to retrieve application settings", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(settings); err != nil {
log.WithError(err).Error("failed to encode application settings response")
http.Error(w, "Failed to encode application settings response", http.StatusInternalServerError)
return
}
}
func (a *API) PostSettings(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "PostSettingsHandler")
var newSettings store.Settings
if err := json.NewDecoder(r.Body).Decode(&newSettings); err != nil {
log.WithError(err).Error("invalid JSON in settings update request")
http.Error(w, "Invalid request body format for settings", http.StatusBadRequest)
return
}
if apiEndpoint := newSettings.APIEndpoint; apiEndpoint != "" {
baseURL, err := url.Parse(apiEndpoint)
if err != nil {
errMsg := fmt.Sprintf("Invalid API Endpoint URL: %q", baseURL)
log.WithError(err).Error(errMsg)
http.Error(w, errMsg, http.StatusBadRequest)
return
}
testClient := client.NewClient(baseURL)
if _, err := testClient.GetModels(r.Context()); err != nil {
log.WithError(err).Error("failed to access configured API endpoint")
http.Error(w, "API endpoint inaccessible", http.StatusBadRequest)
return
}
a.client = nil
}
if err := a.store.SaveSettings(&newSettings); err != nil {
log.WithError(err).Error("failed to save settings")
http.Error(w, "Failed to save application settings", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (a *API) GetModels(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "GetModelsHandler")
client, err := a.getClient()
if err != nil {
log.WithError(err).Error("failed to initialize API client")
http.Error(w, "Failed to initialize API client", http.StatusInternalServerError)
return
}
models, err := client.GetModels(r.Context())
if err != nil {
log.WithError(err).Error("failed to retrieve available models")
http.Error(w, "Failed to retrieve available models from API", http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(models); err != nil {
log.WithError(err).Error("failed to encode available models response")
http.Error(w, "Failed to encode available models response", http.StatusInternalServerError)
return
}
}
func (a *API) GetImages(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "GetImagesHandler")
files, err := os.ReadDir(path.Join(a.dataDir, "generated/images"))
if err != nil {
log.WithError(err).Error("failed to read images directory")
http.Error(w, "Failed to read images directory", http.StatusInternalServerError)
return
}
imageList := make([]ImageRecord, 0)
for _, file := range files {
if !file.IsDir() && strings.HasSuffix(strings.ToLower(file.Name()), ".png") {
info, err := file.Info()
if err != nil {
continue
}
imageList = append(imageList, ImageRecord{
Name: file.Name(),
Path: "/generated/images/" + file.Name(),
Size: info.Size(),
Date: info.ModTime().Format(time.RFC3339),
})
}
}
sort.Slice(imageList, func(i, j int) bool {
return imageList[i].Date > imageList[j].Date
})
if err := json.NewEncoder(w).Encode(imageList); err != nil {
log.WithError(err).Error("failed to encode image list metadata response")
http.Error(w, "Failed to encode image list metadata response", http.StatusInternalServerError)
return
}
}
func (a *API) PostImage(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "PostImageHandler")
client, err := a.getClient()
if err != nil {
log.WithError(err).Error("failed to initialize API client")
http.Error(w, "Failed to initialize API client", http.StatusInternalServerError)
return
}
var genReq GenerateImageRequest
if err := json.NewDecoder(r.Body).Decode(&genReq); err != nil {
log.WithError(err).Error("invalid JSON in image generation request")
http.Error(w, "Invalid request body format for image generation", http.StatusBadRequest)
return
}
if err := genReq.Validate(); err != nil {
log.WithError(err).Error("invalid request")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Edit vs Generate Request
var images []openai.Image
var reqErr error
if genReq.isEdit() {
editParams, err := genReq.getEditParams()
if err != nil {
log.WithError(err).Error("invalid image edit parameters")
http.Error(w, "Invalid image edit parameters", http.StatusBadRequest)
return
}
images, reqErr = client.EditImage(r.Context(), *editParams)
} else {
genParams, err := genReq.getGenerateParams()
if err != nil {
log.WithError(err).Error("invalid image generation parameters")
http.Error(w, "Invalid image generation parameters", http.StatusBadRequest)
return
}
images, reqErr = client.GenerateImages(r.Context(), *genParams)
}
// Check Error
if reqErr != nil {
log.WithError(reqErr).Error("failed to generate images")
http.Error(w, "Failed to generate images via API", http.StatusInternalServerError)
return
}
// Normalize Responses
imageRecords := make([]ImageRecord, 0)
for i, img := range images {
if img.B64JSON == "" {
log.Warnf("empty image data at index %d, skipping", i)
continue
}
// Decode Image
imgBytes, err := base64.StdEncoding.DecodeString(img.B64JSON)
if err != nil {
log.WithError(err).WithField("index", i).Error("failed to decode image")
continue
}
// Save Image
filename := fmt.Sprintf("image_%d_%d.png", time.Now().Unix(), i)
filePath := path.Join(a.dataDir, "generated/images", filename)
if err := os.WriteFile(filePath, imgBytes, 0644); err != nil {
log.WithError(err).WithField("file", filePath).Error("failed to save generated image")
continue
}
// Record Image
imageRecords = append(imageRecords, ImageRecord{
Name: filename,
Path: fmt.Sprintf("/generated/images/%s", filename),
Date: time.Now().Format(time.RFC3339),
Size: int64(len(imgBytes)),
})
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(imageRecords); err != nil {
log.WithError(err).Error("failed to encode generated images response")
http.Error(w, "Failed to encode generated images response", http.StatusInternalServerError)
return
}
}
func (a *API) DeleteImage(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "DeleteImageHandler")
filename := r.PathValue("filename")
if filename == "" {
log.Error("missing filename parameter")
http.Error(w, "Filename parameter is required", http.StatusBadRequest)
return
}
// Delete Image
imgDir := path.Join(a.dataDir, "generated/images")
safePath := path.Join(imgDir, filepath.Base(filename))
if err := os.Remove(safePath); err != nil {
log.WithError(err).WithField("file", safePath).Error("failed to delete image file")
http.Error(w, "Failed to delete image file", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (a *API) GetChats(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "GetChatsHandler")
chats, err := a.store.ListChats()
if err != nil {
log.WithError(err).Error("failed to list chats")
http.Error(w, "Failed to retrieve chats", http.StatusInternalServerError)
return
}
sort.Slice(chats, func(i, j int) bool {
iLast, iFound := slices.Last(chats[i].Messages)
if !iFound {
return false
}
jLast, jFound := slices.Last(chats[j].Messages)
if !jFound {
return true
}
return iLast.CreatedAt.After(jLast.CreatedAt)
})
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(ChatListResponse{Chats: slices.Map(chats, toChatNoMessages)}); err != nil {
log.WithError(err).Error("failed to encode chats list response")
http.Error(w, "Failed to encode chats list response", http.StatusInternalServerError)
return
}
}
func (a *API) PostChat(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "PostChatHandler")
// Decode Request
var genReq GenerateTextRequest
if err := json.NewDecoder(r.Body).Decode(&genReq); err != nil {
log.WithError(err).Error("invalid JSON in text generation request")
http.Error(w, "Invalid request body format for new chat", http.StatusBadRequest)
return
}
if err := genReq.Validate(); err != nil {
log.WithError(err).Error("invalid request")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Create Chat
var chat store.Chat
if err := a.store.SaveChat(&chat); err != nil {
log.WithError(err).Error("failed to create new chat")
http.Error(w, "Failed to create new chat", http.StatusInternalServerError)
return
}
// Send Message
if err := a.sendMessage(r.Context(), w, chat.ID, genReq.Model, genReq.Prompt); err != nil {
log.WithError(err).WithField("chat_id", chat.ID).Error("failed to send message")
http.Error(w, "Failed to send message", http.StatusInternalServerError)
}
}
func (a *API) DeleteChat(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "DeleteChatHandler")
chatIDStr := r.PathValue("chatId")
if chatIDStr == "" {
log.Error("missing chat ID parameter")
http.Error(w, "Chat ID is required", http.StatusBadRequest)
return
}
chatID, err := uuid.Parse(chatIDStr)
if err != nil {
log.WithError(err).Error("invalid chat ID format")
http.Error(w, "Invalid chat ID format", http.StatusBadRequest)
return
}
// Delete Chat
if err := a.store.DeleteChat(chatID); err != nil {
log.WithError(err).WithField("chat_id", chatID).Error("failed to delete chat")
if errors.Is(err, store.ErrChatNotFound) {
http.Error(w, "Chat not found", http.StatusNotFound)
} else {
http.Error(w, "Failed to delete chat", http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusNoContent)
}
func (a *API) GetChat(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "GetChatHandler")
chatID := r.PathValue("chatId")
if chatID == "" {
log.Error("missing chat ID parameter")
http.Error(w, "Chat ID is required", http.StatusBadRequest)
return
}
parsedChatID, err := uuid.Parse(chatID)
if err != nil {
log.WithError(err).Error("invalid chat ID format")
http.Error(w, "Invalid chat ID format", http.StatusBadRequest)
return
}
chat, err := a.store.GetChat(parsedChatID)
if err != nil {
log.WithError(err).WithField("chat_id", parsedChatID).Error("failed to get chat")
http.Error(w, "Failed to get chat", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(toChat(chat)); err != nil {
log.WithError(err).Error("failed to encode chat messages response")
http.Error(w, "Failed to encode chat messages response", http.StatusInternalServerError)
return
}
}
func (a *API) PostChatMessage(w http.ResponseWriter, r *http.Request) {
log := a.logger.WithField("handler", "PostChatMessageHandler")
rawChatID := r.PathValue("chatId")
if rawChatID == "" {
log.Error("missing chat ID parameter")
http.Error(w, "Chat ID is required", http.StatusBadRequest)
return
}
chatID, err := uuid.Parse(rawChatID)
if err != nil {
log.WithError(err).Error("invalid chat ID format")
http.Error(w, "Invalid chat ID format", http.StatusBadRequest)
return
}
var genReq GenerateTextRequest
if err := json.NewDecoder(r.Body).Decode(&genReq); err != nil {
log.WithError(err).Error("invalid JSON in text generation request")
http.Error(w, "Invalid request body format for text generation", http.StatusBadRequest)
return
}
if err := genReq.Validate(); err != nil {
log.WithError(err).Error("invalid request")
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := a.sendMessage(r.Context(), w, chatID, genReq.Model, genReq.Prompt); err != nil {
log.WithError(err).WithField("chat_id", chatID).Error("failed to send message")
http.Error(w, "Failed to send message", http.StatusInternalServerError)
}
}
func (a *API) getClient() (*client.Client, error) {
if a.client != nil {
return a.client, nil
}
// Get Settings & Validate Endpoint
settings, err := a.store.GetSettings()
if err != nil {
return nil, fmt.Errorf("failed to retrieve application settings: %w", err)
} else if settings.APIEndpoint == "" {
return nil, errors.New("no API endpoint configured in settings")
}
baseURL, err := url.Parse(settings.APIEndpoint)
if err != nil {
return nil, fmt.Errorf("invalid API endpoint URL: %w", err)
}
a.client = client.NewClient(baseURL)
return a.client, nil
}
func (a *API) sendMessage(ctx context.Context, w http.ResponseWriter, chatID uuid.UUID, chatModel, userMessage string) error {
apiClient, err := a.getClient()
if err != nil {
return fmt.Errorf("failed to get client: %w", err)
}
// Detach Request Context
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), time.Minute*5)
defer cancel()
// Create User Message
userMsg := &store.Message{ChatID: chatID, Role: "user", Content: userMessage}
if err := a.store.SaveChatMessage(userMsg); err != nil {
return fmt.Errorf("failed to add user message to chat: %w", err)
}
// Add Assistant Response - TODO: Ensure InProgress Flag?
assistantMsg := &store.Message{ChatID: chatID, Role: "assistant"}
if err := a.store.SaveChatMessage(assistantMsg); err != nil {
return fmt.Errorf("failed to add assistant message to chat: %w", err)
}
// Get Chat
chat, err := a.store.GetChat(chatID)
if err != nil {
return fmt.Errorf("failed to get chat: %w", err)
}
// Set Headers
w.Header().Set("Content-Type", "application/x-ndjson")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
// Create Flush Writer
flushWriter := newFlushWriter(w)
// Send Initial Chunk - User Message & Chat
if err := json.NewEncoder(flushWriter).Encode(&MessageChunk{
Chat: toChatNoMessages(chat),
UserMessage: userMsg,
}); err != nil {
return fmt.Errorf("failed to send initial chunk: %w", err)
}
// Send Message
if _, err := apiClient.SendMessage(ctx, chat.Messages, chatModel, func(m *client.MessageChunk) error {
var apiMsgChunk MessageChunk
if m.Stats != nil {
assistantMsg.Stats = m.Stats
}
if m.Message != nil {
assistantMsg.Content += *m.Message
apiMsgChunk.AssistantMessage = assistantMsg
}
if m.Thinking != nil {
assistantMsg.Thinking += *m.Thinking
apiMsgChunk.AssistantMessage = assistantMsg
}
// Send Progress Chunk
if err := json.NewEncoder(flushWriter).Encode(apiMsgChunk); err != nil {
return fmt.Errorf("failed to send progress chunk: %w", err)
}
return nil
}); err != nil {
return fmt.Errorf("failed to generate text stream: %w", err)
}
// Summarize & Update Chat Title
if chat.Title == "" {
chat.Title, err = apiClient.CreateTitle(ctx, chat.Messages[0].Content, chatModel)
if err != nil {
a.logger.WithError(err).WithField("chat_id", chat.ID).Error("failed to create chat title")
} else if err := a.store.SaveChat(chat); err != nil {
a.logger.WithError(err).WithField("chat_id", chat.ID).Error("failed to update chat")
}
}
// Update Assistant Message
if err := a.store.SaveChatMessage(assistantMsg); err != nil {
return fmt.Errorf("failed to save assistant message to chat: %w", err)
}
// Send Final Chunk
if err := json.NewEncoder(flushWriter).Encode(&MessageChunk{
Chat: toChatNoMessages(chat),
AssistantMessage: assistantMsg,
}); err != nil {
return fmt.Errorf("failed to send final chunk: %w", err)
}
return nil
}