mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-23 17:32:48 +08:00
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { tool } from 'ai';
|
|
import Exa from 'exa-js';
|
|
import { z } from 'zod';
|
|
|
|
import { Config } from '../../../base';
|
|
import { toolError } from './error';
|
|
|
|
export const createExaSearchTool = (config: Config) => {
|
|
return tool({
|
|
description: 'Search the web for information',
|
|
inputSchema: z.object({
|
|
query: z.string().describe('The query to search the web for.'),
|
|
mode: z
|
|
.enum(['MUST', 'AUTO'])
|
|
.describe('The mode to search the web for.'),
|
|
}),
|
|
execute: async ({ query, mode }) => {
|
|
try {
|
|
const { key } = config.copilot.exa;
|
|
const exa = new Exa(key);
|
|
const result = await exa.search(query, {
|
|
contents: {
|
|
summary: true,
|
|
livecrawl: mode === 'MUST' ? 'always' : undefined,
|
|
},
|
|
numResults: 10,
|
|
});
|
|
return result.results.map(data => ({
|
|
title: data.title,
|
|
url: data.url,
|
|
content: data.summary,
|
|
favicon: data.favicon,
|
|
publishedDate: data.publishedDate,
|
|
author: data.author,
|
|
}));
|
|
} catch (e: any) {
|
|
return toolError('Exa Search Failed', e.message);
|
|
}
|
|
},
|
|
});
|
|
};
|