Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(api): log developer category searches to the code_searches ledger

Karan Lokchandanib292323121c6
diff --git a/apps/api/src/controllers/v2/__tests__/search-developer-ledger.test.ts b/apps/api/src/controllers/v2/__tests__/search-developer-ledger.test.ts
new file mode 100644
index 000000000..40c19b622
--- /dev/null
+++ b/apps/api/src/controllers/v2/__tests__/search-developer-ledger.test.ts
@@ -0,0 +1,268 @@
+import { vi } from "vitest";
+
+const mockLogRequest = vi.fn();
+const mockLogSearch = vi.fn();
+const mockLogResearchEndpoint = vi.fn();
+
+vi.mock("../../../services/logging/log_job", () => ({
+ logRequest: (...args: any[]) => mockLogRequest(...args),
+ logSearch: (...args: any[]) => mockLogSearch(...args),
+ logResearchEndpoint: (...args: any[]) => mockLogResearchEndpoint(...args),
+}));
+
+const mockExecuteSearch = vi.fn();
+
+vi.mock("../../../search/execute", () => ({
+ executeSearch: (...args: any[]) => mockExecuteSearch(...args),
+}));
+
+vi.mock("../../../services/billing/credit_billing", () => ({
+ billTeam: vi.fn().mockResolvedValue(undefined),
+}));
+
+vi.mock("../../../lib/keyless", () => ({
+ KEYLESS_FREE_TIER_LIMIT_MESSAGE: "keyless limit reached",
+ adjustKeylessCredits: vi.fn().mockResolvedValue(undefined),
+ logKeylessCreditUsage: vi.fn().mockResolvedValue(undefined),
+ reserveKeylessCredits: vi.fn().mockResolvedValue({ ok: true }),
+}));
+
+vi.mock("../../../lib/keyless-credit-projection", () => ({
+ projectSearchTotalCredits: () => 0,
+}));
+
+vi.mock("../../../lib/threat-protection/request", () => ({
+ resolveThreatProtection: vi.fn().mockResolvedValue({ policy: null }),
+ checkUrlsAgainstThreatPolicy: vi.fn(),
+}));
+
+vi.mock("../../../lib/key-restriction", () => ({
+ actionTypesOf: () => [],
+ formatTypesOf: () => [],
+ checkKeyFormatRestriction: vi.fn().mockResolvedValue({ allowed: true }),
+ checkKeyEndpointRestriction: vi.fn().mockResolvedValue({ allowed: true }),
+}));
+
+vi.mock("../../../services/sentry", () => ({
+ applyZdrScope: vi.fn(),
+ captureExceptionWithZdrCheck: vi.fn(),
+}));
+
+vi.mock("../../../lib/logger", () => ({
+ logger: {
+ info: vi.fn(),
+ warn: vi.fn(),
+ error: vi.fn(),
+ debug: vi.fn(),
+ child: vi.fn().mockReturnThis(),
+ },
+}));
+
+import { searchController } from "../search";
+
+const TEAM_ID = "11111111-1111-1111-1111-111111111111";
+
+const developerResults = [
+ {
+ url: "https://github.com/example/repo",
+ title: "example/repo",
+ description: "a repo",
+ position: 1,
+ category: "developer",
+ },
+ {
+ url: "https://github.com/example/other",
+ title: "example/other",
+ description: "another repo",
+ position: 2,
+ category: "developer",
+ },
+];
+
+function executeResult(overrides: Record<string, any> = {}) {
+ return {
+ response: { web: [], developer: developerResults },
+ totalResultsCount: 2,
+ searchCredits: 2,
+ scrapeCredits: 0,
+ totalCredits: 2,
+ shouldScrape: false,
+ ...overrides,
+ };
+}
+
+function makeReq(body: Record<string, any>, headers: Record<string, any> = {}) {
+ return {
+ body,
+ headers,
+ auth: { team_id: TEAM_ID },
+ acuc: { api_key_id: 7, flags: null },
+ } as any;
+}
+
+function makeRes() {
+ const res: any = {
+ status: vi.fn(),
+ json: vi.fn(),
+ };
+ res.status.mockReturnValue(res);
+ res.json.mockReturnValue(res);
+ return res;
+}
+
+async function flushAsync() {
+ await new Promise(resolve => setImmediate(resolve));
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mockLogRequest.mockResolvedValue(undefined);
+ mockLogSearch.mockResolvedValue(undefined);
+ mockLogResearchEndpoint.mockResolvedValue(undefined);
+ mockExecuteSearch.mockResolvedValue(executeResult());
+});
+
+describe("developer category code_searches ledger", () => {
+ it("writes exactly one code_searches row for a developer category search", async () => {
+ const req = makeReq({
+ query: "vector database client",
+ categories: ["developer"],
+ origin: "sdk",
+ integration: "cli",
+ });
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ expect(res.status).toHaveBeenCalledWith(200);
+ expect(mockLogResearchEndpoint).toHaveBeenCalledTimes(1);
+ const row = mockLogResearchEndpoint.mock.calls[0][0];
+ expect(row.table).toBe("code_searches");
+ expect(row.team_id).toBe(TEAM_ID);
+ expect(row.target).toBe("vector database client");
+ expect(row.num_results).toBe(2);
+ expect(row.credits_cost).toBe(0);
+ expect(row.is_successful).toBe(true);
+ expect(row.options.origin).toBe("sdk");
+ expect(row.options.integration).toBe("cli");
+ expect(row.options.api_version).toBe("v2");
+ expect(row.options.categories).toEqual([{ type: "developer" }]);
+ expect(row.options.via).toBe("search_category");
+ expect(row.request_id).toBe(res.json.mock.calls[0][0].id);
+ });
+
+ it("counts alias category inputs the same as developer", async () => {
+ for (const alias of ["repo", "code", "docs", "developer_index"]) {
+ mockLogResearchEndpoint.mockClear();
+ const req = makeReq({
+ query: "http client",
+ categories: [alias],
+ origin: "sdk",
+ });
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ expect(res.status).toHaveBeenCalledWith(200);
+ expect(mockLogResearchEndpoint).toHaveBeenCalledTimes(1);
+ const row = mockLogResearchEndpoint.mock.calls[0][0];
+ expect(row.table).toBe("code_searches");
+ expect(row.options.categories).toEqual([{ type: "developer" }]);
+ }
+ });
+
+ it("writes no row for a search without the developer category", async () => {
+ mockExecuteSearch.mockResolvedValue(
+ executeResult({ response: { web: [] } }),
+ );
+
+ for (const categories of [undefined, ["github"]]) {
+ mockLogResearchEndpoint.mockClear();
+ const req = makeReq({
+ query: "http client",
+ ...(categories ? { categories } : {}),
+ });
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ expect(res.status).toHaveBeenCalledWith(200);
+ expect(mockLogResearchEndpoint).not.toHaveBeenCalled();
+ }
+ });
+
+ it("uses the x-origin header when the body has no origin", async () => {
+ const req = makeReq(
+ { query: "http client", categories: ["developer"] },
+ { "x-origin": "mcp-server" },
+ );
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ expect(mockLogResearchEndpoint).toHaveBeenCalledTimes(1);
+ const row = mockLogResearchEndpoint.mock.calls[0][0];
+ expect(row.options.origin).toBe("mcp-server");
+ });
+
+ it("prefers the body origin over the x-origin header", async () => {
+ const req = makeReq(
+ { query: "http client", categories: ["developer"], origin: "website" },
+ { "x-origin": "mcp-server" },
+ );
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ const row = mockLogResearchEndpoint.mock.calls[0][0];
+ expect(row.options.origin).toBe("website");
+ });
+
+ it("falls back to api when neither body origin nor header is present", async () => {
+ const req = makeReq({ query: "http client", categories: ["developer"] });
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ const row = mockLogResearchEndpoint.mock.calls[0][0];
+ expect(row.options.origin).toBe("api");
+ });
+
+ it("records zero results when the developer arm returns nothing", async () => {
+ mockExecuteSearch.mockResolvedValue(
+ executeResult({ response: { web: [] }, totalResultsCount: 0 }),
+ );
+ const req = makeReq({ query: "http client", categories: ["developer"] });
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ expect(mockLogResearchEndpoint).toHaveBeenCalledTimes(1);
+ expect(mockLogResearchEndpoint.mock.calls[0][0].num_results).toBe(0);
+ });
+
+ it("keeps the response unchanged when the ledger write fails", async () => {
+ mockLogResearchEndpoint.mockRejectedValue(new Error("ledger down"));
+ const req = makeReq({
+ query: "vector database client",
+ categories: ["developer"],
+ });
+ const res = makeRes();
+
+ await searchController(req, res);
+ await flushAsync();
+
+ expect(res.status).toHaveBeenCalledWith(200);
+ const body = res.json.mock.calls[0][0];
+ expect(body.success).toBe(true);
+ expect(body.data.developer).toEqual(developerResults);
+ expect(body.creditsUsed).toBe(2);
+ });
+});
diff --git a/apps/api/src/controllers/v2/research-proxy.ts b/apps/api/src/controllers/v2/research-proxy.ts
index b4120cc5e..8e1579dc0 100644
--- a/apps/api/src/controllers/v2/research-proxy.ts
+++ b/apps/api/src/controllers/v2/research-proxy.ts
@@ -17,6 +17,7 @@ import type {
import type { RequestWithAuth } from "../v1/types";
import { wrap } from "../../routes/shared";
import { integrationSchema } from "../../utils/integration";
+import { requestOrigin } from "../../lib/request-origin";
const SEARCH_CREDITS_PER_TEN_RESULTS = 2;
const ZDR_SEARCH_CREDITS_PER_TEN_RESULTS = 10;
@@ -193,16 +194,6 @@ function resultCount(body: any): number {
return Array.isArray(body?.results) ? body.results.length : 0;
}
-function firstHeaderValue(req: Request, key: string): string | undefined {
- const value = req.headers[key];
- if (Array.isArray(value)) return value[0];
- return typeof value === "string" ? value : undefined;
-}
-
-function requestOrigin(params: ResearchQueryParams, req: Request) {
- return params.origin ?? firstHeaderValue(req, "x-origin") ?? "api";
-}
-
function creditsFor(
config: ResearchEndpointConfig,
body: any,
diff --git a/apps/api/src/controllers/v2/search.ts b/apps/api/src/controllers/v2/search.ts
index e0f70f3de..3ed88d9e4 100644
--- a/apps/api/src/controllers/v2/search.ts
+++ b/apps/api/src/controllers/v2/search.ts
@@ -14,7 +14,11 @@ import {
reserveKeylessCredits,
} from "../../lib/keyless";
import { v7 as uuidv7 } from "uuid";
-import { logSearch, logRequest } from "../../services/logging/log_job";
+import {
+ logSearch,
+ logRequest,
+ logResearchEndpoint,
+} from "../../services/logging/log_job";
import { logger as _logger } from "../../lib/logger";
import { ScrapeJobTimeoutError } from "../../lib/error";
import { z } from "zod";
@@ -36,6 +40,7 @@ import {
formatTypesOf,
} from "../../lib/key-restriction";
import { wantsDeveloperCategory } from "../../search/developer";
+import { requestOrigin } from "../../lib/request-origin";
export async function searchController(
req: RequestWithAuth<{}, SearchResponse, SearchRequest>,
@@ -67,6 +72,8 @@ export async function searchController(
let reconciledKeylessCredits = false;
try {
+ const rawOrigin =
+ typeof req.body?.origin === "string" ? req.body.origin : undefined;
req.body = searchRequestSchema.parse(req.body);
const requestedFormats = formatTypesOf(req.body.scrapeOptions?.formats);
@@ -300,6 +307,33 @@ export async function searchController(
false,
);
+ if (wantsDeveloperCategory(req.body.categories as CategoryOption[])) {
+ logResearchEndpoint({
+ table: "code_searches",
+ id: uuidv7(),
+ request_id: agentRequestId ?? jobId,
+ team_id: req.auth.team_id,
+ target: req.body.query,
+ options: {
+ origin: requestOrigin({ origin: rawOrigin }, req),
+ integration: req.body.integration ?? null,
+ api_version: "v2",
+ categories: req.body.categories,
+ via: "search_category",
+ },
+ response: null,
+ num_results: result.response.developer?.length ?? 0,
+ time_taken: timeTakenInSeconds,
+ credits_cost: 0,
+ is_successful: true,
+ zeroDataRetention,
+ }).catch(ledgerError => {
+ logger.warn("Failed to log developer category usage", {
+ error: ledgerError,
+ });
+ });
+ }
+
const totalRequestTime = new Date().getTime() - middlewareStartTime;
const controllerTime = new Date().getTime() - controllerStartTime;
diff --git a/apps/api/src/lib/request-origin.ts b/apps/api/src/lib/request-origin.ts
new file mode 100644
index 000000000..047ff25b3
--- /dev/null
+++ b/apps/api/src/lib/request-origin.ts
@@ -0,0 +1,17 @@
+import type { Request } from "express";
+
+function firstHeaderValue(
+ req: Pick<Request, "headers">,
+ key: string,
+): string | undefined {
+ const value = req.headers[key];
+ if (Array.isArray(value)) return value[0];
+ return typeof value === "string" ? value : undefined;
+}
+
+export function requestOrigin(
+ params: { origin?: string },
+ req: Pick<Request, "headers">,
+): string {
+ return params.origin ?? firstHeaderValue(req, "x-origin") ?? "api";
+}