@@ -16,6 +16,8 @@ import { FileSearchTool, FunctionTool, ResponseInput, ResponseInputItem, Respons
16
16
import { courseAssistants , type CourseAssistant } from './courseAssistants'
17
17
import { createFileSearchTool } from './util'
18
18
19
+ import type { ResponseStreamValue } from '../../../shared/types'
20
+
19
21
const endpoint = `https://${ AZURE_RESOURCE } .openai.azure.com/`
20
22
21
23
export const getAzureOpenAIClient = ( deployment : string ) =>
@@ -48,9 +50,6 @@ export class ResponsesClient {
48
50
courseAssistant = courseAssistants . find ( ( assistant ) => assistant . name === 'default' )
49
51
}
50
52
51
- this . model = deploymentId
52
- this . instructions = courseAssistant . assistant_instruction
53
-
54
53
const fileSearchTool = courseId
55
54
? [
56
55
createFileSearchTool ( {
@@ -59,14 +58,16 @@ export class ResponsesClient {
59
58
]
60
59
: [ ] // needs to retrun empty array for null
61
60
61
+ this . model = deploymentId
62
+ this . instructions = courseAssistant . assistant_instruction
62
63
this . tools = fileSearchTool
63
64
}
64
65
65
- async createResponse ( { input } : { input : ResponseInput } ) : Promise < Stream < ResponseStreamEvent > | APIError > {
66
+ async createResponse ( { input, prevResponseId } : { input : ResponseInput ; prevResponseId ?: string } ) : Promise < Stream < ResponseStreamEvent > | APIError > {
66
67
try {
67
68
return await client . responses . create ( {
68
69
model : this . model ,
69
- // previous_response_id=response.id // THIS MIGHT BE IT!!!!!!1
70
+ previous_response_id : prevResponseId ,
70
71
instructions : this . instructions ,
71
72
input,
72
73
stream : true ,
@@ -89,7 +90,14 @@ export class ResponsesClient {
89
90
90
91
switch ( event . type ) {
91
92
case 'response.output_text.delta' :
92
- await this . writeDelta ( event . delta , res )
93
+ await this . write (
94
+ {
95
+ status : 'writing' ,
96
+ text : event . delta ,
97
+ prevResponseId : null ,
98
+ } ,
99
+ res ,
100
+ )
93
101
94
102
contents . push ( event . delta )
95
103
tokenCount += encoding . encode ( event . delta ) . length ?? 0
@@ -107,8 +115,15 @@ export class ResponsesClient {
107
115
console . log ( 'ANNOTATIONS ADDED' , JSON . stringify ( event , null , 2 ) )
108
116
break
109
117
110
- case 'response.function_call_arguments.done' :
111
- // Listen to file_search instead
118
+ case 'response.completed' :
119
+ await this . write (
120
+ {
121
+ status : 'complete' ,
122
+ text : null ,
123
+ prevResponseId : event . response . id ,
124
+ } ,
125
+ res ,
126
+ )
112
127
break
113
128
}
114
129
}
@@ -119,16 +134,24 @@ export class ResponsesClient {
119
134
}
120
135
}
121
136
122
- private async writeDelta ( text : string , res : Response ) {
123
- // if (!inProduction) logger.info(text )
137
+ private async write ( { status , text, prevResponseId } : ResponseStreamValue , res : Response ) {
138
+ // if (!inProduction) logger.info(message )
124
139
125
140
await new Promise ( ( resolve ) => {
126
- if (
127
- ! res . write ( text , ( err ) => {
128
- if ( err ) logger . error ( `${ text } ${ err } ` )
129
- } )
130
- ) {
131
- logger . info ( `${ text } res.write returned false, waiting for drain` )
141
+ const data : ResponseStreamValue = {
142
+ status,
143
+ text,
144
+ prevResponseId : prevResponseId ,
145
+ }
146
+
147
+ const success = res . write ( JSON . stringify ( data ) + '\n' , ( err ) => {
148
+ if ( err ) {
149
+ logger . error ( err )
150
+ }
151
+ } )
152
+
153
+ if ( ! success ) {
154
+ logger . info ( 'res.write returned false, waiting for drain' )
132
155
res . once ( 'drain' , resolve )
133
156
} else {
134
157
process . nextTick ( resolve )
0 commit comments