opencode-feishu 0.7.3 → 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
  }
@@ -99315,6 +99361,10 @@ function isModelError(errMsg, rawError) {
99315
99361
  if (rawError && typeof rawError === "object") {
99316
99362
  const e = rawError;
99317
99363
  const fields = [e.type, e.name, e.message].filter(Boolean).map(String);
99364
+ if (e.data && typeof e.data === "object" && "message" in e.data) {
99365
+ const dataMsg = e.data.message;
99366
+ if (dataMsg) fields.push(String(dataMsg));
99367
+ }
99318
99368
  return fields.some((f) => f.includes("ModelNotFound") || f.includes("ProviderModelNotFound"));
99319
99369
  }
99320
99370
  return false;
@@ -99353,11 +99403,17 @@ async function handleEvent(event, deps) {
99353
99403
  errMsg = error;
99354
99404
  } else if (error && typeof error === "object") {
99355
99405
  const e = error;
99356
- errMsg = String(e.message ?? e.type ?? e.name ?? "An unexpected error occurred");
99406
+ const rawDataMsg = e.data && typeof e.data === "object" && "message" in e.data ? e.data.message : void 0;
99407
+ const dataMsg = rawDataMsg != null ? String(rawDataMsg) : void 0;
99408
+ errMsg = String(e.message ?? dataMsg ?? e.type ?? e.name ?? "An unexpected error occurred");
99357
99409
  } else {
99358
99410
  errMsg = String(error);
99359
99411
  }
99360
- const safeErrorFields = error && typeof error === "object" ? { type: error.type, name: error.name } : { raw: String(error) };
99412
+ const safeErrorFields = error && typeof error === "object" ? {
99413
+ type: error.type,
99414
+ name: error.name,
99415
+ dataMessage: error.data?.message
99416
+ } : { raw: String(error) };
99361
99417
  deps.log("warn", "\u6536\u5230 session.error \u4E8B\u4EF6", {
99362
99418
  sessionId,
99363
99419
  error: safeErrorFields,
@@ -99367,21 +99423,28 @@ async function handleEvent(event, deps) {
99367
99423
  if (isModelError(errMsg, props.error)) {
99368
99424
  const sessionKey = invalidateCachedSession(sessionId);
99369
99425
  if (sessionKey) {
99370
- try {
99371
- const newSession = await forkSession(deps.client, sessionId, sessionKey, deps.directory);
99372
- setCachedSession(sessionKey, newSession);
99373
- deps.log("warn", "\u6A21\u578B\u4E0D\u517C\u5BB9\uFF0C\u5DF2\u4E3B\u52A8 fork \u4F1A\u8BDD", {
99374
- oldSessionId: sessionId,
99375
- newSessionId: newSession.id,
99376
- sessionKey
99377
- });
99378
- migratePending(sessionId, newSession.id);
99379
- } catch (forkErr) {
99380
- deps.log("error", "\u4E3B\u52A8 fork \u5931\u8D25", {
99381
- sessionId,
99382
- sessionKey,
99383
- error: forkErr instanceof Error ? forkErr.message : String(forkErr)
99384
- });
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
+ }
99385
99448
  }
99386
99449
  }
99387
99450
  }
@@ -99467,6 +99530,7 @@ async function handleChat(ctx, deps) {
99467
99530
  body: { parts }
99468
99531
  });
99469
99532
  const finalText = await pollForResponse(client, session.id, { timeout, pollInterval, stablePolls, query });
99533
+ clearForkAttempts(sessionKey);
99470
99534
  await replyOrUpdate(feishuClient, chatId, placeholderId, finalText || "\u26A0\uFE0F \u54CD\u5E94\u8D85\u65F6");
99471
99535
  const { autoPrompt } = config;
99472
99536
  if (autoPrompt.enabled && shouldReply) {
@@ -99512,7 +99576,7 @@ async function handleChat(ctx, deps) {
99512
99576
  sessionKey: sessionKey.replace(/-[^-]+$/, "-***"),
99513
99577
  chatType,
99514
99578
  error: thrownError,
99515
- ...sessionError ? { sessionError } : {}
99579
+ ...sessionError ? { sessionError } : { sseRaceMiss: true }
99516
99580
  });
99517
99581
  const msg = "\u274C " + errorMessage;
99518
99582
  await replyOrUpdate(feishuClient, chatId, placeholderId, msg);