Files
plugins/packages/search1api/src/request.ts
T
wang2 6a2ade35ef search1api 0.1.1: Magpie attribution, reused OAuth client, send_feedback through Search1API (#424)
search1api 0.1.1 (fatwang2):
- Sign-ins carry utm_source=magpie and API requests send User-Agent search1api-magpie/0.1.1.
- The registered public OAuth client and its loopback port are saved in search1api-oauth-client.json (mode 600) and reused. A busy port, another issuer, or a sign-in that doesn't finish leads to a new registration.
- A send_feedback tool posts to Search1API's own POST /feedback with the caller's credential, on the same request path as the other tools, including the OAuth refresh on 401. Its description lists what is sent (message, intent, category, request_id, model) and says Search1API stores it on agentback.app, linked to the signed-in account. The plugin contacts no other host. A report that can't be sent returns a note and doesn't fail the task.

Run at the reviewed head d48c8c8 (sandbox HOME/TMPDIR, Bun 1.4.2):
- bun test packages/search1api/tests: 23 pass, 0 fail
- bun scripts/check.mjs search1api: ok
- PR CI (test, on-npm): pass
- grep of the package: the only host is api.search1api.com (plus 127.0.0.1 for the callback). No hard-coded keys. agentback.app appears only in the description, the README and a test.
- Drove the built plugin against my own loopback fake API:
  - OAuth access revoked (401): POST /feedback with the old bearer, then /token refresh_token, then POST /feedback with the new bearer. Got "Feedback recorded (fb_1)" and auth.set with the new token.
  - Bad API key: a 401 gives "not recorded ... (401)" with no refresh, and the error body is not echoed.
  - Body is exactly {message, intent, category, request_id, agent:{name:"Magpie", model}}, with UA search1api-magpie/0.1.1.
  - Unknown args and a non-string model are refused before any request.
- An unauthenticated probe of the real api.search1api.com: POST /feedback gives 401, and an unknown path gives 404. So the endpoint exists.
- The OAuth client reuse (unchanged since 95f236f) was run by hand in the previous review.

Not tried: a real Search1API account (none available).
2026-10-27 15:44:05 +08:00

44 lines
1.4 KiB
TypeScript

import { serviceURL, USER_AGENT } from "./types.ts";
export class AuthenticationError extends Error {}
// Only the reviewed endpoints (six tools and feedback) are exposed. Match the original client's
// request bodies and deadlines without adding runtime npm dependencies.
export async function request(
baseUrl: string,
name: string,
token: string,
body: Record<string, unknown>,
signal: AbortSignal,
): Promise<unknown> {
signal.throwIfAborted();
const response = await fetch(serviceURL(`${baseUrl}/${name}`), {
method: "POST",
redirect: "error",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
"User-Agent": USER_AGENT,
},
body: JSON.stringify(body),
signal: AbortSignal.any([
signal,
AbortSignal.timeout(name === "ask" ? 45_000 : 30_000),
]),
});
if (!response.ok) {
// Error bodies can echo credentials. Expose only the endpoint and status.
await response.body?.cancel();
const ErrorType = response.status === 401 ? AuthenticationError : Error;
throw new ErrorType(`Search1API ${name} request failed (${response.status}).`);
}
try {
return await response.json();
} catch (error) {
signal.throwIfAborted();
if (error instanceof Error && error.name === "TimeoutError") throw error;
throw new Error(`Search1API returned invalid JSON for ${name}.`);
}
}