diff --git a/CHANGELOG.md b/CHANGELOG.md index cbfc949..713a408 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 2.5.0 - 2026-11-26 + +`s1` names itself to Search1API in `X-Search1API-Client: cli/`, 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. diff --git a/package-lock.json b/package-lock.json index 29e1e75..625b498 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "search1api-cli", - "version": "2.4.0", + "version": "2.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "search1api-cli", - "version": "2.4.0", + "version": "2.5.0", "hasInstallScript": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 58633dd..453b260 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/api.ts b/src/api.ts index 8f4cf09..a44883e 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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; @@ -13,12 +14,19 @@ export async function send( options: RequestOptions = {} ): Promise { 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}`, }, }) diff --git a/src/auth.ts b/src/auth.ts index 2c14f71..51e1ac9 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -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; @@ -796,7 +797,7 @@ export async function fetchWithAuth( export async function validateCredential(credential: string): Promise { const response = await fetch(`${API_BASE}/usage`, { - headers: { Authorization: `Bearer ${credential}` }, + headers: { ...clientHeaders(), Authorization: `Bearer ${credential}` }, }); if (!response.ok) { const text = await response.text(); diff --git a/src/client.ts b/src/client.ts new file mode 100644 index 0000000..668eeed --- /dev/null +++ b/src/client.ts @@ -0,0 +1,53 @@ +/** + * Names the CLI, and the coding agent running it, to the Search1API gateway: + * `X-Search1API-Client: cli/` 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 = [ + ["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 { + if (version === undefined) return {}; + const agent = detectAgent(env); + return { + "X-Search1API-Client": `cli/${version}`, + ...(agent ? { "X-Search1API-Client-App": agent } : {}), + }; +} diff --git a/src/index.ts b/src/index.ts index 0d3aeca..72272cd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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({ diff --git a/tests/client.test.mjs b/tests/client.test.mjs new file mode 100644 index 0000000..d8e80dd --- /dev/null +++ b/tests/client.test.mjs @@ -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"); +});