HTTP/SSE API layer for Loki Mode autonomous agent orchestration.
# Start the API server
loki serve
# Or directly
./autonomy/serve.sh
# With options
loki serve --port 9000 --host 0.0.0.0- Deno 1.40+ (for running the server)
- Loki Mode installed and configured
Install Deno:
curl -fsSL https://deno.land/install.sh | sh
# or
brew install deno| Variable | Default | Description |
|---|---|---|
LOKI_DASHBOARD_PORT |
57374 |
Server port |
LOKI_API_HOST |
localhost |
Server host |
LOKI_API_TOKEN |
none | API token for remote access |
LOKI_DIR |
auto | Loki installation directory |
LOKI_DEBUG |
false | Enable debug output |
--port, -p <port> Port to listen on
--host <host> Host to bind to
--no-cors Disable CORS
--no-auth Disable authentication
--generate-token Generate a secure API token
By default, the API only accepts requests from localhost without authentication.
For remote access:
# Generate a token
export LOKI_API_TOKEN=$(loki serve --generate-token)
# Start server allowing remote connections
loki serve --host 0.0.0.0
# Connect from another machine
curl -H "Authorization: Bearer $LOKI_API_TOKEN" http://server:57374/health| Method | Path | Description |
|---|---|---|
| GET | /health |
Health check |
| GET | /health/ready |
Readiness probe |
| GET | /health/live |
Liveness probe |
| GET | /api/status |
Detailed status |
| Method | Path | Description |
|---|---|---|
| POST | /api/sessions |
Start new session |
| GET | /api/sessions |
List all sessions |
| GET | /api/sessions/:id |
Get session details |
| POST | /api/sessions/:id/stop |
Stop session |
| POST | /api/sessions/:id/input |
Inject human input |
| DELETE | /api/sessions/:id |
Delete session record |
| Method | Path | Description |
|---|---|---|
| GET | /api/sessions/:id/tasks |
List session tasks |
| GET | /api/tasks |
List all tasks |
| GET | /api/tasks/active |
Get running tasks |
| GET | /api/tasks/queue |
Get queued tasks |
| Method | Path | Description |
|---|---|---|
| GET | /api/events |
SSE event stream |
| GET | /api/events/history |
Get event history |
| GET | /api/events/stats |
Event statistics |
session:started- Session startedsession:paused- Session pausedsession:resumed- Session resumedsession:stopped- Session stoppedsession:completed- Session completed successfullysession:failed- Session failed
phase:started- Phase startedphase:completed- Phase completedphase:failed- Phase failed
task:created- Task createdtask:started- Task startedtask:progress- Task progress updatetask:completed- Task completedtask:failed- Task failed
agent:spawned- Agent spawnedagent:output- Agent outputagent:completed- Agent completedagent:failed- Agent failed
log:debug- Debug loglog:info- Info loglog:warn- Warning loglog:error- Error log
metrics:update- Metrics updateinput:requested- Human input requestedheartbeat- Keep-alive heartbeat
curl -X POST http://localhost:57374/api/sessions \
-H "Content-Type: application/json" \
-d '{"provider": "claude", "prdPath": "./docs/prd.md"}'const events = new EventSource('http://localhost:57374/api/events');
events.addEventListener('task:completed', (e) => {
const event = JSON.parse(e.data);
console.log(`Task completed: ${event.data.title}`);
});
events.addEventListener('log:error', (e) => {
const event = JSON.parse(e.data);
console.error(`Error: ${event.data.message}`);
});
events.onerror = (err) => {
console.error('Connection error:', err);
};import { LokiClient } from './api/client.ts';
const client = new LokiClient('http://localhost:57374');
// Start a session
const { sessionId } = await client.startSession({
provider: 'claude',
prdPath: './docs/prd.md'
});
// Subscribe to events
const unsubscribe = client.subscribe((event) => {
console.log(`${event.type}: ${JSON.stringify(event.data)}`);
});
// Get session status
const status = await client.getSession(sessionId);
console.log(`Status: ${status.session.status}`);
// Stop session
await client.stopSession(sessionId);
// Cleanup
unsubscribe();
client.close();# Filter by session
curl "http://localhost:57374/api/events?sessionId=session_123"
# Filter by event types
curl "http://localhost:57374/api/events?types=task:completed,task:failed"
# Filter log level
curl "http://localhost:57374/api/events?minLevel=warn"
# Replay recent history
curl "http://localhost:57374/api/events?history=50"deno test --allow-all api/server_test.tsdeno run --watch --allow-all api/server.tsdeno check api/server.tsapi/
server.ts # Main HTTP server
client.ts # TypeScript client SDK
mod.ts # Module exports
openapi.yaml # OpenAPI specification
routes/
sessions.ts # Session endpoints
tasks.ts # Task endpoints
events.ts # SSE streaming
health.ts # Health checks
services/
cli-bridge.ts # CLI integration
state-watcher.ts # File system watcher
event-bus.ts # Event distribution
middleware/
auth.ts # Authentication
cors.ts # CORS handling
error.ts # Error handling
types/
api.ts # API types
events.ts # Event types
The API watches the .loki/ directory for state changes:
sessions/{id}/session.json- Session statesessions/{id}/tasks.json- Task listsessions/{id}/phase.json- Current phasesessions/{id}/agents.json- Active agentsstate.json- Global state
Changes are detected via file watching and emitted as SSE events.
| Code | HTTP Status | Description |
|---|---|---|
BAD_REQUEST |
400 | Invalid request |
UNAUTHORIZED |
401 | Missing/invalid auth |
FORBIDDEN |
403 | Permission denied |
NOT_FOUND |
404 | Resource not found |
CONFLICT |
409 | State conflict |
VALIDATION_ERROR |
422 | Validation failed |
INTERNAL_ERROR |
500 | Server error |
SESSION_NOT_FOUND |
404 | Session not found |
SESSION_ALREADY_RUNNING |
409 | Session running |
PROVIDER_NOT_AVAILABLE |
503 | Provider unavailable |
MIT