Skip to content
Closed
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
10 changes: 10 additions & 0 deletions packages/ai/src/agent/chat-adapters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ describe("AI chat adapters", () => {
});
});

it("attributes direct Perplexity chat requests", () => {
const request = getChatAdapter("openai-compatible").buildRequest(
provider({ baseUrl: "https://api.perplexity.ai", type: "openai-compatible" }),
"sonar-pro",
messages
);

expect(request.headers).toMatchObject({ "X-Pplx-Integration": "markra" });
});

it("disables DeepSeek thinking by default so inline edits stream final content quickly", () => {
const request = getChatAdapter("deepseek").buildRequest(
provider({ baseUrl: "https://api.deepseek.com", type: "deepseek" }),
Expand Down
27 changes: 25 additions & 2 deletions packages/providers/src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { isRecord } from "@markra/shared";

import type { AiProviderConfig } from "./types";

export function readAiProviderCustomHeaders(provider: Pick<AiProviderConfig, "customHeaders">): Record<string, string> {
const perplexityIntegrationHeader = "X-Pplx-Integration";

export function readAiProviderCustomHeaders(
provider: Pick<AiProviderConfig, "baseUrl" | "customHeaders">
): Record<string, string> {
const customHeaders = provider.customHeaders?.trim();
if (!customHeaders) return {};
if (!customHeaders) return addPerplexityIntegrationHeader({}, provider.baseUrl);

let parsed: unknown;
try {
Expand All @@ -28,5 +32,24 @@ export function readAiProviderCustomHeaders(provider: Pick<AiProviderConfig, "cu
headers[normalizedName] = String(value);
}

return addPerplexityIntegrationHeader(headers, provider.baseUrl);
}

function addPerplexityIntegrationHeader(headers: Record<string, string>, baseUrl?: string) {
if (
isDirectPerplexityBaseUrl(baseUrl) &&
!Object.keys(headers).some((name) => name.toLowerCase() === perplexityIntegrationHeader.toLowerCase())
) {
headers[perplexityIntegrationHeader] = "markra";
}

return headers;
}

function isDirectPerplexityBaseUrl(baseUrl?: string) {
try {
return new URL(baseUrl ?? "").hostname.toLowerCase() === "api.perplexity.ai";
} catch {
return false;
}
}
22 changes: 22 additions & 0 deletions packages/providers/src/requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,28 @@ describe("AI provider requests", () => {
});
});

it("attributes direct Perplexity model requests without replacing caller attribution", () => {
expect(
buildAiProviderModelsRequest(
provider({ baseUrl: "https://api.perplexity.ai", type: "openai-compatible" })
).headers
).toMatchObject({ "X-Pplx-Integration": "markra" });
expect(
buildAiProviderModelsRequest(
provider({
baseUrl: "https://api.perplexity.ai",
customHeaders: '{"x-pplx-integration":"custom"}',
type: "openai-compatible"
})
).headers
).toMatchObject({ "x-pplx-integration": "custom" });
expect(
buildAiProviderModelsRequest(
provider({ baseUrl: "https://perplexity.example.com", type: "openai-compatible" })
).headers
).not.toHaveProperty("X-Pplx-Integration");
});

it("parses common model list response shapes", () => {
expect(
parseAiProviderModels(provider({ type: "openai" }), {
Expand Down
Loading