sam-coder-cli 1.0.3 → 1.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.
Files changed (2) hide show
  1. package/bin/agi-cli.js +49 -51
  2. package/package.json +2 -2
package/bin/agi-cli.js CHANGED
@@ -400,6 +400,9 @@ async function callOpenRouterWithTools(messages) {
400
400
  });
401
401
 
402
402
  if (!response.ok) {
403
+ if (response.status === 401) {
404
+ throw new Error('AuthenticationError: Invalid API key. Please run /setup to reconfigure.');
405
+ }
403
406
  const error = await response.json();
404
407
  throw new Error(`API error: ${error.error?.message || response.statusText}`);
405
408
  }
@@ -431,6 +434,9 @@ async function callOpenRouterWithFunctions(messages) {
431
434
  });
432
435
 
433
436
  if (!response.ok) {
437
+ if (response.status === 401) {
438
+ throw new Error('AuthenticationError: Invalid API key. Please run /setup to reconfigure.');
439
+ }
434
440
  const error = await response.json();
435
441
  throw new Error(`API error: ${error.error?.message || response.statusText}`);
436
442
  }
@@ -502,12 +508,10 @@ async function handleToolCalls(toolCalls, messages) {
502
508
  }
503
509
 
504
510
  // Process a query with tool calling
505
- async function processQueryWithTools(query, conversation = [], maxIterations = 5) {
511
+ async function processQueryWithTools(query, conversation = []) {
506
512
  // Add user message to conversation
507
513
  const userMessage = { role: 'user', content: query };
508
- let messages = [...conversation, userMessage];
509
- let iteration = 0;
510
- let finalResponse = null;
514
+ const messages = [...conversation, userMessage];
511
515
 
512
516
  // Add system message if this is the first message
513
517
  if (conversation.length === 0) {
@@ -520,54 +524,50 @@ If a tool fails, you can try again with different parameters or a different appr
520
524
  });
521
525
  }
522
526
 
523
- // Process in a loop to handle multiple tool calls
524
- while (iteration < maxIterations) {
525
- ui.startThinking();
526
-
527
- try {
528
- const response = await callOpenRouterWithTools(messages);
529
- const assistantMessage = response.choices[0].message;
530
- messages.push(assistantMessage);
531
-
532
- // If there are no tool calls, we're done
533
- if (!assistantMessage.tool_calls || assistantMessage.tool_calls.length === 0) {
534
- ui.stopThinking();
535
- finalResponse = assistantMessage.content || 'No content in response';
536
- break;
537
- }
538
-
539
- // Process tool calls
540
- console.log(`đŸ› ī¸ Executing ${assistantMessage.tool_calls.length} tools...`);
541
- const toolResults = await handleToolCalls(assistantMessage.tool_calls, messages);
542
-
543
- // Add tool results to messages
544
- messages = [...messages, ...toolResults];
545
-
546
- // If we've reached max iterations, get a final response
547
- if (iteration === maxIterations - 1) {
548
- console.log('â„šī¸ Reached maximum number of iterations. Getting final response...');
549
- const finalResponseObj = await callOpenRouterWithTools(messages);
550
- finalResponse = finalResponseObj.choices[0].message.content || 'No content in final response';
551
- }
552
-
553
- iteration++;
554
- } catch (error) {
527
+ ui.startThinking();
528
+
529
+ try {
530
+ const response = await callOpenRouterWithTools(messages);
531
+ const assistantMessage = response.choices[0].message;
532
+ messages.push(assistantMessage);
533
+
534
+ // If there are no tool calls, we're done
535
+ if (!assistantMessage.tool_calls || assistantMessage.tool_calls.length === 0) {
555
536
  ui.stopThinking();
556
- console.error('❌ Error during processing:', error);
557
- finalResponse = `An error occurred: ${error.message}`;
558
- break;
537
+ return {
538
+ response: assistantMessage.content || 'No content in response',
539
+ conversation: messages
540
+ };
559
541
  }
542
+
543
+ // Process tool calls
544
+ ui.stopThinking(); // Stop thinking while tools execute
545
+ console.log(`đŸ› ī¸ Executing ${assistantMessage.tool_calls.length} tools...`);
546
+ const toolResults = await handleToolCalls(assistantMessage.tool_calls, messages);
547
+
548
+ // Add tool results to messages
549
+ messages.push(...toolResults);
550
+
551
+ // Now, get the AI's response to the tool results
552
+ ui.startThinking();
553
+ const finalResponseObj = await callOpenRouterWithTools(messages);
554
+ const finalAssistantMessage = finalResponseObj.choices[0].message;
555
+ messages.push(finalAssistantMessage);
556
+ ui.stopThinking();
557
+
558
+ return {
559
+ response: finalAssistantMessage.content || 'Actions executed. What is the next step?',
560
+ conversation: messages
561
+ };
562
+
563
+ } catch (error) {
564
+ ui.stopThinking();
565
+ console.error('❌ Error during processing:', error);
566
+ return {
567
+ response: `An error occurred: ${error.message}`,
568
+ conversation: messages
569
+ };
560
570
  }
561
-
562
- // If we don't have a final response yet (shouldn't happen, but just in case)
563
- if (!finalResponse) {
564
- finalResponse = 'No response generated. The operation may have timed out or encountered an error.';
565
- }
566
-
567
- return {
568
- response: finalResponse,
569
- conversation: messages
570
- };
571
571
  }
572
572
 
573
573
  // Execute a single action from the action system
@@ -790,8 +790,6 @@ async function start() {
790
790
  let config = await readConfig();
791
791
  if (!config || !config.OPENROUTER_API_KEY) {
792
792
  config = await runSetup();
793
- console.log('\nSetup complete. Please start the application again.');
794
- process.exit(0);
795
793
  }
796
794
 
797
795
  OPENROUTER_API_KEY = config.OPENROUTER_API_KEY;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "sam-coder-cli",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "SAM-CODER: An animated command-line AI assistant with agency capabilities.",
5
5
  "main": "bin/agi-cli.js",
6
6
  "bin": {
7
- "sam-coder": "./bin/agi-cli.js"
7
+ "sam-coder": "bin/agi-cli.js"
8
8
  },
9
9
  "scripts": {
10
10
  "start": "node ./bin/agi-cli.js"