@pilotiq/tiptap 3.4.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/extensions/AiInlineDiffExtension.d.ts +82 -0
  2. package/dist/extensions/AiInlineDiffExtension.d.ts.map +1 -0
  3. package/dist/extensions/AiInlineDiffExtension.js +205 -0
  4. package/dist/extensions/AiInlineDiffExtension.js.map +1 -0
  5. package/dist/extensions/AiSuggestionExtension.d.ts.map +1 -1
  6. package/dist/extensions/AiSuggestionExtension.js +108 -0
  7. package/dist/extensions/AiSuggestionExtension.js.map +1 -1
  8. package/dist/markdownExtension.js +0 -1
  9. package/dist/react/AiSuggestionBanner.d.ts +72 -0
  10. package/dist/react/AiSuggestionBanner.d.ts.map +1 -0
  11. package/dist/react/AiSuggestionBanner.js +72 -0
  12. package/dist/react/AiSuggestionBanner.js.map +1 -0
  13. package/dist/react/CollabTextRenderer.d.ts.map +1 -1
  14. package/dist/react/CollabTextRenderer.js +62 -27
  15. package/dist/react/CollabTextRenderer.js.map +1 -1
  16. package/dist/react/MarkdownEditor.d.ts.map +1 -1
  17. package/dist/react/MarkdownEditor.js +62 -7
  18. package/dist/react/MarkdownEditor.js.map +1 -1
  19. package/dist/react/TiptapEditor.d.ts.map +1 -1
  20. package/dist/react/TiptapEditor.js +47 -5
  21. package/dist/react/TiptapEditor.js.map +1 -1
  22. package/dist/react/useAiInlineDiff.d.ts +57 -0
  23. package/dist/react/useAiInlineDiff.d.ts.map +1 -0
  24. package/dist/react/useAiInlineDiff.js +121 -0
  25. package/dist/react/useAiInlineDiff.js.map +1 -0
  26. package/dist/react/useAiSuggestionBridge.d.ts +37 -1
  27. package/dist/react/useAiSuggestionBridge.d.ts.map +1 -1
  28. package/dist/react/useAiSuggestionBridge.js +63 -32
  29. package/dist/react/useAiSuggestionBridge.js.map +1 -1
  30. package/package.json +26 -24
  31. package/src/extensions/AiInlineDiffExtension.ts +263 -0
  32. package/src/extensions/AiSuggestionExtension.ts +107 -0
  33. package/src/react/AiSuggestionBanner.tsx +184 -0
  34. package/src/react/CollabTextRenderer.tsx +61 -27
  35. package/src/react/MarkdownEditor.tsx +62 -6
  36. package/src/react/TiptapEditor.tsx +48 -4
  37. package/src/react/useAiInlineDiff.ts +146 -0
  38. package/src/react/useAiSuggestionBridge.ts +105 -9
  39. package/dist/markdownExtension.js.map +0 -7
@@ -0,0 +1,263 @@
1
+ /**
2
+ * Inline-diff visualization for whole-field AI suggestions.
3
+ *
4
+ * Sibling to `AiSuggestionExtension`, which handles producer-supplied
5
+ * range suggestions (surgical edits with `meta.editorRange`) via the
6
+ * inline chip widget. This extension handles the *whole-field* case:
7
+ * the AI proposes a new document and the user reviews the structural
8
+ * delta (added paragraphs, deleted text, mark changes, etc.) before
9
+ * accepting or rejecting via the host-mounted `<AiSuggestionBanner>`.
10
+ *
11
+ * Architecture:
12
+ * 1. `startAiInlineDiff(id, newDoc)` captures the current doc as the
13
+ * baseline, replaces the doc body with `newDoc`'s content (so the
14
+ * editor surface IS the proposed state), and initializes a
15
+ * `prosemirror-changeset` tracking the original-to-current
16
+ * transition.
17
+ * 2. The plugin appendTransaction hook keeps the changeset in sync
18
+ * with any further transactions while the diff is pending — e.g.
19
+ * y-prosemirror remote edits arriving during review. (Rare on
20
+ * whole-field flows but free.)
21
+ * 3. A decorations spec walks `ChangeSet.changes` and emits:
22
+ * - inline green-background decorations on inserted ranges
23
+ * - widget decorations rendering the *deleted* text struck
24
+ * through next to the insert point (the deleted content
25
+ * isn't in the current doc, so a widget is the only way to
26
+ * surface it)
27
+ * 4. `acceptAiInlineDiff()` clears the plugin state — the current
28
+ * doc is the accepted state.
29
+ * 5. `rejectAiInlineDiff()` replaces the doc back to the baseline
30
+ * via a single transaction and clears state.
31
+ *
32
+ * For Tiptap Pro parity. See `[[project_pilotiq_text_field_tiptap_rules]]`.
33
+ */
34
+
35
+ import { Extension } from '@tiptap/core'
36
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
37
+ import type { EditorState, Transaction } from '@tiptap/pm/state'
38
+ import { Decoration, DecorationSet } from '@tiptap/pm/view'
39
+ import type { Node as ProseMirrorNode, Slice } from '@tiptap/pm/model'
40
+ import { ChangeSet } from 'prosemirror-changeset'
41
+
42
+ declare module '@tiptap/core' {
43
+ interface Commands<ReturnType> {
44
+ aiInlineDiff: {
45
+ /**
46
+ * Start the inline-diff review session. Snapshots the current
47
+ * doc as the baseline, replaces the doc with `newDocSlice`'s
48
+ * content, and shows the diff overlay.
49
+ *
50
+ * `id` is the host-side `PendingSuggestion.id` — used so the
51
+ * banner / approve handlers can correlate the editor state with
52
+ * the queue entry.
53
+ */
54
+ startAiInlineDiff: (id: string, newDocSlice: Slice) => ReturnType
55
+ /** Clear diff state. Current doc IS the accepted state. */
56
+ acceptAiInlineDiff: () => ReturnType
57
+ /** Revert doc to the captured baseline and clear diff state. */
58
+ rejectAiInlineDiff: () => ReturnType
59
+ }
60
+ }
61
+ }
62
+
63
+ interface DiffState {
64
+ id: string
65
+ /** Original doc captured at `startAiInlineDiff` time — used for revert. */
66
+ baseline: ProseMirrorNode
67
+ /** ChangeSet accumulating diffs since baseline. */
68
+ changeset: ChangeSet
69
+ }
70
+
71
+ export const aiInlineDiffPluginKey = new PluginKey<DiffState | null>('pilotiqAiInlineDiff')
72
+
73
+ /** Read the active diff state, if any. Public for hosts that want to
74
+ * branch their banner UI on "diff active" vs "diff inactive". */
75
+ export function getAiInlineDiffState(state: EditorState): DiffState | null {
76
+ return aiInlineDiffPluginKey.getState(state) ?? null
77
+ }
78
+
79
+ interface StartMeta { type: 'start'; id: string; baseline: ProseMirrorNode }
80
+ interface ClearMeta { type: 'clear' }
81
+ type DiffMeta = StartMeta | ClearMeta
82
+
83
+ export interface AiInlineDiffExtensionOptions {
84
+ /**
85
+ * Class prefix for inline-diff decorations. Defaults to
86
+ * `'pilotiq-ai-diff'`, producing:
87
+ * - `pilotiq-ai-diff-inserted` (green-background span on new ranges)
88
+ * - `pilotiq-ai-diff-deleted` (widget DOM root for deleted text)
89
+ * - `pilotiq-ai-diff-deleted-text` (the strikethrough span inside)
90
+ */
91
+ classPrefix?: string
92
+ }
93
+
94
+ export const AiInlineDiffExtension = Extension.create<AiInlineDiffExtensionOptions>({
95
+ name: 'pilotiqAiInlineDiff',
96
+
97
+ addOptions() {
98
+ return { classPrefix: 'pilotiq-ai-diff' }
99
+ },
100
+
101
+ onCreate() {
102
+ // Mirror the chip CSS injection pattern. Idempotent via sentinel.
103
+ if (typeof document === 'undefined') return
104
+ const SENTINEL = 'data-pilotiq-ai-diff-styles'
105
+ if (document.head.querySelector(`style[${SENTINEL}]`)) return
106
+ const prefix = this.options.classPrefix
107
+ const style = document.createElement('style')
108
+ style.setAttribute(SENTINEL, '')
109
+ style.textContent = `
110
+ .${prefix}-inserted {
111
+ background-color: rgba(187, 247, 208, 0.55);
112
+ color: rgb(20, 83, 45);
113
+ text-decoration: none;
114
+ }
115
+ .${prefix}-deleted {
116
+ display: inline;
117
+ margin-right: 0.125em;
118
+ }
119
+ .${prefix}-deleted-text {
120
+ text-decoration: line-through;
121
+ text-decoration-color: rgba(220, 38, 38, 0.7);
122
+ background-color: rgba(254, 226, 226, 0.55);
123
+ color: rgb(153, 27, 27);
124
+ padding: 0 0.125em;
125
+ }
126
+ `
127
+ document.head.appendChild(style)
128
+ },
129
+
130
+ addCommands() {
131
+ return {
132
+ startAiInlineDiff: (id, newDocSlice) => ({ tr, state, dispatch }) => {
133
+ const baseline = state.doc
134
+ const docEnd = state.doc.content.size
135
+ // Replace the whole doc body with the proposed content. The
136
+ // schema enforces validity — if the slice doesn't fit, ProseMirror
137
+ // throws (callers should pre-validate via `editor.schema`).
138
+ tr.replaceRange(0, docEnd, newDocSlice)
139
+ const meta: StartMeta = { type: 'start', id, baseline }
140
+ tr.setMeta(aiInlineDiffPluginKey, meta)
141
+ if (dispatch) dispatch(tr)
142
+ return true
143
+ },
144
+ acceptAiInlineDiff: () => ({ tr, dispatch }) => {
145
+ const meta: ClearMeta = { type: 'clear' }
146
+ tr.setMeta(aiInlineDiffPluginKey, meta)
147
+ if (dispatch) dispatch(tr)
148
+ return true
149
+ },
150
+ rejectAiInlineDiff: () => ({ tr, state, dispatch }) => {
151
+ const ds = aiInlineDiffPluginKey.getState(state)
152
+ if (!ds) return false
153
+ const docEnd = state.doc.content.size
154
+ // Replace whole body with the baseline's content via a slice that
155
+ // spans the baseline's open boundaries (always 0 for a top-level
156
+ // doc replace).
157
+ tr.replaceWith(0, docEnd, ds.baseline.content)
158
+ const meta: ClearMeta = { type: 'clear' }
159
+ tr.setMeta(aiInlineDiffPluginKey, meta)
160
+ if (dispatch) dispatch(tr)
161
+ return true
162
+ },
163
+ }
164
+ },
165
+
166
+ addProseMirrorPlugins() {
167
+ const ext = this
168
+ return [
169
+ new Plugin<DiffState | null>({
170
+ key: aiInlineDiffPluginKey,
171
+ state: {
172
+ init() { return null },
173
+ apply(tr, value) {
174
+ const meta = tr.getMeta(aiInlineDiffPluginKey) as DiffMeta | undefined
175
+ if (meta?.type === 'start') {
176
+ // Baseline captured BEFORE the replaceRange step in this
177
+ // same transaction. The changeset's `addSteps` consumes
178
+ // the transaction's step list to compute the diff between
179
+ // the baseline doc and the post-transaction doc.
180
+ const cs = ChangeSet.create(meta.baseline).addSteps(tr.doc, tr.mapping.maps, null)
181
+ return { id: meta.id, baseline: meta.baseline, changeset: cs }
182
+ }
183
+ if (meta?.type === 'clear') return null
184
+ if (!value) return value
185
+ // No explicit meta — a regular transaction landed while the
186
+ // diff was active. Fold its steps into the changeset so any
187
+ // further edits (e.g. y-prosemirror remote ops) are reflected.
188
+ if (tr.docChanged) {
189
+ const cs = value.changeset.addSteps(tr.doc, tr.mapping.maps, null)
190
+ return { ...value, changeset: cs }
191
+ }
192
+ return value
193
+ },
194
+ },
195
+ props: {
196
+ decorations(state) {
197
+ const ds = aiInlineDiffPluginKey.getState(state)
198
+ if (!ds) return DecorationSet.empty
199
+ return buildDiffDecorations(state, ds, ext.options.classPrefix ?? 'pilotiq-ai-diff')
200
+ },
201
+ },
202
+ }),
203
+ ]
204
+ },
205
+ })
206
+
207
+ function buildDiffDecorations(
208
+ state: EditorState,
209
+ ds: DiffState,
210
+ prefix: string,
211
+ ): DecorationSet {
212
+ const decos: Decoration[] = []
213
+ const docSize = state.doc.content.size
214
+
215
+ for (const change of ds.changeset.changes) {
216
+ // `fromB..toB` is the range in the CURRENT doc that holds the
217
+ // inserted content. `fromA..toA` is the range in the BASELINE doc
218
+ // that was removed. `inserted` / `deleted` are Span[] arrays whose
219
+ // `length` sums to (toB - fromB) / (toA - fromA) respectively.
220
+ const fromB = Math.max(0, Math.min(change.fromB, docSize))
221
+ const toB = Math.max(fromB, Math.min(change.toB, docSize))
222
+
223
+ if (toB > fromB) {
224
+ decos.push(
225
+ Decoration.inline(fromB, toB, {
226
+ class: `${prefix}-inserted`,
227
+ 'data-pilotiq-ai-diff-id': ds.id,
228
+ }),
229
+ )
230
+ }
231
+
232
+ // Deleted text — pull from the baseline using the `fromA..toA` range.
233
+ // Render via a widget at the change's insert-point (or end of insert)
234
+ // so the deleted text appears immediately before / after the new run.
235
+ // Empty deletions (pure inserts) skip the widget.
236
+ if (change.toA > change.fromA) {
237
+ const deletedText = ds.baseline.textBetween(change.fromA, change.toA, '\n', ' ')
238
+ if (deletedText.length > 0) {
239
+ decos.push(
240
+ Decoration.widget(fromB, () => buildDeletedWidget(deletedText, prefix, ds.id), {
241
+ side: -1,
242
+ ignoreSelection: true,
243
+ key: `pilotiq-ai-diff:deleted:${change.fromA}:${change.toA}`,
244
+ }),
245
+ )
246
+ }
247
+ }
248
+ }
249
+
250
+ return DecorationSet.create(state.doc, decos)
251
+ }
252
+
253
+ function buildDeletedWidget(text: string, prefix: string, id: string): HTMLElement {
254
+ const root = document.createElement('span')
255
+ root.className = `${prefix}-deleted`
256
+ root.setAttribute('data-pilotiq-ai-diff-id', id)
257
+ root.contentEditable = 'false'
258
+ const inner = document.createElement('span')
259
+ inner.className = `${prefix}-deleted-text`
260
+ inner.textContent = text
261
+ root.appendChild(inner)
262
+ return root
263
+ }
@@ -170,6 +170,113 @@ export const AiSuggestionExtension = Extension.create<AiSuggestionExtensionOptio
170
170
  }
171
171
  },
172
172
 
173
+ onCreate() {
174
+ // Inject minimal default styles for the chip + strikethrough on first
175
+ // mount so consumers see the visualization without wiring CSS. Idempotent
176
+ // via the `data-pilotiq-ai-suggestion-styles` sentinel; consumers who
177
+ // want full control just add their own `<style>` with the same class
178
+ // names (last wins — the cascade picks user overrides over our defaults
179
+ // since the user stylesheet appears AFTER our injected one in `<head>`
180
+ // when imported via Vite/Webpack, OR via higher specificity).
181
+ if (typeof document === 'undefined') return
182
+ const SENTINEL = 'data-pilotiq-ai-suggestion-styles'
183
+ if (document.head.querySelector(`style[${SENTINEL}]`)) return
184
+ const prefix = this.options.classPrefix
185
+ const style = document.createElement('style')
186
+ style.setAttribute(SENTINEL, '')
187
+ // Colors picked to look right on light + dark surfaces without theme
188
+ // overrides (60% alpha on background-color, 100% on text). Tuned to
189
+ // match the inline-diff convention used by the Tiptap Pro AI Agent.
190
+ style.textContent = `
191
+ .${prefix}-original {
192
+ text-decoration: line-through;
193
+ text-decoration-color: rgba(220, 38, 38, 0.7);
194
+ background-color: rgba(254, 226, 226, 0.6);
195
+ color: rgb(153, 27, 27);
196
+ }
197
+ .${prefix}-chip {
198
+ display: inline-flex;
199
+ align-items: center;
200
+ gap: 0.25rem;
201
+ margin-left: 0.25rem;
202
+ padding: 0 0.25rem;
203
+ border-radius: 0.25rem;
204
+ background-color: rgba(220, 252, 231, 0.7);
205
+ color: rgb(22, 101, 52);
206
+ font-size: 0.875em;
207
+ line-height: 1.4;
208
+ }
209
+ .${prefix}-replacement {
210
+ padding: 0 0.125rem;
211
+ }
212
+ .${prefix}-accept,
213
+ .${prefix}-reject {
214
+ appearance: none;
215
+ background: transparent;
216
+ border: 0;
217
+ padding: 0 0.25rem;
218
+ cursor: pointer;
219
+ font-size: 0.875em;
220
+ line-height: 1;
221
+ color: inherit;
222
+ }
223
+ .${prefix}-accept:hover { color: rgb(21, 128, 61); }
224
+ .${prefix}-reject:hover { color: rgb(185, 28, 28); }
225
+
226
+ /* Banner — top-of-editor strip for whole-field suggestions on rich
227
+ surfaces (markdown / richtext). Sibling to the chip styles above;
228
+ lives here so both ship via the same extension-mount sentinel.
229
+ Class names live under \`pilotiq-ai-banner-*\` (not \`-suggestion-\`)
230
+ since the banner is a host-mounted React component, not a PM
231
+ decoration. */
232
+ .pilotiq-ai-banner {
233
+ display: flex;
234
+ align-items: center;
235
+ gap: 0.5rem;
236
+ padding: 0.375rem 0.625rem;
237
+ margin-bottom: 0.375rem;
238
+ border-radius: 0.375rem;
239
+ background-color: rgba(254, 252, 232, 0.9);
240
+ border: 1px solid rgba(234, 179, 8, 0.4);
241
+ color: rgb(113, 63, 18);
242
+ font-size: 0.875rem;
243
+ line-height: 1.4;
244
+ }
245
+ .pilotiq-ai-banner-icon { flex: 0 0 auto; }
246
+ .pilotiq-ai-banner-label { flex: 1 1 auto; }
247
+ .pilotiq-ai-banner-actions {
248
+ display: inline-flex;
249
+ gap: 0.375rem;
250
+ flex: 0 0 auto;
251
+ }
252
+ .pilotiq-ai-banner-reject,
253
+ .pilotiq-ai-banner-accept {
254
+ appearance: none;
255
+ cursor: pointer;
256
+ font-size: 0.8125rem;
257
+ font-weight: 500;
258
+ line-height: 1;
259
+ padding: 0.25rem 0.625rem;
260
+ border-radius: 0.25rem;
261
+ border: 1px solid transparent;
262
+ }
263
+ .pilotiq-ai-banner-reject {
264
+ background-color: transparent;
265
+ color: rgb(120, 53, 15);
266
+ border-color: rgba(180, 83, 9, 0.4);
267
+ }
268
+ .pilotiq-ai-banner-reject:hover {
269
+ background-color: rgba(254, 215, 170, 0.4);
270
+ }
271
+ .pilotiq-ai-banner-accept {
272
+ background-color: rgb(22, 101, 52);
273
+ color: white;
274
+ }
275
+ .pilotiq-ai-banner-accept:hover { background-color: rgb(21, 128, 61); }
276
+ `
277
+ document.head.appendChild(style)
278
+ },
279
+
173
280
  addCommands() {
174
281
  return {
175
282
  addAiSuggestion: (suggestion) => ({ tr, state, dispatch }) => {
@@ -0,0 +1,184 @@
1
+ import { useMemo } from 'react'
2
+ import {
3
+ usePendingSuggestionsForField,
4
+ usePendingSuggestions,
5
+ type PendingSuggestion,
6
+ } from '@pilotiq/pilotiq/react'
7
+
8
+ /**
9
+ * Top-of-editor banner UI for whole-field AI suggestions on Tiptap surfaces
10
+ * whose content shape can't survive the inline chip widget's plain-text
11
+ * replace (richtext, markdown). The chip path renders the replacement via
12
+ * `Element.textContent = replacement` which surfaces raw HTML / markdown
13
+ * as literal text — fine for plain `TextField`, ugly for the others.
14
+ *
15
+ * Visible only when at least one pending suggestion targets this field
16
+ * AND lacks `meta.editorRange` (i.e. a whole-field replacement from
17
+ * `update_form_state`'s `set_value` op). Range-anchored suggestions stay
18
+ * on the editor-side chip widget path — those have a precise location
19
+ * the user wants to see in context.
20
+ *
21
+ * Phase 1 ships banner-only ("Changes suggested — Accept / Reject"); no
22
+ * inline diff visualization yet. Phase 2 will replace the banner-only
23
+ * UX with a `prosemirror-changeset`-driven inline diff on the editor's
24
+ * doc itself, with the banner staying as the global Accept-all / Reject
25
+ * control bar. See `[[project_pilotiq_text_field_tiptap_rules]]`.
26
+ *
27
+ * Approve runs the renderer-supplied `onApplyWholeField(value)` callback
28
+ * AND dismisses the suggestion from the queue. Reject just dismisses
29
+ * (no doc mutation). Multiple pending whole-field suggestions on the
30
+ * same field stack — Accept all / Reject all collapse the queue in one
31
+ * pass.
32
+ */
33
+ export interface AiSuggestionBannerProps {
34
+ /** Field name, matches the suggestion's `fieldName`. */
35
+ fieldName: string
36
+ /**
37
+ * Apply a whole-field suggestion to the underlying editor. Receives the
38
+ * raw `suggestedValue` string from the suggestion. The renderer wires
39
+ * its own content-shape-aware `setContent` here (markdown source for
40
+ * MarkdownEditor, HTML / JSON for TiptapEditor).
41
+ *
42
+ * Skipped when `onAcceptViaEditor` is supplied — that path means the
43
+ * editor already holds the proposed state via `AiInlineDiffExtension`,
44
+ * and Accept routes through `acceptAiInlineDiff()` instead. The host
45
+ * still calls `pendingSuggestions.approve(id)` afterwards to dismiss
46
+ * the queue entry.
47
+ */
48
+ onApplyWholeField: (suggestedValue: string) => void
49
+ /**
50
+ * Diff-aware Accept hook. When supplied, the banner calls this first
51
+ * (so the editor commits its diff state) and then dismisses via the
52
+ * context. `onApplyWholeField` is NOT called in this mode — the
53
+ * editor's current doc is already the accepted state.
54
+ *
55
+ * Sparse so the simple banner path (Phase 1, no diff) keeps its
56
+ * existing semantics.
57
+ */
58
+ onAcceptViaEditor?: () => void
59
+ /**
60
+ * Diff-aware Reject hook. When supplied, the banner calls this first
61
+ * (so the editor reverts to the baseline) and then dismisses via the
62
+ * context. Sparse — see `onAcceptViaEditor`.
63
+ */
64
+ onRejectViaEditor?: () => void
65
+ /** Optional class on the outer banner element. Defaults to a minimal styled chrome. */
66
+ className?: string
67
+ }
68
+
69
+ /**
70
+ * Hook variant — returns banner state without rendering, for renderers
71
+ * that want to compose their own chrome. Renderer-agnostic.
72
+ */
73
+ export function useAiSuggestionBanner(fieldName: string): {
74
+ pending: readonly PendingSuggestion[]
75
+ approveAll: (apply: (value: string) => void) => void
76
+ rejectAll: () => void
77
+ } {
78
+ const { list, dismiss } = usePendingSuggestionsForField(fieldName)
79
+
80
+ // Only whole-field suggestions land in the banner. Range-anchored ones
81
+ // ride the editor chip widget.
82
+ const pending = useMemo(
83
+ () => list.filter(s => !hasEditorRange(s)),
84
+ [list],
85
+ )
86
+
87
+ const approveAll = (apply: (value: string) => void): void => {
88
+ for (const s of pending) {
89
+ if (typeof s.suggestedValue === 'string') apply(s.suggestedValue)
90
+ dismiss(s.id)
91
+ }
92
+ }
93
+
94
+ const rejectAll = (): void => {
95
+ for (const s of pending) dismiss(s.id)
96
+ }
97
+
98
+ return { pending, approveAll, rejectAll }
99
+ }
100
+
101
+ function hasEditorRange(s: PendingSuggestion): boolean {
102
+ const meta = (s.meta ?? {}) as Record<string, unknown>
103
+ const range = meta['editorRange'] as { from?: unknown; to?: unknown } | undefined
104
+ return !!(range && typeof range.from === 'number' && typeof range.to === 'number')
105
+ }
106
+
107
+ export function AiSuggestionBanner({
108
+ fieldName,
109
+ onApplyWholeField,
110
+ onAcceptViaEditor,
111
+ onRejectViaEditor,
112
+ className,
113
+ }: AiSuggestionBannerProps): React.ReactElement | null {
114
+ const { pending, approveAll, rejectAll } = useAiSuggestionBanner(fieldName)
115
+ const { dismiss } = usePendingSuggestions()
116
+
117
+ if (pending.length === 0) return null
118
+
119
+ // First (and usually only) pending suggestion drives the agent-label
120
+ // display. Multiple-at-once is rare in practice — the banner shows the
121
+ // most recent producer to keep the chrome compact.
122
+ const head = pending[0]!
123
+ const sourceLabel = head.source?.agentLabel ?? null
124
+
125
+ const handleAccept = (): void => {
126
+ // Diff-active path — editor's current doc IS the accepted state.
127
+ // Commit via the editor command, then drop the queue entries.
128
+ if (onAcceptViaEditor) {
129
+ onAcceptViaEditor()
130
+ for (const s of pending) dismiss(s.id)
131
+ return
132
+ }
133
+ approveAll(onApplyWholeField)
134
+ }
135
+
136
+ const handleReject = (): void => {
137
+ // Diff-active path — editor still holds the proposed state; revert
138
+ // to the captured baseline before dismissing.
139
+ if (onRejectViaEditor) {
140
+ onRejectViaEditor()
141
+ for (const s of pending) dismiss(s.id)
142
+ return
143
+ }
144
+ rejectAll()
145
+ }
146
+
147
+ // Per-suggestion controls when there's more than one — keeps the UX
148
+ // discoverable. Single suggestion: Accept / Reject only.
149
+ const single = pending.length === 1
150
+
151
+ return (
152
+ <div
153
+ role="region"
154
+ aria-label="AI suggested changes"
155
+ data-pilotiq-ai-banner=""
156
+ className={className ?? 'pilotiq-ai-banner'}
157
+ >
158
+ <span className="pilotiq-ai-banner-icon" aria-hidden="true">💡</span>
159
+ <span className="pilotiq-ai-banner-label">
160
+ {single
161
+ ? sourceLabel
162
+ ? `Changes suggested by ${sourceLabel}`
163
+ : 'Changes suggested'
164
+ : `${pending.length} changes suggested`}
165
+ </span>
166
+ <div className="pilotiq-ai-banner-actions">
167
+ <button
168
+ type="button"
169
+ className="pilotiq-ai-banner-reject"
170
+ onClick={handleReject}
171
+ >
172
+ {single ? 'Reject' : 'Reject all'}
173
+ </button>
174
+ <button
175
+ type="button"
176
+ className="pilotiq-ai-banner-accept"
177
+ onClick={handleAccept}
178
+ >
179
+ {single ? 'Accept' : 'Accept all'}
180
+ </button>
181
+ </div>
182
+ </div>
183
+ )
184
+ }
@@ -66,32 +66,47 @@ export function CollabTextRenderer({
66
66
  }, [collabActive])
67
67
 
68
68
  const editor = useEditor(
69
- createPlainTextEditor({
70
- multiline,
71
- ...(placeholder !== undefined ? { placeholder } : {}),
72
- editable: !disabled,
73
- // When Collaboration owns the doc, omit `content` so the editor
74
- // doesn't race the y-prosemirror sync. The post-`synced` effect below
75
- // seeds the fragment on first connect when it's still empty. When
76
- // collab is off, seed from defaultValue directly.
77
- content: collabActive ? '' : defaultValue,
78
- // AI suggestions always-on extension that tracks suggested edits as
79
- // inline strikethrough + Approve/Reject chip widgets. Idle until the
80
- // host calls `editor.commands.addAiSuggestion(...)` via the bridge below.
81
- // Matches the `TiptapEditor` wiring so suggestion mode works uniformly
82
- // across RichTextField / MarkdownField / TextField+TextareaField.
83
- extensions: [...collabExtensions, AiSuggestionExtension],
84
- onUpdate: (text) => onChange(text),
85
- ...(onSubmit ? { onSubmit: () => { onSubmit(); return false } } : {}),
86
- ...(className || editorAttributes
87
- ? {
88
- editorAttributes: {
89
- ...(editorAttributes ?? {}),
90
- ...(className ? { class: className } : {}),
91
- },
92
- }
93
- : {}),
94
- }),
69
+ {
70
+ // Tiptap v3 SSR guard. With `immediatelyRender: true` (default)
71
+ // `useEditor` touches the DOM during construction; under Vike's
72
+ // `onRenderHtml` that throws "SSR has been detected, please set
73
+ // `immediatelyRender` explicitly to `false` to avoid hydration
74
+ // mismatches." Deferring until the first React effect lets SSR
75
+ // produce an empty shell + hydration mount the live editor.
76
+ //
77
+ // Load-bearing for the AI-attached auto-upgrade path: with rule
78
+ // #2, AI fields render the Tiptap surface during SSR (where
79
+ // `useCollabRoom()` is null but `aiActions.length > 0` flips the
80
+ // host's gate). Without this flag the dev server would crash on
81
+ // the first SSR pass of any record-edit page touching AI fields.
82
+ immediatelyRender: false,
83
+ ...createPlainTextEditor({
84
+ multiline,
85
+ ...(placeholder !== undefined ? { placeholder } : {}),
86
+ editable: !disabled,
87
+ // When Collaboration owns the doc, omit `content` so the editor
88
+ // doesn't race the y-prosemirror sync. The post-`synced` effect below
89
+ // seeds the fragment on first connect when it's still empty. When
90
+ // collab is off, seed from defaultValue directly.
91
+ content: collabActive ? '' : defaultValue,
92
+ // AI suggestions — always-on extension that tracks suggested edits as
93
+ // inline strikethrough + Approve/Reject chip widgets. Idle until the
94
+ // host calls `editor.commands.addAiSuggestion(...)` via the bridge below.
95
+ // Matches the `TiptapEditor` wiring so suggestion mode works uniformly
96
+ // across RichTextField / MarkdownField / TextField+TextareaField.
97
+ extensions: [...collabExtensions, AiSuggestionExtension],
98
+ onUpdate: (text) => onChange(text),
99
+ ...(onSubmit ? { onSubmit: () => { onSubmit(); return false } } : {}),
100
+ ...(className || editorAttributes
101
+ ? {
102
+ editorAttributes: {
103
+ ...(editorAttributes ?? {}),
104
+ ...(className ? { class: className } : {}),
105
+ },
106
+ }
107
+ : {}),
108
+ }),
109
+ },
95
110
  // Re-mount when collab toggles. Other props (multiline, name, etc) are
96
111
  // stable per mount under the upstream gate.
97
112
  [collabActive],
@@ -107,7 +122,26 @@ export function CollabTextRenderer({
107
122
  // Cross-package suggestion bridge — sync the host's
108
123
  // `<PendingSuggestionsContext>` queue with the editor's `AiSuggestion`
109
124
  // extension. No-op when no provider is mounted (default no-op context).
110
- useAiSuggestionBridge(editor ?? null, name)
125
+ //
126
+ // Whole-field fallback: chat-driven suggestions (e.g. `update_form_state`)
127
+ // arrive without `meta.editorRange`. Plain-text editors opt into a
128
+ // synthesized full-doc range so the inline-diff chip (red strikethrough on
129
+ // the current value + green chip with the suggested text + ✓/✕ buttons)
130
+ // renders BEFORE the user approves. The extension's `applyApprove` is
131
+ // text-node-based which fits the plain-text schema exactly. The
132
+ // `onApplyWholeField` callback stays as a fallback for cases that don't
133
+ // synthesize (e.g. an empty doc — `from === to` skips the chip but the
134
+ // applier still needs to swap content).
135
+ useAiSuggestionBridge(editor ?? null, name, {
136
+ synthesizeWholeFieldRange: (ed) => ({
137
+ from: 0,
138
+ to: ed.state.doc.content.size,
139
+ }),
140
+ onApplyWholeField: (value) => {
141
+ if (!editor || editor.isDestroyed) return
142
+ editor.commands.setContent(plainTextToDoc(value, !!multiline))
143
+ },
144
+ })
111
145
 
112
146
  // First-load seed when collab is active. Collaboration starts the editor
113
147
  // empty regardless of `defaultValue`; once the provider syncs the room