Merge pull request #874 from trheyi/main

Improve error handling and logging in assistant methods
This commit is contained in:
Max 2025-02-24 10:45:36 +08:00 committed by GitHub
commit 71f71570b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 6 deletions

View file

@ -138,7 +138,7 @@ func jsCall(info *v8go.FunctionCallbackInfo) *v8go.Value {
chatContext.ChatID = fmt.Sprintf("chat_%s", uuid.New().String()) // New chat id
chatContext.Silent = true // Silent mode
var cb func(msg *chatMessage.Message)
var cb func(msg *chatMessage.Message) = nil
if len(args) > 2 {
// Parse the callback

View file

@ -64,29 +64,29 @@ func SubscribeFn(plan_id string, key string, value interface{}, source bool, met
// Data
plan, err := v8plan.GetPlan(plan_id)
if err != nil {
color.Red("Failed to get the plan: %s", err.Error())
color.Red("Subscribe Failed to get the plan: %s", err.Error())
return
}
global, ok := plan.Data().(*GlobalVariables)
if !ok {
color.Red("plan data is not a GlobalVariables")
color.Red("Subscribe Failed: plan data is not a GlobalVariables")
return
}
if global.Assistant == nil {
color.Red("assistant is not set")
color.Red("Subscribe Failed: assistant is not set")
return
}
if global.Assistant.Script == nil {
color.Red("script is not set")
color.Red("Subscribe Failed: script is not set")
return
}
scriptCtx, err := global.Assistant.Script.NewContext(global.ChatContext.Sid, nil)
if err != nil {
color.Red("Failed to create the script context: %s", err.Error())
color.Red("Subscribe Failed: Failed to create the script context: %s", err.Error())
return
}
defer scriptCtx.Close()

View file

@ -698,10 +698,16 @@ func (m *Message) Callback(fn interface{}) *Message {
if fn != nil {
switch v := fn.(type) {
case func(msg *Message):
if v == nil {
break
}
v(m)
break
case func():
if v == nil {
break
}
v()
break
@ -717,8 +723,18 @@ func (m *Message) Callback(fn interface{}) *Message {
func (m *Message) Write(w gin.ResponseWriter) bool {
defer func() {
if r := recover(); r != nil {
// Ignore if done is true
if m.IsDone {
return
}
message := "Write Response Exception: (if client close the connection, it's normal) \n %s\n\n"
color.Red(message, r)
// Print the message
raw, _ := jsoniter.MarshalToString(m)
color.White("Message:\n %s", raw)
}
}()