maksym-work commited on
Commit
49eef8f
·
1 Parent(s): db46dac

feat: enhance text generation logic to conditionally trigger web search and manage tool preferences

Browse files
src/lib/server/textGeneration/index.ts CHANGED
@@ -21,6 +21,7 @@ import type { TextGenerationContext } from "./types";
21
  import type { ToolResult } from "$lib/types/Tool";
22
  import { toolHasName } from "../tools/utils";
23
  import directlyAnswer from "../tools/directlyAnswer";
 
24
 
25
  async function* keepAlive(done: AbortSignal): AsyncGenerator<MessageUpdate, undefined, undefined> {
26
  while (!done.aborted) {
@@ -59,12 +60,28 @@ async function* textGenerationWithoutTitle(
59
 
60
  let webSearchResult: WebSearch | undefined;
61
 
 
 
 
 
 
 
 
 
62
  // run websearch if:
63
  // - it's not continuing a previous message
64
  // - AND the model doesn't support tools and websearch is selected
65
  // - OR the assistant has websearch enabled (no tools for assistants for now)
66
- if (!isContinue && ((webSearch && !conv.assistantId) || assistantHasWebSearch(assistant))) {
67
  webSearchResult = yield* runWebSearch(conv, messages, assistant?.rag);
 
 
 
 
 
 
 
 
68
  }
69
 
70
  let preprompt = conv.preprompt;
@@ -72,10 +89,8 @@ async function* textGenerationWithoutTitle(
72
  preprompt = await processPreprompt(preprompt, messages.at(-1)?.content);
73
  if (messages[0].from === "system") messages[0].content = preprompt;
74
  }
75
-
76
  let toolResults: ToolResult[] = [];
77
- let tools = model.tools ? await getTools(toolsPreference, ctx.assistant) : undefined;
78
-
79
  if (tools) {
80
  const toolCallsRequired = tools.some((tool) => !toolHasName(directlyAnswer.name, tool));
81
  if (toolCallsRequired) {
 
21
  import type { ToolResult } from "$lib/types/Tool";
22
  import { toolHasName } from "../tools/utils";
23
  import directlyAnswer from "../tools/directlyAnswer";
24
+ import { webSearchToolId, fetchUrlToolId } from "$lib/utils/toolIds";
25
 
26
  async function* keepAlive(done: AbortSignal): AsyncGenerator<MessageUpdate, undefined, undefined> {
27
  while (!done.aborted) {
 
60
 
61
  let webSearchResult: WebSearch | undefined;
62
 
63
+ // Check if web search should be triggered
64
+ const hasWebSearchTool = toolsPreference?.includes(webSearchToolId) || false;
65
+ const shouldRunWebSearch =
66
+ !isContinue &&
67
+ ((webSearch && !conv.assistantId) || // Legacy web search flag
68
+ assistantHasWebSearch(assistant) || // Assistant has web search enabled
69
+ (hasWebSearchTool && !conv.assistantId)); // Modern tool-based web search
70
+
71
  // run websearch if:
72
  // - it's not continuing a previous message
73
  // - AND the model doesn't support tools and websearch is selected
74
  // - OR the assistant has websearch enabled (no tools for assistants for now)
75
+ if (shouldRunWebSearch) {
76
  webSearchResult = yield* runWebSearch(conv, messages, assistant?.rag);
77
+
78
+ // Remove web search tool from tools preference after it's used
79
+ // This prevents the AI from calling web search repeatedly in the same conversation
80
+ if (hasWebSearchTool && ctx.toolsPreference) {
81
+ ctx.toolsPreference = ctx.toolsPreference.filter(
82
+ (toolId) => toolId !== webSearchToolId && toolId !== fetchUrlToolId
83
+ );
84
+ }
85
  }
86
 
87
  let preprompt = conv.preprompt;
 
89
  preprompt = await processPreprompt(preprompt, messages.at(-1)?.content);
90
  if (messages[0].from === "system") messages[0].content = preprompt;
91
  }
 
92
  let toolResults: ToolResult[] = [];
93
+ let tools = model.tools ? await getTools(ctx.toolsPreference, ctx.assistant) : undefined;
 
94
  if (tools) {
95
  const toolCallsRequired = tools.some((tool) => !toolHasName(directlyAnswer.name, tool));
96
  if (toolCallsRequired) {