feat: Implement character loading from multiple paths and enhance API…
- Added
loadCharacterTryPathfunction to attempt loading characters from various resolved paths. - Updated
loadCharactersto 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
- 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"]}]}%
- 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"}'
- 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"]}}}'
- 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":[]}]}%
- 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}%
- 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":[]}]}%
feels un-necessary, I will leave to @shakkernerd and @odilitime
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.
@coderabbitai intense review please
[!TIP] For best results, initiate chat on the files or code changes.
@wtfsayo Here's an intense review of the changes:
- Security Concerns:
- The
/agent/startendpoint 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
- Error Handling:
loadCharacterTryPathhas good error logging but exits process on failure inloadCharacterswhich 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
- 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
- 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
- Testing:
- Missing unit tests for new character loading functionality
- No error case testing in the provided examples
- API endpoint tests not included
Recommendations:
- Add input validation using a schema validator
- Implement proper authentication/authorization
- Standardize API response formats
- Add comprehensive error handling
- Include unit and integration tests
- Use configuration for path resolution instead of hardcoding
- 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