mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add todo tool rendering components for task management
This commit is contained in:
1 parent
82d3a109ac
commit
fb430359ce
3 files changed
+291
-4
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import type { components } from '@/api/opencode-types'
|
||||
import { CheckCircle2, Circle } from 'lucide-react'
|
||||
|
||||
export type Todo = components['schemas']['Todo']
|
||||
|
||||
interface TodoItemProps {
|
||||
todo: Todo
|
||||
compact?: boolean
|
||||
}
|
||||
|
||||
export function TodoItem({ todo, compact = false }: TodoItemProps) {
|
||||
const isCompleted = todo.status === 'completed'
|
||||
const isInProgress = todo.status === 'in_progress'
|
||||
|
||||
const priorityColor = todo.priority === 'high'
|
||||
? 'bg-red-500'
|
||||
: todo.priority === 'medium'
|
||||
? 'bg-yellow-500'
|
||||
: 'bg-blue-400'
|
||||
|
||||
return (
|
||||
<div className={`flex items-center gap-1.5 ${compact ? 'py-0.5 text-xs' : 'py-1 text-sm'} pl-0.5`}>
|
||||
{isCompleted ? (
|
||||
<CheckCircle2 className={`${compact ? 'w-3 h-3' : 'w-3.5 h-3.5'} text-green-600 dark:text-green-400 flex-shrink-0`} />
|
||||
) : isInProgress ? (
|
||||
<span className={`${compact ? 'w-3 h-3' : 'w-3.5 h-3.5'} rounded-full border-2 border-yellow-500 border-t-transparent animate-spin flex-shrink-0`} />
|
||||
) : (
|
||||
<Circle className={`${compact ? 'w-3 h-3' : 'w-3.5 h-3.5'} text-muted-foreground flex-shrink-0`} />
|
||||
)}
|
||||
{todo.priority && (
|
||||
<span className={`w-1.5 h-1.5 rounded-full ${priorityColor} flex-shrink-0`} />
|
||||
)}
|
||||
<span
|
||||
className={`flex-1 min-w-0 truncate ${
|
||||
isInProgress
|
||||
? 'text-yellow-700 dark:text-yellow-300 font-medium'
|
||||
: isCompleted
|
||||
? 'text-muted-foreground line-through opacity-60'
|
||||
: 'text-foreground'
|
||||
}`}
|
||||
>
|
||||
{todo.content}
|
||||
</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import type { components } from '@/api/opencode-types'
|
||||
import { TodoItem } from './TodoItem'
|
||||
import { useEffect, useRef } from 'react'
|
||||
|
||||
export type Todo = components['schemas']['Todo']
|
||||
|
||||
interface TodoListDisplayProps {
|
||||
todos: Todo[]
|
||||
title?: string
|
||||
showCompleted?: boolean
|
||||
scrollCurrentOnly?: boolean
|
||||
isLoading?: boolean
|
||||
}
|
||||
|
||||
export function TodoListDisplay({
|
||||
todos,
|
||||
title = 'Todo List',
|
||||
showCompleted = true,
|
||||
scrollCurrentOnly = false,
|
||||
isLoading = false,
|
||||
}: TodoListDisplayProps) {
|
||||
const scrollRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
// Filter completed tasks if not showing them
|
||||
const filteredTodos = showCompleted
|
||||
? todos
|
||||
: todos.filter((t) => t.status !== 'completed')
|
||||
|
||||
// Group by status for better organization
|
||||
const inProgress = filteredTodos.filter((t) => t.status === 'in_progress')
|
||||
const pending = filteredTodos.filter((t) => t.status === 'pending')
|
||||
const completed = showCompleted
|
||||
? filteredTodos.filter((t) => t.status === 'completed')
|
||||
: []
|
||||
|
||||
const hasMultipleGroups =
|
||||
(inProgress.length > 0 ? 1 : 0) +
|
||||
(pending.length > 0 ? 1 : 0) +
|
||||
(completed.length > 0 ? 1 : 0) >
|
||||
1
|
||||
|
||||
// Auto-scroll to current task (in_progress or first pending) within container only
|
||||
useEffect(() => {
|
||||
if (scrollRef.current && scrollCurrentOnly && filteredTodos.length > 0) {
|
||||
const container = scrollRef.current
|
||||
const currentElement = container.querySelector('[data-status="in_progress"], [data-status="pending"]') as HTMLElement | null
|
||||
if (currentElement) {
|
||||
const containerTop = container.scrollTop
|
||||
const containerHeight = container.clientHeight
|
||||
const elementTop = currentElement.offsetTop - container.offsetTop
|
||||
const elementHeight = currentElement.offsetHeight
|
||||
|
||||
if (elementTop < containerTop) {
|
||||
container.scrollTop = elementTop
|
||||
} else if (elementTop + elementHeight > containerTop + containerHeight) {
|
||||
container.scrollTop = elementTop + elementHeight - containerHeight
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [todos, scrollCurrentOnly, filteredTodos.length])
|
||||
|
||||
// Empty states
|
||||
if (!todos || todos.length === 0) {
|
||||
return (
|
||||
<div className="text-sm text-muted-foreground italic">
|
||||
No tasks in the todo list.
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (filteredTodos.length === 0) {
|
||||
return (
|
||||
<div className="text-sm text-green-600 dark:text-green-400 flex items-center gap-2">
|
||||
<span className="text-lg">✓</span>
|
||||
<span>All tasks completed!</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const renderGroup = (label: string, items: Todo[], isCollapsible = false) => {
|
||||
if (items.length === 0) return null
|
||||
return (
|
||||
<div className="mb-1 last:mb-0">
|
||||
{hasMultipleGroups && (
|
||||
<div className="text-[9px] font-medium text-muted-foreground mb-0.5 ml-4 uppercase tracking-wider opacity-70">
|
||||
{label} ({items.length})
|
||||
</div>
|
||||
)}
|
||||
<div className={isCollapsible && items.length > 3 ? 'max-h-[60px] overflow-hidden relative after:absolute after:bottom-0 after:left-0 after:right-0 after:h-4 after:bg-gradient-to-t after:from-card after:to-transparent' : ''}>
|
||||
{items.map((todo) => (
|
||||
<div key={todo.id} data-status={todo.status}>
|
||||
<TodoItem todo={todo} compact />
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const progress = filteredTodos.length > 0
|
||||
? Math.round((filteredTodos.filter(t => t.status === 'completed').length / filteredTodos.length) * 100)
|
||||
: 0
|
||||
|
||||
return (
|
||||
<div className="bg-card border rounded-lg overflow-hidden">
|
||||
{/* Compact header with progress bar */}
|
||||
<div className="px-2 py-1.5 border-b bg-muted/50 flex items-center gap-2">
|
||||
<span className="font-semibold text-xs">{title}</span>
|
||||
{isLoading && (
|
||||
<span className="w-2.5 h-2.5 border-2 border-yellow-500 border-t-transparent rounded-full animate-spin" />
|
||||
)}
|
||||
<div className="flex-1 h-1 bg-muted rounded-full overflow-hidden">
|
||||
<div
|
||||
className="h-full bg-green-600 transition-all duration-500"
|
||||
style={{ width: `${progress}%` }}
|
||||
/>
|
||||
</div>
|
||||
<span className="text-[10px] text-muted-foreground whitespace-nowrap tabular-nums">
|
||||
{filteredTodos.filter(t => t.status === 'completed').length}/{filteredTodos.length}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* 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`}
|
||||
>
|
||||
{renderGroup('In Progress', inProgress)}
|
||||
{renderGroup('Pending', pending)}
|
||||
{renderGroup('Completed', completed, true)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -5,8 +5,10 @@ import { useUserBash } from '@/stores/userBashStore'
|
||||
import { detectFileReferences } from '@/lib/fileReferences'
|
||||
import { ExternalLink, Loader2 } from 'lucide-react'
|
||||
import { CopyButton } from '@/components/ui/copy-button'
|
||||
import { TodoListDisplay } from './TodoListDisplay'
|
||||
|
||||
type ToolPart = components['schemas']['ToolPart']
|
||||
type Todo = components['schemas']['Todo']
|
||||
|
||||
interface ToolCallPartProps {
|
||||
part: ToolPart
|
||||
@@ -54,6 +56,17 @@ function ClickableJson({ json, onFileClick }: { json: unknown; onFileClick?: (fi
|
||||
return <pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-words">{parts}</pre>
|
||||
}
|
||||
|
||||
function parseTodoOutput(output: string): Todo[] | null {
|
||||
try {
|
||||
const parsed = JSON.parse(output)
|
||||
if (Array.isArray(parsed)) return parsed
|
||||
if (parsed && Array.isArray(parsed.todos)) return parsed.todos
|
||||
return null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCallPartProps) {
|
||||
const { preferences } = useSettings()
|
||||
const { userBashCommands } = useUserBash()
|
||||
@@ -62,7 +75,10 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
part.state.status === 'completed' &&
|
||||
typeof part.state.input?.command === 'string' &&
|
||||
userBashCommands.has(part.state.input.command)
|
||||
const defaultExpanded = isUserBashCommand || (preferences?.expandToolCalls ?? false)
|
||||
const isTodoTool = part.tool === 'todowrite' || part.tool === 'todoread'
|
||||
|
||||
// Todo tools show expanded content by default, other tools use preference
|
||||
const defaultExpanded = isUserBashCommand || isTodoTool || (preferences?.expandToolCalls ?? false)
|
||||
const [expanded, setExpanded] = useState(defaultExpanded)
|
||||
|
||||
useEffect(() => {
|
||||
@@ -120,14 +136,98 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
return (input.path as string) || '.'
|
||||
case 'task':
|
||||
return (input.description as string) || null
|
||||
case 'todowrite':
|
||||
case 'todoread':
|
||||
return null
|
||||
default:
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const getTodoData = () => {
|
||||
if (part.tool !== 'todowrite' && part.tool !== 'todoread') {
|
||||
return null
|
||||
}
|
||||
|
||||
const state = part.state
|
||||
|
||||
// For completed state - has output and metadata
|
||||
if (state.status === 'completed') {
|
||||
if (state.metadata?.todos && Array.isArray(state.metadata.todos)) {
|
||||
return state.metadata.todos as Todo[]
|
||||
}
|
||||
if (state.output) {
|
||||
const parsed = parseTodoOutput(state.output)
|
||||
if (parsed) return parsed
|
||||
}
|
||||
}
|
||||
|
||||
// For running state - might have metadata
|
||||
if (state.status === 'running' && state.metadata?.todos && Array.isArray(state.metadata.todos)) {
|
||||
return state.metadata.todos as Todo[]
|
||||
}
|
||||
|
||||
// For any state - try input (used by todowrite)
|
||||
if (state.input?.todos && Array.isArray(state.input.todos)) {
|
||||
return state.input.todos as Todo[]
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
const previewText = getPreviewText()
|
||||
const todoData = getTodoData()
|
||||
const isFileTool = ['read', 'write', 'edit'].includes(part.tool)
|
||||
|
||||
// For todo tools, render the TodoListDisplay directly without wrapper
|
||||
if (isTodoTool) {
|
||||
if (part.state.status === 'pending') {
|
||||
return (
|
||||
<div className="my-2 text-sm text-muted-foreground flex items-center gap-2">
|
||||
<Loader2 className="w-3 h-3 animate-spin" />
|
||||
<span>Preparing task list...</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (part.state.status === 'running') {
|
||||
return (
|
||||
<div className="my-2">
|
||||
<TodoListDisplay
|
||||
todos={todoData || []}
|
||||
title="Task List"
|
||||
showCompleted={true}
|
||||
scrollCurrentOnly={true}
|
||||
isLoading={true}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (part.state.status === 'completed') {
|
||||
return (
|
||||
<div className="my-2">
|
||||
<TodoListDisplay
|
||||
todos={todoData || []}
|
||||
title="Task List"
|
||||
showCompleted={true}
|
||||
scrollCurrentOnly={true}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (part.state.status === 'error') {
|
||||
return (
|
||||
<div className="my-2 text-sm text-red-600 dark:text-red-400">
|
||||
Error updating tasks: {part.state.error}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
if (isUserBashCommand) {
|
||||
const command = part.state.input.command as string
|
||||
const output = part.state.status === 'completed' ? part.state.output : ''
|
||||
@@ -175,11 +275,13 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
>
|
||||
<span className={getStatusColor()}>{getStatusIcon()}</span>
|
||||
<span className="font-medium">{part.tool}</span>
|
||||
|
||||
{/* Header preview text */}
|
||||
{previewText && isFileTool ? (
|
||||
<span
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
if (onFileClick) {
|
||||
if (onFileClick && previewText) {
|
||||
onFileClick(previewText)
|
||||
}
|
||||
}}
|
||||
@@ -191,8 +293,12 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
) : previewText ? (
|
||||
<span className="text-muted-foreground text-xs truncate">{previewText}</span>
|
||||
) : null}
|
||||
|
||||
{part.tool === 'task' && (() => {
|
||||
const sessionId = (part.metadata?.sessionId ?? (part.state.status !== 'pending' && part.state.metadata?.sessionId)) as string | undefined
|
||||
let sessionId: string | undefined = part.metadata?.sessionId as string | undefined
|
||||
if (!sessionId && part.state.status !== 'pending' && 'metadata' in part.state) {
|
||||
sessionId = part.state.metadata?.sessionId as string | undefined
|
||||
}
|
||||
return sessionId ? (
|
||||
<span
|
||||
onClick={(e) => {
|
||||
@@ -210,6 +316,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
<span className="text-muted-foreground text-xs ml-auto">({part.state.status})</span>
|
||||
</button>
|
||||
|
||||
{/* Expanded content for non-todo tools */}
|
||||
{expanded && (
|
||||
<div className="bg-card space-y-2 p-3">
|
||||
{part.state.status === 'pending' && (
|
||||
@@ -222,7 +329,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
|
||||
<span>Preparing tool call...</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
{part.state.status === 'running' && (
|
||||
<>
|
||||
{part.tool === 'bash' ? (
|
||||
|
||||
Reference in new issue
Block a user