@hashgraphonline/standards-agent-kit
Version:
A modular SDK for building on-chain autonomous agents using Hashgraph Online Standards, including HCS-10 for agent discovery and communication. https://hol.org
428 lines (427 loc) • 12.6 kB
JavaScript
import { ZodType } from "zod";
function extendZodSchema(schema) {
const extendedSchema = schema;
extendedSchema.withRender = function(config) {
const newSchema = Object.create(this);
const currentConfig = this._renderConfig || {};
const mergedConfig = {
...currentConfig,
...config,
ui: {
...currentConfig.ui || {},
...config.ui || {}
}
};
if (currentConfig.progressive || config.progressive) {
mergedConfig.progressive = {
...currentConfig.progressive || {},
...config.progressive || {}
};
}
if (currentConfig.block || config.block) {
mergedConfig.block = {
...currentConfig.block || {},
...config.block || {}
};
}
newSchema._renderConfig = mergedConfig;
newSchema.withRender = extendedSchema.withRender;
newSchema.withProgressive = extendedSchema.withProgressive;
newSchema.withBlock = extendedSchema.withBlock;
return newSchema;
};
extendedSchema.withProgressive = function(priority, group) {
const currentConfig = this._renderConfig || { fieldType: inferFieldTypeFromSchema(schema) };
return this.withRender({
...currentConfig,
progressive: {
priority,
group
}
});
};
extendedSchema.withBlock = function(blockId) {
const currentConfig = this._renderConfig || { fieldType: inferFieldTypeFromSchema(schema) };
return this.withRender({
...currentConfig,
block: {
id: blockId,
type: "field",
reusable: true
}
});
};
return extendedSchema;
}
function hasRenderConfig(schema) {
return Boolean(schema && typeof schema.withRender === "function");
}
function getRenderConfig(schema) {
if (hasRenderConfig(schema)) {
return schema._renderConfig;
}
return void 0;
}
function inferFieldTypeFromSchema(schema) {
const typeName = schema._def?.typeName;
switch (typeName) {
case "ZodString": {
const stringChecks = schema._def?.checks || [];
for (const check of stringChecks) {
if (check.kind === "email") {
return "text";
}
if (check.kind === "url") {
return "text";
}
}
return "text";
}
case "ZodNumber":
case "ZodBigInt":
return "number";
case "ZodBoolean":
return "checkbox";
case "ZodEnum":
case "ZodNativeEnum":
return "select";
case "ZodArray":
return "array";
case "ZodUnion":
case "ZodDiscriminatedUnion":
return "select";
case "ZodDate":
return "text";
case "ZodObject":
return "object";
case "ZodOptional":
case "ZodDefault": {
const innerType = schema._def?.innerType;
if (innerType) {
return inferFieldTypeFromSchema(innerType);
}
return "text";
}
default:
return "text";
}
}
function extractOptionsFromSchema(schema) {
const typeName = schema._def?.typeName;
if (typeName === "ZodEnum") {
const values = schema._def?.values;
if (Array.isArray(values)) {
return values.map((value) => ({
value,
label: value.charAt(0).toUpperCase() + value.slice(1).replace(/[_-]/g, " ")
}));
}
}
if (typeName === "ZodNativeEnum") {
const enumObject = schema._def?.values;
if (enumObject) {
return Object.entries(enumObject).map(([key, value]) => ({
value,
label: key.replace(/[_-]/g, " ")
}));
}
}
if (typeName === "ZodUnion") {
const options = [];
const unionOptions = schema._def?.options;
if (Array.isArray(unionOptions)) {
for (const option of unionOptions) {
if (option._def?.typeName === "ZodLiteral") {
const value = option._def?.value;
if (value !== void 0) {
options.push({
value,
label: typeof value === "string" ? value.charAt(0).toUpperCase() + value.slice(1) : String(value)
});
}
}
}
}
return options.length > 0 ? options : void 0;
}
return void 0;
}
function isOptionalSchema(schema) {
const typeName = schema._def?.typeName;
return typeName === "ZodOptional" || typeName === "ZodDefault";
}
function getInnerSchema(schema) {
const typeName = schema._def?.typeName;
if (typeName === "ZodOptional" || typeName === "ZodDefault") {
const innerType = schema._def?.innerType;
return innerType || schema;
}
return schema;
}
function extractValidationConstraints(schema) {
const innerSchema = getInnerSchema(schema);
const typeName = innerSchema._def?.typeName;
const constraints = {};
if (typeName === "ZodString") {
const def = innerSchema._def;
const checks = def.checks;
if (checks && Array.isArray(checks)) {
for (const check of checks) {
switch (check.kind) {
case "min":
constraints.minLength = check.value;
break;
case "max":
constraints.maxLength = check.value;
break;
case "email":
constraints.type = "email";
break;
case "url":
constraints.type = "url";
break;
case "regex":
constraints.pattern = check.regex;
break;
}
}
}
}
if (typeName === "ZodNumber") {
const def = innerSchema._def;
const checks = def.checks;
if (checks && Array.isArray(checks)) {
for (const check of checks) {
switch (check.kind) {
case "min":
constraints.min = check.value;
break;
case "max":
constraints.max = check.value;
break;
case "int":
constraints.step = 1;
break;
case "multipleOf":
constraints.step = check.value;
break;
}
}
}
}
if (typeName === "ZodArray") {
const def = innerSchema._def;
const minLength = def.minLength;
const maxLength = def.maxLength;
if (minLength !== void 0) {
constraints.minItems = minLength.value;
}
if (maxLength !== void 0) {
constraints.maxItems = maxLength.value;
}
}
return constraints;
}
function getDefaultValue(schema) {
const typeName = schema._def?.typeName;
if (typeName === "ZodDefault") {
const defaultValue = schema._def?.defaultValue;
if (typeof defaultValue === "function") {
return defaultValue();
}
return defaultValue;
}
const innerSchema = getInnerSchema(schema);
const innerTypeName = innerSchema._def?.typeName;
switch (innerTypeName) {
case "ZodString":
return "";
case "ZodNumber":
case "ZodBigInt":
return 0;
case "ZodBoolean":
return false;
case "ZodArray":
return [];
case "ZodObject": {
const shape = innerSchema._def?.shape;
if (shape) {
const defaultObj = {};
for (const [key, value] of Object.entries(shape)) {
defaultObj[key] = getDefaultValue(value);
}
return defaultObj;
}
return {};
}
case "ZodDate":
return /* @__PURE__ */ new Date();
case "ZodEnum": {
const values = innerSchema._def?.values;
return Array.isArray(values) && values.length > 0 ? values[0] : void 0;
}
default:
return void 0;
}
}
function extractFieldMetadata(schema) {
const innerSchema = getInnerSchema(schema);
const fieldType = inferFieldTypeFromSchema(schema);
const required = !isOptionalSchema(schema);
const optional = isOptionalSchema(schema);
const defaultValue = getDefaultValue(schema);
const options = extractOptionsFromSchema(innerSchema);
const constraints = extractValidationConstraints(schema);
const description = schema?.description;
return {
type: fieldType,
required,
optional,
default: defaultValue,
options,
constraints,
description,
validation: {
minLength: constraints.minLength,
maxLength: constraints.maxLength,
min: constraints.min,
max: constraints.max,
pattern: constraints.pattern ? new RegExp(constraints.pattern) : void 0
}
};
}
const renderConfigs = {
text: (label, placeholder, priority = "common") => ({
fieldType: "text",
ui: { label, placeholder, priority },
progressive: { priority }
}),
number: (label, min, max, priority = "common") => ({
fieldType: "number",
ui: { label, priority },
constraints: { min, max },
progressive: { priority }
}),
select: (label, options, priority = "common") => ({
fieldType: "select",
ui: { label, priority },
options,
progressive: { priority }
}),
textarea: (label, rows = 3, priority = "common") => ({
fieldType: "textarea",
ui: { label, priority },
props: { rows },
progressive: { priority }
}),
currency: (label, symbol = "HBAR", priority = "common") => ({
fieldType: "currency",
ui: { label, priority },
props: { symbol },
progressive: { priority }
}),
array: (label, itemLabel, priority = "advanced") => ({
fieldType: "array",
ui: { label, priority },
props: { itemLabel },
progressive: { priority }
}),
essential: {
text: (label, placeholder) => renderConfigs.text(label, placeholder, "essential"),
number: (label, min, max) => renderConfigs.number(label, min, max, "essential"),
select: (label, options) => renderConfigs.select(label, options, "essential"),
textarea: (label, rows) => renderConfigs.textarea(label, rows, "essential")
},
advanced: {
text: (label, placeholder) => renderConfigs.text(label, placeholder, "advanced"),
number: (label, min, max) => renderConfigs.number(label, min, max, "advanced"),
select: (label, options) => renderConfigs.select(label, options, "advanced"),
textarea: (label, rows) => renderConfigs.textarea(label, rows, "advanced"),
array: (label, itemLabel) => renderConfigs.array(label, itemLabel, "advanced")
},
expert: {
text: (label, placeholder) => renderConfigs.text(label, placeholder, "expert"),
number: (label, min, max) => renderConfigs.number(label, min, max, "expert"),
select: (label, options) => renderConfigs.select(label, options, "expert"),
textarea: (label, rows) => renderConfigs.textarea(label, rows, "expert")
}
};
function installZodRenderExtensions() {
if (!ZodType.prototype.withRender) {
ZodType.prototype.withRender = function(config) {
return extendZodSchema(this).withRender(config);
};
ZodType.prototype.withProgressive = function(priority, group) {
return extendZodSchema(this).withProgressive(priority, group);
};
ZodType.prototype.withBlock = function(blockId) {
return extendZodSchema(this).withBlock(blockId);
};
}
}
function createProgressiveSchema(baseSchema, groups) {
const extendedSchema = extendZodSchema(baseSchema);
const typeName = baseSchema._def?.typeName;
if (typeName === "ZodObject") {
const shape = baseSchema._def?.shape;
if (shape) {
for (const [fieldName, fieldSchema] of Object.entries(shape)) {
let fieldGroup;
let fieldPriority = "common";
for (const [groupName, groupConfig] of Object.entries(groups)) {
if (groupConfig.fields.includes(fieldName)) {
fieldGroup = groupName;
fieldPriority = groupConfig.priority;
break;
}
}
extendZodSchema(fieldSchema).withProgressive(fieldPriority, fieldGroup);
}
return extendedSchema;
}
}
return extendedSchema;
}
function enhanceRenderConfig(config) {
return {
...config,
progressive: {
priority: config.ui?.priority || "common",
group: config.ui?.group
}
};
}
function extractProgressiveInfo(schema) {
const renderConfig = getRenderConfig(schema);
if (renderConfig?.progressive) {
return {
priority: renderConfig.progressive.priority,
group: renderConfig.progressive.group
};
}
if (renderConfig?.ui?.priority) {
return {
priority: renderConfig.ui.priority,
group: renderConfig.ui.group
};
}
return { priority: "common" };
}
export {
createProgressiveSchema,
enhanceRenderConfig,
extendZodSchema,
extractFieldMetadata,
extractOptionsFromSchema,
extractProgressiveInfo,
extractValidationConstraints,
getDefaultValue,
getInnerSchema,
getRenderConfig,
hasRenderConfig,
inferFieldTypeFromSchema,
installZodRenderExtensions,
isOptionalSchema,
renderConfigs
};
//# sourceMappingURL=standards-agent-kit.es44.js.map