|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | """Session management command handlers.""" |
3 | 3 | # pylint: disable=too-many-branches,too-many-statements,unused-argument |
| 4 | +# pylint: disable=too-many-return-statements |
4 | 5 |
|
5 | 6 | from __future__ import annotations |
6 | 7 |
|
|
15 | 16 | " /session list — list all sessions\n" |
16 | 17 | " /session switch <n> — switch to a topic\n" |
17 | 18 | " /session rename <old> <new> — rename\n" |
| 19 | + " /session fork <new> [src] — fork a session (default src: " |
| 20 | + "current)\n" |
18 | 21 | " /session remove <n> — remove session " |
19 | 22 | "(default cannot be removed)\n" |
20 | 23 | " /session scene — show scene memory of the current topic\n" |
@@ -128,6 +131,35 @@ def _handle_session_command(cmd: str, config, agent) -> None: |
128 | 131 | f"or '{new_name}' already exists[/red]", |
129 | 132 | ) |
130 | 133 |
|
| 134 | + elif subcmd == "fork": |
| 135 | + if len(parts) < 3: |
| 136 | + console.print( |
| 137 | + "[dim]Usage: /session fork <new> [src][/dim]", |
| 138 | + ) |
| 139 | + return |
| 140 | + fork_args = parts[2].split(maxsplit=1) |
| 141 | + dst = fork_args[0] |
| 142 | + src = ( |
| 143 | + fork_args[1] |
| 144 | + if len(fork_args) > 1 |
| 145 | + else (session_mgr.get_current_topic()) |
| 146 | + ) |
| 147 | + # Flush the live conversation when forking the active topic so |
| 148 | + # the fork carries the latest messages. |
| 149 | + if src == session_mgr.get_current_topic(): |
| 150 | + if agent.session_path and agent.messages: |
| 151 | + agent.save_session() |
| 152 | + if session_mgr.fork_topic(src, dst): |
| 153 | + console.print( |
| 154 | + f"[green]Forked session: {src} → {dst}[/green]\n" |
| 155 | + f" [dim]Switch with: /session switch {dst}[/dim]", |
| 156 | + ) |
| 157 | + else: |
| 158 | + console.print( |
| 159 | + f"[red]Fork failed: '{src}' does not exist " |
| 160 | + f"or '{dst}' already exists[/red]", |
| 161 | + ) |
| 162 | + |
131 | 163 | elif subcmd == "remove": |
132 | 164 | topic = parts[2] if len(parts) > 2 else "" |
133 | 165 | if not topic: |
|
0 commit comments