diff --git a/agent/assistant/hook/script.go b/agent/assistant/hook/script.go index 67dcf257..924d2a36 100644 --- a/agent/assistant/hook/script.go +++ b/agent/assistant/hook/script.go @@ -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...) +} diff --git a/agent/context/jsapi.go b/agent/context/jsapi.go new file mode 100644 index 00000000..2eec07d8 --- /dev/null +++ b/agent/context/jsapi.go @@ -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 +} diff --git a/agent/context/jsapi_test.go b/agent/context/jsapi_test.go new file mode 100644 index 00000000..31bf64bc --- /dev/null +++ b/agent/context/jsapi_test.go @@ -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 +} diff --git a/agent/jsapi/jsapi.go b/agent/jsapi/jsapi.go index 4c244c45..6357f99e 100644 --- a/agent/jsapi/jsapi.go +++ b/agent/jsapi/jsapi.go @@ -1,3 +1,4 @@ package jsapi // JSAPI Register the JavaScript API +// Agent API will be registered as a third party object