@pilotiq/tiptap 3.5.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.
- package/dist/extensions/AiInlineDiffExtension.d.ts +82 -0
- package/dist/extensions/AiInlineDiffExtension.d.ts.map +1 -0
- package/dist/extensions/AiInlineDiffExtension.js +205 -0
- package/dist/extensions/AiInlineDiffExtension.js.map +1 -0
- package/dist/extensions/AiSuggestionExtension.d.ts.map +1 -1
- package/dist/extensions/AiSuggestionExtension.js +51 -0
- package/dist/extensions/AiSuggestionExtension.js.map +1 -1
- package/dist/react/AiSuggestionBanner.d.ts +72 -0
- package/dist/react/AiSuggestionBanner.d.ts.map +1 -0
- package/dist/react/AiSuggestionBanner.js +72 -0
- package/dist/react/AiSuggestionBanner.js.map +1 -0
- package/dist/react/MarkdownEditor.d.ts.map +1 -1
- package/dist/react/MarkdownEditor.js +58 -20
- package/dist/react/MarkdownEditor.js.map +1 -1
- package/dist/react/TiptapEditor.d.ts.map +1 -1
- package/dist/react/TiptapEditor.js +43 -18
- package/dist/react/TiptapEditor.js.map +1 -1
- package/dist/react/useAiInlineDiff.d.ts +57 -0
- package/dist/react/useAiInlineDiff.d.ts.map +1 -0
- package/dist/react/useAiInlineDiff.js +121 -0
- package/dist/react/useAiInlineDiff.js.map +1 -0
- package/package.json +24 -22
- package/src/extensions/AiInlineDiffExtension.ts +263 -0
- package/src/extensions/AiSuggestionExtension.ts +51 -0
- package/src/react/AiSuggestionBanner.tsx +184 -0
- package/src/react/MarkdownEditor.tsx +58 -19
- package/src/react/TiptapEditor.tsx +44 -16
- package/src/react/useAiInlineDiff.ts +146 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pilotiq/tiptap",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "Tiptap rich-text editor adapter for @pilotiq/pilotiq — slash menu, draggable blocks, custom-block API",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -30,26 +30,28 @@
|
|
|
30
30
|
"types": "./dist/render.d.ts"
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
|
-
"dependencies": {
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"prosemirror-changeset": "^2.4.1"
|
|
35
|
+
},
|
|
34
36
|
"peerDependencies": {
|
|
35
|
-
"@pilotiq/pilotiq": ">=0.6.0 <1.0.0",
|
|
36
37
|
"@base-ui/react": "^1",
|
|
38
|
+
"@pilotiq/pilotiq": ">=0.6.0 <1.0.0",
|
|
37
39
|
"@tiptap/core": "^3",
|
|
38
|
-
"@tiptap/
|
|
39
|
-
"@tiptap/
|
|
40
|
-
"@tiptap/
|
|
40
|
+
"@tiptap/extension-color": "3.22.4",
|
|
41
|
+
"@tiptap/extension-details": "3.22.4",
|
|
42
|
+
"@tiptap/extension-highlight": "3.22.4",
|
|
43
|
+
"@tiptap/extension-image": "3.22.4",
|
|
41
44
|
"@tiptap/extension-link": "^3",
|
|
42
45
|
"@tiptap/extension-placeholder": "^3",
|
|
43
|
-
"@tiptap/extension-underline": "3.22.4",
|
|
44
46
|
"@tiptap/extension-subscript": "3.22.4",
|
|
45
47
|
"@tiptap/extension-superscript": "3.22.4",
|
|
48
|
+
"@tiptap/extension-table": "3.22.4",
|
|
46
49
|
"@tiptap/extension-text-align": "3.22.4",
|
|
47
50
|
"@tiptap/extension-text-style": "3.22.4",
|
|
48
|
-
"@tiptap/extension-
|
|
49
|
-
"@tiptap/
|
|
50
|
-
"@tiptap/
|
|
51
|
-
"@tiptap/
|
|
52
|
-
"@tiptap/extension-details": "3.22.4",
|
|
51
|
+
"@tiptap/extension-underline": "3.22.4",
|
|
52
|
+
"@tiptap/pm": "^3",
|
|
53
|
+
"@tiptap/react": "^3",
|
|
54
|
+
"@tiptap/starter-kit": "^3",
|
|
53
55
|
"@tiptap/suggestion": "^3",
|
|
54
56
|
"react": "^18 || ^19",
|
|
55
57
|
"react-dom": "^18 || ^19"
|
|
@@ -57,29 +59,29 @@
|
|
|
57
59
|
"devDependencies": {
|
|
58
60
|
"@base-ui/react": "^1",
|
|
59
61
|
"@tiptap/core": "^3",
|
|
60
|
-
"@tiptap/
|
|
61
|
-
"@tiptap/
|
|
62
|
-
"@tiptap/
|
|
62
|
+
"@tiptap/extension-color": "3.22.4",
|
|
63
|
+
"@tiptap/extension-details": "3.22.4",
|
|
64
|
+
"@tiptap/extension-highlight": "3.22.4",
|
|
65
|
+
"@tiptap/extension-image": "3.22.4",
|
|
63
66
|
"@tiptap/extension-link": "^3",
|
|
64
67
|
"@tiptap/extension-placeholder": "^3",
|
|
65
|
-
"@tiptap/extension-underline": "3.22.4",
|
|
66
68
|
"@tiptap/extension-subscript": "3.22.4",
|
|
67
69
|
"@tiptap/extension-superscript": "3.22.4",
|
|
70
|
+
"@tiptap/extension-table": "3.22.4",
|
|
68
71
|
"@tiptap/extension-text-align": "3.22.4",
|
|
69
72
|
"@tiptap/extension-text-style": "3.22.4",
|
|
70
|
-
"@tiptap/extension-
|
|
71
|
-
"@tiptap/
|
|
72
|
-
"@tiptap/
|
|
73
|
-
"@tiptap/
|
|
74
|
-
"@tiptap/extension-details": "3.22.4",
|
|
73
|
+
"@tiptap/extension-underline": "3.22.4",
|
|
74
|
+
"@tiptap/pm": "^3",
|
|
75
|
+
"@tiptap/react": "^3",
|
|
76
|
+
"@tiptap/starter-kit": "^3",
|
|
75
77
|
"@tiptap/suggestion": "^3",
|
|
76
|
-
"tiptap-markdown": "^0.9",
|
|
77
78
|
"@types/node": "^20",
|
|
78
79
|
"@types/react": "^19",
|
|
79
80
|
"@types/react-dom": "^19",
|
|
80
81
|
"esbuild": "^0.27",
|
|
81
82
|
"react": "^19",
|
|
82
83
|
"react-dom": "^19",
|
|
84
|
+
"tiptap-markdown": "^0.9",
|
|
83
85
|
"typescript": "^5",
|
|
84
86
|
"@pilotiq/pilotiq": "^0.19.0"
|
|
85
87
|
},
|
|
@@ -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
|
+
}
|
|
@@ -222,6 +222,57 @@ export const AiSuggestionExtension = Extension.create<AiSuggestionExtensionOptio
|
|
|
222
222
|
}
|
|
223
223
|
.${prefix}-accept:hover { color: rgb(21, 128, 61); }
|
|
224
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); }
|
|
225
276
|
`
|
|
226
277
|
document.head.appendChild(style)
|
|
227
278
|
},
|
|
@@ -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
|
+
}
|