@abacus-ai/cli 1.106.25007 → 2.0.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.oxlintrc.json +8 -0
- package/dist/index.mjs +12603 -0
- package/package.json +7 -39
- package/resources/abacus.ico +0 -0
- package/resources/entitlements.plist +9 -0
- package/src/__e2e__/README.md +196 -0
- package/src/__e2e__/agent-interactions.e2e.test.tsx +61 -0
- package/src/__e2e__/cli-commands.e2e.test.tsx +77 -0
- package/src/__e2e__/conversation-throttle.e2e.test.ts +453 -0
- package/src/__e2e__/conversation.e2e.test.tsx +56 -0
- package/src/__e2e__/diff-preview.e2e.test.tsx +3399 -0
- package/src/__e2e__/file-creation.e2e.test.tsx +149 -0
- package/src/__e2e__/helpers/test-helpers.ts +450 -0
- package/src/__e2e__/keyboard-navigation.e2e.test.tsx +34 -0
- package/src/__e2e__/llm-models.e2e.test.ts +402 -0
- package/src/__e2e__/mcp/mcp-callback-flow.e2e.test.tsx +71 -0
- package/src/__e2e__/mcp/mcp-full-app-ui.e2e.test.tsx +167 -0
- package/src/__e2e__/mcp/mcp-ui-rendering.e2e.test.tsx +185 -0
- package/src/__e2e__/repl.e2e.test.tsx +78 -0
- package/src/__e2e__/shell-compatibility.e2e.test.tsx +76 -0
- package/src/__e2e__/theme-mcp.e2e.test.tsx +98 -0
- package/src/__e2e__/tool-permissions.e2e.test.tsx +66 -0
- package/src/args.ts +22 -0
- package/src/components/__tests__/react-compiler.test.tsx +78 -0
- package/src/components/__tests__/status-indicator.test.tsx +403 -0
- package/src/components/composer/__tests__/bash-runner.test.tsx +263 -0
- package/src/components/composer/agent-mode-indicator.tsx +63 -0
- package/src/components/composer/bash-runner.tsx +54 -0
- package/src/components/composer/commands/default-commands.tsx +615 -0
- package/src/components/composer/commands/handler.tsx +59 -0
- package/src/components/composer/commands/picker.tsx +273 -0
- package/src/components/composer/commands/registry.ts +233 -0
- package/src/components/composer/commands/types.ts +33 -0
- package/src/components/composer/context.tsx +88 -0
- package/src/components/composer/file-mention-picker.tsx +83 -0
- package/src/components/composer/help.tsx +44 -0
- package/src/components/composer/index.tsx +1006 -0
- package/src/components/composer/mentions.ts +57 -0
- package/src/components/composer/message-queue.tsx +70 -0
- package/src/components/composer/mode-panel.tsx +35 -0
- package/src/components/composer/modes/__tests__/bash-handler.test.tsx +755 -0
- package/src/components/composer/modes/__tests__/bash-renderer.test.tsx +1108 -0
- package/src/components/composer/modes/bash-handler.tsx +132 -0
- package/src/components/composer/modes/bash-renderer.tsx +175 -0
- package/src/components/composer/modes/default-handlers.tsx +33 -0
- package/src/components/composer/modes/index.ts +41 -0
- package/src/components/composer/modes/types.ts +21 -0
- package/src/components/composer/persistent-shell.ts +283 -0
- package/src/components/composer/process.ts +65 -0
- package/src/components/composer/types.ts +9 -0
- package/src/components/composer/use-mention-search.ts +68 -0
- package/src/components/error-boundry.tsx +60 -0
- package/src/components/exit-message.tsx +29 -0
- package/src/components/expanded-view.tsx +74 -0
- package/src/components/file-completion.tsx +127 -0
- package/src/components/header.tsx +47 -0
- package/src/components/logo.tsx +37 -0
- package/src/components/segments.tsx +356 -0
- package/src/components/status-indicator.tsx +306 -0
- package/src/components/tool-group-summary.tsx +263 -0
- package/src/components/tool-permissions/ask-user-question-permission-ui.tsx +312 -0
- package/src/components/tool-permissions/diff-preview.tsx +355 -0
- package/src/components/tool-permissions/index.ts +5 -0
- package/src/components/tool-permissions/permission-options.tsx +375 -0
- package/src/components/tool-permissions/permission-preview-header.tsx +57 -0
- package/src/components/tool-permissions/tool-permission-ui.tsx +398 -0
- package/src/components/tools/agent/ask-user-question.tsx +101 -0
- package/src/components/tools/agent/enter-plan-mode.tsx +49 -0
- package/src/components/tools/agent/exit-plan-mode.tsx +75 -0
- package/src/components/tools/agent/handoff-to-main.tsx +27 -0
- package/src/components/tools/agent/subagent.tsx +37 -0
- package/src/components/tools/agent/todo-write.tsx +104 -0
- package/src/components/tools/browser/close-tab.tsx +58 -0
- package/src/components/tools/browser/computer.tsx +70 -0
- package/src/components/tools/browser/get-interactive-elements.tsx +54 -0
- package/src/components/tools/browser/get-tab-content.tsx +51 -0
- package/src/components/tools/browser/navigate-to.tsx +59 -0
- package/src/components/tools/browser/new-tab.tsx +60 -0
- package/src/components/tools/browser/perform-action.tsx +63 -0
- package/src/components/tools/browser/refresh-tab.tsx +43 -0
- package/src/components/tools/browser/switch-tab.tsx +58 -0
- package/src/components/tools/filesystem/delete-file.tsx +104 -0
- package/src/components/tools/filesystem/edit.tsx +220 -0
- package/src/components/tools/filesystem/list-dir.tsx +78 -0
- package/src/components/tools/filesystem/read-file.tsx +180 -0
- package/src/components/tools/filesystem/upload-image.tsx +76 -0
- package/src/components/tools/ide/ide-diagnostics.tsx +62 -0
- package/src/components/tools/index.ts +91 -0
- package/src/components/tools/mcp/mcp-tool.tsx +158 -0
- package/src/components/tools/search/fetch-url.tsx +73 -0
- package/src/components/tools/search/file-search.tsx +78 -0
- package/src/components/tools/search/grep.tsx +90 -0
- package/src/components/tools/search/semantic-search.tsx +66 -0
- package/src/components/tools/search/web-search.tsx +71 -0
- package/src/components/tools/shared/index.tsx +48 -0
- package/src/components/tools/shared/zod-coercion.ts +35 -0
- package/src/components/tools/terminal/bash-tool-output.tsx +174 -0
- package/src/components/tools/terminal/get-terminal-output.tsx +85 -0
- package/src/components/tools/terminal/run-in-terminal.tsx +106 -0
- package/src/components/tools/types.ts +16 -0
- package/src/components/tools.tsx +66 -0
- package/src/components/ui/__tests__/divider.test.tsx +61 -0
- package/src/components/ui/__tests__/gradient.test.tsx +125 -0
- package/src/components/ui/__tests__/input.test.tsx +166 -0
- package/src/components/ui/__tests__/select.test.tsx +273 -0
- package/src/components/ui/__tests__/shimmer.test.tsx +99 -0
- package/src/components/ui/blinking-indicator.tsx +25 -0
- package/src/components/ui/divider.tsx +162 -0
- package/src/components/ui/gradient.tsx +56 -0
- package/src/components/ui/input.tsx +228 -0
- package/src/components/ui/select.tsx +151 -0
- package/src/components/ui/shimmer.tsx +84 -0
- package/src/context/agent-mode.tsx +95 -0
- package/src/context/extension-file.tsx +136 -0
- package/src/context/network-activity.tsx +45 -0
- package/src/context/notification.tsx +62 -0
- package/src/context/shell-size.tsx +49 -0
- package/src/context/shell-title.tsx +38 -0
- package/src/entrypoints/print-mode.ts +312 -0
- package/src/entrypoints/repl.tsx +401 -0
- package/src/hooks/use-agent.ts +15 -0
- package/src/hooks/use-api-client.ts +1 -0
- package/src/hooks/use-available-height.ts +8 -0
- package/src/hooks/use-cleanup.ts +29 -0
- package/src/hooks/use-interrupt-manager.ts +242 -0
- package/src/hooks/use-models.ts +22 -0
- package/src/index.ts +217 -0
- package/src/lib/__tests__/ansi.test.ts +255 -0
- package/src/lib/__tests__/cli.test.ts +122 -0
- package/src/lib/__tests__/commands.test.ts +325 -0
- package/src/lib/__tests__/constants.test.ts +15 -0
- package/src/lib/__tests__/focusables.test.ts +25 -0
- package/src/lib/__tests__/fs.test.ts +231 -0
- package/src/lib/__tests__/markdown.test.tsx +348 -0
- package/src/lib/__tests__/mcpCommandHandler.test.ts +173 -0
- package/src/lib/__tests__/mcpManagement.test.ts +38 -0
- package/src/lib/__tests__/path-paste.test.ts +144 -0
- package/src/lib/__tests__/path.test.ts +300 -0
- package/src/lib/__tests__/queries.test.ts +39 -0
- package/src/lib/__tests__/standaloneMcpService.test.ts +71 -0
- package/src/lib/__tests__/text-buffer.test.ts +328 -0
- package/src/lib/__tests__/text-utils.test.ts +32 -0
- package/src/lib/__tests__/timing.test.ts +78 -0
- package/src/lib/__tests__/utils.test.ts +238 -0
- package/src/lib/__tests__/vim-buffer-actions.test.ts +154 -0
- package/src/lib/ansi.ts +150 -0
- package/src/lib/cli-push-server.ts +112 -0
- package/src/lib/cli.ts +44 -0
- package/src/lib/clipboard.ts +226 -0
- package/src/lib/command-utils.ts +93 -0
- package/src/lib/commands.ts +270 -0
- package/src/lib/constants.ts +3 -0
- package/src/lib/extension-connection.ts +181 -0
- package/src/lib/focusables.ts +7 -0
- package/src/lib/fs.ts +533 -0
- package/src/lib/markdown/code-block.tsx +63 -0
- package/src/lib/markdown/index.ts +4 -0
- package/src/lib/markdown/link.tsx +19 -0
- package/src/lib/markdown/markdown.tsx +372 -0
- package/src/lib/markdown/types.ts +15 -0
- package/src/lib/mcpCommandHandler.ts +121 -0
- package/src/lib/mcpManagement.ts +44 -0
- package/src/lib/path-paste.ts +185 -0
- package/src/lib/path.ts +179 -0
- package/src/lib/queries.ts +15 -0
- package/src/lib/standaloneMcpService.ts +688 -0
- package/src/lib/status-utils.ts +237 -0
- package/src/lib/test-utils.tsx +72 -0
- package/src/lib/text-buffer.ts +2415 -0
- package/src/lib/text-utils.ts +272 -0
- package/src/lib/timing.ts +63 -0
- package/src/lib/types.ts +295 -0
- package/src/lib/utils.ts +182 -0
- package/src/lib/vim-buffer-actions.ts +732 -0
- package/src/providers/agent.tsx +1075 -0
- package/src/providers/api-client.tsx +43 -0
- package/src/services/logger.ts +85 -0
- package/src/terminal/detection.ts +187 -0
- package/src/terminal/exit.ts +279 -0
- package/src/terminal/notification.ts +83 -0
- package/src/terminal/progress.ts +201 -0
- package/src/terminal/setup.ts +797 -0
- package/src/terminal/suspend.ts +58 -0
- package/src/terminal/types.ts +51 -0
- package/src/theme/context.tsx +57 -0
- package/src/theme/index.ts +4 -0
- package/src/theme/themed.tsx +35 -0
- package/src/theme/themes.json +546 -0
- package/src/theme/types.ts +110 -0
- package/src/tools/types.ts +59 -0
- package/src/tools/utils/__tests__/zod-coercion.test.ts +33 -0
- package/src/tools/utils/tool-ui-components.tsx +631 -0
- package/src/tools/utils/zod-coercion.ts +35 -0
- package/tsconfig.json +11 -0
- package/tsconfig.node.json +29 -0
- package/tsconfig.test.json +27 -0
- package/tsdown.config.ts +17 -0
- package/vitest.config.ts +76 -0
- package/README.md +0 -28
- package/dist/index.js +0 -26
|
@@ -0,0 +1,1006 @@
|
|
|
1
|
+
import type { IAbacusConvoListResponse } from "@codellm/api";
|
|
2
|
+
|
|
3
|
+
import { PromptHistory } from "@codellm/agent";
|
|
4
|
+
import { getDeviceId } from "@codellm/auth";
|
|
5
|
+
import { View, Text } from "@codellm/jar";
|
|
6
|
+
import { product } from "@codellm/product";
|
|
7
|
+
import {
|
|
8
|
+
type Ref,
|
|
9
|
+
memo,
|
|
10
|
+
Suspense,
|
|
11
|
+
useCallback,
|
|
12
|
+
useEffect,
|
|
13
|
+
useImperativeHandle,
|
|
14
|
+
useMemo,
|
|
15
|
+
useRef,
|
|
16
|
+
useState,
|
|
17
|
+
} from "react";
|
|
18
|
+
|
|
19
|
+
import type { InterruptHints } from "../../hooks/use-interrupt-manager.js";
|
|
20
|
+
|
|
21
|
+
import { useAgentMode } from "../../context/agent-mode.js";
|
|
22
|
+
import { useExtensionFile } from "../../context/extension-file.js";
|
|
23
|
+
import { useShellSize } from "../../context/shell-size.js";
|
|
24
|
+
import { useAgent } from "../../hooks/use-agent.js";
|
|
25
|
+
import { useApiClient } from "../../hooks/use-api-client.js";
|
|
26
|
+
import { useAvailableHeight } from "../../hooks/use-available-height.js";
|
|
27
|
+
import { useModels } from "../../hooks/use-models.js";
|
|
28
|
+
import {
|
|
29
|
+
clipboardHasImage,
|
|
30
|
+
saveClipboardImage,
|
|
31
|
+
readClipboardText,
|
|
32
|
+
isMacOSTempScreenshot,
|
|
33
|
+
copyTempScreenshotToProject,
|
|
34
|
+
} from "../../lib/clipboard.js";
|
|
35
|
+
import { MAX_RECENT_CONVERSATIONS } from "../../lib/constants.js";
|
|
36
|
+
import {
|
|
37
|
+
parsePastedPaths,
|
|
38
|
+
prettifyMentionPath,
|
|
39
|
+
escapePathForMention,
|
|
40
|
+
} from "../../lib/path-paste.js";
|
|
41
|
+
import { abbreviateHome } from "../../lib/path.js";
|
|
42
|
+
import { isWindows, sleep } from "../../lib/utils.js";
|
|
43
|
+
import { AgentStatus } from "../../providers/agent.js";
|
|
44
|
+
import { gracefulExit } from "../../terminal/exit.js";
|
|
45
|
+
import { useTheme } from "../../theme/index.js";
|
|
46
|
+
import { Input, InputKeyDownEvent } from "../ui/input.js";
|
|
47
|
+
import { registerDefaultCommands } from "./commands/default-commands.js";
|
|
48
|
+
import { commandRegistry } from "./commands/registry.js";
|
|
49
|
+
import { ComposerProvider, useComposerContext } from "./context.js";
|
|
50
|
+
import { buildMentionGrammar, getMentionAtCursor } from "./mentions.js";
|
|
51
|
+
import { MessageQueue } from "./message-queue.js";
|
|
52
|
+
import { ModePanel } from "./mode-panel.js";
|
|
53
|
+
import { useModeHandlers } from "./modes/index.js";
|
|
54
|
+
import { ComposerMode, ModeMeta } from "./types.js";
|
|
55
|
+
|
|
56
|
+
const MODE_META: Record<ComposerMode, ModeMeta> = {
|
|
57
|
+
[ComposerMode.Prompt]: {
|
|
58
|
+
color: undefined,
|
|
59
|
+
placeholder: 'Try "Build a ecommerce website..."',
|
|
60
|
+
prefix: "❯",
|
|
61
|
+
},
|
|
62
|
+
[ComposerMode.Help]: {
|
|
63
|
+
color: undefined,
|
|
64
|
+
placeholder: 'Try "Build a ecommerce website..."',
|
|
65
|
+
prefix: "❯",
|
|
66
|
+
},
|
|
67
|
+
[ComposerMode.Bash]: {
|
|
68
|
+
color: "greenBright",
|
|
69
|
+
placeholder: isWindows() ? 'Try "dir"' : 'Try "ls -la"',
|
|
70
|
+
prefix: "!",
|
|
71
|
+
},
|
|
72
|
+
[ComposerMode.Command]: {
|
|
73
|
+
color: "magentaBright",
|
|
74
|
+
placeholder: 'Try "/resume <chat title>"',
|
|
75
|
+
prefix: "❯",
|
|
76
|
+
},
|
|
77
|
+
[ComposerMode.Files]: { color: "cyanBright", placeholder: 'Try "@help.md"', prefix: "@" },
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const MODE_TRIGGERS: Record<
|
|
81
|
+
string,
|
|
82
|
+
{ mode: ComposerMode; preventDefault: boolean; atStartOnly: boolean }
|
|
83
|
+
> = {
|
|
84
|
+
"!": { mode: ComposerMode.Bash, preventDefault: true, atStartOnly: true },
|
|
85
|
+
"?": { mode: ComposerMode.Help, preventDefault: true, atStartOnly: true },
|
|
86
|
+
"/": { mode: ComposerMode.Command, preventDefault: true, atStartOnly: true },
|
|
87
|
+
"@": { mode: ComposerMode.Files, preventDefault: false, atStartOnly: false },
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export interface ComposerHandle {
|
|
91
|
+
hasText: () => boolean;
|
|
92
|
+
hasQueue: () => boolean;
|
|
93
|
+
saveAndClear: () => void;
|
|
94
|
+
clear: () => void;
|
|
95
|
+
clearQueue: () => void;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const ComposerInner = memo(
|
|
99
|
+
({ hints, composerRef }: { hints?: InterruptHints; composerRef?: Ref<ComposerHandle> }) => {
|
|
100
|
+
const {
|
|
101
|
+
prompt,
|
|
102
|
+
setPrompt,
|
|
103
|
+
mode,
|
|
104
|
+
setMode,
|
|
105
|
+
bufferRef,
|
|
106
|
+
activity,
|
|
107
|
+
setActivity,
|
|
108
|
+
queuedMessages,
|
|
109
|
+
addToQueue,
|
|
110
|
+
clearQueue,
|
|
111
|
+
attachments,
|
|
112
|
+
addAttachment,
|
|
113
|
+
clearAttachments,
|
|
114
|
+
} = useComposerContext();
|
|
115
|
+
|
|
116
|
+
// Inline paste helpers — delegate to TextBuffer's built-in placeholder system
|
|
117
|
+
const expandPlaceholders = useCallback(
|
|
118
|
+
(text: string) => bufferRef.current?.getExpandedText(text) ?? text,
|
|
119
|
+
[bufferRef],
|
|
120
|
+
);
|
|
121
|
+
const clearPlaceholders = useCallback(() => {
|
|
122
|
+
// no-op: TextBuffer clears its own pastedContent map on setText("")
|
|
123
|
+
}, []);
|
|
124
|
+
|
|
125
|
+
// Non-bracketed paste debounce accumulator (for terminals without BP support)
|
|
126
|
+
const nonBpAccumRef = useRef<{ text: string; timer: ReturnType<typeof setTimeout> | null }>({
|
|
127
|
+
text: "",
|
|
128
|
+
timer: null,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// ── Prompt history (persisted across sessions) ──
|
|
132
|
+
const promptHistoryRef = useRef<PromptHistory | null>(null);
|
|
133
|
+
if (!promptHistoryRef.current) {
|
|
134
|
+
promptHistoryRef.current = new PromptHistory("cli-repl");
|
|
135
|
+
void promptHistoryRef.current.load();
|
|
136
|
+
}
|
|
137
|
+
const promptHistory = promptHistoryRef.current;
|
|
138
|
+
const draftRef = useRef<string>("");
|
|
139
|
+
const prevTextLenRef = useRef(0);
|
|
140
|
+
|
|
141
|
+
// Expose imperative handle for interrupt manager
|
|
142
|
+
useImperativeHandle(composerRef, () => ({
|
|
143
|
+
hasText: () => prompt.length > 0,
|
|
144
|
+
hasQueue: () => queuedMessages.length > 0,
|
|
145
|
+
saveAndClear: () => {
|
|
146
|
+
const text = prompt.trim();
|
|
147
|
+
if (text) {
|
|
148
|
+
promptHistory.append(text);
|
|
149
|
+
promptHistory.resetNavigation();
|
|
150
|
+
}
|
|
151
|
+
bufferRef.current?.setText("");
|
|
152
|
+
clearPlaceholders();
|
|
153
|
+
clearAttachments();
|
|
154
|
+
},
|
|
155
|
+
clear: () => {
|
|
156
|
+
bufferRef.current?.setText("");
|
|
157
|
+
clearPlaceholders();
|
|
158
|
+
clearAttachments();
|
|
159
|
+
},
|
|
160
|
+
clearQueue: () => clearQueue(),
|
|
161
|
+
}));
|
|
162
|
+
const { currentFile, selectionStartLine, selectionEndLine } = useExtensionFile();
|
|
163
|
+
const {
|
|
164
|
+
sendMessage,
|
|
165
|
+
clearSegments,
|
|
166
|
+
clearSegmentsOnly,
|
|
167
|
+
clearTempSegments,
|
|
168
|
+
stop,
|
|
169
|
+
addNotification,
|
|
170
|
+
mcp,
|
|
171
|
+
addAllowedDirectory,
|
|
172
|
+
refreshMcpTools,
|
|
173
|
+
loadConversation,
|
|
174
|
+
agentStatus,
|
|
175
|
+
segmentsLength,
|
|
176
|
+
dequeuedOnStop,
|
|
177
|
+
clearDequeuedOnStop,
|
|
178
|
+
networkOnline,
|
|
179
|
+
} = useAgent();
|
|
180
|
+
const { apiClient, authManager } = useApiClient();
|
|
181
|
+
const { data: models = [], refetch: refetchModels } = useModels(apiClient);
|
|
182
|
+
const { width: terminalWidth } = useShellSize();
|
|
183
|
+
const { cycleAgentMode, resetToNormal, isWaitingForYoloConfirm } = useAgentMode();
|
|
184
|
+
const { setTheme } = useTheme();
|
|
185
|
+
const availableHeight = useAvailableHeight();
|
|
186
|
+
const clearBuffer = useCallback(() => {
|
|
187
|
+
bufferRef.current?.setText("");
|
|
188
|
+
}, [bufferRef]);
|
|
189
|
+
const { handlers, getActivity, bashNavigateHistory } = useModeHandlers(
|
|
190
|
+
() => setMode(ComposerMode.Prompt),
|
|
191
|
+
clearBuffer,
|
|
192
|
+
);
|
|
193
|
+
const currentHandler = handlers.get(mode) || null;
|
|
194
|
+
const [conversations, setConversations] = useState<
|
|
195
|
+
NonNullable<IAbacusConvoListResponse["result"]>
|
|
196
|
+
>([]);
|
|
197
|
+
const [selectedModel, setSelectedModel] = useState<string>("");
|
|
198
|
+
const [isLoggedIn, setIsLoggedIn] = useState<boolean | null>(null);
|
|
199
|
+
|
|
200
|
+
const loadConversations = useCallback(async () => {
|
|
201
|
+
try {
|
|
202
|
+
const response = await apiClient.getConversationsList(product.agentType, process.cwd());
|
|
203
|
+
if (response?.result) {
|
|
204
|
+
setConversations(response.result.slice(0, MAX_RECENT_CONVERSATIONS));
|
|
205
|
+
}
|
|
206
|
+
} catch {
|
|
207
|
+
// Silently fail - conversations list is optional
|
|
208
|
+
}
|
|
209
|
+
}, [apiClient]);
|
|
210
|
+
|
|
211
|
+
useEffect(() => {
|
|
212
|
+
void loadConversations();
|
|
213
|
+
}, [loadConversations]);
|
|
214
|
+
|
|
215
|
+
useEffect(() => {
|
|
216
|
+
apiClient
|
|
217
|
+
.getUserInfo()
|
|
218
|
+
.then((res) => setIsLoggedIn(!!res?.result?.email))
|
|
219
|
+
.catch(() => {
|
|
220
|
+
// Network error or other failure — fall back to local auth check.
|
|
221
|
+
// Don't assume "not logged in" just because the server is unreachable.
|
|
222
|
+
setIsLoggedIn(authManager.isAuthenticated());
|
|
223
|
+
});
|
|
224
|
+
}, [apiClient, authManager]);
|
|
225
|
+
|
|
226
|
+
useEffect(() => {
|
|
227
|
+
if (dequeuedOnStop) {
|
|
228
|
+
bufferRef.current?.setText(dequeuedOnStop);
|
|
229
|
+
clearDequeuedOnStop();
|
|
230
|
+
}
|
|
231
|
+
}, [dequeuedOnStop, clearDequeuedOnStop, bufferRef]);
|
|
232
|
+
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
registerDefaultCommands({
|
|
235
|
+
onExit: () => {
|
|
236
|
+
gracefulExit(0);
|
|
237
|
+
},
|
|
238
|
+
onNewChat: () => {
|
|
239
|
+
clearSegments();
|
|
240
|
+
clearQueue();
|
|
241
|
+
bufferRef.current?.setText("");
|
|
242
|
+
},
|
|
243
|
+
onClearChat: () => {
|
|
244
|
+
clearSegmentsOnly();
|
|
245
|
+
bufferRef.current?.setText("");
|
|
246
|
+
},
|
|
247
|
+
onStop: () => {
|
|
248
|
+
stop();
|
|
249
|
+
},
|
|
250
|
+
onModelChange: (llmName: string) => {
|
|
251
|
+
const matchedModel = models.find((m) => m.llmName === llmName);
|
|
252
|
+
if (matchedModel) {
|
|
253
|
+
setSelectedModel(matchedModel.llmName);
|
|
254
|
+
bufferRef.current?.setText("");
|
|
255
|
+
addNotification(
|
|
256
|
+
`Switched to model: ${matchedModel.name || matchedModel.llmName}`,
|
|
257
|
+
"info",
|
|
258
|
+
);
|
|
259
|
+
} else {
|
|
260
|
+
addNotification(`Model not found`, "error");
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
onResumeChat: async (title: string) => {
|
|
264
|
+
const matchedConversation = conversations.find((conv) => {
|
|
265
|
+
const convTitle = conv.name || conv.deploymentConversationId || "Untitled";
|
|
266
|
+
return convTitle.toLowerCase().includes(title.toLowerCase());
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
if (matchedConversation?.deploymentConversationId) {
|
|
270
|
+
try {
|
|
271
|
+
// Clear queue and buffer before loading conversation
|
|
272
|
+
// This ensures state is properly reset when resuming multiple times
|
|
273
|
+
clearQueue();
|
|
274
|
+
bufferRef.current?.setText("");
|
|
275
|
+
await loadConversation(matchedConversation.deploymentConversationId);
|
|
276
|
+
addNotification(
|
|
277
|
+
`Resumed conversation: ${matchedConversation.name || matchedConversation.deploymentConversationId}`,
|
|
278
|
+
"info",
|
|
279
|
+
);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
addNotification(
|
|
282
|
+
`Failed to load conversation: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
283
|
+
"error",
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
addNotification(`Conversation "${title}" not found`, "error");
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
onLogin: async (emailOrApiKey?: string) => {
|
|
291
|
+
try {
|
|
292
|
+
if (authManager.isEnvAuth()) {
|
|
293
|
+
addNotification(
|
|
294
|
+
"You are authenticated via an environment variable. Unset it to use /login or /logout.",
|
|
295
|
+
"error",
|
|
296
|
+
);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
if (!emailOrApiKey) {
|
|
301
|
+
addNotification("Usage: /login <email> or /login <api_key>", "error");
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
306
|
+
const isEmail = emailRegex.test(emailOrApiKey);
|
|
307
|
+
|
|
308
|
+
if (isEmail) {
|
|
309
|
+
const email = emailOrApiKey;
|
|
310
|
+
const deviceId = await getDeviceId();
|
|
311
|
+
const response = await apiClient.authenticateDeviceForCodeLlm(email, deviceId);
|
|
312
|
+
if (!response?.success || !response?.result) {
|
|
313
|
+
addNotification(response?.error ?? "Failed to start device code login", "error");
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const { verificationUri, userCode, pollingInterval } = response.result;
|
|
318
|
+
addNotification(
|
|
319
|
+
`Open the URL and enter the code to authenticate.\n\nURL: ${verificationUri}\nCode: ${userCode}\n\nPolling for completion...`,
|
|
320
|
+
"info",
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
void (async () => {
|
|
324
|
+
const intervalMs = (pollingInterval || 5) * 1000;
|
|
325
|
+
const deadline = Date.now() + 10 * 60 * 1000;
|
|
326
|
+
while (Date.now() < deadline) {
|
|
327
|
+
await sleep(intervalMs);
|
|
328
|
+
try {
|
|
329
|
+
const res = await apiClient.pollDeviceCode(userCode);
|
|
330
|
+
if (res?.success && res.result?.deviceCode) {
|
|
331
|
+
const token = res.result.deviceCode;
|
|
332
|
+
const chatWebUrl = res.result.chatWebUrl;
|
|
333
|
+
await authManager.loginWithDeviceCode(token);
|
|
334
|
+
if (chatWebUrl) {
|
|
335
|
+
const normalized = chatWebUrl.replace(/^https?:\/\//, "");
|
|
336
|
+
apiClient.setChatWebDomain(normalized);
|
|
337
|
+
}
|
|
338
|
+
addNotification("Authentication successful!", "info");
|
|
339
|
+
setIsLoggedIn(true);
|
|
340
|
+
await loadConversations();
|
|
341
|
+
await refetchModels();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
} catch {
|
|
345
|
+
// keep polling
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
addNotification("Authentication timed out. Please try again.", "error");
|
|
349
|
+
})();
|
|
350
|
+
} else {
|
|
351
|
+
// API key login
|
|
352
|
+
await authManager.loginWithApiKey(emailOrApiKey);
|
|
353
|
+
try {
|
|
354
|
+
const userInfo = await apiClient.getUserInfo();
|
|
355
|
+
if (userInfo?.result?.email) {
|
|
356
|
+
addNotification("API key login successful!", "info");
|
|
357
|
+
setIsLoggedIn(true);
|
|
358
|
+
await loadConversations();
|
|
359
|
+
await refetchModels();
|
|
360
|
+
} else {
|
|
361
|
+
await authManager.logout();
|
|
362
|
+
addNotification("Invalid API key — authentication failed", "error");
|
|
363
|
+
}
|
|
364
|
+
} catch (error) {
|
|
365
|
+
await authManager.logout();
|
|
366
|
+
addNotification(
|
|
367
|
+
`API key authentication failed: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
368
|
+
"error",
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
bufferRef.current?.setText("");
|
|
373
|
+
} catch (error) {
|
|
374
|
+
addNotification(
|
|
375
|
+
`Login failed: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
376
|
+
"error",
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
onLogout: async () => {
|
|
381
|
+
if (authManager.isEnvAuth()) {
|
|
382
|
+
addNotification(
|
|
383
|
+
"You are authenticated via an environment variable. Unset it to use /login or /logout.",
|
|
384
|
+
"error",
|
|
385
|
+
);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
await authManager.logout();
|
|
389
|
+
setIsLoggedIn(false);
|
|
390
|
+
},
|
|
391
|
+
getUserInfo: async () => {
|
|
392
|
+
try {
|
|
393
|
+
const response = await apiClient.getUserInfo();
|
|
394
|
+
if (response?.success && response.result) {
|
|
395
|
+
const { name, email, organization } = response.result;
|
|
396
|
+
const team = organization?.name || "No team";
|
|
397
|
+
const authKind = authManager.getAuthKind();
|
|
398
|
+
const authMethod =
|
|
399
|
+
authKind.type === "env_api_key"
|
|
400
|
+
? "API key (env)"
|
|
401
|
+
: authKind.type === "env_device_code"
|
|
402
|
+
? "Device code (env)"
|
|
403
|
+
: authKind.type === "api_key"
|
|
404
|
+
? "API key"
|
|
405
|
+
: authKind.type === "device_code"
|
|
406
|
+
? "Device code"
|
|
407
|
+
: authKind.type === "oauth_token"
|
|
408
|
+
? "OAuth token"
|
|
409
|
+
: "Unknown";
|
|
410
|
+
addNotification(
|
|
411
|
+
`User Status:\n\nName: ${name || "N/A"}\nEmail: ${email || "N/A"}\nTeam: ${team}\nAuth Method: ${authMethod}`,
|
|
412
|
+
"info",
|
|
413
|
+
);
|
|
414
|
+
} else {
|
|
415
|
+
addNotification("Failed to get user info", "error");
|
|
416
|
+
}
|
|
417
|
+
} catch {
|
|
418
|
+
addNotification("Failed to get user info", "error");
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
getModels: async () => {
|
|
422
|
+
return models
|
|
423
|
+
.filter((m) => m.codeAgentSupported)
|
|
424
|
+
.map((m) => ({
|
|
425
|
+
label: m.name || m.llmName,
|
|
426
|
+
value: m.llmName,
|
|
427
|
+
model: m,
|
|
428
|
+
}));
|
|
429
|
+
},
|
|
430
|
+
getChatTitles: async () => {
|
|
431
|
+
return conversations.map((conv) => {
|
|
432
|
+
const title = conv.name || conv.deploymentConversationId || "Untitled";
|
|
433
|
+
return title.length > 50 ? title.substring(0, 47) + "..." : title;
|
|
434
|
+
});
|
|
435
|
+
},
|
|
436
|
+
addNotification,
|
|
437
|
+
mcp,
|
|
438
|
+
addAllowedDirectory,
|
|
439
|
+
refreshMcpTools,
|
|
440
|
+
getAgentStatus: () => agentStatus,
|
|
441
|
+
setTheme,
|
|
442
|
+
checkEnvAuth: () => ({
|
|
443
|
+
isEnvAuth: authManager.isEnvAuth(),
|
|
444
|
+
shouldHide: authManager.isEnvAuth(),
|
|
445
|
+
}),
|
|
446
|
+
});
|
|
447
|
+
}, [
|
|
448
|
+
models,
|
|
449
|
+
clearSegments,
|
|
450
|
+
clearSegmentsOnly,
|
|
451
|
+
clearQueue,
|
|
452
|
+
bufferRef,
|
|
453
|
+
apiClient,
|
|
454
|
+
refetchModels,
|
|
455
|
+
stop,
|
|
456
|
+
addNotification,
|
|
457
|
+
mcp,
|
|
458
|
+
addAllowedDirectory,
|
|
459
|
+
refreshMcpTools,
|
|
460
|
+
loadConversation,
|
|
461
|
+
conversations,
|
|
462
|
+
loadConversations,
|
|
463
|
+
agentStatus,
|
|
464
|
+
setTheme,
|
|
465
|
+
]);
|
|
466
|
+
|
|
467
|
+
// ── Clipboard attachment helper ─────────────────────────────────────────
|
|
468
|
+
const attachImageFromPath = useCallback(
|
|
469
|
+
async (imagePath: string) => {
|
|
470
|
+
try {
|
|
471
|
+
const { default: fsPromises } = await import("node:fs/promises");
|
|
472
|
+
const { default: pathMod } = await import("node:path");
|
|
473
|
+
const stats = await fsPromises.stat(imagePath);
|
|
474
|
+
const buffer = await fsPromises.readFile(imagePath);
|
|
475
|
+
const ext = pathMod.extname(imagePath).toLowerCase();
|
|
476
|
+
const mimeType = ext === ".jpg" || ext === ".jpeg" ? "image/jpeg" : "image/png";
|
|
477
|
+
const docId =
|
|
478
|
+
"tui-" + Math.random().toString(36).slice(2, 11) + "-" + Date.now().toString(36);
|
|
479
|
+
addAttachment({
|
|
480
|
+
doc_id: docId,
|
|
481
|
+
filename: imagePath,
|
|
482
|
+
filePath: pathMod.resolve(imagePath),
|
|
483
|
+
fileName: pathMod.basename(imagePath),
|
|
484
|
+
content: buffer.toString("base64"),
|
|
485
|
+
mimeType,
|
|
486
|
+
mime_type: mimeType,
|
|
487
|
+
size: stats.size,
|
|
488
|
+
metadata: {
|
|
489
|
+
uploadedAt: new Date().toISOString(),
|
|
490
|
+
source: "tui-clipboard",
|
|
491
|
+
isImage: true,
|
|
492
|
+
encoding: "base64",
|
|
493
|
+
},
|
|
494
|
+
});
|
|
495
|
+
} catch {
|
|
496
|
+
// silently ignore attachment errors
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
[addAttachment],
|
|
500
|
+
);
|
|
501
|
+
|
|
502
|
+
// ── Native clipboard paste handler (Ctrl+V / Cmd+V) ─────────────────────
|
|
503
|
+
// Triggered when the OS clipboard is read directly (not via bracketed paste).
|
|
504
|
+
const handleClipboardPaste = useCallback(async () => {
|
|
505
|
+
// 1. Check for clipboard image first
|
|
506
|
+
if (await clipboardHasImage()) {
|
|
507
|
+
const saved = await saveClipboardImage(process.cwd());
|
|
508
|
+
if (saved) {
|
|
509
|
+
const pretty = prettifyMentionPath(saved);
|
|
510
|
+
const escaped = escapePathForMention(pretty);
|
|
511
|
+
bufferRef.current?.insert(`@${escaped} `);
|
|
512
|
+
await attachImageFromPath(saved);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// 2. Read clipboard text
|
|
518
|
+
const text = await readClipboardText();
|
|
519
|
+
if (!text) return;
|
|
520
|
+
|
|
521
|
+
// 3. Check if it looks like file paths
|
|
522
|
+
const paths = parsePastedPaths(text.trim());
|
|
523
|
+
if (paths) {
|
|
524
|
+
bufferRef.current?.insert(paths);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// 4. Fall back to text insert with large-paste detection
|
|
529
|
+
bufferRef.current?.insert(text, { isPaste: true });
|
|
530
|
+
}, [bufferRef, attachImageFromPath]);
|
|
531
|
+
|
|
532
|
+
const handleKeyDown = useCallback(
|
|
533
|
+
(event: InputKeyDownEvent) => {
|
|
534
|
+
const atStart = event.target.cursor[0] === 0 && event.target.cursor[1] === 0;
|
|
535
|
+
|
|
536
|
+
// ── Ctrl+V / Cmd+V: native clipboard paste ───────────────────────────
|
|
537
|
+
// Only handle when bracketed paste is NOT also firing (guard: key.paste not set)
|
|
538
|
+
if (!event.key.paste) {
|
|
539
|
+
const isCmdV = process.platform === "darwin" && event.key.super && event.input === "v";
|
|
540
|
+
const isCtrlV =
|
|
541
|
+
(process.platform !== "darwin" && event.key.ctrl && event.input === "v") ||
|
|
542
|
+
(process.platform === "linux" &&
|
|
543
|
+
event.key.ctrl &&
|
|
544
|
+
event.key.shift &&
|
|
545
|
+
event.input === "V");
|
|
546
|
+
if (isCmdV || isCtrlV) {
|
|
547
|
+
void handleClipboardPaste();
|
|
548
|
+
event.preventDefault();
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// ── Bracketed paste (key.paste = true) ───────────────────────────────
|
|
554
|
+
if (event.key.paste) {
|
|
555
|
+
const rawText = event.input;
|
|
556
|
+
// Normalize: strip trailing \r\n from drag-and-drop
|
|
557
|
+
const text = rawText.replace(/[\r\n]+$/, "");
|
|
558
|
+
|
|
559
|
+
// 1. macOS temp screenshot path?
|
|
560
|
+
const trimmedSeg = text.trim();
|
|
561
|
+
if (isMacOSTempScreenshot(trimmedSeg)) {
|
|
562
|
+
const copied = copyTempScreenshotToProject(trimmedSeg);
|
|
563
|
+
if (copied) {
|
|
564
|
+
const pretty = prettifyMentionPath(copied);
|
|
565
|
+
const escaped = escapePathForMention(pretty);
|
|
566
|
+
bufferRef.current?.insert(`@${escaped} `);
|
|
567
|
+
void attachImageFromPath(copied);
|
|
568
|
+
event.preventDefault();
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
// 2. Try file path detection
|
|
574
|
+
const paths = parsePastedPaths(text.trim());
|
|
575
|
+
if (paths) {
|
|
576
|
+
bufferRef.current?.insert(paths);
|
|
577
|
+
event.preventDefault();
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// 3. Check OS clipboard for image (e.g. GNOME sends 0x16 / SYN)
|
|
582
|
+
void (async () => {
|
|
583
|
+
if (await clipboardHasImage()) {
|
|
584
|
+
const saved = await saveClipboardImage(process.cwd());
|
|
585
|
+
if (saved) {
|
|
586
|
+
const pretty = prettifyMentionPath(saved);
|
|
587
|
+
const escaped = escapePathForMention(pretty);
|
|
588
|
+
bufferRef.current?.insert(`@${escaped} `);
|
|
589
|
+
await attachImageFromPath(saved);
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
// 4. Regular text paste — large-paste detection handled by TextBuffer
|
|
594
|
+
bufferRef.current?.insert(text, { isPaste: true });
|
|
595
|
+
})();
|
|
596
|
+
event.preventDefault();
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// ── Non-bracketed paste debounce (terminals without BP support) ───────
|
|
601
|
+
// Multi-char input arriving rapidly without key.paste is likely a paste
|
|
602
|
+
if (event.input.length > 3 && !event.key.ctrl && !event.key.meta && !event.key.paste) {
|
|
603
|
+
nonBpAccumRef.current.text += event.input;
|
|
604
|
+
if (nonBpAccumRef.current.timer) clearTimeout(nonBpAccumRef.current.timer);
|
|
605
|
+
nonBpAccumRef.current.timer = setTimeout(() => {
|
|
606
|
+
const accumulated = nonBpAccumRef.current.text;
|
|
607
|
+
nonBpAccumRef.current = { text: "", timer: null };
|
|
608
|
+
const paths = parsePastedPaths(accumulated.trim());
|
|
609
|
+
if (paths) {
|
|
610
|
+
bufferRef.current?.insert(paths);
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
bufferRef.current?.insert(accumulated, { isPaste: true });
|
|
614
|
+
}, 50);
|
|
615
|
+
event.preventDefault();
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// Handle editing queued message with up arrow in Prompt mode
|
|
620
|
+
// Only triggers when buffer is empty (cursor already at top edge)
|
|
621
|
+
if (
|
|
622
|
+
mode === ComposerMode.Prompt &&
|
|
623
|
+
event.key.upArrow &&
|
|
624
|
+
event.target.visualCursor[0] === 0 &&
|
|
625
|
+
queuedMessages.length > 0 &&
|
|
626
|
+
prompt.trim() === ""
|
|
627
|
+
) {
|
|
628
|
+
const queuedMessage = queuedMessages[0];
|
|
629
|
+
bufferRef.current?.setText(queuedMessage);
|
|
630
|
+
clearQueue();
|
|
631
|
+
event.preventDefault();
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
// Handle prompt history navigation for Prompt/Help/Files modes.
|
|
636
|
+
// Only trigger when the cursor is at the extreme edge of the buffer:
|
|
637
|
+
// Up arrow → only when cursor is on the first visual line (top of buffer)
|
|
638
|
+
// Down arrow → only when cursor is on the last visual line (bottom of buffer)
|
|
639
|
+
const usesPromptHistory =
|
|
640
|
+
mode === ComposerMode.Prompt || mode === ComposerMode.Help || mode === ComposerMode.Files;
|
|
641
|
+
|
|
642
|
+
if (usesPromptHistory && (event.key.upArrow || event.key.downArrow)) {
|
|
643
|
+
const buf = event.target;
|
|
644
|
+
const atTopEdge = buf.visualCursor[0] === 0;
|
|
645
|
+
const atBottomEdge = buf.visualCursor[0] >= buf.allVisualLines.length - 1;
|
|
646
|
+
|
|
647
|
+
if (event.key.upArrow && atTopEdge) {
|
|
648
|
+
// Save current text as draft before first navigation
|
|
649
|
+
if (promptHistory.getEntries().length > 0) {
|
|
650
|
+
const currentText = prompt;
|
|
651
|
+
const testEntry = promptHistory.navigateUp();
|
|
652
|
+
if (testEntry) {
|
|
653
|
+
if (draftRef.current === "" && currentText.trim() !== "") {
|
|
654
|
+
draftRef.current = currentText;
|
|
655
|
+
}
|
|
656
|
+
bufferRef.current?.setText(testEntry.message);
|
|
657
|
+
event.preventDefault();
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
} else if (event.key.downArrow && atBottomEdge) {
|
|
662
|
+
const entry = promptHistory.navigateDown();
|
|
663
|
+
if (entry) {
|
|
664
|
+
bufferRef.current?.setText(entry.message);
|
|
665
|
+
} else {
|
|
666
|
+
// Past newest — restore draft
|
|
667
|
+
bufferRef.current?.setText(draftRef.current);
|
|
668
|
+
draftRef.current = "";
|
|
669
|
+
}
|
|
670
|
+
event.preventDefault();
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
// Handle history navigation in bash mode — only at buffer edges
|
|
676
|
+
if (mode === ComposerMode.Bash && bashNavigateHistory) {
|
|
677
|
+
const buf = event.target;
|
|
678
|
+
const bashAtTop = event.key.upArrow && buf.visualCursor[0] === 0;
|
|
679
|
+
const bashAtBottom =
|
|
680
|
+
event.key.downArrow && buf.visualCursor[0] >= buf.allVisualLines.length - 1;
|
|
681
|
+
if (bashAtTop || bashAtBottom) {
|
|
682
|
+
const historyCommand = bashNavigateHistory(event.key.upArrow ? "up" : "down");
|
|
683
|
+
if (historyCommand !== null) {
|
|
684
|
+
bufferRef.current?.setText(historyCommand);
|
|
685
|
+
event.preventDefault();
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (currentHandler?.shouldPreventDefault) {
|
|
692
|
+
if (currentHandler.shouldPreventDefault(event.input || "", event.key)) {
|
|
693
|
+
event.preventDefault();
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if (atStart && event.key.backspace) {
|
|
699
|
+
if (isWaitingForYoloConfirm) {
|
|
700
|
+
resetToNormal();
|
|
701
|
+
event.preventDefault();
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
if (mode !== ComposerMode.Prompt) {
|
|
705
|
+
setMode(ComposerMode.Prompt);
|
|
706
|
+
promptHistory.resetNavigation();
|
|
707
|
+
draftRef.current = "";
|
|
708
|
+
event.preventDefault();
|
|
709
|
+
}
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
if (mode === ComposerMode.Prompt && event.key.shift && event.key.tab) {
|
|
714
|
+
cycleAgentMode();
|
|
715
|
+
event.preventDefault();
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
const trigger = MODE_TRIGGERS[event.input];
|
|
720
|
+
if (trigger && (!trigger.atStartOnly || atStart)) {
|
|
721
|
+
if (mode !== trigger.mode) {
|
|
722
|
+
setMode(trigger.mode);
|
|
723
|
+
}
|
|
724
|
+
if (trigger.preventDefault && trigger.mode !== ComposerMode.Command) {
|
|
725
|
+
event.preventDefault();
|
|
726
|
+
}
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
},
|
|
730
|
+
[
|
|
731
|
+
mode,
|
|
732
|
+
setMode,
|
|
733
|
+
currentHandler,
|
|
734
|
+
bashNavigateHistory,
|
|
735
|
+
bufferRef,
|
|
736
|
+
handleClipboardPaste,
|
|
737
|
+
attachImageFromPath,
|
|
738
|
+
queuedMessages,
|
|
739
|
+
prompt,
|
|
740
|
+
clearQueue,
|
|
741
|
+
isWaitingForYoloConfirm,
|
|
742
|
+
resetToNormal,
|
|
743
|
+
cycleAgentMode,
|
|
744
|
+
promptHistory,
|
|
745
|
+
],
|
|
746
|
+
);
|
|
747
|
+
|
|
748
|
+
const handleChange = useCallback(
|
|
749
|
+
(text: string, buffer: any) => {
|
|
750
|
+
const prevLen = prevTextLenRef.current;
|
|
751
|
+
prevTextLenRef.current = text.length;
|
|
752
|
+
setPrompt(text);
|
|
753
|
+
|
|
754
|
+
// Clear temporary messages (like help) when user starts typing a regular message
|
|
755
|
+
const trimmed = text.trim();
|
|
756
|
+
const startsWithSlash = trimmed.startsWith("/");
|
|
757
|
+
const isAgentBusy =
|
|
758
|
+
agentStatus === AgentStatus.Submitted ||
|
|
759
|
+
agentStatus === AgentStatus.Streaming ||
|
|
760
|
+
agentStatus === AgentStatus.ExecutingTool ||
|
|
761
|
+
agentStatus === AgentStatus.WaitingForToolPermission;
|
|
762
|
+
|
|
763
|
+
if (text.length > 0 && !startsWithSlash && !isAgentBusy) {
|
|
764
|
+
clearTempSegments();
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
const mention = getMentionAtCursor(text, buffer.cursor);
|
|
768
|
+
|
|
769
|
+
// Auto-reset to Prompt mode when user deletes all text in trigger-based modes.
|
|
770
|
+
// Only triggers when the buffer previously had content (prevLen > 0), i.e. user
|
|
771
|
+
// backspaced to empty. Does NOT trigger when entering a mode on an already-empty
|
|
772
|
+
// buffer (e.g. typing "!" switches to Bash with empty buffer — stays in Bash).
|
|
773
|
+
if (
|
|
774
|
+
trimmed.length === 0 &&
|
|
775
|
+
prevLen > 0 &&
|
|
776
|
+
(mode === ComposerMode.Command ||
|
|
777
|
+
mode === ComposerMode.Bash ||
|
|
778
|
+
mode === ComposerMode.Help)
|
|
779
|
+
) {
|
|
780
|
+
setMode(ComposerMode.Prompt);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
if (mention) {
|
|
785
|
+
if (mode !== ComposerMode.Files) setMode(ComposerMode.Files);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
if (mode === ComposerMode.Files) {
|
|
790
|
+
if (!mention) {
|
|
791
|
+
setMode(ComposerMode.Prompt);
|
|
792
|
+
}
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
if (mode === ComposerMode.Help && trimmed.length > 0) {
|
|
797
|
+
setMode(ComposerMode.Prompt);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
if (startsWithSlash && mode !== ComposerMode.Command) {
|
|
801
|
+
setMode(ComposerMode.Command);
|
|
802
|
+
} else if (!startsWithSlash && mode === ComposerMode.Command && trimmed.length > 0) {
|
|
803
|
+
setMode(ComposerMode.Prompt);
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
[mode, setPrompt, setMode, clearTempSegments, agentStatus],
|
|
807
|
+
);
|
|
808
|
+
|
|
809
|
+
const handleSubmit = useCallback(
|
|
810
|
+
async (value: string) => {
|
|
811
|
+
const trimmed = value.trim();
|
|
812
|
+
if (!trimmed.startsWith("/")) {
|
|
813
|
+
const aliasMatch = commandRegistry.match(trimmed, mode);
|
|
814
|
+
if (aliasMatch && (await commandRegistry.execute(aliasMatch))) {
|
|
815
|
+
bufferRef.current?.setText("");
|
|
816
|
+
clearAttachments();
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
if (currentHandler?.onSubmit) {
|
|
822
|
+
currentHandler.onSubmit(value);
|
|
823
|
+
if (mode === ComposerMode.Bash) {
|
|
824
|
+
// Clear the input after submitting bash command
|
|
825
|
+
bufferRef.current?.setText("");
|
|
826
|
+
setActivity(getActivity());
|
|
827
|
+
} else if (mode === ComposerMode.Command) {
|
|
828
|
+
setMode(ComposerMode.Prompt);
|
|
829
|
+
}
|
|
830
|
+
clearAttachments();
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
switch (mode) {
|
|
835
|
+
case ComposerMode.Prompt:
|
|
836
|
+
if (isLoggedIn === false) {
|
|
837
|
+
addNotification("Not logged in. Use /login email@example.com to login.", "warning");
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
if (!networkOnline) {
|
|
842
|
+
addNotification("You are offline — check your connection and try again.", "error");
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
const isAgentBusy =
|
|
847
|
+
agentStatus === AgentStatus.Submitted ||
|
|
848
|
+
agentStatus === AgentStatus.Streaming ||
|
|
849
|
+
agentStatus === AgentStatus.ExecutingTool ||
|
|
850
|
+
agentStatus === AgentStatus.WaitingForToolPermission;
|
|
851
|
+
|
|
852
|
+
if (isAgentBusy) {
|
|
853
|
+
const expanded = expandPlaceholders(value.trim());
|
|
854
|
+
addToQueue(expanded);
|
|
855
|
+
promptHistory.append(expanded);
|
|
856
|
+
promptHistory.resetNavigation();
|
|
857
|
+
draftRef.current = "";
|
|
858
|
+
clearPlaceholders();
|
|
859
|
+
bufferRef.current?.setText("");
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
// Fall back to the first available model when none is explicitly selected,
|
|
864
|
+
// so the session never operates with an empty model string.
|
|
865
|
+
const modelToUse = selectedModel || models[0]?.llmName || undefined;
|
|
866
|
+
|
|
867
|
+
// Expand placeholder text with actual pasted content
|
|
868
|
+
const finalMessage = expandPlaceholders(value);
|
|
869
|
+
|
|
870
|
+
promptHistory.append(finalMessage);
|
|
871
|
+
promptHistory.resetNavigation();
|
|
872
|
+
draftRef.current = "";
|
|
873
|
+
|
|
874
|
+
void sendMessage({ message: finalMessage, selectedModel: modelToUse });
|
|
875
|
+
clearPlaceholders();
|
|
876
|
+
clearAttachments();
|
|
877
|
+
break;
|
|
878
|
+
default:
|
|
879
|
+
}
|
|
880
|
+
},
|
|
881
|
+
[
|
|
882
|
+
mode,
|
|
883
|
+
currentHandler,
|
|
884
|
+
sendMessage,
|
|
885
|
+
getActivity,
|
|
886
|
+
setActivity,
|
|
887
|
+
selectedModel,
|
|
888
|
+
setMode,
|
|
889
|
+
bufferRef,
|
|
890
|
+
agentStatus,
|
|
891
|
+
addToQueue,
|
|
892
|
+
addNotification,
|
|
893
|
+
attachments,
|
|
894
|
+
clearAttachments,
|
|
895
|
+
isLoggedIn,
|
|
896
|
+
expandPlaceholders,
|
|
897
|
+
clearPlaceholders,
|
|
898
|
+
promptHistory,
|
|
899
|
+
],
|
|
900
|
+
);
|
|
901
|
+
|
|
902
|
+
const { color: modeColor, placeholder: modePlaceholder, prefix: modePrefix } = MODE_META[mode];
|
|
903
|
+
|
|
904
|
+
// Clear placeholder text after first message has been sent
|
|
905
|
+
const effectivePlaceholder = segmentsLength > 0 ? "" : modePlaceholder;
|
|
906
|
+
|
|
907
|
+
const inputWidth = useMemo(() => {
|
|
908
|
+
if (terminalWidth === undefined) return undefined;
|
|
909
|
+
return Math.max(1, terminalWidth - 3);
|
|
910
|
+
}, [terminalWidth]);
|
|
911
|
+
|
|
912
|
+
const grammar = useMemo(() => buildMentionGrammar(), []);
|
|
913
|
+
const effectiveActivity = mode === ComposerMode.Bash ? getActivity() : activity;
|
|
914
|
+
const isInputDisabled = effectiveActivity === "pending";
|
|
915
|
+
|
|
916
|
+
const pathText = (
|
|
917
|
+
<Text wrap="truncate-middle" color="yellowBright">
|
|
918
|
+
{abbreviateHome(process.cwd())}
|
|
919
|
+
</Text>
|
|
920
|
+
);
|
|
921
|
+
|
|
922
|
+
const statusAndPath = (
|
|
923
|
+
<View flexDirection="row" gap={1} justifyContent="flex-end">
|
|
924
|
+
{isLoggedIn === false && <Text dimColor>Not logged in</Text>}
|
|
925
|
+
{pathText}
|
|
926
|
+
</View>
|
|
927
|
+
);
|
|
928
|
+
|
|
929
|
+
return (
|
|
930
|
+
<View flexDirection="column">
|
|
931
|
+
{queuedMessages.length > 0 ? (
|
|
932
|
+
<MessageQueue pathElement={statusAndPath} />
|
|
933
|
+
) : (
|
|
934
|
+
<View justifyContent="flex-end">{statusAndPath}</View>
|
|
935
|
+
)}
|
|
936
|
+
<View
|
|
937
|
+
borderStyle="single"
|
|
938
|
+
borderLeft={false}
|
|
939
|
+
borderRight={false}
|
|
940
|
+
borderColor={modeColor}
|
|
941
|
+
borderDimColor
|
|
942
|
+
flexDirection="row"
|
|
943
|
+
gap={1}
|
|
944
|
+
>
|
|
945
|
+
<Text color={modeColor}>{modePrefix}</Text>
|
|
946
|
+
<Input
|
|
947
|
+
ref={bufferRef}
|
|
948
|
+
onChange={handleChange}
|
|
949
|
+
onKeyDown={handleKeyDown}
|
|
950
|
+
onSubmit={handleSubmit}
|
|
951
|
+
placeholder={effectivePlaceholder}
|
|
952
|
+
grammar={grammar}
|
|
953
|
+
disabled={isInputDisabled}
|
|
954
|
+
width={inputWidth}
|
|
955
|
+
/>
|
|
956
|
+
</View>
|
|
957
|
+
<View justifyContent="space-between">
|
|
958
|
+
<ModePanel
|
|
959
|
+
mode={mode}
|
|
960
|
+
prompt={prompt}
|
|
961
|
+
handler={currentHandler}
|
|
962
|
+
onExitFilesMode={() => setMode(ComposerMode.Prompt)}
|
|
963
|
+
hints={hints}
|
|
964
|
+
availableHeight={availableHeight}
|
|
965
|
+
/>
|
|
966
|
+
<View flexDirection="row" gap={1}>
|
|
967
|
+
{currentFile && selectionStartLine !== undefined && selectionEndLine !== undefined ? (
|
|
968
|
+
<Text dimColor>
|
|
969
|
+
<Text color="cyan">{selectionEndLine - selectionStartLine + 1} lines selected</Text>{" "}
|
|
970
|
+
in <Text color="cyan">{currentFile}</Text>
|
|
971
|
+
</Text>
|
|
972
|
+
) : (
|
|
973
|
+
currentFile && (
|
|
974
|
+
<Text dimColor>
|
|
975
|
+
in <Text color="cyan">{currentFile}</Text>
|
|
976
|
+
</Text>
|
|
977
|
+
)
|
|
978
|
+
)}
|
|
979
|
+
{hints?.escClear && (
|
|
980
|
+
<Text wrap="truncate-middle" dimColor>
|
|
981
|
+
Press ESC again to clear input
|
|
982
|
+
</Text>
|
|
983
|
+
)}
|
|
984
|
+
</View>
|
|
985
|
+
</View>
|
|
986
|
+
</View>
|
|
987
|
+
);
|
|
988
|
+
},
|
|
989
|
+
);
|
|
990
|
+
ComposerInner.displayName = "ComposerInner";
|
|
991
|
+
|
|
992
|
+
interface ComposerProps {
|
|
993
|
+
hints?: InterruptHints;
|
|
994
|
+
composerRef?: Ref<ComposerHandle>;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
export const Composer = memo(({ hints, composerRef }: ComposerProps) => {
|
|
998
|
+
return (
|
|
999
|
+
<ComposerProvider>
|
|
1000
|
+
<Suspense fallback={<Text dimColor>Loading models...</Text>}>
|
|
1001
|
+
<ComposerInner hints={hints} composerRef={composerRef} />
|
|
1002
|
+
</Suspense>
|
|
1003
|
+
</ComposerProvider>
|
|
1004
|
+
);
|
|
1005
|
+
});
|
|
1006
|
+
Composer.displayName = "Composer";
|