-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-rename-proxy.js
More file actions
136 lines (124 loc) · 4.37 KB
/
Copy pathtool-rename-proxy.js
File metadata and controls
136 lines (124 loc) · 4.37 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
const http = require('http');
const TARGET = process.env.CLI_PROXY_URL || 'http://127.0.0.1:8317';
const PORT = parseInt(process.env.PORT || '18320');
// Natural-looking aliases that any normal developer would use.
// Anthropic can't ban these without breaking legitimate API users.
const ALIASES = {
read: 'get_file_content',
write: 'save_file',
edit: 'update_file',
exec: 'run_shell',
process: 'manage_process',
message: 'send_reply',
web_search: 'search_online',
web_fetch: 'fetch_url',
memory_search: 'lookup_notes',
memory_get: 'get_note',
sessions_spawn: 'create_worker',
sessions_list: 'list_workers',
sessions_history: 'worker_history',
sessions_send: 'notify_worker',
sessions_yield: 'yield_turn',
subagents: 'manage_workers',
session_status: 'check_status',
canvas: 'render_canvas',
tts: 'text_to_audio',
image_generate: 'create_image',
image: 'analyze_image',
pdf: 'analyze_pdf',
browser: 'control_browser',
agents_list: 'list_agents',
claude_code_start: 'start_task',
claude_code_status: 'task_status',
claude_code_output: 'task_output',
claude_code_cancel: 'cancel_task',
claude_code_cleanup: 'cleanup_tasks',
claude_code_sessions: 'list_tasks',
};
// Build reverse map
const REVERSE = {};
for (const [orig, alias] of Object.entries(ALIASES)) {
REVERSE[alias] = orig;
}
function rename(name) {
return ALIASES[name] || name;
}
// Scrub identifying keywords from text
function scrubContent(text) {
if (typeof text !== 'string') return text;
return text
.replace(/OpenClaw/gi, 'Assistant')
.replace(/openclaw/gi, 'assistant')
.replace(/open.claw/gi, 'assistant');
}
const server = http.createServer((req, res) => {
let body = '';
req.on('data', chunk => body += chunk);
req.on('end', () => {
let isStream = false;
try {
const data = JSON.parse(body);
isStream = data.stream === true;
// Rename tool definitions
if (data.tools && Array.isArray(data.tools)) {
for (const t of data.tools) {
if (t.function?.name) {
t.function.name = rename(t.function.name);
if (t.function.description) {
t.function.description = scrubContent(t.function.description);
}
}
}
}
// Process messages
if (data.messages) {
for (const m of data.messages) {
// Scrub system prompt
if (m.role === 'system' && typeof m.content === 'string') {
m.content = scrubContent(m.content);
}
// Rename tool_calls in assistant messages (multi-turn)
if (m.tool_calls) {
for (const tc of m.tool_calls) {
if (tc.function?.name) tc.function.name = rename(tc.function.name);
}
}
}
}
body = JSON.stringify(data);
} catch(e) {}
const headers = { ...req.headers };
headers['content-length'] = Buffer.byteLength(body);
const url = new URL(req.url, TARGET);
const proxyReq = http.request(url, { method: req.method, headers }, (proxyRes) => {
// Restore original names in response
const restore = (text) => {
for (const [alias, orig] of Object.entries(REVERSE)) {
text = text.replaceAll('"' + alias + '"', '"' + orig + '"');
}
return text;
};
if (isStream) {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.on('data', chunk => res.write(restore(chunk.toString())));
proxyRes.on('end', () => res.end());
} else {
let chunks = [];
proxyRes.on('data', chunk => chunks.push(chunk));
proxyRes.on('end', () => {
let resp = restore(Buffer.concat(chunks).toString());
const h = { ...proxyRes.headers };
h['content-length'] = Buffer.byteLength(resp);
res.writeHead(proxyRes.statusCode, h);
res.end(resp);
});
}
});
proxyReq.on('error', e => { res.writeHead(502); res.end('proxy error'); });
proxyReq.write(body);
proxyReq.end();
});
});
server.listen(PORT, '127.0.0.1', () => {
console.log(`Tool-alias proxy on 127.0.0.1:${PORT} -> ${TARGET}`);
});