mirror of
https://github.com/mattermost/mattermost.git
synced 2026-02-03 20:40:00 -05:00
MM-66577 Preserve locale in rewrite prompt (#35013)
Some checks are pending
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Blocked by required conditions
Web App CI / check-types (push) Blocked by required conditions
Web App CI / test (platform) (push) Blocked by required conditions
Web App CI / test (mattermost-redux) (push) Blocked by required conditions
Web App CI / test (channels shard 1/4) (push) Blocked by required conditions
Web App CI / test (channels shard 2/4) (push) Blocked by required conditions
Web App CI / test (channels shard 3/4) (push) Blocked by required conditions
Web App CI / test (channels shard 4/4) (push) Blocked by required conditions
Web App CI / upload-coverage (push) Blocked by required conditions
Web App CI / build (push) Blocked by required conditions
Some checks are pending
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Blocked by required conditions
Web App CI / check-types (push) Blocked by required conditions
Web App CI / test (platform) (push) Blocked by required conditions
Web App CI / test (mattermost-redux) (push) Blocked by required conditions
Web App CI / test (channels shard 1/4) (push) Blocked by required conditions
Web App CI / test (channels shard 2/4) (push) Blocked by required conditions
Web App CI / test (channels shard 3/4) (push) Blocked by required conditions
Web App CI / test (channels shard 4/4) (push) Blocked by required conditions
Web App CI / upload-coverage (push) Blocked by required conditions
Web App CI / build (push) Blocked by required conditions
* Preserve rewrite locale in prompt * Add license header to rewrite tests
This commit is contained in:
parent
aa5d51131d
commit
0263262ef4
2 changed files with 52 additions and 1 deletions
|
|
@ -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.
|
||||
|
|
|
|||
28
server/channels/app/post_rewrite_test.go
Normal file
28
server/channels/app/post_rewrite_test.go
Normal file
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue