refactor: change pointer receivers to value types for Conversation and UserMessage in various structs and functions

This commit is contained in:
mlogclub
2026-04-17 18:48:45 +08:00
parent 976b9defde
commit 51884f1bdf
22 changed files with 63 additions and 115 deletions
@@ -30,17 +30,14 @@ type AnalyzeConversationResult struct {
}
type AnalyzeConversationGraph struct {
conversation *models.Conversation
conversation models.Conversation
}
func NewAnalyzeConversationGraph(conversation *models.Conversation) *AnalyzeConversationGraph {
func NewAnalyzeConversationGraph(conversation models.Conversation) *AnalyzeConversationGraph {
return &AnalyzeConversationGraph{conversation: conversation}
}
func (g *AnalyzeConversationGraph) Run(_ context.Context, argumentsInJSON string) (string, error) {
if g == nil || g.conversation == nil {
return "", fmt.Errorf("analyze conversation graph not initialized")
}
input, err := g.parseInput(argumentsInJSON)
if err != nil {
return "", err
@@ -68,7 +65,7 @@ func (g *AnalyzeConversationGraph) parseInput(argumentsInJSON string) (AnalyzeCo
return input, nil
}
func buildAnalyzeConversationResult(conversation *models.Conversation, messages []models.Message, input AnalyzeConversationInput) AnalyzeConversationResult {
func buildAnalyzeConversationResult(conversation models.Conversation, messages []models.Message, input AnalyzeConversationInput) AnalyzeConversationResult {
joined := strings.ToLower(buildConversationCorpus(conversation, messages, input))
signals := collectRiskSignals(joined, input)
intent := detectUserIntent(joined, input)
@@ -85,14 +82,14 @@ func buildAnalyzeConversationResult(conversation *models.Conversation, messages
return result
}
func buildConversationSummary(conversation *models.Conversation, messages []models.Message, input AnalyzeConversationInput) string {
func buildConversationSummary(conversation models.Conversation, messages []models.Message, input AnalyzeConversationInput) string {
parts := make([]string, 0, 4)
if conversation != nil && strings.TrimSpace(conversation.Subject) != "" {
if strings.TrimSpace(conversation.Subject) != "" {
parts = append(parts, "会话主题:"+strings.TrimSpace(conversation.Subject))
}
if input.ObservedIssue != "" {
parts = append(parts, "当前问题:"+input.ObservedIssue)
} else if conversation != nil && strings.TrimSpace(conversation.LastMessageSummary) != "" {
} else if strings.TrimSpace(conversation.LastMessageSummary) != "" {
parts = append(parts, "当前问题:"+strings.TrimSpace(conversation.LastMessageSummary))
}
if digest := buildRecentMessageDigest(messages); digest != "" {
@@ -104,12 +101,10 @@ func buildConversationSummary(conversation *models.Conversation, messages []mode
return strings.TrimSpace(strings.Join(parts, "\n"))
}
func buildConversationCorpus(conversation *models.Conversation, messages []models.Message, input AnalyzeConversationInput) string {
func buildConversationCorpus(conversation models.Conversation, messages []models.Message, input AnalyzeConversationInput) string {
parts := make([]string, 0, len(messages)+4)
if conversation != nil {
parts = append(parts, strings.TrimSpace(conversation.Subject))
parts = append(parts, strings.TrimSpace(conversation.LastMessageSummary))
}
parts = append(parts, strings.TrimSpace(conversation.Subject))
parts = append(parts, strings.TrimSpace(conversation.LastMessageSummary))
parts = append(parts, input.Goal, input.ObservedIssue, input.AdditionalContext)
for i := range messages {
parts = append(parts, strings.TrimSpace(messages[i].Content))
@@ -203,12 +198,12 @@ func recommendQuestions(intent string, signals []string, input AnalyzeConversati
return questions
}
func buildConversationAnalysisFacts(conversation *models.Conversation, messages []models.Message) []string {
func buildConversationAnalysisFacts(conversation models.Conversation, messages []models.Message) []string {
facts := make([]string, 0, 4)
if conversation != nil && strings.TrimSpace(conversation.Subject) != "" {
if strings.TrimSpace(conversation.Subject) != "" {
facts = append(facts, "会话主题:"+strings.TrimSpace(conversation.Subject))
}
if conversation != nil && strings.TrimSpace(conversation.LastMessageSummary) != "" {
if strings.TrimSpace(conversation.LastMessageSummary) != "" {
facts = append(facts, "最近摘要:"+strings.TrimSpace(conversation.LastMessageSummary))
}
if digest := buildRecentMessageDigest(messages); digest != "" {
@@ -8,7 +8,7 @@ import (
)
func TestBuildAnalyzeConversationResult_RecommendsHandoffForComplaint(t *testing.T) {
conversation := &models.Conversation{
conversation := models.Conversation{
Subject: "用户投诉扣费异常",
LastMessageSummary: "用户反馈被重复扣费,并要求人工处理",
}
@@ -32,7 +32,7 @@ func TestBuildAnalyzeConversationResult_RecommendsHandoffForComplaint(t *testing
}
func TestBuildAnalyzeConversationResult_RecommendsPrepareTicket(t *testing.T) {
conversation := &models.Conversation{
conversation := models.Conversation{
Subject: "订单无法支付",
LastMessageSummary: "用户要求登记问题并尽快处理",
}
@@ -37,11 +37,11 @@ func init() {
}
type CreateTicketGraph struct {
conversation *models.Conversation
conversation models.Conversation
aiAgent models.AIAgent
}
func NewCreateTicketGraph(conversation *models.Conversation, aiAgent models.AIAgent) *CreateTicketGraph {
func NewCreateTicketGraph(conversation models.Conversation, aiAgent models.AIAgent) *CreateTicketGraph {
return &CreateTicketGraph{
conversation: conversation,
aiAgent: aiAgent,
@@ -49,9 +49,6 @@ func NewCreateTicketGraph(conversation *models.Conversation, aiAgent models.AIAg
}
func (g *CreateTicketGraph) Run(ctx context.Context, argumentsInJSON string) (string, error) {
if g == nil || g.conversation == nil {
return "", fmt.Errorf("create ticket graph not initialized")
}
wasInterrupted, hasState, state := componenttool.GetInterruptState[CreateTicketGraphState](ctx)
if !wasInterrupted {
req, err := g.buildCreateRequest(argumentsInJSON)
+2 -5
View File
@@ -32,11 +32,11 @@ func init() {
}
type HandoffGraph struct {
conversation *models.Conversation
conversation models.Conversation
aiAgent models.AIAgent
}
func NewHandoffGraph(conversation *models.Conversation, aiAgent models.AIAgent) *HandoffGraph {
func NewHandoffGraph(conversation models.Conversation, aiAgent models.AIAgent) *HandoffGraph {
return &HandoffGraph{
conversation: conversation,
aiAgent: aiAgent,
@@ -44,9 +44,6 @@ func NewHandoffGraph(conversation *models.Conversation, aiAgent models.AIAgent)
}
func (g *HandoffGraph) Run(ctx context.Context, argumentsInJSON string) (string, error) {
if g == nil || g.conversation == nil {
return "", fmt.Errorf("handoff graph not initialized")
}
wasInterrupted, hasState, state := componenttool.GetInterruptState[HandoffGraphState](ctx)
if !wasInterrupted {
reason, err := g.buildReason(argumentsInJSON)
@@ -34,17 +34,14 @@ type PrepareTicketDraftResult struct {
}
type PrepareTicketDraftGraph struct {
conversation *models.Conversation
conversation models.Conversation
}
func NewPrepareTicketDraftGraph(conversation *models.Conversation) *PrepareTicketDraftGraph {
func NewPrepareTicketDraftGraph(conversation models.Conversation) *PrepareTicketDraftGraph {
return &PrepareTicketDraftGraph{conversation: conversation}
}
func (g *PrepareTicketDraftGraph) Run(_ context.Context, argumentsInJSON string) (string, error) {
if g == nil || g.conversation == nil {
return "", fmt.Errorf("prepare ticket draft graph not initialized")
}
input, err := g.parseInput(argumentsInJSON)
if err != nil {
return "", err
@@ -75,7 +72,7 @@ func (g *PrepareTicketDraftGraph) parseInput(argumentsInJSON string) (PrepareTic
return input, nil
}
func buildPrepareTicketDraftResult(conversation *models.Conversation, messages []models.Message, input PrepareTicketDraftInput) PrepareTicketDraftResult {
func buildPrepareTicketDraftResult(conversation models.Conversation, messages []models.Message, input PrepareTicketDraftInput) PrepareTicketDraftResult {
result := PrepareTicketDraftResult{
Priority: input.Priority,
Severity: input.Severity,
@@ -97,22 +94,22 @@ func buildPrepareTicketDraftResult(conversation *models.Conversation, messages [
return result
}
func buildDraftTitle(conversation *models.Conversation, input PrepareTicketDraftInput) string {
func buildDraftTitle(conversation models.Conversation, input PrepareTicketDraftInput) string {
switch {
case input.Title != "":
return limitText(input.Title, 80)
case input.Issue != "":
return limitText(input.Issue, 80)
case conversation != nil && strings.TrimSpace(conversation.Subject) != "":
case strings.TrimSpace(conversation.Subject) != "":
return limitText(conversation.Subject, 80)
case conversation != nil:
case strings.TrimSpace(conversation.LastMessageSummary) != "":
return limitText(conversation.LastMessageSummary, 80)
default:
return ""
}
}
func buildDraftDescription(conversation *models.Conversation, messages []models.Message, input PrepareTicketDraftInput) string {
func buildDraftDescription(conversation models.Conversation, messages []models.Message, input PrepareTicketDraftInput) string {
if input.Description != "" {
return input.Description
}
@@ -129,7 +126,7 @@ func buildDraftDescription(conversation *models.Conversation, messages []models.
if input.CurrentAttempt != "" {
parts = append(parts, "已尝试处理:"+input.CurrentAttempt)
}
if conversation != nil && strings.TrimSpace(conversation.LastMessageSummary) != "" {
if strings.TrimSpace(conversation.LastMessageSummary) != "" {
parts = append(parts, "会话摘要:"+strings.TrimSpace(conversation.LastMessageSummary))
}
if recent := buildRecentMessageDigest(messages); recent != "" {
@@ -145,12 +142,12 @@ func hasSufficientIssueContext(input PrepareTicketDraftInput, description string
return len([]rune(strings.TrimSpace(description))) >= 30
}
func buildConversationFacts(conversation *models.Conversation, messages []models.Message) []string {
func buildConversationFacts(conversation models.Conversation, messages []models.Message) []string {
facts := make([]string, 0, 4)
if conversation != nil && strings.TrimSpace(conversation.Subject) != "" {
if strings.TrimSpace(conversation.Subject) != "" {
facts = append(facts, "会话主题:"+strings.TrimSpace(conversation.Subject))
}
if conversation != nil && strings.TrimSpace(conversation.LastMessageSummary) != "" {
if strings.TrimSpace(conversation.LastMessageSummary) != "" {
facts = append(facts, "最近摘要:"+strings.TrimSpace(conversation.LastMessageSummary))
}
if digest := buildRecentMessageDigest(messages); digest != "" {
@@ -8,7 +8,7 @@ import (
)
func TestBuildPrepareTicketDraftResult_UsesConversationFallbacks(t *testing.T) {
conversation := &models.Conversation{
conversation := models.Conversation{
Subject: "企业微信登录异常",
LastMessageSummary: "用户反馈企业微信扫码后页面空白,无法进入工作台",
}
@@ -37,7 +37,7 @@ func TestBuildPrepareTicketDraftResult_UsesConversationFallbacks(t *testing.T) {
}
func TestBuildPrepareTicketDraftResult_ReadyWithExplicitIssue(t *testing.T) {
conversation := &models.Conversation{
conversation := models.Conversation{
Subject: "订单支付失败",
LastMessageSummary: "用户反馈连续支付失败",
}
@@ -26,17 +26,14 @@ type TriageServiceRequestResult struct {
}
type TriageServiceRequestGraph struct {
conversation *models.Conversation
conversation models.Conversation
}
func NewTriageServiceRequestGraph(conversation *models.Conversation) *TriageServiceRequestGraph {
func NewTriageServiceRequestGraph(conversation models.Conversation) *TriageServiceRequestGraph {
return &TriageServiceRequestGraph{conversation: conversation}
}
func (g *TriageServiceRequestGraph) Run(_ context.Context, argumentsInJSON string) (string, error) {
if g == nil || g.conversation == nil {
return "", fmt.Errorf("triage service request graph not initialized")
}
input, err := g.parseInput(argumentsInJSON)
if err != nil {
return "", err
@@ -8,7 +8,7 @@ import (
)
func TestTriageServiceRequestResult_PrepareTicket(t *testing.T) {
conversation := &models.Conversation{
conversation := models.Conversation{
Subject: "支付失败需要登记工单",
LastMessageSummary: "用户要求建单跟进支付失败问题",
}
@@ -30,7 +30,7 @@ func TestTriageServiceRequestResult_PrepareTicket(t *testing.T) {
}
func TestTriageServiceRequestResult_Handoff(t *testing.T) {
conversation := &models.Conversation{
conversation := models.Conversation{
Subject: "投诉转人工",
LastMessageSummary: "用户要求人工处理扣费投诉",
}