dsh-selection-annotations 0.0.0-stage → 2.1.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 (46) hide show
  1. package/CHANGELOG.md +166 -0
  2. package/LICENSE +21 -0
  3. package/README.md +161 -3
  4. package/README.zh-CN.md +161 -0
  5. package/client.js +11136 -0
  6. package/cordis.patch.yml +3 -0
  7. package/icon.svg +7 -0
  8. package/lib/annotations-db.js +1157 -0
  9. package/lib/index.js +1304 -0
  10. package/package.json +74 -6
  11. package/scripts/build.mjs +37 -0
  12. package/scripts/check-build.mjs +10 -0
  13. package/src/core/annotation-clipboard.js +93 -0
  14. package/src/core/annotation-utils.js +27 -0
  15. package/src/core/cards.js +502 -0
  16. package/src/core/config.js +91 -0
  17. package/src/core/constants.js +34 -0
  18. package/src/core/draft-codec.js +77 -0
  19. package/src/core/draft-store.js +85 -0
  20. package/src/core/hidden-payload.js +73 -0
  21. package/src/core/image-codec.js +111 -0
  22. package/src/core/jump-icon.js +25 -0
  23. package/src/core/locale.js +59 -0
  24. package/src/core/log.js +19 -0
  25. package/src/core/markdown.js +80 -0
  26. package/src/core/messages.js +311 -0
  27. package/src/core/note-tone.js +77 -0
  28. package/src/core/panel-render.js +315 -0
  29. package/src/core/payload.js +191 -0
  30. package/src/core/preview-bridge.js +216 -0
  31. package/src/core/remote-sync.js +206 -0
  32. package/src/core/scroll-trace.js +177 -0
  33. package/src/core/sent-store.js +148 -0
  34. package/src/core/session.js +60 -0
  35. package/src/core/settings.js +168 -0
  36. package/src/core/side-tab-state.js +30 -0
  37. package/src/core/source-capture.js +530 -0
  38. package/src/core/source-locate.js +220 -0
  39. package/src/core/source-resolve.js +89 -0
  40. package/src/core/styles.js +590 -0
  41. package/src/core/turn-anchor.js +54 -0
  42. package/src/core/util.js +55 -0
  43. package/src/core/uuid.js +23 -0
  44. package/src/core/web-picker.js +426 -0
  45. package/src/entry.js +22 -0
  46. package/src/plugin.js +8706 -0
@@ -0,0 +1,148 @@
1
+ import { safe } from './util.js'
2
+
3
+ /** 已提交记录的条目形状:引文或批注文字至少有一个是字符串。本地台账与远端合并共用这一条判据。 */
4
+ export function isSentItem(value) {
5
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return false
6
+ return typeof value.quote === 'string' || typeof value.note === 'string'
7
+ }
8
+
9
+ /**
10
+ * 已提交批注的本地台账(内存 + localStorage),按会话分表,键是消息锚点键。
11
+ * 记录形如 { savedAt, items, turn?, anchorKey?, batchUuid?, batchSeq? };turn 用来把卡片固定在所属轮次的「已完成」前后,
12
+ * anchorKey 在没有轮次号(旧记录、跨端合并)时用来回到消息行。
13
+ * batchUuid / batchSeq 与条目上的 uuid 只在宿主新接口可用时才有,随记录一起持久化。
14
+ * rewoundAt / rewoundTurn 是「这条批次被会话回退标记过」的意思:回退不删条目,只靠这两个字段让渲染跳过它。
15
+ */
16
+ export function createSentStore({ getStorage, tableFor, prefix, maxEntries, maxBytes, maxItems, onChanged }) {
17
+
18
+ function loadSent(sessionId) {
19
+ const memory = tableFor.get(sessionId)
20
+ if (memory !== undefined) return memory
21
+ const table = new Map()
22
+ const storage = getStorage()
23
+ if (storage !== undefined) {
24
+ const parsed = safe(() => JSON.parse(storage.getItem(`${prefix}${sessionId}`) ?? 'null'))
25
+ if (parsed !== null && typeof parsed === 'object' && parsed.entries !== null && typeof parsed.entries === 'object') {
26
+ for (const [key, value] of Object.entries(parsed.entries)) {
27
+ if (typeof key !== 'string' || key === '') continue
28
+ if (value === null || typeof value !== 'object' || !Array.isArray(value.items)) continue
29
+ const items = value.items.filter(isSentItem).slice(0, maxItems)
30
+ if (items.length === 0) continue
31
+ const turn = Number.isFinite(value.turn) ? Number(value.turn) : undefined
32
+ const anchorKey = typeof value.anchorKey === 'string' && value.anchorKey !== '' ? value.anchorKey : undefined
33
+ const batchUuid =
34
+ typeof value.batchUuid === 'string' && value.batchUuid !== '' ? value.batchUuid : undefined
35
+ const batchSeq = Number.isSafeInteger(value.batchSeq) ? value.batchSeq : undefined
36
+ // 回退标记(rewoundAt/rewoundTurn)必须跟着记录落盘:回退不删任何条目,
37
+ // 「这一批被撤回过了」全靠这两个字段表达,丢了下一次重绘就会把它当成正常批次。
38
+ const rewoundAt = Number.isFinite(value.rewoundAt) ? Number(value.rewoundAt) : undefined
39
+ const rewoundTurn = Number.isFinite(value.rewoundTurn) ? Number(value.rewoundTurn) : undefined
40
+ table.set(key, {
41
+ savedAt: Number(value.savedAt) || 0,
42
+ items,
43
+ ...(turn !== undefined ? { turn } : {}),
44
+ ...(anchorKey !== undefined ? { anchorKey } : {}),
45
+ ...(batchUuid !== undefined ? { batchUuid } : {}),
46
+ ...(batchSeq !== undefined ? { batchSeq } : {}),
47
+ ...(rewoundAt !== undefined ? { rewoundAt } : {}),
48
+ ...(rewoundTurn !== undefined ? { rewoundTurn } : {}),
49
+ // 批次没落库的记录要跟着本地台账活到下一次重试,重启后也不能丢。
50
+ ...(value.unsynced === true ? { unsynced: true } : {}),
51
+ })
52
+ }
53
+ }
54
+ }
55
+ tableFor.set(sessionId, table)
56
+ return table
57
+ }
58
+
59
+ function saveSent(sessionId, table) {
60
+ const storage = getStorage()
61
+ const ordered = [...table.entries()].sort((a, b) => a[1].savedAt - b[1].savedAt)
62
+ // 内存与落盘都保留完整记录;只有 localStorage 受配额限制,写进去时按预算裁剪。
63
+ const kept = ordered.slice(Math.max(0, ordered.length - maxEntries))
64
+ tableFor.set(sessionId, new Map(kept))
65
+ onChanged(sessionId)
66
+ if (storage === undefined) return
67
+ let stored = kept
68
+ if (JSON.stringify(Object.fromEntries(kept)).length > maxBytes) {
69
+ stored = kept.map(([key, value]) => [
70
+ key,
71
+ {
72
+ ...value,
73
+ items: value.items.map((item) => ({
74
+ ...item,
75
+ thumbs: (item.thumbs ?? []).map((thumb) => ({ name: thumb.name, dataUrl: thumb.dataUrl })),
76
+ })),
77
+ },
78
+ ])
79
+ if (JSON.stringify(Object.fromEntries(stored)).length > maxBytes) {
80
+ stored = stored.slice(Math.max(0, stored.length - 40))
81
+ }
82
+ }
83
+ const entries = {}
84
+ for (const [key, value] of stored) entries[key] = value
85
+ try {
86
+ storage.setItem(`${prefix}${sessionId}`, JSON.stringify({ version: 1, entries }))
87
+ } catch {
88
+ /* 配额满:本地缓存放弃写入,落盘通道仍有完整记录 */
89
+ }
90
+ }
91
+
92
+ /** 宿主返回的条目 uuid 与 items 按序对应;只补本地没有的,已有的客户端 uuid 不被覆盖。 */
93
+ function withItemUuids(items, uuids) {
94
+ if (!Array.isArray(uuids) || uuids.length !== items.length) return items
95
+ if (!uuids.every((uuid) => typeof uuid === 'string' && uuid !== '')) return items
96
+ return items.map((item, index) => {
97
+ if (typeof item?.uuid === 'string' && item.uuid !== '') return item
98
+ return { ...item, uuid: uuids[index] }
99
+ })
100
+ }
101
+
102
+ function batchFields(value) {
103
+ return {
104
+ batchUuid: typeof value?.batchUuid === 'string' && value.batchUuid !== '' ? value.batchUuid : undefined,
105
+ batchSeq: Number.isSafeInteger(value?.batchSeq) ? value.batchSeq : undefined,
106
+ }
107
+ }
108
+
109
+ function recordSent(sessionId, anchorKey, items, turn, options) {
110
+ const extra = options ?? {}
111
+ const batch = batchFields(extra)
112
+ const table = loadSent(sessionId)
113
+ table.set(anchorKey, {
114
+ savedAt: Date.now(),
115
+ items: withItemUuids(items, extra.uuids),
116
+ ...(Number.isFinite(turn) ? { turn } : {}),
117
+ // 键本身就是锚点;值里再存一份,跨端合并或旧键(time:/turn-process)时才有地方取。
118
+ anchorKey,
119
+ ...(batch.batchUuid !== undefined ? { batchUuid: batch.batchUuid } : {}),
120
+ ...(batch.batchSeq !== undefined ? { batchSeq: batch.batchSeq } : {}),
121
+ })
122
+ saveSent(sessionId, table)
123
+ }
124
+
125
+ /**
126
+ * 异步补登宿主返回的批次信息:只补字段、不动 savedAt,
127
+ * 免得台账排序和已经在页面上的卡片位置跳一下。记录已被覆盖或删掉时返回 false。
128
+ */
129
+ function updateSentBatch(sessionId, anchorKey, options) {
130
+ const extra = options ?? {}
131
+ const table = loadSent(sessionId)
132
+ const record = table.get(anchorKey)
133
+ if (record === undefined) return false
134
+ const batch = batchFields(extra)
135
+ const items = Array.isArray(extra.uuids) ? withItemUuids(record.items, extra.uuids) : record.items
136
+ if (batch.batchUuid === undefined && batch.batchSeq === undefined && items === record.items) return false
137
+ table.set(anchorKey, {
138
+ ...record,
139
+ items,
140
+ ...(batch.batchUuid !== undefined ? { batchUuid: batch.batchUuid } : {}),
141
+ ...(batch.batchSeq !== undefined ? { batchSeq: batch.batchSeq } : {}),
142
+ })
143
+ saveSent(sessionId, table)
144
+ return true
145
+ }
146
+
147
+ return { loadSent, saveSent, recordSent, updateSentBatch }
148
+ }
@@ -0,0 +1,60 @@
1
+ import { safe } from './util.js'
2
+
3
+ /**
4
+ * 会话定位:优先 sessions.list 里主视图保留的会话(retainedBy.mainView > 0),
5
+ * 其次 uiSession 的 current 绑定,再退回槽位 props 与快照里的 current。
6
+ * latestSessionId 由 React 组件在渲染时回填,所以这里通过 getter 读取,避免拷贝。
7
+ */
8
+ export function createSessionApi(ctx, getLatestSessionId) {
9
+ function currentSessionId() {
10
+ // 主视图只保留一个会话:sessions.list 的 byId 里 retainedBy.mainView > 0 的那条
11
+ // 才是用户正在看的会话。槽位 props 里的 id 会被其他仍挂载的会话覆盖,不能优先。
12
+ const main = safe(() => {
13
+ const byId = ctx.sessions?.list?.getSnapshot?.()?.byId
14
+ if (byId === undefined || byId === null) return undefined
15
+ for (const candidate of Object.values(byId)) {
16
+ if ((candidate?.retainedBy?.mainView ?? 0) > 0 && typeof candidate.id === 'string') return candidate.id
17
+ }
18
+ return undefined
19
+ })
20
+ if (main !== undefined) return main
21
+ const fromUi = safe(() => {
22
+ const binding = ctx.get('uiSession')?.adapter?.current?.getSnapshot?.()
23
+ return typeof binding?.key === 'string' && binding.key !== '' ? binding.key : undefined
24
+ })
25
+ if (fromUi !== undefined) return fromUi
26
+ const latest = getLatestSessionId()
27
+ if (typeof latest === 'string' && latest !== '') return latest
28
+ return safe(() => {
29
+ const snap = ctx.sessions?.list?.getSnapshot?.()
30
+ if (snap === undefined || snap === null) return undefined
31
+ if (typeof snap.current === 'string' && snap.current !== '') return snap.current
32
+ return undefined
33
+ })
34
+ }
35
+
36
+ /** 当前会话的 composer 输入外壳;拿不到就静默降级(批注仍可记录,只是不随消息发送)。 */
37
+ function inputShell() {
38
+ const sessionId = currentSessionId()
39
+ if (sessionId === undefined) return undefined
40
+ return safe(() => {
41
+ const scoped = ctx.sessions.scope(sessionId)
42
+ if (scoped === undefined || scoped === null) return undefined
43
+ return ctx.conversation.input.for(scoped)
44
+ })
45
+ }
46
+
47
+ /**
48
+ * 输入外壳实时持有的草稿附件 id。
49
+ *
50
+ * 刻意不用运行时那张私有草稿表:只发附件(无正文)时,输入态已经交出了附件,
51
+ * 但运行时仍会持有条目直到 admission 结算 —— 拿它当判据就永远等不到「已消费」。
52
+ */
53
+ function liveAttachmentIds(shell) {
54
+ if (Array.isArray(shell?.attachmentIds)) return shell.attachmentIds
55
+ const snapshot = safe(() => shell?.state?.getSnapshot())
56
+ return Array.isArray(snapshot?.attachmentIds) ? snapshot.attachmentIds : undefined
57
+ }
58
+
59
+ return { currentSessionId, inputShell, liveAttachmentIds }
60
+ }
@@ -0,0 +1,168 @@
1
+ import {
2
+ ADOPT_SETTINGS_KEY,
3
+ DEFAULT_ADOPT_TEXT,
4
+ DEFAULT_FALLBACK_TEXT,
5
+ DEFAULT_IMAGE_SETTINGS,
6
+ DEFAULT_INSTRUCTION,
7
+ DEFAULT_REJECT_TEXT,
8
+ FALLBACK_SETTINGS_KEY,
9
+ IMAGE_SETTINGS_KEY,
10
+ INSTRUCTION_SETTINGS_KEY,
11
+ REJECT_SETTINGS_KEY,
12
+ } from './config.js'
13
+ import { safe } from './util.js'
14
+
15
+ /**
16
+ * 本地设置:图片压缩参数与批注指令。
17
+ * 存储通过 getStorage() 延迟获取(浏览器 localStorage 或测试台的内存实现)。
18
+ */
19
+ export function createSettings(getStorage, locale) {
20
+ let imageSettings
21
+ let instructionSettings
22
+ let adoptText
23
+ let rejectText
24
+ let fallbackText
25
+
26
+ /** 图片设置:读坏了回落到默认值。 */
27
+ function loadImageSettings() {
28
+ const settings = { ...DEFAULT_IMAGE_SETTINGS }
29
+ const storage = getStorage()
30
+ if (storage === undefined) return settings
31
+ const parsed = safe(() => JSON.parse(storage.getItem(IMAGE_SETTINGS_KEY) ?? 'null'))
32
+ if (parsed === null || typeof parsed !== 'object') return settings
33
+ settings.compress = parsed.compress !== false
34
+ const quality = Number(parsed.quality)
35
+ if (Number.isFinite(quality)) settings.quality = Math.min(1, Math.max(0.1, quality))
36
+ const minSize = Number(parsed.minSize)
37
+ if (Number.isFinite(minSize)) settings.minSize = Math.min(4096, Math.max(0, Math.round(minSize)))
38
+ const webShot = parsed.webShot
39
+ if (webShot === 'off' || webShot === 'element' || webShot === 'page') settings.webShot = webShot
40
+ return settings
41
+ }
42
+
43
+ function imageSettingsOf() {
44
+ if (imageSettings === undefined) imageSettings = loadImageSettings()
45
+ return imageSettings
46
+ }
47
+
48
+ function persistImageSettings() {
49
+ const storage = getStorage()
50
+ if (storage === undefined) return
51
+ safe(() => storage.setItem(IMAGE_SETTINGS_KEY, JSON.stringify(imageSettingsOf())))
52
+ }
53
+
54
+ /** 批注指令:读坏了或读成空串时回落到默认文案。 */
55
+ function loadInstructionSettings() {
56
+ const storage = getStorage()
57
+ if (storage === undefined) return DEFAULT_INSTRUCTION
58
+ const parsed = safe(() => JSON.parse(storage.getItem(INSTRUCTION_SETTINGS_KEY) ?? 'null'))
59
+ if (parsed === null || typeof parsed !== 'object') return DEFAULT_INSTRUCTION
60
+ const text = typeof parsed.text === 'string' ? parsed.text : ''
61
+ return text.trim() === '' ? DEFAULT_INSTRUCTION : text
62
+ }
63
+
64
+ function instructionOf() {
65
+ if (instructionSettings === undefined) instructionSettings = loadInstructionSettings()
66
+ // 没改过就用当前语言对应的默认文案;改过就保留用户自己的措辞。
67
+ return locale.isDefaultInstruction(instructionSettings) ? locale.defaultInstruction() : instructionSettings
68
+ }
69
+
70
+ function persistInstructionSettings(text) {
71
+ instructionSettings = text
72
+ const storage = getStorage()
73
+ if (storage === undefined) return
74
+ safe(() => storage.setItem(INSTRUCTION_SETTINGS_KEY, JSON.stringify({ text })))
75
+ }
76
+
77
+ /** 「批注采纳」的预填文本:空串回落到默认文案。 */
78
+ function loadAdoptText() {
79
+ const storage = getStorage()
80
+ if (storage === undefined) return DEFAULT_ADOPT_TEXT
81
+ const parsed = safe(() => JSON.parse(storage.getItem(ADOPT_SETTINGS_KEY) ?? 'null'))
82
+ if (parsed === null || typeof parsed !== 'object') return DEFAULT_ADOPT_TEXT
83
+ const text = typeof parsed.text === 'string' ? parsed.text.trim() : ''
84
+ // 旧默认值只在没有版本标记时迁移一次;迁移后写入 version,用户之后特意改回旧措辞不会被再次覆盖。
85
+ if (parsed.version === undefined && (text === '意见采纳。' || text === '意见采纳')) return DEFAULT_ADOPT_TEXT
86
+ return text === '' ? DEFAULT_ADOPT_TEXT : text
87
+ }
88
+
89
+ function adoptTextOf() {
90
+ if (adoptText === undefined) adoptText = loadAdoptText()
91
+ return adoptText
92
+ }
93
+
94
+ function persistAdoptText(text) {
95
+ adoptText = typeof text === 'string' && text.trim() !== '' ? text : DEFAULT_ADOPT_TEXT
96
+ const storage = getStorage()
97
+ if (storage === undefined) return
98
+ safe(() => storage.setItem(ADOPT_SETTINGS_KEY, JSON.stringify({ text: adoptText, version: 1 })))
99
+ }
100
+
101
+ /** 「批注否决」的预填文本:空串回落到默认文案。 */
102
+ function loadRejectText() {
103
+ const storage = getStorage()
104
+ if (storage === undefined) return DEFAULT_REJECT_TEXT
105
+ const parsed = safe(() => JSON.parse(storage.getItem(REJECT_SETTINGS_KEY) ?? 'null'))
106
+ if (parsed === null || typeof parsed !== 'object') return DEFAULT_REJECT_TEXT
107
+ const text = typeof parsed.text === 'string' ? parsed.text.trim() : ''
108
+ return text === '' ? DEFAULT_REJECT_TEXT : text
109
+ }
110
+
111
+ function rejectTextOf() {
112
+ if (rejectText === undefined) rejectText = loadRejectText()
113
+ return rejectText
114
+ }
115
+
116
+ function persistRejectText(text) {
117
+ rejectText = typeof text === 'string' && text.trim() !== '' ? text : DEFAULT_REJECT_TEXT
118
+ const storage = getStorage()
119
+ if (storage === undefined) return
120
+ safe(() => storage.setItem(REJECT_SETTINGS_KEY, JSON.stringify({ text: rejectText, version: 1 })))
121
+ }
122
+
123
+ /** 空提示词填充:空串等同默认文案;旧默认「请按批注内容处理。」静默迁移。 */
124
+ function loadFallbackText() {
125
+ const storage = getStorage()
126
+ if (storage === undefined) return DEFAULT_FALLBACK_TEXT
127
+ const parsed = safe(() => JSON.parse(storage.getItem(FALLBACK_SETTINGS_KEY) ?? 'null'))
128
+ if (parsed === null || typeof parsed !== 'object') return DEFAULT_FALLBACK_TEXT
129
+ const text = typeof parsed.text === 'string' ? parsed.text : ''
130
+ if (text.trim() === '') return DEFAULT_FALLBACK_TEXT
131
+ // 旧默认文案只在没有版本标记时迁移一次。
132
+ if (parsed.version === undefined) {
133
+ if (text === '请按批注内容处理。') return DEFAULT_FALLBACK_TEXT
134
+ if (text.startsWith('此轮对话内容均在批注中给出') && text.includes('请逐一受理')) return DEFAULT_FALLBACK_TEXT
135
+ }
136
+ return text
137
+ }
138
+
139
+ function fallbackTextOf() {
140
+ if (fallbackText === undefined) fallbackText = loadFallbackText()
141
+ return fallbackText
142
+ }
143
+
144
+ function persistFallbackText(text) {
145
+ fallbackText = typeof text === 'string' ? text : ''
146
+ const storage = getStorage()
147
+ if (storage === undefined) return
148
+ safe(() => storage.setItem(FALLBACK_SETTINGS_KEY, JSON.stringify({ text: fallbackText, version: 1 })))
149
+ }
150
+
151
+ return {
152
+ loadImageSettings,
153
+ imageSettingsOf,
154
+ persistImageSettings,
155
+ loadInstructionSettings,
156
+ instructionOf,
157
+ persistInstructionSettings,
158
+ loadAdoptText,
159
+ adoptTextOf,
160
+ persistAdoptText,
161
+ loadRejectText,
162
+ rejectTextOf,
163
+ persistRejectText,
164
+ loadFallbackText,
165
+ fallbackTextOf,
166
+ persistFallbackText,
167
+ }
168
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * 「批注总览」侧栏按钮的动作决策。
3
+ *
4
+ * 只看三个事实:页签是否还开着、右侧栏是否展开、页签是不是当前激活的那个。
5
+ * 决策与 DOM、侧栏服务调用分开,四条分支才好单测。
6
+ */
7
+
8
+ /** 点击按钮的动作。 */
9
+ export const SIDE_TAB_ACTION = {
10
+ /** 页签不存在:打开它。 */
11
+ open: 'open',
12
+ /** 页签开着、侧栏展开、且处于激活:关闭它。 */
13
+ close: 'close',
14
+ /** 页签开着、侧栏展开、但用户切到了别的页签:聚焦回它。 */
15
+ focus: 'focus',
16
+ /** 页签开着但侧栏收成窄条:先展开再聚焦。 */
17
+ reveal: 'reveal',
18
+ }
19
+
20
+ /**
21
+ * 由三个事实推出按钮该做什么。
22
+ * @param {{ hasTab?: boolean, expanded?: boolean, active?: boolean }} state 页签记录、侧栏展开、页签激活。
23
+ * @returns {'open' | 'close' | 'focus' | 'reveal'} 动作。
24
+ */
25
+ export function sideTabAction({ hasTab, expanded, active } = {}) {
26
+ if (hasTab !== true) return SIDE_TAB_ACTION.open
27
+ if (expanded !== true) return SIDE_TAB_ACTION.reveal
28
+ if (active !== true) return SIDE_TAB_ACTION.focus
29
+ return SIDE_TAB_ACTION.close
30
+ }