@blocklet/pages-kit-block-studio 0.4.98 → 0.4.99

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.
@@ -462,88 +462,18 @@ export function generatePropertiesTypesWithZod(properties) {
462
462
  const typeString = zodSchemaToTypeString(zodSchema, 'Props');
463
463
  return typeString;
464
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
465
  /**
538
466
  * 生成特定页面所有 section 数据的 Zod 类型定义
467
+ * Agent 使用,内部不能包含 any 类型
539
468
  * @param state 应用状态
540
469
  * @param pageId 目标页面ID
541
470
  * @returns 包含所有 section 数据类型的 Zod schema 的序列化描述
542
471
  */
543
- export function generatePageSectionsDataZod(state, pageId) {
472
+ export function generatePageSectionsDataZodToAgent(state, pageId) {
544
473
  const page = state.pages[pageId];
545
474
  if (!page) {
546
- return serializeZodSchema(z.object({}).describe('Empty page data'));
475
+ // empty page data
476
+ return null;
547
477
  }
548
478
  const sectionSchemas = {};
549
479
  // 收集页面所有section的配置信息
@@ -583,15 +513,6 @@ export function generatePageSectionsDataZod(state, pageId) {
583
513
  else if (value.type === 'boolean') {
584
514
  acc[key] = z.boolean().optional();
585
515
  }
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
516
  return acc;
596
517
  }, {});
597
518
  sectionSchemas[sectionName] = z.object(zodProperties).describe(section.templateDescription || '');
@@ -601,8 +522,6 @@ export function generatePageSectionsDataZod(state, pageId) {
601
522
  sectionSchemas[sectionName] = z.object({}).describe(section.templateDescription || '');
602
523
  }
603
524
  });
604
- // FIXME: 这里需要优化,AIGNE Framework 校验修改后,不需要序列化,直接使用
605
- // 创建包含所有section的schema并序列化返回
606
- return serializeZodSchema(z.object(sectionSchemas).describe(`Page ${page.title || pageId} sections data`));
525
+ return z.object(sectionSchemas).describe(`Page ${page.title || pageId} sections data`);
607
526
  }
608
527
  export default generateWrapperCode;
@@ -60,39 +60,66 @@ export const PROPERTIES_TYPE_SCHEMA = {
60
60
  width: z.number().optional(),
61
61
  height: z.number().optional(),
62
62
  }),
63
- json: (properties, { addZodDescribe, propLLMConfig }) => {
63
+ json: (properties, { skipAnyType, addZodDescribe, propLLMConfig, }) => {
64
64
  const { subProperties } = properties;
65
65
  if (subProperties && Object.keys(subProperties)?.length > 0) {
66
66
  // 递归处理子属性
67
- return propertiesToZodSchema(subProperties, { addZodDescribe, llmConfig: propLLMConfig?.subProperties });
67
+ return propertiesToZodSchema(subProperties, {
68
+ skipAnyType,
69
+ addZodDescribe,
70
+ llmConfig: propLLMConfig?.subProperties,
71
+ });
68
72
  }
69
73
  return z.object({});
70
74
  },
71
- yaml: (properties, { addZodDescribe, propLLMConfig }) => {
75
+ yaml: (properties, { skipAnyType, addZodDescribe, propLLMConfig, }) => {
72
76
  const { subProperties } = properties;
73
77
  if (subProperties && Object.keys(subProperties)?.length > 0) {
74
78
  // 使用联合类型:字符串或结构化对象
75
- return propertiesToZodSchema(subProperties, { addZodDescribe, llmConfig: propLLMConfig?.subProperties });
79
+ return propertiesToZodSchema(subProperties, {
80
+ skipAnyType,
81
+ addZodDescribe,
82
+ llmConfig: propLLMConfig?.subProperties,
83
+ });
76
84
  }
77
85
  return z.object({});
78
86
  },
79
- array: (properties, { addZodDescribe, propLLMConfig }) => {
87
+ array: (properties, { skipAnyType, addZodDescribe, propLLMConfig, }) => {
80
88
  const { subProperties } = properties;
81
89
  if (subProperties && Object.keys(subProperties)?.length > 0) {
82
90
  // 递归处理子属性
83
- return z.array(propertiesToZodSchema(subProperties, { addZodDescribe, llmConfig: propLLMConfig?.subProperties }));
91
+ return z.array(propertiesToZodSchema(subProperties, {
92
+ skipAnyType,
93
+ addZodDescribe,
94
+ llmConfig: propLLMConfig?.subProperties,
95
+ }));
96
+ }
97
+ if (skipAnyType) {
98
+ return null;
84
99
  }
85
100
  return z.array(z.any());
86
101
  },
87
- component: () => z.any(),
88
- custom: () => z.any(),
102
+ component: ({ skipAnyType }) => {
103
+ if (skipAnyType) {
104
+ return null;
105
+ }
106
+ return z.any();
107
+ },
108
+ custom: ({ skipAnyType }) => {
109
+ if (skipAnyType) {
110
+ return null;
111
+ }
112
+ return z.any();
113
+ },
89
114
  default: () => z.string(),
90
115
  },
91
116
  };
92
117
  /**
93
118
  * 从 Properties 对象创建 Zod schema
94
119
  */
95
- export function propertiesToZodSchema(properties, { addZodDescribe = false,
120
+ export function propertiesToZodSchema(properties, {
121
+ // 是否跳过 any 类型
122
+ skipAnyType = false, addZodDescribe = false,
96
123
  // 这个目前来说都是可选的,避免用户在使用的过程中疯狂报错,并且目前 setting 中没有必填标识符
97
124
  isOptional = true, llmConfig, } = {}) {
98
125
  const schemaObj = {};
@@ -111,7 +138,15 @@ isOptional = true, llmConfig, } = {}) {
111
138
  const getSchemaFn = (propType in PROPERTIES_TYPE_SCHEMA.propertyToZod
112
139
  ? PROPERTIES_TYPE_SCHEMA.propertyToZod[propType]
113
140
  : PROPERTIES_TYPE_SCHEMA.propertyToZod.default);
114
- schemaObj[propKey] = getSchemaFn(prop.data, { addZodDescribe, propLLMConfig });
141
+ const schema = getSchemaFn(prop.data, {
142
+ skipAnyType,
143
+ addZodDescribe,
144
+ propLLMConfig,
145
+ });
146
+ if (!schema) {
147
+ return;
148
+ }
149
+ schemaObj[propKey] = schema;
115
150
  if (isOptional) {
116
151
  schemaObj[propKey] = schemaObj[propKey].optional();
117
152
  }