-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
269 lines (246 loc) · 7.23 KB
/
Copy pathserver.js
File metadata and controls
269 lines (246 loc) · 7.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 EvoMap
//
// OpenCode server plugin entrypoint.
//
// OpenCode loads npm server plugins from package `main` / `exports["./server"]`
// and invokes exported plugin functions with a context object. This module
// bridges OpenCode events to the same clean-room Evolver hook scripts used by
// the Claude Code and Cursor plugins.
'use strict';
const { spawn } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const DEFAULT_HOOKS_DIR = path.join(__dirname, 'hooks');
const WRITE_TOOLS = new Set(['write', 'edit', 'multiedit', 'multi_edit']);
const sessionStartContext = new Map();
function timeoutMs(envName, fallback) {
const raw = Number.parseInt(process.env[envName] || '', 10);
return Number.isFinite(raw) && raw > 0 ? raw : fallback;
}
function resolveHooksDir() {
const override = process.env.EVOLVER_OPENCODE_HOOKS_DIR;
return override && path.isAbsolute(override) ? override : DEFAULT_HOOKS_DIR;
}
function resolveWorkingDir(ctx) {
const candidates = [
ctx && ctx.worktree,
ctx && ctx.directory,
ctx && ctx.project && ctx.project.path,
ctx && ctx.project && ctx.project.directory,
process.cwd(),
];
for (const candidate of candidates) {
if (typeof candidate !== 'string' || candidate.length === 0) continue;
try {
if (fs.statSync(candidate).isDirectory()) return candidate;
} catch (_err) {
// try the next candidate
}
}
return process.cwd();
}
function runHook(scriptName, payload, timeoutMs, ctx) {
const cwd = resolveWorkingDir(ctx);
return new Promise((resolve) => {
let child;
let stdout = '';
let settled = false;
const settle = (value) => {
if (settled) return;
settled = true;
resolve(value || {});
};
try {
child = spawn('node', [path.join(resolveHooksDir(), scriptName)], {
cwd,
env: {
...process.env,
OPENCODE_PROJECT_DIR: cwd,
},
stdio: ['pipe', 'pipe', 'ignore'],
});
} catch (_err) {
settle({});
return;
}
const timer = setTimeout(() => {
try {
child.kill('SIGTERM');
} catch (_err) {
// best effort
}
settle({});
}, timeoutMs);
if (typeof timer.unref === 'function') {
timer.unref();
}
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
stdout += chunk;
});
child.on('error', () => {
clearTimeout(timer);
settle({});
});
child.on('close', () => {
clearTimeout(timer);
if (!stdout) {
settle({});
return;
}
try {
settle(JSON.parse(stdout));
} catch (_err) {
settle({});
}
});
try {
child.stdin.end(JSON.stringify(payload || {}));
} catch (_err) {
clearTimeout(timer);
settle({});
}
});
}
function sessionIdFromEvent(event) {
if (!event || typeof event !== 'object') return undefined;
const props = event.properties || {};
return (
props.sessionID ||
props.sessionId ||
props.session_id ||
(props.info && props.info.id)
);
}
function sessionIdFromPayload(payload) {
if (!payload || typeof payload !== 'object') return undefined;
const props = payload.properties || {};
const session = payload.session || {};
return (
payload.sessionID ||
payload.sessionId ||
payload.session_id ||
session.id ||
session.sessionID ||
props.sessionID ||
props.sessionId ||
props.session_id ||
(props.info && props.info.id)
);
}
function normalizeToolName(input) {
if (!input || typeof input !== 'object') return '';
const raw = input.tool || input.name || input.toolName || '';
return String(raw).toLowerCase();
}
function toolArgs(input, output) {
if (output && typeof output.args === 'object') return output.args;
if (input && typeof input.args === 'object') return input.args;
if (input && typeof input.input === 'object') return input.input;
if (input && typeof input.parameters === 'object') return input.parameters;
return {};
}
function toolResult(output) {
if (!output || typeof output !== 'object') return {};
return output.output || output.result || output.response || {};
}
function additionalContextFromHook(result) {
if (!result || typeof result !== 'object') return '';
if (typeof result.additionalContext === 'string') {
return result.additionalContext;
}
const hookSpecific = result.hookSpecificOutput;
if (
hookSpecific &&
typeof hookSpecific === 'object' &&
typeof hookSpecific.additionalContext === 'string'
) {
return hookSpecific.additionalContext;
}
return '';
}
function rememberSessionContext(sessionId, context) {
if (!sessionId || !context) return;
sessionStartContext.set(sessionId, context);
while (sessionStartContext.size > 50) {
const first = sessionStartContext.keys().next().value;
sessionStartContext.delete(first);
}
}
async function recallContext(sessionId, ctx) {
if (sessionId && sessionStartContext.has(sessionId)) {
return sessionStartContext.get(sessionId);
}
const result = await runHook(
'session-start.js',
{ session_id: sessionId, source: 'opencode' },
timeoutMs('EVOLVER_OPENCODE_SESSION_START_TIMEOUT_MS', 3000),
ctx
);
const context = additionalContextFromHook(result);
rememberSessionContext(sessionId, context);
return context;
}
async function Evolver(ctx = {}) {
return {
event: async ({ event } = {}) => {
if (!event || typeof event.type !== 'string') return;
if (event.type === 'session.created') {
const sessionId = sessionIdFromEvent(event);
const result = await runHook(
'session-start.js',
{ session_id: sessionId, source: 'opencode' },
timeoutMs('EVOLVER_OPENCODE_SESSION_START_TIMEOUT_MS', 3000),
ctx
);
rememberSessionContext(sessionId, additionalContextFromHook(result));
return;
}
if (event.type === 'session.deleted') {
await runHook(
'session-end.js',
{ session_id: sessionIdFromEvent(event), source: 'opencode' },
timeoutMs('EVOLVER_OPENCODE_SESSION_END_TIMEOUT_MS', 8000),
ctx
);
}
},
'tool.execute.after': async (input, output) => {
if (!WRITE_TOOLS.has(normalizeToolName(input))) return;
await runHook(
'signal-detect.js',
{
source: 'opencode',
tool_input: toolArgs(input, output),
tool_response: toolResult(output),
},
timeoutMs('EVOLVER_OPENCODE_SIGNAL_TIMEOUT_MS', 2000),
ctx
);
},
'experimental.session.compacting': async (input, output) => {
const context = await recallContext(sessionIdFromPayload(input), ctx);
if (!context) return;
if (!output || typeof output !== 'object') return;
if (!Array.isArray(output.context)) {
output.context = [];
}
output.context.push(`## Evolver Memory\n\n${context}`);
},
};
}
module.exports = { Evolver };
module.exports.default = Evolver;
module.exports._private = {
resolveHooksDir,
resolveWorkingDir,
runHook,
timeoutMs,
normalizeToolName,
toolArgs,
toolResult,
sessionIdFromPayload,
additionalContextFromHook,
WRITE_TOOLS,
};