@botpress/adk-cli
Version:
Command-line interface for the Botpress Agent Development Kit (ADK)
148 lines (146 loc) • 4.88 kB
JavaScript
// @bun
// src/utils/format-spec.ts
function formatIntegrationInfoText(spec) {
const lines = [];
lines.push(header(spec.name, spec.version, spec.title));
if (spec.description)
lines.push(spec.description);
const configSection = formatConfigSchema(spec.configuration?.schema);
if (configSection) {
lines.push("");
lines.push("Configuration:");
lines.push(configSection);
}
appendNameList(lines, "Channels", spec.channels);
appendNameList(lines, "Actions", spec.actions);
appendNameList(lines, "Events", spec.events);
appendNameList(lines, "States", spec.states);
if (spec.interfaces && Object.keys(spec.interfaces).length > 0) {
const items = Object.values(spec.interfaces).map((i) => `${i.name}@${i.version}`);
lines.push("");
lines.push(`Implements: ${items.join(", ")}`);
}
lines.push("");
lines.push("For the full schema, run with --format json.");
return lines.join(`
`) + `
`;
}
function formatPluginInfoText(spec) {
const lines = [];
lines.push(header(spec.name, spec.version, spec.title));
if (spec.description)
lines.push(spec.description);
const configSection = formatConfigSchema(spec.configuration?.schema);
if (configSection) {
lines.push("");
lines.push("Configuration:");
lines.push(configSection);
}
const deps = spec.dependencies;
if (deps && (deps.integrations && Object.keys(deps.integrations).length || deps.interfaces && Object.keys(deps.interfaces).length)) {
lines.push("");
lines.push("Dependencies:");
if (deps.integrations && Object.keys(deps.integrations).length > 0) {
const items = Object.values(deps.integrations).map((d) => `${d.name}@${d.version}`);
lines.push(` Integrations: ${items.join(", ")}`);
}
if (deps.interfaces && Object.keys(deps.interfaces).length > 0) {
const items = Object.values(deps.interfaces).map((d) => `${d.name}@${d.version}`);
lines.push(` Interfaces: ${items.join(", ")}`);
}
}
appendNameList(lines, "Actions", spec.actions);
appendNameList(lines, "Events", spec.events);
appendNameList(lines, "States", spec.states);
lines.push("");
lines.push("For the full schema, run with --format json.");
return lines.join(`
`) + `
`;
}
function formatConfigBlock(label, config, baseIndent = 2) {
const indent = " ".repeat(baseIndent);
if (config === undefined || config === null) {
return `${indent}${label}: (none)
`;
}
if (typeof config !== "object" || Array.isArray(config)) {
return `${indent}${label}: ${JSON.stringify(config)}
`;
}
const entries = Object.entries(config);
if (entries.length === 0)
return `${indent}${label}: {}
`;
const lines = [`${indent}${label}:`];
for (const [key, value] of entries) {
lines.push(`${indent} ${key}: ${stringifyValue(value, baseIndent + 4)}`);
}
return lines.join(`
`) + `
`;
}
function header(name, version, title) {
const titlePart = title && title !== name ? ` \u2014 ${title}` : "";
return `${name}@${version}${titlePart}`;
}
function appendNameList(lines, label, record) {
if (!record)
return;
const names = Object.keys(record);
if (names.length === 0)
return;
lines.push("");
lines.push(`${label}: ${names.join(", ")}`);
}
function formatConfigSchema(schema) {
const props = schema?.properties;
if (!props || Object.keys(props).length === 0)
return null;
const required = new Set(schema?.required ?? []);
const lines = [];
for (const [key, prop] of Object.entries(props)) {
const typeLabel = typeOf(prop);
const reqLabel = required.has(key) ? ", required" : "";
const desc = prop.description ? ` \u2014 ${prop.description}` : "";
lines.push(` ${key}: ${typeLabel}${reqLabel}${desc}`);
}
return lines.join(`
`);
}
function typeOf(prop) {
if (prop.enum && Array.isArray(prop.enum)) {
const sample = prop.enum.slice(0, 3).map((v) => JSON.stringify(v)).join(" | ");
return prop.enum.length > 3 ? `enum (${sample}, \u2026)` : `enum (${sample})`;
}
if (Array.isArray(prop.type))
return prop.type.join(" | ");
return prop.type ?? "unknown";
}
function stringifyValue(value, indent) {
if (value === null)
return "null";
if (value === undefined)
return "undefined";
if (typeof value === "string")
return JSON.stringify(value);
if (typeof value === "number" || typeof value === "boolean")
return String(value);
if (Array.isArray(value)) {
if (value.length === 0)
return "[]";
return JSON.stringify(value);
}
if (typeof value === "object") {
const entries = Object.entries(value);
if (entries.length === 0)
return "{}";
const inner = " ".repeat(indent);
return `
` + entries.map(([k, v]) => `${inner}${k}: ${stringifyValue(v, indent + 2)}`).join(`
`);
}
return String(value);
}
export { formatIntegrationInfoText, formatPluginInfoText, formatConfigBlock };