| 118 | } |
| 119 | |
| 120 | export const prepareUserMessage = async (params: { |
| 121 | content: string |
| 122 | agentMode: AgentMode |
| 123 | postUserMessage?: (prev: ChatMessage[]) => ChatMessage[] |
| 124 | attachments?: PendingAttachment[] |
| 125 | deps: PrepareUserMessageDeps |
| 126 | }): Promise<{ |
| 127 | userMessageId: string |
| 128 | messageContent: MessageContent[] | undefined |
| 129 | bashContextForPrompt: string |
| 130 | finalContent: string |
| 131 | }> => { |
| 132 | const { content, agentMode, postUserMessage, attachments, deps } = params |
| 133 | const { setMessages, lastMessageMode, setLastMessageMode, scrollToLatest } = |
| 134 | deps |
| 135 | |
| 136 | const { pendingBashMessages, clearPendingBashMessages } = |
| 137 | useChatStore.getState() |
| 138 | const { bashMessages, bashContextForPrompt } = |
| 139 | processBashContext(pendingBashMessages) |
| 140 | |
| 141 | if (bashMessages.length > 0) { |
| 142 | setMessages((prev) => [...prev, ...bashMessages]) |
| 143 | } |
| 144 | clearPendingBashMessages() |
| 145 | |
| 146 | // Split attachments by kind |
| 147 | const allAttachments = |
| 148 | attachments ?? useChatStore.getState().pendingAttachments |
| 149 | if (!attachments && allAttachments.length > 0) { |
| 150 | useChatStore.getState().clearPendingAttachments() |
| 151 | } |
| 152 | |
| 153 | const pendingImages = allAttachments.filter( |
| 154 | (a): a is PendingImageAttachment => a.kind === 'image', |
| 155 | ) |
| 156 | const pendingTextAttachments = allAttachments.filter( |
| 157 | (a): a is PendingTextAttachment => a.kind === 'text', |
| 158 | ) |
| 159 | |
| 160 | const pendingFileAttachments = allAttachments.filter( |
| 161 | (a): a is PendingFileAttachment => a.kind === 'file', |
| 162 | ) |
| 163 | |
| 164 | // Append text attachments to the content |
| 165 | let finalContent = content |
| 166 | if (pendingTextAttachments.length > 0) { |
| 167 | const textAttachmentContent = pendingTextAttachments |
| 168 | .map((att) => `[Pasted Text]\n${att.content}`) |
| 169 | .join('\n\n') |
| 170 | finalContent = content |
| 171 | ? `${content}\n\n${textAttachmentContent}` |
| 172 | : textAttachmentContent |
| 173 | } |
| 174 | |
| 175 | // Append file/folder attachments to the content |
| 176 | if (pendingFileAttachments.length > 0) { |
| 177 | const fileAttachmentContent = pendingFileAttachments |