( messages: SessionMessages, commits: StreamCommit[], rows: LocalReplayRow[], )
| 261 | } |
| 262 | |
| 263 | export function replayLocalRows( |
| 264 | messages: SessionMessages, |
| 265 | commits: StreamCommit[], |
| 266 | rows: LocalReplayRow[], |
| 267 | ): StreamCommit[] { |
| 268 | const persisted = new Set(messages.map((message) => message.info.id)) |
| 269 | return rows.reduce((out, local) => { |
| 270 | const row = local.commit |
| 271 | if (row.kind === "user" && row.messageID && persisted.has(row.messageID)) { |
| 272 | return out |
| 273 | } |
| 274 | |
| 275 | if (!row.messageID) { |
| 276 | return [...out, row] |
| 277 | } |
| 278 | |
| 279 | const exact = local.after |
| 280 | ? out.findIndex( |
| 281 | (commit) => |
| 282 | commit.kind === local.after?.kind && |
| 283 | commit.text === local.after.text && |
| 284 | commit.phase === local.after.phase && |
| 285 | commit.toolState === local.after.toolState && |
| 286 | (local.after.partID ? commit.partID === local.after.partID : commit.messageID === local.after.messageID), |
| 287 | ) |
| 288 | : -1 |
| 289 | const anchored = |
| 290 | exact !== -1 |
| 291 | ? exact |
| 292 | : local.after |
| 293 | ? out.findLastIndex((commit) => |
| 294 | local.after?.partID |
| 295 | ? commit.partID === local.after.partID |
| 296 | : commit.kind === local.after?.kind && commit.messageID === local.after.messageID, |
| 297 | ) |
| 298 | : -1 |
| 299 | if (anchored !== -1) { |
| 300 | const commit = out[anchored] |
| 301 | const visible = local.after?.visible |
| 302 | if (commit && visible && commit.text.startsWith(visible) && commit.text.length > visible.length) { |
| 303 | return [ |
| 304 | ...out.slice(0, anchored), |
| 305 | { ...commit, text: visible }, |
| 306 | row, |
| 307 | { ...commit, text: commit.text.slice(visible.length) }, |
| 308 | ...out.slice(anchored + 1), |
| 309 | ] |
| 310 | } |
| 311 | |
| 312 | return [...out.slice(0, anchored + 1), row, ...out.slice(anchored + 1)] |
| 313 | } |
| 314 | |
| 315 | const after = out.findIndex((commit) => commit.kind === "user" && commit.messageID === row.messageID) |
| 316 | if (after !== -1) { |
| 317 | return [...out.slice(0, after + 1), row, ...out.slice(after + 1)] |
| 318 | } |
| 319 | |
| 320 | const before = out.findIndex((commit) => commit.messageID && row.messageID! < commit.messageID) |
no outgoing calls
no test coverage detected