@ikijs/mcp 0.4.0 → 0.6.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/dist/cli.js CHANGED
@@ -521,6 +521,7 @@ var EYE_ASPECT_MIN = 0.5;
521
521
  var IRIS_OFFSET_MAX_X = 3;
522
522
  var IRIS_OFFSET_MAX_Y = 10;
523
523
  var EDGE_SOLID_MAX = 0.5;
524
+ var LASH_CENTRE_TOL_FRAC = 0.03;
524
525
  var EDGE_RUN_MIN_PX = 40;
525
526
  var FLAT_CUT_MAX_FRAC = 0.1;
526
527
  var FLAT_CUT_MIN_PX = 40;
@@ -625,15 +626,24 @@ async function measureDir(absDir) {
625
626
  const warnings = [];
626
627
  for (const [role, m] of Object.entries(layers)) {
627
628
  const edges = [
628
- ["top", m.edgeTop, m.w],
629
- ["left", m.edgeLeft, m.h],
630
- ["right", m.edgeRight, m.h]
629
+ ["top", m.edgeTop, m.w, m.marginTop],
630
+ ["left", m.edgeLeft, m.h, m.marginLeft],
631
+ ["right", m.edgeRight, m.h, m.marginRight]
631
632
  ];
632
- if (role !== "body") edges.push(["bottom", m.edgeBottom, m.w]);
633
- for (const [side, v, span] of edges) {
633
+ if (role !== "body")
634
+ edges.push(["bottom", m.edgeBottom, m.w, m.marginBottom]);
635
+ for (const [side, v, span, margin] of edges) {
634
636
  if (v > EDGE_SOLID_MAX && v * span >= EDGE_RUN_MIN_PX) {
637
+ const remedy = margin === 0 ? (
638
+ // margin 0 is consistent with a clipped placement AND with art
639
+ // already cropped flush that happens to sit on the boundary, and
640
+ // the flattened layer cannot separate them — so name the free
641
+ // move rather than the cause. If moving it inward leaves the
642
+ // edge opaque, the drawing is the one at fault.
643
+ `it sits flush against the canvas ${side}, which a clipped placement and already-cropped art both produce. Move it inward first \u2014 retune layout.${role}.cx/cy/w, free \u2014 and remeasure; regenerate only if the edge is still opaque with margin to spare.`
644
+ ) : `its own drawing runs to the frame ${margin} px inside the canvas \u2014 regenerate that part with empty margin on that side. Billed.`;
635
645
  warnings.push(
636
- `${role}: ${pct(v)} of its ${side} edge is opaque \u2014 the art is cut off there, which shows as a straight seam once the head turns. Regenerate with empty margin on that side.`
646
+ `${role}: ${pct(v)} of its ${side} edge is opaque \u2014 the art is cut off there, which shows as a straight seam once the head turns. Cause: ${remedy}`
637
647
  );
638
648
  }
639
649
  }
@@ -654,7 +664,7 @@ async function measureDir(absDir) {
654
664
  const aspect = eye.h / eye.w;
655
665
  if (aspect < EYE_ASPECT_MIN) {
656
666
  warnings.push(
657
- `eye_${side}: sclera aspect h/w=${aspect.toFixed(2)} is flatter than ${EYE_ASPECT_MIN} \u2014 a round iris cannot sit inside it. Regenerate the eyewhite taller, not the iris smaller.`
667
+ `eye_${side}: sclera aspect h/w=${aspect.toFixed(2)} is flatter than ${EYE_ASPECT_MIN} \u2014 a round iris cannot sit inside it. Free fix first: set layout.eye_${side}.h and layout.lash_${side}.h to ${Math.ceil(eye.w * EYE_ASPECT_MIN)} (both, or the fold tears) \u2014 stretching a flat white lens is invisible. Regenerate the eyewhite taller only if that distorts the lash.`
658
668
  );
659
669
  }
660
670
  if (iris) {
@@ -675,7 +685,7 @@ async function measureDir(absDir) {
675
685
  if (lash) {
676
686
  const dx = lash.bboxCx - eye.bboxCx;
677
687
  const dTop = lash.marginTop - eye.marginTop;
678
- if (Math.abs(dx) > 0.5 || Math.abs(dTop) > 0.5) {
688
+ if (Math.abs(dx) > eye.w * LASH_CENTRE_TOL_FRAC || Math.abs(dTop) > 0.5) {
679
689
  warnings.push(
680
690
  `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.`
681
691
  );
@@ -738,6 +748,13 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
738
748
  var EYEWHITE_SRC = "eyewhite.png";
739
749
  var EYE_LASH_LUMA = 120;
740
750
  var LASH_KEEP_FRACTION = 0.5;
751
+ var NOSE_INK_TOL = 20;
752
+ var NOSE_INK_LOW = 8;
753
+ var NOSE_GROW = 2;
754
+ var NOSE_MIN_PIXELS = 12;
755
+ var NOSE_MAX_CUT_FRACTION = 0.25;
756
+ var NOSE_ROW_MIN_PIXELS = 8;
757
+ var NOSE_FILL_REACH = 8;
741
758
  var DEFAULT_LAYOUT = {
742
759
  // Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
743
760
  // without them still composes (head-only character).
@@ -821,13 +838,15 @@ function resolveLayout(overrides) {
821
838
  }
822
839
  next[field] = value;
823
840
  }
824
- if (override.w !== void 0) {
825
- if (!Number.isInteger(override.w) || override.w < 1 || override.w > CANVAS) {
841
+ for (const field of ["w", "h"]) {
842
+ const value = override[field];
843
+ if (value === void 0) continue;
844
+ if (!Number.isInteger(value) || value < 1 || value > CANVAS) {
826
845
  throw new AutoRigInputError(
827
- `layout.${role}.w must be an integer in 1..${CANVAS}, got ${String(override.w)}`
846
+ `layout.${role}.${field} must be an integer in 1..${CANVAS}, got ${String(value)}`
828
847
  );
829
848
  }
830
- next.w = override.w;
849
+ next[field] = value;
831
850
  }
832
851
  resolved[role] = next;
833
852
  }
@@ -862,15 +881,19 @@ async function partBuffer(role, cfg, partsDir, inMemory) {
862
881
  if (!cfg.noTrim) img = img.trim({ threshold: 12 });
863
882
  if (cfg.mirror) img = img.flop();
864
883
  const trimmed = await img.png().toBuffer({ resolveWithObject: true });
865
- const scaledH = Math.round(
866
- trimmed.info.height * cfg.w / trimmed.info.width
867
- );
868
- if (scaledH > CANVAS) {
869
- throw new AutoRigInputError(
870
- `layout.${role}.w: resized part ${cfg.w}x${scaledH} exceeds the ${CANVAS} canvas`
884
+ if (cfg.h === void 0) {
885
+ const scaledH = Math.round(
886
+ trimmed.info.height * cfg.w / trimmed.info.width
871
887
  );
888
+ if (scaledH > CANVAS) {
889
+ throw new AutoRigInputError(
890
+ `layout.${role}.w: resized part ${cfg.w}x${scaledH} exceeds the ${CANVAS} canvas`
891
+ );
892
+ }
872
893
  }
873
- const resized = await (0, import_sharp2.default)(trimmed.data).resize({ width: cfg.w }).png().toBuffer({ resolveWithObject: true });
894
+ const resized = await (0, import_sharp2.default)(trimmed.data).resize(
895
+ cfg.h === void 0 ? { width: cfg.w } : { width: cfg.w, height: cfg.h, fit: "fill" }
896
+ ).png().toBuffer({ resolveWithObject: true });
874
897
  return {
875
898
  buf: resized.data,
876
899
  w: resized.info.width,
@@ -943,6 +966,164 @@ async function prepEyeSplit(partsDir) {
943
966
  lash: await cropToBuffer(lash, W, H, region)
944
967
  };
945
968
  }
969
+ function cutNose(rgba, box) {
970
+ const W = CANVAS;
971
+ const x0 = Math.max(0, box.x0);
972
+ const y0 = Math.max(0, box.y0);
973
+ const x1 = Math.min(W, box.x1);
974
+ const y1 = Math.min(W, box.y1);
975
+ if (x1 - x0 < 4 || y1 - y0 < 4) return null;
976
+ const bw = x1 - x0;
977
+ const bh = y1 - y0;
978
+ const at = (x, y) => (y * W + x) * 4;
979
+ const median = (v) => v.sort((a, b) => a - b)[v.length >> 1];
980
+ const all = [[], [], []];
981
+ const rows = [];
982
+ for (let y = y0; y < y1; y++) {
983
+ const row = [[], [], []];
984
+ for (let x = x0; x < x1; x++) {
985
+ const i = at(x, y);
986
+ if (rgba[i + 3] === 0) continue;
987
+ for (let c = 0; c < 3; c++) {
988
+ row[c].push(rgba[i + c]);
989
+ all[c].push(rgba[i + c]);
990
+ }
991
+ }
992
+ rows.push(row);
993
+ }
994
+ const opaqueCount = all[0].length;
995
+ if (opaqueCount === 0) return null;
996
+ const skin = all.map(median);
997
+ const skinRow = rows.map(
998
+ (row) => row[0].length < NOSE_ROW_MIN_PIXELS ? skin : row.map(median)
999
+ );
1000
+ const distAt = (i, y) => {
1001
+ const ref = skinRow[y - y0];
1002
+ return Math.max(
1003
+ Math.abs(rgba[i] - ref[0]),
1004
+ Math.abs(rgba[i + 1] - ref[1]),
1005
+ Math.abs(rgba[i + 2] - ref[2])
1006
+ );
1007
+ };
1008
+ const dist = new Uint8Array(bw * bh);
1009
+ const ink = new Uint8Array(bw * bh);
1010
+ const queue = [];
1011
+ let inkCount = 0;
1012
+ for (let y = y0; y < y1; y++) {
1013
+ for (let x = x0; x < x1; x++) {
1014
+ const i = at(x, y);
1015
+ if (rgba[i + 3] === 0) continue;
1016
+ const d = distAt(i, y);
1017
+ const k = (y - y0) * bw + (x - x0);
1018
+ dist[k] = d;
1019
+ if (d > NOSE_INK_TOL) {
1020
+ ink[k] = 1;
1021
+ queue.push(k);
1022
+ inkCount++;
1023
+ }
1024
+ }
1025
+ }
1026
+ if (inkCount < NOSE_MIN_PIXELS) return null;
1027
+ while (queue.length > 0) {
1028
+ const k = queue.pop();
1029
+ const kx = k % bw;
1030
+ const ky = (k - kx) / bw;
1031
+ for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
1032
+ for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
1033
+ const nx = kx + dx;
1034
+ const ny = ky + dy;
1035
+ if (nx < 0 || nx >= bw || ny < 0 || ny >= bh) continue;
1036
+ const n = ny * bw + nx;
1037
+ if (ink[n] === 0 && dist[n] > NOSE_INK_LOW) {
1038
+ ink[n] = 1;
1039
+ queue.push(n);
1040
+ }
1041
+ }
1042
+ }
1043
+ }
1044
+ const cut = new Uint8Array(bw * bh);
1045
+ for (let y = 0; y < bh; y++) {
1046
+ for (let x = 0; x < bw; x++) {
1047
+ if (ink[y * bw + x] === 0) continue;
1048
+ for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
1049
+ for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
1050
+ const yy = y + dy;
1051
+ const xx = x + dx;
1052
+ if (yy >= 0 && yy < bh && xx >= 0 && xx < bw) cut[yy * bw + xx] = 1;
1053
+ }
1054
+ }
1055
+ }
1056
+ }
1057
+ let cutCount = 0;
1058
+ for (let y = 0; y < bh; y++) {
1059
+ for (let x = 0; x < bw; x++) {
1060
+ if (cut[y * bw + x] === 0) continue;
1061
+ const a = rgba[at(x0 + x, y0 + y) + 3];
1062
+ if (a === 0) continue;
1063
+ if (a < 255) return null;
1064
+ cutCount++;
1065
+ }
1066
+ }
1067
+ if (cutCount > NOSE_MAX_CUT_FRACTION * opaqueCount) return null;
1068
+ const face = Buffer.from(rgba);
1069
+ const nose = Buffer.from(rgba);
1070
+ for (let i = 3; i < nose.length; i += 4) nose[i] = 0;
1071
+ let minX = W;
1072
+ let minY = W;
1073
+ let maxX = -1;
1074
+ let maxY = -1;
1075
+ for (let y = 0; y < bh; y++) {
1076
+ const cy = y0 + y;
1077
+ let x = 0;
1078
+ while (x < bw) {
1079
+ if (cut[y * bw + x] === 0) {
1080
+ x++;
1081
+ continue;
1082
+ }
1083
+ const start = x;
1084
+ while (x < bw && cut[y * bw + x] === 1) x++;
1085
+ const end = x;
1086
+ const skinAt = (bx) => {
1087
+ for (let step = 0; step < NOSE_FILL_REACH && bx >= 0 && bx < bw; step++) {
1088
+ const i = at(x0 + bx, cy);
1089
+ if (rgba[i + 3] > 0 && cut[y * bw + bx] === 0) {
1090
+ if (distAt(i, cy) <= NOSE_INK_TOL / 2) return i;
1091
+ }
1092
+ bx += bx < start ? -1 : 1;
1093
+ }
1094
+ return -1;
1095
+ };
1096
+ const l = skinAt(start - 1);
1097
+ const r = skinAt(end);
1098
+ for (let k = start; k < end; k++) {
1099
+ const cx = x0 + k;
1100
+ const i = at(cx, cy);
1101
+ if (rgba[i + 3] === 0) continue;
1102
+ const t = (k - start + 1) / (end - start + 1);
1103
+ for (let c = 0; c < 3; c++) {
1104
+ const rowSkin = skinRow[y];
1105
+ const lc = l >= 0 ? rgba[l + c] : r >= 0 ? rgba[r + c] : rowSkin[c];
1106
+ const rc = r >= 0 ? rgba[r + c] : l >= 0 ? rgba[l + c] : rowSkin[c];
1107
+ face[i + c] = Math.round(lc * (1 - t) + rc * t);
1108
+ }
1109
+ nose[i] = rgba[i];
1110
+ nose[i + 1] = rgba[i + 1];
1111
+ nose[i + 2] = rgba[i + 2];
1112
+ nose[i + 3] = rgba[i + 3];
1113
+ if (cx < minX) minX = cx;
1114
+ if (cx > maxX) maxX = cx;
1115
+ if (cy < minY) minY = cy;
1116
+ if (cy > maxY) maxY = cy;
1117
+ }
1118
+ }
1119
+ }
1120
+ if (maxX < 0) return null;
1121
+ return {
1122
+ face,
1123
+ nose,
1124
+ bbox: { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 }
1125
+ };
1126
+ }
946
1127
  async function composeLayersFromParts(input) {
947
1128
  try {
948
1129
  const outDir = resolveOutputDir(input.outDir);
@@ -961,6 +1142,7 @@ async function composeLayersFromParts(input) {
961
1142
  const layers = [];
962
1143
  const skipped = [];
963
1144
  const preview = [];
1145
+ let faceRaw;
964
1146
  for (const role of ORDER) {
965
1147
  const cfg = layout[role];
966
1148
  const part = await partBuffer(
@@ -975,9 +1157,15 @@ async function composeLayersFromParts(input) {
975
1157
  continue;
976
1158
  }
977
1159
  const { left, top } = placement(role, cfg, part.w, part.h);
978
- const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
1160
+ const composed = blankCanvas().composite([
1161
+ { input: part.buf, left, top }
1162
+ ]);
979
1163
  const outPath = import_node_path4.default.join(outDir, `${role}.png`);
980
- writeFileAtomic(outPath, layer);
1164
+ if (role === "face") {
1165
+ faceRaw = await composed.raw().toBuffer();
1166
+ } else {
1167
+ writeFileAtomic(outPath, await composed.png().toBuffer());
1168
+ }
981
1169
  layers.push({
982
1170
  role,
983
1171
  path: outPath,
@@ -988,6 +1176,48 @@ async function composeLayersFromParts(input) {
988
1176
  });
989
1177
  preview.push({ input: part.buf, left, top });
990
1178
  }
1179
+ const placed = (role) => layers.find((l) => l.role === role);
1180
+ const eyeL = placed("eye_L");
1181
+ const eyeR = placed("eye_R");
1182
+ const mouth = placed("mouth");
1183
+ const noseCut = cutNose(faceRaw, {
1184
+ x0: eyeR.left + eyeR.width,
1185
+ x1: eyeL.left,
1186
+ y0: Math.round(
1187
+ (eyeL.top + eyeL.height / 2 + eyeR.top + eyeR.height / 2) / 2
1188
+ ),
1189
+ y1: mouth.top - 2
1190
+ });
1191
+ const rawCanvas = {
1192
+ raw: { width: CANVAS, height: CANVAS, channels: 4 }
1193
+ };
1194
+ const facePath = import_node_path4.default.join(outDir, "face.png");
1195
+ const nosePath = import_node_path4.default.join(outDir, "nose.png");
1196
+ if (noseCut === null) {
1197
+ writeFileAtomic(
1198
+ facePath,
1199
+ await (0, import_sharp2.default)(faceRaw, rawCanvas).png().toBuffer()
1200
+ );
1201
+ import_node_fs3.default.rmSync(nosePath, { force: true });
1202
+ skipped.push("nose");
1203
+ } else {
1204
+ writeFileAtomic(
1205
+ facePath,
1206
+ await (0, import_sharp2.default)(noseCut.face, rawCanvas).png().toBuffer()
1207
+ );
1208
+ writeFileAtomic(
1209
+ nosePath,
1210
+ await (0, import_sharp2.default)(noseCut.nose, rawCanvas).png().toBuffer()
1211
+ );
1212
+ layers.splice(layers.findIndex((l) => l.role === "face") + 1, 0, {
1213
+ role: "nose",
1214
+ path: nosePath,
1215
+ width: noseCut.bbox.w,
1216
+ height: noseCut.bbox.h,
1217
+ left: noseCut.bbox.x,
1218
+ top: noseCut.bbox.y
1219
+ });
1220
+ }
991
1221
  const previewPng = await (0, import_sharp2.default)({
992
1222
  create: {
993
1223
  width: CANVAS,
@@ -1015,7 +1245,7 @@ async function composeLayersFromParts(input) {
1015
1245
 
1016
1246
  // src/server.ts
1017
1247
  function createIkiMcpServer() {
1018
- const server = new import_mcp.McpServer({ name: "iki", version: "0.4.0" });
1248
+ const server = new import_mcp.McpServer({ name: "iki", version: "0.6.0" });
1019
1249
  server.registerTool(
1020
1250
  "validate_iki",
1021
1251
  {
@@ -1116,7 +1346,7 @@ function createIkiMcpServer() {
1116
1346
  server.registerTool(
1117
1347
  "compose_layers_from_parts",
1118
1348
  {
1119
- 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 are optional.",
1349
+ description: "Composes AI-generated part PNGs into canvas-aligned, role-named PNG layers on disk, ready for auto_rig_from_layers (the eyewhite split, the nose cut out of the face so the rig can lead the turn with it, 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 are optional; nose.png is cut from face.png when it has a drawn nose (reported under `skipped` otherwise).",
1120
1350
  inputSchema: {
1121
1351
  partsDir: import_zod.z.string().describe(
1122
1352
  "Directory of the source part PNGs (resolved against cwd)."
@@ -1126,9 +1356,14 @@ function createIkiMcpServer() {
1126
1356
  ),
1127
1357
  layout: import_zod.z.record(
1128
1358
  import_zod.z.string(),
1129
- import_zod.z.object({ cx: import_zod.z.number(), cy: import_zod.z.number(), w: import_zod.z.number() }).partial()
1359
+ import_zod.z.object({
1360
+ cx: import_zod.z.number(),
1361
+ cy: import_zod.z.number(),
1362
+ w: import_zod.z.number(),
1363
+ h: import_zod.z.number()
1364
+ }).partial()
1130
1365
  ).optional().describe(
1131
- "Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, 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` is 1..1100."
1366
+ "Per-role override merged over the built-in defaults, keyed by role \u2014 hair_back, body, face, 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)."
1132
1367
  )
1133
1368
  }
1134
1369
  },