Enhance JavaScript bridge argument handling in assistant method calls
- Add support for passing additional arguments to callback functions - Implement flexible argument parsing for JavaScript method calls - Handle both array and single argument scenarios - Update method signature to accommodate optional arguments
This commit is contained in:
parent
5475e0ab39
commit
5fe43f8103
1 changed files with 28 additions and 2 deletions
|
|
@ -186,6 +186,31 @@ func jsCall(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||||
var cb func(msg *chatMessage.Message) = nil
|
var cb func(msg *chatMessage.Message) = nil
|
||||||
if len(args) > 2 {
|
if len(args) > 2 {
|
||||||
|
|
||||||
|
// Rest args
|
||||||
|
var jsArgs *v8go.Value
|
||||||
|
if len(args) > 3 {
|
||||||
|
jsArgs = args[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
goArgs := []interface{}{}
|
||||||
|
if jsArgs.IsArray() {
|
||||||
|
v, err := bridge.GoValue(jsArgs, info.Context())
|
||||||
|
if err != nil {
|
||||||
|
return bridge.JsException(info.Context(), err.Error())
|
||||||
|
}
|
||||||
|
arr, ok := v.([]interface{})
|
||||||
|
if !ok {
|
||||||
|
return bridge.JsException(info.Context(), "Invalid arguments")
|
||||||
|
}
|
||||||
|
goArgs = arr
|
||||||
|
} else {
|
||||||
|
v, err := bridge.GoValue(jsArgs, info.Context())
|
||||||
|
if err != nil {
|
||||||
|
return bridge.JsException(info.Context(), err.Error())
|
||||||
|
}
|
||||||
|
goArgs = []interface{}{v}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse the callback
|
// Parse the callback
|
||||||
funcType := "method"
|
funcType := "method"
|
||||||
name := ""
|
name := ""
|
||||||
|
|
@ -219,6 +244,7 @@ func jsCall(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||||
source := args[2].String()
|
source := args[2].String()
|
||||||
cb = func(msg *chatMessage.Message) {
|
cb = func(msg *chatMessage.Message) {
|
||||||
cbArgs := []interface{}{msg}
|
cbArgs := []interface{}{msg}
|
||||||
|
cbArgs = append(cbArgs, goArgs...)
|
||||||
ctx, err := global.Assistant.Script.NewContext(global.ChatContext.Sid, nil)
|
ctx, err := global.Assistant.Script.NewContext(global.ChatContext.Sid, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Failed to create context", err.Error())
|
fmt.Println("Failed to create context", err.Error())
|
||||||
|
|
@ -283,8 +309,8 @@ func jsCall(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||||
|
|
||||||
// Parse the options
|
// Parse the options
|
||||||
options := map[string]interface{}{}
|
options := map[string]interface{}{}
|
||||||
if len(args) > 3 {
|
if len(args) > 4 {
|
||||||
optionsRaw, err := bridge.GoValue(args[3], info.Context())
|
optionsRaw, err := bridge.GoValue(args[4], info.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return bridge.JsException(info.Context(), err.Error())
|
return bridge.JsException(info.Context(), err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue