Merge pull request #822 from trheyi/main

Refactor call method in Assistant to streamline context handling
This commit is contained in:
Max 2025-01-18 15:30:41 +08:00 committed by GitHub
commit 1d9d9b2aa8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -206,26 +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():
if scriptCtx != nil {
scriptCtx.Close() // Force close the script context
}
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
}