|
| 1 | +/** |
| 2 | + * @since 1.0.0 |
| 3 | + */ |
| 4 | +import * as AiError from "@effect/ai/AiError" |
| 5 | +import * as Headers from "@effect/platform/Headers" |
| 6 | +import * as HttpClient from "@effect/platform/HttpClient" |
| 7 | +import * as HttpClientRequest from "@effect/platform/HttpClientRequest" |
| 8 | +import * as HttpClientResponse from "@effect/platform/HttpClientResponse" |
| 9 | +import * as Arr from "effect/Array" |
| 10 | +import * as Config from "effect/Config" |
| 11 | +import type { ConfigError } from "effect/ConfigError" |
| 12 | +import * as Context from "effect/Context" |
| 13 | +import * as Effect from "effect/Effect" |
| 14 | +import { identity } from "effect/Function" |
| 15 | +import * as Layer from "effect/Layer" |
| 16 | +import * as Redacted from "effect/Redacted" |
| 17 | +import type * as Schema from "effect/Schema" |
| 18 | +import type * as Scope from "effect/Scope" |
| 19 | + |
| 20 | +/** |
| 21 | + * @since 1.0.0 |
| 22 | + * @category Context |
| 23 | + */ |
| 24 | +export class PerplexityClient extends Context.Tag( |
| 25 | + "@effect/ai-perplexity/PerplexityClient" |
| 26 | +)<PerplexityClient, Service>() {} |
| 27 | + |
| 28 | +/** |
| 29 | + * Represents the interface that the `PerplexityClient` service provides. |
| 30 | + * |
| 31 | + * This service abstracts the complexity of communicating with the Perplexity |
| 32 | + * Search API. It exposes the underlying HTTP client (already configured with |
| 33 | + * authentication and the Perplexity base URL) plus a high-level helper for |
| 34 | + * decoding JSON responses into a schema. |
| 35 | + * |
| 36 | + * @since 1.0.0 |
| 37 | + * @category Models |
| 38 | + */ |
| 39 | +export interface Service { |
| 40 | + /** |
| 41 | + * The underlying HTTP client capable of communicating with the Perplexity |
| 42 | + * API. Pre-configured with authentication (`Authorization: Bearer ...`) and |
| 43 | + * the API base URL. |
| 44 | + */ |
| 45 | + readonly httpClient: HttpClient.HttpClient |
| 46 | + |
| 47 | + /** |
| 48 | + * Execute a request and decode the JSON response body using the supplied |
| 49 | + * schema. Maps platform `HttpClient` errors to `@effect/ai` `AiError`s. |
| 50 | + */ |
| 51 | + readonly executeRequest: <A, I, R>( |
| 52 | + request: HttpClientRequest.HttpClientRequest, |
| 53 | + schema: Schema.Schema<A, I, R>, |
| 54 | + method: string |
| 55 | + ) => Effect.Effect<A, AiError.AiError, R> |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * @since 1.0.0 |
| 60 | + * @category Constructors |
| 61 | + */ |
| 62 | +export const make = (options: { |
| 63 | + /** |
| 64 | + * The API key used to authenticate with the Perplexity API. |
| 65 | + * |
| 66 | + * Wrapped in `Redacted` to avoid accidental logging. Sent as a Bearer token |
| 67 | + * in the `Authorization` header on every request. |
| 68 | + */ |
| 69 | + readonly apiKey?: Redacted.Redacted | undefined |
| 70 | + /** |
| 71 | + * The base URL of the Perplexity API. Defaults to `https://api.perplexity.ai`. |
| 72 | + */ |
| 73 | + readonly apiUrl?: string | undefined |
| 74 | + /** |
| 75 | + * Optional transform applied to the underlying HTTP client (e.g. to add |
| 76 | + * middleware, logging, retries). |
| 77 | + */ |
| 78 | + readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined |
| 79 | +}): Effect.Effect<Service, never, HttpClient.HttpClient | Scope.Scope> => |
| 80 | + Effect.gen(function*() { |
| 81 | + const authHeader = "authorization" |
| 82 | + |
| 83 | + yield* Effect.locallyScopedWith(Headers.currentRedactedNames, Arr.append(authHeader)) |
| 84 | + |
| 85 | + const httpClient = (yield* HttpClient.HttpClient).pipe( |
| 86 | + HttpClient.mapRequest((request) => |
| 87 | + request.pipe( |
| 88 | + HttpClientRequest.prependUrl(options.apiUrl ?? "https://api.perplexity.ai"), |
| 89 | + options.apiKey |
| 90 | + ? HttpClientRequest.setHeader(authHeader, `Bearer ${Redacted.value(options.apiKey)}`) |
| 91 | + : identity, |
| 92 | + HttpClientRequest.acceptJson |
| 93 | + ) |
| 94 | + ), |
| 95 | + options.transformClient ? options.transformClient : identity |
| 96 | + ) |
| 97 | + |
| 98 | + const httpClientOk = HttpClient.filterStatusOk(httpClient) |
| 99 | + |
| 100 | + const executeRequest = <A, I, R>( |
| 101 | + request: HttpClientRequest.HttpClientRequest, |
| 102 | + schema: Schema.Schema<A, I, R>, |
| 103 | + method: string |
| 104 | + ): Effect.Effect<A, AiError.AiError, R> => |
| 105 | + httpClientOk.execute(request).pipe( |
| 106 | + Effect.flatMap(HttpClientResponse.schemaBodyJson(schema)), |
| 107 | + Effect.catchTags({ |
| 108 | + RequestError: (error) => |
| 109 | + AiError.HttpRequestError.fromRequestError({ |
| 110 | + module: "PerplexityClient", |
| 111 | + method, |
| 112 | + error |
| 113 | + }), |
| 114 | + ResponseError: (error) => |
| 115 | + AiError.HttpResponseError.fromResponseError({ |
| 116 | + module: "PerplexityClient", |
| 117 | + method, |
| 118 | + error |
| 119 | + }), |
| 120 | + ParseError: (error) => |
| 121 | + Effect.fail( |
| 122 | + new AiError.MalformedOutput({ |
| 123 | + module: "PerplexityClient", |
| 124 | + method, |
| 125 | + description: `Failed to decode response body: ${error.message}`, |
| 126 | + cause: error |
| 127 | + }) |
| 128 | + ) |
| 129 | + }) |
| 130 | + ) |
| 131 | + |
| 132 | + return PerplexityClient.of({ |
| 133 | + httpClient, |
| 134 | + executeRequest |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | +/** |
| 139 | + * @since 1.0.0 |
| 140 | + * @category Layers |
| 141 | + */ |
| 142 | +export const layer = (options: { |
| 143 | + readonly apiKey?: Redacted.Redacted | undefined |
| 144 | + readonly apiUrl?: string | undefined |
| 145 | + readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined |
| 146 | +}): Layer.Layer<PerplexityClient, never, HttpClient.HttpClient> => Layer.scoped(PerplexityClient, make(options)) |
| 147 | + |
| 148 | +/** |
| 149 | + * Build a `PerplexityClient` layer that reads its API key from environment |
| 150 | + * configuration. Looks for `PERPLEXITY_API_KEY` first and falls back to |
| 151 | + * `PPLX_API_KEY`. Either may be supplied; if neither is set the layer fails |
| 152 | + * with a `ConfigError`. |
| 153 | + * |
| 154 | + * @since 1.0.0 |
| 155 | + * @category Layers |
| 156 | + */ |
| 157 | +export const layerConfig = ( |
| 158 | + options?: { |
| 159 | + readonly apiKey?: Config.Config<Redacted.Redacted> | undefined |
| 160 | + readonly apiUrl?: Config.Config<string> | undefined |
| 161 | + } |
| 162 | +): Layer.Layer<PerplexityClient, ConfigError, HttpClient.HttpClient> => |
| 163 | + Layer.scoped( |
| 164 | + PerplexityClient, |
| 165 | + Effect.flatMap( |
| 166 | + Config.all({ |
| 167 | + apiKey: options?.apiKey ?? Config.redacted("PERPLEXITY_API_KEY").pipe( |
| 168 | + Config.orElse(() => Config.redacted("PPLX_API_KEY")) |
| 169 | + ), |
| 170 | + apiUrl: options?.apiUrl ?? Config.string("PERPLEXITY_API_URL").pipe( |
| 171 | + Config.withDefault("https://api.perplexity.ai") |
| 172 | + ) |
| 173 | + }), |
| 174 | + make |
| 175 | + ) |
| 176 | + ) |
0 commit comments