Skip to content

Commit dd99f26

Browse files
committed
feat: support trvily search
1 parent 273abaf commit dd99f26

File tree

8 files changed

+88
-29
lines changed

8 files changed

+88
-29
lines changed

apps/agent-tars/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"dependencies": {
4141
"@agent-infra/mcp-server-commands": "workspace:*",
4242
"@agent-infra/mcp-server-filesystem": "workspace:*",
43-
"@agent-infra/mcp-server-browser": "workspace:*"
43+
"@agent-infra/mcp-server-browser": "workspace:*",
44+
"@tavily/core": "0.3.1"
4445
},
4546
"devDependencies": {
4647
"@modelcontextprotocol/sdk": "^1.7.0",

apps/agent-tars/src/main/customTools/search.ts

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,67 @@
1-
import { SearchSettings, ToolCall } from '@agent-infra/shared';
2-
import { SearchClient, SearchProvider } from '@agent-infra/search';
1+
import { SearchProvider, SearchSettings, ToolCall } from '@agent-infra/shared';
2+
import {
3+
SearchClient,
4+
SearchProvider as SearchProviderEnum,
5+
} from '@agent-infra/search';
36
import { MCPToolResult } from '@main/type';
7+
import { tavily as tavilyCore } from '@tavily/core';
8+
9+
export const tavily = tavilyCore;
410

511
let currentSearchConfig: SearchSettings | null = null;
612

713
export function updateSearchConfig(config: SearchSettings) {
814
currentSearchConfig = config;
915
}
1016

11-
export async function search(toolCall: ToolCall): Promise<MCPToolResult> {
12-
if (!currentSearchConfig) {
13-
throw new Error('Search configuration not set');
14-
}
17+
const searchByTavily = async (options: { count: number; query: string }) => {
18+
const client = tavily({
19+
apiKey: process.env.TAVILY_API_KEY || currentSearchConfig?.apiKey,
20+
});
21+
const searchOptions = {
22+
maxResults: options.count,
23+
};
1524

16-
const args = JSON.parse(toolCall.function.arguments);
25+
const response = await client.search(options.query, searchOptions);
26+
return {
27+
pages: (response.results || []).map((item) => ({
28+
title: item.title || '',
29+
url: item.url,
30+
content: item.content,
31+
})),
32+
};
33+
};
1734

18-
const client = new SearchClient({
19-
provider: SearchProvider.BingSearch,
20-
providerConfig: {
21-
apiKey: currentSearchConfig.apiKey,
22-
baseUrl: currentSearchConfig.baseUrl,
23-
},
24-
});
35+
export async function search(toolCall: ToolCall): Promise<MCPToolResult> {
36+
const args = JSON.parse(toolCall.function.arguments);
2537

2638
try {
27-
const results = await client.search({
28-
query: args.query,
29-
count: 10,
30-
});
39+
if (!currentSearchConfig) {
40+
throw new Error('Search configuration not set');
41+
}
42+
43+
let results;
44+
if (currentSearchConfig.provider === SearchProvider.TAVILY) {
45+
results = await searchByTavily({
46+
count: args.count,
47+
query: args.query,
48+
});
49+
} else {
50+
// Only for Bing Search, because Tavily is not supported in the bundle of this packages
51+
// Error info: trvily is not defined
52+
const client = new SearchClient({
53+
provider: SearchProviderEnum.BingSearch,
54+
providerConfig: {
55+
apiKey: currentSearchConfig.apiKey,
56+
baseUrl: currentSearchConfig.baseUrl,
57+
},
58+
});
59+
60+
results = await client.search({
61+
query: args.query,
62+
count: args.count || 10,
63+
});
64+
}
3165

3266
return [
3367
{
@@ -36,6 +70,7 @@ export async function search(toolCall: ToolCall): Promise<MCPToolResult> {
3670
},
3771
];
3872
} catch (e) {
73+
console.error('Search error:', e);
3974
return [
4075
{
4176
isError: true,

apps/agent-tars/src/renderer/src/components/LeftSidebar/Settings/SearchSettingsTab.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export function SearchSettingsTab({
3030
>
3131
Bing Search
3232
</SelectItem>
33+
<SelectItem
34+
key={SearchProvider.TAVILY}
35+
startContent={getSearchProviderLogo(SearchProvider.TAVILY)}
36+
>
37+
Tavily Search
38+
</SelectItem>
3339
</Select>
3440

3541
<Input
@@ -39,19 +45,29 @@ export function SearchSettingsTab({
3945
value={settings.apiKey}
4046
onChange={(e) => setSettings({ ...settings, apiKey: e.target.value })}
4147
isRequired
42-
description="Your Bing Search API key"
48+
description={
49+
settings.provider === SearchProvider.BING_SEARCH
50+
? 'Your Bing Search API key'
51+
: 'Your Tavily API key'
52+
}
4353
/>
4454

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

48-
<Input
49-
label="Custom Endpoint"
50-
placeholder="https://api.bing.microsoft.com/"
51-
value={settings.baseUrl || ''}
52-
onChange={(e) => setSettings({ ...settings, baseUrl: e.target.value })}
53-
description="Override the default Bing Search API endpoint"
54-
/>
60+
{settings.provider === SearchProvider.BING_SEARCH && (
61+
<Input
62+
label="Custom Endpoint"
63+
placeholder="https://api.bing.microsoft.com/"
64+
value={settings.baseUrl || ''}
65+
onChange={(e) =>
66+
setSettings({ ...settings, baseUrl: e.target.value })
67+
}
68+
description="Override the default Bing Search API endpoint"
69+
/>
70+
)}
5571
</div>
5672
);
5773
}

apps/agent-tars/src/renderer/src/components/LeftSidebar/Settings/searchUtils.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { SiMicrosoftbing } from 'react-icons/si';
2+
import { TbSearch } from 'react-icons/tb';
23
import { SearchProvider } from '@agent-infra/shared';
34

45
export function getSearchProviderLogo(provider: SearchProvider) {
56
switch (provider) {
67
case SearchProvider.BING_SEARCH:
78
return <SiMicrosoftbing size={18} />;
9+
case SearchProvider.TAVILY:
10+
return <TbSearch size={18} />;
811
default:
912
return null;
1013
}

packages/agent-infra/shared/src/agen-tars-types/search.ts renamed to packages/agent-infra/shared/src/agent-tars-types/search.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface FileSystemSettings {
2020

2121
export enum SearchProvider {
2222
BING_SEARCH = 'bing_search',
23+
TAVILY = 'tavily',
2324
}
2425

2526
export interface SearchSettings {

packages/agent-infra/shared/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export * from './common/index';
77
export * from './browser/index';
88
export * from './agent/index';
99
export * from './mcp/tools';
10-
export * from './agen-tars-types';
10+
export * from './agent-tars-types';

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)