dsh-cothread-mcp 0.1.1 → 0.1.4
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 +34 -0
- package/README.md +56 -18
- package/icon.svg +7 -4
- package/lib/client.js +732 -0
- package/package.json +21 -3
- package/scripts/build.mjs +61 -0
- package/src/authorization.js +144 -0
- package/src/automation.js +599 -0
- package/src/device.js +86 -0
- package/src/environments.js +18 -0
- package/src/events.js +7 -0
- package/src/index.js +81 -25
- package/src/service.js +640 -0
- package/src/state.js +78 -0
- package/src/version.js +17 -0
package/src/state.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
2
|
+
import { mkdir, readFile, rename, unlink, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { homedir } from 'node:os'
|
|
4
|
+
import { resolve } from 'node:path'
|
|
5
|
+
|
|
6
|
+
/** 每个 profile 一份状态目录:同一个 DSH 上跑着多个实例时互不干扰。 */
|
|
7
|
+
function scopeDir(ctx) {
|
|
8
|
+
const profile = ctx && typeof ctx.get === 'function' ? ctx.get('profileContext') : undefined
|
|
9
|
+
const scope = profile && profile.dir ? profile.dir : 'default'
|
|
10
|
+
const name = createHash('sha256').update(String(scope)).digest('hex').slice(0, 16)
|
|
11
|
+
return resolve(process.env.DSH_HOME || resolve(homedir(), '.dsh'), 'plugins', 'dsh-cothread-mcp', 'state', name)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** 项目的巡检状态文件。 */
|
|
15
|
+
export function stateFile(ctx, projectId) {
|
|
16
|
+
return resolve(scopeDir(ctx), `${String(projectId).replace(/[^A-Za-z0-9_-]/g, '')}.json`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 读取项目状态:
|
|
21
|
+
* - cursor:上一次已经交给 Agent 的消息位置(afterMessageId)
|
|
22
|
+
* - pending:已经写进任务内容、等待下一次投递的那一轮(投递完成后清空)
|
|
23
|
+
* - tasks:已经提醒过的待处理任务(带版本,任务有更新时重新提醒)
|
|
24
|
+
*/
|
|
25
|
+
export async function readState(ctx, projectId) {
|
|
26
|
+
let raw
|
|
27
|
+
try { raw = await readFile(stateFile(ctx, projectId), 'utf8') } catch (error) {
|
|
28
|
+
if (error.code === 'ENOENT') return emptyState()
|
|
29
|
+
throw error
|
|
30
|
+
}
|
|
31
|
+
const parsed = JSON.parse(raw)
|
|
32
|
+
const tasks = parsed && typeof parsed.tasks === 'object' && parsed.tasks ? parsed.tasks : {}
|
|
33
|
+
return {
|
|
34
|
+
initialized: parsed?.initialized === true,
|
|
35
|
+
cursor: typeof parsed?.cursor === 'string' ? parsed.cursor : '',
|
|
36
|
+
pending: parsed?.pending && typeof parsed.pending === 'object' ? {
|
|
37
|
+
cursor: typeof parsed.pending.cursor === 'string' ? parsed.pending.cursor : '',
|
|
38
|
+
threadId: typeof parsed.pending.threadId === 'string' ? parsed.pending.threadId : '',
|
|
39
|
+
messageIds: Array.isArray(parsed.pending.messageIds) ? parsed.pending.messageIds.filter(id => typeof id === 'string') : [],
|
|
40
|
+
taskIds: Array.isArray(parsed.pending.taskIds) ? parsed.pending.taskIds.filter(id => typeof id === 'string') : [],
|
|
41
|
+
patchedAt: typeof parsed.pending.patchedAt === 'string' ? parsed.pending.patchedAt : '',
|
|
42
|
+
} : null,
|
|
43
|
+
tasks,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function emptyState() {
|
|
48
|
+
return { initialized: false, cursor: '', pending: null, tasks: {} }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** 保存项目状态。 */
|
|
52
|
+
export async function writeState(ctx, projectId, state) {
|
|
53
|
+
const file = stateFile(ctx, projectId)
|
|
54
|
+
await mkdir(resolve(file, '..'), { recursive: true, mode: 0o700 })
|
|
55
|
+
const temp = `${file}.${randomUUID()}.tmp`
|
|
56
|
+
try {
|
|
57
|
+
await writeFile(temp, JSON.stringify({
|
|
58
|
+
initialized: state.initialized === true,
|
|
59
|
+
cursor: state.cursor || '',
|
|
60
|
+
pending: state.pending ? {
|
|
61
|
+
cursor: state.pending.cursor || '',
|
|
62
|
+
threadId: state.pending.threadId || '',
|
|
63
|
+
messageIds: state.pending.messageIds || [],
|
|
64
|
+
taskIds: state.pending.taskIds || [],
|
|
65
|
+
patchedAt: state.pending.patchedAt || '',
|
|
66
|
+
} : null,
|
|
67
|
+
tasks: state.tasks || {},
|
|
68
|
+
}), { mode: 0o600 })
|
|
69
|
+
await rename(temp, file)
|
|
70
|
+
} finally { await unlink(temp).catch(() => {}) }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 任务是否还在等人处理。终态任务不再提醒。 */
|
|
74
|
+
export function isPendingTask(task) {
|
|
75
|
+
const status = String(task?.status || '').toLowerCase()
|
|
76
|
+
if (!status) return false
|
|
77
|
+
return !['completed', 'complete', 'done', 'finished', 'cancelled', 'canceled', 'rejected', 'accepted', 'closed', 'archived'].includes(status)
|
|
78
|
+
}
|
package/src/version.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件版本号。
|
|
3
|
+
*
|
|
4
|
+
* 服务端的设备面板要显示客户端版本,写死会在升版后显示旧版本。
|
|
5
|
+
* 这里从 package.json 读,读不到就退回 '0.0.0',不因此中断授权。
|
|
6
|
+
*/
|
|
7
|
+
import { readFile } from 'node:fs/promises'
|
|
8
|
+
|
|
9
|
+
export async function pluginVersion() {
|
|
10
|
+
try {
|
|
11
|
+
const raw = await readFile(new URL('../package.json', import.meta.url), 'utf8')
|
|
12
|
+
const parsed = JSON.parse(raw)
|
|
13
|
+
return typeof parsed?.version === 'string' && parsed.version.trim() ? parsed.version.trim() : '0.0.0'
|
|
14
|
+
} catch {
|
|
15
|
+
return '0.0.0'
|
|
16
|
+
}
|
|
17
|
+
}
|