picoclaw/web/frontend/src/api/sessions.ts
LiusCraft 81bbef62b1 feat(session): add per-message created_at timestamps
- Persistence layer (jsonl.go addMsg/SetHistory) normalizes CreatedAt
  when missing so the invariant is guaranteed at the storage boundary
- API layer (session.go) exposes created_at on all transcript message
  types with session.updated fallback for legacy messages
- Frontend uses per-message timestamps when available
- messagesContentEqual ignores CreatedAt for tail-matching after
  JSONL roundtrip

Fixes #2787
2026-05-22 13:15:56 +08:00

75 lines
1.7 KiB
TypeScript

import { launcherFetch } from "@/api/http"
export interface SessionSummary {
id: string
title: string
preview: string
message_count: number
created: string
updated: string
}
export interface SessionDetail {
id: string
messages: {
role: "user" | "assistant"
content: string
created_at?: string
kind?: "normal" | "thought" | "tool_calls"
model_name?: string
media?: string[]
attachments?: {
type?: "image" | "audio" | "video" | "file"
url: string
filename?: string
content_type?: string
}[]
tool_calls?: {
id?: string
type?: string
function?: {
name?: string
arguments?: string
}
extra_content?: {
tool_feedback_explanation?: string
}
}[]
}[]
summary: string
created: string
updated: string
}
export async function getSessions(
offset: number = 0,
limit: number = 20,
): Promise<SessionSummary[]> {
const params = new URLSearchParams({
offset: offset.toString(),
limit: limit.toString(),
})
const res = await launcherFetch(`/api/sessions?${params.toString()}`)
if (!res.ok) {
throw new Error(`Failed to fetch sessions: ${res.status}`)
}
return res.json()
}
export async function getSessionHistory(id: string): Promise<SessionDetail> {
const res = await launcherFetch(`/api/sessions/${encodeURIComponent(id)}`)
if (!res.ok) {
throw new Error(`Failed to fetch session ${id}: ${res.status}`)
}
return res.json()
}
export async function deleteSession(id: string): Promise<void> {
const res = await launcherFetch(`/api/sessions/${encodeURIComponent(id)}`, {
method: "DELETE",
})
if (!res.ok) {
throw new Error(`Failed to delete session ${id}: ${res.status}`)
}
}