Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/byok/vscode-node/xAIProvider.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import { IConfigurationService } from '../../../platform/configuration/common/configurationService';
6
import { ILogService } from '../../../platform/log/common/logService';
7
import { IFetcherService } from '../../../platform/networking/common/fetcherService';
8
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
9
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
10
import { BYOKKnownModels, BYOKModelCapabilities } from '../common/byokProvider';
11
import { AbstractOpenAICompatibleLMProvider } from './abstractLanguageModelChatProvider';
12
import { IBYOKStorageService } from './byokStorageService';
13
14
// https://docs.x.ai/docs/api-reference#list-language-models
15
interface XAIModelData {
16
id: string;
17
fingerprint: string;
18
created: number;
19
object: string;
20
owned_by: string;
21
input_modalities: string[];
22
output_modalities: string[];
23
prompt_text_token_price: number;
24
cached_prompt_text_token_price: number;
25
prompt_image_token_price: number;
26
completion_text_token_price: number;
27
search_price?: number;
28
version: string;
29
aliases: string[];
30
}
31
32
export class XAIBYOKLMProvider extends AbstractOpenAICompatibleLMProvider {
33
34
public static readonly providerName = 'xAI';
35
36
constructor(
37
knownModels: BYOKKnownModels,
38
byokStorageService: IBYOKStorageService,
39
@IFetcherService fetcherService: IFetcherService,
40
@ILogService logService: ILogService,
41
@IInstantiationService instantiationService: IInstantiationService,
42
@IConfigurationService configurationService: IConfigurationService,
43
@IExperimentationService expService: IExperimentationService
44
) {
45
super(
46
XAIBYOKLMProvider.providerName.toLowerCase(),
47
XAIBYOKLMProvider.providerName,
48
knownModels,
49
byokStorageService,
50
fetcherService,
51
logService,
52
instantiationService,
53
configurationService,
54
expService
55
);
56
}
57
58
protected getModelsBaseUrl(): string | undefined {
59
return 'https://api.x.ai/v1';
60
}
61
62
protected override getModelsDiscoveryUrl(modelsBaseUrl: string): string {
63
return `${modelsBaseUrl}/language-models`;
64
}
65
66
protected override resolveModelCapabilities(modelData: unknown): BYOKModelCapabilities | undefined {
67
const xaiModelData = modelData as XAIModelData;
68
// Add new model with reasonable defaults
69
let maxInputTokens;
70
let maxOutputTokens;
71
72
// Coding models and Grok 4+ models have larger context windows
73
const parsedVersion = this.parseXAIModelVersion(xaiModelData.id) ?? 0;
74
if (xaiModelData.id.startsWith('grok-code') || parsedVersion >= 4) {
75
maxInputTokens = 120000;
76
maxOutputTokens = 120000;
77
} else {
78
maxInputTokens = 80000;
79
maxOutputTokens = 30000;
80
}
81
82
return {
83
name: this.humanizeXAIModelId(xaiModelData.id),
84
toolCalling: true,
85
vision: xaiModelData.input_modalities.includes('image'),
86
maxInputTokens,
87
maxOutputTokens,
88
};
89
}
90
91
private parseXAIModelVersion(modelId: string): number | undefined {
92
const match = modelId.match(/^grok-(\d+)/);
93
return match ? parseInt(match[1], 10) : undefined;
94
}
95
96
private humanizeXAIModelId(modelId: string): string {
97
const parts = modelId.split('-').filter(p => p.length > 0);
98
return parts.map(p => {
99
if (/^\d+$/.test(p)) {
100
return p; // keep pure numbers as-is
101
}
102
return p.charAt(0).toUpperCase() + p.slice(1);
103
}).join(' ');
104
}
105
}
106
107