refactoring

This commit is contained in:
Ettore Di Giacinto
2025-02-26 22:37:48 +01:00
parent 0139b79835
commit 0a18d8409e
22 changed files with 90 additions and 41 deletions

6
.gitignore vendored
View File

@@ -1,4 +1,6 @@
models/
data/
example/webui/pool
uploads/
pool
uploads/
local-agent-framework
localagent

View File

@@ -17,7 +17,7 @@ COPY . /work
# Set the current work directory inside the container
WORKDIR /work
RUN go build -ldflags="$LDFLAGS" -o webui ./example/webui
RUN go build -ldflags="$LDFLAGS" -o localagent ./
FROM scratch
@@ -26,4 +26,4 @@ COPY --from=builder /work/webui /webui
COPY --from=builder /etc/ssl/ /etc/ssl/
# Define the command that will be run when the container is started
ENTRYPOINT ["/webui"]
ENTRYPOINT ["/localagent"]

View File

@@ -7,8 +7,9 @@ tests:
webui-nokb:
$(MAKE) webui KBDISABLEINDEX=true
.PHONY: webui
webui:
cd example/webui && $(GOCMD) run ./
$(GOCMD) run ./
webui-image:
docker build -t $(IMAGE_NAME) -f Dockerfile.webui .

View File

@@ -1,19 +1,14 @@
package main
import (
"embed"
"log"
"net/http"
"os"
"github.com/donseba/go-htmx"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/state"
"github.com/mudler/local-agent-framework/pkg/llm"
rag "github.com/mudler/local-agent-framework/pkg/vectorstore"
"github.com/mudler/local-agent-framework/webui"
)
var testModel = os.Getenv("TEST_MODEL")
@@ -37,12 +32,6 @@ func init() {
}
}
//go:embed views/*
var viewsfs embed.FS
//go:embed public/*
var embeddedFiles embed.FS
func main() {
// current dir
cwd, err := os.Getwd()
@@ -68,27 +57,16 @@ func main() {
}
}
pool, err := state.NewAgentPool(testModel, apiURL, stateDir, ragDB, Actions, Connectors, timeout)
pool, err := state.NewAgentPool(testModel, apiURL, stateDir, ragDB, webui.Actions, webui.Connectors, timeout)
if err != nil {
panic(err)
}
app := &App{
htmx: htmx.New(),
pool: pool,
}
app := webui.NewApp(webui.WithPool(pool))
if err := pool.StartAll(); err != nil {
panic(err)
}
engine := html.NewFileSystem(http.FS(viewsfs), ".html")
// Initialize a new Fiber app
// Pass the engine to the Views
webapp := fiber.New(fiber.Config{
Views: engine,
})
RegisterRoutes(webapp, pool, app)
log.Fatal(webapp.Listen(":3000"))
log.Fatal(app.Listen(":3000"))
}

View File

@@ -1,4 +1,4 @@
package main
package webui
import (
"context"

View File

@@ -1,4 +1,4 @@
package main
package webui
import (
"bytes"
@@ -17,15 +17,38 @@ import (
"github.com/donseba/go-htmx"
"github.com/dslipak/pdf"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
)
type (
App struct {
htmx *htmx.HTMX
pool *state.AgentPool
htmx *htmx.HTMX
config *Config
*fiber.App
}
)
func NewApp(opts ...Option) *App {
config := NewConfig(opts...)
engine := html.NewFileSystem(http.FS(viewsfs), ".html")
// Initialize a new Fiber app
// Pass the engine to the Views
webapp := fiber.New(fiber.Config{
Views: engine,
})
a := &App{
htmx: htmx.New(),
config: config,
App: webapp,
}
a.registerRoutes(config.Pool, webapp)
return a
}
func (a *App) KnowledgeBaseReset(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
db := pool.GetAgentMemory(c.Params("name"))
@@ -124,7 +147,7 @@ func (a *App) KnowledgeBaseFile(pool *state.AgentPool) func(c *fiber.Ctx) error
}
xlog.Info("Content is", content)
chunkSize := defaultChunkSize
chunkSize := a.config.DefaultChunkSize
if payload.ChunkSize > 0 {
chunkSize = payload.ChunkSize
}
@@ -155,7 +178,7 @@ func (a *App) KnowledgeBase(pool *state.AgentPool) func(c *fiber.Ctx) error {
if website == "" {
return fmt.Errorf("please enter a URL")
}
chunkSize := defaultChunkSize
chunkSize := a.config.DefaultChunkSize
if payload.ChunkSize > 0 {
chunkSize = payload.ChunkSize
}

View File

@@ -1,4 +1,4 @@
package main
package webui
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package main
package webui
import (
"fmt"

36
webui/options.go Normal file
View File

@@ -0,0 +1,36 @@
package webui
import "github.com/mudler/local-agent-framework/core/state"
type Config struct {
DefaultChunkSize int
Pool *state.AgentPool
}
type Option func(*Config)
func WithDefaultChunkSize(size int) Option {
return func(c *Config) {
c.DefaultChunkSize = size
}
}
func WithPool(pool *state.AgentPool) Option {
return func(c *Config) {
c.Pool = pool
}
}
func (c *Config) Apply(opts ...Option) {
for _, opt := range opts {
opt(c)
}
}
func NewConfig(opts ...Option) *Config {
c := &Config{
DefaultChunkSize: 2048,
}
c.Apply(opts...)
return c
}

View File

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

Before

Width:  |  Height:  |  Size: 118 KiB

After

Width:  |  Height:  |  Size: 118 KiB

View File

Before

Width:  |  Height:  |  Size: 341 KiB

After

Width:  |  Height:  |  Size: 341 KiB

View File

@@ -1,6 +1,7 @@
package main
package webui
import (
"embed"
"math/rand"
"net/http"
@@ -11,7 +12,13 @@ import (
"github.com/mudler/local-agent-framework/core/state"
)
func RegisterRoutes(webapp *fiber.App, pool *state.AgentPool, app *App) {
//go:embed views/*
var viewsfs embed.FS
//go:embed public/*
var embeddedFiles embed.FS
func (app *App) registerRoutes(pool *state.AgentPool, webapp *fiber.App) {
webapp.Use("/public", filesystem.New(filesystem.Config{
Root: http.FS(embeddedFiles),
@@ -106,6 +113,8 @@ func RegisterRoutes(webapp *fiber.App, pool *state.AgentPool, app *App) {
})
webapp.Post("/settings/import", app.ImportAgent(pool))
webapp.Get("/settings/export/:name", app.ExportAgent(pool))
return
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")