@ace-sdk/cli
Version:
ACE CLI - Command-line tool for intelligent pattern learning and playbook management
140 lines • 5.02 kB
JavaScript
/**
* Delta command - apply incremental pattern updates
*/
import { readFileSync } from 'fs';
import { globalOptions } from '../cli.js';
import { createContext } from '../types/config.js';
import { ACEServerClient } from '../services/server-client.js';
import { Logger } from '../services/logger.js';
import chalk from 'chalk';
/**
* Apply incremental pattern updates (add|update|remove)
*/
export async function deltaCommand(operation, options) {
const logger = new Logger(globalOptions);
// Validate operation
const validOperations = ['add', 'update', 'remove'];
if (!validOperations.includes(operation.toLowerCase())) {
if (logger.isJson()) {
logger.output({
error: `Invalid operation "${operation}". Must be one of: ${validOperations.join(', ')}`
});
}
else {
logger.error(`Invalid operation "${operation}"`);
logger.info(chalk.dim(` Valid operations: ${validOperations.join(', ')}`));
}
process.exit(1);
}
let bullets = [];
// Read bullets from stdin if specified
if (options.stdin) {
try {
const stdinData = readFileSync(0, 'utf8'); // fd 0 is stdin
const data = JSON.parse(stdinData.trim());
bullets = Array.isArray(data) ? data : [data];
}
catch (error) {
if (logger.isJson()) {
logger.output({ error: 'Failed to read from stdin' });
}
else {
logger.error(`Failed to read from stdin: ${error instanceof Error ? error.message : String(error)}`);
}
process.exit(1);
}
}
// Read bullets from file if specified
else if (options.file) {
try {
const content = readFileSync(options.file, 'utf8');
const data = JSON.parse(content);
bullets = Array.isArray(data) ? data : [data];
}
catch (error) {
if (logger.isJson()) {
logger.output({ error: 'Failed to read bullets file' });
}
else {
logger.error(`Failed to read bullets file: ${error instanceof Error ? error.message : String(error)}`);
}
process.exit(1);
}
}
// Parse bullets from JSON string
else if (options.bullets) {
try {
const data = JSON.parse(options.bullets);
bullets = Array.isArray(data) ? data : [data];
}
catch (error) {
if (logger.isJson()) {
logger.output({ error: 'Failed to parse bullets JSON' });
}
else {
logger.error(`Failed to parse bullets JSON: ${error instanceof Error ? error.message : String(error)}`);
}
process.exit(1);
}
}
else {
if (logger.isJson()) {
logger.output({ error: 'Must provide either --bullets, --file, or --stdin' });
}
else {
logger.error('Must provide either --bullets, --file, or --stdin');
}
process.exit(1);
}
if (bullets.length === 0) {
if (logger.isJson()) {
logger.output({ error: 'No bullets provided' });
}
else {
logger.error('No bullets provided');
}
process.exit(1);
}
const spinner = logger.spinner(`Applying delta operation: ${operation}...`);
try {
const context = await createContext({ org: globalOptions.org, project: globalOptions.project });
const client = new ACEServerClient(context, logger);
// Apply delta operation for each bullet
for (const bullet of bullets) {
await client.applyDelta({
type: operation.toUpperCase(),
section: bullet.section,
content: bullet.content,
bullet_id: bullet.id,
helpful_delta: bullet.helpful,
harmful_delta: bullet.harmful,
evidence: bullet.evidence
});
}
// Invalidate cache
client.invalidateCache();
spinner?.succeed(`Delta operation ${operation} completed`);
if (logger.isJson()) {
logger.output({
success: true,
operation,
bulletsProcessed: bullets.length,
message: `Applied ${operation} operation to ${bullets.length} bullet(s)`
});
}
else {
logger.info(chalk.green(`\n✅ Applied ${operation} operation to ${bullets.length} bullet(s)\n`));
}
}
catch (error) {
spinner?.fail('Delta operation failed');
if (logger.isJson()) {
logger.output({ error: error instanceof Error ? error.message : String(error) });
}
else {
logger.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
}
process.exit(1);
}
}
//# sourceMappingURL=delta.js.map