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

ProviderContinuationRequests.ts

74 lines2.8 KB

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