({endpoint, ...options}: SendOptions)
| 42 | } |
| 43 | |
| 44 | private async send<T>({endpoint, ...options}: SendOptions): Promise<T> { |
| 45 | const url = this.buildUrl({endpoint}); |
| 46 | const headers = this.buildHeaders({headers: options.headers}); |
| 47 | |
| 48 | let response: Response; |
| 49 | try { |
| 50 | response = await this.makeRequest({ |
| 51 | url, |
| 52 | options: {...options, endpoint}, |
| 53 | headers, |
| 54 | }); |
| 55 | } catch (error) { |
| 56 | throw new APIConnectionError({ |
| 57 | cause: error instanceof Error ? error : undefined, |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | if (!response.ok) { |
| 62 | await this.handleErrorResponse({response}); |
| 63 | } |
| 64 | |
| 65 | const threadId = response.headers.get('lb-thread-id'); |
| 66 | |
| 67 | if (options.body?.stream) { |
| 68 | return handleResponseStream({ |
| 69 | response, |
| 70 | rawResponse: options.body.rawResponse, |
| 71 | }) as T; |
| 72 | } |
| 73 | |
| 74 | return this.handleRunResponse({ |
| 75 | response, |
| 76 | isChat: options.body?.chat, |
| 77 | threadId, |
| 78 | rawResponse: options.body?.rawResponse ?? false, |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | private buildUrl({endpoint}: {endpoint: string}): string { |
| 83 | return `${this.config.baseUrl}${endpoint}`; |
no test coverage detected