| 413 | // --------------------------------------------------------------------------- |
| 414 | |
| 415 | function fireTitleGeneration(params: { |
| 416 | chatId?: string |
| 417 | currentChat: CurrentChatSummary |
| 418 | isNewChat: boolean |
| 419 | userId?: string |
| 420 | message: string |
| 421 | titleModel: string |
| 422 | titleProvider?: string |
| 423 | workspaceId?: string |
| 424 | requestId: string |
| 425 | publisher: StreamWriter |
| 426 | otelContext?: Context |
| 427 | }): void { |
| 428 | const { |
| 429 | chatId, |
| 430 | currentChat, |
| 431 | isNewChat, |
| 432 | userId, |
| 433 | message, |
| 434 | titleModel, |
| 435 | titleProvider, |
| 436 | workspaceId, |
| 437 | requestId, |
| 438 | publisher, |
| 439 | otelContext, |
| 440 | } = params |
| 441 | if (!chatId || currentChat?.title || !isNewChat) return |
| 442 | |
| 443 | requestChatTitle({ |
| 444 | message, |
| 445 | model: titleModel, |
| 446 | provider: titleProvider, |
| 447 | userId, |
| 448 | workspaceId, |
| 449 | otelContext, |
| 450 | }) |
| 451 | .then(async (title) => { |
| 452 | if (!title) return |
| 453 | await db.update(copilotChats).set({ title }).where(eq(copilotChats.id, chatId)) |
| 454 | await publisher.publish({ |
| 455 | type: MothershipStreamV1EventType.session, |
| 456 | payload: { kind: MothershipStreamV1SessionKind.title, title }, |
| 457 | }) |
| 458 | if (workspaceId) { |
| 459 | chatPubSub?.publishStatusChanged({ |
| 460 | workspaceId, |
| 461 | chatId, |
| 462 | type: 'renamed', |
| 463 | }) |
| 464 | } |
| 465 | }) |
| 466 | .catch((error) => { |
| 467 | logger.error(`[${requestId}] Title generation failed:`, error) |
| 468 | }) |
| 469 | } |
| 470 | |
| 471 | // --------------------------------------------------------------------------- |
| 472 | // Chat title helper |