Fix pnpm resources (#29)

* Add message undo/refresh features and stop audio floating button

* Exclude workspace directory from pnpm workspace detection

---------

Co-authored-by: Chris Scott <chris@cstech.dev>
This commit is contained in:
Chris ScottandChris Scott authored and GitHub committed 2025-12-21 09:46:38 -05:00
1 parent fb430359ce
commit 0cbfe2e358
8 files changed
+338 -19

No files matched your search

@@ -1,6 +1,7 @@
import { memo, useMemo, useState, useCallback } from 'react'
import { MessagePart } from './MessagePart'
import { MessageActionButtons } from './MessageActionButtons'
import { UserMessageActionButtons } from './UserMessageActionButtons'
import { EditableUserMessage, ClickableUserMessage } from './EditableUserMessage'
import type { MessageWithParts } from '@/api/types'
@@ -19,6 +20,7 @@ interface MessageThreadProps {
messages?: MessageWithParts[]
onFileClick?: (filePath: string, lineNumber?: number) => void
onChildSessionClick?: (sessionId: string) => void
onUndoMessage?: (restoredPrompt: string) => void
model?: string
agent?: string
}
@@ -28,25 +30,20 @@ const isMessageStreaming = (msg: MessageWithParts): boolean => {
return !('completed' in msg.info.time && msg.info.time.completed)
}
const findPendingAssistantMessageId = (messages: MessageWithParts[]): string | undefined => {
const findLastMessageByRole = (
messages: MessageWithParts[],
role: 'user' | 'assistant',
predicate?: (msg: MessageWithParts) => boolean
): string | undefined => {
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i]
if (msg.info.role === 'assistant' && isMessageStreaming(msg)) {
if (msg.info.role === role && (!predicate || predicate(msg))) {
return msg.info.id
}
}
return undefined
}
const findLastAssistantMessageId = (messages: MessageWithParts[]): string | undefined => {
for (let i = messages.length - 1; i >= 0; i--) {
if (messages[i].info.role === 'assistant') {
return messages[i].info.id
}
}
return undefined
}
const findUserMessageBeforeAssistant = (
messages: MessageWithParts[],
assistantMessageId: string
@@ -69,6 +66,7 @@ export const MessageThread = memo(function MessageThread({
messages,
onFileClick,
onChildSessionClick,
onUndoMessage,
model,
agent
}: MessageThreadProps) {
@@ -77,12 +75,17 @@ export const MessageThread = memo(function MessageThread({
const pendingAssistantId = useMemo(() => {
if (!messages) return undefined
return findPendingAssistantMessageId(messages)
return findLastMessageByRole(messages, 'assistant', isMessageStreaming)
}, [messages])
const lastAssistantId = useMemo(() => {
if (!messages) return undefined
return findLastAssistantMessageId(messages)
return findLastMessageByRole(messages, 'assistant')
}, [messages])
const lastUserMessageId = useMemo(() => {
if (!messages) return undefined
return findLastMessageByRole(messages, 'user')
}, [messages])
const isSessionBusy = !!pendingAssistantId
@@ -111,11 +114,13 @@ export const MessageThread = memo(function MessageThread({
const streaming = isMessageStreaming(msg)
const isQueued = msg.info.role === 'user' && pendingAssistantId && msg.info.id > pendingAssistantId
const isLastAssistant = msg.info.id === lastAssistantId
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 userMessageContent = msg.info.role === 'assistant'
? findUserMessageBeforeAssistant(messages, msg.info.id)
@@ -174,6 +179,20 @@ export const MessageThread = memo(function MessageThread({
agent={agent}
/>
)}
{canUndoUserMessage && (
<UserMessageActionButtons
opcodeUrl={opcodeUrl}
sessionId={sessionID}
directory={directory}
userMessageId={msg.info.id}
userMessageContent={messageTextContent}
assistantMessageId={nextAssistantMessage.info.id}
onUndo={onUndoMessage}
model={model}
agent={agent}
/>
)}
</div>
<div className="space-y-2">
@@ -1,4 +1,4 @@
import { useState, useRef, useEffect, useMemo, type KeyboardEvent } from 'react'
import { useState, useRef, useEffect, useMemo, useImperativeHandle, forwardRef, type KeyboardEvent } from 'react'
import { useSendPrompt, useAbortSession, useMessages, useSendShell, useAgents } from '@/hooks/useOpenCode'
import { useSettings } from '@/hooks/useSettings'
import { useCommands } from '@/hooks/useCommands'
@@ -23,6 +23,11 @@ import type { MessageWithParts, FileInfo } from '@/api/types'
type CommandType = components['schemas']['Command']
export interface PromptInputHandle {
setPromptValue: (value: string) => void
clearPrompt: () => void
}
interface PromptInputProps {
opcodeUrl: string
directory?: string
@@ -35,9 +40,10 @@ interface PromptInputProps {
onShowHelpDialog?: () => void
onToggleDetails?: () => boolean
onExportSession?: () => void
onPromptChange?: (hasContent: boolean) => void
}
export function PromptInput({
export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(function PromptInput({
opcodeUrl,
directory,
sessionID,
@@ -48,10 +54,30 @@ export function PromptInput({
onShowModelsDialog,
onShowHelpDialog,
onToggleDetails,
onExportSession
}: PromptInputProps) {
onExportSession,
onPromptChange
}, ref) {
const [prompt, setPrompt] = useState('')
useImperativeHandle(ref, () => ({
setPromptValue: (value: string) => {
setPrompt(value)
if (textareaRef.current) {
textareaRef.current.style.height = 'auto'
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`
textareaRef.current.focus()
}
},
clearPrompt: () => {
setPrompt('')
setAttachedFiles(new Map())
setSelectedAgent(null)
if (textareaRef.current) {
textareaRef.current.style.height = 'auto'
}
}
}), [])
const [isBashMode, setIsBashMode] = useState(false)
const [showSuggestions, setShowSuggestions] = useState(false)
const [suggestionQuery, setSuggestionQuery] = useState('')
@@ -465,6 +491,10 @@ export function PromptInput({
}
}, [disabled])
useEffect(() => {
onPromptChange?.(prompt.trim().length > 0)
}, [prompt, onPromptChange])
return (
@@ -573,4 +603,4 @@ export function PromptInput({
/>
</div>
)
}
})
@@ -0,0 +1,86 @@
import { memo } from 'react'
import { X, RefreshCw, Loader2 } from 'lucide-react'
import { useUndoMessage } from '@/hooks/useUndoMessage'
import { useRefreshMessage } from '@/hooks/useRemoveMessage'
interface UserMessageActionButtonsProps {
opcodeUrl: string
sessionId: string
directory?: string
userMessageId: string
userMessageContent: string
assistantMessageId: string
onUndo: (restoredPrompt: string) => void
model?: string
agent?: string
}
export const UserMessageActionButtons = memo(function UserMessageActionButtons({
opcodeUrl,
sessionId,
directory,
userMessageId,
userMessageContent,
assistantMessageId,
onUndo,
model,
agent
}: UserMessageActionButtonsProps) {
const undoMessage = useUndoMessage({
opcodeUrl,
sessionId,
directory,
onSuccess: onUndo
})
const refreshMessage = useRefreshMessage({ opcodeUrl, sessionId, directory })
const isLoading = undoMessage.isPending || refreshMessage.isPending
const handleUndo = () => {
if (isLoading) return
undoMessage.mutate({
messageID: userMessageId,
messageContent: userMessageContent
})
}
const handleRefresh = () => {
if (isLoading || !userMessageContent) return
refreshMessage.mutate({
assistantMessageID: assistantMessageId,
userMessageContent,
model,
agent
})
}
return (
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<button
onClick={handleRefresh}
disabled={isLoading}
className="p-1 rounded hover:bg-primary/20 text-muted-foreground hover:text-primary transition-colors disabled:opacity-50"
title="Try again for different response"
>
{refreshMessage.isPending ? (
<Loader2 className="w-3.5 h-3.5 animate-spin" />
) : (
<RefreshCw className="w-3.5 h-3.5" />
)}
</button>
<button
onClick={handleUndo}
disabled={isLoading}
className="p-1 rounded hover:bg-destructive/20 text-muted-foreground hover:text-destructive transition-colors disabled:opacity-50"
title="Undo this message"
>
{undoMessage.isPending ? (
<Loader2 className="w-3.5 h-3.5 animate-spin" />
) : (
<X className="w-3.5 h-3.5" />
)}
</button>
</div>
)
})