|
| 1 | +package claude |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/onkernel/cli/internal/claude" |
| 12 | + "github.com/onkernel/cli/pkg/util" |
| 13 | + "github.com/onkernel/kernel-go-sdk" |
| 14 | + "github.com/pterm/pterm" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +var chatCmd = &cobra.Command{ |
| 19 | + Use: "chat <browser-id>", |
| 20 | + Short: "Interactive chat with Claude", |
| 21 | + Long: `Start an interactive chat session with Claude in a Kernel browser. |
| 22 | +
|
| 23 | +This provides a simple command-line interface for having a conversation |
| 24 | +with Claude. Type your messages and receive responses directly in the terminal. |
| 25 | +
|
| 26 | +Special commands: |
| 27 | + /quit, /exit - Exit the chat session |
| 28 | + /clear - Clear the terminal |
| 29 | + /status - Check extension status |
| 30 | + /help - Show available commands`, |
| 31 | + Example: ` # Start chat with existing browser |
| 32 | + kernel claude chat abc123xyz |
| 33 | +
|
| 34 | + # Launch new browser and start chatting |
| 35 | + kernel claude launch -b claude-bundle.zip --chat`, |
| 36 | + Args: cobra.ExactArgs(1), |
| 37 | + RunE: runChat, |
| 38 | +} |
| 39 | + |
| 40 | +func init() { |
| 41 | + chatCmd.Flags().Bool("no-tui", false, "Disable interactive mode (line-by-line I/O)") |
| 42 | +} |
| 43 | + |
| 44 | +func runChat(cmd *cobra.Command, args []string) error { |
| 45 | + browserID := args[0] |
| 46 | + // noTUI, _ := cmd.Flags().GetBool("no-tui") |
| 47 | + // For now, both modes use the same implementation |
| 48 | + |
| 49 | + ctx := cmd.Context() |
| 50 | + client := util.GetKernelClient(cmd) |
| 51 | + |
| 52 | + return runChatWithBrowser(ctx, client, browserID) |
| 53 | +} |
| 54 | + |
| 55 | +func runChatWithBrowser(ctx context.Context, client kernel.Client, browserID string) error { |
| 56 | + // Verify the browser exists |
| 57 | + pterm.Info.Printf("Connecting to browser: %s\n", browserID) |
| 58 | + |
| 59 | + browser, err := client.Browsers.Get(ctx, browserID) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("failed to get browser: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + // Check Claude status first |
| 65 | + pterm.Info.Println("Checking Claude extension status...") |
| 66 | + statusResult, err := client.Browsers.Playwright.Execute(ctx, browser.SessionID, kernel.BrowserPlaywrightExecuteParams{ |
| 67 | + Code: claude.CheckStatusScript, |
| 68 | + TimeoutSec: kernel.Opt(int64(30)), |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("failed to check status: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + if statusResult.Result != nil { |
| 75 | + var status struct { |
| 76 | + ExtensionLoaded bool `json:"extensionLoaded"` |
| 77 | + Authenticated bool `json:"authenticated"` |
| 78 | + Error string `json:"error"` |
| 79 | + } |
| 80 | + resultBytes, _ := json.Marshal(statusResult.Result) |
| 81 | + _ = json.Unmarshal(resultBytes, &status) |
| 82 | + |
| 83 | + if !status.ExtensionLoaded { |
| 84 | + return fmt.Errorf("Claude extension is not loaded. Load it first with: kernel claude load %s -b claude-bundle.zip", browserID) |
| 85 | + } |
| 86 | + if !status.Authenticated { |
| 87 | + pterm.Warning.Println("Claude extension is not authenticated.") |
| 88 | + pterm.Info.Printf("Please log in via the live view: %s\n", browser.BrowserLiveViewURL) |
| 89 | + return fmt.Errorf("authentication required") |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Display chat header |
| 94 | + pterm.Println() |
| 95 | + pterm.DefaultHeader.WithBackgroundStyle(pterm.NewStyle(pterm.BgBlue)). |
| 96 | + WithTextStyle(pterm.NewStyle(pterm.FgWhite)). |
| 97 | + Println("Claude Chat") |
| 98 | + pterm.Println() |
| 99 | + pterm.Info.Printf("Browser: %s\n", browserID) |
| 100 | + pterm.Info.Printf("Live View: %s\n", browser.BrowserLiveViewURL) |
| 101 | + pterm.Println() |
| 102 | + pterm.Info.Println("Type your message and press Enter. Use /help for commands, /quit to exit.") |
| 103 | + pterm.Println() |
| 104 | + |
| 105 | + // Start the chat loop |
| 106 | + scanner := bufio.NewScanner(os.Stdin) |
| 107 | + messageCount := 0 |
| 108 | + |
| 109 | + for { |
| 110 | + // Show prompt |
| 111 | + pterm.Print(pterm.Cyan("You: ")) |
| 112 | + |
| 113 | + if !scanner.Scan() { |
| 114 | + break |
| 115 | + } |
| 116 | + |
| 117 | + input := strings.TrimSpace(scanner.Text()) |
| 118 | + if input == "" { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + // Handle special commands |
| 123 | + if strings.HasPrefix(input, "/") { |
| 124 | + handled, shouldExit := handleChatCommand(ctx, client, browserID, input) |
| 125 | + if shouldExit { |
| 126 | + pterm.Info.Println("Goodbye!") |
| 127 | + return nil |
| 128 | + } |
| 129 | + if handled { |
| 130 | + continue |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Send the message |
| 135 | + messageCount++ |
| 136 | + spinner, _ := pterm.DefaultSpinner.Start("Claude is thinking...") |
| 137 | + |
| 138 | + response, err := sendChatMessage(ctx, client, browser.SessionID, input) |
| 139 | + spinner.Stop() |
| 140 | + |
| 141 | + if err != nil { |
| 142 | + pterm.Error.Printf("Error: %v\n", err) |
| 143 | + pterm.Println() |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + // Display the response |
| 148 | + pterm.Println() |
| 149 | + pterm.Print(pterm.Green("Claude: ")) |
| 150 | + fmt.Println(response) |
| 151 | + pterm.Println() |
| 152 | + } |
| 153 | + |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func handleChatCommand(ctx context.Context, client kernel.Client, browserID, input string) (handled bool, shouldExit bool) { |
| 158 | + parts := strings.Fields(input) |
| 159 | + if len(parts) == 0 { |
| 160 | + return false, false |
| 161 | + } |
| 162 | + |
| 163 | + cmd := strings.ToLower(parts[0]) |
| 164 | + |
| 165 | + switch cmd { |
| 166 | + case "/quit", "/exit", "/q": |
| 167 | + return true, true |
| 168 | + |
| 169 | + case "/clear": |
| 170 | + // Clear terminal (works on most terminals) |
| 171 | + fmt.Print("\033[H\033[2J") |
| 172 | + pterm.Info.Println("Terminal cleared.") |
| 173 | + return true, false |
| 174 | + |
| 175 | + case "/status": |
| 176 | + pterm.Info.Println("Checking status...") |
| 177 | + result, err := client.Browsers.Playwright.Execute(ctx, browserID, kernel.BrowserPlaywrightExecuteParams{ |
| 178 | + Code: claude.CheckStatusScript, |
| 179 | + TimeoutSec: kernel.Opt(int64(30)), |
| 180 | + }) |
| 181 | + if err != nil { |
| 182 | + pterm.Error.Printf("Status check failed: %v\n", err) |
| 183 | + return true, false |
| 184 | + } |
| 185 | + |
| 186 | + var status struct { |
| 187 | + ExtensionLoaded bool `json:"extensionLoaded"` |
| 188 | + Authenticated bool `json:"authenticated"` |
| 189 | + HasConversation bool `json:"hasConversation"` |
| 190 | + Error string `json:"error"` |
| 191 | + } |
| 192 | + if result.Result != nil { |
| 193 | + resultBytes, _ := json.Marshal(result.Result) |
| 194 | + _ = json.Unmarshal(resultBytes, &status) |
| 195 | + } |
| 196 | + |
| 197 | + pterm.Info.Printf("Extension: %v, Auth: %v, Conversation: %v\n", |
| 198 | + status.ExtensionLoaded, status.Authenticated, status.HasConversation) |
| 199 | + if status.Error != "" { |
| 200 | + pterm.Warning.Printf("Error: %s\n", status.Error) |
| 201 | + } |
| 202 | + return true, false |
| 203 | + |
| 204 | + case "/help", "/?": |
| 205 | + pterm.Println() |
| 206 | + pterm.Info.Println("Available commands:") |
| 207 | + pterm.Println(" /quit, /exit - Exit the chat session") |
| 208 | + pterm.Println(" /clear - Clear the terminal") |
| 209 | + pterm.Println(" /status - Check extension status") |
| 210 | + pterm.Println(" /help - Show this help message") |
| 211 | + pterm.Println() |
| 212 | + return true, false |
| 213 | + |
| 214 | + default: |
| 215 | + pterm.Warning.Printf("Unknown command: %s (use /help for available commands)\n", cmd) |
| 216 | + return true, false |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +func sendChatMessage(ctx context.Context, client kernel.Client, browserID, message string) (string, error) { |
| 221 | + // Build the script with the message |
| 222 | + script := fmt.Sprintf(` |
| 223 | +process.env.CLAUDE_MESSAGE = %s; |
| 224 | +process.env.CLAUDE_TIMEOUT_MS = '300000'; |
| 225 | +
|
| 226 | +%s |
| 227 | +`, jsonMarshalString(message), claude.SendMessageScript) |
| 228 | + |
| 229 | + result, err := client.Browsers.Playwright.Execute(ctx, browserID, kernel.BrowserPlaywrightExecuteParams{ |
| 230 | + Code: script, |
| 231 | + TimeoutSec: kernel.Opt(int64(330)), // 5.5 minutes |
| 232 | + }) |
| 233 | + if err != nil { |
| 234 | + return "", fmt.Errorf("failed to send message: %w", err) |
| 235 | + } |
| 236 | + |
| 237 | + if !result.Success { |
| 238 | + if result.Error != "" { |
| 239 | + return "", fmt.Errorf("%s", result.Error) |
| 240 | + } |
| 241 | + return "", fmt.Errorf("send failed") |
| 242 | + } |
| 243 | + |
| 244 | + // Parse the result |
| 245 | + var response struct { |
| 246 | + Response string `json:"response"` |
| 247 | + Warning string `json:"warning"` |
| 248 | + } |
| 249 | + if result.Result != nil { |
| 250 | + resultBytes, _ := json.Marshal(result.Result) |
| 251 | + _ = json.Unmarshal(resultBytes, &response) |
| 252 | + } |
| 253 | + |
| 254 | + if response.Response == "" { |
| 255 | + return "", fmt.Errorf("empty response") |
| 256 | + } |
| 257 | + |
| 258 | + return response.Response, nil |
| 259 | +} |
0 commit comments