-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from dcSpark/feature/add-wikimedia-search-tool
feat: add wikimedia-search tool
- Loading branch information
Showing
6 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"categoryId": "cc6ba888-3987-4e2a-af7e-3b137d997262" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '**') | ||
})) | ||
}; | ||
}; |