MCPcopy Create free account
hub / github.com/Work-Fisher/code-claw / readSessionState

Function readSessionState

tools/claw-launcher-ui/server.mjs:1868–2005  ·  view source on GitHub ↗
(sessionId, workspaceDir)

Source from the content-addressed store, hash-verified

1866}
1867
1868async 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,

Callers 1

readSessionSnapshotFunction · 0.85

Calls 12

sessionDirFunction · 0.85
readFileFunction · 0.85
nextTimestampFunction · 0.85
createIdFunction · 0.85
previewValueFunction · 0.85
summarizeToolResultFunction · 0.85
flattenBlockTextFunction · 0.85
summarizeDiffBundleFunction · 0.85
safeNowFunction · 0.85
getMethod · 0.80
parseMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected