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

ThreadLifecycleService.ts

149 lines5.5 KB

1import {
2 CommandId,
3 type ModelSelection,
4 type OrchestrationV2ThreadProjection,
5 type ProviderInteractionMode,
6 type RuntimeMode,
7 ThreadId,
8} from "@t3tools/contracts";
9import * as Context from "effect/Context";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as Schema from "effect/Schema";
13​
14import * as ThreadManagement from "./ThreadManagementService.ts";
15​
16export class ThreadLifecycleError extends Schema.TaggedError<ThreadLifecycleError>()(
17 "ThreadLifecycleError",
18 {
19 operation: Schema.Literals([
20 "archive",
21 "unarchive",
22 "delete",
23 "update-metadata",
24 "set-runtime-mode",
25 "set-interaction-mode",
26 "set-model-selection",
27 ]),
28 threadId: ThreadId,
29 cause: Schema.Defect(),
30 },
31) {
32 override get message(): string {
33 return `Thread lifecycle operation '${this.operation}' failed for ${this.threadId}.`;
34 }
36​
37export class ThreadLifecycleService extends Context.Service<
38 ThreadLifecycleService,
39 {
40 readonly archive: (input: {
41 readonly commandId: CommandId;
42 readonly threadId: ThreadId;
43 }) => Effect.Effect<Pick<OrchestrationV2ThreadProjection, "thread">, ThreadLifecycleError>;
44 readonly unarchive: (input: {
45 readonly commandId: CommandId;
46 readonly threadId: ThreadId;
47 }) => Effect.Effect<Pick<OrchestrationV2ThreadProjection, "thread">, ThreadLifecycleError>;
48 readonly delete: (input: {
49 readonly commandId: CommandId;
50 readonly threadId: ThreadId;
51 }) => Effect.Effect<Pick<OrchestrationV2ThreadProjection, "thread">, ThreadLifecycleError>;
52 readonly updateMetadata: (input: {
53 readonly commandId: CommandId;
54 readonly threadId: ThreadId;
55 readonly title?: string;
56 readonly branch?: string | null;
57 readonly worktreePath?: string | null;
58 }) => Effect.Effect<Pick<OrchestrationV2ThreadProjection, "thread">, ThreadLifecycleError>;
59 readonly setRuntimeMode: (input: {
60 readonly commandId: CommandId;
61 readonly threadId: ThreadId;
62 readonly runtimeMode: RuntimeMode;
63 }) => Effect.Effect<Pick<OrchestrationV2ThreadProjection, "thread">, ThreadLifecycleError>;
64 readonly setInteractionMode: (input: {
65 readonly commandId: CommandId;
66 readonly threadId: ThreadId;
67 readonly interactionMode: ProviderInteractionMode;
68 }) => Effect.Effect<Pick<OrchestrationV2ThreadProjection, "thread">, ThreadLifecycleError>;
69 readonly setModelSelection: (input: {
70 readonly commandId: CommandId;
71 readonly threadId: ThreadId;
72 readonly modelSelection: ModelSelection;
73 }) => Effect.Effect<Pick<OrchestrationV2ThreadProjection, "thread">, ThreadLifecycleError>;
74 }
75>()("t3/orchestration-v2/ThreadLifecycleService") {}
76​
77const make = Effect.gen(function* () {
78 const threads = yield* ThreadManagement.ThreadManagementService;
79​
80 const dispatch = <Operation extends ThreadLifecycleError["operation"]>(
81 operation: Operation,
82 threadId: ThreadId,
83 command: Parameters<ThreadManagement.ThreadManagementService["Service"]["dispatch"]>[0],
84 ) =>
85 threads.dispatch(command).pipe(
86 Effect.andThen(threads.getThreadRecords(threadId, [])),
87 Effect.mapError((cause) => new ThreadLifecycleError({ operation, threadId, cause })),
88 );
89​
90 return ThreadLifecycleService.of({
91 archive: Effect.fn("ThreadLifecycleService.archive")(function (input) {
92 return dispatch("archive", input.threadId, {
93 type: "thread.archive",
94 commandId: input.commandId,
95 threadId: input.threadId,
96 });
97 }),
98 unarchive: Effect.fn("ThreadLifecycleService.unarchive")(function (input) {
99 return dispatch("unarchive", input.threadId, {
100 type: "thread.unarchive",
101 commandId: input.commandId,
102 threadId: input.threadId,
103 });
104 }),
105 delete: Effect.fn("ThreadLifecycleService.delete")(function (input) {
106 return dispatch("delete", input.threadId, {
107 type: "thread.delete",
108 commandId: input.commandId,
109 threadId: input.threadId,
110 });
111 }),
112 updateMetadata: Effect.fn("ThreadLifecycleService.updateMetadata")(function (input) {
113 return dispatch("update-metadata", input.threadId, {
114 type: "thread.metadata.update",
115 commandId: input.commandId,
116 threadId: input.threadId,
117 ...(input.title === undefined ? {} : { title: input.title }),
118 ...(input.branch === undefined ? {} : { branch: input.branch }),
119 ...(input.worktreePath === undefined ? {} : { worktreePath: input.worktreePath }),
120 });
121 }),
122 setRuntimeMode: Effect.fn("ThreadLifecycleService.setRuntimeMode")(function (input) {
123 return dispatch("set-runtime-mode", input.threadId, {
124 type: "thread.runtime-mode.set",
125 commandId: input.commandId,
126 threadId: input.threadId,
127 runtimeMode: input.runtimeMode,
128 });
129 }),
130 setInteractionMode: Effect.fn("ThreadLifecycleService.setInteractionMode")(function (input) {
131 return dispatch("set-interaction-mode", input.threadId, {
132 type: "thread.interaction-mode.set",
133 commandId: input.commandId,
134 threadId: input.threadId,
135 interactionMode: input.interactionMode,
136 });
137 }),
138 setModelSelection: Effect.fn("ThreadLifecycleService.setModelSelection")(function (input) {
139 return dispatch("set-model-selection", input.threadId, {
140 type: "thread.model-selection.set",
141 commandId: input.commandId,
142 threadId: input.threadId,
143 modelSelection: input.modelSelection,
144 });
145 }),
146 });
147});
148​
149export const layer = Layer.effect(ThreadLifecycleService, make);