refactor(core): use the websearch parameters passed in by the front-end (#11989)

Support [AI-60](https://linear.app/affine-design/issue/AI-60).

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **Refactor**
  - Updated naming conventions for the web search tool to ensure consistency across AI chat features.
  - Simplified internal handling of web search tool options for a more streamlined experience.
  - Unified parameter naming from "mustSearch" to "webSearch" across AI chat inputs and actions.
- **Chores**
  - Removed unused configuration options related to web search from prompt settings.
  - Added support for enabling or disabling web search via new parameters in chat requests.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
akumatus
2025-04-28 07:49:54 +00:00
committed by Yue Wu
parent e366f69707
commit cf5574caf6
12 changed files with 51 additions and 32 deletions
@@ -134,9 +134,7 @@ export class AnthropicProvider
providerOptions: {
anthropic: this.getAnthropicOptions(options),
},
tools: {
webSearch: createExaTool(this.AFFiNEConfig),
},
tools: this.getTools(options),
maxSteps: this.MAX_STEPS,
experimental_continueSteps: true,
});
@@ -168,9 +166,7 @@ export class AnthropicProvider
providerOptions: {
anthropic: this.getAnthropicOptions(options),
},
tools: {
webSearch: createExaTool(this.AFFiNEConfig),
},
tools: this.getTools(options),
maxSteps: this.MAX_STEPS,
experimental_continueSteps: true,
});
@@ -182,7 +178,7 @@ export class AnthropicProvider
break;
}
case 'tool-result': {
if (message.toolName === 'webSearch') {
if (message.toolName === 'web_search') {
this.toolResults.push(this.getWebSearchLinks(message.result));
}
break;
@@ -214,6 +210,15 @@ export class AnthropicProvider
}
}
private getTools(options: CopilotChatOptions) {
if (options?.webSearch) {
return {
web_search: createExaTool(this.AFFiNEConfig),
};
}
return undefined;
}
private getAnthropicOptions(
options: CopilotChatOptions
): AnthropicProviderOptions {
@@ -178,8 +178,8 @@ export class OpenAIProvider
}
}
private getToolUse(options: CopilotChatOptions = {}) {
if (options.webSearch) {
private getTools(options: CopilotChatOptions) {
if (options?.webSearch) {
return {
web_search_preview: openai.tools.webSearchPreview(),
};
@@ -224,6 +224,7 @@ export class OpenAIProvider
providerOptions: {
openai: options.user ? { user: options.user } : {},
},
tools: this.getTools(options),
});
return text.trim();
@@ -245,18 +246,16 @@ export class OpenAIProvider
const [system, msgs] = await chatToGPTMessage(messages);
const modelInstance = options.webSearch
? this.#instance.responses(model)
: this.#instance(model, {
structuredOutputs: Boolean(options.jsonMode),
user: options.user,
});
const modelInstance = this.#instance(model, {
structuredOutputs: Boolean(options.jsonMode),
user: options.user,
});
const { fullStream } = streamText({
model: modelInstance,
system,
messages: msgs,
tools: this.getToolUse(options),
tools: this.getTools(options),
frequencyPenalty: options.frequencyPenalty || 0,
presencePenalty: options.presencePenalty || 0,
temperature: options.temperature || 0,
@@ -22,7 +22,6 @@ export enum CopilotCapability {
export const PromptConfigStrictSchema = z.object({
// openai
jsonMode: z.boolean().nullable().optional(),
webSearch: z.boolean().nullable().optional(),
frequencyPenalty: z.number().nullable().optional(),
presencePenalty: z.number().nullable().optional(),
temperature: z.number().nullable().optional(),
@@ -81,6 +80,7 @@ const CopilotChatOptionsSchema = CopilotProviderOptionsSchema.merge(
)
.extend({
reasoning: z.boolean().optional(),
webSearch: z.boolean().optional(),
})
.optional();