feat: add User-Agent header to API providers (#5492)

This commit is contained in:
Roomote Bot 2025-07-08 20:19:56 -07:00 committed by GitHub
parent fa60a31578
commit 9b0f3b2435
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,61 @@
// npx vitest run src/api/providers/__tests__/constants.spec.ts
import { describe, it, expect } from "vitest"
import { DEFAULT_HEADERS } from "../constants"
import { Package } from "../../../shared/package"
describe("DEFAULT_HEADERS", () => {
it("should contain all required headers", () => {
expect(DEFAULT_HEADERS).toHaveProperty("HTTP-Referer")
expect(DEFAULT_HEADERS).toHaveProperty("X-Title")
expect(DEFAULT_HEADERS).toHaveProperty("User-Agent")
})
it("should have correct HTTP-Referer value", () => {
expect(DEFAULT_HEADERS["HTTP-Referer"]).toBe("https://github.com/RooVetGit/Roo-Cline")
})
it("should have correct X-Title value", () => {
expect(DEFAULT_HEADERS["X-Title"]).toBe("Roo Code")
})
it("should have correct User-Agent format", () => {
const userAgent = DEFAULT_HEADERS["User-Agent"]
expect(userAgent).toBe(`RooCode/${Package.version}`)
// Verify it follows the tool_name/version pattern
expect(userAgent).toMatch(/^[a-zA-Z-]+\/\d+\.\d+\.\d+$/)
})
it("should have User-Agent with correct tool name", () => {
const userAgent = DEFAULT_HEADERS["User-Agent"]
expect(userAgent.startsWith("RooCode/")).toBe(true)
})
it("should have User-Agent with semantic version format", () => {
const userAgent = DEFAULT_HEADERS["User-Agent"]
const version = userAgent.split("/")[1]
// Check semantic version format (major.minor.patch)
expect(version).toMatch(/^\d+\.\d+\.\d+$/)
// Verify current version matches package version
expect(version).toBe(Package.version)
})
it("should be an object with string values", () => {
expect(typeof DEFAULT_HEADERS).toBe("object")
expect(DEFAULT_HEADERS).not.toBeNull()
Object.values(DEFAULT_HEADERS).forEach((value) => {
expect(typeof value).toBe("string")
expect(value.length).toBeGreaterThan(0)
})
})
it("should have exactly 3 headers", () => {
const headerKeys = Object.keys(DEFAULT_HEADERS)
expect(headerKeys).toHaveLength(3)
expect(headerKeys).toEqual(["HTTP-Referer", "X-Title", "User-Agent"])
})
})

View file

@ -5,6 +5,7 @@ import { ApiHandlerOptions } from "../../../shared/api"
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import { openAiModelInfoSaneDefaults } from "@roo-code/types"
import { Package } from "../../../shared/package"
const mockCreate = vitest.fn()
@ -104,6 +105,7 @@ describe("OpenAiHandler", () => {
defaultHeaders: {
"HTTP-Referer": "https://github.com/RooVetGit/Roo-Cline",
"X-Title": "Roo Code",
"User-Agent": `RooCode/${Package.version}`,
},
})
})

View file

@ -8,6 +8,7 @@ import OpenAI from "openai"
import { OpenRouterHandler } from "../openrouter"
import { ApiHandlerOptions } from "../../../shared/api"
import { Package } from "../../../shared/package"
// Mock dependencies
vitest.mock("openai")
@ -62,6 +63,7 @@ describe("OpenRouterHandler", () => {
defaultHeaders: {
"HTTP-Referer": "https://github.com/RooVetGit/Roo-Cline",
"X-Title": "Roo Code",
"User-Agent": `RooCode/${Package.version}`,
},
})
})

View file

@ -5,6 +5,7 @@ import OpenAI from "openai"
import { RequestyHandler } from "../requesty"
import { ApiHandlerOptions } from "../../../shared/api"
import { Package } from "../../../shared/package"
const mockCreate = vitest.fn()
@ -59,6 +60,7 @@ describe("RequestyHandler", () => {
defaultHeaders: {
"HTTP-Referer": "https://github.com/RooVetGit/Roo-Cline",
"X-Title": "Roo Code",
"User-Agent": `RooCode/${Package.version}`,
},
})
})

View file

@ -1,4 +1,7 @@
import { Package } from "../../shared/package"
export const DEFAULT_HEADERS = {
"HTTP-Referer": "https://github.com/RooVetGit/Roo-Cline",
"X-Title": "Roo Code",
"User-Agent": `RooCode/${Package.version}`,
}