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

ProviderContinuationRequests.ts

76 lines2.9 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 /**
22 * Durable ownership for an app-owned delegated-task completion delivery.
23 * The continuation worker re-reads the cohort before dispatching so a later
24 * task can join a queued wake and a stopped or acknowledged cohort is dropped.
25 */
26 readonly delegatedCompletion?: {
27 readonly parentRunId: RunId;
28 readonly generation: number;
29 readonly messageId: MessageId;
30 };
31 /**
32 * How the continuation turn gets its content.
33 *
34 * `adapter_buffered` (default) is the provider-native wake: the adapter has
35 * already buffered the CLI's wake output, and the dispatched message only
36 * triggers ingestion. `ClaudeAdapterV2` deliberately discards the message
37 * text on that path.
38 *
39 * `message_text` is for app-owned work with no buffered provider output, such
40 * as a delegated child finishing. The text is the entire wake, so it must
41 * reach the provider as a real prompt.
42 */
43 readonly delivery?: "adapter_buffered" | "message_text";
44 readonly dispatchIfCurrent?: <A, E, R>(
45 effect: Effect.Effect<A, E, R>,
46 ) => Effect.Effect<Option.Option<A>, E, R>;
47 /** Clears a pending offer that the continuation worker intentionally drops. */
48 readonly clearIfCurrent?: () => Effect.Effect<void>;
50​
51/**
52 * Adapters offer a continuation request when provider-native work completes
53 * outside an active turn (for example a Claude background task wake turn) so
54 * the orchestrator can start a run that ingests it. The default reference
55 * drops requests, keeping adapter construction dependency-free in tests; the
56 * live layer must be shared with the ProviderContinuationService worker that
57 * drains it.
58 */
59export class ProviderContinuationRequests extends Context.Reference<{
60 readonly offer: (request: ProviderContinuationRequest) => Effect.Effect<void>;
61 readonly take: Effect.Effect<ProviderContinuationRequest>;
62}>("t3/orchestration-v2/ProviderContinuationRequests", {
63 defaultValue: () => ({ offer: () => Effect.void, take: Effect.never }),
64}) {}
65​
66export const layer = Layer.effect(
67 ProviderContinuationRequests,
68 Effect.gen(function* () {
69 const queue = yield* Queue.unbounded<ProviderContinuationRequest>();
70 return {
71 offer: (request: ProviderContinuationRequest) =>
72 Queue.offer(queue, request).pipe(Effect.asVoid),
73 take: Queue.take(queue),
74 };
75 }),
76);