( thread: Record<string, unknown>, )
| 335 | } |
| 336 | |
| 337 | export function getResumedTurnState( |
| 338 | thread: Record<string, unknown>, |
| 339 | ): ResumedTurnState { |
| 340 | const explicitState = getExplicitActiveTurnState(thread); |
| 341 | if (explicitState.explicit) { |
| 342 | return { |
| 343 | activeTurnId: explicitState.activeTurnId, |
| 344 | activeTurnStartedAtMs: explicitState.activeTurnStartedAtMs, |
| 345 | confidentNoActiveTurn: !explicitState.activeTurnId, |
| 346 | }; |
| 347 | } |
| 348 | |
| 349 | const turns = Array.isArray(thread.turns) |
| 350 | ? (thread.turns as Array<Record<string, unknown>>) |
| 351 | : []; |
| 352 | let sawTerminalStatus = false; |
| 353 | let sawUnknownStatus = false; |
| 354 | for (let index = turns.length - 1; index >= 0; index -= 1) { |
| 355 | const turn = turns[index]; |
| 356 | if (!turn || typeof turn !== "object") { |
| 357 | sawUnknownStatus = true; |
| 358 | continue; |
| 359 | } |
| 360 | const status = classifyTurnStatus( |
| 361 | normalizeTurnStatus( |
| 362 | turn.status ?? turn.turnStatus ?? turn.turn_status, |
| 363 | ), |
| 364 | ); |
| 365 | if (status === "active") { |
| 366 | const turnId = asString(turn.id ?? turn.turnId ?? turn.turn_id).trim(); |
| 367 | if (turnId) { |
| 368 | return { |
| 369 | activeTurnId: turnId, |
| 370 | activeTurnStartedAtMs: turnStartedAtMs(turn), |
| 371 | confidentNoActiveTurn: false, |
| 372 | }; |
| 373 | } |
| 374 | sawUnknownStatus = true; |
| 375 | continue; |
| 376 | } |
| 377 | if (status === "terminal") { |
| 378 | sawTerminalStatus = true; |
| 379 | continue; |
| 380 | } |
| 381 | sawUnknownStatus = true; |
| 382 | } |
| 383 | return { |
| 384 | activeTurnId: null, |
| 385 | activeTurnStartedAtMs: null, |
| 386 | confidentNoActiveTurn: sawTerminalStatus && !sawUnknownStatus, |
| 387 | }; |
| 388 | } |
| 389 | |
| 390 | export function getResumedActiveTurnId(thread: Record<string, unknown>): string | null { |
| 391 | return getResumedTurnState(thread).activeTurnId; |
no test coverage detected