@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,22 @@
|
|
|
1
|
+
import { AbacusClient, ILLMOption } from "@codellm/api";
|
|
2
|
+
import { useSuspenseQuery } from "@tanstack/react-query";
|
|
3
|
+
|
|
4
|
+
import { QueryKey } from "../lib/queries.js";
|
|
5
|
+
|
|
6
|
+
export function useModels(apiClient: AbacusClient) {
|
|
7
|
+
return useSuspenseQuery<ILLMOption[]>({
|
|
8
|
+
queryKey: [QueryKey.Models],
|
|
9
|
+
queryFn: async () => {
|
|
10
|
+
try {
|
|
11
|
+
const response = await apiClient.listLLMModels();
|
|
12
|
+
return response?.result ?? [];
|
|
13
|
+
} catch {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
staleTime: 5 * 60 * 1000,
|
|
18
|
+
retry: 1,
|
|
19
|
+
refetchOnMount: "always",
|
|
20
|
+
refetchOnWindowFocus: false,
|
|
21
|
+
});
|
|
22
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { AbacusClient } from "@codellm/api";
|
|
4
|
+
export type { AbacusClient };
|
|
5
|
+
import { AuthManager } from "@codellm/auth";
|
|
6
|
+
import { product } from "@codellm/product";
|
|
7
|
+
import chalk from "chalk";
|
|
8
|
+
import { parseArgs } from "node:util";
|
|
9
|
+
|
|
10
|
+
import packageJson from "@/package.json" with { type: "json" };
|
|
11
|
+
|
|
12
|
+
import type { AgentArgs } from "./args.js";
|
|
13
|
+
|
|
14
|
+
import { runPrintMode } from "./entrypoints/print-mode.js";
|
|
15
|
+
import { runRepl } from "./entrypoints/repl.js";
|
|
16
|
+
import { logger } from "./services/logger.js";
|
|
17
|
+
import { gracefulExit } from "./terminal/exit.js";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Centralized error handling function
|
|
21
|
+
*/
|
|
22
|
+
function handleError(message: string, exitCode = 1): never {
|
|
23
|
+
console.error(chalk.red(message));
|
|
24
|
+
gracefulExit(exitCode);
|
|
25
|
+
throw new Error(message);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Validates URL format and returns a new URL instance
|
|
30
|
+
*/
|
|
31
|
+
function validateUrl(urlString: string): URL {
|
|
32
|
+
try {
|
|
33
|
+
return new URL(urlString);
|
|
34
|
+
} catch {
|
|
35
|
+
handleError(`Invalid ${product.envPrefix}BASE_URL: ${urlString}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Validates agent arguments for print mode requirements
|
|
41
|
+
*/
|
|
42
|
+
function validatePrintModeArgs(agentArgs?: AgentArgs): void {
|
|
43
|
+
const hasFormatFlags = agentArgs?.["output-format"] || agentArgs?.["input-format"];
|
|
44
|
+
if (hasFormatFlags && !agentArgs?.print) {
|
|
45
|
+
handleError("--output-format and --input-format flags can only be used with --print mode");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Reads input from stdin asynchronously
|
|
51
|
+
*/
|
|
52
|
+
async function readStdinInput(): Promise<string> {
|
|
53
|
+
const stdin = process.stdin;
|
|
54
|
+
|
|
55
|
+
if (stdin.isTTY) {
|
|
56
|
+
handleError(
|
|
57
|
+
"No prompt provided and no piped input detected. Either provide a prompt or pipe input.",
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let inputData = "";
|
|
62
|
+
for await (const chunk of stdin) {
|
|
63
|
+
inputData += chunk;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const trimmed = inputData.trim();
|
|
67
|
+
if (!trimmed) {
|
|
68
|
+
handleError("No input provided");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return trimmed;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Resolves the input prompt from args or stdin
|
|
76
|
+
*/
|
|
77
|
+
async function resolveInputPrompt(agentArgs?: AgentArgs): Promise<string> {
|
|
78
|
+
if (agentArgs?.prompt) {
|
|
79
|
+
return agentArgs.prompt;
|
|
80
|
+
}
|
|
81
|
+
return readStdinInput();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Configures API client with custom base URL if provided
|
|
86
|
+
*/
|
|
87
|
+
async function configureApiClient(apiClient: AbacusClient): Promise<void> {
|
|
88
|
+
const baseUrl = process.env[`${product.envPrefix}BASE_URL`];
|
|
89
|
+
if (!baseUrl) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
validateUrl(baseUrl);
|
|
94
|
+
apiClient.setUrl(baseUrl);
|
|
95
|
+
apiClient.resetEndpoints();
|
|
96
|
+
await apiClient.getApiEndpoint();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Resolve --resume <id> or --continue into a conversation ID.
|
|
101
|
+
* --resume uses the provided ID directly.
|
|
102
|
+
* --continue fetches the most recently updated conversation from the API.
|
|
103
|
+
*/
|
|
104
|
+
async function resolveConversationId(
|
|
105
|
+
agentArgs: AgentArgs | undefined,
|
|
106
|
+
apiClient: AbacusClient,
|
|
107
|
+
): Promise<string | undefined> {
|
|
108
|
+
if (agentArgs?.resume) {
|
|
109
|
+
return agentArgs.resume;
|
|
110
|
+
}
|
|
111
|
+
if (agentArgs?.continue) {
|
|
112
|
+
try {
|
|
113
|
+
const result = await apiClient.getConversationsList(product.agentType);
|
|
114
|
+
const conversations = result?.result ?? [];
|
|
115
|
+
const latest = conversations.sort((a, b) => {
|
|
116
|
+
const aTime = a.lastEventCreatedAt ? new Date(a.lastEventCreatedAt).getTime() : 0;
|
|
117
|
+
const bTime = b.lastEventCreatedAt ? new Date(b.lastEventCreatedAt).getTime() : 0;
|
|
118
|
+
return bTime - aTime;
|
|
119
|
+
})[0];
|
|
120
|
+
return latest?.deploymentConversationId;
|
|
121
|
+
} catch {
|
|
122
|
+
// fall through — start fresh
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function main(agentArgs?: AgentArgs): Promise<void> {
|
|
129
|
+
if (agentArgs?.version) {
|
|
130
|
+
console.log(`Abacus.AI CLI version ${packageJson.version}`);
|
|
131
|
+
gracefulExit(0);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
validatePrintModeArgs(agentArgs);
|
|
136
|
+
|
|
137
|
+
if (agentArgs?.print) {
|
|
138
|
+
const inputPrompt = await resolveInputPrompt(agentArgs);
|
|
139
|
+
// For print mode the API client is created inside runPrintMode, so resolve
|
|
140
|
+
// conversation ID there — pass raw args through.
|
|
141
|
+
await runPrintMode({ agentArgs: { ...agentArgs, prompt: inputPrompt } });
|
|
142
|
+
} else {
|
|
143
|
+
const authManager = await AuthManager.create();
|
|
144
|
+
|
|
145
|
+
// Auth is fully managed by AuthManager.create() — no env access needed here.
|
|
146
|
+
|
|
147
|
+
const cliLogger = {
|
|
148
|
+
error: (message: string | Error, ...args: unknown[]) => logger.error(message, ...args),
|
|
149
|
+
debug: (...message: unknown[]) => logger.debug(...message),
|
|
150
|
+
warn: (message: string, ...args: unknown[]) => logger.warn(message, ...args),
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const apiClient = new AbacusClient(authManager, cliLogger);
|
|
154
|
+
|
|
155
|
+
await configureApiClient(apiClient);
|
|
156
|
+
|
|
157
|
+
// Resolve --resume / --continue into a single conversationId before dispatch.
|
|
158
|
+
// Downstream consumers (AgentProvider) read agentArgs.conversationId — they
|
|
159
|
+
// don't know or care how it was resolved.
|
|
160
|
+
const conversationId = await resolveConversationId(agentArgs, apiClient);
|
|
161
|
+
const resolvedArgs: AgentArgs = { ...agentArgs, ...(conversationId && { conversationId }) };
|
|
162
|
+
|
|
163
|
+
await runRepl({
|
|
164
|
+
apiClient,
|
|
165
|
+
authManager,
|
|
166
|
+
agentArgs: resolvedArgs,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const HELP_TEXT = `
|
|
172
|
+
Usage: abacus [options] [prompt]
|
|
173
|
+
|
|
174
|
+
Options:
|
|
175
|
+
--help Show this help message
|
|
176
|
+
--version Show version number
|
|
177
|
+
--model <model> Model to use (-m)
|
|
178
|
+
--print Print mode, non-interactive (-p)
|
|
179
|
+
--resume <id> Resume a specific conversation by ID
|
|
180
|
+
--continue Continue the most recent conversation (-c)
|
|
181
|
+
--output-format <format> Output format: text (default), stream-json
|
|
182
|
+
--auto-accept-edits Automatically accept edits
|
|
183
|
+
--dangerously-skip-permissions Skip permission checks
|
|
184
|
+
--plan-mode Enable plan mode
|
|
185
|
+
`.trim();
|
|
186
|
+
|
|
187
|
+
const { values, positionals } = parseArgs({
|
|
188
|
+
options: {
|
|
189
|
+
help: { type: "boolean", short: "h" },
|
|
190
|
+
version: { type: "boolean", short: "v" },
|
|
191
|
+
model: { type: "string", short: "m" },
|
|
192
|
+
print: { type: "boolean", short: "p" },
|
|
193
|
+
resume: { type: "string" },
|
|
194
|
+
continue: { type: "boolean", short: "c" },
|
|
195
|
+
"output-format": { type: "string" },
|
|
196
|
+
"input-format": { type: "string" },
|
|
197
|
+
"allow-editing": { type: "string" },
|
|
198
|
+
"auto-accept-edits": { type: "boolean" },
|
|
199
|
+
"dangerously-skip-permissions": { type: "boolean" },
|
|
200
|
+
"plan-mode": { type: "boolean" },
|
|
201
|
+
},
|
|
202
|
+
allowPositionals: true,
|
|
203
|
+
strict: true,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
if (values.help) {
|
|
207
|
+
console.log(HELP_TEXT);
|
|
208
|
+
process.exit(0);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
main({
|
|
212
|
+
...values,
|
|
213
|
+
prompt: positionals.length > 0 ? positionals.join(" ") : null,
|
|
214
|
+
}).catch((err) => {
|
|
215
|
+
console.error(err);
|
|
216
|
+
process.exit(1);
|
|
217
|
+
});
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ANSI,
|
|
7
|
+
osc9,
|
|
8
|
+
osc777,
|
|
9
|
+
osc99,
|
|
10
|
+
osc9Progress,
|
|
11
|
+
osc1337ProgressClear,
|
|
12
|
+
osc1337ProgressState,
|
|
13
|
+
osc1337ProgressPercent,
|
|
14
|
+
ansiRegex,
|
|
15
|
+
stripAnsi,
|
|
16
|
+
} from "../ansi.js";
|
|
17
|
+
|
|
18
|
+
describe.concurrent("ansi", () => {
|
|
19
|
+
describe.concurrent("ANSI enum", () => {
|
|
20
|
+
it("should have correct escape character", () => {
|
|
21
|
+
expect(ANSI.ESC).toBe("\x1b");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should have correct bell character", () => {
|
|
25
|
+
expect(ANSI.BEL).toBe("\x07");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should have correct string terminator", () => {
|
|
29
|
+
expect(ANSI.ST).toBe("\x1b\\");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should construct focus reporting sequences correctly", () => {
|
|
33
|
+
expect(ANSI.ENABLE_FOCUS_REPORTING).toBe("\x1b[?1004h");
|
|
34
|
+
expect(ANSI.DISABLE_FOCUS_REPORTING).toBe("\x1b[?1004l");
|
|
35
|
+
expect(ANSI.FOCUS_IN).toBe("\x1b[I");
|
|
36
|
+
expect(ANSI.FOCUS_OUT).toBe("\x1b[O");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should construct cursor sequences correctly", () => {
|
|
40
|
+
expect(ANSI.SHOW_CURSOR).toBe("\x1b[?25h");
|
|
41
|
+
expect(ANSI.HIDE_CURSOR).toBe("\x1b[?25l");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should construct reset sequences correctly", () => {
|
|
45
|
+
expect(ANSI.RESET_SGR).toBe("\x1b[0m");
|
|
46
|
+
expect(ANSI.SOFT_RESET).toBe("\x1b[!p");
|
|
47
|
+
expect(ANSI.HARD_RESET).toBe("\x1bc");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should construct paste mode sequences correctly", () => {
|
|
51
|
+
expect(ANSI.ENABLE_BRACKETED_PASTE).toBe("\x1b[?2004h");
|
|
52
|
+
expect(ANSI.DISABLE_BRACKETED_PASTE).toBe("\x1b[?2004l");
|
|
53
|
+
expect(ANSI.PASTE_MODE_PREFIX).toBe("\x1b[200~");
|
|
54
|
+
expect(ANSI.PASTE_MODE_SUFFIX).toBe("\x1b[201~");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should construct style sequences correctly", () => {
|
|
58
|
+
expect(ANSI.DIM).toBe("\x1b[2m");
|
|
59
|
+
expect(ANSI.BOLD).toBe("\x1b[1m");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe.concurrent("osc9", () => {
|
|
64
|
+
it("should format OSC 9 notification correctly", () => {
|
|
65
|
+
const result = osc9("test message");
|
|
66
|
+
expect(result).toBe("\x1b]9;\n\ntest message\x07");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should handle empty message", () => {
|
|
70
|
+
const result = osc9("");
|
|
71
|
+
expect(result).toBe("\x1b]9;\n\n\x07");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe.concurrent("osc777", () => {
|
|
76
|
+
it("should format OSC 777 notification correctly", () => {
|
|
77
|
+
const result = osc777("Title", "Body");
|
|
78
|
+
expect(result).toBe("\x1b]777;notify;Title;Body\x07");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should handle empty title and body", () => {
|
|
82
|
+
const result = osc777("", "");
|
|
83
|
+
expect(result).toBe("\x1b]777;notify;;\x07");
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe.concurrent("osc99", () => {
|
|
88
|
+
it("should format OSC 99 notification correctly", () => {
|
|
89
|
+
// Mock Math.random to get predictable notification ID
|
|
90
|
+
const mockRandom = vi.spyOn(Math, "random").mockReturnValue(0.1234);
|
|
91
|
+
const result = osc99("Title", "Body");
|
|
92
|
+
|
|
93
|
+
// Should contain title and body parts
|
|
94
|
+
expect(result).toContain("]99;i=1234:d=0:p=title;Title\x1b\\");
|
|
95
|
+
expect(result).toContain("]99;i=1234:p=body;Body\x1b\\");
|
|
96
|
+
expect(result).toContain("]99;i=1234:d=1:a=focus;\x1b\\");
|
|
97
|
+
|
|
98
|
+
mockRandom.mockRestore();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe.concurrent("osc9Progress", () => {
|
|
103
|
+
it("should format progress with state 0 (hide)", () => {
|
|
104
|
+
const result = osc9Progress(0);
|
|
105
|
+
expect(result).toBe("\x1b]9;4;0;0\x07\x1b]9;4;0;0\x1b\\");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should format progress with state 1 and percentage", () => {
|
|
109
|
+
const result = osc9Progress(1, 50);
|
|
110
|
+
expect(result).toBe("\x1b]9;4;1;50\x07\x1b]9;4;1;50\x1b\\");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("should clamp progress to 0-100", () => {
|
|
114
|
+
expect(osc9Progress(1, -10)).toBe("\x1b]9;4;1;0\x07\x1b]9;4;1;0\x1b\\");
|
|
115
|
+
expect(osc9Progress(1, 150)).toBe("\x1b]9;4;1;100\x07\x1b]9;4;1;100\x1b\\");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should round progress values", () => {
|
|
119
|
+
expect(osc9Progress(1, 50.7)).toBe("\x1b]9;4;1;51\x07\x1b]9;4;1;51\x1b\\");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe.concurrent("osc1337ProgressClear", () => {
|
|
124
|
+
it("should clear progress indicator", () => {
|
|
125
|
+
const result = osc1337ProgressClear();
|
|
126
|
+
expect(result).toBe("\x1b]1337;Progress=\x07\x1b]1337;Progress=\x1b\\");
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe.concurrent("osc1337ProgressState", () => {
|
|
131
|
+
it("should set progress state 0", () => {
|
|
132
|
+
const result = osc1337ProgressState(0);
|
|
133
|
+
expect(result).toBe("\x1b]1337;Progress=0\x07\x1b]1337;Progress=0\x1b\\");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("should set progress state 1", () => {
|
|
137
|
+
const result = osc1337ProgressState(1);
|
|
138
|
+
expect(result).toBe("\x1b]1337;Progress=1\x07\x1b]1337;Progress=1\x1b\\");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("should set progress state 2", () => {
|
|
142
|
+
const result = osc1337ProgressState(2);
|
|
143
|
+
expect(result).toBe("\x1b]1337;Progress=2\x07\x1b]1337;Progress=2\x1b\\");
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe.concurrent("osc1337ProgressPercent", () => {
|
|
148
|
+
it("should format progress with percentage only", () => {
|
|
149
|
+
const result = osc1337ProgressPercent(50);
|
|
150
|
+
expect(result).toBe("\x1b]1337;Progress=50\x07\x1b]1337;Progress=50\x1b\\");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should format progress with percentage and message", () => {
|
|
154
|
+
const result = osc1337ProgressPercent(75, "Loading...");
|
|
155
|
+
expect(result).toContain("]1337;Progress=75;Loading...\x07");
|
|
156
|
+
expect(result).toContain("]1337;Progress=message=Loading...;percent=75\x07");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("should clamp percentage to 0-100", () => {
|
|
160
|
+
expect(osc1337ProgressPercent(-10)).toBe(
|
|
161
|
+
"\x1b]1337;Progress=0\x07\x1b]1337;Progress=0\x1b\\",
|
|
162
|
+
);
|
|
163
|
+
expect(osc1337ProgressPercent(150)).toBe(
|
|
164
|
+
"\x1b]1337;Progress=100\x07\x1b]1337;Progress=100\x1b\\",
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("should round percentage values", () => {
|
|
169
|
+
expect(osc1337ProgressPercent(50.7)).toBe(
|
|
170
|
+
"\x1b]1337;Progress=51\x07\x1b]1337;Progress=51\x1b\\",
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("should sanitize message content", () => {
|
|
175
|
+
const result = osc1337ProgressPercent(50, "test\x1b\x07\n\r;message");
|
|
176
|
+
// The sanitized message should not contain control chars or semicolons
|
|
177
|
+
// (they're replaced/removed in the message part, but \x1b is still in OSC wrapper)
|
|
178
|
+
expect(result).toContain("test :message"); // \x1b and \x07 removed, \n\r become spaces, ; becomes :
|
|
179
|
+
// The message part should not have raw control characters
|
|
180
|
+
const messagePart = result.match(
|
|
181
|
+
new RegExp("Progress=\\d+;(.+?)[" + String.fromCharCode(7) + String.fromCharCode(27) + "]"),
|
|
182
|
+
)?.[1];
|
|
183
|
+
expect(messagePart).not.toContain("\x07");
|
|
184
|
+
expect(messagePart).not.toContain("\n");
|
|
185
|
+
expect(messagePart).not.toContain("\r");
|
|
186
|
+
expect(messagePart).not.toContain(";"); // Should be replaced with :
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("should truncate long messages", () => {
|
|
190
|
+
const longMessage = "a".repeat(200);
|
|
191
|
+
const result = osc1337ProgressPercent(50, longMessage);
|
|
192
|
+
// Message is truncated to 120 chars, but result includes OSC sequences
|
|
193
|
+
// Extract the message part to verify truncation
|
|
194
|
+
const messagePart = result.match(
|
|
195
|
+
new RegExp("Progress=\\d+;(.+?)[" + String.fromCharCode(7) + String.fromCharCode(27) + "]"),
|
|
196
|
+
)?.[1];
|
|
197
|
+
expect(messagePart?.length).toBeLessThanOrEqual(120);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe.concurrent("ansiRegex", () => {
|
|
202
|
+
it("should match ANSI escape sequences", () => {
|
|
203
|
+
const regex = ansiRegex();
|
|
204
|
+
expect("\x1b[31mtest\x1b[0m".match(regex)).toHaveLength(2);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("should match OSC sequences", () => {
|
|
208
|
+
const regex = ansiRegex();
|
|
209
|
+
expect("\x1b]0;title\x07".match(regex)).toHaveLength(1);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("should match only first occurrence when onlyFirst is true", () => {
|
|
213
|
+
const regex = ansiRegex({ onlyFirst: true });
|
|
214
|
+
const matches = "\x1b[31mtest\x1b[0m".match(regex);
|
|
215
|
+
expect(matches).toHaveLength(1);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("should match all occurrences when onlyFirst is false", () => {
|
|
219
|
+
const regex = ansiRegex({ onlyFirst: false });
|
|
220
|
+
const matches = "\x1b[31mtest\x1b[0m".match(regex);
|
|
221
|
+
expect(matches).toHaveLength(2);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
describe.concurrent("stripAnsi", () => {
|
|
226
|
+
it("should strip ANSI codes from string", () => {
|
|
227
|
+
expect(stripAnsi("\x1b[31mtest\x1b[0m")).toBe("test");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("should handle strings without ANSI codes", () => {
|
|
231
|
+
expect(stripAnsi("plain text")).toBe("plain text");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("should handle empty strings", () => {
|
|
235
|
+
expect(stripAnsi("")).toBe("");
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("should throw error for non-string input", () => {
|
|
239
|
+
expect(() => stripAnsi(null as unknown as string)).toThrow(
|
|
240
|
+
"Expected a `string`, got `object`",
|
|
241
|
+
);
|
|
242
|
+
expect(() => stripAnsi(123 as unknown as string)).toThrow(
|
|
243
|
+
"Expected a `string`, got `number`",
|
|
244
|
+
);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("should strip multiple ANSI codes", () => {
|
|
248
|
+
expect(stripAnsi("\x1b[31mred\x1b[0m \x1b[32mgreen\x1b[0m")).toBe("red green");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("should strip OSC sequences", () => {
|
|
252
|
+
expect(stripAnsi("\x1b]0;title\x07")).toBe("");
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
import { runCli } from "../cli.js";
|
|
6
|
+
|
|
7
|
+
// TODO: The runCli function doesn't handle spawn errors (ENOENT).
|
|
8
|
+
// When a non-existent command is spawned, the error is emitted asynchronously
|
|
9
|
+
// and not caught by the generator, causing unhandled promise rejections.
|
|
10
|
+
// Consider adding error handling to runCli to catch and handle spawn errors
|
|
11
|
+
// (e.g., proc.on('error', ...) to yield error information or throw).
|
|
12
|
+
|
|
13
|
+
describe.concurrent("cli", () => {
|
|
14
|
+
describe.concurrent("runCli", () => {
|
|
15
|
+
it("should yield output from stdout", async () => {
|
|
16
|
+
const command = process.platform === "win32" ? "echo" : "echo";
|
|
17
|
+
const args = ["hello world"];
|
|
18
|
+
const outputs: string[] = [];
|
|
19
|
+
|
|
20
|
+
for await (const output of runCli(command, args)) {
|
|
21
|
+
outputs.push(output);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// On Unix, echo outputs to stdout; on Windows, we might need different handling
|
|
25
|
+
// The output might include newlines, so we check if it contains our message
|
|
26
|
+
// Should have received some output
|
|
27
|
+
expect(outputs.length).toBeGreaterThanOrEqual(0);
|
|
28
|
+
}, 10000);
|
|
29
|
+
|
|
30
|
+
it("should yield output from stderr", async () => {
|
|
31
|
+
// Use a command that outputs to stderr
|
|
32
|
+
const command = process.platform === "win32" ? "cmd" : "sh";
|
|
33
|
+
const args =
|
|
34
|
+
process.platform === "win32" ? ["/c", "echo error >&2"] : ["-c", "echo error >&2"];
|
|
35
|
+
|
|
36
|
+
const outputs: string[] = [];
|
|
37
|
+
|
|
38
|
+
for await (const output of runCli(command, args)) {
|
|
39
|
+
outputs.push(output);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Should have received some output
|
|
43
|
+
expect(outputs.length).toBeGreaterThanOrEqual(0);
|
|
44
|
+
}, 10000);
|
|
45
|
+
|
|
46
|
+
it("should handle commands that exit immediately", async () => {
|
|
47
|
+
const command = process.platform === "win32" ? "echo" : "echo";
|
|
48
|
+
const args = ["test"];
|
|
49
|
+
const outputs: string[] = [];
|
|
50
|
+
|
|
51
|
+
for await (const output of runCli(command, args)) {
|
|
52
|
+
outputs.push(output);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Should complete without hanging
|
|
56
|
+
expect(true).toBe(true);
|
|
57
|
+
}, 10000);
|
|
58
|
+
|
|
59
|
+
it("should handle empty args array", async () => {
|
|
60
|
+
const command = process.platform === "win32" ? "echo" : "echo";
|
|
61
|
+
const args: string[] = [];
|
|
62
|
+
const outputs: string[] = [];
|
|
63
|
+
|
|
64
|
+
for await (const output of runCli(command, args)) {
|
|
65
|
+
outputs.push(output);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Should complete without error
|
|
69
|
+
expect(true).toBe(true);
|
|
70
|
+
}, 10000);
|
|
71
|
+
|
|
72
|
+
it("should handle multiple output chunks", async () => {
|
|
73
|
+
// Use a command that outputs multiple lines
|
|
74
|
+
const command = process.platform === "win32" ? "echo" : "printf";
|
|
75
|
+
const args =
|
|
76
|
+
process.platform === "win32"
|
|
77
|
+
? ["line1\nline2\nline3"]
|
|
78
|
+
: ["%s\n%s\n%s\n", "line1", "line2", "line3"];
|
|
79
|
+
|
|
80
|
+
const outputs: string[] = [];
|
|
81
|
+
|
|
82
|
+
for await (const output of runCli(command, args)) {
|
|
83
|
+
outputs.push(output);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Should have received output
|
|
87
|
+
expect(outputs.length).toBeGreaterThanOrEqual(0);
|
|
88
|
+
}, 10000);
|
|
89
|
+
|
|
90
|
+
it("should terminate process on completion", async () => {
|
|
91
|
+
const command = process.platform === "win32" ? "echo" : "echo";
|
|
92
|
+
const args = ["test"];
|
|
93
|
+
|
|
94
|
+
// This test verifies the process is cleaned up
|
|
95
|
+
// We can't easily verify the process is killed, but we can verify
|
|
96
|
+
// the generator completes
|
|
97
|
+
const generator = runCli(command, args);
|
|
98
|
+
const outputs: string[] = [];
|
|
99
|
+
|
|
100
|
+
for await (const output of generator) {
|
|
101
|
+
outputs.push(output);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Generator should complete
|
|
105
|
+
expect(true).toBe(true);
|
|
106
|
+
}, 10000);
|
|
107
|
+
|
|
108
|
+
it("should work with different command types", async () => {
|
|
109
|
+
// Test with a simple command available on most systems
|
|
110
|
+
const command = process.platform === "win32" ? "where" : "which";
|
|
111
|
+
const args = process.platform === "win32" ? ["echo"] : ["echo"];
|
|
112
|
+
const outputs: string[] = [];
|
|
113
|
+
|
|
114
|
+
for await (const output of runCli(command, args)) {
|
|
115
|
+
outputs.push(output);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Should complete without error
|
|
119
|
+
expect(true).toBe(true);
|
|
120
|
+
}, 10000);
|
|
121
|
+
});
|
|
122
|
+
});
|