StaffML Vault 共享类型包(@staffml/vault-types)技术指南:Schema v1.0 类型契约与跨端集成实践
2026/9/10 12:53:40 网站建设 项目流程

StaffML Vault 共享类型包(@staffml/vault-types)技术指南:Schema v1.0 类型契约与跨端集成实践

【免费下载链接】cs249r_bookMachine Learning Systems项目地址: https://gitcode.com/GitHub_Trending/cs/cs249r_book

导读

@staffml/vault-types是 StaffML 面试题仓库(位于本仓库interviews/目录下)中负责共享 TypeScript 类型契约的零依赖类型包,其全部实体定义集中在 interviews/staffml-vault-types/index.ts。它把权威的 LinkML 题目 Schema(schema_version: "1.0")与 Python 枚举翻译成纯类型,供题库站点(interviews/staffml/)与 Cloudflare Worker(interviews/staffml-vault-worker/)通过 pnpm workspace 协议统一消费。读完本文,你将掌握该包的枚举域、Question/Manifest/Visual等核心类型结构、它与其他端点的镜像/消费关系,以及它在 v1.0 中引入的人类审核(Human Review)等关键字段。

包定位:为什么需要一个"零依赖"的共享类型包

interviews/staffml-vault-types/目录下没有任何package-lock.json。根据 README.md 的说明,这是因为该包没有任何 npm 运行时依赖,只包含类型定义;只有当后续引入依赖或需要可复现安装的发布流程时,才需要新增 lockfile。

从 package.json 可以看到包的完整元信息:

  • name@staffml/vault-typesprivate: true(不发布到公共 registry);
  • version0.1.0
  • main/types:均指向index.ts——即"入口即类型",maintypes指向同一文件;
  • type: "module":包内代码按 ESM 语义处理;
  • description明确指出:包随每次 vault 发布进行版本管理("Versioned with each vault release")。

这种"纯类型、零依赖"的设计使得两端(站点与 Worker)可以共享同一份类型契约而不引入任何运行时开销,也不会造成依赖树的膨胀或版本冲突。

权威 Schema 的来源与对齐关系

index.ts文件头部的注释揭示了类型的权威来源(interviews/staffml-vault-types/index.ts):

Aligned with interviews/vault/schema/question_schema.yaml (LinkML, the authoritative schema) and interviews/vault/schema/enums.py.

也就是说:

  • 权威 Schema是 LinkML 格式的question_schema.yaml(v1.0 约定);
  • Python 枚举定义在enums.py,TypeScript 中的各枚举联合类型与之对齐;
  • 注释还提到CI drift checks against the Python enums计划在后续 PR 中落地,即用 CI 校验 TS 枚举与 Python 枚举是否漂移(drift)。

Worker 侧的镜像类型文件 interviews/staffml-vault-worker/src/types.ts 头部同样声明它是@staffml/vault-typesMirror(镜像),由 LinkML 代码生成,并在 Phase 3 通过 pnpm workspace 协议改用共享包(对应ARCHITECTURE.md§13 中针对问题 H-2 的修复)。这一关系印证了:共享类型包是"单一事实来源"的中间层,Worker 镜像只是过渡期的产物

说明:interviews/vault/schema/目录在当前仓库快照中并未直接列出,index.ts头部注释指向该路径属于权威来源的声明;本文以仓库内可确认的实体(index.ts、worker 镜像、真实题目 YAML)为准。

枚举域(Enums):题目分类的受控词表

index.ts用 TypeScript 联合类型(union types)定义了题目生命周期中所有的受控枚举域。这些枚举直接决定了题目的分类轴、状态机与来源标记。

四轴分类相关枚举

export type Track = "cloud" | "edge" | "mobile" | "tinyml" | "global"; export type Level = "L1" | "L2" | "L3" | "L4" | "L5" | "L6+"; export type Zone = | "recall" | "analyze" | "design" | "implement" | "fluency" | "diagnosis" | "specification" | "optimization" | "evaluation" | "realization" | "mastery"; export type BloomLevel = | "remember" | "understand" | "apply" | "analyze" | "evaluate" | "create"; export type Phase = "training" | "inference" | "both";
  • Track:题目所属的技术栈/部署域,对应仓库中的interviews/vault/questions/<track>/目录划分(cloud / edge / mobile / tinyml),外加全局题(global);
  • Level:难度分级,L1L5,另有最高档L6+("L6 及以上"),属于半开区间式表达;
  • Zone:11 个取值的能力域/题型域,从基础的recallanalyzeoptimizationmastery,覆盖"回忆—分析—设计—实现—熟练—诊断—规格—优化—评估—落地—精通"的完整能力谱系;
  • BloomLevel:布鲁姆教育目标分类学(修订版)的六层动词:remember / understand / apply / analyze / evaluate / create,用于标注题目所考察的认知层级;
  • Phase:机器学习生命周期阶段,仅三个取值:training(训练)、inference(推理)、both(两者皆涉及)。

工作流与来源相关枚举

export type Status = "draft" | "published" | "flagged" | "archived" | "deleted"; export type Provenance = | "human" | "llm-draft" | "llm-then-human-edited" | "imported"; export type HumanReviewStatus = | "not-reviewed" | "verified" | "flagged" | "needs-rework";
  • Status:题目的生命周期状态机——草稿 → 已发布 → 被标记 → 归档 → 删除;
  • Provenance:题目的来源标记,区分纯人工(human)、LLM 初稿(llm-draft)、LLM 初稿后人工编辑(llm-then-human-edited)以及批量导入(imported)。这是对生成式内容治理的关键元数据;
  • HumanReviewStatus:v1.0 新引入的人类审核状态(详见下文HumanReview接口)。

嵌套类型:从资源到可视化附件的结构化设计

Question主接口之前,index.ts定义了 5 个嵌套类型,用于承载题目的组成部分:

ChainRef:链式题目引用

export interface ChainRef { id: string; position: number; }

id指向题库中某道题(如cloud-0231),position表示该题在链(chain)中的顺序位置。题目通过chains数组可以同时属于多条链(见Question接口中的chains?: ChainRef[])。

Resource:外部参考资料

export interface Resource { name: string; url: string; }

提供题目相关的参考资料条目(名称 + URL),挂在QuestionDetails.resources下。

HumanReview:v1.0 的人类审核记录

export interface HumanReview { status: HumanReviewStatus; by?: string | null; date?: string | null; notes?: string | null; }

这是 v1.0 引入的关键治理字段:status使用受控枚举(not-reviewed/verified/flagged/needs-rework),by/date/notes分别记录审核人、审核日期与备注,均可为空(null)。在真实题目数据中,该结构体现为:

human_reviewed: status: not-reviewed by: null date: null notes: null

(示例见 interviews/vault/questions/cloud/architecture/cloud-0231.yaml 第 42-46 行。)

Visual 与 VisualKind:可视化附件(v0.1.2 加固)

export type VisualKind = "svg"; export interface Visual { kind: VisualKind; path: string; // 仅文件名,位于 interviews/vault/visuals/<track>/ alt: string; // 无障碍描述:≥10 字符,≤400 字符 caption: string; // 面向作者的图注:≥5 字符,≤120 字符 }

Visual在 v0.1.2 被"加固(hardened)":

  • kind封闭枚举,当前仅支持"svg"
  • path必须匹配^[a-z0-9-]+\.svg$(纯小写字母数字加连字符、以.svg结尾),且只存裸文件名,实际文件位于interviews/vault/visuals/<track>/目录下;
  • alt(无障碍描述)至少 10 字符、最多 400 字符;caption(图注)至少 5 字符、最多 120 字符;
  • 注释明确指出:服务端由 Pydantic 强制校验这些约束,该接口只是练习页(practice page)消费的形状(shape)。

也就是说,TS 类型只描述"练习页该拿到什么",真正的约束由服务端 Pydantic 模型兜底执行——类型契约与运行时校验各司其职。

QuestionDetails:解题细节容器

export interface QuestionDetails { realistic_solution: string; // 正解/现实解法 common_mistake?: string; // 常见误区(可选) napkin_math?: string; // 估算/纸上计算(可选) resources?: Resource[]; // 参考资料(可选) options?: string[]; // 选项(用于选择题) correct_index?: number; // 正确选项下标 }

从真实题目(cloud-0231.yaml)可以看到,realistic_solution给出工程级正解(如 KV-Cache 内存墙分析),common_mistake以结构化 Markdown 记录"陷阱—理由—后果",napkin_math则以"假设/约束 → 计算 → 结论"三段式组织纸上估算。options/correct_index则支持单选题形态。

Question 主接口:v1.0 题目的完整形态

export interface Question { schema_version: string; // "1.0" id: string; // 4-axis classification track: Track; level: Level; zone: Zone; topic: string; competency_area: string; bloom_level?: BloomLevel; phase?: Phase; // Content title: string; scenario: string; question?: string; visual?: Visual; details: QuestionDetails; // Workflow status: Status; provenance: Provenance; requires_explanation?: boolean; expected_time_minutes?: number; deletion_reason?: string; // Chain membership (plural — a question may belong to multiple chains) chains?: ChainRef[]; // LLM validation validated?: boolean; validation_status?: string; validation_date?: string; validation_model?: string; // Math validation (separate LLM pass) math_verified?: boolean; math_status?: string; math_date?: string; math_model?: string; // Human review (new in v1.0) human_reviewed?: HumanReview; // Free-form classification_review?: string; authors?: string[]; tags?: string[]; created_at?: string; updated_at?: string; last_modified?: string; }

Question接口可以归纳为六个区块:

  1. 版本与标识schema_version(约定为"1.0")与全局唯一id
  2. 四轴分类(4-axis classification)tracklevelzone三个受控枚举轴,加topic(主题)、competency_area(能力域),以及可选的bloom_levelphase
  3. 内容titlescenario(场景)、可选的question(问题正文)、可选的visual(可视化附件)以及必填的details
  4. 工作流状态statusprovenance,以及可选的requires_explanationexpected_time_minutesdeletion_reason
  5. 质量验证:三组并行的验证轨道——LLM 通用验证(validated/validation_status/validation_date/validation_model)、独立的数学验证 passmath_verified/math_status/math_date/math_model)、以及 v1.0 新增的人类审核(human_reviewed)。这体现了"LLM 初筛 + 数学复核 + 人工把关"的三级质量体系;
  6. 自由元数据classification_review(分类复核意见)、authorstagscreated_atupdated_atlast_modified

与真实 YAML 数据的逐字段印证

以 interviews/vault/questions/cloud/architecture/cloud-0231.yaml 为样本对照:

schema_version: '1.0' id: cloud-0231 track: cloud level: L4 zone: optimization topic: attention-scaling competency_area: architecture bloom_level: analyze phase: both title: The KV-Cache Context Explosion scenario: You are serving a Llama-3 8B model. ... status: published provenance: imported requires_explanation: false expected_time_minutes: 10 validated: true validation_status: OK validation_date: '2026-04-01' validation_model: gemini-2.5-flash math_verified: true math_status: CORRECT math_date: '2026-04-03' math_model: gemini-3.1-pro-preview human_reviewed: status: not-reviewed

可见真实数据完整覆盖了类型契约中的分类轴、内容、工作流与三级验证字段,index.ts中标注为可选的字段(如questionvisual)在该样本中按需省略——这正是 TS 可选标记(?)对 YAML 稀疏结构的精确建模。

Manifest 与 API 客户端选项:发布清单与调用端配置

Manifest:发布清单

export interface Manifest { release_id: string; release_hash: string; schema_version: string; // "1.0" policy_version: string; published_count: number; schema_fingerprint_ok: boolean; }

Manifest描述一次 vault 发布的快照:release_id(发布号)与release_hash(内容哈希)用于版本可追溯;schema_version固定为"1.0"policy_version记录治理策略版本;published_count为已发布题目数量;schema_fingerprint_ok表示 schema 指纹校验是否通过。Worker 侧镜像 interviews/staffml-vault-worker/src/types.ts 中的Manifest接口与之逐字段一致,并配套Env环境变量中的SCHEMA_FINGPRINT(源码中拼写为SCHEMA_FINGERPRINT)与缓存 TTL 配置(CACHE_TTL_MANIFEST等)。

VaultApiClientOptions:客户端容错配置

export interface VaultApiClientOptions { release: string; retry?: { attempts: number; backoff: "exponential" | "linear"; jitter?: boolean }; circuitBreaker?: { failThreshold: number; resetMs: number }; headers?: Record<string, string>; }

该接口为 vault API 客户端提供配置约定:必填release(目标发布);可选retry支持指数/线性退避并可加 jitter(抖动)防惊群;可选circuitBreaker支持熔断(失败阈值 + 重置毫秒数);headers用于注入自定义请求头。

跨端集成:pnpm workspace 协议与消费方式

站点侧 interviews/staffml/tsconfig.json 中的 paths 映射展示了实际的消费方式:

"@staffml/vault-types": [ "../staffml-vault-types/index.ts" ]

即通过路径别名把@staffml/vault-types直接指向共享包入口index.ts,从而在 TypeScript 编译期共享类型。而index.ts头部注释与 Worker 镜像注释共同说明:站点与 Worker 都通过 pnpm workspace 协议引入该包interviews/staffml-vault-worker/package.json中未重复声明该依赖,符合 workspace 依赖提升/隐式解析的约定)。

关键设计要点与工程启示

  1. 类型即契约,Schema 单一事实来源index.ts是 LinkML 权威 Schema(v1.0)与 Python 枚举在 TS 侧的投影;Worker 的types.ts仅是"过渡期镜像",最终方向是统一消费共享包,并通过 CI drift check 防止枚举漂移。
  2. 零依赖、纯类型、随发布版本化main/types均指向index.ts,无 lockfile、无运行时依赖;包版本(当前 0.1.0)与每次 vault 发布绑定,保证站点与 Worker 拿到同一份契约。
  3. 封闭枚举 + 服务端兜底校验VisualKind"svg"path有正则约束、alt/caption有长度约束,且由服务端 Pydantic 强制执行;TS 类型负责"形状契约",运行时校验负责"数据安全",两者分层清晰。
  4. 三级质量验证轨道:LLM 通用验证、独立数学验证、人类审核并行存在(v1.0 起),并通过human_reviewed字段与 LLM 验证解耦——HumanReviewStatusvalidated是两套独立的状态机。
  5. 多链归属与稀疏可选字段:一道题可同时属于多条链(chains: ChainRef[]);真实 YAML 中大量字段按需省略,TS 的可选标记精确建模了这一稀疏结构。

延伸阅读

  • 类型包入口:interviews/staffml-vault-types/index.ts
  • 包元信息:interviews/staffml-vault-types/package.json
  • Worker 侧镜像类型:interviews/staffml-vault-worker/src/types.ts
  • 站点消费配置:interviews/staffml/tsconfig.json
  • 真实题目数据样本:interviews/vault/questions/cloud/architecture/cloud-0231.yaml
  • 题库目录:interviews/vault/questions/(cloud / edge / mobile / tinyml / global 五个 track,与Track枚举一一对应)

【免费下载链接】cs249r_bookMachine Learning Systems项目地址: https://gitcode.com/GitHub_Trending/cs/cs249r_book

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询