Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)7.6k · #13174

real-time-collab

Skill này làm gì

Skill này giải thích cách xây dựng chỉnh sửa cộng tác theo thời gian thực trong ứng dụng Agent-Native, dùng Yjs và TipTap để các thay đổi được gộp sạch. Skill dành cho lập trình viên thêm tài liệu chia sẻ hoặc gỡ lỗi đồng bộ.

Khi nào nên dùng

  • ✓Khi bạn thêm chỉnh sửa cộng tác trực tiếp cho một tài liệu trong ứng dụng.
  • ✓Khi hai người hoặc agent cùng sửa một tài liệu và thay đổi bị mất.
  • ✓Khi bạn cần gỡ lỗi các vấn đề đồng bộ giữa người dùng và agent.

Liên kết skill

https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/real-time-collab/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/real-time-collab/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Cho phép hai người cùng chỉnh sửa một tài liệu cùng lúc, và hiển thị các chỉnh sửa của AI cho cả hai.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Chỉnh sửa được lưu như một tài liệu chung tự động gộp các thay đổi. Cập nhật được đẩy đến người dùng, và có phương án dự phòng là kiểm tra định kỳ khi kết nối trực tiếp bị ngắt.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Real-Time Collaboration

Rule

Collaborative editing uses Yjs CRDT via TipTap. The agent and human users are equal participants — both edit the same Y.Doc and changes merge cleanly without conflicts. Always choose an explicit access mode on createCollabPlugin.

How It Works

  • Y.Doc stores the document as a Y.XmlFragment (ProseMirror node tree)
  • TipTap's Collaboration extension binds the editor to the Y.XmlFragment via ySyncPlugin
  • CollaborationCaret extension renders remote users' cursors with names and colors
  • SSE fast-path — /_agent-native/poll-events EventSource delivers collab events push-style; while SSE is healthy the collab poll interval relaxes to ~12 s
  • Polling fallback — /_agent-native/poll is polled every 2 s when SSE drops mid-session. On serverless, /events answers 204 up front (poll-live) and this client relaxes to ~12 s, because /poll is the only channel.
  • Collab poll boost — while the awareness set shows another visible human on the doc (agent and hidden tabs do not count), the connection holds acquireCollabPollBoost() and the shared transport polls every 2.5 s whenever no stream is connected: a no-op while SSE or the hosted gateway is live. A joiner is noticed on the existing user's next ~12 s collab poll (the joiner sees them at once), a leaver within one poll plus the 30 s awareness TTL. The boost lapses after 3 minutes with no input and no remote events. Lone tabs pay nothing extra. Events under the request source a connection posts with (any app-chosen tab id, not only getBrowserTabId()) are this tab's own and never count. A read-only viewer must join the doc too (Design's useViewerPresence; it never uses the ydoc), or it never sees an editor and stays on the idle cadence. A viewer on a different screen than the editor shares no doc with them; its own collab poll sees the design's resource-scoped events from other tabs and starts the boost (60 s) itself.
  • Update batching — local Yjs updates are debounced ~80 ms and coalesced with Y.mergeUpdates before sending; flushed immediately on visibilitychange / pagehide
  • SQL _collab_docs table persists Yjs state as base64 in Postgres. Tombstone compaction fires automatically when the stored blob exceeds 4× the fresh encoded size.

Agent + Human Editing

  1. Human edits → TipTap → ySyncPlugin → Y.XmlFragment → POST /_agent-native/collab/:docId/update
  2. Agent edits → action edits canonical SQL content + bumps updatedAt → change-sync refetch → the open editor reconciles the new content into the live Y.Doc (see below) → poll update → all clients

Both produce Yjs operations that merge cleanly. Agent edits appear without destroying cursor position, selection, or undo history.

The agent does not push edits into Yjs in-process and does not call any localhost probe — those approaches silently no-op on serverless (the action runs in a different process). The peer-editor model below replaced them.

Agent Edits As A Real-Time Peer Editor

SQL is the durable source of truth for document body content. The agent action edits the canonical content column and bumps updatedAt. No localhost calls, no in-process Yjs mutation.

The open editor reconciles authoritative external content into the live Y.Doc. The updatedAt bump flows through change-sync, which refetches the record. The lead client applies the new content via setContent, producing Yjs operations that merge with concurrent human edits. Every connected client receives the result through normal Yjs sync.

The updatedAt gate

// In the editor's reconcile effect
if (loaded.updatedAt > lastAppliedUpdatedAt.current) {
  applyAuthoritativeContent(loaded.content); // adopt
  lastAppliedUpdatedAt.current = loaded.updatedAt;
}
// else: lagging poll / stale snapshot → ignore

Without the gate, a slightly-behind poll response re-applies old content and the edit "reverts on next poll". A fresh mount always adopts whatever content it loaded.

Lead-client election

Exactly ONE connected client applies the authoritative snapshot; the rest receive it through Yjs sync:

import { isReconcileLeadClient } from "@agent-native/core/client/collab";

if (
  loaded.updatedAt > lastAppliedUpdatedAt.current &&
  isReconcileLeadClient(awareness, ydoc.clientID)
) {
  applyAuthoritativeContent(loaded.content);
}

The agent's awareness entry (`AGENT

Câu hỏi thường gặp

Agent và người có thể cùng chỉnh sửa không?

Có. Skill coi agent và người dùng là những bên tham gia ngang nhau trong cùng một tài liệu.

Con trỏ của người dùng khác hiển thị thế nào?

Một tiện ích con trỏ hiển thị con trỏ của người dùng khác cùng tên và màu của họ.

Chuyện gì xảy ra nếu kết nối trực tiếp bị ngắt?

Ứng dụng chuyển sang hỏi máy chủ mỗi vài giây.

Chi tiết

Mô tả gốc của tác giả

Multi-user collaborative editing with Yjs CRDT, SSE fast-path transport, and granular server-side merge. Use when adding real-time collaborative editing to a template, debugging sync issues, or understanding how the agent and humans edit documents simultaneously.

repo: BuilderIO/agent-native

Độ phổ biến

Sao GitHub
7.6k
Vị trí xếp hạng
#13174
Giấy phép
MIT
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự