@agentled/cli
Version:
CLI for Agentled — manage workflows, apps, and knowledge from the command line. Zero context-window cost for AI agents.
106 lines • 4.33 kB
JavaScript
/* eslint-disable no-console */
import { existsSync, readdirSync, readFileSync } from 'node:fs';
import { join, dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const PUBLIC_REPO_URL = 'https://github.com/agentled/agentic-ops';
const CLI_ONLY_PATTERN_SLUGS = new Set(['13-entity-pipeline-lifecycle']);
function resolveBundledPatternsDir() {
const here = fileURLToPath(import.meta.url);
// dist/commands/examples.js → package root is two levels up
const pkgRoot = resolve(dirname(here), '..', '..');
return join(pkgRoot, 'patterns', 'v1');
}
function loadPatterns() {
const dir = resolveBundledPatternsDir();
if (!existsSync(dir))
return [];
return readdirSync(dir)
.filter(f => f.endsWith('.md'))
.sort()
.map(file => {
const path = join(dir, file);
const slug = file.replace(/\.md$/, '');
const m = slug.match(/^(\d+)-(.+)$/);
const content = readFileSync(path, 'utf-8');
const titleMatch = content.match(/^#\s+(.+)$/m);
return {
slug,
number: m?.[1] ?? '',
keyword: m?.[2] ?? slug,
title: titleMatch?.[1]?.trim() ?? slug,
path,
};
});
}
function findPattern(patterns, query) {
const q = query.toLowerCase().replace(/^0+/, '');
// Exact slug match
const slug = patterns.find(p => p.slug.toLowerCase() === query.toLowerCase());
if (slug)
return slug;
// Match by number (with or without leading zero)
const num = patterns.find(p => p.number.replace(/^0+/, '') === q);
if (num)
return num;
// Match by keyword (trigger-design, dedup-gates, etc.)
const kw = patterns.find(p => p.keyword.toLowerCase() === query.toLowerCase());
if (kw)
return kw;
// Partial keyword match
const partial = patterns.find(p => p.keyword.toLowerCase().includes(query.toLowerCase()));
if (partial)
return partial;
return null;
}
export function registerExamplesCommand(program) {
program
.command('examples [pattern]')
.alias('patterns')
.description('Show Agentled workflow best-practice patterns (trigger design, dedup gates, loops, …).')
.option('--list', 'List available patterns (default when no pattern is given)')
.action((pattern, opts) => {
const patterns = loadPatterns();
if (patterns.length === 0) {
console.error('No bundled patterns found. Reinstall @agentled/cli.');
process.exit(1);
}
if (!pattern || opts.list) {
console.log('Available patterns (bundled with the Agentled CLI):\n');
for (const p of patterns) {
console.log(` ${p.slug.padEnd(30)} ${p.title}`);
}
console.log('\nUsage:');
console.log(' agentled examples <pattern> # e.g. `agentled examples trigger-design` or `agentled examples 01`');
return;
}
const found = findPattern(patterns, pattern);
if (!found) {
console.error(`Pattern "${pattern}" not found. Run \`agentled examples\` to list available patterns.`);
process.exit(1);
}
console.log(readFileSync(found.path, 'utf-8'));
if (CLI_ONLY_PATTERN_SLUGS.has(found.slug)) {
console.log(`\n---\nSource: bundled CLI pattern (${found.slug}.md)`);
}
else {
console.log(`\n---\nSource: ${PUBLIC_REPO_URL}/blob/main/patterns/v1/${found.slug}.md`);
}
});
program
.command('best-practices')
.description('Show where to find Agentled workflow best practices (agentic-ops repo).')
.action(() => {
console.log('Agentled workflow best practices are published at:');
console.log(` ${PUBLIC_REPO_URL}`);
console.log('');
console.log('Core patterns are bundled with this CLI and readable offline:');
console.log(' agentled examples # list patterns');
console.log(' agentled examples <pattern> # print a specific pattern');
console.log('');
console.log('Categories (v1):');
for (const p of loadPatterns()) {
console.log(` - ${p.title}`);
}
});
}
//# sourceMappingURL=examples.js.map