Skip to content

Commit

Permalink
Merge pull request #403 from trheyi/main
Browse files Browse the repository at this point in the history
[add] Neo studio api
  • Loading branch information
trheyi committed May 8, 2023
2 parents 569108e + 5223805 commit f345f41
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 12 deletions.
16 changes: 9 additions & 7 deletions engine/load.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package engine

import (
"encoding/json"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -285,23 +284,26 @@ func loadApp(root string) error {
application.Load(app)
}

var info []byte

var appData []byte
var appFile string
// Read app setting
if has, _ := application.App.Exists("app.yao"); has {
info, err = application.App.Read("app.yao")
appFile = "app.yao"
appData, err = application.App.Read("app.yao")
if err != nil {
return err
}

} else if has, _ := application.App.Exists("app.jsonc"); has {
info, err = application.App.Read("app.jsonc")
appFile = "app.jsonc"
appData, err = application.App.Read("app.jsonc")
if err != nil {
return err
}

} else if has, _ := application.App.Exists("app.json"); has {
info, err = application.App.Read("app.json")
appFile = "app.json"
appData, err = application.App.Read("app.json")
if err != nil {
return err
}
Expand All @@ -310,7 +312,7 @@ func loadApp(root string) error {
}

share.App = share.AppInfo{}
return json.Unmarshal(info, &share.App)
return application.Parse(appFile, appData, &share.App)
}

func printErr(mode, widget string, err error) {
Expand Down
1 change: 0 additions & 1 deletion neo/command/driver/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func (driver *Memory) Match(query query.Param, content string) (string, error) {
"content": content,
})

prompts = append([]aigc.Prompt{}, driver.prompts...)
res, ex := driver.ai.ChatCompletions(messages, nil, nil)
if ex != nil {
return "", fmt.Errorf(ex.Message)
Expand Down
57 changes: 53 additions & 4 deletions neo/command/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/process"
v8 "github.com/yaoapp/gou/runtime/v8"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/kun/utils"
"github.com/yaoapp/yao/neo/conversation"
"github.com/yaoapp/yao/neo/message"
"rogchap.com/v8go"
Expand All @@ -30,16 +30,57 @@ func (req *Request) Run(messages []map[string]interface{}, cb func(msg *message.
return nil
}

// Send the command to the service
if req.Command.Optional.Confirm != "" {
req.confirm(args, cb)
return nil
}

utils.Dump(args)
// Execute the command by script
if strings.HasPrefix(req.Command.Process, "scripts.") || strings.HasPrefix(req.Command.Process, "studio.") {

res, err := req.runScript(req.Command.Process, args, cb)
if err != nil {
cb(req.msg().Text("\n\n" + err.Error()))
return err
}

msg := req.msg().Bind(res)
if req.Actions != nil && len(req.Actions) > 0 {
for _, action := range req.Actions {
msg.Action(action.Name, action.Type, action.Payload, action.Next)
}
}

cb(msg.Done())
return nil
}

// Other process
p, err := process.Of(req.Command.Process, args...)
if err != nil {
return err
}

res, err := p.Exec()
if err != nil {
cb(req.msg().Text("\n\n" + err.Error()))
return err
}

msg := req.msg()
if data, ok := res.(map[string]interface{}); ok {
msg = msg.Bind(data)
}

if req.Actions != nil && len(req.Actions) > 0 {
for _, action := range req.Actions {
msg.Action(action.Name, action.Type, action.Payload, action.Next)
}
}

// DONE
cb(req.msg().Done())

return nil
}

Expand Down Expand Up @@ -280,7 +321,15 @@ func (req *Request) runScript(id string, args []interface{}, cb func(msg *messag
namer := strings.Split(id, ".")
method := namer[len(namer)-1]
scriptID := strings.Join(namer[1:len(namer)-1], ".")
script, err := v8.Select(scriptID)

var err error
var script *v8.Script
if namer[0] == "scripts" {
script, err = v8.Select(scriptID)
} else if namer[0] == "studio" {
script, err = v8.SelectRoot(scriptID)
}

if err != nil {
return nil, err
}
Expand Down
25 changes: 25 additions & 0 deletions neo/message/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"strings"

jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/gou/helper"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/yao/openai"
)

Expand Down Expand Up @@ -61,7 +63,15 @@ func (json *JSON) String() string {

// Text set the text
func (json *JSON) Text(text string) *JSON {

json.Message.Text = text
if json.Message.Data != nil {
replaced := helper.Bind(text, json.Message.Data)
if replacedText, ok := replaced.(string); ok {
json.Message.Text = replacedText
}
}

return json
}

Expand Down Expand Up @@ -89,6 +99,11 @@ func (json *JSON) Command(name, id, request string) *JSON {

// Action set the action
func (json *JSON) Action(name string, t string, payload interface{}, next string) *JSON {

if json.Message.Data != nil {
payload = helper.Bind(payload, json.Message.Data)
}

json.Message.Actions = append(json.Message.Actions, Action{
Name: name,
Type: t,
Expand All @@ -98,6 +113,16 @@ func (json *JSON) Action(name string, t string, payload interface{}, next string
return json
}

// Bind replace with data
func (json *JSON) Bind(data map[string]interface{}) *JSON {
if data == nil {
return json
}

json.Message.Data = maps.Of(data).Dot()
return json
}

// IsDone check if the message is done
func (json *JSON) IsDone() bool {
return json.Message.Done
Expand Down
1 change: 1 addition & 0 deletions neo/message/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Message struct {
Confirm bool `json:"confirm,omitempty"`
Command *Command `json:"command,omitempty"`
Actions []Action `json:"actions,omitempty"`
Data map[string]interface{}
}

// Action the action
Expand Down
18 changes: 18 additions & 0 deletions studio/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ func hdCORS(c *gin.Context) {
func hdAuth(c *gin.Context) {

tokenString := c.Request.Header.Get("Authorization")

// Get token from query
if tokenString == "" {

// Temporary solution (will be removed in the future)
tokenString = strings.TrimSpace(strings.TrimPrefix(c.Query("token"), "Bearer "))
if tokenString == "" {
c.JSON(403, gin.H{"code": 403, "message": "No permission"})
c.Abort()
return
}

claims := helper.JwtValidate(tokenString, []byte(config.Conf.JWTSecret))
c.Set("__sid", claims.SID)
c.Next()
return
}

if strings.HasPrefix(tokenString, "Bearer") {
tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer "))
if tokenString == "" {
Expand Down
7 changes: 7 additions & 0 deletions studio/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
v8 "github.com/yaoapp/gou/runtime/v8"
"github.com/yaoapp/yao/neo"
)

var regExcp = regexp.MustCompile("^Exception\\|([0-9]+):(.+)$")
Expand Down Expand Up @@ -236,6 +237,12 @@ func setRouter(router *gin.Engine) {
c.JSON(200, res)
c.Done()
})

// Neo API for studio
if neo.Neo != nil {
neo.Neo.API(router, "/neo")
}

}

func throw(c *gin.Context, code int, message string) {
Expand Down

0 comments on commit f345f41

Please sign in to comment.