0.1.17 #18

Merged
Thilawyn merged 37 commits from next into master 2024-09-07 20:56:30 +02:00
4 changed files with 70 additions and 0 deletions
Showing only changes of commit 8dc1ed7015 - Show all commits

BIN
bun.lockb

Binary file not shown.

View File

@@ -92,6 +92,7 @@
"mobx": "^6.13.1",
"npm-check-updates": "^17.1.1",
"npm-sort": "^0.0.4",
"openai": "^4.57.3",
"tsup": "^8.2.4",
"tsx": "^4.19.0",
"typescript": "^5.5.4"

View File

@@ -0,0 +1,68 @@
import { Config, Context, Effect, Layer } from "effect"
import type { OpenAI } from "openai"
export class OpenAIClient extends Context.Tag("OpenAIClient")<OpenAIClient, OpenAIClientService>() {}
export class OpenAIClientService {
constructor(
readonly openai: Effect.Effect.Success<typeof importOpenAI>,
readonly client: OpenAI,
) {}
try<A>(
try_: (
client: OpenAI,
signal: AbortSignal,
) => Promise<A>
) {
return Effect.tryPromise({
try: signal => try_(this.client, signal),
catch: e => e instanceof this.openai.OpenAIError
? e
: new Error(`Unknown OpenAIClient error: ${ e }`),
})
}
}
const importOpenAI = Effect.tryPromise({
try: () => import("openai"),
catch: cause => new Error("Could not import 'openai'. Make sure it is installed.", { cause }),
})
export const OpenAIClientLive = (
config: {
readonly apiKey: Config.Config<string>
readonly organization?: Config.Config<string | undefined>
readonly project?: Config.Config<string | undefined>
readonly baseURL?: Config.Config<string | undefined>
readonly timeout?: Config.Config<number | undefined>
readonly maxRetries?: Config.Config<number | undefined>
readonly httpAgent?: any
readonly fetch?: any
readonly defaultHeaders?: { [x: string]: string }
readonly defaultQuery?: { [x: string]: string }
}
) => Layer.effect(OpenAIClient, Effect.gen(function*() {
const openai = yield* importOpenAI
return new OpenAIClientService(
openai,
new openai.OpenAI({
apiKey: yield* config.apiKey,
organization: (yield* config.organization || Config.succeed(undefined)) || null,
project: (yield* config.project || Config.succeed(undefined)) || null,
baseURL: (yield* config.baseURL || Config.succeed(undefined)) || "https://api.openai.com/v1",
timeout: yield* config.timeout || Config.succeed(undefined),
maxRetries: yield* config.maxRetries || Config.succeed(undefined),
httpAgent: config.httpAgent,
fetch: config.fetch,
defaultHeaders: config.defaultHeaders,
defaultQuery: config.defaultQuery,
}),
)
}))

View File

@@ -1,2 +1,3 @@
export * from "./express"
export * as JSONWebToken from "./JSONWebToken"
export * as OpenAIClient from "./OpenAIClient"