Files
t3code/apps/server/src/orchestration-v2/ThreadTitleRegenerationService.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

151 lines
5.4 KiB
TypeScript

import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
import {
CommandId,
type ChatAttachment,
type MessageId,
type ServerSettingsError,
type ThreadId,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schedule from "effect/Schedule";
import * as ServerSettings from "../serverSettings.ts";
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
import type { OrchestratorV2Error } from "./Orchestrator.ts";
import * as ProjectStore from "./ProjectStore.ts";
import * as ThreadManagementService from "./ThreadManagementService.ts";
import { formatThreadTitleContext } from "../textGeneration/ThreadTitleContext.ts";
export { formatThreadTitleContext } from "../textGeneration/ThreadTitleContext.ts";
export class ThreadTitleRegenerationService extends Context.Service<
ThreadTitleRegenerationService,
{
readonly execute: (input: {
readonly threadId: ThreadId;
readonly requestId: CommandId;
readonly kind:
| { readonly type: "initial"; readonly messageId: MessageId }
| { readonly type: "regenerate" };
}) => Effect.Effect<
void,
OrchestratorV2Error | ProjectStore.ProjectStoreV2Error | ServerSettingsError
>;
}
>()("t3/orchestration-v2/ThreadTitleRegenerationService") {}
const make = Effect.gen(function* () {
const threads = yield* ThreadManagementService.ThreadManagementService;
const projects = yield* ProjectStore.ProjectStoreV2;
const serverSettings = yield* ServerSettings.ServerSettingsService;
const textGeneration = yield* TextGeneration.TextGeneration;
const complete = (input: {
readonly threadId: ThreadId;
readonly requestId: CommandId;
readonly title?: string;
}) =>
threads
.dispatch({
type: "thread.title.regeneration.complete",
commandId: CommandId.make(`${input.requestId}:title-complete`),
threadId: input.threadId,
requestId: input.requestId,
...(input.title === undefined ? {} : { title: input.title }),
})
.pipe(Effect.asVoid);
const execute: ThreadTitleRegenerationService["Service"]["execute"] = Effect.fn(
"ThreadTitleRegenerationService.execute",
)(function* (input) {
const outcome:
| { readonly type: "stale" }
| { readonly type: "complete"; readonly title?: string } = yield* Effect.gen(function* () {
const projection = yield* threads.getThreadRecords(
input.threadId,
["messages"],
input.kind.type === "initial"
? { messageIds: [input.kind.messageId] }
: { messageRoles: ["user", "assistant"] },
);
if (projection.thread.titleRegeneration?.requestId !== input.requestId) {
return { type: "stale" as const };
}
const project = yield* projects.get(projection.thread.projectId);
if (Option.isNone(project)) {
return { type: "complete" as const };
}
let context: {
readonly message: string;
readonly attachments: ReadonlyArray<ChatAttachment>;
};
if (input.kind.type === "initial") {
const messageId = input.kind.messageId;
const message = projection.messages.find(
(candidate) => candidate.id === messageId && !candidate.streaming,
);
context =
message === undefined
? { message: "", attachments: [] }
: { message: message.text, attachments: message.attachments };
} else {
context = formatThreadTitleContext(
projection.messages.filter((message) => !message.streaming),
);
}
if (context.message.length === 0 && context.attachments.length === 0) {
return { type: "complete" as const };
}
const settings = resolveProjectSettings(
yield* serverSettings.getSettings,
projection.thread.projectId,
).settings;
const result = yield* textGeneration.generateThreadTitle({
cwd: projection.thread.worktreePath ?? project.value.workspaceRoot,
message: context.message,
attachments: context.attachments,
...(input.kind.type === "regenerate" ? { previousTitle: projection.thread.title } : {}),
modelSelection: settings.textGenerationModelSelection,
});
const generatedTitle = result.title.trim();
return generatedTitle === "New thread" ||
(input.kind.type === "regenerate" && generatedTitle === projection.thread.title.trim())
? { type: "complete" as const }
: { type: "complete" as const, title: result.title };
}).pipe(
Effect.retry({
times: input.kind.type === "initial" ? 2 : 0,
schedule: Schedule.exponential("2 seconds"),
}),
Effect.catchCause((cause) =>
Cause.hasInterruptsOnly(cause)
? Effect.interrupt
: Effect.logWarning("Thread title generation failed", {
threadId: input.threadId,
requestId: input.requestId,
cause,
}).pipe(Effect.as({ type: "complete" as const })),
),
);
if (outcome.type === "stale") {
return;
}
yield* complete({
...input,
...(outcome.title === undefined ? {} : { title: outcome.title }),
});
});
return ThreadTitleRegenerationService.of({ execute });
});
export const layer = Layer.effect(ThreadTitleRegenerationService, make);