skillar 0.0.0 → 0.2.1
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 +22 -0
- package/README.md +250 -2
- package/dist/analysis/finding.js +190 -0
- package/dist/analysis/tree.js +157 -0
- package/dist/analysis/usage.js +394 -0
- package/dist/api/flows.js +285 -0
- package/dist/api/routes.js +192 -0
- package/dist/api/security.js +86 -0
- package/dist/api/sse.js +55 -0
- package/dist/api/static.js +33 -0
- package/dist/catalog/fields.js +159 -0
- package/dist/catalog/index.js +133 -0
- package/dist/catalog/scanner.js +204 -0
- package/dist/catalog/uri.js +43 -0
- package/dist/catalog/watcher.js +59 -0
- package/dist/cli/config-writer.js +255 -0
- package/dist/cli/doctor.js +268 -0
- package/dist/cli/eval.js +64 -0
- package/dist/cli/export-import.js +68 -0
- package/dist/cli/init.js +272 -0
- package/dist/cli/main.js +165 -0
- package/dist/cli/open.js +24 -0
- package/dist/cli/serve.js +147 -0
- package/dist/config.js +92 -0
- package/dist/dashboard/assets/app.js +354 -0
- package/dist/dashboard/assets/chunk-5DFWNJC2.js +1 -0
- package/dist/dashboard/assets/chunk-S6B2HJQA.js +17 -0
- package/dist/dashboard/assets/styles.css +542 -0
- package/dist/dashboard/index.html +15 -0
- package/dist/fs-watchdog.js +35 -0
- package/dist/herdr/client.js +205 -0
- package/dist/herdr/sync.js +342 -0
- package/dist/ingest/attribution.js +80 -0
- package/dist/ingest/hooks.js +168 -0
- package/dist/ingest/identity.js +4 -0
- package/dist/ingest/mcp-recorder.js +41 -0
- package/dist/ingest/otlp-traces.js +106 -0
- package/dist/ingest/otlp.js +314 -0
- package/dist/ingest/pipeline.js +119 -0
- package/dist/ingest/redact.js +36 -0
- package/dist/ingest/runtimes.js +25 -0
- package/dist/mcp/dispatch.js +54 -0
- package/dist/mcp/http.js +112 -0
- package/dist/mcp/legacy.js +61 -0
- package/dist/mcp/pagination.js +17 -0
- package/dist/mcp/protocol.js +121 -0
- package/dist/mcp/skills-extension.js +77 -0
- package/dist/mcp/stdio-proxy.js +108 -0
- package/dist/mcp/tools.js +59 -0
- package/dist/packs/claude-code.js +176 -0
- package/dist/packs/codex.js +207 -0
- package/dist/packs/cursor.js +97 -0
- package/dist/packs/gajae.js +7 -0
- package/dist/packs/gemini.js +90 -0
- package/dist/packs/json-config.js +55 -0
- package/dist/packs/omo.js +154 -0
- package/dist/packs/openclaw.js +132 -0
- package/dist/packs/registry.js +13 -0
- package/dist/search/fts.js +61 -0
- package/dist/search/fusion.js +50 -0
- package/dist/search/guidance.js +7 -0
- package/dist/search/index.js +74 -0
- package/dist/search/lexical.js +69 -0
- package/dist/server.js +267 -0
- package/dist/store/automation.js +18 -0
- package/dist/store/db.js +134 -0
- package/dist/store/execution.js +157 -0
- package/dist/store/export.js +269 -0
- package/dist/store/index.js +14 -0
- package/dist/store/migrations/002_tracing.sql +94 -0
- package/dist/store/migrations/003_tracing_indexes.sql +11 -0
- package/dist/store/migrations/004_load_reconciliation.sql +50 -0
- package/dist/store/migrations/005_execution_boundaries.sql +6 -0
- package/dist/store/migrations/006_recording_order.sql +72 -0
- package/dist/store/queries.js +643 -0
- package/dist/store/schema.sql +96 -0
- package/dist/store/workflow.js +250 -0
- package/dist/store/writes.js +315 -0
- package/dist/types.js +1 -0
- package/dist/version.js +19 -0
- package/dist/workflow/compare.js +68 -0
- package/dist/workflow/routes.js +100 -0
- package/dist/workflow/spec.js +40 -0
- package/docs/execution-ownership.md +98 -0
- package/docs/mcp-conformance.md +156 -0
- package/docs/skill-tracing.md +294 -0
- package/package.json +46 -3
- package/packs/claude-code/README.md +5 -0
- package/packs/claude-code/settings.template.json +34 -0
- package/packs/codex/README.md +8 -0
- package/packs/codex/config.template.toml +12 -0
- package/packs/cursor/hooks.example.json +8 -0
- package/packs/cursor/mcp.example.json +8 -0
- package/packs/gajae/extension/discovery.md +5 -0
- package/packs/gajae/extension/index.js +52 -0
- package/packs/gemini/settings.example.json +16 -0
- package/packs/hook-post.mjs +37 -0
- package/packs/omo/extension/discovery.md +5 -0
- package/packs/omo/extension/index.js +52 -0
- package/packs/openclaw/.codex-plugin/plugin.json +11 -0
- package/packs/openclaw/.mcp.json +12 -0
- package/packs/openclaw/hooks/skillar-discovery/HOOK.md +8 -0
- package/packs/openclaw/hooks/skillar-discovery/handler.js +30 -0
- package/packs/openclaw/index.mjs +55 -0
- package/packs/openclaw/openclaw.plugin.json +22 -0
- package/packs/openclaw/package.json +11 -0
- package/packs/openclaw/skillar.json +1 -0
- package/src/store/migrations/002_tracing.sql +94 -0
- package/src/store/migrations/003_tracing_indexes.sql +11 -0
- package/src/store/migrations/004_load_reconciliation.sql +50 -0
- package/src/store/migrations/005_execution_boundaries.sql +6 -0
- package/src/store/migrations/006_recording_order.sql +72 -0
- package/src/store/schema.sql +96 -0
- package/src/store/task-types.json +15 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { ingestOtlpTraces } from "../ingest/otlp-traces.js";
|
|
2
|
+
import { ingestHook, ingestOtlp } from "../ingest/pipeline.js";
|
|
3
|
+
import { isRuntimeId, RUNTIME_IDS } from "../ingest/runtimes.js";
|
|
4
|
+
import { registerWorkflowRoutes } from "../workflow/routes.js";
|
|
5
|
+
import { registerFlowRoutes } from "./flows.js";
|
|
6
|
+
import { deny } from "./security.js";
|
|
7
|
+
const STATES = ["loaded", "exposed_only", "no_match", "aborted", "not_used"];
|
|
8
|
+
const CLIENT_ERROR_CODES = new Set(["invalid_tz", "bad_filter", "bad_request"]);
|
|
9
|
+
class BadRequest extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(code, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function one(value) {
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
return undefined;
|
|
19
|
+
if (Array.isArray(value))
|
|
20
|
+
return value.map(String).join(",");
|
|
21
|
+
return String(value);
|
|
22
|
+
}
|
|
23
|
+
function intParam(q, key, range) {
|
|
24
|
+
const raw = one(q[key]);
|
|
25
|
+
if (raw === undefined || raw === "")
|
|
26
|
+
return undefined;
|
|
27
|
+
const n = Number(raw);
|
|
28
|
+
if (!Number.isInteger(n) || (range && (n < range[0] || n > range[1])))
|
|
29
|
+
throw new BadRequest("bad_filter", `${key} must be an integer${range ? ` in ${range[0]}..${range[1]}` : ""}`);
|
|
30
|
+
return n;
|
|
31
|
+
}
|
|
32
|
+
function listParam(q, key, allowed) {
|
|
33
|
+
const raw = one(q[key]);
|
|
34
|
+
if (raw === undefined || raw === "")
|
|
35
|
+
return undefined;
|
|
36
|
+
const items = raw.split(",").map((s) => s.trim()).filter(Boolean);
|
|
37
|
+
if (allowed)
|
|
38
|
+
for (const item of items)
|
|
39
|
+
if (!allowed.includes(item))
|
|
40
|
+
throw new BadRequest("bad_filter", `unknown ${key} value: ${item}`);
|
|
41
|
+
return items;
|
|
42
|
+
}
|
|
43
|
+
export function parseFilter(q) {
|
|
44
|
+
const f = {};
|
|
45
|
+
const from = intParam(q, "from");
|
|
46
|
+
const to = intParam(q, "to");
|
|
47
|
+
const runtime = listParam(q, "runtime", RUNTIME_IDS);
|
|
48
|
+
const skill = listParam(q, "skill");
|
|
49
|
+
const state = listParam(q, "state", STATES);
|
|
50
|
+
const dow = intParam(q, "dow", [0, 6]);
|
|
51
|
+
const hour = intParam(q, "hour", [0, 23]);
|
|
52
|
+
const hourSpan = intParam(q, "hourSpan", [1, 24]);
|
|
53
|
+
const text = one(q.q)?.trim();
|
|
54
|
+
const tz = one(q.tz);
|
|
55
|
+
const automated = one(q.automated);
|
|
56
|
+
if (automated !== undefined && automated !== "include" && automated !== "only")
|
|
57
|
+
throw new BadRequest("bad_filter", "automated must be include or only");
|
|
58
|
+
if (from !== undefined)
|
|
59
|
+
f.from = from;
|
|
60
|
+
if (to !== undefined)
|
|
61
|
+
f.to = to;
|
|
62
|
+
if (runtime)
|
|
63
|
+
f.runtime = runtime;
|
|
64
|
+
if (skill)
|
|
65
|
+
f.skill = skill;
|
|
66
|
+
if (state)
|
|
67
|
+
f.state = state;
|
|
68
|
+
if (dow !== undefined)
|
|
69
|
+
f.dow = dow;
|
|
70
|
+
if (hour !== undefined)
|
|
71
|
+
f.hour = hour;
|
|
72
|
+
if (hourSpan !== undefined)
|
|
73
|
+
f.hourSpan = hourSpan;
|
|
74
|
+
if (text)
|
|
75
|
+
f.q = text;
|
|
76
|
+
if (tz)
|
|
77
|
+
f.tz = tz;
|
|
78
|
+
if (automated)
|
|
79
|
+
f.automated = automated;
|
|
80
|
+
return f;
|
|
81
|
+
}
|
|
82
|
+
function defaultTz() {
|
|
83
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
84
|
+
}
|
|
85
|
+
function handle(log, fn) {
|
|
86
|
+
return (req, res) => {
|
|
87
|
+
try {
|
|
88
|
+
fn(req, res);
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
const code = error.code;
|
|
92
|
+
if (error instanceof BadRequest || (typeof code === "string" && CLIENT_ERROR_CODES.has(code))) {
|
|
93
|
+
return deny(res, 400, String(code), { message: error.message });
|
|
94
|
+
}
|
|
95
|
+
log(`${req.method} ${req.path} failed: ${error.stack ?? String(error)}`);
|
|
96
|
+
deny(res, 500, "internal_error");
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export function registerRoutes(app, deps) {
|
|
101
|
+
const { store, catalog, log } = deps;
|
|
102
|
+
app.get("/healthz", (_req, res) => {
|
|
103
|
+
const fs = deps.fsHealth?.() ?? { ok: true };
|
|
104
|
+
res.status(fs.ok ? 200 : 503).json({ ok: fs.ok, version: deps.version, name: "skillar", fs });
|
|
105
|
+
});
|
|
106
|
+
app.post("/ingest/otlp/v1/logs", (req, res) => {
|
|
107
|
+
const body = req.body;
|
|
108
|
+
res.status(202).json({ partialSuccess: {} });
|
|
109
|
+
queueMicrotask(() => {
|
|
110
|
+
const result = ingestOtlp(body, deps.ingest);
|
|
111
|
+
if (result.status !== 200)
|
|
112
|
+
log(`otlp batch rejected: ${result.body.code}: ${result.body.message}`);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
app.post("/ingest/otlp/v1/traces", (req, res) => {
|
|
116
|
+
const body = req.body;
|
|
117
|
+
res.status(202).json({ partialSuccess: {} });
|
|
118
|
+
queueMicrotask(() => {
|
|
119
|
+
try {
|
|
120
|
+
const result = ingestOtlpTraces(body, store, Date.now());
|
|
121
|
+
if (result.status !== 200)
|
|
122
|
+
log(`otlp traces batch rejected: ${result.body.code}: ${result.body.message}`);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
// The batch is stored after the 202, so a store failure has nowhere to surface but the log, and an escaping throw would kill the daemon.
|
|
126
|
+
log(`otlp traces batch failed: ${error.stack ?? String(error)}`);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
app.post("/ingest/hook/:runtime", (req, res) => {
|
|
131
|
+
const runtime = String(req.params.runtime);
|
|
132
|
+
if (!isRuntimeId(runtime)) {
|
|
133
|
+
store.countIngest("rejected", "unknown_runtime");
|
|
134
|
+
return deny(res, 400, "unknown_runtime");
|
|
135
|
+
}
|
|
136
|
+
const body = req.body;
|
|
137
|
+
res.status(202).json({ accepted: true });
|
|
138
|
+
queueMicrotask(() => {
|
|
139
|
+
ingestHook(runtime, body, deps.ingest);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
app.get("/api/sessions", handle(log, (req, res) => {
|
|
143
|
+
res.json(store.sessions(parseFilter(req.query)));
|
|
144
|
+
}));
|
|
145
|
+
app.get("/api/sessions/:id/trace", handle(log, (req, res) => {
|
|
146
|
+
res.json(store.trace(String(req.params.id)));
|
|
147
|
+
}));
|
|
148
|
+
app.get("/api/flow", handle(log, (req, res) => {
|
|
149
|
+
const unit = one(req.query.unit) ?? "first_load";
|
|
150
|
+
if (unit !== "first_load" && unit !== "all_loads")
|
|
151
|
+
throw new BadRequest("bad_request", "unit must be first_load or all_loads");
|
|
152
|
+
const top = Math.min(15, intParam(req.query, "top", [1, 1000]) ?? 7);
|
|
153
|
+
res.json(store.flow(parseFilter(req.query), unit, top));
|
|
154
|
+
}));
|
|
155
|
+
app.get("/api/heatmap", handle(log, (req, res) => {
|
|
156
|
+
const filter = parseFilter(req.query);
|
|
157
|
+
const tz = filter.tz ?? defaultTz();
|
|
158
|
+
const weekStart = intParam(req.query, "weekStart", [0, 1]) ?? 1;
|
|
159
|
+
const bucket = intParam(req.query, "bucket") ?? 1;
|
|
160
|
+
if (bucket !== 1 && bucket !== 2 && bucket !== 4)
|
|
161
|
+
throw new BadRequest("bad_request", "bucket must be 1, 2, or 4");
|
|
162
|
+
res.json(store.heatmap(filter, tz, weekStart, bucket));
|
|
163
|
+
}));
|
|
164
|
+
app.get("/api/skill-week", handle(log, (req, res) => {
|
|
165
|
+
res.json(store.skillWeek(parseFilter(req.query)));
|
|
166
|
+
}));
|
|
167
|
+
app.get("/api/stats", handle(log, (req, res) => {
|
|
168
|
+
res.json(store.stats(parseFilter(req.query)));
|
|
169
|
+
}));
|
|
170
|
+
app.get("/api/skills", handle(log, (_req, res) => {
|
|
171
|
+
res.json(catalog.list());
|
|
172
|
+
}));
|
|
173
|
+
app.post("/api/eval-cases", handle(log, (req, res) => {
|
|
174
|
+
const body = (req.body ?? {});
|
|
175
|
+
const query = typeof body.query === "string" ? body.query.trim() : "";
|
|
176
|
+
if (!query)
|
|
177
|
+
throw new BadRequest("bad_request", "query is required");
|
|
178
|
+
for (const key of ["expectedUri", "fromTraceId"]) {
|
|
179
|
+
if (body[key] !== undefined && typeof body[key] !== "string")
|
|
180
|
+
throw new BadRequest("bad_request", `${key} must be a string`);
|
|
181
|
+
}
|
|
182
|
+
store.addEvalCase({
|
|
183
|
+
query,
|
|
184
|
+
...(typeof body.expectedUri === "string" ? { expectedUri: body.expectedUri } : {}),
|
|
185
|
+
...(typeof body.fromTraceId === "string" ? { fromTraceId: body.fromTraceId } : {}),
|
|
186
|
+
});
|
|
187
|
+
res.status(201).json({ ok: true });
|
|
188
|
+
}));
|
|
189
|
+
app.get("/api/events/stream", (req, res) => deps.sse.handler(req, res));
|
|
190
|
+
registerWorkflowRoutes(app, { store, log });
|
|
191
|
+
registerFlowRoutes(app, { store, log });
|
|
192
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
2
|
+
export const DASH_COOKIE = "skillar_dash";
|
|
3
|
+
export function deny(res, status, error, extra = {}) {
|
|
4
|
+
res.status(status).json({ error, ...extra });
|
|
5
|
+
}
|
|
6
|
+
export function safeEqual(a, b) {
|
|
7
|
+
const x = Buffer.from(a);
|
|
8
|
+
const y = Buffer.from(b);
|
|
9
|
+
return x.length === y.length && timingSafeEqual(x, y);
|
|
10
|
+
}
|
|
11
|
+
export function dashCookieValue(token) {
|
|
12
|
+
return createHmac("sha256", token).update("dash").digest("hex");
|
|
13
|
+
}
|
|
14
|
+
export function bearerOf(req) {
|
|
15
|
+
const header = req.header("authorization") ?? "";
|
|
16
|
+
return header.startsWith("Bearer ") ? header.slice(7).trim() : "";
|
|
17
|
+
}
|
|
18
|
+
export function cookieOf(req, name) {
|
|
19
|
+
for (const part of (req.header("cookie") ?? "").split(";")) {
|
|
20
|
+
const eq = part.indexOf("=");
|
|
21
|
+
if (eq > 0 && part.slice(0, eq).trim() === name)
|
|
22
|
+
return part.slice(eq + 1).trim();
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* DNS-rebinding and cross-site guard. `port()` is read per request so a server bound to port 0 checks its real port.
|
|
28
|
+
* Host must be 127.0.0.1:<port> or localhost:<port>; a present Origin must be the matching http origin;
|
|
29
|
+
* `Sec-Fetch-Site: cross-site` is always refused.
|
|
30
|
+
*/
|
|
31
|
+
export function localOnly(port) {
|
|
32
|
+
return (req, res, next) => {
|
|
33
|
+
const p = port();
|
|
34
|
+
const hosts = [`127.0.0.1:${p}`, `localhost:${p}`];
|
|
35
|
+
if (!hosts.includes(req.header("host") ?? ""))
|
|
36
|
+
return deny(res, 403, "host_not_allowed");
|
|
37
|
+
const origin = req.header("origin");
|
|
38
|
+
if (origin !== undefined && !hosts.some((h) => origin === `http://${h}`))
|
|
39
|
+
return deny(res, 403, "origin_not_allowed");
|
|
40
|
+
if ((req.header("sec-fetch-site") ?? "").toLowerCase() === "cross-site")
|
|
41
|
+
return deny(res, 403, "cross_site_not_allowed");
|
|
42
|
+
next();
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* `/mcp` and `/ingest/*` take only the bearer token; `/api/*` also accepts the dashboard cookie; everything else is public.
|
|
47
|
+
* The path is classified lowercased and percent-decoded so case or encoding variants (`/API/`, `/%61pi/`) cannot
|
|
48
|
+
* skip the gate even if a router ever matches them; a path that does not decode is refused.
|
|
49
|
+
*/
|
|
50
|
+
export function authGate(token) {
|
|
51
|
+
const cookie = dashCookieValue(token);
|
|
52
|
+
return (req, res, next) => {
|
|
53
|
+
let path;
|
|
54
|
+
try {
|
|
55
|
+
path = decodeURIComponent(req.path).toLowerCase();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return deny(res, 400, "bad_path");
|
|
59
|
+
}
|
|
60
|
+
const bearerOnly = path === "/mcp" || path.startsWith("/mcp/") || path.startsWith("/ingest/");
|
|
61
|
+
const api = path === "/api" || path.startsWith("/api/");
|
|
62
|
+
if (!bearerOnly && !api)
|
|
63
|
+
return next();
|
|
64
|
+
if (safeEqual(bearerOf(req), token))
|
|
65
|
+
return next();
|
|
66
|
+
if (api) {
|
|
67
|
+
const presented = cookieOf(req, DASH_COOKIE) ?? cookieOf(req, "gisool_dash");
|
|
68
|
+
if (presented !== undefined && safeEqual(presented, cookie))
|
|
69
|
+
return next();
|
|
70
|
+
}
|
|
71
|
+
deny(res, 401, "unauthorized");
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const MUTATIONS = new Set(["POST", "PUT", "PATCH"]);
|
|
75
|
+
/** Mutations must declare a JSON body; OTLP protobuf gets a hint to switch the exporter to http/json. */
|
|
76
|
+
export function requireJson(onReject) {
|
|
77
|
+
return (req, res, next) => {
|
|
78
|
+
if (!MUTATIONS.has(req.method) || req.is("application/json"))
|
|
79
|
+
return next();
|
|
80
|
+
onReject?.(req);
|
|
81
|
+
const protobuf = req.path.startsWith("/ingest/otlp/") && /protobuf/i.test(req.header("content-type") ?? "");
|
|
82
|
+
deny(res, 415, "unsupported_media_type", protobuf
|
|
83
|
+
? { hint: "skillar accepts OTLP/HTTP JSON only; set OTEL_EXPORTER_OTLP_PROTOCOL=http/json (or OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=http/json)" }
|
|
84
|
+
: { hint: "send Content-Type: application/json" });
|
|
85
|
+
};
|
|
86
|
+
}
|
package/dist/api/sse.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export function createSseHub(lookup, log, heartbeatMs = 25_000) {
|
|
2
|
+
const clients = new Set();
|
|
3
|
+
const pending = new Set();
|
|
4
|
+
let scheduled = false;
|
|
5
|
+
const flush = () => {
|
|
6
|
+
scheduled = false;
|
|
7
|
+
const ids = [...pending];
|
|
8
|
+
pending.clear();
|
|
9
|
+
if (clients.size === 0)
|
|
10
|
+
return;
|
|
11
|
+
for (const id of ids) {
|
|
12
|
+
let row;
|
|
13
|
+
try {
|
|
14
|
+
row = lookup(id);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
log(`sse lookup for session ${id} failed: ${error.message}`);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (!row)
|
|
21
|
+
continue;
|
|
22
|
+
const frame = `event: session\ndata: ${JSON.stringify(row)}\n\n`;
|
|
23
|
+
for (const res of clients)
|
|
24
|
+
res.write(frame);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const heartbeat = setInterval(() => {
|
|
28
|
+
for (const res of clients)
|
|
29
|
+
res.write(": ping\n\n");
|
|
30
|
+
}, heartbeatMs);
|
|
31
|
+
heartbeat.unref();
|
|
32
|
+
return {
|
|
33
|
+
handler(req, res) {
|
|
34
|
+
res.writeHead(200, { "Content-Type": "text/event-stream; charset=utf-8", "Cache-Control": "no-store", Connection: "keep-alive", "X-Accel-Buffering": "no" });
|
|
35
|
+
res.write(": connected\n\n");
|
|
36
|
+
clients.add(res);
|
|
37
|
+
req.on("close", () => clients.delete(res));
|
|
38
|
+
},
|
|
39
|
+
notify(sessionId) {
|
|
40
|
+
pending.add(sessionId);
|
|
41
|
+
if (!scheduled) {
|
|
42
|
+
scheduled = true;
|
|
43
|
+
setImmediate(flush);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
clientCount: () => clients.size,
|
|
47
|
+
close() {
|
|
48
|
+
clearInterval(heartbeat);
|
|
49
|
+
for (const res of clients)
|
|
50
|
+
res.end();
|
|
51
|
+
clients.clear();
|
|
52
|
+
pending.clear();
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import express from "express";
|
|
5
|
+
import { DASH_COOKIE, dashCookieValue, deny, safeEqual } from "./security.js";
|
|
6
|
+
export function defaultDashboardDir() {
|
|
7
|
+
return fileURLToPath(new URL("../dashboard/", import.meta.url));
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* `GET /?token=<token>` trades the bearer token for the HttpOnly dashboard cookie and redirects to `/`;
|
|
11
|
+
* `GET /` serves index.html uncached; `/assets/*` serves the built bundle.
|
|
12
|
+
*/
|
|
13
|
+
export function registerStatic(app, opts) {
|
|
14
|
+
const dir = opts.dir ?? defaultDashboardDir();
|
|
15
|
+
const cookie = dashCookieValue(opts.token);
|
|
16
|
+
app.get("/", (req, res) => {
|
|
17
|
+
const presented = req.query.token;
|
|
18
|
+
if (presented !== undefined) {
|
|
19
|
+
if (typeof presented !== "string" || !safeEqual(presented, opts.token))
|
|
20
|
+
return deny(res, 401, "unauthorized");
|
|
21
|
+
// Persistent (400 days, the browser maximum) so the dashboard still opens after a browser restart.
|
|
22
|
+
res.setHeader("Set-Cookie", `${DASH_COOKIE}=${cookie}; HttpOnly; SameSite=Strict; Path=/; Max-Age=34560000`);
|
|
23
|
+
res.setHeader("Cache-Control", "no-store");
|
|
24
|
+
return res.redirect(302, "/");
|
|
25
|
+
}
|
|
26
|
+
const index = join(dir, "index.html");
|
|
27
|
+
if (!existsSync(index))
|
|
28
|
+
return deny(res, 503, "dashboard_not_built", { hint: "run npm run build in the skillar package" });
|
|
29
|
+
res.setHeader("Cache-Control", "no-store");
|
|
30
|
+
res.sendFile(index);
|
|
31
|
+
});
|
|
32
|
+
app.use("/assets", express.static(join(dir, "assets"), { index: false, fallthrough: false }));
|
|
33
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Ported from server/src/index.ts @48e7955
|
|
2
|
+
import { parse as parseYaml } from "yaml";
|
|
3
|
+
export const BODY_HEAD_MAX = 600;
|
|
4
|
+
/** Splits SKILL.md into YAML frontmatter (object) and body; frontmatter is null when absent or invalid. */
|
|
5
|
+
export function splitSkillMarkdown(input) {
|
|
6
|
+
const markdown = input.replace(/\r\n/g, "\n");
|
|
7
|
+
if (!markdown.startsWith("---\n"))
|
|
8
|
+
return { frontmatter: null, body: markdown };
|
|
9
|
+
const end = markdown.indexOf("\n---", 4);
|
|
10
|
+
if (end === -1)
|
|
11
|
+
return { frontmatter: null, body: markdown };
|
|
12
|
+
const afterDelimiter = markdown.slice(end + 4, end + 5);
|
|
13
|
+
if (afterDelimiter !== "" && afterDelimiter !== "\n")
|
|
14
|
+
return { frontmatter: null, body: markdown };
|
|
15
|
+
const body = markdown.slice(end + 5);
|
|
16
|
+
let parsed;
|
|
17
|
+
try {
|
|
18
|
+
parsed = parseYaml(markdown.slice(4, end));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return { frontmatter: null, body };
|
|
22
|
+
}
|
|
23
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
24
|
+
return { frontmatter: null, body };
|
|
25
|
+
return { frontmatter: parsed, body };
|
|
26
|
+
}
|
|
27
|
+
/** Accepts a YAML list of strings or a comma-separated string; anything else yields []. */
|
|
28
|
+
export function stringList(value) {
|
|
29
|
+
if (Array.isArray(value))
|
|
30
|
+
return value.filter((item) => typeof item === "string").map((s) => s.trim()).filter(Boolean);
|
|
31
|
+
if (typeof value === "string")
|
|
32
|
+
return value.split(",").map((s) => s.trim()).filter(Boolean);
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
function explicitGroup(value) {
|
|
36
|
+
if (typeof value === "string" && value.trim())
|
|
37
|
+
return { id: value.trim() };
|
|
38
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
39
|
+
return undefined;
|
|
40
|
+
const record = value;
|
|
41
|
+
if (typeof record.id !== "string" || !record.id.trim())
|
|
42
|
+
return undefined;
|
|
43
|
+
const stage = typeof record.stage === "string" && record.stage.trim() ? record.stage.trim() : typeof record.stage === "number" ? String(record.stage) : undefined;
|
|
44
|
+
return stage === undefined ? { id: record.id.trim() } : { id: record.id.trim(), stage };
|
|
45
|
+
}
|
|
46
|
+
/** First non-heading paragraph of the body, whitespace-collapsed, at most BODY_HEAD_MAX chars. */
|
|
47
|
+
export function bodyHead(body) {
|
|
48
|
+
let inFence = false;
|
|
49
|
+
const paragraphs = [];
|
|
50
|
+
let current = [];
|
|
51
|
+
for (const line of body.split("\n")) {
|
|
52
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
53
|
+
inFence = !inFence;
|
|
54
|
+
if (current.length) {
|
|
55
|
+
paragraphs.push(current.join(" "));
|
|
56
|
+
current = [];
|
|
57
|
+
}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (inFence)
|
|
61
|
+
continue;
|
|
62
|
+
if (!line.trim()) {
|
|
63
|
+
if (current.length) {
|
|
64
|
+
paragraphs.push(current.join(" "));
|
|
65
|
+
current = [];
|
|
66
|
+
}
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (/^\s{0,3}#/.test(line)) {
|
|
70
|
+
if (current.length) {
|
|
71
|
+
paragraphs.push(current.join(" "));
|
|
72
|
+
current = [];
|
|
73
|
+
}
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
current.push(line.trim());
|
|
77
|
+
if (current.join(" ").length > BODY_HEAD_MAX)
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
if (current.length)
|
|
81
|
+
paragraphs.push(current.join(" "));
|
|
82
|
+
const first = paragraphs.find((p) => p.trim()) ?? "";
|
|
83
|
+
return first.replace(/\s+/g, " ").trim().slice(0, BODY_HEAD_MAX);
|
|
84
|
+
}
|
|
85
|
+
/** Extracts the routing fields; returns null when name or description is missing. */
|
|
86
|
+
export function extractFields(frontmatter, body) {
|
|
87
|
+
const { name, description } = frontmatter;
|
|
88
|
+
if (typeof name !== "string" || [...name].length < 1 || [...name].length > 64 ||
|
|
89
|
+
name !== name.toLowerCase() || !/^[\p{L}\p{N}]+(?:-[\p{L}\p{N}]+)*$/u.test(name))
|
|
90
|
+
return null;
|
|
91
|
+
if (typeof description !== "string" || !description.trim() || [...description].length > 1024)
|
|
92
|
+
return null;
|
|
93
|
+
const { compatibility, metadata, license } = frontmatter;
|
|
94
|
+
if (compatibility !== undefined && (typeof compatibility !== "string" || !compatibility.trim() || [...compatibility].length > 500))
|
|
95
|
+
return null;
|
|
96
|
+
if (license !== undefined && typeof license !== "string")
|
|
97
|
+
return null;
|
|
98
|
+
if (frontmatter["allowed-tools"] !== undefined && typeof frontmatter["allowed-tools"] !== "string")
|
|
99
|
+
return null;
|
|
100
|
+
if (metadata !== undefined && (!metadata || typeof metadata !== "object" || Array.isArray(metadata) || Object.values(metadata).some((v) => typeof v !== "string")))
|
|
101
|
+
return null;
|
|
102
|
+
// metadata is preserved verbatim but never interpreted, including reserved io.modelcontextprotocol/* keys.
|
|
103
|
+
const whenRaw = frontmatter.when_to_use ?? frontmatter["when-to-use"];
|
|
104
|
+
const fields = {
|
|
105
|
+
name,
|
|
106
|
+
description,
|
|
107
|
+
tags: stringList(frontmatter.tags),
|
|
108
|
+
examples: stringList(frontmatter.examples),
|
|
109
|
+
aliasesKo: stringList(frontmatter.aliases_ko),
|
|
110
|
+
invocation: frontmatter["disable-model-invocation"] === true || frontmatter["disable-model-invocation"] === "true" ? "explicit" : "automatic",
|
|
111
|
+
bodyHead: bodyHead(body),
|
|
112
|
+
};
|
|
113
|
+
if (typeof whenRaw === "string" && whenRaw.trim())
|
|
114
|
+
fields.whenToUse = whenRaw.trim();
|
|
115
|
+
const group = explicitGroup(frontmatter.group);
|
|
116
|
+
if (group)
|
|
117
|
+
fields.group = group;
|
|
118
|
+
return fields;
|
|
119
|
+
}
|
|
120
|
+
/** Family id derived from a name: the prefix before the first "-" (the whole name when there is none). */
|
|
121
|
+
export function familyPrefix(name) {
|
|
122
|
+
const index = name.indexOf("-");
|
|
123
|
+
return index === -1 ? name : name.slice(0, index);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Assigns derived groups (prefix families with >= 2 distinct names) to entries lacking an explicit group,
|
|
127
|
+
* and returns every family: explicit groups plus derived prefix families, uris sorted.
|
|
128
|
+
*/
|
|
129
|
+
export function applyFamilies(entries, explicit) {
|
|
130
|
+
const namesByPrefix = new Map();
|
|
131
|
+
for (const entry of entries) {
|
|
132
|
+
if (explicit.get(entry.uri))
|
|
133
|
+
continue;
|
|
134
|
+
const prefix = familyPrefix(entry.name);
|
|
135
|
+
if (!namesByPrefix.has(prefix))
|
|
136
|
+
namesByPrefix.set(prefix, new Set());
|
|
137
|
+
namesByPrefix.get(prefix).add(entry.name);
|
|
138
|
+
}
|
|
139
|
+
const families = new Map();
|
|
140
|
+
for (const entry of entries) {
|
|
141
|
+
const group = explicit.get(entry.uri);
|
|
142
|
+
if (group) {
|
|
143
|
+
entry.group = group;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const prefix = familyPrefix(entry.name);
|
|
147
|
+
if ((namesByPrefix.get(prefix)?.size ?? 0) >= 2)
|
|
148
|
+
entry.group = { id: prefix };
|
|
149
|
+
else
|
|
150
|
+
delete entry.group;
|
|
151
|
+
}
|
|
152
|
+
if (entry.group) {
|
|
153
|
+
if (!families.has(entry.group.id))
|
|
154
|
+
families.set(entry.group.id, []);
|
|
155
|
+
families.get(entry.group.id).push(entry.uri);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return [...families.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)).map(([id, uris]) => ({ id, uris: uris.sort() }));
|
|
159
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { applyFamilies } from "./fields.js";
|
|
3
|
+
import { assertRegularSkillPath, CatalogError, scanRoot, sha256 } from "./scanner.js";
|
|
4
|
+
import { parseSkillUri, skillUri } from "./uri.js";
|
|
5
|
+
import { watchRoots } from "./watcher.js";
|
|
6
|
+
export { CatalogError } from "./scanner.js";
|
|
7
|
+
export { parseSkillUri, skillUri } from "./uri.js";
|
|
8
|
+
function deltaIsEmpty(d) {
|
|
9
|
+
return d.added.length === 0 && d.changed.length === 0 && d.removed.length === 0;
|
|
10
|
+
}
|
|
11
|
+
/** Indexes the configured skill roots in place. Nothing is scanned until refresh(); nothing is ever written. */
|
|
12
|
+
export function createCatalog(config) {
|
|
13
|
+
const roots = config.roots.map((r) => ({ id: r.id, path: r.path }));
|
|
14
|
+
const log = config.log ?? console.error;
|
|
15
|
+
let entries = new Map();
|
|
16
|
+
let resourceIndex = new Map();
|
|
17
|
+
let families = [];
|
|
18
|
+
let rootStatus = roots.map((r) => ({ id: r.id, path: r.path, readable: false, count: 0 }));
|
|
19
|
+
const listeners = new Set();
|
|
20
|
+
let chain = Promise.resolve();
|
|
21
|
+
async function scan() {
|
|
22
|
+
const next = [];
|
|
23
|
+
const explicit = new Map();
|
|
24
|
+
const status = [];
|
|
25
|
+
const seen = new Set();
|
|
26
|
+
for (const root of roots) {
|
|
27
|
+
const { readable, skills } = await scanRoot(root, log);
|
|
28
|
+
let count = 0;
|
|
29
|
+
for (const { entry, explicitGroup } of skills) {
|
|
30
|
+
if (seen.has(entry.uri))
|
|
31
|
+
continue;
|
|
32
|
+
seen.add(entry.uri);
|
|
33
|
+
next.push(entry);
|
|
34
|
+
explicit.set(entry.uri, explicitGroup);
|
|
35
|
+
count += 1;
|
|
36
|
+
}
|
|
37
|
+
status.push({ id: root.id, path: root.path, readable, count });
|
|
38
|
+
}
|
|
39
|
+
next.sort((a, b) => (a.uri < b.uri ? -1 : a.uri > b.uri ? 1 : 0));
|
|
40
|
+
const nextFamilies = applyFamilies(next, explicit);
|
|
41
|
+
const delta = { added: [], changed: [], removed: [] };
|
|
42
|
+
const nextMap = new Map(next.map((e) => [e.uri, e]));
|
|
43
|
+
for (const entry of next) {
|
|
44
|
+
const prev = entries.get(entry.uri);
|
|
45
|
+
if (!prev)
|
|
46
|
+
delta.added.push(entry);
|
|
47
|
+
else if (prev.manifestDigest !== entry.manifestDigest || JSON.stringify(prev.group) !== JSON.stringify(entry.group))
|
|
48
|
+
delta.changed.push(entry);
|
|
49
|
+
}
|
|
50
|
+
for (const uri of entries.keys())
|
|
51
|
+
if (!nextMap.has(uri))
|
|
52
|
+
delta.removed.push(uri);
|
|
53
|
+
const nextResources = new Map();
|
|
54
|
+
for (const entry of next)
|
|
55
|
+
for (const r of entry.resources)
|
|
56
|
+
nextResources.set(r.uri, { entry, digest: r.digest, size: r.size });
|
|
57
|
+
entries = nextMap;
|
|
58
|
+
resourceIndex = nextResources;
|
|
59
|
+
families = nextFamilies;
|
|
60
|
+
rootStatus = status;
|
|
61
|
+
if (!deltaIsEmpty(delta)) {
|
|
62
|
+
for (const cb of listeners) {
|
|
63
|
+
try {
|
|
64
|
+
cb(delta);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
log(`catalog onDelta listener failed: ${error.message}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return delta;
|
|
72
|
+
}
|
|
73
|
+
function refresh() {
|
|
74
|
+
const run = chain.then(scan, scan);
|
|
75
|
+
chain = run.catch(() => undefined);
|
|
76
|
+
return run;
|
|
77
|
+
}
|
|
78
|
+
const watcher = config.watch
|
|
79
|
+
? watchRoots(roots, () => {
|
|
80
|
+
refresh().catch((error) => log(`catalog refresh failed: ${error.message}`));
|
|
81
|
+
}, { debounceMs: config.watchDebounceMs, log })
|
|
82
|
+
: undefined;
|
|
83
|
+
return {
|
|
84
|
+
list: () => [...entries.values()],
|
|
85
|
+
get: (uri) => entries.get(uri),
|
|
86
|
+
async readResource(uri) {
|
|
87
|
+
const parsed = parseSkillUri(uri);
|
|
88
|
+
if (!parsed)
|
|
89
|
+
throw new CatalogError("invalid_uri", "not a valid skill://local URI");
|
|
90
|
+
const root = roots.find((r) => r.id === parsed.rootId);
|
|
91
|
+
if (!root)
|
|
92
|
+
throw new CatalogError("unknown_root", `unknown skill root: ${parsed.rootId}`);
|
|
93
|
+
const canonical = skillUri(parsed.rootId, ...parsed.segments);
|
|
94
|
+
const filePath = await assertRegularSkillPath(root.path, parsed.segments);
|
|
95
|
+
const known = resourceIndex.get(canonical);
|
|
96
|
+
if (!known)
|
|
97
|
+
throw new CatalogError("unknown_resource", "resource is not in any skill manifest");
|
|
98
|
+
const bytes = await readFile(filePath);
|
|
99
|
+
const digest = sha256(bytes);
|
|
100
|
+
if (digest !== known.digest)
|
|
101
|
+
throw new CatalogError("digest_mismatch", "resource changed since the last catalog refresh");
|
|
102
|
+
return { text: bytes.toString("utf8"), digest, size: bytes.byteLength, bytes };
|
|
103
|
+
},
|
|
104
|
+
refresh,
|
|
105
|
+
onDelta(cb) {
|
|
106
|
+
listeners.add(cb);
|
|
107
|
+
return () => listeners.delete(cb);
|
|
108
|
+
},
|
|
109
|
+
report() {
|
|
110
|
+
const byName = new Map();
|
|
111
|
+
for (const entry of entries.values()) {
|
|
112
|
+
if (!byName.has(entry.name))
|
|
113
|
+
byName.set(entry.name, { roots: new Set(), uris: [] });
|
|
114
|
+
const slot = byName.get(entry.name);
|
|
115
|
+
slot.roots.add(entry.rootId);
|
|
116
|
+
slot.uris.push(entry.uri);
|
|
117
|
+
}
|
|
118
|
+
const duplicates = [...byName.entries()]
|
|
119
|
+
.filter(([, v]) => v.roots.size > 1)
|
|
120
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
121
|
+
.map(([name, v]) => ({ name, uris: v.uris.sort() }));
|
|
122
|
+
return {
|
|
123
|
+
duplicates,
|
|
124
|
+
families: families.map((f) => ({ id: f.id, uris: [...f.uris] })),
|
|
125
|
+
roots: rootStatus.map((r) => ({ ...r })),
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
close() {
|
|
129
|
+
watcher?.close();
|
|
130
|
+
listeners.clear();
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|