@illuma-ai/agents 1.5.1 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -62
- package/dist/cjs/agents/AgentContext.cjs +160 -259
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +12 -12
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +30 -13
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/graphs/phases/memoryFlushPhase.cjs +1 -1
- package/dist/cjs/graphs/phases/memoryFlushPhase.cjs.map +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/matchers.cjs +2 -2
- package/dist/cjs/hooks/matchers.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +1 -1
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +1 -5
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +33 -61
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +1 -1
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +10 -27
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +3 -84
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/memory/citations.cjs +4 -4
- package/dist/cjs/memory/citations.cjs.map +1 -1
- package/dist/cjs/memory/constants.cjs +17 -17
- package/dist/cjs/memory/constants.cjs.map +1 -1
- package/dist/cjs/memory/mmr.cjs +1 -1
- package/dist/cjs/memory/mmr.cjs.map +1 -1
- package/dist/cjs/memory/paths.cjs +1 -1
- package/dist/cjs/memory/paths.cjs.map +1 -1
- package/dist/cjs/memory/recallTracking.cjs +3 -3
- package/dist/cjs/memory/recallTracking.cjs.map +1 -1
- package/dist/cjs/memory/temporalDecay.cjs +2 -2
- package/dist/cjs/memory/temporalDecay.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +0 -89
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +13 -71
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +11 -21
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +13 -41
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +11 -16
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +78 -13
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/memory/memoryAppendTool.cjs +1 -1
- package/dist/cjs/tools/memory/memoryAppendTool.cjs.map +1 -1
- package/dist/cjs/tools/memory/memoryGetTool.cjs +2 -2
- package/dist/cjs/tools/memory/memoryGetTool.cjs.map +1 -1
- package/dist/cjs/tools/memory/memorySearchTool.cjs +3 -3
- package/dist/cjs/tools/memory/memorySearchTool.cjs.map +1 -1
- package/dist/cjs/tools/memory/shared.cjs +1 -1
- package/dist/cjs/tools/memory/shared.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +3 -11
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +4 -28
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +3 -10
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +48 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/types/graph.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +160 -259
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +12 -12
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +30 -13
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/graphs/phases/memoryFlushPhase.mjs +1 -1
- package/dist/esm/graphs/phases/memoryFlushPhase.mjs.map +1 -1
- package/dist/esm/hooks/HookRegistry.mjs +1 -1
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/matchers.mjs +2 -2
- package/dist/esm/hooks/matchers.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +1 -1
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +1 -5
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +34 -61
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +1 -1
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +10 -27
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -5
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/memory/citations.mjs +4 -4
- package/dist/esm/memory/citations.mjs.map +1 -1
- package/dist/esm/memory/constants.mjs +17 -17
- package/dist/esm/memory/constants.mjs.map +1 -1
- package/dist/esm/memory/mmr.mjs +1 -1
- package/dist/esm/memory/mmr.mjs.map +1 -1
- package/dist/esm/memory/paths.mjs +1 -1
- package/dist/esm/memory/paths.mjs.map +1 -1
- package/dist/esm/memory/recallTracking.mjs +3 -3
- package/dist/esm/memory/recallTracking.mjs.map +1 -1
- package/dist/esm/memory/temporalDecay.mjs +2 -2
- package/dist/esm/memory/temporalDecay.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +0 -89
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +13 -71
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +12 -22
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +14 -41
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +12 -17
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +78 -13
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/memory/memoryAppendTool.mjs +1 -1
- package/dist/esm/tools/memory/memoryAppendTool.mjs.map +1 -1
- package/dist/esm/tools/memory/memoryGetTool.mjs +2 -2
- package/dist/esm/tools/memory/memoryGetTool.mjs.map +1 -1
- package/dist/esm/tools/memory/memorySearchTool.mjs +3 -3
- package/dist/esm/tools/memory/memorySearchTool.mjs.map +1 -1
- package/dist/esm/tools/memory/shared.mjs +1 -1
- package/dist/esm/tools/memory/shared.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +3 -11
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +4 -28
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +3 -10
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +48 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/types/graph.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +25 -95
- package/dist/types/common/enum.d.ts +12 -12
- package/dist/types/graphs/Graph.d.ts +2 -2
- package/dist/types/graphs/phases/memoryFlushPhase.d.ts +2 -2
- package/dist/types/hooks/HookRegistry.d.ts +1 -1
- package/dist/types/hooks/matchers.d.ts +2 -2
- package/dist/types/hooks/types.d.ts +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/llm/bedrock/index.d.ts +1 -54
- package/dist/types/llm/openai/index.d.ts +1 -1
- package/dist/types/memory/citations.d.ts +4 -4
- package/dist/types/memory/constants.d.ts +17 -17
- package/dist/types/memory/mmr.d.ts +3 -3
- package/dist/types/memory/paths.d.ts +1 -1
- package/dist/types/memory/temporalDecay.d.ts +2 -2
- package/dist/types/memory/types.d.ts +3 -3
- package/dist/types/messages/format.d.ts +2 -5
- package/dist/types/tools/CodeExecutor.d.ts +0 -6
- package/dist/types/tools/ToolNode.d.ts +3 -3
- package/dist/types/tools/memory/shared.d.ts +1 -1
- package/dist/types/tools/search/test.d.ts +1 -0
- package/dist/types/tools/search/types.d.ts +5 -99
- package/dist/types/tools/search/utils.d.ts +2 -2
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +29 -0
- package/dist/types/types/graph.d.ts +30 -34
- package/dist/types/types/index.d.ts +0 -1
- package/dist/types/types/messages.d.ts +1 -1
- package/dist/types/types/run.d.ts +1 -3
- package/dist/types/types/tools.d.ts +5 -14
- package/package.json +1 -61
- package/src/agents/AgentContext.test.ts +176 -0
- package/src/agents/AgentContext.ts +179 -305
- package/src/agents/__tests__/AgentContext.test.ts +0 -632
- package/src/common/__tests__/enum.test.ts +1 -1
- package/src/common/enum.ts +12 -12
- package/src/graphs/Graph.ts +32 -13
- package/src/graphs/MultiAgentGraph.ts +1 -1
- package/src/graphs/gapFeatures.test.ts +1 -1
- package/src/graphs/phases/__tests__/memoryFlushPhase.test.ts +1 -1
- package/src/graphs/phases/memoryFlushPhase.ts +2 -2
- package/src/hooks/HookRegistry.ts +1 -1
- package/src/hooks/index.ts +1 -1
- package/src/hooks/matchers.ts +2 -2
- package/src/hooks/types.ts +1 -1
- package/src/index.ts +0 -6
- package/src/llm/anthropic/utils/message_inputs.ts +1 -10
- package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +18 -166
- package/src/llm/bedrock/index.ts +41 -116
- package/src/llm/openai/index.ts +2 -2
- package/src/llm/openai/utils/index.ts +14 -31
- package/src/memory/citations.ts +4 -4
- package/src/memory/constants.ts +17 -17
- package/src/memory/mmr.ts +3 -3
- package/src/memory/paths.ts +1 -1
- package/src/memory/recallTracking.ts +3 -3
- package/src/memory/temporalDecay.ts +2 -2
- package/src/memory/types.ts +3 -3
- package/src/messages/cache.test.ts +24 -62
- package/src/messages/cache.ts +0 -112
- package/src/messages/ensureThinkingBlock.test.ts +1 -1
- package/src/messages/format.ts +13 -92
- package/src/messages/formatAgentMessages.test.ts +1 -1
- package/src/scripts/subagent-configurable-inheritance.ts +263 -0
- package/src/scripts/subagent-event-driven-debug.ts +2 -2
- package/src/specs/anthropic.simple.test.ts +0 -61
- package/src/specs/prune.orphans.test.ts +1 -1
- package/src/tools/BashExecutor.ts +13 -37
- package/src/tools/CodeExecutor.ts +14 -59
- package/src/tools/ProgrammaticToolCalling.ts +14 -29
- package/src/tools/ToolNode.ts +75 -14
- package/src/tools/__tests__/CodeExecutor.test.ts +3 -3
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -60
- package/src/tools/__tests__/SubagentExecutor.test.ts +157 -0
- package/src/tools/memory/memoryAppendTool.ts +1 -1
- package/src/tools/memory/memoryGetTool.ts +2 -2
- package/src/tools/memory/memorySearchTool.ts +3 -3
- package/src/tools/memory/shared.ts +1 -1
- package/src/tools/search/output.md +2775 -0
- package/src/tools/search/search.ts +2 -12
- package/src/tools/search/test.html +884 -0
- package/src/tools/search/test.md +643 -0
- package/src/tools/search/test.ts +159 -0
- package/src/tools/search/tool.ts +2 -36
- package/src/tools/search/types.ts +8 -133
- package/src/tools/search/utils.ts +5 -13
- package/src/tools/subagent/SubagentExecutor.ts +78 -0
- package/src/types/graph.ts +27 -34
- package/src/types/index.ts +0 -1
- package/src/types/messages.ts +1 -1
- package/src/types/run.ts +1 -3
- package/src/types/tools.ts +5 -14
- package/dist/cjs/langchain/google-common.cjs +0 -3
- package/dist/cjs/langchain/google-common.cjs.map +0 -1
- package/dist/cjs/langchain/index.cjs +0 -86
- package/dist/cjs/langchain/index.cjs.map +0 -1
- package/dist/cjs/langchain/language_models/chat_models.cjs +0 -3
- package/dist/cjs/langchain/language_models/chat_models.cjs.map +0 -1
- package/dist/cjs/langchain/messages/tool.cjs +0 -3
- package/dist/cjs/langchain/messages/tool.cjs.map +0 -1
- package/dist/cjs/langchain/messages.cjs +0 -51
- package/dist/cjs/langchain/messages.cjs.map +0 -1
- package/dist/cjs/langchain/openai.cjs +0 -3
- package/dist/cjs/langchain/openai.cjs.map +0 -1
- package/dist/cjs/langchain/prompts.cjs +0 -11
- package/dist/cjs/langchain/prompts.cjs.map +0 -1
- package/dist/cjs/langchain/runnables.cjs +0 -19
- package/dist/cjs/langchain/runnables.cjs.map +0 -1
- package/dist/cjs/langchain/tools.cjs +0 -23
- package/dist/cjs/langchain/tools.cjs.map +0 -1
- package/dist/cjs/langchain/utils/env.cjs +0 -11
- package/dist/cjs/langchain/utils/env.cjs.map +0 -1
- package/dist/cjs/llm/bedrock/cacheSupport.cjs +0 -55
- package/dist/cjs/llm/bedrock/cacheSupport.cjs.map +0 -1
- package/dist/cjs/tools/search/tavily-scraper.cjs +0 -189
- package/dist/cjs/tools/search/tavily-scraper.cjs.map +0 -1
- package/dist/cjs/tools/search/tavily-search.cjs +0 -372
- package/dist/cjs/tools/search/tavily-search.cjs.map +0 -1
- package/dist/cjs/types/agent-cache.cjs +0 -54
- package/dist/cjs/types/agent-cache.cjs.map +0 -1
- package/dist/esm/langchain/google-common.mjs +0 -2
- package/dist/esm/langchain/google-common.mjs.map +0 -1
- package/dist/esm/langchain/index.mjs +0 -5
- package/dist/esm/langchain/index.mjs.map +0 -1
- package/dist/esm/langchain/language_models/chat_models.mjs +0 -2
- package/dist/esm/langchain/language_models/chat_models.mjs.map +0 -1
- package/dist/esm/langchain/messages/tool.mjs +0 -2
- package/dist/esm/langchain/messages/tool.mjs.map +0 -1
- package/dist/esm/langchain/messages.mjs +0 -2
- package/dist/esm/langchain/messages.mjs.map +0 -1
- package/dist/esm/langchain/openai.mjs +0 -2
- package/dist/esm/langchain/openai.mjs.map +0 -1
- package/dist/esm/langchain/prompts.mjs +0 -2
- package/dist/esm/langchain/prompts.mjs.map +0 -1
- package/dist/esm/langchain/runnables.mjs +0 -2
- package/dist/esm/langchain/runnables.mjs.map +0 -1
- package/dist/esm/langchain/tools.mjs +0 -2
- package/dist/esm/langchain/tools.mjs.map +0 -1
- package/dist/esm/langchain/utils/env.mjs +0 -2
- package/dist/esm/langchain/utils/env.mjs.map +0 -1
- package/dist/esm/llm/bedrock/cacheSupport.mjs +0 -52
- package/dist/esm/llm/bedrock/cacheSupport.mjs.map +0 -1
- package/dist/esm/tools/search/tavily-scraper.mjs +0 -186
- package/dist/esm/tools/search/tavily-scraper.mjs.map +0 -1
- package/dist/esm/tools/search/tavily-search.mjs +0 -370
- package/dist/esm/tools/search/tavily-search.mjs.map +0 -1
- package/dist/esm/types/agent-cache.mjs +0 -52
- package/dist/esm/types/agent-cache.mjs.map +0 -1
- package/dist/types/langchain/google-common.d.ts +0 -1
- package/dist/types/langchain/index.d.ts +0 -8
- package/dist/types/langchain/language_models/chat_models.d.ts +0 -1
- package/dist/types/langchain/messages/tool.d.ts +0 -1
- package/dist/types/langchain/messages.d.ts +0 -2
- package/dist/types/langchain/openai.d.ts +0 -1
- package/dist/types/langchain/prompts.d.ts +0 -1
- package/dist/types/langchain/runnables.d.ts +0 -2
- package/dist/types/langchain/tools.d.ts +0 -2
- package/dist/types/langchain/utils/env.d.ts +0 -1
- package/dist/types/llm/bedrock/cacheSupport.d.ts +0 -35
- package/dist/types/tools/search/tavily-scraper.d.ts +0 -19
- package/dist/types/tools/search/tavily-search.d.ts +0 -4
- package/dist/types/tools/subagent/types.d.ts +0 -84
- package/dist/types/types/agent-cache.d.ts +0 -71
- package/src/agents/__tests__/AgentContext.cacheTtl.live.test.ts +0 -259
- package/src/agents/__tests__/AgentContext.crossAgentTier1.live.test.ts +0 -266
- package/src/agents/__tests__/AgentContext.crossUserCache.live.test.ts +0 -342
- package/src/langchain/google-common.ts +0 -1
- package/src/langchain/index.ts +0 -8
- package/src/langchain/language_models/chat_models.ts +0 -1
- package/src/langchain/messages/tool.ts +0 -5
- package/src/langchain/messages.ts +0 -21
- package/src/langchain/openai.ts +0 -1
- package/src/langchain/prompts.ts +0 -1
- package/src/langchain/runnables.ts +0 -7
- package/src/langchain/tools.ts +0 -8
- package/src/langchain/utils/env.ts +0 -1
- package/src/llm/anthropic/utils/server-tool-inputs.test.ts +0 -436
- package/src/llm/bedrock/cacheSupport.test.ts +0 -99
- package/src/llm/bedrock/cacheSupport.ts +0 -53
- package/src/tools/search/tavily-scraper.ts +0 -235
- package/src/tools/search/tavily-search.ts +0 -424
- package/src/tools/search/tavily.test.ts +0 -965
- package/src/tools/subagent/types.test.ts +0 -70
- package/src/tools/subagent/types.ts +0 -115
- package/src/types/agent-cache.ts +0 -74
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import type * as t from './types';
|
|
3
|
-
import { createDefaultLogger } from './utils';
|
|
4
|
-
|
|
5
|
-
const DEFAULT_BASIC_TIMEOUT = 15000;
|
|
6
|
-
const DEFAULT_ADVANCED_TIMEOUT = 30000;
|
|
7
|
-
const MAX_BATCH_SIZE = 20;
|
|
8
|
-
|
|
9
|
-
const getDefaultTimeout = (extractDepth: 'basic' | 'advanced'): number =>
|
|
10
|
-
extractDepth === 'advanced'
|
|
11
|
-
? DEFAULT_ADVANCED_TIMEOUT
|
|
12
|
-
: DEFAULT_BASIC_TIMEOUT;
|
|
13
|
-
|
|
14
|
-
const normalizeUrlKey = (url: string): string => {
|
|
15
|
-
try {
|
|
16
|
-
const parsedUrl = new URL(url);
|
|
17
|
-
parsedUrl.hash = '';
|
|
18
|
-
if (parsedUrl.pathname.length > 1) {
|
|
19
|
-
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, '');
|
|
20
|
-
}
|
|
21
|
-
return parsedUrl.toString();
|
|
22
|
-
} catch {
|
|
23
|
-
return url;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const setUrlResult = (
|
|
28
|
-
map: Map<string, t.TavilyExtractResult>,
|
|
29
|
-
result: t.TavilyExtractResult
|
|
30
|
-
): void => {
|
|
31
|
-
map.set(result.url, result);
|
|
32
|
-
const normalizedUrl = normalizeUrlKey(result.url);
|
|
33
|
-
if (!map.has(normalizedUrl)) {
|
|
34
|
-
map.set(normalizedUrl, result);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export class TavilyScraper implements t.BaseScraper {
|
|
39
|
-
private apiKey: string;
|
|
40
|
-
private apiUrl: string;
|
|
41
|
-
private timeout: number;
|
|
42
|
-
private payloadTimeout: number | undefined;
|
|
43
|
-
private logger: t.Logger;
|
|
44
|
-
private extractDepth: 'basic' | 'advanced';
|
|
45
|
-
private includeImages: boolean;
|
|
46
|
-
private includeFavicon: boolean;
|
|
47
|
-
private format: 'markdown' | 'text' | undefined;
|
|
48
|
-
|
|
49
|
-
constructor(config: t.TavilyScraperConfig = {}) {
|
|
50
|
-
this.apiKey = config.apiKey ?? process.env.TAVILY_API_KEY ?? '';
|
|
51
|
-
this.apiUrl =
|
|
52
|
-
config.apiUrl ??
|
|
53
|
-
process.env.TAVILY_EXTRACT_URL ??
|
|
54
|
-
'https://api.tavily.com/extract';
|
|
55
|
-
this.payloadTimeout = config.timeout;
|
|
56
|
-
this.extractDepth = config.extractDepth ?? 'basic';
|
|
57
|
-
this.timeout = config.timeout ?? getDefaultTimeout(this.extractDepth);
|
|
58
|
-
this.includeImages = config.includeImages ?? false;
|
|
59
|
-
this.includeFavicon = config.includeFavicon ?? false;
|
|
60
|
-
this.format = config.format;
|
|
61
|
-
this.logger = config.logger || createDefaultLogger();
|
|
62
|
-
|
|
63
|
-
if (!this.apiKey) {
|
|
64
|
-
this.logger.warn('TAVILY_API_KEY is not set. Scraping will not work.');
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async scrapeUrl(
|
|
69
|
-
url: string,
|
|
70
|
-
options: t.TavilyScrapeOptions = {}
|
|
71
|
-
): Promise<[string, t.TavilyScrapeResponse]> {
|
|
72
|
-
const results = await this.scrapeUrls([url], options);
|
|
73
|
-
return results[0];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async scrapeUrls(
|
|
77
|
-
urls: string[],
|
|
78
|
-
options: t.TavilyScrapeOptions = {}
|
|
79
|
-
): Promise<Array<[string, t.TavilyScrapeResponse]>> {
|
|
80
|
-
if (!this.apiKey) {
|
|
81
|
-
return urls.map((url) => [
|
|
82
|
-
url,
|
|
83
|
-
{ success: false, error: 'TAVILY_API_KEY is not set' },
|
|
84
|
-
]);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const batches: string[][] = [];
|
|
88
|
-
for (let i = 0; i < urls.length; i += MAX_BATCH_SIZE) {
|
|
89
|
-
batches.push(urls.slice(i, i + MAX_BATCH_SIZE));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const allResults: Array<[string, t.TavilyScrapeResponse]> = [];
|
|
93
|
-
|
|
94
|
-
for (const batch of batches) {
|
|
95
|
-
const batchResults = await this.extractBatch(batch, options);
|
|
96
|
-
allResults.push(...batchResults);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return allResults;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private async extractBatch(
|
|
103
|
-
urls: string[],
|
|
104
|
-
options: t.TavilyScrapeOptions = {}
|
|
105
|
-
): Promise<Array<[string, t.TavilyScrapeResponse]>> {
|
|
106
|
-
try {
|
|
107
|
-
const includeFavicon = options.includeFavicon ?? this.includeFavicon;
|
|
108
|
-
const format = options.format ?? this.format;
|
|
109
|
-
const extractDepth = options.extractDepth ?? this.extractDepth;
|
|
110
|
-
const payload: t.TavilyExtractPayload = {
|
|
111
|
-
urls,
|
|
112
|
-
extract_depth: extractDepth,
|
|
113
|
-
include_images: options.includeImages ?? this.includeImages,
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
if (includeFavicon) {
|
|
117
|
-
payload.include_favicon = true;
|
|
118
|
-
}
|
|
119
|
-
if (format != null) {
|
|
120
|
-
payload.format = format;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const effectiveTimeout =
|
|
124
|
-
options.timeout ??
|
|
125
|
-
this.payloadTimeout ??
|
|
126
|
-
(options.extractDepth != null
|
|
127
|
-
? getDefaultTimeout(extractDepth)
|
|
128
|
-
: this.timeout);
|
|
129
|
-
const payloadTimeout = options.timeout ?? this.payloadTimeout;
|
|
130
|
-
if (payloadTimeout != null) {
|
|
131
|
-
payload.timeout = Math.min(Math.max(payloadTimeout / 1000, 1), 60);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const response = await axios.post<{
|
|
135
|
-
results?: t.TavilyExtractResult[];
|
|
136
|
-
failed_results?: t.TavilyExtractResult[];
|
|
137
|
-
}>(this.apiUrl, payload, {
|
|
138
|
-
headers: {
|
|
139
|
-
Authorization: `Bearer ${this.apiKey}`,
|
|
140
|
-
'Content-Type': 'application/json',
|
|
141
|
-
},
|
|
142
|
-
timeout: effectiveTimeout,
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const data = response.data;
|
|
146
|
-
const successMap = new Map<string, t.TavilyExtractResult>();
|
|
147
|
-
const failedMap = new Map<string, t.TavilyExtractResult>();
|
|
148
|
-
|
|
149
|
-
for (const result of data.results ?? []) {
|
|
150
|
-
setUrlResult(successMap, result);
|
|
151
|
-
}
|
|
152
|
-
for (const result of data.failed_results ?? []) {
|
|
153
|
-
setUrlResult(failedMap, result);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return urls.map((url): [string, t.TavilyScrapeResponse] => {
|
|
157
|
-
const success =
|
|
158
|
-
successMap.get(url) ?? successMap.get(normalizeUrlKey(url));
|
|
159
|
-
if (success && success.error == null) {
|
|
160
|
-
return [
|
|
161
|
-
url,
|
|
162
|
-
{
|
|
163
|
-
success: true,
|
|
164
|
-
data: {
|
|
165
|
-
rawContent: success.raw_content ?? '',
|
|
166
|
-
images: success.images ?? [],
|
|
167
|
-
favicon: success.favicon,
|
|
168
|
-
},
|
|
169
|
-
},
|
|
170
|
-
];
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const failed =
|
|
174
|
-
failedMap.get(url) ?? failedMap.get(normalizeUrlKey(url));
|
|
175
|
-
const error =
|
|
176
|
-
success?.error ??
|
|
177
|
-
failed?.error ??
|
|
178
|
-
'URL not found in Tavily Extract response';
|
|
179
|
-
return [url, { success: false, error }];
|
|
180
|
-
});
|
|
181
|
-
} catch (error) {
|
|
182
|
-
const errorMessage =
|
|
183
|
-
error instanceof Error ? error.message : String(error);
|
|
184
|
-
return urls.map((url) => [
|
|
185
|
-
url,
|
|
186
|
-
{
|
|
187
|
-
success: false,
|
|
188
|
-
error: `Tavily Extract API request failed: ${errorMessage}`,
|
|
189
|
-
},
|
|
190
|
-
]);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
extractContent(
|
|
195
|
-
response: t.TavilyScrapeResponse
|
|
196
|
-
): [string, undefined | t.References] {
|
|
197
|
-
if (!response.success || !response.data) {
|
|
198
|
-
return ['', undefined];
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const content = response.data.rawContent ?? '';
|
|
202
|
-
const images = response.data.images ?? [];
|
|
203
|
-
|
|
204
|
-
const references: t.References | undefined =
|
|
205
|
-
images.length > 0
|
|
206
|
-
? {
|
|
207
|
-
links: [],
|
|
208
|
-
images: images.map((imageUrl) => ({ originalUrl: imageUrl })),
|
|
209
|
-
videos: [],
|
|
210
|
-
}
|
|
211
|
-
: undefined;
|
|
212
|
-
|
|
213
|
-
return [content, references];
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
extractMetadata(response: t.TavilyScrapeResponse): t.GenericScrapeMetadata {
|
|
217
|
-
if (!response.success || !response.data) {
|
|
218
|
-
return {};
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const metadata: t.GenericScrapeMetadata = {
|
|
222
|
-
images_count: response.data.images?.length ?? 0,
|
|
223
|
-
};
|
|
224
|
-
if (response.data.favicon != null) {
|
|
225
|
-
metadata.favicon = response.data.favicon;
|
|
226
|
-
}
|
|
227
|
-
return metadata;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export const createTavilyScraper = (
|
|
232
|
-
config: t.TavilyScraperConfig = {}
|
|
233
|
-
): TavilyScraper => {
|
|
234
|
-
return new TavilyScraper(config);
|
|
235
|
-
};
|
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
import type * as t from './types';
|
|
3
|
-
|
|
4
|
-
const DEFAULT_TAVILY_TIMEOUT = 15000;
|
|
5
|
-
|
|
6
|
-
const TAVILY_COUNTRY_ALIASES: Record<string, string> = {
|
|
7
|
-
ba: 'bosnia and herzegovina',
|
|
8
|
-
cg: 'congo',
|
|
9
|
-
cz: 'czech republic',
|
|
10
|
-
gb: 'united kingdom',
|
|
11
|
-
mm: 'myanmar',
|
|
12
|
-
tr: 'turkey',
|
|
13
|
-
tt: 'trinidad and tobago',
|
|
14
|
-
uk: 'united kingdom',
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const TAVILY_SUPPORTED_COUNTRIES = new Set([
|
|
18
|
-
'afghanistan',
|
|
19
|
-
'albania',
|
|
20
|
-
'algeria',
|
|
21
|
-
'andorra',
|
|
22
|
-
'angola',
|
|
23
|
-
'argentina',
|
|
24
|
-
'armenia',
|
|
25
|
-
'australia',
|
|
26
|
-
'austria',
|
|
27
|
-
'azerbaijan',
|
|
28
|
-
'bahamas',
|
|
29
|
-
'bahrain',
|
|
30
|
-
'bangladesh',
|
|
31
|
-
'barbados',
|
|
32
|
-
'belarus',
|
|
33
|
-
'belgium',
|
|
34
|
-
'belize',
|
|
35
|
-
'benin',
|
|
36
|
-
'bhutan',
|
|
37
|
-
'bolivia',
|
|
38
|
-
'bosnia and herzegovina',
|
|
39
|
-
'botswana',
|
|
40
|
-
'brazil',
|
|
41
|
-
'brunei',
|
|
42
|
-
'bulgaria',
|
|
43
|
-
'burkina faso',
|
|
44
|
-
'burundi',
|
|
45
|
-
'cambodia',
|
|
46
|
-
'cameroon',
|
|
47
|
-
'canada',
|
|
48
|
-
'cape verde',
|
|
49
|
-
'central african republic',
|
|
50
|
-
'chad',
|
|
51
|
-
'chile',
|
|
52
|
-
'china',
|
|
53
|
-
'colombia',
|
|
54
|
-
'comoros',
|
|
55
|
-
'congo',
|
|
56
|
-
'costa rica',
|
|
57
|
-
'croatia',
|
|
58
|
-
'cuba',
|
|
59
|
-
'cyprus',
|
|
60
|
-
'czech republic',
|
|
61
|
-
'denmark',
|
|
62
|
-
'djibouti',
|
|
63
|
-
'dominican republic',
|
|
64
|
-
'ecuador',
|
|
65
|
-
'egypt',
|
|
66
|
-
'el salvador',
|
|
67
|
-
'equatorial guinea',
|
|
68
|
-
'eritrea',
|
|
69
|
-
'estonia',
|
|
70
|
-
'ethiopia',
|
|
71
|
-
'fiji',
|
|
72
|
-
'finland',
|
|
73
|
-
'france',
|
|
74
|
-
'gabon',
|
|
75
|
-
'gambia',
|
|
76
|
-
'georgia',
|
|
77
|
-
'germany',
|
|
78
|
-
'ghana',
|
|
79
|
-
'greece',
|
|
80
|
-
'guatemala',
|
|
81
|
-
'guinea',
|
|
82
|
-
'haiti',
|
|
83
|
-
'honduras',
|
|
84
|
-
'hungary',
|
|
85
|
-
'iceland',
|
|
86
|
-
'india',
|
|
87
|
-
'indonesia',
|
|
88
|
-
'iran',
|
|
89
|
-
'iraq',
|
|
90
|
-
'ireland',
|
|
91
|
-
'israel',
|
|
92
|
-
'italy',
|
|
93
|
-
'jamaica',
|
|
94
|
-
'japan',
|
|
95
|
-
'jordan',
|
|
96
|
-
'kazakhstan',
|
|
97
|
-
'kenya',
|
|
98
|
-
'kuwait',
|
|
99
|
-
'kyrgyzstan',
|
|
100
|
-
'latvia',
|
|
101
|
-
'lebanon',
|
|
102
|
-
'lesotho',
|
|
103
|
-
'liberia',
|
|
104
|
-
'libya',
|
|
105
|
-
'liechtenstein',
|
|
106
|
-
'lithuania',
|
|
107
|
-
'luxembourg',
|
|
108
|
-
'madagascar',
|
|
109
|
-
'malawi',
|
|
110
|
-
'malaysia',
|
|
111
|
-
'maldives',
|
|
112
|
-
'mali',
|
|
113
|
-
'malta',
|
|
114
|
-
'mauritania',
|
|
115
|
-
'mauritius',
|
|
116
|
-
'mexico',
|
|
117
|
-
'moldova',
|
|
118
|
-
'monaco',
|
|
119
|
-
'mongolia',
|
|
120
|
-
'montenegro',
|
|
121
|
-
'morocco',
|
|
122
|
-
'mozambique',
|
|
123
|
-
'myanmar',
|
|
124
|
-
'namibia',
|
|
125
|
-
'nepal',
|
|
126
|
-
'netherlands',
|
|
127
|
-
'new zealand',
|
|
128
|
-
'nicaragua',
|
|
129
|
-
'niger',
|
|
130
|
-
'nigeria',
|
|
131
|
-
'north korea',
|
|
132
|
-
'north macedonia',
|
|
133
|
-
'norway',
|
|
134
|
-
'oman',
|
|
135
|
-
'pakistan',
|
|
136
|
-
'panama',
|
|
137
|
-
'papua new guinea',
|
|
138
|
-
'paraguay',
|
|
139
|
-
'peru',
|
|
140
|
-
'philippines',
|
|
141
|
-
'poland',
|
|
142
|
-
'portugal',
|
|
143
|
-
'qatar',
|
|
144
|
-
'romania',
|
|
145
|
-
'russia',
|
|
146
|
-
'rwanda',
|
|
147
|
-
'saudi arabia',
|
|
148
|
-
'senegal',
|
|
149
|
-
'serbia',
|
|
150
|
-
'singapore',
|
|
151
|
-
'slovakia',
|
|
152
|
-
'slovenia',
|
|
153
|
-
'somalia',
|
|
154
|
-
'south africa',
|
|
155
|
-
'south korea',
|
|
156
|
-
'south sudan',
|
|
157
|
-
'spain',
|
|
158
|
-
'sri lanka',
|
|
159
|
-
'sudan',
|
|
160
|
-
'sweden',
|
|
161
|
-
'switzerland',
|
|
162
|
-
'syria',
|
|
163
|
-
'taiwan',
|
|
164
|
-
'tajikistan',
|
|
165
|
-
'tanzania',
|
|
166
|
-
'thailand',
|
|
167
|
-
'togo',
|
|
168
|
-
'trinidad and tobago',
|
|
169
|
-
'tunisia',
|
|
170
|
-
'turkey',
|
|
171
|
-
'turkmenistan',
|
|
172
|
-
'uganda',
|
|
173
|
-
'ukraine',
|
|
174
|
-
'united arab emirates',
|
|
175
|
-
'united kingdom',
|
|
176
|
-
'united states',
|
|
177
|
-
'uruguay',
|
|
178
|
-
'uzbekistan',
|
|
179
|
-
'venezuela',
|
|
180
|
-
'vietnam',
|
|
181
|
-
'yemen',
|
|
182
|
-
'zambia',
|
|
183
|
-
'zimbabwe',
|
|
184
|
-
]);
|
|
185
|
-
|
|
186
|
-
const TAVILY_REGION_NAMES = new Intl.DisplayNames(['en'], {
|
|
187
|
-
type: 'region',
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
const normalizeTavilyCountryName = (country: string): string =>
|
|
191
|
-
country
|
|
192
|
-
.toLowerCase()
|
|
193
|
-
.replace(/\s*\([^)]*\)/g, '')
|
|
194
|
-
.replace(/\s*&\s*/g, ' and ')
|
|
195
|
-
.normalize('NFD')
|
|
196
|
-
.replace(/\p{Diacritic}/gu, '');
|
|
197
|
-
|
|
198
|
-
const normalizeTavilyCountry = (country?: string): string | undefined => {
|
|
199
|
-
const normalizedCountry = country?.trim().toLowerCase();
|
|
200
|
-
if (normalizedCountry == null || normalizedCountry === '') {
|
|
201
|
-
return undefined;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const countryAlias = TAVILY_COUNTRY_ALIASES[normalizedCountry];
|
|
205
|
-
if (countryAlias != null) {
|
|
206
|
-
return TAVILY_SUPPORTED_COUNTRIES.has(countryAlias)
|
|
207
|
-
? countryAlias
|
|
208
|
-
: undefined;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (/^[a-z]{2}$/.test(normalizedCountry)) {
|
|
212
|
-
const regionName = TAVILY_REGION_NAMES.of(normalizedCountry.toUpperCase());
|
|
213
|
-
if (regionName == null) {
|
|
214
|
-
return undefined;
|
|
215
|
-
}
|
|
216
|
-
const countryName = normalizeTavilyCountryName(regionName);
|
|
217
|
-
return TAVILY_SUPPORTED_COUNTRIES.has(countryName)
|
|
218
|
-
? countryName
|
|
219
|
-
: undefined;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const countryName = normalizeTavilyCountryName(normalizedCountry);
|
|
223
|
-
return TAVILY_SUPPORTED_COUNTRIES.has(countryName) ? countryName : undefined;
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
const normalizeTavilyTimeRange = (
|
|
227
|
-
timeRange?: t.TavilyTimeRangeInput
|
|
228
|
-
): t.TavilyTimeRange | undefined => {
|
|
229
|
-
switch (timeRange) {
|
|
230
|
-
case 'h':
|
|
231
|
-
case 'd':
|
|
232
|
-
return 'day';
|
|
233
|
-
case 'w':
|
|
234
|
-
return 'week';
|
|
235
|
-
case 'm':
|
|
236
|
-
return 'month';
|
|
237
|
-
case 'y':
|
|
238
|
-
return 'year';
|
|
239
|
-
case 'day':
|
|
240
|
-
case 'week':
|
|
241
|
-
case 'month':
|
|
242
|
-
case 'year':
|
|
243
|
-
return timeRange;
|
|
244
|
-
default:
|
|
245
|
-
return undefined;
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
const getHostname = (link: string): string => {
|
|
250
|
-
try {
|
|
251
|
-
return new URL(link).hostname;
|
|
252
|
-
} catch {
|
|
253
|
-
return link;
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
export const createTavilyAPI = (
|
|
258
|
-
apiKey?: string,
|
|
259
|
-
apiUrl?: string,
|
|
260
|
-
options?: t.TavilySearchOptions
|
|
261
|
-
): {
|
|
262
|
-
getSources: (params: t.GetSourcesParams) => Promise<t.SearchResult>;
|
|
263
|
-
} => {
|
|
264
|
-
const config = {
|
|
265
|
-
apiKey: apiKey ?? process.env.TAVILY_API_KEY,
|
|
266
|
-
apiUrl:
|
|
267
|
-
apiUrl ??
|
|
268
|
-
process.env.TAVILY_SEARCH_URL ??
|
|
269
|
-
'https://api.tavily.com/search',
|
|
270
|
-
timeout: options?.timeout ?? DEFAULT_TAVILY_TIMEOUT,
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
if (config.apiKey == null || config.apiKey === '') {
|
|
274
|
-
throw new Error('TAVILY_API_KEY is required for Tavily API');
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const getSources = async ({
|
|
278
|
-
query,
|
|
279
|
-
date,
|
|
280
|
-
country,
|
|
281
|
-
numResults = 8,
|
|
282
|
-
type,
|
|
283
|
-
news,
|
|
284
|
-
}: t.GetSourcesParams): Promise<t.SearchResult> => {
|
|
285
|
-
if (!query.trim()) {
|
|
286
|
-
return { success: false, error: 'Query cannot be empty' };
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
try {
|
|
290
|
-
const timeRange =
|
|
291
|
-
normalizeTavilyTimeRange(options?.timeRange) ??
|
|
292
|
-
(date != null ? (normalizeTavilyTimeRange(date) ?? 'day') : undefined);
|
|
293
|
-
const topic =
|
|
294
|
-
news === true || type === 'news'
|
|
295
|
-
? 'news'
|
|
296
|
-
: (options?.topic ?? 'general');
|
|
297
|
-
const maxResults = options?.maxResults ?? numResults;
|
|
298
|
-
const searchDepth = options?.searchDepth ?? 'basic';
|
|
299
|
-
|
|
300
|
-
const payload: t.TavilySearchPayload = {
|
|
301
|
-
query,
|
|
302
|
-
search_depth: searchDepth,
|
|
303
|
-
topic,
|
|
304
|
-
max_results: Math.min(Math.max(1, maxResults), 20),
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
if (
|
|
308
|
-
options?.safeSearch != null &&
|
|
309
|
-
searchDepth !== 'fast' &&
|
|
310
|
-
searchDepth !== 'ultra-fast'
|
|
311
|
-
) {
|
|
312
|
-
payload.safe_search = options.safeSearch;
|
|
313
|
-
}
|
|
314
|
-
if (timeRange != null) {
|
|
315
|
-
payload.time_range = timeRange;
|
|
316
|
-
}
|
|
317
|
-
const tavilyCountry =
|
|
318
|
-
topic === 'general' ? normalizeTavilyCountry(country) : undefined;
|
|
319
|
-
if (tavilyCountry != null) {
|
|
320
|
-
payload.country = tavilyCountry;
|
|
321
|
-
}
|
|
322
|
-
if (type === 'images' || options?.includeImages) {
|
|
323
|
-
payload.include_images = true;
|
|
324
|
-
}
|
|
325
|
-
if (options?.includeAnswer != null) {
|
|
326
|
-
payload.include_answer = options.includeAnswer;
|
|
327
|
-
}
|
|
328
|
-
if (options?.includeRawContent != null) {
|
|
329
|
-
payload.include_raw_content = options.includeRawContent;
|
|
330
|
-
}
|
|
331
|
-
if (
|
|
332
|
-
options?.includeDomains != null &&
|
|
333
|
-
options.includeDomains.length > 0
|
|
334
|
-
) {
|
|
335
|
-
payload.include_domains = options.includeDomains;
|
|
336
|
-
}
|
|
337
|
-
if (
|
|
338
|
-
options?.excludeDomains != null &&
|
|
339
|
-
options.excludeDomains.length > 0
|
|
340
|
-
) {
|
|
341
|
-
payload.exclude_domains = options.excludeDomains;
|
|
342
|
-
}
|
|
343
|
-
if (options?.includeImageDescriptions != null) {
|
|
344
|
-
payload.include_image_descriptions = options.includeImageDescriptions;
|
|
345
|
-
}
|
|
346
|
-
if (options?.includeFavicon != null) {
|
|
347
|
-
payload.include_favicon = options.includeFavicon;
|
|
348
|
-
}
|
|
349
|
-
if (options?.chunksPerSource != null && searchDepth === 'advanced') {
|
|
350
|
-
payload.chunks_per_source = options.chunksPerSource;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
const response = await axios.post<t.TavilySearchResponse>(
|
|
354
|
-
config.apiUrl,
|
|
355
|
-
payload,
|
|
356
|
-
{
|
|
357
|
-
headers: {
|
|
358
|
-
Authorization: `Bearer ${config.apiKey}`,
|
|
359
|
-
'Content-Type': 'application/json',
|
|
360
|
-
},
|
|
361
|
-
timeout: config.timeout,
|
|
362
|
-
}
|
|
363
|
-
);
|
|
364
|
-
|
|
365
|
-
const data = response.data;
|
|
366
|
-
|
|
367
|
-
const organicResults: t.OrganicResult[] = (data.results ?? []).map(
|
|
368
|
-
(result: t.TavilySearchResult) => ({
|
|
369
|
-
title: result.title ?? '',
|
|
370
|
-
link: result.url ?? '',
|
|
371
|
-
snippet: result.content ?? '',
|
|
372
|
-
date: result.published_date,
|
|
373
|
-
})
|
|
374
|
-
);
|
|
375
|
-
|
|
376
|
-
const imageResults: t.ImageResult[] = Array.isArray(data.images)
|
|
377
|
-
? data.images.slice(0, 6).reduce<t.ImageResult[]>((acc, image) => {
|
|
378
|
-
const imageUrl = typeof image === 'string' ? image : image.url;
|
|
379
|
-
if (imageUrl == null || imageUrl === '') {
|
|
380
|
-
return acc;
|
|
381
|
-
}
|
|
382
|
-
acc.push({
|
|
383
|
-
imageUrl,
|
|
384
|
-
title: typeof image === 'string' ? undefined : image.description,
|
|
385
|
-
position: acc.length + 1,
|
|
386
|
-
});
|
|
387
|
-
return acc;
|
|
388
|
-
}, [])
|
|
389
|
-
: [];
|
|
390
|
-
|
|
391
|
-
const newsResults: t.NewsResult[] =
|
|
392
|
-
topic === 'news'
|
|
393
|
-
? organicResults.map((r) => ({
|
|
394
|
-
title: r.title,
|
|
395
|
-
link: r.link,
|
|
396
|
-
snippet: r.snippet,
|
|
397
|
-
date: r.date,
|
|
398
|
-
source: getHostname(r.link),
|
|
399
|
-
}))
|
|
400
|
-
: [];
|
|
401
|
-
|
|
402
|
-
const results: t.SearchResultData = {
|
|
403
|
-
organic: organicResults,
|
|
404
|
-
images: imageResults,
|
|
405
|
-
topStories: [],
|
|
406
|
-
videos: [],
|
|
407
|
-
news: newsResults,
|
|
408
|
-
answerBox: data.answer != null ? { snippet: data.answer } : undefined,
|
|
409
|
-
relatedSearches: [],
|
|
410
|
-
};
|
|
411
|
-
|
|
412
|
-
return { success: true, data: results };
|
|
413
|
-
} catch (error) {
|
|
414
|
-
const errorMessage =
|
|
415
|
-
error instanceof Error ? error.message : String(error);
|
|
416
|
-
return {
|
|
417
|
-
success: false,
|
|
418
|
-
error: `Tavily API request failed: ${errorMessage}`,
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
return { getSources };
|
|
424
|
-
};
|