检测上次会话是否在中途被中断。 对应 Reference: EP09 §3 detectTurnInterruption(): | 最后消息类型 | 状态 | 动作 | | assistant | 轮次完成 | none | | user(tool_result) | 工具执行中 | interrupted_turn → 注入继续 | | user(text)
(messages: list[ConversationMessage])
| 478 | # ============================================================ |
| 479 | |
| 480 | def detect_interruption(messages: list[ConversationMessage]) -> str: |
| 481 | """ |
| 482 | 检测上次会话是否在中途被中断。 |
| 483 | |
| 484 | 对应 Reference: EP09 §3 |
| 485 | detectTurnInterruption(): |
| 486 | | 最后消息类型 | 状态 | 动作 | |
| 487 | | assistant | 轮次完成 | none | |
| 488 | | user(tool_result) | 工具执行中 | interrupted_turn → 注入继续 | |
| 489 | | user(text) | 提示未响应 | interrupted_prompt | |
| 490 | """ |
| 491 | if not messages: |
| 492 | return "empty" |
| 493 | |
| 494 | last = messages[-1] |
| 495 | |
| 496 | if last.role == "assistant": |
| 497 | return "completed" # 正常结束 |
| 498 | |
| 499 | if last.role == "tool": |
| 500 | return "interrupted_turn" # 工具执行中被中断 |
| 501 | |
| 502 | if last.role == "user": |
| 503 | # 检查是不是 tool_result |
| 504 | for block in last.blocks: |
| 505 | if block.block_type == "tool_result": |
| 506 | return "interrupted_turn" |
| 507 | return "interrupted_prompt" # 用户提问了但没收到回复 |
| 508 | |
| 509 | return "unknown" |
| 510 | |
| 511 | |
| 512 | # ============================================================ |