| 💾 Saved. d722711 k33g 4h ago | 1 | package compact |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/firebase/genkit/go/ai" |
| 7 | ) |
| 8 | |
| 9 | // summarySystem replaces the agent's own system prompt in the summary request. |
| 10 | // The agent's prompt says "you are a coding agent, run commands": sent along |
| 11 | // with a history full of tool calls and no tools declared, it invites the model |
| 12 | // to keep acting. The note-taker's prompt asks for the one thing wanted here. |
| 13 | const summarySystem = `You are the note-taker of a coding agent. You write precise, structured notes about a coding session, in plain markdown, and you never invent details that are not in the conversation.` |
| 14 | |
| 15 | // DefaultPrompt is the summary request (CONTEXT_WINDOW.md § 4.1). The seven |
| 16 | // fixed sections are the contract: what the model must preserve, and a shape |
| 17 | // the agent can rely on. The ban on quoting file contents is deliberate — a |
| 18 | // file may have changed since it was read, and the agent's system prompt |
| 19 | // already forbids stating what a command in THIS answer did not return. |
| 20 | const DefaultPrompt = `You are compressing the history of a coding session so that the same agent |
| 21 | can continue the work with less context. Everything above this message is |
| 22 | that history. Write a summary, in English, using EXACTLY the sections below, |
| 23 | markdown headings included. Be concrete: exact file paths, exact command |
| 24 | lines, exact error messages. Do not invent anything that is not in the |
| 25 | history. Do not quote file contents — files may have changed since; say what |
| 26 | was done to them instead. |
| 27 | |
| 28 | ## Goal |
| 29 | What the user is trying to achieve, in one or two sentences, in their words. |
| 30 | |
| 31 | ## Files touched |
| 32 | One line per file: path — created | modified | deleted | read-only — what |
| 33 | changed. Nothing else. |
| 34 | |
| 35 | ## Decisions |
| 36 | Choices made and their reason (libraries, structure, names, things the user |
| 37 | refused). One line each. |
| 38 | |
| 39 | ## Errors and fixes |
| 40 | Errors met, and what fixed them. One line each. Skip errors that are not |
| 41 | relevant any more. |
| 42 | |
| 43 | ## Done |
| 44 | The tasks completed, one line each. |
| 45 | |
| 46 | ## In progress / next steps |
| 47 | What was being done when the history ends, and what remains. Be explicit |
| 48 | about anything half-finished. |
| 49 | |
| 50 | ## Useful commands |
| 51 | Commands to rebuild, test or run the project, and any background job left |
| 52 | running (with its pid file). One per line. |
| 53 | |
| 54 | Keep the whole summary under 40 lines. If a section is empty, write "none".` |
| 55 | |
| 56 | // mergeLine is appended to the prompt when the history to summarise already |
| 57 | // starts with an earlier summary. |
| 58 | const mergeLine = "\n\nAn earlier summary is at the top of the history; merge it into the new one, do not repeat it as-is." |
| 59 | |
| 60 | // acknowledgement is the model half of the summary pair. It keeps the |
| 61 | // user/assistant alternation that chat templates expect after a user message, |
| 62 | // and it restates the one rule the summary must not erode. |
| 63 | const acknowledgement = "Understood — I will re-check files before editing them." |
| 64 | |
| 65 | // Request builds the summary request: the note-taker's system message, the |
| 66 | // old turns as they were sent to the model, then the prompt. `prompt` empty |
| 67 | // means DefaultPrompt. |
| 68 | func Request(old []*ai.Message, prompt string, merge bool) []*ai.Message { |
| 69 | if prompt == "" { |
| 70 | prompt = DefaultPrompt |
| 71 | } |
| 72 | if merge { |
| 73 | prompt += mergeLine |
| 74 | } |
| 75 | req := make([]*ai.Message, 0, len(old)+2) |
| 76 | req = append(req, ai.NewSystemTextMessage(summarySystem)) |
| 77 | req = append(req, old...) |
| 78 | req = append(req, ai.NewUserTextMessage(prompt)) |
| 79 | return req |
| 80 | } |
| 81 | |
| 82 | // SummaryPair wraps the model's notes into the two messages that replace the |
| 83 | // old turns. The wrapper says what the notes are and what they are not: the |
| 84 | // model must treat them as notes to verify, not as observations — otherwise a |
| 85 | // wrong line in the summary becomes a fact for the rest of the session. |
| 86 | func SummaryPair(summary string, messages, questions, commands int) []*ai.Message { |
| 87 | meta := map[string]any{metaKey: "summary"} |
| 88 | notes := fmt.Sprintf(`[Context summary — the %d earlier messages of this session (%d question(s), %d command(s)) were compressed into the notes below. Treat them as notes, not as observations: re-read a file or re-run a command before relying on its content.] |
| 89 | |
| 90 | %s`, messages, questions, commands, summary) |
| 91 | return []*ai.Message{ |
| 92 | ai.NewMessage(ai.RoleUser, meta, ai.NewTextPart(notes)), |
| 93 | ai.NewMessage(ai.RoleModel, meta, ai.NewTextPart(acknowledgement)), |
| 94 | } |
| 95 | } |