| 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 | configPath?: string; |
| 16 | organizationSlug?: string; |
| 17 | additionalRules?: string[]; |
| 18 | additionalPrompts?: string[]; |
| 19 | toolPermissionOverrides?: { |
| 20 | allow?: string[]; |
| 21 | ask?: string[]; |
| 22 | exclude?: string[]; |
| 23 | mode?: PermissionMode; |
| 24 | }; |
| 25 | skipOnboarding?: boolean; |
| 26 | } |
| 27 | |
| 28 | export async function startTUIChat( |
| 29 | options: StartTUIChatOptions & { customStdin?: NodeJS.ReadStream }, |
| 30 | ) { |
| 31 | const { |
| 32 | initialPrompt, |
| 33 | resume, |
| 34 | configPath, |
| 35 | organizationSlug, |
| 36 | additionalRules, |
| 37 | additionalPrompts, |
| 38 | toolPermissionOverrides, |
| 39 | skipOnboarding, |
| 40 | customStdin, |
| 41 | } = options; |
| 42 | |
| 43 | // Initialize services only if not already done (skipOnboarding means already initialized) |
| 44 | if (!skipOnboarding) { |
| 45 | await initializeServices({ |
| 46 | configPath, |
| 47 | organizationSlug, |
| 48 | rules: additionalRules, |
| 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, |
| 66 | initialPrompt, |
| 67 | resume, |
| 68 | additionalRules, |
| 69 | additionalPrompts, |
| 70 | }), |
| 71 | }), |
| 72 | renderOptions, |
| 73 | ); |
| 74 | |
| 75 | // Handle cleanup |
| 76 | process.on("SIGINT", () => { |
| 77 | unmount(); |
| 78 | process.exit(0); |
| 79 | }); |
| 80 | |
| 81 | return { unmount }; |
| 82 | } |
| 83 | |
| 84 | export async function startRemoteTUIChat( |
| 85 | remoteUrl: string, |
| 86 | initialPrompt?: string, |
| 87 | ) { |
| 88 | // Start the TUI in remote mode - no services needed |
| 89 | const { unmount } = render( |
| 90 | React.createElement(ServiceContainerProvider, { |
| 91 | children: React.createElement(AppRoot, { |
| 92 | remoteUrl, |
| 93 | initialPrompt, |
| 94 | }), |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | // Handle cleanup |
| 99 | process.on("SIGINT", () => { |
| 100 | unmount(); |
| 101 | process.exit(0); |
| 102 | }); |
| 103 | |
| 104 | return { unmount }; |
| 105 | } |