UNPKG

@botpress/adk-cli

Version:

Command-line interface for the Botpress Agent Development Kit (ADK)

204 lines (188 loc) 5.84 kB
// @bun import { createJsxComponent, isAnyJsxComponent, isJsxComponent } from "./chunk-0v8vgrns.js"; import { exports_exports } from "./chunk-54qt5g7m.js"; // ../../node_modules/.bun/llmz@0.0.79+b49d396f5ed96e7f/node_modules/llmz/dist/chunk-YTDNCBQW.js function assertValidComponent(component) { if (!component.name) { throw new Error("Component must have a name"); } const nameRegex = /^[A-Z]{3,50}$/i; if (!nameRegex.test(component.name)) { throw new Error(`Component name "${component.name}" must be 3-50 characters long and start with an uppercase letter`); } if (component.aliases) { for (const alias of component.aliases) { if (!nameRegex.test(alias)) { throw new Error(`Alias "${alias}" must be 3-50 characters long and start with an uppercase letter`); } } } if (!component.description) { throw new Error("Component must have a description"); } if (!component.examples || component.examples.length === 0) { throw new Error("Component must have at least one example"); } if (component.type === "default" && !component.default) { throw new Error("Default component must have default props and children"); } if (component.type === "leaf" && !component.leaf) { throw new Error("Leaf component must have leaf props"); } if (component.type === "container" && !component.container) { throw new Error("Container component must have container props and children"); } } var getDefaultValue = (schema) => { if (schema._def.defaultValue !== undefined) { if (typeof schema._def.defaultValue === "function") { return String(schema._def.defaultValue()).toString(); } else { return String(schema._def.defaultValue); } } return ""; }; function getComponentReference(component) { let doc = `### <${component.name}> `; doc += `${component.description} `; const getPropsDoc = (props) => { const shape = props.shape; if (Object.keys(shape).length === 0) return `_No props._ `; const zodTypeToTsType = { ZodString: "string", ZodNumber: "number", ZodBoolean: "boolean", ZodEnum: "enum", ZodArray: "array", ZodObject: "object", ZodDate: "date", ZodBigInt: "bigint", ZodSymbol: "symbol", ZodUndefined: "undefined", ZodNull: "null", ZodVoid: "void", ZodNever: "never", ZodUnknown: "unknown", ZodAny: "any" }; return Object.entries(shape).map(([name, schema]) => { const naked = schema.naked(); const zodType = naked._def.typeName; const defValue = getDefaultValue(schema); const typings = exports_exports.is.zuiEnum(naked) ? naked._def.values.map((x) => `"${x}"`).join(" | ") : zodTypeToTsType[zodType] || zodType; const required = !schema.isOptional() ? "**(required)**" : "(optional)"; const def = defValue ? ` _Default: \`${defValue}\`_` : ""; const description = schema.description || schema.naked().description || (schema == null ? undefined : schema._def.description) || ""; return `- \`${name}: ${typings}\` ${required} \u2014 ${description}${def}`; }).join(` `) + ` `; }; const getChildrenDoc = (children) => { if (children.length === 0) return `_None allowed._ `; return `Can contain: ` + children.map((child) => `- ${child.description} \u2014 \`<${child.component.name}>\``).join(` `) + ` `; }; const getExamplesDoc = (examples) => { if (!examples.length) return ""; return `**Examples:** ` + examples.map((example) => `**${example.name}** \u2014 ${example.description} \`\`\`tsx ${example.code.trim()} \`\`\` `).join(` `); }; switch (component.type) { case "leaf": doc += `**Props:** `; doc += getPropsDoc(component.leaf.props); doc += `**Children:** `; doc += getChildrenDoc([]); doc += getExamplesDoc(component.examples); break; case "container": doc += `**Props:** `; doc += getPropsDoc(component.container.props); doc += `**Children:** `; doc += getChildrenDoc(component.container.children); doc += getExamplesDoc(component.examples); break; case "default": default: doc += `**Props:** `; doc += getPropsDoc(component.default.props); doc += getExamplesDoc(component.examples); break; } return doc.trim(); } var Component = class { definition; propsType; constructor(definition) { assertValidComponent(definition); this.definition = definition; } render(props, children = []) { return createJsxComponent({ type: this.definition.name, props, children }); } }; function isComponent(rendered, component) { return isJsxComponent(component.definition.name, rendered); } function isAnyComponent(message) { return isAnyJsxComponent(message); } function renderToTsx(component) { const props = Object.entries(component.props).map(([key, value]) => { if (typeof value === "string") { return `${key}="${value}"`; } if (typeof value === "boolean") { return value ? key : ""; } if (typeof value === "number") { return `${key}={${value}}`; } if (value === null || value === undefined) { return ""; } if (typeof value === "object") { return `${key}={${JSON.stringify(value)}}`; } return `${key}={${String(value)}}`; }).filter(Boolean).join(" "); const children = component.children.map((child) => { if (typeof child === "string") { return child; } if (isAnyComponent(child)) { return renderToTsx(child); } return String(child); }).join(""); return `<${component.type}${props ? " " + props : ""}>${children}</${component.type}>`; } export { assertValidComponent, getComponentReference, Component, isComponent, isAnyComponent, renderToTsx };