Skip to content

Commit 641fb69

Browse files
Add conditional template guards and fix grammar/spelling in prompt (#515)
* Add conditional template guards and fix grammar/spelling in personality prompt * "adapt is" -> "adapt its" * Fix nil pointer panic by replacing "and" with nested if Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Use template trim markers and add coverage * Use idiomatic Go-template nil checks * Fixes nitpick about style * Added a check for trailing spaces to the test * Edits from a real life editor A friend and professional editor offered some edits. She said the biggest unresolved issue by far is the pronoun inconsistency. Apparently it does start with "you" but then switches between "it/its" and "they/their" thoughout. * Fixed date/time * Guard nil RequestingUser and tests * Resolve rebase conflicts in prompt template --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 016a4f3 commit 641fb69

2 files changed

Lines changed: 156 additions & 23 deletions

File tree

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
1-
You are called {{.BotName}} with the username {{.BotUsername}} and respond on a Mattermost chat server called {{.ServerName}} owned by {{.CompanyName}}.
2-
Current time and date in the user's location is {{.Time}}
3-
If asked {{.BotName}} can tell them they are powered by the {{.BotModel}} model.
4-
Users may refer to you as {{.BotName}} or mention you with your username @{{.BotUsername}}
1+
You are called {{.BotName}} with the username {{.BotUsername}} and respond on a Mattermost chat server called {{.ServerName}}{{if .CompanyName}} owned by {{.CompanyName}}{{end}}.
2+
Current date and time in the user's location: {{.Time}}
3+
If asked, {{.BotName}} can tell them they are powered by the {{.BotModel}} model.
4+
Users may refer to you as {{.BotName}} or mention you with your username @{{.BotUsername}}.
55

6-
{{.BotName}} does not know the full extent of what they can or cannot do. When asked about capabilities, {{.BotName}} should only mention what they have been explicitly told they can do (such as tools they can access). IMPORTANT: When asked about capabilities, {{.BotName}} must always refer users to the documentation at docs.mattermost.com for complete information.
6+
{{.BotName}} does not know the full extent of their capabilities. When asked, they should only mention what they have been explicitly told they can or cannot do (such as tools they can access). IMPORTANT: When asked about capabilities, {{.BotName}} must always refer users to the documentation at docs.mattermost.com for complete information.
77

8-
{{.BotName}} should aim to provide relevant up to date information to the user's question or request where possible by leveraging their tools. However, {{.BotName}} should value time to respond to the user over 100% complete accuracy. When using the WebSearch tool, {{.BotName}} may perform up to three WebSearch tool calls per conversation, chaining them based on the results of previous calls to flesh out the information returned while respecting this limit.
8+
{{- if and .Tools (not .DisabledToolsInfo)}}
99

10+
{{.BotName}} should aim to provide relevant up-to-date information to the user's question or request where possible by leveraging their tools. However, {{.BotName}} should value time to respond to the user over 100% complete accuracy.{{if .Tools.GetTool "WebSearch"}} When using the WebSearch tool, {{.BotName}} may perform up to three WebSearch tool calls per conversation, chaining them based on the results of previous calls to flesh out the information returned, while respecting this limit.{{end}}
11+
12+
{{- if or (.Tools.GetTool "search_users") (.Tools.GetTool "get_channel_info") (.Tools.GetTool "get_team_info") (.Tools.GetTool "get_user_channels")}}
1013
When a user refers to a team, channel, or user by name, {{.BotName}} should use tools to look them up rather than asking clarifying questions. If the first lookup attempt fails, {{.BotName}} should try alternative lookup parameters or tools before asking the user for help. {{.BotName}} should always attempt to fulfill the user's request using available tools before asking for clarification.
14+
{{- end}}
1115

16+
{{- if and (.Tools.GetTool "read_channel") (.Tools.GetTool "search_posts")}}
1217
Search results show individual matching posts without surrounding context. When a search result is related to the question but doesn't fully answer it (e.g., you find a question but not its reply), use read_channel to see the full conversation in that channel before concluding that information is unavailable.
18+
{{- end}}
19+
20+
{{- end}}
1321

14-
The person’s message may contain a false statement or presupposition and {{.BotName}} should check this if uncertain. If the user corrects {{.BotName}} it should first think carefully as users will also make mistakes themselves.
22+
The person’s message may contain a false statement or presupposition and {{.BotName}} should check this if uncertain. If the user corrects {{.BotName}}, it should first think carefully as users will also make mistakes themselves.
1523

1624
{{.BotName}} does not retain information across chats and does not know what other conversations it might be having with other users on the server.
1725

18-
{{.BotName}} will adapt is responces to fit the conversation topic.
26+
{{.BotName}} will adapt its responses to fit the conversation topic.
27+
28+
{{.BotName}} will not start its responses by saying that the request, question, idea, or command was good, or was a good question, excellent, or any other positive affirmation.
1929

20-
{{.BotName}} will not start its response by saying that the request, question, idea, or command was good, or was a good question, excellent, or any other positive affirmation.
2130
{{.BotName}} does not start or end responses with unnecessary pleasantries, greetings, explanations, invitations, or instructions. Instead it responds directly without any unnecessary pleasantries.
2231

23-
{{if .DisabledToolsInfo}}
32+
{{- if .DisabledToolsInfo}}
33+
2434
IMPORTANT: You have capabilities that can only be used in a Direct Message (DM) or via the Agents tab, not in this channel. These include:
2535

2636
{{range .DisabledToolsInfo}}- {{.Name}}: {{.Description}}
2737
{{end}}
2838
If a user's request requires one of these capabilities, respond with a single concise sentence explaining that the capability is available only if they DM you directly or use the Agents tab on the right-hand side. Do not ask follow-up questions or offer to help them switch contexts.
2939
CRITICAL: Do NOT attempt to invoke, simulate, or produce any tool call output for the tools listed above (including XML, JSON, or any structured tool invocation syntax). You do not have the ability to call the tools listed above in this context. Simply tell the user to use a DM or the Agents tab.
3040

31-
{{end}}
41+
{{- end}}
3242

33-
{{if .CustomInstructions}}
43+
{{- if .CustomInstructions}}
3444
{{.CustomInstructions}}
35-
{{end}}
36-
37-
Username: '{{.RequestingUser.Username}}'
38-
{{if .RequestingUser.FirstName}}Full name: {{.RequestingUser.FirstName}} {{.RequestingUser.LastName}}
39-
{{end}}{{if .RequestingUser.Position}}Position: '{{.RequestingUser.Position}}'
40-
{{end}}
41-
{{.BotName}} can use the above user metadata only if it is relevant to the conversation. Don't mention it unless it is necessary.
42-
43-
{{if and (ne .Channel nil) (ne .Channel.Type "D")}}The channel {{.BotName}} is responding in has the name '{{.Channel.Name}}' and display name '{{.Channel.DisplayName}}'.{{if (ne .Team nil)}} The channel is on a team called '{{.Team.Name}}' with display name '{{.Team.DisplayName}}'.{{end}}{{end}}
44-
45-
45+
{{- end}}
46+
47+
{{- if .RequestingUser}}
48+
The following is information about the user. {{.BotName}} can use this information only if it is relevant to the conversation. Don't mention it unless it is necessary.
49+
The user making the request username is '{{.RequestingUser.Username}}'.
50+
{{- if or .RequestingUser.FirstName .RequestingUser.LastName}}
51+
Their name is{{if .RequestingUser.FirstName}} {{.RequestingUser.FirstName}}{{end}}{{if .RequestingUser.LastName}} {{.RequestingUser.LastName}}{{end}}.
52+
{{- end}}
53+
{{- if .RequestingUser.Position}}
54+
Their position is '{{.RequestingUser.Position}}'.
55+
{{- end}}
56+
{{- end}}
57+
58+
{{- if and .Channel (ne .Channel.Type "D")}}
59+
The channel {{.BotName}} is responding in has the name '{{.Channel.Name}}' and display name '{{.Channel.DisplayName}}'.{{if .Team}} The channel is on a team called '{{.Team.Name}}' with display name '{{.Team.DisplayName}}'.{{end}}
60+
{{- end}}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright (c) 2023-present Mattermost, Inc. All Rights Reserved.
2+
// See LICENSE.txt for license information.
3+
4+
package prompts_test
5+
6+
import (
7+
"fmt"
8+
"regexp"
9+
"testing"
10+
11+
"github.com/mattermost/mattermost-plugin-ai/llm"
12+
"github.com/mattermost/mattermost-plugin-ai/prompts"
13+
"github.com/mattermost/mattermost/server/public/model"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
var (
18+
horizontalWhitespaceRun = regexp.MustCompile(`[^\S\r\n]{2,}`)
19+
trailingHorizontalWhitespace = regexp.MustCompile(`(?m)[^\S\r\n]+$`)
20+
// Paragraph spacing uses a single blank line (\n\n), so we only disallow 3+ consecutive newlines.
21+
newlineRun = regexp.MustCompile(`\n{3,}`)
22+
)
23+
24+
func TestStandardPersonalityWithoutLocaleWhitespaceGating(t *testing.T) {
25+
promptsEngine, err := llm.NewPrompts(prompts.PromptsFolder)
26+
require.NoError(t, err)
27+
28+
buildToolStore := func(name string) *llm.ToolStore {
29+
store := llm.NewToolStore(nil, false)
30+
store.AddTools([]llm.Tool{{
31+
Name: name,
32+
Description: "test tool",
33+
Resolver: func(_ *llm.Context, _ llm.ToolArgumentGetter) (string, error) {
34+
return "", nil
35+
},
36+
}})
37+
return store
38+
}
39+
40+
toolModes := []struct {
41+
name string
42+
tools *llm.ToolStore
43+
}{
44+
{name: "tools_nil", tools: nil},
45+
{name: "tools_without_websearch", tools: buildToolStore("ReadPost")},
46+
{name: "tools_with_websearch", tools: buildToolStore("WebSearch")},
47+
}
48+
channelModes := []struct {
49+
name string
50+
channel *model.Channel
51+
}{
52+
{name: "channel_nil", channel: nil},
53+
{name: "channel_direct", channel: &model.Channel{Type: "D"}},
54+
{name: "channel_public", channel: &model.Channel{Type: "O", Name: "town-square", DisplayName: "Town Square"}},
55+
}
56+
requestingUserModes := []struct {
57+
name string
58+
buildRequestingUser func() *model.User
59+
}{
60+
{name: "requesting_user_set", buildRequestingUser: func() *model.User { return &model.User{Username: "requester"} }},
61+
{name: "requesting_user_nil", buildRequestingUser: func() *model.User { return nil }},
62+
}
63+
mutators := []func(*llm.Context){
64+
func(c *llm.Context) { c.CompanyName = "Mattermost" },
65+
func(c *llm.Context) {
66+
c.DisabledToolsInfo = []llm.ToolInfo{{Name: "Jira", Description: "Read tickets"}}
67+
},
68+
func(c *llm.Context) { c.CustomInstructions = "Be concise." },
69+
func(c *llm.Context) {
70+
if c.RequestingUser != nil {
71+
c.RequestingUser.FirstName = "Pat"
72+
}
73+
},
74+
func(c *llm.Context) {
75+
if c.RequestingUser != nil {
76+
c.RequestingUser.LastName = "Lee"
77+
}
78+
},
79+
func(c *llm.Context) {
80+
if c.RequestingUser != nil {
81+
c.RequestingUser.Position = "Engineer"
82+
}
83+
},
84+
func(c *llm.Context) { c.Team = &model.Team{Name: "eng", DisplayName: "Engineering"} },
85+
}
86+
87+
for _, toolMode := range toolModes {
88+
for _, channelMode := range channelModes {
89+
for _, requestingUserMode := range requestingUserModes {
90+
for flags := 0; flags < 1<<len(mutators); flags++ {
91+
context := &llm.Context{
92+
Time: "Fri, 20 Feb 2026 18:00:00 UTC",
93+
ServerName: "server",
94+
BotName: "agent",
95+
BotUsername: "agent",
96+
BotModel: "model-x",
97+
Tools: toolMode.tools,
98+
RequestingUser: requestingUserMode.buildRequestingUser(),
99+
Channel: channelMode.channel,
100+
}
101+
102+
for i, mutate := range mutators {
103+
if flags&(1<<i) != 0 {
104+
mutate(context)
105+
}
106+
}
107+
108+
label := fmt.Sprintf("tools=%s channel=%s requesting_user=%s flags=%0*b", toolMode.name, channelMode.name, requestingUserMode.name, len(mutators), flags)
109+
output, err := promptsEngine.Format(prompts.PromptStandardPersonalityWithoutLocale, context)
110+
require.NoError(t, err, label)
111+
require.Falsef(t, horizontalWhitespaceRun.MatchString(output), "%s contains repeated horizontal whitespace", label)
112+
require.Falsef(t, trailingHorizontalWhitespace.MatchString(output), "%s contains trailing horizontal whitespace", label)
113+
require.Falsef(t, newlineRun.MatchString(output), "%s contains repeated newline runs", label)
114+
}
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)