核心包开发Skill core-development

本技能是关于一个无依赖的核心软件开发包的开发指南。该核心包专门处理领域特定语言(DSL)的完整处理流程,包括类型定义、输入验证、数据规范化、差异计算和补丁生成。核心功能是实现从YAML格式的DSL输入到内部表示(IR)的转换,并计算不同版本间的差异以生成可应用的补丁。适用于构建低代码开发平台、可视化编程工具或需要动态数据流处理的系统。关键词:DSL处理,核心包开发,类型验证,数据规范化,差异计算,补丁生成,低代码开发,数据流处理。

低代码开发 0 次安装 0 次浏览 更新于 2/28/2026

name: core-development description: 核心包开发工作(类型、验证、规范化、差异计算)。用于修改DSL处理逻辑或数据流时使用。

核心包开发

核心包 (packages/core/) 是无依赖的,负责处理所有DSL处理。

数据流

DSL (YAML输入) → validate() → normalize() → IR → diff() → 补丁

关键文件

文件 用途 导出内容
types.ts 类型定义 DSL*, IR*, 补丁, WebSocket协议
validate.ts YAML验证 validate(dsl): ValidationResult
normalize.ts DSL → IR 转换 normalize(dsl): IRDocument
diff.ts IR 差异计算 diff(prev, next): Patch

类型层次结构

DSL 类型 (用户输入)      IR 类型 (规范化后)
─────────────────────       ────────────────────
DSLDocument                 IRDocument
  ├─ version: number          ├─ version: number
  ├─ docId: string            ├─ docId: string
  ├─ title?: string           ├─ title: string
  ├─ nodes: DSLNode[]         ├─ nodes: Record<string, IRNode>
  └─ edges?: DSLEdge[]        └─ edges: Record<string, IREdge>

DSLNode                     IRNode
  ├─ id: string               ├─ id: string
  ├─ provider: string         ├─ provider: string
  ├─ kind: string             ├─ kind: string
  ├─ label?: string           ├─ label: string (默认: id)
  ├─ parent?: string          ├─ parent: string | null
  └─ layout: DSLLayout        └─ layout: { x, y, w, h }

DSLEdge                     IREdge
  ├─ id: string               ├─ id: string
  ├─ from: string             ├─ from: string
  ├─ to: string               ├─ to: string
  └─ label?: string           └─ label: string (默认: "")

补丁操作

type PatchOp =
  | { op: "upsertNode"; node: IRNode }
  | { op: "removeNode"; id: string }
  | { op: "upsertEdge"; edge: IREdge }
  | { op: "removeEdge"; id: string };

interface Patch {
  baseRev: number;
  nextRev: number;
  ops: PatchOp[];
}

WebSocket 协议类型

// 插件 → CLI
interface HelloMessage {
  type: "hello";
  docId: string;
  secret?: string;
}

interface RequestFullMessage {
  type: "requestFull";
  docId: string;
}

// CLI → 插件
interface FullMessage {
  type: "full";
  rev: number;
  ir: IRDocument;
}

interface PatchMessage {
  type: "patch";
  baseRev: number;
  nextRev: number;
  ops: PatchOp[];
}

interface ErrorMessage {
  type: "error";
  message: string;
}

开发工作流

  1. 修改类型 → 更新 types.ts
  2. 更新验证 → 确保 validate.ts 捕获无效输入
  3. 更新规范化 → 在 normalize.ts 中处理新字段/默认值
  4. 更新差异计算 → 在 diff.ts 中处理新的补丁场景
  5. 添加测试 → 同目录下的 *.test.ts 文件
  6. 运行测试bun test packages/core/

测试

# 所有核心测试
bun test packages/core/

# 特定测试文件
bun test packages/core/src/diff.test.ts
bun test packages/core/src/validate.test.ts
bun test packages/core/src/normalize.test.ts

# 监视模式
bun test --watch packages/core/

常见模式

添加新节点属性

  1. types.ts 中添加到 DSLNodeIRNode
  2. validate.ts 中添加验证
  3. normalize.ts 中添加默认值处理
  4. 如果属性影响相等性判断,则更新差异计算逻辑
  5. 为验证、规范化和差异计算添加测试用例

添加新边属性

  1. types.ts 中添加到 DSLEdgeIREdge
  2. validate.ts 中添加验证
  3. normalize.ts 中添加默认值处理
  4. 更新边相等性检查的差异计算逻辑
  5. 添加测试用例