Repository navigation
Expand file tree
/
Copy pathskill-update.js
More file actions
58 lines (57 loc) · 3.35 KB
/
Copy pathskill-update.js
File metadata and controls
58 lines (57 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import fs from 'node:fs'
import path from 'node:path'
import { createHash, randomUUID } from 'node:crypto'
const hash = (bytes) => createHash('sha256').update(bytes).digest('hex')
export function localDay(date = new Date()) {
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}
function skillFiles(source, target) {
const files = [{ name: 'SKILL.md', source, target }]
const script = path.join(path.dirname(source), 'scripts', 'connect.py')
if (fs.existsSync(script)) files.push({ name: 'scripts/connect.py', source: script, target: path.join(path.dirname(target), 'scripts', 'connect.py') })
return files
}
function fingerprint(files, field) {
const value = createHash('sha256')
for (const file of files) {
value.update(file.name + '\0')
const exists = fs.existsSync(file[field])
value.update(exists ? 'present\0' : 'missing\0')
if (exists) value.update(fs.readFileSync(file[field]))
}
return value.digest('hex')
}
export function skillDifference(source, target) {
if (!fs.existsSync(source)) throw new Error('插件中找不到随记 Skill')
if (!fs.existsSync(target)) return { installed: false, different: false }
const files = skillFiles(source, target)
const bundledHash = fingerprint(files, 'source')
const installedHash = fingerprint(files, 'target')
return { installed: true, different: bundledHash !== installedHash, bundledHash, installedHash }
}
export function checkSkillUpdate({ source, target, store, checkStore = store, day = localDay() }) {
const out = checkStore.once(`skill-check:${path.resolve(target)}:${day}`, () => skillDifference(source, target))
if (!out.first || !out.value?.different) return null
return store.create({ requestId: `skill-update:${day}:${out.value.bundledHash}:${out.value.installedHash}`, eventKey: 'suiji-skill-update',
kind: 'skill-update', source: 'ui', state: 'succeeded', title: '随记 Skill 可更新',
body: '已安装的 Skill 与插件附带版本不同,可安装当前版本。',
actions: [{ id: 'install', type: 'install-skill', label: '安装更新' }, { id: 'later', type: 'dismiss', label: '暂不更新' }] })
}
export function installSkill({ source, target }) {
const files = skillFiles(source, target)
// Helpers first, instructions last: old instructions never refer to an absent
// newly installed helper. No backups or secrets are installed.
for (const file of [...files].reverse()) {
const bytes = fs.readFileSync(file.source)
const dir = path.dirname(file.target)
for (const parent of [path.dirname(target), dir]) if (fs.existsSync(parent) && fs.lstatSync(parent).isSymbolicLink()) throw new Error('Skill 安装目标目录不能是符号链接')
if (fs.existsSync(file.target) && fs.lstatSync(file.target).isSymbolicLink()) throw new Error('Skill 安装目标不能是符号链接')
fs.mkdirSync(dir, { recursive: true })
const temp = path.resolve(`${file.target}.${randomUUID()}.tmp`)
if (path.dirname(temp) !== path.resolve(dir)) throw new Error('Skill 临时路径越界')
try { fs.writeFileSync(temp, bytes, { mode: 0o600 }); fs.renameSync(temp, file.target) }
finally { if (fs.existsSync(temp)) fs.unlinkSync(temp) }
if (hash(fs.readFileSync(file.target)) !== hash(bytes)) throw new Error('Skill 内容校验失败,请重试安装')
}
return { path: target }
}