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