This commit is contained in:
seb
2026-10-06 11:40:02 +02:00
parent 03608017f3
commit 4769a87c54
6 changed files with 1197 additions and 20 deletions

View File

@@ -962,11 +962,22 @@ const actions = {
/**
* Send a user message via POST /api/modelconversation/:id/message.
* Rejects immediately if conversation is busy (409 Conflict avoidance).
* @param {string} text
* @param {string|{ text?: string, attachments?: object[] }} textOrPayload
* @param {object[]} [maybeAttachments]
*/
async sendMessage(text) {
const trimmed = text?.trim();
if (!trimmed) return;
async sendMessage(textOrPayload, maybeAttachments = []) {
let trimmed = '';
let fileAttachments = [];
if (typeof textOrPayload === 'object' && textOrPayload !== null) {
trimmed = (textOrPayload.text || '').trim();
fileAttachments = Array.isArray(textOrPayload.attachments) ? textOrPayload.attachments : [];
} else {
trimmed = (textOrPayload || '').trim();
fileAttachments = Array.isArray(maybeAttachments) ? maybeAttachments : [];
}
if (!trimmed && fileAttachments.length === 0) return;
if (!chatAutomat.state.selectedConversationId && chatAutomat.state.draftConversation) {
// Draft: the conversation really starts now, with the first message.
@@ -983,11 +994,17 @@ const actions = {
}
const tempId = `temp-${Date.now()}`;
const payloadMessage = fileAttachments.length > 0
? JSON.stringify({ content: trimmed, attachments: fileAttachments })
: trimmed;
const optimisticMsg = {
id: tempId,
conversation_id: selectedConversationId,
role: 'user',
message: trimmed,
message: payloadMessage,
content: trimmed,
attachments: fileAttachments,
created_at: new Date().toISOString(),
};
@@ -1014,8 +1031,14 @@ const actions = {
}));
try {
const res = await modelconversationApi.addMessage(selectedConversationId, { message: trimmed });
const res = await modelconversationApi.addMessage(selectedConversationId, {
message: payloadMessage,
attachments: fileAttachments.length > 0 ? fileAttachments : undefined,
});
const createdMsg = res?.message || optimisticMsg;
if (!createdMsg.attachments && fileAttachments.length > 0) {
createdMsg.attachments = fileAttachments;
}
chatAutomat.setState((prev) => {
const alreadyHasRealId = prev.messages.some((m) => m.id === createdMsg.id && m.id !== tempId);
if (alreadyHasRealId) {