(options: {
/**
* The API key to use to communicate with the Google Generative AI API.
*/
readonly apiKey?: Redacted.Redacted | undefined
/**
* The URL to use to communicate with the Google Generative AI API.
*
* Defaults to `"https://generativelanguage.googleapis.com"`.
*/
readonly apiUrl?: string | undefined
/**
* A method which can be used to transform the underlying `HttpClient` which
* will be used to communicate with the Google Generative AI API.
*/
readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined
})
| 58 | * @category Constructors |
| 59 | */ |
| 60 | export const make = (options: { |
| 61 | /** |
| 62 | * The API key to use to communicate with the Google Generative AI API. |
| 63 | */ |
| 64 | readonly apiKey?: Redacted.Redacted | undefined |
| 65 | |
| 66 | /** |
| 67 | * The URL to use to communicate with the Google Generative AI API. |
| 68 | * |
| 69 | * Defaults to `"https://generativelanguage.googleapis.com"`. |
| 70 | */ |
| 71 | readonly apiUrl?: string | undefined |
| 72 | |
| 73 | /** |
| 74 | * A method which can be used to transform the underlying `HttpClient` which |
| 75 | * will be used to communicate with the Google Generative AI API. |
| 76 | */ |
| 77 | readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined |
| 78 | }): Effect.Effect<Service, never, HttpClient.HttpClient | Scope.Scope> => |
| 79 | Effect.gen(function*() { |
| 80 | const apiKeyHeader = "x-goog-api-key" |
| 81 | |
| 82 | yield* Effect.locallyScopedWith(Headers.currentRedactedNames, Arr.append(apiKeyHeader)) |
| 83 | |
| 84 | const httpClient = (yield* HttpClient.HttpClient).pipe( |
| 85 | HttpClient.mapRequest((request) => |
| 86 | request.pipe( |
| 87 | HttpClientRequest.prependUrl(options.apiUrl ?? "https://generativelanguage.googleapis.com"), |
| 88 | options.apiKey ? HttpClientRequest.setHeader(apiKeyHeader, Redacted.value(options.apiKey)) : identity, |
| 89 | HttpClientRequest.acceptJson |
| 90 | ) |
| 91 | ), |
| 92 | options.transformClient ? options.transformClient : identity |
| 93 | ) |
| 94 | |
| 95 | const httpClientOk = HttpClient.filterStatusOk(httpClient) |
| 96 | |
| 97 | const client = Generated.make(httpClient, { |
| 98 | transformClient: (client) => |
| 99 | GoogleConfig.getOrUndefined.pipe( |
| 100 | Effect.map((config) => config?.transformClient ? config.transformClient(client) : client) |
| 101 | ) |
| 102 | }) |
| 103 | |
| 104 | const streamRequest = <A, I, R>( |
| 105 | request: HttpClientRequest.HttpClientRequest, |
| 106 | schema: Schema.Schema<A, I, R> |
| 107 | ): Stream.Stream<A, AiError.AiError, R> => { |
| 108 | const decodeEvents = Schema.decode(Schema.ChunkFromSelf(Schema.parseJson(schema))) |
| 109 | return httpClientOk.execute(request).pipe( |
| 110 | Effect.map((r) => r.stream), |
| 111 | Stream.unwrap, |
| 112 | Stream.decodeText(), |
| 113 | Stream.pipeThroughChannel(Sse.makeChannel()), |
| 114 | Stream.mapChunksEffect((chunk) => decodeEvents(Chunk.map(chunk, (event) => event.data))), |
| 115 | Stream.catchTags({ |
| 116 | RequestError: (error) => |
| 117 | AiError.HttpRequestError.fromRequestError({ |
no test coverage detected