145 lines
4.0 KiB
JavaScript
145 lines
4.0 KiB
JavaScript
import { defineStore } from 'pinia';
|
|
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: {},
|
|
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 || [];
|
|
|
|
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;
|
|
|
|
// 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;
|
|
},
|
|
},
|
|
|
|
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 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;
|
|
}
|
|
},
|
|
},
|
|
});
|