@yogesh0333/yogiway
Version:
YOGIWAY Format - Ultra-compact, nested-aware data format for LLM prompts. Handles deeply nested JSON efficiently, 10-15% more efficient than TOON.
182 lines (176 loc) • 5.99 kB
JavaScript
;
/**
* YOGIWAY Format CLI Tool
* Convert between JSON and YOGIWAY format
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const index_1 = require("./index");
function main() {
const args = process.argv.slice(2);
const options = {};
// Parse arguments
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '-o' || arg === '--output') {
options.output = args[++i];
}
else if (arg === '--stats' || arg === '-s') {
options.stats = true;
}
else if (arg === '--tabs' || arg === '-t') {
options.tabs = true;
}
else if (arg === '--types') {
options.types = true;
}
else if (arg === '--help' || arg === '-h') {
printHelp();
process.exit(0);
}
else if (!arg.startsWith('-') && !options.input) {
options.input = arg;
}
}
// Read input
let inputData;
if (options.input) {
if (!fs.existsSync(options.input)) {
console.error(`Error: File not found: ${options.input}`);
process.exit(1);
}
inputData = fs.readFileSync(options.input, 'utf-8');
}
else {
// Read from stdin
try {
inputData = fs.readFileSync(0, 'utf-8');
}
catch (e) {
console.error('Error: No input provided. Use a file or pipe data.');
process.exit(1);
}
}
// Detect format
const isYogiway = options.input?.endsWith('.yogiway') ||
options.input?.endsWith('.mini') ||
(!options.input && inputData.trim().match(/^[a-zA-Z_]*\[\d+\][a-z]+/));
const isJson = options.input?.endsWith('.json') ||
(!options.input && (inputData.trim().startsWith('{') || inputData.trim().startsWith('[')));
let result;
let stats = null;
if (isYogiway) {
// Decode YOGIWAY to JSON
const decoded = (0, index_1.decode)(inputData);
result = JSON.stringify(decoded, null, 2);
if (options.stats) {
const inputTokens = estimateTokens(inputData);
const outputTokens = estimateTokens(result);
stats = {
input: inputTokens,
output: outputTokens,
reduction: ((inputTokens - outputTokens) / inputTokens) * 100,
};
}
}
else if (isJson) {
// Encode JSON to YOGIWAY
const data = JSON.parse(inputData);
result = (0, index_1.encode)(data, {
useTabs: options.tabs !== false,
includeTypes: options.types || false,
flattenNested: true,
});
if (options.stats) {
const inputTokens = estimateTokens(inputData);
const outputTokens = estimateTokens(result);
stats = {
input: inputTokens,
output: outputTokens,
reduction: ((inputTokens - outputTokens) / inputTokens) * 100,
};
}
}
else {
console.error('Error: Could not detect input format. Use .json or .yogiway extension.');
process.exit(1);
}
// Output result
if (options.output) {
fs.writeFileSync(options.output, result, 'utf-8');
}
else {
process.stdout.write(result);
}
// Print stats
if (options.stats && stats) {
console.error(`\nToken Statistics:`);
console.error(` Input: ${stats.input.toLocaleString()} tokens`);
console.error(` Output: ${stats.output.toLocaleString()} tokens`);
console.error(` Reduction: ${stats.reduction.toFixed(1)}%`);
}
}
function estimateTokens(text) {
// Rough estimation: ~4 characters per token
return Math.ceil(text.length / 4);
}
function printHelp() {
console.log(`
YOGIWAY Format CLI - Ultra-compact, nested-aware data format for LLM prompts
Usage:
yogiway [input] [options]
Options:
-o, --output <file> Output file (default: stdout)
-s, --stats Show token statistics
-t, --tabs Use tab delimiters (default: true)
--types Include type hints in output
-h, --help Show this help message
Examples:
# Convert JSON to YOGIWAY
yogiway data.json -o data.yogiway
# Convert YOGIWAY to JSON
yogiway data.yogiway -o data.json
# Pipe from stdin
cat data.json | yogiway
# Show token savings
yogiway data.json --stats
`);
}
if (require.main === module) {
main();
}
//# sourceMappingURL=cli.js.map