@@ -50,6 +50,8 @@ export type URLSegments = {
50
50
getFilesPostfix : string ;
51
51
} ;
52
52
53
+ // WORK - need to refactor to be in Responses API
54
+ // https://platform.openai.com/docs/guides/responses-vs-chat-completions
53
55
export class OpenAIAssistantIOI extends DirectServiceIO {
54
56
override insertKeyPlaceholderText = 'OpenAI API Key' ;
55
57
override keyHelpUrl = 'https://platform.openai.com/account/api-keys' ;
@@ -59,30 +61,30 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
59
61
functionHandler ?: AssistantFunctionHandler ;
60
62
filesToolType : OpenAIAssistant [ 'files_tool_type' ] ;
61
63
readonly shouldFetchHistory : boolean = false ;
62
- private messages ?: Messages ;
64
+ private _messages ?: Messages ;
63
65
private run_id ?: string ;
64
- private searchedForThreadId = false ;
65
- private readonly config : OpenAIAssistant = { } ;
66
- private readonly newAssistantDetails : OpenAINewAssistant = { model : 'gpt-4' } ;
67
- private waitingForStreamResponse = false ;
68
- private readonly isSSEStream : boolean = false ;
66
+ private _searchedForThreadId = false ;
67
+ private readonly _config : OpenAIAssistant = { } ;
68
+ private readonly _newAssistantDetails : OpenAINewAssistant = { model : 'gpt-4' } ;
69
+ private _waitingForStreamResponse = false ;
70
+ private readonly _isSSEStream : boolean = false ;
69
71
private readonly urlSegments : URLSegments ;
70
- private messageStream : MessageStream | undefined ;
72
+ private _messageStream : MessageStream | undefined ;
71
73
72
74
// prettier-ignore
73
75
constructor ( deepChat : DeepChat , config : OpenAI [ 'assistant' ] , urlSegments : URLSegments ,
74
76
keyVerificationDetails : KeyVerificationDetails , buildHeadersFunc : BuildHeadersFunc , apiKey ?: APIKey ) {
75
77
super ( deepChat , keyVerificationDetails , buildHeadersFunc , apiKey ) ;
76
78
this . urlSegments = urlSegments ;
77
79
if ( typeof config === 'object' ) {
78
- this . config = config ; // stored that assistant_id could be added
79
- const { new_assistant, thread_id, load_thread_history} = this . config ;
80
- Object . assign ( this . newAssistantDetails , new_assistant ) ;
80
+ this . _config = config ; // stored that assistant_id could be added
81
+ const { new_assistant, thread_id, load_thread_history} = this . _config ;
82
+ Object . assign ( this . _newAssistantDetails , new_assistant ) ;
81
83
if ( thread_id ) this . sessionId = thread_id ;
82
84
if ( load_thread_history ) this . shouldFetchHistory = true ;
83
85
}
84
86
this . maxMessages = 1 ; // messages are stored in OpenAI threads and can't create new thread with 'assistant' messages
85
- this . isSSEStream = Boolean ( this . stream && ( typeof this . stream !== 'object' || ! this . stream . simulation ) ) ;
87
+ this . _isSSEStream = Boolean ( this . stream && ( typeof this . stream !== 'object' || ! this . stream . simulation ) ) ;
86
88
}
87
89
88
90
public async fetchHistoryFunc ( ) {
@@ -176,7 +178,7 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
176
178
}
177
179
178
180
private callService ( messages : Messages , pMessages : MessageContentI [ ] , uploadedFiles ?: UploadedFile [ ] ) {
179
- this . messages = messages ;
181
+ this . _messages = messages ;
180
182
if ( this . sessionId ) {
181
183
// https://platform.openai.com/docs/api-reference/messages/createMessage
182
184
this . url = `${ this . urlSegments . threadsPrefix } /${ this . sessionId } /messages${ this . urlSegments . createMessagePostfix } ` ;
@@ -186,7 +188,7 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
186
188
// https://platform.openai.com/docs/api-reference/runs/createThreadAndRun
187
189
this . url = `${ this . urlSegments . threadsPrefix } /runs${ this . urlSegments . threadsPosfix } ` ;
188
190
const body = this . createNewThreadMessages ( this . rawBody , pMessages , uploadedFiles ) ;
189
- if ( this . isSSEStream ) {
191
+ if ( this . _isSSEStream ) {
190
192
this . createStreamRun ( body ) ;
191
193
} else {
192
194
HTTPRequest . request ( this , body , messages ) ;
@@ -195,11 +197,11 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
195
197
}
196
198
197
199
override async callServiceAPI ( messages : Messages , pMessages : MessageContentI [ ] , files ?: File [ ] ) {
198
- this . waitingForStreamResponse = false ;
200
+ this . _waitingForStreamResponse = false ;
199
201
if ( ! this . connectSettings ) throw new Error ( 'Request settings have not been set up' ) ;
200
- this . rawBody . assistant_id ??= this . config . assistant_id || ( await this . createNewAssistant ( ) ) ;
202
+ this . rawBody . assistant_id ??= this . _config . assistant_id || ( await this . createNewAssistant ( ) ) ;
201
203
// here instead of constructor as messages may be loaded later
202
- if ( ! this . searchedForThreadId ) this . searchPreviousMessagesForThreadId ( messages . messageToElements ) ;
204
+ if ( ! this . _searchedForThreadId ) this . searchPreviousMessagesForThreadId ( messages . messageToElements ) ;
203
205
const uploadedFiles = files
204
206
? await OpenAIAssistantUtils . storeFiles ( this , messages , files , this . urlSegments . storeFiles )
205
207
: undefined ;
@@ -210,9 +212,9 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
210
212
private async createNewAssistant ( ) {
211
213
try {
212
214
this . url = this . urlSegments . newAssistantUrl ;
213
- const result = await OpenAIUtils . directFetch ( this , JSON . parse ( JSON . stringify ( this . newAssistantDetails ) ) , 'POST' ) ;
214
- this . config . assistant_id = ( result as OpenAINewAssistantResult ) . id ;
215
- return this . config . assistant_id ;
215
+ const result = await OpenAIUtils . directFetch ( this , JSON . parse ( JSON . stringify ( this . _newAssistantDetails ) ) , 'POST' ) ;
216
+ this . _config . assistant_id = ( result as OpenAINewAssistantResult ) . id ;
217
+ return this . _config . assistant_id ;
216
218
} catch ( e ) {
217
219
console . error ( e ) ;
218
220
console . error ( 'Failed to create a new assistant' ) ; // letting later calls throw and handle error
@@ -223,13 +225,13 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
223
225
private searchPreviousMessagesForThreadId ( messageToElements : MessageToElements ) {
224
226
const messageWithSession = messageToElements . find ( ( [ msgToEls ] ) => msgToEls . _sessionId ) ;
225
227
if ( messageWithSession ) this . sessionId = messageWithSession [ 0 ] . _sessionId ;
226
- this . searchedForThreadId = true ;
228
+ this . _searchedForThreadId = true ;
227
229
}
228
230
229
231
// prettier-ignore
230
232
override async extractResultData ( result : OpenAIAssistantInitReqResult ) :
231
233
Promise < ResponseI | { makingAnotherRequest : true } > {
232
- if ( this . waitingForStreamResponse || ( this . isSSEStream && this . sessionId ) ) {
234
+ if ( this . _waitingForStreamResponse || ( this . _isSSEStream && this . sessionId ) ) {
233
235
return await this . handleStream ( result ) ;
234
236
}
235
237
if ( result . error ) {
@@ -242,7 +244,7 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
242
244
// https://platform.openai.com/docs/api-reference/runs/getRun
243
245
const url = `${ this . urlSegments . threadsPrefix } /${ this . sessionId } /runs/${ this . run_id } ${ this . urlSegments . threadsPosfix } ` ;
244
246
const requestInit = { method : 'GET' , headers : this . connectSettings ?. headers } ;
245
- HTTPRequest . executePollRequest ( this , url , requestInit , this . messages as Messages ) ; // poll for run status
247
+ HTTPRequest . executePollRequest ( this , url , requestInit , this . _messages as Messages ) ; // poll for run status
246
248
return { makingAnotherRequest : true } ;
247
249
}
248
250
@@ -257,8 +259,8 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
257
259
this . run_id = result . id ;
258
260
// updates the user sent message with the session id (the message event sent did not have this id)
259
261
// user can clear the messages when they make a request, hence checking if messages length > 0
260
- if ( this . messages && this . messages . messageToElements . length > 0 ) {
261
- this . messages . messageToElements [ this . messages . messageToElements . length - 1 ] [ 0 ] . _sessionId = this . sessionId ;
262
+ if ( this . _messages && this . _messages . messageToElements . length > 0 ) {
263
+ this . _messages . messageToElements [ this . _messages . messageToElements . length - 1 ] [ 0 ] . _sessionId = this . sessionId ;
262
264
}
263
265
}
264
266
}
@@ -276,7 +278,7 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
276
278
async extractPollResultData ( result : OpenAIRunResult ) : PollResult {
277
279
const { status, required_action} = result ;
278
280
if ( status === 'queued' || status === 'in_progress' ) return { timeoutMS : OpenAIAssistantIOI . POLLING_TIMEOUT_MS } ;
279
- if ( status === 'completed' && this . messages ) {
281
+ if ( status === 'completed' && this . _messages ) {
280
282
const threadMessages = await this . getThreadMessages ( result . thread_id ) ;
281
283
const { text, files} = threadMessages . shift ( ) as ResponseI ;
282
284
setTimeout ( ( ) => {
@@ -317,7 +319,7 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
317
319
const prefix = `${ this . urlSegments . threadsPrefix } /${ this . sessionId } ` ;
318
320
const postfix = `/runs/${ this . run_id } /submit_tool_outputs${ this . urlSegments . threadsPosfix } ` ;
319
321
this . url = `${ prefix } ${ postfix } ` ;
320
- if ( this . isSSEStream ) {
322
+ if ( this . _isSSEStream ) {
321
323
await this . createStreamRun ( { tool_outputs} ) ;
322
324
} else {
323
325
await OpenAIUtils . directFetch ( this , { tool_outputs} , 'POST' ) ;
@@ -331,10 +333,10 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
331
333
this . run_id = result . id ;
332
334
return await this . handleTools ( toolCalls ) ;
333
335
}
334
- if ( this . waitingForStreamResponse ) {
336
+ if ( this . _waitingForStreamResponse ) {
335
337
return this . parseStreamResult ( result ) ;
336
338
}
337
- if ( this . isSSEStream && this . sessionId ) {
339
+ if ( this . _isSSEStream && this . sessionId ) {
338
340
// https://platform.openai.com/docs/api-reference/runs/createRun
339
341
this . url = `${ this . urlSegments . threadsPrefix } /${ this . sessionId } /runs${ this . urlSegments . threadsPosfix } ` ;
340
342
const newBody = JSON . parse ( JSON . stringify ( this . rawBody ) ) ;
@@ -345,14 +347,14 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
345
347
346
348
// prettier-ignore
347
349
private async parseStreamResult ( result : OpenAIAssistantInitReqResult ) {
348
- if ( result . content && result . content . length > 0 && this . messages ) {
350
+ if ( result . content && result . content . length > 0 && this . _messages ) {
349
351
// if file is included and there is an annotation/link in text, process at the end
350
352
const textContent = result . content . find ( ( content ) => content . text ) ;
351
353
if ( textContent ?. text ?. annotations && textContent . text . annotations . length > 0 ) {
352
354
const textFileFirst = result . content . find ( ( content ) => ! ! content . text ) || result . content [ 0 ] ;
353
355
const downloadCb = OpenAIAssistantUtils . getFilesAndText . bind ( this ,
354
356
this , { role : 'assistant' , content : result . content } , this . urlSegments , textFileFirst ) ;
355
- this . messageStream ?. endStreamAfterFileDownloaded ( this . messages , downloadCb ) ;
357
+ this . _messageStream ?. endStreamAfterFileDownloaded ( this . _messages , downloadCb ) ;
356
358
return { text : '' } ;
357
359
}
358
360
}
@@ -377,7 +379,7 @@ export class OpenAIAssistantIOI extends DirectServiceIO {
377
379
// eslint-disable-next-line @typescript-eslint/no-explicit-any
378
380
private async createStreamRun ( body : any ) {
379
381
body . stream = true ;
380
- this . waitingForStreamResponse = true ;
381
- this . messageStream = ( await Stream . request ( this , body , this . messages as Messages , true , true ) ) as MessageStream ;
382
+ this . _waitingForStreamResponse = true ;
383
+ this . _messageStream = ( await Stream . request ( this , body , this . _messages as Messages , true , true ) ) as MessageStream ;
382
384
}
383
385
}
0 commit comments