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" ;
@@ -46,6 +48,29 @@ export class ThreadTitleRegenerationService extends Context.Service<
4648 }
4749> ( ) ( "t3/orchestration-v2/ThreadTitleRegenerationService" ) { }
4850
51+ type Outcome =
52+ | { readonly status : "renamed" ; readonly title : string }
53+ | { readonly status : "unchanged" }
54+ | { readonly status : "failed" ; readonly message : string } ;
55+
56+ const MAX_FAILURE_MESSAGE_LENGTH = 300 ;
57+ const isTextGenerationError = Schema . is ( TextGenerationError ) ;
58+
59+ function failureMessage ( cause : Cause . Cause < unknown > ) : string {
60+ const error = Cause . squash ( cause ) ;
61+ const lines = isTextGenerationError ( error )
62+ ? error . detail
63+ . split ( "\n" )
64+ . map ( ( line ) => line . trim ( ) )
65+ . filter ( ( line ) => line . length > 0 )
66+ : [ ] ;
67+ const detail = lines . at ( - 1 ) ;
68+ if ( detail === undefined ) return "Title generation failed." ;
69+ return detail . length > MAX_FAILURE_MESSAGE_LENGTH
70+ ? `${ detail . slice ( 0 , MAX_FAILURE_MESSAGE_LENGTH - 1 ) } …`
71+ : detail ;
72+ }
73+
4974const make = Effect . gen ( function * ( ) {
5075 const threads = yield * ThreadManagementService . ThreadManagementService ;
5176 const projects = yield * ProjectStore . ProjectStoreV2 ;
@@ -56,15 +81,24 @@ const make = Effect.gen(function* () {
5681 const complete = ( input : {
5782 readonly threadId : ThreadId ;
5883 readonly requestId : CommandId ;
59- readonly title ?: string ;
84+ readonly kind : { readonly type : "initial" | "regenerate" } ;
85+ readonly outcome : Outcome ;
6086 } ) =>
6187 threads
6288 . dispatch ( {
6389 type : "thread.title.regeneration.complete" ,
6490 commandId : CommandId . make ( `${ input . requestId } :title-complete` ) ,
6591 threadId : input . threadId ,
6692 requestId : input . requestId ,
67- ...( input . title === undefined ? { } : { title : input . title } ) ,
93+ ...( input . outcome . status === "renamed" ? { title : input . outcome . title } : { } ) ,
94+ ...( input . kind . type === "regenerate"
95+ ? {
96+ outcome : {
97+ status : input . outcome . status ,
98+ ...( input . outcome . status === "failed" ? { message : input . outcome . message } : { } ) ,
99+ } ,
100+ }
101+ : { } ) ,
68102 } )
69103 . pipe ( Effect . asVoid ) ;
70104
@@ -110,9 +144,7 @@ const make = Effect.gen(function* () {
110144 const execute : ThreadTitleRegenerationService [ "Service" ] [ "execute" ] = Effect . fn (
111145 "ThreadTitleRegenerationService.execute" ,
112146 ) ( function * ( input ) {
113- const outcome :
114- | { readonly type : "stale" }
115- | { readonly type : "complete" ; readonly title ?: string } = yield * Effect . gen ( function * ( ) {
147+ const outcome : Outcome | { readonly status : "stale" } = yield * Effect . gen ( function * ( ) {
116148 const projection = yield * threads . getThreadRecords (
117149 input . threadId ,
118150 [ "messages" ] ,
@@ -121,12 +153,12 @@ const make = Effect.gen(function* () {
121153 : { messageRoles : [ "user" , "assistant" ] } ,
122154 ) ;
123155 if ( projection . thread . titleRegeneration ?. requestId !== input . requestId ) {
124- return { type : "stale" as const } ;
156+ return { status : "stale" as const } ;
125157 }
126158
127159 const project = yield * projects . get ( projection . thread . projectId ) ;
128160 if ( Option . isNone ( project ) ) {
129- return { type : "complete " as const } ;
161+ return { status : "failed " as const , message : "This thread's project no longer exists." } ;
130162 }
131163
132164 let context : {
@@ -151,7 +183,10 @@ const make = Effect.gen(function* () {
151183 ) ;
152184 }
153185 if ( context . message . length === 0 && context . attachments . length === 0 ) {
154- return { type : "complete" as const } ;
186+ return {
187+ status : "failed" as const ,
188+ message : "This thread has no messages to title yet." ,
189+ } ;
155190 }
156191
157192 // A deleted source should not cost the fork its title.
@@ -179,8 +214,8 @@ const make = Effect.gen(function* () {
179214 const generatedTitle = result . title . trim ( ) ;
180215 return generatedTitle === "New thread" ||
181216 ( input . kind . type === "regenerate" && generatedTitle === projection . thread . title . trim ( ) )
182- ? { type : "complete " as const }
183- : { type : "complete " as const , title : result . title } ;
217+ ? { status : "unchanged " as const }
218+ : { status : "renamed " as const , title : result . title } ;
184219 } ) . pipe (
185220 Effect . retry ( {
186221 times : input . kind . type === "initial" ? 2 : 0 ,
@@ -193,17 +228,14 @@ const make = Effect.gen(function* () {
193228 threadId : input . threadId ,
194229 requestId : input . requestId ,
195230 cause,
196- } ) . pipe ( Effect . as ( { type : "complete " as const } ) ) ,
231+ } ) . pipe ( Effect . as ( { status : "failed " as const , message : failureMessage ( cause ) } ) ) ,
197232 ) ,
198233 ) ;
199234
200- if ( outcome . type === "stale" ) {
235+ if ( outcome . status === "stale" ) {
201236 return ;
202237 }
203- yield * complete ( {
204- ...input ,
205- ...( outcome . title === undefined ? { } : { title : outcome . title } ) ,
206- } ) ;
238+ yield * complete ( { ...input , outcome } ) ;
207239 } ) ;
208240
209241 return ThreadTitleRegenerationService . of ( { execute } ) ;
0 commit comments