(params: {
message: string
model: string
provider?: string
userId?: string
workspaceId?: string
otelContext?: Context
})
| 473 | // --------------------------------------------------------------------------- |
| 474 | |
| 475 | export async function requestChatTitle(params: { |
| 476 | message: string |
| 477 | model: string |
| 478 | provider?: string |
| 479 | userId?: string |
| 480 | workspaceId?: string |
| 481 | otelContext?: Context |
| 482 | }): Promise<string | null> { |
| 483 | const { message, model, provider, userId, workspaceId, otelContext } = params |
| 484 | if (!message || !model) return null |
| 485 | |
| 486 | const headers: Record<string, string> = { |
| 487 | 'Content-Type': 'application/json', |
| 488 | } |
| 489 | if (env.COPILOT_API_KEY) { |
| 490 | headers['x-api-key'] = env.COPILOT_API_KEY |
| 491 | } |
| 492 | Object.assign(headers, getMothershipSourceEnvHeaders()) |
| 493 | |
| 494 | try { |
| 495 | const { fetchGo } = await import('@/lib/copilot/request/go/fetch') |
| 496 | const mothershipBaseURL = await getMothershipBaseURL({ userId }) |
| 497 | const response = await fetchGo(`${mothershipBaseURL}/api/generate-chat-title`, { |
| 498 | method: 'POST', |
| 499 | headers, |
| 500 | body: JSON.stringify({ |
| 501 | message, |
| 502 | model, |
| 503 | ...(provider ? { provider } : {}), |
| 504 | ...(workspaceId ? { workspaceId } : {}), |
| 505 | ...(userId ? { userId } : {}), |
| 506 | }), |
| 507 | otelContext, |
| 508 | spanName: 'sim → go /api/generate-chat-title', |
| 509 | operation: 'generate_chat_title', |
| 510 | attributes: { |
| 511 | [TraceAttr.GenAiRequestModel]: model, |
| 512 | ...(provider ? { [TraceAttr.GenAiSystem]: provider } : {}), |
| 513 | }, |
| 514 | }) |
| 515 | |
| 516 | const payload = await response.json().catch(() => ({})) |
| 517 | if (!response.ok) { |
| 518 | logger.warn('Failed to generate chat title via copilot backend', { |
| 519 | status: response.status, |
| 520 | error: payload, |
| 521 | }) |
| 522 | return null |
| 523 | } |
| 524 | |
| 525 | const title = typeof payload?.title === 'string' ? payload.title.trim() : '' |
| 526 | return title || null |
| 527 | } catch (error) { |
| 528 | logger.error('Error generating chat title:', error) |
| 529 | return null |
| 530 | } |
| 531 | } |
no test coverage detected