Skip to content

fix(playground): harden jsonPatch apply and component id assignment - #222

Open
yy-wow wants to merge 15 commits into
devfrom
fix/json-patch-ids-apply-failed
Open

fix(playground): harden jsonPatch apply and component id assignment#222
yy-wow wants to merge 15 commits into
devfrom
fix/json-patch-ids-apply-failed

Conversation

@yy-wow

@yy-wow yy-wow commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

背景

模板对话里,通过增量修改(json-patch)来改页面 schema。大模型输出的 op 有时会出现 id 转换路径后对应不上,导致解析失败。

旧逻辑容易出现如下问题:

  1. 页面卡住或报错:历史会话打开时会执行所有卡片里的 jsonPatch 以确定哪些卡片解析失败,用来显示标记。dev 上的 jsonPatchFormatter.patch 没有捕获错误,报错后会导致卡死。
  2. 流式过程中 id 生成不正确:为了解决流式输出中 id 抖动的问题,之前的方案是按树位置对比复用上一帧已有的 id 。在插入新节点时容易错位复用 id,导致出现不同组件拥有重复的 id,后续对话中很容易找错节点导致生成结果的错乱。

问题分析

1. 解析失败的 patch 怎么处理

  • 版本历史列表以前会跟着选中卡片重算。用户只是点开某个卡片,却可能触发整段历史重跑,这时有报错就会导致历史记录卡住不显示。

2. 失败信息记在哪

以前用组件外的 errorMessagesMap 记失败。问题是:

  • 它不属于会话消息本身,刷新或换会话后容易丢;
  • 卡片要额外传这个 Map 才能显示失败;
  • 想判断某张旧卡是否失败,需要再把 patch 重放一遍。

3. 组件 id 什么时候生成

流式每一帧都补 id,或者按旧树位置借 id,都会让预览不稳定。id 只用在下次大模型生成 op 时做定位用,应该在生成结束、确认没失败后再一次性补齐;已有且不重复的 id 保留,缺的或重复的再生成。

解决方案

1. 解析失败的 patch:全部 op 应用成功才算解析成功

  • 应用 patch 时加上异常捕获;任一 op 找不到有效路径,则为解析失败。
  • 开发调试面板里解析失败时,解析结果展示为空,延续使用上一个对话的结果。
  • 版本历史收集不再依赖“选中卡片”,点击卡片只修改预览 schema,不重跑整段历史。

2. 卡片新增标记字段:applyFailed

  • 给 jsonPatch 消息增加 applyFailed 字段,流式过程中应用成功/失败,直接写到对应卡片。
  • 失败时:不把错误结果写到卡片的结果 schema,但仍保留修改前的基准;预览回退到修改前。
  • 打开旧会话时,扫描一次所有卡片,没有标记字段的补上。
  • 卡片 UI 直接看 applyFailed 显示失败,不再依赖 errorMessagesMap。

3. 组件 id:生成结束后一次性补齐

  • 流式阶段只更新预览内容,不补 id。
  • 收到结束通知且这轮没有失败时,对预览 schema 一次性补 id,再设为当前结果 schema,并写回卡片。
  • 去掉“按旧树位置复用 id”的逻辑;只保留已有且不重复的 id,缺失或重复的重新生成。

其他修改

  • 结束收尾时,如果当前卡 id 暂时拿不到,会回退找还在生成中的卡片,避免漏写失败标记。
  • 去掉历史列表里没用的“当前项高亮”相关死代码。
  • 折叠箭头图标抽成独立 svg。

Prevent duplicate ids on insert, persist applyFailed for history cards, and stop swallowing format errors so failed patches fail closed without crashing debug UI.

Co-authored-by: Cursor <cursoragent@cursor.com>
@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

The update tracks JSON patch application failures through streaming, persisted conversations, schema cards, and rendering. It also preserves schema IDs, separates version-history current marking, hardens patch formatting, and replaces the history-panel chevron asset.

Changes

Schema state and rendering

Layer / File(s) Summary
Schema identity and version history
sites/playground/web/src/components/genui-template/template-chat-utils/schema-id-generator.ts, .../schema-version-history.ts, .../composables/use-template-actions.ts, .../composables/use-template-version-control.ts
Schema generation preserves unique existing IDs, previews use generated IDs, and current version marking is applied separately before grouping.
JSON patch failure detection and state
sites/playground/web/src/components/genui-template/template-chat-utils/conversation-schema.ts, .../json-patch-format.ts
Patch application returns null on errors, failure state is computed and cached, missing flags are backfilled, and finalization propagates the flag.
Streaming, persistence, and error rendering
sites/playground/web/src/components/genui-template/composables/use-template-stream-render.ts, GenuiTemplateChat.vue, chat.types.ts, TemplateSchemaMessageRenderer.vue, .../use-template-conversation.ts, .../use-template-schema.ts
Streaming and finalization persist applyFailed, loading flows backfill it, and rendered cards show a specific patch-application error.
History presentation and development output
sites/playground/web/src/components/genui-template/SchemaVersionHistoryPanel.vue, SchemaVersionCard.vue
The history chevron uses an imported image, while development JSON output falls back to raw operations when formatting fails.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant jsonPatchRenderer
  participant applyJsonPatchOperations
  participant ConversationState
  participant TemplateSchemaMessageRenderer
  jsonPatchRenderer->>applyJsonPatchOperations: apply JSON patch operations
  applyJsonPatchOperations-->>jsonPatchRenderer: return patched schema or null
  jsonPatchRenderer->>ConversationState: store applyFailed flag
  ConversationState->>TemplateSchemaMessageRenderer: provide card state and errors
  TemplateSchemaMessageRenderer-->>jsonPatchRenderer: render patch failure message
Loading

Possibly related PRs

Suggested reviewers: gimmyhehe, rhlin

Poem

I’m a rabbit with patches tucked neat,
Flags hop along where failures meet.
IDs stay unique in the schema trail,
History chevrons no longer pale.
JSON may stumble, but won’t derail—
We save the state and wag our tail!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the two main changes: JSON Patch hardening and component ID assignment fixes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/json-patch-ids-apply-failed

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@sites/playground/web/src/components/genui-template/GenuiTemplateChat.vue`:
- Around line 297-301: Prevent failed schema patches from using stale preview
snapshots: in
sites/playground/web/src/components/genui-template/GenuiTemplateChat.vue lines
297-301, omit the schema field when the card has failed to apply; in
sites/playground/web/src/components/genui-template/template-chat-utils/conversation-schema.ts
lines 161-167, update the reconstruction logic to ignore persisted schema when
applyFailed is true, while preserving normal schema resolution for successful
cards.

In
`@sites/playground/web/src/components/genui-template/template-chat-utils/json-patch-format.ts`:
- Around line 82-85: Update the conversion flow around formatJsonPatch so it
detects any operation without a resolved idToPath and fails the entire patch
instead of filtering that operation out. Preserve conversion of all operations
only when every target resolves, and ensure the failure propagates so mixed
invalid patches cannot be persisted as applyFailed: false.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a482abf7-e93e-4f71-88f9-d5749563247f

📥 Commits

Reviewing files that changed from the base of the PR and between e4e727a and 6bfdb7d.

⛔ Files ignored due to path filters (1)
  • sites/playground/web/src/assets/images/chevron-down.svg is excluded by !**/*.svg
📒 Files selected for processing (14)
  • sites/playground/web/src/components/genui-template/GenuiTemplateChat.vue
  • sites/playground/web/src/components/genui-template/SchemaVersionCard.vue
  • sites/playground/web/src/components/genui-template/SchemaVersionHistoryPanel.vue
  • sites/playground/web/src/components/genui-template/TemplateSchemaMessageRenderer.vue
  • sites/playground/web/src/components/genui-template/chat.types.ts
  • sites/playground/web/src/components/genui-template/composables/use-template-actions.ts
  • sites/playground/web/src/components/genui-template/composables/use-template-conversation.ts
  • sites/playground/web/src/components/genui-template/composables/use-template-schema.ts
  • sites/playground/web/src/components/genui-template/composables/use-template-stream-render.ts
  • sites/playground/web/src/components/genui-template/composables/use-template-version-control.ts
  • sites/playground/web/src/components/genui-template/template-chat-utils/conversation-schema.ts
  • sites/playground/web/src/components/genui-template/template-chat-utils/json-patch-format.ts
  • sites/playground/web/src/components/genui-template/template-chat-utils/schema-id-generator.ts
  • sites/playground/web/src/components/genui-template/template-chat-utils/schema-version-history.ts

Comment thread sites/playground/web/src/components/genui-template/GenuiTemplateChat.vue Outdated
yy-wow and others added 14 commits July 22, 2026 11:32
Skip persisting/rebuilding schema for applyFailed cards, and fail the whole patch when any operation lacks a resolved target.

Co-authored-by: Cursor <cursoragent@cursor.com>
Enhance the handling of applyFailed cards by returning a parsed schema from the previous state instead of null, ensuring better persistence of conversation state.
Refactor the ID merging process to improve the conditions under which IDs can be borrowed from previous nodes. This includes adding a new helper function to streamline the logic and ensuring that child nodes are only processed when their lengths match.
…andling

Update the schema card finalization process to improve ID generation for components and enhance notification handling. The new implementation ensures that the current schema is set correctly based on the preview state, while also streamlining the logic for applying failed cards.
Enhance the error message handling in the schema version card and template message renderer components. Update the logic for applying failed JSON patches and streamline the error message propagation. Remove unnecessary flags and improve the clarity of the error state management.
…enderer

Update the logic for setting the current preview schema to use the stream completion status instead of the successful parse flag, ensuring accurate schema representation during streaming operations.
…ne error handling

Refactor the handling of error messages by removing the errorMessagesMap prop from several components. Update the logic to directly utilize the stream's errorMessagesMap, ensuring a more cohesive error management approach across the template components.
…actions

Add the clearErrorMessages function from useTemplateStreamRender to the useTemplateActions composable. This enhancement ensures that error messages are cleared during the reset process, improving error management and user experience in the template components.
…on history handling

Eliminate the isCurrent flag from the schema version history entry interface and related functions to streamline the versioning logic. Update the SchemaVersionHistoryPanel component to reflect these changes, enhancing clarity and maintainability of the code.
…ema card processing

Refactor the error handling for JSON patches by introducing a new function to set the applyFailed flag directly on schema cards. This change simplifies the error management process across components and ensures accurate tracking of JSON patch application failures. Additionally, remove redundant error message handling logic from various components to improve code clarity and maintainability.
…dling

Refactor the JSON patch error handling by introducing the resolveJsonPatchApplyFailed function, which centralizes the logic for determining the applyFailed state of schema cards. Update the jsonPatchRenderer to utilize the new function, ensuring accurate tracking of JSON patch application failures and enhancing the clarity of schema processing during streaming operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant