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

ProviderService.ts

120 lines3.5 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 { Context } from "effect";
28import type { Effect, Stream } from "effect";
29​
30import type { ProviderServiceError } from "../Errors.ts";
31import type { ProviderAdapterCapabilities } from "./ProviderAdapter.ts";
32import type { ProviderInstanceRoutingInfo } from "./ProviderAdapterRegistry.ts";
33​
34/**
35 * ProviderServiceShape - Service API for provider session and turn orchestration.
36 */
37export interface ProviderServiceShape {
38 /**
39 * Start a provider session.
40 */
41 readonly startSession: (
42 threadId: ThreadId,
43 input: ProviderSessionStartInput,
44 ) => Effect.Effect<ProviderSession, ProviderServiceError>;
45​
46 /**
47 * Send a provider turn.
48 */
49 readonly sendTurn: (
50 input: ProviderSendTurnInput,
51 ) => Effect.Effect<ProviderTurnStartResult, ProviderServiceError>;
52​
53 /**
54 * Interrupt a running provider turn.
55 */
56 readonly interruptTurn: (
57 input: ProviderInterruptTurnInput,
58 ) => Effect.Effect<void, ProviderServiceError>;
59​
60 /**
61 * Respond to a provider approval request.
62 */
63 readonly respondToRequest: (
64 input: ProviderRespondToRequestInput,
65 ) => Effect.Effect<void, ProviderServiceError>;
66​
67 /**
68 * Respond to a provider structured user-input request.
69 */
70 readonly respondToUserInput: (
71 input: ProviderRespondToUserInputInput,
72 ) => Effect.Effect<void, ProviderServiceError>;
73​
74 /**
75 * Stop a provider session.
76 */
77 readonly stopSession: (
78 input: ProviderStopSessionInput,
79 ) => Effect.Effect<void, ProviderServiceError>;
80​
81 /**
82 * List active provider sessions.
83 *
84 * Aggregates runtime session lists from all registered adapters.
85 */
86 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
87​
88 /**
89 * Read capabilities for the adapter bound to a configured provider instance.
90 */
91 readonly getCapabilities: (
92 instanceId: ProviderInstanceId,
93 ) => Effect.Effect<ProviderAdapterCapabilities, ProviderServiceError>;
94​
95 readonly getInstanceInfo: (
96 instanceId: ProviderInstanceId,
97 ) => Effect.Effect<ProviderInstanceRoutingInfo, ProviderServiceError>;
98​
99 /**
100 * Roll back provider conversation state by a number of turns.
101 */
102 readonly rollbackConversation: (input: {
103 readonly threadId: ThreadId;
104 readonly numTurns: number;
105 }) => Effect.Effect<void, ProviderServiceError>;
106​
107 /**
108 * Canonical provider runtime event stream.
109 *
110 * Fan-out is owned by ProviderService (not by a standalone event-bus service).
111 */
112 readonly streamEvents: Stream.Stream<ProviderRuntimeEvent>;
114​
115/**
116 * ProviderService - Service tag for provider orchestration.
117 */
118export class ProviderService extends Context.Service<ProviderService, ProviderServiceShape>()(
119 "t3/provider/Services/ProviderService",
120) {}