|
| 1 | +import type { Event, PermissionRequest, QuestionRequest } from "@opencode-ai/sdk/v2" |
| 2 | +import { bootstrapSessionData, createSessionData, reduceSessionData, type SessionData } from "./session-data" |
| 3 | +import { messagePrompt, type SessionMessages } from "./session.shared" |
| 4 | +import type { FooterPatch, StreamCommit } from "./types" |
| 5 | + |
| 6 | +type ReplayInput = { |
| 7 | + messages: SessionMessages |
| 8 | + permissions: PermissionRequest[] |
| 9 | + questions: QuestionRequest[] |
| 10 | + thinking: boolean |
| 11 | + limits: Record<string, number> |
| 12 | +} |
| 13 | + |
| 14 | +export type SessionReplay = { |
| 15 | + data: SessionData |
| 16 | + commits: StreamCommit[] |
| 17 | + patch?: FooterPatch |
| 18 | +} |
| 19 | + |
| 20 | +type ReplayMessage = { |
| 21 | + commits: StreamCommit[] |
| 22 | + patch?: FooterPatch |
| 23 | +} |
| 24 | + |
| 25 | +function apply(data: SessionData, event: Event, sessionID: string, thinking: boolean, limits: Record<string, number>) { |
| 26 | + return reduceSessionData({ |
| 27 | + data, |
| 28 | + event, |
| 29 | + sessionID, |
| 30 | + thinking, |
| 31 | + limits, |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +function mergePatch(left: FooterPatch | undefined, right: FooterPatch | undefined) { |
| 36 | + if (!left) { |
| 37 | + return right |
| 38 | + } |
| 39 | + |
| 40 | + if (!right) { |
| 41 | + return left |
| 42 | + } |
| 43 | + |
| 44 | + return { |
| 45 | + ...left, |
| 46 | + ...right, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function active(data: SessionData) { |
| 51 | + return data.part.size > 0 || data.tools.size > 0 |
| 52 | +} |
| 53 | + |
| 54 | +function replayPatch(data: SessionData, patch: FooterPatch | undefined) { |
| 55 | + if (active(data)) { |
| 56 | + if (!patch) { |
| 57 | + return { |
| 58 | + phase: "running", |
| 59 | + } satisfies FooterPatch |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + ...patch, |
| 64 | + phase: "running", |
| 65 | + } satisfies FooterPatch |
| 66 | + } |
| 67 | + |
| 68 | + if (data.permissions.length > 0 || data.questions.length > 0) { |
| 69 | + if (!patch) { |
| 70 | + return { |
| 71 | + phase: "idle", |
| 72 | + } satisfies FooterPatch |
| 73 | + } |
| 74 | + |
| 75 | + return { |
| 76 | + ...patch, |
| 77 | + phase: "idle", |
| 78 | + } satisfies FooterPatch |
| 79 | + } |
| 80 | + |
| 81 | + if (!patch) { |
| 82 | + return undefined |
| 83 | + } |
| 84 | + |
| 85 | + return { |
| 86 | + ...patch, |
| 87 | + phase: "idle", |
| 88 | + status: "", |
| 89 | + } satisfies FooterPatch |
| 90 | +} |
| 91 | + |
| 92 | +function replayMessage( |
| 93 | + data: SessionData, |
| 94 | + message: SessionMessages[number], |
| 95 | + thinking: boolean, |
| 96 | + limits: Record<string, number>, |
| 97 | +): ReplayMessage { |
| 98 | + if (message.info.role === "user") { |
| 99 | + const prompt = messagePrompt(message) |
| 100 | + if (!prompt.text.trim()) { |
| 101 | + return { |
| 102 | + commits: [], |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return { |
| 107 | + commits: [ |
| 108 | + { |
| 109 | + kind: "user", |
| 110 | + text: prompt.text, |
| 111 | + phase: "start", |
| 112 | + source: "system", |
| 113 | + messageID: message.info.id, |
| 114 | + }, |
| 115 | + ], |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + const commits: StreamCommit[] = [] |
| 120 | + let patch: FooterPatch | undefined |
| 121 | + |
| 122 | + const info = apply( |
| 123 | + data, |
| 124 | + { |
| 125 | + id: `bootstrap:message:${message.info.id}`, |
| 126 | + type: "message.updated", |
| 127 | + properties: { |
| 128 | + sessionID: message.info.sessionID, |
| 129 | + info: message.info, |
| 130 | + }, |
| 131 | + }, |
| 132 | + message.info.sessionID, |
| 133 | + thinking, |
| 134 | + limits, |
| 135 | + ) |
| 136 | + commits.push(...info.commits) |
| 137 | + patch = mergePatch(patch, info.footer?.patch) |
| 138 | + |
| 139 | + for (const part of message.parts) { |
| 140 | + const next = apply( |
| 141 | + data, |
| 142 | + { |
| 143 | + id: `bootstrap:part:${part.id}`, |
| 144 | + type: "message.part.updated", |
| 145 | + properties: { |
| 146 | + sessionID: part.sessionID, |
| 147 | + part, |
| 148 | + time: 0, |
| 149 | + }, |
| 150 | + }, |
| 151 | + message.info.sessionID, |
| 152 | + thinking, |
| 153 | + limits, |
| 154 | + ) |
| 155 | + patch = mergePatch(patch, next.footer?.patch) |
| 156 | + commits.push(...next.commits) |
| 157 | + } |
| 158 | + |
| 159 | + return { |
| 160 | + commits, |
| 161 | + patch, |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +export function replaySession(input: ReplayInput): SessionReplay { |
| 166 | + const data = createSessionData() |
| 167 | + const commits: StreamCommit[] = [] |
| 168 | + let patch: FooterPatch | undefined |
| 169 | + |
| 170 | + bootstrapSessionData({ |
| 171 | + data, |
| 172 | + messages: input.messages, |
| 173 | + permissions: input.permissions, |
| 174 | + questions: input.questions, |
| 175 | + }) |
| 176 | + |
| 177 | + for (const message of input.messages) { |
| 178 | + const next = replayMessage(data, message, input.thinking, input.limits) |
| 179 | + commits.push(...next.commits) |
| 180 | + patch = mergePatch(patch, next.patch) |
| 181 | + } |
| 182 | + |
| 183 | + return { |
| 184 | + data, |
| 185 | + commits, |
| 186 | + patch: replayPatch(data, patch), |
| 187 | + } |
| 188 | +} |
0 commit comments