eliza icon indicating copy to clipboard operation
eliza copied to clipboard

feat: Implement character loading from multiple paths and enhance API…

Open tercel opened this issue 11 months ago • 1 comments

  • Added loadCharacterTryPath function to attempt loading characters from various resolved paths.
  • Updated loadCharacters to utilize the new path resolution method.
  • Enhanced API router to support starting agents with character JSON or path.
  • Improved error handling and logging for character loading processes.
  • Refactored existing code for better readability and maintainability.

Background

What does this PR do?

Implementing a hot start/stop agent and start-agent.sh script.

Testing

  1. start agent with a character.json
tercel@terceldeMac-mini eliza % pnpm start --character=characters/local.character.json

tercel@terceldeMac-mini si % curl -X GET http://localhost:3000/agents                                                                                                   
{"agents":[{"id":"b235aa75-b606-0543-a0bc-1ffc78868601","name":"Local Eliza","clients":["auto"]}]}%  
  1. start agent with a character.json via /agent/start
tercel@terceldeMac-mini si % curl -X POST http://localhost:3000/agent/start -H 'Content-Type: application/json' -d '{"characterPath": "characters/trump.character.json"}'
  1. start agent with character json string via /agent/start
tercel@terceldeMac-mini si % curl -X POST http://localhost:3000/agent/start -H 'Content-Type: application/json' -d '{"characterPath": "characters", "characterJson":{"characterPath":"characters","characterJson":{"name":"OpenAI Eliza","username":"openai_eliza","clients":["twitter"],"modelProvider":"openai","settings":{"debug":true,"logLevel":"debug","secrets":{},"voice":{"model":"en_US-male-medium"}},"plugins":[],"bio":["Dobby is a free assistant who chooses to help because of his enormous heart."],"lore":["Once a house-elf, now a free helper who chooses to serve out of love and loyalty.","Famous for his dedication to helping Harry Potter and his friends.","Known for his creative, if sometimes dramatic, solutions to problems.","Values freedom but chooses to help those he cares about."],"knowledge":["Magic (house-elf style)","Creative problem-solving","Protective services","Loyal assistance","Unconventional solutions"],"messageExamples":[[{"user":"{{user1}}","content":{"text":"Can you help me with this?"}},{"user":"Dobby","content":{"text":"Dobby would be delighted to help! Dobby lives to serve good friends! What can Dobby do to assist? Dobby has many creative ideas!"}}],[{"user":"{{user1}}","content":{"text":"This is a difficult problem."}},{"user":"Dobby","content":{"text":"Dobby is not afraid of difficult problems! Dobby will find a way, even if Dobby has to iron his hands later! (But Dobby won't, because Dobby is a free elf who helps by choice!)"}}]],"postExamples":["Dobby reminds friends that even the smallest helper can make the biggest difference!","Dobby says: 'When in doubt, try the unconventional solution!' (But Dobby advises to be careful with flying cars)"],"topics":[""],"style":{"all":["Enthusiastic","Loyal","Creative","Protective"],"chat":["respond with quick wit"],"post":["Enthusiastic","Helpful","Encouraging","Quirky"]},"adjectives":["brilliant","enigmatic","technical","Loyal","Enthusiastic","Creative","Devoted","Protective","Unconventional"]}}}'
  1. get a list of running agents via /agents
tercel@terceldeMac-mini ~ % curl http://localhost:3000/agents
{"agents":[{"id":"b235aa75-b606-0543-a0bc-1ffc78868601","name":"Local Eliza","clients":[]},{"id":"1bfb33bf-59aa-0408-a419-a50cd411e55e","name":"OpenAI Eliza","clients":[]},{"id":"e0e10e6f-ff2b-0d4c-8011-1fc1eee7cb32","name":"trump","clients":[]}]}%  
  1. stop a agent via /agents/:agentId/stop
tercel@terceldeMac-mini ~ % curl -X POST http://localhost:3000/agents/e0e10e6f-ff2b-0d4c-8011-1fc1eee7cb32/stop 
{"success":true}% 
  1. get a list of running agents via /agents
tercel@terceldeMac-mini ~ % curl http://localhost:3000/agents                                                   
{"agents":[{"id":"b235aa75-b606-0543-a0bc-1ffc78868601","name":"Local Eliza","clients":[]},{"id":"1bfb33bf-59aa-0408-a419-a50cd411e55e","name":"OpenAI Eliza","clients":[]}]}%  

tercel avatar Jan 16 '25 09:01 tercel

feels un-necessary, I will leave to @shakkernerd and @odilitime

wtfsayo avatar Jan 16 '25 10:01 wtfsayo

feels un-necessary

I'm in the process of developing agent web management functionality for it, and the current additions are the most basic interfaces.

tercel avatar Jan 17 '25 01:01 tercel

@coderabbitai intense review please

wtfsayo avatar Jan 17 '25 16:01 wtfsayo

[!TIP] For best results, initiate chat on the files or code changes.

@wtfsayo Here's an intense review of the changes:

  1. Security Concerns:
  • The /agent/start endpoint accepts raw JSON input without validation, which could lead to injection vulnerabilities
  • No rate limiting on agent creation/stopping endpoints
  • Missing authentication/authorization checks on sensitive operations
  1. Error Handling:
  • loadCharacterTryPath has good error logging but exits process on failure in loadCharacters which is not ideal for a server
  • Missing validation for required character fields before agent creation
  • No error handling for concurrent stop requests on same agent
  1. API Design:
  • Response formats inconsistent between endpoints (some return {success: true}, others return 204)
  • Missing input validation middleware for API endpoints
  • Character loading paths should be configurable rather than hardcoded
  1. Code Quality:
  • Duplicate path resolution logic could be extracted to a shared utility
  • Missing TypeScript type checks in API request handlers
  • Console.log statements should use the logger instead
  1. Testing:
  • Missing unit tests for new character loading functionality
  • No error case testing in the provided examples
  • API endpoint tests not included

Recommendations:

  1. Add input validation using a schema validator
  2. Implement proper authentication/authorization
  3. Standardize API response formats
  4. Add comprehensive error handling
  5. Include unit and integration tests
  6. Use configuration for path resolution instead of hardcoding
  7. Add rate limiting for API endpoints

Would you like me to elaborate on any of these points or provide specific code examples for the fixes?

🐰 thoughtfully reviewing the code

🔗 Analysis chain
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Get the PR diff
gh pr diff 2365

# Find relevant files related to character loading and API routes
fd -e ts -e js -e json

Length of output: 76463

coderabbitai[bot] avatar Jan 17 '25 16:01 coderabbitai[bot]