feat(copilot): finish configuration center
This commit is contained in:
@@ -1,74 +1,144 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import CaptainPreferencesAPI from 'dashboard/api/captain/preferences';
|
||||
import CopilotConfigAPI from 'dashboard/api/copilotConfig';
|
||||
|
||||
const applyAccountPayload = (store, payload = {}) => {
|
||||
store.providers = payload.providers || {};
|
||||
store.models = payload.models || {};
|
||||
store.features = payload.features || {};
|
||||
store.behavior = payload.behavior || {};
|
||||
store.providerConfig = payload.provider_config || {};
|
||||
};
|
||||
|
||||
export const useCaptainConfigStore = defineStore('captainConfig', {
|
||||
state: () => ({
|
||||
providers: {},
|
||||
models: {},
|
||||
features: {},
|
||||
providerConfig: {},
|
||||
uiFlags: {
|
||||
isFetching: false,
|
||||
},
|
||||
}),
|
||||
state: () => ({
|
||||
providers: {},
|
||||
models: {},
|
||||
features: {},
|
||||
behavior: {},
|
||||
providerConfig: {},
|
||||
providerTestResult: null,
|
||||
embeddingReindexStatus: {},
|
||||
uiFlags: {
|
||||
isFetching: false,
|
||||
isSavingAccount: false,
|
||||
isSavingProvider: false,
|
||||
isTestingProvider: false,
|
||||
isStartingReindex: false,
|
||||
},
|
||||
}),
|
||||
|
||||
getters: {
|
||||
getProviders: state => state.providers,
|
||||
getModels: state => state.models,
|
||||
getFeatures: state => state.features,
|
||||
getUIFlags: state => state.uiFlags,
|
||||
getModelsForFeature: state => featureKey => {
|
||||
const feature = state.features[featureKey];
|
||||
const models = feature?.models || [];
|
||||
getters: {
|
||||
getProviders: state => state.providers,
|
||||
getModels: state => state.models,
|
||||
getFeatures: state => state.features,
|
||||
getUIFlags: state => state.uiFlags,
|
||||
getModelsForFeature: state => featureKey => {
|
||||
const feature = state.features[featureKey];
|
||||
const models = feature?.models || [];
|
||||
|
||||
const providerOrder = { openai: 0, anthropic: 1, gemini: 2 };
|
||||
const providerOrder = { openai: 0, anthropic: 1, gemini: 2 };
|
||||
|
||||
return [...models].sort((a, b) => {
|
||||
// Move coming_soon items to the end
|
||||
if (a.coming_soon && !b.coming_soon) return 1;
|
||||
if (!a.coming_soon && b.coming_soon) return -1;
|
||||
return [...models].sort((a, b) => {
|
||||
// Move coming_soon items to the end
|
||||
if (a.coming_soon && !b.coming_soon) return 1;
|
||||
if (!a.coming_soon && b.coming_soon) return -1;
|
||||
|
||||
// Sort by provider
|
||||
const providerA = providerOrder[a.provider] ?? 999;
|
||||
const providerB = providerOrder[b.provider] ?? 999;
|
||||
if (providerA !== providerB) return providerA - providerB;
|
||||
// Sort by provider
|
||||
const providerA = providerOrder[a.provider] ?? 999;
|
||||
const providerB = providerOrder[b.provider] ?? 999;
|
||||
if (providerA !== providerB) return providerA - providerB;
|
||||
|
||||
// Sort by credit_multiplier (highest first)
|
||||
return (b.credit_multiplier || 0) - (a.credit_multiplier || 0);
|
||||
});
|
||||
},
|
||||
getDefaultModelForFeature: state => featureKey => {
|
||||
const feature = state.features[featureKey];
|
||||
return feature?.default || null;
|
||||
},
|
||||
getSelectedModelForFeature: state => featureKey => {
|
||||
const feature = state.features[featureKey];
|
||||
return feature?.selected || feature?.default || null;
|
||||
},
|
||||
},
|
||||
// Sort by credit_multiplier (highest first)
|
||||
return (b.credit_multiplier || 0) - (a.credit_multiplier || 0);
|
||||
});
|
||||
},
|
||||
getDefaultModelForFeature: state => featureKey => {
|
||||
const feature = state.features[featureKey];
|
||||
return feature?.default || null;
|
||||
},
|
||||
getSelectedModelForFeature: state => featureKey => {
|
||||
const feature = state.features[featureKey];
|
||||
return feature?.selected || feature?.default || null;
|
||||
},
|
||||
},
|
||||
|
||||
actions: {
|
||||
async fetch() {
|
||||
this.uiFlags.isFetching = true;
|
||||
try {
|
||||
const response = await CaptainPreferencesAPI.get();
|
||||
this.providers = response.data.providers || {};
|
||||
this.models = response.data.models || {};
|
||||
this.features = response.data.features || {};
|
||||
this.providerConfig = response.data.provider_config || {};
|
||||
} catch (error) {
|
||||
// Ignore error
|
||||
} finally {
|
||||
this.uiFlags.isFetching = false;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
async fetch({ includePlatform = false } = {}) {
|
||||
this.uiFlags.isFetching = true;
|
||||
try {
|
||||
const accountResponse =
|
||||
await CopilotConfigAPI.getAccountConfig();
|
||||
applyAccountPayload(this, accountResponse.data);
|
||||
if (includePlatform) {
|
||||
const [platformResponse, reindexResponse] =
|
||||
await Promise.all([
|
||||
CopilotConfigAPI.getPlatformConfig(),
|
||||
CopilotConfigAPI.getEmbeddingReindexStatus(),
|
||||
]);
|
||||
this.providerConfig = platformResponse.data || {};
|
||||
this.embeddingReindexStatus = reindexResponse.data || {};
|
||||
}
|
||||
} finally {
|
||||
this.uiFlags.isFetching = false;
|
||||
}
|
||||
},
|
||||
|
||||
async updatePreferences(data) {
|
||||
const response = await CaptainPreferencesAPI.updatePreferences(data);
|
||||
this.providers = response.data.providers || {};
|
||||
this.models = response.data.models || {};
|
||||
this.features = response.data.features || {};
|
||||
this.providerConfig = response.data.provider_config || {};
|
||||
},
|
||||
},
|
||||
async updateAccountConfig(data) {
|
||||
this.uiFlags.isSavingAccount = true;
|
||||
try {
|
||||
const response =
|
||||
await CopilotConfigAPI.updateAccountConfig(data);
|
||||
applyAccountPayload(this, response.data);
|
||||
return response.data;
|
||||
} finally {
|
||||
this.uiFlags.isSavingAccount = false;
|
||||
}
|
||||
},
|
||||
|
||||
async updatePreferences(data) {
|
||||
return this.updateAccountConfig(data);
|
||||
},
|
||||
|
||||
async updatePlatformConfig(data) {
|
||||
this.uiFlags.isSavingProvider = true;
|
||||
try {
|
||||
const response =
|
||||
await CopilotConfigAPI.updatePlatformConfig(data);
|
||||
this.providerConfig = response.data || {};
|
||||
this.providerTestResult = null;
|
||||
return response.data;
|
||||
} finally {
|
||||
this.uiFlags.isSavingProvider = false;
|
||||
}
|
||||
},
|
||||
|
||||
async testPlatformConfig(data) {
|
||||
this.uiFlags.isTestingProvider = true;
|
||||
try {
|
||||
const response =
|
||||
await CopilotConfigAPI.testPlatformConfig(data);
|
||||
this.providerTestResult = response.data || null;
|
||||
return response.data;
|
||||
} finally {
|
||||
this.uiFlags.isTestingProvider = false;
|
||||
}
|
||||
},
|
||||
|
||||
async fetchEmbeddingReindexStatus() {
|
||||
const response = await CopilotConfigAPI.getEmbeddingReindexStatus();
|
||||
this.embeddingReindexStatus = response.data || {};
|
||||
return response.data;
|
||||
},
|
||||
|
||||
async startEmbeddingReindex() {
|
||||
this.uiFlags.isStartingReindex = true;
|
||||
try {
|
||||
const response = await CopilotConfigAPI.startEmbeddingReindex();
|
||||
this.embeddingReindexStatus = response.data || {};
|
||||
return response.data;
|
||||
} finally {
|
||||
this.uiFlags.isStartingReindex = false;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user