@llblab/pi-telegram 0.24.2 → 0.24.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/lib/queue.ts CHANGED
@@ -1258,20 +1258,27 @@ export async function handleTelegramAgentEndRuntime<
1258
1258
  const isDeliveryActive = (): boolean =>
1259
1259
  deps.isSessionActive?.() !== false &&
1260
1260
  (!turn || deps.isTurnTransportActive?.(turn) !== false);
1261
+ const updateStatusIgnoringStaleContext = (): void => {
1262
+ try {
1263
+ deps.updateStatus();
1264
+ } catch (error) {
1265
+ if (!isTelegramStaleContextError(error)) throw error;
1266
+ }
1267
+ };
1261
1268
  if (!isDeliveryActive()) {
1262
1269
  deps.resetRuntimeState();
1263
- deps.updateStatus();
1270
+ updateStatusIgnoringStaleContext();
1264
1271
  deps.dispatchNextQueuedTelegramTurn();
1265
1272
  return;
1266
1273
  }
1267
1274
  deps.resetRuntimeState();
1268
1275
  await deps.waitForTypingIdle?.();
1269
1276
  if (!isDeliveryActive()) {
1270
- deps.updateStatus();
1277
+ updateStatusIgnoringStaleContext();
1271
1278
  deps.dispatchNextQueuedTelegramTurn();
1272
1279
  return;
1273
1280
  }
1274
- deps.updateStatus();
1281
+ updateStatusIgnoringStaleContext();
1275
1282
  const endPlan = buildTelegramAgentEndPlan({
1276
1283
  hasTurn: !!turn,
1277
1284
  stopReason: assistant.stopReason,
@@ -2117,6 +2124,8 @@ export interface TelegramDeferredQueueDispatchRuntime<TContext = unknown> {
2117
2124
  bind: (ctx: TContext) => void;
2118
2125
  unbind: () => void;
2119
2126
  isBound: () => boolean;
2127
+ getGeneration: () => number;
2128
+ isGenerationActive: (generation: number) => boolean;
2120
2129
  request: (dispatchNextQueuedTelegramTurn: (ctx: TContext) => void) => void;
2121
2130
  }
2122
2131
 
@@ -2149,6 +2158,9 @@ export function createTelegramDeferredQueueDispatchRuntime<TContext = unknown>(
2149
2158
  clearTimers();
2150
2159
  },
2151
2160
  isBound: () => boundContext !== undefined,
2161
+ getGeneration: () => generation,
2162
+ isGenerationActive: (expectedGeneration) =>
2163
+ boundContext !== undefined && generation === expectedGeneration,
2152
2164
  request: (dispatchNextQueuedTelegramTurn) => {
2153
2165
  if (boundContext === undefined) return;
2154
2166
  const scheduledGeneration = generation;
@@ -2265,6 +2277,8 @@ export interface TelegramQueueDispatchControllerDeps<
2265
2277
  setQueuedItems: (items: TelegramQueueItem<TContext>[]) => void;
2266
2278
  canDispatch: (ctx: TContext) => boolean;
2267
2279
  hasDispatchContext?: () => boolean;
2280
+ getDispatchGeneration?: () => number;
2281
+ isDispatchGenerationActive?: (generation: number) => boolean;
2268
2282
  updateStatus: (ctx: TContext, error?: string) => void;
2269
2283
  sendTextReply: TelegramControlRuntimeDeps<TContext>["sendTextReply"];
2270
2284
  onPromptDispatchStart: (ctx: TContext, chatId: number) => void;
@@ -2318,6 +2332,8 @@ export function createTelegramQueueDispatchRuntime<TContext = unknown>(
2318
2332
  hasPendingMessages: deps.hasPendingMessages,
2319
2333
  }),
2320
2334
  hasDispatchContext: deps.hasDispatchContext,
2335
+ getDispatchGeneration: deps.getDispatchGeneration,
2336
+ isDispatchGenerationActive: deps.isDispatchGenerationActive,
2321
2337
  updateStatus: deps.updateStatus,
2322
2338
  sendTextReply: deps.sendTextReply,
2323
2339
  onPromptDispatchStart: deps.onPromptDispatchStart,
@@ -2363,6 +2379,7 @@ export function createTelegramQueueDispatchController<TContext = unknown>(
2363
2379
  executeTelegramQueueDispatchPlan(dispatchPlan, {
2364
2380
  executeControlItem: (item) => {
2365
2381
  controlDispatchPending = true;
2382
+ const dispatchGeneration = deps.getDispatchGeneration?.();
2366
2383
  deps.updateStatus(ctx);
2367
2384
  void executeTelegramControlItemRuntime(item, {
2368
2385
  ctx,
@@ -2371,6 +2388,13 @@ export function createTelegramQueueDispatchController<TContext = unknown>(
2371
2388
  onSettled: () => {
2372
2389
  controlDispatchPending = false;
2373
2390
  if (deps.hasDispatchContext && !deps.hasDispatchContext()) return;
2391
+ if (
2392
+ dispatchGeneration !== undefined &&
2393
+ deps.isDispatchGenerationActive &&
2394
+ !deps.isDispatchGenerationActive(dispatchGeneration)
2395
+ ) {
2396
+ return;
2397
+ }
2374
2398
  deps.updateStatus(ctx);
2375
2399
  controller.dispatchNext(ctx);
2376
2400
  },