generateOnce performs a single one-shot title generation call against baseModel and returns the sanitized title. An error is returned if the stream cannot be created, if reading from it fails, or if the model produced no usable output.
(ctx context.Context, baseModel provider.Provider, messages []chat.Message)
| 136 | // stream cannot be created, if reading from it fails, or if the model |
| 137 | // produced no usable output. |
| 138 | func generateOnce(ctx context.Context, baseModel provider.Provider, messages []chat.Message) (string, error) { |
| 139 | // Clone the model with title-generation-specific options so each attempt |
| 140 | // gets a consistent, low-token one-shot call. |
| 141 | titleModel := provider.CloneWithOptions( |
| 142 | ctx, |
| 143 | baseModel, |
| 144 | options.WithStructuredOutput(nil), |
| 145 | options.WithMaxTokens(titleMaxTokens), |
| 146 | options.WithNoThinking(), |
| 147 | options.WithGeneratingTitle(), |
| 148 | ) |
| 149 | |
| 150 | stream, err := titleModel.CreateChatCompletionStream(ctx, messages, nil) |
| 151 | if err != nil { |
| 152 | return "", fmt.Errorf("model %q: %w", baseModel.ID(), err) |
| 153 | } |
| 154 | |
| 155 | raw, err := drainStream(stream) |
| 156 | if err != nil { |
| 157 | return "", fmt.Errorf("model %q: %w", baseModel.ID(), err) |
| 158 | } |
| 159 | |
| 160 | title := sanitizeTitle(raw) |
| 161 | if title == "" { |
| 162 | return "", fmt.Errorf("empty title output from model %q", baseModel.ID()) |
| 163 | } |
| 164 | return title, nil |
| 165 | } |
| 166 | |
| 167 | // drainStream reads the entire content of a chat completion stream and |
| 168 | // returns the concatenated delta content. The stream is always closed before |
no test coverage detected