@@ -85,25 +85,7 @@ async function chatReplyProcess(options: RequestOptions) {
85
85
}
86
86
87
87
// Prepare the user message content (text and images)
88
- let content : string | OpenAI . Chat . ChatCompletionContentPart [ ] = message
89
-
90
- // Handle image uploads if present
91
- if ( uploadFileKeys && uploadFileKeys . length > 0 ) {
92
- content = [
93
- {
94
- type : 'text' ,
95
- text : message ,
96
- } ,
97
- ]
98
- for ( const uploadFileKey of uploadFileKeys ) {
99
- content . push ( {
100
- type : 'image_url' ,
101
- image_url : {
102
- url : await convertImageUrl ( uploadFileKey ) ,
103
- } ,
104
- } )
105
- }
106
- }
88
+ const content : string | OpenAI . Chat . ChatCompletionContentPart [ ] = await createContent ( message , uploadFileKeys )
107
89
108
90
// Add the user message
109
91
messages . push ( {
@@ -244,26 +226,7 @@ async function getMessageById(id: string): Promise<ChatMessage | undefined> {
244
226
}
245
227
else {
246
228
if ( isPrompt ) { // prompt
247
- let content : string | OpenAI . Chat . ChatCompletionContentPart [ ] = chatInfo . prompt
248
- if ( chatInfo . images && chatInfo . images . length > 0 ) {
249
- content = [
250
- {
251
- type : 'text' ,
252
- text : chatInfo . prompt ,
253
- } ,
254
- ]
255
- for ( const image of chatInfo . images ) {
256
- const imageUrlBase64 = await convertImageUrl ( image )
257
- if ( imageUrlBase64 ) {
258
- content . push ( {
259
- type : 'image_url' ,
260
- image_url : {
261
- url : await convertImageUrl ( image ) ,
262
- } ,
263
- } )
264
- }
265
- }
266
- }
229
+ const content : string | OpenAI . Chat . ChatCompletionContentPart [ ] = await createContent ( chatInfo . prompt , chatInfo . images )
267
230
return {
268
231
id,
269
232
parentMessageId,
@@ -311,6 +274,35 @@ async function getRandomApiKey(user: UserInfo, chatModel: string): Promise<KeyCo
311
274
return randomKeyConfig ( keys )
312
275
}
313
276
277
+ // Helper function to create content with text and optional images
278
+ async function createContent ( text : string , images ?: string [ ] ) : Promise < string | OpenAI . Chat . ChatCompletionContentPart [ ] > {
279
+ // If no images or empty array, return just the text
280
+ if ( ! images || images . length === 0 )
281
+ return text
282
+
283
+ // Create content with text and images
284
+ const content : OpenAI . Chat . ChatCompletionContentPart [ ] = [
285
+ {
286
+ type : 'text' ,
287
+ text,
288
+ } ,
289
+ ]
290
+
291
+ for ( const image of images ) {
292
+ const imageUrl = await convertImageUrl ( image )
293
+ if ( imageUrl ) {
294
+ content . push ( {
295
+ type : 'image_url' ,
296
+ image_url : {
297
+ url : imageUrl ,
298
+ } ,
299
+ } )
300
+ }
301
+ }
302
+
303
+ return content
304
+ }
305
+
314
306
// Helper function to add previous messages to the conversation context
315
307
async function addPreviousMessages ( parentMessageId : string , maxContextCount : number , messages : OpenAI . Chat . ChatCompletionMessageParam [ ] ) : Promise < void > {
316
308
// Recursively get previous messages
0 commit comments