opencode-feishu 0.7.4 → 0.7.5

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 CHANGED
@@ -99266,12 +99266,58 @@ async function forkSession(client, oldSessionId, sessionKey, directory) {
99266
99266
  }
99267
99267
  return { id: resp.data.id, title };
99268
99268
  }
99269
+ async function createFreshSession(client, sessionKey, directory) {
99270
+ const query = directory ? { directory } : void 0;
99271
+ const title = generateSessionTitle(sessionKey);
99272
+ const resp = await client.session.create({ query, body: { title } });
99273
+ if (!resp?.data?.id) {
99274
+ const err = resp?.error;
99275
+ throw new Error(
99276
+ `\u521B\u5EFA\u65B0\u4F1A\u8BDD\u5931\u8D25: ${err ? JSON.stringify(err) : "unknown"}`
99277
+ );
99278
+ }
99279
+ return { id: resp.data.id, title: resp.data.title };
99280
+ }
99281
+ async function forkOrCreateSession(client, oldSessionId, sessionKey, directory, log) {
99282
+ try {
99283
+ return await forkSession(client, oldSessionId, sessionKey, directory);
99284
+ } catch (forkErr) {
99285
+ log?.("warn", "Fork \u5931\u8D25\uFF0C\u56DE\u9000\u5230\u521B\u5EFA\u65B0\u4F1A\u8BDD", {
99286
+ oldSessionId,
99287
+ sessionKey,
99288
+ error: forkErr instanceof Error ? forkErr.message : String(forkErr)
99289
+ });
99290
+ return await createFreshSession(client, sessionKey, directory);
99291
+ }
99292
+ }
99269
99293
 
99270
99294
  // src/handler/event.ts
99271
99295
  var pendingBySession = /* @__PURE__ */ new Map();
99272
99296
  var sessionErrors = /* @__PURE__ */ new Map();
99273
99297
  var sessionErrorTimeouts = /* @__PURE__ */ new Map();
99274
99298
  var SESSION_ERROR_TTL_MS = 3e4;
99299
+ var forkAttempts = /* @__PURE__ */ new Map();
99300
+ var forkAttemptTimeouts = /* @__PURE__ */ new Map();
99301
+ var MAX_FORK_ATTEMPTS = 2;
99302
+ var FORK_ATTEMPTS_TTL_MS = 36e5;
99303
+ function clearForkAttempts(sessionKey) {
99304
+ forkAttempts.delete(sessionKey);
99305
+ const timer = forkAttemptTimeouts.get(sessionKey);
99306
+ if (timer) {
99307
+ clearTimeout(timer);
99308
+ forkAttemptTimeouts.delete(sessionKey);
99309
+ }
99310
+ }
99311
+ function setForkAttempts(sessionKey, count) {
99312
+ forkAttempts.set(sessionKey, count);
99313
+ const existing = forkAttemptTimeouts.get(sessionKey);
99314
+ if (existing) clearTimeout(existing);
99315
+ const timeoutId = setTimeout(() => {
99316
+ forkAttempts.delete(sessionKey);
99317
+ forkAttemptTimeouts.delete(sessionKey);
99318
+ }, FORK_ATTEMPTS_TTL_MS);
99319
+ forkAttemptTimeouts.set(sessionKey, timeoutId);
99320
+ }
99275
99321
  function getSessionError(sessionId) {
99276
99322
  return sessionErrors.get(sessionId);
99277
99323
  }
@@ -99377,21 +99423,28 @@ async function handleEvent(event, deps) {
99377
99423
  if (isModelError(errMsg, props.error)) {
99378
99424
  const sessionKey = invalidateCachedSession(sessionId);
99379
99425
  if (sessionKey) {
99380
- try {
99381
- const newSession = await forkSession(deps.client, sessionId, sessionKey, deps.directory);
99382
- setCachedSession(sessionKey, newSession);
99383
- deps.log("warn", "\u6A21\u578B\u4E0D\u517C\u5BB9\uFF0C\u5DF2\u4E3B\u52A8 fork \u4F1A\u8BDD", {
99384
- oldSessionId: sessionId,
99385
- newSessionId: newSession.id,
99386
- sessionKey
99387
- });
99388
- migratePending(sessionId, newSession.id);
99389
- } catch (forkErr) {
99390
- deps.log("error", "\u4E3B\u52A8 fork \u5931\u8D25", {
99391
- sessionId,
99392
- sessionKey,
99393
- error: forkErr instanceof Error ? forkErr.message : String(forkErr)
99394
- });
99426
+ const attempts = forkAttempts.get(sessionKey) ?? 0;
99427
+ if (attempts >= MAX_FORK_ATTEMPTS) {
99428
+ deps.log("warn", "\u5DF2\u8FBE fork \u4E0A\u9650\uFF0C\u653E\u5F03\u6062\u590D", { sessionKey, attempts });
99429
+ } else {
99430
+ setForkAttempts(sessionKey, attempts + 1);
99431
+ try {
99432
+ const newSession = await forkOrCreateSession(deps.client, sessionId, sessionKey, deps.directory, deps.log);
99433
+ setCachedSession(sessionKey, newSession);
99434
+ deps.log("warn", "\u6A21\u578B\u4E0D\u517C\u5BB9\uFF0C\u5DF2\u6062\u590D\u4F1A\u8BDD", {
99435
+ oldSessionId: sessionId,
99436
+ newSessionId: newSession.id,
99437
+ sessionKey,
99438
+ forkAttempt: attempts + 1
99439
+ });
99440
+ migratePending(sessionId, newSession.id);
99441
+ } catch (recoverErr) {
99442
+ deps.log("error", "\u4F1A\u8BDD\u6062\u590D\u5931\u8D25", {
99443
+ sessionId,
99444
+ sessionKey,
99445
+ error: recoverErr instanceof Error ? recoverErr.message : String(recoverErr)
99446
+ });
99447
+ }
99395
99448
  }
99396
99449
  }
99397
99450
  }
@@ -99477,6 +99530,7 @@ async function handleChat(ctx, deps) {
99477
99530
  body: { parts }
99478
99531
  });
99479
99532
  const finalText = await pollForResponse(client, session.id, { timeout, pollInterval, stablePolls, query });
99533
+ clearForkAttempts(sessionKey);
99480
99534
  await replyOrUpdate(feishuClient, chatId, placeholderId, finalText || "\u26A0\uFE0F \u54CD\u5E94\u8D85\u65F6");
99481
99535
  const { autoPrompt } = config;
99482
99536
  if (autoPrompt.enabled && shouldReply) {
@@ -99522,7 +99576,7 @@ async function handleChat(ctx, deps) {
99522
99576
  sessionKey: sessionKey.replace(/-[^-]+$/, "-***"),
99523
99577
  chatType,
99524
99578
  error: thrownError,
99525
- ...sessionError ? { sessionError } : {}
99579
+ ...sessionError ? { sessionError } : { sseRaceMiss: true }
99526
99580
  });
99527
99581
  const msg = "\u274C " + errorMessage;
99528
99582
  await replyOrUpdate(feishuClient, chatId, placeholderId, msg);