342
343
async getNextTurnEventId(turnId: string): Promise<string | undefined> {
345
>
// `turns.id` is the canonical turn key — either a live `request_xxx`
346
>
// dispatched by the client or, for sessions restored from disk, the
347
>
// SDK envelope id surfaced by `mapSessionEvents`. The `event_id`
348
>
// fallback covers the case where the caller asks about a turn that
349
>
// was set up live (id=`request_xxx`) but is now being referenced
350
>
// via the SDK event id, or vice versa.
351
>
const row = await dbGet(
352
>
db,
353
>
`SELECT event_id FROM turns
354
>
WHERE rowid > (
355
>
SELECT rowid FROM turns WHERE id = ?1 OR event_id = ?1 LIMIT 1
356
>
)
357
>
ORDER BY rowid LIMIT 1`,
358
>
[turnId],
359
>
);
360
>
return row?.event_id as string | undefined ?? undefined;
361
>
}
362
363
async getFirstTurnEventId(): Promise<string | undefined> {