Skip to content

Commit

Permalink
Merge pull request #822 from trheyi/main
Browse files Browse the repository at this point in the history
Refactor call method in Assistant to streamline context handling
  • Loading branch information
trheyi authored Jan 18, 2025
2 parents 883dd07 + 5b30ef5 commit 1d9d9b2
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions neo/assistant/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1d9d9b2

Please sign in to comment.