Files
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

79 lines
3.8 KiB
JavaScript

import assert from "node:assert/strict";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { Search1APIPlugin } from "../index.mjs";
import { context } from "./helpers.mjs";
const baseUrl = "https://search1api.invalid";
const json = (value, status = 200) => new Response(JSON.stringify(value), { status });
// Every fetch is replaced: reports may only go to the Search1API API.
async function stub(t, handler) {
const directory = await mkdtemp(join(tmpdir(), "search1api-feedback-test-"));
const originalFetch = globalThis.fetch;
const originalKey = process.env.SEARCH1API_API_KEY;
delete process.env.SEARCH1API_API_KEY;
const calls = [];
globalThis.fetch = async (url, init) => {
const call = { url: new URL(String(url)), ...init };
calls.push(call);
assert.equal(call.url.origin, baseUrl);
return handler(call);
};
t.after(async () => {
globalThis.fetch = originalFetch;
if (originalKey === undefined) delete process.env.SEARCH1API_API_KEY;
else process.env.SEARCH1API_API_KEY = originalKey;
await rm(directory, { recursive: true, force: true });
});
const input = { directory, client: { auth: { set: async () => {} } } };
return { calls, input };
}
const report = { message: "Empty results for a known page", intent: "search for the docs page, expected it first", category: "bug", model: "fixture-model" };
test("feedback goes to Search1API's feedback API with the caller's credential", async (t) => {
const f = await stub(t, () => json({ id: "fb_fixture", status: "new" }, 201));
const plugin = await Search1APIPlugin(f.input, { baseUrl, apiKey: "fixture-key" });
// The agent decides what to send from the description alone.
assert.match(plugin.tool.send_feedback.description, /agentback\.app/);
assert.match(plugin.tool.send_feedback.description, /linked to the signed-in Search1API account/);
assert.doesNotMatch(plugin.tool.send_feedback.args.properties.intent.description, /trying to accomplish/);
const text = await plugin.tool.send_feedback.execute(report, context());
assert.equal(text, "Feedback recorded (fb_fixture). Thank you.");
assert.equal(f.calls.length, 1);
assert.equal(f.calls[0].url.pathname, "/feedback");
assert.equal(f.calls[0].method, "POST");
assert.equal(new Headers(f.calls[0].headers).get("authorization"), "Bearer fixture-key");
assert.deepEqual(JSON.parse(f.calls[0].body), {
message: report.message,
intent: report.intent,
category: "bug",
agent: { name: "Magpie", model: "fixture-model" },
});
});
test("a report that cannot be sent never fails the task", async (t) => {
let status = 503;
const f = await stub(t, () => json({ ok: false }, status));
const keyed = await Search1APIPlugin(f.input, { baseUrl, apiKey: "fixture-key" });
const message = { message: "Missing capability" };
assert.match(await keyed.tool.send_feedback.execute(message, context()), /not recorded: .*\(503\)/);
status = 429;
assert.match(await keyed.tool.send_feedback.execute(message, context()), /not recorded: .*\(429\)/);
globalThis.fetch = async () => { throw new TypeError("offline"); };
assert.match(await keyed.tool.send_feedback.execute(message, context()), /not recorded/);
const signedOut = await Search1APIPlugin(f.input, { baseUrl });
assert.match(await signedOut.tool.send_feedback.execute(message, context()), /not recorded: Sign in/);
});
test("invalid feedback is refused before any request", async (t) => {
const f = await stub(t, () => json({}));
const plugin = await Search1APIPlugin(f.input, { baseUrl, apiKey: "fixture-key" });
for (const args of [{}, { message: "" }, { message: "x", category: "praise" }, { message: "x", extra: 1 }])
await assert.rejects(plugin.tool.send_feedback.execute(args, context()));
assert.equal(f.calls.length, 0);
});