| |
| |
| |
| |
| |
|
|
| import type { Content } from '@google/genai'; |
| import type { ConcreteNode } from './types.js'; |
| import { debugLogger } from '../../utils/debugLogger.js'; |
| import type { NodeIdService } from './nodeIdService.js'; |
| import type { HistoryTurn } from '../../core/agentChatHistory.js'; |
|
|
| |
| |
| |
| |
| |
| export function fromGraph( |
| nodes: readonly ConcreteNode[], |
| idService?: NodeIdService, |
| ): HistoryTurn[] { |
| debugLogger.log( |
| `[fromGraph] Reconstructing history from ${nodes.length} nodes`, |
| ); |
|
|
| const history: HistoryTurn[] = []; |
| let currentTurn: { id: string; content: Content } | null = null; |
|
|
| for (const node of nodes) { |
| const turnId = node.turnId || 'orphan'; |
| const durableId = turnId.startsWith('turn_') ? turnId.slice(5) : turnId; |
|
|
| |
| |
| if (idService) { |
| idService.set(node.payload, node.id); |
| } |
|
|
| |
| |
| |
| |
| if ( |
| !currentTurn || |
| currentTurn.content.role !== node.role || |
| currentTurn.id !== durableId |
| ) { |
| currentTurn = { |
| id: durableId, |
| content: { |
| role: node.role, |
| parts: [node.payload], |
| }, |
| }; |
| history.push(currentTurn); |
| } else { |
| currentTurn.content.parts = [ |
| ...(currentTurn.content.parts || []), |
| node.payload, |
| ]; |
| } |
| } |
|
|
| debugLogger.log(`[fromGraph] Reconstructed ${history.length} turns`); |
| return history; |
| } |
|
|