Skip to content

Commit 474d425

Browse files
committed
export image support for json and jsonl
1 parent bb7d65f commit 474d425

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

server/utils/helpers/chat/convertTo.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ async function prepareChatsForExport(format = "jsonl", chatType = "workspace") {
7272
sent_at: chat.createdAt,
7373
};
7474

75+
// Only add images for JSON format since we cannot
76+
// make an array of images in csv
77+
if (format === "json") {
78+
const attachments = responseJson.attachments || [];
79+
baseData.images = attachments.map(attachmentToDataUrl);
80+
}
81+
7582
if (chatType === "embed") {
7683
return {
7784
...baseData,
@@ -120,28 +127,55 @@ async function prepareChatsForExport(format = "jsonl", chatType = "workspace") {
120127
const workspaceChatsMap = chats.reduce((acc, chat) => {
121128
const { prompt, response, workspaceId } = chat;
122129
const responseJson = JSON.parse(response);
130+
const attachments = responseJson.attachments || [];
123131

124132
if (!acc[workspaceId]) {
125133
acc[workspaceId] = {
126134
messages: [
127135
{
128136
role: "system",
129-
content:
130-
chat.workspace?.openAiPrompt ||
131-
"Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed.",
137+
content: [
138+
{
139+
type: "text",
140+
text:
141+
chat.workspace?.openAiPrompt ||
142+
"Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed.",
143+
},
144+
],
132145
},
133146
],
134147
};
135148
}
136149

150+
const userContent = [
151+
{
152+
type: "text",
153+
text: prompt,
154+
},
155+
];
156+
157+
if (attachments.length > 0) {
158+
attachments.forEach((attachment) => {
159+
userContent.push({
160+
type: "image",
161+
image: attachmentToDataUrl(attachment),
162+
});
163+
});
164+
}
165+
137166
acc[workspaceId].messages.push(
138167
{
139168
role: "user",
140-
content: prompt,
169+
content: userContent,
141170
},
142171
{
143172
role: "assistant",
144-
content: responseJson.text,
173+
content: [
174+
{
175+
type: "text",
176+
text: responseJson.text,
177+
},
178+
],
145179
}
146180
);
147181

@@ -203,6 +237,17 @@ function buildSystemPrompt(chat, prompt = null) {
203237
return `${prompt ?? STANDARD_PROMPT}${context}`;
204238
}
205239

240+
/**
241+
* Converts an attachment's content string to a proper data URL format if needed
242+
* @param {Object} attachment - The attachment object containing contentString and mime type
243+
* @returns {string} The properly formatted data URL
244+
*/
245+
function attachmentToDataUrl(attachment) {
246+
return attachment.contentString.startsWith("data:")
247+
? attachment.contentString
248+
: `data:${attachment.mime};base64,${attachment.contentString}`;
249+
}
250+
206251
module.exports = {
207252
prepareChatsForExport,
208253
exportChatsAsType,

0 commit comments

Comments
 (0)