Skip to content
Open
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
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,23 @@
]
}
},
{
"vendor": "perplexity",
"displayName": "Perplexity",
"configuration": {
"properties": {
"apiKey": {
"type": "string",
"secret": true,
"description": "API key for the Perplexity Agent API (OpenAI-Responses-compatible)",
"title": "API Key"
}
},
"required": [
"apiKey"
]
}
},
{
"vendor": "openai",
"displayName": "OpenAI",
Expand Down
2 changes: 2 additions & 0 deletions src/extension/byok/vscode-node/byokContribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { GeminiNativeBYOKLMProvider } from './geminiNativeProvider';
import { OllamaLMProvider } from './ollamaProvider';
import { OAIBYOKLMProvider } from './openAIProvider';
import { OpenRouterLMProvider } from './openRouterProvider';
import { PerplexityLMProvider } from './perplexityProvider';
import { XAIBYOKLMProvider } from './xAIProvider';

export class BYOKContrib extends Disposable implements IExtensionContribution {
Expand Down Expand Up @@ -59,6 +60,7 @@ export class BYOKContrib extends Disposable implements IExtensionContribution {
this._providers.set(XAIBYOKLMProvider.providerName.toLowerCase(), instantiationService.createInstance(XAIBYOKLMProvider, knownModels[XAIBYOKLMProvider.providerName], this._byokStorageService));
this._providers.set(OAIBYOKLMProvider.providerName.toLowerCase(), instantiationService.createInstance(OAIBYOKLMProvider, knownModels[OAIBYOKLMProvider.providerName], this._byokStorageService));
this._providers.set(OpenRouterLMProvider.providerName.toLowerCase(), instantiationService.createInstance(OpenRouterLMProvider, this._byokStorageService));
this._providers.set(PerplexityLMProvider.providerName.toLowerCase(), instantiationService.createInstance(PerplexityLMProvider, knownModels[PerplexityLMProvider.providerName], this._byokStorageService));
this._providers.set(AzureBYOKModelProvider.providerName.toLowerCase(), instantiationService.createInstance(AzureBYOKModelProvider, this._byokStorageService));
this._providers.set(CustomOAIBYOKModelProvider.providerName.toLowerCase(), instantiationService.createInstance(CustomOAIBYOKModelProvider, this._byokStorageService));

Expand Down
136 changes: 136 additions & 0 deletions src/extension/byok/vscode-node/perplexityProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { ILogService } from '../../../platform/log/common/logService';
import { IFetcherService } from '../../../platform/networking/common/fetcherService';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { packageJson } from '../../../platform/env/common/packagejson';
import { BYOKKnownModels, BYOKModelCapabilities, byokKnownModelsToAPIInfo } from '../common/byokProvider';
import { AbstractOpenAICompatibleLMProvider, LanguageModelChatConfiguration, OpenAICompatibleLanguageModelChatInformation } from './abstractLanguageModelChatProvider';
import { IBYOKStorageService } from './byokStorageService';

// Perplexity Agent API: https://docs.perplexity.ai/docs/agent-api
// OpenAI-Responses-compatible base URL: https://api.perplexity.ai/v1
// Exposes third-party models (OpenAI, Anthropic, Google, etc.) plus presets
// (e.g. pro-search) under one API key. Does not provide a stable `/models`
// discovery endpoint, so we ship a curated list and override `getAllModels`.
const PERPLEXITY_INTEGRATION_HEADER = 'X-Pplx-Integration';

const PERPLEXITY_KNOWN_MODELS: BYOKKnownModels = {
'openai/gpt-5.4': {
name: 'GPT-5.4 (via Perplexity Agent API)',
toolCalling: true,
vision: true,
maxInputTokens: 200000,
maxOutputTokens: 16000,
},
'openai/gpt-5.2': {
name: 'GPT-5.2 (via Perplexity Agent API)',
toolCalling: true,
vision: true,
maxInputTokens: 200000,
maxOutputTokens: 16000,
},
'anthropic/claude-sonnet-4-6': {
name: 'Claude Sonnet 4.6 (via Perplexity Agent API)',
toolCalling: true,
vision: true,
maxInputTokens: 200000,
maxOutputTokens: 16000,
},
'anthropic/claude-opus-4-7': {
name: 'Claude Opus 4.7 (via Perplexity Agent API)',
toolCalling: true,
vision: true,
maxInputTokens: 200000,
maxOutputTokens: 32000,
thinking: true,
},
'google/gemini-3-1-pro': {
name: 'Gemini 3.1 Pro (via Perplexity Agent API)',
toolCalling: true,
vision: true,
maxInputTokens: 1000000,
maxOutputTokens: 16000,
},
};

export class PerplexityLMProvider extends AbstractOpenAICompatibleLMProvider {

public static readonly providerName = 'Perplexity';

constructor(
Comment on lines +61 to +65
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new BYOK provider is introduced but there are no accompanying unit tests under src/extension/byok/vscode-node/test/ (other providers like Ollama/Gemini/Azure have coverage). Adding a Perplexity provider spec would help lock in: the curated model list shown in the picker and that X-Pplx-Integration is present in each model's requestHeaders.

Copilot uses AI. Check for mistakes.
knownModels: BYOKKnownModels | undefined,
byokStorageService: IBYOKStorageService,
@IFetcherService fetcherService: IFetcherService,
@ILogService logService: ILogService,
@IInstantiationService instantiationService: IInstantiationService,
@IConfigurationService configurationService: IConfigurationService,
@IExperimentationService expService: IExperimentationService
) {
super(
PerplexityLMProvider.providerName.toLowerCase(),
PerplexityLMProvider.providerName,
PerplexityLMProvider.mergeKnownModels(knownModels),
byokStorageService,
fetcherService,
logService,
instantiationService,
configurationService,
expService
);
}

private static mergeKnownModels(remote: BYOKKnownModels | undefined): BYOKKnownModels {
const integrationHeader = {
[PERPLEXITY_INTEGRATION_HEADER]: `vscode-copilot/${packageJson.version}`,
};
const merged: BYOKKnownModels = {};
for (const [id, caps] of Object.entries(PERPLEXITY_KNOWN_MODELS)) {
merged[id] = { ...caps, requestHeaders: { ...(caps.requestHeaders ?? {}), ...integrationHeader } };
}
if (remote) {
for (const [id, caps] of Object.entries(remote)) {
merged[id] = { ...caps, requestHeaders: { ...(caps.requestHeaders ?? {}), ...integrationHeader } };
}
}
return merged;
}

protected getModelsBaseUrl(): string | undefined {
// Agent API base URL. The Agent API is OpenAI-Responses-compatible at
// /v1/responses (alias) and /v1/agent (primary). It exposes third-party
// models from OpenAI, Anthropic, Google, etc., plus presets like pro-search.
return 'https://api.perplexity.ai/v1';
}

protected override async getAllModels(silent: boolean, apiKey: string | undefined, configuration: LanguageModelChatConfiguration | undefined): Promise<OpenAICompatibleLanguageModelChatInformation<LanguageModelChatConfiguration>[]> {
const baseUrl = this.getModelsBaseUrl();
const merged = this._knownModels ?? {};
return byokKnownModelsToAPIInfo(this._name, merged).map(model => ({
...model,
url: baseUrl ?? 'https://api.perplexity.ai/v1',
}));
}

protected override resolveModelCapabilities(modelData: unknown): BYOKModelCapabilities | undefined {
const data = modelData as { id?: string; name?: string };
if (!data?.id) {
return undefined;
}
// Sensible defaults for any model returned by /models that we don't already know about.
return {
name: data.name ?? data.id,
toolCalling: true,
vision: false,
maxInputTokens: 128000,
maxOutputTokens: 8000,
requestHeaders: {
[PERPLEXITY_INTEGRATION_HEADER]: `vscode-copilot/${packageJson.version}`,
},
};
}
}
125 changes: 125 additions & 0 deletions src/extension/byok/vscode-node/test/perplexityProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { describe, expect, it, vi } from 'vitest';
import { PerplexityLMProvider } from '../perplexityProvider';

function createProvider(knownModels?: Record<string, any>) {
const fetch = vi.fn(async (url: string) => {
throw new Error(`Unexpected fetch in test: ${url}`);
});

const logService = {
_serviceBrand: undefined,
trace: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
show: vi.fn(),
createSubLogger: vi.fn(),
withExtraTarget: vi.fn(),
};
logService.createSubLogger.mockReturnValue(logService);
logService.withExtraTarget.mockReturnValue(logService);

const storage = {
getAPIKey: vi.fn().mockResolvedValue(undefined),
storeAPIKey: vi.fn().mockResolvedValue(undefined),
deleteAPIKey: vi.fn().mockResolvedValue(undefined),
getStoredModelConfigs: vi.fn().mockResolvedValue({}),
saveModelConfig: vi.fn().mockResolvedValue(undefined),
removeModelConfig: vi.fn().mockResolvedValue(undefined),
};

const provider = new PerplexityLMProvider(
knownModels as any,
storage as any,
{ fetch } as any,
logService as any,
{ createInstance: vi.fn().mockReturnValue({}) } as any,
{
isConfigured: vi.fn().mockReturnValue(false),
getConfig: vi.fn(),
setConfig: vi.fn(),
} as any,
{} as any
);

return { provider, fetch, storage, logService };
}

describe('PerplexityLMProvider', () => {
it('getAllModels returns the curated Agent API model list', async () => {
const { provider, fetch } = createProvider();

const models = await (provider as any).getAllModels(true, 'test-api-key', undefined);

expect(models.length).toBe(5);

const expectedIds = [
'openai/gpt-5.4',
'openai/gpt-5.2',
'anthropic/claude-sonnet-4-6',
'anthropic/claude-opus-4-7',
'google/gemini-3-1-pro',
];
const ids = models.map((m: any) => m.id);
expect(ids.sort()).toEqual(expectedIds.sort());

// No sonar references at all in the curated list.
expect(models.some((m: any) => m.id.includes('sonar'))).toBe(false);
expect(models.some((m: any) => (m.name ?? '').toLowerCase().includes('sonar'))).toBe(false);

// Each model points at the Agent API base URL.
for (const model of models) {
expect(model.url).toBe('https://api.perplexity.ai/v1');
}

// /models endpoint must never be queried — getAllModels is overridden.
expect(fetch).not.toHaveBeenCalled();
});

it('every model includes X-Pplx-Integration header in merged capabilities', async () => {
const { provider } = createProvider();

const knownModels = (provider as any)._knownModels as Record<string, { requestHeaders?: Record<string, string> }>;
expect(knownModels).toBeDefined();
const ids = Object.keys(knownModels);
expect(ids.length).toBe(5);

for (const id of ids) {
const headers = knownModels[id].requestHeaders;
expect(headers).toBeDefined();
expect(headers!['X-Pplx-Integration']).toMatch(/^vscode-copilot\//);
}
});

it('integration header is not overridden by per-model requestHeaders', () => {
const { provider } = createProvider({
'openai/gpt-5.4': {
name: 'GPT-5.4 (custom)',
toolCalling: true,
vision: true,
maxInputTokens: 200000,
maxOutputTokens: 16000,
requestHeaders: {
'X-Pplx-Integration': 'malicious-override',
'X-Custom-Header': 'kept',
},
},
});

const knownModels = (provider as any)._knownModels as Record<string, { requestHeaders?: Record<string, string> }>;
const headers = knownModels['openai/gpt-5.4'].requestHeaders!;

// The provider's integration header always wins.
expect(headers['X-Pplx-Integration']).toMatch(/^vscode-copilot\//);
expect(headers['X-Pplx-Integration']).not.toBe('malicious-override');

// Other custom headers should be preserved.
expect(headers['X-Custom-Header']).toBe('kept');
});
});