@surfishs/dsh-prompt-enhance 0.0.2 → 0.0.3
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/lib/client.js +672 -445
- package/lib/index.js +9 -2
- package/lib/index.js.map +2 -2
- package/package.json +1 -1
- package/src/client/ActionIcons.tsx +61 -0
- package/src/client/EnhanceButton.tsx +24 -56
- package/src/client/ResultDock.tsx +261 -0
- package/src/client/ResultPanel.tsx +149 -177
- package/src/client/SparkleFishIcon.tsx +41 -0
- package/src/client/SparkleIcon.tsx +33 -0
- package/src/client/UndoBar.tsx +4 -0
- package/src/client/index.tsx +16 -0
- package/src/client/locales.ts +18 -4
- package/src/client/styles.ts +75 -48
- package/src/client/ui-state.ts +7 -2
- package/src/enhancer.ts +11 -2
- package/src/shared/protocol.ts +2 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@surfishs/dsh-prompt-enhance",
|
|
3
3
|
"description": "Prompt enhancement for the dsh web GUI: rewrite the composer draft into a well-structured prompt through the harness LLM service, preview the before/after, then fill back, copy, or undo. Original draft is always preserved.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"author": "surfishs",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"engines": {
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small inline icons for the action row. They are stroke-only, 24-unit
|
|
3
|
+
* viewBoxes painted with currentColor so they inherit the button's label
|
|
4
|
+
* color in both themes.
|
|
5
|
+
* @module dsh-prompt-enhance/client/ActionIcons
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ReactElement } from 'react'
|
|
9
|
+
|
|
10
|
+
function icon(paths: ReactElement[]): ReactElement {
|
|
11
|
+
return (
|
|
12
|
+
<svg
|
|
13
|
+
width={14}
|
|
14
|
+
height={14}
|
|
15
|
+
viewBox="0 0 24 24"
|
|
16
|
+
fill="none"
|
|
17
|
+
stroke="currentColor"
|
|
18
|
+
strokeWidth={2}
|
|
19
|
+
strokeLinecap="round"
|
|
20
|
+
strokeLinejoin="round"
|
|
21
|
+
aria-hidden="true"
|
|
22
|
+
focusable="false"
|
|
23
|
+
style={{ display: 'block', flexShrink: 0 }}
|
|
24
|
+
>
|
|
25
|
+
{paths}
|
|
26
|
+
</svg>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Circular arrow: run the same prompt again. */
|
|
31
|
+
export function RegenerateIcon(): ReactElement {
|
|
32
|
+
return icon([
|
|
33
|
+
<path key="arc" d="M21 12a9 9 0 1 1-3-6.7L21 8" />,
|
|
34
|
+
<path key="head" d="M21 3v5h-5" />,
|
|
35
|
+
])
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Two overlapping sheets: copy the result. */
|
|
39
|
+
export function CopyIcon(): ReactElement {
|
|
40
|
+
return icon([
|
|
41
|
+
<rect key="front" x="9" y="9" width="13" height="13" rx="2" />,
|
|
42
|
+
<path key="back" d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />,
|
|
43
|
+
])
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Arrow falling into a tray: fill the result into the composer. */
|
|
47
|
+
export function FillIcon(): ReactElement {
|
|
48
|
+
return icon([
|
|
49
|
+
<path key="stem" d="M12 3v12" />,
|
|
50
|
+
<path key="head" d="m7 10 5 5 5-5" />,
|
|
51
|
+
<path key="tray" d="M5 21h14" />,
|
|
52
|
+
])
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Left arrow over a return curve: restore the pre-apply draft. */
|
|
56
|
+
export function UndoIcon(): ReactElement {
|
|
57
|
+
return icon([
|
|
58
|
+
<path key="head" d="M9 14 4 9l5-5" />,
|
|
59
|
+
<path key="curve" d="M4 9h10.5a5.5 5.5 0 0 1 0 11H8" />,
|
|
60
|
+
])
|
|
61
|
+
}
|
|
@@ -10,14 +10,13 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { useCallback, useEffect, useRef, useSyncExternalStore, type ReactNode } from 'react'
|
|
13
|
-
import { createPortal } from 'react-dom'
|
|
14
13
|
import type { PropsLocale, PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots'
|
|
15
14
|
import type {} from '@deepseek-ai/dsh-client-ui-conversation/client'
|
|
16
15
|
import type { InputState } from '@deepseek-ai/dsh-client-ui-conversation/client'
|
|
17
16
|
import type { EnhanceError } from '../shared/protocol'
|
|
18
17
|
import { checkInputText } from '../shared/validate'
|
|
19
18
|
import { EnhanceClientError, requestEnhance, requestEnhanceStream } from './enhance-client'
|
|
20
|
-
import {
|
|
19
|
+
import { SparkleFishIcon } from './SparkleFishIcon'
|
|
21
20
|
import * as ui from './ui-state'
|
|
22
21
|
import { getClientSettings, subscribeClientSettings } from './settings'
|
|
23
22
|
import { useSessionKey, serverSessionId } from './session-key'
|
|
@@ -67,12 +66,6 @@ export function EnhanceButton(props: EnhanceButtonProps): ReactNode {
|
|
|
67
66
|
const draft = useInput((state: InputState) => stringOf((state as { draft?: unknown }).draft))
|
|
68
67
|
const phase = useInput((state: InputState) => (state as { phase?: unknown }).phase)
|
|
69
68
|
const occurrenceCount = useInput((state: InputState) => countOf((state as { occurrences?: unknown }).occurrences))
|
|
70
|
-
// `attachmentIds` is the 0.2.x field; `imageIds` is its 0.1.x predecessor.
|
|
71
|
-
// Both are read defensively so one binary serves both generations.
|
|
72
|
-
const attachmentCount = useInput((state: InputState) => {
|
|
73
|
-
const record = state as { attachmentIds?: unknown; imageIds?: unknown }
|
|
74
|
-
return countOf(record.attachmentIds) + countOf(record.imageIds)
|
|
75
|
-
})
|
|
76
69
|
const settings = useSyncExternalStore(subscribeClientSettings, getClientSettings)
|
|
77
70
|
const panel = useSyncExternalStore(ui.subscribe, ui.getPanel)
|
|
78
71
|
const rootRef = useRef<HTMLButtonElement | null>(null)
|
|
@@ -88,40 +81,38 @@ export function EnhanceButton(props: EnhanceButtonProps): ReactNode {
|
|
|
88
81
|
// cross-session busy state used to disable this button with no explanation,
|
|
89
82
|
// which reads as "broken" in a multi-session layout.
|
|
90
83
|
const otherBusy = anyBusy && !busy
|
|
84
|
+
/** No draft text at all (whitespace and attachment-only drafts count as empty): the icon dims and clicks are no-ops. */
|
|
85
|
+
const empty = draft.trim() === ''
|
|
91
86
|
|
|
92
87
|
/** Guard chain + fetch + panel transition for this session. */
|
|
93
|
-
const start = useCallback((): void => {
|
|
94
|
-
|
|
88
|
+
const start = useCallback((overrideText?: string): void => {
|
|
89
|
+
const source = overrideText ?? draft
|
|
90
|
+
if (anyBusy || source.trim() === '') return
|
|
95
91
|
if (!settings.enabled) {
|
|
96
92
|
const disabled = t('error.disabled')
|
|
97
93
|
ui.openError(uiKey, draft, { code: 'rejected', localized: disabled })
|
|
98
94
|
return
|
|
99
95
|
}
|
|
100
|
-
|
|
101
|
-
const attachmentsOnly = t('error.attachmentsOnly')
|
|
102
|
-
ui.openError(uiKey, draft, { code: 'rejected', localized: attachmentsOnly })
|
|
103
|
-
return
|
|
104
|
-
}
|
|
105
|
-
const check = checkInputText(draft, settings.maxInputChars)
|
|
96
|
+
const check = checkInputText(source, settings.maxInputChars)
|
|
106
97
|
if (!check.ok) {
|
|
107
98
|
const message = check.code === 'empty'
|
|
108
99
|
? t('error.empty')
|
|
109
100
|
: t('error.tooLong', { count: check.count, max: check.max })
|
|
110
|
-
ui.openError(uiKey,
|
|
101
|
+
ui.openError(uiKey, source, { code: 'rejected', localized: message })
|
|
111
102
|
return
|
|
112
103
|
}
|
|
113
104
|
if (occurrenceCount > 0) {
|
|
114
105
|
const occurrences = t('error.occurrences')
|
|
115
|
-
ui.openError(uiKey,
|
|
106
|
+
ui.openError(uiKey, source, { code: 'rejected', localized: occurrences })
|
|
116
107
|
return
|
|
117
108
|
}
|
|
118
109
|
if (phase !== 'plain') {
|
|
119
110
|
const busyPhase = t('error.phase')
|
|
120
|
-
ui.openError(uiKey,
|
|
111
|
+
ui.openError(uiKey, source, { code: 'rejected', localized: busyPhase })
|
|
121
112
|
return
|
|
122
113
|
}
|
|
123
114
|
const controller = new AbortController()
|
|
124
|
-
ui.openLoading({ sessionId: uiKey, original:
|
|
115
|
+
ui.openLoading({ sessionId: uiKey, original: source, abort: () => controller.abort() })
|
|
125
116
|
const settle = (result: Parameters<typeof ui.settleResult>[1]): void => ui.settleResult(uiKey, result)
|
|
126
117
|
const fail = (error: unknown): void => {
|
|
127
118
|
const detail: EnhanceError = error instanceof EnhanceClientError
|
|
@@ -133,14 +124,14 @@ export function EnhanceButton(props: EnhanceButtonProps): ReactNode {
|
|
|
133
124
|
// writes, and degrades to the one-shot call on its own when the host or
|
|
134
125
|
// the transport cannot stream.
|
|
135
126
|
if (settings.streaming) {
|
|
136
|
-
requestEnhanceStream({ sessionId: wireId, text:
|
|
127
|
+
requestEnhanceStream({ sessionId: wireId, text: source }, {
|
|
137
128
|
signal: controller.signal,
|
|
138
129
|
onDelta: (delta: string): void => ui.appendDelta(uiKey, delta),
|
|
139
130
|
}).then(settle, fail)
|
|
140
131
|
return
|
|
141
132
|
}
|
|
142
|
-
requestEnhance({ sessionId: wireId, text:
|
|
143
|
-
}, [anyBusy, draft,
|
|
133
|
+
requestEnhance({ sessionId: wireId, text: source }, controller.signal).then(settle, fail)
|
|
134
|
+
}, [anyBusy, draft, occurrenceCount, phase, settings, uiKey, wireId, t])
|
|
144
135
|
|
|
145
136
|
// The session registry holds a stable identity; run always dispatches to
|
|
146
137
|
// the latest start callback. The refresh rides an effect (never the render
|
|
@@ -152,7 +143,7 @@ export function EnhanceButton(props: EnhanceButtonProps): ReactNode {
|
|
|
152
143
|
useEffect(() => {
|
|
153
144
|
const entry: ui.SessionEntry = {
|
|
154
145
|
root: rootRef.current,
|
|
155
|
-
run: () => runRef.current(),
|
|
146
|
+
run: (text?: string) => runRef.current(text),
|
|
156
147
|
}
|
|
157
148
|
return ui.registerSession(uiKey, entry)
|
|
158
149
|
}, [uiKey])
|
|
@@ -166,49 +157,26 @@ export function EnhanceButton(props: EnhanceButtonProps): ReactNode {
|
|
|
166
157
|
}
|
|
167
158
|
}, [draft, panel, uiKey])
|
|
168
159
|
|
|
169
|
-
/** Apply the enhanced result: remember the CURRENT draft (pre-apply, so
|
|
170
|
-
* undo restores exactly this state even if the user typed during the
|
|
171
|
-
* request), then fill the enhanced text back. */
|
|
172
|
-
const apply = useCallback((): void => {
|
|
173
|
-
if (panel === undefined || panel.phase !== 'result' || panel.result === undefined) return
|
|
174
|
-
ui.pushUndo(uiKey, { original: draft, applied: panel.result.text })
|
|
175
|
-
inputActions.setDraft(panel.result.text)
|
|
176
|
-
ui.closePanel()
|
|
177
|
-
}, [draft, inputActions, panel, uiKey])
|
|
178
|
-
|
|
179
160
|
if (!settings.enabled) return null
|
|
180
161
|
|
|
181
|
-
const owned = panel !== undefined && panel.sessionId === uiKey ? panel : undefined
|
|
182
162
|
return (
|
|
183
163
|
<>
|
|
184
164
|
<button
|
|
185
165
|
ref={rootRef}
|
|
186
166
|
type="button"
|
|
187
|
-
className={`dsh-pe-btn${busy ? ' is-busy' : ''}`}
|
|
188
|
-
title={busy ? t('button.busy') : otherBusy ? t('button.otherBusy') : t('button.title')}
|
|
189
|
-
aria-label={otherBusy ? t('button.otherBusy') : t('button.title')}
|
|
167
|
+
className={`dsh-pe-btn${busy ? ' is-busy' : ''}${otherBusy && !empty ? ' is-blocked' : ''}${empty && !busy ? ' is-empty' : ''}`}
|
|
168
|
+
title={busy ? t('button.busy') : otherBusy ? t('button.otherBusy') : empty ? t('button.emptyTitle') : t('button.title')}
|
|
169
|
+
aria-label={busy ? t('button.busy') : otherBusy ? t('button.otherBusy') : empty ? t('button.emptyTitle') : t('button.title')}
|
|
190
170
|
// `aria-disabled` instead of `disabled`: a disabled button is skipped by
|
|
191
171
|
// the keyboard and by screen readers, so the reason this composer is
|
|
192
|
-
// inert (another session's enhancement occupies the
|
|
193
|
-
// panel) would be unreachable exactly for the users who
|
|
194
|
-
// click is a no-op either way
|
|
195
|
-
aria-disabled={otherBusy || undefined}
|
|
196
|
-
onClick={start}
|
|
172
|
+
// inert (empty draft, or another session's enhancement occupies the
|
|
173
|
+
// single preview panel) would be unreachable exactly for the users who
|
|
174
|
+
// need it. The click is a no-op either way.
|
|
175
|
+
aria-disabled={empty || otherBusy || undefined}
|
|
176
|
+
onClick={() => start()}
|
|
197
177
|
>
|
|
198
|
-
<span className="dsh-pe-btn-icon" aria-hidden
|
|
178
|
+
<span className="dsh-pe-btn-icon" aria-hidden><SparkleFishIcon size={15} spinning={busy} /></span>
|
|
199
179
|
</button>
|
|
200
|
-
{owned !== undefined && (
|
|
201
|
-
createPortal(
|
|
202
|
-
<ResultPanel
|
|
203
|
-
state={owned}
|
|
204
|
-
t={t}
|
|
205
|
-
onApply={apply}
|
|
206
|
-
onCancel={() => ui.closePanel()}
|
|
207
|
-
onRetry={owned.phase === 'error' ? start : undefined}
|
|
208
|
-
/>,
|
|
209
|
-
document.body,
|
|
210
|
-
)
|
|
211
|
-
)}
|
|
212
180
|
</>
|
|
213
181
|
)
|
|
214
182
|
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The result dock (conversation.input.dock): renders the enhancement card in
|
|
3
|
+
* the composer's dock row, above the input, instead of a modal overlay. It
|
|
4
|
+
* owns session ownership (only the session that started the request sees its
|
|
5
|
+
* card), the applied/undo bridge to the composer, Escape handling and the
|
|
6
|
+
* auto-close when the draft is sent.
|
|
7
|
+
* @module dsh-prompt-enhance/client/ResultDock
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { useEffect, useRef, useState, useSyncExternalStore, type ReactNode } from 'react'
|
|
11
|
+
import type { PropsLocale, PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots'
|
|
12
|
+
import type {} from '@deepseek-ai/dsh-client-ui-conversation/client'
|
|
13
|
+
import { ResultPanel } from './ResultPanel'
|
|
14
|
+
import * as ui from './ui-state'
|
|
15
|
+
import { useSessionKey } from './session-key'
|
|
16
|
+
|
|
17
|
+
/** Props of the input.dock entry: the InputZone owner share + session kit + locale seat. */
|
|
18
|
+
export type ResultDockProps = PropsRuntime<'conversation.input.dock'> & PropsLocale<'dsh-prompt-enhance'>
|
|
19
|
+
|
|
20
|
+
/** One session's enhancement card, docked above its composer. */
|
|
21
|
+
export function ResultDock(props: ResultDockProps): ReactNode {
|
|
22
|
+
const { t, sessionId, useInput, inputActions } = props
|
|
23
|
+
// 0.1.1-rc.2 carries sessionId on the props; 0.1.2-rc.1 dropped it. Use the
|
|
24
|
+
// host id for UI keying when present, else a stable per-mount fallback.
|
|
25
|
+
const uiKey = useSessionKey(sessionId, inputActions)
|
|
26
|
+
const rootRef = useRef<HTMLDivElement | null>(null)
|
|
27
|
+
const [undone, setUndone] = useState(false)
|
|
28
|
+
const [regenerated, setRegenerated] = useState(false)
|
|
29
|
+
const [pendingRegenerate, setPendingRegenerate] = useState(false)
|
|
30
|
+
const previousResultRef = useRef<unknown>(undefined)
|
|
31
|
+
const panel = useSyncExternalStore(ui.subscribe, ui.getPanel)
|
|
32
|
+
const draft = useInput((state) => {
|
|
33
|
+
const value = (state as { draft?: unknown }).draft
|
|
34
|
+
return typeof value === 'string' ? value : ''
|
|
35
|
+
})
|
|
36
|
+
const phase = useInput((state) => {
|
|
37
|
+
const value = (state as { phase?: unknown }).phase
|
|
38
|
+
return typeof value === 'string' ? value : ''
|
|
39
|
+
})
|
|
40
|
+
// The host exposes the session store as a standard hook; older hosts may
|
|
41
|
+
// omit it, in which case the input phase is the only send signal.
|
|
42
|
+
const useSession = (props as ResultDockProps & {
|
|
43
|
+
useSession?: (selector: (s: unknown) => unknown) => unknown
|
|
44
|
+
}).useSession
|
|
45
|
+
const sessionRunning = useSession === undefined
|
|
46
|
+
? false
|
|
47
|
+
: Boolean(useSession((state) => (state as { running?: unknown }).running))
|
|
48
|
+
const sessionQueued = useSession === undefined
|
|
49
|
+
? 0
|
|
50
|
+
: Number(useSession((state) => ((state as { queue?: unknown[] }).queue?.length ?? 0)))
|
|
51
|
+
|
|
52
|
+
// The applied chip is derived from the undo stack: it is true exactly while
|
|
53
|
+
// the composer still holds the applied text, and drops itself when the user
|
|
54
|
+
// edits (the UndoBar drops the stale entry in the same situation).
|
|
55
|
+
const entry = ui.peekUndo(uiKey)
|
|
56
|
+
const applied = entry !== undefined && entry.applied === draft
|
|
57
|
+
|
|
58
|
+
// Sending the draft dismisses the card; the undo row can still take over.
|
|
59
|
+
// The phase is checked where the host provides it; the session transition
|
|
60
|
+
// (running false→true, or a new queued turn) is the fallback for hosts that
|
|
61
|
+
// use different phase names.
|
|
62
|
+
const prevSession = useRef({ running: sessionRunning, queued: sessionQueued })
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
const prev = prevSession.current
|
|
65
|
+
prevSession.current = { running: sessionRunning, queued: sessionQueued }
|
|
66
|
+
if (panel === undefined || panel.sessionId !== uiKey) return
|
|
67
|
+
if (phase === 'sending' || (sessionRunning && !prev.running) || sessionQueued > prev.queued) {
|
|
68
|
+
ui.closePanel()
|
|
69
|
+
}
|
|
70
|
+
}, [phase, sessionRunning, sessionQueued, panel, uiKey])
|
|
71
|
+
|
|
72
|
+
// Non-modal, so Escape is the only keyboard affordance: abort/close.
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (panel === undefined || panel.sessionId !== uiKey) return
|
|
75
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
76
|
+
if (event.defaultPrevented || event.isComposing || event.key !== 'Escape') return
|
|
77
|
+
event.preventDefault()
|
|
78
|
+
ui.closePanel()
|
|
79
|
+
}
|
|
80
|
+
document.addEventListener('keydown', onKeyDown)
|
|
81
|
+
return () => document.removeEventListener('keydown', onKeyDown)
|
|
82
|
+
}, [panel, uiKey])
|
|
83
|
+
|
|
84
|
+
// The "undone" chip is transient: it confirms the undo for a moment, then
|
|
85
|
+
// clears without needing another state change.
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
if (!undone) return
|
|
88
|
+
const timer = setTimeout(() => setUndone(false), 2500)
|
|
89
|
+
return () => clearTimeout(timer)
|
|
90
|
+
}, [undone])
|
|
91
|
+
|
|
92
|
+
// "Regenerated" is a completion signal, not a click signal: wait until the
|
|
93
|
+
// panel settles with a different result object, then show the chip briefly.
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (!pendingRegenerate) return
|
|
96
|
+
if (panel === undefined || panel.sessionId !== uiKey) {
|
|
97
|
+
setPendingRegenerate(false)
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
if (panel.phase === 'error') {
|
|
101
|
+
setPendingRegenerate(false)
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
if (panel.phase === 'result' && panel.result !== previousResultRef.current) {
|
|
105
|
+
setPendingRegenerate(false)
|
|
106
|
+
setRegenerated(true)
|
|
107
|
+
}
|
|
108
|
+
}, [panel, pendingRegenerate, uiKey])
|
|
109
|
+
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
if (!regenerated) return
|
|
112
|
+
const timer = setTimeout(() => setRegenerated(false), 2500)
|
|
113
|
+
return () => clearTimeout(timer)
|
|
114
|
+
}, [regenerated])
|
|
115
|
+
|
|
116
|
+
// A regenerate click on an empty draft is a no-op; do not leave the pending
|
|
117
|
+
// flag stuck forever in that case.
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
if (!pendingRegenerate) return
|
|
120
|
+
const timer = setTimeout(() => setPendingRegenerate(false), 15000)
|
|
121
|
+
return () => clearTimeout(timer)
|
|
122
|
+
}, [pendingRegenerate])
|
|
123
|
+
|
|
124
|
+
// The dock row is wider than the composer card in some host layouts (the
|
|
125
|
+
// conversation column spans the pane while the card is centered). Align the
|
|
126
|
+
// card to the composer by measuring the composer's rounded card: find the
|
|
127
|
+
// nearest editor below the dock, walk up to the rounded surface, and reuse
|
|
128
|
+
// its width and left offset. The measurement is re-run on resize; when no
|
|
129
|
+
// editor exists the dock keeps the host's full width.
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
if (panel === undefined || panel.sessionId !== uiKey) return
|
|
132
|
+
const root = rootRef.current
|
|
133
|
+
if (root === null) return
|
|
134
|
+
let frame = 0
|
|
135
|
+
const align = (): void => {
|
|
136
|
+
const parent = root.parentElement
|
|
137
|
+
if (parent === null) return
|
|
138
|
+
// A transform-only offset is applied to the wrong base when the host
|
|
139
|
+
// already centers this entry, and a translated box can be clipped by its
|
|
140
|
+
// parent; use a layout margin instead. The previous margin is subtracted
|
|
141
|
+
// from the measured left so repeated runs do not accumulate drift.
|
|
142
|
+
root.style.transform = ''
|
|
143
|
+
const previousMargin = parseFloat(root.style.marginLeft) || 0
|
|
144
|
+
const rootRect = root.getBoundingClientRect()
|
|
145
|
+
const baseLeft = rootRect.left - previousMargin
|
|
146
|
+
const editors = Array.from(document.querySelectorAll<HTMLElement>('textarea, [contenteditable="true"], [role="textbox"]'))
|
|
147
|
+
const editor = editors.find((node) => {
|
|
148
|
+
const rect = node.getBoundingClientRect()
|
|
149
|
+
return rect.width > 0 && rect.height > 0 && rect.top >= rootRect.bottom - 4
|
|
150
|
+
})
|
|
151
|
+
if (editor === undefined) {
|
|
152
|
+
root.style.width = ''
|
|
153
|
+
root.style.marginLeft = ''
|
|
154
|
+
root.style.transform = ''
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
const editorRect = editor.getBoundingClientRect()
|
|
158
|
+
// Collect every rounded, opaque ancestor of the editor, then take the
|
|
159
|
+
// widest one that is still plausibly the composer card. The first match
|
|
160
|
+
// is often an inner wrapper a few pixels narrower than the card; the
|
|
161
|
+
// conversation pane above it is too wide. Center proximity rejects the
|
|
162
|
+
// pane when the composer is off-center, and the width cap rejects it
|
|
163
|
+
// when both are centered.
|
|
164
|
+
const candidates: { node: HTMLElement; rect: DOMRect }[] = []
|
|
165
|
+
let node: HTMLElement | null = editor
|
|
166
|
+
for (let depth = 0; depth < 12 && node !== null; depth++) {
|
|
167
|
+
const rect = node.getBoundingClientRect()
|
|
168
|
+
const style = getComputedStyle(node)
|
|
169
|
+
if (
|
|
170
|
+
rect.width >= editorRect.width
|
|
171
|
+
&& rect.width > 360
|
|
172
|
+
&& rect.height > 48
|
|
173
|
+
&& (parseFloat(style.borderTopLeftRadius) || 0) >= 4
|
|
174
|
+
&& style.backgroundColor !== 'rgba(0, 0, 0, 0)'
|
|
175
|
+
) {
|
|
176
|
+
candidates.push({ node, rect })
|
|
177
|
+
}
|
|
178
|
+
node = node.parentElement
|
|
179
|
+
}
|
|
180
|
+
const editorCenter = editorRect.left + editorRect.width / 2
|
|
181
|
+
const cardEntry = candidates
|
|
182
|
+
.filter((candidate) => (
|
|
183
|
+
candidate.rect.width <= window.innerWidth * 0.8
|
|
184
|
+
&& Math.abs(candidate.rect.left + candidate.rect.width / 2 - editorCenter) < 40
|
|
185
|
+
))
|
|
186
|
+
.sort((a, b) => b.rect.width - a.rect.width)[0]
|
|
187
|
+
?? candidates.sort((a, b) => b.rect.width - a.rect.width)[0]
|
|
188
|
+
if (cardEntry === undefined) return
|
|
189
|
+
const cardRect = cardEntry.rect
|
|
190
|
+
const width = `${cardRect.width}px`
|
|
191
|
+
if (root.style.width !== width) root.style.width = width
|
|
192
|
+
// Width can shift a centered entry; measure again before deriving the
|
|
193
|
+
// margin so the final left edge matches the composer card.
|
|
194
|
+
const afterWidth = root.getBoundingClientRect()
|
|
195
|
+
const currentMargin = parseFloat(root.style.marginLeft) || 0
|
|
196
|
+
const marginLeft = `${cardRect.left - (afterWidth.left - currentMargin)}px`
|
|
197
|
+
if (root.style.marginLeft !== marginLeft) root.style.marginLeft = marginLeft
|
|
198
|
+
}
|
|
199
|
+
const schedule = (): void => {
|
|
200
|
+
cancelAnimationFrame(frame)
|
|
201
|
+
frame = requestAnimationFrame(align)
|
|
202
|
+
}
|
|
203
|
+
schedule()
|
|
204
|
+
schedule()
|
|
205
|
+
window.addEventListener('resize', schedule)
|
|
206
|
+
const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(schedule)
|
|
207
|
+
observer?.observe(document.documentElement)
|
|
208
|
+
return () => {
|
|
209
|
+
cancelAnimationFrame(frame)
|
|
210
|
+
window.removeEventListener('resize', schedule)
|
|
211
|
+
observer?.disconnect()
|
|
212
|
+
}
|
|
213
|
+
}, [panel, uiKey])
|
|
214
|
+
|
|
215
|
+
if (panel === undefined || panel.sessionId !== uiKey) return null
|
|
216
|
+
|
|
217
|
+
const apply = (): void => {
|
|
218
|
+
if (panel.phase !== 'result' || panel.result === undefined) return
|
|
219
|
+
// Remember the CURRENT draft (which may have diverged from the text the
|
|
220
|
+
// request started with) so undo restores the user's latest edits, then
|
|
221
|
+
// fill the enhanced text. The card stays open in the applied state;
|
|
222
|
+
// closing it falls back to the standalone undo row.
|
|
223
|
+
ui.pushUndo(uiKey, { original: draft, applied: panel.result.text })
|
|
224
|
+
inputActions.setDraft(panel.result.text)
|
|
225
|
+
setUndone(false)
|
|
226
|
+
setRegenerated(false)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const undo = (): void => {
|
|
230
|
+
if (entry === undefined) return
|
|
231
|
+
inputActions.setDraft(entry.original)
|
|
232
|
+
ui.popUndo(uiKey)
|
|
233
|
+
setUndone(true)
|
|
234
|
+
setRegenerated(false)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const regenerate = (): void => {
|
|
238
|
+
if (panel.phase !== 'result') return
|
|
239
|
+
previousResultRef.current = panel.result
|
|
240
|
+
setRegenerated(false)
|
|
241
|
+
setPendingRegenerate(true)
|
|
242
|
+
ui.runSession(uiKey)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
<div ref={rootRef} className="dsh-pe-dock">
|
|
247
|
+
<ResultPanel
|
|
248
|
+
state={panel}
|
|
249
|
+
t={t}
|
|
250
|
+
applied={applied}
|
|
251
|
+
undone={undone}
|
|
252
|
+
regenerated={regenerated}
|
|
253
|
+
onApply={apply}
|
|
254
|
+
onUndo={undo}
|
|
255
|
+
onRegenerate={regenerate}
|
|
256
|
+
onCancel={() => ui.closePanel()}
|
|
257
|
+
onRetry={panel.phase === 'error' ? () => ui.runSession(uiKey) : undefined}
|
|
258
|
+
/>
|
|
259
|
+
</div>
|
|
260
|
+
)
|
|
261
|
+
}
|