@light-merlin-dark/tok
Version:
Fast token estimation and cost calculation for enterprise LLMs with CLI and MCP support
134 lines (127 loc) • 4.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.priceSetCommand = exports.priceListCommand = void 0;
const core_1 = require("../shared/core");
const common_1 = require("../common");
const errors_1 = require("../shared/errors");
const index_1 = require("../../index");
const config_1 = require("../utils/config");
// Shared price table instance
const prices = new index_1.PriceTable();
exports.priceListCommand = (0, core_1.createCommand)({
description: 'List all model prices',
help: `
Usage: tok price list [options]
Description:
Lists all available model prices per million tokens.
Options:
--format <format> Output format: json, table, human (default: human)
Examples:
tok price list
tok price list --format json
tok price list --format table
`,
options: [
{
flag: 'format',
description: 'Output format',
type: 'string',
default: 'human'
}
],
async execute(args, options) {
const opts = options;
const allPrices = prices.list();
if (opts.format === 'json') {
const output = {};
allPrices.forEach((price, model) => {
output[model] = price;
});
(0, common_1.formatJson)(output);
}
else if (opts.format === 'table') {
const rows = Array.from(allPrices.entries()).map(([model, price]) => ({
model,
prompt: `$${price.prompt}/M`,
completion: `$${price.completion}/M`
}));
(0, common_1.formatTable)(rows);
}
else {
common_1.logger.info('Model Pricing (per million tokens)');
console.log('─'.repeat(50));
allPrices.forEach((price, model) => {
console.log(`${model.padEnd(20)} Prompt: $${price.prompt.toString().padEnd(6)} | Completion: $${price.completion}`);
});
console.log('');
}
return { success: true };
}
});
exports.priceSetCommand = (0, core_1.createCommand)({
description: 'Set price for a model',
help: `
Usage: tok price set <model> [options]
Description:
Sets custom pricing for a model. Prices are per million tokens.
Arguments:
model Model name to set pricing for
Options:
-p, --prompt <price> Prompt price per million tokens
-c, --completion <price> Completion price per million tokens
Examples:
tok price set gpt-4-turbo --prompt 10 --completion 30
tok price set custom-model -p 5.00 -c 15.00
`,
arguments: [
{
name: 'model',
description: 'Model name',
required: true
}
],
options: [
{
flag: 'p|prompt',
description: 'Prompt price per million tokens',
type: 'string'
},
{
flag: 'c|completion',
description: 'Completion price per million tokens',
type: 'string'
}
],
async execute(args, options) {
const opts = options;
const model = args[0];
if (!model) {
throw (0, errors_1.createCommandError)(errors_1.ErrorCode.INVALID_ARGUMENT, 'Please provide a model name');
}
const promptPrice = opts.prompt || opts.p;
const completionPrice = opts.completion || opts.c;
if (!promptPrice || !completionPrice) {
throw (0, errors_1.createCommandError)(errors_1.ErrorCode.INVALID_ARGUMENT, 'Please provide both --prompt and --completion prices');
}
const price = {
prompt: parseFloat(promptPrice),
completion: parseFloat(completionPrice)
};
if (isNaN(price.prompt) || isNaN(price.completion)) {
throw (0, errors_1.createCommandError)(errors_1.ErrorCode.INVALID_ARGUMENT, 'Prices must be valid numbers');
}
// Update price table
prices.set(model, price);
// Save to config
const config = (0, config_1.getConfig)();
if (!config.prices)
config.prices = {};
config.prices[model] = price;
(0, config_1.saveConfig)(config);
common_1.logger.success(`✓ Price set for ${model}`);
console.log(` Prompt: $${price.prompt}/M tokens`);
console.log(` Completion: $${price.completion}/M tokens`);
return { success: true, data: { model, price } };
}
});
//# sourceMappingURL=price.js.map