大模型应用
使用 @json-render/yaml 时:通过…
当在 @json-render 生态中需要用 YAML 围栏流式生成/编辑 spec 并接入 AI SDK 时,用本技能生成提示、转换流与解析补丁。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 38/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15
适用场景
使用 @json-render/yaml特定
解决思路
- 使用 YAML 线格式替代 JSONL:通过代码围栏 `yaml-spec`、`yaml-edit`、`yaml-patch`、`diff` 传输内容。
- 使用流式解析器:逐步解析 YAML,并通过 diff 发出 JSON Patch 操作。
- 使用编辑模式:支持 Patch(RFC 6902)、merge(RFC 7396)和 unified diff。
- 使用 AI SDK transform:通过 `TransformStream` 将 YAML 围栏转换为 json-render patches。
- 生成 YAML 提示时,从 `@json-render/yaml` 导入 `yamlPrompt`,并从 `./catalog` 导入 `catalog`。
- 使用 standalone 模式(LLM 仅输出 YAML):调用 `yamlPrompt(catalog, { mode: "standalone", editModes: ["merge"], customRules: ["Always use dark theme"] })`。
- 使用 inline 模式(LLM 对话式回复,把 YAML 包在围栏中):调用 `yamlPrompt(catalog, { mode: "inline" })`。
- 通过 `system`(string)设置自定义 system 消息开头。
- 通过 `mode`("standalone" | "inline")设置输出模式,默认 "standalone"。
- 通过 `customRules`(string[])追加额外规则到提示中。
- 通过 `editModes`(EditMode[])指定要写入提示的编辑模式,默认 ["merge"]。
- 在 AI SDK 中,用 `pipeYamlRender` 作为 `pipeJsonRender` 的即插即用替代。
- 创建 UI message stream:从 `@json-render/yaml` 导入 `pipeYamlRender`,从 `ai` 导入 `createUIMessageStream`、`createUIMessageStreamResponse`;在 `execute` 中调用 `writer.merge(pipeYamlRender(result.toUIMessageStream()))`,再返回 `createUIMessageStreamResponse({ stream })`。
- 多轮编辑时,给 `pipeYamlRender` 传上一版 spec:`pipeYamlRender(result.toUIMessageStream(), { previousSpec: currentSpec })`。
- 让 transform 识别四种围栏类型:`yaml-spec` 表示完整 spec,逐行渐进解析。
- `yaml-edit` 表示部分 YAML,与当前 spec 做 RFC 7396 深合并。
- `yaml-patch` 表示 RFC 6902 JSON Patch 行。
- `diff` 表示应用到序列化 spec 的 unified diff。
- 低层流式解析时,从 `@json-render/yaml` 导入 `createYamlStreamCompiler`。
- 创建编译器:`const compiler = createYamlStreamCompiler<Spec>();`
- 从任意来源按 chunk 喂入数据:`const { result, newPatches } = compiler.push("root: main\n"); compiler.push("elements:\n main:\n type: Card\n");`
- 流结束时 flush 剩余数据:`const { result: final } = compiler.flush();`
- 为下一轮流重置编译器,可选传入初始状态:`compiler.reset({ root: "main", elements: {} });`
- 使用编译器方法:`push(chunk)`、`flush()`、`getResult()`、`getPatches()`、`reset(initial?)`。
- 编辑模式复用 `@json-render/core` 的通用编辑模式系统。
- 从 `@json-render/core` 导入 `buildEditInstructions`、`buildEditUserPrompt`,并导入类型 `EditMode`。
- 为 YAML 格式生成编辑指令:`const instructions = buildEditInstructions({ modes: ["merge", "patch"] }, "yaml");`
- 构建带当前 spec 上下文的用户提示:`const userPrompt = buildEditUserPrompt({ prompt: "Change the title to Dashboard", currentSpec: spec, config: { modes: ["merge"] }, format: "yaml", serializer: (s) => yamlStringify(s, { indent: 2 }).trimEnd() });`
- 自定义解析时使用导出的围栏常量:`YAML_SPEC_FENCE`、`YAML_EDIT_FENCE`、`YAML_PATCH_FENCE`、`DIFF_FENCE`、`FENCE_CLOSE`。
- 从 `@json-render/yaml` 导入这些常量,其值分别为 "```yaml-spec"、"```yaml-edit"、"```yaml-patch"、"```diff"、"```"。
- 使用关键导出 `yamlPrompt`:从 catalog 生成 YAML system prompt。
- 使用 `createYamlTransform`:用于 YAML 围栏的 AI SDK TransformStream。
- 使用 `pipeYamlRender`:便捷 pipe wrapper,替代 `pipeJsonRender`。
- 使用 `createYamlStreamCompiler`:带 patch 发出的流式 YAML 解析器。
- 使用 `YAML_SPEC_FENCE`:yaml-spec 的围栏常量。
- 使用 `YAML_EDIT_FENCE`:yaml-edit 的围栏常量。
- 使用 `YAML_PATCH_FENCE`:yaml-patch 的围栏常量。
- 使用 `DIFF_FENCE`:diff 的围栏常量。
- 使用 `FENCE_CLOSE`:围栏关闭常量。
- 使用 `diffToPatches`:重导出,将对象 diff 转 JSON Patch。
- 使用 `deepMergeSpec`:重导出,执行 RFC 7396 深合并。
适用边界 · 注意事项
- 项目未使用 @json-render/core、@json-render/yaml 或 AI SDK 的 UIMessageStream 时,不适用。
- 不需要流式渲染、YAML 围栏解析或增量编辑时,直接用 JSON/JSONL 更简单。
- 没有多轮编辑的 previousSpec 或 merge/patch/diff 需求时,不必引入 YAML transform 和编辑模式。