@@ -23,7 +23,7 @@ import type {
2323 ImagePart ,
2424 WorkspaceChatMessage ,
2525} from "@/common/types/ipc" ;
26- import { Ok , Err } from "@/common/types/result" ;
26+ import { Ok , Err , type Result } from "@/common/types/result" ;
2727import { validateWorkspaceName } from "@/common/utils/validation/workspaceValidation" ;
2828import type {
2929 WorkspaceMetadata ,
@@ -240,11 +240,33 @@ export class IpcMain {
240240 }
241241 ) : Promise <
242242 | { success : true ; workspaceId : string ; metadata : FrontendWorkspaceMetadata }
243- | { success : false ; error : string }
243+ | Result < void , SendMessageError >
244244 > {
245245 try {
246246 // 1. Generate workspace branch name using AI (use same model as message)
247- const branchName = await generateWorkspaceName ( message , options . model , this . config ) ;
247+ let branchName : string ;
248+ {
249+ const isErrLike = ( v : unknown ) : v is { type : string } =>
250+ typeof v === "object" && v !== null && "type" in v ;
251+ const nameResult = await generateWorkspaceName ( message , options . model , this . aiService ) ;
252+ if ( ! nameResult . success ) {
253+ const err = nameResult . error ;
254+ if ( isErrLike ( err ) ) {
255+ return Err ( err ) ;
256+ }
257+ const toSafeString = ( v : unknown ) : string => {
258+ if ( v instanceof Error ) return v . message ;
259+ try {
260+ return JSON . stringify ( v ) ;
261+ } catch {
262+ return String ( v ) ;
263+ }
264+ } ;
265+ const msg = toSafeString ( err ) ;
266+ return Err ( { type : "unknown" , raw : `Failed to generate workspace name: ${ msg } ` } ) ;
267+ }
268+ branchName = nameResult . data ;
269+ }
248270
249271 log . debug ( "Generated workspace name" , { branchName } ) ;
250272
@@ -277,7 +299,7 @@ export class IpcMain {
277299 }
278300 } catch ( error ) {
279301 const errorMsg = error instanceof Error ? error . message : String ( error ) ;
280- return { success : false , error : errorMsg } ;
302+ return Err ( { type : "unknown" , raw : `Failed to prepare runtime: ${ errorMsg } ` } ) ;
281303 }
282304
283305 const session = this . getOrCreateSession ( workspaceId ) ;
@@ -294,7 +316,7 @@ export class IpcMain {
294316 } ) ;
295317
296318 if ( ! createResult . success || ! createResult . workspacePath ) {
297- return { success : false , error : createResult . error ?? "Failed to create workspace" } ;
319+ return Err ( { type : "unknown" , raw : createResult . error ?? "Failed to create workspace" } ) ;
298320 }
299321
300322 const projectName =
@@ -327,7 +349,7 @@ export class IpcMain {
327349 const allMetadata = await this . config . getAllWorkspaceMetadata ( ) ;
328350 const completeMetadata = allMetadata . find ( ( m ) => m . id === workspaceId ) ;
329351 if ( ! completeMetadata ) {
330- return { success : false , error : "Failed to retrieve workspace metadata" } ;
352+ return Err ( { type : "unknown" , raw : "Failed to retrieve workspace metadata" } ) ;
331353 }
332354
333355 session . emitMetadata ( completeMetadata ) ;
@@ -358,7 +380,7 @@ export class IpcMain {
358380 } catch ( error ) {
359381 const errorMessage = error instanceof Error ? error . message : String ( error ) ;
360382 log . error ( "Unexpected error in createWorkspaceForFirstMessage:" , error ) ;
361- return { success : false , error : `Failed to create workspace: ${ errorMessage } ` } ;
383+ return Err ( { type : "unknown" , raw : `Failed to create workspace: ${ errorMessage } ` } ) ;
362384 }
363385 }
364386
@@ -971,20 +993,35 @@ export class IpcMain {
971993 const messageText = textParts . map ( ( p ) => p . text ) . join ( " " ) ;
972994
973995 if ( messageText . trim ( ) ) {
974- const branchName = await generateWorkspaceName (
996+ const nameResult = await generateWorkspaceName (
975997 messageText ,
976998 "anthropic:claude-sonnet-4-5" , // Use reasonable default model
977- this . config
999+ this . aiService
9781000 ) ;
979-
980- // Update config with regenerated name
981- await this . config . updateWorkspaceMetadata ( workspaceId , {
982- name : branchName ,
983- } ) ;
984-
985- // Return updated metadata
986- metadata . name = branchName ;
987- log . info ( `Regenerated workspace name: ${ branchName } ` ) ;
1001+ if ( nameResult . success ) {
1002+ const branchName = nameResult . data ;
1003+ // Update config with regenerated name
1004+ await this . config . updateWorkspaceMetadata ( workspaceId , {
1005+ name : branchName ,
1006+ } ) ;
1007+
1008+ // Return updated metadata
1009+ metadata . name = branchName ;
1010+ log . info ( `Regenerated workspace name: ${ branchName } ` ) ;
1011+ } else {
1012+ log . info (
1013+ `Skipping title regeneration for ${ workspaceId } : ${
1014+ (
1015+ nameResult . error as {
1016+ type ?: string ;
1017+ provider ?: string ;
1018+ message ?: string ;
1019+ raw ?: string ;
1020+ }
1021+ ) . type ?? "unknown"
1022+ } `
1023+ ) ;
1024+ }
9881025 }
9891026 }
9901027 } catch ( error ) {
0 commit comments