( content: string, maxLength: number = 15 )
| 184 | * @param maxLength 标题最大长度 |
| 185 | */ |
| 186 | export async function generateMessageTitle( |
| 187 | content: string, |
| 188 | maxLength: number = 15 |
| 189 | ): Promise<TitleGenerationResult> { |
| 190 | const { llmConfig, modelConfig } = getDefaultConfigs() |
| 191 | |
| 192 | if (!llmConfig) { |
| 193 | return { title: '', success: false, error: '未配置 LLM' } |
| 194 | } |
| 195 | |
| 196 | try { |
| 197 | const prompt = TITLE_GENERATION_PROMPT.replace('{content}', truncateContent(content)) |
| 198 | const result = await generateWithAI(prompt, llmConfig, modelConfig || undefined) |
| 199 | |
| 200 | // 清理结果:移除可能的引号和多余空白 |
| 201 | let title = result |
| 202 | .replace(/^["'""]|["'""]$/g, '') |
| 203 | .replace(/^标题[::]\s*/i, '') |
| 204 | .trim() |
| 205 | |
| 206 | // 截断到最大长度 |
| 207 | if (title.length > maxLength) { |
| 208 | title = title.slice(0, maxLength) |
| 209 | } |
| 210 | |
| 211 | return { title, success: true } |
| 212 | } catch (error) { |
| 213 | console.error('Failed to generate title:', error) |
| 214 | return { title: '', success: false, error: String(error) } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * 为 Topic 生成名称 |
no test coverage detected