Moonshot: Load models list from endpoint

09b8d59f4d7dced5abfdbadbe048a5496156d8eb

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

1 files changed, +31 -0Ignore whitespace
public/scripts/openai.js+31 -0
@@ -1991,6 +1991,20 @@ function saveModelList(data) {
19911991
19921992 $('#model_xai_select').val(oai_settings.xai_model).trigger('change');
19931993 }
1994+
1995+ if (oai_settings.chat_completion_source == chat_completion_sources.MOONSHOT) {
1996+ $('#model_moonshot_select').empty();
1997+ model_list.forEach((model) => {
1998+ $('#model_moonshot_select').append(new Option(model.id, model.id));
1999+ });
2000+
2001+ const selectedModel = model_list.find(model => model.id === oai_settings.moonshot_model);
2002+ if (model_list.length > 0 && (!selectedModel || !oai_settings.moonshot_model)) {
2003+ oai_settings.moonshot_model = model_list[0].id;
2004+ }
2005+
2006+ $('#model_moonshot_select').val(oai_settings.moonshot_model).trigger('change');
2007+ }
19942008}
19952009
19962010function appendOpenRouterOptions(model_list, groupModels = false, sort = false) {
@@ -4746,11 +4760,24 @@ function getZaiMaxContext(model, isUnlocked) {
47464760 return Object.entries(contextMap).find(([key]) => model.includes(key))?.[1] || max_128k;
47474761}
47484762
4763+/**
4764+ * Get the maximum context size for the Moonshot model
4765+ * @param {string} model Model identifier
4766+ * @param {boolean} isUnlocked If context limits are unlocked
4767+ * @returns {number} Maximum context size in tokens
4768+ */
47494769function getMoonshotMaxContext(model, isUnlocked) {
47504770 if (isUnlocked) {
47514771 return unlocked_max;
47524772 }
47534773
4774+ if (Array.isArray(model_list) && model_list.length > 0) {
4775+ const modelInfo = model_list.find((record) => record.id === model);
4776+ if (modelInfo?.context_length) {
4777+ return modelInfo.context_length;
4778+ }
4779+ }
4780+
47544781 const contextMap = {
47554782 'moonshot-v1-8k': max_8k,
47564783 'moonshot-v1-32k': max_32k,
@@ -4983,6 +5010,10 @@ async function onModelChange() {
49835010 }
49845011
49855012 if (value && $(this).is('#model_moonshot_select')) {
5013+ if (!value) {
5014+ console.debug('Null Moonshot model selected. Ignoring.');
5015+ return;
5016+ }
49865017 console.log('Moonshot model changed to', value);
49875018 oai_settings.moonshot_model = value;
49885019 }