Open History to see the last commit
1/**
2 * ProjectionThreadActivityRepository - Projection repository interface for thread activity.
3 *
4 * Owns persistence operations for activity timeline entries projected from
5 * orchestration events.
6 *
7 * @module ProjectionThreadActivityRepository
8 */
9import { EventId, IsoDateTime, NonNegativeInt, ThreadId, TurnId } from "@t3tools/contracts";
10import { OrchestrationThreadActivityTone } from "@t3tools/contracts/legacy-orchestration";
11import * as Schema from "effect/Schema";
12import * as Context from "effect/Context";
13import type * as Effect from "effect/Effect";
14import type * as Option from "effect/Option";
15
16import type { ProjectionRepositoryError } from "../Errors.ts";
17
18export const ProjectionThreadActivity = Schema.Struct({
19 activityId: EventId,
20 threadId: ThreadId,
21 turnId: Schema.NullOr(TurnId),
22 tone: OrchestrationThreadActivityTone,
23 kind: Schema.String,
24 summary: Schema.String,
25 payload: Schema.Unknown,
26 sequence: Schema.optional(NonNegativeInt),
27 createdAt: IsoDateTime,
28});
29export type ProjectionThreadActivity = typeof ProjectionThreadActivity.Type;
30
31export const ListProjectionThreadActivitiesInput = Schema.Struct({
32 threadId: ThreadId,
33 activityKinds: Schema.optional(Schema.Array(Schema.String)),
34 limit: Schema.optional(NonNegativeInt),
35});
36export type ListProjectionThreadActivitiesInput = typeof ListProjectionThreadActivitiesInput.Type;
37
38export const GetLatestProjectionThreadTaskActivityInput = Schema.Struct({
39 threadId: ThreadId,
40 taskId: Schema.String,
41});
42export type GetLatestProjectionThreadTaskActivityInput =
43 typeof GetLatestProjectionThreadTaskActivityInput.Type;
44
45export const DeleteProjectionThreadActivitiesInput = Schema.Struct({
46 threadId: ThreadId,
47});
48export type DeleteProjectionThreadActivitiesInput =
49 typeof DeleteProjectionThreadActivitiesInput.Type;
50
51/**
52 * ProjectionThreadActivityRepositoryShape - Service API for projected thread activity.
53 */
54export interface ProjectionThreadActivityRepositoryShape {
55 /**
56 * Insert or replace a projected thread activity row.
57 *
58 * Upserts by `activityId` and JSON-encodes payload.
59 */
60 readonly upsert: (
61 row: ProjectionThreadActivity,
62 ) => Effect.Effect<void, ProjectionRepositoryError>;
63
64 /**
65 * List projected thread activity rows for a thread.
66 *
67 * Returned in ascending runtime sequence order (or creation order when
68 * sequence is unavailable). A limit selects the newest matching rows.
69 */
70 readonly listByThreadId: (
71 input: ListProjectionThreadActivitiesInput,
72 ) => Effect.Effect<ReadonlyArray<ProjectionThreadActivity>, ProjectionRepositoryError>;
73
74 /**
75 * List activity rows used to derive pending user-input state.
76 *
77 * Filters in SQLite so unrelated payloads do not enter server memory.
78 */
79 readonly listUserInputLifecycleByThreadId: (
80 input: ListProjectionThreadActivitiesInput,
81 ) => Effect.Effect<ReadonlyArray<ProjectionThreadActivity>, ProjectionRepositoryError>;
82
83 /**
84 * Read the latest task-start or task-progress activity with a usable title.
85 */
86 readonly getLatestTaskActivity: (
87 input: GetLatestProjectionThreadTaskActivityInput,
88 ) => Effect.Effect<Option.Option<ProjectionThreadActivity>, ProjectionRepositoryError>;
89
90 /**
91 * Delete projected thread activity rows by thread.
92 */
93 readonly deleteByThreadId: (
94 input: DeleteProjectionThreadActivitiesInput,
95 ) => Effect.Effect<void, ProjectionRepositoryError>;
96}
97
98/**
99 * ProjectionThreadActivityRepository - Service tag for thread activity persistence.
100 */
101export class ProjectionThreadActivityRepository extends Context.Service<
102 ProjectionThreadActivityRepository,
103 ProjectionThreadActivityRepositoryShape
104>()("t3/persistence/Services/ProjectionThreadActivities/ProjectionThreadActivityRepository") {}