page.mdx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { pageMetadata } from "@/lib/page-metadata"
  2. export const metadata = pageMetadata("docs/api/yaml")
  3. # @json-render/yaml
  4. YAML wire format for json-render. Progressive rendering and surgical edits via streaming YAML.
  5. ## Prompt Generation
  6. ### yamlPrompt
  7. Generate a YAML-format system prompt from any json-render catalog. Works with catalogs from any renderer.
  8. ```typescript
  9. function yamlPrompt(
  10. catalog: Catalog,
  11. options?: YamlPromptOptions
  12. ): string
  13. ```
  14. ```typescript
  15. import { yamlPrompt } from "@json-render/yaml";
  16. const systemPrompt = yamlPrompt(catalog, {
  17. mode: "standalone",
  18. customRules: ["Always use dark theme"],
  19. editModes: ["merge"],
  20. });
  21. ```
  22. ### YamlPromptOptions
  23. <table>
  24. <thead>
  25. <tr>
  26. <th>Option</th>
  27. <th>Type</th>
  28. <th>Default</th>
  29. <th>Description</th>
  30. </tr>
  31. </thead>
  32. <tbody>
  33. <tr>
  34. <td><code>system</code></td>
  35. <td><code>string</code></td>
  36. <td><code>{'\"You are a UI generator that outputs YAML.\"'}</code></td>
  37. <td>Custom system message intro</td>
  38. </tr>
  39. <tr>
  40. <td><code>mode</code></td>
  41. <td><code>{'\"standalone\" | \"inline\"'}</code></td>
  42. <td><code>{'\"standalone\"'}</code></td>
  43. <td>Standalone outputs only YAML; inline allows conversational responses with embedded YAML fences</td>
  44. </tr>
  45. <tr>
  46. <td><code>customRules</code></td>
  47. <td><code>{'string[]'}</code></td>
  48. <td><code>{'[]'}</code></td>
  49. <td>Additional rules appended to the prompt</td>
  50. </tr>
  51. <tr>
  52. <td><code>editModes</code></td>
  53. <td><code>{'EditMode[]'}</code></td>
  54. <td><code>{'[\"merge\"]'}</code></td>
  55. <td>Edit modes to document in the prompt (patch, merge, diff)</td>
  56. </tr>
  57. </tbody>
  58. </table>
  59. ## AI SDK Transform
  60. ### createYamlTransform
  61. Creates a `TransformStream` that intercepts AI SDK stream chunks and converts YAML spec/edit blocks into json-render patch data parts.
  62. ```typescript
  63. function createYamlTransform(
  64. options?: YamlTransformOptions
  65. ): TransformStream<StreamChunk, StreamChunk>
  66. ```
  67. Recognized fence types:
  68. - <code>{'```yaml-spec'}</code> -- Full YAML spec, parsed progressively
  69. - <code>{'```yaml-edit'}</code> -- Partial YAML, deep-merged with current spec
  70. - <code>{'```yaml-patch'}</code> -- RFC 6902 JSON Patch lines
  71. - <code>{'```diff'}</code> -- Unified diff against serialized spec
  72. ### pipeYamlRender
  73. Convenience wrapper that pipes an AI SDK stream through the YAML transform. Drop-in replacement for `pipeJsonRender` from `@json-render/core`.
  74. ```typescript
  75. function pipeYamlRender<T>(
  76. stream: ReadableStream<T>,
  77. options?: YamlTransformOptions
  78. ): ReadableStream<T>
  79. ```
  80. ```typescript
  81. import { pipeYamlRender } from "@json-render/yaml";
  82. import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
  83. const stream = createUIMessageStream({
  84. execute: async ({ writer }) => {
  85. writer.merge(pipeYamlRender(result.toUIMessageStream()));
  86. },
  87. });
  88. return createUIMessageStreamResponse({ stream });
  89. ```
  90. ## Streaming Parser
  91. ### createYamlStreamCompiler
  92. Create a streaming YAML compiler that incrementally parses YAML text and emits JSON Patch operations by diffing each successful parse against the previous snapshot.
  93. ```typescript
  94. function createYamlStreamCompiler<T>(
  95. initial?: Partial<T>
  96. ): YamlStreamCompiler<T>
  97. ```
  98. ```typescript
  99. import { createYamlStreamCompiler } from "@json-render/yaml";
  100. const compiler = createYamlStreamCompiler<Spec>();
  101. compiler.push("root: main\n");
  102. compiler.push("elements:\n main:\n type: Card\n");
  103. const { result, newPatches } = compiler.flush();
  104. ```
  105. ### YamlStreamCompiler
  106. <table>
  107. <thead>
  108. <tr>
  109. <th>Method</th>
  110. <th>Returns</th>
  111. <th>Description</th>
  112. </tr>
  113. </thead>
  114. <tbody>
  115. <tr>
  116. <td><code>push(chunk)</code></td>
  117. <td><code>{'{ result: T; newPatches: JsonPatch[] }'}</code></td>
  118. <td>Push a chunk of text, returns current result and new patches</td>
  119. </tr>
  120. <tr>
  121. <td><code>flush()</code></td>
  122. <td><code>{'{ result: T; newPatches: JsonPatch[] }'}</code></td>
  123. <td>Flush remaining buffer, return final result</td>
  124. </tr>
  125. <tr>
  126. <td><code>getResult()</code></td>
  127. <td><code>T</code></td>
  128. <td>Get the current compiled result</td>
  129. </tr>
  130. <tr>
  131. <td><code>getPatches()</code></td>
  132. <td><code>{'JsonPatch[]'}</code></td>
  133. <td>Get all patches applied so far</td>
  134. </tr>
  135. <tr>
  136. <td><code>reset(initial?)</code></td>
  137. <td><code>void</code></td>
  138. <td>Reset to initial state</td>
  139. </tr>
  140. </tbody>
  141. </table>
  142. ## Fence Constants
  143. Exported string constants for fence detection in custom parsers:
  144. <table>
  145. <thead>
  146. <tr>
  147. <th>Constant</th>
  148. <th>Value</th>
  149. </tr>
  150. </thead>
  151. <tbody>
  152. <tr>
  153. <td><code>YAML_SPEC_FENCE</code></td>
  154. <td><code>{'\"```yaml-spec\"'}</code></td>
  155. </tr>
  156. <tr>
  157. <td><code>YAML_EDIT_FENCE</code></td>
  158. <td><code>{'\"```yaml-edit\"'}</code></td>
  159. </tr>
  160. <tr>
  161. <td><code>YAML_PATCH_FENCE</code></td>
  162. <td><code>{'\"```yaml-patch\"'}</code></td>
  163. </tr>
  164. <tr>
  165. <td><code>DIFF_FENCE</code></td>
  166. <td><code>{'\"```diff\"'}</code></td>
  167. </tr>
  168. <tr>
  169. <td><code>FENCE_CLOSE</code></td>
  170. <td><code>{'\"```\"'}</code></td>
  171. </tr>
  172. </tbody>
  173. </table>
  174. ## Re-exports from @json-render/core
  175. ### diffToPatches
  176. Generate RFC 6902 JSON Patch operations that transform one object into another.
  177. ```typescript
  178. function diffToPatches(
  179. oldObj: Record<string, unknown>,
  180. newObj: Record<string, unknown>,
  181. basePath?: string
  182. ): JsonPatch[]
  183. ```
  184. ### deepMergeSpec
  185. Deep-merge with RFC 7396 semantics: `null` deletes, arrays replace, objects recurse.
  186. ```typescript
  187. function deepMergeSpec(
  188. base: Record<string, unknown>,
  189. patch: Record<string, unknown>
  190. ): Record<string, unknown>
  191. ```