(ctx context.Context, messages []api.Message)
| 23 | `needed to continue the conversation. Output the summary directly with no preamble.` |
| 24 | |
| 25 | func (s *Summarize) Compact(ctx context.Context, messages []api.Message) ([]api.Message, error) { |
| 26 | if len(messages) < s.Threshold { |
| 27 | return messages, nil |
| 28 | } |
| 29 | split := SafeSplitPoint(messages, len(messages)-s.KeepRecent) |
| 30 | if split == 0 { |
| 31 | return messages, nil |
| 32 | } |
| 33 | old, recent := messages[:split], messages[split:] |
| 34 | |
| 35 | instructions := s.Instructions |
| 36 | if instructions == "" { |
| 37 | instructions = defaultSummarizeInstructions |
| 38 | } |
| 39 | prompt := instructions + "\n\n" + api.RenderTranscript(old) |
| 40 | |
| 41 | resp, err := s.Provider.Send(ctx, []api.Message{{ |
| 42 | Role: api.RoleUser, |
| 43 | Content: []api.Block{{Type: api.BlockText, Text: prompt}}, |
| 44 | }}, nil) |
| 45 | if err != nil { |
| 46 | return messages, fmt.Errorf("summarize: %w", err) |
| 47 | } |
| 48 | |
| 49 | var summary string |
| 50 | for _, b := range resp.Content { |
| 51 | if b.Type == api.BlockText { |
| 52 | summary = b.Text |
| 53 | break |
| 54 | } |
| 55 | } |
| 56 | if summary == "" { |
| 57 | return messages, errors.New("summarize: empty response") |
| 58 | } |
| 59 | |
| 60 | fmt.Printf("[compacted %d messages → summary]\n", len(old)) |
| 61 | return append([]api.Message{{ |
| 62 | Role: api.RoleUser, |
| 63 | Content: []api.Block{{Type: api.BlockText, Text: "[earlier conversation summary]\n" + summary}}, |
| 64 | }}, recent...), nil |
| 65 | } |
nothing calls this directly
no test coverage detected