diff --git a/server/channels/app/post.go b/server/channels/app/post.go index 6d159d311a1..f6902d2bd4a 100644 --- a/server/channels/app/post.go +++ b/server/channels/app/post.go @@ -3100,11 +3100,23 @@ func (a *App) RewriteMessage( return nil, model.NewAppError("RewriteMessage", "app.post.rewrite.invalid_action", nil, fmt.Sprintf("invalid action: %s", action), 400) } + userLocale := "" + if session := rctx.Session(); session != nil && session.UserId != "" { + user, appErr := a.GetUser(session.UserId) + if appErr == nil { + userLocale = user.Locale + } else { + rctx.Logger().Warn("Failed to get user for rewrite locale", mlog.Err(appErr), mlog.String("user_id", session.UserId)) + } + } + + systemPrompt := buildRewriteSystemPrompt(userLocale) + // Prepare completion request in the format expected by the client client := a.GetBridgeClient(rctx.Session().UserId) completionRequest := agentclient.CompletionRequest{ Posts: []agentclient.Post{ - {Role: "system", Message: model.RewriteSystemPrompt}, + {Role: "system", Message: systemPrompt}, {Role: "user", Message: userPrompt}, }, } @@ -3278,6 +3290,17 @@ func getRewritePromptForAction(action model.RewriteAction, message string, custo return actionPrompt } +func buildRewriteSystemPrompt(userLocale string) string { + locale := strings.TrimSpace(userLocale) + if locale == "" { + return model.RewriteSystemPrompt + } + + return fmt.Sprintf(`%s + +User locale: %s. Preserve locale-specific spelling, grammar, and formatting. Keep locale identifiers (like %s) unchanged. Do not translate between locales.`, model.RewriteSystemPrompt, locale, locale) +} + // RevealPost reveals a burn-on-read post for a specific user, creating a read receipt // if this is the first time the user is revealing it. Returns the revealed post content // with expiration metadata. diff --git a/server/channels/app/post_rewrite_test.go b/server/channels/app/post_rewrite_test.go new file mode 100644 index 00000000000..571277e7efe --- /dev/null +++ b/server/channels/app/post_rewrite_test.go @@ -0,0 +1,28 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package app + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/mattermost/mattermost/server/public/model" +) + +func TestBuildRewriteSystemPrompt(t *testing.T) { + basePrompt := model.RewriteSystemPrompt + + t.Run("uses_user_locale", func(t *testing.T) { + prompt := buildRewriteSystemPrompt("en_CA") + require.True(t, strings.HasPrefix(prompt, basePrompt)) + require.Contains(t, prompt, "User locale: en_CA.") + }) + + t.Run("returns_base_prompt_when_no_locale", func(t *testing.T) { + prompt := buildRewriteSystemPrompt("") + require.Equal(t, basePrompt, prompt) + }) +}