Overview
HAPI uses an RPC (Remote Procedure Call) system to enable the web app to invoke methods on the CLI. Flow:- CLI registers RPC handlers via Socket.IO
- Web calls REST endpoint
- Hub routes to CLI via Socket.IO
rpc-requestevent - CLI executes method and returns result
- Hub returns result to web
Architecture
Registering Handlers
CLI Side
Common RPC Methods
uploadFile
Called by:POST /api/sessions/:id/upload
Params:
deleteUploadFile
Called by:POST /api/sessions/:id/upload/delete
Params:
checkPathExists
Called by:POST /api/machines/:id/paths/exists
Params:
gitStatus
Called by:GET /api/sessions/:id/git-status
Params:
gitDiffNumstat
Called by:GET /api/sessions/:id/git-diff-numstat
Params:
gitDiffFile
Called by:GET /api/sessions/:id/git-diff-file
Params:
readFile
Called by:GET /api/sessions/:id/file
Params:
runRipgrep
Called by:GET /api/sessions/:id/files
Params:
listDirectory
Called by:GET /api/sessions/:id/directory
Params:
spawnSession
Called by:POST /api/machines/:id/spawn
Params:
listSlashCommands
Called by:GET /api/sessions/:id/slash-commands
Params:
listSkills
Called by:GET /api/sessions/:id/skills
Params: None
Returns:
Error Handling
Timeout
RPC calls timeout after 30 seconds by default:No Handler Registered
If no CLI has registered the method:CLI Error
If the CLI handler throws an error:Hub-Side Implementation
RPC Registry
The hub maintains a registry mapping methods to sockets:Calling RPC from REST
Best Practices
- Register on connect - Register all RPC methods immediately after connecting
- Unregister on disconnect - Clean up handlers when socket disconnects
- Handle errors - Always wrap handler code in try/catch
- Return structured responses - Use
{success, ...}format - Validate params - Check parameter types and values
- Set timeouts - Use reasonable timeouts for long operations
- Log failures - Log RPC errors for debugging