initial commit
This commit is contained in:
28
backend/internal/api/convert.go
Normal file
28
backend/internal/api/convert.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"reichard.io/aethera/internal/store"
|
||||
"reichard.io/aethera/pkg/slices"
|
||||
)
|
||||
|
||||
func toChat(c *store.Chat) *Chat {
|
||||
chat := &Chat{
|
||||
ID: c.ID,
|
||||
CreatedAt: c.CreatedAt,
|
||||
Title: c.Title,
|
||||
MessageCount: len(c.Messages),
|
||||
Messages: c.Messages,
|
||||
}
|
||||
|
||||
if firstMessage, found := slices.First(c.Messages); found {
|
||||
chat.InitialMessage = firstMessage.Content
|
||||
}
|
||||
|
||||
return chat
|
||||
}
|
||||
|
||||
func toChatNoMessages(c *store.Chat) *Chat {
|
||||
chat := toChat(c)
|
||||
chat.Messages = []*store.Message{}
|
||||
return chat
|
||||
}
|
||||
27
backend/internal/api/flush_writer.go
Normal file
27
backend/internal/api/flush_writer.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type flushWriter struct {
|
||||
w http.ResponseWriter
|
||||
f http.Flusher
|
||||
}
|
||||
|
||||
func (fw *flushWriter) Write(p []byte) (n int, err error) {
|
||||
// Write Data
|
||||
n, err = fw.w.Write(p)
|
||||
if err == nil && fw.f != nil {
|
||||
fw.f.Flush()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func newFlushWriter(w http.ResponseWriter) *flushWriter {
|
||||
flusher, _ := w.(http.Flusher)
|
||||
return &flushWriter{
|
||||
w: w,
|
||||
f: flusher,
|
||||
}
|
||||
}
|
||||
549
backend/internal/api/handlers.go
Normal file
549
backend/internal/api/handlers.go
Normal file
@@ -0,0 +1,549 @@
|
||||
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
|
||||
}
|
||||
163
backend/internal/api/types.go
Normal file
163
backend/internal/api/types.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/openai/openai-go/v3"
|
||||
"github.com/openai/openai-go/v3/packages/param"
|
||||
"reichard.io/aethera/internal/store"
|
||||
)
|
||||
|
||||
type ChatListResponse struct {
|
||||
Chats []*Chat `json:"chats"`
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Title string `json:"title"`
|
||||
InitialMessage string `json:"initial_message"`
|
||||
MessageCount int `json:"message_count"`
|
||||
Messages []*store.Message `json:"messages"`
|
||||
}
|
||||
|
||||
type MessageChunk struct {
|
||||
Chat *Chat `json:"chat,omitempty"`
|
||||
UserMessage *store.Message `json:"user_message,omitempty"`
|
||||
AssistantMessage *store.Message `json:"assistant_message,omitempty"`
|
||||
}
|
||||
|
||||
type GenerateImageRequest struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
N int64 `json:"n"`
|
||||
Size string `json:"size"`
|
||||
|
||||
Mask *string `json:"mask"` // Data URL (e.g. "data:image/png;base64,...")
|
||||
Image *string `json:"image"` // Data URL (e.g. "data:image/png;base64,...")
|
||||
|
||||
GenerateImageRequestExtraArgs
|
||||
}
|
||||
|
||||
func (r *GenerateImageRequest) Validate() error {
|
||||
if r.Model == "" {
|
||||
return errors.New("model is required")
|
||||
}
|
||||
if r.Prompt == "" {
|
||||
return errors.New("prompt is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type GenerateImageRequestExtraArgs struct {
|
||||
Seed *int32 `json:"seed,omitempty"`
|
||||
}
|
||||
|
||||
type ImageRecord struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Size int64 `json:"size"`
|
||||
Date string `json:"date"`
|
||||
}
|
||||
|
||||
type GenerateTextRequest struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
}
|
||||
|
||||
func (r *GenerateTextRequest) Validate() error {
|
||||
if r.Model == "" {
|
||||
return errors.New("model is required")
|
||||
}
|
||||
if r.Prompt == "" {
|
||||
return errors.New("prompt is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *GenerateImageRequest) getPrompt() string {
|
||||
prompt := r.Prompt
|
||||
d, _ := json.Marshal(r.GenerateImageRequestExtraArgs)
|
||||
if extraArgs := string(d); extraArgs != "" {
|
||||
prompt += fmt.Sprintf(" <sd_cpp_extra_args>%s</sd_cpp_extra_args>", extraArgs)
|
||||
}
|
||||
return strings.TrimSpace(prompt)
|
||||
}
|
||||
|
||||
func (r *GenerateImageRequest) isEdit() bool {
|
||||
return r.Image != nil
|
||||
}
|
||||
|
||||
func (r *GenerateImageRequest) getEditParams() (*openai.ImageEditParams, error) {
|
||||
if !r.isEdit() {
|
||||
return nil, errors.New("not an edit request")
|
||||
}
|
||||
strippedImage, err := stripDataURLPrefix(*r.Image)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to strip data url prefix for image: %w", err)
|
||||
}
|
||||
imageBytes, err := base64.StdEncoding.DecodeString(strippedImage)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode image: %w", err)
|
||||
}
|
||||
iFile := openai.File(bytes.NewReader(imageBytes), "main.png", "image/png")
|
||||
|
||||
editReq := &openai.ImageEditParams{
|
||||
Model: r.Model,
|
||||
Prompt: r.getPrompt(),
|
||||
Size: openai.ImageEditParamsSize(r.Size),
|
||||
N: param.NewOpt(r.N),
|
||||
OutputFormat: openai.ImageEditParamsOutputFormatPNG,
|
||||
Image: openai.ImageEditParamsImageUnion{OfFileArray: []io.Reader{iFile}},
|
||||
}
|
||||
if r.Mask == nil {
|
||||
return editReq, nil
|
||||
}
|
||||
|
||||
strippedMask, err := stripDataURLPrefix(*r.Mask)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to strip data url prefix for mask: %w", err)
|
||||
}
|
||||
maskBytes, err := base64.StdEncoding.DecodeString(strippedMask)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode image: %w", err)
|
||||
}
|
||||
|
||||
editReq.Mask = openai.File(bytes.NewReader(maskBytes), "mask.png", "image/png")
|
||||
return editReq, nil
|
||||
}
|
||||
|
||||
func (r *GenerateImageRequest) getGenerateParams() (*openai.ImageGenerateParams, error) {
|
||||
if r.isEdit() {
|
||||
return nil, errors.New("not a generate request")
|
||||
}
|
||||
return &openai.ImageGenerateParams{
|
||||
Model: r.Model,
|
||||
Prompt: r.getPrompt(),
|
||||
Size: openai.ImageGenerateParamsSize(r.Size),
|
||||
N: param.NewOpt(r.N),
|
||||
OutputFormat: openai.ImageGenerateParamsOutputFormatPNG,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func stripDataURLPrefix(dataURL string) (string, error) {
|
||||
if !strings.Contains(dataURL, ",") {
|
||||
return dataURL, nil
|
||||
}
|
||||
parts := strings.SplitN(dataURL, ",", 2)
|
||||
prefix := parts[0]
|
||||
switch prefix {
|
||||
case "data:image/png;base64", "data:image/jpeg;base64":
|
||||
return parts[1], nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported image type: %s", prefix)
|
||||
}
|
||||
}
|
||||
290
backend/internal/client/client.go
Normal file
290
backend/internal/client/client.go
Normal file
@@ -0,0 +1,290 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/openai/openai-go/v3"
|
||||
"github.com/openai/openai-go/v3/option"
|
||||
"github.com/openai/openai-go/v3/packages/respjson"
|
||||
"github.com/openai/openai-go/v3/shared"
|
||||
"reichard.io/aethera/internal/store"
|
||||
"reichard.io/aethera/internal/types"
|
||||
"reichard.io/aethera/pkg/ptr"
|
||||
"reichard.io/aethera/pkg/slices"
|
||||
)
|
||||
|
||||
type StreamCallback func(*MessageChunk) error
|
||||
|
||||
type Client struct {
|
||||
oaiClient *openai.Client
|
||||
}
|
||||
|
||||
func (c *Client) GetModels(ctx context.Context) ([]Model, error) {
|
||||
// Get Models
|
||||
currPage, err := c.oaiClient.Models.List(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allData := currPage.Data
|
||||
|
||||
// Pagination
|
||||
for {
|
||||
currPage, err = currPage.GetNextPage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if currPage == nil {
|
||||
break
|
||||
}
|
||||
allData = append(allData, currPage.Data...)
|
||||
}
|
||||
|
||||
// Convert
|
||||
return slices.Map(allData, fromOpenAIModel), nil
|
||||
}
|
||||
|
||||
func (c *Client) GenerateImages(ctx context.Context, body openai.ImageGenerateParams) ([]openai.Image, error) {
|
||||
// Generate Images
|
||||
resp, err := c.oaiClient.Images.Generate(ctx, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
func (c *Client) EditImage(ctx context.Context, body openai.ImageEditParams) ([]openai.Image, error) {
|
||||
// Edit Image
|
||||
resp, err := c.oaiClient.Images.Edit(ctx, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.Data, nil
|
||||
}
|
||||
|
||||
func (c *Client) SendMessage(ctx context.Context, chatMessages []*store.Message, model string, cb StreamCallback) (string, error) {
|
||||
// Ensure Callback
|
||||
if cb == nil {
|
||||
cb = func(mc *MessageChunk) error { return nil }
|
||||
}
|
||||
|
||||
// Map Messages
|
||||
messages := slices.Map(chatMessages, func(m *store.Message) openai.ChatCompletionMessageParamUnion {
|
||||
if m.Role == "user" {
|
||||
return openai.UserMessage(m.Content)
|
||||
}
|
||||
return openai.AssistantMessage(m.Content)
|
||||
})
|
||||
|
||||
// Create Request
|
||||
chatReq := openai.ChatCompletionNewParams{
|
||||
Model: model,
|
||||
Messages: messages,
|
||||
StreamOptions: openai.ChatCompletionStreamOptionsParam{
|
||||
IncludeUsage: openai.Bool(true),
|
||||
},
|
||||
}
|
||||
chatReq.SetExtraFields(map[string]any{
|
||||
"timings_per_token": true, // Llama.cpp
|
||||
})
|
||||
|
||||
// Perform Request & Allocate Stats
|
||||
msgStats := types.MessageStats{StartTime: time.Now()}
|
||||
stream := c.oaiClient.Chat.Completions.NewStreaming(ctx, chatReq)
|
||||
|
||||
// Iterate Stream
|
||||
var respContent string
|
||||
for stream.Next() {
|
||||
// Check Context
|
||||
if ctx.Err() != nil {
|
||||
return respContent, ctx.Err()
|
||||
}
|
||||
|
||||
// Load Chunk
|
||||
chunk := stream.Current()
|
||||
msgChunk := &MessageChunk{Stats: &msgStats}
|
||||
|
||||
// Populate Timings
|
||||
sendUpdate := populateLlamaCPPTimings(&msgStats, chunk.JSON.ExtraFields)
|
||||
sendUpdate = populateUsageTimings(&msgStats, chunk.Usage) || sendUpdate
|
||||
|
||||
if len(chunk.Choices) > 0 {
|
||||
delta := chunk.Choices[0].Delta
|
||||
|
||||
// Check Thinking
|
||||
if thinkingField, found := delta.JSON.ExtraFields["reasoning_content"]; found {
|
||||
var thinkingContent string
|
||||
if err := json.Unmarshal([]byte(thinkingField.Raw()), &thinkingContent); err != nil {
|
||||
return respContent, fmt.Errorf("thinking unmarshal error: %w", err)
|
||||
} else if thinkingContent != "" {
|
||||
msgStats.RecordFirstToken()
|
||||
sendUpdate = true
|
||||
msgChunk.Thinking = ptr.Of(thinkingContent)
|
||||
}
|
||||
}
|
||||
|
||||
// Check Content
|
||||
if delta.Content != "" {
|
||||
msgStats.RecordFirstToken()
|
||||
sendUpdate = true
|
||||
msgChunk.Message = ptr.Of(delta.Content)
|
||||
respContent += delta.Content
|
||||
}
|
||||
}
|
||||
|
||||
// Send Timings
|
||||
if sendUpdate {
|
||||
msgStats.CalculateDerived()
|
||||
if err := cb(msgChunk); err != nil {
|
||||
return respContent, fmt.Errorf("chunk callback error: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check Error
|
||||
if err := stream.Err(); err != nil {
|
||||
return respContent, fmt.Errorf("stream error: %w", err)
|
||||
}
|
||||
|
||||
// Send Final Chunk
|
||||
msgStats.RecordLastToken()
|
||||
msgStats.CalculateDerived()
|
||||
if err := cb(&MessageChunk{Stats: &msgStats}); err != nil {
|
||||
return respContent, fmt.Errorf("chunk callback error: %w", err)
|
||||
}
|
||||
|
||||
return respContent, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateTitle(ctx context.Context, userMessage, model string) (string, error) {
|
||||
prompt := "You are an agent responsible for creating titles for chats based on the initial message. " +
|
||||
"Your titles should be succinct and short. Respond with JUST the chat title. Initial Message: \n\n" + userMessage
|
||||
|
||||
// Generate Text Stream
|
||||
output, err := c.SendMessage(ctx, []*store.Message{{
|
||||
Role: "user",
|
||||
Content: prompt,
|
||||
}}, model, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to sent message: %w", err)
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (c *Client) StructuredOutput(ctx context.Context, target any, prompt, model string) error {
|
||||
// Validate Target Pointer
|
||||
v := reflect.ValueOf(target)
|
||||
if v.Kind() != reflect.Pointer {
|
||||
return fmt.Errorf("target must be a pointer, got %T", target)
|
||||
}
|
||||
if v.IsNil() {
|
||||
return fmt.Errorf("target pointer is nil")
|
||||
}
|
||||
|
||||
// Validate Target Struct
|
||||
elem := v.Elem()
|
||||
if elem.Kind() != reflect.Struct {
|
||||
return fmt.Errorf("target must be a pointer to struct, got pointer to %s", elem.Kind())
|
||||
}
|
||||
|
||||
// Build Schema
|
||||
schema, err := buildJSONSchema(elem.Type())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to build schema: %w", err)
|
||||
}
|
||||
|
||||
// Perform Request
|
||||
resp, err := c.oaiClient.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
|
||||
Model: model,
|
||||
Messages: []openai.ChatCompletionMessageParamUnion{
|
||||
openai.UserMessage(prompt),
|
||||
},
|
||||
ResponseFormat: openai.ChatCompletionNewParamsResponseFormatUnion{
|
||||
OfJSONSchema: schema,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("API call failed: %w", err)
|
||||
}
|
||||
|
||||
// Parse Response
|
||||
content := resp.Choices[0].Message.Content
|
||||
if err := json.Unmarshal([]byte(content), target); err != nil {
|
||||
return fmt.Errorf("failed to unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildJSONSchema(rType reflect.Type) (*shared.ResponseFormatJSONSchemaParam, error) {
|
||||
schema, err := jsonschema.ForType(rType, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &shared.ResponseFormatJSONSchemaParam{
|
||||
JSONSchema: shared.ResponseFormatJSONSchemaJSONSchemaParam{
|
||||
Name: rType.Name(),
|
||||
Schema: map[string]any{
|
||||
"type": schema.Type,
|
||||
"properties": schema.Properties,
|
||||
"required": schema.Required,
|
||||
"additionalProperties": false,
|
||||
},
|
||||
Strict: openai.Bool(true),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func populateLlamaCPPTimings(msgStats *types.MessageStats, extraFields map[string]respjson.Field) bool {
|
||||
rawTimings, found := extraFields["timings"]
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
|
||||
var llamaTimings llamaCPPTimings
|
||||
if err := json.Unmarshal([]byte(rawTimings.Raw()), &llamaTimings); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if llamaTimings.PromptN != 0 {
|
||||
msgStats.PromptTokens = ptr.Of(int32(llamaTimings.PromptN))
|
||||
}
|
||||
if llamaTimings.PredictedN != 0 {
|
||||
msgStats.GeneratedTokens = ptr.Of(int32(llamaTimings.PredictedN))
|
||||
}
|
||||
|
||||
msgStats.PromptPerSec = ptr.Of(float32(llamaTimings.PromptPerSecond))
|
||||
msgStats.GeneratedPerSec = ptr.Of(float32(llamaTimings.PredictedPerSecond))
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func populateUsageTimings(msgStats *types.MessageStats, usage openai.CompletionUsage) (didChange bool) {
|
||||
if usage.PromptTokens == 0 && usage.CompletionTokens == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if msgStats.PromptTokens == nil {
|
||||
didChange = true
|
||||
msgStats.PromptTokens = ptr.Of(int32(usage.PromptTokens))
|
||||
}
|
||||
|
||||
if msgStats.GeneratedTokens == nil {
|
||||
didChange = true
|
||||
reasoningTokens := usage.CompletionTokensDetails.ReasoningTokens
|
||||
msgStats.GeneratedTokens = ptr.Of(int32(usage.CompletionTokens + reasoningTokens))
|
||||
}
|
||||
|
||||
return didChange
|
||||
}
|
||||
|
||||
func NewClient(baseURL *url.URL) *Client {
|
||||
oaiClient := openai.NewClient(option.WithBaseURL(baseURL.String()))
|
||||
return &Client{oaiClient: &oaiClient}
|
||||
}
|
||||
79
backend/internal/client/client_test.go
Normal file
79
backend/internal/client/client_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"reichard.io/aethera/internal/store"
|
||||
)
|
||||
|
||||
const model = "devstral-small-2-instruct"
|
||||
|
||||
func TestSendMessage(t *testing.T) {
|
||||
// Initialize Client
|
||||
baseURL, err := url.Parse("https://llm-api.va.reichard.io/v1")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse base URL: %v", err)
|
||||
}
|
||||
client := NewClient(baseURL)
|
||||
|
||||
// Create Context
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Generate Text Stream
|
||||
var buf bytes.Buffer
|
||||
_, err = client.SendMessage(ctx, []*store.Message{{
|
||||
Role: "user",
|
||||
Content: "Hello, how are you?",
|
||||
}}, model, func(mc *MessageChunk) error {
|
||||
if mc.Message != nil {
|
||||
_, err := buf.Write([]byte(*mc.Message))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate text stream: %v", err)
|
||||
}
|
||||
|
||||
// Verify Results
|
||||
output := buf.String()
|
||||
if output == "" {
|
||||
t.Error("No content was written to the buffer")
|
||||
} else {
|
||||
t.Logf("Successfully received %d bytes from the stream", len(output))
|
||||
t.Logf("Output: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSummarizeChat(t *testing.T) {
|
||||
// Initialize Client
|
||||
baseURL, err := url.Parse("https://llm-api.va.reichard.io/v1")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse base URL: %v", err)
|
||||
}
|
||||
client := NewClient(baseURL)
|
||||
|
||||
// Create Context
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Generate Text Stream
|
||||
userMessage := "Write me a go program that reads in a zip file and prints the contents along with their sizes and mimetype."
|
||||
output, err := client.CreateTitle(ctx, userMessage, model)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate text stream: %v", err)
|
||||
}
|
||||
|
||||
// Verify Results
|
||||
if output == "" {
|
||||
t.Error("No content was written to the buffer")
|
||||
} else {
|
||||
t.Logf("Successfully received %d bytes from the stream", len(output))
|
||||
t.Logf("Output: %s", output)
|
||||
}
|
||||
}
|
||||
41
backend/internal/client/convert.go
Normal file
41
backend/internal/client/convert.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/openai/openai-go/v3"
|
||||
)
|
||||
|
||||
func fromOpenAIModel(m openai.Model) Model {
|
||||
newModel := Model{
|
||||
Model: m,
|
||||
Name: m.ID,
|
||||
}
|
||||
|
||||
extraFields := make(map[string]any)
|
||||
for k, v := range m.JSON.ExtraFields {
|
||||
var val any
|
||||
if err := json.Unmarshal([]byte(v.Raw()), &val); err != nil {
|
||||
continue
|
||||
}
|
||||
extraFields[k] = val
|
||||
}
|
||||
|
||||
// Extract Name
|
||||
if rawName, found := extraFields["name"]; found {
|
||||
if name, ok := rawName.(string); ok {
|
||||
newModel.Name = name
|
||||
}
|
||||
}
|
||||
|
||||
// Extract Meta
|
||||
if rawMeta, found := extraFields["meta"]; found {
|
||||
if parsedMeta, ok := rawMeta.(map[string]any); ok {
|
||||
if llamaMeta, ok := parsedMeta["llamaswap"].(map[string]any); ok {
|
||||
newModel.Meta = llamaMeta
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newModel
|
||||
}
|
||||
31
backend/internal/client/types.go
Normal file
31
backend/internal/client/types.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"github.com/openai/openai-go/v3"
|
||||
"reichard.io/aethera/internal/types"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
openai.Model
|
||||
|
||||
Name string `json:"name"`
|
||||
Meta map[string]any `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
type MessageChunk struct {
|
||||
Thinking *string `json:"thinking,omitempty"`
|
||||
Message *string `json:"message,omitempty"`
|
||||
Stats *types.MessageStats `json:"stats,omitempty"`
|
||||
}
|
||||
|
||||
type llamaCPPTimings struct {
|
||||
CacheN int `json:"cache_n"`
|
||||
PredictedMS float64 `json:"predicted_ms"`
|
||||
PredictedN int `json:"predicted_n"`
|
||||
PredictedPerSecond float64 `json:"predicted_per_second"`
|
||||
PredictedPerTokenMS float64 `json:"predicted_per_token_ms"`
|
||||
PromptMS float64 `json:"prompt_ms"`
|
||||
PromptN int `json:"prompt_n"`
|
||||
PromptPerSecond float64 `json:"prompt_per_second"`
|
||||
PromptPerTokenMS float64 `json:"prompt_per_token_ms"`
|
||||
}
|
||||
95
backend/internal/server/server.go
Normal file
95
backend/internal/server/server.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"reichard.io/aethera/internal/api"
|
||||
"reichard.io/aethera/internal/store"
|
||||
)
|
||||
|
||||
func StartServer(settingsStore store.Store, dataDir, listenAddress string, listenPort int) {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Create API Instance - use settingsStore as the unified store for both settings and chat
|
||||
logger := logrus.New()
|
||||
api := api.New(settingsStore, dataDir, logger)
|
||||
feFS := http.FileServer(http.Dir("../frontend/public/"))
|
||||
mux.Handle("GET /", feFS)
|
||||
|
||||
// Serve UI Pages
|
||||
pagesFS := http.FileServer(http.Dir("../frontend/public/pages/"))
|
||||
mux.Handle("GET /pages/", http.StripPrefix("/pages/", pagesFS))
|
||||
|
||||
// Serve Generated Data
|
||||
genFS := http.FileServer(http.Dir(path.Join(dataDir, "generated")))
|
||||
mux.Handle("GET /generated/", http.StripPrefix("/generated/", genFS))
|
||||
|
||||
// Register API Routes
|
||||
mux.HandleFunc("POST /api/images", api.PostImage)
|
||||
mux.HandleFunc("GET /api/settings", api.GetSettings)
|
||||
mux.HandleFunc("POST /api/settings", api.PostSettings)
|
||||
mux.HandleFunc("GET /api/models", api.GetModels)
|
||||
mux.HandleFunc("GET /api/images", api.GetImages)
|
||||
mux.HandleFunc("DELETE /api/images/{filename}", api.DeleteImage)
|
||||
|
||||
// Register Chat Management Routes
|
||||
mux.HandleFunc("GET /api/chats", api.GetChats)
|
||||
mux.HandleFunc("POST /api/chats", api.PostChat)
|
||||
mux.HandleFunc("GET /api/chats/{chatId}", api.GetChat)
|
||||
mux.HandleFunc("POST /api/chats/{chatId}", api.PostChatMessage)
|
||||
mux.HandleFunc("DELETE /api/chats/{chatId}", api.DeleteChat)
|
||||
|
||||
// Wrap Logging
|
||||
wrappedMux := loggingMiddleware(mux)
|
||||
|
||||
logrus.Infof("Starting server on %s:%d with data directory: %s", listenAddress, listenPort, dataDir)
|
||||
logrus.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", listenAddress, listenPort), wrappedMux))
|
||||
}
|
||||
|
||||
// loggingMiddleware wraps an http.Handler and logs requests
|
||||
func loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
ww := &responseWriterWrapper{ResponseWriter: w}
|
||||
next.ServeHTTP(ww, r)
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"datetime": start.UTC().Format(time.RFC3339),
|
||||
"method": r.Method,
|
||||
"path": r.URL.Path,
|
||||
"remote": r.RemoteAddr,
|
||||
"status": ww.getStatusCode(),
|
||||
"latency": time.Since(start),
|
||||
}).Infof("%s %s", r.Method, r.URL.Path)
|
||||
})
|
||||
}
|
||||
|
||||
// responseWriterWrapper wraps http.ResponseWriter to capture status code
|
||||
type responseWriterWrapper struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
}
|
||||
|
||||
func (w *responseWriterWrapper) Flush() {
|
||||
if f, ok := w.ResponseWriter.(http.Flusher); ok {
|
||||
f.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
func (rw *responseWriterWrapper) getStatusCode() int {
|
||||
if rw.statusCode == 0 {
|
||||
return 200
|
||||
}
|
||||
return rw.statusCode
|
||||
}
|
||||
|
||||
func (rw *responseWriterWrapper) WriteHeader(code int) {
|
||||
if code > 0 {
|
||||
rw.statusCode = code
|
||||
}
|
||||
rw.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
34
backend/internal/storage/storage.go
Normal file
34
backend/internal/storage/storage.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"reichard.io/aethera/internal/api"
|
||||
)
|
||||
|
||||
func ListImages(dataDir string) ([]api.ImageRecord, error) {
|
||||
files, err := os.ReadDir(path.Join(dataDir, "generated/images"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var imageList []api.ImageRecord
|
||||
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, api.ImageRecord{
|
||||
Name: file.Name(),
|
||||
Path: "/generated/images" + file.Name(),
|
||||
Size: info.Size(),
|
||||
Date: info.ModTime().Format("2006-01-02T15:04:05Z07:00"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return imageList, nil
|
||||
}
|
||||
10
backend/internal/store/errors.go
Normal file
10
backend/internal/store/errors.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrChatNotFound = errors.New("chat not found")
|
||||
ErrNilChatID = errors.New("chat id cannot be nil")
|
||||
)
|
||||
18
backend/internal/store/interface.go
Normal file
18
backend/internal/store/interface.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Store interface {
|
||||
// Settings Methods
|
||||
SaveSettings(*Settings) error
|
||||
GetSettings() (*Settings, error)
|
||||
|
||||
// Chat Methods
|
||||
GetChat(chatID uuid.UUID) (*Chat, error)
|
||||
DeleteChat(chatID uuid.UUID) error
|
||||
ListChats() ([]*Chat, error)
|
||||
SaveChat(*Chat) error
|
||||
SaveChatMessage(*Message) error
|
||||
}
|
||||
126
backend/internal/store/memory.go
Normal file
126
backend/internal/store/memory.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"reichard.io/aethera/pkg/slices"
|
||||
)
|
||||
|
||||
var _ Store = (*InMemoryStore)(nil)
|
||||
|
||||
// InMemoryStore implements Store interface using in-memory storage
|
||||
type InMemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
chats map[uuid.UUID]*Chat
|
||||
settings *Settings
|
||||
}
|
||||
|
||||
// NewInMemoryStore creates a new InMemoryStore
|
||||
func NewInMemoryStore() *InMemoryStore {
|
||||
return &InMemoryStore{
|
||||
chats: make(map[uuid.UUID]*Chat),
|
||||
}
|
||||
}
|
||||
|
||||
// SaveChat creates or updates a chat
|
||||
func (s *InMemoryStore) SaveChat(c *Chat) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
c.ensureDefaults()
|
||||
s.chats[c.ID] = c
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetChat retrieves a chat by ID
|
||||
func (s *InMemoryStore) GetChat(chatID uuid.UUID) (*Chat, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
chat, exists := s.chats[chatID]
|
||||
if !exists {
|
||||
return nil, ErrChatNotFound
|
||||
}
|
||||
|
||||
// Return a copy to avoid concurrent modification
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
// DeleteChat removes a chat by ID
|
||||
func (s *InMemoryStore) DeleteChat(chatID uuid.UUID) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, exists := s.chats[chatID]; !exists {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
delete(s.chats, chatID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListChats returns all chat
|
||||
func (s *InMemoryStore) ListChats() ([]*Chat, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
// Convert Map
|
||||
var chats []*Chat
|
||||
for _, chat := range s.chats {
|
||||
chats = append(chats, chat)
|
||||
}
|
||||
|
||||
return chats, nil
|
||||
}
|
||||
|
||||
// SaveChatMessage creates or updates a chat message to a chat
|
||||
func (s *InMemoryStore) SaveChatMessage(m *Message) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if m.ChatID == uuid.Nil {
|
||||
return ErrNilChatID
|
||||
}
|
||||
m.ensureDefaults()
|
||||
|
||||
// Get Chat
|
||||
chat, exists := s.chats[m.ChatID]
|
||||
if !exists {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
// Find Existing
|
||||
existingMsg, found := slices.FindFirst(chat.Messages, func(item *Message) bool {
|
||||
return item.ID == m.ID
|
||||
})
|
||||
|
||||
// Upsert
|
||||
if found {
|
||||
*existingMsg = *m
|
||||
} else {
|
||||
chat.Messages = append(chat.Messages, m)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveSettings saves settings to in-memory storage
|
||||
func (s *InMemoryStore) SaveSettings(settings *Settings) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.settings = settings
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSettings retrieves settings from in-memory storage
|
||||
func (s *InMemoryStore) GetSettings() (*Settings, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
if s.settings == nil {
|
||||
return &Settings{}, nil
|
||||
}
|
||||
return s.settings, nil
|
||||
}
|
||||
190
backend/internal/store/storage.go
Normal file
190
backend/internal/store/storage.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"reichard.io/aethera/pkg/slices"
|
||||
)
|
||||
|
||||
var _ Store = (*FileStore)(nil)
|
||||
|
||||
// Settings represents the application settings
|
||||
type Settings struct {
|
||||
APIEndpoint string `json:"api_endpoint,omitempty"`
|
||||
ImageEditSelector string `json:"image_edit_selector,omitempty"`
|
||||
ImageGenerationSelector string `json:"image_generation_selector,omitempty"`
|
||||
TextGenerationSelector string `json:"text_generation_selector,omitempty"`
|
||||
}
|
||||
|
||||
// FileStore implements the Store interface using a file-based storage
|
||||
type FileStore struct {
|
||||
filePath string
|
||||
chatDir string
|
||||
}
|
||||
|
||||
// NewFileStore creates a new FileStore with the specified file path
|
||||
func NewFileStore(filePath string) (*FileStore, error) {
|
||||
// Derive Chat Directory
|
||||
chatDir := filepath.Join(filepath.Dir(filePath), "chats")
|
||||
|
||||
// Ensure Chat Directory Exists
|
||||
if err := os.MkdirAll(chatDir, 0755); err != nil {
|
||||
return nil, fmt.Errorf("failed to create directory: %w", err)
|
||||
}
|
||||
|
||||
return &FileStore{
|
||||
filePath: filePath,
|
||||
chatDir: chatDir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetSettings reads and returns the settings from the file
|
||||
func (fs *FileStore) GetSettings() (*Settings, error) {
|
||||
data, err := os.ReadFile(fs.filePath)
|
||||
if err != nil {
|
||||
return &Settings{}, nil
|
||||
}
|
||||
|
||||
var settings Settings
|
||||
err = json.Unmarshal(data, &settings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &settings, nil
|
||||
}
|
||||
|
||||
// SaveSettings saves the settings to the file
|
||||
func (fs *FileStore) SaveSettings(settings *Settings) error {
|
||||
data, err := json.MarshalIndent(settings, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(fs.filePath, data, 0644)
|
||||
}
|
||||
|
||||
// GetChat retrieves a chat from disk
|
||||
func (fs *FileStore) GetChat(chatID uuid.UUID) (*Chat, error) {
|
||||
filePath := filepath.Join(fs.chatDir, chatID.String()+".json")
|
||||
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, ErrChatNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("failed to read chat: %w", err)
|
||||
}
|
||||
|
||||
var chat Chat
|
||||
if err := json.Unmarshal(data, &chat); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal chat: %w", err)
|
||||
}
|
||||
|
||||
return &chat, nil
|
||||
}
|
||||
|
||||
// SaveChat creates or updates a chat and persists it to disk
|
||||
func (fs *FileStore) SaveChat(c *Chat) error {
|
||||
c.ensureDefaults()
|
||||
return fs.saveChatSession(c)
|
||||
}
|
||||
|
||||
// DeleteChat removes a chat from disk
|
||||
func (fs *FileStore) DeleteChat(chatID uuid.UUID) error {
|
||||
filePath := filepath.Join(fs.chatDir, chatID.String()+".json")
|
||||
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
if err := os.Remove(filePath); err != nil {
|
||||
return fmt.Errorf("failed to delete chat: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListChats returns all persisted chats
|
||||
func (fs *FileStore) ListChats() ([]*Chat, error) {
|
||||
// Read Files
|
||||
entries, err := os.ReadDir(fs.chatDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read chat directory: %w", err)
|
||||
}
|
||||
|
||||
var chats []*Chat
|
||||
for _, entry := range entries {
|
||||
// Ensure JSON
|
||||
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Extract Chat ID
|
||||
rawChatID := strings.TrimSuffix(entry.Name(), ".json")
|
||||
chatID, err := uuid.Parse(rawChatID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: invalid chat id %s", err, rawChatID)
|
||||
}
|
||||
|
||||
// Read & Parse Chat
|
||||
chat, err := fs.GetChat(chatID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: failed to read chat id %s", err, rawChatID)
|
||||
}
|
||||
|
||||
chats = append(chats, chat)
|
||||
}
|
||||
|
||||
return chats, nil
|
||||
}
|
||||
|
||||
// SaveChatMessage creates or updates a chat message to a chat and persists it to disk
|
||||
func (fs *FileStore) SaveChatMessage(m *Message) error {
|
||||
if m.ChatID == uuid.Nil {
|
||||
return ErrNilChatID
|
||||
}
|
||||
m.ensureDefaults()
|
||||
|
||||
// Get Chat
|
||||
chat, err := fs.GetChat(m.ChatID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Find Existing
|
||||
existingMsg, found := slices.FindFirst(chat.Messages, func(item *Message) bool {
|
||||
return item.ID == m.ID
|
||||
})
|
||||
|
||||
// Upsert
|
||||
if found {
|
||||
*existingMsg = *m
|
||||
} else {
|
||||
chat.Messages = append(chat.Messages, m)
|
||||
}
|
||||
|
||||
// Save
|
||||
return fs.saveChatSession(chat)
|
||||
}
|
||||
|
||||
// saveChatSession is a helper method to save a chat to disk
|
||||
func (fs *FileStore) saveChatSession(session *Chat) error {
|
||||
filePath := filepath.Join(fs.chatDir, session.ID.String()+".json")
|
||||
|
||||
data, err := json.MarshalIndent(session, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal chat: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filePath, data, 0644); err != nil {
|
||||
return fmt.Errorf("failed to write chat file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
39
backend/internal/store/types.go
Normal file
39
backend/internal/store/types.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"reichard.io/aethera/internal/types"
|
||||
)
|
||||
|
||||
type baseModel struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (b *baseModel) ensureDefaults() {
|
||||
if b.ID == uuid.Nil {
|
||||
b.ID = uuid.New()
|
||||
}
|
||||
if b.CreatedAt.IsZero() {
|
||||
b.CreatedAt = time.Now()
|
||||
}
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
baseModel
|
||||
|
||||
Title string `json:"title"`
|
||||
Messages []*Message `json:"messages"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
baseModel
|
||||
|
||||
ChatID uuid.UUID `json:"chat_id"`
|
||||
Role string `json:"role"`
|
||||
Thinking string `json:"thinking"`
|
||||
Content string `json:"content"`
|
||||
Stats *types.MessageStats `json:"stats,omitempty"`
|
||||
}
|
||||
50
backend/internal/types/message_stats.go
Normal file
50
backend/internal/types/message_stats.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"reichard.io/aethera/pkg/ptr"
|
||||
)
|
||||
|
||||
type MessageStats struct {
|
||||
StartTime time.Time `json:"start_time"`
|
||||
EndTime *time.Time `json:"end_time,omitempty"`
|
||||
|
||||
PromptTokens *int32 `json:"prompt_tokens"`
|
||||
GeneratedTokens *int32 `json:"generated_tokens"`
|
||||
|
||||
PromptPerSec *float32 `json:"prompt_per_second"`
|
||||
GeneratedPerSec *float32 `json:"generated_per_second"`
|
||||
|
||||
TimeToFirstToken *int32 `json:"time_to_first_token,omitempty"`
|
||||
TimeToLastToken *int32 `json:"time_to_last_token,omitempty"`
|
||||
}
|
||||
|
||||
func (s *MessageStats) RecordFirstToken() {
|
||||
if s.TimeToFirstToken == nil {
|
||||
s.TimeToFirstToken = ptr.Of(int32(time.Since(s.StartTime).Milliseconds()))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MessageStats) RecordLastToken() {
|
||||
s.TimeToLastToken = ptr.Of(int32(time.Since(s.StartTime).Milliseconds()))
|
||||
}
|
||||
|
||||
func (s *MessageStats) CalculateDerived() {
|
||||
// Populate PromptPerSec
|
||||
if s.PromptPerSec == nil && s.TimeToFirstToken != nil && s.PromptTokens != nil {
|
||||
ttft := *s.TimeToFirstToken
|
||||
pt := *s.PromptTokens
|
||||
if ttft > 0 && pt > 0 {
|
||||
s.PromptPerSec = ptr.Of(float32(1000 * pt / ttft))
|
||||
}
|
||||
}
|
||||
|
||||
// Populate GeneratedPerSec
|
||||
if s.GeneratedPerSec == nil && s.TimeToFirstToken != nil && s.TimeToLastToken != nil && s.GeneratedTokens != nil {
|
||||
genTimeMS := *s.TimeToLastToken - *s.TimeToFirstToken
|
||||
if genTimeMS > 0 && *s.GeneratedTokens > 0 {
|
||||
s.GeneratedPerSec = ptr.Of(float32(1000 * float32(*s.GeneratedTokens) / float32(genTimeMS)))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user