import type { SpeedMode } from '@/lib/types' import type { AttachmentInput, AttachmentOrigin, DrawingPurpose, MessageAttachment, TextAttachment, UploadInfo } from '@/lib/types' import { uploadChatFile } from './composer/attachments/uploads' import { attachmentPart } from '@/lib/moi-attachments' import { composerDraftKey } from '@/lib/session-drafts' import type { QueryClient } from '@tanstack/react-query' import { workspaceKeys } from '@/client/api/workspace-keys' import { changeWorkspaceStateCache } from '@/client/features/workspace/workspace-state' import { attachmentKey, liveStore } from '@/client/features/chat/chat-store' import { useUiStore } from '@/client/store/ui' import type { StagedAttachment } from '@/client/features/chat/composer/attachments/types' import { resolveSpeedMode, resolveSelectedModel } from '@/client/features/chat/composer/model-order' import type { MoiUserMessageOptions } from '@/client/features/workspace/moi-context' import { STREAM_RESPONSES } from '@/client/lib/flags' import { formatChatTitle } from '@/lib/chat-title' import { applyEvent, emptyViewState } from '@/lib/format' import { messageAttachmentLimitError } from '@/lib/message-attachments' import type { Part, SessionSummary, ViewState, WorkspaceHarness, WorkspaceTabId } from '@/lib/types' // Explicit attachments belong to this send, independently of the user's draft. export type PreparedAttachments = { attachments: MessageAttachment[] parts: Part[] } export type ChatSendOptions = MoiUserMessageOptions & { preparedAttachments?: PreparedAttachments } // Both send paths derive the wire reference and optimistic display together. function prepareAttachment( input: TextAttachment | UploadInfo, previewUrl?: string, options: Partial & { purpose?: DrawingPurpose } = {} ): { attachment: MessageAttachment; part: Part } { if ('id' in input) { const metadata = { source: options.source, ...(input.kind === 'image' ? { purpose: options.purpose } : {}) } return { attachment: { type: 'upload', uploadId: input.id, ...metadata }, part: { type: 'file-attachment', mediaType: input.mediaType, label: input.filename, ...metadata, previewUrl: input.kind === 'image' ? previewUrl : undefined } } } const { source, label, text } = input return { attachment: { type: 'text', source, label, text }, part: attachmentPart({ type: 'text', source, label, text }) } } function collectPreparedAttachments( items: ReturnType[] ): PreparedAttachments { return { attachments: items.map(item => item.attachment), parts: items.map(item => item.part) } } export async function prepareChatAttachments( workspaceId: string, inputs: readonly (AttachmentInput & AttachmentOrigin)[] ): Promise { const limitError = messageAttachmentLimitError(inputs) if (limitError) throw new Error(limitError) return collectPreparedAttachments( await Promise.all( inputs.map(async input => { if (input.type === 'text') return prepareAttachment(input) const upload = await uploadChatFile(workspaceId, input.file ?? input.path) return prepareAttachment(upload, `/api/workspaces/${workspaceId}/uploads/${upload.id}`, { source: input.source }) }) ) ) } export function prepareDraftAttachments( attachments: readonly StagedAttachment[] ): PreparedAttachments { return collectPreparedAttachments( attachments.flatMap(attachment => { if (attachment.kind === 'text') return [prepareAttachment(attachment)] if (!attachment.upload) return [] return [ prepareAttachment( { ...attachment.upload, filename: attachment.label, mediaType: attachment.mediaType }, attachment.previewUrl, { source: attachment.source, purpose: attachment.kind === 'drawing' ? attachment.purpose : undefined } ) ] }) ) } // Whether this send owns what the user has staged in the composer. Only a send // FROM the composer does. An applet's message is not the message the user is // building, so it must neither carry files they staged for their own message // nor clear ones still uploading out from under them. export function ownsComposerAttachments(options?: ChatSendOptions): boolean { return !options?.applet && !options?.preparedAttachments } // The fully-uploaded attachments this send should carry — none for a send that // doesn't own the composer's. export function attachmentsForSend( workspaceId: string, sessionId: string | null, options?: ChatSendOptions ): StagedAttachment[] { if (!ownsComposerAttachments(options)) return [] const pending = liveStore.getState().attachments[attachmentKey(workspaceId, sessionId)] ?? [] return pending.filter(a => a.kind === 'text' || (a.status === 'ready' && a.upload)) } type StartOptimisticTurnInput = { queryClient: QueryClient workspaceId: string sessionId: string parts: Part[] } export function startOptimisticTurn({ queryClient, workspaceId, sessionId, parts }: StartOptimisticTurnInput): string { const optimisticId = `optimistic:${crypto.randomUUID()}` queryClient.setQueryData(workspaceKeys.events(workspaceId, sessionId), current => applyEvent(current ?? emptyViewState(), { kind: 'turn', turn: { id: optimisticId, role: 'user', origin: { kind: 'user-input' }, parts, timestamp: new Date().toISOString() } }) ) liveStore.getState().setActivity(workspaceId, sessionId, 'running') liveStore.getState().setError(workspaceId, sessionId, null) return optimisticId } type StartOptimisticSessionInput = { tabId?: WorkspaceTabId queryClient: QueryClient workspaceId: string sessionId: string text: string filenames?: readonly string[] } export function startOptimisticSession({ tabId, queryClient, workspaceId, sessionId, text, filenames = [] }: StartOptimisticSessionInput): void { // File the chat under its tab now; the server confirms it with the send. if (tabId && tabId !== 'overview') changeWorkspaceStateCache(queryClient, workspaceId, draft => { draft.sessions[sessionId] = { ...draft.sessions[sessionId], tabId } }) const summary = formatChatTitle(text, filenames) if (!summary) return queryClient.setQueryData(workspaceKeys.sessions(workspaceId), current => [ { sessionId, summary, lastModified: Date.now() }, ...(current ?? []).filter(session => session.sessionId !== sessionId) ]) } export function resolveChatRunOptions( modelsData: WorkspaceHarness | undefined, pickedModel: string | undefined, pickedEffort: string | undefined, pickedSpeedMode?: SpeedMode ): { model?: string; effort?: string; speedMode?: SpeedMode; stream?: true } { const models = modelsData?.models const savedModel = !pickedModel || !models || models.some(candidate => candidate.value === pickedModel) ? pickedModel : undefined const modelInfo = modelsData?.provider === 'codex' ? resolveSelectedModel(modelsData.models, pickedModel) : models?.find(candidate => candidate.value === (savedModel ?? 'default')) // Omitting Codex's model inherits config.toml (or the resumed thread's // model), which can be unavailable even though the picker shows a supported // fallback. Always send the concrete model the Codex picker displays. const model = modelsData?.provider === 'codex' && modelInfo ? (modelInfo.resolvedModel ?? modelInfo.value) : savedModel const effort = pickedEffort && (!modelInfo || (modelInfo.supportedEffortLevels ?? []).includes(pickedEffort)) ? pickedEffort : undefined const speedMode = modelInfo ? resolveSpeedMode(modelInfo, pickedSpeedMode) : pickedSpeedMode const stream = STREAM_RESPONSES && modelsData?.supportsStreaming ? true : undefined return { model, effort, ...(speedMode !== undefined ? { speedMode } : {}), stream } } export function moveChatDraft(workspaceId: string, from: string, to: string) { liveStore.getState().renameSession(workspaceId, from, to) useUiStore .getState() .moveComposerDraft(composerDraftKey(workspaceId, from), composerDraftKey(workspaceId, to)) } export function prepareOptimisticSend( input: Omit & { text: string }, ready: readonly StagedAttachment[], prepared = prepareDraftAttachments(ready) ) { const parts: Part[] = [...prepared.parts] if (input.text) parts.push({ type: 'text', text: input.text }) return { attachments: prepared.attachments, optimisticId: startOptimisticTurn({ ...input, parts }) } } export function finishComposerSend(workspaceId: string, sent: readonly StagedAttachment[]) { // Local IDs survive native session renames. Remove only this send's snapshot, // preserving files staged while an HTTP submission was in flight. for (const attachment of sent) liveStore.getState().removeAttachment(workspaceId, attachment.localId) useUiStore.getState().markMessageSentFromMoi(workspaceId) }