genpower
Version:
Unified TypeScript library to generate, validate, and structure AI model outputs
91 lines (90 loc) • 3.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePromptFromSchema = generatePromptFromSchema;
const zod_1 = require("zod");
function describeSchema(schema, mode, indent = 0) {
try {
const pad = " ".repeat(indent);
if (schema instanceof zod_1.ZodObject) {
const fields = schema.shape;
if (mode !== "human")
return (pad +
"{\n" +
Object.entries(fields)
.map(([key, value]) => {
return `${pad} "${key}": ${describeSchema(value, mode, indent + 1)}`;
})
.join(",\n") +
`\n${pad}}`);
return (pad +
Object.entries(fields)
.map(([key, value]) => {
return `- ${key}: ${describeSchema(value, mode, indent + 1)}`;
})
.join(`\n${pad}`));
}
if (schema instanceof zod_1.ZodArray) {
const itemType = schema.element;
if (mode !== "human")
return `[\n${describeSchema(itemType, mode, indent + 1)}\n${pad}]`;
return `an array, *placeholder, add description* \n${describeSchema(itemType, mode, indent + 1)}`;
}
if (schema instanceof zod_1.ZodOptional || schema instanceof zod_1.ZodNullable) {
const inner = schema._def.innerType;
return `${describeSchema(inner, mode, indent)} (optional)`;
}
if (schema instanceof zod_1.ZodEnum) {
const values = schema.options;
return `one of (${values.map((v) => `"${v}"`).join(", ")})`;
}
if (schema instanceof zod_1.ZodString) {
if (mode === "json")
return "string";
if (mode === "example")
return "*placeholder, add value*";
return "a string, *placeholder, add description*";
}
if (schema instanceof zod_1.ZodNumber) {
if (mode === "json")
return "number";
if (mode === "example")
return "*placeholder, add value*";
return "a number, *placeholder, add description*";
}
if (schema instanceof zod_1.ZodBoolean) {
if (mode === "json")
return "boolean";
if (mode === "example")
return "*placeholder, add value*";
return "a boolean, *placeholder, add description*";
}
return "unknown";
}
catch (error) {
console.error("Error in interpreting schema:", error instanceof Error ? error.message : error);
return "unknown";
}
}
function generatePromptFromSchema(schema, options) {
const { includeObjective = true, includeInstruction = true, includeOutput = true, includeExample = true, } = options || {};
const structure = describeSchema(schema, "json");
const humanSchema = describeSchema(schema, "human");
const exampleJson = describeSchema(schema, "example");
return [
includeObjective
? `Objective:\n*Placeholder, please add your objective here*\n`
: "",
includeInstruction
? `Instruction:\nYou're a *Placeholder: add role, expertise, or identity (e.g., expert in X, business analyst, consultant, etc.)*.\n` +
`Your task is to *Placeholder: describe the main task to perform*.\n` +
`To do this, follow the instruction below:\n` +
`1. *step 1 placeholder*\n2. *step 2 placeholder*\n3. *step 3 placeholder*\n\n`
: "",
includeOutput
? `Output:\nGenerate a JSON object with:\n${humanSchema}\nOnly return the JSON inside a code block (like \`\`\`json ... \`\`\`).\n`
: "",
includeExample ? `Example format:\n\`\`\`json\n${exampleJson}\n\`\`\`` : "",
]
.filter(Boolean)
.join("\n");
}