(
entries: SessionEntry[],
leafId?: string | null,
options: { deferThinking?: boolean; deferToolResultImages?: boolean } = {},
)
| 192 | } |
| 193 | |
| 194 | export function buildSessionContext( |
| 195 | entries: SessionEntry[], |
| 196 | leafId?: string | null, |
| 197 | options: { deferThinking?: boolean; deferToolResultImages?: boolean } = {}, |
| 198 | ): SessionContext { |
| 199 | const byId = new Map<string, SessionEntry>(); |
| 200 | for (const e of entries) byId.set(e.id, e); |
| 201 | |
| 202 | const piEntries = entries as unknown as PiSessionEntry[]; |
| 203 | const piCtx = piBuildSessionContext(piEntries, leafId, byId as unknown as Map<string, PiSessionEntry>); |
| 204 | |
| 205 | const contextEntries = piBuildContextEntries( |
| 206 | piEntries, |
| 207 | leafId, |
| 208 | byId as unknown as Map<string, PiSessionEntry>, |
| 209 | ); |
| 210 | |
| 211 | // Convert the SDK-selected context entries and their IDs together. This keeps |
| 212 | // fork/navigation targets aligned while preserving pi's compaction ordering. |
| 213 | const messages: AgentMessage[] = []; |
| 214 | const entryIds: string[] = []; |
| 215 | for (const entry of contextEntries) { |
| 216 | const localEntry = entry as unknown as SessionEntry; |
| 217 | const m = entryToUiMessage(localEntry, options); |
| 218 | if (m) { |
| 219 | messages.push(m); |
| 220 | entryIds.push(localEntry.id); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return { |
| 225 | messages, |
| 226 | entryIds, |
| 227 | thinkingLevel: piCtx.thinkingLevel, |
| 228 | model: piCtx.model, |
| 229 | }; |
| 230 | } |
| 231 | |
| 232 | function parseEntryTimestamp(timestamp: string): number | undefined { |
| 233 | const parsed = Date.parse(timestamp); |
no test coverage detected