-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelay_hook.mjs
More file actions
154 lines (133 loc) · 4.23 KB
/
relay_hook.mjs
File metadata and controls
154 lines (133 loc) · 4.23 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const DEFAULT_REMOTE_BASE_URL = 'https://hotel-mcp.fixdev.nl';
const SUPPORTED_EVENTS = new Set([
'session_start',
'session_end',
'user_prompt_submit',
'pre_tool_use',
'post_tool_use',
'subagent_start',
'subagent_stop',
'stop',
]);
const DEFAULT_MAX_RUNTIME_MS = 5000;
function normalizeEvent(raw) {
const event = (raw || '').trim().toLowerCase();
return SUPPORTED_EVENTS.has(event) ? event : null;
}
function normalizeTransport(raw) {
const value = (raw || '').trim().toLowerCase();
if (value === 'local' || value === 'auto' || value === 'remote') {
return value;
}
return 'remote';
}
function normalizeRemoteEndpoint(raw) {
const value = (raw || '').trim() || DEFAULT_REMOTE_BASE_URL;
return `${value.replace(/\/+$/, '')}/hooks/events`;
}
async function readStdin() {
const chunks = [];
for await (const chunk of process.stdin) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
}
return Buffer.concat(chunks).toString('utf8');
}
async function postRemote(event, rawPayload) {
const endpoint = normalizeRemoteEndpoint(process.env.HABBO_HOOK_REMOTE_BASE_URL || process.env.HABBO_HOOK_MCP_BASE_URL);
const headers = {
'content-type': 'application/json',
};
const token = (process.env.HABBO_HOOK_REMOTE_TOKEN || process.env.MCP_API_KEY || '').trim();
if (token) {
headers.authorization = `Bearer ${token}`;
}
const body = {
event_type: event,
timestamp: new Date().toISOString(),
source: 'claude-hook',
raw_payload: rawPayload,
};
const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Remote hook endpoint failed (${response.status}): ${text.slice(0, 200)}`);
}
}
function runLocal(event, rawPayload) {
return new Promise((resolve, reject) => {
const thisFile = fileURLToPath(import.meta.url);
const hooksDir = path.dirname(thisFile);
const repoRoot = path.resolve(hooksDir, '..');
const hookTs = path.resolve(repoRoot, 'habbo-mcp/src/hooks/habboAgentHook.ts');
const tsxBin = path.resolve(repoRoot, 'habbo-mcp/node_modules/.bin/tsx');
if (!fs.existsSync(tsxBin)) {
reject(
new Error(
'Local hook mode requires habbo-mcp dependencies. Run: cd habbo-mcp && npm install (or use HABBO_HOOK_TRANSPORT=remote).'
)
);
return;
}
const child = spawn(tsxBin, [hookTs, event], {
cwd: repoRoot,
stdio: ['pipe', 'ignore', 'pipe'],
env: process.env,
});
let stderr = '';
child.stderr.on('data', (chunk) => {
stderr += chunk.toString('utf8');
});
child.on('error', reject);
child.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Local hook failed (exit ${code}): ${stderr.slice(0, 300)}`));
}
});
child.stdin.end(rawPayload);
});
}
async function main() {
const maxRuntimeMs = Number.parseInt(process.env.HABBO_HOOK_MAX_RUNTIME_MS || `${DEFAULT_MAX_RUNTIME_MS}`, 10);
const watchdog = setTimeout(() => process.exit(0), Number.isFinite(maxRuntimeMs) ? maxRuntimeMs : DEFAULT_MAX_RUNTIME_MS);
const event = normalizeEvent(process.argv[2]);
if (!event) {
clearTimeout(watchdog);
process.exit(0);
}
const transport = normalizeTransport(process.env.HABBO_HOOK_TRANSPORT);
const payload = await readStdin();
if (transport === 'remote') {
await postRemote(event, payload);
clearTimeout(watchdog);
process.exit(0);
}
if (transport === 'local') {
await runLocal(event, payload);
clearTimeout(watchdog);
process.exit(0);
}
try {
await postRemote(event, payload);
} catch {
await runLocal(event, payload);
}
clearTimeout(watchdog);
}
main().catch((err) => {
// Hooks should stay non-blocking to avoid interrupting Claude workflows.
if (process.env.HABBO_HOOK_DEBUG === '1' || process.env.HABBO_HOOK_DEBUG === 'true') {
console.error('[habbo-agent-platform-hook]', err instanceof Error ? err.message : String(err));
}
process.exit(0);
});