mcpField
Y
yaml

yaml

github.com/vercel-labs开发18.8k1.0k 次安装5 天前更新
代码仓库

关于 yaml

@json-render/yaml 为 @json-render/core 提供 YAML 线格式,可增量解析流式 UI 规范并支持多种编辑方式。开发者可生成 YAML 提示词,或将 AI SDK 流转换为渲染补丁。

主要功能

  1. 01根据目录生成 YAML 提示词
  2. 02增量解析 YAML 流
  3. 03支持补丁、合并和差异编辑
  4. 04AI SDK 流转换
  5. 05生成 JSON Patch 操作

适用场景

  1. 01渐进式传输 UI 规范
  2. 02对现有规范应用局部编辑
  3. 03转换 AI SDK 流中的 YAML 代码围栏
  4. 04将 YAML 数据块编译为渲染补丁

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/vercel-labs/json-render --skill yaml

安装后在 Claude Code 中输入/yaml确认已加载。

详细介绍

@json-render/yaml

适用于 @json-render/core 的 YAML 线格式。通过流式 YAML 实现渐进式渲染和精准编辑。

核心概念

  • YAML 线格式:JSONL 的替代格式,使用代码围栏(yaml-spec、yaml-edit、yaml-patch、diff)
  • 流式解析器:增量解析 YAML,通过差异比较发出 JSON Patch 操作
  • 编辑模式:Patch(RFC 6902)、merge(RFC 7396)和 unified diff
  • AI SDK 转换器:将 YAML 围栏转换为 json-render patches 的 TransformStream

生成 YAML 提示词

import { yamlPrompt } from "@json-render/yaml";
import { catalog } from "./catalog";

// Standalone mode (LLM outputs only YAML)
const systemPrompt = yamlPrompt(catalog, {
  mode: "standalone",
  editModes: ["merge"],
  customRules: ["Always use dark theme"],
});

// Inline mode (LLM responds conversationally, wraps YAML in fences)
const chatPrompt = yamlPrompt(catalog, { mode: "inline" });

选项:

  • system(字符串)— 自定义系统消息引言
  • mode("standalone" | "inline")— 输出模式,默认值为 "standalone"
  • customRules(字符串[])— 追加到提示词中的其他规则
  • editModes(EditMode[])— 要说明的编辑模式,默认值为 ["merge"]

AI SDK 转换器

使用 pipeYamlRender 替代 pipeJsonRender:

import { pipeYamlRender } from "@json-render/yaml";
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";

const stream = createUIMessageStream({
  execute: async ({ writer }) => {
    writer.merge(pipeYamlRender(result.toUIMessageStream()));
  },
});
return createUIMessageStreamResponse({ stream });

对于多轮编辑,传入之前的 spec:

pipeYamlRender(result.toUIMessageStream(), {
  previousSpec: currentSpec,
});

转换器可识别四种围栏类型:

  • yaml-spec — 完整 spec,按行渐进式解析
  • yaml-edit — 与当前 spec 进行深度合并的部分 YAML(RFC 7396)
  • yaml-patch — RFC 6902 JSON Patch 行;应用于工作中的 spec,因此后续的 yaml-edit 围栏可以看到它们
  • diff — 将统一差异应用于序列化后的 spec

流式解析器(底层)

import { createYamlStreamCompiler } from "@json-render/yaml";

const compiler = createYamlStreamCompiler<Spec>();

// Feed chunks as they arrive from any source
const { result, newPatches } = compiler.push("root: main\n");
compiler.push("elements:\n  main:\n    type: Card\n");

// Flush remaining data at end of stream
const { result: final } = compiler.flush();

// Reset for next stream (optionally with initial state)
compiler.reset({ root: "main", elements: {} });

方法:push(chunk)、flush()、getResult()、getPatches()、reset(initial?)

编辑模式(来自 @json-render/core)

YAML 包使用 core 中的通用编辑模式系统:

import { buildEditInstructions, buildEditUserPrompt } from "@json-render/core";
import type { EditMode } from "@json-render/core";

// Generate edit instructions for YAML format
const instructions = buildEditInstructions({ modes: ["merge", "patch"] }, "yaml");

// Build user prompt with current spec context
const userPrompt = buildEditUserPrompt({
  prompt: "Change the title to Dashboard",
  currentSpec: spec,
  config: { modes: ["merge"] },
  format: "yaml",
  serializer: (s) => yamlStringify(s, { indent: 2 }).trimEnd(),
});

围栏常量

自定义解析时,请使用导出的常量:

import {
  YAML_SPEC_FENCE,   // "```yaml-spec"
  YAML_EDIT_FENCE,   // "```yaml-edit"
  YAML_PATCH_FENCE,  // "```yaml-patch"
  DIFF_FENCE,        // "```diff"
  FENCE_CLOSE,       // "```"
} from "@json-render/yaml";

主要导出项

导出项描述
yamlPrompt根据目录生成 YAML 系统提示词
createYamlTransform用于 YAML 围栏的 AI SDK TransformStream
pipeYamlRender便捷的 pipe 封装(替代 pipeJsonRender)
createYamlStreamCompiler支持发出 patch 的流式 YAML 解析器
YAML_SPEC_FENCEyaml-spec 的围栏常量
YAML_EDIT_FENCEyaml-edit 的围栏常量
YAML_PATCH_FENCEyaml-patch 的围栏常量
DIFF_FENCEdiff 的围栏常量
FENCE_CLOSE围栏结束常量
diffToPatches重新导出:将对象差异转换为 JSON Patch
deepMergeSpec重新导出:RFC 7396 深度合并

评论

支持 Markdown