@ququ-jiaxk/dsh-groupchat 0.1.9 → 0.1.12
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/client.js +725 -26
- package/index.js +299 -12
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -676,32 +676,60 @@ window.__ModuleLoader__.load({
|
|
|
676
676
|
const o = opts || {}
|
|
677
677
|
const bulletColor = o.muted ? 'rgba(255,255,255,0.7)' : T.textMuted
|
|
678
678
|
|
|
679
|
-
//
|
|
679
|
+
// 行内解析:、**粗体**、`代码`、[文字](链接)
|
|
680
|
+
// 本地路径(行内代码 / 链接 / 图片语法)会渲染成文件卡片,可预览、打开、在 Finder 定位。
|
|
680
681
|
const inline = (str, keyPrefix) => {
|
|
681
682
|
const out = []
|
|
682
|
-
const re = /(\*\*([^*]+)\*\*)|(`([^`]+)`)|(\[([^\]]+)\]\(([^)]+)\))/g
|
|
683
|
+
const re = /(!\[([^\]]*)\]\(([^)]+)\))|(\*\*([^*]+)\*\*)|(`([^`]+)`)|(\[([^\]]+)\]\(([^)]+)\))/g
|
|
683
684
|
let last = 0
|
|
684
685
|
let m
|
|
685
686
|
let i = 0
|
|
686
687
|
while ((m = re.exec(str)) !== null) {
|
|
687
688
|
if (m.index > last) out.push(str.slice(last, m.index))
|
|
688
689
|
const k = keyPrefix + 'i' + (i++)
|
|
689
|
-
if (m[
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
690
|
+
if (m[3] !== undefined) {
|
|
691
|
+
// Markdown 图片:
|
|
692
|
+
const alt = m[2] || ''
|
|
693
|
+
const src = String(m[3] || '').trim()
|
|
694
|
+
if (isFilePath(src)) {
|
|
695
|
+
out.push(h(FileRefCard, { key: k, path: src, label: alt, base: o.base, muted: o.muted }))
|
|
696
|
+
} else if (/^https?:\/\//i.test(src)) {
|
|
697
|
+
out.push(h('img', {
|
|
698
|
+
key: k, src, alt, loading: 'lazy',
|
|
699
|
+
style: { display: 'block', maxWidth: '100%', maxHeight: 260, borderRadius: 8, margin: '6px 0' },
|
|
700
|
+
}))
|
|
701
|
+
} else {
|
|
702
|
+
out.push(h('a', {
|
|
703
|
+
key: k, href: src, target: '_blank', rel: 'noreferrer',
|
|
704
|
+
style: { color: o.muted ? '#fff' : T.accent, textDecoration: 'underline' },
|
|
705
|
+
}, alt || src))
|
|
706
|
+
}
|
|
707
|
+
} else if (m[5] !== undefined) {
|
|
708
|
+
out.push(h('strong', { key: k, style: { fontWeight: 700 } }, m[5]))
|
|
709
|
+
} else if (m[7] !== undefined) {
|
|
710
|
+
const code = m[7]
|
|
711
|
+
if (isFilePath(code)) {
|
|
712
|
+
out.push(h(FileRefCard, { key: k, path: code, base: o.base, muted: o.muted }))
|
|
713
|
+
} else {
|
|
714
|
+
out.push(h('code', {
|
|
715
|
+
key: k,
|
|
716
|
+
style: {
|
|
717
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
718
|
+
fontSize: '0.92em', padding: '1px 5px', borderRadius: 4,
|
|
719
|
+
background: o.muted ? 'rgba(255,255,255,0.18)' : 'rgba(16,41,66,0.07)',
|
|
720
|
+
},
|
|
721
|
+
}, code))
|
|
722
|
+
}
|
|
723
|
+
} else if (m[9] !== undefined) {
|
|
724
|
+
const href = String(m[10] || '').trim()
|
|
725
|
+
if (isFilePath(href)) {
|
|
726
|
+
out.push(h(FileRefCard, { key: k, path: href, label: m[9], base: o.base, muted: o.muted }))
|
|
727
|
+
} else {
|
|
728
|
+
out.push(h('a', {
|
|
729
|
+
key: k, href, target: '_blank', rel: 'noreferrer',
|
|
730
|
+
style: { color: o.muted ? '#fff' : T.accent, textDecoration: 'underline' },
|
|
731
|
+
}, m[9]))
|
|
732
|
+
}
|
|
705
733
|
}
|
|
706
734
|
last = re.lastIndex
|
|
707
735
|
}
|
|
@@ -1198,6 +1226,556 @@ window.__ModuleLoader__.load({
|
|
|
1198
1226
|
}, avatar(m.name, m.role, size, m.agentState, m.avatar || null)))
|
|
1199
1227
|
)
|
|
1200
1228
|
|
|
1229
|
+
// ===== 文件引用卡片 =====
|
|
1230
|
+
// 消息里的本地路径(行内代码 / Markdown 链接 / 图片语法)渲染成可预览、可打开、可在 Finder 定位的卡片。
|
|
1231
|
+
const FILE_IMAGE_EXT_RE = /\.(png|jpe?g|gif|webp|bmp|svg|ico)$/i
|
|
1232
|
+
const FILE_DOC_EXT_RE = /\.(md|markdown|txt|csv|tsv|pdf|docx?|xlsx?|pptx?|png|jpe?g|gif|webp|bmp|svg|zip|tgz|tar|mp4|mov|mp3|wav)$/i
|
|
1233
|
+
const FILE_ANY_EXT_RE = /\.(md|markdown|txt|json|jsonl|csv|tsv|log|yml|yaml|js|mjs|cjs|ts|tsx|jsx|py|sh|bash|zsh|html|htm|css|scss|less|xml|toml|ini|conf|env|sql|java|go|rs|c|h|cpp|hpp|rb|php|vue|svelte|png|jpe?g|gif|webp|bmp|svg|ico|pdf|docx?|xlsx?|pptx?|zip|tgz|tar|mp4|mov|mp3|wav)$/i
|
|
1234
|
+
const FILE_TEXT_EXT_RE = /\.(md|markdown|txt|json|jsonl|csv|tsv|log|yml|yaml|js|mjs|cjs|ts|tsx|jsx|py|sh|bash|zsh|html|htm|css|scss|less|xml|toml|ini|conf|env|sql|java|go|rs|c|h|cpp|hpp|rb|php|vue|svelte|gitignore)$/i
|
|
1235
|
+
|
|
1236
|
+
function isFilePath(value) {
|
|
1237
|
+
const s = String(value == null ? '' : value).trim().replace(/^<|>$/g, '')
|
|
1238
|
+
if (!s || s.length > 600) return false
|
|
1239
|
+
if (/^(https?:\/\/|mailto:|data:|#)/i.test(s)) return false
|
|
1240
|
+
if (/[\r\n]/.test(s)) return false
|
|
1241
|
+
const clean = s.split(/[?#]/)[0]
|
|
1242
|
+
if (!FILE_ANY_EXT_RE.test(clean)) return false
|
|
1243
|
+
if (/^(file:\/\/|\/|~\/|\.\.?\/|[A-Za-z]:[\\/])/.test(s)) return true
|
|
1244
|
+
if (s.includes('/') || s.includes('\\')) return true
|
|
1245
|
+
// 裸文件名:只认文档/图片类扩展,避免把 console.log、foo.json 这类代码误判成文件
|
|
1246
|
+
return FILE_DOC_EXT_RE.test(clean)
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function fileKindOf(value) {
|
|
1250
|
+
const clean = String(value || '').split(/[?#]/)[0]
|
|
1251
|
+
if (FILE_IMAGE_EXT_RE.test(clean)) return 'image'
|
|
1252
|
+
if (FILE_TEXT_EXT_RE.test(clean)) return 'text'
|
|
1253
|
+
return 'binary'
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
function fileBaseName(value) {
|
|
1257
|
+
const clean = String(value || '').replace(/[\\/]+$/, '')
|
|
1258
|
+
const parts = clean.split(/[\\/]/)
|
|
1259
|
+
return parts[parts.length - 1] || clean
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
function fileDirName(value) {
|
|
1263
|
+
const clean = String(value || '').replace(/[\\/]+$/, '')
|
|
1264
|
+
const idx = Math.max(clean.lastIndexOf('/'), clean.lastIndexOf('\\'))
|
|
1265
|
+
return idx > 0 ? clean.slice(0, idx) : ''
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
function fileExtOf(value) {
|
|
1269
|
+
const m = String(value || '').split(/[?#]/)[0].match(/\.([A-Za-z0-9]+)$/)
|
|
1270
|
+
return m ? m[1].toLowerCase() : ''
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
async function fetchFileInfo(path, base) {
|
|
1274
|
+
const q = new URLSearchParams()
|
|
1275
|
+
q.set('path', path)
|
|
1276
|
+
if (base) q.set('base', base)
|
|
1277
|
+
const res = await fetch(API + '/file?' + q.toString())
|
|
1278
|
+
const json = await res.json().catch(() => ({}))
|
|
1279
|
+
if (!res.ok || !json.ok) throw new Error(json.error || '读取失败')
|
|
1280
|
+
return json
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
async function runFileAction(path, base, action) {
|
|
1284
|
+
const res = await fetch(API + '/file-action', {
|
|
1285
|
+
method: 'POST',
|
|
1286
|
+
headers: { 'content-type': 'application/json' },
|
|
1287
|
+
body: JSON.stringify({ path, base, action }),
|
|
1288
|
+
})
|
|
1289
|
+
const json = await res.json().catch(() => ({}))
|
|
1290
|
+
if (!res.ok || !json.ok) throw new Error(json.error || '操作失败')
|
|
1291
|
+
return json
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
function fileGlyph(kind, color, size) {
|
|
1295
|
+
const sz = size || 16
|
|
1296
|
+
const common = {
|
|
1297
|
+
width: sz, height: sz, viewBox: '0 0 16 16', fill: 'none', stroke: color,
|
|
1298
|
+
strokeWidth: 1.4, strokeLinecap: 'round', strokeLinejoin: 'round', style: { flexShrink: 0 },
|
|
1299
|
+
}
|
|
1300
|
+
if (kind === 'image') {
|
|
1301
|
+
return h('svg', common,
|
|
1302
|
+
h('rect', { x: 2, y: 3, width: 12, height: 10, rx: 2 }),
|
|
1303
|
+
h('circle', { cx: 6, cy: 6.8, r: 1.1 }),
|
|
1304
|
+
h('path', { d: 'M3 12l3-2.8 2 1.8 2.4-2.2L13 12' }),
|
|
1305
|
+
)
|
|
1306
|
+
}
|
|
1307
|
+
if (kind === 'text') {
|
|
1308
|
+
return h('svg', common,
|
|
1309
|
+
h('path', { d: 'M4 2.2h5l3 3v8.3a.8.8 0 0 1-.8.8H4a.8.8 0 0 1-.8-.8V3a.8.8 0 0 1 .8-.8z' }),
|
|
1310
|
+
h('path', { d: 'M9 2.2v3h3' }),
|
|
1311
|
+
h('path', { d: 'M5.4 8.4h5.2M5.4 11h3.6' }),
|
|
1312
|
+
)
|
|
1313
|
+
}
|
|
1314
|
+
return h('svg', common,
|
|
1315
|
+
h('path', { d: 'M8 2.2 12.8 5v6L8 13.8 3.2 11V5z' }),
|
|
1316
|
+
h('path', { d: 'M3.4 5.2 8 8l4.6-2.8M8 8v5.6' }),
|
|
1317
|
+
)
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
function fileActionButton(title, onClick, icon, muted) {
|
|
1321
|
+
return h('button', {
|
|
1322
|
+
type: 'button', title, 'aria-label': title,
|
|
1323
|
+
onClick: (e) => { e.stopPropagation(); onClick() },
|
|
1324
|
+
onMouseDown: (e) => e.stopPropagation(),
|
|
1325
|
+
style: {
|
|
1326
|
+
width: 20, height: 20, flexShrink: 0, border: 'none', borderRadius: 5, padding: 0,
|
|
1327
|
+
background: 'transparent', cursor: 'pointer', display: 'inline-flex',
|
|
1328
|
+
alignItems: 'center', justifyContent: 'center',
|
|
1329
|
+
color: muted ? 'rgba(255,255,255,0.8)' : T.textMuted,
|
|
1330
|
+
},
|
|
1331
|
+
onMouseEnter: (e) => { e.currentTarget.style.background = muted ? 'rgba(255,255,255,0.18)' : 'rgba(16,41,66,0.08)' },
|
|
1332
|
+
onMouseLeave: (e) => { e.currentTarget.style.background = 'transparent' },
|
|
1333
|
+
}, icon)
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
function FilePreviewModal({ path, base, name, kind, initialInfo, onClose }) {
|
|
1337
|
+
const [info, setInfo] = useState(initialInfo || null)
|
|
1338
|
+
const [error, setError] = useState('')
|
|
1339
|
+
const [busy, setBusy] = useState(!initialInfo)
|
|
1340
|
+
const [copied, setCopied] = useState(false)
|
|
1341
|
+
|
|
1342
|
+
useEffect(() => {
|
|
1343
|
+
if (initialInfo) return
|
|
1344
|
+
let cancelled = false
|
|
1345
|
+
setBusy(true)
|
|
1346
|
+
fetchFileInfo(path, base)
|
|
1347
|
+
.then((data) => { if (!cancelled) { setInfo(data); setError('') } })
|
|
1348
|
+
.catch((e) => { if (!cancelled) setError(e.message || '读取失败') })
|
|
1349
|
+
.finally(() => { if (!cancelled) setBusy(false) })
|
|
1350
|
+
return () => { cancelled = true }
|
|
1351
|
+
}, [path, base, initialInfo])
|
|
1352
|
+
|
|
1353
|
+
const resolved = (info && info.path) || path
|
|
1354
|
+
const displayName = name || (info && info.name) || fileBaseName(path)
|
|
1355
|
+
const doAction = (action) => {
|
|
1356
|
+
runFileAction(path, base, action).catch((e) => setError(e.message || '操作失败'))
|
|
1357
|
+
}
|
|
1358
|
+
const copy = () => {
|
|
1359
|
+
try {
|
|
1360
|
+
navigator.clipboard.writeText(resolved).then(() => setCopied(true), () => {})
|
|
1361
|
+
setTimeout(() => setCopied(false), 1500)
|
|
1362
|
+
} catch (_e) { /* ignore */ }
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
const iconColor = T.textMuted
|
|
1366
|
+
const actionBtn = (label, onClick, glyph) => h('button', {
|
|
1367
|
+
type: 'button', onClick,
|
|
1368
|
+
style: {
|
|
1369
|
+
display: 'inline-flex', alignItems: 'center', gap: 5, height: 28, padding: '0 10px',
|
|
1370
|
+
borderRadius: 7, border: `1px solid ${T.border}`, background: T.card,
|
|
1371
|
+
color: T.text, fontSize: 12, cursor: 'pointer', fontFamily: 'inherit',
|
|
1372
|
+
},
|
|
1373
|
+
}, glyph, label)
|
|
1374
|
+
|
|
1375
|
+
return h('div', {
|
|
1376
|
+
onClick: onClose,
|
|
1377
|
+
style: {
|
|
1378
|
+
position: 'fixed', inset: 0, zIndex: 320, background: 'rgba(16,41,66,0.45)',
|
|
1379
|
+
display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 32,
|
|
1380
|
+
},
|
|
1381
|
+
},
|
|
1382
|
+
h('div', {
|
|
1383
|
+
onClick: (e) => e.stopPropagation(),
|
|
1384
|
+
style: {
|
|
1385
|
+
width: kind === 'image' ? 'auto' : 'min(860px, 84vw)',
|
|
1386
|
+
maxWidth: '86vw', maxHeight: '84vh', display: 'flex', flexDirection: 'column',
|
|
1387
|
+
background: T.card, color: T.text, borderRadius: 14, overflow: 'hidden',
|
|
1388
|
+
boxShadow: '0 24px 60px rgba(16,41,66,0.35)',
|
|
1389
|
+
},
|
|
1390
|
+
},
|
|
1391
|
+
h('div', {
|
|
1392
|
+
style: {
|
|
1393
|
+
display: 'flex', alignItems: 'center', gap: 8, padding: '10px 14px',
|
|
1394
|
+
borderBottom: `1px solid ${T.border}`, flexShrink: 0,
|
|
1395
|
+
},
|
|
1396
|
+
},
|
|
1397
|
+
fileGlyph(kind, iconColor, 18),
|
|
1398
|
+
h('div', { style: { minWidth: 0, flex: 1 } },
|
|
1399
|
+
h('div', { style: { fontSize: 13, fontWeight: 600, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, displayName),
|
|
1400
|
+
h('div', { title: resolved, style: { fontSize: 10.5, color: T.textMuted, fontFamily: 'ui-monospace, Menlo, monospace', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, resolved),
|
|
1401
|
+
),
|
|
1402
|
+
actionBtn('打开', () => doAction('open'), fileGlyph('binary', iconColor, 13)),
|
|
1403
|
+
actionBtn('定位', () => doAction('reveal'), h('svg', { width: 13, height: 13, viewBox: '0 0 16 16', fill: 'none', stroke: iconColor, strokeWidth: 1.4, strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
1404
|
+
h('path', { d: 'M2 4.5A1.5 1.5 0 0 1 3.5 3h2.2l1.3 1.6h5A1.5 1.5 0 0 1 13.5 6v6A1.5 1.5 0 0 1 12 13.5H3.5A1.5 1.5 0 0 1 2 12z' }))),
|
|
1405
|
+
actionBtn(copied ? '已复制' : '复制路径', copy, h('svg', { width: 13, height: 13, viewBox: '0 0 16 16', fill: 'none', stroke: iconColor, strokeWidth: 1.4, strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
1406
|
+
h('rect', { x: 5.5, y: 5.5, width: 8, height: 8, rx: 1.5 }), h('path', { d: 'M10.5 5.5V3.5A1 1 0 0 0 9.5 2.5h-6a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2' }))),
|
|
1407
|
+
h('button', {
|
|
1408
|
+
type: 'button', onClick: onClose, 'aria-label': '关闭',
|
|
1409
|
+
style: {
|
|
1410
|
+
width: 28, height: 28, border: 'none', borderRadius: 7, background: T.bg,
|
|
1411
|
+
color: T.textMuted, cursor: 'pointer', fontSize: 14, flexShrink: 0,
|
|
1412
|
+
},
|
|
1413
|
+
}, '✕'),
|
|
1414
|
+
),
|
|
1415
|
+
error
|
|
1416
|
+
? h('div', { style: { padding: 20, color: T.danger, fontSize: 13 } }, error)
|
|
1417
|
+
: busy
|
|
1418
|
+
? h('div', { style: { padding: 40, textAlign: 'center', color: T.textMuted, fontSize: 13 } }, '读取中…')
|
|
1419
|
+
: kind === 'image'
|
|
1420
|
+
? h('div', { style: { padding: 16, overflow: 'auto', background: '#F3F5F9' } },
|
|
1421
|
+
h('img', { src: info && info.dataUrl, alt: displayName, style: { display: 'block', maxWidth: '100%', maxHeight: '64vh', margin: '0 auto', borderRadius: 8, background: '#fff' } }))
|
|
1422
|
+
: kind === 'text'
|
|
1423
|
+
? h('div', { style: { flex: 1, minHeight: 0, overflow: 'auto', background: '#FBFCFE' } },
|
|
1424
|
+
/\.(md|markdown)$/i.test(displayName)
|
|
1425
|
+
? h('div', { style: { padding: '16px 20px', fontSize: 13.5 } }, renderRichText(info && info.text, {}))
|
|
1426
|
+
: h('pre', {
|
|
1427
|
+
style: {
|
|
1428
|
+
margin: 0, padding: '14px 18px', fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
1429
|
+
fontSize: 12.3, lineHeight: 1.6, whiteSpace: 'pre-wrap', wordBreak: 'break-word', color: T.text,
|
|
1430
|
+
},
|
|
1431
|
+
}, (info && info.text) || ''),
|
|
1432
|
+
info && info.truncated ? h('div', { style: { padding: '6px 18px 12px', fontSize: 11.5, color: T.warn } }, '文件较大,只预览了前 300KB') : null,
|
|
1433
|
+
)
|
|
1434
|
+
: h('div', { style: { padding: 30, textAlign: 'center', color: T.textMuted, fontSize: 13 } },
|
|
1435
|
+
'该类型不支持内嵌预览,可「打开」或「定位」。'),
|
|
1436
|
+
),
|
|
1437
|
+
)
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
// ===== 群附件(用户发送的图片 / 视频 / 文件)=====
|
|
1441
|
+
function formatFileSize(bytes) {
|
|
1442
|
+
const n = Number(bytes) || 0
|
|
1443
|
+
if (n < 1024) return n + ' B'
|
|
1444
|
+
if (n < 1024 * 1024) return (n / 1024).toFixed(1) + ' KB'
|
|
1445
|
+
if (n < 1024 * 1024 * 1024) return (n / (1024 * 1024)).toFixed(1) + ' MB'
|
|
1446
|
+
return (n / (1024 * 1024 * 1024)).toFixed(1) + ' GB'
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
function attachmentPreviewKind(att) {
|
|
1450
|
+
const ext = fileExtOf(att && att.name)
|
|
1451
|
+
if ((att && att.kind) === 'image' || FILE_IMAGE_EXT_RE.test('.' + ext)) return 'image'
|
|
1452
|
+
if ((att && att.kind) === 'video' || /^(mp4|mov|webm|m4v|avi|mkv|ogv)$/i.test(ext)) return 'video'
|
|
1453
|
+
if (ext === 'pdf') return 'pdf'
|
|
1454
|
+
if (FILE_TEXT_EXT_RE.test('.' + ext)) return 'text'
|
|
1455
|
+
return 'other'
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
function attachmentIconKind(att) {
|
|
1459
|
+
const kind = attachmentPreviewKind(att)
|
|
1460
|
+
if (kind === 'image') return 'image'
|
|
1461
|
+
if (kind === 'video') return 'video'
|
|
1462
|
+
if (kind === 'text') return 'text'
|
|
1463
|
+
return 'binary'
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
function AttachmentPreviewModal({ att, onClose }) {
|
|
1467
|
+
const kind = attachmentPreviewKind(att)
|
|
1468
|
+
const [text, setText] = useState('')
|
|
1469
|
+
const [error, setError] = useState('')
|
|
1470
|
+
const [copied, setCopied] = useState(false)
|
|
1471
|
+
|
|
1472
|
+
useEffect(() => {
|
|
1473
|
+
if (kind !== 'text') return
|
|
1474
|
+
let cancelled = false
|
|
1475
|
+
fetch(att.url)
|
|
1476
|
+
.then(r => r.text().then(body => { if (!r.ok) throw new Error('读取失败'); return body }))
|
|
1477
|
+
.then(body => { if (!cancelled) setText(body) })
|
|
1478
|
+
.catch(e => { if (!cancelled) setError(e.message || '读取失败') })
|
|
1479
|
+
return () => { cancelled = true }
|
|
1480
|
+
}, [att.url, kind])
|
|
1481
|
+
|
|
1482
|
+
const doAction = (action) => {
|
|
1483
|
+
if (!att.path) { setError('附件路径不可用'); return }
|
|
1484
|
+
runFileAction(att.path, '', action).catch(e => setError(e.message || '操作失败'))
|
|
1485
|
+
}
|
|
1486
|
+
const copy = () => {
|
|
1487
|
+
try {
|
|
1488
|
+
navigator.clipboard.writeText(att.path || att.name).then(() => setCopied(true), () => {})
|
|
1489
|
+
setTimeout(() => setCopied(false), 1500)
|
|
1490
|
+
} catch (_e) { /* ignore */ }
|
|
1491
|
+
}
|
|
1492
|
+
const iconColor = T.textMuted
|
|
1493
|
+
const actionBtn = (label, onClick, glyph) => h('button', {
|
|
1494
|
+
type: 'button', onClick,
|
|
1495
|
+
style: {
|
|
1496
|
+
display: 'inline-flex', alignItems: 'center', gap: 5, height: 28, padding: '0 10px',
|
|
1497
|
+
borderRadius: 7, border: `1px solid ${T.border}`, background: T.card,
|
|
1498
|
+
color: T.text, fontSize: 12, cursor: 'pointer', fontFamily: 'inherit',
|
|
1499
|
+
},
|
|
1500
|
+
}, glyph, label)
|
|
1501
|
+
const downloadBtn = h('a', {
|
|
1502
|
+
href: att.url + '?download=1', download: att.name,
|
|
1503
|
+
style: {
|
|
1504
|
+
display: 'inline-flex', alignItems: 'center', gap: 5, height: 28, padding: '0 10px',
|
|
1505
|
+
borderRadius: 7, border: `1px solid ${T.border}`, background: T.card,
|
|
1506
|
+
color: T.text, fontSize: 12, textDecoration: 'none',
|
|
1507
|
+
},
|
|
1508
|
+
}, '下载')
|
|
1509
|
+
|
|
1510
|
+
return h('div', {
|
|
1511
|
+
onClick: onClose,
|
|
1512
|
+
style: {
|
|
1513
|
+
position: 'fixed', inset: 0, zIndex: 320, background: 'rgba(16,41,66,0.45)',
|
|
1514
|
+
display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 32,
|
|
1515
|
+
},
|
|
1516
|
+
},
|
|
1517
|
+
h('div', {
|
|
1518
|
+
onClick: (e) => e.stopPropagation(),
|
|
1519
|
+
style: {
|
|
1520
|
+
width: kind === 'image' || kind === 'video' ? 'auto' : 'min(860px, 84vw)',
|
|
1521
|
+
maxWidth: '88vw', maxHeight: '86vh', display: 'flex', flexDirection: 'column',
|
|
1522
|
+
background: T.card, color: T.text, borderRadius: 14, overflow: 'hidden',
|
|
1523
|
+
boxShadow: '0 24px 60px rgba(16,41,66,0.35)',
|
|
1524
|
+
},
|
|
1525
|
+
},
|
|
1526
|
+
h('div', {
|
|
1527
|
+
style: { display: 'flex', alignItems: 'center', gap: 8, padding: '10px 14px', borderBottom: `1px solid ${T.border}`, flexShrink: 0 },
|
|
1528
|
+
},
|
|
1529
|
+
fileGlyph(attachmentIconKind(att), iconColor, 18),
|
|
1530
|
+
h('div', { style: { minWidth: 0, flex: 1 } },
|
|
1531
|
+
h('div', { style: { fontSize: 13, fontWeight: 600, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, att.name),
|
|
1532
|
+
h('div', { style: { fontSize: 10.5, color: T.textMuted } }, formatFileSize(att.size) + (att.mime ? ' · ' + att.mime : '')),
|
|
1533
|
+
),
|
|
1534
|
+
actionBtn('打开', () => doAction('open'), fileGlyph('binary', iconColor, 13)),
|
|
1535
|
+
actionBtn('定位', () => doAction('reveal'), h('svg', { width: 13, height: 13, viewBox: '0 0 16 16', fill: 'none', stroke: iconColor, strokeWidth: 1.4, strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
1536
|
+
h('path', { d: 'M2 4.5A1.5 1.5 0 0 1 3.5 3h2.2l1.3 1.6h5A1.5 1.5 0 0 1 13.5 6v6A1.5 1.5 0 0 1 12 13.5H3.5A1.5 1.5 0 0 1 2 12z' }))),
|
|
1537
|
+
actionBtn(copied ? '已复制' : '复制路径', copy, h('svg', { width: 13, height: 13, viewBox: '0 0 16 16', fill: 'none', stroke: iconColor, strokeWidth: 1.4, strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
1538
|
+
h('rect', { x: 5.5, y: 5.5, width: 8, height: 8, rx: 1.5 }), h('path', { d: 'M10.5 5.5V3.5A1 1 0 0 0 9.5 2.5h-6a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2' }))),
|
|
1539
|
+
downloadBtn,
|
|
1540
|
+
h('button', {
|
|
1541
|
+
type: 'button', onClick: onClose, 'aria-label': '关闭',
|
|
1542
|
+
style: { width: 28, height: 28, border: 'none', borderRadius: 7, background: T.bg, color: T.textMuted, cursor: 'pointer', fontSize: 14, flexShrink: 0 },
|
|
1543
|
+
}, '✕'),
|
|
1544
|
+
),
|
|
1545
|
+
error ? h('div', { style: { padding: 16, color: T.danger, fontSize: 12.5 } }, error) : null,
|
|
1546
|
+
kind === 'image'
|
|
1547
|
+
? h('div', { style: { padding: 16, overflow: 'auto', background: '#F3F5F9' } },
|
|
1548
|
+
h('img', { src: att.url, alt: att.name, style: { display: 'block', maxWidth: '100%', maxHeight: '68vh', margin: '0 auto', borderRadius: 8, background: '#fff' } }))
|
|
1549
|
+
: kind === 'video'
|
|
1550
|
+
? h('div', { style: { padding: 16, background: '#0B1220', display: 'flex', justifyContent: 'center' } },
|
|
1551
|
+
h('video', { src: att.url, controls: true, autoPlay: true, style: { maxWidth: '100%', maxHeight: '68vh', borderRadius: 8 } }))
|
|
1552
|
+
: kind === 'pdf'
|
|
1553
|
+
? h('iframe', { src: att.url, title: att.name, style: { width: '80vw', height: '70vh', border: 0, background: '#fff' } })
|
|
1554
|
+
: kind === 'text'
|
|
1555
|
+
? h('div', { style: { flex: 1, minHeight: 0, overflow: 'auto', background: '#FBFCFE' } },
|
|
1556
|
+
/\.(md|markdown)$/i.test(att.name)
|
|
1557
|
+
? h('div', { style: { padding: '16px 20px', fontSize: 13.5 } }, renderRichText(text, {}))
|
|
1558
|
+
: h('pre', {
|
|
1559
|
+
style: {
|
|
1560
|
+
margin: 0, padding: '14px 18px', fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
1561
|
+
fontSize: 12.3, lineHeight: 1.6, whiteSpace: 'pre-wrap', wordBreak: 'break-word', color: T.text,
|
|
1562
|
+
},
|
|
1563
|
+
}, text),
|
|
1564
|
+
)
|
|
1565
|
+
: h('div', { style: { padding: 30, textAlign: 'center', color: T.textMuted, fontSize: 13 } },
|
|
1566
|
+
'该类型不支持内嵌预览,可「打开」或「下载」。'),
|
|
1567
|
+
),
|
|
1568
|
+
)
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
function MessageAttachments({ attachments, muted }) {
|
|
1572
|
+
const [preview, setPreview] = useState(null)
|
|
1573
|
+
if (!attachments || attachments.length === 0) return null
|
|
1574
|
+
const subColor = muted ? 'rgba(255,255,255,0.75)' : T.textMuted
|
|
1575
|
+
const cardBg = muted ? 'rgba(255,255,255,0.16)' : 'rgba(16,41,66,0.04)'
|
|
1576
|
+
const cardBorder = muted ? 'rgba(255,255,255,0.32)' : T.border
|
|
1577
|
+
|
|
1578
|
+
return h(React.Fragment, null,
|
|
1579
|
+
h('div', { style: { display: 'flex', flexWrap: 'wrap', gap: 8, marginTop: 8 } },
|
|
1580
|
+
attachments.map((att) => {
|
|
1581
|
+
const kind = attachmentPreviewKind(att)
|
|
1582
|
+
if (kind === 'image') {
|
|
1583
|
+
return h('span', {
|
|
1584
|
+
key: att.id,
|
|
1585
|
+
onClick: () => setPreview(att),
|
|
1586
|
+
title: att.name + ' — 点击查看大图',
|
|
1587
|
+
style: {
|
|
1588
|
+
position: 'relative', display: 'inline-flex', width: 168, height: 108,
|
|
1589
|
+
borderRadius: 9, overflow: 'hidden', border: `1px solid ${cardBorder}`,
|
|
1590
|
+
background: '#F3F5F9', cursor: 'zoom-in',
|
|
1591
|
+
},
|
|
1592
|
+
onMouseEnter: (e) => { e.currentTarget.style.borderColor = muted ? 'rgba(255,255,255,0.6)' : T.accent },
|
|
1593
|
+
onMouseLeave: (e) => { e.currentTarget.style.borderColor = cardBorder },
|
|
1594
|
+
},
|
|
1595
|
+
h('img', { src: att.url, alt: att.name, loading: 'lazy', style: { width: '100%', height: '100%', objectFit: 'cover', display: 'block' } }),
|
|
1596
|
+
h('span', {
|
|
1597
|
+
style: {
|
|
1598
|
+
position: 'absolute', left: 0, right: 0, bottom: 0, padding: '2px 6px',
|
|
1599
|
+
background: 'rgba(16,41,66,0.55)', color: '#fff', fontSize: 10.5,
|
|
1600
|
+
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
|
1601
|
+
},
|
|
1602
|
+
}, att.name),
|
|
1603
|
+
)
|
|
1604
|
+
}
|
|
1605
|
+
if (kind === 'video') {
|
|
1606
|
+
return h('span', {
|
|
1607
|
+
key: att.id,
|
|
1608
|
+
onClick: () => setPreview(att),
|
|
1609
|
+
title: att.name + ' — 点击播放',
|
|
1610
|
+
style: {
|
|
1611
|
+
position: 'relative', display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
|
|
1612
|
+
width: 200, height: 112, borderRadius: 9, overflow: 'hidden',
|
|
1613
|
+
background: 'linear-gradient(135deg,#111827,#334155)', border: `1px solid ${cardBorder}`,
|
|
1614
|
+
cursor: 'pointer',
|
|
1615
|
+
},
|
|
1616
|
+
},
|
|
1617
|
+
h('span', {
|
|
1618
|
+
style: {
|
|
1619
|
+
width: 40, height: 40, borderRadius: '50%', background: 'rgba(255,255,255,0.9)',
|
|
1620
|
+
display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#111827',
|
|
1621
|
+
fontSize: 16, paddingLeft: 3,
|
|
1622
|
+
},
|
|
1623
|
+
}, '▶'),
|
|
1624
|
+
h('span', {
|
|
1625
|
+
style: {
|
|
1626
|
+
position: 'absolute', left: 0, right: 0, bottom: 0, padding: '2px 6px',
|
|
1627
|
+
background: 'rgba(0,0,0,0.55)', color: '#fff', fontSize: 10.5,
|
|
1628
|
+
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
|
1629
|
+
},
|
|
1630
|
+
}, att.name + ' · ' + formatFileSize(att.size)),
|
|
1631
|
+
)
|
|
1632
|
+
}
|
|
1633
|
+
return h('span', {
|
|
1634
|
+
key: att.id,
|
|
1635
|
+
onClick: () => setPreview(att),
|
|
1636
|
+
title: att.name + ' — 点击查看',
|
|
1637
|
+
style: {
|
|
1638
|
+
display: 'inline-flex', alignItems: 'center', gap: 8, height: 38,
|
|
1639
|
+
padding: '0 10px', borderRadius: 9, border: `1px solid ${cardBorder}`,
|
|
1640
|
+
background: cardBg, color: muted ? '#fff' : T.text, cursor: 'pointer',
|
|
1641
|
+
maxWidth: 320,
|
|
1642
|
+
},
|
|
1643
|
+
onMouseEnter: (e) => { e.currentTarget.style.borderColor = muted ? 'rgba(255,255,255,0.6)' : T.accent },
|
|
1644
|
+
onMouseLeave: (e) => { e.currentTarget.style.borderColor = cardBorder },
|
|
1645
|
+
},
|
|
1646
|
+
fileGlyph(attachmentIconKind(att), muted ? 'rgba(255,255,255,0.85)' : T.accent, 16),
|
|
1647
|
+
h('span', { style: { minWidth: 0, display: 'flex', flexDirection: 'column' } },
|
|
1648
|
+
h('span', { style: { fontSize: 12.2, fontWeight: 500, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, att.name),
|
|
1649
|
+
h('span', { style: { fontSize: 10.5, color: subColor } }, formatFileSize(att.size)),
|
|
1650
|
+
),
|
|
1651
|
+
)
|
|
1652
|
+
}),
|
|
1653
|
+
),
|
|
1654
|
+
preview ? h(AttachmentPreviewModal, { att: preview, onClose: () => setPreview(null) }) : null,
|
|
1655
|
+
)
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
function FileRefCard({ path, label, base, muted }) {
|
|
1659
|
+
const kind = fileKindOf(path)
|
|
1660
|
+
const name = label || fileBaseName(path)
|
|
1661
|
+
const dir = fileDirName(path)
|
|
1662
|
+
const [info, setInfo] = useState(null)
|
|
1663
|
+
const [error, setError] = useState('')
|
|
1664
|
+
const [busy, setBusy] = useState(kind === 'image')
|
|
1665
|
+
const [preview, setPreview] = useState(false)
|
|
1666
|
+
|
|
1667
|
+
useEffect(() => {
|
|
1668
|
+
if (kind !== 'image') return
|
|
1669
|
+
let cancelled = false
|
|
1670
|
+
setBusy(true)
|
|
1671
|
+
fetchFileInfo(path, base)
|
|
1672
|
+
.then((data) => { if (!cancelled) { setInfo(data); setError('') } })
|
|
1673
|
+
.catch((e) => { if (!cancelled) setError(e.message || '读取失败') })
|
|
1674
|
+
.finally(() => { if (!cancelled) setBusy(false) })
|
|
1675
|
+
return () => { cancelled = true }
|
|
1676
|
+
}, [path, base, kind])
|
|
1677
|
+
|
|
1678
|
+
const color = muted ? '#fff' : T.text
|
|
1679
|
+
const subColor = muted ? 'rgba(255,255,255,0.75)' : T.textMuted
|
|
1680
|
+
const cardBg = muted ? 'rgba(255,255,255,0.16)' : 'rgba(16,41,66,0.04)'
|
|
1681
|
+
const cardBorder = muted ? 'rgba(255,255,255,0.32)' : T.border
|
|
1682
|
+
const stop = (e) => e.stopPropagation()
|
|
1683
|
+
|
|
1684
|
+
const openIt = () => {
|
|
1685
|
+
if (kind === 'binary') { runFileAction(path, base, 'open').catch((e) => setError(e.message || '打开失败')); return }
|
|
1686
|
+
setPreview(true)
|
|
1687
|
+
}
|
|
1688
|
+
const doAction = (action) => { runFileAction(path, base, action).catch((e) => setError(e.message || '操作失败')) }
|
|
1689
|
+
const actionIcons = () => h(React.Fragment, null,
|
|
1690
|
+
fileActionButton('打开', () => doAction('open'), h('svg', { width: 13, height: 13, viewBox: '0 0 16 16', fill: 'none', stroke: 'currentColor', strokeWidth: 1.5, strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
1691
|
+
h('path', { d: 'M6 3h7v7M13 3 7.5 8.5M4 4.5H3.2a.9.9 0 0 0-.9.9v7.4a.9.9 0 0 0 .9.9h7.4a.9.9 0 0 0 .9-.9V12' })), muted),
|
|
1692
|
+
fileActionButton('在 Finder 中显示', () => doAction('reveal'), h('svg', { width: 13, height: 13, viewBox: '0 0 16 16', fill: 'none', stroke: 'currentColor', strokeWidth: 1.5, strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
1693
|
+
h('path', { d: 'M2 4.5A1.5 1.5 0 0 1 3.5 3h2.2l1.3 1.6h5A1.5 1.5 0 0 1 13.5 6v6A1.5 1.5 0 0 1 12 13.5H3.5A1.5 1.5 0 0 1 2 12z' })), muted),
|
|
1694
|
+
)
|
|
1695
|
+
|
|
1696
|
+
if (kind === 'image') {
|
|
1697
|
+
if (busy) {
|
|
1698
|
+
return h('span', {
|
|
1699
|
+
style: {
|
|
1700
|
+
display: 'inline-flex', alignItems: 'center', gap: 6, padding: '4px 9px', margin: '3px 0',
|
|
1701
|
+
borderRadius: 8, border: `1px solid ${cardBorder}`, background: cardBg,
|
|
1702
|
+
color: subColor, fontSize: 12,
|
|
1703
|
+
},
|
|
1704
|
+
}, fileGlyph('image', subColor, 14), name, '预览中…')
|
|
1705
|
+
}
|
|
1706
|
+
if (!info || !info.dataUrl) {
|
|
1707
|
+
return h('span', {
|
|
1708
|
+
title: error || '无法预览',
|
|
1709
|
+
style: {
|
|
1710
|
+
display: 'inline-flex', alignItems: 'center', gap: 6, padding: '4px 9px', margin: '3px 0',
|
|
1711
|
+
borderRadius: 8, border: `1px solid ${cardBorder}`, background: cardBg,
|
|
1712
|
+
color: subColor, fontSize: 12,
|
|
1713
|
+
},
|
|
1714
|
+
}, fileGlyph('image', subColor, 14), name, error ? '(' + error + ')' : '')
|
|
1715
|
+
}
|
|
1716
|
+
return h(React.Fragment, null,
|
|
1717
|
+
h('span', {
|
|
1718
|
+
onClick: openIt,
|
|
1719
|
+
title: name + ' — 点击查看大图',
|
|
1720
|
+
style: {
|
|
1721
|
+
display: 'inline-flex', position: 'relative', width: 168, height: 108,
|
|
1722
|
+
margin: '3px 6px 3px 0', borderRadius: 9, overflow: 'hidden',
|
|
1723
|
+
border: `1px solid ${cardBorder}`, background: '#F3F5F9',
|
|
1724
|
+
verticalAlign: 'middle', cursor: 'zoom-in',
|
|
1725
|
+
},
|
|
1726
|
+
onMouseEnter: (e) => { e.currentTarget.style.borderColor = muted ? 'rgba(255,255,255,0.6)' : T.accent },
|
|
1727
|
+
onMouseLeave: (e) => { e.currentTarget.style.borderColor = cardBorder },
|
|
1728
|
+
},
|
|
1729
|
+
h('img', {
|
|
1730
|
+
src: info.dataUrl, alt: name, loading: 'lazy',
|
|
1731
|
+
style: { width: '100%', height: '100%', objectFit: 'cover', display: 'block' },
|
|
1732
|
+
}),
|
|
1733
|
+
h('span', {
|
|
1734
|
+
style: {
|
|
1735
|
+
position: 'absolute', left: 0, right: 0, bottom: 0, padding: '2px 6px',
|
|
1736
|
+
background: 'rgba(16,41,66,0.55)', color: '#fff', fontSize: 10.5,
|
|
1737
|
+
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
|
1738
|
+
},
|
|
1739
|
+
}, name),
|
|
1740
|
+
),
|
|
1741
|
+
preview ? h(FilePreviewModal, { path, base, name, kind: 'image', initialInfo: info, onClose: () => setPreview(false) }) : null,
|
|
1742
|
+
)
|
|
1743
|
+
}
|
|
1744
|
+
|
|
1745
|
+
return h(React.Fragment, null,
|
|
1746
|
+
h('span', {
|
|
1747
|
+
onClick: openIt,
|
|
1748
|
+
title: (kind === 'text' ? '点击预览' : '点击打开') + ':' + path,
|
|
1749
|
+
style: {
|
|
1750
|
+
display: 'inline-flex', alignItems: 'center', gap: 6, maxWidth: '100%',
|
|
1751
|
+
padding: '2px 4px 2px 7px', margin: '2px 0', borderRadius: 7,
|
|
1752
|
+
border: `1px solid ${cardBorder}`, background: cardBg, cursor: 'pointer',
|
|
1753
|
+
verticalAlign: 'middle', color,
|
|
1754
|
+
},
|
|
1755
|
+
onMouseEnter: (e) => { e.currentTarget.style.borderColor = muted ? 'rgba(255,255,255,0.6)' : T.accent },
|
|
1756
|
+
onMouseLeave: (e) => { e.currentTarget.style.borderColor = cardBorder },
|
|
1757
|
+
},
|
|
1758
|
+
fileGlyph(kind, muted ? 'rgba(255,255,255,0.85)' : T.accent, 14),
|
|
1759
|
+
h('span', {
|
|
1760
|
+
style: {
|
|
1761
|
+
maxWidth: 320, fontSize: 12.2, fontWeight: 500, overflow: 'hidden',
|
|
1762
|
+
textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
|
1763
|
+
},
|
|
1764
|
+
}, name),
|
|
1765
|
+
dir ? h('span', {
|
|
1766
|
+
title: path,
|
|
1767
|
+
style: {
|
|
1768
|
+
maxWidth: 200, fontSize: 10.5, color: subColor, overflow: 'hidden',
|
|
1769
|
+
textOverflow: 'ellipsis', whiteSpace: 'nowrap', direction: 'rtl', textAlign: 'left',
|
|
1770
|
+
},
|
|
1771
|
+
}, dir) : null,
|
|
1772
|
+
h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: 1 }, onClick: stop }, actionIcons()),
|
|
1773
|
+
),
|
|
1774
|
+
error ? h('span', { style: { fontSize: 10.5, color: muted ? '#fff' : T.danger, marginLeft: 4 } }, error) : null,
|
|
1775
|
+
preview ? h(FilePreviewModal, { path, base, name, kind, onClose: () => setPreview(false) }) : null,
|
|
1776
|
+
)
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1201
1779
|
// ---- 群聊欢迎页(没选中群时的主区域) ----
|
|
1202
1780
|
// 群列表在左栏常驻,这里只负责「进来先看到什么」,并给出新建群 / 花名册入口。
|
|
1203
1781
|
function GroupchatWelcome({ lang, btn, setView, groups, loading }) {
|
|
@@ -1862,6 +2440,55 @@ window.__ModuleLoader__.load({
|
|
|
1862
2440
|
|
|
1863
2441
|
// 草稿从记忆恢复:切走再回来,没发出去的字还在
|
|
1864
2442
|
const [draft, setDraft] = useState(() => panelMemo.drafts[group.id] || '')
|
|
2443
|
+
// 待发送附件:选/拖/粘贴后立刻上传,发送时只带 attachment id
|
|
2444
|
+
const [pending, setPending] = useState([])
|
|
2445
|
+
const [dragOver, setDragOver] = useState(false)
|
|
2446
|
+
const fileInputRef = useRef(null)
|
|
2447
|
+
const updatePending = (localId, patch) => setPending(list => list.map(x => x.localId === localId ? { ...x, ...patch } : x))
|
|
2448
|
+
const removePending = (localId) => setPending(list => list.filter(x => x.localId !== localId))
|
|
2449
|
+
const uploadOne = (file) => {
|
|
2450
|
+
const localId = 'p' + Date.now() + Math.random().toString(36).slice(2, 7)
|
|
2451
|
+
const name = file.name || ('粘贴文件-' + Date.now())
|
|
2452
|
+
const mime = file.type || 'application/octet-stream'
|
|
2453
|
+
const kind = mime.startsWith('image/') ? 'image' : mime.startsWith('video/') ? 'video' : 'file'
|
|
2454
|
+
setPending(list => [...list, { localId, name, size: file.size, mime, kind, status: 'uploading', progress: 0 }])
|
|
2455
|
+
const query = new URLSearchParams({ name, mime })
|
|
2456
|
+
const xhr = new XMLHttpRequest()
|
|
2457
|
+
xhr.open('POST', API + '/upload?' + query.toString())
|
|
2458
|
+
xhr.upload.onprogress = (e) => {
|
|
2459
|
+
if (!e.lengthComputable) return
|
|
2460
|
+
updatePending(localId, { progress: Math.max(1, Math.round((e.loaded / e.total) * 100)) })
|
|
2461
|
+
}
|
|
2462
|
+
xhr.onerror = () => updatePending(localId, { status: 'error', error: '网络错误' })
|
|
2463
|
+
xhr.onload = () => {
|
|
2464
|
+
let json = {}
|
|
2465
|
+
try { json = JSON.parse(xhr.responseText || '{}') } catch (_e) { json = {} }
|
|
2466
|
+
if (xhr.status >= 200 && xhr.status < 300 && json.ok && json.attachment) {
|
|
2467
|
+
updatePending(localId, {
|
|
2468
|
+
status: 'done', progress: 100, id: json.attachment.id, url: json.attachment.url,
|
|
2469
|
+
kind: json.attachment.kind, size: json.attachment.size,
|
|
2470
|
+
})
|
|
2471
|
+
} else {
|
|
2472
|
+
updatePending(localId, { status: 'error', error: json.error || ('上传失败(' + xhr.status + ')') })
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
xhr.send(file)
|
|
2476
|
+
}
|
|
2477
|
+
const uploadFiles = (files) => {
|
|
2478
|
+
for (const file of Array.from(files || [])) {
|
|
2479
|
+
if (!file || !file.size) continue
|
|
2480
|
+
if (file.size > 200 * 1024 * 1024) {
|
|
2481
|
+
const localId = 'p' + Date.now() + Math.random().toString(36).slice(2, 7)
|
|
2482
|
+
setPending(prev => [...prev, { localId, name: file.name, size: file.size, status: 'error', error: '超过 200MB' }])
|
|
2483
|
+
continue
|
|
2484
|
+
}
|
|
2485
|
+
uploadOne(file)
|
|
2486
|
+
}
|
|
2487
|
+
}
|
|
2488
|
+
const handlePaste = (e) => {
|
|
2489
|
+
const files = e.clipboardData && e.clipboardData.files
|
|
2490
|
+
if (files && files.length) { e.preventDefault(); uploadFiles(files) }
|
|
2491
|
+
}
|
|
1865
2492
|
const [showMembers, setShowMembers] = useState(false)
|
|
1866
2493
|
const [showBoard, setShowBoard] = useState(false)
|
|
1867
2494
|
const [addIds, setAddIds] = useState([])
|
|
@@ -2125,12 +2752,19 @@ window.__ModuleLoader__.load({
|
|
|
2125
2752
|
}
|
|
2126
2753
|
|
|
2127
2754
|
const send = () => act(async () => {
|
|
2128
|
-
if (
|
|
2755
|
+
if (pending.some(p => p.status === 'uploading')) return
|
|
2756
|
+
const ready = pending.filter(p => p.status === 'done' && p.id)
|
|
2757
|
+
if (!draft.trim() && ready.length === 0) return
|
|
2129
2758
|
await api(`/groups/${group.id}/post`, {
|
|
2130
2759
|
method: 'POST',
|
|
2131
2760
|
headers: { 'content-type': 'application/json' },
|
|
2132
|
-
body: JSON.stringify({
|
|
2761
|
+
body: JSON.stringify({
|
|
2762
|
+
content: draft.trim(),
|
|
2763
|
+
sessionId: getCurrentSessionId(),
|
|
2764
|
+
...(ready.length ? { attachments: ready.map(p => p.id) } : {}),
|
|
2765
|
+
}),
|
|
2133
2766
|
})
|
|
2767
|
+
setPending([])
|
|
2134
2768
|
setDraft('')
|
|
2135
2769
|
panelMemo.drafts[group.id] = ''
|
|
2136
2770
|
// 自己发的消息要滚到底部:清掉「向上翻」标记,让新消息到达时自动滚动
|
|
@@ -2305,6 +2939,9 @@ window.__ModuleLoader__.load({
|
|
|
2305
2939
|
? h('div', { style: { color: T.textMuted, textAlign: 'center', padding: 40 } }, '—')
|
|
2306
2940
|
: displayMessages.map(m => {
|
|
2307
2941
|
const isMe = m.authorRole === 'user'
|
|
2942
|
+
const authorInfo = memberOf(m.authorName)
|
|
2943
|
+
const authorBase = (authorInfo && authorInfo.sessionInfo && authorInfo.sessionInfo.cwd)
|
|
2944
|
+
|| ((memberOf(group.sAgentName) || {}).sessionInfo || {}).cwd || ''
|
|
2308
2945
|
const isNotify = m.kind === 'notify'
|
|
2309
2946
|
const isSystem = m.kind === 'system'
|
|
2310
2947
|
const roleColor = { manager: '#764ba2', dev: '#2E6BE6', product: '#1E7D46', test: '#8A5A12' }[m.authorRole] || T.accent
|
|
@@ -2365,15 +3002,76 @@ window.__ModuleLoader__.load({
|
|
|
2365
3002
|
...(isSystem ? { textAlign: 'center', fontSize: 11.5, color: T.textMuted } : {}),
|
|
2366
3003
|
},
|
|
2367
3004
|
// 正文走轻量 Markdown 渲染:保留段落、换行、标题、列表、粗体
|
|
2368
|
-
}, renderRichText(m.content, { muted: isMe }) || m.content
|
|
3005
|
+
}, renderRichText(m.content, { muted: isMe, base: authorBase }) || m.content,
|
|
3006
|
+
m.attachments && m.attachments.length
|
|
3007
|
+
? h(MessageAttachments, { attachments: m.attachments, muted: isMe })
|
|
3008
|
+
: null),
|
|
2369
3009
|
),
|
|
2370
3010
|
)
|
|
2371
3011
|
}),
|
|
2372
3012
|
),
|
|
2373
|
-
// 输入区(带 @
|
|
2374
|
-
h('div', {
|
|
2375
|
-
|
|
2376
|
-
|
|
3013
|
+
// 输入区(带 @ 成员快捷选择 + 附件)
|
|
3014
|
+
h('div', {
|
|
3015
|
+
onDragOver: (e) => { e.preventDefault(); if (!dragOver) setDragOver(true) },
|
|
3016
|
+
onDragLeave: (e) => { if (!e.currentTarget.contains(e.relatedTarget)) setDragOver(false) },
|
|
3017
|
+
onDrop: (e) => {
|
|
3018
|
+
e.preventDefault()
|
|
3019
|
+
setDragOver(false)
|
|
3020
|
+
if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length) uploadFiles(e.dataTransfer.files)
|
|
3021
|
+
},
|
|
3022
|
+
style: { padding: '12px 20px 14px', borderTop: `1px solid ${T.border}`, background: T.card, flexShrink: 0, position: 'relative' },
|
|
3023
|
+
},
|
|
3024
|
+
dragOver ? h('div', {
|
|
3025
|
+
style: {
|
|
3026
|
+
position: 'absolute', inset: 6, borderRadius: 12, border: `2px dashed ${T.accent}`,
|
|
3027
|
+
background: T.accentSoft, color: T.accent, fontSize: 13, fontWeight: 500, zIndex: 5,
|
|
3028
|
+
display: 'flex', alignItems: 'center', justifyContent: 'center', pointerEvents: 'none',
|
|
3029
|
+
},
|
|
3030
|
+
}, '松开鼠标上传附件') : null,
|
|
3031
|
+
pending.length ? h('div', { style: { display: 'flex', flexWrap: 'wrap', gap: 8, marginBottom: 8 } },
|
|
3032
|
+
pending.map(p => h('div', {
|
|
3033
|
+
key: p.localId,
|
|
3034
|
+
style: {
|
|
3035
|
+
display: 'flex', alignItems: 'center', gap: 8, maxWidth: 300,
|
|
3036
|
+
padding: '5px 8px 5px 10px', borderRadius: 9,
|
|
3037
|
+
border: `1px solid ${p.status === 'error' ? T.danger : T.border}`,
|
|
3038
|
+
background: p.status === 'error' ? 'rgba(192,57,43,0.06)' : T.bg,
|
|
3039
|
+
fontSize: 12, color: T.text,
|
|
3040
|
+
},
|
|
3041
|
+
},
|
|
3042
|
+
fileGlyph(p.status === 'error' ? 'binary' : (p.kind === 'image' ? 'image' : 'text'), p.status === 'error' ? T.danger : T.accent, 15),
|
|
3043
|
+
h('div', { style: { minWidth: 0, flex: 1 } },
|
|
3044
|
+
h('div', { style: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, p.name),
|
|
3045
|
+
p.status === 'uploading'
|
|
3046
|
+
? h('div', { style: { marginTop: 3, height: 3, borderRadius: 2, background: 'rgba(16,41,66,0.1)', overflow: 'hidden' } },
|
|
3047
|
+
h('div', { style: { height: '100%', width: (p.progress || 0) + '%', background: T.accent, transition: 'width .2s ease' } }))
|
|
3048
|
+
: h('div', { style: { fontSize: 10.5, color: p.status === 'error' ? T.danger : T.textMuted } },
|
|
3049
|
+
p.status === 'error' ? (p.error || '上传失败') : formatFileSize(p.size) + ' · 已上传'),
|
|
3050
|
+
),
|
|
3051
|
+
h('button', {
|
|
3052
|
+
type: 'button', title: '移除', onClick: () => removePending(p.localId),
|
|
3053
|
+
style: { width: 20, height: 20, border: 'none', borderRadius: 5, background: 'transparent', color: T.textMuted, cursor: 'pointer', flexShrink: 0 },
|
|
3054
|
+
}, '✕'),
|
|
3055
|
+
)),
|
|
3056
|
+
) : null,
|
|
3057
|
+
h('div', { style: { display: 'flex', gap: 10, alignItems: 'flex-end' } },
|
|
3058
|
+
h('button', {
|
|
3059
|
+
type: 'button', title: '添加附件(图片 / 视频 / 文件)', 'aria-label': '添加附件',
|
|
3060
|
+
onClick: () => { if (fileInputRef.current) fileInputRef.current.click() },
|
|
3061
|
+
style: {
|
|
3062
|
+
width: 38, height: 38, flexShrink: 0, borderRadius: 10, border: `1.5px solid ${T.border}`,
|
|
3063
|
+
background: T.bg, color: T.textMuted, cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
3064
|
+
},
|
|
3065
|
+
onMouseEnter: (e) => { e.currentTarget.style.color = T.accent; e.currentTarget.style.borderColor = T.accent },
|
|
3066
|
+
onMouseLeave: (e) => { e.currentTarget.style.color = T.textMuted; e.currentTarget.style.borderColor = T.border },
|
|
3067
|
+
}, h('svg', { width: 17, height: 17, viewBox: '0 0 20 20', fill: 'none', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round' },
|
|
3068
|
+
h('path', { d: 'M12.8 6.2 7 12a2.1 2.1 0 0 0 3 3l6.2-6.2a3.8 3.8 0 0 0-5.4-5.4L4.6 9.6a5.4 5.4 0 0 0 7.6 7.6l5-5' }))),
|
|
3069
|
+
h('input', {
|
|
3070
|
+
ref: fileInputRef, type: 'file', multiple: true, style: { display: 'none' },
|
|
3071
|
+
onChange: (e) => { uploadFiles(e.target.files); e.target.value = '' },
|
|
3072
|
+
}),
|
|
3073
|
+
h('div', { style: { flex: 1, position: 'relative' } },
|
|
3074
|
+
input(draft, handleDraftChange, lang.placeholder, undefined, handleKeyDown, undefined, { ...composerCompositionProps, onPaste: handlePaste }),
|
|
2377
3075
|
// @ 成员快捷选择弹层
|
|
2378
3076
|
showMention && mentionMembers.length > 0 ? h('div', {
|
|
2379
3077
|
style: {
|
|
@@ -2399,7 +3097,8 @@ window.__ModuleLoader__.load({
|
|
|
2399
3097
|
)),
|
|
2400
3098
|
) : null,
|
|
2401
3099
|
),
|
|
2402
|
-
|
|
3100
|
+
btn(lang.send, send),
|
|
3101
|
+
),
|
|
2403
3102
|
),
|
|
2404
3103
|
// 成员管理抽屉
|
|
2405
3104
|
showMembers ? drawer(lang.manageMembers,
|
package/index.js
CHANGED
|
@@ -17,9 +17,10 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { randomUUID } from 'node:crypto'
|
|
20
|
-
import { readFileSync, writeFileSync, mkdirSync, renameSync, existsSync, readdirSync } from 'node:fs'
|
|
21
|
-
import { homedir } from 'node:os'
|
|
22
|
-
import { join } from 'node:path'
|
|
20
|
+
import { readFileSync, writeFileSync, mkdirSync, renameSync, existsSync, readdirSync, statSync, createReadStream } from 'node:fs'
|
|
21
|
+
import { homedir, tmpdir } from 'node:os'
|
|
22
|
+
import { join, resolve, isAbsolute, basename, extname, dirname } from 'node:path'
|
|
23
|
+
import { execFile } from 'node:child_process'
|
|
23
24
|
|
|
24
25
|
/** 本插件所需的 Cordis 服务。 */
|
|
25
26
|
export const inject = ['tools', 'agents', 'sessions', 'webServer', 'agentDefaultModel', 'schedule', 'llm']
|
|
@@ -381,6 +382,9 @@ function replyTargetOfRecentMember(state, group, message) {
|
|
|
381
382
|
function dispatch(state, group, message) {
|
|
382
383
|
const targets = new Set()
|
|
383
384
|
const sAgent = sagentOf(group)
|
|
385
|
+
// 只有附件、没有正文的消息也算「用户发言」,不能落进寒暄分支被静默丢弃
|
|
386
|
+
const hasAttachments = !!(message.attachments && message.attachments.length)
|
|
387
|
+
const attachmentLabel = hasAttachments ? '[附件] ' + message.attachments.map(a => a.name).join('、') : ''
|
|
384
388
|
// 解析 @ 目标(用户消息和 agent 消息都要用)
|
|
385
389
|
const addressed = resolveAddressedTo(group, message.addressedTo)
|
|
386
390
|
const addressedMembers = addressed.filter(t => memberSessionIds(group).has(t) && t !== sAgent)
|
|
@@ -395,12 +399,12 @@ function dispatch(state, group, message) {
|
|
|
395
399
|
if (addressedMembers.length > 0 && !userAddressedSagent) {
|
|
396
400
|
// 只 @ 了成员:只发给被点名的人
|
|
397
401
|
for (const t of addressedMembers) targets.add(t)
|
|
398
|
-
} else if (!userAddressedSagent && replyTargetOfRecentMember(state, group, message)) {
|
|
402
|
+
} else if (!userAddressedSagent && !hasAttachments && replyTargetOfRecentMember(state, group, message)) {
|
|
399
403
|
// 用户在对刚才那位成员说"谢谢/好的":交给**那位成员**回一句「不客气」。
|
|
400
404
|
// 既不是 PM 代答,也不是谁都不理。
|
|
401
405
|
const who = replyTargetOfRecentMember(state, group, message)
|
|
402
406
|
if (who && memberSessionIds(group).has(who)) targets.add(who)
|
|
403
|
-
} else if (!userAddressedSagent && isSmallTalkOnly(message.content)) {
|
|
407
|
+
} else if (!userAddressedSagent && !hasAttachments && isSmallTalkOnly(message.content)) {
|
|
404
408
|
// 没 @ 人、也没有明确对话对象的纯寒暄(如群里第一次说"你好")→ 不惊动任何人
|
|
405
409
|
} else {
|
|
406
410
|
targets.add(sAgent)
|
|
@@ -455,9 +459,12 @@ function dispatch(state, group, message) {
|
|
|
455
459
|
// summary 只是 inbox 预览,真正全文走 group_read_signal;但预览也不能砍半,
|
|
456
460
|
// 否则 agent 会用不完整的信息决策。仅对极端超长(>4000 字)做折叠,
|
|
457
461
|
// 并在折叠处明确标注剩余长度。
|
|
458
|
-
summary:
|
|
459
|
-
|
|
460
|
-
|
|
462
|
+
summary: (() => {
|
|
463
|
+
const base = String(message.content || '').trim() ? message.content : (attachmentLabel || '(空消息)')
|
|
464
|
+
return base.length > 4000
|
|
465
|
+
? base.slice(0, 4000) + `\n…(后续还有 ${base.length - 4000} 字,请用 group_read_signal 读全文后再执行)`
|
|
466
|
+
: base
|
|
467
|
+
})(),
|
|
461
468
|
messageId: message.id,
|
|
462
469
|
userMentioned,
|
|
463
470
|
})
|
|
@@ -964,7 +971,7 @@ export function apply(ctx) {
|
|
|
964
971
|
}
|
|
965
972
|
|
|
966
973
|
// ---- 写一条群消息 + 派发 + 唤醒(带 held draft 新鲜度检查) ----
|
|
967
|
-
function postMessage(group, { authorSessionId, authorName, authorRole, kind, content, addressedTo, basedOnVersion }) {
|
|
974
|
+
function postMessage(group, { authorSessionId, authorName, authorRole, kind, content, addressedTo, basedOnVersion, attachments }) {
|
|
968
975
|
group.version = typeof group.version === 'number' ? group.version : 0
|
|
969
976
|
// held draft:发送时若基于的 version 已过时,不直接提交,返回 held 状态 + 期间到达的内容摘要
|
|
970
977
|
if (basedOnVersion !== undefined && basedOnVersion !== group.version) {
|
|
@@ -990,6 +997,7 @@ export function apply(ctx) {
|
|
|
990
997
|
kind: kind || 'chat',
|
|
991
998
|
content,
|
|
992
999
|
addressedTo: addressedTo || [],
|
|
1000
|
+
...(attachments && attachments.length ? { attachments } : {}),
|
|
993
1001
|
ts: Date.now(),
|
|
994
1002
|
}
|
|
995
1003
|
// 记录「用户这条消息 @ 了谁」:成员随后回复时据此判断是否为"回答用户点名",
|
|
@@ -1365,8 +1373,13 @@ export function apply(ctx) {
|
|
|
1365
1373
|
signal.ingested = true
|
|
1366
1374
|
saveState(state)
|
|
1367
1375
|
if (!msg) return { content: signal.summary, authorName: signal.fromName, authorRole: 'unknown', groupId: signal.groupId, kind: signal.kind }
|
|
1376
|
+
const attachmentBlock = (msg.attachments || []).length
|
|
1377
|
+
? '\n\n[附件]\n' + msg.attachments
|
|
1378
|
+
.map(a => `- ${a.name}(${a.mime || '未知类型'},${formatBytes(a.size)}):${a.path}`)
|
|
1379
|
+
.join('\n')
|
|
1380
|
+
: ''
|
|
1368
1381
|
return {
|
|
1369
|
-
content: msg.content, authorName: msg.authorName, authorRole: msg.authorRole,
|
|
1382
|
+
content: msg.content + attachmentBlock, authorName: msg.authorName, authorRole: msg.authorRole,
|
|
1370
1383
|
groupId: msg.groupId, kind: msg.kind,
|
|
1371
1384
|
}
|
|
1372
1385
|
},
|
|
@@ -1669,6 +1682,157 @@ export function apply(ctx) {
|
|
|
1669
1682
|
return { sessionId, reused }
|
|
1670
1683
|
}
|
|
1671
1684
|
|
|
1685
|
+
|
|
1686
|
+
// ---- 文件引用:预览 / 打开 / 在 Finder 中显示 ----
|
|
1687
|
+
const FILE_IMAGE_EXT = new Set(['.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp', '.svg', '.ico'])
|
|
1688
|
+
const FILE_TEXT_EXT = new Set([
|
|
1689
|
+
'.md', '.markdown', '.txt', '.json', '.jsonl', '.csv', '.tsv', '.log', '.yml', '.yaml',
|
|
1690
|
+
'.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.py', '.sh', '.bash', '.zsh', '.html', '.htm',
|
|
1691
|
+
'.css', '.scss', '.less', '.xml', '.toml', '.ini', '.conf', '.env', '.sql', '.java', '.go',
|
|
1692
|
+
'.rs', '.c', '.h', '.cpp', '.hpp', '.rb', '.php', '.vue', '.svelte', '.gitignore', '.dockerfile',
|
|
1693
|
+
])
|
|
1694
|
+
const FILE_MAX_IMAGE = 12 * 1024 * 1024
|
|
1695
|
+
const FILE_MAX_TEXT = 300 * 1024
|
|
1696
|
+
|
|
1697
|
+
function mimeOfImage(ext) {
|
|
1698
|
+
return ({
|
|
1699
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif',
|
|
1700
|
+
'.webp': 'image/webp', '.bmp': 'image/bmp', '.svg': 'image/svg+xml', '.ico': 'image/x-icon',
|
|
1701
|
+
})[ext] || 'application/octet-stream'
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
/** 把消息里的路径解析成绝对路径:支持 ~、file://、相对路径(按 base / home 解析)。 */
|
|
1705
|
+
function resolveMessagePath(raw, base) {
|
|
1706
|
+
let p = String(raw || '').trim().replace(/^<|>$/g, '')
|
|
1707
|
+
if (!p) return null
|
|
1708
|
+
if (/^file:\/\//i.test(p)) {
|
|
1709
|
+
try {
|
|
1710
|
+
p = decodeURIComponent(new URL(p).pathname)
|
|
1711
|
+
if (/^\/[A-Za-z]:\//.test(p)) p = p.slice(1)
|
|
1712
|
+
} catch (_e) { return null }
|
|
1713
|
+
}
|
|
1714
|
+
if (p === '~' || p.startsWith('~/')) p = join(homedir(), p.slice(1))
|
|
1715
|
+
if (!isAbsolute(p)) {
|
|
1716
|
+
const root = base && isAbsolute(String(base)) ? String(base) : homedir()
|
|
1717
|
+
p = resolve(root, p)
|
|
1718
|
+
}
|
|
1719
|
+
return p
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
/** 允许读取的根目录:当前用户目录、临时目录,以及群里各会话所在的工作区(agent 的 cwd)。 */
|
|
1723
|
+
function fileAllowedRoots() {
|
|
1724
|
+
const roots = new Set([homedir(), tmpdir(), '/tmp', '/private/tmp'])
|
|
1725
|
+
const add = (cwd) => {
|
|
1726
|
+
const value = String(cwd || '').replace(/[\\/]+$/, '')
|
|
1727
|
+
if (value && isAbsolute(value)) roots.add(value)
|
|
1728
|
+
}
|
|
1729
|
+
try {
|
|
1730
|
+
for (const g of state.groups || []) {
|
|
1731
|
+
const sagent = sessionInfoOf(g.sAgentSessionId)
|
|
1732
|
+
if (sagent) add(sagent.cwd)
|
|
1733
|
+
for (const member of g.members || []) {
|
|
1734
|
+
const info = sessionInfoOf(member.sessionId)
|
|
1735
|
+
if (info) add(info.cwd)
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
for (const profile of state.profiles || []) {
|
|
1739
|
+
const info = profile.sessionId ? sessionInfoOf(profile.sessionId) : null
|
|
1740
|
+
if (info) add(info.cwd)
|
|
1741
|
+
}
|
|
1742
|
+
} catch (_e) { /* ignore */ }
|
|
1743
|
+
return [...roots]
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
/** 只允许读上面这些根目录下的文件,避免把这个接口放大成任意文件读取。 */
|
|
1747
|
+
function insideFileRoots(p) {
|
|
1748
|
+
return fileAllowedRoots().some(root => p === root || p.startsWith(root + '/') || p.startsWith(root + '\\'))
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
|
|
1752
|
+
// ---- 附件存储(群聊里发送的图片 / 视频 / 文件)----
|
|
1753
|
+
const ATTACH_DIR = join(DATA_DIR, 'attachments')
|
|
1754
|
+
const ATTACH_MAX_BYTES = 200 * 1024 * 1024
|
|
1755
|
+
const ATTACH_VIDEO_EXT = new Set(['.mp4', '.mov', '.webm', '.m4v', '.avi', '.mkv', '.ogv'])
|
|
1756
|
+
|
|
1757
|
+
function sanitizeFileName(raw) {
|
|
1758
|
+
let name = String(raw || '').split(/[\\/]/).pop()
|
|
1759
|
+
name = name.replace(/[\u0000-\u001f<>:"|?*]/g, '_').trim()
|
|
1760
|
+
if (!name) name = 'attachment'
|
|
1761
|
+
if (name.length > 120) {
|
|
1762
|
+
const dot = name.lastIndexOf('.')
|
|
1763
|
+
const ext = dot > 0 ? name.slice(dot) : ''
|
|
1764
|
+
name = name.slice(0, Math.max(1, 120 - ext.length)) + ext
|
|
1765
|
+
}
|
|
1766
|
+
return name
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
function attachmentKindOf(name, mime) {
|
|
1770
|
+
const ext = extname(String(name || '')).toLowerCase()
|
|
1771
|
+
if (/^image\//i.test(mime || '') || FILE_IMAGE_EXT.has(ext)) return 'image'
|
|
1772
|
+
if (/^video\//i.test(mime || '') || ATTACH_VIDEO_EXT.has(ext)) return 'video'
|
|
1773
|
+
return 'file'
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
function attachmentMetaPath(id) { return join(ATTACH_DIR, String(id), 'meta.json') }
|
|
1777
|
+
|
|
1778
|
+
function readAttachmentMeta(id) {
|
|
1779
|
+
try { return JSON.parse(readFileSync(attachmentMetaPath(id), 'utf8')) } catch (_e) { return null }
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
function publicAttachment(meta) {
|
|
1783
|
+
if (!meta) return null
|
|
1784
|
+
return {
|
|
1785
|
+
id: meta.id, name: meta.name, size: meta.size, mime: meta.mime, kind: meta.kind,
|
|
1786
|
+
path: meta.path, createdAt: meta.createdAt,
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
function saveAttachment(buffer, rawName, mime) {
|
|
1791
|
+
const name = sanitizeFileName(rawName)
|
|
1792
|
+
const id = randomUUID()
|
|
1793
|
+
const dir = join(ATTACH_DIR, id)
|
|
1794
|
+
mkdirSync(dir, { recursive: true })
|
|
1795
|
+
const filePath = join(dir, name)
|
|
1796
|
+
writeFileSync(filePath, buffer)
|
|
1797
|
+
const meta = {
|
|
1798
|
+
id, name, size: buffer.length,
|
|
1799
|
+
mime: mime || 'application/octet-stream',
|
|
1800
|
+
kind: attachmentKindOf(name, mime),
|
|
1801
|
+
path: filePath,
|
|
1802
|
+
createdAt: Date.now(),
|
|
1803
|
+
}
|
|
1804
|
+
writeFileSync(attachmentMetaPath(id), JSON.stringify(meta, null, 2))
|
|
1805
|
+
return meta
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
function attachmentUrl(id) { return '/groupchat/api/attachment/' + encodeURIComponent(id) }
|
|
1809
|
+
|
|
1810
|
+
function formatBytes(bytes) {
|
|
1811
|
+
const n = Number(bytes) || 0
|
|
1812
|
+
if (n < 1024) return n + ' B'
|
|
1813
|
+
if (n < 1024 * 1024) return (n / 1024).toFixed(1) + ' KB'
|
|
1814
|
+
if (n < 1024 * 1024 * 1024) return (n / (1024 * 1024)).toFixed(1) + ' MB'
|
|
1815
|
+
return (n / (1024 * 1024 * 1024)).toFixed(1) + ' GB'
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
function readRawBody(req, limitBytes) {
|
|
1819
|
+
return new Promise((resolvePromise, reject) => {
|
|
1820
|
+
const chunks = []
|
|
1821
|
+
let size = 0
|
|
1822
|
+
req.on('data', (chunk) => {
|
|
1823
|
+
size += chunk.length
|
|
1824
|
+
if (size > limitBytes) {
|
|
1825
|
+
reject(new Error('文件超过 ' + formatBytes(limitBytes)))
|
|
1826
|
+
req.destroy()
|
|
1827
|
+
return
|
|
1828
|
+
}
|
|
1829
|
+
chunks.push(chunk)
|
|
1830
|
+
})
|
|
1831
|
+
req.on('end', () => resolvePromise(Buffer.concat(chunks)))
|
|
1832
|
+
req.on('error', reject)
|
|
1833
|
+
})
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1672
1836
|
// ---- HTTP API(Client 面板通过 fetch 调用)----
|
|
1673
1837
|
function sendJson(res, code, body) {
|
|
1674
1838
|
res.statusCode = code
|
|
@@ -1796,6 +1960,7 @@ export function apply(ctx) {
|
|
|
1796
1960
|
messages: state.messages.map(m => ({
|
|
1797
1961
|
id: m.id, groupId: m.groupId, authorName: m.authorName, authorRole: m.authorRole,
|
|
1798
1962
|
kind: m.kind, content: m.content, ts: m.ts,
|
|
1963
|
+
...(m.attachments && m.attachments.length ? { attachments: m.attachments.map(a => ({ ...a, url: attachmentUrl(a.id) })) } : {}),
|
|
1799
1964
|
})),
|
|
1800
1965
|
agentStates: Object.fromEntries(agentStates),
|
|
1801
1966
|
}
|
|
@@ -1810,6 +1975,120 @@ export function apply(ctx) {
|
|
|
1810
1975
|
const pathname = url.pathname
|
|
1811
1976
|
const method = req.method || 'GET'
|
|
1812
1977
|
|
|
1978
|
+
|
|
1979
|
+
|
|
1980
|
+
// POST /groupchat/api/upload?name=&mime= —— 群附件上传(原始二进制 body)
|
|
1981
|
+
if (pathname === '/groupchat/api/upload' && method === 'POST') {
|
|
1982
|
+
const name = url.searchParams.get('name') || 'attachment'
|
|
1983
|
+
const mime = url.searchParams.get('mime') || req.headers['content-type'] || ''
|
|
1984
|
+
let buffer
|
|
1985
|
+
try {
|
|
1986
|
+
buffer = await readRawBody(req, ATTACH_MAX_BYTES)
|
|
1987
|
+
} catch (e) {
|
|
1988
|
+
return sendJson(res, 413, { ok: false, error: e.message || '文件过大' })
|
|
1989
|
+
}
|
|
1990
|
+
if (!buffer || buffer.length === 0) return sendJson(res, 400, { ok: false, error: '空文件' })
|
|
1991
|
+
const meta = saveAttachment(buffer, name, mime)
|
|
1992
|
+
return sendJson(res, 200, { ok: true, attachment: { ...publicAttachment(meta), url: attachmentUrl(meta.id) } })
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
// GET /groupchat/api/attachment/:id[?download=1] —— 读取附件(支持 Range,视频可拖动)
|
|
1996
|
+
const attachmentMatch = pathname.match(/^\/groupchat\/api\/attachment\/([^/]+)$/)
|
|
1997
|
+
if (attachmentMatch && method === 'GET') {
|
|
1998
|
+
const meta = readAttachmentMeta(decodeURIComponent(attachmentMatch[1]))
|
|
1999
|
+
if (!meta) return sendJson(res, 404, { ok: false, error: '附件不存在' })
|
|
2000
|
+
let stat
|
|
2001
|
+
try { stat = statSync(meta.path) } catch (_e) { return sendJson(res, 404, { ok: false, error: '附件文件已丢失' }) }
|
|
2002
|
+
const download = url.searchParams.get('download') === '1'
|
|
2003
|
+
const headers = {
|
|
2004
|
+
'content-type': meta.mime || 'application/octet-stream',
|
|
2005
|
+
'accept-ranges': 'bytes',
|
|
2006
|
+
'cache-control': 'private, max-age=3600',
|
|
2007
|
+
}
|
|
2008
|
+
if (download) headers['content-disposition'] = "attachment; filename*=UTF-8''" + encodeURIComponent(meta.name)
|
|
2009
|
+
const range = req.headers.range
|
|
2010
|
+
if (range) {
|
|
2011
|
+
const m = /^bytes=(\d*)-(\d*)$/.exec(String(range))
|
|
2012
|
+
if (m) {
|
|
2013
|
+
const start = m[1] ? parseInt(m[1], 10) : 0
|
|
2014
|
+
const endRaw = m[2] ? parseInt(m[2], 10) : stat.size - 1
|
|
2015
|
+
const end = Math.min(endRaw, stat.size - 1)
|
|
2016
|
+
if (Number.isNaN(start) || Number.isNaN(end) || start > end || start >= stat.size) {
|
|
2017
|
+
res.writeHead(416, { 'content-range': `bytes */${stat.size}` })
|
|
2018
|
+
return res.end()
|
|
2019
|
+
}
|
|
2020
|
+
res.writeHead(206, {
|
|
2021
|
+
...headers,
|
|
2022
|
+
'content-range': `bytes ${start}-${end}/${stat.size}`,
|
|
2023
|
+
'content-length': end - start + 1,
|
|
2024
|
+
})
|
|
2025
|
+
createReadStream(meta.path, { start, end }).pipe(res)
|
|
2026
|
+
return
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
res.writeHead(200, { ...headers, 'content-length': stat.size })
|
|
2030
|
+
createReadStream(meta.path).pipe(res)
|
|
2031
|
+
return
|
|
2032
|
+
}
|
|
2033
|
+
|
|
2034
|
+
// GET /groupchat/api/file?path=&base= —— 读文件元信息;图片返回 dataUrl,文本返回正文
|
|
2035
|
+
if (pathname === '/groupchat/api/file' && method === 'GET') {
|
|
2036
|
+
const target = resolveMessagePath(url.searchParams.get('path'), url.searchParams.get('base'))
|
|
2037
|
+
if (!target) return sendJson(res, 400, { ok: false, error: '需要有效的 path' })
|
|
2038
|
+
if (!insideFileRoots(target)) return sendJson(res, 403, { ok: false, error: '路径不在允许范围(仅限用户目录与临时目录)' })
|
|
2039
|
+
let stat
|
|
2040
|
+
try { stat = statSync(target) } catch (_e) { return sendJson(res, 404, { ok: false, error: '文件不存在或不可读' }) }
|
|
2041
|
+
if (!stat.isFile()) return sendJson(res, 400, { ok: false, error: '不是文件' })
|
|
2042
|
+
const name = basename(target)
|
|
2043
|
+
const ext = extname(target).toLowerCase()
|
|
2044
|
+
const payload = { ok: true, path: target, name, ext, size: stat.size, mtime: stat.mtimeMs, kind: 'binary' }
|
|
2045
|
+
if (FILE_IMAGE_EXT.has(ext)) {
|
|
2046
|
+
if (stat.size > FILE_MAX_IMAGE) return sendJson(res, 413, { ok: false, error: '图片超过 12MB,无法预览' })
|
|
2047
|
+
const mime = mimeOfImage(ext)
|
|
2048
|
+
const dataUrl = 'data:' + mime + ';base64,' + readFileSync(target).toString('base64')
|
|
2049
|
+
return sendJson(res, 200, { ...payload, kind: 'image', mime, dataUrl })
|
|
2050
|
+
}
|
|
2051
|
+
if (FILE_TEXT_EXT.has(ext)) {
|
|
2052
|
+
const buf = readFileSync(target)
|
|
2053
|
+
const truncated = buf.length > FILE_MAX_TEXT
|
|
2054
|
+
const text = (truncated ? buf.subarray(0, FILE_MAX_TEXT) : buf).toString('utf8')
|
|
2055
|
+
if (text.includes('\u0000')) return sendJson(res, 200, payload)
|
|
2056
|
+
return sendJson(res, 200, { ...payload, kind: 'text', text, truncated })
|
|
2057
|
+
}
|
|
2058
|
+
return sendJson(res, 200, payload)
|
|
2059
|
+
}
|
|
2060
|
+
|
|
2061
|
+
// POST /groupchat/api/file-action —— 用系统默认程序打开,或在 Finder / 资源管理器中定位
|
|
2062
|
+
if (pathname === '/groupchat/api/file-action' && method === 'POST') {
|
|
2063
|
+
const body = await readBody(req)
|
|
2064
|
+
const target = resolveMessagePath(body.path, body.base)
|
|
2065
|
+
if (!target) return sendJson(res, 400, { ok: false, error: '需要有效的 path' })
|
|
2066
|
+
if (!insideFileRoots(target)) return sendJson(res, 403, { ok: false, error: '路径不在允许范围' })
|
|
2067
|
+
let stat
|
|
2068
|
+
try { stat = statSync(target) } catch (_e) { return sendJson(res, 404, { ok: false, error: '文件不存在或不可读' }) }
|
|
2069
|
+
if (!stat.isFile()) return sendJson(res, 400, { ok: false, error: '不是文件' })
|
|
2070
|
+
const action = body.action === 'reveal' ? 'reveal' : 'open'
|
|
2071
|
+
const platform = process.platform
|
|
2072
|
+
let cmd
|
|
2073
|
+
let args
|
|
2074
|
+
if (platform === 'darwin') {
|
|
2075
|
+
cmd = 'open'
|
|
2076
|
+
args = action === 'reveal' ? ['-R', target] : [target]
|
|
2077
|
+
} else if (platform === 'win32') {
|
|
2078
|
+
cmd = 'explorer'
|
|
2079
|
+
args = action === 'reveal' ? ['/select,' + target] : [target]
|
|
2080
|
+
} else {
|
|
2081
|
+
cmd = 'xdg-open'
|
|
2082
|
+
args = [action === 'reveal' ? dirname(target) : target]
|
|
2083
|
+
}
|
|
2084
|
+
try {
|
|
2085
|
+
await new Promise((ok, fail) => execFile(cmd, args, (error) => (error ? fail(error) : ok())))
|
|
2086
|
+
return sendJson(res, 200, { ok: true, path: target, action })
|
|
2087
|
+
} catch (e) {
|
|
2088
|
+
return sendJson(res, 500, { ok: false, error: '打开失败:' + (e.message || e) })
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
|
|
1813
2092
|
// GET /groupchat/api/state —— 全量或增量(?since=ts 只返回 since 之后的变化)
|
|
1814
2093
|
if (pathname === '/groupchat/api/state' && method === 'GET') {
|
|
1815
2094
|
const since = url.searchParams.get('since')
|
|
@@ -2081,8 +2360,15 @@ export function apply(ctx) {
|
|
|
2081
2360
|
const body = await readBody(req)
|
|
2082
2361
|
const group = state.groups.find(g => g.id === postMatch[1])
|
|
2083
2362
|
if (!group) return sendJson(res, 404, { ok: false, error: '群不存在' })
|
|
2084
|
-
|
|
2085
|
-
|
|
2363
|
+
const attachmentIds = Array.isArray(body.attachments) ? body.attachments : []
|
|
2364
|
+
if ((!body.content || !String(body.content).trim()) && attachmentIds.length === 0) {
|
|
2365
|
+
return sendJson(res, 400, { ok: false, error: '内容不能为空' })
|
|
2366
|
+
}
|
|
2367
|
+
const content = String(body.content || '').trim()
|
|
2368
|
+
const attachments = attachmentIds
|
|
2369
|
+
.map(id => publicAttachment(readAttachmentMeta(String(id))))
|
|
2370
|
+
.filter(Boolean)
|
|
2371
|
+
.map(a => ({ ...a, url: attachmentUrl(a.id) }))
|
|
2086
2372
|
// 从内容里提取 @成员名字(如「@测试小张」),作为 addressedTo
|
|
2087
2373
|
const mentioned = []
|
|
2088
2374
|
const members = [{ name: group.sAgentName }, ...(group.members || [])]
|
|
@@ -2108,6 +2394,7 @@ export function apply(ctx) {
|
|
|
2108
2394
|
content,
|
|
2109
2395
|
addressedTo,
|
|
2110
2396
|
basedOnVersion: body.basedOnVersion,
|
|
2397
|
+
attachments,
|
|
2111
2398
|
})
|
|
2112
2399
|
if (msg.held) return sendJson(res, 200, { ok: true, held: true, basedOnVersion: msg.basedOnVersion, currentVersion: msg.currentVersion, arrivedWhileWriting: msg.arrivedWhileWriting })
|
|
2113
2400
|
return sendJson(res, 200, { ok: true, state: publicState() })
|