[add] Neo Get all commands api
This commit is contained in:
parent
4bb5fca430
commit
9a37b1c72b
4 changed files with 45 additions and 1 deletions
|
|
@ -58,11 +58,18 @@ func Exit(sid string) error {
|
||||||
if DefaultStore == nil {
|
if DefaultStore == nil {
|
||||||
return fmt.Errorf("command store is not set")
|
return fmt.Errorf("command store is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultStore.DelRequest(sid)
|
DefaultStore.DelRequest(sid)
|
||||||
return nil
|
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
|
// save the command to the store
|
||||||
func (cmd *Command) save() error {
|
func (cmd *Command) save() error {
|
||||||
if DefaultStore == nil {
|
if DefaultStore == nil {
|
||||||
|
|
@ -81,6 +88,7 @@ func (cmd *Command) save() error {
|
||||||
|
|
||||||
data := driver.Command{
|
data := driver.Command{
|
||||||
ID: cmd.ID,
|
ID: cmd.ID,
|
||||||
|
Name: cmd.Name,
|
||||||
Use: cmd.Use,
|
Use: cmd.Use,
|
||||||
Description: cmd.Description,
|
Description: cmd.Description,
|
||||||
Args: args,
|
Args: args,
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package driver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
|
@ -172,6 +173,26 @@ func (driver *Memory) DelRequest(sid string) {
|
||||||
requests.Delete(sid)
|
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
|
// NewAI create a new AI
|
||||||
func (driver *Memory) newAI() (aigc.AI, error) {
|
func (driver *Memory) newAI() (aigc.AI, error) {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,4 +84,5 @@ type Store interface {
|
||||||
SetRequest(sid, id, cid string) error
|
SetRequest(sid, id, cid string) error
|
||||||
GetRequest(sid string) (string, string, bool)
|
GetRequest(sid string) (string, string, bool)
|
||||||
DelRequest(sid string)
|
DelRequest(sid string)
|
||||||
|
GetCommands() ([]driver.Command, error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
neo/neo.go
14
neo/neo.go
|
|
@ -78,6 +78,20 @@ func (neo *DSL) API(router *gin.Engine, path string) error {
|
||||||
c.Done()
|
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
|
// api router exit command mode
|
||||||
router.POST(path, func(c *gin.Context) {
|
router.POST(path, func(c *gin.Context) {
|
||||||
sid := c.GetString("__sid")
|
sid := c.GetString("__sid")
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue