commit
f345f41865
7 changed files with 113 additions and 12 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
package engine
|
package engine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -285,23 +284,26 @@ func loadApp(root string) error {
|
||||||
application.Load(app)
|
application.Load(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
var info []byte
|
var appData []byte
|
||||||
|
var appFile string
|
||||||
// Read app setting
|
// Read app setting
|
||||||
if has, _ := application.App.Exists("app.yao"); has {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if has, _ := application.App.Exists("app.jsonc"); has {
|
} 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if has, _ := application.App.Exists("app.json"); has {
|
} 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -310,7 +312,7 @@ func loadApp(root string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
share.App = share.AppInfo{}
|
share.App = share.AppInfo{}
|
||||||
return json.Unmarshal(info, &share.App)
|
return application.Parse(appFile, appData, &share.App)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printErr(mode, widget string, err error) {
|
func printErr(mode, widget string, err error) {
|
||||||
|
|
|
||||||
|
|
@ -92,7 +92,6 @@ func (driver *Memory) Match(query query.Param, content string) (string, error) {
|
||||||
"content": content,
|
"content": content,
|
||||||
})
|
})
|
||||||
|
|
||||||
prompts = append([]aigc.Prompt{}, driver.prompts...)
|
|
||||||
res, ex := driver.ai.ChatCompletions(messages, nil, nil)
|
res, ex := driver.ai.ChatCompletions(messages, nil, nil)
|
||||||
if ex != nil {
|
if ex != nil {
|
||||||
return "", fmt.Errorf(ex.Message)
|
return "", fmt.Errorf(ex.Message)
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,10 @@ import (
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/yaoapp/gou/process"
|
||||||
v8 "github.com/yaoapp/gou/runtime/v8"
|
v8 "github.com/yaoapp/gou/runtime/v8"
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
"github.com/yaoapp/kun/maps"
|
"github.com/yaoapp/kun/maps"
|
||||||
"github.com/yaoapp/kun/utils"
|
|
||||||
"github.com/yaoapp/yao/neo/conversation"
|
"github.com/yaoapp/yao/neo/conversation"
|
||||||
"github.com/yaoapp/yao/neo/message"
|
"github.com/yaoapp/yao/neo/message"
|
||||||
"rogchap.com/v8go"
|
"rogchap.com/v8go"
|
||||||
|
|
@ -30,16 +30,57 @@ func (req *Request) Run(messages []map[string]interface{}, cb func(msg *message.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send the command to the service
|
||||||
if req.Command.Optional.Confirm != "" {
|
if req.Command.Optional.Confirm != "" {
|
||||||
req.confirm(args, cb)
|
req.confirm(args, cb)
|
||||||
return nil
|
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
|
// DONE
|
||||||
cb(req.msg().Done())
|
cb(req.msg().Done())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -280,7 +321,15 @@ func (req *Request) runScript(id string, args []interface{}, cb func(msg *messag
|
||||||
namer := strings.Split(id, ".")
|
namer := strings.Split(id, ".")
|
||||||
method := namer[len(namer)-1]
|
method := namer[len(namer)-1]
|
||||||
scriptID := strings.Join(namer[1: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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/yaoapp/gou/helper"
|
||||||
"github.com/yaoapp/kun/log"
|
"github.com/yaoapp/kun/log"
|
||||||
|
"github.com/yaoapp/kun/maps"
|
||||||
"github.com/yaoapp/yao/openai"
|
"github.com/yaoapp/yao/openai"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -61,7 +63,15 @@ func (json *JSON) String() string {
|
||||||
|
|
||||||
// Text set the text
|
// Text set the text
|
||||||
func (json *JSON) Text(text string) *JSON {
|
func (json *JSON) Text(text string) *JSON {
|
||||||
|
|
||||||
json.Message.Text = text
|
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
|
return json
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,6 +99,11 @@ func (json *JSON) Command(name, id, request string) *JSON {
|
||||||
|
|
||||||
// Action set the action
|
// Action set the action
|
||||||
func (json *JSON) Action(name string, t string, payload interface{}, next string) *JSON {
|
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{
|
json.Message.Actions = append(json.Message.Actions, Action{
|
||||||
Name: name,
|
Name: name,
|
||||||
Type: t,
|
Type: t,
|
||||||
|
|
@ -98,6 +113,16 @@ func (json *JSON) Action(name string, t string, payload interface{}, next string
|
||||||
return json
|
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
|
// IsDone check if the message is done
|
||||||
func (json *JSON) IsDone() bool {
|
func (json *JSON) IsDone() bool {
|
||||||
return json.Message.Done
|
return json.Message.Done
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ type Message struct {
|
||||||
Confirm bool `json:"confirm,omitempty"`
|
Confirm bool `json:"confirm,omitempty"`
|
||||||
Command *Command `json:"command,omitempty"`
|
Command *Command `json:"command,omitempty"`
|
||||||
Actions []Action `json:"actions,omitempty"`
|
Actions []Action `json:"actions,omitempty"`
|
||||||
|
Data map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Action the action
|
// Action the action
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,24 @@ func hdCORS(c *gin.Context) {
|
||||||
func hdAuth(c *gin.Context) {
|
func hdAuth(c *gin.Context) {
|
||||||
|
|
||||||
tokenString := c.Request.Header.Get("Authorization")
|
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") {
|
if strings.HasPrefix(tokenString, "Bearer") {
|
||||||
tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer "))
|
tokenString = strings.TrimSpace(strings.TrimPrefix(tokenString, "Bearer "))
|
||||||
if tokenString == "" {
|
if tokenString == "" {
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
v8 "github.com/yaoapp/gou/runtime/v8"
|
v8 "github.com/yaoapp/gou/runtime/v8"
|
||||||
|
"github.com/yaoapp/yao/neo"
|
||||||
)
|
)
|
||||||
|
|
||||||
var regExcp = regexp.MustCompile("^Exception\\|([0-9]+):(.+)$")
|
var regExcp = regexp.MustCompile("^Exception\\|([0-9]+):(.+)$")
|
||||||
|
|
@ -236,6 +237,12 @@ func setRouter(router *gin.Engine) {
|
||||||
c.JSON(200, res)
|
c.JSON(200, res)
|
||||||
c.Done()
|
c.Done()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Neo API for studio
|
||||||
|
if neo.Neo != nil {
|
||||||
|
neo.Neo.API(router, "/neo")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func throw(c *gin.Context, code int, message string) {
|
func throw(c *gin.Context, code int, message string) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue