@ikijs/mcp 0.10.0 → 0.11.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.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  createIkiMcpServer
4
- } from "./chunk-OCIICRUA.mjs";
4
+ } from "./chunk-4AE4D6SE.mjs";
5
5
 
6
6
  // src/cli.ts
7
7
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
package/dist/index.d.mts CHANGED
@@ -66,7 +66,9 @@ interface AutoRigInput {
66
66
  /** Output `.iki` path (relative paths resolve against the process cwd). */
67
67
  outputPath?: string;
68
68
  /** Palette-quantize the atlas PNG to this many colours (integer, 2..256).
69
- * Omitted = lossless. See renderAtlasToDataUri for the size/quality trade. */
69
+ * Omitted = lossless. See renderAtlasToDataUri for the size/quality trade.
70
+ * A `nose` layer is left out of the quantized page, on a second, lossless
71
+ * page of its own, because the palette rims a soft-shaded nose's feather. */
70
72
  quantizeColors?: number;
71
73
  /** Head-turn cues to fit the rig to, as `measure_turn_reference` reports
72
74
  * them. */
@@ -84,6 +86,8 @@ type AutoRigResult = {
84
86
  height: number;
85
87
  };
86
88
  partCount: number;
89
+ /** The atlas's embedded size: its data URI's length, summed over both
90
+ * pages when a quantized rig puts the nose on a page of its own. */
87
91
  atlasBytes: number;
88
92
  /** The head's half-width at the eye row, measured off the layers.
89
93
  * Absent when the opaque (alpha >= 128) union has no span there — a
@@ -129,6 +133,19 @@ type AutoRigResult = {
129
133
  left?: IrisStrand;
130
134
  right?: IrisStrand;
131
135
  };
136
+ /** The nose's dense core, canvas px — its pixels at alpha >= 128, not
137
+ * grown — the box handed to the generator as the nose layer's
138
+ * `denseCore` and, there, its turn landmark. Absent without a `nose`
139
+ * layer, when the nose has no pixel at that threshold (painted wholly
140
+ * translucent), or when its core is a speck of its crop
141
+ * (`isSpeckCore`: a lone nostril mark or highlight); the crop stands
142
+ * in for it in both cases. */
143
+ noseCore?: {
144
+ x: number;
145
+ y: number;
146
+ w: number;
147
+ h: number;
148
+ };
132
149
  /** What the turn solve settled on — the cues the rig reaches, the
133
150
  * targets it had to cut down (`clamped`) and, per side whose far iris
134
151
  * the bangs' run still covers at some far stop, how much of it
@@ -154,7 +171,14 @@ type AutoRigResult = {
154
171
  * run it would slide under are measured on the iris's own centre row too
155
172
  * (`strandEdges`), so the turn keeps the far iris from sliding under the bangs
156
173
  * any further than it is painted; a run narrower than half the iris there is
157
- * hair detail the iris may cross, and is skipped.
174
+ * hair detail the iris may cross, and is skipped. The nose layer alone also
175
+ * gets its dense core measured (`LayerInput.denseCore`, returned as
176
+ * `noseCore`), so a shaded nose is fitted and tilted by the drawing inside its
177
+ * feather rather than by the feather itself — unless that core is a speck of
178
+ * the nose's crop (`isSpeckCore`), which is not handed over: the crop stands
179
+ * in, as it does for a nose with no core. Under `quantizeColors` that nose
180
+ * is atlased alone on a second, lossless page, which the palette would
181
+ * otherwise rim; `atlasBytes` sums both pages.
158
182
  *
159
183
  * Re-host of examples/editor/src/store.ts `importLayerSet` with the three DOM
160
184
  * pixel functions swapped for the sharp-backed ./node-images helpers; the pure
@@ -242,7 +266,8 @@ declare function formatMeasureReport(result: MeasureReport & {
242
266
  * Compose AI-generated part PNGs into canvas-aligned, role-named layers for the
243
267
  * Iki auto-rig (`auto_rig_from_layers` / `@ikijs/editor` generateIkiFromLayerSet).
244
268
  * Each part is alpha-trimmed, resized to a target width, optionally mirrored,
245
- * and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas.
269
+ * and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas
270
+ * — the nose by its dense core, the drawing inside a soft nose's feather.
246
271
  *
247
272
  * Ports the composer that shipped as a script in the Claude Code plugin, so a
248
273
  * plugin user gets it from the MCP server instead of an ad-hoc `sharp` install.
@@ -277,7 +302,6 @@ declare const DEFAULT_LAYOUT: {
277
302
  nose: {
278
303
  src: string;
279
304
  cx: number;
280
- cy: number;
281
305
  w: number;
282
306
  optional: true;
283
307
  };
package/dist/index.d.ts CHANGED
@@ -66,7 +66,9 @@ interface AutoRigInput {
66
66
  /** Output `.iki` path (relative paths resolve against the process cwd). */
67
67
  outputPath?: string;
68
68
  /** Palette-quantize the atlas PNG to this many colours (integer, 2..256).
69
- * Omitted = lossless. See renderAtlasToDataUri for the size/quality trade. */
69
+ * Omitted = lossless. See renderAtlasToDataUri for the size/quality trade.
70
+ * A `nose` layer is left out of the quantized page, on a second, lossless
71
+ * page of its own, because the palette rims a soft-shaded nose's feather. */
70
72
  quantizeColors?: number;
71
73
  /** Head-turn cues to fit the rig to, as `measure_turn_reference` reports
72
74
  * them. */
@@ -84,6 +86,8 @@ type AutoRigResult = {
84
86
  height: number;
85
87
  };
86
88
  partCount: number;
89
+ /** The atlas's embedded size: its data URI's length, summed over both
90
+ * pages when a quantized rig puts the nose on a page of its own. */
87
91
  atlasBytes: number;
88
92
  /** The head's half-width at the eye row, measured off the layers.
89
93
  * Absent when the opaque (alpha >= 128) union has no span there — a
@@ -129,6 +133,19 @@ type AutoRigResult = {
129
133
  left?: IrisStrand;
130
134
  right?: IrisStrand;
131
135
  };
136
+ /** The nose's dense core, canvas px — its pixels at alpha >= 128, not
137
+ * grown — the box handed to the generator as the nose layer's
138
+ * `denseCore` and, there, its turn landmark. Absent without a `nose`
139
+ * layer, when the nose has no pixel at that threshold (painted wholly
140
+ * translucent), or when its core is a speck of its crop
141
+ * (`isSpeckCore`: a lone nostril mark or highlight); the crop stands
142
+ * in for it in both cases. */
143
+ noseCore?: {
144
+ x: number;
145
+ y: number;
146
+ w: number;
147
+ h: number;
148
+ };
132
149
  /** What the turn solve settled on — the cues the rig reaches, the
133
150
  * targets it had to cut down (`clamped`) and, per side whose far iris
134
151
  * the bangs' run still covers at some far stop, how much of it
@@ -154,7 +171,14 @@ type AutoRigResult = {
154
171
  * run it would slide under are measured on the iris's own centre row too
155
172
  * (`strandEdges`), so the turn keeps the far iris from sliding under the bangs
156
173
  * any further than it is painted; a run narrower than half the iris there is
157
- * hair detail the iris may cross, and is skipped.
174
+ * hair detail the iris may cross, and is skipped. The nose layer alone also
175
+ * gets its dense core measured (`LayerInput.denseCore`, returned as
176
+ * `noseCore`), so a shaded nose is fitted and tilted by the drawing inside its
177
+ * feather rather than by the feather itself — unless that core is a speck of
178
+ * the nose's crop (`isSpeckCore`), which is not handed over: the crop stands
179
+ * in, as it does for a nose with no core. Under `quantizeColors` that nose
180
+ * is atlased alone on a second, lossless page, which the palette would
181
+ * otherwise rim; `atlasBytes` sums both pages.
158
182
  *
159
183
  * Re-host of examples/editor/src/store.ts `importLayerSet` with the three DOM
160
184
  * pixel functions swapped for the sharp-backed ./node-images helpers; the pure
@@ -242,7 +266,8 @@ declare function formatMeasureReport(result: MeasureReport & {
242
266
  * Compose AI-generated part PNGs into canvas-aligned, role-named layers for the
243
267
  * Iki auto-rig (`auto_rig_from_layers` / `@ikijs/editor` generateIkiFromLayerSet).
244
268
  * Each part is alpha-trimmed, resized to a target width, optionally mirrored,
245
- * and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas.
269
+ * and pasted at a chosen center on a shared transparent CANVAS x CANVAS canvas
270
+ * — the nose by its dense core, the drawing inside a soft nose's feather.
246
271
  *
247
272
  * Ports the composer that shipped as a script in the Claude Code plugin, so a
248
273
  * plugin user gets it from the MCP server instead of an ad-hoc `sharp` install.
@@ -277,7 +302,6 @@ declare const DEFAULT_LAYOUT: {
277
302
  nose: {
278
303
  src: string;
279
304
  cx: number;
280
- cy: number;
281
305
  w: number;
282
306
  optional: true;
283
307
  };
package/dist/index.js CHANGED
@@ -270,6 +270,28 @@ var BG_HUE_MAX = 260;
270
270
  var BG_SAT_MAX = 0.2;
271
271
  var BG_V_MIN = 0.55;
272
272
  var BG_V_BLACK = 0.03;
273
+ function denseCoreOf(rgba, width, height) {
274
+ let minX = width;
275
+ let minY = height;
276
+ let maxX = -1;
277
+ let maxY = -1;
278
+ for (let y = 0; y < height; y++) {
279
+ for (let x = 0; x < width; x++) {
280
+ if (rgba[(y * width + x) * 4 + 3] >= ALPHA_OPAQUE) {
281
+ if (x < minX) minX = x;
282
+ if (x > maxX) maxX = x;
283
+ if (y < minY) minY = y;
284
+ if (y > maxY) maxY = y;
285
+ }
286
+ }
287
+ }
288
+ if (maxX < 0) return null;
289
+ return { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 };
290
+ }
291
+ var SPECK_CORE_FRACTION = 0.25;
292
+ function isSpeckCore(core, part) {
293
+ return core.w < SPECK_CORE_FRACTION * part.w || core.h < SPECK_CORE_FRACTION * part.h;
294
+ }
273
295
  function rgbToHsv(r, g, b) {
274
296
  r /= 255;
275
297
  g /= 255;
@@ -791,6 +813,19 @@ function facePlateHalfOf(layers) {
791
813
  return face.cropW / 2;
792
814
  }
793
815
  var STRAND_MIN_RUN_FRACTION = 0.5;
816
+ var LOSSLESS_PAGE_ROLE = "nose";
817
+ async function renderAtlasPage(crops, quantizeColors) {
818
+ const layout = (0, import_editor2.packAtlas)(
819
+ crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
820
+ );
821
+ if (layout.pageWidth * layout.pageHeight > MAX_ATLAS_AREA) {
822
+ throw new AutoRigInputError(
823
+ `atlas page ${layout.pageWidth}x${layout.pageHeight} exceeds max area ${MAX_ATLAS_AREA}`
824
+ );
825
+ }
826
+ const dataUri = await renderAtlasToDataUri(crops, layout, quantizeColors);
827
+ return { crops, layout, dataUri };
828
+ }
794
829
  async function autoRigFromLayers(input) {
795
830
  try {
796
831
  const layers = input.layers;
@@ -902,10 +937,14 @@ async function autoRigFromLayers(input) {
902
937
  );
903
938
  }
904
939
  layer.rowHalfWidths = rowHalfWidths;
940
+ } else if (role === "nose") {
941
+ const core = denseCoreOf(png.rgba, canvasW, canvasH);
942
+ if (core !== null && !isSpeckCore(core, bbox)) layer.denseCore = core;
905
943
  }
906
944
  layerInputs.push(layer);
907
945
  crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
908
946
  }
947
+ const noseCore = layerInputs.find((l) => l.role === "nose")?.denseCore;
909
948
  const eyeRow = eyeRowOf(layerInputs);
910
949
  const span = foregroundSpan(
911
950
  opaque,
@@ -1015,46 +1054,58 @@ async function autoRigFromLayers(input) {
1015
1054
  throw new AutoRigInputError(e.message);
1016
1055
  }
1017
1056
  const doc = new import_editor2.EditorDocument(model);
1018
- const layout = (0, import_editor2.packAtlas)(
1019
- crops.map((c) => ({ id: c.id, width: c.width, height: c.height }))
1020
- );
1021
- if (layout.pageWidth * layout.pageHeight > MAX_ATLAS_AREA) {
1022
- throw new AutoRigInputError(
1023
- `atlas page ${layout.pageWidth}x${layout.pageHeight} exceeds max area ${MAX_ATLAS_AREA}`
1024
- );
1025
- }
1026
- const dataUri = await renderAtlasToDataUri(crops, layout, quantizeColors);
1027
- if (dataUri.length > MAX_OUTPUT_BYTES) {
1057
+ const losslessCrop = quantizeColors === void 0 ? void 0 : crops.find((c) => c.id === LOSSLESS_PAGE_ROLE);
1058
+ const pages = losslessCrop === void 0 ? [await renderAtlasPage(crops, quantizeColors)] : [
1059
+ await renderAtlasPage(
1060
+ crops.filter((c) => c !== losslessCrop),
1061
+ quantizeColors
1062
+ ),
1063
+ await renderAtlasPage([losslessCrop], void 0)
1064
+ ];
1065
+ const atlasBytes = pages.reduce((sum, p) => sum + p.dataUri.length, 0);
1066
+ if (atlasBytes > MAX_OUTPUT_BYTES) {
1028
1067
  throw new AutoRigInputError(
1029
- `atlas data URI ${dataUri.length} bytes exceeds ${MAX_OUTPUT_BYTES}`
1068
+ `atlas data URI ${atlasBytes} bytes exceeds ${MAX_OUTPUT_BYTES}`
1030
1069
  );
1031
1070
  }
1032
- const partTextureAssignments = crops.map((crop) => {
1033
- const placement2 = layout.placements.find((p) => p.id === crop.id);
1034
- if (placement2 === void 0) {
1035
- throw new Error(`auto-rig: no atlas placement for "${crop.id}"`);
1036
- }
1037
- return {
1038
- partId: crop.id,
1039
- uv: (0, import_editor2.uvRectFor)(placement2, {
1040
- width: layout.pageWidth,
1041
- height: layout.pageHeight
1042
- })
1043
- };
1071
+ const partTextureAssignments = pages.flatMap(
1072
+ ({ crops: pageCrops, layout }) => pageCrops.map((crop) => {
1073
+ const placement2 = layout.placements.find((p) => p.id === crop.id);
1074
+ if (placement2 === void 0) {
1075
+ throw new Error(`auto-rig: no atlas placement for "${crop.id}"`);
1076
+ }
1077
+ return {
1078
+ partId: crop.id,
1079
+ uv: (0, import_editor2.uvRectFor)(placement2, {
1080
+ width: layout.pageWidth,
1081
+ height: layout.pageHeight
1082
+ })
1083
+ };
1084
+ })
1085
+ );
1086
+ doc.applyAtlas({
1087
+ textures: [{ source: pages[0].dataUri }],
1088
+ partTextureAssignments
1044
1089
  });
1045
- doc.applyAtlas({ textures: [{ source: dataUri }], partTextureAssignments });
1046
- const finalModel = (0, import_format.parseIkiModel)(doc.getModel());
1090
+ let patched = doc.getModel();
1091
+ if (losslessCrop !== void 0) {
1092
+ patched = structuredClone(patched);
1093
+ patched.textures = pages.map((p) => ({ source: p.dataUri }));
1094
+ patched.parts.find((p) => p.id === losslessCrop.id).texture.index = 1;
1095
+ }
1096
+ const finalModel = (0, import_format.parseIkiModel)(patched);
1047
1097
  writeFileAtomic(outPath, JSON.stringify(finalModel));
1048
1098
  return {
1049
1099
  ok: true,
1050
1100
  path: outPath,
1051
1101
  canvas: { width: canvasW, height: canvasH },
1052
1102
  partCount: finalModel.parts.length,
1053
- atlasBytes: dataUri.length,
1103
+ atlasBytes,
1054
1104
  ...headHalfWidth === void 0 ? {} : { headHalfWidth },
1055
1105
  headHalfWidthApplied,
1056
1106
  ...headEdges === void 0 ? {} : { headEdges },
1057
1107
  ...strandEdges === void 0 ? {} : { strandEdges },
1108
+ ...noseCore === void 0 ? {} : { noseCore },
1058
1109
  ...turn === void 0 ? {} : { turn }
1059
1110
  };
1060
1111
  } catch (err) {
@@ -1160,6 +1211,16 @@ async function layerStats(filePath) {
1160
1211
  };
1161
1212
  }
1162
1213
  var pct = (v) => `${(v * 100).toFixed(0)}%`;
1214
+ function noseSpeckWarning({ core, part }, source) {
1215
+ const [found, consequence] = source === "compose" ? [
1216
+ `the source part's dense core is ${core.w}x${core.h} in its ${part.w}x${part.h} trimmed part`,
1217
+ `compose_layers_from_parts sized and placed the whole part instead \u2014 layout.nose's w and h now size it, feather included`
1218
+ ] : [
1219
+ `the layer's dense core is ${core.w}x${core.h} in its ${part.w}x${part.h} crop`,
1220
+ `auto_rig_from_layers turns and tilts the nose about its whole crop instead`
1221
+ ];
1222
+ return `nose: ${found} \u2014 under ${SPECK_CORE_FRACTION} of its width or height, a speck: a lone nostril mark or highlight at alpha >= 128, not the drawing, so ${consequence}. Regenerate the nose with its shading painted denser, so the drawing itself reaches alpha 128. Billed.`;
1223
+ }
1163
1224
  async function lashOffSclera(eyePath, lashPath) {
1164
1225
  const eye = await decodePng(eyePath);
1165
1226
  const lash = await decodePng(lashPath);
@@ -1175,7 +1236,7 @@ async function lashOffSclera(eyePath, lashPath) {
1175
1236
  }
1176
1237
  return { opaque, off };
1177
1238
  }
1178
- async function measureDir(absDir) {
1239
+ async function measureDir(absDir, noseSpeck) {
1179
1240
  const files = import_node_fs2.default.readdirSync(absDir).filter((f) => f.endsWith(".png") && f !== "preview.png").sort();
1180
1241
  if (files.length === 0) {
1181
1242
  throw new AutoRigInputError(`no role layers in ${absDir}`);
@@ -1281,6 +1342,17 @@ async function measureDir(absDir) {
1281
1342
  }
1282
1343
  }
1283
1344
  }
1345
+ if (noseSpeck !== void 0) {
1346
+ warnings.push(noseSpeckWarning(noseSpeck, "compose"));
1347
+ } else if (layers.nose !== void 0) {
1348
+ const nose = await decodePng(import_node_path4.default.join(absDir, "nose.png"));
1349
+ const core = denseCoreOf(nose.rgba, nose.width, nose.height);
1350
+ if (core !== null) {
1351
+ const crop = detectAlphaBbox(nose.rgba, nose.width, nose.height);
1352
+ if (isSpeckCore(core, crop))
1353
+ warnings.push(noseSpeckWarning({ core, part: crop }, "layer"));
1354
+ }
1355
+ }
1284
1356
  for (const [role, why] of [
1285
1357
  ["body", "without it the character reads as a floating head on head-turn"],
1286
1358
  ["hair_back", "without it the silhouette is flat behind the face"],
@@ -1341,6 +1413,7 @@ var IRIS_W = Math.round(EYE_W * 0.5625);
1341
1413
  var EYEWHITE_SRC = "eyewhite.png";
1342
1414
  var EYE_LASH_LUMA = 120;
1343
1415
  var LASH_KEEP_FRACTION = 0.5;
1416
+ var NOSE_TIP_AT = 0.86;
1344
1417
  var DEFAULT_LAYOUT = {
1345
1418
  // Back hair and body sit behind the face. Both are OPTIONAL: a parts dir
1346
1419
  // without them still composes (head-only character).
@@ -1352,9 +1425,11 @@ var DEFAULT_LAYOUT = {
1352
1425
  face: { src: "face.png", cx: 550, cy: 475, w: 400 },
1353
1426
  // The nose, drawn on its own (the face is drawn without one): the auto-rig
1354
1427
  // leads the head turn with it and keys the other features' slide on its
1355
- // presence, so without it the features stay on the face plate. Sits between
1356
- // the eyes and above the mouth. OPTIONAL: a parts dir without it composes.
1357
- nose: { src: "nose.png", cx: 550, cy: 586, w: 40, optional: true },
1428
+ // presence, so without it the features stay on the face plate. `w` is the
1429
+ // width of its dense core, and it has no `cy`: its row comes from the tip
1430
+ // rule (NOSE_TIP_AT), between the eyes and the mouth, so it follows them
1431
+ // when they are retuned. OPTIONAL: a parts dir without it composes.
1432
+ nose: { src: "nose.png", cx: 550, w: 40, optional: true },
1358
1433
  mouth: { src: "mouth.png", cx: 550, cy: 619, w: 68 },
1359
1434
  // Cross-fades with `mouth` on ParamMouthOpenY (opacity, not scaleY) once the
1360
1435
  // auto-rig sees both roles. Same cx/w as `mouth` so the lip width matches;
@@ -1425,7 +1500,7 @@ function resolveLayout(overrides) {
1425
1500
  );
1426
1501
  }
1427
1502
  if (override === void 0) continue;
1428
- const next = { ...resolved[role] };
1503
+ const set = {};
1429
1504
  for (const field of ["cx", "cy"]) {
1430
1505
  const value = override[field];
1431
1506
  if (value === void 0) continue;
@@ -1434,7 +1509,7 @@ function resolveLayout(overrides) {
1434
1509
  `layout.${role}.${field} must be a finite number, got ${String(value)}`
1435
1510
  );
1436
1511
  }
1437
- next[field] = value;
1512
+ set[field] = value;
1438
1513
  }
1439
1514
  for (const field of ["w", "h"]) {
1440
1515
  const value = override[field];
@@ -1444,9 +1519,11 @@ function resolveLayout(overrides) {
1444
1519
  `layout.${role}.${field} must be an integer in 1..${CANVAS}, got ${String(value)}`
1445
1520
  );
1446
1521
  }
1447
- next[field] = value;
1522
+ set[field] = value;
1448
1523
  }
1449
- resolved[role] = next;
1524
+ const key = role;
1525
+ if (key === "nose") resolved.nose = { ...resolved.nose, ...set };
1526
+ else resolved[key] = { ...resolved[key], ...set };
1450
1527
  }
1451
1528
  return resolved;
1452
1529
  }
@@ -1471,6 +1548,28 @@ function keyWhiteToAlpha(rgba) {
1471
1548
  }
1472
1549
  return keyed;
1473
1550
  }
1551
+ async function coreAndBoundsOf(png) {
1552
+ const { data, info } = await (0, import_sharp2.default)(png).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
1553
+ return {
1554
+ core: denseCoreOf(data, info.width, info.height),
1555
+ bounds: { x: 0, y: 0, w: info.width, h: info.height }
1556
+ };
1557
+ }
1558
+ async function noseSizingOf(png) {
1559
+ const { core, bounds } = await coreAndBoundsOf(png);
1560
+ if (core === null) return { box: bounds, whole: true };
1561
+ if (isSpeckCore(core, bounds)) {
1562
+ return {
1563
+ box: bounds,
1564
+ whole: true,
1565
+ speck: {
1566
+ core: { w: core.w, h: core.h },
1567
+ part: { w: bounds.w, h: bounds.h }
1568
+ }
1569
+ };
1570
+ }
1571
+ return { box: core, whole: false };
1572
+ }
1474
1573
  async function partBuffer(role, cfg, partsDir, inMemory, flipSource) {
1475
1574
  let img;
1476
1575
  if (inMemory !== void 0) {
@@ -1491,33 +1590,58 @@ async function partBuffer(role, cfg, partsDir, inMemory, flipSource) {
1491
1590
  if (!cfg.noTrim) img = img.trim({ threshold: 12 });
1492
1591
  if ((cfg.mirror ?? false) !== flipSource) img = img.flop();
1493
1592
  const trimmed = await img.png().toBuffer({ resolveWithObject: true });
1494
- if (cfg.h === void 0) {
1495
- const scaledH = Math.round(
1496
- trimmed.info.height * cfg.w / trimmed.info.width
1593
+ const { width: trimmedW, height: trimmedH } = trimmed.info;
1594
+ const nose = role === "nose" ? await noseSizingOf(trimmed.data) : void 0;
1595
+ const sized = nose?.box ?? { x: 0, y: 0, w: trimmedW, h: trimmedH };
1596
+ const width = Math.round(trimmedW * cfg.w / sized.w);
1597
+ const height = cfg.h === void 0 ? Math.round(trimmedH * width / trimmedW) : Math.round(trimmedH * cfg.h / sized.h);
1598
+ if (width > CANVAS || height > CANVAS) {
1599
+ const field = width > CANVAS || cfg.h === void 0 ? "w" : "h";
1600
+ throw new AutoRigInputError(
1601
+ `layout.${role}.${field}: resized part ${width}x${height} exceeds the ${CANVAS} canvas`
1497
1602
  );
1498
- if (scaledH > CANVAS) {
1499
- throw new AutoRigInputError(
1500
- `layout.${role}.w: resized part ${cfg.w}x${scaledH} exceeds the ${CANVAS} canvas`
1501
- );
1502
- }
1503
1603
  }
1504
- const resized = await (0, import_sharp2.default)(trimmed.data).resize(
1505
- cfg.h === void 0 ? { width: cfg.w } : { width: cfg.w, height: cfg.h, fit: "fill" }
1506
- ).png().toBuffer({ resolveWithObject: true });
1604
+ const resized = await (0, import_sharp2.default)(trimmed.data).resize(cfg.h === void 0 ? { width } : { width, height, fit: "fill" }).png().toBuffer({ resolveWithObject: true });
1507
1605
  return {
1508
1606
  buf: resized.data,
1509
1607
  w: resized.info.width,
1510
- h: resized.info.height
1608
+ h: resized.info.height,
1609
+ nose
1511
1610
  };
1512
1611
  }
1513
- function placement(role, cfg, w, h) {
1514
- const left = Math.round(cfg.cx - w / 2);
1515
- const top = Math.round(cfg.cy - h / 2);
1612
+ function assertOnCanvas(source, w, h, left, top) {
1516
1613
  if (left + w <= 0 || top + h <= 0 || left >= CANVAS || top >= CANVAS) {
1517
1614
  throw new AutoRigInputError(
1518
- `layout.${role}.cx/cy: the placed part (${w}x${h} at ${left},${top}) falls entirely outside the ${CANVAS} canvas`
1615
+ `${source}: the placed part (${w}x${h} at ${left},${top}) falls entirely outside the ${CANVAS} canvas`
1519
1616
  );
1520
1617
  }
1618
+ }
1619
+ function placement(role, cfg, w, h) {
1620
+ const left = Math.round(cfg.cx - w / 2);
1621
+ const top = Math.round(cfg.cy - h / 2);
1622
+ assertOnCanvas(`layout.${role}.cx/cy`, w, h, left, top);
1623
+ return { left, top };
1624
+ }
1625
+ function nosePlacement(layout, box, w, h) {
1626
+ const { cx, cy } = layout.nose;
1627
+ const left = Math.round(cx - (box.x + box.w / 2));
1628
+ let top;
1629
+ if (cy !== void 0) {
1630
+ top = Math.round(cy - (box.y + box.h / 2));
1631
+ } else {
1632
+ const eyeRow = (layout.eye_L.cy + layout.eye_R.cy) / 2;
1633
+ const tipRow = Math.round(
1634
+ eyeRow + NOSE_TIP_AT * (layout.mouth.cy - eyeRow)
1635
+ );
1636
+ top = tipRow - (box.y + box.h - 1);
1637
+ }
1638
+ assertOnCanvas(
1639
+ cy !== void 0 ? "layout.nose.cx/cy" : "layout.nose.cx/cy (cy unset: its tip row comes from layout.eye_L/eye_R/mouth.cy)",
1640
+ w,
1641
+ h,
1642
+ left,
1643
+ top
1644
+ );
1521
1645
  return { left, top };
1522
1646
  }
1523
1647
  function blankCanvas() {
@@ -1594,6 +1718,7 @@ async function composeLayersFromParts(input) {
1594
1718
  ]);
1595
1719
  const placed = [];
1596
1720
  const skipped = [];
1721
+ let noseSpeck;
1597
1722
  for (const role of ORDER) {
1598
1723
  const cfg = layout[role];
1599
1724
  const inMemory = splitSources.get(cfg.src);
@@ -1609,7 +1734,16 @@ async function composeLayersFromParts(input) {
1609
1734
  skipped.push(role);
1610
1735
  continue;
1611
1736
  }
1612
- const { left, top } = placement(role, cfg, part.w, part.h);
1737
+ let left;
1738
+ let top;
1739
+ if (role === "nose") {
1740
+ noseSpeck = part.nose?.speck;
1741
+ const { core, bounds } = await coreAndBoundsOf(part.buf);
1742
+ const box = part.nose?.whole ? bounds : core ?? bounds;
1743
+ ({ left, top } = nosePlacement(layout, box, part.w, part.h));
1744
+ } else {
1745
+ ({ left, top } = placement(role, layout[role], part.w, part.h));
1746
+ }
1613
1747
  if (role === "lash_L" || role === "lash_R") {
1614
1748
  const eyeRole = role === "lash_L" ? "eye_L" : "eye_R";
1615
1749
  const eye = placed.find((p) => p.role === eyeRole);
@@ -1656,7 +1790,7 @@ async function composeLayersFromParts(input) {
1656
1790
  layers,
1657
1791
  skipped,
1658
1792
  preview: previewPath,
1659
- measure: await measureDir(outDir)
1793
+ measure: await measureDir(outDir, noseSpeck)
1660
1794
  };
1661
1795
  } catch (err) {
1662
1796
  if (err instanceof AutoRigInputError)
@@ -1669,7 +1803,7 @@ async function composeLayersFromParts(input) {
1669
1803
  var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
1670
1804
  var import_zod = require("zod");
1671
1805
  function createIkiMcpServer() {
1672
- const server = new import_mcp.McpServer({ name: "iki", version: "0.10.0" });
1806
+ const server = new import_mcp.McpServer({ name: "iki", version: "0.11.1" });
1673
1807
  server.registerTool(
1674
1808
  "validate_iki",
1675
1809
  {
@@ -1748,7 +1882,7 @@ function createIkiMcpServer() {
1748
1882
  "Output .iki path (resolved against cwd; parent dir must exist)."
1749
1883
  ),
1750
1884
  quantizeColors: import_zod.z.number().int().min(2).max(256).optional().describe(
1751
- "Palette-quantize the atlas PNG to this many colours (2..256). Flat-shaded art keeps its look at 256 and the model shrinks to about a quarter; omit for lossless."
1885
+ "Palette-quantize the atlas PNG to this many colours (2..256). Flat-shaded art keeps its look at 256 and the model shrinks to about a quarter; omit for lossless. A `nose` layer stays lossless on a second atlas page of its own, since the palette rims a soft-shaded nose's feather; the nose's own lossless page adds a content-dependent amount, since splitting it off also changes how page 0 re-quantizes without it; the result's `atlasBytes` sums both pages."
1752
1886
  ),
1753
1887
  turnTargets: import_zod.z.object({
1754
1888
  eyeShift: import_zod.z.number().optional(),
@@ -1757,7 +1891,7 @@ function createIkiMcpServer() {
1757
1891
  noseShift: import_zod.z.number().optional(),
1758
1892
  mouthShift: import_zod.z.number().optional()
1759
1893
  }).optional().describe(
1760
- "What the head turn is fitted to, as `measure_turn_reference` reports it off a front/turned reference pair. `eyeShift` (and the optional `noseShift`/`mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width \u2014 a MAGNITUDE: the rig turns both ways, so the sign is ignored. `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest, `silhouetteRatio` the head's half-width turned over at rest. The half-width the shifts are fractions of is measured off the layers themselves, so it is not an input. An omitted field falls back to a default, which is clamped to what this layer set can do, and so is a passed `eyeShift` past the room the art leaves the far eye \u2014 the face plate's edge, or the bangs' side strand over the far iris (the result's `turn.clamped` lists every clamped field, `turn.achieved` what the rig reaches, and `turn.strandOverlap` how much of a far iris the bangs still cover). Any other passed target this layer set cannot reach, including an `eyeShift` smaller than the slide the face's own turn already gives the eyes, comes back as `INVALID: \u2026` naming the field and the attainable range, since it is a measurement rather than a preference."
1894
+ "What the head turn is fitted to, as `measure_turn_reference` reports it off a front/turned reference pair. `eyeShift` (and the optional `noseShift`/`mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width \u2014 a MAGNITUDE: the rig turns both ways, so the sign is ignored. `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest, `silhouetteRatio` the head's half-width turned over at rest. The half-width the shifts are fractions of is measured off the layers themselves, so it is not an input. An omitted field falls back to a default, which is clamped to what this layer set can do, and so is a passed `eyeShift` past the room the art leaves the far eye \u2014 the face plate's edge, or the bangs' side strand over the far iris (the result's `turn.clamped` lists every clamped field, `turn.achieved` what the rig reaches, and `turn.strandOverlap` how much of a far iris the bangs still cover). Any other passed target this layer set cannot reach, including an `eyeShift` smaller than the slide the face's own turn already gives the eyes, comes back as `INVALID: \u2026` naming the field and the attainable range, since it is a measurement rather than a preference \u2014 except a passed `silhouetteRatio` when no ratio in its whole accepted range renders on this layer set: that refusal reads `\u2026 is unreachable for this layer set, and so is every silhouetteRatio in [0.5, 1.5]` and names no range, so leave the field out, since a defaulted one clamps."
1761
1895
  )
1762
1896
  }
1763
1897
  },
@@ -1779,7 +1913,7 @@ function createIkiMcpServer() {
1779
1913
  server.registerTool(
1780
1914
  "compose_layers_from_parts",
1781
1915
  {
1782
- 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 rig does not slide the features on the head turn.",
1916
+ 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 rig does not slide the features on the head turn. 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.",
1783
1917
  inputSchema: {
1784
1918
  partsDir: import_zod.z.string().describe(
1785
1919
  "Directory of the source part PNGs (resolved against cwd)."
@@ -1796,7 +1930,7 @@ function createIkiMcpServer() {
1796
1930
  h: import_zod.z.number()
1797
1931
  }).partial()
1798
1932
  ).optional().describe(
1799
- "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."
1933
+ "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."
1800
1934
  ),
1801
1935
  mirrorParts: import_zod.z.array(import_zod.z.string()).optional().describe(
1802
1936
  "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."
@@ -1824,7 +1958,7 @@ function createIkiMcpServer() {
1824
1958
  server.registerTool(
1825
1959
  "measure_layers",
1826
1960
  {
1827
- description: "Reports read-only geometry checks over an already-composed layers directory \u2014 the same checks compose_layers_from_parts returns inline (iris ratio/offset, eye aspect, cropped/cut edges, missing optional roles). Use to re-check a layers dir without recomposing.",
1961
+ description: "Reports read-only geometry checks over an already-composed layers directory \u2014 the same checks compose_layers_from_parts returns inline (iris ratio/offset, eye aspect, cropped/cut edges, a nose whose dense core is a speck of its crop, missing optional roles). Use to re-check a layers dir without recomposing. Its nose check reads the composed file against its crop, which is what auto_rig_from_layers acts on. Compose's inline report does the same unless the composer found the source part's core a speck, which it reports instead \u2014 so a speck that resampling erased from the layer shows there and not here.",
1828
1962
  inputSchema: {
1829
1963
  layersDir: import_zod.z.string().describe(
1830
1964
  "Directory of role-named layer PNGs (resolved against cwd)."