| 💾 Saved. d722711 k33g 4h ago | 1 | // Package session holds what the two front ends — the terminal REPL |
| 2 | // (internal/agent) and the editor front end (internal/acp) — agree a |
| 3 | // conversation starts from, and how it is reset by the /new command. |
| 4 | // |
| 5 | // A session, for both of them, is a plain []*ai.Message whose first element is |
| 6 | // the system prompt. "Starting a new session" means going back to exactly that: |
| 7 | // the system prompt alone, every question, answer and tool turn forgotten. |
| 8 | // The package knows nothing about the screen or the protocol: printing the |
| 9 | // report line, or sending the session/update, stays each front end's job. |
| 10 | package session |
| 11 | |
| 12 | import ( |
| 13 | "strings" |
| 14 | |
| 15 | "github.com/firebase/genkit/go/ai" |
| 16 | ) |
| 17 | |
| 18 | // NewCommand is the slash command that clears the history and starts a new |
| 19 | // session. The same spelling is used at the terminal prompt and in an ACP |
| 20 | // prompt; ACP advertises it without the slash (see NewCommandName). |
| 21 | const NewCommand = "/new" |
| 22 | |
| 23 | // NewCommandName is NewCommand as ACP names it: clients list commands by bare |
| 24 | // name and send them back as prompt text with the leading slash. |
| 25 | const NewCommandName = "new" |
| 26 | |
| 27 | // IsNewCommand reports whether the user's input is the /new command. The |
| 28 | // input is trimmed first: an editor sends the line as typed, spaces and |
| 29 | // trailing newline included, and "/new " must not be mistaken for a question. |
| 30 | // |
| 31 | // session.IsNewCommand("/new") // true |
| 32 | // session.IsNewCommand(" /new\n") // true |
| 33 | // session.IsNewCommand("/new one") // false — not the command |
| 34 | func IsNewCommand(input string) bool { |
| 35 | return strings.TrimSpace(input) == NewCommand |
| 36 | } |
| 37 | |
| 38 | // Fresh returns the history of a brand-new session: the system prompt and |
| 39 | // nothing else. Both front ends start from it, and /new returns to it. |
| 40 | // |
| 41 | // messages := session.Fresh(config.Cfg.System) |
| 42 | // // len(messages) == 1, messages[0].Role == ai.RoleSystem |
| 43 | func Fresh(system string) []*ai.Message { |
| 44 | return []*ai.Message{ai.NewSystemTextMessage(system)} |
| 45 | } |
| 46 | |
| 47 | // Forgotten counts the messages a reset throws away: everything except the |
| 48 | // system prompt. It is the figure the report line shows ("12 message(s) |
| 49 | // forgotten"), so the user sees that the command did something — or that |
| 50 | // there was nothing to forget. |
| 51 | // |
| 52 | // session.Forgotten(session.Fresh("you are bob")) // 0 |
| 53 | func Forgotten(msgs []*ai.Message) int { |
| 54 | n := 0 |
| 55 | for _, m := range msgs { |
| 56 | if m.Role != ai.RoleSystem { |
| 57 | n++ |
| 58 | } |
| 59 | } |
| 60 | return n |
| 61 | } |