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

ProviderService.ts

121 lines3.6 KB

1/**
2 * ProviderService - Service interface for provider sessions, turns, and checkpoints.
3 *
4 * Acts as the cross-provider facade used by transports (WebSocket/RPC). It
5 * resolves provider adapters through `ProviderAdapterRegistry`, routes
6 * session-scoped calls via `ProviderSessionDirectory`, and exposes one unified
7 * provider event stream to callers.
8 *
9 * Uses Effect `Context.Service` for dependency injection and returns typed
10 * domain errors for validation, session, codex, and checkpoint workflows.
11 *
12 * @module ProviderService
13 */
14import type {
15 ProviderInterruptTurnInput,
16 ProviderInstanceId,
17 ProviderRespondToRequestInput,
18 ProviderRespondToUserInputInput,
19 ProviderRuntimeEvent,
20 ProviderSendTurnInput,
21 ProviderSession,
22 ProviderSessionStartInput,
23 ProviderStopSessionInput,
24 ThreadId,
25 ProviderTurnStartResult,
26} from "@t3tools/contracts";
27import * as Context from "effect/Context";
28import type * as Effect from "effect/Effect";
29import type * as Stream from "effect/Stream";
30​
31import type { ProviderServiceError } from "../Errors.ts";
32import type { ProviderAdapterCapabilities } from "./ProviderAdapter.ts";
33import type { ProviderInstanceRoutingInfo } from "./ProviderAdapterRegistry.ts";
34​
35/**
36 * ProviderServiceShape - Service API for provider session and turn orchestration.
37 */
38export interface ProviderServiceShape {
39 /**
40 * Start a provider session.
41 */
42 readonly startSession: (
43 threadId: ThreadId,
44 input: ProviderSessionStartInput,
45 ) => Effect.Effect<ProviderSession, ProviderServiceError>;
46​
47 /**
48 * Send a provider turn.
49 */
50 readonly sendTurn: (
51 input: ProviderSendTurnInput,
52 ) => Effect.Effect<ProviderTurnStartResult, ProviderServiceError>;
53​
54 /**
55 * Interrupt a running provider turn.
56 */
57 readonly interruptTurn: (
58 input: ProviderInterruptTurnInput,
59 ) => Effect.Effect<void, ProviderServiceError>;
60​
61 /**
62 * Respond to a provider approval request.
63 */
64 readonly respondToRequest: (
65 input: ProviderRespondToRequestInput,
66 ) => Effect.Effect<void, ProviderServiceError>;
67​
68 /**
69 * Respond to a provider structured user-input request.
70 */
71 readonly respondToUserInput: (
72 input: ProviderRespondToUserInputInput,
73 ) => Effect.Effect<void, ProviderServiceError>;
74​
75 /**
76 * Stop a provider session.
77 */
78 readonly stopSession: (
79 input: ProviderStopSessionInput,
80 ) => Effect.Effect<void, ProviderServiceError>;
81​
82 /**
83 * List active provider sessions.
84 *
85 * Aggregates runtime session lists from all registered adapters.
86 */
87 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
88​
89 /**
90 * Read capabilities for the adapter bound to a configured provider instance.
91 */
92 readonly getCapabilities: (
93 instanceId: ProviderInstanceId,
94 ) => Effect.Effect<ProviderAdapterCapabilities, ProviderServiceError>;
95​
96 readonly getInstanceInfo: (
97 instanceId: ProviderInstanceId,
98 ) => Effect.Effect<ProviderInstanceRoutingInfo, ProviderServiceError>;
99​
100 /**
101 * Roll back provider conversation state by a number of turns.
102 */
103 readonly rollbackConversation: (input: {
104 readonly threadId: ThreadId;
105 readonly numTurns: number;
106 }) => Effect.Effect<void, ProviderServiceError>;
107​
108 /**
109 * Canonical provider runtime event stream.
110 *
111 * Fan-out is owned by ProviderService (not by a standalone event-bus service).
112 */
113 readonly streamEvents: Stream.Stream<ProviderRuntimeEvent>;
115​
116/**
117 * ProviderService - Service tag for provider orchestration.
118 */
119export class ProviderService extends Context.Service<ProviderService, ProviderServiceShape>()(
120 "t3/provider/Services/ProviderService",
121) {}