skillmkt 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +213 -2
- package/client/bin/skillmkt.js +84 -0
- package/client/commands/audit.js +34 -0
- package/client/commands/build.js +61 -0
- package/client/commands/config.js +34 -0
- package/client/commands/conflicts.js +14 -0
- package/client/commands/decide.js +19 -0
- package/client/commands/diff.js +52 -0
- package/client/commands/doctor.js +109 -0
- package/client/commands/download.js +35 -0
- package/client/commands/init.js +82 -0
- package/client/commands/install.js +169 -0
- package/client/commands/list.js +56 -0
- package/client/commands/merge.js +49 -0
- package/client/commands/outdated.js +34 -0
- package/client/commands/registry.js +42 -0
- package/client/commands/remove.js +48 -0
- package/client/commands/resolve.js +76 -0
- package/client/commands/rollback.js +30 -0
- package/client/commands/search.js +46 -0
- package/client/commands/serve.js +99 -0
- package/client/commands/show.js +52 -0
- package/client/commands/sync.js +54 -0
- package/client/commands/targets.js +75 -0
- package/client/commands/update.js +39 -0
- package/client/commands/upgrade.js +75 -0
- package/client/lib/ai.js +67 -0
- package/client/lib/audit.js +87 -0
- package/client/lib/autoarb.js +26 -0
- package/client/lib/conflicts.js +65 -0
- package/client/lib/ctx.js +68 -0
- package/client/lib/daemon.js +47 -0
- package/client/lib/dashboard.js +29 -0
- package/client/lib/decide.js +43 -0
- package/client/lib/evidence.js +56 -0
- package/client/lib/githash.js +36 -0
- package/client/lib/hub.js +125 -0
- package/client/lib/llm.js +85 -0
- package/client/lib/package.js +73 -0
- package/client/lib/project.js +97 -0
- package/client/lib/prompt.js +22 -0
- package/client/lib/ref.js +37 -0
- package/client/lib/registries/github.js +106 -0
- package/client/lib/registries/index.js +37 -0
- package/client/lib/registries/rfc.js +42 -0
- package/client/lib/registries/skillssh.js +59 -0
- package/client/lib/render.js +53 -0
- package/client/lib/rules.js +36 -0
- package/client/lib/scanner.js +158 -0
- package/client/lib/state.js +50 -0
- package/client/lib/yamlls.js +14 -0
- package/client/public/app.js +63 -0
- package/client/public/index.html +21 -0
- package/package.json +44 -4
- package/pipeline/lib/env.js +40 -0
- package/pipeline/lib/gh.js +53 -0
- package/pipeline/lib/llm.js +108 -0
- package/pipeline/lib/util.js +103 -0
- package/skill/SKILL.md +270 -0
- package/skill/references/a11y.md +82 -0
- package/skill/references/code.md +415 -0
- package/skill/references/color.md +84 -0
- package/skill/references/copy.md +197 -0
- package/skill/references/imagery.md +157 -0
- package/skill/references/layout.md +404 -0
- package/skill/references/motion.md +104 -0
- package/skill/references/process.md +303 -0
- package/skill/references/type.md +84 -0
- package/skill/rules-manifest.yml +13092 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// conflicts.js — 候选对生成(同域 or jaccard≥0.15)+ 静态矛盾信号 + fingerprint 去重 + 队列 IO
|
|
2
|
+
// LLM 语义判定批次 6 插入 detectConflicts 的 filter 阶段;静态版用指令词/数值/否定不对称代理
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { tokens, jaccard, conflictFingerprint, nextId, readYAML, writeYAML } from '../../pipeline/lib/util.js';
|
|
6
|
+
|
|
7
|
+
export const conflictsDir = (hub) => path.join(hub, 'conflicts');
|
|
8
|
+
|
|
9
|
+
// 矛盾代理信号:相似主题 + (不同数值 | 不同引用词 | 否定不对称)
|
|
10
|
+
const NUMS = (t) => (t.match(/\d+(\.\d+)?\s*(px|em|rem|%|pt|ms|s|x)?/g) || []).join(',');
|
|
11
|
+
const QUOTED = (t) => (t.match(/["'`#][\w-]+["'`]?|#[0-9a-fA-F]{3,8}\b/g) || []).join(',');
|
|
12
|
+
const NEG = /never|must not|do not|no |禁止|绝不|不得|avoid|don't/i;
|
|
13
|
+
function contradicts(a, b) {
|
|
14
|
+
if (a.domain === 'misc' && b.domain === 'misc') return false; // 全域 misc 噪声大,要求更严信号
|
|
15
|
+
const na = NUMS(a.text), nb = NUMS(b.text);
|
|
16
|
+
if (na && nb && na !== nb) return true;
|
|
17
|
+
const qa = QUOTED(a.text), qb = QUOTED(b.text);
|
|
18
|
+
if (qa && qb && qa !== qb) return true;
|
|
19
|
+
if (NEG.test(a.text) !== NEG.test(b.text)) return true; // 一边禁止一边无禁→可疑
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// rules[](跨 skill)→ 冲突候选(fingerprint 去重,已决不复活)
|
|
24
|
+
export function detectConflicts(hub, rules, { threshold = 0.15 } = {}) {
|
|
25
|
+
const existing = loadConflicts(hub);
|
|
26
|
+
const seenFp = new Set(existing.map((c) => c.fingerprint));
|
|
27
|
+
const out = [];
|
|
28
|
+
for (let i = 0; i < rules.length; i++) {
|
|
29
|
+
for (let j = i + 1; j < rules.length; j++) {
|
|
30
|
+
const a = rules[i], b = rules[j];
|
|
31
|
+
if (a.source === b.source) continue;
|
|
32
|
+
const sameDomain = a.domain === b.domain && a.domain !== 'misc';
|
|
33
|
+
if (!sameDomain && jaccard(tokens(a.text), tokens(b.text)) < threshold) continue;
|
|
34
|
+
if (!contradicts(a, b)) continue;
|
|
35
|
+
const sides = [
|
|
36
|
+
{ key: 'A', source: a.source, rule_id: a.id, hash: a.hash, severity: a.severity, text: a.text },
|
|
37
|
+
{ key: 'B', source: b.source, rule_id: b.id, hash: b.hash, severity: b.severity, text: b.text },
|
|
38
|
+
];
|
|
39
|
+
const fp = conflictFingerprint(sides);
|
|
40
|
+
if (seenFp.has(fp)) continue;
|
|
41
|
+
seenFp.add(fp);
|
|
42
|
+
out.push({ status: 'pending', fingerprint: fp, detected_at: new Date().toISOString(), domain: a.domain === b.domain ? a.domain : `${a.domain}×${b.domain}`, sides, evidence: 'text' });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// 写盘:续用现有 id 序列
|
|
46
|
+
const dir = conflictsDir(hub);
|
|
47
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
48
|
+
for (const c of out) {
|
|
49
|
+
c.id = nextId(dir, 'conf');
|
|
50
|
+
writeYAML(path.join(dir, `${c.id}.yml`), c);
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function loadConflicts(hub, { pendingOnly = false } = {}) {
|
|
56
|
+
const dir = conflictsDir(hub);
|
|
57
|
+
if (!fs.existsSync(dir)) return [];
|
|
58
|
+
return fs.readdirSync(dir).filter((f) => f.endsWith('.yml')).sort()
|
|
59
|
+
.map((f) => readYAML(path.join(dir, f))).filter(Boolean)
|
|
60
|
+
.filter((c) => !pendingOnly || c.status === 'pending');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function writeConflict(hub, c) {
|
|
64
|
+
writeYAML(path.join(conflictsDir(hub), `${c.id}.yml`), c);
|
|
65
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// ctx.js — 全局 flag 解析 + 命令上下文构造
|
|
2
|
+
import { hubPath, readConfig, readLibrary, readLock, expandHome } from './hub.js';
|
|
3
|
+
import { detectHarnesses, detectProject } from './scanner.js';
|
|
4
|
+
import { resolveLLM } from './llm.js';
|
|
5
|
+
|
|
6
|
+
const VALUE_FLAGS = new Set(['hub', 'base-url', 'model', 'api-key-env', 'from', 'to', 'target', 'scope', 'path', 'registry', 'name', 'resolution', 'reason', 'custom', 'port', 'project', 'type', 'url', 'token', 'out', 'limit']);
|
|
7
|
+
const BOOL_FLAGS = new Set(['json', 'verbose', 'no-llm', 'dry-run', 'ui', 'cli', 'no-ui', 'yes', 'y', 'help', 'h', 'version', 'V', 'force', 'all', 'llm', 'semantic', 'raw', 'files', 'rules', 'audit-only', 'purge', 'link', 'remote', 'local', 'wait', 'stop', 'daemon', 'keep-scripts', 'report', 'check', 'once']);
|
|
8
|
+
|
|
9
|
+
export function parseArgv(argv) {
|
|
10
|
+
const flags = {};
|
|
11
|
+
const positional = [];
|
|
12
|
+
for (let i = 0; i < argv.length; i++) {
|
|
13
|
+
const a = argv[i];
|
|
14
|
+
if (a === '--') { positional.push(...argv.slice(i + 1)); break; }
|
|
15
|
+
if (!a.startsWith('-')) { positional.push(a); continue; }
|
|
16
|
+
const eq = a.indexOf('=');
|
|
17
|
+
const key = (eq > 0 ? a.slice(0, eq) : a).replace(/^--?/, '');
|
|
18
|
+
if (eq > 0) flags[camel(key)] = a.slice(eq + 1);
|
|
19
|
+
else if (VALUE_FLAGS.has(key) && i + 1 < argv.length && !argv[i + 1].startsWith('-')) flags[camel(key)] = argv[++i];
|
|
20
|
+
else flags[camel(key)] = true;
|
|
21
|
+
}
|
|
22
|
+
return { flags: normalizeFlags(flags), positional };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const camel = (s) => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
26
|
+
|
|
27
|
+
function normalizeFlags(f) {
|
|
28
|
+
return {
|
|
29
|
+
json: !!f.json,
|
|
30
|
+
hub: f.hub ? expandHome(f.hub) : null,
|
|
31
|
+
verbose: !!f.verbose,
|
|
32
|
+
noLlm: !!f.noLlm,
|
|
33
|
+
dryRun: !!f.dryRun,
|
|
34
|
+
ui: f.ui ? 'force' : (f.cli || f.noUi) ? 'off' : 'auto',
|
|
35
|
+
yes: !!(f.yes || f.y),
|
|
36
|
+
baseUrl: f.baseUrl || null,
|
|
37
|
+
model: f.model || null,
|
|
38
|
+
apiKeyEnv: f.apiKeyEnv || null,
|
|
39
|
+
raw: f,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function makeCtx(argv, { wantProject = true } = {}) {
|
|
44
|
+
const { flags, positional } = parseArgv(argv);
|
|
45
|
+
const hub = hubPath(flags.hub);
|
|
46
|
+
const config = readConfig(hub);
|
|
47
|
+
const cwd = process.cwd();
|
|
48
|
+
const ctx = {
|
|
49
|
+
flags,
|
|
50
|
+
positional,
|
|
51
|
+
hub,
|
|
52
|
+
config,
|
|
53
|
+
llm: null,
|
|
54
|
+
tty: !!process.stdout.isTTY,
|
|
55
|
+
ci: !!process.env.CI,
|
|
56
|
+
cwd,
|
|
57
|
+
project: wantProject ? detectProject(cwd) : null,
|
|
58
|
+
};
|
|
59
|
+
ctx.llm = resolveLLM(ctx);
|
|
60
|
+
return ctx;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function withState(ctx) {
|
|
64
|
+
ctx.library = readLibrary(ctx.hub);
|
|
65
|
+
ctx.lock = readLock(ctx.hub);
|
|
66
|
+
ctx.harnesses = detectHarnesses();
|
|
67
|
+
return ctx;
|
|
68
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// daemon.js — 单例守护:daemon.json{pid,port,token,started} + spawn + openURL
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import { spawn, execFileSync } from 'node:child_process';
|
|
6
|
+
|
|
7
|
+
export const daemonFile = (hub) => path.join(hub, 'daemon.json');
|
|
8
|
+
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch { return false; } };
|
|
9
|
+
|
|
10
|
+
export function readDaemon(hub) {
|
|
11
|
+
try { return JSON.parse(fs.readFileSync(daemonFile(hub), 'utf8')); } catch { return null; }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function ensureDaemon(ctx) {
|
|
15
|
+
const d = readDaemon(ctx.hub);
|
|
16
|
+
if (d && alive(d.pid)) return d;
|
|
17
|
+
const bin = path.join(new URL('.', import.meta.url).pathname, '../bin/skillmkt.js');
|
|
18
|
+
const child = spawn(process.execPath, [bin, 'serve', '--daemon'], {
|
|
19
|
+
env: { ...process.env, SKILLMKT_HOME: ctx.hub },
|
|
20
|
+
detached: true, stdio: 'ignore',
|
|
21
|
+
});
|
|
22
|
+
child.unref();
|
|
23
|
+
for (let i = 0; i < 40; i++) {
|
|
24
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
25
|
+
const nd = readDaemon(ctx.hub);
|
|
26
|
+
if (nd && alive(nd.pid)) return nd;
|
|
27
|
+
}
|
|
28
|
+
throw new Error('daemon 启动超时');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function openURL(d, route = '/') {
|
|
32
|
+
const url = `http://127.0.0.1:${d.port}${route}?token=${d.token}`;
|
|
33
|
+
try { execFileSync('open', [url]); } catch { /* linux: xdg-open */ try { execFileSync('xdg-open', [url]); } catch { /* noop */ } }
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// CLI 自动接管:TTY + 未禁 ui + daemon 可用 → 开浏览器返回 true;否则 false 走终端
|
|
38
|
+
export async function maybeHandoff(ctx, route = '/') {
|
|
39
|
+
if (!process.stdout.isTTY || ctx.flags.raw.cli || ctx.flags.raw.noUi) return false;
|
|
40
|
+
if (ctx.flags.raw.ui === false) return false;
|
|
41
|
+
try {
|
|
42
|
+
const d = await ensureDaemon(ctx);
|
|
43
|
+
const url = openURL(d, route);
|
|
44
|
+
console.log(`opened ${url}`);
|
|
45
|
+
return true;
|
|
46
|
+
} catch { return false; }
|
|
47
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// dashboard.js — 裸跑数据汇聚 + 渲染(尊重 ui.enabled)
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { readYAMLFile } from './hub.js';
|
|
5
|
+
import { scanAll } from './scanner.js';
|
|
6
|
+
import { treeHash } from './package.js';
|
|
7
|
+
import { dashboard } from './render.js';
|
|
8
|
+
import { listYAML } from './yamlls.js';
|
|
9
|
+
|
|
10
|
+
export function renderDashboard(ctx) {
|
|
11
|
+
const { hub, library, harnesses, llm, project, config } = ctx;
|
|
12
|
+
|
|
13
|
+
// ui.enabled=always → 开面板(daemon 接管,批次7 实装前降级终端仪表盘)
|
|
14
|
+
if (config.ui.enabled === 'always' && ctx.tty && ctx.flags.ui !== 'off') {
|
|
15
|
+
// TODO(batch7): ensureDaemon + openURL('/')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const rows = scanAll(hub, harnesses, library, project?.root);
|
|
19
|
+
const managedHash = {};
|
|
20
|
+
for (const [name] of Object.entries(library.skills || {})) {
|
|
21
|
+
const p = path.join(hub, 'skills', name);
|
|
22
|
+
if (fs.existsSync(p)) managedHash[name] = treeHash(p);
|
|
23
|
+
}
|
|
24
|
+
const upstream = readYAMLFile(path.join(hub, 'state', 'upstream.yml'), { skills: {} });
|
|
25
|
+
const conflicts = listYAML(path.join(hub, 'conflicts'));
|
|
26
|
+
|
|
27
|
+
dashboard({ hub, library, scan: { rows, harnesses, managedHash }, upstream, conflicts, llm, project });
|
|
28
|
+
return 0;
|
|
29
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// decide.js — decisions/*.yml 写盘(reason 必填 / supersedes 自动 / fingerprint 关联)
|
|
2
|
+
// 与管线 pipeline/decide.js 不变量一致;decisions 是唯一手写(或经命令)的真理源
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { readYAML, writeYAML } from '../../pipeline/lib/util.js';
|
|
6
|
+
|
|
7
|
+
export const decisionsDir = (hub) => path.join(hub, 'decisions');
|
|
8
|
+
|
|
9
|
+
export function loadDecisions(hub) {
|
|
10
|
+
const dir = decisionsDir(hub);
|
|
11
|
+
if (!fs.existsSync(dir)) return [];
|
|
12
|
+
return fs.readdirSync(dir).filter((f) => f.endsWith('.yml')).sort()
|
|
13
|
+
.map((f) => readYAML(path.join(dir, f))).filter(Boolean);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// activeMap: fingerprint → decision(latest active wins;overturn 链追 supersedes)
|
|
17
|
+
export function activeDecisions(hub) {
|
|
18
|
+
const map = new Map();
|
|
19
|
+
for (const d of loadDecisions(hub)) {
|
|
20
|
+
if (d.status !== 'active') continue;
|
|
21
|
+
map.set(d.fingerprint, d);
|
|
22
|
+
}
|
|
23
|
+
return map;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// decide(hub, conflict, {resolution, custom, reason, by}) → decision obj
|
|
27
|
+
// resolution: keep-a | keep-b | merge | dismissed;reason 必填(AGENTS 不变量)
|
|
28
|
+
export function decide(hub, conflict, { resolution, custom = null, reason, by = 'human' }) {
|
|
29
|
+
if (!reason) throw new Error('decision requires reason');
|
|
30
|
+
const dir = decisionsDir(hub);
|
|
31
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
32
|
+
// 同 fingerprint 已有 active → 本次为推翻,记 supersedes
|
|
33
|
+
const prior = activeDecisions(hub).get(conflict.fingerprint);
|
|
34
|
+
const d = {
|
|
35
|
+
id: conflict.id, fingerprint: conflict.fingerprint, status: 'active',
|
|
36
|
+
resolution, custom, reason,
|
|
37
|
+
alternatives_rejected: [], trigger: 'manual', decided_at: new Date().toISOString(),
|
|
38
|
+
decided_by: by, supersedes: prior ? prior.decided_at : null,
|
|
39
|
+
};
|
|
40
|
+
// 单文件=fingerprint 最新决策;supersedes 记时间戳链(与管线同构)
|
|
41
|
+
writeYAML(path.join(dir, `${conflict.id}.yml`), d);
|
|
42
|
+
return d;
|
|
43
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// evidence.js — 设计域冲突的可视证据:LLM 写 A/B fixture → playwright 截图
|
|
2
|
+
// 非设计域走 text A/B + ai_explanation(conflicts.js 默认 evidence:'text')
|
|
3
|
+
// playwright 缺失/渲染同图 → inconclusive.txt 标记,不阻塞队列
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import crypto from 'node:crypto';
|
|
7
|
+
import { chat } from './llm.js';
|
|
8
|
+
import { writeConflict } from './conflicts.js';
|
|
9
|
+
|
|
10
|
+
const DESIGN_DOMAINS = new Set(['design', 'ui', 'ux', 'css', 'layout', 'color', 'motion', 'type', 'typography', 'frontend', 'a11y', 'visual', 'style', 'imagery']);
|
|
11
|
+
export const isDesignDomain = (d) => [...String(d || '').split(/[×x]/i)].some((x) => DESIGN_DOMAINS.has(x.trim()));
|
|
12
|
+
const evidenceDir = (hub, id) => path.join(hub, 'evidence', id);
|
|
13
|
+
|
|
14
|
+
const SYS = `You build minimal evidence fixtures for a UI-design rule dispute. Output ONE self-contained HTML per option: same fictional product & layout scaffold, ONLY the disputed rule applied differently. Difference visible at a glance in 1200x800 static screenshot (motion/hover → show triggered state or before/after pair). Inline <style> only, no external assets, short plausible copy. Caption bar bottom-left: "OPTION A — <5 words>". <200 lines. Reply ONLY JSON {"a_html":"…","b_html":"…","note":"what to look for"}`;
|
|
15
|
+
|
|
16
|
+
export async function makeEvidence(llm, hub, conflict) {
|
|
17
|
+
const dir = evidenceDir(hub, conflict.id);
|
|
18
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
19
|
+
const [A, B] = conflict.sides;
|
|
20
|
+
const m = String(await chat(llm, {
|
|
21
|
+
system: SYS,
|
|
22
|
+
user: `OPTION A (${A.source}): ${A.text}\nOPTION B (${B.source}): ${B.text}\n${conflict.ai_explanation ? `Why they conflict: ${conflict.ai_explanation}` : ''}`,
|
|
23
|
+
maxTokens: 12000,
|
|
24
|
+
})).match(/```(?:json)?\s*([\s\S]*?)```/) || [];
|
|
25
|
+
const o = JSON.parse((m[1] || '{}').trim() || '{}');
|
|
26
|
+
if (!o.a_html?.includes('<') || !o.b_html?.includes('<')) throw new Error('fixture gen failed');
|
|
27
|
+
fs.writeFileSync(path.join(dir, 'option-a.html'), o.a_html);
|
|
28
|
+
fs.writeFileSync(path.join(dir, 'option-b.html'), o.b_html);
|
|
29
|
+
fs.writeFileSync(path.join(dir, 'note.txt'), o.note || '');
|
|
30
|
+
|
|
31
|
+
const files = [];
|
|
32
|
+
try {
|
|
33
|
+
const { chromium } = await import('playwright');
|
|
34
|
+
const browser = await chromium.launch();
|
|
35
|
+
for (const opt of ['a', 'b']) {
|
|
36
|
+
const page = await browser.newPage({ viewport: { width: 1200, height: 800 } });
|
|
37
|
+
await page.goto(`file://${path.join(dir, `option-${opt}.html`)}`);
|
|
38
|
+
const png = path.join(dir, `option-${opt}.png`);
|
|
39
|
+
await page.screenshot({ path: png });
|
|
40
|
+
files.push(`option-${opt}.png`, `option-${opt}.html`);
|
|
41
|
+
await page.close();
|
|
42
|
+
}
|
|
43
|
+
await browser.close();
|
|
44
|
+
// 同图=证据无效
|
|
45
|
+
const ha = crypto.createHash('sha1').update(fs.readFileSync(path.join(dir, 'option-a.png'))).digest('hex');
|
|
46
|
+
const hb = crypto.createHash('sha1').update(fs.readFileSync(path.join(dir, 'option-b.png'))).digest('hex');
|
|
47
|
+
if (ha === hb) { fs.writeFileSync(path.join(dir, 'inconclusive.txt'), 'A/B rendered identically'); }
|
|
48
|
+
conflict.evidence = 'screenshot';
|
|
49
|
+
conflict.evidence_files = files;
|
|
50
|
+
} catch {
|
|
51
|
+
conflict.evidence = 'text'; // playwright 未装 → 降级文本证据
|
|
52
|
+
conflict.evidence_files = ['option-a.html', 'option-b.html'];
|
|
53
|
+
}
|
|
54
|
+
writeConflict(hub, conflict);
|
|
55
|
+
return conflict;
|
|
56
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// githash.js — git blob-sha1 风格 tree-hash:本地与远端 git/trees API 可等值对比
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import { walk } from './package.js';
|
|
6
|
+
|
|
7
|
+
const blobSha = (buf) => crypto.createHash('sha1').update(`blob ${buf.length}\0`).update(buf).digest('hex');
|
|
8
|
+
|
|
9
|
+
// 本地目录 → git tree 等值 hash(sha1(sorted "path\0blobsha\n"))
|
|
10
|
+
export function gitTreeHash(dir) {
|
|
11
|
+
const entries = [];
|
|
12
|
+
for (const p of walk(dir)) {
|
|
13
|
+
const rel = path.relative(dir, p).split(path.sep).join('/');
|
|
14
|
+
entries.push(`${rel}\0${blobSha(fs.readFileSync(p))}`);
|
|
15
|
+
}
|
|
16
|
+
entries.sort();
|
|
17
|
+
return 'sha1:' + crypto.createHash('sha1').update(entries.join('\n')).digest('hex');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 远端 git/trees 响应 → 同算法 hash(过滤 subtree)
|
|
21
|
+
// sub 定位与 fetchSkill 同策略:skills.sh slug 常省略 skills/ 前缀,多级候选回退
|
|
22
|
+
export function remoteTreeHash(tree, sub = null) {
|
|
23
|
+
if (!sub) {
|
|
24
|
+
const entries = (tree || []).filter((e) => e.type === 'blob').map((e) => `${e.path}\0${e.sha}`).sort();
|
|
25
|
+
return 'sha1:' + crypto.createHash('sha1').update(entries.join('\n')).digest('hex');
|
|
26
|
+
}
|
|
27
|
+
const tops = new Set((tree || []).map((e) => e.path.split('/')[0]));
|
|
28
|
+
const cands = [sub, `skills/${sub}`, ...[...tops].map((d) => `${d}/${sub}`)];
|
|
29
|
+
const hit = cands.find((c) => tree.some((e) => e.path === c || e.path.startsWith(`${c}/`)));
|
|
30
|
+
if (!hit) return 'sha1:' + crypto.createHash('sha1').digest('hex'); // 未命中→空树 hash(必然≠本地,outdated 会标 changed)
|
|
31
|
+
const entries = (tree || [])
|
|
32
|
+
.filter((e) => e.type === 'blob' && (e.path === hit || e.path.startsWith(`${hit}/`)))
|
|
33
|
+
.map((e) => `${e.path.slice(hit.length + 1)}\0${e.sha}`)
|
|
34
|
+
.sort();
|
|
35
|
+
return 'sha1:' + crypto.createHash('sha1').update(entries.join('\n')).digest('hex');
|
|
36
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// hub.js — hub 路径、骨架、library/lock/config 原子读写、history/trash
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import YAML from 'yaml';
|
|
6
|
+
|
|
7
|
+
export function expandHome(p) {
|
|
8
|
+
return p && p.startsWith('~') ? path.join(os.homedir(), p.slice(1)) : p;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function hubPath(flagHub) {
|
|
12
|
+
return path.resolve(expandHome(flagHub || process.env.SKILLMKT_HOME || '~/.skillmkt'));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const SUBDIRS = [
|
|
16
|
+
'skills', 'scans', 'conflicts', 'decisions', 'merged',
|
|
17
|
+
'state', 'cache', '.history', '.trash', 'quarantine',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export function hubExists(hub) {
|
|
21
|
+
return fs.existsSync(path.join(hub, 'library.yml'));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function ensureHub(hub) {
|
|
25
|
+
for (const d of SUBDIRS) fs.mkdirSync(path.join(hub, d), { recursive: true });
|
|
26
|
+
return hub;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function readYAMLFile(file, fallback = null) {
|
|
30
|
+
try { return YAML.parse(fs.readFileSync(file, 'utf8')) ?? fallback; }
|
|
31
|
+
catch { return fallback; }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function writeYAMLFile(file, data) {
|
|
35
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
36
|
+
const tmp = `${file}.tmp.${process.pid}`;
|
|
37
|
+
fs.writeFileSync(tmp, YAML.stringify(data));
|
|
38
|
+
fs.renameSync(tmp, file); // 同目录原子替换
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function readJSONFile(file, fallback = null) {
|
|
42
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf8')); }
|
|
43
|
+
catch { return fallback; }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function writeJSONFile(file, data) {
|
|
47
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
48
|
+
const tmp = `${file}.tmp.${process.pid}`;
|
|
49
|
+
fs.writeFileSync(tmp, JSON.stringify(data, null, 2));
|
|
50
|
+
fs.renameSync(tmp, file);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const F = (hub, ...p) => path.join(hub, ...p);
|
|
54
|
+
|
|
55
|
+
export const readLibrary = (hub) => readYAMLFile(F(hub, 'library.yml'), { version: 1, skills: {} });
|
|
56
|
+
export const writeLibrary = (hub, d) => writeYAMLFile(F(hub, 'library.yml'), d);
|
|
57
|
+
export const readLock = (hub) => readYAMLFile(F(hub, 'lock.yml'), { version: 1, skills: {} });
|
|
58
|
+
export const writeLock = (hub, d) => writeYAMLFile(F(hub, 'lock.yml'), d);
|
|
59
|
+
|
|
60
|
+
export function registerSkill(hub, name, entry) {
|
|
61
|
+
const lib = readLibrary(hub);
|
|
62
|
+
lib.skills[name] = entry;
|
|
63
|
+
writeLibrary(hub, lib);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function unregisterSkill(hub, name) {
|
|
67
|
+
const lib = readLibrary(hub);
|
|
68
|
+
delete lib.skills[name];
|
|
69
|
+
writeLibrary(hub, lib);
|
|
70
|
+
const lock = readLock(hub);
|
|
71
|
+
delete lock.skills[name];
|
|
72
|
+
writeLock(hub, lock);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function updateLock(hub, name, contentHash, targets) {
|
|
76
|
+
const lock = readLock(hub);
|
|
77
|
+
lock.skills[name] = { content_hash: contentHash, targets };
|
|
78
|
+
writeLock(hub, lock);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function readConfig(hub) {
|
|
82
|
+
const cfg = readYAMLFile(F(hub, 'config.yml'), {}) || {};
|
|
83
|
+
return {
|
|
84
|
+
llm: cfg.llm || null,
|
|
85
|
+
ui: { enabled: cfg.ui?.enabled ?? 'auto' },
|
|
86
|
+
policy: cfg.policy ?? 'balanced',
|
|
87
|
+
default_targets: cfg.default_targets ?? [],
|
|
88
|
+
targets: cfg.targets ?? {},
|
|
89
|
+
registries: cfg.registries ?? [{ type: 'github', default: true }],
|
|
90
|
+
daemon: { idle_minutes: cfg.daemon?.idle_minutes ?? 30 },
|
|
91
|
+
_raw: cfg,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function writeConfig(hub, raw) {
|
|
96
|
+
writeYAMLFile(F(hub, 'config.yml'), raw);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function setConfigKey(raw, dotted, value) {
|
|
100
|
+
const keys = dotted.split('.');
|
|
101
|
+
let cur = raw;
|
|
102
|
+
for (const k of keys.slice(0, -1)) cur = cur[k] ??= {};
|
|
103
|
+
cur[keys.at(-1)] = value;
|
|
104
|
+
return raw;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function getConfigKey(raw, dotted) {
|
|
108
|
+
return dotted.split('.').reduce((o, k) => o?.[k], raw);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function trashPath(hub, name) {
|
|
112
|
+
return F(hub, '.trash', `${name}-${Date.now()}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function historyDir(hub, name) {
|
|
116
|
+
return F(hub, '.history', name);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function cacheDir(hub, name, hash) {
|
|
120
|
+
return F(hub, 'cache', `${name}-${(hash || 'nohash').slice(7, 15)}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function quarantinePath(hub, name) {
|
|
124
|
+
return F(hub, 'quarantine', `${name}-${Date.now()}`);
|
|
125
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// llm.js — 端点解析链(flag > SKILLMKT_LLM_* > OMLX_* > config.llm > .env)
|
|
2
|
+
// 批次6 补:chatJSON 真调用、probeEndpoints、端口探测。铁律:非 init/config 零探测。
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
function loadDotEnv(cwd) {
|
|
7
|
+
try {
|
|
8
|
+
const out = {};
|
|
9
|
+
for (const line of fs.readFileSync(path.join(cwd, '.env'), 'utf8').split('\n')) {
|
|
10
|
+
const m = line.match(/^\s*([A-Z_][A-Z0-9_]*)\s*=\s*(.*)\s*$/);
|
|
11
|
+
if (m && !line.trim().startsWith('#')) out[m[1]] = m[2].replace(/^["']|["']$/g, '');
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
} catch { return {}; }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function resolveLLM(ctx) {
|
|
18
|
+
if (ctx.flags.noLlm) return null;
|
|
19
|
+
const env = { ...loadDotEnv(ctx.cwd), ...process.env };
|
|
20
|
+
const cfg = ctx.config.llm || {};
|
|
21
|
+
const base = ctx.flags.baseUrl || env.SKILLMKT_LLM_BASE_URL || env.OMLX_BASE_URL || cfg.base_url;
|
|
22
|
+
const model = ctx.flags.model || env.SKILLMKT_LLM_MODEL || env.OMLX_MODEL || cfg.model;
|
|
23
|
+
const keyRef = ctx.flags.apiKeyEnv || env.SKILLMKT_LLM_API_KEY || env.OMLX_API_KEY || cfg.api_key;
|
|
24
|
+
if (!base || !model) return null;
|
|
25
|
+
// api_key 形如 "env:VAR" → 取环境变量;裸值(不建议)原样用
|
|
26
|
+
const key = keyRef?.startsWith('env:') ? env[keyRef.slice(4)] : keyRef;
|
|
27
|
+
return {
|
|
28
|
+
base_url: base.replace(/\/+$/, ''),
|
|
29
|
+
model,
|
|
30
|
+
api_key: key || '',
|
|
31
|
+
concurrency: +(cfg.concurrency ?? 2),
|
|
32
|
+
timeout_ms: +(cfg.timeout_ms ?? 60000),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// OpenAI 兼容 chat——返回文本内容或抛错(init 冒烟 + 后续 LLM 功能复用)
|
|
37
|
+
export async function chat(llm, { system = '', user, maxTokens = 64, temperature = 0 } = {}) {
|
|
38
|
+
const ctrl = new AbortController();
|
|
39
|
+
const t = setTimeout(() => ctrl.abort(), llm.timeout_ms);
|
|
40
|
+
try {
|
|
41
|
+
const res = await fetch(`${llm.base_url}/chat/completions`, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
signal: ctrl.signal,
|
|
44
|
+
headers: { 'content-type': 'application/json', ...(llm.api_key ? { authorization: `Bearer ${llm.api_key}` } : {}) },
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
model: llm.model,
|
|
47
|
+
temperature,
|
|
48
|
+
max_tokens: maxTokens,
|
|
49
|
+
messages: [...(system ? [{ role: 'system', content: system }] : []), { role: 'user', content: user }],
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
if (!res.ok) throw new Error(`llm ${res.status}: ${(await res.text()).slice(0, 200)}`);
|
|
53
|
+
const data = await res.json();
|
|
54
|
+
return data.choices?.[0]?.message?.content ?? '';
|
|
55
|
+
} finally { clearTimeout(t); }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const LLM_PROBES = [
|
|
59
|
+
{ port: 8001, name: 'OMLX' },
|
|
60
|
+
{ port: 11434, name: 'Ollama', probe: '/api/version' },
|
|
61
|
+
{ port: 1234, name: 'LM Studio' },
|
|
62
|
+
{ port: 8000, name: 'vLLM' },
|
|
63
|
+
{ port: 8080, name: 'mlx-lm/llama.cpp' },
|
|
64
|
+
{ port: 1337, name: 'Jan' },
|
|
65
|
+
{ port: 5000, name: 'text-gen-webui' },
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
// 仅 init/config 使用——并发扫端口,能回 /v1/models 即可用端点
|
|
69
|
+
export async function probeEndpoints(timeoutMs = 300) {
|
|
70
|
+
const results = await Promise.allSettled(LLM_PROBES.map(async ({ port, name, probe }) => {
|
|
71
|
+
const base = `http://localhost:${port}/v1`;
|
|
72
|
+
const ctrl = new AbortController();
|
|
73
|
+
const t = setTimeout(() => ctrl.abort(), timeoutMs);
|
|
74
|
+
try {
|
|
75
|
+
const res = await fetch(`${base}/models`, { signal: ctrl.signal });
|
|
76
|
+
if (!res.ok) return null;
|
|
77
|
+
const data = await res.json();
|
|
78
|
+
return { name, port, base_url: base, models: (data.data || []).map((m) => m.id) };
|
|
79
|
+
} catch {
|
|
80
|
+
if (probe) { /* ollama 等特征端点降级可日后补 */ }
|
|
81
|
+
return null;
|
|
82
|
+
} finally { clearTimeout(t); }
|
|
83
|
+
}));
|
|
84
|
+
return results.map((r) => r.value).filter(Boolean);
|
|
85
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// package.js — SkillPackage 归一化、tree-hash、frontmatter 解析
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import YAML from 'yaml';
|
|
6
|
+
|
|
7
|
+
const FRONT_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/;
|
|
8
|
+
|
|
9
|
+
export function parseFrontmatter(file) {
|
|
10
|
+
try {
|
|
11
|
+
const src = fs.readFileSync(file, 'utf8');
|
|
12
|
+
const m = src.match(FRONT_RE);
|
|
13
|
+
const meta = m ? (YAML.parse(m[1]) || {}) : {};
|
|
14
|
+
// skills.sh 惯例:version 常藏在 metadata.version
|
|
15
|
+
if (meta.version == null && meta.metadata?.version != null) meta.version = String(meta.metadata.version);
|
|
16
|
+
return { meta, body: src.replace(FRONT_RE, '') };
|
|
17
|
+
} catch {
|
|
18
|
+
return { meta: {}, body: '' };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function* walk(dir) {
|
|
23
|
+
if (!fs.existsSync(dir)) return;
|
|
24
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
25
|
+
const p = path.join(dir, e.name);
|
|
26
|
+
if (e.isDirectory()) yield* walk(p);
|
|
27
|
+
else if (e.isFile() || e.isSymbolicLink()) yield p;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 排序路径+内容哈希;目录名不参与(重命名稳定)
|
|
32
|
+
export function treeHash(dir) {
|
|
33
|
+
const h = crypto.createHash('sha256');
|
|
34
|
+
for (const p of walk(dir)) {
|
|
35
|
+
const rel = path.relative(dir, p).split(path.sep).join('/');
|
|
36
|
+
h.update(rel);
|
|
37
|
+
h.update('\0');
|
|
38
|
+
try { h.update(fs.readFileSync(p)); } catch { /* unreadable → skip */ }
|
|
39
|
+
h.update('\n');
|
|
40
|
+
}
|
|
41
|
+
return 'sha256:' + h.digest('hex');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function readSkillDir(absPath) {
|
|
45
|
+
const skill = path.join(absPath, 'SKILL.md');
|
|
46
|
+
if (!fs.existsSync(skill)) return null;
|
|
47
|
+
const { meta } = parseFrontmatter(skill);
|
|
48
|
+
const files = [...walk(absPath)].map((p) => path.relative(absPath, p));
|
|
49
|
+
return {
|
|
50
|
+
name: meta.name || path.basename(absPath),
|
|
51
|
+
version: meta.version || '0.0.0',
|
|
52
|
+
description: meta.description || '',
|
|
53
|
+
path: absPath,
|
|
54
|
+
files,
|
|
55
|
+
fileCount: files.length,
|
|
56
|
+
contentHash: treeHash(absPath),
|
|
57
|
+
meta,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function copyDir(src, dst) {
|
|
62
|
+
fs.mkdirSync(dst, { recursive: true });
|
|
63
|
+
for (const e of fs.readdirSync(src, { withFileTypes: true })) {
|
|
64
|
+
const s = path.join(src, e.name);
|
|
65
|
+
const d = path.join(dst, e.name);
|
|
66
|
+
if (e.isDirectory()) copyDir(s, d);
|
|
67
|
+
else fs.copyFileSync(s, d);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function rmDir(dir) {
|
|
72
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
73
|
+
}
|