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

ProviderSessionRuntime.ts

92 lines3.1 KB

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