Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderContinuationRequests.ts

60 lines2.2 KB

1import {
2 type OrchestrationV2Notification,
3 MessageId,
4 ProviderDriverKind,
5 ProviderThreadId,
6 RunId,
7 ThreadId,
8} from "@t3tools/contracts";
9import * as Context from "effect/Context";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Option from "effect/Option";
13import * as Queue from "effect/Queue";
14​
15export interface ProviderContinuationRequest {
16 readonly threadId: ThreadId;
17 readonly providerThreadId: ProviderThreadId;
18 readonly driver: ProviderDriverKind;
19 readonly detail: string | null;
20 readonly notification?: OrchestrationV2Notification;
21 readonly delegatedCompletion?: {
22 readonly parentRunId: RunId;
23 readonly generation: number;
24 readonly messageId: MessageId;
25 };
26 /**
27 * `adapter_buffered` (default) is the provider-native wake: the adapter has already buffered the
28 * CLI's wake output, and the dispatched message only triggers ingestion. `message_text` is for
29 * app-owned work with no buffered provider output, such as a delegated child finishing.
30 */
31 readonly delivery?: "adapter_buffered" | "message_text";
32 readonly messageId?: MessageId;
33 readonly dispatchIfCurrent?: <A, E, R>(
34 effect: Effect.Effect<A, E, R>,
35 ) => Effect.Effect<Option.Option<A>, E, R>;
36 readonly clearIfCurrent?: () => Effect.Effect<void>;
38​
39/**
40 * The default reference drops requests, keeping adapter construction dependency-free in tests; the
41 * live layer must be shared with the ProviderContinuationService worker that drains it.
42 */
43export class ProviderContinuationRequests extends Context.Reference<{
44 readonly offer: (request: ProviderContinuationRequest) => Effect.Effect<void>;
45 readonly take: Effect.Effect<ProviderContinuationRequest>;
46}>("@t3tools/provider-core/server/ProviderContinuationRequests", {
47 defaultValue: () => ({ offer: () => Effect.void, take: Effect.never }),
48}) {}
49​
50export const layer = Layer.effect(
51 ProviderContinuationRequests,
52 Effect.gen(function* () {
53 const queue = yield* Queue.unbounded<ProviderContinuationRequest>();
54 return {
55 offer: (request: ProviderContinuationRequest) =>
56 Queue.offer(queue, request).pipe(Effect.asVoid),
57 take: Queue.take(queue),
58 };
59 }),
60);