2026-06-01 23:19:54 -05:00
|
|
|
import {
|
|
|
|
|
TooManyRequestsError,
|
|
|
|
|
errorMessage,
|
|
|
|
|
type LlmChunk,
|
|
|
|
|
} from "@trustgraph/base";
|
2026-06-02 04:33:48 -05:00
|
|
|
import { Config, Effect, Ref, Result, Stream } from "effect";
|
2026-06-01 23:19:54 -05:00
|
|
|
import * as O from "effect/Option";
|
2026-06-02 03:00:52 -05:00
|
|
|
import * as Predicate from "effect/Predicate";
|
2026-06-01 23:19:54 -05:00
|
|
|
import * as S from "effect/Schema";
|
|
|
|
|
|
|
|
|
|
export class TextCompletionConfigError extends S.TaggedErrorClass<TextCompletionConfigError>()(
|
|
|
|
|
"TextCompletionConfigError",
|
|
|
|
|
{
|
|
|
|
|
message: S.String,
|
|
|
|
|
provider: S.String,
|
|
|
|
|
key: S.String,
|
|
|
|
|
},
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
export class TextCompletionProviderError extends S.TaggedErrorClass<TextCompletionProviderError>()(
|
|
|
|
|
"TextCompletionProviderError",
|
|
|
|
|
{
|
|
|
|
|
message: S.String,
|
|
|
|
|
provider: S.String,
|
|
|
|
|
},
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
export type TextCompletionRuntimeError =
|
|
|
|
|
| TextCompletionProviderError
|
|
|
|
|
| TooManyRequestsError;
|
|
|
|
|
|
2026-06-02 04:33:48 -05:00
|
|
|
type StreamingTokenTotals = {
|
|
|
|
|
readonly inToken: number;
|
|
|
|
|
readonly outToken: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type LlmStreamPart = {
|
|
|
|
|
readonly text: O.Option<string>;
|
|
|
|
|
readonly inToken: O.Option<number>;
|
|
|
|
|
readonly outToken: O.Option<number>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const initialTokenTotals = {
|
|
|
|
|
inToken: 0,
|
|
|
|
|
outToken: 0,
|
|
|
|
|
} satisfies StreamingTokenTotals;
|
|
|
|
|
|
|
|
|
|
const updateTokenTotals = (
|
|
|
|
|
current: StreamingTokenTotals,
|
|
|
|
|
part: LlmStreamPart,
|
|
|
|
|
): StreamingTokenTotals => ({
|
|
|
|
|
inToken: O.getOrElse(part.inToken, () => current.inToken),
|
|
|
|
|
outToken: O.getOrElse(part.outToken, () => current.outToken),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const finalChunk = (model: string, totals: StreamingTokenTotals): LlmChunk => ({
|
|
|
|
|
text: "",
|
|
|
|
|
inToken: totals.inToken,
|
|
|
|
|
outToken: totals.outToken,
|
|
|
|
|
model,
|
|
|
|
|
isFinal: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const textChunk = (model: string, text: string): LlmChunk => ({
|
|
|
|
|
text,
|
|
|
|
|
inToken: null,
|
|
|
|
|
outToken: null,
|
|
|
|
|
model,
|
|
|
|
|
isFinal: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const contentPartText = (part: unknown): O.Option<string> =>
|
|
|
|
|
Predicate.isObject(part) &&
|
|
|
|
|
Predicate.hasProperty(part, "text") &&
|
|
|
|
|
Predicate.isString(part.text)
|
|
|
|
|
? O.some(part.text)
|
|
|
|
|
: O.none();
|
|
|
|
|
|
|
|
|
|
export const textFromContent = (content: unknown): string => {
|
|
|
|
|
if (Predicate.isString(content)) {
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Array.isArray(content)
|
|
|
|
|
? content.flatMap((part) => O.toArray(contentPartText(part))).join("")
|
|
|
|
|
: "";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const llmStreamPart = (part: {
|
|
|
|
|
readonly text?: string | null | undefined;
|
|
|
|
|
readonly inToken?: number | null | undefined;
|
|
|
|
|
readonly outToken?: number | null | undefined;
|
|
|
|
|
}): LlmStreamPart => ({
|
|
|
|
|
text: O.fromNullishOr(part.text),
|
|
|
|
|
inToken: O.fromNullishOr(part.inToken),
|
|
|
|
|
outToken: O.fromNullishOr(part.outToken),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const streamTextCompletionChunks = <A>(
|
|
|
|
|
iterable: AsyncIterable<A>,
|
|
|
|
|
options: {
|
|
|
|
|
readonly model: string;
|
|
|
|
|
readonly mapError: (error: unknown) => TextCompletionRuntimeError;
|
|
|
|
|
readonly extract: (chunk: A) => LlmStreamPart;
|
|
|
|
|
readonly finalTokens?: Effect.Effect<StreamingTokenTotals, TextCompletionRuntimeError>;
|
|
|
|
|
},
|
|
|
|
|
): Stream.Stream<LlmChunk, TextCompletionRuntimeError> =>
|
|
|
|
|
Stream.unwrap(Effect.gen(function* () {
|
|
|
|
|
const totals = yield* Ref.make(initialTokenTotals);
|
|
|
|
|
|
|
|
|
|
const chunks = Stream.fromAsyncIterable(iterable, options.mapError).pipe(
|
|
|
|
|
Stream.mapEffect((chunk) =>
|
|
|
|
|
Effect.gen(function* () {
|
|
|
|
|
const part = options.extract(chunk);
|
|
|
|
|
yield* Ref.update(totals, (current) => updateTokenTotals(current, part));
|
|
|
|
|
return O.map(
|
|
|
|
|
O.filter(part.text, (text) => text.length > 0),
|
|
|
|
|
(text) => textChunk(options.model, text),
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
Stream.filterMap((chunk) =>
|
|
|
|
|
O.match(chunk, {
|
|
|
|
|
onNone: () => Result.fail(undefined),
|
|
|
|
|
onSome: Result.succeed,
|
|
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const tokenTotals = options.finalTokens ?? Ref.get(totals);
|
|
|
|
|
return chunks.pipe(
|
|
|
|
|
Stream.concat(Stream.fromEffect(tokenTotals.pipe(
|
|
|
|
|
Effect.map((tokens) => finalChunk(options.model, tokens)),
|
|
|
|
|
))),
|
|
|
|
|
);
|
|
|
|
|
}));
|
|
|
|
|
|
2026-06-01 23:19:54 -05:00
|
|
|
export const optionalStringConfig = Effect.fn("TextCompletion.optionalStringConfig")(function*(
|
|
|
|
|
provider: string,
|
|
|
|
|
name: string,
|
|
|
|
|
) {
|
|
|
|
|
const value = yield* Config.string(name).pipe(
|
|
|
|
|
Config.option,
|
|
|
|
|
Effect.mapError((cause) =>
|
|
|
|
|
TextCompletionConfigError.make({
|
|
|
|
|
provider,
|
|
|
|
|
key: name,
|
|
|
|
|
message: errorMessage(cause),
|
|
|
|
|
})
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return O.getOrUndefined(value);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const requiredString = (
|
|
|
|
|
value: string | undefined,
|
|
|
|
|
provider: string,
|
|
|
|
|
key: string,
|
|
|
|
|
message: string,
|
|
|
|
|
) =>
|
|
|
|
|
value !== undefined && value.length > 0
|
|
|
|
|
? Effect.succeed(value)
|
|
|
|
|
: Effect.fail(TextCompletionConfigError.make({ provider, key, message }));
|
|
|
|
|
|
|
|
|
|
export const providerRuntimeError = (
|
|
|
|
|
provider: string,
|
|
|
|
|
error: unknown,
|
|
|
|
|
): TextCompletionRuntimeError =>
|
|
|
|
|
TextCompletionProviderError.make({
|
|
|
|
|
provider,
|
|
|
|
|
message: errorMessage(error),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const providerStatusError = (
|
|
|
|
|
provider: string,
|
|
|
|
|
error: unknown,
|
|
|
|
|
): TextCompletionRuntimeError => {
|
2026-06-02 03:00:52 -05:00
|
|
|
const status = Predicate.isObject(error) && Predicate.hasProperty(error, "status")
|
|
|
|
|
? error.status
|
2026-06-01 23:19:54 -05:00
|
|
|
: undefined;
|
2026-06-02 03:00:52 -05:00
|
|
|
const statusCode = Predicate.isObject(error) && Predicate.hasProperty(error, "statusCode")
|
|
|
|
|
? error.statusCode
|
2026-06-01 23:19:54 -05:00
|
|
|
: undefined;
|
|
|
|
|
return status === 429 || statusCode === 429
|
|
|
|
|
? TooManyRequestsError.make({ message: "Rate limit exceeded" })
|
|
|
|
|
: providerRuntimeError(provider, error);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const toAsyncGenerator = (
|
|
|
|
|
iterable: AsyncIterable<LlmChunk>,
|
|
|
|
|
mapError: (error: unknown) => TextCompletionRuntimeError,
|
|
|
|
|
): AsyncGenerator<LlmChunk> => {
|
|
|
|
|
const iterator = iterable[Symbol.asyncIterator]();
|
|
|
|
|
let generator: AsyncGenerator<LlmChunk>;
|
|
|
|
|
generator = {
|
2026-06-02 02:58:19 -05:00
|
|
|
next: (value?: unknown) => iterator.next(value),
|
2026-06-01 23:19:54 -05:00
|
|
|
return: (value?: unknown) =>
|
|
|
|
|
iterator.return === undefined
|
2026-06-02 02:58:19 -05:00
|
|
|
? Promise.resolve({ done: true, value })
|
|
|
|
|
: iterator.return(value),
|
2026-06-01 23:19:54 -05:00
|
|
|
throw: (error?: unknown) =>
|
|
|
|
|
iterator.throw === undefined
|
2026-06-02 02:58:19 -05:00
|
|
|
? Promise.reject(mapError(error))
|
|
|
|
|
: iterator.throw(error),
|
2026-06-01 23:19:54 -05:00
|
|
|
[Symbol.asyncIterator]: () => generator,
|
2026-06-02 02:58:19 -05:00
|
|
|
};
|
2026-06-01 23:19:54 -05:00
|
|
|
return generator;
|
|
|
|
|
};
|