MCPcopy Create free account
hub / github.com/betta-tech/byo-coding-agent / Compact

Method Compact

internal/compact/summarize.go:25–65  ·  view source on GitHub ↗
(ctx context.Context, messages []api.Message)

Source from the content-addressed store, hash-verified

23 `needed to continue the conversation. Output the summary directly with no preamble.`
24
25func (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}

Callers

nothing calls this directly

Calls 3

RenderTranscriptFunction · 0.92
SafeSplitPointFunction · 0.85
SendMethod · 0.65

Tested by

no test coverage detected