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

ProviderService.ts

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