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

ProviderService.ts

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