skillmkt 0.2.4 → 0.2.5
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/init.js +2 -2
- package/client/commands/serve.js +1 -1
- package/client/lib/llm.js +34 -6
- package/client/public/app.js +1 -1
- package/package.json +1 -1
package/client/commands/init.js
CHANGED
|
@@ -47,7 +47,7 @@ export async function run(ctx) {
|
|
|
47
47
|
|
|
48
48
|
// LLM 端点探测(只在用户进入配置步骤时探测——铁律)
|
|
49
49
|
info('探测本地 LLM 服务…');
|
|
50
|
-
const found = await probeEndpoints();
|
|
50
|
+
const found = await probeEndpoints(400, { extraBases: [ctx.config.llm?.base_url] });
|
|
51
51
|
let base_url = null, model = null;
|
|
52
52
|
|
|
53
53
|
if (found.length) {
|
|
@@ -69,7 +69,7 @@ export async function run(ctx) {
|
|
|
69
69
|
({ base_url, model } = sel.v);
|
|
70
70
|
}
|
|
71
71
|
} else {
|
|
72
|
-
info('未发现本地 LLM
|
|
72
|
+
info('未发现本地 LLM 服务(已扫本机所有监听端口)');
|
|
73
73
|
info('→ 手动:skillmkt config set llm.base_url http://host:port/v1');
|
|
74
74
|
}
|
|
75
75
|
|
package/client/commands/serve.js
CHANGED
|
@@ -45,7 +45,7 @@ export async function startServer(ctx, { quiet = false, port = 0 } = {}) {
|
|
|
45
45
|
'GET /api/conflicts': () => loadConflicts(ctx.hub),
|
|
46
46
|
'GET /api/decisions': () => loadDecisions(ctx.hub),
|
|
47
47
|
'GET /api/config': () => readConfig(ctx.hub)._raw,
|
|
48
|
-
'GET /api/probe': async () => probeEndpoints(
|
|
48
|
+
'GET /api/probe': async () => probeEndpoints(400, { extraBases: [readConfig(ctx.hub).llm?.base_url] }),
|
|
49
49
|
};
|
|
50
50
|
const body = (req) => new Promise((r) => { let b = ''; req.on('data', (c) => b += c); req.on('end', () => r(b || '{}')); });
|
|
51
51
|
|
package/client/lib/llm.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// 批次6 补:chatJSON 真调用、probeEndpoints、端口探测。铁律:非 init/config 零探测。
|
|
3
3
|
import fs from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
+
import { execFileSync } from 'node:child_process';
|
|
5
6
|
|
|
6
7
|
function loadDotEnv(cwd) {
|
|
7
8
|
try {
|
|
@@ -57,6 +58,21 @@ export async function chat(llm, { system = '', user, maxTokens = 64, temperature
|
|
|
57
58
|
|
|
58
59
|
export const LLM_PROBES = [8001, 11434, 1234, 8000, 8080, 1337, 5000];
|
|
59
60
|
|
|
61
|
+
// 本机所有 LISTEN 端口(lsof)——模型服务换任意端口都能被扫到;拿不到就只用固定列表
|
|
62
|
+
function listeningPorts() {
|
|
63
|
+
try {
|
|
64
|
+
const out = execFileSync('lsof', ['-nP', '-iTCP', '-sTCP:LISTEN'], { encoding: 'utf8', timeout: 4000 });
|
|
65
|
+
const ports = new Set();
|
|
66
|
+
for (const line of out.split('\n')) {
|
|
67
|
+
const m = line.match(/(?:127\.0\.0\.1|localhost|\*|\[?::1\]?):(\d+)/i);
|
|
68
|
+
if (m) ports.add(+m[1]);
|
|
69
|
+
}
|
|
70
|
+
return [...ports];
|
|
71
|
+
} catch { return []; }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const portOf = (base) => { try { return +(new URL(base).port || 0) || null; } catch { return null; } };
|
|
75
|
+
|
|
60
76
|
// 识别服务实现——有特征端点才冠名,认不出如实标"OpenAI 兼容服务"(oMLX 跑在 1234 时不该叫 LM Studio)
|
|
61
77
|
async function identifyImpl(port, timeoutMs) {
|
|
62
78
|
const get = async (p) => {
|
|
@@ -70,11 +86,16 @@ async function identifyImpl(port, timeoutMs) {
|
|
|
70
86
|
return 'OpenAI 兼容服务';
|
|
71
87
|
}
|
|
72
88
|
|
|
73
|
-
// 仅 init/config
|
|
89
|
+
// 仅 init/config 使用——候选端口 = 常见端口 ∪ 本机全部监听端口 ∪ env/config 已知 base_url 端口,
|
|
90
|
+
// 并发试 /v1/models。响应里 data 不是数组则不算 LLM 端点(防止 dev server 等返回 200 被误收录);
|
|
74
91
|
// 401/403 时用已知环境 key 重试,仍不通则标 needs_key 保留(别静默漏掉 oMLX 这类要 key 的本地服务)
|
|
75
|
-
export async function probeEndpoints(timeoutMs = 400) {
|
|
92
|
+
export async function probeEndpoints(timeoutMs = 400, { extraBases = [] } = {}) {
|
|
93
|
+
const candidates = new Set(LLM_PROBES);
|
|
94
|
+
for (const p of listeningPorts()) candidates.add(p);
|
|
95
|
+
for (const v of ['SKILLMKT_LLM_BASE_URL', 'OMLX_BASE_URL']) { const p = portOf(process.env[v]); if (p) candidates.add(p); }
|
|
96
|
+
for (const b of extraBases) { const p = portOf(b); if (p) candidates.add(p); }
|
|
76
97
|
const envKeys = ['SKILLMKT_LLM_API_KEY', 'OMLX_API_KEY'].map((v) => process.env[v]).filter(Boolean);
|
|
77
|
-
const results = await Promise.allSettled(
|
|
98
|
+
const results = await Promise.allSettled([...candidates].map(async (port) => {
|
|
78
99
|
const base = `http://localhost:${port}/v1`;
|
|
79
100
|
const tryModels = async (key) => {
|
|
80
101
|
const ctrl = new AbortController();
|
|
@@ -91,10 +112,17 @@ export async function probeEndpoints(timeoutMs = 400) {
|
|
|
91
112
|
if (res.status === 401 || res.status === 403) {
|
|
92
113
|
for (const k of envKeys) { res = await tryModels(k); if (res?.ok) break; }
|
|
93
114
|
}
|
|
115
|
+
if (!res.ok) {
|
|
116
|
+
// 只有 OpenAI 风格错误体({"error":{...}})才算"要 key 的 LLM 端点"——普通鉴权服务不误报
|
|
117
|
+
const err = await res.json().catch(() => null);
|
|
118
|
+
if (!err?.error) return null;
|
|
119
|
+
const name = await identifyImpl(port, timeoutMs);
|
|
120
|
+
return { name, port, base_url: base, models: [], needs_key: true };
|
|
121
|
+
}
|
|
122
|
+
const data = await res.json().catch(() => null);
|
|
123
|
+
if (!Array.isArray(data?.data)) return null; // 200 但不是 OpenAI models 响应 → 非 LLM 端点
|
|
94
124
|
const name = await identifyImpl(port, timeoutMs);
|
|
95
|
-
|
|
96
|
-
const data = await res.json();
|
|
97
|
-
return { name, port, base_url: base, models: (data.data || []).map((m) => m.id) };
|
|
125
|
+
return { name, port, base_url: base, models: data.data.map((m) => m.id) };
|
|
98
126
|
}));
|
|
99
127
|
return results.map((r) => r.value).filter(Boolean);
|
|
100
128
|
}
|
package/client/public/app.js
CHANGED
|
@@ -170,7 +170,7 @@ async function render() {
|
|
|
170
170
|
app.querySelector('#eps').innerHTML = eps.map((e) => e.needs_key
|
|
171
171
|
? `<div class="ep" data-b="${esc(e.base_url)}">${esc(e.name)}:${e.port} — <span class="mut">需要密钥(点这行填地址,再填密钥、测试连接)</span></div>`
|
|
172
172
|
: `<div class="ep" data-b="${esc(e.base_url)}">${esc(e.name)}:${e.port} — ${e.models.length} 个模型(在下方「模型名称」下拉框里搜索)</div>`).join('')
|
|
173
|
-
|| '<p class="mut" style="margin-top:8px"
|
|
173
|
+
|| '<p class="mut" style="margin-top:8px">没扫到——已尝试本机所有监听端口。确认模型服务已启动,或直接把服务地址填到下面</p>';
|
|
174
174
|
app.querySelector('#probe-out').textContent = `发现 ${eps.length} 个服务、${modelMap.size} 个模型`;
|
|
175
175
|
app.querySelectorAll('#eps [data-b]').forEach((d) => d.onclick = () => {
|
|
176
176
|
app.querySelectorAll('#eps .ep').forEach((x) => x.classList.remove('sel'));
|