(sessionId, workspaceDir)
| 1866 | } |
| 1867 | |
| 1868 | async function readSessionState(sessionId, workspaceDir) { |
| 1869 | const path = join(sessionDir(workspaceDir), `${sessionId}.json`) |
| 1870 | const payload = JSON.parse(await readFile(path, 'utf8')) |
| 1871 | const transcript = [] |
| 1872 | const toolCalls = [] |
| 1873 | const timeline = [] |
| 1874 | const toolMap = new Map() |
| 1875 | const messages = Array.isArray(payload.messages) ? payload.messages : [] |
| 1876 | const base = Date.now() - messages.length * 5000 |
| 1877 | let offset = 0 |
| 1878 | |
| 1879 | const nextTimestamp = () => new Date(base + offset++ * 1000).toISOString() |
| 1880 | |
| 1881 | for (const message of messages) { |
| 1882 | const role = String(message?.role || 'assistant') |
| 1883 | const blocks = Array.isArray(message?.blocks) ? message.blocks : [] |
| 1884 | for (const block of blocks) { |
| 1885 | const timestamp = nextTimestamp() |
| 1886 | if (block?.type === 'text') { |
| 1887 | const content = String(block.text || '').trim() |
| 1888 | if (!content) { |
| 1889 | continue |
| 1890 | } |
| 1891 | transcript.push({ |
| 1892 | id: createId('session-text'), |
| 1893 | role: role === 'user' ? 'user' : 'assistant', |
| 1894 | entryType: role === 'user' ? 'user' : 'assistant', |
| 1895 | title: role === 'user' ? 'You' : 'Assistant', |
| 1896 | content, |
| 1897 | timestamp, |
| 1898 | streaming: false, |
| 1899 | isError: false, |
| 1900 | }) |
| 1901 | continue |
| 1902 | } |
| 1903 | |
| 1904 | if (block?.type === 'tool_use') { |
| 1905 | const inputPreview = previewValue(block.input, 500) |
| 1906 | const entry = { |
| 1907 | id: block.id || createId('tool'), |
| 1908 | name: block.name || 'tool', |
| 1909 | title: block.name || 'tool', |
| 1910 | status: 'completed', |
| 1911 | inputPreview, |
| 1912 | resultPreview: '', |
| 1913 | startedAt: timestamp, |
| 1914 | completedAt: timestamp, |
| 1915 | diff: null, |
| 1916 | } |
| 1917 | toolMap.set(entry.id, entry) |
| 1918 | transcript.push({ |
| 1919 | id: `tool-use-${entry.id}`, |
| 1920 | role: 'tool', |
| 1921 | entryType: 'tool_use', |
| 1922 | title: entry.title, |
| 1923 | content: inputPreview || 'Tool call', |
| 1924 | timestamp, |
| 1925 | streaming: false, |
no test coverage detected