Files
SpecStream c0329b2c0c specstream 0.1.0: web search and extract tools, and a web search API entry (#866)
SpecStream's Search API and Extract API as tools for every agent, and as a web search entry in Settings > Models > Web search.

Reviewed at 2ae405947ab71dab6f9557a4191a860bd92a2ddf. Run on mote: bun test packages/specstream (9 pass), bun scripts/check.mjs, bun test scripts (22 pass). Both endpoints, called for real without a key, answer 401 {"error":{"code":"missing_api_key",...}}, which unwrap() raises with its status, so a refused key rests in the rotation.

Not tried: a call with a real key.
2026-11-10 04:37:22 +08:00

46 lines
1.8 KiB
JavaScript

// The two SpecStream calls. A reply is {data: {...}}, or {error: {code, message}} when the call stops.
const BASE = "https://api.specstream.ai"
// Only the key the caller gives: the tools pass SPECSTREAM_API_KEY, the search entry the key magpie keeps.
export const keyOf = (key) => {
const k = typeof key === "string" ? key.trim() : ""
if (!k) throw new Error("SpecStream needs an API key")
return k
}
// Reads a reply: its `data`, or an error carrying error.message and the HTTP status.
export async function unwrap(r) {
let body = null
try {
body = await r.json()
} catch {}
if (!r.ok || !body || body.data == null) {
const message = body?.error?.message || `SpecStream answered ${r.status}`
throw Object.assign(new Error(message), { status: r.status, code: body?.error?.code })
}
return body.data
}
export async function search({ query, count, key, signal }) {
const params = new URLSearchParams({ q: String(query).slice(0, 2000) })
// num, 1 to 100: how many sources come back (https://specstream.ai/agents.md, "num and page")
const n = Math.trunc(Number(count))
if (n >= 1) params.set("num", String(Math.min(100, n)))
const r = await fetch(`${BASE}/v1/web-search?${params}`, {
headers: { authorization: `Bearer ${keyOf(key)}` },
signal,
})
return unwrap(r)
}
export async function extract({ urls, format = "markdown", key, signal }) {
if (!Array.isArray(urls) || urls.length < 1 || urls.length > 20) throw new Error("urls is a list of 1 to 20 addresses")
const r = await fetch(`${BASE}/v1/extract`, {
method: "POST",
headers: { authorization: `Bearer ${keyOf(key)}`, "content-type": "application/json" },
body: JSON.stringify({ urls, format: format === "text" ? "text" : "markdown" }),
signal,
})
return unwrap(r)
}