( content: string, maxLength: number = 15 )
| 221 | * @param maxLength Topic 最大长度 |
| 222 | */ |
| 223 | export async function generateTopicTitle( |
| 224 | content: string, |
| 225 | maxLength: number = 15 |
| 226 | ): Promise<TitleGenerationResult> { |
| 227 | const { llmConfig, modelConfig } = getDefaultConfigs() |
| 228 | |
| 229 | if (!llmConfig) { |
| 230 | return { title: '', success: false, error: '未配置 LLM' } |
| 231 | } |
| 232 | |
| 233 | try { |
| 234 | const prompt = TOPIC_GENERATION_PROMPT.replace('{content}', truncateContent(content)) |
| 235 | const result = await generateWithAI(prompt, llmConfig, modelConfig || undefined) |
| 236 | |
| 237 | // 清理结果:移除可能的引号和多余空白 |
| 238 | let title = result |
| 239 | .replace(/^["'""]|["'""]$/g, '') |
| 240 | .replace(/^(话题|主题)[::]\s*/i, '') |
| 241 | .trim() |
| 242 | |
| 243 | // 截断到最大长度 |
| 244 | if (title.length > maxLength) { |
| 245 | title = title.slice(0, maxLength) |
| 246 | } |
| 247 | |
| 248 | return { title, success: true } |
| 249 | } catch (error) { |
| 250 | console.error('Failed to generate topic title:', error) |
| 251 | return { title: '', success: false, error: String(error) } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | export interface TopicSuggestionResult { |
| 256 | shouldCreateTopic: boolean |
no test coverage detected