Skip to content

Commit 8861a5f

Browse files
authored
Add system setting to allow native web search in channels (#494)
* Add system setting to allow native web search in channels * Fix e2e
1 parent 644bf6e commit 8861a5f

File tree

11 files changed

+165
-90
lines changed

11 files changed

+165
-90
lines changed

anthropic/anthropic.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,22 @@ func (a *Anthropic) buildAPIParams(state *messageState) anthropicSDK.MessageNewP
208208
Messages: state.messages,
209209
}
210210

211+
// Add regular tools only if not disabled
211212
if !state.config.ToolsDisabled {
212213
params.Tools = convertTools(state.tools)
214+
}
213215

214-
if a.isNativeToolEnabled("web_search") {
215-
params.Tools = append(params.Tools, anthropicSDK.ToolUnionParam{
216-
OfWebSearchTool20250305: &anthropicSDK.WebSearchTool20250305Param{
217-
Name: "web_search",
218-
Type: "web_search_20250305",
219-
},
220-
})
221-
}
216+
// Add native web search if:
217+
// 1. Tools are not disabled, OR
218+
// 2. Native web search is explicitly allowed (for channel context)
219+
// AND the agent has web_search enabled in their native tools config
220+
if (!state.config.ToolsDisabled || state.config.NativeWebSearchAllowed) && a.isNativeToolEnabled("web_search") {
221+
params.Tools = append(params.Tools, anthropicSDK.ToolUnionParam{
222+
OfWebSearchTool20250305: &anthropicSDK.WebSearchTool20250305Param{
223+
Name: "web_search",
224+
Type: "web_search_20250305",
225+
},
226+
})
222227
}
223228

224229
if state.system != "" {

bots/bot.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func (b *Bot) GetService() llm.ServiceConfig {
4242
return b.service
4343
}
4444

45+
func (b *Bot) HasNativeWebSearchEnabled() bool {
46+
for _, tool := range b.cfg.EnabledNativeTools {
47+
if tool == "web_search" {
48+
return true
49+
}
50+
}
51+
return false
52+
}
53+
4554
func (b *Bot) SetLLMForTest(llm llm.LanguageModel) {
4655
b.llm = llm
4756
}

config/config.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ import (
1616
)
1717

1818
type Config struct {
19-
Services []llm.ServiceConfig `json:"services"`
20-
Bots []llm.BotConfig `json:"bots"`
21-
DefaultBotName string `json:"defaultBotName"`
22-
TranscriptGenerator string `json:"transcriptBackend"`
23-
EnableLLMTrace bool `json:"enableLLMTrace"`
24-
EnableTokenUsageLogging bool `json:"enableTokenUsageLogging"`
25-
AllowedUpstreamHostnames string `json:"allowedUpstreamHostnames"`
26-
AllowUnsafeLinks bool `json:"allowUnsafeLinks"`
27-
EmbeddingSearchConfig embeddings.EmbeddingSearchConfig `json:"embeddingSearchConfig"`
28-
MCP mcp.Config `json:"mcp"`
29-
WebSearch WebSearchConfig `json:"webSearch"`
19+
Services []llm.ServiceConfig `json:"services"`
20+
Bots []llm.BotConfig `json:"bots"`
21+
DefaultBotName string `json:"defaultBotName"`
22+
TranscriptGenerator string `json:"transcriptBackend"`
23+
EnableLLMTrace bool `json:"enableLLMTrace"`
24+
EnableTokenUsageLogging bool `json:"enableTokenUsageLogging"`
25+
AllowedUpstreamHostnames string `json:"allowedUpstreamHostnames"`
26+
AllowUnsafeLinks bool `json:"allowUnsafeLinks"`
27+
AllowNativeWebSearchInChannels bool `json:"allowNativeWebSearchInChannels"`
28+
EmbeddingSearchConfig embeddings.EmbeddingSearchConfig `json:"embeddingSearchConfig"`
29+
MCP mcp.Config `json:"mcp"`
30+
WebSearch WebSearchConfig `json:"webSearch"`
3031
}
3132

3233
type WebSearchConfig struct {
@@ -121,6 +122,15 @@ func (c *Container) AllowUnsafeLinks() bool {
121122
return cfg.AllowUnsafeLinks
122123
}
123124

125+
func (c *Container) AllowNativeWebSearchInChannels() bool {
126+
cfg := c.cfg.Load()
127+
if cfg == nil {
128+
return false
129+
}
130+
131+
return cfg.AllowNativeWebSearchInChannels
132+
}
133+
124134
func (c *Container) RegisterUpdateListener(listener UpdateListener) {
125135
c.listeners = append(c.listeners, listener)
126136
}

conversations/conversations.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ type AIThread struct {
3939
UpdateAt int64 `json:"update_at"`
4040
}
4141

42+
type ConfigProvider interface {
43+
AllowNativeWebSearchInChannels() bool
44+
}
45+
4246
type Conversations struct {
4347
prompts *llm.Prompts
4448
mmClient mmapi.Client
@@ -49,6 +53,7 @@ type Conversations struct {
4953
licenseChecker *enterprise.LicenseChecker
5054
i18n *i18n.Bundle
5155
meetingsService MeetingsService
56+
configProvider ConfigProvider
5257
}
5358

5459
// MeetingsService defines the interface for meetings functionality needed by conversations
@@ -67,6 +72,7 @@ func New(
6772
licenseChecker *enterprise.LicenseChecker,
6873
i18nBundle *i18n.Bundle,
6974
meetingsService MeetingsService,
75+
configProvider ConfigProvider,
7076
) *Conversations {
7177
return &Conversations{
7278
prompts: prompts,
@@ -78,6 +84,7 @@ func New(
7884
licenseChecker: licenseChecker,
7985
i18n: i18nBundle,
8086
meetingsService: meetingsService,
87+
configProvider: configProvider,
8188
}
8289
}
8390

@@ -133,8 +140,11 @@ func (c *Conversations) ProcessUserRequestWithContext(bot *bots.Bot, postingUser
133140
}
134141
var opts []llm.LanguageModelOption
135142
if !isDM {
136-
// In non-DM channels, disable tools for security but provide info about DM-only tools
137143
opts = append(opts, llm.WithToolsDisabled())
144+
145+
if c.configProvider != nil && c.configProvider.AllowNativeWebSearchInChannels() && bot.HasNativeWebSearchEnabled() {
146+
opts = append(opts, llm.WithNativeWebSearchAllowed())
147+
}
138148
}
139149
result, err := bot.LLM().ChatCompletion(completionRequest, opts...)
140150
if err != nil {

conversations/conversations_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func TestConversationMentionHandling(t *testing.T) {
131131
licenseChecker,
132132
i18n.Init(),
133133
nil,
134+
nil,
134135
)
135136

136137
// Create a mock bot

conversations/direct_message_eval_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func TestDirectMessageConversations(t *testing.T) {
129129
licenseChecker,
130130
i18n.Init(),
131131
nil,
132+
nil,
132133
)
133134

134135
// Create a mock bot for DM

0 commit comments

Comments
 (0)