feat: restructure for better reuse

This commit is contained in:
Eugen Eisler 2024-10-12 22:37:35 +03:00
parent 17bde814cc
commit 56f995afb4
21 changed files with 63 additions and 62 deletions

View File

@ -2,6 +2,7 @@ package cli
import (
"fmt"
"github.com/danielmiessler/fabric/db/fs"
"github.com/danielmiessler/fabric/plugins/tools/converter"
"github.com/danielmiessler/fabric/restapi"
"os"
@ -10,7 +11,6 @@ import (
"strings"
"github.com/danielmiessler/fabric/core"
"github.com/danielmiessler/fabric/db"
)
// Cli Controls the cli. It takes in the flags and runs the appropriate functions
@ -30,7 +30,7 @@ func Cli(version string) (err error) {
return
}
fabricDb := db.NewDb(filepath.Join(homedir, ".config/fabric"))
fabricDb := fs.NewDb(filepath.Join(homedir, ".config/fabric"))
// if the setup flag is set, run the setup function
if currentFlags.Setup || currentFlags.SetupSkipPatterns || currentFlags.SetupVendor != "" {
@ -213,7 +213,7 @@ func Cli(version string) (err error) {
return
}
var session *db.Session
var session *fs.Session
if session, err = chatter.Send(currentFlags.BuildChatRequest(strings.Join(os.Args[1:], " ")), currentFlags.BuildChatOptions()); err != nil {
return
}
@ -244,7 +244,7 @@ func Cli(version string) (err error) {
return
}
func Setup(db *db.Db, skipUpdatePatterns bool) (ret *core.Fabric, err error) {
func Setup(db *fs.Db, skipUpdatePatterns bool) (ret *core.Fabric, err error) {
instance := core.NewFabricForSetup(db)
if err = instance.Setup(); err != nil {
@ -260,7 +260,7 @@ func Setup(db *db.Db, skipUpdatePatterns bool) (ret *core.Fabric, err error) {
return
}
func SetupVendor(db *db.Db, vendorName string) (ret *core.Fabric, err error) {
func SetupVendor(db *fs.Db, vendorName string) (ret *core.Fabric, err error) {
ret = core.NewFabricForSetup(db)
if err = ret.SetupVendor(vendorName); err != nil {
return

View File

@ -2,10 +2,10 @@ package cli
import (
"github.com/danielmiessler/fabric/core"
"github.com/danielmiessler/fabric/db/fs"
"os"
"testing"
"github.com/danielmiessler/fabric/db"
"github.com/stretchr/testify/assert"
)
@ -21,7 +21,7 @@ func TestCli(t *testing.T) {
}
func TestSetup(t *testing.T) {
mockDB := db.NewDb(os.TempDir())
mockDB := fs.NewDb(os.TempDir())
fabric, err := Setup(mockDB, false)
assert.Error(t, err)

View File

@ -4,14 +4,14 @@ import (
"context"
"fmt"
"github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/db"
"github.com/danielmiessler/fabric/db/fs"
"github.com/danielmiessler/fabric/plugins/ai"
goopenai "github.com/sashabaranov/go-openai"
"strings"
)
type Chatter struct {
db *db.Db
db *fs.Db
Stream bool
DryRun bool
@ -20,7 +20,7 @@ type Chatter struct {
vendor ai.Vendor
}
func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (session *db.Session, err error) {
func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (session *fs.Session, err error) {
if session, err = o.BuildSession(request, opts.Raw); err != nil {
return
}
@ -62,16 +62,16 @@ func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (s
return
}
func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *db.Session, err error) {
func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *fs.Session, err error) {
if request.SessionName != "" {
var sess *db.Session
var sess *fs.Session
if sess, err = o.db.Sessions.Get(request.SessionName); err != nil {
err = fmt.Errorf("could not find session %s: %v", request.SessionName, err)
return
}
session = sess
} else {
session = &db.Session{}
session = &fs.Session{}
}
if request.Meta != "" {
@ -80,7 +80,7 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
var contextContent string
if request.ContextName != "" {
var ctx *db.Context
var ctx *fs.Context
if ctx, err = o.db.Contexts.Get(request.ContextName); err != nil {
err = fmt.Errorf("could not find context %s: %v", request.ContextName, err)
return
@ -90,7 +90,7 @@ func (o *Chatter) BuildSession(request *common.ChatRequest, raw bool) (session *
var patternContent string
if request.PatternName != "" {
var pattern *db.Pattern
var pattern *fs.Pattern
if pattern, err = o.db.Patterns.GetApplyVariables(request.PatternName, request.PatternVariables); err != nil {
err = fmt.Errorf("could not find pattern %s: %v", request.PatternName, err)
return

View File

@ -5,7 +5,7 @@ import (
"fmt"
"github.com/atotto/clipboard"
"github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/db"
"github.com/danielmiessler/fabric/db/fs"
"github.com/danielmiessler/fabric/plugins/ai/anthropic"
"github.com/danielmiessler/fabric/plugins/ai/azure"
"github.com/danielmiessler/fabric/plugins/ai/dryrun"
@ -28,20 +28,20 @@ const DefaultPatternsGitRepoFolder = "patterns"
const NoSessionPatternUserMessages = "no session, pattern or user messages provided"
func NewFabric(db *db.Db) (ret *Fabric, err error) {
func NewFabric(db *fs.Db) (ret *Fabric, err error) {
ret = NewFabricBase(db)
err = ret.Configure()
return
}
func NewFabricForSetup(db *db.Db) (ret *Fabric) {
func NewFabricForSetup(db *fs.Db) (ret *Fabric) {
ret = NewFabricBase(db)
_ = ret.Configure()
return
}
// NewFabricBase Create a new Fabric from a list of already configured VendorsController
func NewFabricBase(db *db.Db) (ret *Fabric) {
func NewFabricBase(db *fs.Db) (ret *Fabric) {
ret = &Fabric{
VendorsManager: NewVendorsManager(),
@ -77,7 +77,7 @@ type Fabric struct {
*youtube.YouTube
Jina *jina.Client
Db *db.Db
Db *fs.Db
DefaultVendor *common.Setting
DefaultModel *common.SetupQuestion

View File

@ -1,21 +1,20 @@
package core
import (
"github.com/danielmiessler/fabric/db/fs"
"os"
"testing"
"github.com/danielmiessler/fabric/db"
)
func TestNewFabric(t *testing.T) {
_, err := NewFabric(db.NewDb(os.TempDir()))
_, err := NewFabric(fs.NewDb(os.TempDir()))
if err == nil {
t.Fatal("without setup error expected")
}
}
func TestSaveEnvFile(t *testing.T) {
fabric := NewFabricBase(db.NewDb(os.TempDir()))
fabric := NewFabricBase(fs.NewDb(os.TempDir()))
err := fabric.SaveEnvFile()
if err != nil {
@ -25,7 +24,7 @@ func TestSaveEnvFile(t *testing.T) {
func TestCopyToClipboard(t *testing.T) {
t.Skip("skipping test, because of docker env. in ci.")
fabric := NewFabricBase(db.NewDb(os.TempDir()))
fabric := NewFabricBase(fs.NewDb(os.TempDir()))
message := "test message"
err := fabric.CopyToClipboard(message)
@ -35,7 +34,7 @@ func TestCopyToClipboard(t *testing.T) {
}
func TestCreateOutputFile(t *testing.T) {
mockDb := &db.Db{}
mockDb := &fs.Db{}
fabric := NewFabricBase(mockDb)
fileName := "test_output.txt"

View File

@ -2,6 +2,7 @@ package core
import (
"fmt"
"github.com/danielmiessler/fabric/db/fs"
"io"
"os"
"path/filepath"
@ -9,7 +10,6 @@ import (
"strings"
"github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/db"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
@ -17,7 +17,7 @@ import (
"github.com/otiai10/copy"
)
func NewPatternsLoader(patterns *db.PatternsEntity) (ret *PatternsLoader) {
func NewPatternsLoader(patterns *fs.PatternsEntity) (ret *PatternsLoader) {
label := "Patterns Loader"
ret = &PatternsLoader{
Patterns: patterns,
@ -42,7 +42,7 @@ func NewPatternsLoader(patterns *db.PatternsEntity) (ret *PatternsLoader) {
type PatternsLoader struct {
*common.Configurable
Patterns *db.PatternsEntity
Patterns *fs.PatternsEntity
DefaultGitRepoUrl *common.SetupQuestion
DefaultFolder *common.SetupQuestion
@ -156,7 +156,7 @@ func (o *PatternsLoader) gitCloneAndCopy() (err error) {
return err
}
var changes []db.DirectoryChange
var changes []fs.DirectoryChange
// ... iterates over the commits
if err = cIter.ForEach(func(c *object.Commit) (err error) {
// GetApplyVariables the files changed in this commit by comparing with its parents
@ -171,7 +171,7 @@ func (o *PatternsLoader) gitCloneAndCopy() (err error) {
for _, fileStat := range patch.Stats() {
if strings.HasPrefix(fileStat.Name, o.pathPatternsPrefix) {
dir := filepath.Dir(fileStat.Name)
changes = append(changes, db.DirectoryChange{Dir: dir, Timestamp: c.Committer.When})
changes = append(changes, fs.DirectoryChange{Dir: dir, Timestamp: c.Committer.When})
}
}
return
@ -256,7 +256,7 @@ func (o *PatternsLoader) writeBlobToFile(blob *object.Blob, path string) (err er
return
}
func (o *PatternsLoader) makeUniqueList(changes []db.DirectoryChange) (err error) {
func (o *PatternsLoader) makeUniqueList(changes []fs.DirectoryChange) (err error) {
uniqueItems := make(map[string]bool)
for _, change := range changes {
if strings.TrimSpace(change.Dir) != "" && !strings.Contains(change.Dir, "=>") {

View File

@ -1,4 +1,4 @@
package db
package fs
import "fmt"

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"os"

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"fmt"

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"os"

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"fmt"

1
db/fs/patterns_test.go Normal file
View File

@ -0,0 +1 @@
package fs

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"fmt"

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"testing"

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package db
package fs
import (
"testing"

View File

@ -1 +0,0 @@
package db

View File

@ -1,19 +1,19 @@
package restapi
import (
"github.com/danielmiessler/fabric/db"
"github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin"
)
// ContextsHandler defines the handler for contexts-related operations
type ContextsHandler struct {
*StorageHandler[db.Context]
contexts *db.ContextsEntity
*StorageHandler[fs.Context]
contexts *fs.ContextsEntity
}
// NewContextsHandler creates a new ContextsHandler
func NewContextsHandler(r *gin.Engine, contexts *db.ContextsEntity) (ret *ContextsHandler) {
func NewContextsHandler(r *gin.Engine, contexts *fs.ContextsEntity) (ret *ContextsHandler) {
ret = &ContextsHandler{
StorageHandler: NewStorageHandler[db.Context](r, "contexts", contexts), contexts: contexts}
StorageHandler: NewStorageHandler[fs.Context](r, "contexts", contexts), contexts: contexts}
return
}

View File

@ -1,27 +1,29 @@
package restapi
import (
"github.com/danielmiessler/fabric/db"
"github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin"
"net/http"
)
// PatternsHandler defines the handler for patterns-related operations
type PatternsHandler struct {
*StorageHandler[db.Pattern]
patterns *db.PatternsEntity
*StorageHandler[fs.Pattern]
patterns *fs.PatternsEntity
}
// NewPatternsHandler creates a new PatternsHandler
func NewPatternsHandler(r *gin.Engine, patterns *db.PatternsEntity) (ret *PatternsHandler) {
func NewPatternsHandler(r *gin.Engine, patterns *fs.PatternsEntity) (ret *PatternsHandler) {
ret = &PatternsHandler{
StorageHandler: NewStorageHandler[db.Pattern](r, "patterns", patterns), patterns: patterns}
r.GET("/patterns/:name", ret.GetPattern)
StorageHandler: NewStorageHandler[fs.Pattern](r, "patterns", patterns), patterns: patterns}
// TODO: Add custom, replacement routes here
//r.GET("/patterns/:name", ret.Get)
return
}
// GetPattern handles the GET /patterns/:name route
func (h *PatternsHandler) GetPattern(c *gin.Context) {
// Get handles the GET /patterns/:name route
func (h *PatternsHandler) Get(c *gin.Context) {
name := c.Param("name")
variables := make(map[string]string) // Assuming variables are passed somehow
pattern, err := h.patterns.GetApplyVariables(name, variables)

View File

@ -1,11 +1,11 @@
package restapi
import (
"github.com/danielmiessler/fabric/db"
"github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin"
)
func Serve(fabricDb *db.Db, address string) (err error) {
func Serve(fabricDb *fs.Db, address string) (err error) {
r := gin.Default()
// Middleware

View File

@ -1,19 +1,19 @@
package restapi
import (
"github.com/danielmiessler/fabric/db"
"github.com/danielmiessler/fabric/db/fs"
"github.com/gin-gonic/gin"
)
// SessionsHandler defines the handler for sessions-related operations
type SessionsHandler struct {
*StorageHandler[db.Session]
sessions *db.SessionsEntity
*StorageHandler[fs.Session]
sessions *fs.SessionsEntity
}
// NewSessionsHandler creates a new SessionsHandler
func NewSessionsHandler(r *gin.Engine, sessions *db.SessionsEntity) (ret *SessionsHandler) {
func NewSessionsHandler(r *gin.Engine, sessions *fs.SessionsEntity) (ret *SessionsHandler) {
ret = &SessionsHandler{
StorageHandler: NewStorageHandler[db.Session](r, "sessions", sessions), sessions: sessions}
StorageHandler: NewStorageHandler[fs.Session](r, "sessions", sessions), sessions: sessions}
return ret
}