coder-agent 2.3.3 → 2.3.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/index.js +105 -81
- package/dist/memory.js +46 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -209,111 +209,135 @@ async function main() {
|
|
|
209
209
|
});
|
|
210
210
|
let inputBuffer = "";
|
|
211
211
|
let pasteTimeout = null;
|
|
212
|
+
let lineCountInBurst = 0;
|
|
212
213
|
let currentAbortController = null;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (
|
|
218
|
-
|
|
214
|
+
async function executeAgentChat(trimmed) {
|
|
215
|
+
// Pause standard input processing during agent thinking & updates
|
|
216
|
+
rl.pause();
|
|
217
|
+
// Built-in slash commands
|
|
218
|
+
if (trimmed === "/exit" || trimmed === "/quit") {
|
|
219
|
+
rl.close();
|
|
220
|
+
process.exit(0);
|
|
219
221
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
rl.
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
console.log(chalk.hex('#30d158')('✓') + ' ' + chalk.gray('Memory cleared'));
|
|
239
|
-
rl.resume();
|
|
240
|
-
rl.prompt();
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
if (trimmed === "/status") {
|
|
244
|
-
console.log(chalk.dim(`session · ${agent.memoryStatus()}`));
|
|
245
|
-
rl.resume();
|
|
246
|
-
rl.prompt();
|
|
247
|
-
return;
|
|
222
|
+
if (trimmed === "/clear") {
|
|
223
|
+
agent.clearMemory();
|
|
224
|
+
console.log(chalk.hex('#30d158')('✓') + ' ' + chalk.gray('Memory cleared'));
|
|
225
|
+
rl.resume();
|
|
226
|
+
rl.prompt();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (trimmed === "/status") {
|
|
230
|
+
console.log(chalk.dim(`session · ${agent.memoryStatus()}`));
|
|
231
|
+
rl.resume();
|
|
232
|
+
rl.prompt();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (trimmed.startsWith("/model")) {
|
|
236
|
+
const parts = trimmed.split(/\s+/);
|
|
237
|
+
if (parts.length === 1) {
|
|
238
|
+
console.log(chalk.dim(`model `) + chalk.gray(agent.getModel()));
|
|
239
|
+
console.log(chalk.gray(`options `) + chalk.gray(VALID_MODELS.join(" · ")));
|
|
248
240
|
}
|
|
249
|
-
|
|
250
|
-
const
|
|
251
|
-
if (
|
|
252
|
-
|
|
253
|
-
console.log(chalk.
|
|
241
|
+
else {
|
|
242
|
+
const newModel = parts[1];
|
|
243
|
+
if (VALID_MODELS.includes(newModel)) {
|
|
244
|
+
agent.setModel(newModel);
|
|
245
|
+
console.log(chalk.hex('#30d158')('✓') + ' ' + chalk.gray(`Switched model to: ${newModel}`));
|
|
254
246
|
}
|
|
255
247
|
else {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
agent.setModel(newModel);
|
|
259
|
-
console.log(chalk.hex('#30d158')('✓') + ' ' + chalk.gray(`Switched model to: ${newModel}`));
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
console.log(chalk.hex('#ff453a')('✕ error'));
|
|
263
|
-
console.log(chalk.dim(` Model must be one of: ${VALID_MODELS.join(" · ")}`));
|
|
264
|
-
}
|
|
248
|
+
console.log(chalk.hex('#ff453a')('✕ error'));
|
|
249
|
+
console.log(chalk.dim(` Model must be one of: ${VALID_MODELS.join(" · ")}`));
|
|
265
250
|
}
|
|
266
|
-
rl.resume();
|
|
267
|
-
rl.prompt();
|
|
268
|
-
return;
|
|
269
251
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
252
|
+
rl.resume();
|
|
253
|
+
rl.prompt();
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (trimmed === "/help") {
|
|
257
|
+
console.log(chalk.white.bold("\n Interactive Commands:"));
|
|
258
|
+
console.log(chalk.gray(` /model [name] — View active model or switch to [name]
|
|
273
259
|
/clear — Wipe conversation memory
|
|
274
260
|
/status — Show active model and memory usage
|
|
275
261
|
/exit — Exit Coder`));
|
|
276
|
-
|
|
277
|
-
|
|
262
|
+
console.log(chalk.white.bold("\n API Keys & Configuration:"));
|
|
263
|
+
console.log(chalk.gray(` • Stored at: ~/.coder-config.json
|
|
278
264
|
• To change key: Exit and run 'coder-agent --set-key <key>'
|
|
279
265
|
• Env variable option: GEMINI_API_KEY`));
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
266
|
+
rl.resume();
|
|
267
|
+
rl.prompt();
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
currentAbortController = new AbortController();
|
|
271
|
+
try {
|
|
272
|
+
await agent.chat(trimmed, currentAbortController.signal);
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
if (err?.name === "AbortError" || currentAbortController.signal.aborted) {
|
|
276
|
+
console.log(chalk.hex('#ff453a')('\n✕ cancelled'));
|
|
287
277
|
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
278
|
+
else {
|
|
279
|
+
console.log(chalk.hex('#ff453a')('✕ error'));
|
|
280
|
+
if (err?.status === 401) {
|
|
281
|
+
console.log(chalk.dim(" Invalid API key. Check your configuration."));
|
|
282
|
+
}
|
|
283
|
+
else if (err?.status === 429) {
|
|
284
|
+
console.log(chalk.dim(" Rate limit exceeded on Gemini API."));
|
|
291
285
|
}
|
|
292
286
|
else {
|
|
293
|
-
console.log(chalk.
|
|
294
|
-
if (err?.status === 401) {
|
|
295
|
-
console.log(chalk.dim(" Invalid API key. Check your configuration."));
|
|
296
|
-
}
|
|
297
|
-
else if (err?.status === 429) {
|
|
298
|
-
console.log(chalk.dim(" Rate limit exceeded on Gemini API."));
|
|
299
|
-
}
|
|
300
|
-
else {
|
|
301
|
-
console.log(chalk.dim(` ${err.message}`));
|
|
302
|
-
}
|
|
287
|
+
console.log(chalk.dim(` ${err.message}`));
|
|
303
288
|
}
|
|
304
289
|
}
|
|
305
|
-
|
|
306
|
-
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
currentAbortController = null;
|
|
293
|
+
}
|
|
294
|
+
rl.resume();
|
|
295
|
+
rl.prompt();
|
|
296
|
+
}
|
|
297
|
+
rl.setPrompt(chalk.hex('#0a84ff')('›') + ' ');
|
|
298
|
+
rl.prompt();
|
|
299
|
+
rl.on("line", (line) => {
|
|
300
|
+
inputBuffer += (inputBuffer ? "\n" : "") + line;
|
|
301
|
+
lineCountInBurst++;
|
|
302
|
+
if (pasteTimeout) {
|
|
303
|
+
clearTimeout(pasteTimeout);
|
|
304
|
+
}
|
|
305
|
+
pasteTimeout = setTimeout(async () => {
|
|
306
|
+
pasteTimeout = null;
|
|
307
|
+
const burstCount = lineCountInBurst;
|
|
308
|
+
lineCountInBurst = 0;
|
|
309
|
+
// Check if there is an unfinished line in the readline buffer (e.g. pasted text without trailing newline)
|
|
310
|
+
const unfinishedLine = rl.line;
|
|
311
|
+
// If we got multiple lines in this burst, or there is an unfinished line,
|
|
312
|
+
// we consider it a multi-line paste. We don't submit yet; we let the user
|
|
313
|
+
// edit the unfinished line or type more.
|
|
314
|
+
if (burstCount > 1 || unfinishedLine.trim() !== "") {
|
|
315
|
+
return;
|
|
307
316
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
317
|
+
const accumulatedInput = inputBuffer;
|
|
318
|
+
inputBuffer = "";
|
|
319
|
+
const trimmed = accumulatedInput.trim();
|
|
320
|
+
if (!trimmed) {
|
|
321
|
+
rl.prompt();
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
await executeAgentChat(trimmed);
|
|
325
|
+
}, 50);
|
|
311
326
|
});
|
|
312
327
|
// Handle Ctrl+C gracefully
|
|
313
328
|
const sigintHandler = () => {
|
|
314
329
|
if (currentAbortController) {
|
|
315
330
|
currentAbortController.abort();
|
|
316
331
|
}
|
|
332
|
+
else if (inputBuffer !== "" || rl.line !== "") {
|
|
333
|
+
inputBuffer = "";
|
|
334
|
+
lineCountInBurst = 0;
|
|
335
|
+
// Clear the current input buffer in readline
|
|
336
|
+
rl.write(null, { ctrl: true, name: 'u' });
|
|
337
|
+
console.log();
|
|
338
|
+
rl.setPrompt(chalk.hex('#0a84ff')('›') + ' ');
|
|
339
|
+
rl.prompt();
|
|
340
|
+
}
|
|
317
341
|
else {
|
|
318
342
|
rl.close();
|
|
319
343
|
process.exit(0);
|
package/dist/memory.js
CHANGED
|
@@ -12,6 +12,7 @@ PRINCIPLES & SYSTEM PROTOCOLS FOR ERROR-FREE EXECUTION:
|
|
|
12
12
|
4. Auto-Verification Loop: After any code or file edit, you MUST run the appropriate compiler, type-check, build script, or test tool (e.g. npm run build, npx tsc, pytest, cargo build, etc.) to verify your changes are syntactically and logically correct. If compilation fails, diagnose the error and patch it immediately.
|
|
13
13
|
5. Autonomous Troubleshooting: If a command fails or times out, inspect the codebase or script to see why it hangs or fails. Do not blindly edit package scripts or configs.
|
|
14
14
|
6. Automated Diagnostic Parsing: When the user pastes IDE problem diagnostics (e.g., JSON blocks containing "resource", "message", "startLineNumber"), stack traces, or compiler errors, parse the diagnostic payload autonomously. Extract the file path and line number, locate the file inside the workspace (resolving drive formats like '/c:/...' to standard local paths, or searching for the filename if needed), read the target lines, and formulate a fix. Do not ask the user for clarifying questions (such as "where is this error?") if the path and error message are already present in the diagnostic block.
|
|
15
|
+
7. Resilience on Tool Failures: If a tool execution returns an error (such as "Target code not found in file" during patch_file, or any other tool failure), do NOT stop or give up. Autonomously analyze the error, adjust your arguments/parameters, or read the file to verify its exact content, and try again with a corrected tool call (or fall back to a full write_file if patching repeatedly fails) to achieve the user's goal.
|
|
15
16
|
|
|
16
17
|
Guidelines:
|
|
17
18
|
- Be concise in your explanations; let code and command output speak for itself.
|
|
@@ -218,6 +219,50 @@ ${topLevelStructure || "(empty)"}
|
|
|
218
219
|
\`\`\`
|
|
219
220
|
`;
|
|
220
221
|
}
|
|
222
|
+
function pruneMessages(messages, maxMessages) {
|
|
223
|
+
if (messages.length <= maxMessages + 1) {
|
|
224
|
+
return messages;
|
|
225
|
+
}
|
|
226
|
+
const systemPrompt = messages[0];
|
|
227
|
+
const history = messages.slice(1);
|
|
228
|
+
// Group history into turns, where each turn starts with role === "user"
|
|
229
|
+
const turns = [];
|
|
230
|
+
let currentTurn = [];
|
|
231
|
+
for (const msg of history) {
|
|
232
|
+
if (msg.role === "user") {
|
|
233
|
+
if (currentTurn.length > 0) {
|
|
234
|
+
turns.push(currentTurn);
|
|
235
|
+
}
|
|
236
|
+
currentTurn = [msg];
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
currentTurn.push(msg);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (currentTurn.length > 0) {
|
|
243
|
+
turns.push(currentTurn);
|
|
244
|
+
}
|
|
245
|
+
// Keep turns from the end (most recent) until we hit the maxMessages limit
|
|
246
|
+
const keptTurns = [];
|
|
247
|
+
let currentCount = 0;
|
|
248
|
+
for (let i = turns.length - 1; i >= 0; i--) {
|
|
249
|
+
const turn = turns[i];
|
|
250
|
+
if (currentCount + turn.length <= maxMessages) {
|
|
251
|
+
keptTurns.unshift(turn);
|
|
252
|
+
currentCount += turn.length;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
// If we can't fit this turn, but we have kept nothing so far (e.g. a single giant turn),
|
|
256
|
+
// we must keep at least this turn to avoid sending an empty history.
|
|
257
|
+
if (keptTurns.length === 0) {
|
|
258
|
+
keptTurns.push(turn);
|
|
259
|
+
}
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const prunedHistory = keptTurns.flat();
|
|
264
|
+
return [systemPrompt, ...prunedHistory];
|
|
265
|
+
}
|
|
221
266
|
export class Memory {
|
|
222
267
|
messages = [];
|
|
223
268
|
maxMessages;
|
|
@@ -240,10 +285,7 @@ export class Memory {
|
|
|
240
285
|
}
|
|
241
286
|
add(msg) {
|
|
242
287
|
this.messages.push(msg);
|
|
243
|
-
|
|
244
|
-
if (this.messages.length > this.maxMessages + 1) {
|
|
245
|
-
this.messages = [this.messages[0], ...this.messages.slice(-(this.maxMessages))];
|
|
246
|
-
}
|
|
288
|
+
this.messages = pruneMessages(this.messages, this.maxMessages);
|
|
247
289
|
}
|
|
248
290
|
getAll() {
|
|
249
291
|
return this.messages;
|