Elffuss BraidHub

kikocis/continue public

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