owpengram-server/internal/app/ai/local.go
2026-07-04 01:14:47 +08:00

85 lines
1.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ai
import (
"context"
"strings"
"telesrv/internal/domain"
)
// LocalProvider 是默认开发 provider不出网、不记录内容做确定性轻量整理。
type LocalProvider struct{}
func (LocalProvider) Name() string { return "local" }
func (LocalProvider) Compose(_ context.Context, req ProviderRequest) (domain.AIComposeText, error) {
if req.Purpose == ProviderPurposeTextGeneration {
return domain.AIComposeText{}, domain.ErrAIComposeProviderUnavailable
}
text := localTransform(req.Request.Text.Text, req.Request, req.Tone)
return domain.AIComposeText{Text: text}, nil
}
func localTransform(text string, req domain.AIComposeRequest, tone domain.AIComposeTone) string {
text = strings.TrimSpace(collapseWhitespace(text))
if text == "" {
return text
}
if req.TranslateToLang != "" {
return text
}
switch tone.Slug {
case "formal":
return ensureSentencePunctuation(text)
case "friendly":
return ensureSentencePunctuation(text)
case "short", "concise":
return trimVerboseLead(text)
default:
return ensureSentencePunctuation(text)
}
}
func collapseWhitespace(s string) string {
lines := strings.Split(s, "\n")
for i := range lines {
lines[i] = strings.Join(strings.Fields(lines[i]), " ")
}
out := make([]string, 0, len(lines))
for _, line := range lines {
if line != "" {
out = append(out, line)
}
}
return strings.Join(out, "\n")
}
func ensureSentencePunctuation(s string) string {
if s == "" {
return s
}
var last rune
for _, r := range s {
last = r
}
switch last {
case '.', '!', '?', ':', ';', '。', '', '':
return s
default:
return s + "."
}
}
func trimVerboseLead(s string) string {
prefixes := []string{
"I just wanted to ",
"I wanted to ",
"Just wanted to ",
}
for _, p := range prefixes {
if strings.HasPrefix(s, p) {
return ensureSentencePunctuation(strings.TrimPrefix(s, p))
}
}
return ensureSentencePunctuation(s)
}