From 2a04074a82f9d80b146e4935464d984d914742cd Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 4 May 2023 05:31:01 +0800 Subject: [PATCH] [add] Neo Exit Command Mode & History API --- neo/command/command.go | 10 +++++++ neo/neo.go | 63 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/neo/command/command.go b/neo/command/command.go index eee56a91..1b503aef 100644 --- a/neo/command/command.go +++ b/neo/command/command.go @@ -34,6 +34,16 @@ func Match(sid string, query query.Param, input string) (string, error) { return DefaultStore.Match(query, input) } +// Exit the command +func Exit(sid string) error { + if DefaultStore == nil { + return fmt.Errorf("command store is not set") + } + + DefaultStore.DelRequest(sid) + return nil +} + // save the command to the store func (cmd *Command) save() error { if DefaultStore == nil { diff --git a/neo/neo.go b/neo/neo.go index c17970af..90fdd120 100644 --- a/neo/neo.go +++ b/neo/neo.go @@ -41,7 +41,7 @@ func (neo *DSL) API(router *gin.Engine, path string) error { // Cross-Domain neo.crossDomain(router, path) - // api router + // api router chat router.GET(path, func(c *gin.Context) { sid := c.GetString("__sid") @@ -78,6 +78,66 @@ func (neo *DSL) API(router *gin.Engine, path string) error { }) + // api router chat histor + router.GET(path+"/history", func(c *gin.Context) { + sid := c.GetString("__sid") + if sid == "" { + c.JSON(400, gin.H{"message": "sid is required", "code": 400}) + c.Done() + return + } + + history, err := neo.Conversation.GetHistory(sid) + if err != nil { + c.JSON(500, gin.H{"message": err.Error(), "code": 500}) + c.Done() + return + } + + c.JSON(200, history) + c.Done() + }) + + // api router exit command mode + router.POST(path, func(c *gin.Context) { + sid := c.GetString("__sid") + if sid == "" { + c.JSON(400, gin.H{"message": "sid is required", "code": 400}) + c.Done() + return + } + + var payload map[string]interface{} + err := c.ShouldBindJSON(&payload) + if err != nil { + c.JSON(400, gin.H{"message": err.Error(), "code": 400}) + c.Done() + return + } + + cmd, ok := payload["cmd"].(string) + if !ok { + c.JSON(400, gin.H{"message": "command is required", "code": 400}) + c.Done() + return + } + + switch cmd { + case "ExitCommandMode": + err := command.Exit(sid) + if err != nil { + c.JSON(500, gin.H{"message": err.Error(), "code": 500}) + c.Done() + return + } + c.JSON(200, gin.H{"message": "success", "code": 200}) + c.Done() + + default: + c.JSON(400, gin.H{"message": "command is not supported", "code": 400}) + } + }) + return nil } @@ -168,6 +228,7 @@ func (neo *DSL) Answer(ctx command.Context, answer Answer, messages []map[string return nil } +// matchCommand match the command func (neo *DSL) matchCommand(ctx command.Context, messages []map[string]interface{}) (*command.Command, bool) { if len(messages) < 1 { return nil, false