this workaround using the below in my PATCH works, but breaks another part of my functionality so don’t want to have to use it…
// Encode special characters before saving
const encodedMessages = messages.map(msg => ({
...msg,
content: msg.content.replace(/\?/g, '{{QUESTION_MARK}}')
}));
const updateBody = {
Id: conversationId,
messages: JSON.stringify(encodedMessages),
};
and this in my GET:
// Parse and decode messages
let decodedMessages;
if (typeof conversation.messages === 'string') {
decodedMessages = JSON.parse(conversation.messages);
} else {
decodedMessages = conversation.messages;
}
// Decode the special characters
decodedMessages = decodedMessages.map(msg => ({
...msg,
content: msg.content.replace(/{{QUESTION_MARK}}/g, '?')
}));
conversation.messages = decodedMessages;
return NextResponse.json({ conversation });
This is my PATCH that’s hitting the issues without the above included:
export async function PATCH(req) {
const body = await req.json();
console.log('PATCH request body:', JSON.stringify(body, null, 2));
const { conversationId, messages } = body;
if (!conversationId) {
console.error('conversationId is missing in the PATCH request');
return NextResponse.json({ error: 'conversationId is required' }, { status: 400 });
}
try {
console.log(`Updating conversation with ID: ${conversationId}`);
console.log('Messages to be updated:', JSON.stringify(messages, null, 2));
// Replace dollar signs with question marks
const fixedMessages = messages.map(msg => ({
...msg,
content: msg.content.replace(/\$/g, '?')
}));
const updateBody = {
Id: conversationId,
messages: JSON.stringify(fixedMessages),
};
console.log('Update request body:', JSON.stringify(updateBody, null, 2));
const updateConversationResponse = await fetch(`${BASE_URL}/${CONVERSATIONS_TABLE_ID}/records`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'xc-token': process.env.NEXT_PUBLIC_NOCODB_AUTH_TOKEN,
},
body: JSON.stringify(updateBody),
});
console.log('Update response status:', updateConversationResponse.status);
const updateResponseText = await updateConversationResponse.text();
console.log('Update response body as text:', updateResponseText);
if (!updateConversationResponse.ok) {
throw new Error(`NocoDB API responded with status ${updateConversationResponse.status}: ${updateResponseText}`);
}
// Fetch the updated conversation to verify the changes
const fetchUpdatedConversationResponse = await fetch(`${BASE_URL}/${CONVERSATIONS_TABLE_ID}/records/${conversationId}`, {
headers: {
'xc-token': process.env.NEXT_PUBLIC_NOCODB_AUTH_TOKEN,
},
});
if (!fetchUpdatedConversationResponse.ok) {
throw new Error(`Failed to fetch updated conversation: ${fetchUpdatedConversationResponse.status}`);
}
const updatedConversation = await fetchUpdatedConversationResponse.json();
console.log('Updated conversation:', JSON.stringify(updatedConversation, null, 2));
// Parse messages if they're stored as a string
let savedMessages;
if (typeof updatedConversation.messages === 'string') {
savedMessages = JSON.parse(updatedConversation.messages);
} else {
savedMessages = updatedConversation.messages;
}
console.log('Saved messages:', JSON.stringify(savedMessages, null, 2));
return NextResponse.json({ success: true, updatedConversation: { ...updatedConversation, messages: savedMessages } });
} catch (error) {
console.error('Error updating conversation:', error);
return NextResponse.json({ error: 'Failed to update conversation', details: error.message }, { status: 500 });
}
}