( eventsUrl: string, headers: Record<string, string>, )
| 79 | } |
| 80 | |
| 81 | export async function fetchHistoricalSessionEvents( |
| 82 | eventsUrl: string, |
| 83 | headers: Record<string, string>, |
| 84 | ): Promise<ReplayableHistoricalSessionEvent[]> { |
| 85 | const replayable: ReplayableHistoricalSessionEvent[] = [] |
| 86 | let cursor: string | null = null |
| 87 | const maxPages = 50 |
| 88 | |
| 89 | for (let page = 0; page < maxPages; page++) { |
| 90 | const response = await axios.get<HistoricalSessionEventsResponse>( |
| 91 | eventsUrl, |
| 92 | { |
| 93 | headers, |
| 94 | params: cursor ? { after_id: cursor } : undefined, |
| 95 | timeout: 20_000, |
| 96 | validateStatus: status => status < 500, |
| 97 | }, |
| 98 | ) |
| 99 | |
| 100 | if (response.status === 404) { |
| 101 | return [] |
| 102 | } |
| 103 | if (response.status !== 200) { |
| 104 | throw new Error( |
| 105 | `Failed to fetch historical session events: ${response.status}`, |
| 106 | ) |
| 107 | } |
| 108 | |
| 109 | const body = response.data |
| 110 | if (!body || !Array.isArray(body.data)) { |
| 111 | throw new Error('Invalid historical session events response') |
| 112 | } |
| 113 | |
| 114 | for (const event of body.data) { |
| 115 | if (isReplayableHistoricalSessionEvent(event)) { |
| 116 | replayable.push(event) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | cursor = body.last_id ?? null |
| 121 | if (!body.has_more || !cursor) { |
| 122 | break |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return replayable |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Bidirectional streaming for SDK mode with session tracking |
no test coverage detected