Skip to content

Commit 3ba380d

Browse files
committed
perf: import tool
1 parent 41a936c commit 3ba380d

File tree

27 files changed

+635
-70
lines changed

27 files changed

+635
-70
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { defineTool } from '@tool/type';
2+
3+
export default defineTool({
4+
toolId: 'search',
5+
versionList: [
6+
{
7+
version: '0.1.0',
8+
description: 'Default version'
9+
}
10+
],
11+
type: 'search',
12+
name: {
13+
'zh-CN': 'DuckDuckGo 网络搜索',
14+
en: 'DuckDuckGo Network Search'
15+
},
16+
description: {
17+
'zh-CN': '使用 DuckDuckGo 进行网络搜索',
18+
en: 'Use DuckDuckGo to search the web'
19+
},
20+
icon: 'core/workflow/template/duckduckgo',
21+
inputs: [
22+
{
23+
renderTypeList: ['input', 'reference'],
24+
selectedTypeIndex: 0,
25+
valueType: 'string',
26+
key: 'query',
27+
label: 'query',
28+
description: '检索词',
29+
required: true,
30+
toolDescription: '检索词'
31+
}
32+
],
33+
outputs: [
34+
{
35+
id: 'result',
36+
valueType: 'string',
37+
key: 'result',
38+
label: '检索结果',
39+
type: 'static'
40+
}
41+
]
42+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import config from './config';
2+
import { InputType, tool as toolCb } from './src';
3+
import { exportTool } from '@tool/utils/tool';
4+
5+
export default exportTool({
6+
toolCb,
7+
InputType,
8+
config
9+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { search, SafeSearchType } from 'duck-duck-scrape';
2+
import { z } from 'zod';
3+
import { getErrText } from '@tool/utils/err';
4+
import { delay } from '@tool/utils/delay';
5+
6+
export const InputType = z.object({
7+
query: z.string()
8+
});
9+
10+
export const OutputType = z.object({
11+
result: z.string()
12+
});
13+
14+
const func = async (query: string, retry = 3) => {
15+
try {
16+
const searchResults = await search(query, {
17+
safeSearch: SafeSearchType.STRICT,
18+
time: 'y'
19+
});
20+
21+
const result = searchResults.results
22+
.map((item) => ({
23+
title: item.title,
24+
link: item.url,
25+
snippet: item.description
26+
}))
27+
.slice(0, 10);
28+
29+
return {
30+
result: JSON.stringify(result)
31+
};
32+
} catch (error) {
33+
console.log(error);
34+
if (retry <= 0) {
35+
console.log('DuckDuckGo error', { error });
36+
return {
37+
result: getErrText(error, 'Failed to fetch data from DuckDuckGo')
38+
};
39+
}
40+
41+
await delay(Math.random() * 5000);
42+
return func(query, retry - 1);
43+
}
44+
};
45+
46+
export async function tool(props: z.infer<typeof InputType>): Promise<z.infer<typeof OutputType>> {
47+
const { result } = await func(props.query);
48+
return { result };
49+
}

packages/tool/packages/duckduckgo/children/searchImg/test/index.test.ts renamed to packages/tool/packages/duckduckgo/search/test/index.test.ts

File renamed without changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { defineTool } from '@tool/type';
2+
3+
export default defineTool({
4+
toolId: 'searchImg',
5+
versionList: [
6+
{
7+
version: '0.1.0',
8+
description: 'Default version'
9+
}
10+
],
11+
type: 'search',
12+
name: {
13+
'zh-CN': 'DuckDuckGo 图片搜索',
14+
en: 'DockDuckGo Image Search'
15+
},
16+
description: {
17+
'zh-CN': '使用 DuckDuckGo 进行图片搜索',
18+
en: 'Use DuckDuckGo to search images'
19+
},
20+
icon: 'core/workflow/template/duckduckgo',
21+
inputs: [
22+
{
23+
renderTypeList: ['input', 'reference'],
24+
selectedTypeIndex: 0,
25+
valueType: 'string',
26+
key: 'query',
27+
label: 'query',
28+
description: '检索词',
29+
required: true,
30+
toolDescription: '检索词'
31+
}
32+
],
33+
outputs: [
34+
{
35+
id: 'result',
36+
type: 'static',
37+
valueType: 'string',
38+
key: 'result',
39+
label: 'result',
40+
description: ' 检索结果'
41+
}
42+
]
43+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import config from './config';
2+
import { InputType, tool as toolCb } from './src';
3+
import { exportTool } from '@tool/utils/tool';
4+
5+
export default exportTool({
6+
toolCb,
7+
InputType,
8+
config
9+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { delay } from '@tool/utils/delay';
2+
import { getErrText } from '@tool/utils/err';
3+
import { SafeSearchType, searchImages } from 'duck-duck-scrape';
4+
import { z } from 'zod';
5+
6+
export const InputType = z.object({
7+
query: z.string()
8+
});
9+
10+
export const OutputType = z.object({
11+
result: z.string()
12+
});
13+
14+
const func = async (query: string, retry = 3) => {
15+
try {
16+
const searchResults = await searchImages(query, {
17+
safeSearch: SafeSearchType.STRICT
18+
});
19+
20+
const result = searchResults.results
21+
.map((item) => ({
22+
title: item.title,
23+
image: item.image
24+
}))
25+
.slice(0, 10);
26+
27+
return {
28+
result: JSON.stringify(result)
29+
};
30+
} catch (error) {
31+
if (retry <= 0) {
32+
return {
33+
result: getErrText(error, 'Failed to fetch data from DuckDuckGo')
34+
};
35+
}
36+
37+
await delay(Math.random() * 5000);
38+
return func(query, retry - 1);
39+
}
40+
};
41+
42+
export async function tool(props: z.infer<typeof InputType>): Promise<z.infer<typeof OutputType>> {
43+
const { result } = await func(props.query);
44+
return { result };
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, test, vi } from 'vitest';
2+
import { main } from '..';
3+
4+
test('get current time', async () => {
5+
// Mock Date constructor to return a fixed date
6+
const date = new Date('2023-01-01T12:00:00.000');
7+
vi.useFakeTimers();
8+
vi.setSystemTime(date);
9+
const res = await main({
10+
formatStr: 'yyyy-MM-dd HH:mm:ss'
11+
});
12+
expect(res.output?.time).toBeDefined();
13+
expect(res.output?.time).toEqual('2023-01-01 12:00:00');
14+
vi.useRealTimers();
15+
});
16+
17+
test('get current time with a invalid format', async () => {
18+
// Mock Date constructor to return a fixed date
19+
const date = new Date('2023-01-01T12:00:00.000');
20+
vi.useFakeTimers();
21+
vi.setSystemTime(date);
22+
const res = await main({
23+
formatStr: 'someFormatStrisInvalid'
24+
});
25+
expect(res.error).toBeDefined();
26+
expect(res.output?.time).toBeUndefined();
27+
vi.useRealTimers();
28+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { defineTool } from '@tool/type';
2+
3+
export default defineTool({
4+
toolId: 'searchNews',
5+
versionList: [
6+
{
7+
version: '0.1.0',
8+
description: 'Default version'
9+
}
10+
],
11+
type: 'search',
12+
name: {
13+
'zh-CN': 'DuckDuckGo 新闻检索',
14+
en: 'DockDuckGo News Search'
15+
},
16+
description: {
17+
'zh-CN': '使用 DuckDuckGo 进行新闻检索',
18+
en: 'Use DuckDuckGo to search news'
19+
},
20+
icon: 'core/workflow/template/duckduckgo',
21+
inputs: [
22+
{
23+
renderTypeList: ['input', 'reference'],
24+
selectedTypeIndex: 0,
25+
valueType: 'string',
26+
key: 'query',
27+
label: 'query',
28+
description: '检索词',
29+
required: true,
30+
toolDescription: '检索词'
31+
}
32+
],
33+
outputs: [
34+
{
35+
id: 'result',
36+
type: 'static',
37+
valueType: 'string',
38+
key: 'result',
39+
label: 'result',
40+
description: ' 检索结果'
41+
}
42+
]
43+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import config from './config';
2+
import { InputType, tool as toolCb } from './src';
3+
import { exportTool } from '@tool/utils/tool';
4+
5+
export default exportTool({
6+
toolCb,
7+
InputType,
8+
config
9+
});

0 commit comments

Comments
 (0)