import { type ChatAttachment, CommandId, 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 type { ProjectionRepositoryError } from "../persistence/Errors.ts"; import { ProjectionProjectRepository } from "../persistence/Services/ProjectionProjects.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as TextGeneration from "../textGeneration/TextGeneration.ts"; import type { OrchestratorV2Error } from "./Orchestrator.ts"; import { ThreadManagementService } from "./ThreadManagementService.ts"; const MAX_REGENERATION_ATTACHMENTS = 4; const MAX_THREAD_TITLE_CONTEXT_CHARS = 8_000; const MAX_FIRST_USER_TITLE_CONTEXT_CHARS = 2_000; const THREAD_TITLE_CONTEXT_TRUNCATION_MARKER = "[Earlier content truncated]\n\n"; const FIRST_USER_CONTEXT_TRUNCATION_MARKER = "\n[First user message truncated]"; type ThreadTitleMessage = { readonly role: "user" | "assistant" | "system"; readonly text: string; readonly attachments?: ReadonlyArray | undefined; }; function formatThreadTitleSection(message: ThreadTitleMessage): string | undefined { if (message.role === "system") { return undefined; } const text = message.text.trim(); const attachmentSummary = (message.attachments ?? []) .map((attachment) => attachment.name) .join(", "); const contents = [ ...(text.length > 0 ? [text] : []), ...(attachmentSummary.length > 0 ? [`[Attachments: ${attachmentSummary}]`] : []), ].join("\n"); return contents.length > 0 ? `${message.role.toUpperCase()}:\n${contents}` : undefined; } function limitFirstUserSection(section: string): string { if (section.length <= MAX_FIRST_USER_TITLE_CONTEXT_CHARS) { return section; } return `${section.slice( 0, MAX_FIRST_USER_TITLE_CONTEXT_CHARS - FIRST_USER_CONTEXT_TRUNCATION_MARKER.length, )}${FIRST_USER_CONTEXT_TRUNCATION_MARKER}`; } function collectRecentThreadTitleContext( messages: ReadonlyArray, maxChars: number, ): { readonly context: string; readonly attachments: ReadonlyArray; readonly truncated: boolean; } { let context = ""; let truncated = false; const retainedAttachments: Array = []; for (const message of messages.toReversed()) { const section = formatThreadTitleSection(message); if (section === undefined) { continue; } const separator = context.length > 0 ? "\n\n" : ""; const available = maxChars - context.length - separator.length; if (section.length > available) { if (available > 0) { context = `${section.slice(-available)}${separator}${context}`; retainedAttachments.unshift(...(message.attachments ?? [])); } truncated = true; break; } context = `${section}${separator}${context}`; retainedAttachments.unshift(...(message.attachments ?? [])); } return { context, attachments: retainedAttachments, truncated }; } /** * Conversation digest for the title prompt. Recent messages fill the budget * newest-first; when the conversation no longer fits, the first user message * is pinned ahead of the recent tail so regenerated titles stay anchored to * what the thread is actually about. Mirrors the v1 reactor's flow (#5365). */ export function formatThreadTitleContext(messages: ReadonlyArray): { readonly message: string; readonly attachments: ReadonlyArray; } { const recent = collectRecentThreadTitleContext(messages, MAX_THREAD_TITLE_CONTEXT_CHARS); if (!recent.truncated) { return { message: recent.context, attachments: recent.attachments.slice(-MAX_REGENERATION_ATTACHMENTS), }; } const firstUserMessage = messages.find( (message) => message.role === "user" && formatThreadTitleSection(message), ); const firstUserSection = firstUserMessage ? formatThreadTitleSection(firstUserMessage) : undefined; if (!firstUserMessage || !firstUserSection) { return { message: `${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${recent.context}`, attachments: recent.attachments.slice(-MAX_REGENERATION_ATTACHMENTS), }; } const pinnedSection = limitFirstUserSection(firstUserSection); const recentContextBudget = MAX_THREAD_TITLE_CONTEXT_CHARS - pinnedSection.length - "\n\n".length - THREAD_TITLE_CONTEXT_TRUNCATION_MARKER.length; const retainedRecent = collectRecentThreadTitleContext(messages, recentContextBudget); const pinnedAttachment = firstUserMessage.attachments?.[0]; const recentAttachments = retainedRecent.attachments.filter( (attachment) => attachment.id !== pinnedAttachment?.id, ); return { message: `${pinnedSection}\n\n${THREAD_TITLE_CONTEXT_TRUNCATION_MARKER}${retainedRecent.context}`, attachments: [ ...(pinnedAttachment ? [pinnedAttachment] : []), ...recentAttachments.slice( -(MAX_REGENERATION_ATTACHMENTS - (pinnedAttachment === undefined ? 0 : 1)), ), ], }; } 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 | ProjectionRepositoryError | ServerSettingsError >; } >()("t3/orchestration-v2/ThreadTitleRegenerationService") {} export const make = Effect.gen(function* () { const threads = yield* ThreadManagementService; const projects = yield* ProjectionProjectRepository; 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.getThreadProjection(input.threadId); if (projection.thread.titleRegeneration?.requestId !== input.requestId) { return { type: "stale" as const }; } const project = yield* projects.getById({ projectId: projection.thread.projectId }); if (Option.isNone(project)) { return { type: "complete" as const }; } let context: { readonly message: string; readonly attachments: ReadonlyArray; }; 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 = yield* serverSettings.getSettings; 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.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);