codeep 1.0.24 → 1.0.26

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.
@@ -205,7 +205,7 @@ async function agentChat(messages, systemPrompt, onChunk, abortSignal, dynamicTi
205
205
  tools: getOpenAITools(),
206
206
  tool_choice: 'auto',
207
207
  temperature: config.get('temperature'),
208
- max_tokens: config.get('maxTokens'),
208
+ max_tokens: Math.max(config.get('maxTokens'), 16384), // Ensure enough tokens for large file generation
209
209
  };
210
210
  }
211
211
  else {
@@ -216,7 +216,7 @@ async function agentChat(messages, systemPrompt, onChunk, abortSignal, dynamicTi
216
216
  messages: messages,
217
217
  tools: getAnthropicTools(),
218
218
  temperature: config.get('temperature'),
219
- max_tokens: config.get('maxTokens'),
219
+ max_tokens: Math.max(config.get('maxTokens'), 16384), // Ensure enough tokens for large file generation
220
220
  };
221
221
  }
222
222
  const response = await fetch(endpoint, {
@@ -626,13 +626,33 @@ export async function runAgent(prompt, projectContext, options = {}) {
626
626
  toolCalls = textToolCalls;
627
627
  }
628
628
  }
629
- // If no tool calls, agent is done - simple and clean like Claude Code/Aider
629
+ // If no tool calls, check if model wants to continue or is really done
630
630
  if (toolCalls.length === 0) {
631
- console.error(`[DEBUG] No tool calls at iteration ${iteration}, agent finished`);
631
+ console.error(`[DEBUG] No tool calls at iteration ${iteration}`);
632
632
  // Remove <think>...</think> tags from response (some models include thinking)
633
633
  finalResponse = content.replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
634
- // Trust the model - if it says it's done, it's done
635
- // No forcing, no blocking, no artificial requirements
634
+ // Check if model indicates it wants to continue (incomplete response)
635
+ const continueIndicators = [
636
+ 'let me', 'i will', 'i\'ll', 'now i', 'next i',
637
+ 'creating', 'writing', 'generating',
638
+ 'let\'s', 'going to', 'need to create', 'need to write'
639
+ ];
640
+ const lowerResponse = finalResponse.toLowerCase();
641
+ const wantsToContinue = continueIndicators.some(indicator => lowerResponse.includes(indicator));
642
+ // Also check if there were tool call parsing failures in this iteration
643
+ // by looking for incomplete actions (e.g., write_file without content)
644
+ const hasIncompleteWork = iteration < 10 && wantsToContinue && finalResponse.length < 500;
645
+ if (hasIncompleteWork) {
646
+ console.error(`[DEBUG] Model wants to continue, prompting for next action`);
647
+ messages.push({ role: 'assistant', content });
648
+ messages.push({
649
+ role: 'user',
650
+ content: 'Continue. Execute the tool calls now.'
651
+ });
652
+ continue;
653
+ }
654
+ // Model is done
655
+ console.error(`[DEBUG] Agent finished at iteration ${iteration}`);
636
656
  break;
637
657
  }
638
658
  // Add assistant response to history
@@ -204,20 +204,132 @@ function normalizeToolName(name) {
204
204
  export function parseOpenAIToolCalls(toolCalls) {
205
205
  if (!toolCalls || !Array.isArray(toolCalls))
206
206
  return [];
207
- return toolCalls.map(tc => {
207
+ const parsed = [];
208
+ for (const tc of toolCalls) {
209
+ const toolName = normalizeToolName(tc.function?.name || '');
210
+ if (!toolName)
211
+ continue;
208
212
  let parameters = {};
213
+ const rawArgs = tc.function?.arguments || '{}';
209
214
  try {
210
- parameters = JSON.parse(tc.function?.arguments || '{}');
215
+ parameters = JSON.parse(rawArgs);
211
216
  }
212
- catch {
213
- parameters = {};
217
+ catch (e) {
218
+ // JSON parsing failed - likely truncated response
219
+ // Try to extract what we can from partial JSON
220
+ console.error(`[DEBUG] Failed to parse tool arguments for ${toolName}, attempting partial extraction...`);
221
+ const partialParams = extractPartialToolParams(toolName, rawArgs);
222
+ if (partialParams) {
223
+ parameters = partialParams;
224
+ console.error(`[DEBUG] Successfully extracted partial params for ${toolName}`);
225
+ }
226
+ else {
227
+ console.error(`[DEBUG] Could not extract params, skipping ${toolName}`);
228
+ continue;
229
+ }
214
230
  }
215
- return {
216
- tool: normalizeToolName(tc.function?.name || ''),
231
+ // Validate required parameters for specific tools
232
+ if (toolName === 'write_file' && (!parameters.path || parameters.content === undefined)) {
233
+ console.error(`[DEBUG] Skipping write_file with missing path or content`);
234
+ continue;
235
+ }
236
+ if (toolName === 'read_file' && !parameters.path) {
237
+ console.error(`[DEBUG] Skipping read_file with missing path`);
238
+ continue;
239
+ }
240
+ if (toolName === 'edit_file' && (!parameters.path || parameters.old_text === undefined || parameters.new_text === undefined)) {
241
+ console.error(`[DEBUG] Skipping edit_file with missing parameters`);
242
+ continue;
243
+ }
244
+ parsed.push({
245
+ tool: toolName,
217
246
  parameters,
218
247
  id: tc.id,
219
- };
220
- }).filter(tc => tc.tool);
248
+ });
249
+ }
250
+ return parsed;
251
+ }
252
+ /**
253
+ * Extract parameters from truncated/partial JSON for tool calls
254
+ * This is a fallback when JSON.parse fails due to API truncation
255
+ */
256
+ function extractPartialToolParams(toolName, rawArgs) {
257
+ try {
258
+ // For write_file, try to extract path and content
259
+ if (toolName === 'write_file') {
260
+ const pathMatch = rawArgs.match(/"path"\s*:\s*"([^"]+)"/);
261
+ const contentMatch = rawArgs.match(/"content"\s*:\s*"([\s\S]*?)(?:"|$)/);
262
+ if (pathMatch && contentMatch) {
263
+ // Unescape the content
264
+ let content = contentMatch[1];
265
+ content = content
266
+ .replace(/\\n/g, '\n')
267
+ .replace(/\\t/g, '\t')
268
+ .replace(/\\r/g, '\r')
269
+ .replace(/\\"/g, '"')
270
+ .replace(/\\\\/g, '\\');
271
+ // If content appears truncated (doesn't end properly), add a comment
272
+ if (!content.endsWith('\n') && !content.endsWith('}') && !content.endsWith(';') && !content.endsWith('>')) {
273
+ content += '\n<!-- Content may be truncated -->\n';
274
+ }
275
+ return { path: pathMatch[1], content };
276
+ }
277
+ }
278
+ // For read_file, just need path
279
+ if (toolName === 'read_file') {
280
+ const pathMatch = rawArgs.match(/"path"\s*:\s*"([^"]+)"/);
281
+ if (pathMatch) {
282
+ return { path: pathMatch[1] };
283
+ }
284
+ }
285
+ // For list_files, just need path
286
+ if (toolName === 'list_files') {
287
+ const pathMatch = rawArgs.match(/"path"\s*:\s*"([^"]+)"/);
288
+ if (pathMatch) {
289
+ return { path: pathMatch[1] };
290
+ }
291
+ }
292
+ // For create_directory, just need path
293
+ if (toolName === 'create_directory') {
294
+ const pathMatch = rawArgs.match(/"path"\s*:\s*"([^"]+)"/);
295
+ if (pathMatch) {
296
+ return { path: pathMatch[1] };
297
+ }
298
+ }
299
+ // For edit_file, need path, old_text, new_text
300
+ if (toolName === 'edit_file') {
301
+ const pathMatch = rawArgs.match(/"path"\s*:\s*"([^"]+)"/);
302
+ const oldTextMatch = rawArgs.match(/"old_text"\s*:\s*"([\s\S]*?)(?:"|$)/);
303
+ const newTextMatch = rawArgs.match(/"new_text"\s*:\s*"([\s\S]*?)(?:"|$)/);
304
+ if (pathMatch && oldTextMatch && newTextMatch) {
305
+ return {
306
+ path: pathMatch[1],
307
+ old_text: oldTextMatch[1].replace(/\\n/g, '\n').replace(/\\"/g, '"'),
308
+ new_text: newTextMatch[1].replace(/\\n/g, '\n').replace(/\\"/g, '"'),
309
+ };
310
+ }
311
+ }
312
+ // For execute_command
313
+ if (toolName === 'execute_command') {
314
+ const commandMatch = rawArgs.match(/"command"\s*:\s*"([^"]+)"/);
315
+ if (commandMatch) {
316
+ const argsMatch = rawArgs.match(/"args"\s*:\s*\[([\s\S]*?)\]/);
317
+ let args = [];
318
+ if (argsMatch) {
319
+ const argStrings = argsMatch[1].match(/"([^"]+)"/g);
320
+ if (argStrings) {
321
+ args = argStrings.map(s => s.replace(/"/g, ''));
322
+ }
323
+ }
324
+ return { command: commandMatch[1], args };
325
+ }
326
+ }
327
+ return null;
328
+ }
329
+ catch (e) {
330
+ console.error(`[DEBUG] Error in extractPartialToolParams:`, e);
331
+ return null;
332
+ }
221
333
  }
222
334
  /**
223
335
  * Parse tool calls from Anthropic response
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codeep",
3
- "version": "1.0.24",
3
+ "version": "1.0.26",
4
4
  "description": "AI-powered coding assistant built for the terminal. Multiple LLM providers, project-aware context, and a seamless development workflow.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",