Add Execute method to Script type for enhanced script execution
This commit is contained in:
parent
9c97035cc6
commit
71698b1405
4 changed files with 101 additions and 0 deletions
|
|
@ -1 +1,20 @@
|
|||
package hook
|
||||
|
||||
import "github.com/yaoapp/yao/agent/context"
|
||||
|
||||
// Execute execute the script
|
||||
func (s *Script) Execute(ctx *context.Context, method string, args ...interface{}) (interface{}, error) {
|
||||
if s == nil || s.Script == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
scriptCtx, err := s.NewContext(ctx.Sid, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer scriptCtx.Close()
|
||||
|
||||
// The first argument is the context
|
||||
args = append([]interface{}{ctx}, args...)
|
||||
return scriptCtx.CallWith(ctx.Context, method, args...)
|
||||
}
|
||||
|
|
|
|||
23
agent/context/jsapi.go
Normal file
23
agent/context/jsapi.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"rogchap.com/v8go"
|
||||
)
|
||||
|
||||
// JsValue return the JavaScript value of the context
|
||||
func (ctx *Context) JsValue(v8ctx *v8go.Context) (*v8go.Value, error) {
|
||||
return ctx.NewObject(v8ctx)
|
||||
}
|
||||
|
||||
// NewObject Create a new JavaScript object from the context
|
||||
func (ctx *Context) NewObject(v8ctx *v8go.Context) (*v8go.Value, error) {
|
||||
jsObject := v8go.NewObjectTemplate(v8ctx.Isolate())
|
||||
jsObject.Set("ChatID", ctx.ChatID)
|
||||
jsObject.Set("AssistantID", ctx.AssistantID)
|
||||
jsObject.Set("Sid", ctx.Sid)
|
||||
instance, err := jsObject.NewInstance(v8ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return instance.Value, nil
|
||||
}
|
||||
58
agent/context/jsapi_test.go
Normal file
58
agent/context/jsapi_test.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package context
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
v8 "github.com/yaoapp/gou/runtime/v8"
|
||||
"github.com/yaoapp/gou/runtime/v8/bridge"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/test"
|
||||
"rogchap.com/v8go"
|
||||
)
|
||||
|
||||
// TestJsValue test the JsValue function
|
||||
func TestJsValue(t *testing.T) {
|
||||
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
|
||||
cxt := &Context{
|
||||
ChatID: "ChatID-123456",
|
||||
AssistantID: "AssistantID-1234",
|
||||
Sid: "Sid-1234",
|
||||
}
|
||||
|
||||
v8.RegisterFunction("testContextJsvalue", testContextJsvalueEmbed)
|
||||
res, err := v8.Call(v8.CallOptions{}, `
|
||||
function test(cxt) {
|
||||
return testContextJsvalue(cxt)
|
||||
}`, cxt)
|
||||
if err != nil {
|
||||
t.Fatalf("Call failed: %v", err)
|
||||
}
|
||||
assert.Equal(t, "ChatID-123456", res)
|
||||
}
|
||||
|
||||
func testContextJsvalueEmbed(iso *v8go.Isolate) *v8go.FunctionTemplate {
|
||||
return v8go.NewFunctionTemplate(iso, testContextJsvalueFunction)
|
||||
}
|
||||
|
||||
func testContextJsvalueFunction(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||
var args = info.Args()
|
||||
if len(args) < 1 {
|
||||
return bridge.JsException(info.Context(), "Missing parameters")
|
||||
}
|
||||
|
||||
ctx, err := args[0].AsObject()
|
||||
if err != nil {
|
||||
return bridge.JsException(info.Context(), err)
|
||||
}
|
||||
|
||||
chatID, err := ctx.Get("ChatID")
|
||||
if err != nil {
|
||||
return bridge.JsException(info.Context(), err)
|
||||
}
|
||||
|
||||
return chatID
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
package jsapi
|
||||
|
||||
// JSAPI Register the JavaScript API
|
||||
// Agent API will be registered as a third party object
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue