handleForkSession creates a fork of the current session. If a session is loaded, forks from that. Otherwise, forks from in-memory history.
(newName string)
| 446 | // handleForkSession creates a fork of the current session. |
| 447 | // If a session is loaded, forks from that. Otherwise, forks from in-memory history. |
| 448 | func (cli *ChatCLI) handleForkSession(newName string) { |
| 449 | // Build session data from current state |
| 450 | sd := &SessionData{ |
| 451 | Version: 2, |
| 452 | ChatHistory: make([]models.Message, len(cli.history)), |
| 453 | } |
| 454 | copy(sd.ChatHistory, cli.history) |
| 455 | |
| 456 | // If the current session has a name (was loaded/saved), we can fork from file |
| 457 | if cli.currentSessionName != "" { |
| 458 | if err := cli.sessionManager.ForkSession(cli.currentSessionName, newName); err != nil { |
| 459 | fmt.Println(colorize(fmt.Sprintf(" %s", i18n.T("sess.cmd.fork_error", err)), ColorRed)) |
| 460 | return |
| 461 | } |
| 462 | } else { |
| 463 | // Fork from in-memory state |
| 464 | if err := cli.sessionManager.ForkCurrentToNew(newName, sd); err != nil { |
| 465 | fmt.Println(colorize(fmt.Sprintf(" %s", i18n.T("sess.cmd.fork_error", err)), ColorRed)) |
| 466 | return |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // Switch to the forked session |
| 471 | oldName := cli.currentSessionName |
| 472 | if oldName == "" { |
| 473 | oldName = i18n.T("sess.cmd.fork_unsaved") |
| 474 | } |
| 475 | cli.currentSessionName = newName |
| 476 | |
| 477 | fmt.Println() |
| 478 | fmt.Println(uiBox("✅", i18n.T("sess.cmd.fork_header"), ColorGreen)) |
| 479 | p := uiPrefix(ColorGreen) |
| 480 | fmt.Println(p + fmt.Sprintf(" %s%s%s %s", ColorGray, i18n.T("sess.cmd.fork_from"), ColorReset, oldName)) |
| 481 | fmt.Println(p + fmt.Sprintf(" %s%s%s %s", ColorGray, i18n.T("sess.cmd.fork_to"), ColorReset, colorize(newName, ColorCyan))) |
| 482 | fmt.Println(p + fmt.Sprintf(" %s%s%s %d", ColorGray, i18n.T("sess.cmd.fork_messages"), ColorReset, len(cli.history))) |
| 483 | fmt.Println(p + colorize(" "+i18n.T("sess.cmd.fork_footer"), ColorGray)) |
| 484 | fmt.Println(uiBoxEnd(ColorGreen)) |
| 485 | fmt.Println() |
| 486 | } |
no test coverage detected