( options: RunOptionsStream, response: RunResponseStream, )
| 178 | } |
| 179 | |
| 180 | private async handleStreamResponse( |
| 181 | options: RunOptionsStream, |
| 182 | response: RunResponseStream, |
| 183 | ): Promise<RunResponseStream> { |
| 184 | const endpoint = '/v1/pipes/run'; |
| 185 | const stream = this.isStreamRequested(options); |
| 186 | const body = {...options, stream}; |
| 187 | |
| 188 | const [streamForToolCall, streamForReturn] = response.stream.tee(); |
| 189 | const tools = await getToolsFromStream(streamForToolCall); |
| 190 | |
| 191 | if (tools.length) { |
| 192 | let messages = options.messages || []; |
| 193 | |
| 194 | let currentResponse: RunResponseStream = { |
| 195 | stream: streamForReturn, |
| 196 | threadId: response.threadId, |
| 197 | rawResponse: response.rawResponse, |
| 198 | }; |
| 199 | |
| 200 | let callCount = 0; |
| 201 | |
| 202 | while (callCount < this.maxCalls) { |
| 203 | const [streamForToolCall, streamForReturn] = |
| 204 | currentResponse.stream.tee(); |
| 205 | |
| 206 | const tools = await getToolsFromStream(streamForToolCall); |
| 207 | |
| 208 | if (tools.length === 0) { |
| 209 | return { |
| 210 | stream: streamForReturn, |
| 211 | threadId: currentResponse.threadId, |
| 212 | rawResponse: response.rawResponse, |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | const toolResults = await this.runTools(tools); |
| 217 | |
| 218 | const responseMessage = { |
| 219 | role: 'assistant', |
| 220 | content: null, |
| 221 | tool_calls: tools, |
| 222 | } as Message; |
| 223 | |
| 224 | messages = this.getMessagesToSend( |
| 225 | messages, |
| 226 | responseMessage, |
| 227 | toolResults, |
| 228 | ); |
| 229 | |
| 230 | currentResponse = await this.createRequest<RunResponseStream>( |
| 231 | endpoint, |
| 232 | { |
| 233 | ...body, |
| 234 | messages, |
| 235 | threadId: currentResponse.threadId, |
| 236 | }, |
| 237 | ); |
no test coverage detected