Add import/export

This commit is contained in:
Ettore Di Giacinto
2024-04-14 00:05:19 +02:00
parent 54de6221d1
commit 7c0f615952
5 changed files with 158 additions and 5 deletions

View File

@@ -465,6 +465,14 @@ func (a *AgentPool) GetAgent(name string) *Agent {
return a.agents[name]
}
func (a *AgentPool) GetConfig(name string) *AgentConfig {
agent, exists := a.pool[name]
if !exists {
return nil
}
return &agent
}
func (a *AgentPool) GetManager(name string) Manager {
return a.managers[name]
}

View File

@@ -2,6 +2,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
@@ -30,7 +31,6 @@ func (a *App) KnowledgeBaseReset(db *InMemoryDatabase) func(c *fiber.Ctx) error
func (a *App) KnowledgeBaseFile(db *InMemoryDatabase) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
// https://golang.withcodeexample.com/blog/file-upload-handling-golang-fiber-guide/
// Handle file upload logic
file, err := c.FormFile("file")
if err != nil {
// Handle error
@@ -179,6 +179,59 @@ func (a *App) Create(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) ExportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
agent := pool.GetConfig(c.Params("name"))
if agent == nil {
return c.Status(http.StatusNotFound).SendString("Agent not found")
}
c.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.json", agent.Name))
return c.JSON(agent)
}
}
func (a *App) ImportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
file, err := c.FormFile("file")
if err != nil {
// Handle error
return err
}
os.MkdirAll("./uploads", os.ModePerm)
destination := fmt.Sprintf("./uploads/%s", file.Filename)
if err := c.SaveFile(file, destination); err != nil {
// Handle error
return err
}
data, err := os.ReadFile(destination)
if err != nil {
return err
}
config := AgentConfig{}
if err := json.Unmarshal(data, &config); err != nil {
return err
}
fmt.Println("Importing agent", config.Name)
if config.Name == "" {
c.Status(http.StatusBadRequest).SendString("Name is required")
return nil
}
if err := pool.CreateAgent(config.Name, &config); err != nil {
c.Status(http.StatusInternalServerError).SendString(err.Error())
return nil
}
return c.Redirect("/agents")
}
}
func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
payload := struct {

View File

@@ -91,6 +91,14 @@ func RegisterRoutes(webapp *fiber.App, pool *AgentPool, db *InMemoryDatabase, ap
})
})
webapp.Get("/settings/:name", func(c *fiber.Ctx) error {
return c.Render("views/settings", fiber.Map{
// "Character": agent.Character,
"Name": c.Params("name"),
})
})
webapp.Post("/settings/import", app.ImportAgent(pool))
webapp.Get("/settings/export/:name", app.ExportAgent(pool))
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

View File

@@ -47,7 +47,7 @@
<span class="sr-only">Stop</span>
</th>
<th scope="col" class="px-6 py-3 text-center">
<span class="sr-only">Delete</span>
<span class="sr-only">Settings</span>
</th>
</tr>
</thead>
@@ -79,8 +79,8 @@
</button>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center text-sm font-medium">
<a href="/delete/{{.}}" class="text-red-500 hover:text-red-400">
<i class="fas fa-trash-alt"></i> Delete
<a href="/settings/{{.}}" class="text-indigo-500 hover:text-indigo-400">
<i class="fas fa-cog"></i> Settings
</a>
</td>
</tr>
@@ -92,7 +92,19 @@
</div>
</section>
<footer class="mt-8 text-center">
<!-- Moved the button up for alignment with the table -->
<!-- File Upload Form -->
<div class="section-box">
<form id='form' hx-encoding='multipart/form-data' hx-post='/settings/import'>
<h2>Import</h2>
<label for="file">File:</label>
<input type='file' name='file' id='file'>
<button type="submit">Upload File</button>
<progress id='progress' value='0' max='100'></progress>
</form>
</div>
</footer>
</div>
</body>

View File

@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Agent settings {{.Name}}</title>
{{template "views/partials/header"}}
<style>
.section-box {
background-color: #2a2a2a; /* Darker background for the form */
padding: 20px;
margin-bottom: 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
body {
background-color: #1a1a1a; /* Lighter overall background */
color: #ffffff;
font-family: sans-serif;
padding: 20px;
}
input, button {
width: 100%;
padding: 10px;
margin-top: 5px;
border-radius: 5px;
border: none;
}
input[type="text"], input[type="file"] {
background-color: #333333;
color: white;
}
button {
background-color: #4a76a8; /* Blue color for buttons */
color: white;
cursor: pointer;
}
button:hover {
background-color: #5a86b8;
}
</style>
</head>
<body>
{{template "views/partials/menu"}}
<header class="text-center mb-8">
<h1 class="text-3xl md:text-5xl font-bold">Agent settings - {{.Name}}</h1>
</header>
<div class="max-w-4xl mx-auto">
<div class="section-box">
<h2>Export</h2>
<a href="/settings/export/{{.Name}}" >Export</a>
</div>
<div class="section-box">
<h2>Danger section</h2>
<a href="/delete/{{.}}" class="text-red-500 hover:text-red-400">
<i class="fas fa-trash-alt"></i> Delete
</a>
</div>
</div>
<script>
htmx.on('#form', 'htmx:xhr:progress', function(evt) {
htmx.find('#progress').setAttribute('value', evt.detail.loaded / evt.detail.total * 100);
});
</script>
</body>
</html>