UpdateSummary 更新会话摘要
( ctx context.Context, sessionID string, newMessages []types.Message, )
| 180 | |
| 181 | // UpdateSummary 更新会话摘要 |
| 182 | func (m *SessionSummaryManager) UpdateSummary( |
| 183 | ctx context.Context, |
| 184 | sessionID string, |
| 185 | newMessages []types.Message, |
| 186 | ) (*SessionSummary, error) { |
| 187 | if !m.config.Enabled { |
| 188 | return nil, errors.New("session summary is disabled") |
| 189 | } |
| 190 | |
| 191 | // 获取现有摘要 |
| 192 | m.mu.RLock() |
| 193 | existingSummary, exists := m.summaries[sessionID] |
| 194 | m.mu.RUnlock() |
| 195 | |
| 196 | // 如果不存在,生成新摘要 |
| 197 | if !exists { |
| 198 | return m.GenerateSummary(ctx, sessionID, newMessages) |
| 199 | } |
| 200 | |
| 201 | // 构建增量更新提示词 |
| 202 | prompt := m.buildIncrementalPrompt(existingSummary, newMessages) |
| 203 | |
| 204 | // 调用 LLM 更新摘要 |
| 205 | resp, err := m.provider.Complete(ctx, []types.Message{ |
| 206 | { |
| 207 | Role: "user", |
| 208 | Content: prompt, |
| 209 | }, |
| 210 | }, &provider.StreamOptions{ |
| 211 | MaxTokens: 1000, |
| 212 | Temperature: 0.3, |
| 213 | }) |
| 214 | if err != nil { |
| 215 | return nil, fmt.Errorf("failed to update summary: %w", err) |
| 216 | } |
| 217 | |
| 218 | // 解析响应 |
| 219 | updatedSummary, err := m.parseSummaryResponse(resp.Message.Content) |
| 220 | if err != nil { |
| 221 | return nil, fmt.Errorf("failed to parse updated summary: %w", err) |
| 222 | } |
| 223 | |
| 224 | // 更新基本信息 |
| 225 | updatedSummary.SessionID = sessionID |
| 226 | updatedSummary.CreatedAt = existingSummary.CreatedAt |
| 227 | updatedSummary.UpdatedAt = time.Now() |
| 228 | updatedSummary.MessageCount = existingSummary.MessageCount + len(newMessages) |
| 229 | updatedSummary.TokenCount = existingSummary.TokenCount + m.estimateTokens(newMessages) |
| 230 | |
| 231 | // 存储更新后的摘要 |
| 232 | m.mu.Lock() |
| 233 | m.summaries[sessionID] = updatedSummary |
| 234 | m.mu.Unlock() |
| 235 | |
| 236 | return updatedSummary, nil |
| 237 | } |
| 238 | |
| 239 | // GetSummary 获取会话摘要 |