@supatest/cli 0.0.4 → 0.0.5
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/dist/commands/login.js +392 -0
- package/dist/commands/setup.js +234 -0
- package/dist/config.js +29 -0
- package/dist/core/agent.js +259 -0
- package/dist/index.js +154 -6586
- package/dist/modes/headless.js +117 -0
- package/dist/modes/interactive.js +418 -0
- package/dist/presenters/composite.js +32 -0
- package/dist/presenters/console.js +163 -0
- package/dist/presenters/react.js +217 -0
- package/dist/presenters/types.js +1 -0
- package/dist/presenters/web.js +78 -0
- package/dist/prompts/builder.js +181 -0
- package/dist/prompts/fixer.js +148 -0
- package/dist/prompts/index.js +3 -0
- package/dist/prompts/planner.js +70 -0
- package/dist/services/api-client.js +244 -0
- package/dist/services/event-streamer.js +130 -0
- package/dist/types.js +1 -0
- package/dist/ui/App.js +322 -0
- package/dist/ui/components/AuthBanner.js +24 -0
- package/dist/ui/components/AuthDialog.js +32 -0
- package/dist/ui/components/Banner.js +12 -0
- package/dist/ui/components/ExpandableSection.js +17 -0
- package/dist/ui/components/Header.js +51 -0
- package/dist/ui/components/HelpMenu.js +89 -0
- package/dist/ui/components/InputPrompt.js +286 -0
- package/dist/ui/components/MessageList.js +42 -0
- package/dist/ui/components/QueuedMessageDisplay.js +31 -0
- package/dist/ui/components/Scrollable.js +103 -0
- package/dist/ui/components/SessionSelector.js +196 -0
- package/dist/ui/components/StatusBar.js +34 -0
- package/dist/ui/components/messages/AssistantMessage.js +20 -0
- package/dist/ui/components/messages/ErrorMessage.js +26 -0
- package/dist/ui/components/messages/LoadingMessage.js +28 -0
- package/dist/ui/components/messages/ThinkingMessage.js +17 -0
- package/dist/ui/components/messages/TodoMessage.js +44 -0
- package/dist/ui/components/messages/ToolMessage.js +218 -0
- package/dist/ui/components/messages/UserMessage.js +14 -0
- package/dist/ui/contexts/KeypressContext.js +527 -0
- package/dist/ui/contexts/MouseContext.js +98 -0
- package/dist/ui/contexts/SessionContext.js +129 -0
- package/dist/ui/hooks/useAnimatedScrollbar.js +83 -0
- package/dist/ui/hooks/useBatchedScroll.js +22 -0
- package/dist/ui/hooks/useBracketedPaste.js +31 -0
- package/dist/ui/hooks/useFocus.js +50 -0
- package/dist/ui/hooks/useKeypress.js +26 -0
- package/dist/ui/hooks/useModeToggle.js +25 -0
- package/dist/ui/types/auth.js +13 -0
- package/dist/ui/utils/file-completion.js +56 -0
- package/dist/ui/utils/input.js +50 -0
- package/dist/ui/utils/markdown.js +376 -0
- package/dist/ui/utils/mouse.js +189 -0
- package/dist/ui/utils/theme.js +59 -0
- package/dist/utils/banner.js +9 -0
- package/dist/utils/encryption.js +71 -0
- package/dist/utils/events.js +36 -0
- package/dist/utils/keychain-storage.js +120 -0
- package/dist/utils/logger.js +209 -0
- package/dist/utils/node-version.js +89 -0
- package/dist/utils/plan-file.js +75 -0
- package/dist/utils/project-instructions.js +23 -0
- package/dist/utils/rich-logger.js +208 -0
- package/dist/utils/stdin.js +25 -0
- package/dist/utils/stdio.js +80 -0
- package/dist/utils/summary.js +94 -0
- package/dist/utils/token-storage.js +242 -0
- package/dist/version.js +6 -0
- package/package.json +3 -4
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
4
|
+
import { config as envConfig } from "../config";
|
|
5
|
+
import { loadProjectInstructions } from "../utils/project-instructions";
|
|
6
|
+
export class CoreAgent {
|
|
7
|
+
presenter;
|
|
8
|
+
abortController = null;
|
|
9
|
+
constructor(presenter) {
|
|
10
|
+
this.presenter = presenter;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Abort the current query execution.
|
|
14
|
+
* This will cancel any running operations including LLM calls and tool executions.
|
|
15
|
+
*/
|
|
16
|
+
abort() {
|
|
17
|
+
if (this.abortController) {
|
|
18
|
+
this.abortController.abort();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async run(config) {
|
|
22
|
+
// Create a fresh AbortController for this run
|
|
23
|
+
this.abortController = new AbortController();
|
|
24
|
+
await this.presenter.onStart(config);
|
|
25
|
+
// Resolve path to Claude Code executable
|
|
26
|
+
const claudeCodePath = await this.resolveClaudeCodePath();
|
|
27
|
+
// Build the prompt
|
|
28
|
+
let prompt = config.task;
|
|
29
|
+
if (config.logs) {
|
|
30
|
+
prompt = `${config.task}\n\nHere are the logs to analyze:\n\`\`\`\n${config.logs}\n\`\`\``;
|
|
31
|
+
}
|
|
32
|
+
// Apply permission mode based on agent mode
|
|
33
|
+
// Plan mode uses 'plan' permission which restricts to read-only tools
|
|
34
|
+
// Build mode uses 'bypassPermissions' for full tool access
|
|
35
|
+
const isPlanMode = config.mode === 'plan';
|
|
36
|
+
const cwd = config.cwd || process.cwd();
|
|
37
|
+
// Only load system prompt for new sessions - resumed sessions already have it
|
|
38
|
+
// This avoids duplicating system prompt tokens on every continuation
|
|
39
|
+
const isResumingSession = !!config.providerSessionId;
|
|
40
|
+
let systemPromptAppend;
|
|
41
|
+
if (!isResumingSession) {
|
|
42
|
+
// Load project instructions from SUPATEST.md
|
|
43
|
+
const projectInstructions = loadProjectInstructions(cwd);
|
|
44
|
+
// Combine system prompts: base prompt + project instructions
|
|
45
|
+
systemPromptAppend = [
|
|
46
|
+
config.systemPromptAppend,
|
|
47
|
+
projectInstructions && `\n\n# Project Instructions (from SUPATEST.md)\n\n${projectInstructions}`,
|
|
48
|
+
].filter(Boolean).join("\n") || undefined;
|
|
49
|
+
}
|
|
50
|
+
const queryOptions = {
|
|
51
|
+
// AbortController for cancellation support
|
|
52
|
+
abortController: this.abortController,
|
|
53
|
+
maxTurns: config.maxIterations,
|
|
54
|
+
cwd,
|
|
55
|
+
model: envConfig.anthropicModelName,
|
|
56
|
+
permissionMode: isPlanMode ? "plan" : "bypassPermissions",
|
|
57
|
+
allowDangerouslySkipPermissions: !isPlanMode,
|
|
58
|
+
pathToClaudeCodeExecutable: claudeCodePath,
|
|
59
|
+
includePartialMessages: true,
|
|
60
|
+
executable: "node",
|
|
61
|
+
// MCP servers for enhanced capabilities
|
|
62
|
+
mcpServers: {
|
|
63
|
+
playwright: {
|
|
64
|
+
command: "npx",
|
|
65
|
+
args: ["-y", "@playwright/mcp@latest"],
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
// Resume from previous session if providerSessionId is provided
|
|
69
|
+
// This allows the agent to continue conversations with full context
|
|
70
|
+
// Note: Sessions expire after ~30 days due to Anthropic's data retention policy
|
|
71
|
+
...(config.providerSessionId && {
|
|
72
|
+
resume: config.providerSessionId,
|
|
73
|
+
}),
|
|
74
|
+
// Only append system prompt for new sessions - resumed sessions already have context
|
|
75
|
+
...(systemPromptAppend && {
|
|
76
|
+
systemPrompt: {
|
|
77
|
+
type: "preset",
|
|
78
|
+
preset: "claude_code",
|
|
79
|
+
append: systemPromptAppend,
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
env: {
|
|
83
|
+
...process.env,
|
|
84
|
+
ANTHROPIC_API_KEY: config.supatestApiKey,
|
|
85
|
+
ANTHROPIC_BASE_URL: process.env.ANTHROPIC_BASE_URL || "",
|
|
86
|
+
ANTHROPIC_AUTH_TOKEN: "",
|
|
87
|
+
CLAUDE_CODE_AUTH_TOKEN: "",
|
|
88
|
+
},
|
|
89
|
+
stderr: (msg) => {
|
|
90
|
+
this.presenter.onLog(`[Claude Code stderr] ${msg}`);
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
let resultText = "";
|
|
94
|
+
let hasError = false;
|
|
95
|
+
const errors = [];
|
|
96
|
+
let iterations = 0;
|
|
97
|
+
const filesModified = new Set();
|
|
98
|
+
let wasInterrupted = false;
|
|
99
|
+
// Capture the SDK's session_id for future resume capability
|
|
100
|
+
let providerSessionId;
|
|
101
|
+
// Helper to check if an error indicates an expired/invalid session
|
|
102
|
+
const isSessionExpiredError = (errorMsg) => {
|
|
103
|
+
const expiredPatterns = [
|
|
104
|
+
"no conversation found",
|
|
105
|
+
"session not found",
|
|
106
|
+
"session expired",
|
|
107
|
+
"invalid session",
|
|
108
|
+
];
|
|
109
|
+
const lowerError = errorMsg.toLowerCase();
|
|
110
|
+
return expiredPatterns.some((pattern) => lowerError.includes(pattern));
|
|
111
|
+
};
|
|
112
|
+
// Helper to run the query and process messages
|
|
113
|
+
const runQuery = async (options) => {
|
|
114
|
+
for await (const msg of query({ prompt, options })) {
|
|
115
|
+
// Capture session_id from any message that has it
|
|
116
|
+
// All SDK messages include session_id which we need for resuming
|
|
117
|
+
if ("session_id" in msg && msg.session_id) {
|
|
118
|
+
providerSessionId = msg.session_id;
|
|
119
|
+
}
|
|
120
|
+
if (msg.type === "assistant") {
|
|
121
|
+
iterations++;
|
|
122
|
+
const content = msg.message.content;
|
|
123
|
+
if (Array.isArray(content)) {
|
|
124
|
+
for (const block of content) {
|
|
125
|
+
if (block.type === "text") {
|
|
126
|
+
resultText += block.text + "\n";
|
|
127
|
+
await this.presenter.onAssistantText(block.text);
|
|
128
|
+
}
|
|
129
|
+
else if (block.type === "thinking") {
|
|
130
|
+
await this.presenter.onThinking(block.thinking);
|
|
131
|
+
}
|
|
132
|
+
else if (block.type === "tool_use") {
|
|
133
|
+
const toolName = block.name;
|
|
134
|
+
const input = block.input;
|
|
135
|
+
// Track file modifications
|
|
136
|
+
if ((toolName === "Write" || toolName === "Edit") &&
|
|
137
|
+
input?.file_path) {
|
|
138
|
+
filesModified.add(input.file_path);
|
|
139
|
+
}
|
|
140
|
+
await this.presenter.onToolUse(toolName, input, block.id);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// Notify presenter that the turn is complete
|
|
145
|
+
await this.presenter.onTurnComplete(content);
|
|
146
|
+
}
|
|
147
|
+
else if (msg.type === "result") {
|
|
148
|
+
iterations = msg.num_turns;
|
|
149
|
+
if (msg.subtype === "success") {
|
|
150
|
+
resultText = msg.result || resultText;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
hasError = true;
|
|
154
|
+
if ("errors" in msg && Array.isArray(msg.errors)) {
|
|
155
|
+
errors.push(...msg.errors);
|
|
156
|
+
for (const error of msg.errors) {
|
|
157
|
+
await this.presenter.onError(error);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else if (msg.type === "user") {
|
|
163
|
+
// User message contains tool results - end tool timing
|
|
164
|
+
const userContent = msg.message?.content;
|
|
165
|
+
if (Array.isArray(userContent)) {
|
|
166
|
+
for (const block of userContent) {
|
|
167
|
+
if (block.type === "tool_result" && block.tool_use_id) {
|
|
168
|
+
// Notify presenter of tool result
|
|
169
|
+
if (this.presenter.onToolResult) {
|
|
170
|
+
const resultContent = Array.isArray(block.content)
|
|
171
|
+
? block.content.map((c) => c.text || "").join("\n")
|
|
172
|
+
: typeof block.content === "string"
|
|
173
|
+
? block.content
|
|
174
|
+
: "";
|
|
175
|
+
await this.presenter.onToolResult(block.tool_use_id, resultContent);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
try {
|
|
184
|
+
await runQuery(queryOptions);
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
188
|
+
// Check if this was an abort (user interrupt)
|
|
189
|
+
// The SDK may throw AbortError or a message containing "aborted"
|
|
190
|
+
const isAbortError = (error instanceof Error && error.name === "AbortError") ||
|
|
191
|
+
errorMessage.toLowerCase().includes("aborted");
|
|
192
|
+
if (isAbortError) {
|
|
193
|
+
wasInterrupted = true;
|
|
194
|
+
}
|
|
195
|
+
else if (config.providerSessionId && isSessionExpiredError(errorMessage)) {
|
|
196
|
+
// If the error indicates an expired session and we were trying to resume,
|
|
197
|
+
// show a user-friendly error message
|
|
198
|
+
const expiredMessage = "Can't continue conversation older than 30 days. Please start a new session.";
|
|
199
|
+
await this.presenter.onError(expiredMessage);
|
|
200
|
+
hasError = true;
|
|
201
|
+
errors.push(expiredMessage);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
await this.presenter.onError(errorMessage);
|
|
205
|
+
hasError = true;
|
|
206
|
+
errors.push(errorMessage);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const result = {
|
|
210
|
+
success: !hasError && errors.length === 0 && !wasInterrupted,
|
|
211
|
+
summary: wasInterrupted ? "Interrupted by user" : resultText || "Task completed",
|
|
212
|
+
filesModified: Array.from(filesModified),
|
|
213
|
+
iterations,
|
|
214
|
+
error: wasInterrupted
|
|
215
|
+
? "Interrupted by user"
|
|
216
|
+
: errors.length > 0
|
|
217
|
+
? errors.join("; ")
|
|
218
|
+
: undefined,
|
|
219
|
+
// Include the provider session ID for resume capability
|
|
220
|
+
providerSessionId,
|
|
221
|
+
};
|
|
222
|
+
await this.presenter.onComplete(result);
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
225
|
+
async resolveClaudeCodePath() {
|
|
226
|
+
// Allow override via environment variable
|
|
227
|
+
if (envConfig.claudeCodeExecutablePath) {
|
|
228
|
+
this.presenter.onLog(`Using CLAUDE_CODE_EXECUTABLE_PATH: ${envConfig.claudeCodeExecutablePath}`);
|
|
229
|
+
return envConfig.claudeCodeExecutablePath;
|
|
230
|
+
}
|
|
231
|
+
// Determine binary directory
|
|
232
|
+
const isCompiledBinary = process.execPath && !process.execPath.includes("node");
|
|
233
|
+
let claudeCodePath;
|
|
234
|
+
if (isCompiledBinary) {
|
|
235
|
+
claudeCodePath = join(dirname(process.execPath), "claude-code-cli.js");
|
|
236
|
+
this.presenter.onLog(`Production mode: ${claudeCodePath}`);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
const require = createRequire(import.meta.url);
|
|
240
|
+
const sdkPath = require.resolve("@anthropic-ai/claude-agent-sdk/sdk.mjs");
|
|
241
|
+
claudeCodePath = join(dirname(sdkPath), "cli.js");
|
|
242
|
+
this.presenter.onLog(`Development mode: ${claudeCodePath}`);
|
|
243
|
+
}
|
|
244
|
+
// Verify the file exists
|
|
245
|
+
const fs = await import("node:fs/promises");
|
|
246
|
+
try {
|
|
247
|
+
await fs.access(claudeCodePath);
|
|
248
|
+
this.presenter.onLog(`✓ Claude Code CLI found: ${claudeCodePath}`);
|
|
249
|
+
}
|
|
250
|
+
catch {
|
|
251
|
+
const error = `Claude Code executable not found at: ${claudeCodePath}\n` +
|
|
252
|
+
"For compiled binaries, ensure claude-code-cli.js is in the same directory as the binary.\n" +
|
|
253
|
+
"Set CLAUDE_CODE_EXECUTABLE_PATH environment variable to override.";
|
|
254
|
+
await this.presenter.onError(error);
|
|
255
|
+
throw new Error(error);
|
|
256
|
+
}
|
|
257
|
+
return claudeCodePath;
|
|
258
|
+
}
|
|
259
|
+
}
|