skillmkt 0.2.5 → 0.2.6
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 +12 -4
- package/client/lib/llm.js +28 -3
- package/client/public/app.js +2 -2
- 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 } from '../lib/llm.js';
|
|
13
|
+
import { probeEndpoints, chat, upsertEnvFile, 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,16 +85,24 @@ 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
|
-
|
|
88
|
+
let keyEnv = false;
|
|
89
|
+
if (b.llm !== undefined) {
|
|
90
|
+
if (b.llm.api_key && !String(b.llm.api_key).startsWith('env:')) {
|
|
91
|
+
upsertEnvFile(path.join(ctx.hub, '.env'), 'SKILLMKT_LLM_KEY', b.llm.api_key);
|
|
92
|
+
b.llm.api_key = 'env:SKILLMKT_LLM_KEY'; // config.yml 只留引用
|
|
93
|
+
keyEnv = true;
|
|
94
|
+
}
|
|
95
|
+
raw.llm = b.llm;
|
|
96
|
+
}
|
|
89
97
|
if (b.ui !== undefined) raw.ui = b.ui;
|
|
90
98
|
if (b.default_targets !== undefined) raw.default_targets = b.default_targets;
|
|
91
99
|
if (b.policy) raw.policy = b.policy;
|
|
92
100
|
writeConfig(ctx.hub, raw);
|
|
93
101
|
emit('config', {});
|
|
94
|
-
return res.end(
|
|
102
|
+
return res.end(JSON.stringify({ ok: true, key_env: keyEnv }));
|
|
95
103
|
}
|
|
96
104
|
if (u.pathname === '/api/llm-smoke') { // init 冒烟
|
|
97
|
-
const key = b.api_key
|
|
105
|
+
const key = resolveKeyRef(b.api_key, ctx); // env: 引用走 hub/.env 等解析链
|
|
98
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 });
|
|
99
107
|
return res.end(JSON.stringify({ ok: true, reply: r.trim().slice(0, 80) }));
|
|
100
108
|
}
|
package/client/lib/llm.js
CHANGED
|
@@ -4,10 +4,10 @@ import fs from 'node:fs';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { execFileSync } from 'node:child_process';
|
|
6
6
|
|
|
7
|
-
function loadDotEnv(
|
|
7
|
+
function loadDotEnv(file) {
|
|
8
8
|
try {
|
|
9
9
|
const out = {};
|
|
10
|
-
for (const line of fs.readFileSync(
|
|
10
|
+
for (const line of fs.readFileSync(file, 'utf8').split('\n')) {
|
|
11
11
|
const m = line.match(/^\s*([A-Z_][A-Z0-9_]*)\s*=\s*(.*)\s*$/);
|
|
12
12
|
if (m && !line.trim().startsWith('#')) out[m[1]] = m[2].replace(/^["']|["']$/g, '');
|
|
13
13
|
}
|
|
@@ -15,9 +15,34 @@ function loadDotEnv(cwd) {
|
|
|
15
15
|
} catch { return {}; }
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
// upsert KEY=value 到 env 文件(600 权限)——web 保存明文密钥时落到 hub/.env,config.yml 只留 env: 引用
|
|
19
|
+
export function upsertEnvFile(file, key, value) {
|
|
20
|
+
let lines = [];
|
|
21
|
+
try { lines = fs.readFileSync(file, 'utf8').split('\n'); } catch { /* 新文件 */ }
|
|
22
|
+
const idx = lines.findIndex((l) => l.startsWith(`${key}=`));
|
|
23
|
+
const entry = `${key}=${value}`;
|
|
24
|
+
if (idx >= 0) lines[idx] = entry; else lines.push(entry);
|
|
25
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
26
|
+
fs.writeFileSync(file, lines.filter(Boolean).join('\n') + '\n', { mode: 0o600 });
|
|
27
|
+
try { fs.chmodSync(file, 0o600); } catch { /* windows 无 mode */ }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// "env:VAR" → 真值(hub/.env + cwd/.env + 环境变量);裸值原样返回
|
|
31
|
+
export function resolveKeyRef(keyRef, ctx) {
|
|
32
|
+
if (!keyRef?.startsWith('env:')) return keyRef || '';
|
|
33
|
+
const v = keyRef.slice(4);
|
|
34
|
+
const env = {
|
|
35
|
+
...(ctx?.hub ? loadDotEnv(path.join(ctx.hub, '.env')) : {}),
|
|
36
|
+
...loadDotEnv(path.join(ctx?.cwd ?? '.', '.env')),
|
|
37
|
+
...process.env,
|
|
38
|
+
};
|
|
39
|
+
return env[v] ?? '';
|
|
40
|
+
}
|
|
41
|
+
|
|
18
42
|
export function resolveLLM(ctx) {
|
|
19
43
|
if (ctx.flags.noLlm) return null;
|
|
20
|
-
|
|
44
|
+
// 解析链:hub/.env < cwd/.env < 真环境变量(后者覆盖前者)
|
|
45
|
+
const env = { ...(ctx.hub ? loadDotEnv(path.join(ctx.hub, '.env')) : {}), ...loadDotEnv(path.join(ctx.cwd, '.env')), ...process.env };
|
|
21
46
|
const cfg = ctx.config.llm || {};
|
|
22
47
|
const base = ctx.flags.baseUrl || env.SKILLMKT_LLM_BASE_URL || env.OMLX_BASE_URL || cfg.base_url;
|
|
23
48
|
const model = ctx.flags.model || env.SKILLMKT_LLM_MODEL || env.OMLX_MODEL || cfg.model;
|
package/client/public/app.js
CHANGED
|
@@ -151,7 +151,7 @@ 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">本机服务一般留空。填了明文密钥会存到 <code>~/.skillmkt/.env</code>(仅本人可读),配置文件里只留引用——不会以明文落进 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>
|
|
@@ -193,7 +193,7 @@ async function render() {
|
|
|
193
193
|
app.querySelector('#save').onclick = async () => {
|
|
194
194
|
const targets = [...app.querySelectorAll('[data-h]:checked')].map((x) => x.dataset.h);
|
|
195
195
|
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 ?
|
|
196
|
+
app.querySelector('#save-out').textContent = r.ok ? `已保存到 config.yml${r.key_env ? ',密钥存入 .env' : ''}` : `保存失败:${r.error}`;
|
|
197
197
|
refresh();
|
|
198
198
|
};
|
|
199
199
|
}
|