MCPcopy Index your code
hub / github.com/Effect-TS/effect / make

Function make

packages/ai/google/src/GoogleClient.ts:60–186  ·  view source on GitHub ↗
(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
})

Source from the content-addressed store, hash-verified

58 * @category Constructors
59 */
60export 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({

Callers 2

layerFunction · 0.70
layerConfigFunction · 0.70

Calls 8

fromRequestErrorMethod · 0.80
fromResponseErrorMethod · 0.80
pipeMethod · 0.65
makeMethod · 0.65
mapMethod · 0.65
ofMethod · 0.65
valueMethod · 0.45
fromParseErrorMethod · 0.45

Tested by

no test coverage detected