mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
refactor: inline message parts with batched SSE updates (#137)
This commit is contained in:
1 parent
63561640fd
commit
725122b588
12 files changed
+408
-337
No files matched your search
@@ -4,10 +4,9 @@ import { MessagePart } from './MessagePart'
|
||||
import { UserMessageActionButtons } from './UserMessageActionButtons'
|
||||
import { EditableUserMessage, ClickableUserMessage } from './EditableUserMessage'
|
||||
import { MessageError } from './MessageError'
|
||||
import type { Message, Part } from '@/api/types'
|
||||
import type { Message, Part, MessageWithParts } from '@/api/types'
|
||||
import { useSessionStatusForSession } from '@/stores/sessionStatusStore'
|
||||
import { useSessionTodos } from '@/stores/sessionTodosStore'
|
||||
import { useMessageParts, usePartsForMessage, usePartsVersion } from '@/stores/messagePartsStore'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
import type { Todo } from '@/components/message/SessionTodoDisplay'
|
||||
import type { OpenCodeError } from '@/lib/opencode-errors'
|
||||
@@ -24,7 +23,7 @@ interface MessageThreadProps {
|
||||
opcodeUrl: string
|
||||
sessionID: string
|
||||
directory?: string
|
||||
messages?: Message[]
|
||||
messages?: MessageWithParts[]
|
||||
onFileClick?: (filePath: string, lineNumber?: number) => void
|
||||
onChildSessionClick?: (sessionId: string) => void
|
||||
onUndoMessage?: (restoredPrompt: string) => void
|
||||
@@ -48,12 +47,12 @@ const compareMessageIds = (id1: string, id2: string): number => {
|
||||
}
|
||||
|
||||
const findLastMessageByRole = (
|
||||
messages: Message[],
|
||||
messages: MessageWithParts[],
|
||||
role: 'user' | 'assistant',
|
||||
predicate?: (msg: Message) => boolean
|
||||
): string | undefined => {
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const msg = messages[i]
|
||||
const msg = messages[i].info
|
||||
if (msg.role === role && (!predicate || predicate(msg))) {
|
||||
return msg.id
|
||||
}
|
||||
@@ -62,9 +61,9 @@ const findLastMessageByRole = (
|
||||
}
|
||||
|
||||
interface MessageRowProps {
|
||||
msg: Message
|
||||
msgWithParts: MessageWithParts
|
||||
index: number
|
||||
messages: Message[]
|
||||
messages: MessageWithParts[]
|
||||
pendingAssistantId: string | undefined
|
||||
lastUserMessageId: string | undefined
|
||||
isSessionBusy: boolean
|
||||
@@ -82,7 +81,7 @@ interface MessageRowProps {
|
||||
}
|
||||
|
||||
const MessageRow = memo(function MessageRow({
|
||||
msg,
|
||||
msgWithParts,
|
||||
index,
|
||||
messages,
|
||||
pendingAssistantId,
|
||||
@@ -100,13 +99,15 @@ const MessageRow = memo(function MessageRow({
|
||||
handleCancelEdit,
|
||||
model,
|
||||
}: MessageRowProps) {
|
||||
const parts = usePartsForMessage(msg.id)
|
||||
const msg = msgWithParts.info
|
||||
const parts = msgWithParts.parts
|
||||
const streaming = isMessageStreaming(msg)
|
||||
const isQueued = msg.role === 'user' && pendingAssistantId && compareMessageIds(msg.id, pendingAssistantId) > 0
|
||||
const isLastUserMessage = msg.role === 'user' && msg.id === lastUserMessageId
|
||||
const messageTextContent = getMessageTextContent(parts)
|
||||
|
||||
const nextAssistantMessage = messages.slice(index + 1).find(m => m.role === 'assistant')
|
||||
const nextAssistantMessage = messages.slice(index + 1).find(m => m.info.role === 'assistant')
|
||||
const nextAssistantMsg = nextAssistantMessage?.info
|
||||
const isUserBeforeAssistant = msg.role === 'user' && nextAssistantMessage
|
||||
const canEditUserMessage = isLastUserMessage && isUserBeforeAssistant && !isSessionBusy
|
||||
const canUndoUserMessage = isLastUserMessage && nextAssistantMessage && !isSessionBusy && onUndoMessage
|
||||
@@ -139,9 +140,9 @@ const MessageRow = memo(function MessageRow({
|
||||
{new Date(msg.time.created).toLocaleTimeString()}
|
||||
</span>
|
||||
)}
|
||||
{canEditUserMessage && nextAssistantMessage && (
|
||||
{canEditUserMessage && nextAssistantMsg && (
|
||||
<button
|
||||
onClick={() => handleStartEditUserMessage(msg.id, nextAssistantMessage.id)}
|
||||
onClick={() => handleStartEditUserMessage(msg.id, nextAssistantMsg.id)}
|
||||
className="p-1 rounded hover:bg-accent text-muted-foreground hover:text-foreground transition-colors"
|
||||
title="Edit message"
|
||||
>
|
||||
@@ -178,10 +179,10 @@ const MessageRow = memo(function MessageRow({
|
||||
onCancel={handleCancelEdit}
|
||||
model={model}
|
||||
/>
|
||||
) : msg.role === 'user' && canEditUserMessage && nextAssistantMessage ? (
|
||||
) : msg.role === 'user' && canEditUserMessage && nextAssistantMsg ? (
|
||||
<ClickableUserMessage
|
||||
content={messageTextContent}
|
||||
onClick={() => handleStartEditUserMessage(msg.id, nextAssistantMessage.id)}
|
||||
onClick={() => handleStartEditUserMessage(msg.id, nextAssistantMsg.id)}
|
||||
isEditable={false}
|
||||
/>
|
||||
) : parts.length > 0 ? (
|
||||
@@ -234,19 +235,11 @@ export const MessageThread = memo(function MessageThread({
|
||||
|
||||
const isSessionBusy = !!pendingAssistantId || isSessionInRetry(sessionStatus)
|
||||
const setSessionTodos = useSessionTodos((state) => state.setTodos)
|
||||
const partsVersion = usePartsVersion()
|
||||
|
||||
useEffect(() => {
|
||||
if (!messages || messages.length === 0) return
|
||||
|
||||
const partsMap = useMessageParts.getState().parts
|
||||
const allParts: Part[] = []
|
||||
for (const msg of messages) {
|
||||
const msgParts = partsMap.get(msg.id)
|
||||
if (msgParts) {
|
||||
allParts.push(...msgParts)
|
||||
}
|
||||
}
|
||||
const allParts = messages.flatMap(m => m.parts)
|
||||
|
||||
const latestTodoPart = allParts
|
||||
.filter((part): part is components['schemas']['ToolPart'] => part.type === 'tool' && (part.tool === 'todowrite' || part.tool === 'todoread'))
|
||||
@@ -281,7 +274,7 @@ export const MessageThread = memo(function MessageThread({
|
||||
setSessionTodos(sessionID, todos)
|
||||
}
|
||||
}
|
||||
}, [messages, sessionID, setSessionTodos, partsVersion])
|
||||
}, [messages, sessionID, setSessionTodos])
|
||||
|
||||
const handleStartEditUserMessage = useCallback((userMessageId: string, assistantMessageId: string) => {
|
||||
setEditingUserMessageId(userMessageId)
|
||||
@@ -303,10 +296,10 @@ export const MessageThread = memo(function MessageThread({
|
||||
|
||||
return (
|
||||
<div className="flex flex-col space-y-2 p-2 overflow-x-hidden">
|
||||
{messages.map((msg, index) => (
|
||||
{messages.map((msgWithParts, index) => (
|
||||
<MessageRow
|
||||
key={msg.id}
|
||||
msg={msg}
|
||||
key={msgWithParts.info.id}
|
||||
msgWithParts={msgWithParts}
|
||||
index={index}
|
||||
messages={messages}
|
||||
pendingAssistantId={pendingAssistantId}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useRef, useEffect, useMemo, useImperativeHandle, forwardRef, type KeyboardEvent } from 'react'
|
||||
import { useSendPrompt, useAbortSession, useMessages, useSendShell, useAgents } from '@/hooks/useOpenCode'
|
||||
import { useState, useRef, useEffect, useMemo, useImperativeHandle, forwardRef, memo, type KeyboardEvent } from 'react'
|
||||
import { useSendPrompt, useAbortSession, useSendShell, useAgents } from '@/hooks/useOpenCode'
|
||||
import { useCommands } from '@/hooks/useCommands'
|
||||
import { useCommandHandler } from '@/hooks/useCommandHandler'
|
||||
import { useFileSearch } from '@/hooks/useFileSearch'
|
||||
@@ -10,7 +10,7 @@ import { useSTT } from '@/hooks/useSTT'
|
||||
|
||||
import { useUserBash } from '@/stores/userBashStore'
|
||||
import { useMobile } from '@/hooks/useMobile'
|
||||
import { useSessionStatusForSession } from '@/stores/sessionStatusStore'
|
||||
|
||||
import { usePermissions } from '@/contexts/EventContext'
|
||||
import { ChevronDown, Upload, X, Mic, MicOff } from 'lucide-react'
|
||||
|
||||
@@ -25,7 +25,7 @@ import { detectMentionTrigger, parsePromptToParts, getFilename, filterAgentsByQu
|
||||
|
||||
|
||||
import type { components } from '@/api/opencode-types'
|
||||
import type { Message, FileAttachmentInfo, ImageAttachment } from '@/api/types'
|
||||
import type { FileAttachmentInfo, ImageAttachment } from '@/api/types'
|
||||
|
||||
const ACCEPTED_IMAGE_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp", "image/heic", "image/heif"]
|
||||
|
||||
@@ -55,6 +55,7 @@ interface PromptInputProps {
|
||||
sessionID: string
|
||||
disabled?: boolean
|
||||
showScrollButton?: boolean
|
||||
hasActiveStream?: boolean
|
||||
onScrollToBottom?: () => void
|
||||
onShowSessionsDialog?: () => void
|
||||
onShowModelsDialog?: () => void
|
||||
@@ -64,12 +65,13 @@ interface PromptInputProps {
|
||||
onPromptChange?: (hasContent: boolean) => void
|
||||
}
|
||||
|
||||
export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(function PromptInput({
|
||||
export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(function PromptInput({
|
||||
opcodeUrl,
|
||||
directory,
|
||||
sessionID,
|
||||
disabled,
|
||||
showScrollButton,
|
||||
hasActiveStream = false,
|
||||
onScrollToBottom,
|
||||
onShowSessionsDialog,
|
||||
onShowModelsDialog,
|
||||
@@ -137,7 +139,6 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
|
||||
const sendPrompt = useSendPrompt(opcodeUrl, directory)
|
||||
const sendShell = useSendShell(opcodeUrl, directory)
|
||||
const abortSession = useAbortSession(opcodeUrl, directory, sessionID)
|
||||
const { data: messages } = useMessages(opcodeUrl, sessionID, directory)
|
||||
const { filterCommands } = useCommands(opcodeUrl)
|
||||
const { executeCommand } = useCommandHandler({
|
||||
opcodeUrl,
|
||||
@@ -184,7 +185,7 @@ export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(funct
|
||||
}, [agents, searchResults, mentionQuery])
|
||||
|
||||
|
||||
const { addUserBashCommand } = useUserBash()
|
||||
const addUserBashCommand = useUserBash((s) => s.addUserBashCommand)
|
||||
|
||||
const handleSubmit = () => {
|
||||
if (disabled) return
|
||||
@@ -715,14 +716,6 @@ if (isIOS && isSecureContext && navigator.clipboard && navigator.clipboard.read)
|
||||
}
|
||||
}
|
||||
|
||||
const isMessageIncomplete = (msg: Message): boolean => {
|
||||
if (msg.role !== 'assistant') return false
|
||||
return !('completed' in msg.time && msg.time.completed)
|
||||
}
|
||||
|
||||
const lastAssistantMessage = messages?.filter(msg => msg.role === 'assistant').at(-1)
|
||||
const hasIncompleteMessages = lastAssistantMessage ? isMessageIncomplete(lastAssistantMessage) : false
|
||||
|
||||
const sessionAgent = useSessionAgent(opcodeUrl, sessionID, directory)
|
||||
const currentMode = localMode ?? sessionAgent.agent
|
||||
|
||||
@@ -733,10 +726,7 @@ const { model, modelString } = useModelSelection(opcodeUrl, directory)
|
||||
const { setShowDialog, hasForSession: hasPermissionsForSession } = usePermissions()
|
||||
const hasPendingPermissionForSession = hasPermissionsForSession(sessionID)
|
||||
const { hasVariants, currentVariant, cycleVariant } = useVariants(opcodeUrl, directory)
|
||||
const sessionStatus = useSessionStatusForSession(sessionID)
|
||||
const isSessionActive = sessionStatus.type === 'busy' || sessionStatus.type === 'retry'
|
||||
const hasActiveStream = hasIncompleteMessages && isSessionActive
|
||||
const showStopButton = isSessionActive && hasIncompleteMessages
|
||||
const showStopButton = hasActiveStream
|
||||
const hideSecondaryButtons = isMobile && hasActiveStream
|
||||
|
||||
|
||||
@@ -969,4 +959,4 @@ return (
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
}))
|
||||
Reference in new issue
Block a user