generateCompactionSummary asks the model to summarize the conversation so far. The provided messages should contain the complete history (system prompt, user/assistant turns, tool results). A final user message with the summary prompt is appended before calling the model.
( ctx context.Context, model fantasy.LanguageModel, messages []fantasy.Message, options CompactionOptions, )
| 361 | // results). A final user message with the summary prompt is appended |
| 362 | // before calling the model. |
| 363 | func generateCompactionSummary( |
| 364 | ctx context.Context, |
| 365 | model fantasy.LanguageModel, |
| 366 | messages []fantasy.Message, |
| 367 | options CompactionOptions, |
| 368 | ) (summary string, err error) { |
| 369 | summaryPrompt := make([]fantasy.Message, 0, len(messages)+1) |
| 370 | summaryPrompt = append(summaryPrompt, messages...) |
| 371 | summaryPrompt = append(summaryPrompt, fantasy.Message{ |
| 372 | Role: fantasy.MessageRoleUser, |
| 373 | Content: []fantasy.MessagePart{ |
| 374 | fantasy.TextPart{Text: options.SummaryPrompt}, |
| 375 | }, |
| 376 | }) |
| 377 | toolChoice := fantasy.ToolChoiceNone |
| 378 | |
| 379 | summaryCtx, cancel := context.WithTimeout(ctx, options.Timeout) |
| 380 | defer cancel() |
| 381 | |
| 382 | summaryCtx, finishDebugRun := startCompactionDebugRun(summaryCtx, options) |
| 383 | defer func() { |
| 384 | // If model.Generate (or anything else below) panics, the |
| 385 | // named err return is still nil at this point. Without the |
| 386 | // recover hook we would finalize the debug run as Completed |
| 387 | // in the exact crash path operators rely on to diagnose |
| 388 | // failures. Finalize with the panic as an error status and |
| 389 | // re-panic so the caller's recovery still observes the |
| 390 | // original panic value. |
| 391 | if r := recover(); r != nil { |
| 392 | finishDebugRun(xerrors.Errorf("panic during compaction summary: %v", r)) |
| 393 | panic(r) |
| 394 | } |
| 395 | finishDebugRun(err) |
| 396 | }() |
| 397 | |
| 398 | response, err := model.Generate(summaryCtx, fantasy.Call{ |
| 399 | Prompt: summaryPrompt, |
| 400 | ToolChoice: &toolChoice, |
| 401 | }) |
| 402 | if err != nil { |
| 403 | return "", xerrors.Errorf("generate summary text: %w", err) |
| 404 | } |
| 405 | |
| 406 | parts := make([]string, 0, len(response.Content)) |
| 407 | for _, block := range response.Content { |
| 408 | textBlock, ok := fantasy.AsContentType[fantasy.TextContent](block) |
| 409 | if !ok { |
| 410 | continue |
| 411 | } |
| 412 | text := strings.TrimSpace(textBlock.Text) |
| 413 | if text == "" { |
| 414 | continue |
| 415 | } |
| 416 | parts = append(parts, text) |
| 417 | } |
| 418 | return strings.TrimSpace(strings.Join(parts, " ")), nil |
| 419 | } |