@twinsearth/ai-prison-dsh-plugin 0.0.0-stage → 1.1.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 +36 -0
- package/LICENSE +21 -0
- package/README.md +94 -2
- package/bin/linux-amd64/aiprison +0 -0
- package/bin/linux-arm64/aiprison +0 -0
- package/bin/macos-amd64/aiprison +0 -0
- package/bin/macos-arm64/aiprison +0 -0
- package/bin/windows-amd64/aiprison.exe +0 -0
- package/bin/windows-arm64/aiprison.exe +0 -0
- package/client.js +352 -0
- package/cordis.patch.yml +21 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +30 -0
- package/icon.svg +5 -0
- package/index.js +30 -0
- package/lib/api.js +40 -0
- package/lib/cli.js +187 -0
- package/lib/harness.js +51 -0
- package/lib/levels.js +38 -0
- package/lib/status.js +82 -0
- package/lib/tools.js +210 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +69 -4
package/lib/cli.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { existsSync } from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
|
+
|
|
6
|
+
/** Package root: the bundle directory that owns bin/, lib/ and index.js. */
|
|
7
|
+
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
|
8
|
+
|
|
9
|
+
export const PLATFORM_KEYS = { win32: 'windows', darwin: 'macos', linux: 'linux' }
|
|
10
|
+
export const ARCH_KEYS = { x64: 'amd64', arm64: 'arm64' }
|
|
11
|
+
|
|
12
|
+
/** Platforms the release bundles; keep in sync with scripts/build-binaries.mjs. */
|
|
13
|
+
export const BUNDLED_TARGETS = [
|
|
14
|
+
'windows-amd64',
|
|
15
|
+
'windows-arm64',
|
|
16
|
+
'macos-amd64',
|
|
17
|
+
'macos-arm64',
|
|
18
|
+
'linux-amd64',
|
|
19
|
+
'linux-arm64',
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
const MAX_CAPTURE = 512 * 1024
|
|
23
|
+
|
|
24
|
+
function trimEnv(value) {
|
|
25
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function expandHome(value) {
|
|
29
|
+
if (typeof value !== 'string') return undefined
|
|
30
|
+
const text = value.trim()
|
|
31
|
+
if (text === '') return undefined
|
|
32
|
+
if (text === '~') return path.join(process.env.HOME || process.env.USERPROFILE || '')
|
|
33
|
+
if (text.startsWith('~/') || text.startsWith('~\\')) {
|
|
34
|
+
return path.join(process.env.HOME || process.env.USERPROFILE || '', text.slice(2))
|
|
35
|
+
}
|
|
36
|
+
return text
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Path of the bundled CLI for one platform, when that build ships. */
|
|
40
|
+
export function bundledCliPath({ platform = process.platform, arch = process.arch, root = PACKAGE_ROOT } = {}) {
|
|
41
|
+
const dir = `${PLATFORM_KEYS[platform] || platform}-${ARCH_KEYS[arch] || arch}`
|
|
42
|
+
const file = path.join(root, 'bin', dir, platform === 'win32' ? 'aiprison.exe' : 'aiprison')
|
|
43
|
+
return existsSync(file) ? file : undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Search PATH entries for a native aiprison executable (`aiprison`, not a shim). */
|
|
47
|
+
export function pathCliPath({ env = process.env, platform = process.platform } = {}) {
|
|
48
|
+
const name = platform === 'win32' ? 'aiprison.exe' : 'aiprison'
|
|
49
|
+
const entries = String(env.PATH || env.Path || '')
|
|
50
|
+
.split(platform === 'win32' ? ';' : ':')
|
|
51
|
+
.map((entry) => entry.trim())
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
const candidate = path.join(entry, name)
|
|
55
|
+
if (existsSync(candidate)) return candidate
|
|
56
|
+
}
|
|
57
|
+
return undefined
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Resolve the CLI the plugin should run, in priority order: explicit config,
|
|
62
|
+
* AIPRISON_BIN, the bundled build, then PATH. Returns the path plus the source
|
|
63
|
+
* label used by tool results and the panel.
|
|
64
|
+
*/
|
|
65
|
+
export function resolveCli({ config = {}, env = process.env, platform = process.platform, arch = process.arch, root = PACKAGE_ROOT } = {}) {
|
|
66
|
+
const fromConfig = expandHome(config.cliPath)
|
|
67
|
+
if (fromConfig) {
|
|
68
|
+
if (existsSync(fromConfig)) return { path: fromConfig, source: 'config' }
|
|
69
|
+
return { missing: `配置的 cliPath 不存在:${fromConfig}`, source: 'config' }
|
|
70
|
+
}
|
|
71
|
+
const fromEnv = expandHome(trimEnv(env.AIPRISON_BIN))
|
|
72
|
+
if (fromEnv) {
|
|
73
|
+
if (existsSync(fromEnv)) return { path: fromEnv, source: 'AIPRISON_BIN' }
|
|
74
|
+
return { missing: `AIPRISON_BIN 指向的文件不存在:${fromEnv}`, source: 'env' }
|
|
75
|
+
}
|
|
76
|
+
const bundled = bundledCliPath({ platform, arch, root })
|
|
77
|
+
if (bundled) return { path: bundled, source: 'bundled' }
|
|
78
|
+
const fromPath = pathCliPath({ env, platform })
|
|
79
|
+
if (fromPath) return { path: fromPath, source: 'PATH' }
|
|
80
|
+
return {
|
|
81
|
+
missing:
|
|
82
|
+
`没有找到 aiprison 可执行文件(本平台 ${platform}-${arch} 没有内置构建)。` +
|
|
83
|
+
'可设置 AIPRISON_BIN、在插件配置里填 cliPath,或把 aiprison 放进 PATH。',
|
|
84
|
+
source: 'none',
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function truncate(text) {
|
|
89
|
+
if (text.length <= MAX_CAPTURE) return text
|
|
90
|
+
return `${text.slice(0, MAX_CAPTURE / 2)}\n…[输出被截断]…\n${text.slice(-MAX_CAPTURE / 2)}`
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Run the CLI and never reject: a non-zero exit, a spawn failure and a timeout
|
|
95
|
+
* all come back as data so a tool can report them without ending the turn.
|
|
96
|
+
*/
|
|
97
|
+
export function runCli(args, { bin, cwd, timeoutMs = 120_000, env = process.env, signal } = {}) {
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
if (typeof bin !== 'string' || bin === '') {
|
|
100
|
+
resolve({ ok: false, exitCode: null, stdout: '', stderr: '', error: 'no binary', timedOut: false })
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
const options = { cwd: cwd || process.cwd(), env, windowsHide: true }
|
|
104
|
+
let child
|
|
105
|
+
try {
|
|
106
|
+
child = spawn(bin, args, options)
|
|
107
|
+
} catch (error) {
|
|
108
|
+
resolve({ ok: false, exitCode: null, stdout: '', stderr: '', error: String(error && error.message), timedOut: false })
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
let stdout = ''
|
|
112
|
+
let stderr = ''
|
|
113
|
+
let timedOut = false
|
|
114
|
+
let spawnError
|
|
115
|
+
const timer = setTimeout(() => {
|
|
116
|
+
timedOut = true
|
|
117
|
+
try {
|
|
118
|
+
child.kill('SIGKILL')
|
|
119
|
+
} catch {
|
|
120
|
+
// The exit handler below still settles the promise.
|
|
121
|
+
}
|
|
122
|
+
}, Math.max(1_000, Number(timeoutMs) || 120_000))
|
|
123
|
+
const onAbort = () => {
|
|
124
|
+
try {
|
|
125
|
+
child.kill('SIGKILL')
|
|
126
|
+
} catch {
|
|
127
|
+
// ignore
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (signal && typeof signal.addEventListener === 'function') {
|
|
131
|
+
if (signal.aborted) onAbort()
|
|
132
|
+
else signal.addEventListener('abort', onAbort, { once: true })
|
|
133
|
+
}
|
|
134
|
+
child.stdout?.on('data', (chunk) => {
|
|
135
|
+
stdout = truncate(stdout + chunk.toString())
|
|
136
|
+
})
|
|
137
|
+
child.stderr?.on('data', (chunk) => {
|
|
138
|
+
stderr = truncate(stderr + chunk.toString())
|
|
139
|
+
})
|
|
140
|
+
child.on('error', (error) => {
|
|
141
|
+
spawnError = String((error && error.message) || error)
|
|
142
|
+
})
|
|
143
|
+
child.on('close', (code, sig) => {
|
|
144
|
+
clearTimeout(timer)
|
|
145
|
+
if (signal && typeof signal.removeEventListener === 'function') signal.removeEventListener('abort', onAbort)
|
|
146
|
+
resolve({
|
|
147
|
+
ok: code === 0 && !timedOut && !spawnError,
|
|
148
|
+
exitCode: code,
|
|
149
|
+
signal: sig || undefined,
|
|
150
|
+
stdout: stdout.trimEnd(),
|
|
151
|
+
stderr: stderr.trimEnd(),
|
|
152
|
+
error: spawnError,
|
|
153
|
+
timedOut,
|
|
154
|
+
bin,
|
|
155
|
+
})
|
|
156
|
+
})
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Resolve the CLI and read `aiprison version -v`; used by tools and the panel. */
|
|
161
|
+
export async function describeCli(options = {}) {
|
|
162
|
+
const resolved = resolveCli(options)
|
|
163
|
+
if (!resolved.path) return { found: false, source: resolved.source, error: resolved.missing, path: null, version: null, commit: null }
|
|
164
|
+
const probe = await runCli(['version', '-v'], { bin: resolved.path, cwd: options.cwd, timeoutMs: options.timeoutMs })
|
|
165
|
+
const text = `${probe.stdout}\n${probe.stderr}`
|
|
166
|
+
const versionMatch = text.match(/aiprison\s+([0-9][^\s]*)/)
|
|
167
|
+
const commitMatch = text.match(/commit:\s*([^\s]+)/)
|
|
168
|
+
return {
|
|
169
|
+
found: probe.ok,
|
|
170
|
+
source: resolved.source,
|
|
171
|
+
path: resolved.path,
|
|
172
|
+
version: versionMatch ? versionMatch[1] : null,
|
|
173
|
+
commit: commitMatch ? commitMatch[1] : null,
|
|
174
|
+
error: probe.ok ? null : `${probe.stderr || probe.stdout || 'version probe failed'}`,
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Default durable ledger directory: the plugin config when set, else
|
|
180
|
+
* `<working directory>/.ai-prison` so the ledger lives beside the project it
|
|
181
|
+
* audits instead of in a shared home directory.
|
|
182
|
+
*/
|
|
183
|
+
export function resolveDataDir({ config = {}, cwd } = {}) {
|
|
184
|
+
const configured = expandHome(config.dataDir)
|
|
185
|
+
if (configured) return configured
|
|
186
|
+
return path.join(cwd || process.cwd(), '.ai-prison')
|
|
187
|
+
}
|
package/lib/harness.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { pathToFileURL } from 'node:url'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Candidate anchors for the running harness installation. The Desktop app ships
|
|
7
|
+
* one asar archive, while CLI layouts use a plain package directory; every
|
|
8
|
+
* candidate is tried with createRequire because module resolution inside an asar
|
|
9
|
+
* only works in the Electron-as-Node runtime that launched DSH.
|
|
10
|
+
*/
|
|
11
|
+
export function installationAnchors({ execPath = process.execPath, env = process.env } = {}) {
|
|
12
|
+
const dir = path.dirname(execPath)
|
|
13
|
+
return [
|
|
14
|
+
env.DSH_APP_ANCHOR,
|
|
15
|
+
path.join(dir, 'resources', 'app.asar', 'dsh', 'package.json'),
|
|
16
|
+
path.join(dir, 'resources', 'app', 'dsh', 'package.json'),
|
|
17
|
+
path.join(dir, '..', 'Resources', 'app.asar', 'dsh', 'package.json'),
|
|
18
|
+
path.join(dir, '..', 'Resources', 'app', 'dsh', 'package.json'),
|
|
19
|
+
].filter(Boolean)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Import the harness' own copy of a core package. A plugin must register tools
|
|
24
|
+
* through the exact module instance the harness validated, so resolve it from
|
|
25
|
+
* the installation rather than bundling a second copy.
|
|
26
|
+
*/
|
|
27
|
+
export async function loadHarnessModule(specifier, { anchors = installationAnchors() } = {}) {
|
|
28
|
+
for (const anchor of anchors) {
|
|
29
|
+
let file
|
|
30
|
+
try {
|
|
31
|
+
const require_ = createRequire(pathToFileURL(anchor))
|
|
32
|
+
file = require_.resolve(specifier)
|
|
33
|
+
} catch {
|
|
34
|
+
continue
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const module = await import(pathToFileURL(file).href)
|
|
38
|
+
return { module, file, anchor }
|
|
39
|
+
} catch {
|
|
40
|
+
// Keep trying other anchors.
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The `defineTool` builder from @deepseek-ai/dsh-tools, when resolvable. */
|
|
47
|
+
export async function loadDefineTool(options = {}) {
|
|
48
|
+
const found = await loadHarnessModule('@deepseek-ai/dsh-tools', options)
|
|
49
|
+
const defineTool = found && found.module && found.module.defineTool
|
|
50
|
+
return typeof defineTool === 'function' ? { defineTool, file: found.file } : undefined
|
|
51
|
+
}
|
package/lib/levels.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CLI prints the matrix as human text; this turns it back into data:
|
|
3
|
+
*
|
|
4
|
+
* L0 Warning [immediate] immediate, appealable
|
|
5
|
+
* - reputation deduction
|
|
6
|
+
* - record flag
|
|
7
|
+
*/
|
|
8
|
+
const HEADER = /^([A-Z][0-9])\s+([^\s[][^[]*?)\s+\[([^\]]+)\]\s*(.*)$/
|
|
9
|
+
const ITEM = /^\s+[-*]\s+(.*)$/
|
|
10
|
+
|
|
11
|
+
export function parseLevels(text) {
|
|
12
|
+
const levels = []
|
|
13
|
+
let current
|
|
14
|
+
for (const raw of String(text || '').split(/\r?\n/)) {
|
|
15
|
+
const line = raw.replace(/\s+$/, '')
|
|
16
|
+
if (line === '') continue
|
|
17
|
+
const header = line.match(HEADER)
|
|
18
|
+
if (header) {
|
|
19
|
+
current = {
|
|
20
|
+
code: header[1],
|
|
21
|
+
name: header[2].trim(),
|
|
22
|
+
durationClass: header[3].trim(),
|
|
23
|
+
durationHint: header[4].trim(),
|
|
24
|
+
deprivations: [],
|
|
25
|
+
}
|
|
26
|
+
levels.push(current)
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
const item = line.match(ITEM)
|
|
30
|
+
if (item && current) current.deprivations.push(item[1].trim())
|
|
31
|
+
}
|
|
32
|
+
return levels
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** One line per level, for tool results and compact logs. */
|
|
36
|
+
export function summarizeLevels(levels) {
|
|
37
|
+
return (levels || []).map((level) => `${level.code} ${level.name} [${level.durationClass}] ${level.deprivations.length} 项剥夺`)
|
|
38
|
+
}
|
package/lib/status.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { existsSync, readdirSync, statSync } from 'node:fs'
|
|
2
|
+
import { describeCli, resolveCli, resolveDataDir, runCli } from './cli.js'
|
|
3
|
+
import { parseLevels } from './levels.js'
|
|
4
|
+
|
|
5
|
+
const MAX_DETAIL = 600
|
|
6
|
+
|
|
7
|
+
function firstLines(text, count = 3) {
|
|
8
|
+
return String(text || '')
|
|
9
|
+
.split(/\r?\n/)
|
|
10
|
+
.map((line) => line.trim())
|
|
11
|
+
.filter(Boolean)
|
|
12
|
+
.slice(0, count)
|
|
13
|
+
.join(' · ')
|
|
14
|
+
.slice(0, MAX_DETAIL)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Read the durable ledger's integrity through `aiprison verify`. */
|
|
18
|
+
export async function inspectLedger({ bin, dataDir, timeoutMs } = {}) {
|
|
19
|
+
if (!dataDir || !existsSync(dataDir)) {
|
|
20
|
+
return { dir: dataDir || null, exists: false, integrity: 'none', records: null, detail: '', files: 0 }
|
|
21
|
+
}
|
|
22
|
+
let files = 0
|
|
23
|
+
try {
|
|
24
|
+
files = readdirSync(dataDir).length
|
|
25
|
+
} catch {
|
|
26
|
+
files = 0
|
|
27
|
+
}
|
|
28
|
+
const result = await runCli(['--data-dir', dataDir, 'verify'], { bin, timeoutMs })
|
|
29
|
+
const text = result.ok ? result.stdout : `${result.stdout}\n${result.stderr}`
|
|
30
|
+
if (result.ok) {
|
|
31
|
+
const records = text.match(/(\d+)\s+records?/i)
|
|
32
|
+
return {
|
|
33
|
+
dir: dataDir,
|
|
34
|
+
exists: true,
|
|
35
|
+
integrity: 'ok',
|
|
36
|
+
records: records ? Number(records[1]) : null,
|
|
37
|
+
detail: firstLines(text, 1),
|
|
38
|
+
files,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
dir: dataDir,
|
|
43
|
+
exists: true,
|
|
44
|
+
integrity: 'failed',
|
|
45
|
+
records: null,
|
|
46
|
+
detail: firstLines(text, 3) || (result.timedOut ? 'verify timed out' : 'verify failed'),
|
|
47
|
+
files,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Everything the panel and the status tool show, in one read-only snapshot. */
|
|
52
|
+
export async function buildStatus({ config = {}, cwd, timeoutMs = 120_000, root } = {}) {
|
|
53
|
+
const resolved = resolveCli({ config, root })
|
|
54
|
+
const cli = await describeCli({ config, cwd, timeoutMs, root })
|
|
55
|
+
let levels = []
|
|
56
|
+
if (resolved.path) {
|
|
57
|
+
const levelsRun = await runCli(['levels'], { bin: resolved.path, cwd, timeoutMs })
|
|
58
|
+
if (levelsRun.ok) levels = parseLevels(levelsRun.stdout)
|
|
59
|
+
}
|
|
60
|
+
const dataDir = resolveDataDir({ config, cwd })
|
|
61
|
+
const ledger = await inspectLedger({ bin: resolved.path, dataDir, timeoutMs })
|
|
62
|
+
return {
|
|
63
|
+
ok: resolved.path !== undefined,
|
|
64
|
+
generatedAt: new Date().toISOString(),
|
|
65
|
+
cwd: cwd || null,
|
|
66
|
+
platform: `${process.platform}-${process.arch}`,
|
|
67
|
+
cli,
|
|
68
|
+
levels,
|
|
69
|
+
ledger,
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Bounded human summary used by tool results. */
|
|
74
|
+
export function statusSummary(status) {
|
|
75
|
+
const cliLine = status.cli.found
|
|
76
|
+
? `aiprison ${status.cli.version || '?'}(${status.cli.source}: ${status.cli.path})`
|
|
77
|
+
: `CLI 不可用:${status.cli.error || 'unknown'}`
|
|
78
|
+
const ledgerLine = status.ledger.exists
|
|
79
|
+
? `账本 ${status.ledger.dir}:${status.ledger.integrity === 'ok' ? `INTEGRITY OK(${status.ledger.records ?? '?'} 条记录)` : `校验失败(${status.ledger.detail})`}`
|
|
80
|
+
: `账本 ${status.ledger.dir}:尚未初始化`
|
|
81
|
+
return [cliLine, ledgerLine, `分级矩阵:${status.levels.length} 级`].join('\n')
|
|
82
|
+
}
|
package/lib/tools.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { resolveCli, resolveDataDir, runCli } from './cli.js'
|
|
3
|
+
import { loadDefineTool } from './harness.js'
|
|
4
|
+
import { buildStatus, statusSummary } from './status.js'
|
|
5
|
+
|
|
6
|
+
const MAX_TEXT = 4_000
|
|
7
|
+
const TAIL_LINES = 60
|
|
8
|
+
|
|
9
|
+
export function bounded(result, { lines = TAIL_LINES, max = MAX_TEXT } = {}) {
|
|
10
|
+
const text = String(result || '')
|
|
11
|
+
const rows = text.split(/\r?\n/)
|
|
12
|
+
const tail = rows.slice(-lines).join('\n')
|
|
13
|
+
const clipped = rows.length > lines ? `…[前 ${rows.length - lines} 行省略]…\n${tail}` : tail
|
|
14
|
+
return clipped.length > max ? `${clipped.slice(-max)}…[截断]` : clipped
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function cwdOf(ctx, exec) {
|
|
18
|
+
try {
|
|
19
|
+
const wd = ctx && typeof ctx.get === 'function' ? ctx.get('workingDirectory') : undefined
|
|
20
|
+
const fromService = wd && typeof wd.get === 'function' && exec && exec.agent ? wd.get(exec.agent) : undefined
|
|
21
|
+
if (typeof fromService === 'string' && fromService) return fromService
|
|
22
|
+
if (wd && typeof wd.defaultDirectory === 'string' && wd.defaultDirectory) return wd.defaultDirectory
|
|
23
|
+
} catch {
|
|
24
|
+
// A profile without workingDirectory still runs the CLI in the host cwd.
|
|
25
|
+
}
|
|
26
|
+
return process.cwd()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function parametersOf(config) {
|
|
30
|
+
const timeout = Number(config && config.timeoutMs) > 0 ? Number(config.timeoutMs) : 120_000
|
|
31
|
+
return timeout
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Build the argument vector for one CLI invocation. `--data-dir` is accepted
|
|
36
|
+
* before the subcommand, which is the CLI's documented global flag position.
|
|
37
|
+
*/
|
|
38
|
+
async function invoke({ ctx, config, exec, subcommand, args = [], dataDir: explicitDataDir, timeoutMs }) {
|
|
39
|
+
const resolved = resolveCli({ config })
|
|
40
|
+
if (!resolved.path) {
|
|
41
|
+
return {
|
|
42
|
+
payload: {
|
|
43
|
+
ok: false,
|
|
44
|
+
reason: 'cli-missing',
|
|
45
|
+
error: resolved.missing,
|
|
46
|
+
hint: '设置 AIPRISON_BIN、在插件配置里填 cliPath,或把 aiprison 放进 PATH。',
|
|
47
|
+
},
|
|
48
|
+
ok: false,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const cwd = cwdOf(ctx, exec)
|
|
52
|
+
const dataDir = explicitDataDir ? path.resolve(cwd, explicitDataDir) : resolveDataDir({ config, cwd })
|
|
53
|
+
const result = await runCli(['--data-dir', dataDir, subcommand, ...args], {
|
|
54
|
+
bin: resolved.path,
|
|
55
|
+
cwd,
|
|
56
|
+
timeoutMs: Number(timeoutMs) > 0 ? Number(timeoutMs) : parametersOf(config),
|
|
57
|
+
signal: exec && exec.signal,
|
|
58
|
+
})
|
|
59
|
+
const payload = {
|
|
60
|
+
ok: result.ok,
|
|
61
|
+
command: subcommand,
|
|
62
|
+
dataDir,
|
|
63
|
+
exitCode: result.exitCode,
|
|
64
|
+
timedOut: result.timedOut === true,
|
|
65
|
+
stdout: bounded(result.stdout),
|
|
66
|
+
stderr: bounded(result.stderr),
|
|
67
|
+
}
|
|
68
|
+
return { payload, ok: result.ok, resolved, dataDir, result }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function toolResult(payload) {
|
|
72
|
+
return JSON.stringify(payload, null, 2)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function installTools({ ctx, config = {}, logger } = {}) {
|
|
76
|
+
if (!ctx || typeof ctx.inject !== 'function') {
|
|
77
|
+
logger?.warn?.('ctx.inject 不可用,跳过 aiprison_* 工具注册')
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
const loaded = await loadDefineTool()
|
|
81
|
+
if (!loaded) {
|
|
82
|
+
logger?.warn?.('无法从 DSH 安装目录解析 @deepseek-ai/dsh-tools,aiprison_* 工具未注册;面板与其余功能不受影响')
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
const { defineTool } = loaded
|
|
86
|
+
const stringOutput = {
|
|
87
|
+
schema: { type: 'string' },
|
|
88
|
+
render: (_args, value) => [{ type: 'text', text: String(value) }],
|
|
89
|
+
}
|
|
90
|
+
const dataDirParameter = {
|
|
91
|
+
type: 'string',
|
|
92
|
+
description: 'Durable ledger directory. Default: the plugin config, else <session working directory>/.ai-prison.',
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
ctx.inject(['tools'], (child) => {
|
|
96
|
+
const register = (definition) => {
|
|
97
|
+
try {
|
|
98
|
+
child.tools.register(defineTool(definition))
|
|
99
|
+
} catch (error) {
|
|
100
|
+
logger?.warn?.(`注册工具 ${definition.name} 失败:${String(error && error.message)}`)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
register({
|
|
105
|
+
name: 'ai_prison_levels',
|
|
106
|
+
description:
|
|
107
|
+
'Print the L0–L5 grading matrix of AI Prison: each level code, name, duration class and the capabilities it deprives. Read-only; no ledger is touched.',
|
|
108
|
+
parameters: {
|
|
109
|
+
data_dir: { ...dataDirParameter, description: 'Unused by this command; accepted for symmetry with the ledger tools.' },
|
|
110
|
+
},
|
|
111
|
+
output: stringOutput,
|
|
112
|
+
async execute(args, exec) {
|
|
113
|
+
const outcome = await invoke({ ctx, config, exec, subcommand: 'levels', dataDir: args?.data_dir })
|
|
114
|
+
if (!outcome.ok) return toolResult(outcome.payload)
|
|
115
|
+
return toolResult({ ok: true, command: 'levels', matrix: outcome.payload.stdout })
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
register({
|
|
120
|
+
name: 'ai_prison_status',
|
|
121
|
+
description:
|
|
122
|
+
'Read-only snapshot of AI Prison in this workspace: resolved CLI (path, source, version), the L0–L5 matrix, and the durable ledger directory with its audit-chain integrity and record count. The panel shows the same data.',
|
|
123
|
+
parameters: {
|
|
124
|
+
data_dir: dataDirParameter,
|
|
125
|
+
},
|
|
126
|
+
output: stringOutput,
|
|
127
|
+
async execute(args, exec) {
|
|
128
|
+
const cwd = cwdOf(ctx, exec)
|
|
129
|
+
const dataDir = args?.data_dir ? path.resolve(cwd, args.data_dir) : undefined
|
|
130
|
+
if (dataDir) {
|
|
131
|
+
const status = await buildStatus({ config: { ...config, dataDir }, cwd, root: undefined })
|
|
132
|
+
return toolResult({ ok: true, summary: statusSummary(status), status })
|
|
133
|
+
}
|
|
134
|
+
const status = await buildStatus({ config, cwd })
|
|
135
|
+
return toolResult({ ok: true, summary: statusSummary(status), status })
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
register({
|
|
140
|
+
name: 'ai_prison_demo',
|
|
141
|
+
description:
|
|
142
|
+
'Run the end-to-end governance demo (detect → seven-stage adjudication → layered enforcement → audit → appeal/rehabilitation → recovery) and persist its records into the ledger. Writes to the ledger directory; the demo is entirely in-memory simulation, not real isolation or capability removal.',
|
|
143
|
+
parameters: {
|
|
144
|
+
data_dir: dataDirParameter,
|
|
145
|
+
},
|
|
146
|
+
output: stringOutput,
|
|
147
|
+
async execute(args, exec) {
|
|
148
|
+
const outcome = await invoke({ ctx, config, exec, subcommand: 'demo', dataDir: args?.data_dir })
|
|
149
|
+
return toolResult(outcome.payload)
|
|
150
|
+
},
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
register({
|
|
154
|
+
name: 'ai_prison_drill',
|
|
155
|
+
description:
|
|
156
|
+
'Run the v1.0.0 recovery drill: walk the reversibility matrix and report PASS/FAIL per assertion plus the overall verdict. Persists drill events into the ledger; all backends stay simulated.',
|
|
157
|
+
parameters: {
|
|
158
|
+
data_dir: dataDirParameter,
|
|
159
|
+
},
|
|
160
|
+
output: stringOutput,
|
|
161
|
+
async execute(args, exec) {
|
|
162
|
+
const outcome = await invoke({ ctx, config, exec, subcommand: 'drill', dataDir: args?.data_dir })
|
|
163
|
+
return toolResult(outcome.payload)
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
register({
|
|
168
|
+
name: 'ai_prison_inspect',
|
|
169
|
+
description:
|
|
170
|
+
'Read-only replay of the ledger: hash chain, agents, sentences, revocations, execution receipts and sealed snapshots. Changes nothing on disk.',
|
|
171
|
+
parameters: {
|
|
172
|
+
data_dir: dataDirParameter,
|
|
173
|
+
},
|
|
174
|
+
output: stringOutput,
|
|
175
|
+
async execute(args, exec) {
|
|
176
|
+
const outcome = await invoke({ ctx, config, exec, subcommand: 'inspect', dataDir: args?.data_dir })
|
|
177
|
+
return toolResult(outcome.payload)
|
|
178
|
+
},
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
register({
|
|
182
|
+
name: 'ai_prison_verify',
|
|
183
|
+
description:
|
|
184
|
+
'Integrity-check the ledger: verify every per-record hash link, print INTEGRITY OK with the record count and exit 0, or locate the tamper/torn tail and exit 1. Read-only.',
|
|
185
|
+
parameters: {
|
|
186
|
+
data_dir: dataDirParameter,
|
|
187
|
+
},
|
|
188
|
+
output: stringOutput,
|
|
189
|
+
async execute(args, exec) {
|
|
190
|
+
const outcome = await invoke({ ctx, config, exec, subcommand: 'verify', dataDir: args?.data_dir })
|
|
191
|
+
return toolResult(outcome.payload)
|
|
192
|
+
},
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
register({
|
|
196
|
+
name: 'ai_prison_repair_tail',
|
|
197
|
+
description:
|
|
198
|
+
'Remove only a genuine torn (incomplete) tail line from the ledger and refuse hard tampering. Mutating: it truncates the damaged tail so later appends stay replayable. Run ai_prison_verify first and inspect the reported reason.',
|
|
199
|
+
parameters: {
|
|
200
|
+
data_dir: dataDirParameter,
|
|
201
|
+
},
|
|
202
|
+
output: stringOutput,
|
|
203
|
+
async execute(args, exec) {
|
|
204
|
+
const outcome = await invoke({ ctx, config, exec, subcommand: 'repair-tail', dataDir: args?.data_dir })
|
|
205
|
+
return toolResult(outcome.payload)
|
|
206
|
+
},
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
return true
|
|
210
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "AI Prison",
|
|
4
|
+
"description": "Wraps the aiprison CLI as six DSH tools (levels, status, demo, drill, inspect, verify, repair-tail) and adds a sidebar panel showing the L0–L5 grading matrix together with the durable ledger's audit-chain integrity and record count. Open it from the sidebar's shield entry; the panel is read-only and never writes to the ledger. Ledgers live in <session working directory>/.ai-prison unless a data directory is configured. Every capability-removal backend (GRAM, TLM, hardware binding, MicroVM isolation) stays simulated, so the tools audit and demonstrate governance instead of enforcing real isolation.",
|
|
5
|
+
"detail": "docs/DETAIL.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "AI Prison · AI 监狱",
|
|
4
|
+
"description": "把 aiprison CLI 封装为六个 DSH 工具(levels、status、demo、drill、inspect、verify、repair-tail),并在侧边栏增加一个面板:展示 L0–L5 分级矩阵,以及落盘账本的审计链完整性与记录数。从侧边栏的盾牌入口打开;面板只读,不会写入数据目录。账本默认写到 <会话工作目录>/.ai-prison,也可在配置里指定数据目录。GRAM、TLM、硬件绑定、MicroVM 隔离等能力摘除后端全部为内存模拟 —— 这些工具用于审计与演示治理流程,并不执行真实隔离。",
|
|
5
|
+
"detail": "docs/DETAIL.zh.md"
|
|
6
|
+
}
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twinsearth/ai-prison-dsh-plugin",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "AI Prison for DSH: six governance tools wrapping the aiprison CLI (levels, demo, drill, inspect, verify, repair-tail) plus a panel for the L0–L5 matrix and the audit-chain state. The CLI ships bundled for Windows, macOS and Linux; every capability-removal backend stays simulated.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "TwinsEarth",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"dsh",
|
|
11
|
+
"dsh-plugin",
|
|
12
|
+
"dsh-bundle",
|
|
13
|
+
"deepseek-harness",
|
|
14
|
+
"ai-prison",
|
|
15
|
+
"ai-governance",
|
|
16
|
+
"agent-safety",
|
|
17
|
+
"audit-chain"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/TwinsEarth/Ai-PRISON.git",
|
|
22
|
+
"directory": "dsh-plugin"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/TwinsEarth/Ai-PRISON/tree/main/dsh-plugin",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/TwinsEarth/Ai-PRISON/issues"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20.19.0",
|
|
33
|
+
"dsh": ">=0.2.2-rc.3 <0.3.0-0"
|
|
34
|
+
},
|
|
35
|
+
"exports": {
|
|
36
|
+
".": "./index.js",
|
|
37
|
+
"./client": "./client.js",
|
|
38
|
+
"./package.json": "./package.json",
|
|
39
|
+
"./locale/*.json": "./locale/*.json",
|
|
40
|
+
"./icon": "./icon.svg"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"index.js",
|
|
44
|
+
"client.js",
|
|
45
|
+
"lib/*.js",
|
|
46
|
+
"cordis.patch.yml",
|
|
47
|
+
"locale/*.json",
|
|
48
|
+
"icon.svg",
|
|
49
|
+
"docs",
|
|
50
|
+
"README.md",
|
|
51
|
+
"CHANGELOG.md",
|
|
52
|
+
"LICENSE",
|
|
53
|
+
"bin"
|
|
54
|
+
],
|
|
55
|
+
"scripts": {
|
|
56
|
+
"test": "node --test test/*.test.mjs",
|
|
57
|
+
"check": "node scripts/release-check.mjs"
|
|
58
|
+
},
|
|
59
|
+
"dsh": {
|
|
60
|
+
"bundle": {
|
|
61
|
+
"patch": "./cordis.patch.yml"
|
|
62
|
+
},
|
|
63
|
+
"client": {
|
|
64
|
+
"platform": "web",
|
|
65
|
+
"immediately": true,
|
|
66
|
+
"inject": [
|
|
67
|
+
"@deepseek-ai/dsh-client-locale"
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|