| 35 | } |
| 36 | |
| 37 | export class Request { |
| 38 | private config: RequestConfig; |
| 39 | |
| 40 | constructor(config: RequestConfig) { |
| 41 | this.config = config; |
| 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}`; |
| 84 | } |
| 85 | |
| 86 | private buildHeaders({ |
| 87 | headers, |
| 88 | }: { |
| 89 | headers?: Record<string, string>; |
| 90 | }): Record<string, string> { |
| 91 | return { |
| 92 | 'Content-Type': 'application/json', |
| 93 | Authorization: `Bearer ${this.config.apiKey}`, |
| 94 | 'LB-LLM-Key': this.config.llmKey ?? '', |
nothing calls this directly
no outgoing calls
no test coverage detected