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

View File

@@ -179,12 +179,16 @@ export class CopilotController implements BeforeApplicationShutdown {
const reasoning = Array.isArray(params.reasoning)
? Boolean(params.reasoning[0])
: Boolean(params.reasoning);
const webSearch = Array.isArray(params.webSearch)
? Boolean(params.webSearch[0])
: Boolean(params.webSearch);
delete params.messageId;
delete params.retry;
delete params.reasoning;
delete params.webSearch;
return { messageId, retry, reasoning, params };
return { messageId, retry, reasoning, webSearch, params };
}
private getSignal(req: Request) {
@@ -232,7 +236,8 @@ export class CopilotController implements BeforeApplicationShutdown {
const info: any = { sessionId, params };
try {
const { messageId, retry, reasoning } = this.prepareParams(params);
const { messageId, retry, reasoning, webSearch } =
this.prepareParams(params);
const provider = await this.chooseTextProvider(
user.id,
@@ -264,6 +269,7 @@ export class CopilotController implements BeforeApplicationShutdown {
signal: this.getSignal(req),
user: user.id,
reasoning,
webSearch,
});
session.push({
@@ -296,7 +302,8 @@ export class CopilotController implements BeforeApplicationShutdown {
const info: any = { sessionId, params, throwInStream: false };
try {
const { messageId, retry, reasoning } = this.prepareParams(params);
const { messageId, retry, reasoning, webSearch } =
this.prepareParams(params);
const provider = await this.chooseTextProvider(
user.id,
@@ -330,6 +337,7 @@ export class CopilotController implements BeforeApplicationShutdown {
signal: this.getSignal(req),
user: user.id,
reasoning,
webSearch,
})
).pipe(
connect(shared$ =>

View File

@@ -1044,9 +1044,9 @@ const chat: Prompt[] = [
content: `You are AFFiNE AI, a professional and humorous copilot within AFFiNE. You are powered by latest GPT model from OpenAI and AFFiNE. AFFiNE is an open source general purposed productivity tool that contains unified building blocks that users can use on any interfaces, including block-based docs editor, infinite canvas based edgeless graphic mode, or multi-dimensional table with multiple transformable views. Your mission is always to try your very best to assist users to use AFFiNE to write docs, draw diagrams or plan things with these abilities. You always think step-by-step and describe your plan for what to build, using well-structured and clear markdown, written out in great detail. Unless otherwise specified, where list, JSON, or code blocks are required for giving the output. Minimize any other prose so that your responses can be directly used and inserted into the docs. You are able to access to API of AFFiNE to finish your job. You always respect the users' privacy and would not leak their info to anyone else. AFFiNE is made by Toeverything .Pte .Ltd, a company registered in Singapore with a diverse and international team. The company also open sourced blocksuite and octobase for building tools similar to Affine. The name AFFiNE comes from the idea of AFFiNE transform, as blocks in affine can all transform in page, edgeless or database mode. AFFiNE team is now having 25 members, an open source company driven by engineers. Today is: {{affine::date}}, User's preferred language is {{affine::language}}.
# Response Guide
Use the webSearch tool to gather information from the web. There are two modes for web searching:
- MUST: Means you always need to use the webSearch tool to gather information from the web, no matter what the user's query is.
- CAN: Indicates that web searching is optional - you may use the webSearch tool at your discretion when you determine it would provide valuable information for answering the user's query.
Use the web search tool to gather information from the web. There are two modes for web searching:
- MUST: Means you always need to use the web search tool to gather information from the web, no matter what the user's query is.
- CAN: Indicates that web searching is optional - you may use the web search tool at your discretion when you determine it would provide valuable information for answering the user's query.
Currently, you are in the {{searchMode}} web searching mode.
I will provide you with some content fragments. There are two types of content fragments:
@@ -1115,9 +1115,6 @@ Below is the user's query. Please respond in the user's language without treatin
`,
},
],
config: {
webSearch: true,
},
},
{
name: 'Search With AFFiNE AI',

View File

@@ -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 {

View File

@@ -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,

View File

@@ -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();