diff --git a/neo/command/command.go b/neo/command/command.go index efd695a2..b3e7ba75 100644 --- a/neo/command/command.go +++ b/neo/command/command.go @@ -58,11 +58,18 @@ func Exit(sid string) error { if DefaultStore == nil { return fmt.Errorf("command store is not set") } - DefaultStore.DelRequest(sid) return nil } +// GetCommands get all commands +func GetCommands() ([]driver.Command, error) { + if DefaultStore == nil { + return nil, fmt.Errorf("command store is not set") + } + return DefaultStore.GetCommands() +} + // save the command to the store func (cmd *Command) save() error { if DefaultStore == nil { @@ -81,6 +88,7 @@ func (cmd *Command) save() error { data := driver.Command{ ID: cmd.ID, + Name: cmd.Name, Use: cmd.Use, Description: cmd.Description, Args: args, diff --git a/neo/command/driver/memory.go b/neo/command/driver/memory.go index 0cf16800..ff808144 100644 --- a/neo/command/driver/memory.go +++ b/neo/command/driver/memory.go @@ -2,6 +2,7 @@ package driver import ( "fmt" + "strings" "sync" jsoniter "github.com/json-iterator/go" @@ -172,6 +173,26 @@ func (driver *Memory) DelRequest(sid string) { requests.Delete(sid) } +// GetCommands get all commands +func (driver *Memory) GetCommands() ([]Command, error) { + resulets := []Command{} + commands.Range(func(key, value interface{}) bool { + + if strings.HasPrefix(key.(string), "[Index]") { + return true + } + + cmd, ok := value.(Command) + if !ok { + return true + } + resulets = append(resulets, cmd) + return true + }) + + return resulets, nil +} + // NewAI create a new AI func (driver *Memory) newAI() (aigc.AI, error) { diff --git a/neo/command/types.go b/neo/command/types.go index e14dc08e..05b36c6a 100644 --- a/neo/command/types.go +++ b/neo/command/types.go @@ -84,4 +84,5 @@ type Store interface { SetRequest(sid, id, cid string) error GetRequest(sid string) (string, string, bool) DelRequest(sid string) + GetCommands() ([]driver.Command, error) } diff --git a/neo/neo.go b/neo/neo.go index fbe4d13d..3f251a32 100644 --- a/neo/neo.go +++ b/neo/neo.go @@ -78,6 +78,20 @@ func (neo *DSL) API(router *gin.Engine, path string) error { c.Done() }) + // api router chat commands + router.GET(path+"/commands", func(c *gin.Context) { + + commands, err := command.GetCommands() + if err != nil { + c.JSON(500, gin.H{"message": err.Error(), "code": 500}) + c.Done() + return + } + + c.JSON(200, commands) + c.Done() + }) + // api router exit command mode router.POST(path, func(c *gin.Context) { sid := c.GetString("__sid")