Files
selfrelease da9c8334d8
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled
CI / security-scan (push) Has been cancelled
feat: 十轮网关优化 - 安全加固/可观测性/性能/可靠性
- SSE Keepalive Ping (15s心跳防止代理断连)
- Timing HTTP 头 (X-Timing-Queue/Inference/Total-Ms)
- Adapter Request-ID 传播到后端
- Session 清理日志回调
- Server 安全加固 (ReadHeaderTimeout/MaxHeaderBytes 防 slowloris)
- Usage Tracker 数据保留清理 (retentionDays + 定期清理)
- Config Reload 后 Adapter Registry 更新 (RegisterIfAbsent + RWMutex)
- Rate Limiter 空闲 Bucket 清理 (30分钟过期)
- Shutdown Drain 超时可配置 (ShutdownDrainSeconds)
- Config 模型字段校验增强 (provider/endpoint/actual_model)
- Auth 过期 Key 自动清理 (5分钟扫描)
- Admin API Rate Limiting
- Adapter Health Check 独立超时 (每个 adapter 3s)
- TCP 连接阶段超时 (DialContext 5s + KeepAlive 30s)
- 幂等键缓存、审计日志、Gzip 中间件、CORS Expose Headers
- Backpressure 响应头、熔断器 Prometheus 指标
- 连接池优化、Trace-ID 全链路传播
2026-08-03 15:43:11 +08:00

49 lines
1.4 KiB
Go

package context
import (
"context"
"fmt"
"strings"
"time"
"github.com/edgeai/gateway/pkg/api"
)
// SummaryGenerator 定义摘要生成接口,允许注入不同的实现。
type SummaryGenerator interface {
// GenerateSummary 对给定消息生成对话摘要,返回摘要文本。
GenerateSummary(ctx context.Context, messages []api.Message) (string, error)
}
// Summarizer 使用 LLM 对历史消息生成对话摘要。
type Summarizer struct {
generator SummaryGenerator
timeout time.Duration
}
// NewSummarizer 创建一个新的摘要器。
func NewSummarizer(generator SummaryGenerator, timeout time.Duration) *Summarizer {
return &Summarizer{
generator: generator,
timeout: timeout,
}
}
// Summarize 对被裁剪的老消息生成摘要。
// 如果摘要生成失败,回退到占位符文本。
func (s *Summarizer) Summarize(messages []api.Message) string {
if s == nil || s.generator == nil || len(messages) == 0 {
return "[Earlier conversation history has been summarized and omitted.]"
}
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
summary, err := s.generator.GenerateSummary(ctx, messages)
if err != nil || strings.TrimSpace(summary) == "" {
return "[Earlier conversation history has been summarized and omitted.]"
}
return fmt.Sprintf("[Earlier conversation summary: %s]", strings.TrimSpace(summary))
}