Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

1.3 KB
Raw Blame
import { CliError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';
import { wikiFetch } from './utils.js';
cli({
    site: 'wikipedia',
    name: 'search',
    access: 'read',
    description: 'Search Wikipedia articles',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'query', positional: true, required: true, help: 'Search keyword' },
        { name: 'limit', type: 'int', default: 10, help: 'Max results' },
        { name: 'lang', default: 'en', help: 'Language code (e.g. en, zh, ja)' },
    ],
    columns: ['title', 'snippet', 'url'],
    func: async (args) => {
        const limit = Math.max(1, Math.min(Number(args.limit), 50));
        const lang = args.lang || 'en';
        const q = encodeURIComponent(args.query);
        const data = (await wikiFetch(lang, `/w/api.php?action=query&list=search&srsearch=${q}&srlimit=${limit}&format=json&utf8=1`));
        const results = data?.query?.search;
        if (!results?.length)
            throw new CliError('NOT_FOUND', 'No articles found', 'Try a different keyword');
        return results.map((r) => ({
            title: r.title,
            snippet: r.snippet.replace(/<[^>]+>/g, '').slice(0, 120),
            url: `https://${lang}.wikipedia.org/wiki/${encodeURIComponent(r.title.replace(/ /g, '_'))}`,
        }));
    },
});