({
messages,
status,
}: {
messages: SBChatMessage[];
status: ChatStatus;
})
| 336 | } |
| 337 | |
| 338 | export const getTurnProgressState = ({ |
| 339 | messages, |
| 340 | status, |
| 341 | }: { |
| 342 | messages: SBChatMessage[]; |
| 343 | status: ChatStatus; |
| 344 | }) => { |
| 345 | const isNetworkActive = status === 'submitted' || status === 'streaming'; |
| 346 | const latestMessage = messages.at(-1); |
| 347 | const latestAssistantMessage = latestMessage?.role === 'assistant' ? latestMessage : undefined; |
| 348 | const latestStepToolParts = getLastStepParts(latestAssistantMessage?.parts ?? []) |
| 349 | .filter(isSBChatToolPart); |
| 350 | |
| 351 | const hasPendingToolApproval = latestStepToolParts.some( |
| 352 | (part) => part.state === 'approval-requested' |
| 353 | ); |
| 354 | const hasApprovalContinuationReady = |
| 355 | latestStepToolParts.some((part) => part.state === 'approval-responded') && |
| 356 | latestStepToolParts.every((part) => |
| 357 | part.state === 'output-available' || |
| 358 | part.state === 'output-error' || |
| 359 | part.state === 'approval-responded' |
| 360 | ); |
| 361 | |
| 362 | const isReady = status === 'ready'; |
| 363 | const isTurnInProgress = |
| 364 | isNetworkActive || |
| 365 | (isReady && (hasPendingToolApproval || hasApprovalContinuationReady)); |
| 366 | const isAwaitingToolApproval = isReady && hasPendingToolApproval; |
| 367 | const shouldGuardNavigation = isNetworkActive || (isReady && hasApprovalContinuationReady); |
| 368 | |
| 369 | return { |
| 370 | isNetworkActive, |
| 371 | hasPendingToolApproval, |
| 372 | hasApprovalContinuationReady, |
| 373 | isAwaitingToolApproval, |
| 374 | isTurnInProgress, |
| 375 | shouldGuardNavigation, |
| 376 | }; |
| 377 | } |
| 378 | |
| 379 | // LLMs like to not follow instructions... this takes care of some common mistakes they tend to make. |
| 380 | export const repairReferences = (text: string): string => { |
no test coverage detected