| 1 | import { render } from "ink"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { PermissionMode } from "../permissions/types.js"; |
| 5 | import { initializeServices } from "../services/index.js"; |
| 6 | import { ServiceContainerProvider } from "../services/ServiceContainerContext.js"; |
| 7 | |
| 8 | import { AppRoot } from "./AppRoot.js"; |
| 9 | |
| 10 | export { MarkdownRenderer } from "./MarkdownRenderer.js"; |
| 11 | |
| 12 | interface StartTUIChatOptions { |
| 13 | initialPrompt?: string; |
| 14 | resume?: boolean; |
| 15 | config?: string; |
| 16 | org?: string; |
| 17 | rule?: string[]; |
| 18 | toolPermissionOverrides?: { |
| 19 | allow?: string[]; |
| 20 | ask?: string[]; |
| 21 | exclude?: string[]; |
| 22 | mode?: PermissionMode; |
| 23 | }; |
| 24 | skipOnboarding?: boolean; |
| 25 | } |
| 26 | |
| 27 | export async function startTUIChat( |
| 28 | options: StartTUIChatOptions & { customStdin?: NodeJS.ReadStream }, |
| 29 | ) { |
| 30 | const { |
| 31 | initialPrompt, |
| 32 | resume, |
| 33 | config, |
| 34 | org, |
| 35 | rule, |
| 36 | toolPermissionOverrides, |
| 37 | skipOnboarding, |
| 38 | customStdin, |
| 39 | } = options; |
| 40 | |
| 41 | // Initialize services only if not already done (skipOnboarding means already initialized) |
| 42 | if (!skipOnboarding) { |
| 43 | await initializeServices({ |
| 44 | options: { config, org, rule }, |
| 45 | headless: false, |
| 46 | toolPermissionOverrides, |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | // Use static imports since we're always loading TUI when there's piped input |
| 51 | |
| 52 | // Start the TUI immediately - it will handle loading states |
| 53 | const renderOptions: any = {}; |
| 54 | if (customStdin) { |
| 55 | renderOptions.stdin = customStdin; |
| 56 | } |
| 57 | |
| 58 | const { unmount } = render( |
| 59 | React.createElement(ServiceContainerProvider, { |
| 60 | children: React.createElement(AppRoot, { |
| 61 | configPath: config, |
| 62 | initialPrompt, |
| 63 | resume, |
| 64 | additionalRules: rule, |
| 65 | }), |
| 66 | }), |
| 67 | renderOptions, |
| 68 | ); |
| 69 | |
| 70 | // Handle cleanup |
| 71 | process.on("SIGINT", () => { |
| 72 | unmount(); |
| 73 | process.exit(0); |
| 74 | }); |
| 75 | |
| 76 | return { unmount }; |
| 77 | } |
| 78 | |
| 79 | export async function startRemoteTUIChat( |
| 80 | remoteUrl: string, |
| 81 | initialPrompt?: string, |
| 82 | ) { |
| 83 | // Start the TUI in remote mode - no services needed |
| 84 | const { unmount } = render( |
| 85 | React.createElement(ServiceContainerProvider, { |
| 86 | children: React.createElement(AppRoot, { |
| 87 | remoteUrl, |
| 88 | initialPrompt, |
| 89 | }), |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | // Handle cleanup |
| 94 | process.on("SIGINT", () => { |
| 95 | unmount(); |
| 96 | process.exit(0); |
| 97 | }); |
| 98 | |
| 99 | return { unmount }; |
| 100 | } |