-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
133 lines (113 loc) · 3.59 KB
/
Copy pathindex.ts
File metadata and controls
133 lines (113 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* ClawBox Backend Server
* Bun + Hono on port 13000
* All agent functionality proxied through OpenClaw gateway
*/
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { chatRoutes } from './routes/chat'
import { sessionRoutes } from './routes/sessions'
import { agentRoutes } from './routes/agents'
import { configRoutes } from './routes/config'
import { modelsRoutes } from './routes/models'
import { titleRoutes } from './routes/titles'
import { toolsRoutes } from './routes/tools'
import { skillsRoutes } from './routes/skills'
import { onboardRoutes, ensureGateway, refreshWindowsPath, killGatewayChildren } from './routes/onboard'
import { channelRoutes } from './routes/channels'
import { cronRoutes } from './routes/cron'
import { soulRoutes } from './routes/soul'
import pkg from '../package.json'
import { loadConfig, getConfig, startConfigWatcher } from './config/index'
import { openclawGetClient } from './providers/openclaw-rpc'
import { createLogger } from './logger'
const rawPort = Number(process.env.CLAWBOX_BACKEND_PORT || '13000')
const PORT = Number.isFinite(rawPort) && rawPort > 0 ? rawPort : 13000
const log = createLogger('Server')
const app = new Hono()
// Middleware
app.use('*', cors({
origin: '*',
allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
allowHeaders: ['Content-Type', 'Authorization'],
exposeHeaders: ['Content-Type'],
}))
// Health & lifecycle endpoints
app.get('/api/health', (c) => {
const gatewayConnected = openclawGetClient().isConnected()
return c.json({ status: 'ok', gatewayConnected, timestamp: Date.now() })
})
app.post('/api/shutdown', (c) => {
log.info('Shutdown requested...')
setTimeout(async () => {
try {
killGatewayChildren()
if (bunServer) {
bunServer.stop(true)
log.info('Server socket closed')
await new Promise(r => setTimeout(r, 500))
}
} catch {}
process.exit(0)
}, 200)
return c.json({ ok: true })
})
app.get('/api/status', (c) => {
const config = getConfig()
return c.json({
healthy: true,
timestamp: Date.now(),
gateway: config.providers.openclaw.baseUrl,
version: pkg.version,
})
})
// API Routes
app.route('/api/chat', chatRoutes)
app.route('/api/sessions', sessionRoutes)
app.route('/api/agents', agentRoutes)
app.route('/api/config', configRoutes)
app.route('/api/models', modelsRoutes)
app.route('/api/titles', titleRoutes)
app.route('/api/tools', toolsRoutes)
app.route('/api/skills', skillsRoutes)
app.route('/api/onboard', onboardRoutes)
app.route('/api/channels', channelRoutes)
app.route('/api/cron', cronRoutes)
app.route('/api/soul', soulRoutes)
// Error handling
app.onError((err, c) => {
log.error(`Unhandled error: ${err.message}`)
return c.json({ error: err.message }, 500)
})
app.notFound((c) => c.json({ error: 'Not found' }, 404))
// Initialization
async function initializeServer(): Promise<void> {
loadConfig()
startConfigWatcher()
if (process.platform === 'win32') {
await refreshWindowsPath()
}
const config = getConfig()
ensureGateway().catch((e) => {
log.warn(`Gateway auto-start failed: ${e.message}`)
})
log.info(`Server started on port ${PORT}`)
}
await initializeServer()
const bunServer = (globalThis as any).Bun.serve({
port: PORT,
fetch: app.fetch,
idleTimeout: 255,
reusePort: true,
})
log.info(`Bun server listening on port ${bunServer.port}`)
function gracefulShutdown() {
try {
killGatewayChildren()
bunServer.stop(true)
} catch {}
process.exit(0)
}
process.on('SIGTERM', gracefulShutdown)
process.on('SIGINT', gracefulShutdown)
process.on('SIGBREAK', gracefulShutdown)