Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.5.0 - 2026-11-26

`s1` names itself to Search1API in `X-Search1API-Client: cli/<version>`, and the coding agent running it (from `AI_AGENT`, or markers such as `CLAUDECODE`, `CODEX_SANDBOX`, `GEMINI_CLI`) in `X-Search1API-Client-App`, so API analytics can tell CLI and agent-skill traffic apart. Analytics only; it does not affect billing or auth. Importing the package as a library sends neither header.

## 2.4.0 - 2026-11-24

`s1 login` now works when the browser is on another device (SSH, servers, containers). It opens your browser as before and also prints a URL and short code; approve the code in a browser on any device and the CLI receives the tokens (OAuth 2.0 Device Authorization Grant). Whichever finishes first is used. `s1 login --device` uses only the short-code flow.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "search1api-cli",
"version": "2.4.0",
"version": "2.5.0",
"description": "CLI tool for Search1API - web search, maps, news, crawl, deepcrawl, sitemap, and trending",
"type": "module",
"exports": {
Expand Down
12 changes: 10 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fetchWithAuth } from "./auth.js";
import { API_BASE } from "./config.js";
import { clientHeaders } from "./client.js";

export interface RequestOptions {
apiKey?: string;
Expand All @@ -13,12 +14,19 @@ export async function send(
options: RequestOptions = {}
): Promise<Response> {
const url = `${API_BASE}${path}`;
const withSignal: RequestInit = { ...init, signal: options.signal };
const withSignal: RequestInit = {
...init,
headers: {
...clientHeaders(),
...Object.fromEntries(new Headers(init.headers).entries()),
},
signal: options.signal,
};
return options.apiKey
? fetch(url, {
...withSignal,
headers: {
...Object.fromEntries(new Headers(init.headers).entries()),
...Object.fromEntries(new Headers(withSignal.headers).entries()),
Authorization: `Bearer ${options.apiKey}`,
},
})
Expand Down
3 changes: 2 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
saveOAuthClientConfig,
saveOAuthConfig,
} from "./config.js";
import { clientHeaders } from "./client.js";

const CALLBACK_PATH = "/callback";
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1000;
Expand Down Expand Up @@ -796,7 +797,7 @@ export async function fetchWithAuth(

export async function validateCredential(credential: string): Promise<void> {
const response = await fetch(`${API_BASE}/usage`, {
headers: { Authorization: `Bearer ${credential}` },
headers: { ...clientHeaders(), Authorization: `Bearer ${credential}` },
});
if (!response.ok) {
const text = await response.text();
Expand Down
53 changes: 53 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Names the CLI, and the coding agent running it, to the Search1API gateway:
* `X-Search1API-Client: cli/<version>` and `X-Search1API-Client-App`. The
* gateway reduces the agent name to a fixed list for its request analytics.
* Analytics only: it never bills or authorizes on it.
*
* Only the `s1` entry point turns this on: code that imports the package as
* a library is not the CLI and sends no client headers.
*/
let version: string | undefined;

/** Called once by the CLI entry point, which knows the package version. */
export function setClientVersion(value: string): void {
version = value;
}

// Variables agents set in the shells they run commands in. AI_AGENT is the
// shared convention (e.g. "claude-code_2-1-332_agent"); the rest are each
// agent's own marker, checked in order.
const AGENT_MARKERS: ReadonlyArray<readonly [string, string]> = [
["CLAUDECODE", "claude-code"],
["CURSOR_AGENT", "cursor"],
["GEMINI_CLI", "gemini-cli"],
["CODEX_SANDBOX", "codex"],
["CODEX_THREAD_ID", "codex"],
["OPENCODE", "opencode"],
["OPENCODE_CLIENT", "opencode"],
["AUGMENT_AGENT", "augment"],
// Any Cursor terminal, agent or not; least specific, so last.
["CURSOR_TRACE_ID", "cursor"],
];

export function detectAgent(
env: NodeJS.ProcessEnv = process.env
): string | undefined {
const declared = env.AI_AGENT?.replace(/[^\x20-\x7e]/g, "").trim();
if (declared) return declared.slice(0, 100);
for (const [name, agent] of AGENT_MARKERS) {
if (env[name]) return agent;
}
return undefined;
}

export function clientHeaders(
env: NodeJS.ProcessEnv = process.env
): Record<string, string> {
if (version === undefined) return {};
const agent = detectAgent(env);
return {
"X-Search1API-Client": `cli/${version}`,
...(agent ? { "X-Search1API-Client-App": agent } : {}),
};
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from "chalk";
import { Command } from "commander";
import { feedbackHint } from "agentback/cli";
import { registerSearchCommand } from "./commands/search.js";
import { setClientVersion } from "./client.js";

declare const __PKG_VERSION__: string | undefined;
declare const __PKG_NAME__: string | undefined;
Expand All @@ -18,6 +19,7 @@ if (isBinary) {
const require = createRequire(import.meta.url);
pkg = require("../package.json");
}
setClientVersion(pkg.version);
const notifier = updateNotifier({ pkg });
if (isBinary && notifier.update) {
notifier.notify({
Expand Down
56 changes: 56 additions & 0 deletions tests/client.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { createServer } from "node:http";
import test from "node:test";
import { fileURLToPath } from "node:url";

const { clientHeaders, detectAgent, setClientVersion } = await import("../dist/client.js");
const CLI = fileURLToPath(new URL("../dist/index.js", import.meta.url));

test("library use sends no client headers", () => {
assert.deepEqual(clientHeaders({ CLAUDECODE: "1" }), {});
});

test("detects the agent running the CLI", () => {
assert.equal(detectAgent({ AI_AGENT: "claude-code_2-1-332_agent", CODEX_SANDBOX: "seatbelt" }), "claude-code_2-1-332_agent");
assert.equal(detectAgent({ CLAUDECODE: "1" }), "claude-code");
assert.equal(detectAgent({ CODEX_SANDBOX: "seatbelt" }), "codex");
assert.equal(detectAgent({ GEMINI_CLI: "1" }), "gemini-cli");
assert.equal(detectAgent({ CURSOR_AGENT: "1", CURSOR_TRACE_ID: "x" }), "cursor");
assert.equal(detectAgent({}), undefined);
assert.equal(detectAgent({ AI_AGENT: "x".repeat(300) }).length, 100);
});

test("names the CLI version and the agent once the entry point sets it", () => {
setClientVersion("2.4.0");
assert.deepEqual(clientHeaders({ CLAUDECODE: "1" }), {
"X-Search1API-Client": "cli/2.4.0",
"X-Search1API-Client-App": "claude-code",
});
assert.deepEqual(clientHeaders({}), { "X-Search1API-Client": "cli/2.4.0" });
});

test("s1 search sends the client headers to the API", async (context) => {
const seen = [];
const server = createServer((req, res) => {
seen.push(req.headers);
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ results: [] }));
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
context.after(() => server.close());
const { port } = server.address();
const env = { ...process.env, SEARCH1API_KEY: "test-key", SEARCH1API_API_BASE: `http://127.0.0.1:${port}`, NO_UPDATE_NOTIFIER: "1" };
for (const name of Object.keys(env)) {
if (/^(AI_AGENT|CLAUDECODE|CURSOR_|GEMINI_CLI|CODEX_|OPENCODE|AUGMENT_AGENT)/.test(name)) delete env[name];
}
env.GEMINI_CLI = "1";
await new Promise((resolve, reject) => {
const child = spawn(process.execPath, [CLI, "search", "agents", "--json"], { env, stdio: "ignore" });
child.on("error", reject);
child.on("exit", resolve);
});
assert.equal(seen.length, 1);
assert.match(seen[0]["x-search1api-client"], /^cli\/\d+\.\d+\.\d+/);
assert.equal(seen[0]["x-search1api-client-app"], "gemini-cli");
});