Skip to content

feat: support trvily search #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/agent-tars/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"dependencies": {
"@agent-infra/mcp-server-commands": "workspace:*",
"@agent-infra/mcp-server-filesystem": "workspace:*",
"@agent-infra/mcp-server-browser": "workspace:*"
"@agent-infra/mcp-server-browser": "workspace:*",
"@tavily/core": "0.3.1"
},
"devDependencies": {
"@modelcontextprotocol/sdk": "^1.7.0",
Expand Down
71 changes: 53 additions & 18 deletions apps/agent-tars/src/main/customTools/search.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,67 @@
import { SearchSettings, ToolCall } from '@agent-infra/shared';
import { SearchClient, SearchProvider } from '@agent-infra/search';
import { SearchProvider, SearchSettings, ToolCall } from '@agent-infra/shared';
import {
SearchClient,
SearchProvider as SearchProviderEnum,
} from '@agent-infra/search';
import { MCPToolResult } from '@main/type';
import { tavily as tavilyCore } from '@tavily/core';

export const tavily = tavilyCore;

let currentSearchConfig: SearchSettings | null = null;

export function updateSearchConfig(config: SearchSettings) {
currentSearchConfig = config;
}

export async function search(toolCall: ToolCall): Promise<MCPToolResult> {
if (!currentSearchConfig) {
throw new Error('Search configuration not set');
}
const searchByTavily = async (options: { count: number; query: string }) => {
const client = tavily({
apiKey: process.env.TAVILY_API_KEY || currentSearchConfig?.apiKey,
});
const searchOptions = {
maxResults: options.count,
};

const args = JSON.parse(toolCall.function.arguments);
const response = await client.search(options.query, searchOptions);
return {
pages: (response.results || []).map((item) => ({
title: item.title || '',
url: item.url,
content: item.content,
})),
};
};

const client = new SearchClient({
provider: SearchProvider.BingSearch,
providerConfig: {
apiKey: currentSearchConfig.apiKey,
baseUrl: currentSearchConfig.baseUrl,
},
});
export async function search(toolCall: ToolCall): Promise<MCPToolResult> {
const args = JSON.parse(toolCall.function.arguments);

try {
const results = await client.search({
query: args.query,
count: 10,
});
if (!currentSearchConfig) {
throw new Error('Search configuration not set');
}

let results;
if (currentSearchConfig.provider === SearchProvider.TAVILY) {
results = await searchByTavily({
count: args.count,
query: args.query,
});
} else {
// Only for Bing Search, because Tavily is not supported in the bundle of this packages
// Error info: trvily is not defined
const client = new SearchClient({
provider: SearchProviderEnum.BingSearch,
providerConfig: {
apiKey: currentSearchConfig.apiKey,
baseUrl: currentSearchConfig.baseUrl,
},
});

results = await client.search({
query: args.query,
count: args.count || 10,
});
}

return [
{
Expand All @@ -36,6 +70,7 @@ export async function search(toolCall: ToolCall): Promise<MCPToolResult> {
},
];
} catch (e) {
console.error('Search error:', e);
return [
{
isError: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export function SearchSettingsTab({
>
Bing Search
</SelectItem>
<SelectItem
key={SearchProvider.TAVILY}
startContent={getSearchProviderLogo(SearchProvider.TAVILY)}
>
Tavily Search
</SelectItem>
</Select>

<Input
Expand All @@ -39,19 +45,29 @@ export function SearchSettingsTab({
value={settings.apiKey}
onChange={(e) => setSettings({ ...settings, apiKey: e.target.value })}
isRequired
description="Your Bing Search API key"
description={
settings.provider === SearchProvider.BING_SEARCH
? 'Your Bing Search API key'
: 'Your Tavily API key'
}
/>

<Divider className="my-2" />
<p className="text-sm text-default-500">Advanced Settings (Optional)</p>
{settings.provider === SearchProvider.BING_SEARCH ? (
<p className="text-sm text-default-500">Advanced Settings (Optional)</p>
) : null}

<Input
label="Custom Endpoint"
placeholder="https://api.bing.microsoft.com/"
value={settings.baseUrl || ''}
onChange={(e) => setSettings({ ...settings, baseUrl: e.target.value })}
description="Override the default Bing Search API endpoint"
/>
{settings.provider === SearchProvider.BING_SEARCH && (
<Input
label="Custom Endpoint"
placeholder="https://api.bing.microsoft.com/"
value={settings.baseUrl || ''}
onChange={(e) =>
setSettings({ ...settings, baseUrl: e.target.value })
}
description="Override the default Bing Search API endpoint"
/>
)}
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { SiMicrosoftbing } from 'react-icons/si';
import { TbSearch } from 'react-icons/tb';
import { SearchProvider } from '@agent-infra/shared';

export function getSearchProviderLogo(provider: SearchProvider) {
switch (provider) {
case SearchProvider.BING_SEARCH:
return <SiMicrosoftbing size={18} />;
case SearchProvider.TAVILY:
return <TbSearch size={18} />;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface FileSystemSettings {

export enum SearchProvider {
BING_SEARCH = 'bing_search',
TAVILY = 'tavily',
}

export interface SearchSettings {
Expand Down
2 changes: 1 addition & 1 deletion packages/agent-infra/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * from './common/index';
export * from './browser/index';
export * from './agent/index';
export * from './mcp/tools';
export * from './agen-tars-types';
export * from './agent-tars-types';
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading