Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/opencode/src/cli/cmd/tui/context/sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,57 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
)
fullSyncedSessions.add(sessionID)
},
async loadConversationHistory(sessionID: string) {
const messages = store.message[sessionID]
if (!messages || messages.length === 0) return

const earliest = messages[0]
const result = await sdk.client.session.messages({
sessionID,
ts_before: earliest.time.created,
breakpoint: true,
})

if (!result.data || result.data.length === 0) {
return 0
}

setStore(
produce((draft) => {
const existing = draft.message[sessionID] ?? []
draft.message[sessionID] = [...result.data!.map((x) => x.info), ...existing]
for (const message of result.data!) {
draft.part[message.info.id] = message.parts
}
}),
)
return result.data.length
},
async loadFullSessionHistory(sessionID: string) {
const messages = store.message[sessionID]
if (!messages || messages.length === 0) return

const earliest = messages[0]
const result = await sdk.client.session.messages({
sessionID,
ts_before: earliest.time.created,
})

if (!result.data || result.data.length === 0) {
return 0
}

setStore(
produce((draft) => {
const existing = draft.message[sessionID] ?? []
draft.message[sessionID] = [...result.data!.map((x) => x.info), ...existing]
for (const message of result.data!) {
draft.part[message.info.id] = message.parts
}
}),
)
return result.data.length
},
},
bootstrap,
}
Expand Down
87 changes: 86 additions & 1 deletion packages/opencode/src/cli/cmd/tui/routes/session/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ export function Session() {
.toSorted((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
})
const messages = createMemo(() => sync.data.message[route.sessionID] ?? [])

const messagesDisplay = createMemo(() => {
const msgs = messages()
if (msgs.length >= 100) {
const synthetic = {
id: "__load_more__",
sessionID: route.sessionID,
role: "system" as const,
time: { created: 0, updated: 0, completed: null },
_synthetic: true,
} as any
return [synthetic, ...msgs]
}
return msgs
})

const permissions = createMemo(() => {
if (session()?.parentID) return []
return children().flatMap((x) => sync.data.permission[x.id] ?? [])
Expand Down Expand Up @@ -921,9 +937,78 @@ export function Session() {
flexGrow={1}
scrollAcceleration={scrollAcceleration()}
>
<For each={messages()}>
<For each={messagesDisplay()}>
{(message, index) => (
<Switch>
<Match when={message.id === "__load_more__"}>
{(function () {
const [hoveredButton, setHoveredButton] = createSignal<"conversation" | "full" | null>(null)
const [loading, setLoading] = createSignal(false)

const handleLoadConversation = async () => {
if (loading()) return
setLoading(true)
try {
const count = await sync.session.loadConversationHistory(route.sessionID)
if (count === 0) {
toast.show({ message: "No more messages loaded", variant: "info" })
} else {
toast.show({ message: `History loaded (${count} messages)`, variant: "success" })
}
} finally {
setLoading(false)
}
}

const handleLoadFull = async () => {
if (loading()) return
setLoading(true)
try {
const count = await sync.session.loadFullSessionHistory(route.sessionID)
if (count === 0) {
toast.show({ message: "No more messages loaded", variant: "info" })
} else {
toast.show({ message: `History loaded (${count} messages)`, variant: "success" })
}
} finally {
setLoading(false)
}
}

return (
<box
paddingLeft={2}
paddingRight={2}
paddingTop={1}
paddingBottom={1}
marginBottom={1}
flexDirection="row"
backgroundColor={theme.backgroundPanel}
>
<text fg={theme.textMuted}>Load more messages: </text>
<box
onMouseOver={() => setHoveredButton("conversation")}
onMouseOut={() => setHoveredButton(null)}
onMouseUp={handleLoadConversation}
>
<text fg={hoveredButton() === "conversation" ? theme.accent : theme.text}>
load conversation history
</text>
</box>
<text fg={theme.textMuted}> or </text>
<box
onMouseOver={() => setHoveredButton("full")}
onMouseOut={() => setHoveredButton(null)}
onMouseUp={handleLoadFull}
>
<text fg={hoveredButton() === "full" ? theme.accent : theme.text}>
load full session history
</text>
</box>
</box>
)
})()}
</Match>
<Match when={message.id === revert()?.messageID}>
{(function () {
const command = useCommandDialog()
Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/src/server/routes/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,17 @@ export const SessionRoutes = lazy(() =>
"query",
z.object({
limit: z.coerce.number().optional(),
ts_before: z.coerce.number().optional(),
breakpoint: z.coerce.boolean().optional(),
}),
),
async (c) => {
const query = c.req.valid("query")
const messages = await Session.messages({
sessionID: c.req.valid("param").sessionID,
limit: query.limit,
ts_before: query.ts_before,
breakpoint: query.breakpoint,
})
return c.json(messages)
},
Expand Down
4 changes: 4 additions & 0 deletions packages/opencode/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,16 @@ export namespace Session {
z.object({
sessionID: Identifier.schema("session"),
limit: z.number().optional(),
ts_before: z.number().optional(),
breakpoint: z.boolean().optional(),
}),
async (input) => {
const result = [] as MessageV2.WithParts[]
for await (const msg of MessageV2.stream(input.sessionID)) {
if (input.ts_before && msg.info.time.created >= input.ts_before) continue
if (input.limit && result.length >= input.limit) break
result.push(msg)
if (input.ts_before && input.breakpoint && msg.parts.some((p) => p.type === "compaction")) break
}
result.reverse()
return result
Expand Down
Loading