88 type OrchestrationV2ConversationMessage ,
99 type RunId ,
1010 type ServerSettingsError ,
11+ TextGenerationError ,
1112 type ThreadId ,
1213} from "@t3tools/contracts" ;
1314import * as Cause from "effect/Cause" ;
@@ -17,6 +18,7 @@ import * as FileSystem from "effect/FileSystem";
1718import * as Layer from "effect/Layer" ;
1819import * as Option from "effect/Option" ;
1920import * as Schedule from "effect/Schedule" ;
21+ import * as Schema from "effect/Schema" ;
2022
2123import * as ServerSettings from "../serverSettings.ts" ;
2224import * as TextGeneration from "../textGeneration/TextGeneration.ts" ;
@@ -25,8 +27,24 @@ import * as ProjectStore from "./ProjectStore.ts";
2527import * as ThreadManagementService from "./ThreadManagementService.ts" ;
2628
2729import { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext" ;
30+ import { withoutContextHandoff } from "./ContextHandoffDelivery.ts" ;
2831export { formatThreadTitleContext } from "@t3tools/provider-core/server/threadTitleContext" ;
2932
33+ // TextGenerationError's message names the operation; the user wants the provider's reason.
34+ const isTextGenerationError = Schema . is ( TextGenerationError ) ;
35+ const failureDetail = ( error : unknown ) : string => {
36+ const detail = isTextGenerationError ( error )
37+ ? error . detail
38+ : error instanceof Error
39+ ? error . message
40+ : String ( error ) ;
41+ return detail . trim ( ) . slice ( 0 , 500 ) || "Unknown error." ;
42+ } ;
43+
44+ type TitleOutcome =
45+ | { readonly type : "stale" }
46+ | { readonly type : "complete" ; readonly title ?: string ; readonly failure ?: string } ;
47+
3048export class ThreadTitleRegenerationService extends Context . Service <
3149 ThreadTitleRegenerationService ,
3250 {
@@ -57,6 +75,7 @@ const make = Effect.gen(function* () {
5775 readonly threadId : ThreadId ;
5876 readonly requestId : CommandId ;
5977 readonly title ?: string ;
78+ readonly failure ?: string ;
6079 } ) =>
6180 threads
6281 . dispatch ( {
@@ -65,6 +84,7 @@ const make = Effect.gen(function* () {
6584 threadId : input . threadId ,
6685 requestId : input . requestId ,
6786 ...( input . title === undefined ? { } : { title : input . title } ) ,
87+ ...( input . failure === undefined ? { } : { failure : input . failure } ) ,
6888 } )
6989 . pipe ( Effect . asVoid ) ;
7090
@@ -110,12 +130,10 @@ const make = Effect.gen(function* () {
110130 const execute : ThreadTitleRegenerationService [ "Service" ] [ "execute" ] = Effect . fn (
111131 "ThreadTitleRegenerationService.execute" ,
112132 ) ( function * ( input ) {
113- const outcome :
114- | { readonly type : "stale" }
115- | { readonly type : "complete" ; readonly title ?: string } = yield * Effect . gen ( function * ( ) {
133+ const outcome : TitleOutcome = yield * Effect . gen ( function * ( ) {
116134 const projection = yield * threads . getThreadRecords (
117135 input . threadId ,
118- [ "messages" ] ,
136+ [ "messages" , "providerThreads" ] ,
119137 input . kind . type === "initial"
120138 ? { messageIds : [ input . kind . messageId ] }
121139 : { messageRoles : [ "user" , "assistant" ] } ,
@@ -142,12 +160,16 @@ const make = Effect.gen(function* () {
142160 message === undefined
143161 ? { message : "" , attachments : [ ] }
144162 : {
145- message : isAttachmentOnlyBootstrapPrompt ( message . text ) ? "" : message . text ,
163+ message : isAttachmentOnlyBootstrapPrompt ( message . text )
164+ ? ""
165+ : withoutContextHandoff ( message . text ) ,
146166 attachments : message . attachments ,
147167 } ;
148168 } else {
149169 context = formatThreadTitleContext (
150- projection . messages . filter ( ( message ) => ! message . streaming ) ,
170+ projection . messages
171+ . filter ( ( message ) => ! message . streaming )
172+ . map ( ( message ) => ( { ...message , text : withoutContextHandoff ( message . text ) } ) ) ,
151173 ) ;
152174 }
153175 if ( context . message . length === 0 && context . attachments . length === 0 ) {
@@ -162,6 +184,13 @@ const make = Effect.gen(function* () {
162184 yield * serverSettings . getSettings ,
163185 projection . thread . projectId ,
164186 ) . settings ;
187+ const modelSelection = settings . threadTitleModelSelection ?? projection . thread . modelSelection ;
188+ // Forking the thread's own session keeps the provider's cached prefix warm.
189+ const sourceSessionId = projection . providerThreads . find (
190+ ( providerThread ) =>
191+ providerThread . id === projection . thread . activeProviderThreadId &&
192+ providerThread . providerInstanceId === modelSelection . instanceId ,
193+ ) ?. nativeThreadRef ?. nativeId ;
165194 // Storage cleanup removes worktrees but keeps the path; CLIs cannot spawn in a missing cwd.
166195 const worktreePath = projection . thread . worktreePath ;
167196 const worktreeExists =
@@ -174,8 +203,9 @@ const make = Effect.gen(function* () {
174203 attachments : context . attachments ,
175204 ...( input . kind . type === "regenerate" ? { previousTitle : projection . thread . title } : { } ) ,
176205 ...( inherited === undefined ? { } : { forkSource : inherited } ) ,
206+ ...( sourceSessionId == null ? { } : { sourceSessionId } ) ,
177207 instructions : settings . threadTitleInstructions ,
178- modelSelection : settings . textGenerationModelSelection ,
208+ modelSelection,
179209 projectId : projection . thread . projectId ,
180210 } ) ;
181211 const generatedTitle = result . title . trim ( ) ;
@@ -195,7 +225,12 @@ const make = Effect.gen(function* () {
195225 threadId : input . threadId ,
196226 requestId : input . requestId ,
197227 cause,
198- } ) . pipe ( Effect . as ( { type : "complete" as const } ) ) ,
228+ } ) . pipe (
229+ Effect . as ( {
230+ type : "complete" as const ,
231+ failure : failureDetail ( Cause . squash ( cause ) ) ,
232+ } ) ,
233+ ) ,
199234 ) ,
200235 ) ;
201236
@@ -205,6 +240,7 @@ const make = Effect.gen(function* () {
205240 yield * complete ( {
206241 ...input ,
207242 ...( outcome . title === undefined ? { } : { title : outcome . title } ) ,
243+ ...( outcome . failure === undefined ? { } : { failure : outcome . failure } ) ,
208244 } ) ;
209245 } ) ;
210246
0 commit comments