| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | |
| 6 | "reasonix/internal/agent" |
| 7 | |
| 8 | tea "charm.land/bubbletea/v2" |
| 9 | ) |
| 10 | |
| 11 | // webHandoffState is populated by /web before the TUI restores the terminal. |
| 12 | // A fresh session carries only its reserved identity; an existing transcript |
| 13 | // also carries the materialized path so the Web controller can resume it. |
| 14 | type webHandoffState struct { |
| 15 | launchWebOnExit bool |
| 16 | launchWebResumePath string |
| 17 | launchWebSessionID string |
| 18 | launchWebModelRef string |
| 19 | } |
| 20 | |
| 21 | func (m *chatTUI) runHelpOrWebSlash(input, command string) tea.Cmd { |
| 22 | m.echoLocalCommand(input) |
| 23 | if command == "/help" { |
| 24 | m.showHelp() |
| 25 | return nil |
| 26 | } |
| 27 | return m.runWebSlash() |
| 28 | } |
| 29 | |
| 30 | func (m *chatTUI) runWebSlash() tea.Cmd { |
| 31 | if m.runtimeSwitchBusy() { |
| 32 | m.notice("wait for active work to finish, then retry /web") |
| 33 | return nil |
| 34 | } |
| 35 | if err := m.ctrl.Snapshot(); err != nil { |
| 36 | m.notice("web: could not save the current session: " + err.Error()) |
| 37 | return nil |
| 38 | } |
| 39 | resumePath := m.ctrl.SessionPath() |
| 40 | if resumePath != "" { |
| 41 | info, err := os.Stat(resumePath) |
| 42 | switch { |
| 43 | case err == nil && !info.Mode().IsRegular(): |
| 44 | m.notice("web: the current session path is not a regular file") |
| 45 | return nil |
| 46 | case err == nil: |
| 47 | case os.IsNotExist(err): |
| 48 | resumePath = "" |
| 49 | default: |
| 50 | m.notice("web: could not inspect the current session: " + err.Error()) |
| 51 | return nil |
| 52 | } |
| 53 | } |
| 54 | m.followSessionLease() |
| 55 | m.launchWebOnExit = true |
| 56 | m.launchWebResumePath = resumePath |
| 57 | m.launchWebSessionID = agent.BranchID(m.ctrl.SessionPath()) |
| 58 | m.launchWebModelRef = m.modelRef |
| 59 | return shutdownNow |
| 60 | } |
| 61 | |
| 62 | func webHandoffArgs(sessionPath, sessionID, modelRef string) []string { |
| 63 | args := []string{} |
| 64 | if sessionPath != "" { |
| 65 | args = append(args, "--resume", sessionPath) |
| 66 | } else if sessionID != "" { |
| 67 | args = append(args, "--session-id", sessionID) |
| 68 | } |
| 69 | if modelRef != "" { |
| 70 | args = append(args, "--model", modelRef) |
| 71 | } |
| 72 | return args |
| 73 | } |
| 74 |