@changfenhuang/dsh-genui 0.9.2 → 0.9.6
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/CHANGELOG.md +23 -0
- package/README.md +11 -0
- package/README.zh-CN.md +17 -0
- package/lib/client.js +3 -3
- package/lib/index.js +11 -4
- package/lib/types/client/TemplateDrawer.d.ts +7 -0
- package/lib/types/client/achievement-store.d.ts +35 -0
- package/lib/types/client/achievement-toast.d.ts +2 -0
- package/lib/types/client/achievements.d.ts +48 -0
- package/lib/types/client/panel.d.ts +4 -1
- package/lib/types/client/templates.d.ts +22 -0
- package/package.json +1 -1
- package/src/client/GenuiBlock.module.css +151 -14
- package/src/client/GenuiBlock.tsx +16 -1
- package/src/client/PlotBlock.tsx +23 -10
- package/src/client/TemplateDrawer.module.css +152 -0
- package/src/client/TemplateDrawer.tsx +137 -0
- package/src/client/achievement-store.ts +151 -0
- package/src/client/achievement-toast.tsx +68 -0
- package/src/client/achievements.ts +111 -0
- package/src/client/blocks/advanced.tsx +36 -4
- package/src/client/guard.ts +20 -4
- package/src/client/index.tsx +13 -0
- package/src/client/panel.tsx +109 -10
- package/src/client/templates.ts +280 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TemplateDrawer — GenUI 模板中心(0.9.4 新手引导)。
|
|
3
|
+
*
|
|
4
|
+
* 分类浏览 → 点击卡片 → 内嵌预览(demo spec 由 GenuiBlock 直接渲染,
|
|
5
|
+
* dogfooding)+「试用」插入输入框 /「复制指令」。用户先看到效果,
|
|
6
|
+
* 再决定把指令发给模型——genui 能力说明书。
|
|
7
|
+
*/
|
|
8
|
+
import { useMemo, useRef, useState } from 'react'
|
|
9
|
+
import { GENUI_TEMPLATES, type GenuiTemplate } from './templates.ts'
|
|
10
|
+
import { GenuiBlock } from './GenuiBlock.tsx'
|
|
11
|
+
import { ErrorBoundary } from './ErrorBoundary.tsx'
|
|
12
|
+
import { panelStateKey } from './interaction-store.ts'
|
|
13
|
+
import { buildAchievementsSpec } from './achievements.ts'
|
|
14
|
+
import { getAchievementSnapshot, subscribeAchievements } from './achievement-store.ts'
|
|
15
|
+
import { useSyncExternalStore } from 'react'
|
|
16
|
+
import css from './TemplateDrawer.module.css'
|
|
17
|
+
|
|
18
|
+
export interface TemplateDrawerProps {
|
|
19
|
+
/** 试用:把模板指令插入当前输入框草稿。 */
|
|
20
|
+
onUse: (instruction: string) => void
|
|
21
|
+
/** 当前面板(模板中心/成就页),由面板 header 按钮控制。 */
|
|
22
|
+
tab: 'templates' | 'achievements'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const CATEGORIES = ['全部', '仪表盘', '数据', '流程', '图表', '交互', '测验', '高级'] as const
|
|
26
|
+
type Category = (typeof CATEGORIES)[number]
|
|
27
|
+
|
|
28
|
+
/** Copy to clipboard with a legacy fallback (like GenuiCopy). */
|
|
29
|
+
function copyText(text: string): Promise<void> {
|
|
30
|
+
if (navigator.clipboard !== undefined) return navigator.clipboard.writeText(text)
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
try {
|
|
33
|
+
const ta = document.createElement('textarea')
|
|
34
|
+
ta.value = text
|
|
35
|
+
ta.style.position = 'fixed'
|
|
36
|
+
ta.style.opacity = '0'
|
|
37
|
+
document.body.appendChild(ta)
|
|
38
|
+
ta.select()
|
|
39
|
+
document.execCommand('copy')
|
|
40
|
+
ta.remove()
|
|
41
|
+
resolve()
|
|
42
|
+
} catch (error) {
|
|
43
|
+
reject(error instanceof Error ? error : new Error(String(error)))
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function TemplateDrawer({ onUse, tab }: TemplateDrawerProps) {
|
|
49
|
+
const [category, setCategory] = useState<Category>('全部')
|
|
50
|
+
const [selected, setSelected] = useState<GenuiTemplate | null>(null)
|
|
51
|
+
const [copied, setCopied] = useState(false)
|
|
52
|
+
const timer = useRef(0)
|
|
53
|
+
// Achievements tab: live state snapshot.
|
|
54
|
+
const achievements = useSyncExternalStore(subscribeAchievements, () => getAchievementSnapshot())
|
|
55
|
+
const achievementSpec = useMemo(
|
|
56
|
+
() => buildAchievementsSpec(achievements.state, achievements.unlocked),
|
|
57
|
+
[achievements],
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const items = useMemo(
|
|
61
|
+
() => (category === '全部' ? [...GENUI_TEMPLATES] : GENUI_TEMPLATES.filter(t => t.category === category)),
|
|
62
|
+
[category],
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
const copy = async (text: string): Promise<void> => {
|
|
66
|
+
try {
|
|
67
|
+
await copyText(text)
|
|
68
|
+
setCopied(true)
|
|
69
|
+
window.clearTimeout(timer.current)
|
|
70
|
+
timer.current = window.setTimeout(() => setCopied(false), 1500)
|
|
71
|
+
} catch {
|
|
72
|
+
// Clipboard denied: the instruction stays in the drawer; nothing else
|
|
73
|
+
// to do — the use flow does not depend on it.
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<div className={css.wrap} data-genui-templates={tab === 'templates' ? undefined : 'achievements'}>
|
|
79
|
+
{tab === 'achievements' ? (
|
|
80
|
+
<div className={css.achievements} data-genui-achievements>
|
|
81
|
+
<ErrorBoundary label="成就页">
|
|
82
|
+
<GenuiBlock spec={achievementSpec} stateKey={panelStateKey('genui-achievements', JSON.stringify(achievementSpec))} />
|
|
83
|
+
</ErrorBoundary>
|
|
84
|
+
</div>
|
|
85
|
+
) : (
|
|
86
|
+
<>
|
|
87
|
+
<div className={css.cats} role="tablist" aria-label="模板分类">
|
|
88
|
+
{CATEGORIES.map(c => (
|
|
89
|
+
<button
|
|
90
|
+
key={c}
|
|
91
|
+
type="button"
|
|
92
|
+
role="tab"
|
|
93
|
+
aria-selected={category === c}
|
|
94
|
+
className={`${css.cat}${category === c ? ` ${css.catActive}` : ''}`}
|
|
95
|
+
onClick={() => { setCategory(c); setSelected(null) }}
|
|
96
|
+
>
|
|
97
|
+
{c}
|
|
98
|
+
</button>
|
|
99
|
+
))}
|
|
100
|
+
</div>
|
|
101
|
+
<div className={css.list}>
|
|
102
|
+
{items.map(tpl => (
|
|
103
|
+
<button
|
|
104
|
+
key={tpl.id}
|
|
105
|
+
type="button"
|
|
106
|
+
className={`${css.card}${selected?.id === tpl.id ? ` ${css.cardActive}` : ''}`}
|
|
107
|
+
onClick={() => setSelected(prev => (prev?.id === tpl.id ? null : tpl))}
|
|
108
|
+
>
|
|
109
|
+
<span className={css.cardName}>{tpl.name}</span>
|
|
110
|
+
<span className={css.cardMeta}>{tpl.category}</span>
|
|
111
|
+
<span className={css.cardDesc}>{tpl.description}</span>
|
|
112
|
+
</button>
|
|
113
|
+
))}
|
|
114
|
+
</div>
|
|
115
|
+
{selected !== null && (
|
|
116
|
+
<div className={css.detail} data-genui-template-preview>
|
|
117
|
+
<div className={css.toolbar}>
|
|
118
|
+
<span className={css.toolbarTitle}>{selected.name}</span>
|
|
119
|
+
<button type="button" className={css.try} onClick={() => onUse(selected.instruction)}>
|
|
120
|
+
试用:插入输入框
|
|
121
|
+
</button>
|
|
122
|
+
<button type="button" className={css.copy} onClick={() => void copy(selected.instruction)}>
|
|
123
|
+
{copied ? '✓ 已复制' : '复制指令'}
|
|
124
|
+
</button>
|
|
125
|
+
</div>
|
|
126
|
+
<div className={css.preview}>
|
|
127
|
+
<ErrorBoundary label="模板预览">
|
|
128
|
+
<GenuiBlock spec={selected.demo} stateKey={panelStateKey('genui-tpl', selected.id)} />
|
|
129
|
+
</ErrorBoundary>
|
|
130
|
+
</div>
|
|
131
|
+
</div>
|
|
132
|
+
)}
|
|
133
|
+
</>
|
|
134
|
+
)}
|
|
135
|
+
</div>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* achievement-store.ts — 成就状态存储与埋点入口(模块级单例)。
|
|
3
|
+
*
|
|
4
|
+
* 只存计数与解锁时间戳;绝不含消息/内容。localStorage 持久化,
|
|
5
|
+
* 订阅接口供 toast 与成就页消费。埋点入口:
|
|
6
|
+
* - recordFence(spec):一次「内容不重复」的界面渲染(按 spec 指纹去重,
|
|
7
|
+
* LRU 指纹表跨刷新防重放重复计数)
|
|
8
|
+
* - recordPanel():面板 dock 出现
|
|
9
|
+
* - recordInteraction():组件动作回传(去抖后)
|
|
10
|
+
* - recordTemplateUse():模板中心试用
|
|
11
|
+
*/
|
|
12
|
+
import type { GenuiSpec } from './spec.ts'
|
|
13
|
+
import { ACHIEVEMENTS, checkAchievements, countSpecKinds, emptyState, type AchieveState, type AchievementDef } from './achievements.ts'
|
|
14
|
+
|
|
15
|
+
const STORE_KEY = 'dsh.genui.achievements'
|
|
16
|
+
const SEEN_KEY = 'dsh.genui.achievements.seen'
|
|
17
|
+
const SEEN_MAX = 200
|
|
18
|
+
|
|
19
|
+
interface AchieveStore {
|
|
20
|
+
state: AchieveState
|
|
21
|
+
unlocked: Record<string, number>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let store: AchieveStore = loadStore()
|
|
25
|
+
const listeners = new Set<() => void>()
|
|
26
|
+
/** 解锁 toast 队列(FIFO;成就页打开时也读)。 */
|
|
27
|
+
let toastQueue: AchievementDef[] = []
|
|
28
|
+
|
|
29
|
+
function loadStore(): AchieveStore {
|
|
30
|
+
try {
|
|
31
|
+
const raw = localStorage.getItem(STORE_KEY)
|
|
32
|
+
if (raw === null) return { state: emptyState(), unlocked: {} }
|
|
33
|
+
const parsed = JSON.parse(raw) as Partial<AchieveStore>
|
|
34
|
+
const state = { ...emptyState(), ...(parsed.state ?? {}) }
|
|
35
|
+
return { state, unlocked: typeof parsed.unlocked === 'object' && parsed.unlocked !== null ? parsed.unlocked as Record<string, number> : {} }
|
|
36
|
+
} catch {
|
|
37
|
+
return { state: emptyState(), unlocked: {} }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function saveStore(): void {
|
|
42
|
+
try {
|
|
43
|
+
localStorage.setItem(STORE_KEY, JSON.stringify(store))
|
|
44
|
+
} catch {
|
|
45
|
+
// Quota / privacy-mode failures are non-fatal: achievements still tick
|
|
46
|
+
// in memory for this page session.
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function readSeen(): string[] {
|
|
51
|
+
try {
|
|
52
|
+
const raw = localStorage.getItem(SEEN_KEY)
|
|
53
|
+
if (raw === null) return []
|
|
54
|
+
const parsed = JSON.parse(raw) as unknown
|
|
55
|
+
return Array.isArray(parsed) ? parsed.filter((v): v is string => typeof v === 'string') : []
|
|
56
|
+
} catch {
|
|
57
|
+
return []
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function writeSeen(list: string[]): void {
|
|
62
|
+
try {
|
|
63
|
+
localStorage.setItem(SEEN_KEY, JSON.stringify(list.slice(0, SEEN_MAX)))
|
|
64
|
+
} catch {
|
|
65
|
+
// non-fatal
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function emit(): void {
|
|
70
|
+
for (const listener of listeners) listener()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 应用一次状态更新:持久化 + 检查新解锁(进 toast 队列)+ 通知订阅者。 */
|
|
74
|
+
function applyDelta(patch: Partial<AchieveState>): void {
|
|
75
|
+
store.state = { ...store.state, ...patch }
|
|
76
|
+
const fresh = checkAchievements(store.state, store.unlocked)
|
|
77
|
+
for (const ach of fresh) store.unlocked[ach.id] = Date.now()
|
|
78
|
+
if (fresh.length > 0) toastQueue.push(...fresh)
|
|
79
|
+
saveStore()
|
|
80
|
+
emit()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 53-bit string hash (cyrb53-style): dedupe fingerprints are stored as
|
|
84
|
+
* hashes, never as the serialized spec itself — localStorage must not
|
|
85
|
+
* persist conversation-derived content (table rows, text, …). */
|
|
86
|
+
function fingerprintOf(spec: GenuiSpec): string {
|
|
87
|
+
const s = JSON.stringify(spec)
|
|
88
|
+
let h1 = 0xdeadbeef
|
|
89
|
+
let h2 = 0x41c6ce57
|
|
90
|
+
for (let i = 0; i < s.length; i++) {
|
|
91
|
+
const ch = s.charCodeAt(i)
|
|
92
|
+
h1 = Math.imul(h1 ^ ch, 2654435761)
|
|
93
|
+
h2 = Math.imul(h2 ^ ch, 1597334677)
|
|
94
|
+
}
|
|
95
|
+
return `${(h1 >>> 0).toString(36)}-${(h2 >>> 0).toString(36)}`
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 记录一次「内容不重复」的 fence 渲染。 */
|
|
99
|
+
export function recordFence(spec: GenuiSpec): void {
|
|
100
|
+
const fingerprint = fingerprintOf(spec)
|
|
101
|
+
const seen = readSeen()
|
|
102
|
+
if (seen.includes(fingerprint)) return
|
|
103
|
+
writeSeen([fingerprint, ...seen])
|
|
104
|
+
const kinds = countSpecKinds(spec)
|
|
105
|
+
applyDelta({
|
|
106
|
+
fences: store.state.fences + 1,
|
|
107
|
+
charts: store.state.charts + (kinds.charts > 0 ? 1 : 0),
|
|
108
|
+
advanced: store.state.advanced + (kinds.advanced > 0 ? 1 : 0),
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** 记录一次面板 dock 出现(仅会话从无到有时)。 */
|
|
113
|
+
export function recordPanel(): void {
|
|
114
|
+
applyDelta({ panels: store.state.panels + 1 })
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 记录一次组件交互动作回传。 */
|
|
118
|
+
export function recordInteraction(): void {
|
|
119
|
+
applyDelta({ interactions: store.state.interactions + 1 })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 记录一次模板试用。 */
|
|
123
|
+
export function recordTemplateUse(): void {
|
|
124
|
+
applyDelta({ templates: store.state.templates + 1 })
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 当前状态快照(订阅者读取)。 */
|
|
128
|
+
export function getAchievementSnapshot(): { state: AchieveState, unlocked: Record<string, number> } {
|
|
129
|
+
return store
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 取走新解锁(toast 消费;一次性)。 */
|
|
133
|
+
export function consumeUnlocks(): AchievementDef[] {
|
|
134
|
+
const queue = toastQueue
|
|
135
|
+
toastQueue = []
|
|
136
|
+
return queue
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** 订阅状态变化(toast 与成就页)。 */
|
|
140
|
+
export function subscribeAchievements(listener: () => void): () => void {
|
|
141
|
+
listeners.add(listener)
|
|
142
|
+
return () => listeners.delete(listener)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 成就页 spec 数据源(面板内渲染用)。 */
|
|
146
|
+
export function achievementCount(): { total: number, unlocked: number } {
|
|
147
|
+
return {
|
|
148
|
+
total: ACHIEVEMENTS.length,
|
|
149
|
+
unlocked: ACHIEVEMENTS.filter(a => store.unlocked[a.id] !== undefined).length,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* achievement-toast.tsx — 解锁 toast 栈(右下角,3.6s 自动消退)。
|
|
3
|
+
*
|
|
4
|
+
* 独立 React root(document.body 级):面板/inline fence 任一解锁都会
|
|
5
|
+
* 弹——不依赖面板存在。消费 achievement-store 的新解锁队列。
|
|
6
|
+
*/
|
|
7
|
+
import { createElement, useEffect, useRef, useState, useSyncExternalStore } from 'react'
|
|
8
|
+
import { createRoot } from 'react-dom/client'
|
|
9
|
+
import { consumeUnlocks, subscribeAchievements } from './achievement-store.ts'
|
|
10
|
+
import type { AchievementDef } from './achievements.ts'
|
|
11
|
+
import css from './GenuiBlock.module.css'
|
|
12
|
+
|
|
13
|
+
interface ToastItem {
|
|
14
|
+
key: number
|
|
15
|
+
ach: AchievementDef
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let toastKey = 0
|
|
19
|
+
|
|
20
|
+
/** 底部固定栈:显示队列中的最后一个(FIFO 展开为纵向栈)。 */
|
|
21
|
+
function AchievementToasts() {
|
|
22
|
+
const [, force] = useSyncExternalStore(subscribeAchievements, () => '')
|
|
23
|
+
const [items, setItems] = useState<ToastItem[]>([])
|
|
24
|
+
const timers = useRef<Map<number, number>>(new Map())
|
|
25
|
+
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
const queue = consumeUnlocks()
|
|
28
|
+
if (queue.length === 0) return
|
|
29
|
+
const next: ToastItem[] = queue.map(ach => ({ key: ++toastKey, ach }))
|
|
30
|
+
setItems(prev => [...prev, ...next])
|
|
31
|
+
for (const item of next) {
|
|
32
|
+
const t = window.setTimeout(() => {
|
|
33
|
+
setItems(prev => prev.filter(i => i.key !== item.key))
|
|
34
|
+
timers.current.delete(item.key)
|
|
35
|
+
}, 3600)
|
|
36
|
+
timers.current.set(item.key, t)
|
|
37
|
+
}
|
|
38
|
+
}, [force])
|
|
39
|
+
|
|
40
|
+
useEffect(() => () => {
|
|
41
|
+
for (const t of timers.current.values()) window.clearTimeout(t)
|
|
42
|
+
}, [])
|
|
43
|
+
|
|
44
|
+
if (items.length === 0) return null
|
|
45
|
+
return createElement('div', { className: css.achToasts, 'data-genui-achievement-toasts': true },
|
|
46
|
+
items.map(item => createElement('div', { key: item.key, className: css.achToast },
|
|
47
|
+
createElement('span', { className: css.achToastBadge, 'aria-hidden': true }, '🏆'),
|
|
48
|
+
createElement('div', { className: css.achToastBody },
|
|
49
|
+
createElement('div', { className: css.achToastName }, `成就解锁:${item.ach.name}`),
|
|
50
|
+
createElement('div', { className: css.achToastDesc }, item.ach.description),
|
|
51
|
+
),
|
|
52
|
+
)),
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 挂载 toast 栈(apply 调用;返回卸载函数)。 */
|
|
57
|
+
export function mountAchievementToasts(): () => void {
|
|
58
|
+
if (typeof document === 'undefined') return () => {}
|
|
59
|
+
const host = document.createElement('div')
|
|
60
|
+
host.dataset.dshGenuiAchievements = '1'
|
|
61
|
+
document.body.appendChild(host)
|
|
62
|
+
const root = createRoot(host)
|
|
63
|
+
root.render(createElement(AchievementToasts))
|
|
64
|
+
return () => {
|
|
65
|
+
root.unmount()
|
|
66
|
+
host.remove()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* achievements.ts — GenUI 探索成就(0.9.5)。
|
|
3
|
+
*
|
|
4
|
+
* 轻量本地成就:只统计「使用事件计数」(渲染过的 fence、面板出现、
|
|
5
|
+
* 交互回传、模板试用),绝不读消息/内容;状态存 localStorage,跨会话
|
|
6
|
+
* 保留。解锁时进 toast 队列(achievement-toast 消费),面板「成就」
|
|
7
|
+
* tab 用 dsh-ui 渲染自己的成就页(dogfooding)。
|
|
8
|
+
*
|
|
9
|
+
* 设计与 dsh-achievements 的分层快照思路一致,但零宿主改动:埋点全部
|
|
10
|
+
* 在本包的渲染/交互路径上(GenuiBlock/TemplateDrawer/GenuiPanel)。
|
|
11
|
+
*/
|
|
12
|
+
import type { GenuiSpec, GenuiNode } from './spec.ts'
|
|
13
|
+
|
|
14
|
+
/** 累积使用计数(成就的输入)。 */
|
|
15
|
+
export interface AchieveState {
|
|
16
|
+
/** 渲染过的不重复 fence 数。 */
|
|
17
|
+
fences: number
|
|
18
|
+
/** 面板(panel dock)出现过的会话数。 */
|
|
19
|
+
panels: number
|
|
20
|
+
/** 交互组件动作回传次数(去抖后)。 */
|
|
21
|
+
interactions: number
|
|
22
|
+
/** 模板试用次数。 */
|
|
23
|
+
templates: number
|
|
24
|
+
/** 图表节点(chart/plot/echart)出现过的 fence 数。 */
|
|
25
|
+
charts: number
|
|
26
|
+
/** 高级节点(scene3d/mermaid/diagram)出现过的 fence 数。 */
|
|
27
|
+
advanced: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function emptyState(): AchieveState {
|
|
31
|
+
return { fences: 0, panels: 0, interactions: 0, templates: 0, charts: 0, advanced: 0 }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface AchievementDef {
|
|
35
|
+
id: string
|
|
36
|
+
name: string
|
|
37
|
+
description: string
|
|
38
|
+
/** 解锁前隐藏名称/描述(彩蛋)。 */
|
|
39
|
+
hidden?: boolean
|
|
40
|
+
/** 稀有度。 */
|
|
41
|
+
rarity: 'common' | 'rare' | 'legendary'
|
|
42
|
+
check: (s: AchieveState) => boolean
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const fences = (n: number) => (s: AchieveState): boolean => s.fences >= n
|
|
46
|
+
|
|
47
|
+
export const ACHIEVEMENTS: readonly AchievementDef[] = [
|
|
48
|
+
{ id: 'first-fence', name: '初次相见', description: '渲染了第一块 GenUI 界面。', rarity: 'common', check: fences(1) },
|
|
49
|
+
{ id: 'fence-5', name: '渐入佳境', description: '累计渲染 5 个界面。', rarity: 'common', check: fences(5) },
|
|
50
|
+
{ id: 'fence-25', name: '界面编织者', description: '累计渲染 25 个界面。', rarity: 'rare', check: fences(25) },
|
|
51
|
+
{ id: 'fence-50', name: '蓝图之魂', description: '累计渲染 50 个界面——你是 GenUI 的老朋友了。', rarity: 'legendary', hidden: true, check: fences(50) },
|
|
52
|
+
{ id: 'first-interaction', name: '按钮按得动', description: '触发了一次组件交互(动作回传)。', rarity: 'common', check: s => s.interactions >= 1 },
|
|
53
|
+
{ id: 'interaction-10', name: '小小操纵者', description: '累计触发 10 次组件交互。', rarity: 'rare', check: s => s.interactions >= 10 },
|
|
54
|
+
{ id: 'first-panel', name: '钉在墙上', description: '第一次把内容钉进了会话面板。', rarity: 'common', check: s => s.panels >= 1 },
|
|
55
|
+
{ id: 'panel-5', name: '面板管家', description: '5 个会话都用过面板。', rarity: 'rare', check: s => s.panels >= 5 },
|
|
56
|
+
{ id: 'first-chart', name: '图表爱好者', description: '渲染了第一张图表(chart/plot/echart)。', rarity: 'common', check: s => s.charts >= 1 },
|
|
57
|
+
{ id: 'chart-10', name: '数据可视化人', description: '10 个界面里出现过图表。', rarity: 'rare', check: s => s.charts >= 10 },
|
|
58
|
+
{ id: 'first-advanced', name: '高级玩家', description: '用上了 3D/图表引擎/架构图之一。', rarity: 'rare', check: s => s.advanced >= 1 },
|
|
59
|
+
{ id: 'template-1', name: '模板学员', description: '从模板中心试用了一个模板。', rarity: 'common', check: s => s.templates >= 1 },
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
/** 统计一个 spec 里出现的相关节点类别(与 guard 同口径遍历)。 */
|
|
63
|
+
export function countSpecKinds(spec: GenuiSpec): { charts: number, advanced: number } {
|
|
64
|
+
let charts = 0
|
|
65
|
+
let advanced = 0
|
|
66
|
+
const walk = (list: unknown): void => {
|
|
67
|
+
if (!Array.isArray(list)) return
|
|
68
|
+
for (const item of list) {
|
|
69
|
+
if (typeof item !== 'object' || item === null) continue
|
|
70
|
+
const v = item as Record<string, unknown>
|
|
71
|
+
const type = v.type
|
|
72
|
+
if (type === 'chart' || type === 'plot' || type === 'echart') charts += 1
|
|
73
|
+
if (type === 'scene3d' || type === 'mermaid' || type === 'diagram') advanced += 1
|
|
74
|
+
// 容器与列表项递归
|
|
75
|
+
if (Array.isArray(v.items)) walk(v.items as unknown[])
|
|
76
|
+
if (Array.isArray((v as { tabs?: unknown }).tabs)) {
|
|
77
|
+
for (const tab of (v as { tabs: Array<{ items?: unknown }> }).tabs) walk(tab.items)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
walk(spec.items)
|
|
82
|
+
return { charts, advanced }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 生成成就页 spec(dsh-ui 渲染):进度 stat + 解锁列表 + 稀有度徽标。 */
|
|
86
|
+
export function buildAchievementsSpec(state: AchieveState, unlocked: Record<string, number>): GenuiSpec {
|
|
87
|
+
const total = ACHIEVEMENTS.length
|
|
88
|
+
const unlockedCount = ACHIEVEMENTS.filter(a => unlocked[a.id] !== undefined).length
|
|
89
|
+
const items: GenuiNode[] = [
|
|
90
|
+
{ type: 'grid', cols: 3, items: [
|
|
91
|
+
{ type: 'stat', label: '已解锁', value: `${unlockedCount} / ${total}` },
|
|
92
|
+
{ type: 'stat', label: '渲染界面', value: String(state.fences) },
|
|
93
|
+
{ type: 'stat', label: '组件交互', value: String(state.interactions) },
|
|
94
|
+
] },
|
|
95
|
+
{ type: 'progress', label: '探索进度', value: Math.round(unlockedCount / total * 100), valueLabel: `${Math.round(unlockedCount / total * 100)}%` },
|
|
96
|
+
{ type: 'list', items: ACHIEVEMENTS.map(a => ({
|
|
97
|
+
type: 'row',
|
|
98
|
+
items: [
|
|
99
|
+
{ type: 'badge', label: a.rarity === 'legendary' ? '传说' : a.rarity === 'rare' ? '稀有' : '普通', tone: unlocked[a.id] !== undefined ? 'success' : 'warn' },
|
|
100
|
+
{ type: 'text', size: 'body', content: a.hidden && unlocked[a.id] === undefined ? '?' : a.name },
|
|
101
|
+
{ type: 'text', size: 'muted', content: a.hidden && unlocked[a.id] === undefined ? '继续探索以揭示' : a.description },
|
|
102
|
+
],
|
|
103
|
+
})) },
|
|
104
|
+
]
|
|
105
|
+
return { title: 'GenUI 探索成就', items }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 检查给定状态下的新解锁(不受 hidden 限制——规则只管阈值)。 */
|
|
109
|
+
export function checkAchievements(state: AchieveState, unlocked: Record<string, number>): AchievementDef[] {
|
|
110
|
+
return ACHIEVEMENTS.filter(a => unlocked[a.id] === undefined && a.check(state))
|
|
111
|
+
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @module @changfenhuang/dsh-genui/client/blocks/advanced
|
|
6
6
|
*/
|
|
7
7
|
import { memo, useEffect, useId, useMemo, useRef, useState, type ReactNode } from 'react'
|
|
8
|
-
import { CodeBlock, DiffBlock, JsonTree } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
8
|
+
import { CodeBlock, DiffBlock, JsonTree, writeClipboard } from '@deepseek-ai/dsh-client-ui-primitives'
|
|
9
9
|
import css from '../GenuiBlock.module.css'
|
|
10
10
|
import { GENUI_LIMITS } from '../guard.ts'
|
|
11
11
|
import { PlotBlock } from '../PlotBlock.tsx'
|
|
@@ -209,6 +209,35 @@ export function AccordionNode({ node, onAction, depth = 0, answers }: {
|
|
|
209
209
|
)
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
+
async function writeCopyText(text: string): Promise<boolean> {
|
|
213
|
+
/* oxlint-disable-next-line typescript/no-unnecessary-condition */
|
|
214
|
+
const hasAsyncClipboard = typeof navigator.clipboard?.writeText === 'function'
|
|
215
|
+
const accepted = await writeClipboard(text)
|
|
216
|
+
if (accepted || !hasAsyncClipboard) return accepted
|
|
217
|
+
|
|
218
|
+
// ponytail: remove this retry once the host helper falls back after Clipboard API rejection.
|
|
219
|
+
/* oxlint-disable typescript/no-deprecated */
|
|
220
|
+
const exec = typeof document.execCommand === 'function'
|
|
221
|
+
? document.execCommand.bind(document)
|
|
222
|
+
: undefined
|
|
223
|
+
if (exec === undefined) return false
|
|
224
|
+
const el = document.createElement('textarea')
|
|
225
|
+
el.value = text
|
|
226
|
+
el.setAttribute('readonly', '')
|
|
227
|
+
el.style.position = 'fixed'
|
|
228
|
+
el.style.left = '-9999px'
|
|
229
|
+
document.body.appendChild(el)
|
|
230
|
+
el.select()
|
|
231
|
+
try {
|
|
232
|
+
return exec('copy')
|
|
233
|
+
} catch {
|
|
234
|
+
return false
|
|
235
|
+
} finally {
|
|
236
|
+
el.remove()
|
|
237
|
+
}
|
|
238
|
+
/* oxlint-enable typescript/no-deprecated */
|
|
239
|
+
}
|
|
240
|
+
|
|
212
241
|
/** Copy: a one-click copy chip. The live region is a visually-hidden SIBLING
|
|
213
242
|
* (not inside the button) — button content is atomic to screen readers, so a
|
|
214
243
|
* live region inside it would never announce. */
|
|
@@ -220,9 +249,12 @@ export const CopyNode = memo(function CopyNode({ node }: { node: GenuiCopy }) {
|
|
|
220
249
|
type="button"
|
|
221
250
|
className={`${css.copyChip} ${copied ? css.copyChipDone : ''}`}
|
|
222
251
|
onClick={() => {
|
|
223
|
-
void
|
|
224
|
-
|
|
225
|
-
|
|
252
|
+
void writeCopyText(node.text).then((ok) => {
|
|
253
|
+
if (ok) {
|
|
254
|
+
setCopied(true)
|
|
255
|
+
setTimeout(() => setCopied(false), 1500)
|
|
256
|
+
}
|
|
257
|
+
})
|
|
226
258
|
}}
|
|
227
259
|
>
|
|
228
260
|
{copied ? '✓ 已复制' : (node.label ?? '复制')}
|
package/src/client/guard.ts
CHANGED
|
@@ -519,7 +519,7 @@ function repairNode(value: unknown, ctx: RepairCtx, depth: number): GenuiNode |
|
|
|
519
519
|
}
|
|
520
520
|
case 'quiz': {
|
|
521
521
|
const question = str(v.question, GENUI_LIMITS.maxString)
|
|
522
|
-
const options = repairQuizOptions(v.options)
|
|
522
|
+
const options = repairQuizOptions(v.options, v.answer)
|
|
523
523
|
if (question === undefined || options === undefined) return null
|
|
524
524
|
return {
|
|
525
525
|
type: 'quiz', question, options,
|
|
@@ -983,13 +983,13 @@ function walkTree(v: unknown, cap: number, depthLeft: number): GenuiFileTreeNode
|
|
|
983
983
|
return out
|
|
984
984
|
}
|
|
985
985
|
|
|
986
|
-
function repairQuizOptions(v: unknown): Array<{ label: string; correct?: boolean; feedback?: string }> | undefined {
|
|
986
|
+
function repairQuizOptions(v: unknown, answer?: unknown): Array<{ label: string; correct?: boolean; feedback?: string }> | undefined {
|
|
987
987
|
if (!Array.isArray(v)) return undefined
|
|
988
988
|
const out: Array<{ label: string; correct?: boolean; feedback?: string }> = []
|
|
989
989
|
for (const optItem of v) {
|
|
990
990
|
if (out.length >= GENUI_LIMITS.maxQuizOptions) break
|
|
991
991
|
const o = obj(optItem)
|
|
992
|
-
const label = o === undefined ? undefined : str(o.label, 512)
|
|
992
|
+
const label = typeof optItem === 'string' ? str(optItem, 512) : o === undefined ? undefined : str(o.label, 512)
|
|
993
993
|
if (label === undefined) continue
|
|
994
994
|
out.push({
|
|
995
995
|
label,
|
|
@@ -997,7 +997,23 @@ function repairQuizOptions(v: unknown): Array<{ label: string; correct?: boolean
|
|
|
997
997
|
...opt('feedback', o === undefined ? undefined : str(o.feedback, GENUI_LIMITS.maxString)),
|
|
998
998
|
})
|
|
999
999
|
}
|
|
1000
|
-
return
|
|
1000
|
+
// Nothing recoverable (empty array, all-non-string non-object items): return
|
|
1001
|
+
// undefined rather than an empty list, so repairNode drops the whole quiz
|
|
1002
|
+
// instead of half-rendering a question with no options — the same
|
|
1003
|
+
// silently-unusable state the string-options case produces when kept.
|
|
1004
|
+
if (out.length === 0) return undefined
|
|
1005
|
+
|
|
1006
|
+
// The canonical quiz shape stores correctness on each option. Models
|
|
1007
|
+
// commonly emit the simpler { options: string[], answer } form; only
|
|
1008
|
+
// use that alias when no canonical correct marker was supplied.
|
|
1009
|
+
if (out.some(option => option.correct === true)) return out
|
|
1010
|
+
const answerIndex = typeof answer === 'number' && Number.isFinite(answer)
|
|
1011
|
+
? Math.trunc(answer)
|
|
1012
|
+
: typeof answer === 'string'
|
|
1013
|
+
? out.findIndex(option => option.label === answer.slice(0, 512))
|
|
1014
|
+
: -1
|
|
1015
|
+
if (answerIndex < 0 || answerIndex >= out.length) return out
|
|
1016
|
+
return out.map((option, index) => index === answerIndex ? { ...option, correct: true } : option)
|
|
1001
1017
|
}
|
|
1002
1018
|
|
|
1003
1019
|
/**
|
package/src/client/index.tsx
CHANGED
|
@@ -32,6 +32,7 @@ import { renderGenuiFence, type GenuiFenceContext } from './fence-render.tsx'
|
|
|
32
32
|
import { createPanelSlashSource } from './panel-command.ts'
|
|
33
33
|
import { GenuiPanel, type GenuiPanelInjected } from './panel.tsx'
|
|
34
34
|
import { GenuiToolView } from './toolview.tsx'
|
|
35
|
+
import { mountAchievementToasts } from './achievement-toast.tsx'
|
|
35
36
|
import type { InputTriggerServiceContract } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
|
|
36
37
|
import { assetUrl } from './asset-loader.ts'
|
|
37
38
|
|
|
@@ -77,6 +78,16 @@ function panelActionSend(ctx: Context, sessionId: SessionId): GenuiPanelInjected
|
|
|
77
78
|
console.warn(`[genui] 面板动作 "${action}" 发送失败(session ${sessionId}):`, err instanceof Error ? err.message : String(err))
|
|
78
79
|
})
|
|
79
80
|
},
|
|
81
|
+
insertTemplate: (text) => {
|
|
82
|
+
// Same insert channel as dsh-memory's organizeMemory: the standard
|
|
83
|
+
// conversation.input.for(scope).setDraft — templates land in the
|
|
84
|
+
// composer draft, not a queued message, so the user can edit before
|
|
85
|
+
// sending.
|
|
86
|
+
const input = (conversation as unknown as {
|
|
87
|
+
input?: { for?(actx: unknown): { setDraft(text: string): void } | undefined }
|
|
88
|
+
} | undefined)?.input?.for?.(scoped)
|
|
89
|
+
input?.setDraft(text)
|
|
90
|
+
},
|
|
80
91
|
}
|
|
81
92
|
}
|
|
82
93
|
|
|
@@ -130,6 +141,8 @@ export function apply(ctx: Context): () => void {
|
|
|
130
141
|
// a session usually hits a warm cache instead of paying the fetch on first
|
|
131
142
|
// use. Never blocks first paint; the loader still handles a miss.
|
|
132
143
|
prefetchGenuiAssets()
|
|
144
|
+
// Achievement toasts (0.9.5): a body-level stack independent of the panel.
|
|
145
|
+
disposers.push(mountAchievementToasts())
|
|
133
146
|
// Keyed toolview: the harness dispatches 'tool.call.toolview' by wire tool
|
|
134
147
|
// name; registering under 'render_ui' gives the tool's result card the
|
|
135
148
|
// GenUI renderer (reading the repaired spec from result meta). The toolview
|