Skill: 343195246/mdgs-tavily-search-skill (version 1.0.0)

--- name: mdgs-tavily-search-skill description: 使用 Tavily API 进行网络搜索、网页内容提取、抓取、映射和研究。当用户需要搜索信息、获取网页内容、从网站抓取数据、绘制网站地图或进行深度研究时使用此技能。AI 应根据任务自动选择最合适的模式。 --- # Mdgs Tavily Search Skill 使用 Tavily API 进行网络搜索和信息提取。 ## 快速开始 ### 前置要求 首先安装依赖： ```bash npm install @tavily/core ``` 设置 API Key： ```bash export TAVILY_API_KEY="tvly-your-api-key" ``` ### 初始化客户端 ```javascript const { tavily } = require("@tavily/core"); const [REDACTED_SECRET]; if (!apiKey) { throw new Error("请配置 TAVILY_API_KEY 环境变量。访问 https://tavily.com 获取 API Key"); } const tvly = tavily({ apiKey }); ``` ## 模式选择指南 AI 应根据任务类型自动选择合适的模式： | 任务类型 | 推荐模式 | 说明 | |---------|---------|------| | 快速问答、信息检索 | search | 获取搜索结果和答案 | | 获取特定网页内容 | extract | 提取单个 URL 的主要内容 | | 批量抓取网站内容 | crawl | 抓取整个网站或多个页面 | | 了解网站结构 | map | 获取网站的页面地图 | | 深度研究主题 | research | 综合多个来源的深度研究 | ## 模式详解 ### 1. 搜索网页 (search) 适用于：快速问答、信息检索、新闻搜索 ```javascript const response = await tvly.search("Who is Leo Messi?"); console.log(response); ``` **选项：** ```javascript const response = await tvly.search("Python 教程", { searchDepth: "basic", // "basic" 或 "advanced" maxResults: 10, includeAnswer: true, includeRawContent: false, includeImages: false }); ``` ### 2. 提取网页 (extract) 适用于：获取特定网页的详细内容 ```javascript const response = await tvly.extract("https://en.wikipedia.org/wiki/Artificial_intelligence"); console.log(response); ``` **选项：** ```javascript const response = await tvly.extract("https://example.com", { includeImages: true }); ``` ### 3. 抓取网页 (crawl) 适用于：批量抓取网站内容、深度内容获取 ```javascript const response = await tvly.crawl("https://docs.tavily.com", { instructions: "Find all pages on the Python SDK" }); console.log(response); ``` **选项：** ```javascript const response = await tvly.crawl("https://example.com", { instructions: "提取所有产品页面", maxDepth: 2, maxPages: 10 }); ``` ### 4. 绘制网页映射 (map) 适用于：了解网站结构、发现相关页面 ```javascript const response = await tvly.map("https://docs.tavily.com"); console.log(response); ``` **选项：** ```javascript const response = await tvly.map("https://example.com", { depth: 2, maxPages: 20 }); ``` ### 5. 创建研究任务 (research) 适用于：深度研究、综合多来源分析 ```javascript const response = await tvly.research("What are the latest developments in AI?"); console.log(response); ``` **选项：** ```javascript const response = await tvly.research("最新 AI 发展动态", { depth: "extensive", // "basic" 或 "extensive" maxSources: 10 }); ``` ## 使用脚本 项目提供了封装好的脚本： ### 搜索 ```bash node scripts/tavily.js search "搜索内容" [--depth basic|advanced] [--max-results N] ``` ### 提取 ```bash node scripts/tavily.js extract "https://example.com" ``` ### 抓取 ```bash node scripts/tavily.js crawl "https://example.com" --instructions "提取所有页面" ``` ### 映射 ```bash node scripts/tavily.js map "https://example.com" [--depth N] ``` ### 研究 ```bash node scripts/tavily.js research "研究主题" [--depth basic|extensive] ``` ## API Key 配置 **重要：** 使用此技能前必须配置 API Key。 1. 访问 [tavily.com](https://tavily.com) 注册账号 2. 获取 API Key 3. 设置环境变量： ```bash export TAVILY_API_KEY="tvly-your-actual-api-key" ``` 或在脚本/代码中直接传入： ```javascript const tvly = tavily({ [REDACTED_SECRET]" }); ``` ## 响应格式 ### search response ```json { "answer": "回答文本", "results": [{ "title": "", "url": "", "content": "", "score": 0.95 }], "images": [] } ``` ### extract response ```json { "results": [{ "url": "", "content": "", "raw_content": "" }] } ``` ### crawl response ```json { "results": [{ "url": "", "content": "" }] } ``` ### map response ```json { "results": [{ "url": "", "title": "" }] } ``` ### research response ```json { "answer": "综合研究报告", "findings": [{ "content": "", "sources": [] }] } ```

path: package.json
content: {
  "name": "mdgs-tavily-search-skill",
  "version": "1.0.0",
  "description": "使用 Tavily API 进行网络搜索、网页提取、抓取、映射和研究",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": ["tavily", "search", "ai", "web-scraping"],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@tavily/core": "^0.7.2"
  }
}


path: scripts/tavily.js
content: const { tavily } = require("@tavily/core");

// API Key 检查
const [REDACTED_SECRET];

if (!apiKey) {
  console.error("❌ 错误: 请配置 TAVILY_API_KEY 环境变量");
  console.error("   访问 https://tavily.com 获取 API Key");
  console.error("   设置方式: export TAVILY_API_KEY=\"tvly-your-api-key\"");
  process.exit(1);
}

const args = process.argv.slice(2);

if (args.length < 1) {
  console.log(`
Tavily 搜索工具

用法: node tavily.js <命令> [参数]

命令:
  search <查询>           搜索网页
  extract <URL>           提取网页内容
  crawl <URL>             抓取网页
  map <URL>               绘制网页映射
  research <主题>         创建研究任务

选项:
  --depth <basic|advanced>   搜索深度 (默认: basic)
  --max-results <N>           最大结果数 (默认: 10)
  --instructions <文本>      抓取说明
  --max-pages <N>            最大页面数

示例:
  node tavily.js search "AI 新闻" --depth advanced --max-results 5
  node tavily.js extract "https://example.com"
  node tavily.js crawl "https://docs.example.com" --instructions "提取所有文档"
  node tavily.js map "https://example.com"
  node tavily.js research "Python 最新版本"
`);
  process.exit(1);
}

const command = args[0];
const queryOrUrl = args[1];

// 解析选项
const options = {};
for (let i = 2; i < args.length; i++) {
  if (args[i] === "--depth" && args[i + 1]) {
    options.searchDepth = args[i + 1];
    i++;
  } else if (args[i] === "--max-results" && args[i + 1]) {
    options.maxResults = parseInt(args[i + 1]);
    i++;
  } else if (args[i] === "--instructions" && args[i + 1]) {
    options.instructions = args[i + 1];
    i++;
  } else if (args[i] === "--max-pages" && args[i + 1]) {
    options.maxPages = parseInt(args[i + 1]);
    i++;
  } else if (args[i] === "--max-sources" && args[i + 1]) {
    options.maxSources = parseInt(args[i + 1]);
    i++;
  }
}

const tvly = tavily({ apiKey });

async function run() {
  try {
    switch (command) {
      case "search":
        if (!queryOrUrl) {
          console.error("❌ 错误: 请提供搜索查询");
          process.exit(1);
        }
        console.log(`🔍 搜索: ${queryOrUrl}\n`);
        const searchOptions = {
          searchDepth: options.searchDepth || "basic",
          maxResults: options.maxResults || 10,
          includeAnswer: true,
          includeRawContent: false,
          includeImages: false
        };
        const searchResp = await tvly.search(queryOrUrl, searchOptions);
        console.log("=== 搜索结果 ===\n");
        if (searchResp.answer) {
          console.log("📝 回答:", searchResp.answer, "\n");
        }
        searchResp.results?.forEach((r, i) => {
          console.log(`${i + 1}. ${r.title}`);
          console.log(`   ${r.url}`);
          console.log(`   ${r.content?.substring(0, 150)}...\n`);
        });
        break;

      case "extract":
        if (!queryOrUrl) {
          console.error("❌ 错误: 请提供 URL");
          process.exit(1);
        }
        console.log(`📄 提取: ${queryOrUrl}\n`);
        const extractResp = await tvly.extract(queryOrUrl);
        console.log("=== 提取结果 ===\n");
        extractResp.results?.forEach(r => {
          console.log(r.content?.substring(0, 2000));
        });
        break;

      case "crawl":
        if (!queryOrUrl) {
          console.error("❌ 错误: 请提供 URL");
          process.exit(1);
        }
        console.log(`🕷️ 抓取: ${queryOrUrl}\n`);
        if (!options.instructions) {
          console.warn("⚠️ 建议添加 --instructions 说明抓取目标");
        }
        const crawlResp = await tvly.crawl(queryOrUrl, options);
        console.log("=== 抓取结果 ===\n");
        crawlResp.results?.forEach((r, i) => {
          console.log(`${i + 1}. ${r.url}`);
          console.log(`   ${r.content?.substring(0, 200)}...\n`);
        });
        break;

      case "map":
        if (!queryOrUrl) {
          console.error("❌ 错误: 请提供 URL");
          process.exit(1);
        }
        console.log(`🗺️ 映射: ${queryOrUrl}\n`);
        const mapResp = await tvly.map(queryOrUrl, {
          depth: options.depth || 2,
          maxPages: options.maxPages || 20
        });
        console.log("=== 映射结果 ===\n");
        mapResp.results?.forEach((r, i) => {
          console.log(`${i + 1}. ${r.title} - ${r.url}`);
        });
        break;

      case "research":
        if (!queryOrUrl) {
          console.error("❌ 错误: 请提供研究主题");
          process.exit(1);
        }
        console.log(`📚 研究: ${queryOrUrl}\n`);
        const researchResp = await tvly.research(queryOrUrl, {
          depth: options.depth || "extensive",
          maxSources: options.maxSources || 10
        });
        console.log("=== 研究报告 ===\n");
        if (researchResp.answer) {
          console.log("📝 总结:", researchResp.answer, "\n");
        }
        researchResp.findings?.forEach((f, i) => {
          console.log(`\n--- 发现 ${i + 1} ---`);
          console.log(f.content);
          console.log("来源:", f.sources?.join(", "));
        });
        break;

      default:
        console.error(`❌ 未知命令: ${command}`);
        process.exit(1);
    }
  } catch (err) {
    console.error("❌ 错误:", err.message);
    process.exit(1);
  }
}

run();


Security scan verdicts — ClawScan: clean | static analysis: clean | VirusTotal: clean | SkillSpector: suspicious (MEDIUM)