Merge pull request #407 from trheyi/main

[add] Neo Get all commands  api
This commit is contained in:
Max 2023-05-12 13:42:37 +08:00 committed by GitHub
commit f6ca56d579
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 1 deletions

View file

@ -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,

View file

@ -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) {

View file

@ -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)
}

View file

@ -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")