@@ -46,16 +46,20 @@ export async function POST(req: Request) {
46
46
openaiKey : string
47
47
} = await req . json ( )
48
48
49
- let decryptedKey = openaiKey ?
50
- await decryptKeyIfNeeded ( openaiKey ) :
51
- process . env . VLADS_OPENAI_KEY
49
+ // console.log('Received request with openaiKey:', openaiKey)
50
+ let decryptedKey = openaiKey
51
+ ? await decryptKeyIfNeeded ( openaiKey )
52
+ : process . env . VLADS_OPENAI_KEY
52
53
53
54
if ( ! decryptedKey ?. startsWith ( 'sk-' ) ) {
54
55
decryptedKey = process . env . VLADS_OPENAI_KEY as string
55
56
}
56
57
58
+ // console.log('Using key for function calling:', decryptedKey)
59
+
57
60
// Format messages
58
- const message_to_send : ChatCompletionMessageParam [ ] = conversationToMessages ( conversation )
61
+ const message_to_send : ChatCompletionMessageParam [ ] =
62
+ conversationToMessages ( conversation )
59
63
60
64
// Add system message
61
65
const globalToolsSytemPromptPrefix =
@@ -97,70 +101,80 @@ export async function POST(req: Request) {
97
101
} )
98
102
99
103
if ( ! response . ok ) {
104
+ console . log ( 'OpenAI API error:' , response . status , response . statusText )
100
105
return new Response (
101
- JSON . stringify ( { error : `OpenAI API error: ${ response . status } ` } ) ,
102
- {
106
+ JSON . stringify ( { error : `OpenAI API error: ${ response . status } ` } ) ,
107
+ {
103
108
status : response . status ,
104
- headers : { 'Content-Type' : 'application/json' }
105
- }
109
+ headers : { 'Content-Type' : 'application/json' } ,
110
+ } ,
106
111
)
107
112
}
108
113
109
114
const data = await response . json ( )
110
115
111
116
if ( ! data . choices ) {
117
+ console . log ( 'No response from OpenAI' )
112
118
return new Response (
113
- JSON . stringify ( { error : 'No response from OpenAI' } ) ,
114
- {
119
+ JSON . stringify ( { error : 'No response from OpenAI' } ) ,
120
+ {
115
121
status : 500 ,
116
- headers : { 'Content-Type' : 'application/json' }
117
- }
122
+ headers : { 'Content-Type' : 'application/json' } ,
123
+ } ,
118
124
)
119
125
}
120
126
121
127
if ( ! data . choices [ 0 ] ?. message . tool_calls ) {
128
+ console . log ( 'No tool calls from OpenAI' )
122
129
return new Response (
123
130
JSON . stringify ( {
124
- choices : [ {
125
- message : {
126
- content : 'No tools invoked by OpenAI' ,
127
- role : 'assistant'
128
- }
129
- } ]
131
+ choices : [
132
+ {
133
+ message : {
134
+ content : 'No tools invoked by OpenAI' ,
135
+ role : 'assistant' ,
136
+ } ,
137
+ } ,
138
+ ] ,
130
139
} ) ,
131
140
{
132
141
status : 200 ,
133
- headers : { 'Content-Type' : 'application/json' }
134
- }
142
+ headers : { 'Content-Type' : 'application/json' } ,
143
+ } ,
135
144
)
136
145
}
137
146
138
147
const toolCalls = data . choices [ 0 ] . message . tool_calls
139
148
140
149
return new Response (
141
150
JSON . stringify ( {
142
- choices : [ {
143
- message : {
144
- content : JSON . stringify ( toolCalls ) ,
145
- role : 'assistant' ,
146
- tool_calls : toolCalls
147
- }
148
- } ]
151
+ choices : [
152
+ {
153
+ message : {
154
+ content : JSON . stringify ( toolCalls ) ,
155
+ role : 'assistant' ,
156
+ tool_calls : toolCalls ,
157
+ } ,
158
+ } ,
159
+ ] ,
149
160
} ) ,
150
161
{
151
162
status : 200 ,
152
- headers : { 'Content-Type' : 'application/json' }
153
- }
163
+ headers : { 'Content-Type' : 'application/json' } ,
164
+ } ,
154
165
)
155
166
} catch ( error ) {
156
167
return new Response (
157
- JSON . stringify ( {
158
- error : error instanceof Error ? error . message : 'An unexpected error occurred'
168
+ JSON . stringify ( {
169
+ error :
170
+ error instanceof Error
171
+ ? error . message
172
+ : 'An unexpected error occurred' ,
159
173
} ) ,
160
- {
174
+ {
161
175
status : 500 ,
162
- headers : { 'Content-Type' : 'application/json' }
163
- }
176
+ headers : { 'Content-Type' : 'application/json' } ,
177
+ } ,
164
178
)
165
179
}
166
180
}
0 commit comments