refactor: support i18n

This commit is contained in:
mlogclub
2026-05-25 12:06:15 +08:00
parent 309ac1fe9e
commit 988f55c80d
179 changed files with 10968 additions and 3763 deletions
+71
View File
@@ -4,6 +4,7 @@ import (
"strings"
"cs-agent/internal/pkg/enums"
"cs-agent/internal/pkg/i18nx"
)
type ToolSpec struct {
@@ -218,6 +219,20 @@ func GetRegisteredToolTitle(toolCode string) string {
return spec.Title
}
func GetRegisteredToolTitleLocale(toolCode string, locale string) string {
spec, ok := GetRegisteredToolSpec(toolCode)
if !ok {
return ""
}
if i18nx.NormalizeLocale(locale) != i18nx.LocaleEnUS {
return spec.Title
}
if text := registeredToolEnglishTitle(spec.Code); text != "" {
return text
}
return spec.Title
}
func GetRegisteredToolDescription(toolCode string) string {
spec, ok := GetRegisteredToolSpec(toolCode)
if !ok {
@@ -226,6 +241,62 @@ func GetRegisteredToolDescription(toolCode string) string {
return spec.Description
}
func GetRegisteredToolDescriptionLocale(toolCode string, locale string) string {
spec, ok := GetRegisteredToolSpec(toolCode)
if !ok {
return ""
}
if i18nx.NormalizeLocale(locale) != i18nx.LocaleEnUS {
return spec.Description
}
if text := registeredToolEnglishDescription(spec.Code); text != "" {
return text
}
return spec.Description
}
func registeredToolEnglishTitle(toolCode string) string {
switch toolCode {
case BuiltinToolSearch.Code:
return "Search and Run Dynamic Tools"
case BuiltinSkill.Code:
return "Load Skill Instructions"
case GraphTriageServiceRequest.Code:
return "Route Service Request"
case GraphAnalyzeConversation.Code:
return "Analyze Conversation Risk and Summary"
case GraphPrepareTicketDraft.Code:
return "Prepare Ticket Draft"
case GraphCreateTicketConfirm.Code:
return "Create Ticket With Confirmation"
case GraphHandoffConversation.Code:
return "Handoff to Human With Confirmation"
default:
return ""
}
}
func registeredToolEnglishDescription(toolCode string) string {
switch toolCode {
case BuiltinToolSearch.Code:
return "Searches the MCP tools currently available to the agent and runs the selected tool after its toolCode is confirmed. Best for long-tail tools; it should not replace fixed built-in workflow tools."
case BuiltinSkill.Code:
return "Loads specialized skill instructions for the current agent when extra task-specific guidance is needed."
case GraphTriageServiceRequest.Code:
return "Analyzes the current conversation to decide whether to keep answering, prepare a ticket draft, or hand off to a human, including a ticket draft when ticket creation is appropriate."
case GraphAnalyzeConversation.Code:
return "Summarizes the current conversation, identifies risk signals, and recommends whether to keep answering, create a ticket, or hand off to a human."
case GraphPrepareTicketDraft.Code:
return "Turns the current conversation and collected details into a ticket draft with a suggested title, description, missing fields, and follow-up questions."
case GraphCreateTicketConfirm.Code:
return "Guides ticket creation with parameter preparation, customer confirmation, actual ticket creation, and final result delivery."
case GraphHandoffConversation.Code:
return "Guides human handoff with reason preparation, customer confirmation, actual transfer, and final result delivery."
default:
return ""
}
}
func GetRegisteredToolIdentity(toolCode string) (serverCode, toolName string, ok bool) {
spec, ok := GetRegisteredToolSpec(toolCode)
if !ok {
+30 -1
View File
@@ -1,6 +1,10 @@
package toolx
import "testing"
import (
"testing"
"cs-agent/internal/pkg/i18nx"
)
func TestResolveToolMetadata(t *testing.T) {
item := ResolveToolMetadata("builtin/create_ticket_with_confirmation", "")
@@ -33,3 +37,28 @@ func TestResolveToolMetadataFallsBackToName(t *testing.T) {
t.Fatalf("unexpected source type: %s", item.SourceType)
}
}
func TestRegisteredToolTextUsesEnglishLocale(t *testing.T) {
title := GetRegisteredToolTitleLocale(GraphCreateTicketConfirm.Code, i18nx.LocaleEnUS)
if title != "Create Ticket With Confirmation" {
t.Fatalf("unexpected english title: %q", title)
}
description := GetRegisteredToolDescriptionLocale(GraphCreateTicketConfirm.Code, i18nx.LocaleEnUS)
want := "Guides ticket creation with parameter preparation, customer confirmation, actual ticket creation, and final result delivery."
if description != want {
t.Fatalf("unexpected english description: %q", description)
}
}
func TestRegisteredToolTextKeepsChineseLocale(t *testing.T) {
title := GetRegisteredToolTitleLocale(GraphCreateTicketConfirm.Code, i18nx.LocaleZhCN)
if title != GraphCreateTicketConfirm.Title {
t.Fatalf("unexpected chinese title: %q", title)
}
description := GetRegisteredToolDescriptionLocale(GraphCreateTicketConfirm.Code, i18nx.LocaleZhCN)
if description != GraphCreateTicketConfirm.Description {
t.Fatalf("unexpected chinese description: %q", description)
}
}