@xiangyixie/dsh-superagent 0.14.2 → 0.14.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 ADDED
@@ -0,0 +1,210 @@
1
+ /**
2
+ * dsh-superagent — browser half.
3
+ *
4
+ * One left-sidebar navigation entry ("超级智能体") that opens the existing
5
+ * loopback console page inside a main panel. The console keeps its own
6
+ * conversation, task lines, summary, artifacts and history; this module adds
7
+ * no second UI, it only seats the page in the host frame.
8
+ *
9
+ * Failure containment is deliberate: this file must never take the host page
10
+ * down. The top level only registers a factory; everything else runs inside
11
+ * `withFallback`, so a bad address route or a missing slot leaves the entry
12
+ * absent instead of breaking the frame.
13
+ *
14
+ * @module dsh-superagent/client
15
+ */
16
+
17
+ window.__ModuleLoader__.load({
18
+ id: '@xiangyixie/dsh-superagent',
19
+ factory: require => {
20
+ // The host rejects the whole composition when a client module exposes no
21
+ // `apply`, so publish a valid face first and only then fill it in. Anything
22
+ // that fails below costs the sidebar row, never the page.
23
+ const exports = { apply() {} }
24
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
25
+
26
+ const PANEL_ID = 'superagent'
27
+ const PANEL_LABEL = '超级智能体'
28
+ /** Host web-server route owned by this plugin's node half. */
29
+ const ADDRESS_ROUTE = '/superagent/console-address'
30
+ const RETRIES = 3
31
+ const RETRY_MS = 700
32
+
33
+ let react = null
34
+ try {
35
+ react = require('react')
36
+ } catch (error) {
37
+ console.error('[dsh-superagent] cannot load react; the sidebar entry stays absent', error)
38
+ }
39
+
40
+ const h = react ? react.createElement : null
41
+
42
+ /** Decorative four-point spark used by the sidebar row. */
43
+ function EntryIcon(props) {
44
+ const size = typeof props?.size === 'number' ? props.size : 16
45
+ return h(
46
+ 'svg',
47
+ {
48
+ viewBox: '0 0 16 16',
49
+ width: size,
50
+ height: size,
51
+ fill: 'none',
52
+ stroke: 'currentColor',
53
+ strokeWidth: 1.4,
54
+ strokeLinecap: 'round',
55
+ strokeLinejoin: 'round',
56
+ 'aria-hidden': 'true',
57
+ },
58
+ h('path', { d: 'M8 1.6l1.7 4.7L14.4 8l-4.7 1.7L8 14.4 6.3 9.7 1.6 8l4.7-1.7z' }),
59
+ )
60
+ }
61
+
62
+ const frameStyle = { position: 'absolute', inset: 0, width: '100%', height: '100%', border: '0', display: 'block' }
63
+ const seatStyle = { position: 'relative', width: '100%', height: '100%', minHeight: '520px', background: 'transparent' }
64
+ const noteStyle = {
65
+ position: 'absolute',
66
+ inset: 0,
67
+ display: 'flex',
68
+ flexDirection: 'column',
69
+ alignItems: 'center',
70
+ justifyContent: 'center',
71
+ gap: '10px',
72
+ padding: '24px',
73
+ textAlign: 'center',
74
+ font: '13px/1.6 system-ui, sans-serif',
75
+ color: 'inherit',
76
+ }
77
+ const buttonStyle = {
78
+ font: 'inherit',
79
+ padding: '6px 14px',
80
+ borderRadius: '999px',
81
+ border: '1px solid currentColor',
82
+ background: 'transparent',
83
+ color: 'inherit',
84
+ cursor: 'pointer',
85
+ }
86
+
87
+ /**
88
+ * Ask the plugin's node half where the console listens. The answer carries
89
+ * the one-time token because this route is same-origin and loopback-only.
90
+ * @returns {Promise<{src: string} | {reason: string}>} iframe address or why it is missing.
91
+ */
92
+ async function readAddress() {
93
+ let response
94
+ try {
95
+ response = await fetch(ADDRESS_ROUTE, { headers: { accept: 'application/json' }, cache: 'no-store' })
96
+ } catch (error) {
97
+ return { reason: `取不到控制台地址:${error?.message ?? error}` }
98
+ }
99
+ let body = null
100
+ try {
101
+ body = await response.json()
102
+ } catch {
103
+ // A non-JSON answer means the host web server did not route to us.
104
+ }
105
+ const data = body?.data
106
+ if (response.ok && data?.ready === true && data.url && data.token) {
107
+ return { src: `${data.url}/?token=${encodeURIComponent(data.token)}` }
108
+ }
109
+ const reason = body?.message
110
+ ?? (typeof data?.reason === 'string' ? data.reason : null)
111
+ ?? (response.status === 404
112
+ ? '宿主还没接入 Superagent 控制台(console-address 路由未注册)。'
113
+ : `控制台暂不可用(HTTP ${response.status})。`)
114
+ return { reason }
115
+ }
116
+
117
+ /**
118
+ * The main panel: the console page itself, filling the frame.
119
+ * @returns {object} React element.
120
+ */
121
+ function ConsoleSurface() {
122
+ const [view, setView] = react.useState({ phase: 'loading', src: null, reason: '' })
123
+ const [attempt, setAttempt] = react.useState(0)
124
+
125
+ react.useEffect(() => {
126
+ let alive = true
127
+ let timer = null
128
+ let tries = 0
129
+ const settle = next => {
130
+ if (alive) setView(next)
131
+ }
132
+ const load = async () => {
133
+ const address = await readAddress()
134
+ if (!alive) return
135
+ if (address.src) {
136
+ settle({ phase: 'ready', src: address.src, reason: '' })
137
+ return
138
+ }
139
+ // The node half binds the console after the plugin is ready; a first
140
+ // miss is ordinary startup, not a failure the user should see.
141
+ if (tries < RETRIES) {
142
+ tries += 1
143
+ timer = setTimeout(load, RETRY_MS)
144
+ return
145
+ }
146
+ settle({ phase: 'error', src: null, reason: address.reason })
147
+ }
148
+ load()
149
+ return () => {
150
+ alive = false
151
+ if (timer) clearTimeout(timer)
152
+ }
153
+ }, [attempt])
154
+
155
+ const children = []
156
+ if (view.phase === 'ready') {
157
+ children.push(h('iframe', {
158
+ key: 'frame',
159
+ src: view.src,
160
+ title: 'Superagent 专属空间',
161
+ style: frameStyle,
162
+ allow: 'clipboard-write',
163
+ }))
164
+ } else if (view.phase === 'loading') {
165
+ children.push(h('div', { key: 'note', style: noteStyle }, '正在连接 Superagent 专属空间…'))
166
+ } else {
167
+ children.push(h(
168
+ 'div',
169
+ { key: 'note', style: noteStyle },
170
+ h('div', null, view.reason),
171
+ h('div', { style: { opacity: 0.7 } }, '在「高级」里确认控制台端口已开启(consolePort),或重试。'),
172
+ h('button', { style: buttonStyle, type: 'button', onClick: () => setAttempt(n => n + 1) }, '重试'),
173
+ ))
174
+ }
175
+ return h('div', { style: seatStyle }, children)
176
+ }
177
+
178
+ /** @param {object} ctx - client root context. */
179
+ function apply(ctx) {
180
+ try {
181
+ if (!h) return
182
+ ctx.slots.inject('main', () => ctx.slots.register(
183
+ { name: 'main', key: PANEL_ID },
184
+ ConsoleSurface,
185
+ ))
186
+ ctx.slots.inject('sidebar.panellist', () => ctx.slots.register(
187
+ {
188
+ name: 'sidebar.panellist',
189
+ id: PANEL_ID,
190
+ order: 5,
191
+ label: PANEL_LABEL,
192
+ },
193
+ EntryIcon,
194
+ ))
195
+ } catch (error) {
196
+ // A seat that cannot be taken must cost only this entry, never the frame.
197
+ console.error('[dsh-superagent] the sidebar entry could not be seated', error)
198
+ }
199
+ }
200
+
201
+ let inject = ['slots']
202
+ try {
203
+ exports.apply = apply
204
+ exports.inject = inject
205
+ } catch (error) {
206
+ console.error('[dsh-superagent] client exports could not be published', error)
207
+ }
208
+ return exports
209
+ },
210
+ })