Skip to content
Merged
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
18 changes: 18 additions & 0 deletions ui-packages/web/src/core/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type StoredConversation = {
titledBy?: TitleSource
selection?: ModelSelection
draft: ConversationDraft
// Sent while a reply was running and not yet given to the model.
queued?: ConversationDraft[]
createdAt: number
lastActiveAt: number
}
Expand All @@ -40,6 +42,22 @@ export type ConversationPage = {

export const emptyConversationDraft = (): ConversationDraft => ({ text: '', attachments: [] })

// Queued messages were written before the draft, so they come first.
export const foldQueued = (
draft: ConversationDraft,
queued: readonly ConversationDraft[],
): ConversationDraft => ({
text: [...queued.map(item => item.text), draft.text].filter(text => text.trim()).join('\n\n'),
attachments: [...queued.flatMap(item => item.attachments), ...draft.attachments],
})

// A queue only means something while its reply runs; a conversation opened later gets it back as
// its draft.
export const returnQueuedToDraft = (conversation: StoredConversation): StoredConversation =>
conversation.queued?.length
? { ...conversation, draft: foldQueued(conversation.draft, conversation.queued), queued: [] }
: conversation

export const conversationTitle = (draft: ConversationDraft) => {
const firstLine = draft.text.trim().split(/\r?\n/, 1)[0]?.replace(/\s+/g, ' ')
const source = firstLine || draft.attachments[0]?.name || 'New conversation'
Expand Down
16 changes: 16 additions & 0 deletions ui-packages/web/src/data/conversation-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ describe('conversation store', () => {
expect((await listStoredFiles()).some(file => file.path === 'Shared.md')).toBe(true)
})

it('opens a conversation with its unsent queue returned to the draft', async () => {
await saveStoredConversationDraft({
...conversation('queued', 10),
draft: { text: 'Still typing', attachments: [] },
queued: [
{ text: 'First queued', attachments: [] },
{ text: 'Second queued', attachments: [] },
],
})

const opened = await getStoredConversation('queued')

expect(opened?.conversation.draft.text).toBe('First queued\n\nSecond queued\n\nStill typing')
expect(opened?.conversation.queued).toEqual([])
})

it('pages by activity and id without duplicates when timestamps match', async () => {
await Promise.all(
Array.from({ length: 65 }, (_, index) =>
Expand Down
15 changes: 8 additions & 7 deletions ui-packages/web/src/data/conversation-store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { AgentMessage } from '@earendil-works/pi-agent-core'
import type {
ConversationCursor,
ConversationPage,
StoredConversation,
TitleSource,
import {
type ConversationCursor,
type ConversationPage,
returnQueuedToDraft,
type StoredConversation,
type TitleSource,
} from '../core/conversations'
import { openWorkspaceDatabase, requestPersistentStorage } from './workspace-database'

Expand Down Expand Up @@ -55,7 +56,7 @@ export const getStoredConversation = async (id: string) => {
])
if (!conversation) return null
return {
conversation,
conversation: returnQueuedToDraft(conversation),
messages: records
.sort((left, right) => left.position - right.position)
.map(record => record.message),
Expand Down Expand Up @@ -98,7 +99,7 @@ export const touchStoredConversation = async (id: string, lastActiveAt: number)
const database = await openWorkspaceDatabase()
const conversation = await database.get('conversations', id)
if (!conversation) throw new Error('Conversation is unavailable.')
const updated = { ...conversation, lastActiveAt }
const updated = { ...returnQueuedToDraft(conversation), lastActiveAt }
await database.put('conversations', updated)
return updated
}
Expand Down
1 change: 1 addition & 0 deletions ui-packages/web/src/features/agent/create-reader-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const createReaderAgent = (
const agent = new Agent({
sessionId: session.id,
toolExecution: 'sequential',
steeringMode: 'all',
initialState: {
model: session.model,
thinkingLevel: session.thinkingLevel,
Expand Down
56 changes: 39 additions & 17 deletions ui-packages/web/src/features/conversation/composer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ArrowUp, Paperclip, Square } from 'lucide-react'
import { type DragEvent, type RefObject, useLayoutEffect, useRef, useState } from 'react'
import type { ConversationDraft } from '../../../core/conversations'
import { DraftAttachmentTray } from '../conversation-attachments'
import { ConversationModelControl, type ModelControlProps } from '../model-control'
import type { ConversationPhase } from '../use-conversation'
import type { DraftAttachment } from '../use-draft-attachments'
import { QueuedMessages } from './queued-messages'
import styles from './style.module.scss'

type ComposerProps = {
Expand All @@ -14,6 +16,8 @@ type ComposerProps = {
inputRef: RefObject<HTMLTextAreaElement | null>
draft: string
phase: ConversationPhase
queued: readonly ConversationDraft[]
onRemoveQueued: (index: number) => void
attachments: DraftAttachment[]
limitReached: boolean
unsettled: boolean
Expand All @@ -40,6 +44,8 @@ export const ConversationComposer = ({
onConfigureModels,
draft,
phase,
queued,
onRemoveQueued,
attachments,
limitReached,
unsettled,
Expand All @@ -57,8 +63,9 @@ export const ConversationComposer = ({
const running = phase === 'running' || phase === 'stopping'
const readyAttachments = attachments.filter(item => item.status === 'ready')
const hasUnavailable = readyAttachments.some(item => !availableFileIds.has(item.metadata.id))
// While a reply runs, sending queues the message.
const canSend =
phase === 'ready' &&
(phase === 'ready' || running) &&
!unsettled &&
!hasUnavailable &&
Boolean(draft.trim() || readyAttachments.length)
Expand Down Expand Up @@ -100,6 +107,7 @@ export const ConversationComposer = ({
}}
onDrop={addDroppedFiles}
>
<QueuedMessages queued={queued} onRemove={onRemoveQueued} />
<DraftAttachmentTray
attachments={attachments}
onOpen={onOpenFile}
Expand All @@ -125,13 +133,13 @@ export const ConversationComposer = ({
}}
rows={1}
onKeyDown={event => {
if (
event.key !== 'Enter' ||
event.shiftKey ||
event.nativeEvent.isComposing ||
event.keyCode === 229
)
if (event.nativeEvent.isComposing || event.keyCode === 229) return
if (event.key === 'Escape' && running) {
event.preventDefault()
onStop()
return
}
if (event.key !== 'Enter' || event.shiftKey) return
event.preventDefault()
if (canSend) onSend()
}}
Expand Down Expand Up @@ -168,16 +176,30 @@ export const ConversationComposer = ({
/>
)}
{limitReached && <span className={styles.attachmentLimit}>10 attachments maximum</span>}
<button
type="button"
className={styles.sendButton}
aria-label={running ? 'Stop generation' : 'Send question'}
title={running ? 'Stop generation' : 'Send question'}
disabled={phase === 'stopping' || (!running && !canSend)}
onClick={running ? onStop : onSend}
>
{running ? <Square size={13} /> : <ArrowUp size={17} />}
</button>
{running && (
<button
type="button"
className={styles.sendButton}
aria-label="Stop generation"
title="Stop generation"
disabled={phase === 'stopping'}
onClick={onStop}
>
<Square size={13} />
</button>
)}
{(!running || canSend) && (
<button
type="button"
className={styles.sendButton}
aria-label={running ? 'Queue message' : 'Send question'}
title={running ? 'Queue message' : 'Send question'}
disabled={!canSend}
onClick={onSend}
>
<ArrowUp size={17} />
</button>
)}
</div>
{dragging && <span className={styles.dropHint}>Drop files to attach</span>}
</fieldset>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Paperclip, X } from 'lucide-react'
import type { ConversationDraft } from '../../../core/conversations'
import styles from './style.module.scss'

export const QueuedMessages = ({
queued,
onRemove,
}: {
queued: readonly ConversationDraft[]
onRemove: (index: number) => void
}) => {
if (!queued.length) return null
return (
<ul className={styles.queued} aria-label="Queued messages">
{queued.map((item, index) => (
// biome-ignore lint/suspicious/noArrayIndexKey: items have no identity; the list only appends and removes
<li key={index} className={styles.queuedItem}>
<span className={styles.queuedText}>
{item.text || item.attachments.map(attachment => attachment.name).join(', ')}
</span>
{item.attachments.length > 0 && (
<span className={styles.queuedAttachments}>
<Paperclip size={11} aria-hidden />
{item.attachments.length}
</span>
)}
<button
type="button"
className="icon-button"
aria-label="Remove queued message"
title="Remove queued message"
onClick={() => onRemove(index)}
>
<X size={12} />
</button>
</li>
))}
</ul>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,45 @@
background: var(--accent);
color: var(--surface);
}
.bottom .sendButton + .sendButton {
margin-left: 6px;
}
.queued {
display: grid;
gap: 4px;
margin: 0 0 8px;
padding: 0;
list-style: none;
}
.queuedItem {
display: flex;
align-items: center;
gap: 6px;
min-width: 0;
padding: 3px 4px 3px 8px;
border-radius: 5px;
background: oklch(0.96 0 0);
color: var(--muted);
font-size: 12px;
}
.queuedItem > button {
flex-shrink: 0;
width: 22px;
height: 22px;
}
.queuedText {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.queuedAttachments {
display: inline-flex;
align-items: center;
gap: 2px;
flex-shrink: 0;
}

@media (max-width: 599px) {
.composer textarea {
Expand Down
Loading