-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (132 loc) · 4.63 KB
/
index.js
File metadata and controls
146 lines (132 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* @contextwire/sdk — Official Node.js SDK for ContextWire
* https://contextwire.dev
*
* Usage:
* import { ContextWire } from '@contextwire/sdk';
* const cw = new ContextWire('YOUR_API_KEY');
* const answer = await cw.ask('Who invented the telephone?');
* console.log(answer.answer); // "Alexander Graham Bell"
*/
const DEFAULT_BASE = 'https://contextwire.dev/api';
export class ContextWire {
#key;
#base;
#timeout;
/**
* @param {string} apiKey - Your ContextWire API key
* @param {object} [options]
* @param {string} [options.baseUrl] - API base URL (default: https://contextwire.dev/api)
* @param {number} [options.timeout] - Request timeout in ms (default: 30000)
*/
constructor(apiKey, options = {}) {
if (!apiKey) throw new Error('API key is required. Get one at https://contextwire.dev/#signup');
this.#key = apiKey;
this.#base = (options.baseUrl || DEFAULT_BASE).replace(/\/+$/, '');
this.#timeout = options.timeout || 30000;
}
async #fetch(path, options = {}) {
const url = `${this.#base}${path}`;
const res = await fetch(url, {
...options,
headers: {
'Authorization': `Bearer ${this.#key}`,
'Content-Type': 'application/json',
...options.headers,
},
signal: AbortSignal.timeout(this.#timeout),
});
if (!res.ok) {
const body = await res.text();
throw new Error(`ContextWire API error ${res.status}: ${body}`);
}
return res.json();
}
/**
* Ask a factual question — full research loop (94.3% on SimpleQA).
* @param {string} question
* @returns {Promise<{answer: string, source: string, latency_ms: number, sources: Array, pageExcerpts: Array}>}
*/
async ask(question) {
return this.#fetch(`/ask?q=${encodeURIComponent(question)}`);
}
/**
* Search the web using 100+ engines.
* @param {string} query
* @param {object} [options]
* @param {string} [options.profile] - Search profile (web, news, academic, code, etc.)
* @param {number} [options.maxResults] - Max results (1-100, default: 10)
* @param {string} [options.freshness] - Time filter (day, week, month, year)
* @param {number} [options.includeContent] - Number of results to fetch full content for
* @returns {Promise<{results: Array, answer?: string, suggestions?: Array}>}
*/
async search(query, options = {}) {
const params = new URLSearchParams({ q: query, format: 'json' });
if (options.profile) params.set('profile', options.profile);
if (options.maxResults) params.set('max_results', String(options.maxResults));
if (options.freshness) params.set('freshness', options.freshness);
if (options.includeContent) params.set('include_content', String(options.includeContent));
return this.#fetch(`/search?${params}`);
}
/**
* Extract clean text or markdown from a URL.
* @param {string} url - URL to extract content from
* @param {'text'|'markdown'} [format='text']
* @returns {Promise<{title: string, url: string, content: string, word_count: number}>}
*/
async extract(url, format = 'text') {
return this.#fetch(`/extract?url=${encodeURIComponent(url)}&format=${format}`);
}
/**
* Search academic papers (arXiv, Semantic Scholar, CrossRef, PubMed, OpenAlex).
* @param {string} query
* @param {number} [maxPapers=5]
* @returns {Promise<{papers: Array}>}
*/
async research(query, maxPapers = 5) {
return this.#fetch(`/research?q=${encodeURIComponent(query)}&max_papers=${maxPapers}`);
}
/**
* Run multiple searches in parallel.
* @param {Array<{q: string, profile?: string, max_results?: number}>} queries
* @returns {Promise<{results: Array}>}
*/
async batchSearch(queries) {
return this.#fetch('/search/batch', {
method: 'POST',
body: JSON.stringify({ queries }),
});
}
/**
* Get autocomplete suggestions.
* @param {string} query - Partial query
* @returns {Promise<{suggestions: string[]}>}
*/
async suggest(query) {
return this.#fetch(`/suggest?q=${encodeURIComponent(query)}`);
}
/**
* List available search engines.
* @param {string} [category] - Filter by category
* @returns {Promise<{engines: Array}>}
*/
async engines(category) {
const params = category ? `?category=${encodeURIComponent(category)}` : '';
return this.#fetch(`/engines${params}`);
}
/**
* List all search profiles.
* @returns {Promise<{profiles: Array}>}
*/
async profiles() {
return this.#fetch('/profiles');
}
/**
* Check API health.
* @returns {Promise<{status: string, version: string}>}
*/
async health() {
return this.#fetch('/health');
}
}
export default ContextWire;