Open History to see the last commit
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 ProviderRespondToRequestInput,
17 ProviderRuntimeEvent,
18 ProviderSendTurnInput,
19 ProviderSession,
20 ProviderSessionId,
21 ProviderSessionStartInput,
22 ProviderStopSessionInput,
23 ThreadId,
24 ProviderTurnStartResult,
25} from "@t3tools/contracts";
26import { ServiceMap } from "effect";
27import type { Effect, Stream } from "effect";
28
29import type { ProviderServiceError } from "../Errors.ts";
30
31/**
32 * ProviderServiceShape - Service API for provider session and turn orchestration.
33 */
34export interface ProviderServiceShape {
35 /**
36 * Start a provider session.
37 */
38 readonly startSession: (
39 threadId: ThreadId,
40 input: ProviderSessionStartInput,
41 ) => Effect.Effect<ProviderSession, ProviderServiceError>;
42
43 /**
44 * Send a provider turn.
45 */
46 readonly sendTurn: (
47 input: ProviderSendTurnInput,
48 ) => Effect.Effect<ProviderTurnStartResult, ProviderServiceError>;
49
50 /**
51 * Interrupt a running provider turn.
52 */
53 readonly interruptTurn: (
54 input: ProviderInterruptTurnInput,
55 ) => Effect.Effect<void, ProviderServiceError>;
56
57 /**
58 * Respond to a provider approval request.
59 */
60 readonly respondToRequest: (
61 input: ProviderRespondToRequestInput,
62 ) => Effect.Effect<void, ProviderServiceError>;
63
64 /**
65 * Stop a provider session.
66 */
67 readonly stopSession: (
68 input: ProviderStopSessionInput,
69 ) => Effect.Effect<void, ProviderServiceError>;
70
71 /**
72 * List active provider sessions.
73 *
74 * Aggregates runtime session lists from all registered adapters.
75 */
76 readonly listSessions: () => Effect.Effect<ReadonlyArray<ProviderSession>>;
77
78 /**
79 * Roll back provider conversation state by a number of turns.
80 */
81 readonly rollbackConversation: (input: {
82 readonly sessionId: ProviderSessionId;
83 readonly numTurns: number;
84 }) => Effect.Effect<void, ProviderServiceError>;
85
86 /**
87 * Stop all active provider sessions.
88 *
89 * Persisted session bindings remain so stale sessions can be resumed after
90 * process restart.
91 *
92 * Runtime alias mappings are cleared as part of shutdown.
93 */
94 readonly stopAll: () => Effect.Effect<void, ProviderServiceError>;
95
96 /**
97 * Canonical provider runtime event stream.
98 *
99 * Fan-out is owned by ProviderService (not by a standalone event-bus service).
100 */
101 readonly streamEvents: Stream.Stream<ProviderRuntimeEvent>;
102}
103
104/**
105 * ProviderService - Service tag for provider orchestration.
106 */
107export class ProviderService extends ServiceMap.Service<ProviderService, ProviderServiceShape>()(
108 "t3/provider/Services/ProviderService",
109) {}