Open History to see the last commit
1/**
2 * ProviderSessionRuntimeRepository - Repository interface for provider runtime sessions.
3 *
4 * Owns persistence operations for provider runtime metadata and resume cursors.
5 *
6 * @module ProviderSessionRuntimeRepository
7 */
8import {
9 IsoDateTime,
10 ProviderInstanceId,
11 ProviderSessionRuntimeStatus,
12 RuntimeMode,
13 ThreadId,
14} from "@t3tools/contracts";
15import { Option, Schema, Context } from "effect";
16import type { Effect } from "effect";
17
18import type { ProviderSessionRuntimeRepositoryError } from "../Errors.ts";
19
20export const ProviderSessionRuntime = Schema.Struct({
21 threadId: ThreadId,
22 providerName: Schema.String,
23 /**
24 * User-defined routing key for the configured provider instance that
25 * owns this session. Nullable only at the storage/migration boundary:
26 * rows persisted before the driver/instance split carry only
27 * `providerName`. Repository consumers must materialize a concrete
28 * instance id before routing.
29 */
30 providerInstanceId: Schema.NullOr(ProviderInstanceId),
31 adapterKey: Schema.String,
32 runtimeMode: RuntimeMode,
33 status: ProviderSessionRuntimeStatus,
34 lastSeenAt: IsoDateTime,
35 resumeCursor: Schema.NullOr(Schema.Unknown),
36 runtimePayload: Schema.NullOr(Schema.Unknown),
37});
38export type ProviderSessionRuntime = typeof ProviderSessionRuntime.Type;
39
40export const GetProviderSessionRuntimeInput = Schema.Struct({ threadId: ThreadId });
41export type GetProviderSessionRuntimeInput = typeof GetProviderSessionRuntimeInput.Type;
42
43export const DeleteProviderSessionRuntimeInput = Schema.Struct({ threadId: ThreadId });
44export type DeleteProviderSessionRuntimeInput = typeof DeleteProviderSessionRuntimeInput.Type;
45
46/**
47 * ProviderSessionRuntimeRepositoryShape - Service API for provider runtime records.
48 */
49export interface ProviderSessionRuntimeRepositoryShape {
50 /**
51 * Insert or replace a provider runtime row.
52 *
53 * Upserts by canonical `threadId`, including JSON payload/cursor fields.
54 */
55 readonly upsert: (
56 runtime: ProviderSessionRuntime,
57 ) => Effect.Effect<void, ProviderSessionRuntimeRepositoryError>;
58
59 /**
60 * Read provider runtime state by canonical thread id.
61 */
62 readonly getByThreadId: (
63 input: GetProviderSessionRuntimeInput,
64 ) => Effect.Effect<Option.Option<ProviderSessionRuntime>, ProviderSessionRuntimeRepositoryError>;
65
66 /**
67 * List all provider runtime rows.
68 *
69 * Returned in ascending last-seen order.
70 */
71 readonly list: () => Effect.Effect<
72 ReadonlyArray<ProviderSessionRuntime>,
73 ProviderSessionRuntimeRepositoryError
74 >;
75
76 /**
77 * Delete provider runtime state by canonical thread id.
78 */
79 readonly deleteByThreadId: (
80 input: DeleteProviderSessionRuntimeInput,
81 ) => Effect.Effect<void, ProviderSessionRuntimeRepositoryError>;
82}
83
84/**
85 * ProviderSessionRuntimeRepository - Service tag for provider runtime persistence.
86 */
87export class ProviderSessionRuntimeRepository extends Context.Service<
88 ProviderSessionRuntimeRepository,
89 ProviderSessionRuntimeRepositoryShape
90>()("t3/persistence/Services/ProviderSessionRuntime/ProviderSessionRuntimeRepository") {}