Skip to content

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

feat(api): accept input aliases for the developer search category

Karan Lokchandaniebfdf459e911
diff --git a/apps/api/src/__tests__/snips/v2/types-validation.test.ts b/apps/api/src/__tests__/snips/v2/types-validation.test.ts
index 05dfe895e..fcdda5a63 100644
--- a/apps/api/src/__tests__/snips/v2/types-validation.test.ts
+++ b/apps/api/src/__tests__/snips/v2/types-validation.test.ts
@@ -1046,6 +1046,75 @@ describe("V2 Types Validation", () => {
).toThrow();
});
+ it("should normalize every developer category alias to developer", () => {
+ const aliases = [
+ "repo",
+ "code",
+ "developer",
+ "docs",
+ "devdex",
+ "repo_search",
+ "developer_index",
+ ];
+
+ for (const alias of aliases) {
+ expect(
+ searchRequestSchema.parse({ query: "test", categories: [alias] })
+ .categories,
+ ).toEqual([{ type: "developer" }]);
+
+ expect(
+ searchRequestSchema.parse({
+ query: "test",
+ categories: [{ type: alias }],
+ }).categories,
+ ).toEqual([{ type: "developer" }]);
+ }
+ });
+
+ it("should deduplicate developer aliases into one developer category", () => {
+ expect(
+ searchRequestSchema.parse({
+ query: "test",
+ categories: ["code", "developer"],
+ }).categories,
+ ).toEqual([{ type: "developer" }]);
+
+ expect(
+ searchRequestSchema.parse({
+ query: "test",
+ categories: [{ type: "repo_search" }, { type: "developer" }],
+ }).categories,
+ ).toEqual([{ type: "developer" }]);
+
+ expect(
+ searchRequestSchema.parse({
+ query: "test",
+ categories: ["docs", "github", "developer_index"],
+ }).categories,
+ ).toEqual([{ type: "developer" }, { type: "github" }]);
+ });
+
+ it("should reject developer alias params and unknown categories", () => {
+ expect(() =>
+ searchRequestSchema.parse({
+ query: "test",
+ categories: [{ type: "code", repos: ["firecrawl/firecrawl"] }],
+ }),
+ ).toThrow();
+
+ expect(() =>
+ searchRequestSchema.parse({ query: "test", categories: ["bogus"] }),
+ ).toThrow();
+
+ expect(() =>
+ searchRequestSchema.parse({
+ query: "test",
+ categories: [{ type: "bogus" }],
+ }),
+ ).toThrow();
+ });
+
it("should accept search request with advanced categories format", () => {
const input: SearchRequestInput = {
query: "test",
diff --git a/apps/api/src/controllers/v2/types.ts b/apps/api/src/controllers/v2/types.ts
index 75266121d..948ecd0a7 100644
--- a/apps/api/src/controllers/v2/types.ts
+++ b/apps/api/src/controllers/v2/types.ts
@@ -1913,6 +1913,49 @@ const developerCategoryOptions = z.strictObject({
type: z.literal("developer"),
});
+const developerCategoryAliases = new Set([
+ "repo",
+ "code",
+ "developer",
+ "docs",
+ "devdex",
+ "repo_search",
+ "developer_index",
+]);
+
+function isDeveloperCategoryAlias(value: unknown): value is string {
+ return typeof value === "string" && developerCategoryAliases.has(value);
+}
+
+function normalizeDeveloperCategoryAliases(value: unknown): unknown {
+ if (!Array.isArray(value)) return value;
+ let seenString = false;
+ let seenObject = false;
+ const normalized: unknown[] = [];
+ for (const category of value) {
+ if (isDeveloperCategoryAlias(category)) {
+ if (seenString) continue;
+ seenString = true;
+ normalized.push("developer");
+ } else if (
+ typeof category === "object" &&
+ category !== null &&
+ isDeveloperCategoryAlias((category as { type?: unknown }).type)
+ ) {
+ if (Object.keys(category).length === 1) {
+ if (seenObject) continue;
+ seenObject = true;
+ normalized.push({ type: "developer" });
+ } else {
+ normalized.push({ ...category, type: "developer" });
+ }
+ } else {
+ normalized.push(category);
+ }
+ }
+ return normalized;
+}
+
const searchDomainSchema = z
.string()
.trim()
@@ -1947,19 +1990,20 @@ export const searchRequestSchema = z
.optional()
.prefault(["web"]),
categories: z
- .union([
- // Array of strings (simple format)
- z.array(z.enum(["github", "research", "pdf", "developer"])),
- // Array of objects (advanced format)
- z.array(
- z.union([
- githubCategoryOptions,
- researchCategoryOptions,
- pdfCategoryOptions,
- developerCategoryOptions,
- ]),
- ),
- ])
+ .preprocess(
+ normalizeDeveloperCategoryAliases,
+ z.union([
+ z.array(z.enum(["github", "research", "pdf", "developer"])),
+ z.array(
+ z.union([
+ githubCategoryOptions,
+ researchCategoryOptions,
+ pdfCategoryOptions,
+ developerCategoryOptions,
+ ]),
+ ),
+ ]),
+ )
.optional(),
includeDomains: z.array(searchDomainSchema).optional(),
excludeDomains: z.array(searchDomainSchema).optional(),