Merge pull request #103 from continuedev/persist-model
Add model persistence to authentication configuration
This commit is contained in:
commit
dfd846aaf5
3 changed files with 87 additions and 9 deletions
|
|
@ -25,6 +25,7 @@ export interface AuthenticatedConfig {
|
|||
expiresAt: number;
|
||||
organizationId: string | null; // null means personal organization
|
||||
configUri?: string; // Optional config URI (file:// or slug://owner/slug)
|
||||
modelName?: string; // Name of the selected model
|
||||
}
|
||||
|
||||
// Represents configuration when using environment variable auth
|
||||
|
|
@ -32,6 +33,7 @@ export interface EnvironmentAuthConfig {
|
|||
accessToken: string;
|
||||
organizationId: null; // Environment auth always uses personal organization
|
||||
configUri?: string; // Optional config URI (file:// or slug://owner/slug)
|
||||
modelName?: string; // Name of the selected model
|
||||
}
|
||||
|
||||
// Union type representing the possible authentication states
|
||||
|
|
@ -79,6 +81,14 @@ export function getConfigUri(config: AuthConfig): string | null {
|
|||
return config.configUri || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the model name from any auth config type
|
||||
*/
|
||||
export function getModelName(config: AuthConfig): string | null {
|
||||
if (config === null) return null;
|
||||
return config.modelName || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility functions for config URI conversion
|
||||
*/
|
||||
|
|
@ -149,6 +159,7 @@ export function loadAuthConfig(): AuthConfig {
|
|||
expiresAt: data.expiresAt,
|
||||
organizationId: data.organizationId || null,
|
||||
configUri: data.configUri,
|
||||
modelName: data.modelName,
|
||||
};
|
||||
} else {
|
||||
console.warn("Invalid auth config found, ignoring.");
|
||||
|
|
@ -203,6 +214,25 @@ export function updateConfigUri(configUri: string | null): void {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the model name in the authentication configuration
|
||||
*/
|
||||
export function updateModelName(modelName: string | null): void {
|
||||
// If using CONTINUE_API_KEY environment variable, don't save anything
|
||||
if (process.env.CONTINUE_API_KEY) {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = loadAuthConfig();
|
||||
if (config && isAuthenticatedConfig(config)) {
|
||||
const updatedConfig: AuthenticatedConfig = {
|
||||
...config,
|
||||
modelName: modelName || undefined,
|
||||
};
|
||||
saveAuthConfig(updatedConfig);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy functions for backward compatibility
|
||||
*/
|
||||
|
|
@ -446,6 +476,10 @@ async function refreshToken(
|
|||
configUri: isAuthenticatedConfig(existingConfig)
|
||||
? existingConfig.configUri
|
||||
: undefined,
|
||||
// Preserve existing modelName if it exists
|
||||
modelName: isAuthenticatedConfig(existingConfig)
|
||||
? existingConfig.modelName
|
||||
: undefined,
|
||||
};
|
||||
|
||||
// Save the config
|
||||
|
|
@ -543,6 +577,7 @@ export async function ensureOrganization(
|
|||
expiresAt: authenticatedConfig.expiresAt,
|
||||
organizationId: null, // Default to personal organization
|
||||
configUri: authenticatedConfig.configUri,
|
||||
modelName: authenticatedConfig.modelName,
|
||||
};
|
||||
saveAuthConfig(updatedConfig);
|
||||
return updatedConfig;
|
||||
|
|
@ -564,6 +599,7 @@ export async function ensureOrganization(
|
|||
expiresAt: authenticatedConfig.expiresAt,
|
||||
organizationId: null,
|
||||
configUri: authenticatedConfig.configUri,
|
||||
modelName: authenticatedConfig.modelName,
|
||||
};
|
||||
saveAuthConfig(updatedConfig);
|
||||
return updatedConfig;
|
||||
|
|
@ -581,6 +617,7 @@ export async function ensureOrganization(
|
|||
expiresAt: authenticatedConfig.expiresAt,
|
||||
organizationId: selectedOrgId,
|
||||
configUri: authenticatedConfig.configUri,
|
||||
modelName: authenticatedConfig.modelName,
|
||||
};
|
||||
|
||||
saveAuthConfig(updatedConfig);
|
||||
|
|
@ -599,6 +636,7 @@ export async function ensureOrganization(
|
|||
expiresAt: authenticatedConfig.expiresAt,
|
||||
organizationId: null,
|
||||
configUri: authenticatedConfig.configUri,
|
||||
modelName: authenticatedConfig.modelName,
|
||||
};
|
||||
saveAuthConfig(updatedConfig);
|
||||
return updatedConfig;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml";
|
||||
import { BaseLlmApi, constructLlmApi, LLMConfig } from "@continuedev/openai-adapters";
|
||||
import { getLlmApi } from '../config.js';
|
||||
import { AuthConfig, getAccessToken, getOrganizationId } from '../auth/workos.js';
|
||||
import { AuthConfig, getAccessToken, getOrganizationId, getModelName } from '../auth/workos.js';
|
||||
import logger from '../util/logger.js';
|
||||
import { ModelServiceState } from './types.js';
|
||||
|
||||
|
|
@ -34,16 +34,34 @@ export class ModelService {
|
|||
model && model.roles?.includes("chat")
|
||||
) || []) as ModelConfig[];
|
||||
|
||||
const [llmApi, model] = getLlmApi(assistant, authConfig);
|
||||
|
||||
this.currentState = {
|
||||
llmApi,
|
||||
model
|
||||
};
|
||||
// Check if we have a persisted model name and use it if valid
|
||||
const persistedModelName = getModelName(authConfig);
|
||||
if (persistedModelName) {
|
||||
const modelIndex = this.getModelIndexByName(persistedModelName);
|
||||
if (modelIndex !== -1) {
|
||||
// Use the persisted model
|
||||
const state = await this.switchModel(modelIndex);
|
||||
this.currentState = state;
|
||||
} else {
|
||||
// Model name not found, use default model selection
|
||||
const [llmApi, model] = getLlmApi(assistant, authConfig);
|
||||
this.currentState = {
|
||||
llmApi,
|
||||
model
|
||||
};
|
||||
}
|
||||
} else {
|
||||
// Use default model selection
|
||||
const [llmApi, model] = getLlmApi(assistant, authConfig);
|
||||
this.currentState = {
|
||||
llmApi,
|
||||
model
|
||||
};
|
||||
}
|
||||
|
||||
logger.debug('ModelService initialized successfully', {
|
||||
modelProvider: model.provider,
|
||||
modelName: (model as any).name || 'unnamed',
|
||||
modelProvider: this.currentState.model?.provider,
|
||||
modelName: (this.currentState.model as any)?.name || 'unnamed',
|
||||
availableModels: this.availableModels.length
|
||||
});
|
||||
|
||||
|
|
@ -209,4 +227,20 @@ export class ModelService {
|
|||
(model as any).name === (this.currentState.model as any).name
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get model index by name and provider
|
||||
*/
|
||||
getModelIndexByName(modelName: string, provider?: string): number {
|
||||
return this.availableModels.findIndex(model => {
|
||||
const name = (model as any).name || (model as any).model;
|
||||
const nameMatches = name === modelName;
|
||||
|
||||
if (provider) {
|
||||
return nameMatches && model.provider === provider;
|
||||
}
|
||||
|
||||
return nameMatches;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import { useState } from "react";
|
||||
import { services, serviceContainer, SERVICE_NAMES } from "../../services/index.js";
|
||||
import { ModelServiceState } from "../../services/types.js";
|
||||
import { updateModelName } from "../../auth/workos.js";
|
||||
|
||||
interface ModelOption {
|
||||
id: string;
|
||||
|
|
@ -35,6 +36,11 @@ export function useModelSelector({
|
|||
const currentState = services.model.getState();
|
||||
serviceContainer.set<ModelServiceState>(SERVICE_NAMES.MODEL, currentState);
|
||||
|
||||
// Persist the model choice using the actual model name
|
||||
if (modelInfo?.name) {
|
||||
updateModelName(modelInfo.name);
|
||||
}
|
||||
|
||||
onMessage({
|
||||
role: "system",
|
||||
content: `Switched to model: ${modelInfo?.provider}/${modelInfo?.name}`,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue