MCPcopy
hub / github.com/docker/docker-agent / generateOnce

Function generateOnce

pkg/sessiontitle/generator.go:138–165  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

136// stream cannot be created, if reading from it fails, or if the model
137// produced no usable output.
138func 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

Callers 1

GenerateMethod · 0.85

Calls 9

CloneWithOptionsFunction · 0.92
WithStructuredOutputFunction · 0.92
WithMaxTokensFunction · 0.92
WithNoThinkingFunction · 0.92
WithGeneratingTitleFunction · 0.92
sanitizeTitleFunction · 0.85
drainStreamFunction · 0.70
IDMethod · 0.65

Tested by

no test coverage detected