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