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.
This commit is contained in:
Max 2025-01-18 15:30:08 +08:00
parent 50faf7ed77
commit 5b30ef5fde

View file

@ -206,23 +206,10 @@ func (ast *Assistant) call(ctx context.Context, method string, context chatctx.C
return nil, fmt.Errorf(HookErrorMethodNotFound) return nil, fmt.Errorf(HookErrorMethodNotFound)
} }
// Create done channel for handling cancellation // Call the method directly in the current thread
done := make(chan struct{}) args = append([]interface{}{context.Map()}, args...)
var result interface{} if scriptCtx != nil {
var callErr error return scriptCtx.CallWith(ctx, method, args...)
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
} }
return nil, nil
} }