@@ -28,7 +28,13 @@ interface OpenAIRequest {
2828 model : string ;
2929 messages : Array < {
3030 role : 'user' | 'system' | 'assistant' ;
31- content : string ;
31+ content : string | Array < {
32+ type : 'text' | 'image_url' ;
33+ text ?: string ;
34+ image_url ?: {
35+ url : string ;
36+ } ;
37+ } > ;
3238 } > ;
3339 stream : boolean ;
3440 max_tokens ?: number ;
@@ -68,14 +74,47 @@ export async function handleQuestion(question: string, context: string, model?:
6874
6975 await addMessage ( 'user' , finalQuestion ) ;
7076
71- const systemMessage = context ?
72- `You are analyzing the following content:\n\n${ context } ` :
73- 'You are analyzing the current webpage.' ;
74-
7577 // Create placeholder message for streaming
7678 const selectedModel = model || settings . model ;
7779 await addMessage ( 'assistant' , '' , selectedModel ) ;
7880
81+ // Prepare messages based on whether we have an image or text
82+ const messages = [ ] ;
83+
84+ // Add system message
85+ if ( context . startsWith ( 'data:image' ) ) {
86+ messages . push ( {
87+ role : 'system' ,
88+ content : 'You are analyzing the provided image. Be specific and detailed in your observations.'
89+ } ) ;
90+ // Add user message with image
91+ messages . push ( {
92+ role : 'user' ,
93+ content : [
94+ {
95+ type : 'image_url' ,
96+ image_url : {
97+ url : context
98+ }
99+ } ,
100+ {
101+ type : 'text' ,
102+ text : finalQuestion
103+ }
104+ ]
105+ } ) ;
106+ } else {
107+ messages . push ( {
108+ role : 'system' ,
109+ content : context ? `You are analyzing the following content:\n\n${ context } ` : 'You are analyzing the current webpage.'
110+ } ) ;
111+ // Add user message with text
112+ messages . push ( {
113+ role : 'user' ,
114+ content : finalQuestion
115+ } ) ;
116+ }
117+
79118 // First make a non-streaming request to get token usage
80119 const nonStreamingResponse = await fetch ( `${ settings . apiUrl } /chat/completions` , {
81120 method : 'POST' ,
@@ -85,10 +124,7 @@ export async function handleQuestion(question: string, context: string, model?:
85124 } ,
86125 body : JSON . stringify ( {
87126 model : selectedModel ,
88- messages : [
89- { role : 'system' , content : systemMessage } ,
90- { role : 'user' , content : finalQuestion }
91- ] ,
127+ messages,
92128 stream : false
93129 } as OpenAIRequest )
94130 } ) ;
@@ -110,10 +146,7 @@ export async function handleQuestion(question: string, context: string, model?:
110146 } ,
111147 body : JSON . stringify ( {
112148 model : selectedModel ,
113- messages : [
114- { role : 'system' , content : systemMessage } ,
115- { role : 'user' , content : finalQuestion }
116- ] ,
149+ messages,
117150 stream : true
118151 } as OpenAIRequest )
119152 } ) ;
0 commit comments