@blocklet/pages-kit-block-studio 0.4.90 → 0.4.92

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  // 导入 Zod 工具集
2
2
  import { getComponentDependencies } from '@blocklet/pages-kit/utils/property';
3
+ import { z } from 'zod';
3
4
  import { BASIC_COMPONENT_SECTION_TYPES, BASIC_COMPONENT_SECTION_JSON_SCHEMAS } from './block-props-utils';
4
5
  import { propertiesToZodSchema, zodSchemaToTypeString, zodSchemaToJsonSchema } from './zod-utils';
5
6
  export async function generateWrapperCode({ project, state, version }) {
@@ -461,4 +462,147 @@ export function generatePropertiesTypesWithZod(properties) {
461
462
  const typeString = zodSchemaToTypeString(zodSchema, 'Props');
462
463
  return typeString;
463
464
  }
465
+ /**
466
+ * 将 Zod schema 序列化为 JSON 描述结构
467
+ */
468
+ export function serializeZodSchema(schema) {
469
+ // 处理不同类型的 schema
470
+ if (schema instanceof z.ZodObject) {
471
+ const shape = schema._def.shape();
472
+ const properties = {};
473
+ Object.entries(shape).forEach(([key, value]) => {
474
+ properties[key] = serializeZodSchema(value);
475
+ });
476
+ return {
477
+ type: 'object',
478
+ properties,
479
+ description: schema.description,
480
+ };
481
+ }
482
+ if (schema instanceof z.ZodString) {
483
+ return {
484
+ type: 'string',
485
+ description: schema.description,
486
+ optional: schema.isOptional(),
487
+ };
488
+ }
489
+ if (schema instanceof z.ZodNumber) {
490
+ return {
491
+ type: 'number',
492
+ description: schema.description,
493
+ optional: schema.isOptional(),
494
+ };
495
+ }
496
+ if (schema instanceof z.ZodBoolean) {
497
+ return {
498
+ type: 'boolean',
499
+ description: schema.description,
500
+ optional: schema.isOptional(),
501
+ };
502
+ }
503
+ if (schema instanceof z.ZodArray) {
504
+ return {
505
+ type: 'array',
506
+ items: serializeZodSchema(schema._def.type),
507
+ description: schema.description,
508
+ optional: schema.isOptional(),
509
+ };
510
+ }
511
+ if (schema instanceof z.ZodRecord) {
512
+ return {
513
+ type: 'record',
514
+ valueType: serializeZodSchema(schema._def.valueType),
515
+ description: schema.description,
516
+ optional: schema.isOptional(),
517
+ };
518
+ }
519
+ if (schema instanceof z.ZodOptional) {
520
+ const innerType = serializeZodSchema(schema._def.innerType);
521
+ innerType.optional = true;
522
+ return innerType;
523
+ }
524
+ if (schema instanceof z.ZodAny) {
525
+ return {
526
+ type: 'any',
527
+ description: schema.description,
528
+ optional: schema.isOptional(),
529
+ };
530
+ }
531
+ // 默认情况
532
+ return {
533
+ type: 'unknown',
534
+ description: schema.description,
535
+ };
536
+ }
537
+ /**
538
+ * 生成特定页面所有 section 数据的 Zod 类型定义
539
+ * @param state 应用状态
540
+ * @param pageId 目标页面ID
541
+ * @returns 包含所有 section 数据类型的 Zod schema 的序列化描述
542
+ */
543
+ export function generatePageSectionsDataZod(state, pageId) {
544
+ const page = state.pages[pageId];
545
+ if (!page) {
546
+ return serializeZodSchema(z.object({}).describe('Empty page data'));
547
+ }
548
+ const sectionSchemas = {};
549
+ // 收集页面所有section的配置信息
550
+ page.sectionIds.forEach((sectionId) => {
551
+ const section = page.sections[sectionId];
552
+ if (!section || !section.isTemplateSection)
553
+ return;
554
+ const sectionName = section.name || section.id;
555
+ if (section.component === 'custom-component') {
556
+ const componentId = section.config?.componentId;
557
+ if (!componentId)
558
+ return;
559
+ const component = state.components[componentId]?.data || state.resources.components?.[componentId]?.component;
560
+ if (!component)
561
+ return;
562
+ // 使用propertiesToZodSchema生成Zod Schema,并包含llmConfig配置
563
+ const zodSchema = propertiesToZodSchema(component.properties || {}, {
564
+ addZodDescribe: false,
565
+ llmConfig: section.llmConfig?.properties || {},
566
+ });
567
+ // 添加section描述
568
+ sectionSchemas[sectionName] = zodSchema.describe(section.templateDescription || '');
569
+ }
570
+ else if (BASIC_COMPONENT_SECTION_TYPES[section.component]) {
571
+ // 处理基本组件类型
572
+ const componentType = section.component;
573
+ const schemaProperties = BASIC_COMPONENT_SECTION_JSON_SCHEMAS[componentType];
574
+ // 将JSON Schema转换为Zod Schema
575
+ const zodProperties = Object.entries(schemaProperties || {}).reduce((acc, [key, value]) => {
576
+ // 简单处理常见类型
577
+ if (value.type === 'string') {
578
+ acc[key] = z.string().optional();
579
+ }
580
+ else if (value.type === 'number') {
581
+ acc[key] = z.number().optional();
582
+ }
583
+ else if (value.type === 'boolean') {
584
+ acc[key] = z.boolean().optional();
585
+ }
586
+ else if (value.type === 'array') {
587
+ acc[key] = z.array(z.any()).optional();
588
+ }
589
+ else if (value.type === 'object') {
590
+ acc[key] = z.record(z.string(), z.any()).optional();
591
+ }
592
+ else {
593
+ acc[key] = z.any().optional();
594
+ }
595
+ return acc;
596
+ }, {});
597
+ sectionSchemas[sectionName] = z.object(zodProperties).describe(section.templateDescription || '');
598
+ }
599
+ else {
600
+ // 其他类型的section,使用默认对象
601
+ sectionSchemas[sectionName] = z.object({}).describe(section.templateDescription || '');
602
+ }
603
+ });
604
+ // FIXME: 这里需要优化,AIGNE Framework 校验修改后,不需要序列化,直接使用
605
+ // 创建包含所有section的schema并序列化返回
606
+ return serializeZodSchema(z.object(sectionSchemas).describe(`Page ${page.title || pageId} sections data`));
607
+ }
464
608
  export default generateWrapperCode;