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).
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
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);
|
||||
});
|
||||
@@ -87,6 +87,14 @@ test("fetch stub: all six tools preserve wire bodies, defaults and complete JSON
|
||||
assert.deepEqual(plugin.auth.methods.map((m) => m.type), ["oauth", "api"]);
|
||||
});
|
||||
|
||||
test("fetch stub: API requests identify the plugin version", async (t) => {
|
||||
const f = await stub(t, () => json({ results: [] }));
|
||||
const plugin = await Search1APIPlugin(f.input, { baseUrl, apiKey: "fixture-key" });
|
||||
await plugin.tool.search.execute({ query: "fixture" }, context());
|
||||
const { version } = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"));
|
||||
assert.equal(new Headers(f.calls[0].headers).get("user-agent"), `search1api-magpie/${version}`);
|
||||
});
|
||||
|
||||
test("fetch stub: invalid arguments are refused before any request", async (t) => {
|
||||
const f = await stub(t, () => { throw new Error("must not fetch"); });
|
||||
const plugin = await Search1APIPlugin(f.input, { baseUrl, apiKey: "fixture-key" });
|
||||
|
||||
@@ -1,13 +1,24 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { access, writeFile } from "node:fs/promises";
|
||||
import { createServer } from "node:net";
|
||||
import { join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { startOAuth } from "../src/oauth.ts";
|
||||
import { fixture } from "./helpers.mjs";
|
||||
|
||||
const registrations = (f) => f.calls.filter((call) => call.path === "/register").length;
|
||||
|
||||
async function signIn(login) {
|
||||
await fetch(login.url);
|
||||
return login.callback();
|
||||
}
|
||||
|
||||
test("browser OAuth uses PKCE S256, public registration, state and issuer checks", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const login = await startOAuth(f.baseUrl, 1000);
|
||||
const authURL = new URL(login.url);
|
||||
assert.equal(authURL.searchParams.get("code_challenge_method"), "S256");
|
||||
assert.equal(authURL.searchParams.get("utm_source"), "magpie");
|
||||
const redirect = new URL(authURL.searchParams.get("redirect_uri"));
|
||||
redirect.searchParams.set("state", "wrong");
|
||||
redirect.searchParams.set("code", "wrong");
|
||||
@@ -38,3 +49,50 @@ test("declined consent and abandoned sign-ins finish without leaking tokens or c
|
||||
const redirect = new URL(abandoned.url).searchParams.get("redirect_uri");
|
||||
await assert.rejects(fetch(redirect));
|
||||
});
|
||||
|
||||
test("sign-ins reuse the registered client and its callback port", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const file = join(f.directory, "search1api-oauth-client.json");
|
||||
const first = await startOAuth(f.baseUrl, 1000, file);
|
||||
assert.equal((await signIn(first)).type, "success");
|
||||
const second = await startOAuth(f.baseUrl, 1000, file);
|
||||
assert.equal(
|
||||
new URL(second.url).searchParams.get("redirect_uri"),
|
||||
new URL(first.url).searchParams.get("redirect_uri"),
|
||||
);
|
||||
const result = await signIn(second);
|
||||
assert.equal(result.type, "success");
|
||||
assert.equal(result.clientId, "test-public-client");
|
||||
assert.equal(registrations(f), 1);
|
||||
assert.equal(f.state.grants, 2);
|
||||
});
|
||||
|
||||
test("a busy callback port or a saved client for another issuer registers anew", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const file = join(f.directory, "search1api-oauth-client.json");
|
||||
const first = await startOAuth(f.baseUrl, 1000, file);
|
||||
await signIn(first);
|
||||
const port = new URL(new URL(first.url).searchParams.get("redirect_uri")).port;
|
||||
const blocker = createServer();
|
||||
await new Promise((resolve) => blocker.listen(Number(port), "127.0.0.1", resolve));
|
||||
t.after(() => blocker.close());
|
||||
const busy = await startOAuth(f.baseUrl, 1000, file);
|
||||
assert.notEqual(new URL(new URL(busy.url).searchParams.get("redirect_uri")).port, port);
|
||||
assert.equal((await signIn(busy)).type, "success");
|
||||
assert.equal(registrations(f), 2);
|
||||
await writeFile(file, JSON.stringify({ issuer: "https://other.example", clientId: "x", redirectUri: "http://127.0.0.1:1/callback" }));
|
||||
await signIn(await startOAuth(f.baseUrl, 1000, file));
|
||||
assert.equal(registrations(f), 3);
|
||||
});
|
||||
|
||||
test("a reused client that does not finish signing in is forgotten", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const file = join(f.directory, "search1api-oauth-client.json");
|
||||
await signIn(await startOAuth(f.baseUrl, 1000, file));
|
||||
f.state.decline = true;
|
||||
assert.equal((await signIn(await startOAuth(f.baseUrl, 1000, file))).type, "failed");
|
||||
await assert.rejects(access(file));
|
||||
f.state.decline = false;
|
||||
assert.equal((await signIn(await startOAuth(f.baseUrl, 1000, file))).type, "success");
|
||||
assert.equal(registrations(f), 2);
|
||||
});
|
||||
|
||||
@@ -17,6 +17,7 @@ test("all six tools forward arguments to the API and preserve result metadata",
|
||||
"crawl",
|
||||
"sitemap",
|
||||
"trending",
|
||||
"send_feedback",
|
||||
]);
|
||||
const cases = [
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user