yaml
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese@json-render/yaml
@json-render/yaml
YAML wire format for . Progressive rendering and surgical edits via streaming YAML.
@json-render/core适用于的YAML有线格式。通过流式YAML实现渐进式渲染和精准编辑。
@json-render/coreKey Concepts
核心概念
- YAML wire format: Alternative to JSONL that uses code fences (,
yaml-spec,yaml-edit,yaml-patch)diff - Streaming parser: Incrementally parses YAML, emits JSON Patch operations via diffing
- Edit modes: Patch (RFC 6902), merge (RFC 7396), and unified diff
- AI SDK transform: that converts YAML fences into json-render patches
TransformStream
- YAML有线格式:JSONL的替代方案,使用代码块(、
yaml-spec、yaml-edit、yaml-patch)diff - 流式解析器:增量解析YAML,通过对比差异生成JSON Patch操作
- 编辑模式:Patch(RFC 6902)、Merge(RFC 7396)和统一差异对比
- AI SDK转换:将YAML代码块转换为json-render补丁的
TransformStream
Generating YAML Prompts
生成YAML提示词
typescript
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" });Options:
- (string) — Custom system message intro
system - ("standalone" | "inline") — Output mode, default "standalone"
mode - (string[]) — Additional rules appended to prompt
customRules - (EditMode[]) — Edit modes to document, default ["merge"]
editModes
typescript
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 - ("standalone" | "inline")——输出模式,默认值为"standalone"
mode - (字符串数组)——附加到提示词的额外规则
customRules - (EditMode数组)——需要说明的编辑模式,默认值为["merge"]
editModes
AI SDK Transform
AI SDK转换
Use as a drop-in replacement for :
pipeYamlRenderpipeJsonRendertypescript
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 });For multi-turn edits, pass the previous spec:
typescript
pipeYamlRender(result.toUIMessageStream(), {
previousSpec: currentSpec,
});The transform recognizes four fence types:
- — Full spec, parsed progressively line-by-line
yaml-spec - — Partial YAML deep-merged with current spec (RFC 7396)
yaml-edit - — RFC 6902 JSON Patch lines
yaml-patch - — Unified diff applied to serialized spec
diff
使用作为的直接替代方案:
pipeYamlRenderpipeJsonRendertypescript
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 });对于多轮编辑,传入之前的规范:
typescript
pipeYamlRender(result.toUIMessageStream(), {
previousSpec: currentSpec,
});该转换支持四种代码块类型:
- ——完整规范,逐行渐进式解析
yaml-spec - ——部分YAML与当前规范深度合并(RFC 7396)
yaml-edit - ——RFC 6902 JSON Patch行
yaml-patch - ——应用于序列化规范的统一差异对比
diff
Streaming Parser (Low-Level)
流式解析器(底层)
typescript
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: {} });Methods: , , , ,
push(chunk)flush()getResult()getPatches()reset(initial?)typescript
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?)Edit Modes (from @json-render/core)
编辑模式(来自@json-render/core)
The YAML package uses the universal edit mode system from core:
typescript
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(),
});YAML包使用core中的通用编辑模式系统:
typescript
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(),
});Fence Constants
代码块常量
For custom parsing, use the exported constants:
typescript
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";对于自定义解析,使用导出的常量:
typescript
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";Key Exports
主要导出项
| Export | Description |
|---|---|
| Generate YAML system prompt from catalog |
| AI SDK TransformStream for YAML fences |
| Convenience pipe wrapper (replaces |
| Streaming YAML parser with patch emission |
| Fence constant for yaml-spec |
| Fence constant for yaml-edit |
| Fence constant for yaml-patch |
| Fence constant for diff |
| Fence close constant |
| Re-export: object diff to JSON Patch |
| Re-export: RFC 7396 deep merge |
| 导出项 | 描述 |
|---|---|
| 从目录生成YAML系统提示词 |
| 用于YAML代码块的AI SDK TransformStream |
| 便捷的管道包装器(替代 |
| 支持补丁生成的流式YAML解析器 |
| yaml-spec对应的代码块常量 |
| yaml-edit对应的代码块常量 |
| yaml-patch对应的代码块常量 |
| diff对应的代码块常量 |
| 代码块结束标记常量 |
| 重导出:对象差异转换为JSON Patch |
| 重导出:RFC 7396深度合并 |