// Searches the web and extracts structured key findings
const apiKey = process.env.WEBLINQ_API_KEY;
const base = 'https://api.weblinq.dev/v1';
export async function researchTopic(query) {
// 1️⃣ search
const {
data: { results },
} = await post('/web/search', { query, limit: 5 });
// 2️⃣ minimal schema
const schema = {
type: 'object',
properties: {
title: { type: 'string' },
keyFindings: { type: 'array', items: { type: 'string' } },
conclusions: { type: 'array', items: { type: 'string' } },
},
required: ['title', 'keyFindings'],
};
// 3️⃣ structured extract
const extracted = await Promise.all(
results.map((r) =>
post('/web/extract-json', {
url: r.url,
response_format: { type: 'json_schema', json_schema: schema },
prompt: `Key findings on "${query}"`,
}),
),
);
return results.map((r, i) => ({
url: r.url,
...extracted[i].data.extracted,
}));
}
function post(p, body) {
return fetch(`${base}${p}`, {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
}).then((r) => r.json());
}