@gricha/perry 0.3.12 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/perry-worker
CHANGED
|
Binary file
|
|
@@ -13,6 +13,19 @@ async function findAvailablePort(containerName) {
|
|
|
13
13
|
});
|
|
14
14
|
return parseInt(result.stdout.trim(), 10);
|
|
15
15
|
}
|
|
16
|
+
async function findExistingServer(containerName) {
|
|
17
|
+
try {
|
|
18
|
+
const result = await execInContainer(containerName, ['sh', '-c', 'pgrep -a -f "opencode serve" | grep -oP "\\--port \\K[0-9]+" | head -1'], { user: 'workspace' });
|
|
19
|
+
const port = parseInt(result.stdout.trim(), 10);
|
|
20
|
+
if (port && (await isServerRunning(containerName, port))) {
|
|
21
|
+
return port;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
// No existing server
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
16
29
|
async function isServerRunning(containerName, port) {
|
|
17
30
|
try {
|
|
18
31
|
const result = await execInContainer(containerName, ['curl', '-s', '-o', '/dev/null', '-w', '%{http_code}', `http://localhost:${port}/session`], { user: 'workspace' });
|
|
@@ -34,8 +47,14 @@ async function getServerLogs(containerName) {
|
|
|
34
47
|
}
|
|
35
48
|
}
|
|
36
49
|
async function startServer(containerName) {
|
|
37
|
-
const
|
|
38
|
-
if (
|
|
50
|
+
const cached = serverPorts.get(containerName);
|
|
51
|
+
if (cached && (await isServerRunning(containerName, cached))) {
|
|
52
|
+
return cached;
|
|
53
|
+
}
|
|
54
|
+
const existing = await findExistingServer(containerName);
|
|
55
|
+
if (existing) {
|
|
56
|
+
console.log(`[opencode] Found existing server on port ${existing} in ${containerName}`);
|
|
57
|
+
serverPorts.set(containerName, existing);
|
|
39
58
|
return existing;
|
|
40
59
|
}
|
|
41
60
|
const starting = serverStarting.get(containerName);
|
|
@@ -176,6 +195,7 @@ export class OpenCodeAdapter {
|
|
|
176
195
|
if (!this.agentSessionId) {
|
|
177
196
|
this.agentSessionId = await this.createSession(baseUrl);
|
|
178
197
|
this.emit({ type: 'system', content: `Session: ${this.agentSessionId.slice(0, 8)}...` });
|
|
198
|
+
this.statusCallback?.(this.status);
|
|
179
199
|
}
|
|
180
200
|
this.setStatus('running');
|
|
181
201
|
this.emit({ type: 'system', content: 'Processing...' });
|
|
@@ -229,7 +249,10 @@ export class OpenCodeAdapter {
|
|
|
229
249
|
return session.id;
|
|
230
250
|
}
|
|
231
251
|
async sendAndStream(baseUrl, message) {
|
|
232
|
-
|
|
252
|
+
let sseError = null;
|
|
253
|
+
const sseReady = this.startSSEStream().catch((err) => {
|
|
254
|
+
sseError = err;
|
|
255
|
+
});
|
|
233
256
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
234
257
|
const payload = { parts: [{ type: 'text', text: message }] };
|
|
235
258
|
if (this.isHost) {
|
|
@@ -263,6 +286,9 @@ export class OpenCodeAdapter {
|
|
|
263
286
|
}
|
|
264
287
|
}
|
|
265
288
|
await sseReady;
|
|
289
|
+
if (sseError) {
|
|
290
|
+
throw sseError;
|
|
291
|
+
}
|
|
266
292
|
}
|
|
267
293
|
startSSEStream() {
|
|
268
294
|
return new Promise((resolve, reject) => {
|
|
@@ -284,6 +310,7 @@ export class OpenCodeAdapter {
|
|
|
284
310
|
this.sseProcess = proc;
|
|
285
311
|
const decoder = new TextDecoder();
|
|
286
312
|
let buffer = '';
|
|
313
|
+
let eventCount = 0;
|
|
287
314
|
const finish = () => {
|
|
288
315
|
if (!resolved) {
|
|
289
316
|
resolved = true;
|
|
@@ -316,7 +343,9 @@ export class OpenCodeAdapter {
|
|
|
316
343
|
continue;
|
|
317
344
|
try {
|
|
318
345
|
const event = JSON.parse(data);
|
|
346
|
+
eventCount++;
|
|
319
347
|
if (event.type === 'session.idle') {
|
|
348
|
+
console.log(`[opencode] SSE received session.idle after ${eventCount} events`);
|
|
320
349
|
receivedIdle = true;
|
|
321
350
|
clearTimeout(timeout);
|
|
322
351
|
proc.kill();
|
|
@@ -375,7 +404,7 @@ export class OpenCodeAdapter {
|
|
|
375
404
|
}
|
|
376
405
|
else if (!resolved) {
|
|
377
406
|
resolved = true;
|
|
378
|
-
reject(new Error(
|
|
407
|
+
reject(new Error(`SSE stream ended unexpectedly without session.idle (received ${eventCount} events)`));
|
|
379
408
|
}
|
|
380
409
|
})().catch((err) => {
|
|
381
410
|
clearTimeout(timeout);
|
|
@@ -90,7 +90,7 @@ export class LiveChatHandler {
|
|
|
90
90
|
const agentType = message.agentType || this.agentType;
|
|
91
91
|
if (message.sessionId) {
|
|
92
92
|
// Look up by internal sessionId or agentSessionId (Claude session ID)
|
|
93
|
-
const found = sessionManager.findSession(message.sessionId);
|
|
93
|
+
const found = await sessionManager.findSession(message.sessionId);
|
|
94
94
|
if (found) {
|
|
95
95
|
connection.sessionId = found.sessionId;
|
|
96
96
|
const sendFn = (msg) => {
|
|
@@ -241,18 +241,41 @@ export class SessionManager {
|
|
|
241
241
|
const session = this.sessions.get(sessionId);
|
|
242
242
|
return session?.info ?? null;
|
|
243
243
|
}
|
|
244
|
-
findSession(id) {
|
|
245
|
-
// First try direct lookup by internal sessionId
|
|
244
|
+
async findSession(id) {
|
|
245
|
+
// First try direct lookup by internal sessionId (in-memory cache)
|
|
246
246
|
const direct = this.sessions.get(id);
|
|
247
247
|
if (direct) {
|
|
248
248
|
return { sessionId: id, info: direct.info };
|
|
249
249
|
}
|
|
250
|
-
// Then search by agentSessionId
|
|
250
|
+
// Then search by agentSessionId in memory
|
|
251
251
|
for (const [sessionId, session] of this.sessions) {
|
|
252
252
|
if (session.info.agentSessionId === id) {
|
|
253
253
|
return { sessionId, info: session.info };
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
|
+
// Not in memory - check disk registry
|
|
257
|
+
if (this.stateDir) {
|
|
258
|
+
// Try lookup by perrySessionId first
|
|
259
|
+
let record = await registry.getSession(this.stateDir, id);
|
|
260
|
+
// Then try by agentSessionId
|
|
261
|
+
if (!record) {
|
|
262
|
+
record = await registry.findByAgentSessionId(this.stateDir, id);
|
|
263
|
+
}
|
|
264
|
+
// Found on disk - restore the session
|
|
265
|
+
if (record) {
|
|
266
|
+
const restoredId = await this.startSession({
|
|
267
|
+
sessionId: record.perrySessionId,
|
|
268
|
+
workspaceName: record.workspaceName,
|
|
269
|
+
agentType: record.agentType,
|
|
270
|
+
agentSessionId: record.agentSessionId ?? undefined,
|
|
271
|
+
projectPath: record.projectPath ?? undefined,
|
|
272
|
+
});
|
|
273
|
+
const restored = this.sessions.get(restoredId);
|
|
274
|
+
if (restored) {
|
|
275
|
+
return { sessionId: restoredId, info: restored.info };
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
256
279
|
return null;
|
|
257
280
|
}
|
|
258
281
|
getSessionStatus(sessionId) {
|
|
@@ -98,6 +98,25 @@ export async function getSessionsForWorkspace(stateDir, workspaceName) {
|
|
|
98
98
|
.filter((record) => record.workspaceName === workspaceName)
|
|
99
99
|
.sort((a, b) => new Date(b.lastActivity).getTime() - new Date(a.lastActivity).getTime());
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Get a specific session by perrySessionId.
|
|
103
|
+
*/
|
|
104
|
+
export async function getSession(stateDir, perrySessionId) {
|
|
105
|
+
const registry = await loadRegistry(stateDir);
|
|
106
|
+
return registry.sessions[perrySessionId] ?? null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Find a session by agentSessionId.
|
|
110
|
+
*/
|
|
111
|
+
export async function findByAgentSessionId(stateDir, agentSessionId) {
|
|
112
|
+
const registry = await loadRegistry(stateDir);
|
|
113
|
+
for (const record of Object.values(registry.sessions)) {
|
|
114
|
+
if (record.agentSessionId === agentSessionId) {
|
|
115
|
+
return record;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
101
120
|
/**
|
|
102
121
|
* Import an external session (discovered from agent storage).
|
|
103
122
|
* Creates a Perry session record for a session that wasn't started through Perry.
|