fix(profile): handle km compatibility for legacy kimi api users

This commit is contained in:
Tam Nhu Tran
2026-02-19 12:43:41 +07:00
parent 7efbac6c1a
commit cf8070b5f0
17 changed files with 266 additions and 54 deletions
+10 -4
View File
@@ -5,6 +5,7 @@ import * as path from 'path';
import { Settings } from '../types';
import { ValidationResult } from '../types/utils';
import { getCcsDir } from './config-manager';
import { getProfileLookupCandidates } from './profile-compat';
/**
* Extended validation result for delegation profiles
@@ -24,19 +25,24 @@ interface DelegationValidationResult extends ValidationResult {
export class DelegationValidator {
/**
* Validate a delegation profile
* @param profileName - Name of profile to validate (e.g., 'glm', 'kimi')
* @param profileName - Name of profile to validate (e.g., 'glm', 'km')
* @returns Validation result { valid: boolean, error?: string, settingsPath?: string }
*/
static validate(profileName: string): DelegationValidationResult {
const settingsPath = path.join(getCcsDir(), `${profileName}.settings.json`);
const ccsDir = getCcsDir();
const candidateSettingsPath = getProfileLookupCandidates(profileName)
.map((candidate) => path.join(ccsDir, `${candidate}.settings.json`))
.find((candidatePath) => fs.existsSync(candidatePath));
const primarySettingsPath = path.join(ccsDir, `${profileName}.settings.json`);
const settingsPath = candidateSettingsPath || primarySettingsPath;
// Check if profile directory exists
if (!fs.existsSync(settingsPath)) {
if (!candidateSettingsPath) {
return {
valid: false,
error: `Profile not found: ${profileName}`,
suggestion:
`Profile settings missing at: ${settingsPath}\n\n` +
`Profile settings missing at: ${primarySettingsPath}\n\n` +
`To set up ${profileName} profile:\n` +
` 1. Copy base settings: cp config/base-${profileName}.settings.json ~/.ccs/${profileName}.settings.json\n` +
` 2. Edit settings: Edit ~/.ccs/${profileName}.settings.json\n` +
+39
View File
@@ -0,0 +1,39 @@
/**
* Profile compatibility helpers for renamed commands/profiles.
*
* Current compatibility mappings:
* - `km` is the canonical Kimi API profile command
* - `kimi` remains as legacy API profile name in existing user configs
*/
const PROFILE_COMPAT_ALIASES: Readonly<Record<string, readonly string[]>> = Object.freeze({
km: ['kimi'],
});
/**
* Build lookup candidates for a profile.
* Order: exact input -> lowercase form (if different) -> legacy aliases.
*/
export function getProfileLookupCandidates(profileName: string): string[] {
const raw = profileName.trim();
const normalized = raw.toLowerCase();
const aliases = PROFILE_COMPAT_ALIASES[normalized] || [];
const ordered = [raw, normalized, ...aliases];
return [...new Set(ordered.filter(Boolean))];
}
/**
* Check whether a resolved profile name came from a legacy alias.
*/
export function isLegacyProfileAlias(requestedName: string, resolvedName: string): boolean {
const requestedNormalized = requestedName.trim().toLowerCase();
const resolvedNormalized = resolvedName.trim().toLowerCase();
if (requestedNormalized === resolvedNormalized) {
return false;
}
const aliases = PROFILE_COMPAT_ALIASES[requestedNormalized] || [];
return aliases.includes(resolvedNormalized);
}