shennian 0.2.6 → 0.2.7
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/dist/bin/shennian.d.ts +2 -0
- package/dist/bin/shennian.js +2 -0
- package/dist/src/agents/adapter.d.ts +35 -0
- package/dist/src/agents/adapter.js +19 -0
- package/dist/src/agents/claude.d.ts +25 -0
- package/dist/src/agents/claude.js +234 -0
- package/dist/src/agents/codex.d.ts +52 -0
- package/dist/src/agents/codex.js +829 -0
- package/dist/src/agents/command-spec.d.ts +36 -0
- package/dist/src/agents/command-spec.js +303 -0
- package/dist/src/agents/cursor.d.ts +22 -0
- package/dist/src/agents/cursor.js +248 -0
- package/dist/src/agents/custom.d.ts +30 -0
- package/dist/src/agents/custom.js +210 -0
- package/dist/src/agents/detect.d.ts +9 -0
- package/dist/src/agents/detect.js +46 -0
- package/dist/src/agents/gemini.d.ts +17 -0
- package/dist/src/agents/gemini.js +172 -0
- package/dist/src/agents/model-registry/cache.d.ts +4 -0
- package/dist/src/agents/model-registry/cache.js +37 -0
- package/dist/src/agents/model-registry/discovery.d.ts +4 -0
- package/dist/src/agents/model-registry/discovery.js +166 -0
- package/dist/src/agents/model-registry/parsers.d.ts +11 -0
- package/dist/src/agents/model-registry/parsers.js +349 -0
- package/dist/src/agents/model-registry/runner.d.ts +6 -0
- package/dist/src/agents/model-registry/runner.js +29 -0
- package/dist/src/agents/model-registry/service.d.ts +6 -0
- package/dist/src/agents/model-registry/service.js +71 -0
- package/dist/src/agents/model-registry/types.d.ts +30 -0
- package/dist/src/agents/model-registry/types.js +8 -0
- package/dist/src/agents/model-registry.d.ts +13 -0
- package/dist/src/agents/model-registry.js +15 -0
- package/dist/src/agents/openclaw.d.ts +20 -0
- package/dist/src/agents/openclaw.js +273 -0
- package/dist/src/agents/pi.d.ts +51 -0
- package/dist/src/agents/pi.js +793 -0
- package/dist/src/commands/agent.d.ts +2 -0
- package/dist/src/commands/agent.js +131 -0
- package/dist/src/commands/daemon.d.ts +35 -0
- package/dist/src/commands/daemon.js +612 -0
- package/dist/src/commands/pair-qr.d.ts +4 -0
- package/dist/src/commands/pair-qr.js +6 -0
- package/dist/src/commands/pair.d.ts +8 -0
- package/dist/src/commands/pair.js +162 -0
- package/dist/src/commands/upgrade.d.ts +5 -0
- package/dist/src/commands/upgrade.js +145 -0
- package/dist/src/config/index.d.ts +32 -0
- package/dist/src/config/index.js +31 -0
- package/dist/src/fs/handler.d.ts +26 -0
- package/dist/src/fs/handler.js +130 -0
- package/dist/src/fs/security.d.ts +2 -0
- package/dist/src/fs/security.js +32 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +277 -0
- package/dist/src/log-reporter.d.ts +18 -0
- package/dist/src/log-reporter.js +16 -0
- package/dist/src/native-fusion/parsers.d.ts +12 -0
- package/dist/src/native-fusion/parsers.js +687 -0
- package/dist/src/native-fusion/service.d.ts +25 -0
- package/dist/src/native-fusion/service.js +176 -0
- package/dist/src/native-fusion/state.d.ts +3 -0
- package/dist/src/native-fusion/state.js +22 -0
- package/dist/src/native-fusion/types.d.ts +24 -0
- package/dist/src/native-fusion/types.js +1 -0
- package/dist/src/region.d.ts +15 -0
- package/dist/src/region.js +99 -0
- package/dist/src/relay/client.d.ts +62 -0
- package/dist/src/relay/client.js +311 -0
- package/dist/src/session/handlers/agents.d.ts +4 -0
- package/dist/src/session/handlers/agents.js +55 -0
- package/dist/src/session/handlers/chat.d.ts +4 -0
- package/dist/src/session/handlers/chat.js +326 -0
- package/dist/src/session/handlers/control.d.ts +5 -0
- package/dist/src/session/handlers/control.js +57 -0
- package/dist/src/session/handlers/fs.d.ts +10 -0
- package/dist/src/session/handlers/fs.js +256 -0
- package/dist/src/session/manager.d.ts +28 -0
- package/dist/src/session/manager.js +174 -0
- package/dist/src/session/store.d.ts +7 -0
- package/dist/src/session/store.js +46 -0
- package/dist/src/session/types.d.ts +35 -0
- package/dist/src/session/types.js +4 -0
- package/dist/src/upgrade/engine.d.ts +64 -0
- package/dist/src/upgrade/engine.js +238 -0
- package/package.json +2 -2
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export type UpgradeProgress = {
|
|
2
|
+
step: 'checking';
|
|
3
|
+
} | {
|
|
4
|
+
step: 'backing-up';
|
|
5
|
+
} | {
|
|
6
|
+
step: 'installing';
|
|
7
|
+
version: string;
|
|
8
|
+
} | {
|
|
9
|
+
step: 'verifying';
|
|
10
|
+
version: string;
|
|
11
|
+
} | {
|
|
12
|
+
step: 'restarting';
|
|
13
|
+
from: string;
|
|
14
|
+
to: string;
|
|
15
|
+
} | {
|
|
16
|
+
step: 'done';
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
};
|
|
20
|
+
export type UpgradeResult = {
|
|
21
|
+
ok: true;
|
|
22
|
+
from: string;
|
|
23
|
+
to: string;
|
|
24
|
+
} | {
|
|
25
|
+
ok: false;
|
|
26
|
+
error: string;
|
|
27
|
+
rolledBack?: boolean;
|
|
28
|
+
};
|
|
29
|
+
type UpgradeAttempt = {
|
|
30
|
+
from: string;
|
|
31
|
+
to: string;
|
|
32
|
+
attemptCount: number;
|
|
33
|
+
attemptAt: number;
|
|
34
|
+
};
|
|
35
|
+
export declare function getCurrentVersion(): string;
|
|
36
|
+
export declare function fetchLatestVersion(): Promise<string>;
|
|
37
|
+
/**
|
|
38
|
+
* Compare semver strings. Returns:
|
|
39
|
+
* 'none' — same or older
|
|
40
|
+
* 'patch' — only patch differs
|
|
41
|
+
* 'minor' — minor or patch differs
|
|
42
|
+
* 'major' — major differs
|
|
43
|
+
*/
|
|
44
|
+
export declare function compareVersions(current: string, latest: string): 'none' | 'patch' | 'minor' | 'major';
|
|
45
|
+
export declare function readUpgradeAttempt(): UpgradeAttempt | null;
|
|
46
|
+
export declare function writeUpgradeAttempt(attempt: UpgradeAttempt): void;
|
|
47
|
+
export declare function clearUpgradeAttempt(): void;
|
|
48
|
+
/**
|
|
49
|
+
* Called on daemon startup. Detects crash loops from a failed upgrade and
|
|
50
|
+
* initiates rollback if crash count exceeds threshold.
|
|
51
|
+
* Returns true if rollback was performed (caller should exit and let service manager restart).
|
|
52
|
+
*/
|
|
53
|
+
export declare function handleStartupCrashCheck(): Promise<boolean>;
|
|
54
|
+
export declare function performUpgrade(targetVersion: string, onProgress: (p: UpgradeProgress) => void): Promise<UpgradeResult>;
|
|
55
|
+
export type VersionCheckResult = {
|
|
56
|
+
hasUpdate: false;
|
|
57
|
+
} | {
|
|
58
|
+
hasUpdate: true;
|
|
59
|
+
current: string;
|
|
60
|
+
latest: string;
|
|
61
|
+
changeType: 'patch' | 'minor' | 'major';
|
|
62
|
+
};
|
|
63
|
+
export declare function checkForUpdate(): Promise<VersionCheckResult>;
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
// @arch docs/architecture/cli/upgrade.md
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { execSync, exec as execCb } from 'node:child_process';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
import { getShennianDir, resolveShennianPath } from '../config/index.js';
|
|
7
|
+
const exec = promisify(execCb);
|
|
8
|
+
const SHENNIAN_DIR = getShennianDir();
|
|
9
|
+
const BACKUP_DIR = resolveShennianPath('backup');
|
|
10
|
+
const UPGRADE_ATTEMPT_FILE = resolveShennianPath('upgrade-attempt.json');
|
|
11
|
+
const MAX_CRASH_COUNT = 3;
|
|
12
|
+
const BACKUP_TTL_DAYS = 7;
|
|
13
|
+
// ─── Version helpers ─────────────────────────────────────────────────────────
|
|
14
|
+
export function getCurrentVersion() {
|
|
15
|
+
try {
|
|
16
|
+
const pkgPath = findPackageJson();
|
|
17
|
+
if (pkgPath) {
|
|
18
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
19
|
+
return pkg.version;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch { /* noop */ }
|
|
23
|
+
return '0.0.0';
|
|
24
|
+
}
|
|
25
|
+
function findPackageJson() {
|
|
26
|
+
// Walk up from this file's location to find package.json
|
|
27
|
+
let dir = path.dirname(new URL(import.meta.url).pathname);
|
|
28
|
+
for (let i = 0; i < 6; i++) {
|
|
29
|
+
const candidate = path.join(dir, 'package.json');
|
|
30
|
+
if (fs.existsSync(candidate)) {
|
|
31
|
+
const pkg = JSON.parse(fs.readFileSync(candidate, 'utf-8'));
|
|
32
|
+
if (pkg.name === 'shennian')
|
|
33
|
+
return candidate;
|
|
34
|
+
}
|
|
35
|
+
dir = path.dirname(dir);
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
export async function fetchLatestVersion() {
|
|
40
|
+
const res = await fetch('https://registry.npmjs.org/shennian/latest', {
|
|
41
|
+
headers: { Accept: 'application/json' },
|
|
42
|
+
signal: AbortSignal.timeout(10_000),
|
|
43
|
+
});
|
|
44
|
+
if (!res.ok)
|
|
45
|
+
throw new Error(`npm registry returned ${res.status}`);
|
|
46
|
+
const data = (await res.json());
|
|
47
|
+
return data.version;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Compare semver strings. Returns:
|
|
51
|
+
* 'none' — same or older
|
|
52
|
+
* 'patch' — only patch differs
|
|
53
|
+
* 'minor' — minor or patch differs
|
|
54
|
+
* 'major' — major differs
|
|
55
|
+
*/
|
|
56
|
+
export function compareVersions(current, latest) {
|
|
57
|
+
const parse = (v) => v.replace(/^v/, '').split('.').map(Number);
|
|
58
|
+
const [cMaj, cMin, cPat] = parse(current);
|
|
59
|
+
const [lMaj, lMin, lPat] = parse(latest);
|
|
60
|
+
if (lMaj > cMaj)
|
|
61
|
+
return 'major';
|
|
62
|
+
if (lMaj === cMaj && lMin > cMin)
|
|
63
|
+
return 'minor';
|
|
64
|
+
if (lMaj === cMaj && lMin === cMin && lPat > cPat)
|
|
65
|
+
return 'patch';
|
|
66
|
+
return 'none';
|
|
67
|
+
}
|
|
68
|
+
// ─── npm global path helpers ──────────────────────────────────────────────────
|
|
69
|
+
function getNpmGlobalRoot() {
|
|
70
|
+
return execSync('npm root -g', { encoding: 'utf-8', stdio: 'pipe' }).trim();
|
|
71
|
+
}
|
|
72
|
+
function getGlobalPkgDir() {
|
|
73
|
+
return path.join(getNpmGlobalRoot(), 'shennian');
|
|
74
|
+
}
|
|
75
|
+
function getGlobalBinScript() {
|
|
76
|
+
const root = getNpmGlobalRoot();
|
|
77
|
+
return path.join(root, 'shennian', 'dist', 'bin', 'shennian.js');
|
|
78
|
+
}
|
|
79
|
+
// ─── Backup / Restore ─────────────────────────────────────────────────────────
|
|
80
|
+
function backupVersion(version) {
|
|
81
|
+
const pkgDir = getGlobalPkgDir();
|
|
82
|
+
const dest = path.join(BACKUP_DIR, version);
|
|
83
|
+
fs.mkdirSync(dest, { recursive: true });
|
|
84
|
+
fs.cpSync(pkgDir, dest, { recursive: true });
|
|
85
|
+
}
|
|
86
|
+
function restoreVersion(version) {
|
|
87
|
+
const src = path.join(BACKUP_DIR, version);
|
|
88
|
+
if (!fs.existsSync(src))
|
|
89
|
+
throw new Error(`Backup for ${version} not found`);
|
|
90
|
+
const pkgDir = getGlobalPkgDir();
|
|
91
|
+
fs.cpSync(src, pkgDir, { recursive: true, force: true });
|
|
92
|
+
}
|
|
93
|
+
function cleanOldBackups() {
|
|
94
|
+
try {
|
|
95
|
+
if (!fs.existsSync(BACKUP_DIR))
|
|
96
|
+
return;
|
|
97
|
+
const cutoff = Date.now() - BACKUP_TTL_DAYS * 86400_000;
|
|
98
|
+
for (const entry of fs.readdirSync(BACKUP_DIR)) {
|
|
99
|
+
const full = path.join(BACKUP_DIR, entry);
|
|
100
|
+
const stat = fs.statSync(full);
|
|
101
|
+
if (stat.mtimeMs < cutoff) {
|
|
102
|
+
fs.rmSync(full, { recursive: true, force: true });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch { /* noop */ }
|
|
107
|
+
}
|
|
108
|
+
// ─── Upgrade attempt tracking (crash detection) ───────────────────────────────
|
|
109
|
+
export function readUpgradeAttempt() {
|
|
110
|
+
try {
|
|
111
|
+
return JSON.parse(fs.readFileSync(UPGRADE_ATTEMPT_FILE, 'utf-8'));
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export function writeUpgradeAttempt(attempt) {
|
|
118
|
+
fs.mkdirSync(SHENNIAN_DIR, { recursive: true });
|
|
119
|
+
fs.writeFileSync(UPGRADE_ATTEMPT_FILE, JSON.stringify(attempt, null, 2));
|
|
120
|
+
}
|
|
121
|
+
export function clearUpgradeAttempt() {
|
|
122
|
+
try {
|
|
123
|
+
fs.unlinkSync(UPGRADE_ATTEMPT_FILE);
|
|
124
|
+
}
|
|
125
|
+
catch { /* noop */ }
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Called on daemon startup. Detects crash loops from a failed upgrade and
|
|
129
|
+
* initiates rollback if crash count exceeds threshold.
|
|
130
|
+
* Returns true if rollback was performed (caller should exit and let service manager restart).
|
|
131
|
+
*/
|
|
132
|
+
export async function handleStartupCrashCheck() {
|
|
133
|
+
const attempt = readUpgradeAttempt();
|
|
134
|
+
if (!attempt)
|
|
135
|
+
return false;
|
|
136
|
+
const elapsed = Date.now() - attempt.attemptAt;
|
|
137
|
+
// If more than 10 minutes have passed, this is a stale record — clear and continue
|
|
138
|
+
if (elapsed > 10 * 60 * 1000) {
|
|
139
|
+
clearUpgradeAttempt();
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
attempt.attemptCount++;
|
|
143
|
+
if (attempt.attemptCount >= MAX_CRASH_COUNT) {
|
|
144
|
+
console.error(`[upgrade] New version crashed ${attempt.attemptCount} times, rolling back to ${attempt.from}...`);
|
|
145
|
+
try {
|
|
146
|
+
restoreVersion(attempt.from);
|
|
147
|
+
clearUpgradeAttempt();
|
|
148
|
+
console.error(`[upgrade] Rolled back to ${attempt.from}. Restarting...`);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
catch (err) {
|
|
152
|
+
console.error(`[upgrade] Rollback failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
153
|
+
clearUpgradeAttempt();
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
writeUpgradeAttempt(attempt);
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
// ─── Core upgrade ─────────────────────────────────────────────────────────────
|
|
161
|
+
export async function performUpgrade(targetVersion, onProgress) {
|
|
162
|
+
const currentVersion = getCurrentVersion();
|
|
163
|
+
if (currentVersion === targetVersion) {
|
|
164
|
+
return { ok: false, error: `Already on version ${targetVersion}` };
|
|
165
|
+
}
|
|
166
|
+
// Step 1: Verify npm is accessible
|
|
167
|
+
onProgress({ step: 'checking' });
|
|
168
|
+
try {
|
|
169
|
+
execSync('npm --version', { stdio: 'pipe' });
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return { ok: false, error: 'npm is not available in PATH' };
|
|
173
|
+
}
|
|
174
|
+
// Step 2: Backup current version
|
|
175
|
+
onProgress({ step: 'backing-up' });
|
|
176
|
+
try {
|
|
177
|
+
fs.mkdirSync(BACKUP_DIR, { recursive: true });
|
|
178
|
+
backupVersion(currentVersion);
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
return { ok: false, error: `Backup failed: ${err instanceof Error ? err.message : String(err)}` };
|
|
182
|
+
}
|
|
183
|
+
// Step 3: npm install new version
|
|
184
|
+
onProgress({ step: 'installing', version: targetVersion });
|
|
185
|
+
try {
|
|
186
|
+
await exec(`npm install -g shennian@${targetVersion}`, { timeout: 120_000 });
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
// Restore backup and abort
|
|
190
|
+
try {
|
|
191
|
+
restoreVersion(currentVersion);
|
|
192
|
+
}
|
|
193
|
+
catch { /* best effort */ }
|
|
194
|
+
return {
|
|
195
|
+
ok: false,
|
|
196
|
+
error: `npm install failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
197
|
+
rolledBack: true,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
// Step 4: Smoke test — verify the new binary works
|
|
201
|
+
onProgress({ step: 'verifying', version: targetVersion });
|
|
202
|
+
try {
|
|
203
|
+
const binScript = getGlobalBinScript();
|
|
204
|
+
const { stdout } = await exec(`node "${binScript}" --version`, { timeout: 10_000 });
|
|
205
|
+
if (!stdout.trim())
|
|
206
|
+
throw new Error('Empty output from --version check');
|
|
207
|
+
}
|
|
208
|
+
catch (err) {
|
|
209
|
+
try {
|
|
210
|
+
restoreVersion(currentVersion);
|
|
211
|
+
}
|
|
212
|
+
catch { /* best effort */ }
|
|
213
|
+
return {
|
|
214
|
+
ok: false,
|
|
215
|
+
error: `Smoke test failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
216
|
+
rolledBack: true,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
// Step 5: Write upgrade attempt file (crash detection on restart)
|
|
220
|
+
writeUpgradeAttempt({
|
|
221
|
+
from: currentVersion,
|
|
222
|
+
to: targetVersion,
|
|
223
|
+
attemptCount: 0,
|
|
224
|
+
attemptAt: Date.now(),
|
|
225
|
+
});
|
|
226
|
+
cleanOldBackups();
|
|
227
|
+
// Step 6: Signal restart (caller sends upgrade.status: restarting before killing)
|
|
228
|
+
onProgress({ step: 'restarting', from: currentVersion, to: targetVersion });
|
|
229
|
+
return { ok: true, from: currentVersion, to: targetVersion };
|
|
230
|
+
}
|
|
231
|
+
export async function checkForUpdate() {
|
|
232
|
+
const current = getCurrentVersion();
|
|
233
|
+
const latest = await fetchLatestVersion();
|
|
234
|
+
const changeType = compareVersions(current, latest);
|
|
235
|
+
if (changeType === 'none')
|
|
236
|
+
return { hasUpdate: false };
|
|
237
|
+
return { hasUpdate: true, current, latest, changeType };
|
|
238
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shennian",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "Shennian — AI Agent Mobile Console CLI",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsc",
|
|
53
|
-
"build:publish": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.publish.json",
|
|
53
|
+
"build:publish": "node -e \"const fs=require('node:fs'); fs.rmSync('dist', { recursive: true, force: true }); fs.rmSync('.tsbuildinfo.publish', { force: true })\" && tsc -p tsconfig.publish.json",
|
|
54
54
|
"dev": "tsc --watch"
|
|
55
55
|
}
|
|
56
56
|
}
|