dsh-ta 0.2.0 → 0.3.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 +21 -1
- package/README.md +225 -93
- package/client.js +2371 -435
- package/config.js +0 -1
- package/docs/RELEASE.md +9 -5
- package/docs/USAGE.md +19 -2
- package/index.js +42 -48
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +13 -3
- package/reme.js +3 -1
- package/runtime-manager.js +153 -0
- package/runtime.js +26 -46
- package/settings/defaults.js +41 -0
- package/settings/errors.js +29 -0
- package/settings/home.js +315 -0
- package/settings/im.js +131 -0
- package/settings/lifecycle.js +387 -0
- package/settings/persona.js +203 -0
- package/settings/routes.js +296 -0
- package/settings/schedule.js +156 -0
- package/settings/status.js +164 -0
- package/storage.js +29 -2
- package/usage/ledger.js +35 -1
- package/usage/serve.js +78 -0
package/settings/home.js
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import {
|
|
2
|
+
accessSync, constants, existsSync, mkdirSync, readFileSync, readdirSync,
|
|
3
|
+
realpathSync, renameSync, statSync,
|
|
4
|
+
} from 'node:fs'
|
|
5
|
+
import { homedir } from 'node:os'
|
|
6
|
+
import { basename, dirname, isAbsolute, join, parse, resolve, sep } from 'node:path'
|
|
7
|
+
import { writeAtomic } from '../storage.js'
|
|
8
|
+
import { badRequest } from './errors.js'
|
|
9
|
+
import {
|
|
10
|
+
DEFAULT_SOUL_TEMPLATE, DEFAULT_USER_TEMPLATE, HOME_MARKER, HOME_MARKER_VERSION,
|
|
11
|
+
REQUIRED_DIRS,
|
|
12
|
+
} from './defaults.js'
|
|
13
|
+
|
|
14
|
+
const SYSTEM_DIRS = ['/', '/home', '/tmp', '/var', '/usr', '/etc', '/opt', '/root', '/bin', '/sbin', '/lib']
|
|
15
|
+
|
|
16
|
+
export function expandUserPath(raw) {
|
|
17
|
+
if (typeof raw !== 'string') return ''
|
|
18
|
+
const value = raw.trim()
|
|
19
|
+
if (!value) return ''
|
|
20
|
+
const home = process.env.HOME || homedir()
|
|
21
|
+
if (value === '~') return home
|
|
22
|
+
if (value.startsWith('~/')) return resolve(home, value.slice(2))
|
|
23
|
+
return isAbsolute(value) ? resolve(value) : ''
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Resolve symlinks for the deepest existing ancestor, then append the missing
|
|
28
|
+
* tail. This keeps comparisons meaningful before a home directory exists.
|
|
29
|
+
*/
|
|
30
|
+
export function physicalPath(value) {
|
|
31
|
+
let current = resolve(String(value ?? ''))
|
|
32
|
+
const tail = []
|
|
33
|
+
for (let depth = 0; depth < 64; depth += 1) {
|
|
34
|
+
try {
|
|
35
|
+
const real = realpathSync.native(current)
|
|
36
|
+
return tail.length === 0 ? real : resolve(real, ...tail.reverse())
|
|
37
|
+
} catch {
|
|
38
|
+
const parent = dirname(current)
|
|
39
|
+
if (parent === current) return resolve(String(value ?? ''))
|
|
40
|
+
tail.push(basename(current))
|
|
41
|
+
current = parent
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return resolve(String(value ?? ''))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function isSameOrInside(parent, child) {
|
|
48
|
+
const base = physicalPath(parent)
|
|
49
|
+
const target = physicalPath(child)
|
|
50
|
+
return target === base || target.startsWith(base + sep)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function nearestExistingAncestor(path) {
|
|
54
|
+
let current = path
|
|
55
|
+
for (let depth = 0; depth < 64; depth += 1) {
|
|
56
|
+
if (existsSync(current)) return current
|
|
57
|
+
const parent = dirname(current)
|
|
58
|
+
if (parent === current) return current
|
|
59
|
+
current = parent
|
|
60
|
+
}
|
|
61
|
+
return dirname(path)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function assertWritableAncestor(path) {
|
|
65
|
+
const ancestor = nearestExistingAncestor(path)
|
|
66
|
+
let stat
|
|
67
|
+
try {
|
|
68
|
+
stat = statSync(ancestor)
|
|
69
|
+
} catch {
|
|
70
|
+
throw badRequest('HOME_PATH_UNWRITABLE', '找不到可写的上级目录')
|
|
71
|
+
}
|
|
72
|
+
if (!stat.isDirectory()) throw badRequest('HOME_PATH_NOT_DIRECTORY', '上级路径不是目录')
|
|
73
|
+
try {
|
|
74
|
+
accessSync(ancestor, constants.W_OK)
|
|
75
|
+
} catch {
|
|
76
|
+
throw badRequest('HOME_PATH_UNWRITABLE', '上级目录不可写,请换一个路径')
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Validate a path a user may adopt as TA_HOME (creation may be pending). */
|
|
81
|
+
export function validateHomePath(raw) {
|
|
82
|
+
const expanded = expandUserPath(raw)
|
|
83
|
+
if (!expanded) {
|
|
84
|
+
throw badRequest('HOME_PATH_INVALID', '请填写绝对路径,例如 ~/TATA/home')
|
|
85
|
+
}
|
|
86
|
+
const normalized = resolve(expanded)
|
|
87
|
+
if (normalized === parse(normalized).root) {
|
|
88
|
+
throw badRequest('HOME_PATH_FORBIDDEN', '不能把文件系统根目录作为 TA 的家')
|
|
89
|
+
}
|
|
90
|
+
const userHome = physicalPath(process.env.HOME || homedir())
|
|
91
|
+
if (physicalPath(normalized) === userHome) {
|
|
92
|
+
throw badRequest('HOME_PATH_FORBIDDEN', '不能把用户主目录本身作为 TA 的家')
|
|
93
|
+
}
|
|
94
|
+
if (existsSync(normalized)) {
|
|
95
|
+
let stat
|
|
96
|
+
try {
|
|
97
|
+
stat = statSync(normalized)
|
|
98
|
+
} catch {
|
|
99
|
+
throw badRequest('HOME_PATH_INVALID', '无法读取这个路径')
|
|
100
|
+
}
|
|
101
|
+
if (!stat.isDirectory()) {
|
|
102
|
+
throw badRequest('HOME_PATH_NOT_DIRECTORY', '这个路径已经存在,而且不是目录')
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
assertWritableAncestor(normalized)
|
|
106
|
+
return { path: normalized, physical: physicalPath(normalized) }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Validate the destination of a reincarnation. */
|
|
110
|
+
export function validateNewHome(oldHome, raw) {
|
|
111
|
+
const next = validateHomePath(raw)
|
|
112
|
+
const oldPhysical = physicalPath(oldHome)
|
|
113
|
+
const newPhysical = physicalPath(next.path)
|
|
114
|
+
if (newPhysical === oldPhysical) {
|
|
115
|
+
throw badRequest('NEW_HOME_SAME', '新家不能和旧家是同一个目录')
|
|
116
|
+
}
|
|
117
|
+
if (newPhysical.startsWith(oldPhysical + sep)) {
|
|
118
|
+
throw badRequest('NEW_HOME_INSIDE_OLD', '新家不能在旧家内部')
|
|
119
|
+
}
|
|
120
|
+
if (oldPhysical.startsWith(newPhysical + sep)) {
|
|
121
|
+
throw badRequest('NEW_HOME_ANCESTOR', '新家不能是旧家的上级目录')
|
|
122
|
+
}
|
|
123
|
+
let existed = false
|
|
124
|
+
if (existsSync(next.path)) {
|
|
125
|
+
existed = true
|
|
126
|
+
let entries = []
|
|
127
|
+
try {
|
|
128
|
+
entries = readdirSync(next.path)
|
|
129
|
+
} catch {
|
|
130
|
+
throw badRequest('NEW_HOME_UNREADABLE', '无法读取新家路径')
|
|
131
|
+
}
|
|
132
|
+
if (entries.length > 0) {
|
|
133
|
+
throw badRequest('NEW_HOME_NOT_EMPTY', '新家的目录必须为空,或尚未创建')
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { ...next, existed }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** @returns {{ exists: boolean, valid: boolean, version?: number, createdAt?: string }} */
|
|
140
|
+
export function readHomeMarker(home) {
|
|
141
|
+
const target = join(home, HOME_MARKER)
|
|
142
|
+
if (!existsSync(target)) return { exists: false, valid: false }
|
|
143
|
+
try {
|
|
144
|
+
const value = JSON.parse(readFileSync(target, 'utf8'))
|
|
145
|
+
const valid = Boolean(value)
|
|
146
|
+
&& typeof value === 'object'
|
|
147
|
+
&& value.version === HOME_MARKER_VERSION
|
|
148
|
+
&& typeof value.createdAt === 'string'
|
|
149
|
+
return { exists: true, valid, version: value?.version, createdAt: value?.createdAt }
|
|
150
|
+
} catch {
|
|
151
|
+
return { exists: true, valid: false }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Write the marker unless a valid one already exists. Returns whether this call
|
|
157
|
+
* wrote it. A damaged marker is only replaced when `repair` is explicitly set;
|
|
158
|
+
* the damaged file is renamed beside it rather than deleted.
|
|
159
|
+
*/
|
|
160
|
+
export function writeHomeMarker(home, meta = {}, { repair = false } = {}) {
|
|
161
|
+
const existing = readHomeMarker(home)
|
|
162
|
+
if (existing.exists && existing.valid) return false
|
|
163
|
+
mkdirSync(home, { recursive: true, mode: 0o700 })
|
|
164
|
+
if (existing.exists) {
|
|
165
|
+
if (!repair) return false
|
|
166
|
+
const damaged = join(home, `${HOME_MARKER}.broken-${Date.now()}`)
|
|
167
|
+
try {
|
|
168
|
+
renameSync(join(home, HOME_MARKER), damaged)
|
|
169
|
+
} catch {
|
|
170
|
+
return false
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
writeAtomic(join(home, HOME_MARKER), JSON.stringify({
|
|
174
|
+
version: HOME_MARKER_VERSION,
|
|
175
|
+
createdAt: new Date().toISOString(),
|
|
176
|
+
kind: 'ta-home',
|
|
177
|
+
...meta,
|
|
178
|
+
}, null, 2) + '\n')
|
|
179
|
+
return true
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Create the standard structure in an empty directory, or adopt an existing
|
|
184
|
+
* directory without overwriting any file that is already there.
|
|
185
|
+
*/
|
|
186
|
+
export function ensureHome(home) {
|
|
187
|
+
const { path } = validateHomePath(home)
|
|
188
|
+
const existed = existsSync(path)
|
|
189
|
+
const adopted = existed && readdirSync(path).length > 0
|
|
190
|
+
mkdirSync(path, { recursive: true, mode: 0o700 })
|
|
191
|
+
const createdDirs = []
|
|
192
|
+
for (const name of REQUIRED_DIRS) {
|
|
193
|
+
const directory = join(path, name)
|
|
194
|
+
if (!existsSync(directory)) {
|
|
195
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 })
|
|
196
|
+
createdDirs.push(name)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const createdFiles = []
|
|
200
|
+
const soul = join(path, 'SOUL.md')
|
|
201
|
+
if (!existsSync(soul)) {
|
|
202
|
+
writeAtomic(soul, DEFAULT_SOUL_TEMPLATE)
|
|
203
|
+
createdFiles.push('SOUL.md')
|
|
204
|
+
}
|
|
205
|
+
const user = join(path, 'USER.md')
|
|
206
|
+
if (!existsSync(user)) {
|
|
207
|
+
writeAtomic(user, DEFAULT_USER_TEMPLATE)
|
|
208
|
+
createdFiles.push('USER.md')
|
|
209
|
+
}
|
|
210
|
+
const markerBefore = readHomeMarker(path)
|
|
211
|
+
const marked = writeHomeMarker(path, {}, { repair: true })
|
|
212
|
+
return {
|
|
213
|
+
path,
|
|
214
|
+
created: !existed,
|
|
215
|
+
adopted,
|
|
216
|
+
marked,
|
|
217
|
+
repairedMarker: markerBefore.exists && !markerBefore.valid && marked,
|
|
218
|
+
createdDirs,
|
|
219
|
+
createdFiles,
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export function isTaHome(home) {
|
|
224
|
+
try {
|
|
225
|
+
if (!statSync(home).isDirectory()) return false
|
|
226
|
+
} catch {
|
|
227
|
+
return false
|
|
228
|
+
}
|
|
229
|
+
return readHomeMarker(home).valid
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function protectedPaths(ctx, extra) {
|
|
233
|
+
const userHome = physicalPath(process.env.HOME || homedir())
|
|
234
|
+
const cwd = physicalPath(process.cwd())
|
|
235
|
+
const values = [...SYSTEM_DIRS, process.env.HOME || homedir()].map(value => ({
|
|
236
|
+
raw: value,
|
|
237
|
+
physical: physicalPath(value),
|
|
238
|
+
inside: false,
|
|
239
|
+
allowSelf: false,
|
|
240
|
+
}))
|
|
241
|
+
// DSH 常从 HOME 启动;此时 cwd 内部就是默认 TA 家所在位置,不能拒绝。
|
|
242
|
+
values.push({ raw: process.cwd(), physical: cwd, inside: cwd !== userHome, allowSelf: false })
|
|
243
|
+
for (const key of ['DSH_PROFILE_DIR', 'DSH_INSTALL_DIR', 'DSH_HOME']) {
|
|
244
|
+
if (process.env[key]) {
|
|
245
|
+
values.push({
|
|
246
|
+
raw: process.env[key],
|
|
247
|
+
physical: physicalPath(process.env[key]),
|
|
248
|
+
inside: true,
|
|
249
|
+
allowSelf: false,
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
const workspaces = ctx?.get?.('workspaceRegistry')?.list?.()
|
|
255
|
+
for (const workspace of Array.isArray(workspaces) ? workspaces : []) {
|
|
256
|
+
if (typeof workspace?.path === 'string' && workspace.path) {
|
|
257
|
+
// TA 的家本来就会被注册成工作区(“去找TA”/自动注册),所以
|
|
258
|
+
// 工作区根只在它是 TA 家的上级目录时保护;与旧家本身相等时允许删除。
|
|
259
|
+
values.push({
|
|
260
|
+
raw: workspace.path,
|
|
261
|
+
physical: physicalPath(workspace.path),
|
|
262
|
+
inside: false,
|
|
263
|
+
allowSelf: true,
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
} catch {
|
|
268
|
+
/* workspace registry is optional; the static guards still apply */
|
|
269
|
+
}
|
|
270
|
+
for (const value of extra ?? []) {
|
|
271
|
+
if (typeof value === 'string' && value) {
|
|
272
|
+
values.push({ raw: value, physical: physicalPath(value), inside: true, allowSelf: false })
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return values
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Validate that a home may be deleted. Requires a valid marker and refuses
|
|
280
|
+
* protected directories, ancestors of protected directories, and locations
|
|
281
|
+
* inside the current project or DSH profile.
|
|
282
|
+
*/
|
|
283
|
+
export function assertDestroyableHome(home, { ctx, extraProtected } = {}) {
|
|
284
|
+
const { path } = validateHomePath(home)
|
|
285
|
+
const marker = readHomeMarker(path)
|
|
286
|
+
if (!marker.exists) {
|
|
287
|
+
throw badRequest('HOME_NOT_MARKED', '这个目录没有 .ta-home 标记,不是 TA 的家')
|
|
288
|
+
}
|
|
289
|
+
if (!marker.valid) {
|
|
290
|
+
throw badRequest('HOME_MARKER_INVALID', '.ta-home 标记已损坏,拒绝删除')
|
|
291
|
+
}
|
|
292
|
+
const target = physicalPath(path)
|
|
293
|
+
const candidates = protectedPaths(ctx, extraProtected)
|
|
294
|
+
for (const candidate of candidates) {
|
|
295
|
+
if (!candidate.physical) continue
|
|
296
|
+
if (target === candidate.physical) {
|
|
297
|
+
if (candidate.allowSelf) continue
|
|
298
|
+
throw badRequest('HOME_PROTECTED', `拒绝删除受保护目录:${candidate.raw}`)
|
|
299
|
+
}
|
|
300
|
+
if (candidate.physical.startsWith(target + sep)) {
|
|
301
|
+
throw badRequest('HOME_CONTAINS_PROTECTED', `目录包含受保护路径:${candidate.raw}`)
|
|
302
|
+
}
|
|
303
|
+
if (candidate.inside && target.startsWith(candidate.physical + sep)) {
|
|
304
|
+
throw badRequest('HOME_INSIDE_PROTECTED', `拒绝删除受保护目录内部的路径:${candidate.raw}`)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
for (const key of ['DSH_PROFILE_DIR', 'DSH_INSTALL_DIR']) {
|
|
308
|
+
if (!process.env[key]) continue
|
|
309
|
+
const protectedRoot = physicalPath(process.env[key])
|
|
310
|
+
if (target.startsWith(protectedRoot + sep)) {
|
|
311
|
+
throw badRequest('HOME_INSIDE_DSH', '拒绝删除 DSH 安装目录或 profile 内部的路径')
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return target
|
|
315
|
+
}
|
package/settings/im.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* im-connect 只读检测。
|
|
3
|
+
*
|
|
4
|
+
* 只读 config.imConnectDir 下的 channels.json 与 sessions.json,绝不修改文件,
|
|
5
|
+
* 也绝不把 appSecret / config / chatId / sessionId 等内部字段返回给设置页。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { readFileSync, statSync } from 'node:fs'
|
|
9
|
+
import { join } from 'node:path'
|
|
10
|
+
import { realPath } from '../storage.js'
|
|
11
|
+
|
|
12
|
+
function isPlainObject(value) {
|
|
13
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isDirectory(path) {
|
|
17
|
+
try {
|
|
18
|
+
return statSync(path).isDirectory()
|
|
19
|
+
} catch {
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 读取一个 JSON 文件,返回 { value } / { missing: true } / { invalid: true } / { error: true }。
|
|
26
|
+
* 任何异常都不要向上抛:设置页需要的是可展示的错误文案。
|
|
27
|
+
*/
|
|
28
|
+
function readJson(path) {
|
|
29
|
+
let text
|
|
30
|
+
try {
|
|
31
|
+
text = readFileSync(path, 'utf8')
|
|
32
|
+
} catch (cause) {
|
|
33
|
+
return cause?.code === 'ENOENT' ? { missing: true } : { error: true }
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
return { value: JSON.parse(text) }
|
|
37
|
+
} catch {
|
|
38
|
+
return { invalid: true }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** sessions.json 中是否存在该 channel 的 :dm: 绑定。 */
|
|
43
|
+
function hasDirectBinding(channelId, sessions) {
|
|
44
|
+
for (const [key, binding] of Object.entries(sessions)) {
|
|
45
|
+
if (!key.includes(':dm:') || !isPlainObject(binding)) continue
|
|
46
|
+
if (key.startsWith(`${channelId}:dm:`) || binding.channel === channelId) return true
|
|
47
|
+
}
|
|
48
|
+
return false
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 只挑选安全字段,账号配置里的密钥等内部信息一律不返回。 */
|
|
52
|
+
function accountView(key, channel, taHomePhysical, sessions) {
|
|
53
|
+
const id = typeof channel.id === 'string' && channel.id ? channel.id : key
|
|
54
|
+
const cwd = typeof channel.cwd === 'string' ? channel.cwd : ''
|
|
55
|
+
const agentPreset = typeof channel.agentPreset === 'string' ? channel.agentPreset : ''
|
|
56
|
+
const privateAccess = typeof channel.privateAccess === 'string' ? channel.privateAccess : ''
|
|
57
|
+
const groupAccess = typeof channel.groupAccess === 'string' ? channel.groupAccess : ''
|
|
58
|
+
const platform = typeof channel.platform === 'string' && channel.platform
|
|
59
|
+
? channel.platform
|
|
60
|
+
: (id.includes('_') ? id.slice(0, id.indexOf('_')) : '')
|
|
61
|
+
const name = typeof channel.name === 'string' && channel.name.trim() ? channel.name : id
|
|
62
|
+
|
|
63
|
+
const reasons = []
|
|
64
|
+
if (!taHomePhysical || realPath(cwd) !== taHomePhysical) reasons.push('工作目录不是 TA 的家')
|
|
65
|
+
if (agentPreset !== 'ta') reasons.push('没有使用 ta 预设')
|
|
66
|
+
if (groupAccess !== 'disabled') reasons.push('群聊权限没有关闭')
|
|
67
|
+
if (privateAccess !== 'approved') reasons.push('私聊权限不是仅批准用户')
|
|
68
|
+
if (channel.enabled === false) reasons.push('账号已停用')
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
id,
|
|
72
|
+
name,
|
|
73
|
+
platform,
|
|
74
|
+
compatible: reasons.length === 0,
|
|
75
|
+
cwd,
|
|
76
|
+
agentPreset,
|
|
77
|
+
privateAccess,
|
|
78
|
+
groupAccess,
|
|
79
|
+
bound: hasDirectBinding(id, sessions),
|
|
80
|
+
reasons,
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 读取 IM 集成状态。
|
|
86
|
+
* 返回 { installed, compatible, accounts, error? }。
|
|
87
|
+
* installed:目录存在即视为已安装(channels.json 缺失时尤其依赖目录判断)。
|
|
88
|
+
*/
|
|
89
|
+
export function readImState(config) {
|
|
90
|
+
const dir = typeof config?.imConnectDir === 'string' ? config.imConnectDir : ''
|
|
91
|
+
const installed = dir.length > 0 && isDirectory(dir)
|
|
92
|
+
const empty = { installed, compatible: false, accounts: [] }
|
|
93
|
+
if (!installed) return empty
|
|
94
|
+
|
|
95
|
+
const channelsRead = readJson(join(dir, 'channels.json'))
|
|
96
|
+
if (channelsRead.missing) return empty
|
|
97
|
+
if (channelsRead.error) return { ...empty, error: '无法读取 im-connect 账号配置(channels.json)' }
|
|
98
|
+
if (channelsRead.invalid) return { ...empty, error: 'im-connect 账号配置(channels.json)不是有效的 JSON' }
|
|
99
|
+
if (!isPlainObject(channelsRead.value)) return { ...empty, error: 'im-connect 账号配置格式不正确' }
|
|
100
|
+
|
|
101
|
+
// sessions.json 缺失是正常情况(还没绑定过会话),解析失败则要提示但保留账号列表。
|
|
102
|
+
const sessionsRead = readJson(join(dir, 'sessions.json'))
|
|
103
|
+
let sessions = {}
|
|
104
|
+
let error
|
|
105
|
+
if (sessionsRead.value !== undefined) {
|
|
106
|
+
if (isPlainObject(sessionsRead.value)) {
|
|
107
|
+
sessions = sessionsRead.value
|
|
108
|
+
} else {
|
|
109
|
+
error = 'im-connect 会话绑定格式不正确'
|
|
110
|
+
}
|
|
111
|
+
} else if (sessionsRead.invalid) {
|
|
112
|
+
error = 'im-connect 会话绑定(sessions.json)不是有效的 JSON'
|
|
113
|
+
} else if (sessionsRead.error) {
|
|
114
|
+
error = '无法读取 im-connect 会话绑定(sessions.json)'
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const taHomePhysical = realPath(typeof config?.taHome === 'string' ? config.taHome : '')
|
|
118
|
+
const channelMap = isPlainObject(channelsRead.value.channels) ? channelsRead.value.channels : {}
|
|
119
|
+
const accounts = []
|
|
120
|
+
for (const [key, channel] of Object.entries(channelMap)) {
|
|
121
|
+
if (!isPlainObject(channel)) continue
|
|
122
|
+
accounts.push(accountView(key, channel, taHomePhysical, sessions))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
installed: true,
|
|
127
|
+
compatible: accounts.some(account => account.compatible),
|
|
128
|
+
accounts,
|
|
129
|
+
...(error ? { error } : {}),
|
|
130
|
+
}
|
|
131
|
+
}
|