diff --git a/src/index.ts b/src/index.ts index 2fb842254..60bbfd72a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,18 +29,8 @@ async function chat() { process.exit(1); } - // If TUI mode is requested, start the TUI chat - if (args.useTUI) { - await startTUIChat(args.configPath, args.prompt); - return; - } - const authConfig = loadAuthConfig(); - // Check if the assistant path is a file or a slug - let assistant: ContinueClient["assistant"]; - let client: ContinueClient["client"]; - // This was the previous default behavior, but currently the SDK // only supports slugs, so we've disabled reading local assistant files @@ -69,14 +59,20 @@ async function chat() { // } // } + // Initialize ContinueSDK and MCPService once + let assistant: ContinueClient["assistant"]; + let client: ContinueClient["client"]; + let mcpService: MCPService; + try { - let continueSdk = await initializeContinueSDK( + const continueSdk = await initializeContinueSDK( authConfig.accessToken, args.configPath ); assistant = continueSdk.assistant; client = continueSdk.client; + mcpService = await MCPService.create(assistant.config); } catch (error) { console.error( chalk.red(`Error loading assistant ${args.configPath}:`), @@ -85,16 +81,20 @@ async function chat() { throw error; } - const mcpService = await MCPService.create(assistant!.config); + // If TUI mode is requested, start the TUI chat with the initialized services + if (args.useTUI) { + await startTUIChat(assistant, client, mcpService, args.prompt); + return; + } // Only show intro message if not in headless mode if (!args.isHeadless) { - introMessage(assistant!, mcpService); + introMessage(assistant, mcpService); } // Rules const chatHistory: ChatCompletionMessageParam[] = []; - const systemMessage = assistant!.systemMessage; + const systemMessage = assistant.systemMessage; if (systemMessage) { chatHistory.push({ role: "system", content: systemMessage }); } @@ -115,7 +115,7 @@ async function chat() { isFirstMessage = false; // Handle slash commands - const commandResult = handleSlashCommands(userInput, assistant!.config); + const commandResult = handleSlashCommands(userInput, assistant.config); if (commandResult) { if (commandResult.exit) { break; @@ -140,7 +140,7 @@ async function chat() { } try { - await streamChatResponse(chatHistory, assistant!, client!); + await streamChatResponse(chatHistory, assistant, client); } catch (e: any) { console.error(`\n${chalk.red(`Error: ${e.message}`)}`); console.info( diff --git a/src/ui/index.ts b/src/ui/index.ts index 616414ae2..36eb0fcb5 100644 --- a/src/ui/index.ts +++ b/src/ui/index.ts @@ -1,34 +1,16 @@ import { ContinueClient } from "@continuedev/sdk"; -import chalk from "chalk"; import { render } from "ink"; import React from "react"; -import { loadAuthConfig } from "../auth/workos.js"; -import { initializeContinueSDK } from "../continueSDK.js"; import { introMessage } from "../intro.js"; import { MCPService } from "../mcp.js"; import TUIChat from "./TUIChat.js"; -export async function startTUIChat(configPath: string, initialPrompt?: string) { - const authConfig = loadAuthConfig(); - - let assistant: ContinueClient["assistant"]; - let client: ContinueClient["client"]; - - try { - const continueSdk = await initializeContinueSDK( - authConfig.accessToken, - configPath - ); - - assistant = continueSdk.assistant; - client = continueSdk.client; - } catch (error) { - console.error(chalk.red(`Error loading assistant ${configPath}:`), error); - throw error; - } - - const mcpService = await MCPService.create(assistant.config); - +export async function startTUIChat( + assistant: ContinueClient["assistant"], + client: ContinueClient["client"], + mcpService: MCPService, + initialPrompt?: string +) { // Show intro message before starting TUI introMessage(assistant, mcpService); @@ -48,4 +30,4 @@ export async function startTUIChat(configPath: string, initialPrompt?: string) { }); return { unmount }; -} +} \ No newline at end of file