inference-server
Version:
Libraries and server to build AI applications. Adapters to various native bindings allowing local inference. Integrate it with your application, or use as a microservice.
96 lines • 3.42 kB
JavaScript
import prettyBytes from 'pretty-bytes';
import chalk from 'chalk';
import path from 'node:path';
import { getCacheDirPath } from '../lib/getCacheDirPath.js';
import { indexModelCache } from '../cli/lib/indexModelCache.js';
import { loadConfig } from '../cli/lib/loadConfig.js';
import { renderListView } from '../cli/lib/renderListView.js';
import { renderTreeView } from '../cli/lib/renderTreeView.js';
async function listModels({ showFiles = false, json = false, list = false, showAll, configPath, }) {
const config = await loadConfig(configPath);
let modelsCachePath = getCacheDirPath('models');
if (config?.options.cachePath) {
modelsCachePath = path.join(config.options.cachePath, 'models');
}
try {
// look up cached files on disk
const cacheInfo = await indexModelCache(modelsCachePath, {
includeFiles: showFiles,
includeUnused: showAll ?? (config ? false : true),
usedModels: config?.options.models,
});
const totalSize = cacheInfo.fileTree.reduce((acc, model) => acc + model.size, 0);
if (json) {
console.log(JSON.stringify(cacheInfo.fileTree, null, 2));
}
else {
if (configPath) {
console.log(chalk.cyanBright(`Loaded config from: ${configPath}`));
}
console.log(chalk.cyan(`Models cache path: ${modelsCachePath}`));
console.log(chalk.cyan(`Total cache size: ${prettyBytes(totalSize)}`));
console.log(chalk.green(`\n${cacheInfo.fileCount} files:`));
// Render either as tree or list
const rendered = list ? renderListView(cacheInfo.fileTree) : renderTreeView(cacheInfo.fileTree);
console.log(rendered.join('\n'));
}
}
catch (error) {
if (error.code === 'ENOENT') {
if (json) {
console.log(JSON.stringify({ error: `No cached models found in ${modelsCachePath}` }));
}
else {
console.log(chalk.yellow(`\nNo cached models found in ${modelsCachePath}`));
}
}
else {
if (json) {
console.log(JSON.stringify({ error: error.message }));
}
else {
console.error(chalk.red(`Error: ${error.message}`));
}
}
}
}
export const listCommand = {
command: 'list [configPath]',
aliases: ['ls', 'dir'],
describe: 'List stored models',
builder: {
all: {
alias: 'a',
type: 'boolean',
description: 'Show all models in the cache',
},
files: {
alias: 'f',
type: 'boolean',
description: 'Include individual files',
default: false,
},
json: {
alias: 'j',
type: 'boolean',
description: 'Output as JSON',
default: false,
},
list: {
alias: 'l',
type: 'boolean',
description: 'Show as flat list instead of tree',
default: false,
},
},
handler: async (argv) => {
await listModels({
configPath: argv.configPath,
showFiles: argv.files,
json: argv.json,
list: argv.list,
showAll: argv.all,
});
},
};
//# sourceMappingURL=listCommand.js.map