From 0e6d48efccbd832e224a5cbcc8297daa8bde9143 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 4 May 2023 02:30:29 +0800 Subject: [PATCH] [change] optimize code structure --- neo/command/command.go | 3 ++- neo/command/driver/memory.go | 3 ++- neo/command/driver/memory_test.go | 5 +++-- neo/command/driver/types.go | 6 ------ neo/command/{driver => query}/query.go | 14 ++++++++++---- neo/command/types.go | 3 ++- neo/neo.go | 4 ++-- 7 files changed, 21 insertions(+), 17 deletions(-) rename neo/command/{driver => query}/query.go (72%) diff --git a/neo/command/command.go b/neo/command/command.go index 59cafc85..eee56a91 100644 --- a/neo/command/command.go +++ b/neo/command/command.go @@ -6,6 +6,7 @@ import ( "github.com/yaoapp/gou/connector" "github.com/yaoapp/yao/aigc" "github.com/yaoapp/yao/neo/command/driver" + "github.com/yaoapp/yao/neo/command/query" "github.com/yaoapp/yao/openai" ) @@ -18,7 +19,7 @@ func SetStore(store Store) { } // Match the command from the content -func Match(sid string, query driver.Query, input string) (string, error) { +func Match(sid string, query query.Param, input string) (string, error) { if DefaultStore == nil { return "", fmt.Errorf("command store is not set") diff --git a/neo/command/driver/memory.go b/neo/command/driver/memory.go index cb0741e2..17f0e01c 100644 --- a/neo/command/driver/memory.go +++ b/neo/command/driver/memory.go @@ -7,6 +7,7 @@ import ( jsoniter "github.com/json-iterator/go" "github.com/yaoapp/gou/connector" "github.com/yaoapp/yao/aigc" + "github.com/yaoapp/yao/neo/command/query" "github.com/yaoapp/yao/openai" ) @@ -47,7 +48,7 @@ func NewMemory(model string, prompts []aigc.Prompt) (*Memory, error) { } // Match match the command data -func (driver *Memory) Match(query Query, content string) (string, error) { +func (driver *Memory) Match(query query.Param, content string) (string, error) { prompts := append([]aigc.Prompt{}, driver.prompts...) has := false commands.Range(func(key, value interface{}) bool { diff --git a/neo/command/driver/memory_test.go b/neo/command/driver/memory_test.go index ec57e053..3aedd1d2 100644 --- a/neo/command/driver/memory_test.go +++ b/neo/command/driver/memory_test.go @@ -5,6 +5,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/neo/command/query" "github.com/yaoapp/yao/test" ) @@ -51,13 +52,13 @@ func TestMemoryMatch(t *testing.T) { defer test.Clean() mem := prepare(t) - id, err := mem.Match(Query{Stack: "Table.Page.pet"}, "Generate table test data") + id, err := mem.Match(query.Param{Stack: "Table.Page.pet"}, "Generate table test data") if err != nil { t.Fatal(err) } assert.Equal(t, "table.data", id) - id, err = mem.Match(Query{Stack: "Form.Page.pet", Path: "/Form/pet"}, "Generate table test data") + id, err = mem.Match(query.Param{Stack: "Form.Page.pet", Path: "/Form/pet"}, "Generate table test data") assert.ErrorContains(t, err, "no related command found") } diff --git a/neo/command/driver/types.go b/neo/command/driver/types.go index af343430..b0c19feb 100644 --- a/neo/command/driver/types.go +++ b/neo/command/driver/types.go @@ -16,9 +16,3 @@ type Command struct { Stack string `json:"stack,omitempty"` Path string `json:"path,omitempty"` } - -// Query the query struct -type Query struct { - Stack string `json:"stack,omitempty"` - Path string `json:"path,omitempty"` -} diff --git a/neo/command/driver/query.go b/neo/command/query/query.go similarity index 72% rename from neo/command/driver/query.go rename to neo/command/query/query.go index 039c738d..7af58b10 100644 --- a/neo/command/driver/query.go +++ b/neo/command/query/query.go @@ -1,12 +1,18 @@ -package driver +package query import ( "regexp" "strings" ) +// Param the command param +type Param struct { + Stack string `json:"stack,omitempty"` + Path string `json:"path,omitempty"` +} + // MatchStack match the stack -func (query Query) MatchStack(stack string) bool { +func (query Param) MatchStack(stack string) bool { if stack == "" || stack == "*" || query.Stack == "" { return true @@ -21,7 +27,7 @@ func (query Query) MatchStack(stack string) bool { } // MatchPath match the path -func (query Query) MatchPath(path string) bool { +func (query Param) MatchPath(path string) bool { if path == "" || path == "*" || query.Path == "" { return true } @@ -35,7 +41,7 @@ func (query Query) MatchPath(path string) bool { } // MatchAny match the stack or path -func (query Query) MatchAny(stack, path string) bool { +func (query Param) MatchAny(stack, path string) bool { if path == "" || path == "-" { return query.MatchStack(stack) diff --git a/neo/command/types.go b/neo/command/types.go index 1a415430..85da3a24 100644 --- a/neo/command/types.go +++ b/neo/command/types.go @@ -5,6 +5,7 @@ import ( "github.com/yaoapp/yao/aigc" "github.com/yaoapp/yao/neo/command/driver" + "github.com/yaoapp/yao/neo/command/query" ) // Request the command request @@ -71,7 +72,7 @@ type Context struct { // Store the command driver type Store interface { - Match(query driver.Query, content string) (string, error) + Match(query query.Param, content string) (string, error) Set(id string, cmd driver.Command) error Get(id string) (driver.Command, bool) Del(id string) diff --git a/neo/neo.go b/neo/neo.go index ff12bd62..84064219 100644 --- a/neo/neo.go +++ b/neo/neo.go @@ -15,7 +15,7 @@ import ( "github.com/yaoapp/kun/log" "github.com/yaoapp/yao/helper" "github.com/yaoapp/yao/neo/command" - "github.com/yaoapp/yao/neo/command/driver" + "github.com/yaoapp/yao/neo/command/query" "github.com/yaoapp/yao/neo/conversation" "github.com/yaoapp/yao/openai" ) @@ -91,7 +91,7 @@ func (neo *DSL) Answer(ctx command.Context, answer Answer, messages []map[string var cmd *command.Command var isCommand = false input := messages[len(messages)-1]["content"].(string) - name, err := command.Match(ctx.Sid, driver.Query{Stack: ctx.Stack, Path: ctx.Path}, input) + name, err := command.Match(ctx.Sid, query.Param{Stack: ctx.Stack, Path: ctx.Path}, input) if err == nil && name != "" { cmd, isCommand = command.Commands[name] }