@michengai/dsh-im-connect 1.0.49 → 1.0.50

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.
@@ -108,7 +108,23 @@ function failureKind(error: unknown): string {
108
108
  return 'unknown'
109
109
  }
110
110
 
111
- export interface TurnCompletion { sessionId: string; turn: number; items: MessageProgress[]; status: 'completed' | 'empty' | 'error' | 'cancelled' | 'delivery-failed'; reasonKind?: string }
111
+ /** 提取宿主模型回合结束时附带的错误诊断信息,供渠道在失败通知中透传。 */
112
+ export function extractErrorDetail(error: unknown): string | undefined {
113
+ if (!error) return undefined
114
+ if (typeof error === 'string') {
115
+ const trimmed = error.trim()
116
+ return trimmed || undefined
117
+ }
118
+ if (typeof error === 'object') {
119
+ const value = error as { message?: unknown; description?: unknown; detail?: unknown }
120
+ if (typeof value.message === 'string' && value.message.trim()) return value.message.trim()
121
+ if (typeof value.description === 'string' && value.description.trim()) return value.description.trim()
122
+ if (typeof value.detail === 'string' && value.detail.trim()) return value.detail.trim()
123
+ }
124
+ return undefined
125
+ }
126
+
127
+ export interface TurnCompletion { sessionId: string; turn: number; items: MessageProgress[]; status: 'completed' | 'empty' | 'error' | 'cancelled' | 'delivery-failed'; reasonKind?: string; errorDetail?: string }
112
128
 
113
129
  type Group = { requestId: string; sessionId: string; items: MessageProgress[]; turn?: number; revision?: number; waiting?: number }
114
130
 
@@ -190,10 +206,13 @@ export class ProgressTracker {
190
206
  this.groups.delete(group)
191
207
  }
192
208
  if (![...this.groups].some(group => group.sessionId === sessionId)) this.currentTurn.delete(sessionId)
209
+ const errorDetail = extractErrorDetail(data?.reason?.error)
193
210
  if (live.length && !superseded) this.onComplete({ sessionId, turn: data.turn, items: live.flatMap(group => group.items),
194
211
  status: data?.reason?.kind === 'error' ? 'error' : data?.reason?.kind !== 'completed' ? 'cancelled'
195
212
  : results.length === 0 ? 'empty' : allSent ? 'completed' : 'delivery-failed',
196
- reasonKind: data?.reason?.kind })
213
+ reasonKind: data?.reason?.kind,
214
+ errorDetail,
215
+ })
197
216
  })
198
217
  }
199
218
  }
@@ -254,9 +273,12 @@ export class ProgressTracker {
254
273
  }
255
274
  }
256
275
 
257
- type TypingGroup = { users: number; scope: AbortController; timer?: ReturnType<typeof setInterval>; pending?: Promise<void> }
276
+ type TypingGroup = { users: number; scope: AbortController; timer?: ReturnType<typeof setInterval>; retry?: ReturnType<typeof setTimeout>; pending?: Promise<void>; missed?: boolean }
258
277
  const typingGroups = new WeakMap<ChannelAdapter, Map<string, TypingGroup>>()
259
278
 
279
+ /** 一次心跳失败后的补发延迟:网络抖一下不该干等到下一个 4 秒点。 */
280
+ const TYPING_RETRY_AFTER_FAILURE_MS = 1200
281
+
260
282
  /** 同一账号同一聊天只有一个 typing 定时器;旧任务结束不能停掉新任务。 */
261
283
  function acquireTyping(channel: ChannelAdapter, chatId: string,
262
284
  safely: (operation: (signal: AbortSignal) => Promise<unknown>, timeoutMs?: number) => Promise<void>): () => void {
@@ -270,18 +292,32 @@ function acquireTyping(channel: ChannelAdapter, chatId: string,
270
292
  const current = group
271
293
  const typingBudget = positiveTimeout(channel.typingTimeoutMs, DEFAULT_TYPING_TIMEOUT_MS)
272
294
  const pulse = () => {
273
- if (current.pending || current.scope.signal.aborted || chats!.get(chatId) !== current) return
295
+ if (current.scope.signal.aborted || chats!.get(chatId) !== current || current.retry) return
296
+ if (current.pending) { current.missed = true; return }
297
+ current.missed = false
274
298
  // pending 必须跟随有界等待,而不是可能永不结束的旧 SDK 请求。
299
+ let failed = false
300
+ let succeeded = false
275
301
  const pending = safely(async signal => {
276
302
  const guard = AbortSignal.any([signal, current.scope.signal])
277
303
  await fileOperation(Promise.resolve().then(() => {
278
304
  guard.throwIfAborted()
279
305
  if (chats!.get(chatId) !== current || current.users === 0) return
280
- return channel.sendAction!(chatId, 'typing', guard)
306
+ return channel.sendAction!(chatId, 'typing', guard).catch((error: unknown) => { failed = true; throw error })
281
307
  }), guard)
308
+ succeeded = true
282
309
  }, typingBudget)
283
310
  current.pending = pending
284
- void pending.finally(() => { if (current.pending === pending) current.pending = undefined })
311
+ void pending.finally(() => {
312
+ if (current.pending === pending) current.pending = undefined
313
+ if (current.scope.signal.aborted || chats!.get(chatId) !== current) return
314
+ // 请求跨过续发点时合并补一次;保持单飞,失败仍走退避而不立即补发。
315
+ if (!failed) { if (succeeded && current.missed) pulse(); return }
316
+ // 抖动导致的单次失败:隔 1.2 秒补一次,不让提示空窗撑到下一个整点。
317
+ if (current.retry) clearTimeout(current.retry)
318
+ current.retry = setTimeout(() => { current.retry = undefined; pulse() }, TYPING_RETRY_AFTER_FAILURE_MS)
319
+ current.retry.unref?.()
320
+ })
285
321
  }
286
322
  pulse()
287
323
  if (channel.typingIntervalMs) {
@@ -10,6 +10,10 @@ export type HistoryHost = { get?(name: string): unknown }
10
10
  * 官方 Session 没有公开 events,同步活日志是 snapshotEvents。
11
11
  * events 只服务夹具和非官方对象;空数组继续往下看,避免把“还没写上”当成没有历史。
12
12
  */
13
+ export function hasSessionHistoryCapability(session?: HistorySession): boolean {
14
+ return typeof session?.snapshotEvents === 'function' || Array.isArray(session?.events)
15
+ }
16
+
13
17
  export function readLiveSessionHistory(session?: HistorySession): readonly HistoryEvent[] | undefined {
14
18
  if (Array.isArray(session?.events) && session.events.length > 0) return session.events
15
19
  if (typeof session?.snapshotEvents === 'function') {