mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-01 01:19:38 +02:00
Use Effect primitives for AI and response fanout
This commit is contained in:
parent
8f47456a4b
commit
24a2447cc3
5 changed files with 392 additions and 59 deletions
|
|
@ -12,8 +12,8 @@ Verified source roots:
|
||||||
- Effect v4 subtree: `/home/elpresidank/YeeBois/projects/beep-effect2/.repos/effect-v4`
|
- Effect v4 subtree: `/home/elpresidank/YeeBois/projects/beep-effect2/.repos/effect-v4`
|
||||||
- Installed Effect beta used by this workspace: `ts/node_modules/effect`
|
- Installed Effect beta used by this workspace: `ts/node_modules/effect`
|
||||||
|
|
||||||
Current signal counts from `ts/packages` after the 2026-06-02 workbench theme
|
Current signal counts from `ts/packages` after the 2026-06-02 Effect AI
|
||||||
storage slice:
|
adapter and native request/response PubSub slices:
|
||||||
|
|
||||||
| Signal | Count |
|
| Signal | Count |
|
||||||
| --- | ---: |
|
| --- | ---: |
|
||||||
|
|
@ -21,9 +21,9 @@ storage slice:
|
||||||
| `Effect.runPromiseWith` | 0 |
|
| `Effect.runPromiseWith` | 0 |
|
||||||
| `Effect.cached` | 0 |
|
| `Effect.cached` | 0 |
|
||||||
| `Layer.succeed` | 12 |
|
| `Layer.succeed` | 12 |
|
||||||
| `Map<` | 38 |
|
| `Map<` | 37 |
|
||||||
| `WebSocket` | 72 |
|
| `WebSocket` | 72 |
|
||||||
| `new Map` | 60 |
|
| `new Map` | 59 |
|
||||||
| `toPromiseRequestor` | 0 |
|
| `toPromiseRequestor` | 0 |
|
||||||
| `makeAsyncProcessor` | 19 |
|
| `makeAsyncProcessor` | 19 |
|
||||||
| `receive(` | 17 |
|
| `receive(` | 17 |
|
||||||
|
|
@ -31,7 +31,7 @@ storage slice:
|
||||||
| `new Error` | 7 |
|
| `new Error` | 7 |
|
||||||
| `new Promise` | 9 |
|
| `new Promise` | 9 |
|
||||||
| `JSON.parse` | 4 |
|
| `JSON.parse` | 4 |
|
||||||
| `localStorage` | 9 |
|
| `localStorage` | 11 |
|
||||||
| `JSON.stringify` | 8 |
|
| `JSON.stringify` | 8 |
|
||||||
| `setTimeout` | 3 |
|
| `setTimeout` | 3 |
|
||||||
| `process.env` | 3 |
|
| `process.env` | 3 |
|
||||||
|
|
@ -138,6 +138,18 @@ Notes:
|
||||||
`BrowserKeyValueStore.layerLocalStorage`; the first-paint host script reads
|
`BrowserKeyValueStore.layerLocalStorage`; the first-paint host script reads
|
||||||
that JSON-encoded key before React mounts and falls back to `tg-theme` only
|
that JSON-encoded key before React mounts and falls back to `tg-theme` only
|
||||||
for legacy installs.
|
for legacy installs.
|
||||||
|
- The Effect AI `LanguageModel` adapter slice added a reusable
|
||||||
|
`makeLanguageModelProvider` bridge in text-completion common code. It maps
|
||||||
|
`generateText` responses to `LlmResult`, maps streaming `text-delta` and
|
||||||
|
final `finish.usage` parts to TrustGraph chunks, and converts Effect AI rate
|
||||||
|
and quota failures into `TooManyRequestsError`. No concrete provider has
|
||||||
|
been flipped yet.
|
||||||
|
- The native request/response PubSub slice removed the local
|
||||||
|
`Map<string, Queue>` response subscriber fanout in
|
||||||
|
`makeEffectRequestResponseFromPubSub`. Response dispatch now publishes
|
||||||
|
`{ id, value }` envelopes through native `effect/PubSub`, and each request
|
||||||
|
uses a scoped `PubSub.Subscription` plus `Stream.fromSubscription` to wait
|
||||||
|
for its matching response.
|
||||||
- A focused broker-backend scout found no remaining P0 broker runtime rewrite
|
- A focused broker-backend scout found no remaining P0 broker runtime rewrite
|
||||||
after the producer, NATS, consumer concurrency, rate-limit, and
|
after the producer, NATS, consumer concurrency, rate-limit, and
|
||||||
request-response stop slices. `PubSubBackend` remains an intentional
|
request-response stop slices. `PubSubBackend` remains an intentional
|
||||||
|
|
@ -1358,6 +1370,38 @@ Notes:
|
||||||
- `cd ts && bun run test`
|
- `cd ts && bun run test`
|
||||||
- `cd ts && bun run lint`
|
- `cd ts && bun run lint`
|
||||||
|
|
||||||
|
### 2026-06-02: Effect AI LanguageModel Adapter Slice
|
||||||
|
|
||||||
|
- Status: migrated and package-verified.
|
||||||
|
- Completed:
|
||||||
|
- Added `makeLanguageModelProvider`, a bridge from
|
||||||
|
`effect/unstable/ai/LanguageModel` into the existing TrustGraph
|
||||||
|
`LlmProvider` contract.
|
||||||
|
- Covered non-streaming text/token mapping, streaming text/final-token
|
||||||
|
mapping, and Effect AI rate/quota failure mapping with fake
|
||||||
|
`LanguageModel` tests.
|
||||||
|
- Kept concrete provider swaps deferred until provider-specific parity is
|
||||||
|
proven.
|
||||||
|
- Verification:
|
||||||
|
- `cd ts && bun run check:tsgo`
|
||||||
|
- `cd ts/packages/flow && bunx --bun vitest run src/__tests__/text-completion-common.test.ts src/__tests__/text-completion-providers.test.ts`
|
||||||
|
|
||||||
|
### 2026-06-02: Native Request/Response PubSub Fanout Slice
|
||||||
|
|
||||||
|
- Status: migrated and package-verified.
|
||||||
|
- Completed:
|
||||||
|
- Replaced the request/response runtime's hand-managed
|
||||||
|
`Map<string, Queue>` response fanout with native `effect/PubSub`.
|
||||||
|
- Each request subscribes before sending, consumes through
|
||||||
|
`Stream.fromSubscription`, filters by response id, and releases the
|
||||||
|
subscription at scope exit.
|
||||||
|
- Kept `PubSubBackend` as the broker boundary because Effect native PubSub is
|
||||||
|
in-process only and does not provide NATS topics, ack/nack, durable
|
||||||
|
subscriptions, schema codecs, or backend lifecycle.
|
||||||
|
- Verification:
|
||||||
|
- `cd ts && bun run check:tsgo`
|
||||||
|
- `cd ts/packages/base && bunx --bun vitest run src/__tests__/messaging-runtime.test.ts src/__tests__/request-response.test.ts src/__tests__/flow-spec-runtime.test.ts`
|
||||||
|
|
||||||
## Subagent Findings To Preserve
|
## Subagent Findings To Preserve
|
||||||
|
|
||||||
- MCP/workbench:
|
- MCP/workbench:
|
||||||
|
|
@ -1442,9 +1486,9 @@ Notes:
|
||||||
text, token counts, streaming final usage, and rate-limit mapping. The
|
text, token counts, streaming final usage, and rate-limit mapping. The
|
||||||
local provider layer-construction cleanup is complete; remaining provider
|
local provider layer-construction cleanup is complete; remaining provider
|
||||||
work is adapter/parity work, not `Layer.succeed` cleanup.
|
work is adapter/parity work, not `Layer.succeed` cleanup.
|
||||||
- The next provider PR should add a small `effect/unstable/ai/LanguageModel`
|
- The `effect/unstable/ai/LanguageModel` to TrustGraph `LlmProvider` adapter
|
||||||
to TrustGraph `LlmProvider` adapter and prove it with fake
|
baseline is complete. The next provider PR should migrate Claude through
|
||||||
`LanguageModel` parts before migrating Claude. Direct OpenAI, Azure, and
|
that adapter with provider-specific parity tests. Direct OpenAI, Azure, and
|
||||||
OpenAI-compatible swaps are no-ops until Responses-vs-Chat-Completions
|
OpenAI-compatible swaps are no-ops until Responses-vs-Chat-Completions
|
||||||
parity is proven.
|
parity is proven.
|
||||||
- FalkorDB scoped lifecycle is complete for triples query/store. Use the
|
- FalkorDB scoped lifecycle is complete for triples query/store. Use the
|
||||||
|
|
@ -1499,6 +1543,9 @@ Notes:
|
||||||
handles.
|
handles.
|
||||||
- Treat request-response pending shutdown semantics as complete; do not flag
|
- Treat request-response pending shutdown semantics as complete; do not flag
|
||||||
`waitForResponse` timeout behavior for stopped runtimes.
|
`waitForResponse` timeout behavior for stopped runtimes.
|
||||||
|
- Treat request-response in-process fanout as complete: response routing now
|
||||||
|
uses native `effect/PubSub` subscriptions instead of a hand-managed
|
||||||
|
subscriber map.
|
||||||
- Treat the legacy consumer facade as a completed compatibility wrapper over
|
- Treat the legacy consumer facade as a completed compatibility wrapper over
|
||||||
`makeEffectConsumerFromPubSub`; do not flag blocking `start()` semantics.
|
`makeEffectConsumerFromPubSub`; do not flag blocking `start()` semantics.
|
||||||
|
|
||||||
|
|
@ -1510,13 +1557,9 @@ Notes:
|
||||||
- `effect/unstable/ai/LanguageModel`, `effect/unstable/ai/EmbeddingModel`,
|
- `effect/unstable/ai/LanguageModel`, `effect/unstable/ai/EmbeddingModel`,
|
||||||
Effect AI OpenAI/Anthropic provider layers.
|
Effect AI OpenAI/Anthropic provider layers.
|
||||||
- Rewrite shape:
|
- Rewrite shape:
|
||||||
- Add an Effect AI `LanguageModel` to `LlmProvider` adapter beside the
|
- Adapter baseline is complete: `makeLanguageModelProvider` bridges
|
||||||
current `LlmProvider` contract before flipping any public provider
|
`LanguageModel` into `LlmProvider`.
|
||||||
interface.
|
- Claude is the first plausible provider migration through the adapter.
|
||||||
- Prove `LlmResult`, streaming `text-delta` plus final `finish.usage`,
|
|
||||||
`AiError.RateLimitError` mapping, and missing-token config behavior with
|
|
||||||
fake Effect `LanguageModel` tests.
|
|
||||||
- Claude is the first plausible provider migration after the adapter.
|
|
||||||
- Do not directly swap OpenAI, Azure, or OpenAI-compatible providers yet:
|
- Do not directly swap OpenAI, Azure, or OpenAI-compatible providers yet:
|
||||||
current TrustGraph code uses Chat Completions/local-server semantics while
|
current TrustGraph code uses Chat Completions/local-server semantics while
|
||||||
`@effect/ai-openai` is Responses API backed.
|
`@effect/ai-openai` is Responses API backed.
|
||||||
|
|
@ -1560,7 +1603,7 @@ Notes:
|
||||||
|
|
||||||
## Recommended PR Order
|
## Recommended PR Order
|
||||||
|
|
||||||
1. Effect AI `LanguageModel` to `LlmProvider` adapter, then Claude parity.
|
1. Claude provider parity through the Effect AI `LanguageModel` adapter.
|
||||||
2. MCP Effect stdio parity and canonicalization.
|
2. MCP Effect stdio parity and canonicalization.
|
||||||
|
|
||||||
## No-Op Rules
|
## No-Op Rules
|
||||||
|
|
|
||||||
|
|
@ -322,7 +322,7 @@ describe("Effect-native messaging runtime", () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
it.effect(
|
it.effect(
|
||||||
"routes request-response replies through an Effect queue",
|
"routes request-response replies through Effect PubSub",
|
||||||
Effect.fnUntraced(function* () {
|
Effect.fnUntraced(function* () {
|
||||||
const responseConsumer = new ScriptedConsumer<string>();
|
const responseConsumer = new ScriptedConsumer<string>();
|
||||||
const backend = new RuntimeBackend(
|
const backend = new RuntimeBackend(
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,20 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { Context, Deferred, Duration, Effect, Fiber, Layer, Queue, Ref, Result, Schedule, Scope, Stream } from "effect";
|
import {
|
||||||
|
Context,
|
||||||
|
Deferred,
|
||||||
|
Duration,
|
||||||
|
Effect,
|
||||||
|
Fiber,
|
||||||
|
Layer,
|
||||||
|
PubSub as EffectPubSub,
|
||||||
|
Ref,
|
||||||
|
Result,
|
||||||
|
Schedule,
|
||||||
|
Scope,
|
||||||
|
Stream,
|
||||||
|
} from "effect";
|
||||||
import * as O from "effect/Option";
|
import * as O from "effect/Option";
|
||||||
import * as S from "effect/Schema";
|
import * as S from "effect/Schema";
|
||||||
import type {
|
import type {
|
||||||
|
|
@ -121,6 +134,11 @@ export interface FlowRuntimeService {
|
||||||
) => Effect.Effect<void, FlowRuntimeError, SpecRuntimeRequirements | Requirements>;
|
) => Effect.Effect<void, FlowRuntimeError, SpecRuntimeRequirements | Requirements>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ResponseEnvelope<T> {
|
||||||
|
readonly id: string;
|
||||||
|
readonly value: T;
|
||||||
|
}
|
||||||
|
|
||||||
export class ProducerFactory extends Context.Service<ProducerFactory, ProducerFactoryService>()(
|
export class ProducerFactory extends Context.Service<ProducerFactory, ProducerFactoryService>()(
|
||||||
"@trustgraph/base/messaging/runtime/ProducerFactory",
|
"@trustgraph/base/messaging/runtime/ProducerFactory",
|
||||||
) {}
|
) {}
|
||||||
|
|
@ -395,7 +413,7 @@ export const makeEffectConsumerFromPubSub = Effect.fn("makeEffectConsumerFromPub
|
||||||
const dispatchResponseLoop = <T>(
|
const dispatchResponseLoop = <T>(
|
||||||
backend: BackendConsumer<T>,
|
backend: BackendConsumer<T>,
|
||||||
responseTopic: string,
|
responseTopic: string,
|
||||||
subscribers: Map<string, Queue.Queue<T>>,
|
responses: EffectPubSub.PubSub<ResponseEnvelope<T>>,
|
||||||
config: MessagingRuntimeConfig,
|
config: MessagingRuntimeConfig,
|
||||||
): Effect.Effect<void> =>
|
): Effect.Effect<void> =>
|
||||||
Effect.whileLoop({
|
Effect.whileLoop({
|
||||||
|
|
@ -408,10 +426,12 @@ const dispatchResponseLoop = <T>(
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = message.properties().id;
|
const id = message.properties().id;
|
||||||
const queue = id === undefined ? undefined : subscribers.get(id);
|
|
||||||
return Effect.gen(function* () {
|
return Effect.gen(function* () {
|
||||||
if (queue !== undefined) {
|
if (id !== undefined) {
|
||||||
yield* Queue.offer(queue, message.value());
|
yield* EffectPubSub.publish(responses, {
|
||||||
|
id,
|
||||||
|
value: message.value(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
yield* acknowledgeMessage(backend, message, responseTopic);
|
yield* acknowledgeMessage(backend, message, responseTopic);
|
||||||
});
|
});
|
||||||
|
|
@ -427,19 +447,24 @@ const dispatchResponseLoop = <T>(
|
||||||
});
|
});
|
||||||
|
|
||||||
const waitForResponse = Effect.fn("waitForResponse")(function* <TRes, E, R>(
|
const waitForResponse = Effect.fn("waitForResponse")(function* <TRes, E, R>(
|
||||||
queue: Queue.Queue<TRes>,
|
subscription: EffectPubSub.Subscription<ResponseEnvelope<TRes>>,
|
||||||
|
id: string,
|
||||||
options: EffectRequestOptions<TRes, E, R> | undefined,
|
options: EffectRequestOptions<TRes, E, R> | undefined,
|
||||||
) {
|
) {
|
||||||
const response = yield* Stream.fromQueue(queue).pipe(
|
const response = yield* Stream.fromSubscription(subscription).pipe(
|
||||||
Stream.filterMapEffect((candidate) => {
|
Stream.filterMapEffect((candidate) => {
|
||||||
if (options?.recipient === undefined) {
|
if (candidate.id !== id) {
|
||||||
return Effect.succeed(Result.succeed(candidate));
|
return Effect.succeed(Result.fail(undefined));
|
||||||
}
|
}
|
||||||
|
|
||||||
return options.recipient(candidate).pipe(
|
if (options?.recipient === undefined) {
|
||||||
|
return Effect.succeed(Result.succeed(candidate.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
return options.recipient(candidate.value).pipe(
|
||||||
Effect.map((complete) =>
|
Effect.map((complete) =>
|
||||||
complete
|
complete
|
||||||
? Result.succeed(candidate)
|
? Result.succeed(candidate.value)
|
||||||
: Result.fail(undefined)
|
: Result.fail(undefined)
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
@ -475,9 +500,9 @@ export const makeEffectRequestResponseFromPubSub = Effect.fn("makeEffectRequestR
|
||||||
...(options.responseSchema === undefined ? {} : { schema: options.responseSchema }),
|
...(options.responseSchema === undefined ? {} : { schema: options.responseSchema }),
|
||||||
};
|
};
|
||||||
const backend = yield* pubsub.createConsumer<TRes>(createOptions);
|
const backend = yield* pubsub.createConsumer<TRes>(createOptions);
|
||||||
const subscribers = new Map<string, Queue.Queue<TRes>>();
|
const responses = yield* EffectPubSub.unbounded<ResponseEnvelope<TRes>>();
|
||||||
const stoppedSignal = yield* Deferred.make<never, MessagingLifecycleError>();
|
const stoppedSignal = yield* Deferred.make<never, MessagingLifecycleError>();
|
||||||
const fiber = yield* dispatchResponseLoop(backend, options.responseTopic, subscribers, config).pipe(Effect.forkScoped);
|
const fiber = yield* dispatchResponseLoop(backend, options.responseTopic, responses, config).pipe(Effect.forkScoped);
|
||||||
let stopped = false;
|
let stopped = false;
|
||||||
|
|
||||||
const stop = Effect.fn(`RequestResponse.stop:${options.requestTopic}`)(function* () {
|
const stop = Effect.fn(`RequestResponse.stop:${options.requestTopic}`)(function* () {
|
||||||
|
|
@ -487,6 +512,7 @@ export const makeEffectRequestResponseFromPubSub = Effect.fn("makeEffectRequestR
|
||||||
stoppedSignal,
|
stoppedSignal,
|
||||||
messagingLifecycleError(`${options.requestTopic}:${options.responseTopic}`, "stop", "RequestResponse stopped"),
|
messagingLifecycleError(`${options.requestTopic}:${options.responseTopic}`, "stop", "RequestResponse stopped"),
|
||||||
).pipe(Effect.ignore);
|
).pipe(Effect.ignore);
|
||||||
|
yield* EffectPubSub.shutdown(responses).pipe(Effect.ignore);
|
||||||
yield* Fiber.interrupt(fiber);
|
yield* Fiber.interrupt(fiber);
|
||||||
yield* producer.close;
|
yield* producer.close;
|
||||||
yield* closeConsumerBackend(backend, options.responseTopic, options.subscription);
|
yield* closeConsumerBackend(backend, options.responseTopic, options.subscription);
|
||||||
|
|
@ -510,33 +536,19 @@ export const makeEffectRequestResponseFromPubSub = Effect.fn("makeEffectRequestR
|
||||||
const id = randomUUID();
|
const id = randomUUID();
|
||||||
const timeoutMs = requestOptions?.timeoutMs ?? config.requestTimeoutMs;
|
const timeoutMs = requestOptions?.timeoutMs ?? config.requestTimeoutMs;
|
||||||
|
|
||||||
return Effect.acquireUseRelease(
|
return Effect.scoped(
|
||||||
Queue.unbounded<TRes>().pipe(
|
Effect.gen(function* () {
|
||||||
Effect.tap((queue) =>
|
const subscription = yield* EffectPubSub.subscribe(responses);
|
||||||
Effect.sync(() => {
|
yield* producer.send(id, request);
|
||||||
subscribers.set(id, queue);
|
const result = yield* waitForResponse(subscription, id, requestOptions).pipe(
|
||||||
}),
|
Effect.raceFirst(Deferred.await(stoppedSignal)),
|
||||||
),
|
Effect.timeoutOption(Duration.millis(timeoutMs)),
|
||||||
),
|
);
|
||||||
(queue) =>
|
return yield* O.match(result, {
|
||||||
Effect.gen(function* () {
|
onNone: () => Effect.fail(messagingTimeoutError("request-response", timeoutMs)),
|
||||||
yield* producer.send(id, request);
|
onSome: Effect.succeed,
|
||||||
const result = yield* waitForResponse(queue, requestOptions).pipe(
|
});
|
||||||
Effect.raceFirst(Deferred.await(stoppedSignal)),
|
}),
|
||||||
Effect.timeoutOption(Duration.millis(timeoutMs)),
|
|
||||||
);
|
|
||||||
return yield* O.match(result, {
|
|
||||||
onNone: () => Effect.fail(messagingTimeoutError("request-response", timeoutMs)),
|
|
||||||
onSome: Effect.succeed,
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
(queue) =>
|
|
||||||
Effect.sync(() => {
|
|
||||||
subscribers.delete(id);
|
|
||||||
}).pipe(
|
|
||||||
Effect.flatMap(() => Queue.shutdown(queue)),
|
|
||||||
Effect.ignore,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
stop: stop(),
|
stop: stop(),
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
import { describe, expect, it } from "@effect/vitest";
|
import { describe, expect, it } from "@effect/vitest";
|
||||||
import type { LlmChunk } from "@trustgraph/base";
|
import type { LlmChunk } from "@trustgraph/base";
|
||||||
import { Effect, Stream } from "effect";
|
import { Effect, Layer, ManagedRuntime, Stream } from "effect";
|
||||||
|
import { AiError, LanguageModel } from "effect/unstable/ai";
|
||||||
import {
|
import {
|
||||||
llmStreamPart,
|
llmStreamPart,
|
||||||
|
makeLanguageModelProvider,
|
||||||
providerRuntimeError,
|
providerRuntimeError,
|
||||||
providerStatusError,
|
providerStatusError,
|
||||||
streamTextCompletionChunks,
|
streamTextCompletionChunks,
|
||||||
|
|
@ -10,6 +12,36 @@ import {
|
||||||
toAsyncGenerator,
|
toAsyncGenerator,
|
||||||
} from "../model/text-completion/common.js";
|
} from "../model/text-completion/common.js";
|
||||||
|
|
||||||
|
const languageModelRuntime = ManagedRuntime.make(Layer.empty);
|
||||||
|
|
||||||
|
const usage = (inputTokens: number, outputTokens: number) => ({
|
||||||
|
inputTokens: {
|
||||||
|
uncached: undefined,
|
||||||
|
total: inputTokens,
|
||||||
|
cacheRead: undefined,
|
||||||
|
cacheWrite: undefined,
|
||||||
|
},
|
||||||
|
outputTokens: {
|
||||||
|
total: outputTokens,
|
||||||
|
text: undefined,
|
||||||
|
reasoning: undefined,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const finishPart = (inputTokens: number, outputTokens: number) => ({
|
||||||
|
type: "finish",
|
||||||
|
reason: "stop",
|
||||||
|
usage: usage(inputTokens, outputTokens),
|
||||||
|
response: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
const aiError = (reason: AiError.AiErrorReason) =>
|
||||||
|
new AiError.AiError({
|
||||||
|
module: "FakeLanguageModel",
|
||||||
|
method: "generateText",
|
||||||
|
reason,
|
||||||
|
});
|
||||||
|
|
||||||
const emptyChunkIterator = (): AsyncIterable<LlmChunk> => ({
|
const emptyChunkIterator = (): AsyncIterable<LlmChunk> => ({
|
||||||
[Symbol.asyncIterator]: () => ({
|
[Symbol.asyncIterator]: () => ({
|
||||||
next: () => Promise.resolve({ done: true, value: undefined }),
|
next: () => Promise.resolve({ done: true, value: undefined }),
|
||||||
|
|
@ -84,4 +116,107 @@ describe("text completion common helpers", () => {
|
||||||
expect(textFromContent([{ text: "a" }, { text: "b" }, { wrong: "skip" }])).toBe("ab");
|
expect(textFromContent([{ text: "a" }, { text: "b" }, { wrong: "skip" }])).toBe("ab");
|
||||||
expect(textFromContent([{ text: 1 }])).toBe("");
|
expect(textFromContent([{ text: 1 }])).toBe("");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("adapts Effect LanguageModel generateText responses to LlmProvider results", async () => {
|
||||||
|
const provider = makeLanguageModelProvider({
|
||||||
|
provider: "FakeLanguageModel",
|
||||||
|
defaultModel: "fake-model",
|
||||||
|
defaultTemperature: 0.1,
|
||||||
|
runtime: languageModelRuntime,
|
||||||
|
makeLanguageModel: ({ model, temperature }) =>
|
||||||
|
LanguageModel.make({
|
||||||
|
generateText: () =>
|
||||||
|
Effect.succeed([
|
||||||
|
{ type: "text", text: `model=${model};temperature=${temperature}` },
|
||||||
|
finishPart(11, 7),
|
||||||
|
]),
|
||||||
|
streamText: () => Stream.empty,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(provider.generateContent("system", "prompt", "override-model", 0.4)).resolves.toEqual({
|
||||||
|
text: "model=override-model;temperature=0.4",
|
||||||
|
inToken: 11,
|
||||||
|
outToken: 7,
|
||||||
|
model: "override-model",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adapts Effect LanguageModel stream parts to TrustGraph chunks", async () => {
|
||||||
|
const provider = makeLanguageModelProvider({
|
||||||
|
provider: "FakeLanguageModel",
|
||||||
|
defaultModel: "fake-stream-model",
|
||||||
|
defaultTemperature: 0,
|
||||||
|
runtime: languageModelRuntime,
|
||||||
|
makeLanguageModel: () =>
|
||||||
|
LanguageModel.make({
|
||||||
|
generateText: () =>
|
||||||
|
Effect.succeed([
|
||||||
|
{ type: "text", text: "unused" },
|
||||||
|
finishPart(1, 1),
|
||||||
|
]),
|
||||||
|
streamText: () =>
|
||||||
|
Stream.fromArray([
|
||||||
|
{ type: "text-delta", id: "part-1", delta: "hel" },
|
||||||
|
{ type: "text-delta", id: "part-1", delta: "lo" },
|
||||||
|
finishPart(13, 8),
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const chunks: Array<LlmChunk> = [];
|
||||||
|
for await (const chunk of provider.generateContentStream("system", "prompt")) {
|
||||||
|
chunks.push(chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(chunks).toEqual([
|
||||||
|
{
|
||||||
|
text: "hel",
|
||||||
|
inToken: null,
|
||||||
|
outToken: null,
|
||||||
|
model: "fake-stream-model",
|
||||||
|
isFinal: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "lo",
|
||||||
|
inToken: null,
|
||||||
|
outToken: null,
|
||||||
|
model: "fake-stream-model",
|
||||||
|
isFinal: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "",
|
||||||
|
inToken: 13,
|
||||||
|
outToken: 8,
|
||||||
|
model: "fake-stream-model",
|
||||||
|
isFinal: true,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("maps Effect AI rate and quota failures to TrustGraph retry errors", async () => {
|
||||||
|
const reasons = [
|
||||||
|
new AiError.RateLimitError({}),
|
||||||
|
new AiError.QuotaExhaustedError({}),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const reason of reasons) {
|
||||||
|
const provider = makeLanguageModelProvider({
|
||||||
|
provider: "FakeLanguageModel",
|
||||||
|
defaultModel: "fake-model",
|
||||||
|
defaultTemperature: 0,
|
||||||
|
runtime: languageModelRuntime,
|
||||||
|
makeLanguageModel: () =>
|
||||||
|
LanguageModel.make({
|
||||||
|
generateText: () => Effect.fail(aiError(reason)),
|
||||||
|
streamText: () => Stream.fail(aiError(reason)),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(provider.generateContent("system", "prompt")).rejects.toMatchObject({
|
||||||
|
_tag: "TooManyRequestsError",
|
||||||
|
message: "Rate limit exceeded",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,14 @@ import {
|
||||||
errorMessage,
|
errorMessage,
|
||||||
makeLlmServiceShape,
|
makeLlmServiceShape,
|
||||||
type LlmChunk,
|
type LlmChunk,
|
||||||
|
type LlmResult,
|
||||||
type LlmProvider,
|
type LlmProvider,
|
||||||
} from "@trustgraph/base";
|
} from "@trustgraph/base";
|
||||||
import { Config, Effect, Layer, Ref, Result, Stream } from "effect";
|
import { Config, Effect, Layer, ManagedRuntime, Ref, Result, Stream } from "effect";
|
||||||
import * as O from "effect/Option";
|
import * as O from "effect/Option";
|
||||||
import * as Predicate from "effect/Predicate";
|
import * as Predicate from "effect/Predicate";
|
||||||
import * as S from "effect/Schema";
|
import * as S from "effect/Schema";
|
||||||
|
import { AiError, LanguageModel, Prompt, Response } from "effect/unstable/ai";
|
||||||
|
|
||||||
export class TextCompletionConfigError extends S.TaggedErrorClass<TextCompletionConfigError>()(
|
export class TextCompletionConfigError extends S.TaggedErrorClass<TextCompletionConfigError>()(
|
||||||
"TextCompletionConfigError",
|
"TextCompletionConfigError",
|
||||||
|
|
@ -32,6 +34,21 @@ export type TextCompletionRuntimeError =
|
||||||
| TextCompletionProviderError
|
| TextCompletionProviderError
|
||||||
| TooManyRequestsError;
|
| TooManyRequestsError;
|
||||||
|
|
||||||
|
export interface LanguageModelProviderRequest {
|
||||||
|
readonly model: string;
|
||||||
|
readonly temperature: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LanguageModelProviderOptions<Requirements> {
|
||||||
|
readonly provider: string;
|
||||||
|
readonly defaultModel: string;
|
||||||
|
readonly defaultTemperature: number;
|
||||||
|
readonly runtime: ManagedRuntime.ManagedRuntime<Requirements, TextCompletionRuntimeError>;
|
||||||
|
readonly makeLanguageModel: (
|
||||||
|
request: LanguageModelProviderRequest,
|
||||||
|
) => Effect.Effect<LanguageModel.Service, TextCompletionRuntimeError, Requirements>;
|
||||||
|
}
|
||||||
|
|
||||||
export const makeTextCompletionLayer = <E, R>(
|
export const makeTextCompletionLayer = <E, R>(
|
||||||
provider: Effect.Effect<LlmProvider, E, R>,
|
provider: Effect.Effect<LlmProvider, E, R>,
|
||||||
): Layer.Layer<Llm, E, R> =>
|
): Layer.Layer<Llm, E, R> =>
|
||||||
|
|
@ -83,6 +100,33 @@ const textChunk = (model: string, text: string): LlmChunk => ({
|
||||||
isFinal: false,
|
isFinal: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const effectAiProviderError = (
|
||||||
|
provider: string,
|
||||||
|
error: unknown,
|
||||||
|
): TextCompletionRuntimeError => {
|
||||||
|
if (
|
||||||
|
AiError.isAiError(error) &&
|
||||||
|
(error.reason._tag === "RateLimitError" || error.reason._tag === "QuotaExhaustedError")
|
||||||
|
) {
|
||||||
|
return TooManyRequestsError.make({ message: "Rate limit exceeded" });
|
||||||
|
}
|
||||||
|
return providerRuntimeError(provider, error);
|
||||||
|
};
|
||||||
|
|
||||||
|
const usageInputTokens = (usage: Response.Usage): number =>
|
||||||
|
usage.inputTokens.total ?? 0;
|
||||||
|
|
||||||
|
const usageOutputTokens = (usage: Response.Usage): number =>
|
||||||
|
usage.outputTokens.total ?? 0;
|
||||||
|
|
||||||
|
const languageModelPrompt = (
|
||||||
|
system: string,
|
||||||
|
prompt: string,
|
||||||
|
): Prompt.RawInput => [
|
||||||
|
{ role: "system", content: system },
|
||||||
|
{ role: "user", content: [{ type: "text", text: prompt }] },
|
||||||
|
];
|
||||||
|
|
||||||
const contentPartText = (part: unknown): O.Option<string> =>
|
const contentPartText = (part: unknown): O.Option<string> =>
|
||||||
Predicate.isObject(part) &&
|
Predicate.isObject(part) &&
|
||||||
Predicate.hasProperty(part, "text") &&
|
Predicate.hasProperty(part, "text") &&
|
||||||
|
|
@ -200,6 +244,105 @@ export const providerStatusError = (
|
||||||
: providerRuntimeError(provider, error);
|
: providerRuntimeError(provider, error);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const languageModelResult = (
|
||||||
|
response: LanguageModel.GenerateTextResponse<{}>,
|
||||||
|
model: string,
|
||||||
|
): LlmResult => ({
|
||||||
|
text: response.text,
|
||||||
|
inToken: usageInputTokens(response.usage),
|
||||||
|
outToken: usageOutputTokens(response.usage),
|
||||||
|
model,
|
||||||
|
});
|
||||||
|
|
||||||
|
const languageModelStreamChunk = (
|
||||||
|
provider: string,
|
||||||
|
model: string,
|
||||||
|
part: Response.StreamPart<{}>,
|
||||||
|
): Effect.Effect<Result.Result<LlmChunk, undefined>, TextCompletionRuntimeError> => {
|
||||||
|
switch (part.type) {
|
||||||
|
case "text-delta":
|
||||||
|
return Effect.succeed(
|
||||||
|
part.delta.length > 0
|
||||||
|
? Result.succeed(textChunk(model, part.delta))
|
||||||
|
: Result.fail(undefined),
|
||||||
|
);
|
||||||
|
case "finish":
|
||||||
|
return Effect.succeed(
|
||||||
|
Result.succeed(
|
||||||
|
finalChunk(model, {
|
||||||
|
inToken: usageInputTokens(part.usage),
|
||||||
|
outToken: usageOutputTokens(part.usage),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
case "error":
|
||||||
|
return Effect.fail(effectAiProviderError(provider, part.error));
|
||||||
|
default:
|
||||||
|
return Effect.succeed(Result.fail(undefined));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const runLanguageModelStream = <RuntimeRequirements, StreamRequirements extends RuntimeRequirements>(
|
||||||
|
runtime: ManagedRuntime.ManagedRuntime<RuntimeRequirements, TextCompletionRuntimeError>,
|
||||||
|
stream: Stream.Stream<LlmChunk, TextCompletionRuntimeError, StreamRequirements>,
|
||||||
|
): AsyncIterable<LlmChunk> => ({
|
||||||
|
[Symbol.asyncIterator]: () => {
|
||||||
|
const iterator = runtime.context().then((context) =>
|
||||||
|
Stream.toAsyncIterableWith(stream, context)[Symbol.asyncIterator]()
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
next: () => iterator.then((current) => current.next()),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const makeLanguageModelProvider = <Requirements>(
|
||||||
|
options: LanguageModelProviderOptions<Requirements>,
|
||||||
|
): LlmProvider => ({
|
||||||
|
generateContent: (system, prompt, model, temperature) => {
|
||||||
|
const modelName = model ?? options.defaultModel;
|
||||||
|
const temp = temperature ?? options.defaultTemperature;
|
||||||
|
return options.runtime.runPromise(
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const languageModel = yield* options.makeLanguageModel({
|
||||||
|
model: modelName,
|
||||||
|
temperature: temp,
|
||||||
|
});
|
||||||
|
const response = yield* languageModel.generateText({
|
||||||
|
prompt: languageModelPrompt(system, prompt),
|
||||||
|
}).pipe(
|
||||||
|
Effect.mapError((error) => effectAiProviderError(options.provider, error)),
|
||||||
|
);
|
||||||
|
return languageModelResult(response, modelName);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
supportsStreaming: () => true,
|
||||||
|
generateContentStream: (system, prompt, model, temperature) => {
|
||||||
|
const modelName = model ?? options.defaultModel;
|
||||||
|
const temp = temperature ?? options.defaultTemperature;
|
||||||
|
const stream = Stream.unwrap(
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const languageModel = yield* options.makeLanguageModel({
|
||||||
|
model: modelName,
|
||||||
|
temperature: temp,
|
||||||
|
});
|
||||||
|
return languageModel.streamText({
|
||||||
|
prompt: languageModelPrompt(system, prompt),
|
||||||
|
}).pipe(
|
||||||
|
Stream.mapError((error) => effectAiProviderError(options.provider, error)),
|
||||||
|
Stream.filterMapEffect((part) =>
|
||||||
|
languageModelStreamChunk(options.provider, modelName, part)
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return toAsyncGenerator(runLanguageModelStream(options.runtime, stream), (error) =>
|
||||||
|
effectAiProviderError(options.provider, error)
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export const toAsyncGenerator = (
|
export const toAsyncGenerator = (
|
||||||
iterable: AsyncIterable<LlmChunk>,
|
iterable: AsyncIterable<LlmChunk>,
|
||||||
mapError: (error: unknown) => TextCompletionRuntimeError,
|
mapError: (error: unknown) => TextCompletionRuntimeError,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue