@goodandready/dsh-context-lens 0.1.24 → 0.1.26
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 +14 -0
- package/README.md +47 -96
- package/README.ru.md +39 -113
- package/README.zh.md +42 -116
- package/lib/auto-compress.js +23 -4
- package/lib/client.js +356 -891
- package/lib/compression/log-compressor.js +53 -28
- package/lib/index.js +58 -173
- package/lib/tools.js +99 -113
- package/package.json +5 -12
- package/lib/http.js +0 -114
- package/lib/tokens/estimate.js +0 -6
- package/lib/tokens/tracker.js +0 -89
- package/lib/updater.js +0 -237
package/lib/updater.js
DELETED
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
-
import { readFile } from 'node:fs/promises';
|
|
4
|
-
import { homedir } from 'node:os';
|
|
5
|
-
import { basename, dirname, isAbsolute, resolve } from 'node:path';
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
|
-
import { writeJson, isTrustedUpdateRequest } from './http.js';
|
|
8
|
-
|
|
9
|
-
const VERSION_CACHE_MS = 60_000;
|
|
10
|
-
const UPDATE_TIMEOUT_MS = 120_000;
|
|
11
|
-
|
|
12
|
-
let latestCache;
|
|
13
|
-
|
|
14
|
-
function validProfileName(value) {
|
|
15
|
-
return typeof value === 'string' && value !== '' && value !== '.' && value !== '..'
|
|
16
|
-
&& !value.includes('/') && !value.includes('\\') && !/[\0-\x1f\x7f]/.test(value);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function profileNameFromArgv(argv) {
|
|
20
|
-
if (!Array.isArray(argv)) return undefined;
|
|
21
|
-
for (let index = 2; index < argv.length; index += 1) {
|
|
22
|
-
if (argv[index] === '--profile') return argv[index + 1];
|
|
23
|
-
if (typeof argv[index] === 'string' && argv[index].startsWith('--profile=')) {
|
|
24
|
-
return argv[index].slice('--profile='.length);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return undefined;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function findDshCliEntry() {
|
|
31
|
-
const value = process.argv[1];
|
|
32
|
-
if (typeof value !== 'string' || value.length === 0) return undefined;
|
|
33
|
-
const entry = value.startsWith('file:') ? fileURLToPath(value) : resolve(process.cwd(), value);
|
|
34
|
-
if (!existsSync(entry)) return undefined;
|
|
35
|
-
for (let directory = dirname(entry); ; directory = dirname(directory)) {
|
|
36
|
-
const manifestPath = resolve(directory, 'package.json');
|
|
37
|
-
if (existsSync(manifestPath)) {
|
|
38
|
-
try {
|
|
39
|
-
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
40
|
-
const bin = typeof manifest.bin === 'string'
|
|
41
|
-
? manifest.bin
|
|
42
|
-
: typeof manifest.bin === 'object' && manifest.bin !== null
|
|
43
|
-
? manifest.bin.dsh
|
|
44
|
-
: undefined;
|
|
45
|
-
if (manifest.name === '@deepseek-ai/dsh' && typeof bin === 'string'
|
|
46
|
-
&& !isAbsolute(bin) && resolve(directory, bin) === resolve(entry)) return entry;
|
|
47
|
-
} catch {
|
|
48
|
-
/* continue */
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
const parent = dirname(directory);
|
|
52
|
-
if (parent === directory) return undefined;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function detectRuntime() {
|
|
57
|
-
const profileDir = resolve(process.env.DSH_PROFILE_DIR
|
|
58
|
-
?? resolve(homedir(), '.dsh', 'profiles', 'web'));
|
|
59
|
-
const selected = profileNameFromArgv(process.argv);
|
|
60
|
-
const profileName = validProfileName(selected)
|
|
61
|
-
? selected
|
|
62
|
-
: validProfileName(basename(profileDir)) ? basename(profileDir) : 'web';
|
|
63
|
-
const cliEntry = findDshCliEntry();
|
|
64
|
-
return cliEntry === undefined ? { profileName, profileDir } : { profileName, profileDir, cliEntry };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function parseSemver(value) {
|
|
68
|
-
const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(String(value || ''));
|
|
69
|
-
if (match === null) return undefined;
|
|
70
|
-
return {
|
|
71
|
-
core: [Number(match[1]), Number(match[2]), Number(match[3])],
|
|
72
|
-
prerelease: match[4]?.split('.') ?? [],
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function comparePrerelease(candidateParts, currentParts) {
|
|
77
|
-
if (candidateParts.length === 0 && currentParts.length > 0) return 1;
|
|
78
|
-
if (candidateParts.length > 0 && currentParts.length === 0) return -1;
|
|
79
|
-
if (candidateParts.length === 0 && currentParts.length === 0) return 0;
|
|
80
|
-
|
|
81
|
-
const len = Math.max(candidateParts.length, currentParts.length);
|
|
82
|
-
for (let i = 0; i < len; i += 1) {
|
|
83
|
-
const a = candidateParts[i];
|
|
84
|
-
const b = currentParts[i];
|
|
85
|
-
if (a === undefined) return -1;
|
|
86
|
-
if (b === undefined) return 1;
|
|
87
|
-
if (a === b) continue;
|
|
88
|
-
|
|
89
|
-
const aNum = /^\d+$/.test(a) ? Number(a) : undefined;
|
|
90
|
-
const bNum = /^\d+$/.test(b) ? Number(b) : undefined;
|
|
91
|
-
|
|
92
|
-
if (aNum !== undefined && bNum !== undefined) {
|
|
93
|
-
return aNum > bNum ? 1 : -1;
|
|
94
|
-
}
|
|
95
|
-
if (aNum !== undefined && bNum === undefined) {
|
|
96
|
-
return -1;
|
|
97
|
-
}
|
|
98
|
-
if (aNum === undefined && bNum !== undefined) {
|
|
99
|
-
return 1;
|
|
100
|
-
}
|
|
101
|
-
return a.localeCompare(b) > 0 ? 1 : -1;
|
|
102
|
-
}
|
|
103
|
-
return 0;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function isNewerVersion(currentValue, candidateValue) {
|
|
107
|
-
const current = parseSemver(currentValue);
|
|
108
|
-
const candidate = parseSemver(candidateValue);
|
|
109
|
-
if (current === undefined || candidate === undefined) return false;
|
|
110
|
-
for (let index = 0; index < 3; index += 1) {
|
|
111
|
-
if (candidate.core[index] !== current.core[index]) return candidate.core[index] > current.core[index];
|
|
112
|
-
}
|
|
113
|
-
return comparePrerelease(candidate.prerelease, current.prerelease) > 0;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
async function fetchLatestVersion(packageName, registry) {
|
|
117
|
-
if (latestCache?.packageName === packageName && latestCache.registry === registry && Date.now() < latestCache.expiresAt) {
|
|
118
|
-
return latestCache.version;
|
|
119
|
-
}
|
|
120
|
-
try {
|
|
121
|
-
const reg = registry.replace(/\/+$/, '');
|
|
122
|
-
const response = await fetch(`${reg}/${encodeURIComponent(packageName)}/latest`, {
|
|
123
|
-
signal: AbortSignal.timeout(6000),
|
|
124
|
-
});
|
|
125
|
-
if (!response.ok) return undefined;
|
|
126
|
-
const value = await response.json();
|
|
127
|
-
if (typeof value?.version !== 'string' || !value.version) return undefined;
|
|
128
|
-
latestCache = { packageName, registry, version: value.version, expiresAt: Date.now() + VERSION_CACHE_MS };
|
|
129
|
-
return value.version;
|
|
130
|
-
} catch {
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async function readCurrentVersion(manifestUrl) {
|
|
136
|
-
const value = JSON.parse(await readFile(manifestUrl, 'utf8'));
|
|
137
|
-
if (typeof value?.version !== 'string' || !value.version) throw new Error('Cannot read current plugin version.');
|
|
138
|
-
return value.version;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export async function checkUpdateStatus(options, target = detectRuntime()) {
|
|
142
|
-
const current = await readCurrentVersion(options.manifestUrl);
|
|
143
|
-
const latest = await fetchLatestVersion(options.packageName, options.registry || 'https://registry.npmjs.org');
|
|
144
|
-
return {
|
|
145
|
-
packageName: options.packageName,
|
|
146
|
-
currentVersion: current,
|
|
147
|
-
...(latest === undefined ? {} : { latestVersion: latest }),
|
|
148
|
-
latestCheckFailed: latest === undefined,
|
|
149
|
-
updateAvailable: latest !== undefined && isNewerVersion(current, latest),
|
|
150
|
-
profileName: target.profileName,
|
|
151
|
-
canAutoUpdate: target.cliEntry !== undefined,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async function installExact(target, packageSpec, options) {
|
|
156
|
-
if (target.cliEntry === undefined) throw new Error('Automatic update is unavailable in this runtime.');
|
|
157
|
-
await new Promise((resolvePromise, reject) => {
|
|
158
|
-
// Note: per issue #61 / dsh-clinebot#24 / dsh-agent-loop-guard#38, do NOT disable pnpm minimum release age quarantine
|
|
159
|
-
const child = spawn(process.execPath, [
|
|
160
|
-
target.cliEntry, 'plugin', '--profile', target.profileName, 'add',
|
|
161
|
-
packageSpec,
|
|
162
|
-
`--registry=${options.registry || 'https://registry.npmjs.org/'}`,
|
|
163
|
-
], {
|
|
164
|
-
cwd: target.profileDir,
|
|
165
|
-
windowsHide: true,
|
|
166
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
167
|
-
env: { ...process.env, NO_COLOR: '1' },
|
|
168
|
-
});
|
|
169
|
-
let detail = '';
|
|
170
|
-
child.stdout?.on('data', (chunk) => { detail = (detail + String(chunk)).slice(-4000); });
|
|
171
|
-
child.stderr?.on('data', (chunk) => { detail = (detail + String(chunk)).slice(-4000); });
|
|
172
|
-
const timer = setTimeout(() => {
|
|
173
|
-
child.kill();
|
|
174
|
-
reject(new Error('Update timed out.'));
|
|
175
|
-
}, UPDATE_TIMEOUT_MS);
|
|
176
|
-
child.once('error', (error) => { clearTimeout(timer); reject(error); });
|
|
177
|
-
child.once('exit', (code) => {
|
|
178
|
-
clearTimeout(timer);
|
|
179
|
-
if (code === 0) resolvePromise();
|
|
180
|
-
else reject(new Error(detail.trim() || `Update exited with code ${String(code)}.`));
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function registerPluginUpdater(ctx, options) {
|
|
186
|
-
if (!ctx?.webServer?.register) return () => {};
|
|
187
|
-
let installing = false;
|
|
188
|
-
return ctx.webServer.register({
|
|
189
|
-
kind: 'exact',
|
|
190
|
-
path: options.endpoint,
|
|
191
|
-
handler: async (request, response) => {
|
|
192
|
-
try {
|
|
193
|
-
const target = detectRuntime();
|
|
194
|
-
if (request.method === 'GET' || request.method === 'HEAD') {
|
|
195
|
-
const payload = await checkUpdateStatus(options, target);
|
|
196
|
-
writeJson(response, 200, payload);
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
if (request.method !== 'POST') {
|
|
200
|
-
response.writeHead(405, { Allow: 'GET, HEAD, POST' });
|
|
201
|
-
response.end();
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
if (!isTrustedUpdateRequest(request)) {
|
|
205
|
-
writeJson(response, 403, { ok: false, error: 'Rejected non-local or cross-origin update request.' });
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
if (installing) {
|
|
209
|
-
writeJson(response, 409, { ok: false, error: 'This plugin is already updating.' });
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
installing = true;
|
|
213
|
-
try {
|
|
214
|
-
const before = await checkUpdateStatus(options, target);
|
|
215
|
-
if (before.latestVersion === undefined) {
|
|
216
|
-
writeJson(response, 503, { ok: false, error: 'The latest version is temporarily unavailable.' });
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
if (!before.updateAvailable) {
|
|
220
|
-
writeJson(response, 200, before);
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
await installExact(target, `${options.packageName}@${before.latestVersion}`, options);
|
|
224
|
-
writeJson(response, 200, {
|
|
225
|
-
...before,
|
|
226
|
-
updatedVersion: before.latestVersion,
|
|
227
|
-
restartRequired: true,
|
|
228
|
-
});
|
|
229
|
-
} finally {
|
|
230
|
-
installing = false;
|
|
231
|
-
}
|
|
232
|
-
} catch (error) {
|
|
233
|
-
writeJson(response, 500, { ok: false, error: String(error?.message || error) });
|
|
234
|
-
}
|
|
235
|
-
},
|
|
236
|
-
});
|
|
237
|
-
}
|