1210
}
1211
if (options?.fork) {
1213
>
const peerState = this._stateManager.getChatState(sourceKey);
1214
>
const sourceState = peerState ?? this._stateManager.getDefaultChatState(sourceKey);
1215
>
// Canonical chat URI the source's local turns are keyed by: when the
1216
>
// source was found as a peer chat it is `sourceKey`; otherwise it was
1217
>
// addressed by session URI and its default chat URI is canonical.
1218
>
const sourceChatUri = peerState ? sourceKey : buildDefaultChatUri(sourceKey);
1219
>
const sourceTurns = sourceState?.turns ?? [];
1220
>
const forkIndex = sourceTurns.findIndex(t => t.id === options.fork!.turnId);
1221
>
if (forkIndex < 0) {
1222
// The fork point is unknown, so a fork is indistinguishable from a
1223
// fresh chat. Drop the fork to avoid the provider inheriting the
1224
// whole backend chat while the UI is seeded with no turns.
1225
createOptions = { ...options, fork: undefined };
1227
const slice = sourceTurns.slice(0, forkIndex + 1);
1228
const turnIdMapping = new Map<string, string>();