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

4
.gitignore vendored
View File

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

View File

@@ -17,7 +17,7 @@ COPY . /work
# Set the current work directory inside the container # Set the current work directory inside the container
WORKDIR /work WORKDIR /work
RUN go build -ldflags="$LDFLAGS" -o webui ./example/webui RUN go build -ldflags="$LDFLAGS" -o localagent ./
FROM scratch FROM scratch
@@ -26,4 +26,4 @@ COPY --from=builder /work/webui /webui
COPY --from=builder /etc/ssl/ /etc/ssl/ COPY --from=builder /etc/ssl/ /etc/ssl/
# Define the command that will be run when the container is started # 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: webui-nokb:
$(MAKE) webui KBDISABLEINDEX=true $(MAKE) webui KBDISABLEINDEX=true
.PHONY: webui
webui: webui:
cd example/webui && $(GOCMD) run ./ $(GOCMD) run ./
webui-image: webui-image:
docker build -t $(IMAGE_NAME) -f Dockerfile.webui . docker build -t $(IMAGE_NAME) -f Dockerfile.webui .

View File

@@ -1,19 +1,14 @@
package main package main
import ( import (
"embed"
"log" "log"
"net/http"
"os" "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/agent"
"github.com/mudler/local-agent-framework/core/state" "github.com/mudler/local-agent-framework/core/state"
"github.com/mudler/local-agent-framework/pkg/llm" "github.com/mudler/local-agent-framework/pkg/llm"
rag "github.com/mudler/local-agent-framework/pkg/vectorstore" rag "github.com/mudler/local-agent-framework/pkg/vectorstore"
"github.com/mudler/local-agent-framework/webui"
) )
var testModel = os.Getenv("TEST_MODEL") 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() { func main() {
// current dir // current dir
cwd, err := os.Getwd() 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 { if err != nil {
panic(err) panic(err)
} }
app := &App{ app := webui.NewApp(webui.WithPool(pool))
htmx: htmx.New(),
pool: pool,
}
if err := pool.StartAll(); err != nil { if err := pool.StartAll(); err != nil {
panic(err) 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(app.Listen(":3000"))
log.Fatal(webapp.Listen(":3000"))
} }

View File

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

View File

@@ -1,4 +1,4 @@
package main package webui
import ( import (
"bytes" "bytes"
@@ -17,15 +17,38 @@ import (
"github.com/donseba/go-htmx" "github.com/donseba/go-htmx"
"github.com/dslipak/pdf" "github.com/dslipak/pdf"
fiber "github.com/gofiber/fiber/v2" fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
) )
type ( type (
App struct { App struct {
htmx *htmx.HTMX htmx *htmx.HTMX
pool *state.AgentPool 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 { func (a *App) KnowledgeBaseReset(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error {
db := pool.GetAgentMemory(c.Params("name")) 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) xlog.Info("Content is", content)
chunkSize := defaultChunkSize chunkSize := a.config.DefaultChunkSize
if payload.ChunkSize > 0 { if payload.ChunkSize > 0 {
chunkSize = payload.ChunkSize chunkSize = payload.ChunkSize
} }
@@ -155,7 +178,7 @@ func (a *App) KnowledgeBase(pool *state.AgentPool) func(c *fiber.Ctx) error {
if website == "" { if website == "" {
return fmt.Errorf("please enter a URL") return fmt.Errorf("please enter a URL")
} }
chunkSize := defaultChunkSize chunkSize := a.config.DefaultChunkSize
if payload.ChunkSize > 0 { if payload.ChunkSize > 0 {
chunkSize = payload.ChunkSize chunkSize = payload.ChunkSize
} }

View File

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

View File

@@ -1,4 +1,4 @@
package main package webui
import ( import (
"fmt" "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 ( import (
"embed"
"math/rand" "math/rand"
"net/http" "net/http"
@@ -11,7 +12,13 @@ import (
"github.com/mudler/local-agent-framework/core/state" "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{ webapp.Use("/public", filesystem.New(filesystem.Config{
Root: http.FS(embeddedFiles), 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.Post("/settings/import", app.ImportAgent(pool))
webapp.Get("/settings/export/:name", app.ExportAgent(pool)) webapp.Get("/settings/export/:name", app.ExportAgent(pool))
return
} }
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")