From c1f8f9525db14f30a1488379afe2443a4942e59c Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Tue, 17 Jun 2025 18:42:33 -0700 Subject: [PATCH] feat: add meta param to search queries --- src/agent.ts | 19 +++++++++++++++---- src/app.ts | 22 ++++++++++++++++++++++ src/tools/jina-search.ts | 4 +++- src/types.ts | 1 + 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/agent.ts b/src/agent.ts index dfe1c0c..c87f43d 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -18,7 +18,8 @@ import { BoostedSearchSnippet, SearchSnippet, EvaluationResponse, Reference, SERPQuery, RepeatEvaluationType, UnNormalizedSearchSnippet, WebContent, ImageObject, - ImageReference + ImageReference, + SearchAction } from "./types"; import { TrackerContext } from "./types"; import { search } from "./tools/jina-search"; @@ -281,7 +282,8 @@ async function executeSearchQueries( SchemaGen: Schemas, webContents: Record, onlyHostnames?: string[], - searchProvider?: string + searchProvider?: string, + meta?: string ): Promise<{ newKnowledge: KnowledgeItem[], searchedQueries: string[] @@ -303,7 +305,7 @@ async function executeSearchQueries( switch (searchProvider || SEARCH_PROVIDER) { case 'jina': case 'arxiv': - results = (await search(query, searchProvider, 30, context.tokenTracker)).response.results || []; + results = (await search(query, searchProvider, 30, meta, context.tokenTracker)).response.results || []; break; case 'duck': results = (await duckSearch(query.q, { safeSearch: SafeSearchType.STRICT })).results; @@ -380,6 +382,14 @@ async function executeSearchQueries( type: 'side-info', updated: query.tbs ? formatDateRange(query) : undefined }); + } finally { + context.actionTracker.trackAction({ + thisStep: { + action: 'search', + think: '', + searchRequests: [oldQuery] + } as SearchAction + }) } @@ -802,7 +812,8 @@ But then you realized you have asked them before. You decided to to think out of SchemaGen, allWebContents, undefined, - searchProvider + searchProvider, + 'more' ); allKeywords.push(...searchedQueries); diff --git a/src/app.ts b/src/app.ts index 0923458..df75666 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,6 +8,7 @@ import { ChatCompletionChunk, AnswerAction, Model, StepAction, VisitAction, + SearchAction, } from './types'; import { TokenTracker } from "./utils/token-tracker"; import { ActionTracker } from "./utils/action-tracker"; @@ -561,6 +562,27 @@ app.post('/v1/chat/completions', validationRules, (async (req: Request, res: Res res.write(`data: ${JSON.stringify(chunk)}\n\n`); }); } + + if (step.action === 'search') { + // emit every search request in the search action in url field + ((step as SearchAction).searchRequests as string[])?.forEach((query) => { + const chunk: ChatCompletionChunk = { + id: requestId, + object: 'chat.completion.chunk', + created, + model: body.model, + system_fingerprint: 'fp_' + requestId, + choices: [{ + index: 0, + delta: { type: 'think', query }, + logprobs: null, + finish_reason: null, + }] + }; + res.write(`data: ${JSON.stringify(chunk)}\n\n`); + }); + } + if (step.think) { // if not ends with a space, add one const content = step.think + ' '; diff --git a/src/tools/jina-search.ts b/src/tools/jina-search.ts index 0bcbcf1..6ca1bc9 100644 --- a/src/tools/jina-search.ts +++ b/src/tools/jina-search.ts @@ -8,6 +8,7 @@ export async function search( query: SERPQuery, domain?: string, num?: number, + meta?: string, tracker?: TokenTracker ): Promise<{ response: JinaSearchResponse }> { try { @@ -20,7 +21,8 @@ export async function search( { ...query, domain, - num + num, + meta }, { headers: { diff --git a/src/types.ts b/src/types.ts index 62e2d94..5dbff89 100644 --- a/src/types.ts +++ b/src/types.ts @@ -327,6 +327,7 @@ export interface ChatCompletionChunk { content?: string; type?: 'text' | 'think' | 'json' | 'error'; url?: string; + query?: string; annotations?: Array; }; logprobs: null;