From 50faf7ed7779b8287428b0212997afb37a4773a3 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 18 Jan 2025 15:14:27 +0800 Subject: [PATCH 1/2] Refactor context handling in Assistant's call method - Removed the forced closure of the script context upon context cancellation in the call method, improving the handling of script execution and cancellation scenarios. - This change enhances the robustness of the Neo API assistant by preventing potential issues related to premature context termination, paving the way for more reliable assistant functionalities. --- neo/assistant/hooks.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/neo/assistant/hooks.go b/neo/assistant/hooks.go index 3ab7d434..6a801c94 100644 --- a/neo/assistant/hooks.go +++ b/neo/assistant/hooks.go @@ -221,9 +221,6 @@ func (ast *Assistant) call(ctx context.Context, method string, context chatctx.C // Wait for either context cancellation or method completion select { case <-ctx.Done(): - if scriptCtx != nil { - scriptCtx.Close() // Force close the script context - } return nil, ctx.Err() case <-done: return result, callErr From 5b30ef5fdec20e05c9d0c2967b053d6449b70fa9 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 18 Jan 2025 15:30:08 +0800 Subject: [PATCH 2/2] Refactor call method in Assistant to streamline context handling - Removed the asynchronous execution of method calls, allowing for direct invocation within the current thread. - Simplified cancellation handling by eliminating the done channel, improving clarity and reducing complexity in the call method. - Enhanced error handling by ensuring the script context is checked before method invocation. These changes improve the maintainability and robustness of the Neo API assistant, paving the way for more efficient method execution and context management. --- neo/assistant/hooks.go | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/neo/assistant/hooks.go b/neo/assistant/hooks.go index 6a801c94..bf21fa25 100644 --- a/neo/assistant/hooks.go +++ b/neo/assistant/hooks.go @@ -206,23 +206,10 @@ func (ast *Assistant) call(ctx context.Context, method string, context chatctx.C return nil, fmt.Errorf(HookErrorMethodNotFound) } - // Create done channel for handling cancellation - done := make(chan struct{}) - var result interface{} - var callErr error - - go func() { - defer close(done) - // Call the method - args = append([]interface{}{context.Map()}, args...) - result, callErr = scriptCtx.Call(method, args...) - }() - - // Wait for either context cancellation or method completion - select { - case <-ctx.Done(): - return nil, ctx.Err() - case <-done: - return result, callErr + // Call the method directly in the current thread + args = append([]interface{}{context.Map()}, args...) + if scriptCtx != nil { + return scriptCtx.CallWith(ctx, method, args...) } + return nil, nil }