@yusho/server 0.0.0-stage → 0.1.17
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/README.md +135 -2
- package/app/appguide.mjs +33 -0
- package/app/asr.mjs +82 -0
- package/app/assets/agents/claude-192.webp +0 -0
- package/app/assets/agents/claude-96.webp +0 -0
- package/app/assets/agents/codex-192.webp +0 -0
- package/app/assets/agents/codex-96.webp +0 -0
- package/app/assets/agents/default-192.webp +0 -0
- package/app/assets/agents/default-96.webp +0 -0
- package/app/assets/agents/dsh-192.webp +0 -0
- package/app/assets/agents/dsh-96.webp +0 -0
- package/app/assets/brand/logo.webp +0 -0
- package/app/assets/lib/THIRD-PARTY.txt +1 -0
- package/app/assets/lib/jsqr.js +10102 -0
- package/app/assets/linux/alpine-arm64.tar.gz +0 -0
- package/app/assets/linux/alpine-x64.tar.gz +0 -0
- package/app/assets/live/claude.livliv +0 -0
- package/app/assets/live/dafeiyu.livliv +0 -0
- package/app/assets/live/gpt.livliv +0 -0
- package/app/assets/live/sho.livliv +0 -0
- package/app/assets/live//345/275/242/350/261/241/346/235/245/346/272/220.md +5 -0
- package/app/assets/livliv/LICENSE +202 -0
- package/app/assets/livliv/NOTICE +7 -0
- package/app/assets/livliv/player.mjs +15 -0
- package/app/assets/looks/claude.webp +0 -0
- package/app/assets/looks/dafeiyu.webp +0 -0
- package/app/assets/looks/gpt.webp +0 -0
- package/app/assets/looks//345/275/242/350/261/241/346/235/245/346/272/220.md +5 -0
- package/app/assets/sho/avatar.webp +0 -0
- package/app/assets/sho/done.webp +0 -0
- package/app/assets/sho/eat.webp +0 -0
- package/app/assets/sho/idle-blink.webp +0 -0
- package/app/assets/sho/idle.webp +0 -0
- package/app/assets/sho/listen.webp +0 -0
- package/app/assets/sho/need.webp +0 -0
- package/app/assets/sho/work.webp +0 -0
- package/app/companion.mjs +519 -0
- package/app/diag.mjs +51 -0
- package/app/index.html +497 -0
- package/app/judge.mjs +103 -0
- package/app/lanxfer.mjs +157 -0
- package/app/launcher.mjs +2715 -0
- package/app/linux.mjs +97 -0
- package/app/livliv-pack.mjs +12 -0
- package/app/memory.mjs +53 -0
- package/app/net.mjs +717 -0
- package/app/office.mjs +172 -0
- package/app/p2plink.mjs +134 -0
- package/app/pcupdate.mjs +55 -0
- package/app/plans.mjs +126 -0
- package/app/plugins.mjs +79 -0
- package/app/realtime.mjs +67 -0
- package/app/skills/app-icon/SKILL.md +31 -0
- package/app/skills/daily-brief/SKILL.md +29 -0
- package/app/skills/expense-review/SKILL.md +28 -0
- package/app/skills/make-app/SKILL.md +92 -0
- package/app/skills/make-skin/SKILL.md +51 -0
- package/app/skills/meeting-notes/SKILL.md +23 -0
- package/app/skills/reading-notes/SKILL.md +25 -0
- package/app/skills/sho-stickers/SKILL.md +34 -0
- package/app/skills/sho-stickers/stickers/eat.webp +0 -0
- package/app/skills/sho-stickers/stickers/got-it.webp +0 -0
- package/app/skills/sho-stickers/stickers/hi.webp +0 -0
- package/app/skills/sho-stickers/stickers/huh.webp +0 -0
- package/app/skills/sho-stickers/stickers/on-it.webp +0 -0
- package/app/skills/sho-stickers/stickers/yay.webp +0 -0
- package/app/skills/sho-stickers/stickers.json +12 -0
- package/app/skills/study-plan/SKILL.md +30 -0
- package/app/skills/trip-planner/SKILL.md +29 -0
- package/app/skills/weekly-report/SKILL.md +30 -0
- package/app/skills/workout-plan/SKILL.md +30 -0
- package/app/skills/writing-polish/SKILL.md +33 -0
- package/app/skills.mjs +173 -0
- package/app/skins.mjs +40 -0
- package/app/social.mjs +1067 -0
- package/app/storage.mjs +143 -0
- package/app/tts.mjs +68 -0
- package/app/ttslocal.mjs +77 -0
- package/app/voices.mjs +102 -0
- package/backup.mjs +109 -0
- package/bin/yusho.mjs +141 -0
- package/connector/lib/api.mjs +14 -0
- package/connector/lib/autostart.mjs +40 -0
- package/connector/lib/client.js +165 -0
- package/connector/lib/connector.mjs +468 -0
- package/connector/lib/dsh-acp.mjs +129 -0
- package/connector/lib/dsh-headless.patch.yml +4 -0
- package/connector/lib/dsh-plugin.mjs +80 -0
- package/connector/lib/lanfile.mjs +151 -0
- package/connector/lib/nostr.mjs +53 -0
- package/connector/lib/page.html +121 -0
- package/connector/lib/qr.mjs +4 -0
- package/connector/lib/service.mjs +54 -0
- package/node_modules/@local/shoal-mobile/lib/index.js +485 -0
- package/node_modules/@local/shoal-mobile/lib/polyfill.js +82 -0
- package/node_modules/@local/shoal-mobile/lib/session-context.js +34 -0
- package/node_modules/@local/shoal-mobile/lib/shell.js +60 -0
- package/node_modules/@local/shoal-mobile/lib/web-startup.js +33 -0
- package/node_modules/@local/shoal-mobile/package.json +24 -0
- package/node_modules/@local/yusho-pc/lib/client.js +9100 -0
- package/node_modules/@local/yusho-pc/lib/index.js +3 -0
- package/node_modules/@local/yusho-pc/package.json +28 -0
- package/node_modules/@local/yusho-web-login/brand/icon-192.webp +0 -0
- package/node_modules/@local/yusho-web-login/brand/logo-dark.svg +25 -0
- package/node_modules/@local/yusho-web-login/brand.mjs +8 -0
- package/node_modules/@local/yusho-web-login/dsh-web-login.mjs +974 -0
- package/node_modules/@local/yusho-web-login/lib/client.js +607 -0
- package/node_modules/@local/yusho-web-login/login-page.mjs +609 -0
- package/node_modules/@local/yusho-web-login/login-store.mjs +708 -0
- package/node_modules/@local/yusho-web-login/package.json +25 -0
- package/node_modules/@local/yusho-web-login/whale-scene.mjs +509 -0
- package/node_modules/@local/yusho-web-login/yusho-scene.mjs +227 -0
- package/package.json +38 -4
- package/pc.patch.yml +83 -0
- package/run.mjs +138 -0
package/app/skills.mjs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// 技能管理(任务页的“技能”块):列出、查看、启用 / 停用、删除、从链接安装。
|
|
2
|
+
// 技能就是 DSH 的格式:<名字>/SKILL.md(开头写 name、description),放进技能目录 DSH 就自动认(不用重启)。
|
|
3
|
+
// 随 App 带的:内核目录里的 skills/(只读,升级跟着换)
|
|
4
|
+
// 用户装的:DSH 家目录 skills/(升级不丢);停用的挪到 skills-off/(DSH 不看那里)
|
|
5
|
+
// 手机上只装纯文字技能(说明、参考资料、图片、清单)。带脚本的技能手机上跑不了,拒装并说明。
|
|
6
|
+
import fs from 'node:fs'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import crypto from 'node:crypto'
|
|
9
|
+
|
|
10
|
+
const TEXT_OK = /\.(md|txt|json|ya?ml|csv|tsv|png|jpe?g|webp|gif|svg)$/i
|
|
11
|
+
const SCRIPT = /\.(sh|bash|py|js|mjs|cjs|ts|ps1|bat|cmd|exe|rb|pl|go|rs|java|lua|php)$/i
|
|
12
|
+
const MAX_FILES = 60, MAX_FILE = 2 * 1024 * 1024, MAX_TOTAL = 6 * 1024 * 1024
|
|
13
|
+
|
|
14
|
+
function frontmatter(md) {
|
|
15
|
+
const m = /^---\r?\n([\s\S]*?)\r?\n---/.exec(md); if (!m) return {}
|
|
16
|
+
const o = {}; for (const l of m[1].split(/\r?\n/)) { const k = /^([A-Za-z][\w-]*):\s*(.*)$/.exec(l); if (k) o[k[1]] = k[2].replace(/^["']|["']$/g, '').trim() }
|
|
17
|
+
return o
|
|
18
|
+
}
|
|
19
|
+
const walk = (d, base = d) => { const out = []; for (const e of fs.readdirSync(d, { withFileTypes: true })) { const p = path.join(d, e.name); if (e.isDirectory()) out.push(...walk(p, base)); else out.push(path.relative(base, p).split(path.sep).join('/')) } return out }
|
|
20
|
+
|
|
21
|
+
export function createSkills({ bundled, home, log = () => {} }) {
|
|
22
|
+
bundled = path.resolve(bundled); home = path.resolve(home)
|
|
23
|
+
const USER = path.join(home, 'skills'), OFF = path.join(home, 'skills-off')
|
|
24
|
+
const okName = (n) => /^[a-z0-9][a-z0-9-]{0,63}$/.test(String(n ?? ''))
|
|
25
|
+
|
|
26
|
+
function read(dir, kind, enabled) {
|
|
27
|
+
const f = path.join(dir, 'SKILL.md'); if (!fs.existsSync(f)) return null
|
|
28
|
+
const md = fs.readFileSync(f, 'utf8'), fm = frontmatter(md)
|
|
29
|
+
let source = null; try { source = JSON.parse(fs.readFileSync(path.join(dir, '.yusho-source.json'), 'utf8')) } catch {}
|
|
30
|
+
let stickers = null; try { const s = JSON.parse(fs.readFileSync(path.join(dir, 'stickers.json'), 'utf8')); stickers = { pack: s.pack, title: s.title, count: (s.items ?? []).length } } catch {}
|
|
31
|
+
return { id: path.basename(dir), name: fm.name || path.basename(dir), description: fm.description || '', kind, enabled, source: source && { url: source.url, installedAt: source.installedAt }, stickers }
|
|
32
|
+
}
|
|
33
|
+
function list() {
|
|
34
|
+
const out = []
|
|
35
|
+
const scan = (root, kind, enabled) => { if (!fs.existsSync(root)) return; for (const e of fs.readdirSync(root, { withFileTypes: true })) if (e.isDirectory()) { const s = read(path.join(root, e.name), kind, enabled); if (s) out.push(s) } }
|
|
36
|
+
scan(bundled, 'bundled', true); scan(USER, 'user', true); scan(OFF, 'user', false)
|
|
37
|
+
return out.sort((a, b) => (a.kind === b.kind ? a.name.localeCompare(b.name) : a.kind === 'bundled' ? -1 : 1))
|
|
38
|
+
}
|
|
39
|
+
const dirOf = (id) => { if (!okName(id)) return null; for (const [root, kind, on] of [[bundled, 'bundled', true], [USER, 'user', true], [OFF, 'user', false]]) { const d = path.join(root, id); if (fs.existsSync(path.join(d, 'SKILL.md'))) return { d, kind, on } } return null }
|
|
40
|
+
function get(id) {
|
|
41
|
+
const x = dirOf(id); if (!x) throw new Error('没有这个技能')
|
|
42
|
+
return { ...read(x.d, x.kind, x.on), skillMd: fs.readFileSync(path.join(x.d, 'SKILL.md'), 'utf8').slice(0, 100_000), files: walk(x.d).filter((f) => !f.startsWith('.')).slice(0, 200) }
|
|
43
|
+
}
|
|
44
|
+
function toggle(id, on) {
|
|
45
|
+
const x = dirOf(id); if (!x) throw new Error('没有这个技能')
|
|
46
|
+
if (x.kind === 'bundled') throw new Error('随 App 带的技能不能停用')
|
|
47
|
+
if (x.on === !!on) return list()
|
|
48
|
+
const to = path.join(on ? USER : OFF, id); fs.mkdirSync(path.dirname(to), { recursive: true }); fs.rmSync(to, { recursive: true, force: true }); fs.renameSync(x.d, to)
|
|
49
|
+
return list()
|
|
50
|
+
}
|
|
51
|
+
function remove(id) {
|
|
52
|
+
const x = dirOf(id); if (!x) throw new Error('没有这个技能')
|
|
53
|
+
if (x.kind === 'bundled') throw new Error('随 App 带的技能不能删')
|
|
54
|
+
fs.rmSync(x.d, { recursive: true, force: true }); return list()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 从链接装:GitHub 上技能所在的文件夹(https://github.com/作者/仓库/tree/分支/路径),或者一个 SKILL.md 的直接链接
|
|
58
|
+
async function get_(url, asJson) {
|
|
59
|
+
let r; try { r = await fetch(url, { headers: { 'user-agent': 'yusho-app', accept: asJson ? 'application/vnd.github+json' : '*/*' }, signal: AbortSignal.timeout(20_000) }) } catch (e) { const host = new URL(url).hostname; throw new Error('连不上 ' + host + '(' + (e.cause?.code || e.message) + ')' + (/github/.test(host) ? '。国内连 GitHub 常常不稳,可以先从上面的“技能清单”里装' : '')) }
|
|
60
|
+
if (!r.ok) throw new Error((r.status === 404 ? '找不到:' : '下载失败(HTTP ' + r.status + '):') + url)
|
|
61
|
+
return asJson ? r.json() : Buffer.from(await r.arrayBuffer())
|
|
62
|
+
}
|
|
63
|
+
async function fetchFiles(url) {
|
|
64
|
+
const u = new URL(url)
|
|
65
|
+
if (u.hostname === 'github.com') {
|
|
66
|
+
const [owner, repo, kind, ref, ...rest] = u.pathname.split('/').filter(Boolean)
|
|
67
|
+
if (!owner || !repo) throw new Error('GitHub 链接要指到仓库或里面的文件夹')
|
|
68
|
+
const sub = kind === 'tree' || kind === 'blob' ? rest.join('/').replace(/\/?SKILL\.md$/i, '') : ''
|
|
69
|
+
const q = (p) => `https://api.github.com/repos/${owner}/${repo}/contents/${p}${kind === 'tree' || kind === 'blob' ? '?ref=' + encodeURIComponent(ref) : ''}`
|
|
70
|
+
const files = []
|
|
71
|
+
const rec = async (p, rel) => {
|
|
72
|
+
const items = await get_(q(p), true); if (!Array.isArray(items)) throw new Error('这个链接不是文件夹')
|
|
73
|
+
for (const it of items) {
|
|
74
|
+
if (files.length > MAX_FILES) throw new Error('文件太多(超过 ' + MAX_FILES + ' 个),不像是一个技能')
|
|
75
|
+
const r2 = rel ? rel + '/' + it.name : it.name
|
|
76
|
+
if (it.type === 'dir') await rec(it.path, r2)
|
|
77
|
+
else if (it.type === 'file') files.push({ rel: r2, url: it.download_url, size: it.size })
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
await rec(sub, '')
|
|
81
|
+
if (!files.some((f) => f.rel === 'SKILL.md')) {
|
|
82
|
+
const cands = [...new Set(files.filter((f) => /(^|\/)SKILL\.md$/.test(f.rel)).map((f) => f.rel.replace(/\/?SKILL\.md$/, '')))].slice(0, 8)
|
|
83
|
+
throw new Error(cands.length ? '这个链接里有好几个技能,请打开其中一个文件夹再复制链接:' + cands.join('、') : '这个文件夹里没有 SKILL.md,不是技能')
|
|
84
|
+
}
|
|
85
|
+
return files
|
|
86
|
+
}
|
|
87
|
+
if (/\.md($|\?)/i.test(u.pathname)) return [{ rel: 'SKILL.md', url, size: 0 }]
|
|
88
|
+
throw new Error('只认 GitHub 上的技能文件夹链接,或者 SKILL.md 的直接链接')
|
|
89
|
+
}
|
|
90
|
+
async function install(url) {
|
|
91
|
+
const files = await fetchFiles(String(url ?? '').trim())
|
|
92
|
+
const scripts = files.filter((f) => SCRIPT.test(f.rel) || /(^|\/)scripts\//.test(f.rel))
|
|
93
|
+
if (scripts.length) throw new Error('这个技能带脚本(' + scripts.slice(0, 3).map((f) => f.rel).join('、') + '),手机上跑不了。请在电脑上的 DSH 里装')
|
|
94
|
+
const skip = files.filter((f) => !TEXT_OK.test(f.rel) && !/(^|\/)(LICENSE|README)/i.test(f.rel))
|
|
95
|
+
const keep = files.filter((f) => !skip.includes(f))
|
|
96
|
+
let total = 0; const got = []
|
|
97
|
+
for (const f of keep) {
|
|
98
|
+
const b = await get_(f.url); if (b.length > MAX_FILE) throw new Error(f.rel + ' 太大(超过 2 MB)')
|
|
99
|
+
total += b.length; if (total > MAX_TOTAL) throw new Error('技能太大(超过 6 MB)')
|
|
100
|
+
got.push({ rel: f.rel, b })
|
|
101
|
+
}
|
|
102
|
+
const md = got.find((f) => f.rel === 'SKILL.md')?.b.toString('utf8') ?? ''
|
|
103
|
+
const fm = frontmatter(md)
|
|
104
|
+
if (!fm.name || !fm.description) throw new Error('SKILL.md 开头要写 name 和 description,这个不是规范的技能')
|
|
105
|
+
const id = String(fm.name).toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 64)
|
|
106
|
+
if (!okName(id)) throw new Error('技能名不合规:' + fm.name)
|
|
107
|
+
if (fs.existsSync(path.join(bundled, id))) throw new Error('和随 App 带的技能重名:' + id)
|
|
108
|
+
const to = path.join(USER, id), tmp = to + '.installing'
|
|
109
|
+
fs.rmSync(tmp, { recursive: true, force: true })
|
|
110
|
+
for (const f of got) { const p = path.join(tmp, ...f.rel.split('/')); if (!p.startsWith(tmp + path.sep)) throw new Error('文件路径不对:' + f.rel); fs.mkdirSync(path.dirname(p), { recursive: true }); fs.writeFileSync(p, f.b) }
|
|
111
|
+
// 记下来源和每个文件的哈希:以后更新、核对有没有被换过包
|
|
112
|
+
fs.writeFileSync(path.join(tmp, '.yusho-source.json'), JSON.stringify({ url, installedAt: Date.now(), files: got.map((f) => ({ path: f.rel, sha256: crypto.createHash('sha256').update(f.b).digest('hex') })) }, null, 1))
|
|
113
|
+
fs.rmSync(path.join(OFF, id), { recursive: true, force: true }); fs.rmSync(to, { recursive: true, force: true }); fs.mkdirSync(USER, { recursive: true }); fs.renameSync(tmp, to)
|
|
114
|
+
log('装好了技能 ' + id + '(' + got.length + ' 个文件)')
|
|
115
|
+
return { id, name: fm.name, skipped: skip.map((f) => f.rel) }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ── 鱼Sho 技能清单(https://yusho.chat/skills/):国内直接能连,不用翻墙。文件按哈希存,下载后逐个核对
|
|
119
|
+
const CATALOG = process.env.SHOAL_SKILLS_CATALOG || 'https://yusho.chat/skills/'
|
|
120
|
+
// 两份清单:精选(我们写的、官方的)和社区(我们从别处挑来、审过、镜像到自己服务器的)。格式一样,地址不同
|
|
121
|
+
const base = (src) => (src === 'community' ? CATALOG + 'community/' : CATALOG)
|
|
122
|
+
const cats = {}
|
|
123
|
+
async function catalog(force, src = 'featured') {
|
|
124
|
+
const c0 = cats[src]; if (!force && c0 && Date.now() - c0.at < 10 * 60_000) return c0.data
|
|
125
|
+
const j = await get_(base(src) + 'index.json', true)
|
|
126
|
+
const mine = new Map(list().map((x) => [x.id, x]))
|
|
127
|
+
const catCache = { updatedAt: j.updatedAt, skills: (j.skills ?? []).map((x) => ({ id: x.id, name: x.name, title: x.title, description: x.description, category: x.category, license: x.license, source: x.source, size: (x.files ?? []).reduce((a, b) => a + (b.size || 0), 0), installed: mine.has(x.id) ? (mine.get(x.id).enabled ? 'on' : 'off') : null, files: x.files })) }
|
|
128
|
+
cats[src] = { at: Date.now(), data: catCache }; return catCache
|
|
129
|
+
}
|
|
130
|
+
async function installFromCatalog(id, src = 'featured') {
|
|
131
|
+
const c = await catalog(false, src); const x = c.skills.find((k) => k.id === id); if (!x) throw new Error('清单里没有这个技能')
|
|
132
|
+
if (!okName(id)) throw new Error('技能名不合规')
|
|
133
|
+
if (fs.existsSync(path.join(bundled, id))) throw new Error('和随 App 带的技能重名')
|
|
134
|
+
const to = path.join(USER, id), tmp = to + '.installing'; fs.rmSync(tmp, { recursive: true, force: true })
|
|
135
|
+
for (const file of x.files) {
|
|
136
|
+
if (SCRIPT.test(file.path) || !TEXT_OK.test(file.path)) continue
|
|
137
|
+
const b = await get_(base(src) + 'blobs/' + file.sha256)
|
|
138
|
+
if (crypto.createHash('sha256').update(b).digest('hex') !== file.sha256) throw new Error('下载的文件对不上(' + file.path + '),已停止安装')
|
|
139
|
+
const p = path.join(tmp, ...file.path.split('/')); if (!p.startsWith(tmp + path.sep)) throw new Error('文件路径不对:' + file.path)
|
|
140
|
+
fs.mkdirSync(path.dirname(p), { recursive: true }); fs.writeFileSync(p, b)
|
|
141
|
+
}
|
|
142
|
+
if (!fs.existsSync(path.join(tmp, 'SKILL.md'))) throw new Error('这个技能缺 SKILL.md')
|
|
143
|
+
fs.writeFileSync(path.join(tmp, '.yusho-source.json'), JSON.stringify({ url: x.source, catalog: base(src), tier: src, installedAt: Date.now(), files: x.files }, null, 1))
|
|
144
|
+
fs.rmSync(path.join(OFF, id), { recursive: true, force: true }); fs.rmSync(to, { recursive: true, force: true }); fs.mkdirSync(USER, { recursive: true }); fs.renameSync(tmp, to)
|
|
145
|
+
for (const k2 of Object.keys(cats)) delete cats[k2]; log('从鱼Sho 清单装好了技能 ' + id + '(' + src + ')')
|
|
146
|
+
return { id, name: x.title || x.name }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 贴纸:所有启用的、带 stickers.json 的技能。[贴纸:名字] 或 [贴纸:包/名字]
|
|
150
|
+
function stickerFile(pack, name) {
|
|
151
|
+
for (const s of list().filter((x) => x.enabled && x.stickers)) {
|
|
152
|
+
if (pack && s.stickers.pack !== pack) continue
|
|
153
|
+
const d = dirOf(s.id).d, man = JSON.parse(fs.readFileSync(path.join(d, 'stickers.json'), 'utf8'))
|
|
154
|
+
const it = (man.items ?? []).find((i) => i.name === name); if (!it) continue
|
|
155
|
+
const p = path.resolve(d, it.file); if (p.startsWith(d + path.sep) && fs.existsSync(p)) return p
|
|
156
|
+
}
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 所有启用的贴纸包(给聊天里的贴纸面板用) */
|
|
161
|
+
function stickerPacks() { return list().filter((x) => x.enabled && x.stickers).map((x) => { const man = JSON.parse(fs.readFileSync(path.join(dirOf(x.id).d, 'stickers.json'), 'utf8')); return { pack: man.pack, title: man.title, items: (man.items ?? []).map((i) => ({ name: i.name })) } }) }
|
|
162
|
+
/** 用一段说明建一个自己的技能:id 用英文小写和横线;同名的加个数字 */
|
|
163
|
+
function create({ title, description, body }) {
|
|
164
|
+
const base = 'mine-' + (Date.now().toString(36)), id = okName(base) ? base : 'mine-skill'
|
|
165
|
+
const name = String(title ?? '').replace(/[\r\n]/g, ' ').trim().slice(0, 60) || '拿来的做法'
|
|
166
|
+
const desc = String(description ?? '').replace(/[\r\n]/g, ' ').trim().slice(0, 300) || name
|
|
167
|
+
const to = path.join(USER, id); fs.mkdirSync(to, { recursive: true })
|
|
168
|
+
fs.writeFileSync(path.join(to, 'SKILL.md'), '---\nname: ' + id + '\ndescription: ' + JSON.stringify(name + ':' + desc) + '\n---\n\n# ' + name + '\n\n' + String(body ?? '').slice(0, 20000) + '\n')
|
|
169
|
+
fs.writeFileSync(path.join(to, '.yusho-source.json'), JSON.stringify({ url: 'moment', installedAt: Date.now() }))
|
|
170
|
+
return { id, list: list() }
|
|
171
|
+
}
|
|
172
|
+
return { create, list, get, toggle, remove, install, stickerFile, catalog, installFromCatalog, stickerPacks }
|
|
173
|
+
}
|
package/app/skins.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// 自定义皮肤:助理按用户的描述(或参考图)生成一组外观变量,存成 skins/<id>.json;界面只能用、删
|
|
2
|
+
// 只收白名单里的 CSS 变量,值只能是颜色、渐变、长度、阴影(不能 url()、不能跑脚本、不能写别的规则)
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
|
|
6
|
+
export const SKIN_VARS = {
|
|
7
|
+
'--room-bg': '助理页背景(颜色或渐变;不给就按主色自动配)', '--room-top': '助理页最上面的颜色(和状态栏接上)',
|
|
8
|
+
'--page': '页面背景(颜色或渐变)', '--page-top': '顶部颜色', '--bar': '底栏、输入框的底(可半透明)',
|
|
9
|
+
'--bg-2': '卡片底色', '--bg-elev': '弹出层、面板底色', '--fill': '按钮、输入框的浅底', '--fill-press': '按下时的浅底', '--line': '分隔线',
|
|
10
|
+
'--text': '正文字色', '--text-1': '标题字色', '--text-2': '次要文字', '--text-3': '最弱的文字(说明、时间)',
|
|
11
|
+
'--brand': '主色(链接、选中)', '--brand-press': '主色按下', '--brand-fill': '主按钮底色', '--on-brand': '主按钮上的字', '--brand-soft': '主色的浅底(标签、选中的格子)', '--brand-soft-ink': '浅底上的字',
|
|
12
|
+
'--card-edge': '卡片描边(box-shadow 写法)', '--card-sh': '卡片阴影', '--r-card': '卡片圆角(px)', '--r-ctl': '输入框圆角', '--r-btn': '按钮圆角', '--r-bubble': '气泡圆角',
|
|
13
|
+
'--bub-bg': '别人消息气泡的底', '--bub-ink': '别人消息的字', '--bub-me-bg': '自己消息气泡的底(可渐变)', '--bub-me-ink': '自己消息的字', '--btn-pri-bg': '主按钮的底(可渐变,覆盖 --brand-fill)',
|
|
14
|
+
}
|
|
15
|
+
const FN = /^(rgba?|hsla?|color-mix|linear-gradient|radial-gradient|conic-gradient|repeating-linear-gradient|repeating-radial-gradient|calc|var)\($/i
|
|
16
|
+
/** 一个值是否安全:只许颜色、数字、长度、百分比、关键字、上面这些函数;不许 url、引号、分号、花括号、注释 */
|
|
17
|
+
export function safeValue(v) {
|
|
18
|
+
const s = String(v ?? '').trim(); if (!s || s.length > 600) return null
|
|
19
|
+
if (/url\s*\(|expression|javascript:|@|;|\{|\}|<|>|\/\*|\\|["'`]/i.test(s)) return null
|
|
20
|
+
const tokens = s.match(/[a-z-]+\(|#[0-9a-f]{3,8}\b|-?[\d.]+(px|%|deg|em|rem|vh|vw)?|[a-z-]+|[(),/]|\s+/gi) ?? []
|
|
21
|
+
if (tokens.join('') !== s) return null
|
|
22
|
+
for (const t of tokens) { if (/^[a-z-]+\($/i.test(t) && !FN.test(t)) return null; if (/^--/.test(t) && !SKIN_VARS[t]) return null }
|
|
23
|
+
return s
|
|
24
|
+
}
|
|
25
|
+
export function createSkins({ APP, log = () => {} }) {
|
|
26
|
+
const DIR = path.join(APP, 'skins'); fs.mkdirSync(DIR, { recursive: true })
|
|
27
|
+
const list = () => fs.readdirSync(DIR).filter((f) => f.endsWith('.json')).map((f) => { try { return JSON.parse(fs.readFileSync(path.join(DIR, f), 'utf8')) } catch { return null } }).filter(Boolean).sort((a, b) => a.createdAt - b.createdAt)
|
|
28
|
+
function save({ name, dark, vars, note }) {
|
|
29
|
+
const nm = String(name ?? '').replace(/[\u0000-\u001f<>]/g, '').trim().slice(0, 16); if (!nm) throw new Error('要给皮肤起个名字')
|
|
30
|
+
const ok = {}, bad = []
|
|
31
|
+
for (const [k, v] of Object.entries(vars ?? {})) { if (!SKIN_VARS[k]) { bad.push(k + '(不认识的变量)'); continue } const sv = safeValue(v); if (!sv) { bad.push(k + '(值不合格)'); continue } ok[k] = sv }
|
|
32
|
+
for (const k of ['--page', '--text', '--brand', '--bg-2']) if (!ok[k]) bad.push(k + '(必须给)')
|
|
33
|
+
if (bad.length) throw new Error('这些没通过:' + bad.join('、'))
|
|
34
|
+
if (list().length >= 20) throw new Error('最多存 20 套自定义皮肤,先删几套')
|
|
35
|
+
const id = 'c' + Date.now().toString(36), sk = { id, name: nm, dark: !!dark, vars: ok, note: String(note ?? '').slice(0, 120), createdAt: Date.now() }
|
|
36
|
+
fs.writeFileSync(path.join(DIR, id + '.json'), JSON.stringify(sk)); log('做好了皮肤', nm); return sk
|
|
37
|
+
}
|
|
38
|
+
const remove = (id) => { if (!/^c[0-9a-z]{4,16}$/.test(String(id))) throw new Error('没有这套皮肤'); fs.rmSync(path.join(DIR, id + '.json'), { force: true }); return { ok: true } }
|
|
39
|
+
return { list, save, remove }
|
|
40
|
+
}
|