@sisu-ai/tool-wikipedia 1.0.0
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 +41 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +72 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @sisu-ai/tool-wikipedia
|
|
2
|
+
|
|
3
|
+
Wikipedia lookup tool using the REST API. Fetch a page summary, HTML, or related pages given an approximate title. The REST API performs redirects and normalization, so near-matches often resolve.
|
|
4
|
+
|
|
5
|
+
Install
|
|
6
|
+
```bash
|
|
7
|
+
npm i @sisu-ai/tool-wikipedia
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Environment / Flags
|
|
11
|
+
- Language: `WIKIPEDIA_LANG` or `WIKI_LANG` (e.g., `en`, `sv`). CLI flags follow kebab-case, e.g., `--wikipedia-lang=sv`.
|
|
12
|
+
- Base URL override: `WIKIPEDIA_BASE_URL` or `WIKI_BASE_URL` (e.g., `https://en.wikipedia.org/api/rest_v1`). CLI flag `--wikipedia-base-url=...`.
|
|
13
|
+
- Defaults to `https://en.wikipedia.org/api/rest_v1`.
|
|
14
|
+
|
|
15
|
+
Usage
|
|
16
|
+
```ts
|
|
17
|
+
import { Agent } from '@sisu-ai/core';
|
|
18
|
+
import { registerTools } from '@sisu-ai/mw-register-tools';
|
|
19
|
+
import { toolCalling } from '@sisu-ai/mw-tool-calling';
|
|
20
|
+
import { wikipedia } from '@sisu-ai/tool-wikipedia';
|
|
21
|
+
|
|
22
|
+
const app = new Agent()
|
|
23
|
+
.use(registerTools([wikipedia]))
|
|
24
|
+
.use(toolCalling);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Tool
|
|
28
|
+
- Name: `wikipediaLookup`
|
|
29
|
+
- Args:
|
|
30
|
+
- `title: string` — approximate page title
|
|
31
|
+
- `format?: 'summary'|'html'|'related'` — default `summary`
|
|
32
|
+
- `lang?: string` — language code; otherwise from env/flags
|
|
33
|
+
|
|
34
|
+
Returns
|
|
35
|
+
- `summary`: `{ type?, title, description?, extract?, url?, thumbnailUrl? }`
|
|
36
|
+
- `html`: `string` HTML
|
|
37
|
+
- `related`: `Array<{ title, description?, extract?, url?, thumbnailUrl? }>`
|
|
38
|
+
|
|
39
|
+
Notes
|
|
40
|
+
- For search-like behavior, consider calling `related` first and then fetching the best candidate's `summary`.
|
|
41
|
+
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Tool } from '@sisu-ai/core';
|
|
2
|
+
export type WikipediaFormat = 'summary' | 'html' | 'related';
|
|
3
|
+
export interface WikipediaArgs {
|
|
4
|
+
title: string;
|
|
5
|
+
format?: WikipediaFormat;
|
|
6
|
+
lang?: string;
|
|
7
|
+
}
|
|
8
|
+
export type WikipediaSummaryResult = {
|
|
9
|
+
type?: string;
|
|
10
|
+
title: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
extract?: string;
|
|
13
|
+
url?: string;
|
|
14
|
+
thumbnailUrl?: string;
|
|
15
|
+
};
|
|
16
|
+
export type WikipediaRelatedResult = Array<{
|
|
17
|
+
title: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
extract?: string;
|
|
20
|
+
url?: string;
|
|
21
|
+
thumbnailUrl?: string;
|
|
22
|
+
}>;
|
|
23
|
+
export declare const wikipedia: Tool<WikipediaArgs>;
|
|
24
|
+
export default wikipedia;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { firstConfigValue } from '@sisu-ai/core';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export const wikipedia = {
|
|
4
|
+
name: 'wikipediaLookup',
|
|
5
|
+
description: 'Fetch a Wikipedia page summary, HTML, or related pages given an approximate title. Defaults to summary.',
|
|
6
|
+
schema: z.object({
|
|
7
|
+
title: z.string().min(1),
|
|
8
|
+
format: z.enum(['summary', 'html', 'related']).optional(),
|
|
9
|
+
lang: z.string().min(2).max(10).optional(),
|
|
10
|
+
}),
|
|
11
|
+
handler: async ({ title, format = 'summary', lang }, ctx) => {
|
|
12
|
+
const base = resolveBaseUrl(ctx, lang);
|
|
13
|
+
if (format === 'html')
|
|
14
|
+
return fetchHtml(base, title);
|
|
15
|
+
if (format === 'related')
|
|
16
|
+
return fetchRelated(base, title);
|
|
17
|
+
return fetchSummary(base, title);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
export default wikipedia;
|
|
21
|
+
function resolveBaseUrl(ctx, lang) {
|
|
22
|
+
// Precedence: CLI flags (via core helpers) > env vars; allow overriding full base or just language
|
|
23
|
+
const baseFromFlags = firstConfigValue(['WIKIPEDIA_BASE_URL', 'WIKI_BASE_URL']);
|
|
24
|
+
const langFromFlags = firstConfigValue(['WIKIPEDIA_LANG', 'WIKI_LANG']) || lang;
|
|
25
|
+
if (baseFromFlags)
|
|
26
|
+
return baseFromFlags.replace(/\/$/, '');
|
|
27
|
+
const chosenLang = (langFromFlags || 'en').toLowerCase();
|
|
28
|
+
return `https://${chosenLang}.wikipedia.org/api/rest_v1`;
|
|
29
|
+
}
|
|
30
|
+
async function fetchSummary(baseUrl, title) {
|
|
31
|
+
const url = `${baseUrl}/page/summary/${encodeURIComponent(title)}`;
|
|
32
|
+
const res = await fetch(url, { headers: { Accept: 'application/json' } });
|
|
33
|
+
const raw = await res.text();
|
|
34
|
+
if (!res.ok)
|
|
35
|
+
throw new Error(`Wikipedia summary failed: ${res.status} ${res.statusText}`);
|
|
36
|
+
const json = raw ? JSON.parse(raw) : {};
|
|
37
|
+
return {
|
|
38
|
+
type: json.type,
|
|
39
|
+
title: String(json.displaytitle || json.title || title),
|
|
40
|
+
description: json.description,
|
|
41
|
+
extract: json.extract,
|
|
42
|
+
url: json.content_urls?.desktop?.page || json.content_urls?.mobile?.page,
|
|
43
|
+
thumbnailUrl: json.thumbnail?.source,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
async function fetchHtml(baseUrl, title) {
|
|
47
|
+
const url = `${baseUrl}/page/html/${encodeURIComponent(title)}`;
|
|
48
|
+
const res = await fetch(url, { headers: { Accept: 'text/html' } });
|
|
49
|
+
const raw = await res.text();
|
|
50
|
+
const ct = res.headers?.get?.('content-type') || '';
|
|
51
|
+
if (!res.ok)
|
|
52
|
+
throw new Error(`Wikipedia html failed: ${res.status} ${res.statusText}`);
|
|
53
|
+
if (!ct.toLowerCase().includes('text/html'))
|
|
54
|
+
throw new Error(`Wikipedia html returned non-HTML content (content-type: ${ct})`);
|
|
55
|
+
return raw;
|
|
56
|
+
}
|
|
57
|
+
async function fetchRelated(baseUrl, title) {
|
|
58
|
+
const url = `${baseUrl}/page/related/${encodeURIComponent(title)}`;
|
|
59
|
+
const res = await fetch(url, { headers: { Accept: 'application/json' } });
|
|
60
|
+
const raw = await res.text();
|
|
61
|
+
if (!res.ok)
|
|
62
|
+
throw new Error(`Wikipedia related failed: ${res.status} ${res.statusText}`);
|
|
63
|
+
const json = raw ? JSON.parse(raw) : {};
|
|
64
|
+
const pages = Array.isArray(json.pages) ? json.pages : [];
|
|
65
|
+
return pages.map(p => ({
|
|
66
|
+
title: String(p.title || ''),
|
|
67
|
+
description: p.description,
|
|
68
|
+
extract: p.extract,
|
|
69
|
+
url: p.content_urls?.desktop?.page,
|
|
70
|
+
thumbnailUrl: p.thumbnail?.source,
|
|
71
|
+
})).filter(r => r.title);
|
|
72
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sisu-ai/tool-wikipedia",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc -b"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"zod": "^3.23.8"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@sisu-ai/core": "0.3.0"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/finger-gun/sisu",
|
|
22
|
+
"directory": "packages/tools/wikipedia"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/finger-gun/sisu#readme",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/finger-gun/sisu/issues"
|
|
27
|
+
}
|
|
28
|
+
}
|