wikivectors 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +53 -2
- package/package.json +14 -4
- package/src/index.d.ts +80 -0
- package/src/index.js +157 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bionics LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,54 @@
|
|
|
1
|
-
#
|
|
1
|
+
# wikivectors
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Fresh English Wikipedia for AI apps. Ask in plain English and get the right paragraphs, each with its link and revision, updated within the hour. Also short cited answers, whole pages, infobox facts, similar pages and a change feed.
|
|
4
|
+
|
|
5
|
+
Zero dependencies. Works in Node 18+, Deno, Bun, edge runtimes and browsers (it uses `fetch`). Types included.
|
|
6
|
+
|
|
7
|
+
Free key (1,000 searches a month): https://wikivectors.com/account · Docs: https://wikivectors.com/docs
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install wikivectors
|
|
11
|
+
export WIKIVECTORS_KEY=wv_live_...
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Search
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { WikiVectors } from "wikivectors";
|
|
18
|
+
|
|
19
|
+
const wv = new WikiVectors(); // reads WIKIVECTORS_KEY
|
|
20
|
+
for (const p of await wv.search("who won the 2026 FIFA World Cup", { limit: 3 })) {
|
|
21
|
+
console.log(p.title, p.permalink);
|
|
22
|
+
console.log(p.text);
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Everything else
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
await wv.answer("who won the 2026 FIFA World Cup"); // short answer + sources, or answered: false
|
|
30
|
+
await wv.pages(["Ada Lovelace", "Alan Turing"]); // whole pages
|
|
31
|
+
await wv.facts("Ada Lovelace"); // infobox as JSON: born, died, known for...
|
|
32
|
+
await wv.identify("Michael Jordan", { hint: "professor, machine learning" });
|
|
33
|
+
await wv.similar("Ada Lovelace", { limit: 5 }); // pages most like this one
|
|
34
|
+
await wv.titles({ prefix: "2025 in " });
|
|
35
|
+
await wv.changes({ pages: ["Ada Lovelace"] }); // recent edits; pass next_after back as after
|
|
36
|
+
await wv.watch({ page: "Ada Lovelace", callbackUrl: "https://example.com/hook" });
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Let your app or AI get its own key
|
|
40
|
+
|
|
41
|
+
No key yet? Ask for one by email. The person gets a code and an Approve button; the key comes back once they click it.
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { WikiVectors, requestKey } from "wikivectors";
|
|
45
|
+
|
|
46
|
+
const key = await requestKey("ada@example.com", "My App"); // logs the code, waits up to 15 min
|
|
47
|
+
const wv = new WikiVectors({ apiKey: key });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Errors throw `WikiVectorsError` with `.status`, `.code`, `.detail` and `.requestId`.
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
The client is MIT. Wikipedia text is CC BY-SA 4.0: show the `attribution` line, or at least the link, wherever you show the text.
|
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wikivectors",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fresh English Wikipedia for AI apps: search, cited answers, pages, infobox facts, similar pages and a change feed. Zero dependencies.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": { ".": { "types": "./src/index.d.ts", "default": "./src/index.js" } },
|
|
9
|
+
"files": ["src", "README.md", "LICENSE"],
|
|
10
|
+
"engines": { "node": ">=18" },
|
|
11
|
+
"scripts": { "test": "node --test" },
|
|
12
|
+
"keywords": ["wikipedia", "rag", "retrieval", "search", "llm", "grounding", "mcp", "ai"],
|
|
13
|
+
"homepage": "https://wikivectors.com/docs",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "WikiVectors"
|
|
16
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export declare const VERSION: string;
|
|
2
|
+
export declare const DEFAULT_BASE_URL: string;
|
|
3
|
+
|
|
4
|
+
/** The API said no. */
|
|
5
|
+
export declare class WikiVectorsError extends Error {
|
|
6
|
+
status: number;
|
|
7
|
+
code: string;
|
|
8
|
+
/** The API's own message, without status and code. */
|
|
9
|
+
detail: string;
|
|
10
|
+
requestId?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** One Wikipedia paragraph with its link and revision. Text is CC BY-SA 4.0: show attribution or the link. */
|
|
14
|
+
export interface Passage {
|
|
15
|
+
passage_id: number;
|
|
16
|
+
title: string;
|
|
17
|
+
section?: string;
|
|
18
|
+
text: string;
|
|
19
|
+
url: string;
|
|
20
|
+
permalink: string;
|
|
21
|
+
revision_id: number;
|
|
22
|
+
score?: number;
|
|
23
|
+
attribution?: string;
|
|
24
|
+
license?: string;
|
|
25
|
+
[extra: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type Json = Record<string, any>;
|
|
29
|
+
|
|
30
|
+
export interface ClientOptions {
|
|
31
|
+
/** Defaults to the WIKIVECTORS_KEY environment variable. */
|
|
32
|
+
apiKey?: string;
|
|
33
|
+
baseUrl?: string;
|
|
34
|
+
/** Milliseconds per call. Default 30000. */
|
|
35
|
+
timeout?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export declare class WikiVectors {
|
|
39
|
+
constructor(options?: ClientOptions);
|
|
40
|
+
apiKey: string;
|
|
41
|
+
baseUrl: string;
|
|
42
|
+
timeout: number;
|
|
43
|
+
/** Best-matching paragraphs, best first (POST /v1/search). */
|
|
44
|
+
search(query: string, options?: { limit?: number }): Promise<Passage[]>;
|
|
45
|
+
/** A short answer that cites its paragraphs, or answered: false (POST /v1/answer). */
|
|
46
|
+
answer(query: string, options?: { options?: Json }): Promise<Json>;
|
|
47
|
+
/** Pages by title (POST /v1/pages). full: true (1-5 titles) adds sections and every paragraph. */
|
|
48
|
+
pages(titles: string | string[], options?: { full?: boolean }): Promise<Json[]>;
|
|
49
|
+
/** A page's infobox as structured facts (GET /v1/pages/{title}/facts). */
|
|
50
|
+
facts(title: string): Promise<Json>;
|
|
51
|
+
/** Which page a name means (POST /v1/identify). */
|
|
52
|
+
identify(name: string, options?: { hint?: string; kind?: string }): Promise<Json>;
|
|
53
|
+
/** Pages most like a page or a paragraph (POST /v1/similar). */
|
|
54
|
+
similar(title: string, options?: { limit?: number }): Promise<Json>;
|
|
55
|
+
similar(options: { title: string; limit?: number } | { passageId: number; limit?: number }): Promise<Json>;
|
|
56
|
+
/** Page titles by their start or a pattern (GET /v1/titles). */
|
|
57
|
+
titles(options?: { prefix?: string; pattern?: string; sort?: string; limit?: number; offset?: number }): Promise<Json>;
|
|
58
|
+
/** Recent edits we have loaded (GET /v1/changes). Pass next_after back as after. */
|
|
59
|
+
changes(options?: { pages?: string | string[]; prefix?: string; pattern?: string; since?: string; after?: number; limit?: number }): Promise<Json>;
|
|
60
|
+
/** Get a POST to callbackUrl when a page (or family of pages) changes (POST /v1/watches). */
|
|
61
|
+
watch(options: { callbackUrl: string; page?: string; prefix?: string; pattern?: string }): Promise<Json>;
|
|
62
|
+
/** Your watches (GET /v1/watches). */
|
|
63
|
+
watches(): Promise<Json>;
|
|
64
|
+
/** Stop a watch (DELETE /v1/watches/{id}). */
|
|
65
|
+
unwatch(id: number | string): Promise<Json>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Ask a person for a key by email; resolves to the key once they press Approve. */
|
|
69
|
+
export declare function requestKey(
|
|
70
|
+
email: string,
|
|
71
|
+
app: string,
|
|
72
|
+
options?: {
|
|
73
|
+
onCode?: (code: string, message: string) => void;
|
|
74
|
+
baseUrl?: string;
|
|
75
|
+
/** Seconds to wait for approval. Default 900. */
|
|
76
|
+
wait?: number;
|
|
77
|
+
timeout?: number;
|
|
78
|
+
sleep?: (seconds: number) => Promise<void>;
|
|
79
|
+
},
|
|
80
|
+
): Promise<string>;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// WikiVectors: fresh English Wikipedia for AI apps. Zero dependencies; uses fetch (Node 18+, Deno, Bun, browsers, edge).
|
|
2
|
+
//
|
|
3
|
+
// import { WikiVectors } from "wikivectors";
|
|
4
|
+
// const wv = new WikiVectors(); // reads WIKIVECTORS_KEY
|
|
5
|
+
// for (const p of await wv.search("who won the 2026 FIFA World Cup", { limit: 3 })) console.log(p.title, p.permalink);
|
|
6
|
+
//
|
|
7
|
+
// Docs: https://wikivectors.com/docs
|
|
8
|
+
|
|
9
|
+
export const VERSION = "0.1.0";
|
|
10
|
+
export const DEFAULT_BASE_URL = "https://wikivectors.com";
|
|
11
|
+
|
|
12
|
+
/** The API said no. status is the HTTP code; code, message and requestId come from the error body. */
|
|
13
|
+
export class WikiVectorsError extends Error {
|
|
14
|
+
constructor(status, code, message, requestId) {
|
|
15
|
+
super(`${status} ${code}: ${message}${requestId ? ` (request ${requestId})` : ""}`);
|
|
16
|
+
this.name = "WikiVectorsError";
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.code = code;
|
|
19
|
+
this.detail = message;
|
|
20
|
+
this.requestId = requestId;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const envKey = () => (typeof process !== "undefined" && process.env ? process.env.WIKIVECTORS_KEY : undefined) || "";
|
|
25
|
+
|
|
26
|
+
/** One client per key. Every method returns the API's JSON. */
|
|
27
|
+
export class WikiVectors {
|
|
28
|
+
constructor({ apiKey, baseUrl = DEFAULT_BASE_URL, timeout = 30000 } = {}) {
|
|
29
|
+
this.apiKey = apiKey || envKey();
|
|
30
|
+
if (!this.apiKey) throw new Error("Pass apiKey or set WIKIVECTORS_KEY. Get a free key at https://wikivectors.com/account");
|
|
31
|
+
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
32
|
+
this.timeout = timeout;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Best-matching paragraphs, best first (POST /v1/search). Returns the results list. */
|
|
36
|
+
async search(query, { limit } = {}) {
|
|
37
|
+
return (await this._request("POST", "/v1/search", clean({ query, limit }))).results;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A short answer that cites its paragraphs, or answered: false (POST /v1/answer). */
|
|
41
|
+
answer(query, { options } = {}) {
|
|
42
|
+
return this._request("POST", "/v1/answer", clean({ query, options }));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Pages by title, one string or a list (POST /v1/pages). Returns the pages list.
|
|
47
|
+
* { full: true } (1-5 titles) also gives sections and passages: every paragraph in order.
|
|
48
|
+
*/
|
|
49
|
+
async pages(titles, { full } = {}) {
|
|
50
|
+
const body = { titles: typeof titles === "string" ? [titles] : [...titles] };
|
|
51
|
+
if (full) body.full = true;
|
|
52
|
+
return (await this._request("POST", "/v1/pages", body)).pages;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A page's infobox as structured facts (GET /v1/pages/{title}/facts). */
|
|
56
|
+
facts(title) {
|
|
57
|
+
return this._request("GET", `/v1/pages/${encodeURIComponent(title)}/facts`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Which page a name means, e.g. identify("Michael Jordan", { hint: "professor" }) (POST /v1/identify). */
|
|
61
|
+
identify(name, { hint, kind } = {}) {
|
|
62
|
+
return this._request("POST", "/v1/identify", clean({ name, hint, kind }));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Pages most like a page or a paragraph, by meaning (POST /v1/similar).
|
|
67
|
+
* similar("Ada Lovelace") or similar({ title, limit }) or similar({ passageId, limit }) (passage_id from search).
|
|
68
|
+
*/
|
|
69
|
+
similar(arg, opts = {}) {
|
|
70
|
+
const o = typeof arg === "string" ? { ...opts, title: arg } : { ...(arg || {}) };
|
|
71
|
+
if ((o.title == null) === (o.passageId == null)) throw new Error("Pass title or passageId (one of them).");
|
|
72
|
+
return this._request("POST", "/v1/similar", clean({ title: o.title, passage_id: o.passageId, limit: o.limit }));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Page titles by their start or a pattern (GET /v1/titles). */
|
|
76
|
+
titles({ prefix, pattern, sort, limit, offset } = {}) {
|
|
77
|
+
return this._request("GET", "/v1/titles", undefined, { prefix, pattern, sort, limit, offset });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Recent edits we have loaded (GET /v1/changes). Pass next_after back as after to page on. */
|
|
81
|
+
changes({ pages, prefix, pattern, since, after, limit } = {}) {
|
|
82
|
+
if (Array.isArray(pages)) pages = pages.join("|");
|
|
83
|
+
return this._request("GET", "/v1/changes", undefined, { pages, prefix, pattern, since, after, limit });
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Get a POST to callbackUrl when a page (or a family of pages) changes (POST /v1/watches). */
|
|
87
|
+
watch({ callbackUrl, page, prefix, pattern }) {
|
|
88
|
+
return this._request("POST", "/v1/watches", clean({ page, prefix, pattern, callback_url: callbackUrl }));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Your watches (GET /v1/watches). */
|
|
92
|
+
watches() {
|
|
93
|
+
return this._request("GET", "/v1/watches");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Stop a watch (DELETE /v1/watches/{id}). */
|
|
97
|
+
unwatch(id) {
|
|
98
|
+
return this._request("DELETE", `/v1/watches/${Number.parseInt(id, 10)}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async _request(method, path, body, params) {
|
|
102
|
+
let url = this.baseUrl + path;
|
|
103
|
+
const q = new URLSearchParams(clean(params || {}));
|
|
104
|
+
if ([...q].length) url += "?" + q;
|
|
105
|
+
return (await call(method, url, body, this.timeout, this.apiKey)).json;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Get a key for a person without them copying one: we email them a code and an Approve button.
|
|
111
|
+
* Shows the code (onCode(code, message), default: console.log the message), waits until they
|
|
112
|
+
* approve, and resolves to the new key. Rejects with WikiVectorsError code "denied", "expired"
|
|
113
|
+
* or "timeout" if no key comes. No key is needed to ask.
|
|
114
|
+
*/
|
|
115
|
+
export async function requestKey(email, app, { onCode, baseUrl = DEFAULT_BASE_URL, wait = 900, timeout = 30000, sleep } = {}) {
|
|
116
|
+
baseUrl = baseUrl.replace(/\/+$/, "");
|
|
117
|
+
sleep = sleep || ((s) => new Promise((r) => setTimeout(r, s * 1000)));
|
|
118
|
+
const start = (await call("POST", baseUrl + "/v1/keys/agent", { email, app }, timeout)).json;
|
|
119
|
+
if (onCode) onCode(start.code, start.message || "");
|
|
120
|
+
else console.log(start.message || `Check your email and approve code ${start.code}.`);
|
|
121
|
+
const interval = start.interval || 5;
|
|
122
|
+
const poll = `${baseUrl}/v1/keys/agent/${encodeURIComponent(start.request)}`;
|
|
123
|
+
let waited = 0;
|
|
124
|
+
for (;;) {
|
|
125
|
+
await sleep(interval);
|
|
126
|
+
waited += interval;
|
|
127
|
+
const { status, json } = await call("GET", poll, undefined, timeout, undefined, true);
|
|
128
|
+
const state = json && json.status;
|
|
129
|
+
if (state === "approved" && json.key) return json.key;
|
|
130
|
+
if (state !== "pending") throw new WikiVectorsError(status, state || "http_error", (json && json.message) || "No key was made.");
|
|
131
|
+
if (waited >= wait) throw new WikiVectorsError(408, "timeout", `No approval after ${waited} seconds. Ask again with requestKey.`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// One HTTP call. Resolves to { status, json }. Errors reject with WikiVectorsError, unless raw: then a JSON error body comes back.
|
|
136
|
+
async function call(method, url, body, timeout, apiKey, raw = false) {
|
|
137
|
+
const headers = { Accept: "application/json", "User-Agent": `wikivectors-js/${VERSION}` };
|
|
138
|
+
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
|
|
139
|
+
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
140
|
+
const res = await fetch(url, {
|
|
141
|
+
method,
|
|
142
|
+
headers,
|
|
143
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
144
|
+
signal: AbortSignal.timeout(timeout),
|
|
145
|
+
});
|
|
146
|
+
const text = await res.text();
|
|
147
|
+
let parsed = null;
|
|
148
|
+
try { parsed = text ? JSON.parse(text) : null; } catch { parsed = undefined; }
|
|
149
|
+
if (res.ok) return { status: res.status, json: parsed };
|
|
150
|
+
if (raw && parsed && typeof parsed === "object") return { status: res.status, json: parsed };
|
|
151
|
+
const err = (parsed && typeof parsed === "object" && parsed.error) || {};
|
|
152
|
+
throw new WikiVectorsError(res.status, err.code || "http_error", err.message || text.slice(0, 200) || res.statusText, err.request_id);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function clean(o) {
|
|
156
|
+
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined && v !== null));
|
|
157
|
+
}
|