opencode-auto-resume 1.0.2 → 1.0.4
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/index.js +53 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -203,6 +203,8 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
async function checkForToolCallAsText(sid, w) {
|
|
206
|
+
if (typeof sid !== "string" || !sid)
|
|
207
|
+
return;
|
|
206
208
|
if (w.userCancelled || w.toolTextRecovered)
|
|
207
209
|
return;
|
|
208
210
|
if (w.toolTextAttempts > 0) {
|
|
@@ -250,7 +252,7 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
250
252
|
try {
|
|
251
253
|
await ctx.client.session.prompt({
|
|
252
254
|
path: { id: sid },
|
|
253
|
-
body: { parts: [{ type: "text", text: TOOL_TEXT_RECOVERY_PROMPT }] }
|
|
255
|
+
body: { agent: typeof w.agent === "string" ? w.agent : undefined, parts: [{ type: "text", text: TOOL_TEXT_RECOVERY_PROMPT }] }
|
|
254
256
|
});
|
|
255
257
|
recordContinue(sid);
|
|
256
258
|
w.lastRetryAt = Date.now();
|
|
@@ -273,7 +275,7 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
273
275
|
try {
|
|
274
276
|
await ctx.client.session.prompt({
|
|
275
277
|
path: { id: sid },
|
|
276
|
-
body: { parts: [{ type: "text", text: "continue" }] }
|
|
278
|
+
body: { agent: typeof w.agent === "string" ? w.agent : undefined, parts: [{ type: "text", text: "continue" }] }
|
|
277
279
|
});
|
|
278
280
|
recordContinue(sid);
|
|
279
281
|
w.lastRetryAt = Date.now();
|
|
@@ -293,6 +295,8 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
293
295
|
}
|
|
294
296
|
}
|
|
295
297
|
async function tryAbortAndResume(sid, w) {
|
|
298
|
+
if (typeof sid !== "string" || !sid)
|
|
299
|
+
return false;
|
|
296
300
|
if (w.aborting)
|
|
297
301
|
return false;
|
|
298
302
|
w.aborting = true;
|
|
@@ -313,7 +317,7 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
313
317
|
try {
|
|
314
318
|
await ctx.client.session.prompt({
|
|
315
319
|
path: { id: sid },
|
|
316
|
-
body: { parts: [{ type: "text", text: "continue" }] }
|
|
320
|
+
body: { agent: typeof w.agent === "string" ? w.agent : undefined, parts: [{ type: "text", text: "continue" }] }
|
|
317
321
|
});
|
|
318
322
|
recordContinue(sid);
|
|
319
323
|
await log("info", `${short(sid)} - abort+continue done`);
|
|
@@ -330,6 +334,10 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
330
334
|
}
|
|
331
335
|
}
|
|
332
336
|
async function tryResume(sid, w, reason) {
|
|
337
|
+
if (typeof sid !== "string" || !sid) {
|
|
338
|
+
await log("warn", `tryResume called with invalid sid: ${sid}`);
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
333
341
|
const now = Date.now();
|
|
334
342
|
const elapsedSinceRetry = now - w.lastRetryAt;
|
|
335
343
|
const requiredBackoff = backoffMs(w.resumeAttempts);
|
|
@@ -342,10 +350,11 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
342
350
|
w.resumeAttempts++;
|
|
343
351
|
const idleSec = Math.round((now - w.lastActivityAt) / 1000);
|
|
344
352
|
await log("info", `${reason} on ${short(sid)} (${idleSec}s, retry ${w.resumeAttempts}/${maxRetries})`);
|
|
353
|
+
const agent = typeof w.agent === "string" ? w.agent : undefined;
|
|
345
354
|
try {
|
|
346
355
|
await ctx.client.session.prompt({
|
|
347
356
|
path: { id: sid },
|
|
348
|
-
body: { parts: [{ type: "text", text: "continue" }] }
|
|
357
|
+
body: { agent, model: true, parts: [{ type: "text", text: "continue" }] }
|
|
349
358
|
});
|
|
350
359
|
recordContinue(sid);
|
|
351
360
|
await log("info", `${short(sid)} - retry sent`);
|
|
@@ -358,6 +367,34 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
358
367
|
return false;
|
|
359
368
|
}
|
|
360
369
|
}
|
|
370
|
+
async function getSessionAgent(sid) {
|
|
371
|
+
if (typeof sid !== "string" || !sid)
|
|
372
|
+
return;
|
|
373
|
+
try {
|
|
374
|
+
const response = await ctx.client.session.messages({
|
|
375
|
+
path: { id: sid }
|
|
376
|
+
});
|
|
377
|
+
const data = response;
|
|
378
|
+
let messages = [];
|
|
379
|
+
if (Array.isArray(data)) {
|
|
380
|
+
messages = data;
|
|
381
|
+
} else if (Array.isArray(data.data)) {
|
|
382
|
+
messages = data.data;
|
|
383
|
+
} else if (Array.isArray(data.messages)) {
|
|
384
|
+
messages = data.messages;
|
|
385
|
+
}
|
|
386
|
+
for (let i = messages.length - 1;i >= 0; i--) {
|
|
387
|
+
const msg = messages[i];
|
|
388
|
+
const role = msg.role;
|
|
389
|
+
if (role === "assistant") {
|
|
390
|
+
const agent = msg.agent;
|
|
391
|
+
if (agent)
|
|
392
|
+
return agent;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
} catch {}
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
361
398
|
async function discoverSessions() {
|
|
362
399
|
try {
|
|
363
400
|
const response = await ctx.client.session.list();
|
|
@@ -370,7 +407,8 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
370
407
|
}
|
|
371
408
|
for (const s of list) {
|
|
372
409
|
const sid = s.id;
|
|
373
|
-
if (sid
|
|
410
|
+
if (sid) {
|
|
411
|
+
const isNew = !sessions.has(sid);
|
|
374
412
|
ensureWatch(sid);
|
|
375
413
|
const status = s.status;
|
|
376
414
|
if (status) {
|
|
@@ -379,7 +417,16 @@ var AutoResumePlugin = async (ctx, options) => {
|
|
|
379
417
|
if (status === "idle")
|
|
380
418
|
w.idleSince = Date.now();
|
|
381
419
|
}
|
|
382
|
-
|
|
420
|
+
if (isNew) {
|
|
421
|
+
const agent = await getSessionAgent(sid);
|
|
422
|
+
if (agent) {
|
|
423
|
+
const w = sessions.get(sid);
|
|
424
|
+
w.agent = agent;
|
|
425
|
+
log("debug", `Discovered session ${short(sid)} with agent: ${agent}`);
|
|
426
|
+
} else {
|
|
427
|
+
log("debug", `Discovered session ${short(sid)} via list()`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
383
430
|
}
|
|
384
431
|
}
|
|
385
432
|
} catch (err) {
|
package/package.json
CHANGED