@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/index.d.mts CHANGED
@@ -284,6 +284,8 @@ declare const DEFAULT_LAYOUT: {
284
284
  };
285
285
  };
286
286
  type Role = keyof typeof DEFAULT_LAYOUT;
287
+ /** A written layer's role: a placed part, or the nose cut out of the face. */
288
+ type LayerRole = Role | "nose";
287
289
  /** Per-role placement overrides; anything omitted keeps the default above. */
288
290
  type LayoutOverride = Partial<Record<Role, {
289
291
  cx?: number;
@@ -304,7 +306,7 @@ interface ComposeInput {
304
306
  layout?: LayoutOverride;
305
307
  }
306
308
  interface ComposedLayer {
307
- role: Role;
309
+ role: LayerRole;
308
310
  path: string;
309
311
  /** Size of the placed part itself (the file is always CANVAS x CANVAS). */
310
312
  width: number;
@@ -316,8 +318,9 @@ type ComposeResult = {
316
318
  ok: true;
317
319
  outDir: string;
318
320
  layers: ComposedLayer[];
319
- /** Roles whose optional part was absent from the parts dir. */
320
- skipped: Role[];
321
+ /** Roles whose optional part was absent from the parts dir — and `nose`
322
+ * when the face had no drawn nose to cut. */
323
+ skipped: LayerRole[];
321
324
  preview: string;
322
325
  measure: MeasureReport;
323
326
  } | {
package/dist/index.d.ts CHANGED
@@ -284,6 +284,8 @@ declare const DEFAULT_LAYOUT: {
284
284
  };
285
285
  };
286
286
  type Role = keyof typeof DEFAULT_LAYOUT;
287
+ /** A written layer's role: a placed part, or the nose cut out of the face. */
288
+ type LayerRole = Role | "nose";
287
289
  /** Per-role placement overrides; anything omitted keeps the default above. */
288
290
  type LayoutOverride = Partial<Record<Role, {
289
291
  cx?: number;
@@ -304,7 +306,7 @@ interface ComposeInput {
304
306
  layout?: LayoutOverride;
305
307
  }
306
308
  interface ComposedLayer {
307
- role: Role;
309
+ role: LayerRole;
308
310
  path: string;
309
311
  /** Size of the placed part itself (the file is always CANVAS x CANVAS). */
310
312
  width: number;
@@ -316,8 +318,9 @@ type ComposeResult = {
316
318
  ok: true;
317
319
  outDir: string;
318
320
  layers: ComposedLayer[];
319
- /** Roles whose optional part was absent from the parts dir. */
320
- skipped: Role[];
321
+ /** Roles whose optional part was absent from the parts dir — and `nose`
322
+ * when the face had no drawn nose to cut. */
323
+ skipped: LayerRole[];
321
324
  preview: string;
322
325
  measure: MeasureReport;
323
326
  } | {
package/dist/index.js CHANGED
@@ -762,6 +762,13 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
762
762
  var EYEWHITE_SRC = "eyewhite.png";
763
763
  var EYE_LASH_LUMA = 120;
764
764
  var LASH_KEEP_FRACTION = 0.5;
765
+ var NOSE_INK_TOL = 20;
766
+ var NOSE_INK_LOW = 8;
767
+ var NOSE_GROW = 2;
768
+ var NOSE_MIN_PIXELS = 12;
769
+ var NOSE_MAX_CUT_FRACTION = 0.25;
770
+ var NOSE_ROW_MIN_PIXELS = 8;
771
+ var NOSE_FILL_REACH = 8;
765
772
  var DEFAULT_LAYOUT = {
766
773
  // Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
767
774
  // without them still composes (head-only character).
@@ -973,6 +980,164 @@ async function prepEyeSplit(partsDir) {
973
980
  lash: await cropToBuffer(lash, W, H, region)
974
981
  };
975
982
  }
983
+ function cutNose(rgba, box) {
984
+ const W = CANVAS;
985
+ const x0 = Math.max(0, box.x0);
986
+ const y0 = Math.max(0, box.y0);
987
+ const x1 = Math.min(W, box.x1);
988
+ const y1 = Math.min(W, box.y1);
989
+ if (x1 - x0 < 4 || y1 - y0 < 4) return null;
990
+ const bw = x1 - x0;
991
+ const bh = y1 - y0;
992
+ const at = (x, y) => (y * W + x) * 4;
993
+ const median = (v) => v.sort((a, b) => a - b)[v.length >> 1];
994
+ const all = [[], [], []];
995
+ const rows = [];
996
+ for (let y = y0; y < y1; y++) {
997
+ const row = [[], [], []];
998
+ for (let x = x0; x < x1; x++) {
999
+ const i = at(x, y);
1000
+ if (rgba[i + 3] === 0) continue;
1001
+ for (let c = 0; c < 3; c++) {
1002
+ row[c].push(rgba[i + c]);
1003
+ all[c].push(rgba[i + c]);
1004
+ }
1005
+ }
1006
+ rows.push(row);
1007
+ }
1008
+ const opaqueCount = all[0].length;
1009
+ if (opaqueCount === 0) return null;
1010
+ const skin = all.map(median);
1011
+ const skinRow = rows.map(
1012
+ (row) => row[0].length < NOSE_ROW_MIN_PIXELS ? skin : row.map(median)
1013
+ );
1014
+ const distAt = (i, y) => {
1015
+ const ref = skinRow[y - y0];
1016
+ return Math.max(
1017
+ Math.abs(rgba[i] - ref[0]),
1018
+ Math.abs(rgba[i + 1] - ref[1]),
1019
+ Math.abs(rgba[i + 2] - ref[2])
1020
+ );
1021
+ };
1022
+ const dist = new Uint8Array(bw * bh);
1023
+ const ink = new Uint8Array(bw * bh);
1024
+ const queue = [];
1025
+ let inkCount = 0;
1026
+ for (let y = y0; y < y1; y++) {
1027
+ for (let x = x0; x < x1; x++) {
1028
+ const i = at(x, y);
1029
+ if (rgba[i + 3] === 0) continue;
1030
+ const d = distAt(i, y);
1031
+ const k = (y - y0) * bw + (x - x0);
1032
+ dist[k] = d;
1033
+ if (d > NOSE_INK_TOL) {
1034
+ ink[k] = 1;
1035
+ queue.push(k);
1036
+ inkCount++;
1037
+ }
1038
+ }
1039
+ }
1040
+ if (inkCount < NOSE_MIN_PIXELS) return null;
1041
+ while (queue.length > 0) {
1042
+ const k = queue.pop();
1043
+ const kx = k % bw;
1044
+ const ky = (k - kx) / bw;
1045
+ for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
1046
+ for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
1047
+ const nx = kx + dx;
1048
+ const ny = ky + dy;
1049
+ if (nx < 0 || nx >= bw || ny < 0 || ny >= bh) continue;
1050
+ const n = ny * bw + nx;
1051
+ if (ink[n] === 0 && dist[n] > NOSE_INK_LOW) {
1052
+ ink[n] = 1;
1053
+ queue.push(n);
1054
+ }
1055
+ }
1056
+ }
1057
+ }
1058
+ const cut = new Uint8Array(bw * bh);
1059
+ for (let y = 0; y < bh; y++) {
1060
+ for (let x = 0; x < bw; x++) {
1061
+ if (ink[y * bw + x] === 0) continue;
1062
+ for (let dy = -NOSE_GROW; dy <= NOSE_GROW; dy++) {
1063
+ for (let dx = -NOSE_GROW; dx <= NOSE_GROW; dx++) {
1064
+ const yy = y + dy;
1065
+ const xx = x + dx;
1066
+ if (yy >= 0 && yy < bh && xx >= 0 && xx < bw) cut[yy * bw + xx] = 1;
1067
+ }
1068
+ }
1069
+ }
1070
+ }
1071
+ let cutCount = 0;
1072
+ for (let y = 0; y < bh; y++) {
1073
+ for (let x = 0; x < bw; x++) {
1074
+ if (cut[y * bw + x] === 0) continue;
1075
+ const a = rgba[at(x0 + x, y0 + y) + 3];
1076
+ if (a === 0) continue;
1077
+ if (a < 255) return null;
1078
+ cutCount++;
1079
+ }
1080
+ }
1081
+ if (cutCount > NOSE_MAX_CUT_FRACTION * opaqueCount) return null;
1082
+ const face = Buffer.from(rgba);
1083
+ const nose = Buffer.from(rgba);
1084
+ for (let i = 3; i < nose.length; i += 4) nose[i] = 0;
1085
+ let minX = W;
1086
+ let minY = W;
1087
+ let maxX = -1;
1088
+ let maxY = -1;
1089
+ for (let y = 0; y < bh; y++) {
1090
+ const cy = y0 + y;
1091
+ let x = 0;
1092
+ while (x < bw) {
1093
+ if (cut[y * bw + x] === 0) {
1094
+ x++;
1095
+ continue;
1096
+ }
1097
+ const start = x;
1098
+ while (x < bw && cut[y * bw + x] === 1) x++;
1099
+ const end = x;
1100
+ const skinAt = (bx) => {
1101
+ for (let step = 0; step < NOSE_FILL_REACH && bx >= 0 && bx < bw; step++) {
1102
+ const i = at(x0 + bx, cy);
1103
+ if (rgba[i + 3] > 0 && cut[y * bw + bx] === 0) {
1104
+ if (distAt(i, cy) <= NOSE_INK_TOL / 2) return i;
1105
+ }
1106
+ bx += bx < start ? -1 : 1;
1107
+ }
1108
+ return -1;
1109
+ };
1110
+ const l = skinAt(start - 1);
1111
+ const r = skinAt(end);
1112
+ for (let k = start; k < end; k++) {
1113
+ const cx = x0 + k;
1114
+ const i = at(cx, cy);
1115
+ if (rgba[i + 3] === 0) continue;
1116
+ const t = (k - start + 1) / (end - start + 1);
1117
+ for (let c = 0; c < 3; c++) {
1118
+ const rowSkin = skinRow[y];
1119
+ const lc = l >= 0 ? rgba[l + c] : r >= 0 ? rgba[r + c] : rowSkin[c];
1120
+ const rc = r >= 0 ? rgba[r + c] : l >= 0 ? rgba[l + c] : rowSkin[c];
1121
+ face[i + c] = Math.round(lc * (1 - t) + rc * t);
1122
+ }
1123
+ nose[i] = rgba[i];
1124
+ nose[i + 1] = rgba[i + 1];
1125
+ nose[i + 2] = rgba[i + 2];
1126
+ nose[i + 3] = rgba[i + 3];
1127
+ if (cx < minX) minX = cx;
1128
+ if (cx > maxX) maxX = cx;
1129
+ if (cy < minY) minY = cy;
1130
+ if (cy > maxY) maxY = cy;
1131
+ }
1132
+ }
1133
+ }
1134
+ if (maxX < 0) return null;
1135
+ return {
1136
+ face,
1137
+ nose,
1138
+ bbox: { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 }
1139
+ };
1140
+ }
976
1141
  async function composeLayersFromParts(input) {
977
1142
  try {
978
1143
  const outDir = resolveOutputDir(input.outDir);
@@ -991,6 +1156,7 @@ async function composeLayersFromParts(input) {
991
1156
  const layers = [];
992
1157
  const skipped = [];
993
1158
  const preview = [];
1159
+ let faceRaw;
994
1160
  for (const role of ORDER) {
995
1161
  const cfg = layout[role];
996
1162
  const part = await partBuffer(
@@ -1005,9 +1171,15 @@ async function composeLayersFromParts(input) {
1005
1171
  continue;
1006
1172
  }
1007
1173
  const { left, top } = placement(role, cfg, part.w, part.h);
1008
- const layer = await blankCanvas().composite([{ input: part.buf, left, top }]).png().toBuffer();
1174
+ const composed = blankCanvas().composite([
1175
+ { input: part.buf, left, top }
1176
+ ]);
1009
1177
  const outPath = import_node_path4.default.join(outDir, `${role}.png`);
1010
- writeFileAtomic(outPath, layer);
1178
+ if (role === "face") {
1179
+ faceRaw = await composed.raw().toBuffer();
1180
+ } else {
1181
+ writeFileAtomic(outPath, await composed.png().toBuffer());
1182
+ }
1011
1183
  layers.push({
1012
1184
  role,
1013
1185
  path: outPath,
@@ -1018,6 +1190,48 @@ async function composeLayersFromParts(input) {
1018
1190
  });
1019
1191
  preview.push({ input: part.buf, left, top });
1020
1192
  }
1193
+ const placed = (role) => layers.find((l) => l.role === role);
1194
+ const eyeL = placed("eye_L");
1195
+ const eyeR = placed("eye_R");
1196
+ const mouth = placed("mouth");
1197
+ const noseCut = cutNose(faceRaw, {
1198
+ x0: eyeR.left + eyeR.width,
1199
+ x1: eyeL.left,
1200
+ y0: Math.round(
1201
+ (eyeL.top + eyeL.height / 2 + eyeR.top + eyeR.height / 2) / 2
1202
+ ),
1203
+ y1: mouth.top - 2
1204
+ });
1205
+ const rawCanvas = {
1206
+ raw: { width: CANVAS, height: CANVAS, channels: 4 }
1207
+ };
1208
+ const facePath = import_node_path4.default.join(outDir, "face.png");
1209
+ const nosePath = import_node_path4.default.join(outDir, "nose.png");
1210
+ if (noseCut === null) {
1211
+ writeFileAtomic(
1212
+ facePath,
1213
+ await (0, import_sharp2.default)(faceRaw, rawCanvas).png().toBuffer()
1214
+ );
1215
+ import_node_fs3.default.rmSync(nosePath, { force: true });
1216
+ skipped.push("nose");
1217
+ } else {
1218
+ writeFileAtomic(
1219
+ facePath,
1220
+ await (0, import_sharp2.default)(noseCut.face, rawCanvas).png().toBuffer()
1221
+ );
1222
+ writeFileAtomic(
1223
+ nosePath,
1224
+ await (0, import_sharp2.default)(noseCut.nose, rawCanvas).png().toBuffer()
1225
+ );
1226
+ layers.splice(layers.findIndex((l) => l.role === "face") + 1, 0, {
1227
+ role: "nose",
1228
+ path: nosePath,
1229
+ width: noseCut.bbox.w,
1230
+ height: noseCut.bbox.h,
1231
+ left: noseCut.bbox.x,
1232
+ top: noseCut.bbox.y
1233
+ });
1234
+ }
1021
1235
  const previewPng = await (0, import_sharp2.default)({
1022
1236
  create: {
1023
1237
  width: CANVAS,
@@ -1047,7 +1261,7 @@ async function composeLayersFromParts(input) {
1047
1261
  var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
1048
1262
  var import_zod = require("zod");
1049
1263
  function createIkiMcpServer() {
1050
- const server = new import_mcp.McpServer({ name: "iki", version: "0.5.0" });
1264
+ const server = new import_mcp.McpServer({ name: "iki", version: "0.6.0" });
1051
1265
  server.registerTool(
1052
1266
  "validate_iki",
1053
1267
  {
@@ -1148,7 +1362,7 @@ function createIkiMcpServer() {
1148
1362
  server.registerTool(
1149
1363
  "compose_layers_from_parts",
1150
1364
  {
1151
- 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.",
1365
+ 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).",
1152
1366
  inputSchema: {
1153
1367
  partsDir: import_zod.z.string().describe(
1154
1368
  "Directory of the source part PNGs (resolved against cwd)."