@ikijs/mcp 0.12.1 → 0.13.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/dist/cli.js CHANGED
@@ -570,6 +570,7 @@ async function autoRigFromLayers(input) {
570
570
  var import_node_fs3 = __toESM(require("fs"));
571
571
  var import_node_path5 = __toESM(require("path"));
572
572
  var import_sharp2 = __toESM(require("sharp"));
573
+ var import_editor5 = require("@ikijs/editor");
573
574
 
574
575
  // src/measure.ts
575
576
  var import_node_fs2 = __toESM(require("fs"));
@@ -1218,6 +1219,8 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
1218
1219
  var EYEWHITE_SRC = "eyewhite.png";
1219
1220
  var EYE_LASH_LUMA = 120;
1220
1221
  var LASH_KEEP_FRACTION = 0.5;
1222
+ var TRIM_THRESHOLD = 12;
1223
+ var STRAY_SPECK_FRACTION = 0.01;
1221
1224
  var NOSE_TIP_AT = 0.86;
1222
1225
  var DEFAULT_LAYOUT = {
1223
1226
  // Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
@@ -1227,6 +1230,19 @@ var DEFAULT_LAYOUT = {
1227
1230
  // floating head. It rides its own `bodyDeformer`, which only breathes.
1228
1231
  body: { src: "body.png", cx: 550, cy: 1017, w: 840, optional: true },
1229
1232
  face: { src: "face.png", cx: 550, cy: 475, w: 400 },
1233
+ // The cheek blush, one part for both cheeks: blush_R (screen left) takes it
1234
+ // as drawn and blush_L mirrors it, like the eyewhite. It draws over the face
1235
+ // and under the nose. Its box sits below each eye, centred just outside the
1236
+ // eye's centre and above the nose tip. Tuned on the hero's face at its
1237
+ // default placement, which narrows fast below the eye row: a 96-wide box at
1238
+ // row 552 ran up to 24 px past the cheek's drawn outline (inside the ears),
1239
+ // and this one, for a 2:1 blush, stays inside it by 1 px at its lower outer
1240
+ // corner. OPTIONAL, and decoration: a parts dir without it composes, and
1241
+ // nothing reports it missing.
1242
+ blush_L: { src: "blush.png", cx: 662, cy: 530, w: 56, optional: true, mirror: true },
1243
+ // prettier-ignore
1244
+ blush_R: { src: "blush.png", cx: 438, cy: 530, w: 56, optional: true, mirror: false },
1245
+ // prettier-ignore
1230
1246
  // The nose, drawn on its own (the face is drawn without one): the auto-rig
1231
1247
  // leads the head turn with it and keys the other features' slide on its
1232
1248
  // presence, so without it the features stay on the face plate. `w` is the
@@ -1275,6 +1291,8 @@ var ORDER = [
1275
1291
  "hair_back",
1276
1292
  "body",
1277
1293
  "face",
1294
+ "blush_L",
1295
+ "blush_R",
1278
1296
  "nose",
1279
1297
  "mouth",
1280
1298
  "mouth_open",
@@ -1391,7 +1409,15 @@ async function partBuffer(role, cfg, partsDir, inMemory, flipSource) {
1391
1409
  raw: { width: png.width, height: png.height, channels: 4 }
1392
1410
  });
1393
1411
  }
1394
- if (!cfg.noTrim) img = img.trim({ threshold: 12 });
1412
+ if (!cfg.noTrim) {
1413
+ const kept = await img.trim({ threshold: TRIM_THRESHOLD }).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
1414
+ const { width: keptW, height: keptH } = kept.info;
1415
+ img = (0, import_sharp2.default)(kept.data, {
1416
+ raw: { width: keptW, height: keptH, channels: 4 }
1417
+ });
1418
+ const box = boxWithoutStraySpecks(kept.data, keptW, keptH);
1419
+ if (box !== null) img = img.extract(box);
1420
+ }
1395
1421
  if ((cfg.mirror ?? false) !== flipSource) img = img.flop();
1396
1422
  const trimmed = await img.png().toBuffer({ resolveWithObject: true });
1397
1423
  const { width: trimmedW, height: trimmedH } = trimmed.info;
@@ -1458,6 +1484,152 @@ function blankCanvas() {
1458
1484
  }
1459
1485
  });
1460
1486
  }
1487
+ function luma(rgba, i) {
1488
+ return 0.299 * rgba[i] + 0.587 * rgba[i + 1] + 0.114 * rgba[i + 2];
1489
+ }
1490
+ function boxWithoutStraySpecks(rgba, W, H) {
1491
+ const n = W * H;
1492
+ let kept = 0;
1493
+ for (let p = 0; p < n; p++) if (rgba[p * 4 + 3] > TRIM_THRESHOLD) kept++;
1494
+ const visited = new Uint8Array(n);
1495
+ let stack = new Int32Array(1024);
1496
+ const drawing = [W, H, -1, -1];
1497
+ const strays = [W, H, -1, -1];
1498
+ let anyOpaque = false;
1499
+ for (let start = 0; start < n; start++) {
1500
+ if (visited[start] || rgba[start * 4 + 3] <= TRIM_THRESHOLD) continue;
1501
+ let setLeft = W;
1502
+ let setTop = H;
1503
+ let setRight = -1;
1504
+ let setBottom = -1;
1505
+ let size = 0;
1506
+ let opaque = false;
1507
+ let depth = 0;
1508
+ visited[start] = 1;
1509
+ stack[depth++] = start;
1510
+ while (depth > 0) {
1511
+ const p = stack[--depth];
1512
+ size++;
1513
+ if (rgba[p * 4 + 3] >= import_editor5.ALPHA_OPAQUE) opaque = true;
1514
+ const px = p % W;
1515
+ const py = (p - px) / W;
1516
+ setLeft = Math.min(setLeft, px);
1517
+ setTop = Math.min(setTop, py);
1518
+ setRight = Math.max(setRight, px);
1519
+ setBottom = Math.max(setBottom, py);
1520
+ for (let y = Math.max(0, py - 1); y <= Math.min(H - 1, py + 1); y++) {
1521
+ for (let x = Math.max(0, px - 1); x <= Math.min(W - 1, px + 1); x++) {
1522
+ const q = y * W + x;
1523
+ if (!visited[q] && rgba[q * 4 + 3] > TRIM_THRESHOLD) {
1524
+ visited[q] = 1;
1525
+ if (depth === stack.length) {
1526
+ const grown = new Int32Array(Math.min(n, stack.length * 2));
1527
+ grown.set(stack);
1528
+ stack = grown;
1529
+ }
1530
+ stack[depth++] = q;
1531
+ }
1532
+ }
1533
+ }
1534
+ }
1535
+ anyOpaque ||= opaque;
1536
+ const into = !opaque && size < kept * STRAY_SPECK_FRACTION ? strays : drawing;
1537
+ into[0] = Math.min(into[0], setLeft);
1538
+ into[1] = Math.min(into[1], setTop);
1539
+ into[2] = Math.max(into[2], setRight);
1540
+ into[3] = Math.max(into[3], setBottom);
1541
+ }
1542
+ if (!anyOpaque || strays[2] < 0) return null;
1543
+ const [left, top, right, bottom] = drawing;
1544
+ if (strays[0] >= left && strays[1] >= top && strays[2] <= right && strays[3] <= bottom)
1545
+ return null;
1546
+ return { left, top, width: right - left + 1, height: bottom - top + 1 };
1547
+ }
1548
+ function detachedAboveWhite(rgba, W, H) {
1549
+ const n = W * H;
1550
+ const ink = new Uint8Array(n);
1551
+ for (let p = 0; p < n; p++) {
1552
+ const alpha = rgba[p * 4 + 3];
1553
+ if (alpha > 0 && luma(rgba, p * 4) >= EYE_LASH_LUMA)
1554
+ ink[p] = alpha >= import_editor5.ALPHA_OPAQUE ? 2 : 1;
1555
+ }
1556
+ const label = new Int32Array(n).fill(-1);
1557
+ const stack = new Int32Array(n);
1558
+ const near = (p, visit) => {
1559
+ const px = p % W;
1560
+ const py = (p - px) / W;
1561
+ for (let y = Math.max(0, py - 1); y <= Math.min(H - 1, py + 1); y++) {
1562
+ for (let x = Math.max(0, px - 1); x <= Math.min(W - 1, px + 1); x++) {
1563
+ visit(y * W + x);
1564
+ }
1565
+ }
1566
+ };
1567
+ const sizes = [];
1568
+ const fill = (start) => {
1569
+ const id = sizes.length;
1570
+ let size = 0;
1571
+ let depth = 0;
1572
+ label[start] = id;
1573
+ stack[depth++] = start;
1574
+ while (depth > 0) {
1575
+ const p = stack[--depth];
1576
+ size++;
1577
+ near(p, (q) => {
1578
+ if (ink[q] === ink[start] && label[q] === -1) {
1579
+ label[q] = id;
1580
+ stack[depth++] = q;
1581
+ }
1582
+ });
1583
+ }
1584
+ sizes.push(size);
1585
+ };
1586
+ for (let p = 0; p < n; p++) if (ink[p] === 2 && label[p] === -1) fill(p);
1587
+ const paintedSets = sizes.length;
1588
+ let head = 0;
1589
+ let tail = 0;
1590
+ for (let p = 0; p < n; p++) if (ink[p] === 2) stack[tail++] = p;
1591
+ while (head < tail) {
1592
+ const p = stack[head++];
1593
+ near(p, (q) => {
1594
+ if (ink[q] === 1 && label[q] === -1 && rgba[q * 4 + 3] <= rgba[p * 4 + 3]) {
1595
+ label[q] = label[p];
1596
+ stack[tail++] = q;
1597
+ }
1598
+ });
1599
+ }
1600
+ for (let p = 0; p < n; p++) if (ink[p] === 1 && label[p] === -1) fill(p);
1601
+ const detached = new Uint8Array(n);
1602
+ if (paintedSets === 0 || sizes.length < 2) return detached;
1603
+ let white = 0;
1604
+ for (let id = 1; id < paintedSets; id++) {
1605
+ if (sizes[id] > sizes[white]) white = id;
1606
+ }
1607
+ const own = (p) => ink[p] === 2 || label[p] >= paintedSets;
1608
+ const whiteTop = new Int32Array(W).fill(-1);
1609
+ for (let p = 0; p < n; p++) {
1610
+ const x = p % W;
1611
+ if (label[p] === white && own(p) && whiteTop[x] === -1)
1612
+ whiteTop[x] = (p - x) / W;
1613
+ }
1614
+ let first = 0;
1615
+ while (whiteTop[first] === -1) first++;
1616
+ let last = W - 1;
1617
+ while (whiteTop[last] === -1) last--;
1618
+ whiteTop.fill(whiteTop[first], 0, first);
1619
+ whiteTop.fill(whiteTop[last], last + 1);
1620
+ const above = new Uint8Array(sizes.length).fill(1);
1621
+ for (let p = 0; p < n; p++) {
1622
+ const id = label[p];
1623
+ if (id === -1 || id === white || !own(p)) continue;
1624
+ const x = p % W;
1625
+ if ((p - x) / W >= whiteTop[x]) above[id] = 0;
1626
+ }
1627
+ for (let p = 0; p < n; p++) {
1628
+ const id = label[p];
1629
+ if (id !== -1 && id !== white && above[id]) detached[p] = 1;
1630
+ }
1631
+ return detached;
1632
+ }
1461
1633
  async function prepEyeSplit(partsDir) {
1462
1634
  const srcPath = import_node_path5.default.join(partsDir, EYEWHITE_SRC);
1463
1635
  if (!import_node_fs3.default.existsSync(srcPath)) {
@@ -1482,14 +1654,15 @@ async function prepEyeSplit(partsDir) {
1482
1654
  throw new AutoRigInputError(`eyewhite is fully transparent: ${srcPath}`);
1483
1655
  }
1484
1656
  const lashCutoffY = minY + LASH_KEEP_FRACTION * (maxY - minY + 1);
1657
+ const detached = detachedAboveWhite(data, W, H);
1485
1658
  const sclera = Buffer.from(data);
1486
1659
  const lash = Buffer.from(data);
1487
1660
  for (let y = 0; y < H; y++) {
1488
1661
  for (let x = 0; x < W; x++) {
1489
1662
  const i = (y * W + x) * 4;
1490
- const lum = 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
1491
- const isDark = data[i + 3] > 0 && lum < EYE_LASH_LUMA;
1663
+ const isDark = data[i + 3] > 0 && luma(data, i) < EYE_LASH_LUMA;
1492
1664
  if (isDark) sclera[i] = sclera[i + 1] = sclera[i + 2] = 255;
1665
+ if (detached[y * W + x]) sclera[i + 3] = 0;
1493
1666
  if (!(isDark && y <= lashCutoffY)) lash[i + 3] = 0;
1494
1667
  }
1495
1668
  }
@@ -1605,7 +1778,7 @@ async function composeLayersFromParts(input) {
1605
1778
 
1606
1779
  // src/server.ts
1607
1780
  function createIkiMcpServer() {
1608
- const server = new import_mcp.McpServer({ name: "iki", version: "0.12.1" });
1781
+ const server = new import_mcp.McpServer({ name: "iki", version: "0.13.1" });
1609
1782
  server.registerTool(
1610
1783
  "validate_iki",
1611
1784
  {
@@ -1725,7 +1898,7 @@ function createIkiMcpServer() {
1725
1898
  server.registerTool(
1726
1899
  "compose_layers_from_parts",
1727
1900
  {
1728
- description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split, alpha-trim/white-key, and placement pipeline the character-generation skill needs), with the same geometry report measure_layers returns standalone included inline. face, mouth, eyewhite, iris, brow, hair_front are required; hair_back, body, mouth_open, nose are optional \u2014 without a nose layer the head still turns on the rig's Live2D profile, but it has no nose to lead the turn, and `auto_rig_from_layers` fits no turn to the art (`turnTargets` is inert, no `turn` report). A nose whose dense core is a speck \u2014 under a quarter of its trimmed part's width or height, such as a lone nostril mark or highlight \u2014 is sized and placed whole, and the inline report warns of it.",
1901
+ description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split, alpha-trim/white-key, and placement pipeline the character-generation skill needs), with the same geometry report measure_layers returns standalone included inline. face, mouth, eyewhite, iris, brow, hair_front are required; hair_back, body, mouth_open, blush (one blush.png makes the blush_L/blush_R pair) and nose are optional \u2014 without a nose layer the head still turns on the rig's Live2D profile, but it has no nose to lead the turn, and `auto_rig_from_layers` fits no turn to the art (`turnTargets` is inert, no `turn` report). A nose whose dense core is a speck \u2014 under a quarter of its trimmed part's width or height, such as a lone nostril mark or highlight \u2014 is sized and placed whole, and the inline report warns of it. A light mark drawn detached above the eye white, such as a double-eyelid crease, is dropped from the sclera (the iris's clip); dark strokes there stay lash ink.",
1729
1902
  inputSchema: {
1730
1903
  partsDir: import_zod.z.string().describe(
1731
1904
  "Directory of the source part PNGs (resolved against cwd)."
@@ -1742,10 +1915,10 @@ function createIkiMcpServer() {
1742
1915
  h: import_zod.z.number()
1743
1916
  }).partial()
1744
1917
  ).optional().describe(
1745
- "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. The nose is sized and placed by its dense core (its pixels at alpha >= 128, the drawing inside a soft nose's feather), not its whole part: `w`/`h` size the core, `cx` centres it, and a set `cy` centres its rows. A nose whose core is a speck \u2014 under a quarter of the trimmed part's width or height, such as a lone nostril mark or highlight \u2014 is sized and placed whole instead, and the report warns of it. Its `cy` defaults to unset, which lands the core's bottom row (the tip) 0.86 of the way from the eye row down to the mouth's, so the nose follows a retuned eye or mouth row. 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."
1918
+ "Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, blush_L, blush_R, 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. The nose is sized and placed by its dense core (its pixels at alpha >= 128, the drawing inside a soft nose's feather), not its whole part: `w`/`h` size the core, `cx` centres it, and a set `cy` centres its rows. A nose whose core is a speck \u2014 under a quarter of the trimmed part's width or height, such as a lone nostril mark or highlight \u2014 is sized and placed whole instead, and the report warns of it. Its `cy` defaults to unset, which lands the core's bottom row (the tip) 0.86 of the way from the eye row down to the mouth's, so the nose follows a retuned eye or mouth row. 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."
1746
1919
  ),
1747
1920
  mirrorParts: import_zod.z.array(import_zod.z.string()).optional().describe(
1748
- "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."
1921
+ "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), brow.png as the brow on the screen RIGHT (thick head at the image's left end, tail at its right), and blush.png as the blush on the screen LEFT (outer end, toward the face's edge, at the image's left end, inner end 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."
1749
1922
  )
1750
1923
  }
1751
1924
  },