@vibedev-si/dsh-island 0.0.0-stage → 0.2.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/CHANGELOG.md +63 -0
- package/LICENSE +27 -0
- package/THIRD-PARTY-NOTICES.txt +8458 -0
- package/approvals.js +125 -0
- package/attention.js +123 -0
- package/bin/win32-x64/vibedev-island.exe +0 -0
- package/changelog.js +80 -0
- package/client.js +2809 -0
- package/contributions.js +261 -0
- package/contributor-display.js +68 -0
- package/contributor-entry.js +49 -0
- package/cordis.patch.yml +6 -0
- package/diagnostics.js +92 -0
- package/events.js +89 -0
- package/executable.js +37 -0
- package/icon.svg +21 -0
- package/index.js +1291 -0
- package/island-items.js +191 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/log.js +53 -0
- package/openers.js +77 -0
- package/outbox.js +78 -0
- package/package.json +80 -4
- package/recent.js +194 -0
- package/requests.js +84 -0
- package/routes.js +201 -0
- package/selection.js +78 -0
- package/sessions.js +258 -0
- package/settings.js +344 -0
- package/third-party.json +1666 -0
- package/translate.js +53 -0
- package/wechat.js +53 -0
- package/README.md +0 -3
package/contributions.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 别的 VibeDev 插件放到岛上的后台任务(`island` 服务的 `contributor()` 背后的那本账)。
|
|
3
|
+
*
|
|
4
|
+
* 规划见 docs/island-plan.md 第 3 节。这一块是**后台任务**:有标题、有一句说明、有真实进度
|
|
5
|
+
* (或者没有,那就转圈)、会结束。同一个入口上的「最近生成」在 recent.js,通知上的「查看」在 openers.js;
|
|
6
|
+
* 状态项、页、通知也可以走 `ctx.island` 的直接方法(island-items.js)。
|
|
7
|
+
*
|
|
8
|
+
* 几条不变的规则(规划 3.7):
|
|
9
|
+
* - 只收文字和数字。标题最多 40 个字、说明最多 120 个字,超出的截断;进度夹到 0–1,
|
|
10
|
+
* 不是数字就当「不知道」——岛上画成转圈,这里不替插件编百分比。
|
|
11
|
+
* - 每个插件最多 10 条任务。
|
|
12
|
+
* - 谁能放由调用方给的 `allowed(插件名)` 决定(设置里的总开关和按插件的开关)。不让放的插件,
|
|
13
|
+
* 任务照样记着,只是不上岛;放开之后补发现在的样子(`refresh()`)。
|
|
14
|
+
* - 岛屿没在运行时先记着;它起来(或重启)之后把还在进行的任务补发过去。
|
|
15
|
+
* - 插件不再需要时调用 `clear()`,它的任务马上从岛上消失。
|
|
16
|
+
*
|
|
17
|
+
* 这个模块不碰宿主也不碰进程:发消息、定时、时钟都由调用方给,测试里换成假的。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** 每个插件最多同时有多少条任务。 */
|
|
21
|
+
export const TASK_LIMIT = 10
|
|
22
|
+
/** 标题、说明的长度上限(按字数,不按字节)。 */
|
|
23
|
+
export const TITLE_MAX = 40
|
|
24
|
+
export const DETAIL_MAX = 120
|
|
25
|
+
/** 同一条任务两次更新之间至少隔这么久;中间的更新只留最后一份。 */
|
|
26
|
+
export const MIN_INTERVAL_MS = 250
|
|
27
|
+
/** 结束的任务再留这么久,让人看一眼结果,然后撤下。 */
|
|
28
|
+
export const FINISHED_LINGER_MS = 6000
|
|
29
|
+
|
|
30
|
+
const ID_PATTERN = /^[\w.-]{1,64}$/
|
|
31
|
+
|
|
32
|
+
/** 什么都不做:不收的插件、放不下的任务拿到的就是它。 */
|
|
33
|
+
const nothing = () => undefined
|
|
34
|
+
const idleJob = Object.freeze({ update: nothing, finish: nothing, remove: nothing })
|
|
35
|
+
const idleContributor = Object.freeze({ task: () => idleJob, clear: nothing })
|
|
36
|
+
|
|
37
|
+
const isRecord = (value) => value !== null && typeof value === 'object'
|
|
38
|
+
|
|
39
|
+
/** 截到 `max` 个字;截过的末尾是省略号。不是字符串或只有空白时是 `undefined`。 */
|
|
40
|
+
function cut(value, max) {
|
|
41
|
+
if (typeof value !== 'string') return undefined
|
|
42
|
+
const text = value.replace(/\s+/g, ' ').trim()
|
|
43
|
+
if (text === '') return undefined
|
|
44
|
+
const letters = Array.from(text)
|
|
45
|
+
return letters.length <= max ? text : `${letters.slice(0, max - 1).join('')}…`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 0–1 之间的真实进度;给的不是有限的数字就是 `null`(不知道)。 */
|
|
49
|
+
function progressOf(value) {
|
|
50
|
+
return typeof value === 'number' && Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {object} options
|
|
55
|
+
* @param {(method: string, params: object) => boolean} options.send 把一条消息交给岛屿;岛屿没在运行时返回 false。
|
|
56
|
+
* @param {(callback: () => void, ms: number) => unknown} options.later 定时;返回值交回给 `cancel`。
|
|
57
|
+
* @param {(timer: unknown) => void} options.cancel
|
|
58
|
+
* @param {() => number} [options.now]
|
|
59
|
+
* @param {(line: string) => void} [options.log]
|
|
60
|
+
* @param {(owner: string) => boolean} [options.allowed] 这个插件现在能不能上岛;不给就都能。
|
|
61
|
+
* @param {(owner: string) => { title: string, icon: string }} [options.displayOf] 插件在小岛上的名字和图标;
|
|
62
|
+
* 不给就用插件名和通用图标。
|
|
63
|
+
*/
|
|
64
|
+
export function createContributions(options) {
|
|
65
|
+
const send = options.send
|
|
66
|
+
const later = options.later
|
|
67
|
+
const cancel = options.cancel
|
|
68
|
+
const now = options.now ?? Date.now
|
|
69
|
+
const log = options.log ?? nothing
|
|
70
|
+
const allowed = options.allowed ?? (() => true)
|
|
71
|
+
const displayOf = options.displayOf ?? ((owner) => ({ title: owner, icon: 'box' }))
|
|
72
|
+
/** 插件名 → (任务 id → 任务)。 */
|
|
73
|
+
const owners = new Map()
|
|
74
|
+
/** 已经说过一次「不收」的插件名,免得日志被刷满。 */
|
|
75
|
+
const refused = new Set()
|
|
76
|
+
|
|
77
|
+
const everyTask = () => Array.from(owners.values()).flatMap((tasks) => Array.from(tasks.values()))
|
|
78
|
+
|
|
79
|
+
const stopTimer = (task, key) => {
|
|
80
|
+
if (task[key] === undefined) return
|
|
81
|
+
cancel(task[key])
|
|
82
|
+
task[key] = undefined
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 岛上不再有这条任务的样子(被撤下、岛屿退出或重启):下一次要整份重发。 */
|
|
86
|
+
const forget = (task) => {
|
|
87
|
+
stopTimer(task, 'pending')
|
|
88
|
+
task.shown = undefined
|
|
89
|
+
task.sentAt = undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** 把这条任务现在的样子送到岛上;和上一次送的一样就不送,太密就等一会儿只送最后一份。 */
|
|
93
|
+
const flush = (task) => {
|
|
94
|
+
if (task.gone || !allowed(task.owner)) return
|
|
95
|
+
const display = displayOf(task.owner)
|
|
96
|
+
const payload = {
|
|
97
|
+
id: task.wireId,
|
|
98
|
+
kind: 'task',
|
|
99
|
+
owner: task.owner,
|
|
100
|
+
source: display.title,
|
|
101
|
+
icon: display.icon,
|
|
102
|
+
title: task.title,
|
|
103
|
+
detail: task.detail ?? null,
|
|
104
|
+
progress: task.progress,
|
|
105
|
+
// 结束了的任务说明是走完了还是失败了(小岛据此画成「已完成」或「失败」);还在进行的不带。
|
|
106
|
+
...(task.state === undefined ? {} : { state: task.state }),
|
|
107
|
+
}
|
|
108
|
+
const serialized = JSON.stringify(payload)
|
|
109
|
+
if (serialized === task.shown) return
|
|
110
|
+
const wait = task.sentAt === undefined ? 0 : MIN_INTERVAL_MS - (now() - task.sentAt)
|
|
111
|
+
if (wait > 0) {
|
|
112
|
+
if (task.pending === undefined) {
|
|
113
|
+
task.pending = later(() => {
|
|
114
|
+
task.pending = undefined
|
|
115
|
+
flush(task)
|
|
116
|
+
}, wait)
|
|
117
|
+
}
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
if (!send('contrib/upsert', payload)) return
|
|
121
|
+
task.shown = serialized
|
|
122
|
+
task.sentAt = now()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const drop = (owner, id) => {
|
|
126
|
+
const tasks = owners.get(owner)
|
|
127
|
+
const task = tasks?.get(id)
|
|
128
|
+
if (task === undefined) return
|
|
129
|
+
task.gone = true
|
|
130
|
+
stopTimer(task, 'pending')
|
|
131
|
+
stopTimer(task, 'linger')
|
|
132
|
+
if (task.shown !== undefined) send('contrib/remove', { id: task.wireId })
|
|
133
|
+
tasks.delete(id)
|
|
134
|
+
if (tasks.size === 0) owners.delete(owner)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 把补丁里认识的几项写到任务上。标题不能改成空的。 */
|
|
138
|
+
const applyPatch = (task, patch) => {
|
|
139
|
+
if (!isRecord(patch)) return
|
|
140
|
+
if ('title' in patch) task.title = cut(patch.title, TITLE_MAX) ?? task.title
|
|
141
|
+
if ('subtitle' in patch) task.detail = cut(patch.subtitle, DETAIL_MAX)
|
|
142
|
+
if ('progress' in patch) task.progress = progressOf(patch.progress)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const jobFor = (owner, id, task) => ({
|
|
146
|
+
/** 更新标题、说明或进度;没提到的保持原样。结束之后的更新不算数。 */
|
|
147
|
+
update(patch) {
|
|
148
|
+
if (task.gone || task.finished) return
|
|
149
|
+
applyPatch(task, patch)
|
|
150
|
+
flush(task)
|
|
151
|
+
},
|
|
152
|
+
/**
|
|
153
|
+
* 任务结束:把最后的样子留几秒再撤下。没给进度时,走完的画满;`state: 'failed'`(失败,或者结果不明)
|
|
154
|
+
* 的停在原来的进度上,原来不知道进度的画成空的——结束了的任务不能还在转圈。
|
|
155
|
+
*/
|
|
156
|
+
finish(patch) {
|
|
157
|
+
if (task.gone || task.finished) return
|
|
158
|
+
task.finished = true
|
|
159
|
+
applyPatch(task, patch)
|
|
160
|
+
const failed = isRecord(patch) && patch.state === 'failed'
|
|
161
|
+
const stated = isRecord(patch) && 'progress' in patch
|
|
162
|
+
task.state = failed ? 'failed' : 'done'
|
|
163
|
+
// 结束了的任务不再转圈:走完的画满;没走完的停在原来的进度上,本来就不知道进度的画成空的。
|
|
164
|
+
if (!stated) task.progress = failed ? (task.progress ?? 0) : 1
|
|
165
|
+
flush(task)
|
|
166
|
+
task.linger = later(() => {
|
|
167
|
+
task.linger = undefined
|
|
168
|
+
drop(owner, id)
|
|
169
|
+
}, FINISHED_LINGER_MS)
|
|
170
|
+
},
|
|
171
|
+
/** 立刻撤下。 */
|
|
172
|
+
remove() {
|
|
173
|
+
drop(owner, id)
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
/**
|
|
179
|
+
* 一个插件往岛上放任务的入口。
|
|
180
|
+
* @param {string} owner 插件名(例如 `dsh-film`)。
|
|
181
|
+
* @returns {{ task(id: string, spec: object): { update(patch: object): void, finish(patch?: object): void, remove(): void }, clear(): void }}
|
|
182
|
+
*/
|
|
183
|
+
contributor(owner) {
|
|
184
|
+
if (typeof owner !== 'string' || owner.trim() === '') {
|
|
185
|
+
const name = String(owner)
|
|
186
|
+
if (!refused.has(name)) {
|
|
187
|
+
refused.add(name)
|
|
188
|
+
log(`contributions: a contributor without a usable name (${name}) was refused`)
|
|
189
|
+
}
|
|
190
|
+
return idleContributor
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
/**
|
|
194
|
+
* 放一条任务;同一个 id 再放一次是更新它。
|
|
195
|
+
* @param {string} id 这个插件自己的任务 id(字母、数字、`.`、`_`、`-`,最多 64 个)。
|
|
196
|
+
* @param {{ title: string, subtitle?: string, progress?: number | null }} spec
|
|
197
|
+
*/
|
|
198
|
+
task(id, spec) {
|
|
199
|
+
const title = cut(spec?.title, TITLE_MAX)
|
|
200
|
+
if (typeof id !== 'string' || !ID_PATTERN.test(id) || title === undefined) {
|
|
201
|
+
log(`contributions: ${owner} sent a task without a usable id or title`)
|
|
202
|
+
return idleJob
|
|
203
|
+
}
|
|
204
|
+
const tasks = owners.get(owner) ?? new Map()
|
|
205
|
+
let task = tasks.get(id)
|
|
206
|
+
if (task === undefined) {
|
|
207
|
+
if (tasks.size >= TASK_LIMIT) {
|
|
208
|
+
log(`contributions: ${owner} already has ${TASK_LIMIT} tasks; ${id} was not added`)
|
|
209
|
+
return idleJob
|
|
210
|
+
}
|
|
211
|
+
task = { owner, wireId: `plugin-${owner}-${id}`, title, detail: undefined, progress: null }
|
|
212
|
+
tasks.set(id, task)
|
|
213
|
+
owners.set(owner, tasks)
|
|
214
|
+
log(`contributions: ${owner} added task ${id}`)
|
|
215
|
+
} else {
|
|
216
|
+
// 结束后还留着的那几秒里又开始了:按新任务算。
|
|
217
|
+
stopTimer(task, 'linger')
|
|
218
|
+
task.finished = false
|
|
219
|
+
task.state = undefined
|
|
220
|
+
}
|
|
221
|
+
applyPatch(task, { subtitle: undefined, progress: null, ...spec })
|
|
222
|
+
flush(task)
|
|
223
|
+
return jobFor(owner, id, task)
|
|
224
|
+
},
|
|
225
|
+
/** 这个插件的任务全部撤下(它被卸载、停用,或者不再需要岛屿时)。 */
|
|
226
|
+
clear() {
|
|
227
|
+
for (const id of Array.from(owners.get(owner)?.keys() ?? Array.of())) drop(owner, id)
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 设置里的开关变了:不再让上岛的插件,已经显示的撤下(任务本身照常记着);重新放开的,补发现在的样子。
|
|
234
|
+
* 没变的什么都不发,调几次都一样。
|
|
235
|
+
*/
|
|
236
|
+
refresh() {
|
|
237
|
+
for (const task of everyTask()) {
|
|
238
|
+
if (allowed(task.owner)) {
|
|
239
|
+
flush(task)
|
|
240
|
+
continue
|
|
241
|
+
}
|
|
242
|
+
if (task.shown !== undefined) send('contrib/remove', { id: task.wireId })
|
|
243
|
+
forget(task)
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
/** 岛屿刚起来(或重启):它那边什么都没有,把还在的任务全部补发。它退出时这里不用做什么:没在运行的岛屿什么都收不到。 */
|
|
248
|
+
islandStarted() {
|
|
249
|
+
for (const task of everyTask()) {
|
|
250
|
+
forget(task)
|
|
251
|
+
flush(task)
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
|
|
255
|
+
/** 现在记着多少条任务。 */
|
|
256
|
+
count: () => everyTask().length,
|
|
257
|
+
|
|
258
|
+
/** 放过任务的插件和各自现在记着几条(设置页上列出来,可以单独关掉)。 */
|
|
259
|
+
summary: () => Array.from(owners, ([name, tasks]) => ({ name, items: tasks.size })),
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 小岛上怎么称呼一个放东西的插件:名字和图标。
|
|
3
|
+
*
|
|
4
|
+
* 插件拿入口时可以自己报:`ctx.island.contributor('dsh-film', { title: '影视工作台', icon: 'film' })`。
|
|
5
|
+
* 没报的时候,认得的 VibeDev 插件用这里的默认值(老版本的影视工作台还不会报),其余的用包名和一个通用图标。
|
|
6
|
+
* 图标只能从下面几种里挑:小岛不画插件给的图,也不放插件的 HTML。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** 小岛认得的图标。 */
|
|
10
|
+
export const ICONS = Object.freeze(['film', 'image', 'music', 'sparkles', 'box'])
|
|
11
|
+
/** 名字最多这么多个字。 */
|
|
12
|
+
export const DISPLAY_TITLE_MAX = 20
|
|
13
|
+
|
|
14
|
+
/** VibeDev 自己的插件:还没报名字的版本也显示得像样。 */
|
|
15
|
+
export const KNOWN_CONTRIBUTORS = Object.freeze({
|
|
16
|
+
'dsh-film': Object.freeze({ title: '影视工作台', icon: 'film' }),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
/** 去掉多余空白,截到 `max` 个字(截过的末尾是省略号);不是字符串或只有空白时是 undefined。 */
|
|
20
|
+
function cut(value, max) {
|
|
21
|
+
if (typeof value !== 'string') return undefined
|
|
22
|
+
const text = value.replace(/\s+/g, ' ').trim()
|
|
23
|
+
if (text === '') return undefined
|
|
24
|
+
const letters = Array.from(text)
|
|
25
|
+
return letters.length <= max ? text : `${letters.slice(0, max - 1).join('')}…`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 插件报上来的名字和图标里能用的部分。
|
|
30
|
+
* @param {unknown} input `{ title?, icon? }`
|
|
31
|
+
* @returns {{ title?: string, icon?: string }}
|
|
32
|
+
*/
|
|
33
|
+
export function displayInput(input) {
|
|
34
|
+
if (input === null || typeof input !== 'object') return {}
|
|
35
|
+
const title = cut(input.title, DISPLAY_TITLE_MAX)
|
|
36
|
+
const icon = ICONS.includes(input.icon) ? input.icon : undefined
|
|
37
|
+
return { ...(title === undefined ? {} : { title }), ...(icon === undefined ? {} : { icon }) }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 每个插件在小岛上的名字和图标。 */
|
|
41
|
+
export function createDisplays() {
|
|
42
|
+
/** 插件名 → 它自己报的。 */
|
|
43
|
+
const declared = new Map()
|
|
44
|
+
return {
|
|
45
|
+
/**
|
|
46
|
+
* 插件报上名字和图标。返回和之前比有没有变(变了的话,已经在岛上的东西要重发一遍)。
|
|
47
|
+
* @param {string} owner
|
|
48
|
+
* @param {unknown} input
|
|
49
|
+
*/
|
|
50
|
+
declare(owner, input) {
|
|
51
|
+
const next = displayInput(input)
|
|
52
|
+
if (Object.keys(next).length === 0) return false
|
|
53
|
+
const before = JSON.stringify(declared.get(owner) ?? {})
|
|
54
|
+
declared.set(owner, next)
|
|
55
|
+
return JSON.stringify(next) !== before
|
|
56
|
+
},
|
|
57
|
+
/**
|
|
58
|
+
* 这个插件在小岛上的名字和图标。
|
|
59
|
+
* @param {string} owner
|
|
60
|
+
* @returns {{ title: string, icon: string }}
|
|
61
|
+
*/
|
|
62
|
+
of(owner) {
|
|
63
|
+
const own = declared.get(owner) ?? {}
|
|
64
|
+
const known = KNOWN_CONTRIBUTORS[owner] ?? {}
|
|
65
|
+
return { title: own.title ?? known.title ?? owner, icon: own.icon ?? known.icon ?? 'box' }
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ctx.island.contributor(名字)` 交给插件的那个对象:后台任务、最近生成、通知,合在一个入口上。
|
|
3
|
+
*
|
|
4
|
+
* 各自的规则在各自的模块里:任务在 contributions.js,最近生成在 recent.js,通知在 island-items.js,
|
|
5
|
+
* 通知上的「查看」在 openers.js。这里只是把它们按插件名接起来。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const refuse = () => false
|
|
9
|
+
const idleJob = Object.freeze({ update: () => undefined, finish: () => undefined, remove: () => undefined })
|
|
10
|
+
|
|
11
|
+
/** 名字不能用时给的入口:什么都不做。 */
|
|
12
|
+
export const IDLE_ENTRY = Object.freeze({
|
|
13
|
+
task: () => idleJob,
|
|
14
|
+
recent: refuse,
|
|
15
|
+
forget: refuse,
|
|
16
|
+
notify: refuse,
|
|
17
|
+
clear: () => undefined,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {string} name 插件名
|
|
22
|
+
* @param {object} books
|
|
23
|
+
* @param {{ contributor(name: string): { task: Function, clear(): void } }} books.tasks contributions.js
|
|
24
|
+
* @param {{ put: Function, forget: Function, clear(name: string): void }} books.recent recent.js
|
|
25
|
+
* @param {{ notify(name: string, input: unknown, openId?: string): boolean }} books.items island-items.js
|
|
26
|
+
* @param {{ register(name: string, open: () => void): string, clear(name: string): void }} books.openers openers.js
|
|
27
|
+
*/
|
|
28
|
+
export function contributorEntry(name, { tasks, recent, items, openers }) {
|
|
29
|
+
if (typeof name !== 'string' || name.trim() === '') return IDLE_ENTRY
|
|
30
|
+
const own = tasks.contributor(name)
|
|
31
|
+
return Object.freeze({
|
|
32
|
+
/** 一条后台任务:`task(id, { title, subtitle?, progress? })`,返回 `update` / `finish` / `remove`。 */
|
|
33
|
+
task: (id, spec) => own.task(id, spec),
|
|
34
|
+
/** 一条最近生成:`recent(id, { title, subtitle?, media?, thumbnail?, at?, open? })`。返回收下了没有。 */
|
|
35
|
+
recent: (id, spec) => recent.put(name, id, spec),
|
|
36
|
+
/** 撤下一条最近生成。 */
|
|
37
|
+
forget: (id) => recent.forget(name, id),
|
|
38
|
+
/** 一条会自己消失的通知:`notify({ title, body?, open? })`;有 `open` 时带「查看」。一分钟最多 3 条。 */
|
|
39
|
+
notify(spec) {
|
|
40
|
+
const open = typeof spec?.open === 'function' ? spec.open : undefined
|
|
41
|
+
return items.notify(name, spec, open === undefined ? undefined : openers.register(name, open))
|
|
42
|
+
},
|
|
43
|
+
/** 这个插件的任务和最近生成全部撤下。 */
|
|
44
|
+
clear() {
|
|
45
|
+
own.clear()
|
|
46
|
+
recent.clear(name)
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
}
|
package/cordis.patch.yml
ADDED
package/diagnostics.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「导出诊断信息」:把版本、WebView2、各项状态、设置和最近的日志拼成一份文本。
|
|
3
|
+
*
|
|
4
|
+
* 纯函数;收集数据、写文件、打开资源管理器在 index.js。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** 日志最多带这么多行。 */
|
|
8
|
+
export const LOG_LINES = 200
|
|
9
|
+
|
|
10
|
+
/** QQ 邮箱的状态 → 一句话(设置页「概览」和诊断信息共用)。 */
|
|
11
|
+
export function mailSummary(mail) {
|
|
12
|
+
if (mail === undefined || mail === null) return '还没读到'
|
|
13
|
+
if (mail.configured !== true) return '没设置'
|
|
14
|
+
if (mail.enabled !== true) return '已关闭'
|
|
15
|
+
if (typeof mail.error === 'string' && mail.error !== '') return `出错:${mail.error}`
|
|
16
|
+
if (mail.syncing === true) return '正在同步'
|
|
17
|
+
const states = {
|
|
18
|
+
connected: '已连接',
|
|
19
|
+
connecting: '正在连接',
|
|
20
|
+
disconnected: '没连上',
|
|
21
|
+
paused: '暂停了',
|
|
22
|
+
authRequired: '需要重新授权',
|
|
23
|
+
error: '出错',
|
|
24
|
+
}
|
|
25
|
+
const state = states[mail.status] ?? String(mail.status ?? '未知')
|
|
26
|
+
return mail.status === 'connected' && typeof mail.unreadCount === 'number'
|
|
27
|
+
? `${state} · 未读 ${mail.unreadCount}`
|
|
28
|
+
: state
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const line = (label, value) => `${label}:${value === undefined || value === null || value === '' ? '—' : value}`
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param input.status 宿主 status() 的结果
|
|
35
|
+
* @param input.settings 当前设置
|
|
36
|
+
* @param input.logText 日志文件的内容(取最后 200 行)
|
|
37
|
+
* @param input.system `{ os, arch, node }`
|
|
38
|
+
* @param input.now 生成时刻
|
|
39
|
+
*/
|
|
40
|
+
export function diagnosticsReport({ status, settings, logText = '', system = {}, now = new Date() }) {
|
|
41
|
+
const lines = String(logText)
|
|
42
|
+
.split(/\r?\n/)
|
|
43
|
+
.filter((text) => text !== '')
|
|
44
|
+
const tail = lines.slice(-LOG_LINES)
|
|
45
|
+
const sections = [
|
|
46
|
+
'VibeDev 岛屿 · 诊断信息',
|
|
47
|
+
line('生成时间', now.toISOString()),
|
|
48
|
+
'',
|
|
49
|
+
'## 版本',
|
|
50
|
+
line('小岛', status?.version),
|
|
51
|
+
line('插件', status?.pluginVersion),
|
|
52
|
+
line('协议', status?.protocol),
|
|
53
|
+
line('WebView2', status?.webview2),
|
|
54
|
+
line('系统', [system.os, system.arch].filter(Boolean).join(' ')),
|
|
55
|
+
line('Node', system.node),
|
|
56
|
+
'',
|
|
57
|
+
'## 运行',
|
|
58
|
+
line('在运行', status?.running === true ? '是' : '否'),
|
|
59
|
+
line('启动于', status?.startedAt === undefined ? undefined : new Date(status.startedAt).toISOString()),
|
|
60
|
+
line('重启次数', status?.restarts),
|
|
61
|
+
line('上次退出', status?.lastExit === undefined ? undefined : JSON.stringify(status.lastExit)),
|
|
62
|
+
line('最近的错误', status?.lastError),
|
|
63
|
+
'',
|
|
64
|
+
'## 各功能',
|
|
65
|
+
line('QQ 通知', status?.qq?.status),
|
|
66
|
+
line('微信提醒', status?.wechat?.status),
|
|
67
|
+
line('QQ 邮箱', mailSummary(status?.mail)),
|
|
68
|
+
line('翻译', status?.translation?.available === true ? 'VibeDev 翻译可用' : '用本地密钥'),
|
|
69
|
+
line('快捷键', status?.shortcuts === undefined ? undefined : JSON.stringify(status.shortcuts)),
|
|
70
|
+
line(
|
|
71
|
+
'其它插件',
|
|
72
|
+
Array.isArray(status?.contributors) && status.contributors.length > 0
|
|
73
|
+
? status.contributors.map((item) => `${item.name}${item.blocked ? '(已关掉)' : ''}`).join('、')
|
|
74
|
+
: '没有',
|
|
75
|
+
),
|
|
76
|
+
'',
|
|
77
|
+
'## 设置(不含密钥:密钥在 Windows 凭据里)',
|
|
78
|
+
JSON.stringify(settings ?? null, null, 2),
|
|
79
|
+
'',
|
|
80
|
+
`## 日志(最后 ${tail.length} 行;可能有会话标题,发给别人前看一眼)`,
|
|
81
|
+
...tail,
|
|
82
|
+
'',
|
|
83
|
+
]
|
|
84
|
+
return sections.join('\n')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 诊断文件的名字:带上生成时刻,不会覆盖上一份。 */
|
|
88
|
+
export function diagnosticsFileName(now = new Date()) {
|
|
89
|
+
const pad = (value) => String(value).padStart(2, '0')
|
|
90
|
+
const stamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`
|
|
91
|
+
return `vibedev-island-diagnostics-${stamp}.txt`
|
|
92
|
+
}
|
package/events.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主 → 页面的动作通道。
|
|
3
|
+
*
|
|
4
|
+
* 岛屿在浏览器外面,「打开这条会话」要从插件的宿主一半走到页面一半,页面那边才有
|
|
5
|
+
* `uiWorkspace.openSession()`。宿主没有现成的推送通道给插件用(`session.control` 只推
|
|
6
|
+
* 投影数据),所以页面保持一个长轮询,宿主有事就立刻回,没事等到超时再回。
|
|
7
|
+
*
|
|
8
|
+
* 事件刻意是短命的:晚连上的页面不应该重放一条旧的「打开那条会话」。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** 事件超过这个年纪就不再投递。 */
|
|
12
|
+
export const PAGE_EVENT_MAX_AGE_MS = 15_000
|
|
13
|
+
/** 一次长轮询最多挂多久;到点返回空列表,页面接着再问。 */
|
|
14
|
+
export const PAGE_EVENT_WAIT_MS = 20_000
|
|
15
|
+
/** 只留最近这么多条。 */
|
|
16
|
+
const KEEP = 20
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param options.now 取当前时间(毫秒)
|
|
20
|
+
* @param options.waitMs 一次等待的上限
|
|
21
|
+
* @param options.maxAgeMs 事件的保质期
|
|
22
|
+
*/
|
|
23
|
+
export function createPageEvents(options) {
|
|
24
|
+
const now = options?.now ?? Date.now
|
|
25
|
+
const waitMs = options?.waitMs ?? PAGE_EVENT_WAIT_MS
|
|
26
|
+
const maxAgeMs = options?.maxAgeMs ?? PAGE_EVENT_MAX_AGE_MS
|
|
27
|
+
let seq = 0
|
|
28
|
+
/** seq -> { seq, at, type, ...payload },按发布顺序。 */
|
|
29
|
+
const recent = new Map()
|
|
30
|
+
/** 正在等待的页面:每个是一个"现在就回"的函数。 */
|
|
31
|
+
const waiters = new Set()
|
|
32
|
+
|
|
33
|
+
/** 序号大于 `after`、还没过期的事件。 */
|
|
34
|
+
const fresh = (after) =>
|
|
35
|
+
Array.from(recent.values())
|
|
36
|
+
.filter((event) => event.seq > after && now() - event.at <= maxAgeMs)
|
|
37
|
+
.map(({ at, ...event }) => event)
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
/** 发布一个动作,叫醒所有在等的页面。返回它的序号。 */
|
|
41
|
+
publish(type, payload) {
|
|
42
|
+
seq += 1
|
|
43
|
+
recent.set(seq, { ...payload, seq, at: now(), type })
|
|
44
|
+
for (const old of recent.keys()) {
|
|
45
|
+
if (recent.size <= KEEP) break
|
|
46
|
+
recent.delete(old)
|
|
47
|
+
}
|
|
48
|
+
for (const wake of Array.from(waiters)) wake()
|
|
49
|
+
return seq
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 页面的一次长轮询。
|
|
54
|
+
*
|
|
55
|
+
* @param after 页面已经处理到的序号;负数表示"我刚连上",只拿当前序号,不要之前的事件
|
|
56
|
+
* @param signal 请求断开时结束等待
|
|
57
|
+
* @returns `{ seq, events }`:`seq` 是页面下次该带上的序号
|
|
58
|
+
*/
|
|
59
|
+
waitAfter(after, signal) {
|
|
60
|
+
// 序号比我们的还大:页面记着的是上一个插件实例的序号(插件重载过)。让它重新对齐。
|
|
61
|
+
if (after < 0 || after > seq) return Promise.resolve({ seq, events: fresh(seq) })
|
|
62
|
+
const pending = fresh(after)
|
|
63
|
+
if (pending.length > 0 || signal?.aborted === true) return Promise.resolve({ seq, events: pending })
|
|
64
|
+
|
|
65
|
+
return new Promise((resolve) => {
|
|
66
|
+
const finish = () => {
|
|
67
|
+
waiters.delete(finish)
|
|
68
|
+
clearTimeout(timer)
|
|
69
|
+
signal?.removeEventListener('abort', finish)
|
|
70
|
+
resolve({ seq, events: fresh(after) })
|
|
71
|
+
}
|
|
72
|
+
const timer = setTimeout(finish, waitMs)
|
|
73
|
+
timer.unref?.()
|
|
74
|
+
signal?.addEventListener('abort', finish, { once: true })
|
|
75
|
+
waiters.add(finish)
|
|
76
|
+
})
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
/** 插件卸载时放走所有在等的页面。 */
|
|
80
|
+
dispose() {
|
|
81
|
+
for (const wake of Array.from(waiters)) wake()
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
/** 当前挂着的长轮询数量,也就是连着的页面数。 */
|
|
85
|
+
get waiting() {
|
|
86
|
+
return waiters.size
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
}
|
package/executable.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 岛屿程序在哪。
|
|
3
|
+
*
|
|
4
|
+
* 顺序:设置里指定的路径 › 随插件一起装上的(从 npm 装的包自带,见 native/scripts/pack-plugin.mjs)
|
|
5
|
+
* › `%LOCALAPPDATA%\VibeDev\island`(开发时手动拷过去的那一份)。随包的排在前面:从 npm 装过之后,
|
|
6
|
+
* 机器上旧的手动拷贝不会顶替新版本。开发用的插件目录里没有随包的程序,所以开发时照旧用手动拷的那份。
|
|
7
|
+
*
|
|
8
|
+
* 岛屿只有 Windows 版:别的系统上没有候选,宿主据此说明「只支持 Windows」。
|
|
9
|
+
*/
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
|
|
12
|
+
/** 随包的岛屿程序相对插件目录的位置。打包脚本把编译好的程序放到这里。 */
|
|
13
|
+
export const BUNDLED_EXECUTABLE = Object.freeze(['bin', 'win32-x64', 'vibedev-island.exe'])
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 按顺序该去哪几个地方找岛屿程序。
|
|
17
|
+
*
|
|
18
|
+
* @param {{ configured?: unknown, platform: string, pluginDir: string, localAppData: string, appDirName: string }} where
|
|
19
|
+
* @returns {string[]} 候选路径;设置里指定了就只有那一个,不是 Windows 时是空的
|
|
20
|
+
*/
|
|
21
|
+
export function executableCandidates({ configured, platform, pluginDir, localAppData, appDirName }) {
|
|
22
|
+
const chosen = typeof configured === 'string' ? configured.trim() : ''
|
|
23
|
+
if (chosen !== '') return [chosen]
|
|
24
|
+
if (platform !== 'win32') return []
|
|
25
|
+
return [join(pluginDir, ...BUNDLED_EXECUTABLE), join(localAppData, appDirName, 'island', 'vibedev-island.exe')]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 找不到岛屿程序时给用户看的一句话。
|
|
30
|
+
*
|
|
31
|
+
* @param {string} platform `process.platform`
|
|
32
|
+
* @param {string[]} candidates 找过的地方
|
|
33
|
+
*/
|
|
34
|
+
export function missingExecutableMessage(platform, candidates) {
|
|
35
|
+
if (platform !== 'win32' && candidates.length === 0) return 'VibeDev 岛屿只支持 Windows。'
|
|
36
|
+
return `没有找到岛屿程序(找过:${candidates.join(';')})。重新安装插件试试。`
|
|
37
|
+
}
|
package/icon.svg
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="VibeDev Island">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="vd-island" x1="0" y1="0" x2="1" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#5b7bff"/>
|
|
5
|
+
<stop offset="1" stop-color="#8a5bff"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect width="64" height="64" rx="16" fill="url(#vd-island)"/>
|
|
9
|
+
<!-- 屏幕顶部的胶囊:左边是运行状态灯,右边是波形。 -->
|
|
10
|
+
<rect x="12" y="14" width="40" height="13" rx="6.5" fill="#0a0a0b"/>
|
|
11
|
+
<circle cx="19.5" cy="20.5" r="2.6" fill="#3ae489"/>
|
|
12
|
+
<rect x="25" y="19.1" width="15" height="2.8" rx="1.4" fill="#ffffff" opacity="0.86"/>
|
|
13
|
+
<g fill="#8ef0b8">
|
|
14
|
+
<rect x="42" y="18.4" width="1.9" height="4.2" rx="0.95"/>
|
|
15
|
+
<rect x="45.4" y="17" width="1.9" height="7" rx="0.95"/>
|
|
16
|
+
<rect x="48.8" y="18.9" width="1.9" height="3.2" rx="0.95"/>
|
|
17
|
+
</g>
|
|
18
|
+
<!-- 岛屿下方的两层弧线,呼应展开后的面板。 -->
|
|
19
|
+
<path d="M16 38c5-4 10.5-6 16-6s11 2 16 6" fill="none" stroke="#ffffff" stroke-width="2.4" stroke-linecap="round" opacity="0.55"/>
|
|
20
|
+
<path d="M21 47c3.6-2.6 7.2-3.9 11-3.9s7.4 1.3 11 3.9" fill="none" stroke="#ffffff" stroke-width="2.4" stroke-linecap="round" opacity="0.32"/>
|
|
21
|
+
</svg>
|