@@ -8,6 +8,7 @@ import (
88 "strings"
99 "time"
1010
11+ "github.com/rs/zerolog/log"
1112 "github.com/xonecas/symb/internal/mcp"
1213 "github.com/xonecas/symb/internal/provider"
1314 )
@@ -46,18 +47,38 @@ type ProcessTurnOptions struct {
4647 // streamAndCollect runs one LLM call: streams events, collects the response,
4748 // reports usage, and returns the ChatResponse.
4849 func streamAndCollect(ctx context.Context, opts *ProcessTurnOptions, tools []provider.Tool) (*provider.ChatResponse, error) {
49- stream, err := opts.Provider.ChatStream(ctx, opts.History, tools)
50- if err != nil {
51- return nil, err
52- }
53- resp, err := collectWithDeltas(stream, opts.OnDelta)
54- if err != nil {
55- return nil, err
50+ const maxEmptyRetries = 1
51+
52+ for attempt := 0; attempt <= maxEmptyRetries; attempt++ {
53+ stream, err := opts.Provider.ChatStream(ctx, opts.History, tools)
54+ if err != nil {
55+ return nil, err
56+ }
57+ resp, err := collectWithDeltas(stream, opts.OnDelta)
58+ if err != nil {
59+ return nil, err
60+ }
61+ if opts.OnUsage != nil && (resp.InputTokens > 0 || resp.OutputTokens > 0) {
62+ opts.OnUsage(resp.InputTokens, resp.OutputTokens)
63+ }
64+ if !isEmptyResponse(resp) {
65+ return resp, nil
66+ }
67+
68+ log.Warn().
69+ Str("provider", opts.Provider.Name()).
70+ Int("attempt", attempt+1).
71+ Msg("Empty response from provider")
5672 }
57- if opts.OnUsage != nil && (resp.InputTokens > 0 || resp.OutputTokens > 0) {
58- opts.OnUsage(resp.InputTokens, resp.OutputTokens)
73+
74+ return nil, fmt.Errorf("empty response from provider %s", opts.Provider.Name())
75+}
76+
77+func isEmptyResponse(resp *provider.ChatResponse) bool {
78+ if resp == nil {
79+ return true
5980 }
60- return resp, nil
81+ return resp.Content == "" && resp.Reasoning == "" && len(resp.ToolCalls) == 0
6182 }
6283
6384 // emitAssistant builds an assistant message from a ChatResponse, emits it, and appends to history.