Skip to content

Commit

Permalink
Merge pull request #141 from dcSpark/feature/add-wikimedia-search-tool
Browse files Browse the repository at this point in the history
feat: add wikimedia-search tool
  • Loading branch information
guillevalin authored Feb 4, 2025
2 parents 3d86114 + 8e9bcb5 commit b654ecd
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
Binary file added tools/wikimedia-search/banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tools/wikimedia-search/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions tools/wikimedia-search/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect } from '@jest/globals';
import { getToolTestClient } from '../../src/test/utils';
import * as path from 'path';

describe('Wikimedia Search Tool', () => {
const toolPath = path.join(__dirname, 'tool.ts');
const client = getToolTestClient();

it('searches content with default parameters', async () => {
const response = await client.executeToolFromFile(toolPath, {
query: 'artificial intelligence'
});
console.log("Response: ", response);

expect(response).toHaveProperty('results');
expect(Array.isArray(response.results)).toBe(true);
expect(response.results.length).toBeGreaterThan(0);
expect(response.results.length).toBeLessThanOrEqual(10);

const firstResult = response.results[0];
expect(firstResult).toHaveProperty('title');
expect(firstResult).toHaveProperty('description');
expect(firstResult).toHaveProperty('excerpt');
}, 30000);

it('respects limit parameter', async () => {
const limit = 5;
const response = await client.executeToolFromFile(toolPath, {
query: 'artificial intelligence',
limit
});

expect(response.results.length).toBeLessThanOrEqual(limit);
}, 30000);
});
66 changes: 66 additions & 0 deletions tools/wikimedia-search/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"id": "wikimedia-search",
"version": "1.0.0",
"name": "Wikimedia Search",
"description": "Search across Wikimedia page content with full-text search capabilities",
"author": "Shinkai",
"keywords": [
"wikimedia",
"search",
"wikipedia",
"full-text",
"content"
],
"configurations": {
"type": "object",
"properties": {
"project": {
"type": "string",
"description": "Wikimedia project (e.g., wikipedia)",
"default": "wikipedia"
},
"language": {
"type": "string",
"description": "Language code (e.g., en)",
"default": "en"
}
},
"required": []
},
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Search query"
},
"limit": {
"type": "integer",
"description": "Maximum number of results (1-50)",
"minimum": 1,
"maximum": 50,
"default": 10
}
},
"required": ["query"]
},
"result": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {"type": "string"},
"description": {"type": "string"},
"excerpt": {"type": "string"},
"url": {"type": "string"}
},
"required": ["title", "description", "excerpt", "url"]
}
}
},
"required": ["results"]
}
}
3 changes: 3 additions & 0 deletions tools/wikimedia-search/store.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"categoryId": "cc6ba888-3987-4e2a-af7e-3b137d997262"
}
51 changes: 51 additions & 0 deletions tools/wikimedia-search/tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import axios from 'npm:axios';

type Configurations = {
project?: string;
language?: string;
};

type Parameters = {
query: string;
limit?: number;
};

type Result = {
results: Array<{
title: string;
description: string;
excerpt: string;
}>;
};

export type Run<C extends Record<string, any>, I extends Record<string, any>, R extends Record<string, any>> = (
config: C,
inputs: I
) => Promise<R>;

export const run: Run<Configurations, Parameters, Result> = async (
configurations: Configurations,
params: Parameters
): Promise<Result> => {
const project = configurations?.project || 'wikipedia';
const language = configurations?.language || 'en';
const limit = params.limit || 10;

const api_url = `https://api.wikimedia.org/core/v1/${project}/${language}/search/page`;

const response = await axios.get(api_url, {
params: {
q: params.query,
limit: Math.min(Math.max(1, limit), 50)
}
});

return {
results: response.data.pages.map((page: any) => ({
title: page.title,
description: page.description || '',
excerpt: page.excerpt.replace(/<span class="searchmatch">/g, '**')
.replace(/<\/span>/g, '**')
}))
};
};

0 comments on commit b654ecd

Please sign in to comment.