@pixerate/editor 0.272.0 → 0.273.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.
Files changed (65) hide show
  1. package/dist/agent/index.cjs +34 -32
  2. package/dist/agent/index.js +10 -8
  3. package/dist/chart/contract.d.cts +95 -0
  4. package/dist/chart/contract.d.ts +95 -0
  5. package/dist/chart/controller.d.cts +4 -0
  6. package/dist/chart/controller.d.ts +4 -0
  7. package/dist/chart/index.cjs +492 -26
  8. package/dist/chart/index.d.cts +1 -0
  9. package/dist/chart/index.d.ts +1 -0
  10. package/dist/chart/index.js +476 -10
  11. package/dist/chart/tools.d.cts +3 -0
  12. package/dist/chart/tools.d.ts +3 -0
  13. package/dist/chart-maps/index.cjs +4 -64
  14. package/dist/chart-maps/index.js +3 -63
  15. package/dist/chunk-2A5IKZMU.js +557 -0
  16. package/dist/{chunk-NMZQMPFZ.js → chunk-4LZQO5Y7.js} +2 -0
  17. package/dist/chunk-5UGRMVNE.js +328 -0
  18. package/dist/{chunk-X4FTNFIA.js → chunk-7MGPW2OV.js} +1 -1
  19. package/dist/{chunk-DGITFN73.cjs → chunk-BWJBZAUE.cjs} +8 -8
  20. package/dist/chunk-CHZYVNXZ.js +336 -0
  21. package/dist/chunk-DRMZ5R3S.cjs +67 -0
  22. package/dist/chunk-EI5GFTKG.js +67 -0
  23. package/dist/chunk-JV7DKLMX.js +1073 -0
  24. package/dist/chunk-MFBUQC67.cjs +328 -0
  25. package/dist/{chunk-IBXEQOGI.js → chunk-PYHB2EHE.js} +2 -324
  26. package/dist/chunk-QEUI6OGE.cjs +336 -0
  27. package/dist/{chunk-6ZSFRRJI.js → chunk-RSF6BT34.js} +1 -1
  28. package/dist/chunk-T2HUQBRC.cjs +1073 -0
  29. package/dist/{chunk-GSLOH2IN.cjs → chunk-VF6ODSYX.cjs} +2 -0
  30. package/dist/{chunk-NQUKTRKN.cjs → chunk-WZLNNGG3.cjs} +45 -45
  31. package/dist/{chunk-NRBGAEFI.cjs → chunk-Y3RUYOOW.cjs} +8 -330
  32. package/dist/chunk-YH6SKC7P.cjs +557 -0
  33. package/dist/commands/index.cjs +7 -323
  34. package/dist/commands/index.js +5 -321
  35. package/dist/extensions/index.cjs +4 -4
  36. package/dist/extensions/index.js +3 -3
  37. package/dist/index.cjs +9 -9
  38. package/dist/index.js +3 -3
  39. package/dist/paint-editor/controller.d.cts +86 -0
  40. package/dist/paint-editor/controller.d.ts +86 -0
  41. package/dist/paint-editor/document.d.cts +1 -1
  42. package/dist/paint-editor/document.d.ts +1 -1
  43. package/dist/paint-editor/index.cjs +559 -166
  44. package/dist/paint-editor/index.d.cts +1 -0
  45. package/dist/paint-editor/index.d.ts +1 -0
  46. package/dist/paint-editor/index.js +397 -4
  47. package/dist/paint-editor/presence.d.cts +5 -1
  48. package/dist/paint-editor/presence.d.ts +5 -1
  49. package/dist/paint-editor/ui.d.cts +5 -2
  50. package/dist/paint-editor/ui.d.ts +5 -2
  51. package/dist/persistence/index.cjs +24 -1042
  52. package/dist/persistence/index.js +22 -1040
  53. package/dist/raster/fill.d.cts +85 -0
  54. package/dist/raster/fill.d.ts +85 -0
  55. package/dist/raster/index.cjs +9 -1
  56. package/dist/raster/index.d.cts +1 -0
  57. package/dist/raster/index.d.ts +1 -0
  58. package/dist/raster/index.js +8 -0
  59. package/dist/tools/index.cjs +5 -3
  60. package/dist/tools/index.js +8 -6
  61. package/package.json +1 -1
  62. package/skills/pixerate-paint-editor/SKILL.md +19 -43
  63. package/skills/pixerate-paint-editor/references/evolution.md +27 -0
  64. package/skills/pixerate-paint-sketch-to-finish/SKILL.md +16 -5
  65. package/skills/pixerate-paint-sketch-to-finish/evals/flats-under-ink.json +2 -3
@@ -0,0 +1,328 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+ var _chunk7TD63DZPcjs = require('./chunk-7TD63DZP.cjs');
4
+
5
+
6
+ var _chunkWOFXZ3XUcjs = require('./chunk-WOFXZ3XU.cjs');
7
+
8
+
9
+
10
+ var _chunk2BSBFBOAcjs = require('./chunk-2BSBFBOA.cjs');
11
+
12
+ // src/tools/style.ts
13
+ var STYLE_SKILL_NAME = /^[a-z0-9][a-z0-9-]{0,63}$/;
14
+ var isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
15
+ function parseStyleDomains(markdown) {
16
+ const match = /^---\r?\n([\s\S]*?)\r?\n---/.exec(markdown);
17
+ const line = _optionalChain([match, 'optionalAccess', _ => _[1], 'access', _2 => _2.split, 'call', _3 => _3(/\r?\n/), 'access', _4 => _4.find, 'call', _5 => _5((l) => /^domains:/.test(l))]);
18
+ if (!line) return void 0;
19
+ const raw = line.slice("domains:".length).trim().replace(/^\[|\]$/g, "");
20
+ return raw.split(",").map((d) => d.trim().replace(/^(['"])(.*)\1$/, "$2")).filter(Boolean);
21
+ }
22
+ function memoryStyleStore(initial = {}, state = {}) {
23
+ const files = /* @__PURE__ */ new Map();
24
+ const states = new Map(Object.entries(state));
25
+ const listeners = /* @__PURE__ */ new Set();
26
+ const notify = () => listeners.forEach((l) => l());
27
+ for (const [path, text] of Object.entries(initial)) {
28
+ const [dir, ...rest] = path.replace(/\\/g, "/").replace(/^\.?\//, "").split("/");
29
+ if (!dir || !rest.length) continue;
30
+ if (!files.has(dir)) files.set(dir, /* @__PURE__ */ new Map());
31
+ files.get(dir).set(rest.join("/"), text);
32
+ }
33
+ return {
34
+ list: () => [.../* @__PURE__ */ new Set([...files.keys(), ...states.keys()])].sort().map((name) => ({ name, ...states.get(name) })),
35
+ read: (name, file) => _optionalChain([files, 'access', _6 => _6.get, 'call', _7 => _7(name), 'optionalAccess', _8 => _8.get, 'call', _9 => _9(file)]),
36
+ files: (name) => [..._nullishCoalesce(_optionalChain([files, 'access', _10 => _10.get, 'call', _11 => _11(name), 'optionalAccess', _12 => _12.keys, 'call', _13 => _13()]), () => ( []))].sort(),
37
+ subscribe(listener) {
38
+ listeners.add(listener);
39
+ return () => listeners.delete(listener);
40
+ },
41
+ set(name, skillFiles) {
42
+ files.set(name, new Map(Object.entries(skillFiles)));
43
+ notify();
44
+ },
45
+ remove(name) {
46
+ files.delete(name);
47
+ states.delete(name);
48
+ notify();
49
+ },
50
+ setState(name, next) {
51
+ if (next) states.set(name, next);
52
+ else states.delete(name);
53
+ notify();
54
+ }
55
+ };
56
+ }
57
+ function resolveTokens(value, tokens, path, errors) {
58
+ if (Array.isArray(value)) return value.map((v, i) => resolveTokens(v, tokens, `${path}[${i}]`, errors));
59
+ if (!isPlainObject(value)) return value;
60
+ if ("$token" in value) {
61
+ const token = value.$token;
62
+ const mode = value.mode;
63
+ if (typeof token !== "string" || !token || mode !== void 0 && typeof mode !== "string" || Object.keys(value).some((k) => k !== "$token" && k !== "mode")) {
64
+ errors.push(`${path || "profile"}: a token reference is { "$token": "<variable name or id>", "mode"?: "<mode>" }.`);
65
+ return void 0;
66
+ }
67
+ if (!tokens) {
68
+ errors.push(`${path}: "${token}" is a token reference, but this host has no design tokens to resolve it.`);
69
+ return void 0;
70
+ }
71
+ const resolved = tokens.resolve({ token, ...mode ? { mode } : {} });
72
+ if (!resolved.ok) errors.push(`${path}: ${resolved.message}`);
73
+ return resolved.ok ? resolved.value : void 0;
74
+ }
75
+ const out = {};
76
+ for (const [k, v] of Object.entries(value)) out[k] = resolveTokens(v, tokens, path ? `${path}.${k}` : k, errors);
77
+ return out;
78
+ }
79
+ function checkListErrors(list, domain) {
80
+ if (list === void 0) return [];
81
+ if (!Array.isArray(list)) return ['checks: expected a list of check ids or { "id": ..., options } objects.'];
82
+ const errors = [];
83
+ list.forEach((entry, i) => {
84
+ const id = typeof entry === "string" ? entry : isPlainObject(entry) ? entry.id : void 0;
85
+ if (typeof id !== "string" || !id) {
86
+ errors.push(`checks[${i}]: expected a check id or { "id": ..., options }.`);
87
+ return;
88
+ }
89
+ const check = domain.checks.find((c) => c.id === id);
90
+ if (!check || typeof entry === "string") return;
91
+ const { id: _id, ...options } = entry;
92
+ const schema = _nullishCoalesce(check.optionsSchema, () => ( { type: "object", properties: {}, additionalProperties: false }));
93
+ for (const e of _chunkWOFXZ3XUcjs.validateJsonSchema.call(void 0, options, schema)) errors.push(`checks[${i}] (${id})${e.path}: ${e.message}`);
94
+ });
95
+ return errors;
96
+ }
97
+ function mergeProfile(target, value, layer, skill, sources, locked, issues, prefix = "") {
98
+ for (const [key, next] of Object.entries(value)) {
99
+ if (_chunk2BSBFBOAcjs.isUnsafeJsonKey.call(void 0, key) || next === void 0) continue;
100
+ const path = prefix ? `${prefix}.${key}` : key;
101
+ const lockedBy = [...locked.entries()].find(([p]) => p === path || path.startsWith(`${p}.`) || p.startsWith(`${path}.`));
102
+ if (isPlainObject(next)) {
103
+ if (!isPlainObject(target[key])) {
104
+ if (lockedBy && lockedBy[1] !== layer.id && target[key] !== void 0) {
105
+ issues.push({ skill, layer: layer.id, message: `"${path}" is locked by the ${lockedBy[1]} layer, so this skill's value is ignored.` });
106
+ continue;
107
+ }
108
+ target[key] = {};
109
+ delete sources[path];
110
+ }
111
+ mergeProfile(target[key], next, layer, skill, sources, locked, issues, path);
112
+ continue;
113
+ }
114
+ if (lockedBy && lockedBy[1] !== layer.id) {
115
+ issues.push({ skill, layer: layer.id, message: `"${path}" is locked by the ${lockedBy[1]} layer, so this skill's value is ignored.` });
116
+ continue;
117
+ }
118
+ target[key] = Array.isArray(next) ? structuredClone(next) : next;
119
+ for (const p of Object.keys(sources)) if (p.startsWith(`${path}.`)) delete sources[p];
120
+ sources[path] = layer.id;
121
+ if (layer.locked) locked.set(path, layer.id);
122
+ }
123
+ }
124
+ function createStyleResolver(options) {
125
+ const layers = [...options.layers];
126
+ const domains = [...options.domains];
127
+ const ids = /* @__PURE__ */ new Set();
128
+ for (const layer of layers) {
129
+ if (ids.has(layer.id)) throw new Error(`Two style layers are called "${layer.id}"; give each layer its own id.`);
130
+ ids.add(layer.id);
131
+ }
132
+ const domainIds = domains.map((d) => d.id);
133
+ const requireDomain = (id) => {
134
+ const domain = domains.find((d) => d.id === id);
135
+ if (!domain) throw new Error(`There is no house-style domain "${id}"; expected one of ${domainIds.map((d) => `"${d}"`).join(", ") || "(none registered)"}.`);
136
+ return domain;
137
+ };
138
+ async function load() {
139
+ const issues = [];
140
+ const entries = await Promise.all(layers.map(async (layer) => [...await layer.store.list()]));
141
+ const names = [...new Set(entries.flat().map((e) => e.name))];
142
+ const skills = [];
143
+ for (const name of names) {
144
+ if (!STYLE_SKILL_NAME.test(name)) {
145
+ issues.push({ skill: name, message: `"${name}" is not a valid skill name (lowercase letters, digits and hyphens, at most 64).` });
146
+ continue;
147
+ }
148
+ const copies = [];
149
+ for (let i = 0; i < layers.length; i++) {
150
+ const text = await layers[i].store.read(name, "SKILL.md");
151
+ if (text !== void 0) copies.push({ index: i, text });
152
+ }
153
+ if (!copies.length) continue;
154
+ const lockedCopy = copies.find((c) => layers[c.index].locked);
155
+ const winner = _nullishCoalesce(lockedCopy, () => ( copies[copies.length - 1]));
156
+ const layer = layers[winner.index];
157
+ const skillIssues = [];
158
+ for (const c of copies) {
159
+ if (c.index > winner.index) skillIssues.push(`The ${layers[c.index].id} layer's copy is ignored: the ${layer.id} layer is locked.`);
160
+ }
161
+ let enabled = true;
162
+ let order = 0;
163
+ for (let i = 0; i < layers.length; i++) {
164
+ const entry = entries[i].find((e) => e.name === name);
165
+ if (!entry) continue;
166
+ if (i === winner.index && entry.order !== void 0) order = entry.order;
167
+ if (entry.enabled === void 0) continue;
168
+ if (layer.required && i !== winner.index) {
169
+ if (entry.enabled === false) skillIssues.push(`The ${layers[i].id} layer cannot switch it off: the ${layer.id} layer requires it.`);
170
+ continue;
171
+ }
172
+ enabled = entry.enabled;
173
+ }
174
+ const { description = "", body } = _chunk7TD63DZPcjs.parseSkillFrontmatter.call(void 0, winner.text);
175
+ const named = parseStyleDomains(winner.text);
176
+ const applies = /* @__PURE__ */ new Set();
177
+ for (const d of _nullishCoalesce(named, () => ( domainIds))) {
178
+ if (domainIds.includes(d)) applies.add(d);
179
+ else skillIssues.push(`It names an unknown domain "${d}"; the domains here are ${domainIds.join(", ")}.`);
180
+ }
181
+ const profiles = /* @__PURE__ */ new Map();
182
+ for (const domain of domains) {
183
+ const raw = await layer.store.read(name, `${domain.id}.json`);
184
+ if (raw === void 0) continue;
185
+ const file = `${domain.id}.json`;
186
+ if (!applies.has(domain.id)) {
187
+ skillIssues.push(`${file} is left out: the skill's domains do not include ${domain.id}.`);
188
+ continue;
189
+ }
190
+ let parsed;
191
+ try {
192
+ parsed = _chunk2BSBFBOAcjs.stripPrototypeKeys.call(void 0, JSON.parse(raw));
193
+ } catch (error) {
194
+ skillIssues.push(`${file} is not valid JSON: ${error.message}`);
195
+ continue;
196
+ }
197
+ if (!isPlainObject(parsed)) {
198
+ skillIssues.push(`${file} must hold a JSON object.`);
199
+ continue;
200
+ }
201
+ const errors = [];
202
+ const resolved = resolveTokens(parsed, options.tokens, "", errors);
203
+ const { checks, ...rest } = resolved;
204
+ errors.push(..._chunkWOFXZ3XUcjs.validateJsonSchema.call(void 0, rest, domain.profileSchema).map((e) => `${e.path || "(profile)"}: ${e.message}`));
205
+ errors.push(...checkListErrors(checks, domain));
206
+ if (errors.length) {
207
+ skillIssues.push(`${file} is left out: ${errors.join(" ")}`);
208
+ continue;
209
+ }
210
+ profiles.set(domain.id, resolved);
211
+ }
212
+ skills.push({
213
+ info: {
214
+ name,
215
+ description,
216
+ layer: layer.id,
217
+ domains: [...applies],
218
+ profiles: [...profiles.keys()],
219
+ enabled,
220
+ order,
221
+ issues: skillIssues
222
+ },
223
+ layerIndex: winner.index,
224
+ body: body.trim(),
225
+ profiles,
226
+ applies
227
+ });
228
+ }
229
+ skills.sort((a, b) => a.layerIndex - b.layerIndex || a.info.order - b.info.order || a.info.name.localeCompare(b.info.name));
230
+ return { skills, issues };
231
+ }
232
+ async function resolve(domainId) {
233
+ requireDomain(domainId);
234
+ const { skills, issues: loadIssues } = await load();
235
+ const issues = [...loadIssues];
236
+ const profile = {};
237
+ const sources = {};
238
+ const locked = /* @__PURE__ */ new Map();
239
+ const applied = [];
240
+ for (const skill of skills) {
241
+ if (!skill.applies.has(domainId)) continue;
242
+ for (const message of skill.info.issues) issues.push({ skill: skill.info.name, layer: skill.info.layer, message });
243
+ if (!skill.info.enabled) continue;
244
+ applied.push({ name: skill.info.name, description: skill.info.description, layer: skill.info.layer, body: skill.body });
245
+ const own = skill.profiles.get(domainId);
246
+ if (own) mergeProfile(profile, own, layers[skill.layerIndex], skill.info.name, sources, locked, issues);
247
+ }
248
+ return { domain: domainId, skills: applied, profile, sources, issues };
249
+ }
250
+ async function winnerLayer(name) {
251
+ const copies = [];
252
+ for (let i = 0; i < layers.length; i++) if (await layers[i].store.read(name, "SKILL.md") !== void 0) copies.push(i);
253
+ if (!copies.length) return void 0;
254
+ return layers[_nullishCoalesce(copies.find((i) => layers[i].locked), () => ( copies[copies.length - 1]))];
255
+ }
256
+ return {
257
+ layers,
258
+ domains,
259
+ async list(opts = {}) {
260
+ if (opts.domain !== void 0) requireDomain(opts.domain);
261
+ const { skills } = await load();
262
+ return skills.filter((s) => opts.domain === void 0 || s.applies.has(opts.domain)).map((s) => s.info);
263
+ },
264
+ resolve,
265
+ async readFile(name, file) {
266
+ const layer = await winnerLayer(name);
267
+ return layer ? layer.store.read(name, file) : void 0;
268
+ },
269
+ async files(name) {
270
+ const layer = await winnerLayer(name);
271
+ return layer ? [...await layer.store.files(name)] : void 0;
272
+ },
273
+ async check(domainId, state) {
274
+ const domain = requireDomain(domainId);
275
+ const style = await resolve(domainId);
276
+ return { ...runStyleChecks(domain, state, style), style };
277
+ },
278
+ subscribe(listener) {
279
+ const offs = [...layers.map((l) => _optionalChain([l, 'access', _14 => _14.store, 'access', _15 => _15.subscribe, 'optionalCall', _16 => _16(listener)])), _optionalChain([options, 'access', _17 => _17.tokens, 'optionalAccess', _18 => _18.subscribe, 'optionalCall', _19 => _19(listener)])];
280
+ return () => offs.forEach((off) => _optionalChain([off, 'optionalCall', _20 => _20()]));
281
+ }
282
+ };
283
+ }
284
+ function runStyleChecks(domain, state, style) {
285
+ const report = { findings: [], ran: [], skipped: [] };
286
+ const list = Array.isArray(style.profile.checks) ? style.profile.checks : [];
287
+ const layer = style.sources.checks;
288
+ for (const entry of list) {
289
+ const id = typeof entry === "string" ? entry : isPlainObject(entry) && typeof entry.id === "string" ? entry.id : void 0;
290
+ if (!id) continue;
291
+ const check = domain.checks.find((c) => c.id === id);
292
+ if (!check) {
293
+ report.skipped.push({ check: id, reason: `There is no "${id}" check for ${domain.id}; it has ${domain.checks.map((c) => `"${c.id}"`).join(", ")}.` });
294
+ continue;
295
+ }
296
+ const { id: _id, ...options } = isPlainObject(entry) ? entry : { id };
297
+ try {
298
+ for (const f of check.run(state, style.profile, options)) report.findings.push({ ...f, check: id, ...layer ? { layer } : {} });
299
+ report.ran.push(id);
300
+ } catch (error) {
301
+ report.skipped.push({ check: id, reason: `The check could not run: ${error.message}` });
302
+ }
303
+ }
304
+ return report;
305
+ }
306
+ var escapeAttribute = (value) => value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
307
+ function renderStyle(style) {
308
+ if (!style.skills.length) return `No house style applies to ${style.domain}.`;
309
+ const blocks = style.skills.map((s) => {
310
+ const body = s.body.replace(/<(\/?)style_skill/gi, "&lt;$1style_skill");
311
+ return `<style_skill name="${escapeAttribute(s.name)}" layer="${escapeAttribute(s.layer)}">
312
+ ${body}
313
+ </style_skill>`;
314
+ });
315
+ const profile = Object.keys(style.profile).length ? `
316
+
317
+ Merged ${style.domain} profile:
318
+ ${JSON.stringify(style.profile, null, 2)}` : "";
319
+ return `${blocks.join("\n\n")}${profile}`;
320
+ }
321
+
322
+
323
+
324
+
325
+
326
+
327
+
328
+ exports.parseStyleDomains = parseStyleDomains; exports.memoryStyleStore = memoryStyleStore; exports.createStyleResolver = createStyleResolver; exports.runStyleChecks = runStyleChecks; exports.renderStyle = renderStyle;
@@ -1,16 +1,9 @@
1
1
  import {
2
- parseSkillFrontmatter
3
- } from "./chunk-K4BGRCSH.js";
2
+ renderStyle
3
+ } from "./chunk-5UGRMVNE.js";
4
4
  import {
5
5
  readingTime
6
6
  } from "./chunk-EHTX34RE.js";
7
- import {
8
- validateJsonSchema
9
- } from "./chunk-V2ZCJRDM.js";
10
- import {
11
- isUnsafeJsonKey,
12
- stripPrototypeKeys
13
- } from "./chunk-HF5QZS7Z.js";
14
7
 
15
8
  // src/tools/skill-evals.ts
16
9
  function listSkillEvals(files) {
@@ -182,316 +175,6 @@ async function runToolScript(source, script, options = {}) {
182
175
  return finish(true);
183
176
  }
184
177
 
185
- // src/tools/style.ts
186
- var STYLE_SKILL_NAME = /^[a-z0-9][a-z0-9-]{0,63}$/;
187
- var isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
188
- function parseStyleDomains(markdown) {
189
- const match = /^---\r?\n([\s\S]*?)\r?\n---/.exec(markdown);
190
- const line = match?.[1].split(/\r?\n/).find((l) => /^domains:/.test(l));
191
- if (!line) return void 0;
192
- const raw = line.slice("domains:".length).trim().replace(/^\[|\]$/g, "");
193
- return raw.split(",").map((d) => d.trim().replace(/^(['"])(.*)\1$/, "$2")).filter(Boolean);
194
- }
195
- function memoryStyleStore(initial = {}, state = {}) {
196
- const files = /* @__PURE__ */ new Map();
197
- const states = new Map(Object.entries(state));
198
- const listeners = /* @__PURE__ */ new Set();
199
- const notify = () => listeners.forEach((l) => l());
200
- for (const [path, text] of Object.entries(initial)) {
201
- const [dir, ...rest] = path.replace(/\\/g, "/").replace(/^\.?\//, "").split("/");
202
- if (!dir || !rest.length) continue;
203
- if (!files.has(dir)) files.set(dir, /* @__PURE__ */ new Map());
204
- files.get(dir).set(rest.join("/"), text);
205
- }
206
- return {
207
- list: () => [.../* @__PURE__ */ new Set([...files.keys(), ...states.keys()])].sort().map((name) => ({ name, ...states.get(name) })),
208
- read: (name, file) => files.get(name)?.get(file),
209
- files: (name) => [...files.get(name)?.keys() ?? []].sort(),
210
- subscribe(listener) {
211
- listeners.add(listener);
212
- return () => listeners.delete(listener);
213
- },
214
- set(name, skillFiles) {
215
- files.set(name, new Map(Object.entries(skillFiles)));
216
- notify();
217
- },
218
- remove(name) {
219
- files.delete(name);
220
- states.delete(name);
221
- notify();
222
- },
223
- setState(name, next) {
224
- if (next) states.set(name, next);
225
- else states.delete(name);
226
- notify();
227
- }
228
- };
229
- }
230
- function resolveTokens(value, tokens, path, errors) {
231
- if (Array.isArray(value)) return value.map((v, i) => resolveTokens(v, tokens, `${path}[${i}]`, errors));
232
- if (!isPlainObject(value)) return value;
233
- if ("$token" in value) {
234
- const token = value.$token;
235
- const mode = value.mode;
236
- if (typeof token !== "string" || !token || mode !== void 0 && typeof mode !== "string" || Object.keys(value).some((k) => k !== "$token" && k !== "mode")) {
237
- errors.push(`${path || "profile"}: a token reference is { "$token": "<variable name or id>", "mode"?: "<mode>" }.`);
238
- return void 0;
239
- }
240
- if (!tokens) {
241
- errors.push(`${path}: "${token}" is a token reference, but this host has no design tokens to resolve it.`);
242
- return void 0;
243
- }
244
- const resolved = tokens.resolve({ token, ...mode ? { mode } : {} });
245
- if (!resolved.ok) errors.push(`${path}: ${resolved.message}`);
246
- return resolved.ok ? resolved.value : void 0;
247
- }
248
- const out = {};
249
- for (const [k, v] of Object.entries(value)) out[k] = resolveTokens(v, tokens, path ? `${path}.${k}` : k, errors);
250
- return out;
251
- }
252
- function checkListErrors(list, domain) {
253
- if (list === void 0) return [];
254
- if (!Array.isArray(list)) return ['checks: expected a list of check ids or { "id": ..., options } objects.'];
255
- const errors = [];
256
- list.forEach((entry, i) => {
257
- const id = typeof entry === "string" ? entry : isPlainObject(entry) ? entry.id : void 0;
258
- if (typeof id !== "string" || !id) {
259
- errors.push(`checks[${i}]: expected a check id or { "id": ..., options }.`);
260
- return;
261
- }
262
- const check = domain.checks.find((c) => c.id === id);
263
- if (!check || typeof entry === "string") return;
264
- const { id: _id, ...options } = entry;
265
- const schema = check.optionsSchema ?? { type: "object", properties: {}, additionalProperties: false };
266
- for (const e of validateJsonSchema(options, schema)) errors.push(`checks[${i}] (${id})${e.path}: ${e.message}`);
267
- });
268
- return errors;
269
- }
270
- function mergeProfile(target, value, layer, skill, sources, locked, issues, prefix = "") {
271
- for (const [key, next] of Object.entries(value)) {
272
- if (isUnsafeJsonKey(key) || next === void 0) continue;
273
- const path = prefix ? `${prefix}.${key}` : key;
274
- const lockedBy = [...locked.entries()].find(([p]) => p === path || path.startsWith(`${p}.`) || p.startsWith(`${path}.`));
275
- if (isPlainObject(next)) {
276
- if (!isPlainObject(target[key])) {
277
- if (lockedBy && lockedBy[1] !== layer.id && target[key] !== void 0) {
278
- issues.push({ skill, layer: layer.id, message: `"${path}" is locked by the ${lockedBy[1]} layer, so this skill's value is ignored.` });
279
- continue;
280
- }
281
- target[key] = {};
282
- delete sources[path];
283
- }
284
- mergeProfile(target[key], next, layer, skill, sources, locked, issues, path);
285
- continue;
286
- }
287
- if (lockedBy && lockedBy[1] !== layer.id) {
288
- issues.push({ skill, layer: layer.id, message: `"${path}" is locked by the ${lockedBy[1]} layer, so this skill's value is ignored.` });
289
- continue;
290
- }
291
- target[key] = Array.isArray(next) ? structuredClone(next) : next;
292
- for (const p of Object.keys(sources)) if (p.startsWith(`${path}.`)) delete sources[p];
293
- sources[path] = layer.id;
294
- if (layer.locked) locked.set(path, layer.id);
295
- }
296
- }
297
- function createStyleResolver(options) {
298
- const layers = [...options.layers];
299
- const domains = [...options.domains];
300
- const ids = /* @__PURE__ */ new Set();
301
- for (const layer of layers) {
302
- if (ids.has(layer.id)) throw new Error(`Two style layers are called "${layer.id}"; give each layer its own id.`);
303
- ids.add(layer.id);
304
- }
305
- const domainIds = domains.map((d) => d.id);
306
- const requireDomain = (id) => {
307
- const domain = domains.find((d) => d.id === id);
308
- if (!domain) throw new Error(`There is no house-style domain "${id}"; expected one of ${domainIds.map((d) => `"${d}"`).join(", ") || "(none registered)"}.`);
309
- return domain;
310
- };
311
- async function load() {
312
- const issues = [];
313
- const entries = await Promise.all(layers.map(async (layer) => [...await layer.store.list()]));
314
- const names = [...new Set(entries.flat().map((e) => e.name))];
315
- const skills = [];
316
- for (const name of names) {
317
- if (!STYLE_SKILL_NAME.test(name)) {
318
- issues.push({ skill: name, message: `"${name}" is not a valid skill name (lowercase letters, digits and hyphens, at most 64).` });
319
- continue;
320
- }
321
- const copies = [];
322
- for (let i = 0; i < layers.length; i++) {
323
- const text = await layers[i].store.read(name, "SKILL.md");
324
- if (text !== void 0) copies.push({ index: i, text });
325
- }
326
- if (!copies.length) continue;
327
- const lockedCopy = copies.find((c) => layers[c.index].locked);
328
- const winner = lockedCopy ?? copies[copies.length - 1];
329
- const layer = layers[winner.index];
330
- const skillIssues = [];
331
- for (const c of copies) {
332
- if (c.index > winner.index) skillIssues.push(`The ${layers[c.index].id} layer's copy is ignored: the ${layer.id} layer is locked.`);
333
- }
334
- let enabled = true;
335
- let order = 0;
336
- for (let i = 0; i < layers.length; i++) {
337
- const entry = entries[i].find((e) => e.name === name);
338
- if (!entry) continue;
339
- if (i === winner.index && entry.order !== void 0) order = entry.order;
340
- if (entry.enabled === void 0) continue;
341
- if (layer.required && i !== winner.index) {
342
- if (entry.enabled === false) skillIssues.push(`The ${layers[i].id} layer cannot switch it off: the ${layer.id} layer requires it.`);
343
- continue;
344
- }
345
- enabled = entry.enabled;
346
- }
347
- const { description = "", body } = parseSkillFrontmatter(winner.text);
348
- const named = parseStyleDomains(winner.text);
349
- const applies = /* @__PURE__ */ new Set();
350
- for (const d of named ?? domainIds) {
351
- if (domainIds.includes(d)) applies.add(d);
352
- else skillIssues.push(`It names an unknown domain "${d}"; the domains here are ${domainIds.join(", ")}.`);
353
- }
354
- const profiles = /* @__PURE__ */ new Map();
355
- for (const domain of domains) {
356
- const raw = await layer.store.read(name, `${domain.id}.json`);
357
- if (raw === void 0) continue;
358
- const file = `${domain.id}.json`;
359
- if (!applies.has(domain.id)) {
360
- skillIssues.push(`${file} is left out: the skill's domains do not include ${domain.id}.`);
361
- continue;
362
- }
363
- let parsed;
364
- try {
365
- parsed = stripPrototypeKeys(JSON.parse(raw));
366
- } catch (error) {
367
- skillIssues.push(`${file} is not valid JSON: ${error.message}`);
368
- continue;
369
- }
370
- if (!isPlainObject(parsed)) {
371
- skillIssues.push(`${file} must hold a JSON object.`);
372
- continue;
373
- }
374
- const errors = [];
375
- const resolved = resolveTokens(parsed, options.tokens, "", errors);
376
- const { checks, ...rest } = resolved;
377
- errors.push(...validateJsonSchema(rest, domain.profileSchema).map((e) => `${e.path || "(profile)"}: ${e.message}`));
378
- errors.push(...checkListErrors(checks, domain));
379
- if (errors.length) {
380
- skillIssues.push(`${file} is left out: ${errors.join(" ")}`);
381
- continue;
382
- }
383
- profiles.set(domain.id, resolved);
384
- }
385
- skills.push({
386
- info: {
387
- name,
388
- description,
389
- layer: layer.id,
390
- domains: [...applies],
391
- profiles: [...profiles.keys()],
392
- enabled,
393
- order,
394
- issues: skillIssues
395
- },
396
- layerIndex: winner.index,
397
- body: body.trim(),
398
- profiles,
399
- applies
400
- });
401
- }
402
- skills.sort((a, b) => a.layerIndex - b.layerIndex || a.info.order - b.info.order || a.info.name.localeCompare(b.info.name));
403
- return { skills, issues };
404
- }
405
- async function resolve2(domainId) {
406
- requireDomain(domainId);
407
- const { skills, issues: loadIssues } = await load();
408
- const issues = [...loadIssues];
409
- const profile = {};
410
- const sources = {};
411
- const locked = /* @__PURE__ */ new Map();
412
- const applied = [];
413
- for (const skill of skills) {
414
- if (!skill.applies.has(domainId)) continue;
415
- for (const message of skill.info.issues) issues.push({ skill: skill.info.name, layer: skill.info.layer, message });
416
- if (!skill.info.enabled) continue;
417
- applied.push({ name: skill.info.name, description: skill.info.description, layer: skill.info.layer, body: skill.body });
418
- const own = skill.profiles.get(domainId);
419
- if (own) mergeProfile(profile, own, layers[skill.layerIndex], skill.info.name, sources, locked, issues);
420
- }
421
- return { domain: domainId, skills: applied, profile, sources, issues };
422
- }
423
- async function winnerLayer(name) {
424
- const copies = [];
425
- for (let i = 0; i < layers.length; i++) if (await layers[i].store.read(name, "SKILL.md") !== void 0) copies.push(i);
426
- if (!copies.length) return void 0;
427
- return layers[copies.find((i) => layers[i].locked) ?? copies[copies.length - 1]];
428
- }
429
- return {
430
- layers,
431
- domains,
432
- async list(opts = {}) {
433
- if (opts.domain !== void 0) requireDomain(opts.domain);
434
- const { skills } = await load();
435
- return skills.filter((s) => opts.domain === void 0 || s.applies.has(opts.domain)).map((s) => s.info);
436
- },
437
- resolve: resolve2,
438
- async readFile(name, file) {
439
- const layer = await winnerLayer(name);
440
- return layer ? layer.store.read(name, file) : void 0;
441
- },
442
- async files(name) {
443
- const layer = await winnerLayer(name);
444
- return layer ? [...await layer.store.files(name)] : void 0;
445
- },
446
- async check(domainId, state) {
447
- const domain = requireDomain(domainId);
448
- const style = await resolve2(domainId);
449
- return { ...runStyleChecks(domain, state, style), style };
450
- },
451
- subscribe(listener) {
452
- const offs = [...layers.map((l) => l.store.subscribe?.(listener)), options.tokens?.subscribe?.(listener)];
453
- return () => offs.forEach((off) => off?.());
454
- }
455
- };
456
- }
457
- function runStyleChecks(domain, state, style) {
458
- const report = { findings: [], ran: [], skipped: [] };
459
- const list = Array.isArray(style.profile.checks) ? style.profile.checks : [];
460
- const layer = style.sources.checks;
461
- for (const entry of list) {
462
- const id = typeof entry === "string" ? entry : isPlainObject(entry) && typeof entry.id === "string" ? entry.id : void 0;
463
- if (!id) continue;
464
- const check = domain.checks.find((c) => c.id === id);
465
- if (!check) {
466
- report.skipped.push({ check: id, reason: `There is no "${id}" check for ${domain.id}; it has ${domain.checks.map((c) => `"${c.id}"`).join(", ")}.` });
467
- continue;
468
- }
469
- const { id: _id, ...options } = isPlainObject(entry) ? entry : { id };
470
- try {
471
- for (const f of check.run(state, style.profile, options)) report.findings.push({ ...f, check: id, ...layer ? { layer } : {} });
472
- report.ran.push(id);
473
- } catch (error) {
474
- report.skipped.push({ check: id, reason: `The check could not run: ${error.message}` });
475
- }
476
- }
477
- return report;
478
- }
479
- var escapeAttribute = (value) => value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
480
- function renderStyle(style) {
481
- if (!style.skills.length) return `No house style applies to ${style.domain}.`;
482
- const blocks = style.skills.map((s) => {
483
- const body = s.body.replace(/<(\/?)style_skill/gi, "&lt;$1style_skill");
484
- return `<style_skill name="${escapeAttribute(s.name)}" layer="${escapeAttribute(s.layer)}">
485
- ${body}
486
- </style_skill>`;
487
- });
488
- const profile = Object.keys(style.profile).length ? `
489
-
490
- Merged ${style.domain} profile:
491
- ${JSON.stringify(style.profile, null, 2)}` : "";
492
- return `${blocks.join("\n\n")}${profile}`;
493
- }
494
-
495
178
  // src/tools/style-tools.ts
496
179
  var READ = { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false };
497
180
  function createStyleToolProvider(styles, options = {}) {
@@ -773,11 +456,6 @@ export {
773
456
  toolResultValue,
774
457
  toolResultError,
775
458
  runToolScript,
776
- parseStyleDomains,
777
- memoryStyleStore,
778
- createStyleResolver,
779
- runStyleChecks,
780
- renderStyle,
781
459
  createStyleToolProvider,
782
460
  createOriginMiddleware,
783
461
  ConformanceIds,