mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
Implement session-level todo state management with inline display
This commit is contained in:
1 parent
84c947d19a
commit
a6fd795eaa
6 files changed
+143
-14
No files matched your search
@@ -1,10 +1,14 @@
|
||||
import { memo, useMemo, useState, useCallback } from 'react'
|
||||
import { memo, useMemo, useState, useCallback, useEffect } from 'react'
|
||||
import { MessagePart } from './MessagePart'
|
||||
import { MessageActionButtons } from './MessageActionButtons'
|
||||
import { UserMessageActionButtons } from './UserMessageActionButtons'
|
||||
import { EditableUserMessage, ClickableUserMessage } from './EditableUserMessage'
|
||||
import { SessionTodoDisplay } from './SessionTodoDisplay'
|
||||
import type { MessageWithParts } from '@/api/types'
|
||||
import { useSessionStatusForSession } from '@/stores/sessionStatusStore'
|
||||
import { useSessionTodos } from '@/stores/sessionTodosStore'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
import type { Todo } from '@/components/message/SessionTodoDisplay'
|
||||
|
||||
function getMessageTextContent(msg: MessageWithParts): string {
|
||||
return msg.parts
|
||||
@@ -80,6 +84,46 @@ export const MessageThread = memo(function MessageThread({
|
||||
}, [messages])
|
||||
|
||||
const isSessionBusy = !!pendingAssistantId || isSessionInRetry(sessionStatus)
|
||||
const setSessionTodos = useSessionTodos((state) => state.setTodos)
|
||||
|
||||
useEffect(() => {
|
||||
if (!messages || messages.length === 0) return
|
||||
|
||||
const latestTodoPart = messages
|
||||
.flatMap(msg => msg.parts)
|
||||
.filter((part): part is components['schemas']['ToolPart'] => part.type === 'tool' && (part.tool === 'todowrite' || part.tool === 'todoread'))
|
||||
.filter(part => part.state.status === 'completed' && 'time' in part.state)
|
||||
.sort((a, b) => {
|
||||
const aState = a.state as { time?: { end?: number } }
|
||||
const bState = b.state as { time?: { end?: number } }
|
||||
const aEndTime = aState.time?.end ?? 0
|
||||
const bEndTime = bState.time?.end ?? 0
|
||||
return bEndTime - aEndTime
|
||||
})[0]
|
||||
|
||||
if (latestTodoPart) {
|
||||
const state = latestTodoPart.state
|
||||
let todos: Todo[] = []
|
||||
|
||||
if ('metadata' in state && state.metadata?.todos && Array.isArray(state.metadata.todos)) {
|
||||
todos = state.metadata.todos as Todo[]
|
||||
} else if ('output' in state && state.output) {
|
||||
try {
|
||||
const parsed = JSON.parse(state.output)
|
||||
todos = Array.isArray(parsed)
|
||||
? parsed as Todo[]
|
||||
: parsed?.todos ? parsed.todos as Todo[]
|
||||
: []
|
||||
} catch (_) {
|
||||
console.warn('Failed to parse todo output:', _)
|
||||
}
|
||||
}
|
||||
|
||||
if (todos.length > 0) {
|
||||
setSessionTodos(sessionID, todos)
|
||||
}
|
||||
}
|
||||
}, [messages, sessionID, setSessionTodos])
|
||||
|
||||
const handleStartEditUserMessage = useCallback((userMessageId: string, assistantMessageId: string) => {
|
||||
setEditingUserMessageId(userMessageId)
|
||||
@@ -106,19 +150,22 @@ export const MessageThread = memo(function MessageThread({
|
||||
const isQueued = msg.info.role === 'user' && pendingAssistantId && msg.info.id > pendingAssistantId
|
||||
const isLastUserMessage = msg.info.role === 'user' && msg.info.id === lastUserMessageId
|
||||
const messageTextContent = getMessageTextContent(msg)
|
||||
|
||||
|
||||
const nextAssistantMessage = messages.slice(index + 1).find(m => m.info.role === 'assistant')
|
||||
const isUserBeforeAssistant = msg.info.role === 'user' && nextAssistantMessage
|
||||
const canEditUserMessage = isUserBeforeAssistant && nextAssistantMessage?.info.id === lastAssistantId && !isSessionBusy
|
||||
const canUndoUserMessage = isLastUserMessage && nextAssistantMessage && !isSessionBusy && onUndoMessage
|
||||
|
||||
const isEditingThisMessage = editingUserMessageId === msg.info.id
|
||||
|
||||
|
||||
return (
|
||||
<div
|
||||
key={msg.info.id}
|
||||
className="flex flex-col group"
|
||||
>
|
||||
{msg.info.role === 'assistant' && streaming && (
|
||||
<SessionTodoDisplay sessionID={sessionID} />
|
||||
)}
|
||||
<div
|
||||
className={`w-full rounded-lg p-1.5 ${
|
||||
msg.info.role === 'user'
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { TodoListDisplay } from './TodoListDisplay'
|
||||
import { useSessionTodosForSession } from '@/stores/sessionTodosStore'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
|
||||
export type Todo = components['schemas']['Todo']
|
||||
|
||||
interface SessionTodoDisplayProps {
|
||||
sessionID: string | undefined
|
||||
}
|
||||
|
||||
export function SessionTodoDisplay({ sessionID }: SessionTodoDisplayProps) {
|
||||
const todos = useSessionTodosForSession(sessionID)
|
||||
|
||||
if (!sessionID || !todos || todos.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mb-2">
|
||||
<TodoListDisplay
|
||||
todos={todos}
|
||||
title="Task List"
|
||||
showCompleted={true}
|
||||
scrollCurrentOnly={true}
|
||||
isLoading={false}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -123,7 +123,7 @@ export function TodoListDisplay({
|
||||
{/* Scrollable content area - optimized height for visibility */}
|
||||
<div
|
||||
ref={scrollRef}
|
||||
className={`max-h-[160px] sm:max-h-[280px] overflow-y-auto ${scrollCurrentOnly ? 'scroll-smooth' : ''} p-1.5 sm:p-2`}
|
||||
className={`max-h-[80px] sm:max-h-[160px] overflow-y-auto ${scrollCurrentOnly ? 'scroll-smooth' : ''} p-1.5 sm:p-2`}
|
||||
>
|
||||
{renderGroup('In Progress', inProgress)}
|
||||
{renderGroup('Pending', pending)}
|
||||
|
||||
@@ -205,13 +205,8 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
|
||||
if (part.state.status === 'completed') {
|
||||
return (
|
||||
<div className="my-2">
|
||||
<TodoListDisplay
|
||||
todos={todoData || []}
|
||||
title="Task List"
|
||||
showCompleted={true}
|
||||
scrollCurrentOnly={true}
|
||||
/>
|
||||
<div className="my-2 text-xs text-muted-foreground">
|
||||
Task list updated
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { permissionEvents } from './usePermissionRequests'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import { settingsApi } from '@/api/settings'
|
||||
import { useSessionStatus } from '@/stores/sessionStatusStore'
|
||||
import { useSessionTodos } from '@/stores/sessionTodosStore'
|
||||
|
||||
const MAX_RECONNECT_DELAY = 30000
|
||||
const INITIAL_RECONNECT_DELAY = 1000
|
||||
@@ -52,6 +53,7 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [isReconnecting, setIsReconnecting] = useState(false)
|
||||
const setSessionStatus = useSessionStatus((state) => state.setStatus)
|
||||
const setSessionTodos = useSessionTodos((state) => state.setTodos)
|
||||
|
||||
const scheduleReconnect = useCallback((connectFn: () => void) => {
|
||||
if (!mountedRef.current) return
|
||||
@@ -326,9 +328,11 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
|
||||
break
|
||||
|
||||
case 'todo.updated':
|
||||
if ('sessionID' in event.properties) {
|
||||
if ('sessionID' in event.properties && 'todos' in event.properties) {
|
||||
const { sessionID, todos } = event.properties
|
||||
setSessionTodos(sessionID, todos)
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['opencode', 'todos', opcodeUrl, event.properties.sessionID, directory]
|
||||
queryKey: ['opencode', 'todos', opcodeUrl, sessionID, directory]
|
||||
})
|
||||
}
|
||||
break
|
||||
@@ -450,7 +454,7 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string)
|
||||
setIsConnected(false)
|
||||
}
|
||||
}
|
||||
}, [client, queryClient, opcodeUrl, directory, scheduleReconnect, resetReconnectDelay, setSessionStatus])
|
||||
}, [client, queryClient, opcodeUrl, directory, scheduleReconnect, resetReconnectDelay, setSessionStatus, setSessionTodos])
|
||||
|
||||
return { isConnected, error, isReconnecting }
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { create } from 'zustand'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
|
||||
type Todo = components['schemas']['Todo']
|
||||
|
||||
interface SessionTodosStore {
|
||||
todos: Map<string, Todo[]>
|
||||
setTodos: (sessionID: string, todos: Todo[]) => void
|
||||
getTodos: (sessionID: string) => Todo[]
|
||||
clearSession: (sessionID: string) => void
|
||||
hasSessionTodos: (sessionID: string) => boolean
|
||||
}
|
||||
|
||||
export const useSessionTodos = create<SessionTodosStore>((set, get) => ({
|
||||
todos: new Map(),
|
||||
|
||||
setTodos: (sessionID: string, todos: Todo[]) => {
|
||||
set((state) => {
|
||||
const newMap = new Map(state.todos)
|
||||
newMap.set(sessionID, todos)
|
||||
return { todos: newMap }
|
||||
})
|
||||
},
|
||||
|
||||
getTodos: (sessionID: string) => {
|
||||
return get().todos.get(sessionID) || []
|
||||
},
|
||||
|
||||
clearSession: (sessionID: string) => {
|
||||
set((state) => {
|
||||
const newMap = new Map(state.todos)
|
||||
newMap.delete(sessionID)
|
||||
return { todos: newMap }
|
||||
})
|
||||
},
|
||||
|
||||
hasSessionTodos: (sessionID: string) => {
|
||||
const sessionTodos = get().todos.get(sessionID)
|
||||
return !!sessionTodos && sessionTodos.length > 0
|
||||
},
|
||||
}))
|
||||
|
||||
export const useSessionTodosForSession = (sessionID: string | undefined): Todo[] => {
|
||||
const todos = useSessionTodos((state) =>
|
||||
sessionID ? state.todos.get(sessionID) : undefined
|
||||
)
|
||||
return todos || []
|
||||
}
|
||||
|
||||
export const useHasSessionTodos = (sessionID: string | undefined): boolean => {
|
||||
return useSessionTodos((state) =>
|
||||
sessionID ? (state.todos.get(sessionID)?.length ?? 0) > 0 : false
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user