@ikijs/mcp 0.9.0 → 0.10.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/README.md CHANGED
@@ -33,12 +33,13 @@ Composes a directory of AI-generated part PNGs into canvas-aligned, role-named l
33
33
 
34
34
  - **`partsDir`** — directory of the source part PNGs (resolved against the server's working directory). Required sources: `face.png`, `eyewhite.png` (split into the sclera + lash layers), `iris.png`, `mouth.png`, `brow.png`, `hair_front.png`. Optional: `hair_back.png`, `body.png`, `mouth_open.png`, `nose.png` — a parts dir without them still composes, minus those roles. The nose is what the auto-rig leads the head turn with (without a `nose` layer nothing on the face slides), and a face supplied alongside it must be drawn without one, or the painted nose stays on the face plate while the real one moves.
35
35
  - **`outDir`** — an existing directory (resolved against the working directory, confined to it exactly like `auto_rig_from_layers`'s `outputPath`; the tool never creates directories) to write the role layer PNGs and a flattened `preview.png` into. Reusing a directory across runs is intentional — a role this run skips (e.g. composing a head-only set that omits `body`) has its stale `<role>.png` from an earlier run deleted, so the directory always holds exactly this run's roles.
36
- - **`layout`** — optional per-role override merged over the built-in defaults, e.g. `{ "eye_L": { "cx": 660 } }`, so a character can be retuned by re-running compose rather than editing a script. Each of `cx`, `cy`, `w`, `h` is optional; `w` and `h` must be integers in 1..1100 (the canvas size). Omitting `h` keeps the part's own aspect ratio, which is what every role wants until one does not: a generated eyewhite often comes back flatter than its reference, and stretching a flat white lens costs nothing where regenerating it is billed and unreliable. Set `h` on a sclera and its lash together — they share one frame, and the blink fold tears if they diverge.
37
- - **Result** — on success the text is the written layer paths (one per line) followed by the same geometry report `measure_layers` returns; `structuredContent` is `{ ok: true, outDir, layers, skipped, preview, measure }`. Invalid input (a missing required part, an unknown `layout` role, an out-of-range `w` or `h`, a part that lands entirely off-canvas, a non-existent `outDir`) returns `{ ok: false, error }` with `INVALID: …` text.
36
+ - **`layout`** — optional per-role override merged over the built-in defaults, e.g. `{ "eye_L": { "cx": 660 } }`, so a character can be retuned by re-running compose rather than editing a script. Each of `cx`, `cy`, `w`, `h` is optional; `w` and `h` must be integers in 1..1100 (the canvas size). Omitting `h` keeps the part's own aspect ratio, which is what every role wants until one does not: a generated eyewhite often comes back flatter than its reference, and stretching a flat white lens costs nothing where regenerating it is billed and unreliable. Set `h` on a sclera and its lash together — they share one frame, and the blink fold tears if they diverge, so a pair set to land on different frames is rejected.
37
+ - **`mirrorParts`** — optional list of part files to flip left-right as they are read, e.g. `["eyewhite.png"]`. Which way a part faces is fixed by the layout, and "left eye" cannot pin it down — it reads as the viewer's left or the character's — so in image terms: `eyewhite.png` is read as the eye on the screen left, lash wing (outer corner) at the image's left end and lash-free tear duct at its right; `brow.png` as the brow on the screen right, thick head at the image's left end and tail at its right. A part drawn the other way round is fixed here for free instead of regenerated. It flips the source, so every role cut from it flips together and an eye's sclera and lash stay in one frame.
38
+ - **Result** — on success the text is the written layer paths (one per line) followed by the same geometry report `measure_layers` returns; `structuredContent` is `{ ok: true, outDir, layers, skipped, preview, measure }`. Invalid input (a missing required part, an unknown `layout` role, an out-of-range `w` or `h`, a lash placed on a different frame from its sclera's, a `mirrorParts` entry that is not a part file, a part that lands entirely off-canvas, a non-existent `outDir`) returns `{ ok: false, error }` with `INVALID: …` text.
38
39
 
39
40
  ### `measure_layers`
40
41
 
41
- Reports the same read-only geometry checks `compose_layers_from_parts` returns inline (iris size/offset relative to the sclera, eye aspect, cropped or flat-cut edges, missing optional roles), over an already-composed layers directory — for re-checking a layers dir without recomposing it.
42
+ Reports the same read-only geometry checks `compose_layers_from_parts` returns inline (iris size/offset relative to the sclera, eye aspect, an eye drawn facing the other way, cropped or flat-cut edges, missing optional roles), over an already-composed layers directory — for re-checking a layers dir without recomposing it.
42
43
 
43
44
  - **`layersDir`** — directory of role-named layer PNGs (resolved against the working directory).
44
45
  - **Result** — the text is the report: a per-layer size/bbox-centre/mass-centre/margins table, then the checks that fired. `structuredContent` is `{ ok: true, layersDir, layers, empty, warnings, passed }`. A missing or non-directory `layersDir` returns `{ ok: false, error }` with `INVALID: …` text.
@@ -1040,7 +1040,8 @@ var EYE_ASPECT_MIN = 0.5;
1040
1040
  var IRIS_OFFSET_MAX_X = 3;
1041
1041
  var IRIS_OFFSET_MAX_Y = 10;
1042
1042
  var EDGE_SOLID_MAX = 0.5;
1043
- var LASH_CENTRE_TOL_FRAC = 0.03;
1043
+ var LASH_STRAY_TOL_FRAC = 0.02;
1044
+ var EYE_FACING_TOL_FRAC = 0.02;
1044
1045
  var EDGE_RUN_MIN_PX = 40;
1045
1046
  var FLAT_CUT_MAX_FRAC = 0.1;
1046
1047
  var FLAT_CUT_MIN_PX = 40;
@@ -1121,6 +1122,21 @@ async function layerStats(filePath) {
1121
1122
  };
1122
1123
  }
1123
1124
  var pct = (v) => `${(v * 100).toFixed(0)}%`;
1125
+ async function lashOffSclera(eyePath, lashPath) {
1126
+ const eye = await decodePng(eyePath);
1127
+ const lash = await decodePng(lashPath);
1128
+ let opaque = 0;
1129
+ let off = 0;
1130
+ for (let y = 0; y < lash.height; y++) {
1131
+ for (let x = 0; x < lash.width; x++) {
1132
+ if (lash.rgba[(y * lash.width + x) * 4 + 3] < 128) continue;
1133
+ opaque++;
1134
+ const onEye = x < eye.width && y < eye.height;
1135
+ if (!onEye || eye.rgba[(y * eye.width + x) * 4 + 3] <= 8) off++;
1136
+ }
1137
+ }
1138
+ return { opaque, off };
1139
+ }
1124
1140
  async function measureDir(absDir) {
1125
1141
  const files = fs2.readdirSync(absDir).filter((f) => f.endsWith(".png") && f !== "preview.png").sort();
1126
1142
  if (files.length === 0) {
@@ -1202,12 +1218,28 @@ async function measureDir(absDir) {
1202
1218
  }
1203
1219
  }
1204
1220
  if (lash) {
1205
- const dx = lash.bboxCx - eye.bboxCx;
1221
+ const { opaque, off } = await lashOffSclera(
1222
+ path4.join(absDir, `eye_${side}.png`),
1223
+ path4.join(absDir, `lash_${side}.png`)
1224
+ );
1206
1225
  const dTop = lash.marginTop - eye.marginTop;
1207
- if (Math.abs(dx) > eye.w * LASH_CENTRE_TOL_FRAC || Math.abs(dTop) > 0.5) {
1226
+ const faults = [
1227
+ ...off > opaque * LASH_STRAY_TOL_FRAC ? [`${off} of its ${opaque} opaque px lie off eye_${side}'s sclera`] : [],
1228
+ ...Math.abs(dTop) > 0.5 ? [`its top edge is ${dTop.toFixed(1)} px off the sclera's`] : []
1229
+ ];
1230
+ if (faults.length > 0) {
1208
1231
  warnings.push(
1209
- `lash_${side}: centre is ${dx.toFixed(1)} px and top edge ${dTop.toFixed(1)} px off eye_${side}. They are split from one source and MUST share cx/cy/w in layout, or the blink fold tears. Retune to match.`
1232
+ `lash_${side}: ${faults.join(" and ")} \u2014 they are split from one source and MUST share cx/cy/w/h in layout, or the blink fold tears. Retune to match.`
1210
1233
  );
1234
+ } else {
1235
+ const bareLeft = lash.marginLeft - eye.marginLeft;
1236
+ const bareRight = lash.marginRight - eye.marginRight;
1237
+ const [bareOuter, bareInner] = side === "R" ? [bareLeft, bareRight] : [bareRight, bareLeft];
1238
+ if ((bareOuter - bareInner) / eye.w > EYE_FACING_TOL_FRAC) {
1239
+ warnings.push(
1240
+ `eye_${side}: its lash stops ${pct(bareOuter / eye.w)} of the eye width short of the outer corner and ${pct(bareInner / eye.w)} short of the nose side \u2014 the eyewhite is drawn facing the other way, lash wing at the inner corner. The composer reads eyewhite.png as the eye on the screen LEFT: lash wing at the image's left end, lash-free tear duct at its right. Free fix: compose again with mirrorParts: ["eyewhite.png"] \u2014 no regeneration.`
1241
+ );
1242
+ }
1211
1243
  }
1212
1244
  }
1213
1245
  }
@@ -1342,6 +1374,12 @@ var ORDER = [
1342
1374
  "brow_R",
1343
1375
  "hair_front"
1344
1376
  ];
1377
+ var PART_FILES = [
1378
+ ...new Set(
1379
+ Object.values(DEFAULT_LAYOUT).map((cfg) => cfg.src).filter((src) => !src.startsWith("eyewhite_"))
1380
+ ),
1381
+ EYEWHITE_SRC
1382
+ ];
1345
1383
  function resolveLayout(overrides) {
1346
1384
  const resolved = { ...DEFAULT_LAYOUT };
1347
1385
  if (overrides === void 0) return resolved;
@@ -1377,6 +1415,18 @@ function resolveLayout(overrides) {
1377
1415
  }
1378
1416
  return resolved;
1379
1417
  }
1418
+ function resolveMirrorParts(names) {
1419
+ const mirrored = /* @__PURE__ */ new Set();
1420
+ for (const [i, name] of (names ?? []).entries()) {
1421
+ if (!PART_FILES.includes(name)) {
1422
+ throw new AutoRigInputError(
1423
+ `mirrorParts[${i}]: unknown part ${JSON.stringify(name)} \u2014 expected one of ${PART_FILES.join(", ")}`
1424
+ );
1425
+ }
1426
+ mirrored.add(name);
1427
+ }
1428
+ return mirrored;
1429
+ }
1380
1430
  function keyWhiteToAlpha(rgba) {
1381
1431
  const keyed = Buffer.from(rgba);
1382
1432
  for (let i = 0; i < keyed.length; i += 4) {
@@ -1386,7 +1436,7 @@ function keyWhiteToAlpha(rgba) {
1386
1436
  }
1387
1437
  return keyed;
1388
1438
  }
1389
- async function partBuffer(role, cfg, partsDir, inMemory) {
1439
+ async function partBuffer(role, cfg, partsDir, inMemory, flipSource) {
1390
1440
  let img;
1391
1441
  if (inMemory !== void 0) {
1392
1442
  img = sharp2(inMemory);
@@ -1404,7 +1454,7 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
1404
1454
  });
1405
1455
  }
1406
1456
  if (!cfg.noTrim) img = img.trim({ threshold: 12 });
1407
- if (cfg.mirror) img = img.flop();
1457
+ if ((cfg.mirror ?? false) !== flipSource) img = img.flop();
1408
1458
  const trimmed = await img.png().toBuffer({ resolveWithObject: true });
1409
1459
  if (cfg.h === void 0) {
1410
1460
  const scaledH = Math.round(
@@ -1501,28 +1551,47 @@ async function composeLayersFromParts(input) {
1501
1551
  );
1502
1552
  }
1503
1553
  const layout = resolveLayout(input.layout);
1554
+ const mirrored = resolveMirrorParts(input.mirrorParts);
1504
1555
  const split = await prepEyeSplit(partsDir);
1505
1556
  const splitSources = /* @__PURE__ */ new Map([
1506
1557
  ["eyewhite_sclera.png", split.sclera],
1507
1558
  ["eyewhite_lash.png", split.lash]
1508
1559
  ]);
1509
- const layers = [];
1560
+ const placed = [];
1510
1561
  const skipped = [];
1511
- const preview = [];
1512
1562
  for (const role of ORDER) {
1513
1563
  const cfg = layout[role];
1564
+ const inMemory = splitSources.get(cfg.src);
1514
1565
  const part = await partBuffer(
1515
1566
  role,
1516
1567
  cfg,
1517
1568
  partsDir,
1518
- splitSources.get(cfg.src)
1569
+ inMemory,
1570
+ // The split halves are cut from eyewhite.png, the file a caller names.
1571
+ mirrored.has(inMemory === void 0 ? cfg.src : EYEWHITE_SRC)
1519
1572
  );
1520
1573
  if (part === null) {
1521
1574
  skipped.push(role);
1522
- fs3.rmSync(path5.join(outDir, `${role}.png`), { force: true });
1523
1575
  continue;
1524
1576
  }
1525
1577
  const { left, top } = placement(role, cfg, part.w, part.h);
1578
+ if (role === "lash_L" || role === "lash_R") {
1579
+ const eyeRole = role === "lash_L" ? "eye_L" : "eye_R";
1580
+ const eye = placed.find((p) => p.role === eyeRole);
1581
+ if (eye !== void 0 && (eye.left !== left || eye.top !== top || eye.part.w !== part.w || eye.part.h !== part.h)) {
1582
+ throw new AutoRigInputError(
1583
+ `layout.${role} places the lash at ${part.w}x${part.h} (${left},${top}), but layout.${eyeRole} places its sclera at ${eye.part.w}x${eye.part.h} (${eye.left},${eye.top}) \u2014 they are cut from one eyewhite into one frame, and the blink fold tears where they part. Set cx/cy/w/h the same on both.`
1584
+ );
1585
+ }
1586
+ }
1587
+ placed.push({ role, part, left, top });
1588
+ }
1589
+ for (const role of skipped) {
1590
+ fs3.rmSync(path5.join(outDir, `${role}.png`), { force: true });
1591
+ }
1592
+ const layers = [];
1593
+ const preview = [];
1594
+ for (const { role, part, left, top } of placed) {
1526
1595
  const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
1527
1596
  const outPath = path5.join(outDir, `${role}.png`);
1528
1597
  writeFileAtomic(outPath, layer);
@@ -1565,7 +1634,7 @@ async function composeLayersFromParts(input) {
1565
1634
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1566
1635
  import { z } from "zod";
1567
1636
  function createIkiMcpServer() {
1568
- const server = new McpServer({ name: "iki", version: "0.9.0" });
1637
+ const server = new McpServer({ name: "iki", version: "0.10.0" });
1569
1638
  server.registerTool(
1570
1639
  "validate_iki",
1571
1640
  {
@@ -1692,7 +1761,10 @@ function createIkiMcpServer() {
1692
1761
  h: z.number()
1693
1762
  }).partial()
1694
1763
  ).optional().describe(
1695
- "Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, nose, mouth, mouth_open, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` and `h` are integers in 1..1100; omit `h` to keep the part's own aspect, set it to stretch (set it on a sclera and its lash together, or the blink fold tears)."
1764
+ "Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, nose, mouth, mouth_open, eye_L, eye_R, iris_L, iris_R, lash_L, lash_R, brow_L, brow_R, hair_front \u2014 e.g. `{ eye_L: { cx: 660 } }`. `w` and `h` are integers in 1..1100; omit `h` to keep the part's own aspect, set it to stretch. eye_L/lash_L and eye_R/lash_R are cut from one eyewhite into one frame, so each pair must land on the same frame \u2014 a pair set to land apart is rejected."
1765
+ ),
1766
+ mirrorParts: z.array(z.string()).optional().describe(
1767
+ "Part files to flip left-right as they are read, e.g. `[\"eyewhite.png\"]`. The composer reads eyewhite.png as the eye on the screen LEFT (lash wing at the image's left end, lash-free tear duct at its right) and brow.png as the brow on the screen RIGHT (thick head at the image's left end, tail at its right); a part drawn the other way round is fixed here for free. It flips the source, so every role cut from it flips together and an eye's sclera and lash stay in one frame."
1696
1768
  )
1697
1769
  }
1698
1770
  },
@@ -1794,4 +1866,4 @@ export {
1794
1866
  composeLayersFromParts,
1795
1867
  createIkiMcpServer
1796
1868
  };
1797
- //# sourceMappingURL=chunk-R6LYJVYC.mjs.map
1869
+ //# sourceMappingURL=chunk-OCIICRUA.mjs.map