flashbacker
Version:
Claude Code state management with session continuity and AI personas
108 lines • 3.78 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseModelFromSettings = parseModelFromSettings;
exports.getContextWindow = getContextWindow;
exports.calculateThresholds = calculateThresholds;
exports.getModelConfig = getModelConfig;
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
/**
* Parse model information from Claude Code settings
* Reads ~/.claude/settings.json to determine current model
*/
async function parseModelFromSettings() {
try {
const settingsPath = path_1.default.join(os_1.default.homedir(), '.claude', 'settings.json');
if (await fs_extra_1.default.pathExists(settingsPath)) {
const settings = JSON.parse(await fs_extra_1.default.readFile(settingsPath, 'utf8'));
const model = settings.model || 'unknown';
return { model, found: true };
}
return { model: 'unknown', found: false };
}
catch (error) {
console.error('Error parsing model from settings:', error);
return { model: 'error', found: false };
}
}
/**
* Get context window size for a given model
* Maps model names to their context window limits
*/
function getContextWindow(model) {
// Parse context window from model name patterns
if (model.includes('[1m]')) {
return 1000000; // 1M context models
}
else if (model.includes('sonnet')) {
return 200000; // Standard Sonnet models
}
else if (model.includes('gpt-4')) {
return 128000; // GPT-4 models
}
else if (model.includes('gpt-3.5')) {
return 16000; // GPT-3.5 models
}
else if (model.includes('haiku')) {
return 200000; // Claude Haiku
}
else if (model.includes('opus')) {
return 200000; // Claude Opus
}
// Conservative default for unknown models
return 200000;
}
/**
* Calculate warning and emergency thresholds based on model context window
* Returns 80% and 95% thresholds for the given model
* @param model - Model identifier string (e.g., 'sonnet[1m]', 'gpt-4')
* @returns Object containing warning (80%) and emergency (95%) token thresholds
*/
function calculateThresholds(model) {
const contextWindow = getContextWindow(model);
return {
warning: Math.floor(contextWindow * 0.8),
emergency: Math.floor(contextWindow * 0.95),
};
}
/**
* Get complete model configuration with thresholds
* Combines model detection, context window mapping, and threshold calculation
*/
async function getModelConfig() {
try {
const { model, found } = await parseModelFromSettings();
if (!found) {
// Return conservative defaults for unknown configuration
return {
model: 'unknown',
contextWindow: 200000,
warningThreshold: 160000,
emergencyThreshold: 190000,
};
}
const contextWindow = getContextWindow(model);
const thresholds = calculateThresholds(model);
return {
model,
contextWindow,
warningThreshold: thresholds.warning,
emergencyThreshold: thresholds.emergency,
};
}
catch (error) {
console.error('Error getting model configuration:', error);
// Return safe defaults on error
return {
model: 'error',
contextWindow: 200000,
warningThreshold: 160000,
emergencyThreshold: 190000,
};
}
}
//# sourceMappingURL=model-detection.js.map
;