| 50 | |
| 51 | // Helper to determine selectedSuggestion when updating suggestions |
| 52 | function getPreservedSelection(prevSuggestions: SuggestionItem[], prevSelection: number, newSuggestions: SuggestionItem[]): number { |
| 53 | // No new suggestions |
| 54 | if (newSuggestions.length === 0) { |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | // No previous selection |
| 59 | if (prevSelection < 0) { |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | // Get the previously selected item |
| 64 | const prevSelectedItem = prevSuggestions[prevSelection]; |
| 65 | if (!prevSelectedItem) { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | // Try to find the same item in the new list by ID |
| 70 | const newIndex = newSuggestions.findIndex(item => item.id === prevSelectedItem.id); |
| 71 | |
| 72 | // Return the new index if found, otherwise default to 0 |
| 73 | return newIndex >= 0 ? newIndex : 0; |
| 74 | } |
| 75 | function buildResumeInputFromSuggestion(suggestion: SuggestionItem): string { |
| 76 | const metadata = suggestion.metadata as { |
| 77 | sessionId: string; |