[change] optimize code structure

This commit is contained in:
Max 2023-05-04 02:30:29 +08:00
parent 7b8459b688
commit 0e6d48efcc
7 changed files with 21 additions and 17 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/yaoapp/gou/connector" "github.com/yaoapp/gou/connector"
"github.com/yaoapp/yao/aigc" "github.com/yaoapp/yao/aigc"
"github.com/yaoapp/yao/neo/command/driver" "github.com/yaoapp/yao/neo/command/driver"
"github.com/yaoapp/yao/neo/command/query"
"github.com/yaoapp/yao/openai" "github.com/yaoapp/yao/openai"
) )
@ -18,7 +19,7 @@ func SetStore(store Store) {
} }
// Match the command from the content // 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 { if DefaultStore == nil {
return "", fmt.Errorf("command store is not set") return "", fmt.Errorf("command store is not set")

View file

@ -7,6 +7,7 @@ import (
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/connector" "github.com/yaoapp/gou/connector"
"github.com/yaoapp/yao/aigc" "github.com/yaoapp/yao/aigc"
"github.com/yaoapp/yao/neo/command/query"
"github.com/yaoapp/yao/openai" "github.com/yaoapp/yao/openai"
) )
@ -47,7 +48,7 @@ func NewMemory(model string, prompts []aigc.Prompt) (*Memory, error) {
} }
// Match match the command data // 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...) prompts := append([]aigc.Prompt{}, driver.prompts...)
has := false has := false
commands.Range(func(key, value interface{}) bool { commands.Range(func(key, value interface{}) bool {

View file

@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/neo/command/query"
"github.com/yaoapp/yao/test" "github.com/yaoapp/yao/test"
) )
@ -51,13 +52,13 @@ func TestMemoryMatch(t *testing.T) {
defer test.Clean() defer test.Clean()
mem := prepare(t) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
assert.Equal(t, "table.data", id) 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") assert.ErrorContains(t, err, "no related command found")
} }

View file

@ -16,9 +16,3 @@ type Command struct {
Stack string `json:"stack,omitempty"` Stack string `json:"stack,omitempty"`
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
} }
// Query the query struct
type Query struct {
Stack string `json:"stack,omitempty"`
Path string `json:"path,omitempty"`
}

View file

@ -1,12 +1,18 @@
package driver package query
import ( import (
"regexp" "regexp"
"strings" "strings"
) )
// Param the command param
type Param struct {
Stack string `json:"stack,omitempty"`
Path string `json:"path,omitempty"`
}
// MatchStack match the stack // MatchStack match the stack
func (query Query) MatchStack(stack string) bool { func (query Param) MatchStack(stack string) bool {
if stack == "" || stack == "*" || query.Stack == "" { if stack == "" || stack == "*" || query.Stack == "" {
return true return true
@ -21,7 +27,7 @@ func (query Query) MatchStack(stack string) bool {
} }
// MatchPath match the path // MatchPath match the path
func (query Query) MatchPath(path string) bool { func (query Param) MatchPath(path string) bool {
if path == "" || path == "*" || query.Path == "" { if path == "" || path == "*" || query.Path == "" {
return true return true
} }
@ -35,7 +41,7 @@ func (query Query) MatchPath(path string) bool {
} }
// MatchAny match the stack or path // 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 == "-" { if path == "" || path == "-" {
return query.MatchStack(stack) return query.MatchStack(stack)

View file

@ -5,6 +5,7 @@ import (
"github.com/yaoapp/yao/aigc" "github.com/yaoapp/yao/aigc"
"github.com/yaoapp/yao/neo/command/driver" "github.com/yaoapp/yao/neo/command/driver"
"github.com/yaoapp/yao/neo/command/query"
) )
// Request the command request // Request the command request
@ -71,7 +72,7 @@ type Context struct {
// Store the command driver // Store the command driver
type Store interface { 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 Set(id string, cmd driver.Command) error
Get(id string) (driver.Command, bool) Get(id string) (driver.Command, bool)
Del(id string) Del(id string)

View file

@ -15,7 +15,7 @@ import (
"github.com/yaoapp/kun/log" "github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/helper" "github.com/yaoapp/yao/helper"
"github.com/yaoapp/yao/neo/command" "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/neo/conversation"
"github.com/yaoapp/yao/openai" "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 cmd *command.Command
var isCommand = false var isCommand = false
input := messages[len(messages)-1]["content"].(string) 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 != "" { if err == nil && name != "" {
cmd, isCommand = command.Commands[name] cmd, isCommand = command.Commands[name]
} }