@ikijs/mcp 0.5.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
@@ -748,6 +748,13 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
748
748
  var EYEWHITE_SRC = "eyewhite.png";
749
749
  var EYE_LASH_LUMA = 120;
750
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;
751
758
  var DEFAULT_LAYOUT = {
752
759
  // Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
753
760
  // without them still composes (head-only character).
@@ -959,6 +966,164 @@ async function prepEyeSplit(partsDir) {
959
966
  lash: await cropToBuffer(lash, W, H, region)
960
967
  };
961
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
+ }
962
1127
  async function composeLayersFromParts(input) {
963
1128
  try {
964
1129
  const outDir = resolveOutputDir(input.outDir);
@@ -977,6 +1142,7 @@ async function composeLayersFromParts(input) {
977
1142
  const layers = [];
978
1143
  const skipped = [];
979
1144
  const preview = [];
1145
+ let faceRaw;
980
1146
  for (const role of ORDER) {
981
1147
  const cfg = layout[role];
982
1148
  const part = await partBuffer(
@@ -991,9 +1157,15 @@ async function composeLayersFromParts(input) {
991
1157
  continue;
992
1158
  }
993
1159
  const { left, top } = placement(role, cfg, part.w, part.h);
994
- const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
1160
+ const composed = blankCanvas().composite([
1161
+ { input: part.buf, left, top }
1162
+ ]);
995
1163
  const outPath = import_node_path4.default.join(outDir, `${role}.png`);
996
- writeFileAtomic(outPath, layer);
1164
+ if (role === "face") {
1165
+ faceRaw = await composed.raw().toBuffer();
1166
+ } else {
1167
+ writeFileAtomic(outPath, await composed.png().toBuffer());
1168
+ }
997
1169
  layers.push({
998
1170
  role,
999
1171
  path: outPath,
@@ -1004,6 +1176,48 @@ async function composeLayersFromParts(input) {
1004
1176
  });
1005
1177
  preview.push({ input: part.buf, left, top });
1006
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
+ }
1007
1221
  const previewPng = await (0, import_sharp2.default)({
1008
1222
  create: {
1009
1223
  width: CANVAS,
@@ -1031,7 +1245,7 @@ async function composeLayersFromParts(input) {
1031
1245
 
1032
1246
  // src/server.ts
1033
1247
  function createIkiMcpServer() {
1034
- const server = new import_mcp.McpServer({ name: "iki", version: "0.5.0" });
1248
+ const server = new import_mcp.McpServer({ name: "iki", version: "0.6.0" });
1035
1249
  server.registerTool(
1036
1250
  "validate_iki",
1037
1251
  {
@@ -1132,7 +1346,7 @@ function createIkiMcpServer() {
1132
1346
  server.registerTool(
1133
1347
  "compose_layers_from_parts",
1134
1348
  {
1135
- 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).",
1136
1350
  inputSchema: {
1137
1351
  partsDir: import_zod.z.string().describe(
1138
1352
  "Directory of the source part PNGs (resolved against cwd)."