skillmkt 0.2.6 → 0.2.8
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/client/commands/serve.js +19 -7
- package/client/lib/llm.js +42 -13
- package/client/public/app.js +36 -7
- package/package.json +1 -1
package/client/commands/serve.js
CHANGED
|
@@ -10,7 +10,7 @@ import { readLibrary, readLock, readConfig, writeConfig, registerSkill, updateLo
|
|
|
10
10
|
import { readUpstream } from '../lib/state.js';
|
|
11
11
|
import { readYAML } from '../../pipeline/lib/util.js';
|
|
12
12
|
import { detectHarnesses } from '../lib/scanner.js';
|
|
13
|
-
import { probeEndpoints, chat,
|
|
13
|
+
import { probeEndpoints, chat, encryptSecret, resolveKeyRef } from '../lib/llm.js';
|
|
14
14
|
import { projectSkill, unproject, targetDirFor } from '../lib/project.js';
|
|
15
15
|
import { treeHash } from '../lib/package.js';
|
|
16
16
|
import { ok, info } from '../lib/render.js';
|
|
@@ -85,12 +85,12 @@ export async function startServer(ctx, { quiet = false, port = 0 } = {}) {
|
|
|
85
85
|
}
|
|
86
86
|
if (u.pathname === '/api/config') { // init:合并写 config
|
|
87
87
|
const raw = readConfig(ctx.hub)._raw;
|
|
88
|
-
let
|
|
88
|
+
let keyEnc = false;
|
|
89
89
|
if (b.llm !== undefined) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
b.llm.api_key =
|
|
93
|
-
|
|
90
|
+
const k = String(b.llm.api_key || '');
|
|
91
|
+
if (k && !k.startsWith('env:') && !k.startsWith('enc:')) {
|
|
92
|
+
b.llm.api_key = encryptSecret(k); // 机器码加密后存 config.yml
|
|
93
|
+
keyEnc = true;
|
|
94
94
|
}
|
|
95
95
|
raw.llm = b.llm;
|
|
96
96
|
}
|
|
@@ -99,13 +99,25 @@ export async function startServer(ctx, { quiet = false, port = 0 } = {}) {
|
|
|
99
99
|
if (b.policy) raw.policy = b.policy;
|
|
100
100
|
writeConfig(ctx.hub, raw);
|
|
101
101
|
emit('config', {});
|
|
102
|
-
return res.end(JSON.stringify({ ok: true,
|
|
102
|
+
return res.end(JSON.stringify({ ok: true, key_enc: keyEnc }));
|
|
103
103
|
}
|
|
104
104
|
if (u.pathname === '/api/llm-smoke') { // init 冒烟
|
|
105
105
|
const key = resolveKeyRef(b.api_key, ctx); // env: 引用走 hub/.env 等解析链
|
|
106
106
|
const r = await chat({ base_url: b.base_url, model: b.model, api_key: key, timeout_ms: 15000 }, { user: 'Reply with exactly: OK', maxTokens: 8 });
|
|
107
107
|
return res.end(JSON.stringify({ ok: true, reply: r.trim().slice(0, 80) }));
|
|
108
108
|
}
|
|
109
|
+
if (u.pathname === '/api/models') { // 指定端点+密钥拉模型列表
|
|
110
|
+
const key = resolveKeyRef(b.api_key, ctx);
|
|
111
|
+
try {
|
|
112
|
+
const r = await fetch(`${String(b.base_url || '').replace(/\/+$/, '')}/models`, {
|
|
113
|
+
headers: key ? { authorization: `Bearer ${key}` } : {},
|
|
114
|
+
signal: AbortSignal.timeout(8000),
|
|
115
|
+
});
|
|
116
|
+
if (!r.ok) return res.end(JSON.stringify({ models: [], error: `http ${r.status}` }));
|
|
117
|
+
const d = await r.json();
|
|
118
|
+
return res.end(JSON.stringify({ models: (d.data || []).map((m) => m.id) }));
|
|
119
|
+
} catch (e) { return res.end(JSON.stringify({ models: [], error: e.message.slice(0, 80) })); }
|
|
120
|
+
}
|
|
109
121
|
if (u.pathname === '/api/targets') { // web 改投影目标
|
|
110
122
|
const name = b.name, targets = b.targets || [];
|
|
111
123
|
const lib = readLibrary(ctx.hub);
|
package/client/lib/llm.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// 批次6 补:chatJSON 真调用、probeEndpoints、端口探测。铁律:非 init/config 零探测。
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import crypto from 'node:crypto';
|
|
5
7
|
import { execFileSync } from 'node:child_process';
|
|
6
8
|
|
|
7
9
|
function loadDotEnv(file) {
|
|
@@ -15,20 +17,47 @@ function loadDotEnv(file) {
|
|
|
15
17
|
} catch { return {}; }
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
// 机器码绑定密钥加密:config.yml 只存 enc:v1: 密文,文件离开本机解不开
|
|
21
|
+
let _machineId = null;
|
|
22
|
+
function machineId() {
|
|
23
|
+
if (_machineId !== null) return _machineId;
|
|
24
|
+
try {
|
|
25
|
+
if (process.platform === 'darwin') {
|
|
26
|
+
_machineId = execFileSync('ioreg', ['-rd1', '-c', 'IOPlatformExpertDevice'], { encoding: 'utf8', timeout: 3000 })
|
|
27
|
+
.match(/"IOPlatformUUID" = "([0-9A-F-]+)"/)?.[1];
|
|
28
|
+
} else if (process.platform === 'linux') {
|
|
29
|
+
_machineId = fs.readFileSync('/etc/machine-id', 'utf8').trim() || fs.readFileSync('/var/lib/dbus/machine-id', 'utf8').trim();
|
|
30
|
+
} else if (process.platform === 'win32') {
|
|
31
|
+
_machineId = execFileSync('reg', ['query', 'HKLM\\SOFTWARE\\Microsoft\\Cryptography', '/v', 'MachineGuid'], { encoding: 'utf8' })
|
|
32
|
+
.match(/MachineGuid\s+REG_SZ\s+(\S+)/)?.[1];
|
|
33
|
+
}
|
|
34
|
+
} catch { /* 走兜底 */ }
|
|
35
|
+
if (!_machineId) _machineId = `${os.hostname()}:${os.userInfo().username}`;
|
|
36
|
+
return _machineId;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const encKey = () => crypto.scryptSync(`skillmkt:${machineId()}`, 'skillmkt-secret-v1', 32);
|
|
40
|
+
|
|
41
|
+
export function encryptSecret(plain) {
|
|
42
|
+
const iv = crypto.randomBytes(12);
|
|
43
|
+
const c = crypto.createCipheriv('aes-256-gcm', encKey(), iv);
|
|
44
|
+
const ct = Buffer.concat([c.update(String(plain), 'utf8'), c.final()]);
|
|
45
|
+
return `enc:v1:${iv.toString('base64')}:${c.getAuthTag().toString('base64')}:${ct.toString('base64')}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function decryptSecret(payload) {
|
|
49
|
+
const m = String(payload ?? '').match(/^enc:v1:([A-Za-z0-9+/=]+):([A-Za-z0-9+/=]+):([A-Za-z0-9+/=]+)$/);
|
|
50
|
+
if (!m) return null;
|
|
51
|
+
try {
|
|
52
|
+
const d = crypto.createDecipheriv('aes-256-gcm', encKey(), Buffer.from(m[1], 'base64'));
|
|
53
|
+
d.setAuthTag(Buffer.from(m[2], 'base64'));
|
|
54
|
+
return Buffer.concat([d.update(Buffer.from(m[3], 'base64')), d.final()]).toString('utf8');
|
|
55
|
+
} catch { return null; } // 别的机器/篡改 → 解不开
|
|
28
56
|
}
|
|
29
57
|
|
|
30
|
-
// "env:VAR" → 真值(hub/.env + cwd/.env +
|
|
58
|
+
// "env:VAR" → 真值(hub/.env + cwd/.env + 环境变量);"enc:v1:…" → 本机解密;裸值原样返回
|
|
31
59
|
export function resolveKeyRef(keyRef, ctx) {
|
|
60
|
+
if (keyRef?.startsWith('enc:')) return decryptSecret(keyRef) ?? '';
|
|
32
61
|
if (!keyRef?.startsWith('env:')) return keyRef || '';
|
|
33
62
|
const v = keyRef.slice(4);
|
|
34
63
|
const env = {
|
|
@@ -48,8 +77,8 @@ export function resolveLLM(ctx) {
|
|
|
48
77
|
const model = ctx.flags.model || env.SKILLMKT_LLM_MODEL || env.OMLX_MODEL || cfg.model;
|
|
49
78
|
const keyRef = ctx.flags.apiKeyEnv || env.SKILLMKT_LLM_API_KEY || env.OMLX_API_KEY || cfg.api_key;
|
|
50
79
|
if (!base || !model) return null;
|
|
51
|
-
// api_key
|
|
52
|
-
const key = keyRef?.startsWith('env:') ? env[keyRef.slice(4)] : keyRef;
|
|
80
|
+
// api_key:"env:VAR"→环境变量;"enc:v1:…"→本机解密;裸值(不建议)原样用
|
|
81
|
+
const key = keyRef?.startsWith('env:') ? env[keyRef.slice(4)] : keyRef?.startsWith('enc:') ? decryptSecret(keyRef) : keyRef;
|
|
53
82
|
return {
|
|
54
83
|
base_url: base.replace(/\/+$/, ''),
|
|
55
84
|
model,
|
package/client/public/app.js
CHANGED
|
@@ -151,37 +151,66 @@ async function render() {
|
|
|
151
151
|
<label class="field">模型名称</label><input type="text" id="mmodel" list="model-opts" placeholder="可输入关键字搜索,扫描后自动列出发现的模型" value="${esc(cfg.llm?.model || '')}">
|
|
152
152
|
<datalist id="model-opts"></datalist>
|
|
153
153
|
<label class="field">访问密钥</label><input type="text" id="mkey" value="${esc(cfg.llm?.api_key || '')}">
|
|
154
|
-
<p class="mut"
|
|
154
|
+
<p class="mut">本机服务一般留空。填了密钥会用本机机器码加密后存入 config.yml——文件拷到别的机器解不开,换机需重填。</p>
|
|
155
155
|
<div class="row"><button id="smoke">测试连接</button><span id="smoke-out" class="mut"></span></div></div>
|
|
156
156
|
<div class="card"><h3>第 2 步 · skill 默认安装位置</h3>
|
|
157
157
|
<p class="mut">勾选的 AI 工具会在你安装新 skill 时自动收到一份副本。已勾过的话保存即生效。</p>
|
|
158
158
|
${h.map((x) => `<label class="check"><input type="checkbox" data-h="${esc(x.id)}" ${sel.has(x.id) ? 'checked' : ''} ${x.state === 'absent' ? 'disabled' : ''}> ${esc(x.id)} ${pill(x.state)} <span class="mut">${esc(x.primaryDir || '')}</span></label>`).join('')}</div>
|
|
159
159
|
<div class="card"><h3>第 3 步 · 保存</h3>
|
|
160
160
|
<div class="row"><button class="primary" id="save">保存设置</button><span id="save-out" class="mut"></span></div></div>`;
|
|
161
|
-
const modelMap = new Map(); //
|
|
162
|
-
|
|
161
|
+
const modelMap = new Map(); // model → base_url(选中模型自动回填地址)
|
|
162
|
+
const loadedModels = new Map(); // 密钥端点拉到的模型——重扫后保留
|
|
163
|
+
let epsList = [], lastScan = 0;
|
|
164
|
+
// 模型下拉按当前服务地址过滤:地址匹配已扫描端点→只给它的模型;不匹配→给全部
|
|
165
|
+
const refreshOpts = () => {
|
|
166
|
+
const cur = app.querySelector('#murl').value.trim().replace(/\/+$/, '');
|
|
167
|
+
const known = epsList.some((e) => e.base_url === cur);
|
|
168
|
+
const list = [...modelMap.keys()].filter((m) => !known || modelMap.get(m) === cur);
|
|
169
|
+
app.querySelector('#model-opts').innerHTML = list.map((m) => `<option value="${esc(m)}">`).join('');
|
|
170
|
+
return list.length;
|
|
171
|
+
};
|
|
163
172
|
const doScan = async (quiet = false) => {
|
|
164
173
|
lastScan = Date.now();
|
|
165
174
|
if (!quiet) app.querySelector('#probe-out').textContent = '扫描中…';
|
|
166
175
|
const eps = await api('/probe');
|
|
176
|
+
epsList = eps;
|
|
167
177
|
modelMap.clear();
|
|
168
178
|
for (const e of eps) for (const m of e.models) if (!modelMap.has(m)) modelMap.set(m, e.base_url);
|
|
169
|
-
|
|
179
|
+
for (const [m, b] of loadedModels) modelMap.set(m, b);
|
|
180
|
+
refreshOpts();
|
|
170
181
|
app.querySelector('#eps').innerHTML = eps.map((e) => e.needs_key
|
|
171
182
|
? `<div class="ep" data-b="${esc(e.base_url)}">${esc(e.name)}:${e.port} — <span class="mut">需要密钥(点这行填地址,再填密钥、测试连接)</span></div>`
|
|
172
|
-
: `<div class="ep" data-b="${esc(e.base_url)}">${esc(e.name)}:${e.port} — ${e.models.length}
|
|
183
|
+
: `<div class="ep" data-b="${esc(e.base_url)}">${esc(e.name)}:${e.port} — ${e.models.length} 个模型(点这行选中,在「模型名称」下拉框里搜索)</div>`).join('')
|
|
173
184
|
|| '<p class="mut" style="margin-top:8px">没扫到——已尝试本机所有监听端口。确认模型服务已启动,或直接把服务地址填到下面</p>';
|
|
174
185
|
app.querySelector('#probe-out').textContent = `发现 ${eps.length} 个服务、${modelMap.size} 个模型`;
|
|
175
186
|
app.querySelectorAll('#eps [data-b]').forEach((d) => d.onclick = () => {
|
|
176
187
|
app.querySelectorAll('#eps .ep').forEach((x) => x.classList.remove('sel'));
|
|
177
188
|
d.classList.add('sel');
|
|
178
189
|
app.querySelector('#murl').value = d.dataset.b;
|
|
190
|
+
refreshOpts();
|
|
179
191
|
});
|
|
180
192
|
};
|
|
181
193
|
app.querySelector('#probe').onclick = () => doScan();
|
|
182
194
|
app.querySelector('#mmodel').addEventListener('input', (e) => {
|
|
183
195
|
const b = modelMap.get(e.target.value);
|
|
184
|
-
if (b) app.querySelector('#murl').value = b;
|
|
196
|
+
if (b) { app.querySelector('#murl').value = b; refreshOpts(); }
|
|
197
|
+
});
|
|
198
|
+
app.querySelector('#murl').addEventListener('input', () => refreshOpts());
|
|
199
|
+
// 需要密钥的端点:密钥填完自动拉它的模型列表
|
|
200
|
+
app.querySelector('#mkey').addEventListener('change', async () => {
|
|
201
|
+
const cur = app.querySelector('#murl').value.trim().replace(/\/+$/, '');
|
|
202
|
+
const ep = epsList.find((e) => e.base_url === cur);
|
|
203
|
+
const key = app.querySelector('#mkey').value.trim();
|
|
204
|
+
if (!ep?.needs_key || !key) return;
|
|
205
|
+
const out = app.querySelector('#smoke-out');
|
|
206
|
+
out.textContent = '读取模型列表…';
|
|
207
|
+
const r = await api('/models', { method: 'POST', body: JSON.stringify({ base_url: cur, api_key: key }) });
|
|
208
|
+
if (r.models?.length) {
|
|
209
|
+
for (const m of r.models) { modelMap.set(m, cur); loadedModels.set(m, cur); }
|
|
210
|
+
ep.needs_key = false; ep.models = r.models;
|
|
211
|
+
refreshOpts();
|
|
212
|
+
out.textContent = `已加载 ${r.models.length} 个模型`;
|
|
213
|
+
} else out.textContent = r.error ? `读取失败:${r.error}` : '密钥通了,但没读到模型';
|
|
185
214
|
});
|
|
186
215
|
// 点进模型框时自动重扫——下载了新模型不用先点「扫描」
|
|
187
216
|
app.querySelector('#mmodel').addEventListener('focus', () => { if (Date.now() - lastScan > 10e3) doScan(true); });
|
|
@@ -193,7 +222,7 @@ async function render() {
|
|
|
193
222
|
app.querySelector('#save').onclick = async () => {
|
|
194
223
|
const targets = [...app.querySelectorAll('[data-h]:checked')].map((x) => x.dataset.h);
|
|
195
224
|
const r = await api('/config', { method: 'POST', body: JSON.stringify({ llm: { base_url: app.querySelector('#murl').value || null, model: app.querySelector('#mmodel').value || null, api_key: app.querySelector('#mkey').value || undefined, concurrency: 2, timeout_ms: 60000 }, default_targets: targets }) });
|
|
196
|
-
app.querySelector('#save-out').textContent = r.ok ? `已保存到 config.yml${r.
|
|
225
|
+
app.querySelector('#save-out').textContent = r.ok ? `已保存到 config.yml${r.key_enc ? ',密钥已加密' : ''}` : `保存失败:${r.error}`;
|
|
197
226
|
refresh();
|
|
198
227
|
};
|
|
199
228
|
}
|