@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 262 lines 10.2 KB
Raw
1 import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
2 import { isAttachmentOnlyBootstrapPrompt } from "@t3tools/shared/composerContextReferences";
3 import {
4 CommandId,
5 type ChatAttachment,
6 type MessageId,
7 type OrchestrationV2AppThread,
8 type OrchestrationV2ConversationMessage,
9 type RunId,
10 type ServerSettingsError,
11 TextGenerationError,
12 type ThreadId,
13 } from "@t3tools/contracts";
14 import * as Cause from "effect/Cause";
15 import * as Context from "effect/Context";
16 import * as Effect from "effect/Effect";
17 import type * as DateTime from "effect/DateTime";
18 import * as FileSystem from "effect/FileSystem";
19 import * as Layer from "effect/Layer";
20 import * as Option from "effect/Option";
21 import * as Schedule from "effect/Schedule";
22 import * as Schema from "effect/Schema";
23
24 import * as ServerSettings from "../serverSettings.ts";
25 import * as TextGeneration from "../textGeneration/TextGeneration.ts";
26 import type { OrchestratorV2Error } from "./Orchestrator.ts";
27 import * as ProjectStore from "./ProjectStore.ts";
28 import { forkInheritedRunOrdinals, ProjectionStoreThreadNotFoundError } from "./ProjectionStore.ts";
29 import * as ThreadManagementService from "./ThreadManagementService.ts";
30
31 import { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext";
32 export { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext";
33
34 export class ThreadTitleRegenerationService extends Context.Service<
35 ThreadTitleRegenerationService,
36 {
37 readonly execute: (input: {
38 readonly threadId: ThreadId;
39 readonly requestId: CommandId;
40 readonly kind:
41 | { readonly type: "initial"; readonly messageId: MessageId }
42 | { readonly type: "regenerate" };
43 }) => Effect.Effect<
44 void,
45 | OrchestratorV2Error
46 | ProjectStore.ProjectStoreV2Error
47 | ServerSettingsError
48 | ThreadManagementService.ThreadManagementProviderInstanceDisabledError
49 >;
50 }
51 >()("t3/orchestration-v2/ThreadTitleRegenerationService") {}
52
53 type Outcome =
54 | { readonly status: "renamed"; readonly title: string }
55 | { readonly status: "unchanged" }
56 | { readonly status: "failed"; readonly message: string };
57
58 const MAX_FAILURE_MESSAGE_LENGTH = 300;
59 const isTextGenerationError = Schema.is(TextGenerationError);
60 const isProjectionStoreThreadNotFoundError = Schema.is(ProjectionStoreThreadNotFoundError);
61
62 function failureMessage(cause: Cause.Cause<unknown>): string {
63 const error = Cause.squash(cause);
64 const lines = isTextGenerationError(error)
65 ? error.detail
66 .split("\n")
67 .map((line) => line.trim())
68 .filter((line) => line.length > 0)
69 : [];
70 const detail = lines.at(-1);
71 if (detail === undefined) return "Title generation failed.";
72 return detail.length > MAX_FAILURE_MESSAGE_LENGTH
73 ? `${detail.slice(0, MAX_FAILURE_MESSAGE_LENGTH - 1)}…`
74 : detail;
75 }
76
77 const make = Effect.gen(function* () {
78 const threads = yield* ThreadManagementService.ThreadManagementService;
79 const projects = yield* ProjectStore.ProjectStoreV2;
80 const serverSettings = yield* ServerSettings.ServerSettingsService;
81 const textGeneration = yield* TextGeneration.TextGeneration;
82 const fileSystem = yield* FileSystem.FileSystem;
83
84 const complete = (input: {
85 readonly threadId: ThreadId;
86 readonly requestId: CommandId;
87 readonly kind: { readonly type: "initial" | "regenerate" };
88 readonly outcome: Outcome;
89 }) =>
90 threads
91 .dispatch({
92 type: "thread.title.regeneration.complete",
93 commandId: CommandId.make(`${input.requestId}:title-complete`),
94 threadId: input.threadId,
95 requestId: input.requestId,
96 ...(input.outcome.status === "renamed" ? { title: input.outcome.title } : {}),
97 ...(input.kind.type === "regenerate"
98 ? {
99 outcome: {
100 status: input.outcome.status,
101 ...(input.outcome.status === "failed" ? { message: input.outcome.message } : {}),
102 },
103 }
104 : {}),
105 })
106 .pipe(Effect.asVoid);
107
108 const messagesThroughRun = Effect.fn("ThreadTitleRegenerationService.messagesThroughRun")(
109 function* (threadId: ThreadId, runId: RunId, forkCreatedAt: DateTime.Utc) {
110 const source = yield* threads.getThreadRecords(threadId, ["messages", "runs"], {
111 messageRoles: ["user", "assistant"],
112 });
113 const ordinals = forkInheritedRunOrdinals(source.runs, forkCreatedAt);
114 const forkOrdinal = ordinals.get(runId);
115 if (forkOrdinal === undefined) return undefined;
116 const messages = source.messages.filter((message) => {
117 if (message.streaming) return false;
118 if (message.runId === null) return source.thread.historyOrigin === "v1_import";
119 return (ordinals.get(message.runId) ?? Infinity) <= forkOrdinal;
120 });
121 return { thread: source.thread, messages };
122 },
123 );
124
125 // A fork starts with no messages of its own, so its subject lives in the history it inherited.
126 const forkSource = Effect.fn("ThreadTitleRegenerationService.forkSource")(function* (
127 thread: OrchestrationV2AppThread,
128 ) {
129 let title: string | undefined;
130 const history: Array<ReadonlyArray<OrchestrationV2ConversationMessage>> = [];
131 const seen = new Set([thread.id]);
132 let current = thread;
133 while (
134 current.forkedFrom?.type === "run" &&
135 current.lineage.sideChat !== true &&
136 !seen.has(current.forkedFrom.threadId)
137 ) {
138 const { threadId, runId } = current.forkedFrom;
139 seen.add(threadId);
140 const source = yield* messagesThroughRun(threadId, runId, current.createdAt);
141 if (source === undefined) break;
142 history.push(source.messages);
143 title ??= source.thread.title;
144 current = source.thread;
145 }
146 const context = formatThreadTitleContext(history.reverse().flat());
147 return title === undefined || context.message.length === 0
148 ? undefined
149 : { title, history: context.message };
150 });
151
152 const execute: ThreadTitleRegenerationService["Service"]["execute"] = Effect.fn(
153 "ThreadTitleRegenerationService.execute",
154 )(function* (input) {
155 const outcome: Outcome | { readonly status: "stale" } = yield* Effect.gen(function* () {
156 const projection = yield* threads.getThreadRecords(
157 input.threadId,
158 ["messages"],
159 input.kind.type === "initial"
160 ? { messageIds: [input.kind.messageId] }
161 : { messageRoles: ["user", "assistant"] },
162 );
163 if (projection.thread.titleRegeneration?.requestId !== input.requestId) {
164 return { status: "stale" as const };
165 }
166
167 const project = yield* projects.get(projection.thread.projectId);
168 if (Option.isNone(project)) {
169 return { status: "failed" as const, message: "This thread's project no longer exists." };
170 }
171
172 let context: {
173 readonly message: string;
174 readonly attachments: ReadonlyArray<ChatAttachment>;
175 };
176 if (input.kind.type === "initial") {
177 const messageId = input.kind.messageId;
178 const message = projection.messages.find(
179 (candidate) => candidate.id === messageId && !candidate.streaming,
180 );
181 context =
182 message === undefined
183 ? { message: "", attachments: [] }
184 : {
185 message: isAttachmentOnlyBootstrapPrompt(message.text) ? "" : message.text,
186 attachments: message.attachments,
187 };
188 } else {
189 context = formatThreadTitleContext(
190 projection.messages.filter((message) => !message.streaming),
191 );
192 }
193 // A deleted source should not cost the fork its title.
194 const inherited = yield* forkSource(projection.thread).pipe(
195 Effect.catchTags({
196 OrchestratorProjectionError: (error) =>
197 isProjectionStoreThreadNotFoundError(error.cause)
198 ? Effect.undefined
199 : Effect.fail(error),
200 }),
201 );
202 if (
203 context.message.length === 0 &&
204 context.attachments.length === 0 &&
205 inherited === undefined
206 ) {
207 return {
208 status: "failed" as const,
209 message: "This thread has no messages to title yet.",
210 };
211 }
212 const settings = resolveProjectSettings(
213 yield* serverSettings.getSettings,
214 projection.thread.projectId,
215 ).settings;
216 // Storage cleanup removes worktrees but keeps the path; CLIs cannot spawn in a missing cwd.
217 const worktreePath = projection.thread.worktreePath;
218 const worktreeExists =
219 worktreePath !== null &&
220 (yield* fileSystem.exists(worktreePath).pipe(Effect.orElseSucceed(() => false)));
221 const result = yield* textGeneration.generateThreadTitle({
222 cwd: worktreeExists ? worktreePath : project.value.workspaceRoot,
223 message: context.message,
224 projectTitle: project.value.title,
225 attachments: context.attachments,
226 ...(input.kind.type === "regenerate" ? { previousTitle: projection.thread.title } : {}),
227 ...(inherited === undefined ? {} : { forkSource: inherited }),
228 instructions: settings.threadTitleInstructions,
229 modelSelection: settings.textGenerationModelSelection,
230 projectId: projection.thread.projectId,
231 });
232 const generatedTitle = result.title.trim();
233 return generatedTitle === "New thread" ||
234 (input.kind.type === "regenerate" && generatedTitle === projection.thread.title.trim())
235 ? { status: "unchanged" as const }
236 : { status: "renamed" as const, title: result.title };
237 }).pipe(
238 Effect.retry({
239 times: input.kind.type === "initial" ? 2 : 0,
240 schedule: Schedule.exponential("2 seconds"),
241 }),
242 Effect.catchCause((cause) =>
243 Cause.hasInterruptsOnly(cause)
244 ? Effect.interrupt
245 : Effect.logWarning("Thread title generation failed", {
246 threadId: input.threadId,
247 requestId: input.requestId,
248 cause,
249 }).pipe(Effect.as({ status: "failed" as const, message: failureMessage(cause) })),
250 ),
251 );
252
253 if (outcome.status === "stale") {
254 return;
255 }
256 yield* complete({ ...input, outcome });
257 });
258
259 return ThreadTitleRegenerationService.of({ execute });
260 });
261
262 export const layer = Layer.effect(ThreadTitleRegenerationService, make);