Add xAI as chat completion source

1c52099ed6d8f964bfcc69a3571ccdb22ba10e78

Cohee <18619528+Cohee1207@users.noreply.github.com>

11 files changed, +119 -11Showing whitespace changes
public/index.html+35 -9
@@ -685,7 +685,7 @@
685685 </span>
686686 </div>
687687 </div>
688688 <div class="range-block" data-source="openai,claude,windowai,openrouter,ai21,scale,makersuite,mistralai,custom,cohere,perplexity,groq,01ai,nanogpt,deepseek,xai">
689689 <div class="range-block-title" data-i18n="Temperature">
690690 Temperature
691691 </div>
@@ -698,7 +698,7 @@
698698 </div>
699699 </div>
700700 </div>
701701 <div class="range-block" data-source="openai,openrouter,custom,cohere,perplexity,groq,mistralai,nanogpt,deepseek,xai">
702702 <div class="range-block-title" data-i18n="Frequency Penalty">
703703 Frequency Penalty
704704 </div>
@@ -711,7 +711,7 @@
711711 </div>
712712 </div>
713713 </div>
714714 <div class="range-block" data-source="openai,openrouter,custom,cohere,perplexity,groq,mistralai,nanogpt,deepseek,xai">
715715 <div class="range-block-title" data-i18n="Presence Penalty">
716716 Presence Penalty
717717 </div>
@@ -737,7 +737,7 @@
737737 </div>
738738 </div>
739739 </div>
740740 <div class="range-block" data-source="openai,claude,openrouter,ai21,scale,makersuite,mistralai,custom,cohere,perplexity,groq,01ai,nanogpt,deepseek,xai">
741741 <div class="range-block-title" data-i18n="Top P">
742742 Top P
743743 </div>
@@ -974,7 +974,7 @@
974974 </div>
975975 </div>
976976 </div>
977977 <div class="range-block" data-source="openai,openrouter,mistralai,custom,cohere,groq,nanogpt,xai">
978978 <div class="range-block-title justifyLeft" data-i18n="Seed">
979979 Seed
980980 </div>
@@ -1965,7 +1965,7 @@
19651965 </span>
19661966 </div>
19671967 </div>
19681968 <div class="range-block" data-source="openai,cohere,mistralai,custom,claude,openrouter,groq,deepseek,makersuite,ai21,xai">
19691969 <label for="openai_function_calling" class="checkbox_label flexWrap widthFreeExpand">
19701970 <input id="openai_function_calling" type="checkbox" />
19711971 <span data-i18n="Enable function calling">Enable function calling</span>
@@ -1975,7 +1975,7 @@
19751975 <span data-i18n="enable_functions_desc_3">Can be utilized by various extensions to provide additional functionality.</span>
19761976 </div>
19771977 </div>
19781978 <div class="range-block" data-source="openai,openrouter,makersuite,claude,custom,01ai,xai">
19791979 <label for="openai_image_inlining" class="checkbox_label flexWrap widthFreeExpand">
19801980 <input id="openai_image_inlining" type="checkbox" />
19811981 <span data-i18n="Send inline images">Send inline images</span>
@@ -2031,7 +2031,7 @@
20312031 </span>
20322032 </div>
20332033 </div>
20342034 <div class="range-block" data-source="deepseek,openrouter,custom,claude,xai">
20352035 <label for="openai_show_thoughts" class="checkbox_label widthFreeExpand">
20362036 <input id="openai_show_thoughts" type="checkbox" />
20372037 <span>
@@ -2045,7 +2045,7 @@
20452045 </span>
20462046 </div>
20472047 </div>
20482048 <div class="flex-container flexFlowColumn wide100p textAlignCenter marginTop10" data-source="openai,custom,claude,xai">
20492049 <div class="flex-container oneline-dropdown" title="Constrains effort on reasoning for reasoning models.&#10;Currently supported values are low, medium, and high.&#10;Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response." data-i18n="[title]Constrains effort on reasoning for reasoning models.">
20502050 <label for="openai_reasoning_effort">
20512051 <span data-i18n="Reasoning Effort">Reasoning Effort</span>
@@ -2756,6 +2756,7 @@
27562756 <option value="perplexity">Perplexity</option>
27572757 <option value="scale">Scale</option>
27582758 <option value="windowai">Window AI</option>
2759+ <option value="xai">xAI (Grok)</option>
27592760 </optgroup>
27602761 </select>
27612762 <div class="inline-drawer wide100p" data-source="openai,claude,mistralai,makersuite,deepseek">
@@ -3424,6 +3425,31 @@
34243425 <select id="model_01ai_select">
34253426 </select>
34263427 </div>
3428+ <div id="xai_form" data-source="xai">
3429+ <h4>
3430+ <a data-i18n="xAI API Key" href="https://console.x.ai/" target="_blank" rel="noopener noreferrer">
3431+ xAI API Key
3432+ </a>
3433+ </h4>
3434+ <div class="flex-container">
3435+ <input id="api_key_xai" name="api_key_xai" class="text_pole flex1" value="" type="text" autocomplete="off">
3436+ <div title="Clear your API key" data-i18n="[title]Clear your API key" class="menu_button fa-solid fa-circle-xmark clear-api-key" data-key="api_key_xai"></div>
3437+ </div>
3438+ <div data-for="api_key_01ai" class="neutral_warning" data-i18n="For privacy reasons, your API key will be hidden after you reload the page.">
3439+ For privacy reasons, your API key will be hidden after you reload the page.
3440+ </div>
3441+ <h4 data-i18n="xAI Model">xAI Model</h4>
3442+ <select id="model_xai_select">
3443+ <option value="grok-3-beta">grok-3-beta</option>
3444+ <option value="grok-3-fast-beta">grok-3-fast-beta</option>
3445+ <option value="grok-3-mini-beta">grok-3-mini-beta</option>
3446+ <option value="grok-3-mini-fast-beta">grok-3-mini-fast-beta</option>
3447+ <option value="grok-2-vision-1212">grok-2-vision-1212</option>
3448+ <option value="grok-2-1212">grok-2-1212</option>
3449+ <option value="grok-vision-beta">grok-vision-beta</option>
3450+ <option value="grok-beta">grok-beta</option>
3451+ </select>
3452+ </div>
34273453 <div id="prompt_post_porcessing_form" data-source="custom,openrouter">
34283454 <h4 data-i18n="Prompt Post-Processing">Prompt Post-Processing</h4>
34293455 <select id="custom_prompt_post_processing" class="text_pole" title="Applies additional processing to the prompt before sending it to the API." data-i18n="[title]Applies additional processing to the prompt before sending it to the API.">
public/scripts/RossAscends-mods.js+1 -0
@@ -409,6 +409,7 @@ function RA_autoconnect(PrevApi) {
409409 || (secret_state[SECRET_KEYS.ZEROONEAI] && oai_settings.chat_completion_source == chat_completion_sources.ZEROONEAI)
410410 || (secret_state[SECRET_KEYS.NANOGPT] && oai_settings.chat_completion_source == chat_completion_sources.NANOGPT)
411411 || (secret_state[SECRET_KEYS.DEEPSEEK] && oai_settings.chat_completion_source == chat_completion_sources.DEEPSEEK)
412+ || (secret_state[SECRET_KEYS.XAI] && oai_settings.chat_completion_source == chat_completion_sources.XAI)
412413 || (isValidUrl(oai_settings.custom_url) && oai_settings.chat_completion_source == chat_completion_sources.CUSTOM)
413414 ) {
414415 $('#api_button_openai').trigger('click');
public/scripts/openai.js+60 -2
@@ -184,6 +184,7 @@ export const chat_completion_sources = {
184184 ZEROONEAI: '01ai',
185185 NANOGPT: 'nanogpt',
186186 DEEPSEEK: 'deepseek',
187+ XAI: 'xai',
187188};
188189
189190const character_names_behavior = {
@@ -257,6 +258,7 @@ export const settingsToUpdate = {
257258 nanogpt_model: ['#model_nanogpt_select', 'nanogpt_model', false],
258259 deepseek_model: ['#model_deepseek_select', 'deepseek_model', false],
259260 zerooneai_model: ['#model_01ai_select', 'zerooneai_model', false],
261+ xai_model: ['#model_xai_select', 'xai_model', false],
260262 custom_model: ['#custom_model_id', 'custom_model', false],
261263 custom_url: ['#custom_api_url_text', 'custom_url', false],
262264 custom_include_body: ['#custom_include_body', 'custom_include_body', false],
@@ -345,6 +347,7 @@ const default_settings = {
345347 nanogpt_model: 'gpt-4o-mini',
346348 zerooneai_model: 'yi-large',
347349 deepseek_model: 'deepseek-chat',
350+ xai_model: 'grok-3-beta',
348351 custom_model: '',
349352 custom_url: '',
350353 custom_include_body: '',
@@ -425,6 +428,7 @@ const oai_settings = {
425428 nanogpt_model: 'gpt-4o-mini',
426429 zerooneai_model: 'yi-large',
427430 deepseek_model: 'deepseek-chat',
431+ xai_model: 'grok-3-beta',
428432 custom_model: '',
429433 custom_url: '',
430434 custom_include_body: '',
@@ -1644,6 +1648,8 @@ export function getChatCompletionModel(source = null) {
16441648 return oai_settings.nanogpt_model;
16451649 case chat_completion_sources.DEEPSEEK:
16461650 return oai_settings.deepseek_model;
1651+ case chat_completion_sources.XAI:
1652+ return oai_settings.xai_model;
16471653 default:
16481654 throw new Error(`Unknown chat completion source: ${activeSource}`);
16491655 }
@@ -1961,6 +1967,7 @@ async function sendOpenAIRequest(type, messages, signal) {
19611967 const is01AI = oai_settings.chat_completion_source == chat_completion_sources.ZEROONEAI;
19621968 const isNano = oai_settings.chat_completion_source == chat_completion_sources.NANOGPT;
19631969 const isDeepSeek = oai_settings.chat_completion_source == chat_completion_sources.DEEPSEEK;
1970+ const isXAI = oai_settings.chat_completion_source == chat_completion_sources.XAI;
19641971 const isTextCompletion = isOAI && textCompletionModels.includes(oai_settings.openai_model);
19651972 const isQuiet = type === 'quiet';
19661973 const isImpersonate = type === 'impersonate';
@@ -2033,7 +2040,7 @@ async function sendOpenAIRequest(type, messages, signal) {
20332040 }
20342041
20352042 // Add logprobs request (currently OpenAI only, max 5 on their side)
20362043 if (useLogprobs && (isOAI || isCustom || isDeepSeek || isXAI)) {
20372044 generate_data['logprobs'] = 5;
20382045 }
20392046
@@ -2152,7 +2159,7 @@ async function sendOpenAIRequest(type, messages, signal) {
21522159 }
21532160 }
21542161
21552162 if ((isOAI || isOpenRouter || isMistral || isCustom || isCohere || isNano || isXAI) && oai_settings.seed >= 0) {
21562163 generate_data['seed'] = oai_settings.seed;
21572164 }
21582165
@@ -2278,6 +2285,11 @@ export function getStreamingReply(data, state, { chatCompletionSource = null, ov
22782285 state.reasoning += (data.choices?.filter(x => x?.delta?.reasoning_content)?.[0]?.delta?.reasoning_content || '');
22792286 }
22802287 return data.choices?.[0]?.delta?.content || '';
2288+ } else if (chat_completion_source === chat_completion_sources.XAI) {
2289+ if (show_thoughts) {
2290+ state.reasoning += (data.choices?.filter(x => x?.delta?.reasoning_content)?.[0]?.delta?.reasoning_content || '');
2291+ }
2292+ return data.choices?.[0]?.delta?.content || '';
22812293 } else if (chat_completion_source === chat_completion_sources.OPENROUTER) {
22822294 if (show_thoughts) {
22832295 state.reasoning += (data.choices?.filter(x => x?.delta?.reasoning)?.[0]?.delta?.reasoning || '');
@@ -2310,6 +2322,7 @@ function parseChatCompletionLogprobs(data) {
23102322 switch (oai_settings.chat_completion_source) {
23112323 case chat_completion_sources.OPENAI:
23122324 case chat_completion_sources.DEEPSEEK:
2325+ case chat_completion_sources.XAI:
23132326 case chat_completion_sources.CUSTOM:
23142327 if (!data.choices?.length) {
23152328 return null;
@@ -3231,6 +3244,7 @@ function loadOpenAISettings(data, settings) {
32313244 oai_settings.nanogpt_model = settings.nanogpt_model ?? default_settings.nanogpt_model;
32323245 oai_settings.deepseek_model = settings.deepseek_model ?? default_settings.deepseek_model;
32333246 oai_settings.zerooneai_model = settings.zerooneai_model ?? default_settings.zerooneai_model;
3247+ oai_settings.xai_model = settings.xai_model ?? default_settings.xai_model;
32343248 oai_settings.custom_model = settings.custom_model ?? default_settings.custom_model;
32353249 oai_settings.custom_url = settings.custom_url ?? default_settings.custom_url;
32363250 oai_settings.custom_include_body = settings.custom_include_body ?? default_settings.custom_include_body;
@@ -3316,6 +3330,8 @@ function loadOpenAISettings(data, settings) {
33163330 $('#model_deepseek_select').val(oai_settings.deepseek_model);
33173331 $(`#model_deepseek_select option[value="${oai_settings.deepseek_model}"`).prop('selected', true);
33183332 $('#model_01ai_select').val(oai_settings.zerooneai_model);
3333+ $('#model_xai_select').val(oai_settings.xai_model);
3334+ $(`#model_xai_select option[value="${oai_settings.xai_model}"`).attr('selected', true);
33193335 $('#custom_model_id').val(oai_settings.custom_model);
33203336 $('#custom_api_url_text').val(oai_settings.custom_url);
33213337 $('#openai_max_context').val(oai_settings.openai_max_context);
@@ -4312,6 +4328,11 @@ async function onModelChange() {
43124328 $('#custom_model_id').val(value).trigger('input');
43134329 }
43144330
4331+ if ($(this).is('#model_xai_select')) {
4332+ console.log('XAI model changed to', value);
4333+ oai_settings.xai_model = value;
4334+ }
4335+
43154336 if (oai_settings.chat_completion_source == chat_completion_sources.SCALE) {
43164337 if (oai_settings.max_context_unlocked) {
43174338 $('#openai_max_context').attr('max', unlocked_max);
@@ -4584,6 +4605,22 @@ async function onModelChange() {
45844605 $('#temp_openai').attr('max', oai_max_temp).val(oai_settings.temp_openai).trigger('input');
45854606 }
45864607
4608+ if (oai_settings.chat_completion_source === chat_completion_sources.XAI) {
4609+ if (oai_settings.max_context_unlocked) {
4610+ $('#openai_max_context').attr('max', unlocked_max);
4611+ } else if (oai_settings.xai_model.includes('grok-2-vision')) {
4612+ $('#openai_max_context').attr('max', max_32k);
4613+ } else if (oai_settings.xai_model.includes('grok-vision')) {
4614+ $('#openai_max_context').attr('max', max_8k);
4615+ } else {
4616+ $('#openai_max_context').attr('max', max_128k);
4617+ }
4618+
4619+ oai_settings.openai_max_context = Math.min(Number($('#openai_max_context').attr('max')), oai_settings.openai_max_context);
4620+ $('#openai_max_context').val(oai_settings.openai_max_context).trigger('input');
4621+ $('#temp_openai').attr('max', oai_max_temp).val(oai_settings.temp_openai).trigger('input');
4622+ }
4623+
45874624 if (oai_settings.chat_completion_source === chat_completion_sources.COHERE) {
45884625 oai_settings.pres_pen_openai = Math.min(Math.max(0, oai_settings.pres_pen_openai), 1);
45894626 $('#pres_pen_openai').attr('max', 1).attr('min', 0).val(oai_settings.pres_pen_openai).trigger('input');
@@ -4822,6 +4859,19 @@ async function onConnectButtonClick(e) {
48224859 }
48234860 }
48244861
4862+ if (oai_settings.chat_completion_source === chat_completion_sources.XAI) {
4863+ const api_key_xai = String($('#api_key_xai').val()).trim();
4864+
4865+ if (api_key_xai.length) {
4866+ await writeSecret(SECRET_KEYS.XAI, api_key_xai);
4867+ }
4868+
4869+ if (!secret_state[SECRET_KEYS.XAI]) {
4870+ console.log('No secret key saved for XAI');
4871+ return;
4872+ }
4873+ }
4874+
48254875 startStatusLoading();
48264876 saveSettingsDebounced();
48274877 await getStatusOpen();
@@ -4878,6 +4928,9 @@ function toggleChatCompletionForms() {
48784928 else if (oai_settings.chat_completion_source == chat_completion_sources.DEEPSEEK) {
48794929 $('#model_deepseek_select').trigger('change');
48804930 }
4931+ else if (oai_settings.chat_completion_source == chat_completion_sources.XAI) {
4932+ $('#model_xai_select').trigger('change');
4933+ }
48814934 $('[data-source]').each(function () {
48824935 const validSources = $(this).data('source').split(',');
48834936 $(this).toggle(validSources.includes(oai_settings.chat_completion_source));
@@ -5014,6 +5067,8 @@ export function isImageInliningSupported() {
50145067 'pixtral-large-2411',
50155068 'c4ai-aya-vision-8b',
50165069 'c4ai-aya-vision-32b',
5070+ 'grok-2-vision',
5071+ 'grok-vision',
50175072 ];
50185073
50195074 switch (oai_settings.chat_completion_source) {
@@ -5033,6 +5088,8 @@ export function isImageInliningSupported() {
50335088 return visionSupportedModels.some(model => oai_settings.mistralai_model.includes(model));
50345089 case chat_completion_sources.COHERE:
50355090 return visionSupportedModels.some(model => oai_settings.cohere_model.includes(model));
5091+ case chat_completion_sources.XAI:
5092+ return visionSupportedModels.some(model => oai_settings.xai_model.includes(model));
50365093 default:
50375094 return false;
50385095 }
@@ -5629,6 +5686,7 @@ export function initOpenAI() {
56295686 $('#model_deepseek_select').on('change', onModelChange);
56305687 $('#model_01ai_select').on('change', onModelChange);
56315688 $('#model_custom_select').on('change', onModelChange);
5689+ $('#model_xai_select').on('change', onModelChange);
56325690 $('#settings_preset_openai').on('change', onSettingsPresetChange);
56335691 $('#new_oai_preset').on('click', onNewPresetClick);
56345692 $('#delete_oai_preset').on('click', onDeletePresetClick);
public/scripts/reasoning.js+2 -0
@@ -109,6 +109,8 @@ export function extractReasoningFromData(data, {
109109 switch (chatCompletionSource ?? oai_settings.chat_completion_source) {
110110 case chat_completion_sources.DEEPSEEK:
111111 return data?.choices?.[0]?.message?.reasoning_content ?? '';
112+ case chat_completion_sources.XAI:
113+ return data?.choices?.[0]?.message?.reasoning_content ?? '';
112114 case chat_completion_sources.OPENROUTER:
113115 return data?.choices?.[0]?.message?.reasoning ?? '';
114116 case chat_completion_sources.MAKERSUITE:
public/scripts/secrets.js+2 -0
@@ -42,6 +42,7 @@ export const SECRET_KEYS = {
4242 DEEPSEEK: 'api_key_deepseek',
4343 SERPER: 'api_key_serper',
4444 FALAI: 'api_key_falai',
45+ XAI: 'api_key_xai',
4546};
4647
4748const INPUT_MAP = {
@@ -76,6 +77,7 @@ const INPUT_MAP = {
7677 [SECRET_KEYS.NANOGPT]: '#api_key_nanogpt',
7778 [SECRET_KEYS.GENERIC]: '#api_key_generic',
7879 [SECRET_KEYS.DEEPSEEK]: '#api_key_deepseek',
80+ [SECRET_KEYS.XAI]: '#api_key_xai',
7981};
8082
8183async function clearSecret() {
public/scripts/slash-commands.js+1 -0
@@ -3942,6 +3942,7 @@ function getModelOptions(quiet) {
39423942 { id: 'model_nanogpt_select', api: 'openai', type: chat_completion_sources.NANOGPT },
39433943 { id: 'model_01ai_select', api: 'openai', type: chat_completion_sources.ZEROONEAI },
39443944 { id: 'model_deepseek_select', api: 'openai', type: chat_completion_sources.DEEPSEEK },
3945+ { id: 'model_xai_select', api: 'openai', type: chat_completion_sources.XAI },
39453946 { id: 'model_novel_select', api: 'novel', type: null },
39463947 { id: 'horde_model', api: 'koboldhorde', type: null },
39473948 ];
public/scripts/tool-calling.js+1 -0
@@ -586,6 +586,7 @@ export class ToolManager {
586586 chat_completion_sources.DEEPSEEK,
587587 chat_completion_sources.MAKERSUITE,
588588 chat_completion_sources.AI21,
589+ chat_completion_sources.XAI,
589590 ];
590591 return supportedSources.includes(oai_settings.chat_completion_source);
591592 }
src/constants.js+1 -0
@@ -176,6 +176,7 @@ export const CHAT_COMPLETION_SOURCES = {
176176 ZEROONEAI: '01ai',
177177 NANOGPT: 'nanogpt',
178178 DEEPSEEK: 'deepseek',
179+ XAI: 'xai',
179180};
180181
181182/**
src/endpoints/backends/chat-completions.js+15 -0
@@ -53,6 +53,7 @@ const API_01AI = 'https://api.lingyiwanwu.com/v1';
5353const API_AI21 = 'https://api.ai21.com/studio/v1';
5454const API_NANOGPT = 'https://nano-gpt.com/api/v1';
5555const API_DEEPSEEK = 'https://api.deepseek.com/beta';
56+const API_XAI = 'https://api.x.ai/v1';
5657
5758/**
5859 * Applies a post-processing step to the generated messages.
@@ -872,6 +873,9 @@ router.post('/status', async function (request, response_getstatus_openai) {
872873 api_url = new URL(request.body.reverse_proxy || API_DEEPSEEK.replace('/beta', ''));
873874 api_key_openai = request.body.reverse_proxy ? request.body.proxy_password : readSecret(request.user.directories, SECRET_KEYS.DEEPSEEK);
874875 headers = {};
876+ } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.XAI) {
877+ api_url = API_XAI;
878+ api_key_openai = readSecret(request.user.directories, SECRET_KEYS.XAI);
875879 } else {
876880 console.warn('This chat completion source is not supported yet.');
877881 return response_getstatus_openai.status(400).send({ error: true });
@@ -1150,6 +1154,11 @@ router.post('/generate', function (request, response) {
11501154 apiKey = readSecret(request.user.directories, SECRET_KEYS.ZEROONEAI);
11511155 headers = {};
11521156 bodyParams = {};
1157+ } else if (request.body.chat_completion_source === CHAT_COMPLETION_SOURCES.XAI) {
1158+ apiUrl = API_XAI;
1159+ apiKey = readSecret(request.user.directories, SECRET_KEYS.XAI);
1160+ headers = {};
1161+ bodyParams = {};
11531162 } else {
11541163 console.warn('This chat completion source is not supported yet.');
11551164 return response.status(400).send({ error: true });
@@ -1162,6 +1171,12 @@ router.post('/generate', function (request, response) {
11621171 }
11631172 }
11641173
1174+ if ([CHAT_COMPLETION_SOURCES.XAI].includes(request.body.chat_completion_source)) {
1175+ if (['grok-3-mini-beta', 'grok-3-mini-fast-beta'].includes(request.body.model)) {
1176+ bodyParams['reasoning_effort'] = request.body.reasoning_effort === 'high' ? 'high' : 'low';
1177+ }
1178+ }
1179+
11651180 if (!apiKey && !request.body.reverse_proxy && request.body.chat_completion_source !== CHAT_COMPLETION_SOURCES.CUSTOM) {
11661181 console.warn('OpenAI API key is missing.');
11671182 return response.status(400).send({ error: true });
src/endpoints/secrets.js+1 -0
@@ -52,6 +52,7 @@ export const SECRET_KEYS = {
5252 GENERIC: 'api_key_generic',
5353 DEEPSEEK: 'api_key_deepseek',
5454 SERPER: 'api_key_serper',
55+ XAI: 'api_key_xai',
5556};
5657
5758// These are the keys that are safe to expose, even if allowKeysExposure is false
src/prompt-converters.js+0 -0