@fleox/agentphoto 0.0.0-stage → 0.1.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/LICENSE +143 -0
- package/README.md +334 -2
- package/changelog.json +35 -0
- package/dist/agent/agentphoto.mjs +3354 -0
- package/dist/agent/chunks/assets-BZVUUHjg.mjs +151 -0
- package/dist/agent/chunks/catalog-DIZ5dnH5.mjs +953 -0
- package/dist/agent/chunks/context-CNpRboiC.mjs +75 -0
- package/dist/agent/chunks/doc-D_v_yE07.mjs +512 -0
- package/dist/agent/chunks/export-aNJcPNbJ.mjs +140 -0
- package/dist/agent/chunks/geometry-BKIhUO9x.mjs +315 -0
- package/dist/agent/chunks/ids-DAmiNwd9.mjs +28 -0
- package/dist/agent/chunks/imageInfo-QxAfu8R6.mjs +173 -0
- package/dist/agent/chunks/jsonPath-B7vAPXsi.mjs +155 -0
- package/dist/agent/chunks/library-C_EXiphj.mjs +16 -0
- package/dist/agent/chunks/models-CxrjmA0W.mjs +387 -0
- package/dist/agent/chunks/output-3F12IR9O.mjs +130 -0
- package/dist/agent/chunks/package-Cu1GyvKL.mjs +71 -0
- package/dist/agent/chunks/project-o1DH2nOw.mjs +200 -0
- package/dist/agent/chunks/rolldown-runtime-CMQPkI6i.mjs +18 -0
- package/dist/agent/chunks/server-I0trf5tM.mjs +241 -0
- package/dist/agent/chunks/templates-BJ2me4XZ.mjs +61 -0
- package/dist/agent/chunks/tools-DKaAPEV5.mjs +1419 -0
- package/dist/agent/chunks/tree-D8qZKPAb.mjs +127 -0
- package/dist/agent/chunks/types-DOK7tAAu.mjs +30 -0
- package/dist/agent/chunks/validate-CXPFB-i6.mjs +936 -0
- package/dist/engine/agentphoto-engine.js +2833 -0
- package/dist/engine/chunks/transformers.web-D5Vsg6-A.js +18494 -0
- package/dist/engine/ort/ort-wasm-simd-threaded.asyncify.mjs +124 -0
- package/dist/engine/ort/ort-wasm-simd-threaded.asyncify.wasm +0 -0
- package/dist/studio/assets/index-CmOzzoXv.css +1 -0
- package/dist/studio/assets/index-D3Iw5q8v.js +28 -0
- package/dist/studio/assets/transformers.web-osEqoMhg.js +38 -0
- package/dist/studio/index.html +15 -0
- package/dist/studio/library/LICENSES.md +57 -0
- package/dist/studio/library/fonts/OFL.txt +90 -0
- package/dist/studio/library/fonts/anton/anton-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/anton/anton-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/archivo-black/archivo-black-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/archivo-black/archivo-black-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/bebas-neue/bebas-neue-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/bebas-neue/bebas-neue-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/caveat/caveat-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/dm-serif/dm-serif-display-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/dm-serif/dm-serif-display-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/inter/inter-latin-ext-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/lobster/lobster-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/lobster/lobster-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/montserrat/montserrat-latin-ext-800-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/oswald/oswald-latin-ext-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/pacifico/pacifico-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/pacifico/pacifico-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-400-italic.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-ext-400-italic.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/playfair/playfair-display-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-ext-600-normal.woff2 +0 -0
- package/dist/studio/library/fonts/poppins/poppins-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/LICENSE.txt +202 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-ext-400-normal.woff2 +0 -0
- package/dist/studio/library/fonts/roboto-slab/roboto-slab-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-500-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-700-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-ext-500-normal.woff2 +0 -0
- package/dist/studio/library/fonts/space-grotesk/space-grotesk-latin-ext-700-normal.woff2 +0 -0
- package/dist/studio/library/images/city.jpg +0 -0
- package/dist/studio/library/images/food.jpg +0 -0
- package/dist/studio/library/images/landscape.jpg +0 -0
- package/dist/studio/library/images/pet.jpg +0 -0
- package/dist/studio/library/images/portrait.jpg +0 -0
- package/dist/studio/library/images/product.jpg +0 -0
- package/dist/studio/library/index.json +752 -0
- package/dist/studio/templates/avatar-pro/assets/masks/photo-cutout.png +0 -0
- package/dist/studio/templates/avatar-pro/photo.json +13 -0
- package/dist/studio/templates/avatar-pro/preview.jpg +0 -0
- package/dist/studio/templates/avatar-pro/template.json +1 -0
- package/dist/studio/templates/index.json +1 -0
- package/dist/studio/templates/og-article/photo.json +13 -0
- package/dist/studio/templates/og-article/preview.jpg +0 -0
- package/dist/studio/templates/og-article/template.json +1 -0
- package/dist/studio/templates/polaroid/photo.json +13 -0
- package/dist/studio/templates/polaroid/preview.jpg +0 -0
- package/dist/studio/templates/polaroid/template.json +1 -0
- package/dist/studio/templates/product-white/assets/masks/photo-cutout.png +0 -0
- package/dist/studio/templates/product-white/photo.json +72 -0
- package/dist/studio/templates/product-white/preview.jpg +0 -0
- package/dist/studio/templates/product-white/template.json +1 -0
- package/dist/studio/templates/sale-banner/photo.json +125 -0
- package/dist/studio/templates/sale-banner/preview.jpg +0 -0
- package/dist/studio/templates/sale-banner/template.json +1 -0
- package/dist/studio/templates/social-quote/photo.json +13 -0
- package/dist/studio/templates/social-quote/preview.jpg +0 -0
- package/dist/studio/templates/social-quote/template.json +1 -0
- package/dist/studio/templates/story-promo/photo.json +13 -0
- package/dist/studio/templates/story-promo/preview.jpg +0 -0
- package/dist/studio/templates/story-promo/template.json +1 -0
- package/dist/studio/templates/youtube-thumbnail/photo.json +12 -0
- package/dist/studio/templates/youtube-thumbnail/preview.jpg +0 -0
- package/dist/studio/templates/youtube-thumbnail/template.json +1 -0
- package/library/LICENSES.md +57 -0
- package/library/fonts/OFL.txt +90 -0
- package/library/fonts/anton/anton-latin-400-normal.woff2 +0 -0
- package/library/fonts/anton/anton-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/archivo-black/archivo-black-latin-400-normal.woff2 +0 -0
- package/library/fonts/archivo-black/archivo-black-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/bebas-neue/bebas-neue-latin-400-normal.woff2 +0 -0
- package/library/fonts/bebas-neue/bebas-neue-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-400-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-700-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/caveat/caveat-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/dm-serif/dm-serif-display-latin-400-normal.woff2 +0 -0
- package/library/fonts/dm-serif/dm-serif-display-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-400-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-600-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-700-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-800-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-600-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/inter/inter-latin-ext-800-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-400-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-700-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/jetbrains-mono/jetbrains-mono-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/lobster/lobster-latin-400-normal.woff2 +0 -0
- package/library/fonts/lobster/lobster-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-400-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-700-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-800-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/montserrat/montserrat-latin-ext-800-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-400-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-600-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/oswald/oswald-latin-ext-600-normal.woff2 +0 -0
- package/library/fonts/pacifico/pacifico-latin-400-normal.woff2 +0 -0
- package/library/fonts/pacifico/pacifico-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-400-italic.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-400-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-700-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-ext-400-italic.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/playfair/playfair-display-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-400-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-600-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-700-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-ext-600-normal.woff2 +0 -0
- package/library/fonts/poppins/poppins-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/LICENSE.txt +202 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-400-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-700-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-ext-400-normal.woff2 +0 -0
- package/library/fonts/roboto-slab/roboto-slab-latin-ext-700-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-500-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-700-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-ext-500-normal.woff2 +0 -0
- package/library/fonts/space-grotesk/space-grotesk-latin-ext-700-normal.woff2 +0 -0
- package/library/images/city.jpg +0 -0
- package/library/images/food.jpg +0 -0
- package/library/images/landscape.jpg +0 -0
- package/library/images/pet.jpg +0 -0
- package/library/images/portrait.jpg +0 -0
- package/library/images/product.jpg +0 -0
- package/library/index.json +752 -0
- package/package.json +65 -4
- package/plugin/.claude-plugin/plugin.json +19 -0
- package/plugin/skills/agentphoto-cutout/SKILL.md +137 -0
- package/plugin/skills/agentphoto-design/SKILL.md +104 -0
- package/plugin/skills/agentphoto-design/references/layouts.md +60 -0
- package/plugin/skills/agentphoto-publish/SKILL.md +79 -0
- package/plugin/skills/agentphoto-retouch/SKILL.md +144 -0
- package/plugin/skills/agentphoto-retouch/references/filters.md +51 -0
- package/plugin/skills/agentphoto-retouch/references/patch-ops.md +67 -0
- package/plugin/skills/agentphoto-retouch/references/recipes.md +129 -0
- package/templates/avatar-pro/assets/masks/photo-cutout.png +0 -0
- package/templates/avatar-pro/photo.json +13 -0
- package/templates/avatar-pro/preview.jpg +0 -0
- package/templates/avatar-pro/template.json +1 -0
- package/templates/og-article/photo.json +13 -0
- package/templates/og-article/preview.jpg +0 -0
- package/templates/og-article/template.json +1 -0
- package/templates/polaroid/photo.json +13 -0
- package/templates/polaroid/preview.jpg +0 -0
- package/templates/polaroid/template.json +1 -0
- package/templates/product-white/assets/masks/photo-cutout.png +0 -0
- package/templates/product-white/photo.json +72 -0
- package/templates/product-white/preview.jpg +0 -0
- package/templates/product-white/template.json +1 -0
- package/templates/sale-banner/photo.json +125 -0
- package/templates/sale-banner/preview.jpg +0 -0
- package/templates/sale-banner/template.json +1 -0
- package/templates/social-quote/photo.json +13 -0
- package/templates/social-quote/preview.jpg +0 -0
- package/templates/social-quote/template.json +1 -0
- package/templates/story-promo/photo.json +13 -0
- package/templates/story-promo/preview.jpg +0 -0
- package/templates/story-promo/template.json +1 -0
- package/templates/youtube-thumbnail/photo.json +12 -0
- package/templates/youtube-thumbnail/preview.jpg +0 -0
- package/templates/youtube-thumbnail/template.json +1 -0
|
@@ -0,0 +1,1419 @@
|
|
|
1
|
+
import { createRequire as __apRequire } from "node:module";
|
|
2
|
+
import { fileURLToPath as __apPath } from "node:url";
|
|
3
|
+
import { dirname as __apDirname } from "node:path";
|
|
4
|
+
__apRequire(import.meta.url);
|
|
5
|
+
const __filename = __apPath(import.meta.url);
|
|
6
|
+
__apDirname(__filename);
|
|
7
|
+
import { c as closest, i as validateDoc, u as listOptions } from "./validate-CXPFB-i6.mjs";
|
|
8
|
+
import { n as KB, r as OpsError } from "./context-CNpRboiC.mjs";
|
|
9
|
+
import { c as filterDef, o as MODELS, r as DEFAULT_MODELS, s as SIZE_PRESETS, t as CATALOG } from "./catalog-DIZ5dnH5.mjs";
|
|
10
|
+
import { a as readAssetManifest, i as missingFiles, o as removeAsset, r as listAssets, t as addAsset } from "./assets-BZVUUHjg.mjs";
|
|
11
|
+
import { n as imageInfo } from "./imageInfo-QxAfu8R6.mjs";
|
|
12
|
+
import { n as isLibraryRef, r as libraryId, t as findLibraryAsset } from "./library-C_EXiphj.mjs";
|
|
13
|
+
import { a as patchDoc, i as getDoc, n as applyDoc, o as summarizeDoc, t as PATCH_OPS } from "./doc-D_v_yE07.mjs";
|
|
14
|
+
import { o as layerBox, t as ANCHORS } from "./geometry-BKIhUO9x.mjs";
|
|
15
|
+
import { l as walkLayers, n as findLayer, r as flattenLayers, s as referencedFiles, t as allLayerIds } from "./tree-D8qZKPAb.mjs";
|
|
16
|
+
import { a as loadProject, i as loadManifest, r as createProject } from "./project-o1DH2nOw.mjs";
|
|
17
|
+
import { n as listTemplates, t as findTemplate } from "./templates-BJ2me4XZ.mjs";
|
|
18
|
+
import { i as uniqueId } from "./ids-DAmiNwd9.mjs";
|
|
19
|
+
import { i as isImageMask } from "./types-DOK7tAAu.mjs";
|
|
20
|
+
import { r as planExport, t as IMAGE_FORMATS } from "./export-aNJcPNbJ.mjs";
|
|
21
|
+
// src/ops/check.ts
|
|
22
|
+
async function checkProject(fs, opts = {}) {
|
|
23
|
+
const doc = await getDoc(fs);
|
|
24
|
+
const issues = [...validateDoc(doc)];
|
|
25
|
+
const add = (severity, code, path, message, hint) => issues.push({
|
|
26
|
+
severity,
|
|
27
|
+
code,
|
|
28
|
+
path,
|
|
29
|
+
message,
|
|
30
|
+
...hint ? { hint } : {}
|
|
31
|
+
});
|
|
32
|
+
for (const m of await missingFiles(fs, doc)) add("error", "missing-file", `layers#${m.by}.${m.role === "src" ? "src" : "mask.src"}`, `${m.path} does not exist in the project.`, "Import it with assets_add, or point the layer at an existing file (assets_list).");
|
|
33
|
+
const sizes = {};
|
|
34
|
+
const srcs = /* @__PURE__ */ new Set();
|
|
35
|
+
walkLayers(doc.layers, (l) => {
|
|
36
|
+
if (l.type === "image") srcs.add(l.src);
|
|
37
|
+
});
|
|
38
|
+
for (const src of srcs) {
|
|
39
|
+
if (isLibraryRef(src)) {
|
|
40
|
+
const a = findLibraryAsset(opts.library, libraryId(src));
|
|
41
|
+
if (opts.library && !a) add("error", "unknown-library", src, `Unknown library image "${src}".`, "library_list lists the built-in images (reference them as \"library:<id>\").");
|
|
42
|
+
if (a?.width && a.height) sizes[src] = {
|
|
43
|
+
width: a.width,
|
|
44
|
+
height: a.height
|
|
45
|
+
};
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const info = imageInfo(await fs.readBytes(src));
|
|
50
|
+
if (info) sizes[src] = {
|
|
51
|
+
width: info.width,
|
|
52
|
+
height: info.height
|
|
53
|
+
};
|
|
54
|
+
else add("error", "bad-image", src, `${src} is not a readable image.`);
|
|
55
|
+
} catch {}
|
|
56
|
+
}
|
|
57
|
+
const W = doc.canvas.width;
|
|
58
|
+
const H = doc.canvas.height;
|
|
59
|
+
let layers = 0;
|
|
60
|
+
let cost = 0;
|
|
61
|
+
const mp = W * H / 1e6;
|
|
62
|
+
walkLayers(doc.layers, (l) => {
|
|
63
|
+
layers++;
|
|
64
|
+
const p = `layers#${l.id}`;
|
|
65
|
+
for (const f of l.filters ?? []) if (f.enabled !== false) cost += (filterDef(f.type)?.cost ?? 1) * mp;
|
|
66
|
+
if (l.visible === false) return;
|
|
67
|
+
if (l.opacity === 0) add("info", "invisible", p, `Layer "${l.id}" has opacity 0 (renders nothing).`);
|
|
68
|
+
const box = layerBox(l, sizes);
|
|
69
|
+
if (box && !l.rotation) {
|
|
70
|
+
if (box.x >= W || box.y >= H || box.x + box.width <= 0 || box.y + box.height <= 0) add("warning", "off-canvas", p, `Layer "${l.id}" is entirely outside the canvas (${Math.round(box.x)}, ${Math.round(box.y)}, ${Math.round(box.width)}×${Math.round(box.height)}).`, `The canvas is ${W}×${H}: move it (x, y) or resize it.`);
|
|
71
|
+
else if (l.type === "text" && !l.width && (box.x + box.width > W + 2 || box.x < -2)) add("info", "text-may-overflow", p, `Text "${l.id}" may run past the canvas edge (estimated width ${Math.round(box.width)} px).`, "Give it a width (it wraps), reduce size, or look at render_preview.");
|
|
72
|
+
}
|
|
73
|
+
if (l.type === "image" && box) {
|
|
74
|
+
const nat = sizes[l.src];
|
|
75
|
+
const src = l.crop ?? nat;
|
|
76
|
+
if (src && nat) {
|
|
77
|
+
const sx = box.width / src.width;
|
|
78
|
+
const sy = box.height / src.height;
|
|
79
|
+
const k = l.fit === "contain" ? Math.min(sx, sy) : Math.max(sx, sy);
|
|
80
|
+
if (k > 1.6) add("warning", "upscaled", p, `Image "${l.id}" is shown at ${Math.round(k * 100)} % of its resolution (${src.width}×${src.height} stretched to ${Math.round(box.width)}×${Math.round(box.height)}): it will look soft.`, "Use a larger source, or a smaller layer / canvas.");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (l.clip && l.type === "group") add("info", "clip-group", p, "Clipping applies to the group as a whole.");
|
|
84
|
+
});
|
|
85
|
+
if (!doc.layers.length) add("warning", "empty", "layers", "The document has no layers.", "layer_add adds one (an image from assets_add, text, shapes…).");
|
|
86
|
+
if (cost > 60) add("info", "heavy", "", `Rendering cost ${Math.round(cost)} (filters × megapixels): previews may be slow.`, "Fewer blur / sharpen / clarity filters, or a smaller canvas.");
|
|
87
|
+
const manifest = await readAssetManifest(fs);
|
|
88
|
+
const used = /* @__PURE__ */ new Set();
|
|
89
|
+
walkLayers(doc.layers, (l) => {
|
|
90
|
+
if (l.type === "image") used.add(l.src);
|
|
91
|
+
if (l.mask && "src" in l.mask) used.add(l.mask.src);
|
|
92
|
+
});
|
|
93
|
+
const licences = manifest.assets.filter((a) => used.has(a.path) && (a.license || a.attribution)).map((a) => ({
|
|
94
|
+
path: a.path,
|
|
95
|
+
license: a.license,
|
|
96
|
+
author: a.author,
|
|
97
|
+
attribution: a.attribution,
|
|
98
|
+
source: a.source
|
|
99
|
+
}));
|
|
100
|
+
for (const src of used) {
|
|
101
|
+
if (!isLibraryRef(src)) continue;
|
|
102
|
+
const a = findLibraryAsset(opts.library, libraryId(src));
|
|
103
|
+
if (a) licences.push({
|
|
104
|
+
path: src,
|
|
105
|
+
license: a.license,
|
|
106
|
+
author: a.author,
|
|
107
|
+
attribution: void 0,
|
|
108
|
+
source: a.source ?? "library"
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
let assetsBytes = 0;
|
|
112
|
+
for (const a of manifest.assets) if (used.has(a.path)) assetsBytes += a.bytes ?? 0;
|
|
113
|
+
const counts = {
|
|
114
|
+
errors: 0,
|
|
115
|
+
warnings: 0,
|
|
116
|
+
infos: 0
|
|
117
|
+
};
|
|
118
|
+
for (const i of issues) counts[i.severity === "error" ? "errors" : i.severity === "warning" ? "warnings" : "infos"]++;
|
|
119
|
+
return {
|
|
120
|
+
ok: counts.errors === 0,
|
|
121
|
+
issues,
|
|
122
|
+
counts,
|
|
123
|
+
canvas: {
|
|
124
|
+
width: W,
|
|
125
|
+
height: H,
|
|
126
|
+
megapixels: Math.round(mp * 100) / 100
|
|
127
|
+
},
|
|
128
|
+
layers,
|
|
129
|
+
cost: Math.round(cost * 10) / 10,
|
|
130
|
+
assetsKb: KB(assetsBytes),
|
|
131
|
+
licences
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
// src/ai/shared.ts
|
|
135
|
+
function libraryReader(ctx) {
|
|
136
|
+
const lib = ctx.host?.library;
|
|
137
|
+
if (!lib) return void 0;
|
|
138
|
+
return async (id) => {
|
|
139
|
+
const a = lib.index.assets.find((x) => x.id === id);
|
|
140
|
+
return a?.path ? lib.read(a.path) : null;
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function str(input, key, required = true) {
|
|
144
|
+
const v = input[key];
|
|
145
|
+
if (typeof v === "string" && v.trim()) return v.trim();
|
|
146
|
+
if (required) throw new OpsError("invalid", `Missing "${key}" (string).`);
|
|
147
|
+
return "";
|
|
148
|
+
}
|
|
149
|
+
function optStr(input, key) {
|
|
150
|
+
const v = input[key];
|
|
151
|
+
return typeof v === "string" && v.trim() ? v.trim() : void 0;
|
|
152
|
+
}
|
|
153
|
+
function bool(input, key, fallback = false) {
|
|
154
|
+
const v = input[key];
|
|
155
|
+
return typeof v === "boolean" ? v : fallback;
|
|
156
|
+
}
|
|
157
|
+
function num(input, key) {
|
|
158
|
+
const v = input[key];
|
|
159
|
+
return typeof v === "number" && Number.isFinite(v) ? v : void 0;
|
|
160
|
+
}
|
|
161
|
+
function compactIssues(issues, max = 40) {
|
|
162
|
+
const order = {
|
|
163
|
+
error: 0,
|
|
164
|
+
warning: 1,
|
|
165
|
+
info: 2
|
|
166
|
+
};
|
|
167
|
+
const sorted = [...issues].sort((a, b) => order[a.severity] - order[b.severity]);
|
|
168
|
+
const shown = sorted.slice(0, max);
|
|
169
|
+
return sorted.length > max ? {
|
|
170
|
+
issues: shown,
|
|
171
|
+
omitted: sorted.length - max
|
|
172
|
+
} : { issues: shown };
|
|
173
|
+
}
|
|
174
|
+
function issueCounts(issues) {
|
|
175
|
+
const c = {
|
|
176
|
+
errors: 0,
|
|
177
|
+
warnings: 0,
|
|
178
|
+
infos: 0
|
|
179
|
+
};
|
|
180
|
+
for (const i of issues) c[i.severity === "error" ? "errors" : i.severity === "warning" ? "warnings" : "infos"]++;
|
|
181
|
+
return c;
|
|
182
|
+
}
|
|
183
|
+
function mutationResult(r, dryRun) {
|
|
184
|
+
const counts = issueCounts(r.issues);
|
|
185
|
+
const status = counts.errors ? "refused" : r.plan.empty ? "unchanged" : dryRun ? "dry-run" : "applied";
|
|
186
|
+
return {
|
|
187
|
+
status,
|
|
188
|
+
applied: r.applied,
|
|
189
|
+
plan: r.plan.empty ? { summary: r.plan.summary } : r.plan,
|
|
190
|
+
counts,
|
|
191
|
+
...compactIssues(r.issues.filter((i) => counts.errors === 0 || i.severity !== "info")),
|
|
192
|
+
hint: status === "refused" ? "Nothing was written: fix the errors (each issue has a hint) and call again." : status === "applied" ? "Saved. Call render_preview and look at the image to verify the result." : status === "dry-run" ? "Dry run: call again without dryRun to write." : "The document already matches: nothing to do."
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function checkInput(schema, input, tool) {
|
|
196
|
+
if (input === void 0 || input === null) input = {};
|
|
197
|
+
if (typeof input !== "object" || Array.isArray(input)) throw new OpsError("invalid", `${tool}: input must be a JSON object.`);
|
|
198
|
+
const obj = input;
|
|
199
|
+
for (const k of schema.required ?? []) if (obj[k] === void 0 || obj[k] === null || obj[k] === "") throw new OpsError("invalid", `${tool}: missing required input "${k}".`, schema.properties?.[k]?.description);
|
|
200
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
201
|
+
const p = schema.properties?.[k];
|
|
202
|
+
if (!p || v === void 0 || v === null || !p.type) continue;
|
|
203
|
+
const types = Array.isArray(p.type) ? p.type : [p.type];
|
|
204
|
+
const actual = Array.isArray(v) ? "array" : typeof v === "number" ? Number.isInteger(v) ? "integer" : "number" : typeof v;
|
|
205
|
+
if (!types.some((t) => t === actual || t === "number" && actual === "integer")) throw new OpsError("invalid", `${tool}: "${k}" must be ${types.join(" or ")}, got ${actual}.`, p.description);
|
|
206
|
+
if (p.enum && !p.enum.includes(v)) throw new OpsError("invalid", `${tool}: "${k}" must be one of ${p.enum.map((x) => JSON.stringify(x)).join(", ")}.`);
|
|
207
|
+
}
|
|
208
|
+
return obj;
|
|
209
|
+
}
|
|
210
|
+
// src/ai/docTools.ts
|
|
211
|
+
var dryRunProp = {
|
|
212
|
+
type: "boolean",
|
|
213
|
+
description: "Preview the plan and issues without writing (default false)."
|
|
214
|
+
};
|
|
215
|
+
var LAYER_SCHEMA = {
|
|
216
|
+
type: "object",
|
|
217
|
+
description: "A layer: { \"id\": \"title\", \"type\": \"image|text|shape|fill|adjustment|group\", \"name\"?, \"visible\"?, \"opacity\" (0..1)?, \"blend\"?, \"mask\"?, \"clip\"?, \"filters\"?: [{ \"type\": …, …params }], \"effects\"?: { dropShadow, outerGlow, innerShadow, stroke, colorOverlay } } + by type — image { src: \"assets/…\" | \"library:<id>\", x, y, width?, height?, rotation?, flipX?, flipY?, crop?, fit?: fill|cover|contain, focus? }; text { text, x, y, width? (wraps), font (catalog id), size, weight, italic, color (colour | gradient), align, valign, lineHeight, letterSpacing, transform, background? { color, padding, radius } }; shape { shape: rect|ellipse|line|polygon|star, x, y, width, height, fill, stroke { color, width, dash }, radius, sides, innerRadius }; fill { fill: colour | gradient } (whole canvas); adjustment { filters } (affects layers below); group { children: [layers] }. Pixels, origin top-left, degrees.",
|
|
218
|
+
additionalProperties: true
|
|
219
|
+
};
|
|
220
|
+
var positionProps = {
|
|
221
|
+
parent: {
|
|
222
|
+
type: ["string", "null"],
|
|
223
|
+
description: "Group id to put it in (default: the root)."
|
|
224
|
+
},
|
|
225
|
+
index: {
|
|
226
|
+
type: "integer",
|
|
227
|
+
description: "Position in the parent, 0 = bottom (default: top)."
|
|
228
|
+
},
|
|
229
|
+
above: {
|
|
230
|
+
type: "string",
|
|
231
|
+
description: "Place it just above this layer id."
|
|
232
|
+
},
|
|
233
|
+
below: {
|
|
234
|
+
type: "string",
|
|
235
|
+
description: "Place it just below this layer id."
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
var SECTIONS = Object.keys(CATALOG);
|
|
239
|
+
function catalogGet(section, id) {
|
|
240
|
+
if (!section) return {
|
|
241
|
+
sections: {
|
|
242
|
+
layerTypes: CATALOG.layerTypes.map((t) => `${t.id} — ${t.description}`),
|
|
243
|
+
filters: CATALOG.filters.map((f) => `${f.id} (${f.group}) — ${f.description}`),
|
|
244
|
+
looks: CATALOG.looks.map((l) => `${l.id} — ${l.description}`),
|
|
245
|
+
effects: CATALOG.effects.map((e) => `${e.id} — ${e.description}`),
|
|
246
|
+
blendModes: CATALOG.blendModes,
|
|
247
|
+
shapes: CATALOG.shapes,
|
|
248
|
+
fonts: CATALOG.fonts.map((f) => `${f.id} (${f.style}, weights ${f.weights.join("/")}) — ${f.description}`),
|
|
249
|
+
sizes: CATALOG.sizes.map((s) => `${s.id} ${s.width}×${s.height} — ${s.title}`),
|
|
250
|
+
models: CATALOG.models.map((m) => `${m.id} (${m.task}, ${m.sizeMb} MB) — ${m.description}`)
|
|
251
|
+
},
|
|
252
|
+
commonFilterParams: CATALOG.commonFilterParams,
|
|
253
|
+
hint: "catalog_get { section: \"filters\", id: \"curves\" } gives the parameters (types, ranges, defaults) and an example of one entry."
|
|
254
|
+
};
|
|
255
|
+
const sec = SECTIONS.find((s) => s.toLowerCase() === section.toLowerCase()) ?? SECTIONS.find((s) => s.toLowerCase().startsWith(section.toLowerCase()));
|
|
256
|
+
if (!sec) {
|
|
257
|
+
const s = closest(section, SECTIONS);
|
|
258
|
+
throw new OpsError("notFound", `Unknown catalog section "${section}".`, `${s ? `Did you mean "${s}"? ` : ""}Sections: ${SECTIONS.join(", ")}.`);
|
|
259
|
+
}
|
|
260
|
+
const list = CATALOG[sec];
|
|
261
|
+
if (!id) return {
|
|
262
|
+
section: sec,
|
|
263
|
+
entries: list
|
|
264
|
+
};
|
|
265
|
+
const entry = list.find((e) => typeof e === "object" && e && e.id === id) ?? list.find((e) => e === id);
|
|
266
|
+
if (!entry) {
|
|
267
|
+
const ids = list.map((e) => typeof e === "string" ? e : e.id);
|
|
268
|
+
const s = closest(id, ids);
|
|
269
|
+
throw new OpsError("notFound", `No "${id}" in ${sec}.`, `${s ? `Did you mean "${s}"? ` : ""}Valid ids: ${listOptions(ids)}.`);
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
section: sec,
|
|
273
|
+
entry
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
async function patch(ctx, ops, dryRun) {
|
|
277
|
+
return mutationResult(await patchDoc(ctx.fs, ops, {
|
|
278
|
+
dryRun,
|
|
279
|
+
readLibrary: libraryReader(ctx)
|
|
280
|
+
}), dryRun);
|
|
281
|
+
}
|
|
282
|
+
async function readInputImage(ctx, input, what) {
|
|
283
|
+
const file = optStr(input, "file") ?? optStr(input, "image");
|
|
284
|
+
const url = optStr(input, "url") ?? optStr(input, "imageUrl");
|
|
285
|
+
if (file) {
|
|
286
|
+
if (/^https?:\/\//.test(file)) return readUrl(ctx, file);
|
|
287
|
+
if (!ctx.host?.readLocalFile) throw new OpsError("invalid", `${what}: this host cannot read local files.`, "Use url, or import the file from the Studio (drag & drop).");
|
|
288
|
+
const bytes = await ctx.host.readLocalFile(file);
|
|
289
|
+
return {
|
|
290
|
+
name: (what === "assets_add" ? optStr(input, "name") : void 0) ?? file.split(/[\\/]/).pop() ?? "image",
|
|
291
|
+
bytes,
|
|
292
|
+
source: "user"
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
if (url) return readUrl(ctx, url);
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
async function readUrl(ctx, url) {
|
|
299
|
+
if (!ctx.fetch) throw new OpsError("invalid", "This host has no network access for URLs.", "Download the file and pass its local path (file).");
|
|
300
|
+
const res = await ctx.fetch(url, { headers: { accept: "image/*" } });
|
|
301
|
+
if (!res.ok) throw new OpsError("io", `Download failed (HTTP ${res.status}): ${url}`);
|
|
302
|
+
const u = new URL(url);
|
|
303
|
+
return {
|
|
304
|
+
name: decodeURIComponent(u.pathname.split("/").pop() || "image"),
|
|
305
|
+
bytes: new Uint8Array(await res.arrayBuffer()),
|
|
306
|
+
source: u.host,
|
|
307
|
+
sourceUrl: url
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
var DOC_TOOLS = [
|
|
311
|
+
{
|
|
312
|
+
name: "project_info",
|
|
313
|
+
description: "The project: manifest, document summary (canvas, layers top → bottom with type, box, filters, masks, effects) and issue counts. Start here.",
|
|
314
|
+
inputSchema: {
|
|
315
|
+
type: "object",
|
|
316
|
+
properties: {}
|
|
317
|
+
},
|
|
318
|
+
mutating: false,
|
|
319
|
+
async handler(ctx) {
|
|
320
|
+
const { manifest, doc } = await loadProject(ctx.fs);
|
|
321
|
+
return {
|
|
322
|
+
manifest,
|
|
323
|
+
summary: summarizeDoc(doc),
|
|
324
|
+
counts: issueCounts(validateDoc(doc))
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
name: "project_create",
|
|
330
|
+
description: "Create a project in the current folder: blank (size preset or width / height), from a photo (image: local path or URL — the canvas takes its size, it becomes the \"photo\" layer), and / or from a template (templates_list). Fails if a project exists unless force.",
|
|
331
|
+
inputSchema: {
|
|
332
|
+
type: "object",
|
|
333
|
+
properties: {
|
|
334
|
+
name: {
|
|
335
|
+
type: "string",
|
|
336
|
+
description: "Project name (slug)."
|
|
337
|
+
},
|
|
338
|
+
title: { type: "string" },
|
|
339
|
+
image: {
|
|
340
|
+
type: "string",
|
|
341
|
+
description: "Local file path or http(s) URL of the starting photo."
|
|
342
|
+
},
|
|
343
|
+
size: {
|
|
344
|
+
type: "string",
|
|
345
|
+
description: "Canvas size preset (catalog \"sizes\": og, square, story, youtube…)."
|
|
346
|
+
},
|
|
347
|
+
width: { type: "integer" },
|
|
348
|
+
height: { type: "integer" },
|
|
349
|
+
background: {
|
|
350
|
+
type: [
|
|
351
|
+
"string",
|
|
352
|
+
"object",
|
|
353
|
+
"null"
|
|
354
|
+
],
|
|
355
|
+
description: "Canvas background (colour, gradient, null = transparent)."
|
|
356
|
+
},
|
|
357
|
+
template: {
|
|
358
|
+
type: "string",
|
|
359
|
+
description: "Template id."
|
|
360
|
+
},
|
|
361
|
+
force: {
|
|
362
|
+
type: "boolean",
|
|
363
|
+
description: "Overwrite an existing project."
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
required: ["name"]
|
|
367
|
+
},
|
|
368
|
+
mutating: true,
|
|
369
|
+
async handler(ctx, input) {
|
|
370
|
+
const image = await readInputImage(ctx, input, "project_create");
|
|
371
|
+
const template = optStr(input, "template");
|
|
372
|
+
let templateFiles;
|
|
373
|
+
if (template) {
|
|
374
|
+
if (!ctx.host?.templates) throw new OpsError("invalid", "Templates are not available on this host.");
|
|
375
|
+
findTemplate(ctx.host.templates.index, template);
|
|
376
|
+
templateFiles = await ctx.host.templates.files(template);
|
|
377
|
+
}
|
|
378
|
+
const r = await createProject(ctx.fs, {
|
|
379
|
+
name: str(input, "name"),
|
|
380
|
+
title: optStr(input, "title"),
|
|
381
|
+
size: optStr(input, "size"),
|
|
382
|
+
width: num(input, "width"),
|
|
383
|
+
height: num(input, "height"),
|
|
384
|
+
background: input.background,
|
|
385
|
+
template,
|
|
386
|
+
templateFiles,
|
|
387
|
+
image: image ?? void 0,
|
|
388
|
+
force: bool(input, "force")
|
|
389
|
+
});
|
|
390
|
+
return {
|
|
391
|
+
created: true,
|
|
392
|
+
manifest: r.manifest,
|
|
393
|
+
files: r.files,
|
|
394
|
+
summary: summarizeDoc(r.doc),
|
|
395
|
+
...compactIssues(r.issues),
|
|
396
|
+
hint: "Next: render_preview to see it, then layer_add / doc_patch to edit."
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
{
|
|
401
|
+
name: "doc_get",
|
|
402
|
+
description: "The full document (photo.json). Prefer doc_summary unless you need exact values.",
|
|
403
|
+
inputSchema: {
|
|
404
|
+
type: "object",
|
|
405
|
+
properties: {}
|
|
406
|
+
},
|
|
407
|
+
mutating: false,
|
|
408
|
+
handler: (ctx) => getDoc(ctx.fs)
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
name: "doc_summary",
|
|
412
|
+
description: "Compact outline: canvas size / background, layers top → bottom (id, type, detail, box [x, y, w, h], opacity, blend, mask kind, filters, effects, children), counts, sources.",
|
|
413
|
+
inputSchema: {
|
|
414
|
+
type: "object",
|
|
415
|
+
properties: {}
|
|
416
|
+
},
|
|
417
|
+
mutating: false,
|
|
418
|
+
handler: async (ctx) => summarizeDoc(await getDoc(ctx.fs))
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
name: "doc_apply",
|
|
422
|
+
description: "Replace the whole document (validated; nothing written on errors). Destructive: confirm with the user when it removes layers. Use doc_patch for edits.",
|
|
423
|
+
inputSchema: {
|
|
424
|
+
type: "object",
|
|
425
|
+
properties: {
|
|
426
|
+
doc: {
|
|
427
|
+
type: "object",
|
|
428
|
+
description: "The full PhotoDoc { version: 1, id, title?, canvas: { width, height, background? }, layers: [...] }.",
|
|
429
|
+
additionalProperties: true
|
|
430
|
+
},
|
|
431
|
+
dryRun: dryRunProp
|
|
432
|
+
},
|
|
433
|
+
required: ["doc"]
|
|
434
|
+
},
|
|
435
|
+
mutating: true,
|
|
436
|
+
async handler(ctx, input) {
|
|
437
|
+
const dry = bool(input, "dryRun");
|
|
438
|
+
return mutationResult(await applyDoc(ctx.fs, input.doc, { dryRun: dry }), dry);
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
{
|
|
442
|
+
name: "doc_patch",
|
|
443
|
+
description: `Atomic list of edits, validated together (nothing written if any fails). Ops: ${PATCH_OPS.join(", ")}. Shapes: { op: "set", path: "layers#title.size", value: 96 } · { op: "merge", path, value } (objects merge, null deletes) · { op: "remove", path } · { op: "addLayer", layer, parent?, index?, above?, below? } · { op: "updateLayer", id, set } (deep merge, null deletes a field) · { op: "removeLayer", id } · { op: "moveLayer", id, parent?, index?, above?, below? } · { op: "duplicateLayer", id, newId?, offset? } · { op: "groupLayers", ids, id, name? } · { op: "ungroup", id } · { op: "addFilter", id, filter, index? } · { op: "updateFilter", id, index, set } · { op: "removeFilter", id, index } · { op: "setMask", id, mask | null } · { op: "crop", x, y, width, height } · { op: "resizeCanvas", width, height, anchor? } · { op: "resizeImage", width?, height?, scale? } · { op: "rotateCanvas", angle: 90|180|270 } · { op: "flipCanvas", axis: horizontal|vertical } · { op: "fitLayer", id, box: { x, y, width, height }, subject?, anchor? } (scale + move a layer so subject — a canvas-px region of it, e.g. remove_background's subject; default its box — fits in box, aligned on anchor: center, bottom…). Paths: "canvas.background", "layers#<id>.<field>", "layers#<id>.filters[0].radius".`,
|
|
444
|
+
inputSchema: {
|
|
445
|
+
type: "object",
|
|
446
|
+
properties: {
|
|
447
|
+
ops: {
|
|
448
|
+
type: "array",
|
|
449
|
+
items: {
|
|
450
|
+
type: "object",
|
|
451
|
+
additionalProperties: true
|
|
452
|
+
},
|
|
453
|
+
description: "Patch operations, applied in order."
|
|
454
|
+
},
|
|
455
|
+
dryRun: dryRunProp
|
|
456
|
+
},
|
|
457
|
+
required: ["ops"]
|
|
458
|
+
},
|
|
459
|
+
mutating: true,
|
|
460
|
+
handler: (ctx, input) => patch(ctx, input.ops, bool(input, "dryRun"))
|
|
461
|
+
},
|
|
462
|
+
{
|
|
463
|
+
name: "doc_validate",
|
|
464
|
+
description: "Validate a document (the given one, or photo.json): issues with JSON paths and hints.",
|
|
465
|
+
inputSchema: {
|
|
466
|
+
type: "object",
|
|
467
|
+
properties: { doc: {
|
|
468
|
+
type: "object",
|
|
469
|
+
additionalProperties: true
|
|
470
|
+
} }
|
|
471
|
+
},
|
|
472
|
+
mutating: false,
|
|
473
|
+
async handler(ctx, input) {
|
|
474
|
+
const doc = input.doc ?? await getDoc(ctx.fs);
|
|
475
|
+
const issues = validateDoc(doc);
|
|
476
|
+
return {
|
|
477
|
+
valid: !issues.some((i) => i.severity === "error"),
|
|
478
|
+
counts: issueCounts(issues),
|
|
479
|
+
...compactIssues(issues)
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
name: "layer_list",
|
|
485
|
+
description: "Every layer top → bottom (flat, with parent and depth): id, type, detail, box, opacity, blend, mask, filters (in order — their index is what updateFilter / removeFilter use), effects.",
|
|
486
|
+
inputSchema: {
|
|
487
|
+
type: "object",
|
|
488
|
+
properties: {}
|
|
489
|
+
},
|
|
490
|
+
mutating: false,
|
|
491
|
+
handler: async (ctx) => ({ layers: flattenLayers(await getDoc(ctx.fs)) })
|
|
492
|
+
},
|
|
493
|
+
{
|
|
494
|
+
name: "layer_add",
|
|
495
|
+
description: "Add a layer (validated). Default position: top of the stack. Image layers need a src from assets_add / library_list.",
|
|
496
|
+
inputSchema: {
|
|
497
|
+
type: "object",
|
|
498
|
+
properties: {
|
|
499
|
+
layer: LAYER_SCHEMA,
|
|
500
|
+
...positionProps,
|
|
501
|
+
dryRun: dryRunProp
|
|
502
|
+
},
|
|
503
|
+
required: ["layer"]
|
|
504
|
+
},
|
|
505
|
+
mutating: true,
|
|
506
|
+
handler: (ctx, input) => patch(ctx, [{
|
|
507
|
+
op: "addLayer",
|
|
508
|
+
layer: input.layer,
|
|
509
|
+
parent: input.parent,
|
|
510
|
+
index: num(input, "index"),
|
|
511
|
+
above: optStr(input, "above"),
|
|
512
|
+
below: optStr(input, "below")
|
|
513
|
+
}], bool(input, "dryRun"))
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
name: "layer_update",
|
|
517
|
+
description: "Change fields of a layer (deep merge; null removes a field, e.g. { \"mask\": null }). Cannot change id / type / children.",
|
|
518
|
+
inputSchema: {
|
|
519
|
+
type: "object",
|
|
520
|
+
properties: {
|
|
521
|
+
id: { type: "string" },
|
|
522
|
+
set: {
|
|
523
|
+
type: "object",
|
|
524
|
+
additionalProperties: true,
|
|
525
|
+
description: "Fields to change, e.g. { \"x\": 40, \"opacity\": 0.8, \"effects\": { \"dropShadow\": { \"blur\": 24 } } }."
|
|
526
|
+
},
|
|
527
|
+
dryRun: dryRunProp
|
|
528
|
+
},
|
|
529
|
+
required: ["id", "set"]
|
|
530
|
+
},
|
|
531
|
+
mutating: true,
|
|
532
|
+
handler: (ctx, input) => patch(ctx, [{
|
|
533
|
+
op: "updateLayer",
|
|
534
|
+
id: str(input, "id"),
|
|
535
|
+
set: input.set
|
|
536
|
+
}], bool(input, "dryRun"))
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
name: "layer_remove",
|
|
540
|
+
description: "Remove a layer (and its children for a group). Its files stay in assets/.",
|
|
541
|
+
inputSchema: {
|
|
542
|
+
type: "object",
|
|
543
|
+
properties: {
|
|
544
|
+
id: { type: "string" },
|
|
545
|
+
dryRun: dryRunProp
|
|
546
|
+
},
|
|
547
|
+
required: ["id"]
|
|
548
|
+
},
|
|
549
|
+
mutating: true,
|
|
550
|
+
handler: (ctx, input) => patch(ctx, [{
|
|
551
|
+
op: "removeLayer",
|
|
552
|
+
id: str(input, "id")
|
|
553
|
+
}], bool(input, "dryRun"))
|
|
554
|
+
},
|
|
555
|
+
{
|
|
556
|
+
name: "layer_move",
|
|
557
|
+
description: "Reorder a layer: above / below another layer, or into a group at an index.",
|
|
558
|
+
inputSchema: {
|
|
559
|
+
type: "object",
|
|
560
|
+
properties: {
|
|
561
|
+
id: { type: "string" },
|
|
562
|
+
...positionProps,
|
|
563
|
+
dryRun: dryRunProp
|
|
564
|
+
},
|
|
565
|
+
required: ["id"]
|
|
566
|
+
},
|
|
567
|
+
mutating: true,
|
|
568
|
+
handler: (ctx, input) => patch(ctx, [{
|
|
569
|
+
op: "moveLayer",
|
|
570
|
+
id: str(input, "id"),
|
|
571
|
+
parent: input.parent,
|
|
572
|
+
index: num(input, "index"),
|
|
573
|
+
above: optStr(input, "above"),
|
|
574
|
+
below: optStr(input, "below")
|
|
575
|
+
}], bool(input, "dryRun"))
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
name: "filter_add",
|
|
579
|
+
description: "Add a non-destructive filter to a layer (or to an adjustment layer — which affects everything below). e.g. { id: \"photo\", filter: { \"type\": \"exposure\", \"exposure\": 0.4 } }. catalog_get section \"filters\" lists them.",
|
|
580
|
+
inputSchema: {
|
|
581
|
+
type: "object",
|
|
582
|
+
properties: {
|
|
583
|
+
id: {
|
|
584
|
+
type: "string",
|
|
585
|
+
description: "Layer id."
|
|
586
|
+
},
|
|
587
|
+
filter: {
|
|
588
|
+
type: "object",
|
|
589
|
+
additionalProperties: true,
|
|
590
|
+
description: "{ type, ...params, amount? }"
|
|
591
|
+
},
|
|
592
|
+
index: { type: "integer" },
|
|
593
|
+
dryRun: dryRunProp
|
|
594
|
+
},
|
|
595
|
+
required: ["id", "filter"]
|
|
596
|
+
},
|
|
597
|
+
mutating: true,
|
|
598
|
+
handler: (ctx, input) => patch(ctx, [{
|
|
599
|
+
op: "addFilter",
|
|
600
|
+
id: str(input, "id"),
|
|
601
|
+
filter: input.filter,
|
|
602
|
+
index: num(input, "index")
|
|
603
|
+
}], bool(input, "dryRun"))
|
|
604
|
+
},
|
|
605
|
+
{
|
|
606
|
+
name: "mask_set",
|
|
607
|
+
description: "Set (or remove with null) a layer mask: { \"src\": \"assets/masks/x.png\" } | { \"shape\": \"rect|ellipse\", x, y, width, height, radius? } | { \"gradient\": { type, angle, stops } } + invert, feather, density, enabled. Box layers: layer-local px (0,0 = the layer's top-left). For AI masks use remove_background / mask_generate.",
|
|
608
|
+
inputSchema: {
|
|
609
|
+
type: "object",
|
|
610
|
+
properties: {
|
|
611
|
+
id: { type: "string" },
|
|
612
|
+
mask: {
|
|
613
|
+
type: ["object", "null"],
|
|
614
|
+
additionalProperties: true
|
|
615
|
+
},
|
|
616
|
+
dryRun: dryRunProp
|
|
617
|
+
},
|
|
618
|
+
required: ["id"]
|
|
619
|
+
},
|
|
620
|
+
mutating: true,
|
|
621
|
+
handler: (ctx, input) => patch(ctx, [{
|
|
622
|
+
op: "setMask",
|
|
623
|
+
id: str(input, "id"),
|
|
624
|
+
mask: input.mask ?? null
|
|
625
|
+
}], bool(input, "dryRun"))
|
|
626
|
+
},
|
|
627
|
+
{
|
|
628
|
+
name: "catalog_get",
|
|
629
|
+
description: "The catalog: layer types, filters (+ params), looks, layer effects, blend modes, shapes, bundled fonts, size presets, AI models. No input = an overview; { section, id } = one entry with its parameters.",
|
|
630
|
+
inputSchema: {
|
|
631
|
+
type: "object",
|
|
632
|
+
properties: {
|
|
633
|
+
section: {
|
|
634
|
+
type: "string",
|
|
635
|
+
enum: SECTIONS
|
|
636
|
+
},
|
|
637
|
+
id: { type: "string" }
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
mutating: false,
|
|
641
|
+
handler: (_ctx, input) => catalogGet(optStr(input, "section"), optStr(input, "id"))
|
|
642
|
+
},
|
|
643
|
+
{
|
|
644
|
+
name: "templates_list",
|
|
645
|
+
description: "Starter templates (social posts, thumbnails, ads, product shots…) with their size and the slot that receives the user's photo. Use with project_create { template, image }.",
|
|
646
|
+
inputSchema: {
|
|
647
|
+
type: "object",
|
|
648
|
+
properties: {
|
|
649
|
+
category: { type: "string" },
|
|
650
|
+
query: { type: "string" }
|
|
651
|
+
}
|
|
652
|
+
},
|
|
653
|
+
mutating: false,
|
|
654
|
+
handler: (ctx, input) => ({ templates: listTemplates(ctx.host?.templates?.index ?? [], {
|
|
655
|
+
category: optStr(input, "category"),
|
|
656
|
+
query: optStr(input, "query")
|
|
657
|
+
}) })
|
|
658
|
+
},
|
|
659
|
+
{
|
|
660
|
+
name: "check",
|
|
661
|
+
description: "Everything to fix before export: validation, missing files, layers off the canvas, upscaled (blurry) photos, text that may overflow, heavy filters, licences needing attribution.",
|
|
662
|
+
inputSchema: {
|
|
663
|
+
type: "object",
|
|
664
|
+
properties: {}
|
|
665
|
+
},
|
|
666
|
+
mutating: false,
|
|
667
|
+
async handler(ctx) {
|
|
668
|
+
const r = await checkProject(ctx.fs, { library: ctx.host?.library?.index });
|
|
669
|
+
return {
|
|
670
|
+
...r,
|
|
671
|
+
...compactIssues(r.issues)
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
name: "assets_list",
|
|
677
|
+
description: "Project files (photos, masks, edits) with size, source, licence and the layers using them.",
|
|
678
|
+
inputSchema: {
|
|
679
|
+
type: "object",
|
|
680
|
+
properties: {}
|
|
681
|
+
},
|
|
682
|
+
mutating: false,
|
|
683
|
+
handler: (ctx) => listAssets(ctx.fs)
|
|
684
|
+
},
|
|
685
|
+
{
|
|
686
|
+
name: "assets_add",
|
|
687
|
+
description: "Import an image into assets/images/ from a local path (file) or an http(s) URL (url). With addLayer: true it also adds an image layer (on top, fitted inside the canvas) and returns its id.",
|
|
688
|
+
inputSchema: {
|
|
689
|
+
type: "object",
|
|
690
|
+
properties: {
|
|
691
|
+
file: {
|
|
692
|
+
type: "string",
|
|
693
|
+
description: "Local file path (CLI / MCP hosts)."
|
|
694
|
+
},
|
|
695
|
+
url: {
|
|
696
|
+
type: "string",
|
|
697
|
+
description: "http(s) URL of an image."
|
|
698
|
+
},
|
|
699
|
+
name: {
|
|
700
|
+
type: "string",
|
|
701
|
+
description: "File name to store it as."
|
|
702
|
+
},
|
|
703
|
+
license: { type: "string" },
|
|
704
|
+
author: { type: "string" },
|
|
705
|
+
addLayer: {
|
|
706
|
+
type: "boolean",
|
|
707
|
+
description: "Also add it as a layer."
|
|
708
|
+
},
|
|
709
|
+
layerId: {
|
|
710
|
+
type: "string",
|
|
711
|
+
description: "Id of the new layer (default from the file name)."
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
},
|
|
715
|
+
mutating: true,
|
|
716
|
+
async handler(ctx, input) {
|
|
717
|
+
const img = await readInputImage(ctx, input, "assets_add");
|
|
718
|
+
if (!img) throw new OpsError("invalid", "assets_add needs file or url.");
|
|
719
|
+
const entry = await addAsset(ctx.fs, {
|
|
720
|
+
name: optStr(input, "name") ?? img.name,
|
|
721
|
+
bytes: img.bytes,
|
|
722
|
+
kind: "image",
|
|
723
|
+
source: img.source,
|
|
724
|
+
sourceUrl: img.sourceUrl,
|
|
725
|
+
license: optStr(input, "license"),
|
|
726
|
+
author: optStr(input, "author")
|
|
727
|
+
});
|
|
728
|
+
if (!bool(input, "addLayer")) return {
|
|
729
|
+
asset: entry,
|
|
730
|
+
hint: `Use it as { "type": "image", "src": "${entry.path}" } (layer_add).`
|
|
731
|
+
};
|
|
732
|
+
const doc = await getDoc(ctx.fs);
|
|
733
|
+
const { uniqueId, slugify } = await import("./ids-DAmiNwd9.mjs").then((n) => n.t);
|
|
734
|
+
const { allLayerIds } = await import("./tree-D8qZKPAb.mjs").then((n) => n.c);
|
|
735
|
+
const id = uniqueId(optStr(input, "layerId") ?? slugify(entry.path.split("/").pop().replace(/\.[^.]+$/, ""), "image"), allLayerIds(doc));
|
|
736
|
+
const W = doc.canvas.width;
|
|
737
|
+
const H = doc.canvas.height;
|
|
738
|
+
const w = entry.width ?? W;
|
|
739
|
+
const h = entry.height ?? H;
|
|
740
|
+
const k = Math.min(1, W / w, H / h);
|
|
741
|
+
return {
|
|
742
|
+
asset: entry,
|
|
743
|
+
layer: id,
|
|
744
|
+
...await patch(ctx, [{
|
|
745
|
+
op: "addLayer",
|
|
746
|
+
layer: {
|
|
747
|
+
id,
|
|
748
|
+
type: "image",
|
|
749
|
+
src: entry.path,
|
|
750
|
+
x: Math.round((W - w * k) / 2),
|
|
751
|
+
y: Math.round((H - h * k) / 2),
|
|
752
|
+
width: Math.round(w * k),
|
|
753
|
+
height: Math.round(h * k)
|
|
754
|
+
}
|
|
755
|
+
}], false)
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
name: "assets_remove",
|
|
761
|
+
description: "Delete a project file. Refused while a layer uses it unless force.",
|
|
762
|
+
inputSchema: {
|
|
763
|
+
type: "object",
|
|
764
|
+
properties: {
|
|
765
|
+
path: { type: "string" },
|
|
766
|
+
force: { type: "boolean" }
|
|
767
|
+
},
|
|
768
|
+
required: ["path"]
|
|
769
|
+
},
|
|
770
|
+
mutating: true,
|
|
771
|
+
handler: (ctx, input) => removeAsset(ctx.fs, str(input, "path"), { force: bool(input, "force") })
|
|
772
|
+
},
|
|
773
|
+
{
|
|
774
|
+
name: "library_list",
|
|
775
|
+
description: "Built-in assets: sample photos (CC0, use as \"library:<id>\" in an image layer src) and fonts (use the id as a text layer font).",
|
|
776
|
+
inputSchema: {
|
|
777
|
+
type: "object",
|
|
778
|
+
properties: {
|
|
779
|
+
kind: {
|
|
780
|
+
type: "string",
|
|
781
|
+
enum: ["image", "font"]
|
|
782
|
+
},
|
|
783
|
+
query: { type: "string" }
|
|
784
|
+
}
|
|
785
|
+
},
|
|
786
|
+
mutating: false,
|
|
787
|
+
handler(ctx, input) {
|
|
788
|
+
const kind = optStr(input, "kind");
|
|
789
|
+
const words = (optStr(input, "query") ?? "").toLowerCase().split(/\s+/).filter(Boolean);
|
|
790
|
+
const assets = (ctx.host?.library?.index.assets ?? []).filter((a) => !kind || a.kind === kind).filter((a) => words.every((w) => `${a.id} ${a.title ?? ""} ${a.description} ${a.tags.join(" ")}`.toLowerCase().includes(w))).map((a) => ({
|
|
791
|
+
id: a.id,
|
|
792
|
+
kind: a.kind,
|
|
793
|
+
ref: a.kind === "image" ? `library:${a.id}` : a.id,
|
|
794
|
+
...a.width ? { size: `${a.width}×${a.height}` } : {},
|
|
795
|
+
license: a.license,
|
|
796
|
+
description: a.description,
|
|
797
|
+
tags: a.tags
|
|
798
|
+
}));
|
|
799
|
+
return {
|
|
800
|
+
assets,
|
|
801
|
+
total: assets.length
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
];
|
|
806
|
+
// src/ai/imageTools.ts
|
|
807
|
+
function engine(ctx) {
|
|
808
|
+
const e = ctx.host?.engine;
|
|
809
|
+
if (!e) throw new OpsError("unavailable", "This host cannot render images.", "Run the CLI (`agentphoto`, needs Google Chrome / Chromium) or the Studio.");
|
|
810
|
+
return e;
|
|
811
|
+
}
|
|
812
|
+
var EXT = {
|
|
813
|
+
"image/png": "png",
|
|
814
|
+
"image/jpeg": "jpg",
|
|
815
|
+
"image/webp": "webp",
|
|
816
|
+
"image/avif": "avif"
|
|
817
|
+
};
|
|
818
|
+
function layerOf(doc, id) {
|
|
819
|
+
const loc = findLayer(doc.layers, id);
|
|
820
|
+
if (!loc) throw new OpsError("notFound", `No layer "${id}".`, "layer_list lists the layer ids.");
|
|
821
|
+
return loc.layer;
|
|
822
|
+
}
|
|
823
|
+
function disableEdits(doc) {
|
|
824
|
+
const strip = (layers) => layers.filter((l) => l.type !== "adjustment").map((l) => {
|
|
825
|
+
const { filters: _f, ...rest } = l;
|
|
826
|
+
return l.type === "group" ? {
|
|
827
|
+
...rest,
|
|
828
|
+
children: strip(l.children ?? [])
|
|
829
|
+
} : rest;
|
|
830
|
+
});
|
|
831
|
+
return {
|
|
832
|
+
...doc,
|
|
833
|
+
layers: strip(doc.layers)
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
async function writeMask(ctx, doc, id, img, opts) {
|
|
837
|
+
const layer = layerOf(doc, id);
|
|
838
|
+
const target = opts.maskId ?? id;
|
|
839
|
+
const entry = await addAsset(ctx.fs, {
|
|
840
|
+
name: `${target}-${opts.label}.png`,
|
|
841
|
+
bytes: img.bytes,
|
|
842
|
+
kind: "mask",
|
|
843
|
+
source: opts.source
|
|
844
|
+
});
|
|
845
|
+
const mask = {
|
|
846
|
+
src: entry.path,
|
|
847
|
+
...opts.invert ? { invert: true } : {},
|
|
848
|
+
...opts.feather ? { feather: opts.feather } : {}
|
|
849
|
+
};
|
|
850
|
+
const r = await patchDoc(ctx.fs, [
|
|
851
|
+
...opts.preOps ?? [],
|
|
852
|
+
{
|
|
853
|
+
op: "setMask",
|
|
854
|
+
id: target,
|
|
855
|
+
mask
|
|
856
|
+
},
|
|
857
|
+
...opts.extraOps ?? []
|
|
858
|
+
], { readLibrary: libraryReader(ctx) }).catch(async (e) => {
|
|
859
|
+
await removeGenerated(ctx.fs, entry.path);
|
|
860
|
+
throw e;
|
|
861
|
+
});
|
|
862
|
+
if (!r.applied) await removeGenerated(ctx.fs, entry.path);
|
|
863
|
+
const prev = layer.mask && isImageMask(layer.mask) ? layer.mask.src : null;
|
|
864
|
+
if (r.applied && prev && prev !== entry.path && prev.startsWith("assets/masks/")) {
|
|
865
|
+
if (!referencedFiles(r.doc).some((f) => f.path === prev)) await removeGenerated(ctx.fs, prev);
|
|
866
|
+
}
|
|
867
|
+
const after = r.applied ? findLayer(r.doc.layers, target)?.layer ?? layer : layer;
|
|
868
|
+
return {
|
|
869
|
+
mask: entry.path,
|
|
870
|
+
size: `${img.width}×${img.height}`,
|
|
871
|
+
...img.bbox !== void 0 ? { subject: subjectBox(after, img) } : {},
|
|
872
|
+
...mutationResult(r, false)
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
function coverCuts(doc, job, measured = {}) {
|
|
876
|
+
const { x, y, width, height } = job.source;
|
|
877
|
+
if (x <= .5 && y <= .5 && width >= doc.canvas.width - 1 && height >= doc.canvas.height - 1) return [];
|
|
878
|
+
const out = [];
|
|
879
|
+
const visit = (layers) => {
|
|
880
|
+
for (const l of layers) {
|
|
881
|
+
if (l.visible === false) continue;
|
|
882
|
+
if (l.type === "group") visit(l.children ?? []);
|
|
883
|
+
if (l.type !== "text" && l.type !== "shape") continue;
|
|
884
|
+
const m = measured[l.id];
|
|
885
|
+
const b = l.type === "text" && m ? m.ink ?? m : layerBox(l);
|
|
886
|
+
if (b && (b.x < x - 1 || b.y < y - 1 || b.x + b.width > x + width + 1 || b.y + b.height > y + height + 1)) out.push(l.id);
|
|
887
|
+
}
|
|
888
|
+
};
|
|
889
|
+
visit(doc.layers);
|
|
890
|
+
return out;
|
|
891
|
+
}
|
|
892
|
+
function subjectBox(layer, img) {
|
|
893
|
+
const b = img.bbox;
|
|
894
|
+
if (!b) return null;
|
|
895
|
+
const l = layer;
|
|
896
|
+
const kx = (l.width ?? img.width) / img.width;
|
|
897
|
+
const ky = (l.height ?? img.height) / img.height;
|
|
898
|
+
const bx = l.flipX ? img.width - b.x - b.width : b.x;
|
|
899
|
+
const by = l.flipY ? img.height - b.y - b.height : b.y;
|
|
900
|
+
const r = (v) => Math.round(v);
|
|
901
|
+
return {
|
|
902
|
+
x: r((l.x ?? 0) + bx * kx),
|
|
903
|
+
y: r((l.y ?? 0) + by * ky),
|
|
904
|
+
width: r(b.width * kx),
|
|
905
|
+
height: r(b.height * ky)
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
async function fitTarget(ctx, layer, fit) {
|
|
909
|
+
if (fit === false || fit === null) return null;
|
|
910
|
+
if (fit !== void 0) {
|
|
911
|
+
const f = fit;
|
|
912
|
+
if (!f || typeof f !== "object" || ![
|
|
913
|
+
"x",
|
|
914
|
+
"y",
|
|
915
|
+
"width",
|
|
916
|
+
"height"
|
|
917
|
+
].every((k) => typeof f[k] === "number" && Number.isFinite(f[k])) || !(f.width >= 1 && f.height >= 1)) throw new OpsError("invalid", "fit must be { x, y, width, height, anchor? } in canvas px (or false).", "e.g. fit: { \"x\": 500, \"y\": 400, \"width\": 1000, \"height\": 850, \"anchor\": \"bottom\" }.");
|
|
918
|
+
const anchor = f.anchor ?? "center";
|
|
919
|
+
if (!ANCHORS.includes(anchor)) throw new OpsError("invalid", `Unknown anchor "${String(f.anchor)}".`, `Anchors: ${ANCHORS.join(", ")}.`);
|
|
920
|
+
return {
|
|
921
|
+
box: {
|
|
922
|
+
x: f.x,
|
|
923
|
+
y: f.y,
|
|
924
|
+
width: f.width,
|
|
925
|
+
height: f.height
|
|
926
|
+
},
|
|
927
|
+
anchor,
|
|
928
|
+
from: "input"
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
if (layer.mask && isImageMask(layer.mask)) return null;
|
|
932
|
+
const manifest = await loadManifest(ctx.fs).catch(() => null);
|
|
933
|
+
const t = manifest?.template ? ctx.host?.templates?.index.find((x) => x.id === manifest.template) : void 0;
|
|
934
|
+
if (!t?.subjectArea || t.slot !== layer.id) return null;
|
|
935
|
+
const { anchor, ...box } = t.subjectArea;
|
|
936
|
+
return {
|
|
937
|
+
box,
|
|
938
|
+
anchor: anchor ?? "center",
|
|
939
|
+
from: `template ${t.id}`
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
async function removeGenerated(fs, path) {
|
|
943
|
+
const { removeAsset } = await import("./assets-BZVUUHjg.mjs").then((n) => n.n);
|
|
944
|
+
await removeAsset(fs, path, { force: true }).catch(() => void 0);
|
|
945
|
+
}
|
|
946
|
+
var IMAGE_TOOLS = [
|
|
947
|
+
{
|
|
948
|
+
name: "render_preview",
|
|
949
|
+
description: "Render the document and LOOK at it (the image is returned to you). Use after every visual change: check composition, crop, legibility of text, colours, mask edges. compare: true also renders the 'before' (no filters / adjustments) for retouching. layer renders one layer alone. zoom: { x, y, width, height } renders a canvas region at full resolution (inspect mask edges, skin, text).",
|
|
950
|
+
inputSchema: {
|
|
951
|
+
type: "object",
|
|
952
|
+
properties: {
|
|
953
|
+
maxWidth: {
|
|
954
|
+
type: "integer",
|
|
955
|
+
description: "Preview width cap in px (default 1000)."
|
|
956
|
+
},
|
|
957
|
+
compare: {
|
|
958
|
+
type: "boolean",
|
|
959
|
+
description: "Also render the unedited version (before / after)."
|
|
960
|
+
},
|
|
961
|
+
layer: {
|
|
962
|
+
type: "string",
|
|
963
|
+
description: "Render only this layer (and the groups holding it)."
|
|
964
|
+
},
|
|
965
|
+
zoom: {
|
|
966
|
+
type: "object",
|
|
967
|
+
description: "Canvas region { x, y, width, height } rendered at 1:1 (max 1600 px).",
|
|
968
|
+
additionalProperties: true
|
|
969
|
+
},
|
|
970
|
+
backdrop: {
|
|
971
|
+
type: "string",
|
|
972
|
+
description: "Under transparent areas: \"checker\" (default when the canvas is transparent or a layer is isolated), a CSS colour, or \"none\"."
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
},
|
|
976
|
+
mutating: false,
|
|
977
|
+
needsEngine: true,
|
|
978
|
+
async handler(ctx, input) {
|
|
979
|
+
const e = engine(ctx);
|
|
980
|
+
let doc = await getDoc(ctx.fs);
|
|
981
|
+
const maxWidth = num(input, "maxWidth") ?? 1e3;
|
|
982
|
+
const zoom = input.zoom;
|
|
983
|
+
if (zoom) {
|
|
984
|
+
const { cropDoc } = await import("./geometry-BKIhUO9x.mjs").then((n) => n.a);
|
|
985
|
+
doc = cropDoc(doc, {
|
|
986
|
+
x: zoom.x,
|
|
987
|
+
y: zoom.y,
|
|
988
|
+
width: Math.min(1600, zoom.width),
|
|
989
|
+
height: Math.min(1600, zoom.height)
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
const only = optStr(input, "layer");
|
|
993
|
+
if (only) layerOf(doc, only);
|
|
994
|
+
const bd = optStr(input, "backdrop") ?? (only || !doc.canvas.background ? "checker" : "none");
|
|
995
|
+
const opts = {
|
|
996
|
+
backdrop: bd === "none" ? void 0 : bd,
|
|
997
|
+
maxWidth: zoom ? void 0 : maxWidth,
|
|
998
|
+
maxHeight: zoom ? void 0 : Math.max(maxWidth, 1e3),
|
|
999
|
+
only: only ? [only] : void 0,
|
|
1000
|
+
format: "png"
|
|
1001
|
+
};
|
|
1002
|
+
const after = await e.render(doc, opts);
|
|
1003
|
+
const path = "renders/preview.png";
|
|
1004
|
+
await ctx.fs.writeBytes(path, after.bytes);
|
|
1005
|
+
const images = [path];
|
|
1006
|
+
if (bool(input, "compare")) {
|
|
1007
|
+
const before = await e.render(disableEdits(doc), opts);
|
|
1008
|
+
const bp = "renders/preview-before.png";
|
|
1009
|
+
await ctx.fs.writeBytes(bp, before.bytes);
|
|
1010
|
+
images.unshift(bp);
|
|
1011
|
+
}
|
|
1012
|
+
return {
|
|
1013
|
+
preview: path,
|
|
1014
|
+
images,
|
|
1015
|
+
size: `${after.width}×${after.height}`,
|
|
1016
|
+
canvas: `${doc.canvas.width}×${doc.canvas.height}`,
|
|
1017
|
+
...after.errors?.length ? {
|
|
1018
|
+
errors: after.errors,
|
|
1019
|
+
hint: "Some layers failed to render (see errors): fix their src / mask."
|
|
1020
|
+
} : {}
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
},
|
|
1024
|
+
{
|
|
1025
|
+
name: "export",
|
|
1026
|
+
description: `Render the final image(s) into dist/ (or out): format png | jpeg | webp | avif (AVIF only where the browser can encode it — otherwise PNG, with a note), quality, size (preset: ${SIZE_PRESETS.map((s) => s.id).join(", ")} — or width / height / scale), fit (cover crops, contain pads) and background (flatten transparency). variants: several outputs at once (e.g. og + square + story). Run check first.`,
|
|
1027
|
+
inputSchema: {
|
|
1028
|
+
type: "object",
|
|
1029
|
+
properties: {
|
|
1030
|
+
format: {
|
|
1031
|
+
type: "string",
|
|
1032
|
+
enum: IMAGE_FORMATS
|
|
1033
|
+
},
|
|
1034
|
+
quality: {
|
|
1035
|
+
type: "integer",
|
|
1036
|
+
minimum: 1,
|
|
1037
|
+
maximum: 100
|
|
1038
|
+
},
|
|
1039
|
+
preset: { type: "string" },
|
|
1040
|
+
width: { type: "integer" },
|
|
1041
|
+
height: { type: "integer" },
|
|
1042
|
+
scale: { type: "number" },
|
|
1043
|
+
fit: {
|
|
1044
|
+
type: "string",
|
|
1045
|
+
enum: [
|
|
1046
|
+
"cover",
|
|
1047
|
+
"contain",
|
|
1048
|
+
"stretch"
|
|
1049
|
+
]
|
|
1050
|
+
},
|
|
1051
|
+
background: { type: ["string", "null"] },
|
|
1052
|
+
name: {
|
|
1053
|
+
type: "string",
|
|
1054
|
+
description: "File name without extension (default: the document id)."
|
|
1055
|
+
},
|
|
1056
|
+
out: {
|
|
1057
|
+
type: "string",
|
|
1058
|
+
description: "Output path inside the project (default dist/<name>.<ext>)."
|
|
1059
|
+
},
|
|
1060
|
+
variants: {
|
|
1061
|
+
type: "array",
|
|
1062
|
+
items: {
|
|
1063
|
+
type: "object",
|
|
1064
|
+
additionalProperties: true
|
|
1065
|
+
},
|
|
1066
|
+
description: "Several export requests (same fields) in one call."
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
},
|
|
1070
|
+
mutating: true,
|
|
1071
|
+
needsEngine: true,
|
|
1072
|
+
async handler(ctx, input) {
|
|
1073
|
+
const e = engine(ctx);
|
|
1074
|
+
const doc = await getDoc(ctx.fs);
|
|
1075
|
+
const base = {
|
|
1076
|
+
format: optStr(input, "format"),
|
|
1077
|
+
quality: num(input, "quality"),
|
|
1078
|
+
preset: optStr(input, "preset"),
|
|
1079
|
+
width: num(input, "width"),
|
|
1080
|
+
height: num(input, "height"),
|
|
1081
|
+
scale: num(input, "scale"),
|
|
1082
|
+
fit: optStr(input, "fit"),
|
|
1083
|
+
background: input.background === null ? null : optStr(input, "background"),
|
|
1084
|
+
name: optStr(input, "name"),
|
|
1085
|
+
out: optStr(input, "out")
|
|
1086
|
+
};
|
|
1087
|
+
const reqs = Array.isArray(input.variants) && input.variants.length ? input.variants.map((v) => ({
|
|
1088
|
+
...base,
|
|
1089
|
+
out: void 0,
|
|
1090
|
+
...v
|
|
1091
|
+
})) : [base];
|
|
1092
|
+
const files = [];
|
|
1093
|
+
let measured;
|
|
1094
|
+
for (const req of reqs) {
|
|
1095
|
+
const job = planExport(doc, req);
|
|
1096
|
+
const img = await e.exportImage(doc, job);
|
|
1097
|
+
let path = job.file;
|
|
1098
|
+
let note;
|
|
1099
|
+
if (img.mime !== job.mime) {
|
|
1100
|
+
path = path.replace(/\.[^.]+$/, `.${EXT[img.mime] ?? "png"}`);
|
|
1101
|
+
note = `${job.format.toUpperCase()} encoding is not supported by this browser: written as ${img.mime}.`;
|
|
1102
|
+
}
|
|
1103
|
+
await ctx.fs.writeBytes(path, img.bytes);
|
|
1104
|
+
if ((job.source.x > .5 || job.source.y > .5 || job.source.width < doc.canvas.width - 1 || job.source.height < doc.canvas.height - 1) && measured === void 0) measured = await e.layerBoxes?.(doc).catch(() => void 0) ?? {};
|
|
1105
|
+
const cut = coverCuts(doc, job, measured);
|
|
1106
|
+
if (cut.length) note = `${note ? `${note} ` : ""}fit "cover" crops the canvas to ${Math.round(job.source.width)}×${Math.round(job.source.height)} and cuts layer${cut.length > 1 ? "s" : ""} ${cut.join(", ")}: use fit "contain", or adapt the layout (doc_patch resizeCanvas / move layers) for this size.`;
|
|
1107
|
+
if (job.scale > 1.05) note = `${note ? `${note} ` : ""}Upscaled ×${Math.round(job.scale * 100) / 100} from the ${doc.canvas.width}×${doc.canvas.height} canvas: it may look soft.`;
|
|
1108
|
+
files.push({
|
|
1109
|
+
path,
|
|
1110
|
+
format: img.mime === "image/jpeg" ? "jpeg" : EXT[img.mime] ?? job.format,
|
|
1111
|
+
width: img.width,
|
|
1112
|
+
height: img.height,
|
|
1113
|
+
kb: Math.round(img.bytes.byteLength / 102.4) / 10,
|
|
1114
|
+
...note ? { note } : {}
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
return {
|
|
1118
|
+
files,
|
|
1119
|
+
hint: "Done. Tell the user where the files are; offer other sizes (variants) if useful."
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
name: "image_analyze",
|
|
1125
|
+
description: "Measure the picture (or one layer): brightness, contrast, tonal range, clipped shadows / highlights, colour cast, saturation, sharpness, dominant colours — with suggested filters (ready to pass to filter_add). Use before retouching.",
|
|
1126
|
+
inputSchema: {
|
|
1127
|
+
type: "object",
|
|
1128
|
+
properties: { layer: {
|
|
1129
|
+
type: "string",
|
|
1130
|
+
description: "Analyse this layer's own pixels (default: the whole render)."
|
|
1131
|
+
} }
|
|
1132
|
+
},
|
|
1133
|
+
mutating: false,
|
|
1134
|
+
needsEngine: true,
|
|
1135
|
+
async handler(ctx, input) {
|
|
1136
|
+
const doc = await getDoc(ctx.fs);
|
|
1137
|
+
const layer = optStr(input, "layer");
|
|
1138
|
+
if (layer) layerOf(doc, layer);
|
|
1139
|
+
return engine(ctx).analyze(doc, { layer });
|
|
1140
|
+
}
|
|
1141
|
+
},
|
|
1142
|
+
{
|
|
1143
|
+
name: "remove_background",
|
|
1144
|
+
description: `Remove the background of a photo layer with local AI (no upload): writes a mask PNG to assets/masks/ and sets it as the layer's mask — non-destructive (mask_set null restores). Optional background: a colour / gradient placed under the cut-out (adds a fill layer), shadow: true adds a soft drop shadow. Models: ${MODELS.filter((m) => m.task === "background-removal").map((m) => `${m.id} (${m.description})`).join("; ")}. First use downloads the model once (25–170 MB, cached).`,
|
|
1145
|
+
inputSchema: {
|
|
1146
|
+
type: "object",
|
|
1147
|
+
properties: {
|
|
1148
|
+
id: {
|
|
1149
|
+
type: "string",
|
|
1150
|
+
description: "Image layer id."
|
|
1151
|
+
},
|
|
1152
|
+
model: {
|
|
1153
|
+
type: "string",
|
|
1154
|
+
enum: MODELS.filter((m) => m.task === "background-removal").map((m) => m.id)
|
|
1155
|
+
},
|
|
1156
|
+
feather: {
|
|
1157
|
+
type: "number",
|
|
1158
|
+
description: "Soften the edge, px (default 0)."
|
|
1159
|
+
},
|
|
1160
|
+
threshold: {
|
|
1161
|
+
type: "integer",
|
|
1162
|
+
description: "Hard edge: keep pixels whose confidence ≥ threshold (0..255). Default: soft alpha (best for hair)."
|
|
1163
|
+
},
|
|
1164
|
+
background: {
|
|
1165
|
+
type: ["string", "object"],
|
|
1166
|
+
description: "Colour / gradient to put behind the subject (adds a fill layer below it)."
|
|
1167
|
+
},
|
|
1168
|
+
shadow: {
|
|
1169
|
+
type: "boolean",
|
|
1170
|
+
description: "Add a soft drop shadow under the subject."
|
|
1171
|
+
},
|
|
1172
|
+
fit: {
|
|
1173
|
+
type: ["object", "boolean"],
|
|
1174
|
+
description: "Scale and move the layer so the cut-out subject fits this canvas box: { x, y, width, height, anchor? } (anchor: center (default), bottom = standing on a floor line, top, left…). false leaves the layer where it is. Default: a template's subject area on its photo slot (first cut-out after a photo swap), otherwise nothing moves.",
|
|
1175
|
+
additionalProperties: true
|
|
1176
|
+
}
|
|
1177
|
+
},
|
|
1178
|
+
required: ["id"]
|
|
1179
|
+
},
|
|
1180
|
+
mutating: true,
|
|
1181
|
+
needsEngine: true,
|
|
1182
|
+
async handler(ctx, input) {
|
|
1183
|
+
const id = str(input, "id");
|
|
1184
|
+
const doc = await getDoc(ctx.fs);
|
|
1185
|
+
const layer = layerOf(doc, id);
|
|
1186
|
+
if (layer.type !== "image") throw new OpsError("invalid", `"${id}" is a ${layer.type} layer: background removal works on image layers.`);
|
|
1187
|
+
const fit = await fitTarget(ctx, layer, input.fit);
|
|
1188
|
+
const model = optStr(input, "model") ?? DEFAULT_MODELS["background-removal"];
|
|
1189
|
+
const img = await engine(ctx).backgroundMask(doc, id, {
|
|
1190
|
+
model,
|
|
1191
|
+
feather: num(input, "feather"),
|
|
1192
|
+
threshold: num(input, "threshold")
|
|
1193
|
+
});
|
|
1194
|
+
const extra = [];
|
|
1195
|
+
const found = subjectBox(layer, img);
|
|
1196
|
+
if (fit && found) extra.push({
|
|
1197
|
+
op: "fitLayer",
|
|
1198
|
+
id,
|
|
1199
|
+
box: fit.box,
|
|
1200
|
+
subject: found,
|
|
1201
|
+
anchor: fit.anchor
|
|
1202
|
+
});
|
|
1203
|
+
if (input.background !== void 0 && input.background !== null) {
|
|
1204
|
+
const bgId = uniqueId(`${id}-background`, allLayerIds(doc));
|
|
1205
|
+
extra.push({
|
|
1206
|
+
op: "addLayer",
|
|
1207
|
+
layer: {
|
|
1208
|
+
id: bgId,
|
|
1209
|
+
type: "fill",
|
|
1210
|
+
name: "Background",
|
|
1211
|
+
fill: input.background
|
|
1212
|
+
},
|
|
1213
|
+
below: id
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
if (bool(input, "shadow")) extra.push({
|
|
1217
|
+
op: "updateLayer",
|
|
1218
|
+
id,
|
|
1219
|
+
set: { effects: { dropShadow: {
|
|
1220
|
+
color: "#000000",
|
|
1221
|
+
opacity: .35,
|
|
1222
|
+
angle: 90,
|
|
1223
|
+
distance: 18,
|
|
1224
|
+
blur: 30
|
|
1225
|
+
} } }
|
|
1226
|
+
});
|
|
1227
|
+
const r = await writeMask(ctx, doc, id, img, {
|
|
1228
|
+
label: "cutout",
|
|
1229
|
+
extraOps: extra,
|
|
1230
|
+
source: `remove_background:${model}`
|
|
1231
|
+
});
|
|
1232
|
+
const fitted = fit && found && r.applied === true ? {
|
|
1233
|
+
box: fit.box,
|
|
1234
|
+
anchor: fit.anchor,
|
|
1235
|
+
from: fit.from
|
|
1236
|
+
} : void 0;
|
|
1237
|
+
const place = fitted ? `The layer was scaled and moved so the subject fits ${JSON.stringify(fitted.box)} (anchor ${fitted.anchor}, from the ${fitted.from}); fit: false keeps the layer where it was.` : `To recentre / rescale it: doc_patch [{ "op": "fitLayer", "id": "${id}", "subject": <subject>, "box": { "x", "y", "width", "height" }, "anchor": "center" | "bottom" }] (or remove_background again with fit).`;
|
|
1238
|
+
return {
|
|
1239
|
+
...r,
|
|
1240
|
+
model,
|
|
1241
|
+
...fitted ? { fitted } : {},
|
|
1242
|
+
hint: `subject = the kept area in canvas px (null: nothing found). ${place} Then render_preview (zoom on an edge: hair, fingers) to check; refine with mask_generate (points) or threshold / feather.`
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
},
|
|
1246
|
+
{
|
|
1247
|
+
name: "mask_generate",
|
|
1248
|
+
description: "Make a mask for a layer from pixels, with local AI or colours, and set it as the layer mask: method \"subject\" (main subject, background-removal model), \"points\" (Segment Anything: points [[x, y, 1 = include | 0 = exclude], …] in layer px), \"box\" (SAM: [x1, y1, x2, y2] layer px), \"color\" (colour range: color + tolerance 0..100). invert: true keeps the rest instead (e.g. select the sky, then invert to protect it; or mask a filter / adjustment). target: \"mask\" (default) sets it on the layer itself; \"copy\" duplicates the layer, masks the copy (cut-out on its own layer, the original stays).",
|
|
1249
|
+
inputSchema: {
|
|
1250
|
+
type: "object",
|
|
1251
|
+
properties: {
|
|
1252
|
+
id: {
|
|
1253
|
+
type: "string",
|
|
1254
|
+
description: "Layer id (layer px = relative to the layer's top-left, before rotation)."
|
|
1255
|
+
},
|
|
1256
|
+
method: {
|
|
1257
|
+
type: "string",
|
|
1258
|
+
enum: [
|
|
1259
|
+
"subject",
|
|
1260
|
+
"points",
|
|
1261
|
+
"box",
|
|
1262
|
+
"color"
|
|
1263
|
+
]
|
|
1264
|
+
},
|
|
1265
|
+
points: {
|
|
1266
|
+
type: "array",
|
|
1267
|
+
items: {
|
|
1268
|
+
type: "array",
|
|
1269
|
+
items: { type: "number" }
|
|
1270
|
+
}
|
|
1271
|
+
},
|
|
1272
|
+
box: {
|
|
1273
|
+
type: "array",
|
|
1274
|
+
items: { type: "number" },
|
|
1275
|
+
minItems: 4,
|
|
1276
|
+
maxItems: 4
|
|
1277
|
+
},
|
|
1278
|
+
color: { type: "string" },
|
|
1279
|
+
tolerance: {
|
|
1280
|
+
type: "number",
|
|
1281
|
+
minimum: 0,
|
|
1282
|
+
maximum: 100
|
|
1283
|
+
},
|
|
1284
|
+
invert: { type: "boolean" },
|
|
1285
|
+
feather: { type: "number" },
|
|
1286
|
+
model: { type: "string" },
|
|
1287
|
+
target: {
|
|
1288
|
+
type: "string",
|
|
1289
|
+
enum: ["mask", "copy"]
|
|
1290
|
+
},
|
|
1291
|
+
copyId: {
|
|
1292
|
+
type: "string",
|
|
1293
|
+
description: "Id of the copy (target \"copy\")."
|
|
1294
|
+
}
|
|
1295
|
+
},
|
|
1296
|
+
required: ["id", "method"]
|
|
1297
|
+
},
|
|
1298
|
+
mutating: true,
|
|
1299
|
+
needsEngine: true,
|
|
1300
|
+
async handler(ctx, input) {
|
|
1301
|
+
const id = str(input, "id");
|
|
1302
|
+
const method = str(input, "method");
|
|
1303
|
+
const doc = await getDoc(ctx.fs);
|
|
1304
|
+
layerOf(doc, id);
|
|
1305
|
+
const e = engine(ctx);
|
|
1306
|
+
const feather = num(input, "feather");
|
|
1307
|
+
let img;
|
|
1308
|
+
if (method === "subject") img = await e.backgroundMask(doc, id, {
|
|
1309
|
+
model: optStr(input, "model"),
|
|
1310
|
+
feather
|
|
1311
|
+
});
|
|
1312
|
+
else if (method === "points" || method === "box") {
|
|
1313
|
+
const points = method === "points" ? input.points : void 0;
|
|
1314
|
+
if (method === "points" && (!Array.isArray(points) || !points.length)) throw new OpsError("invalid", "method \"points\" needs points: [[x, y, 1], …] (layer px; 1 = include, 0 = exclude).");
|
|
1315
|
+
const box = method === "box" ? input.box : void 0;
|
|
1316
|
+
if (method === "box" && (!Array.isArray(box) || box.length !== 4)) throw new OpsError("invalid", "method \"box\" needs box: [x1, y1, x2, y2] (layer px).");
|
|
1317
|
+
img = await e.segmentMask(doc, id, {
|
|
1318
|
+
...points ? { points: points.map((p) => [
|
|
1319
|
+
p[0],
|
|
1320
|
+
p[1],
|
|
1321
|
+
p[2] ?? 1
|
|
1322
|
+
]) } : {},
|
|
1323
|
+
...box ? { box } : {}
|
|
1324
|
+
}, {
|
|
1325
|
+
model: optStr(input, "model"),
|
|
1326
|
+
feather
|
|
1327
|
+
});
|
|
1328
|
+
} else if (method === "color") img = await e.colorRangeMask(doc, id, {
|
|
1329
|
+
color: str(input, "color"),
|
|
1330
|
+
tolerance: num(input, "tolerance"),
|
|
1331
|
+
feather
|
|
1332
|
+
});
|
|
1333
|
+
else throw new OpsError("invalid", `Unknown method "${method}".`, "Methods: subject, points, box, color.");
|
|
1334
|
+
const target = optStr(input, "target") ?? "mask";
|
|
1335
|
+
const invert = bool(input, "invert");
|
|
1336
|
+
if (target === "copy") {
|
|
1337
|
+
const copyId = uniqueId(optStr(input, "copyId") ?? `${id}-cutout`, allLayerIds(doc));
|
|
1338
|
+
return {
|
|
1339
|
+
...await writeMask(ctx, doc, id, img, {
|
|
1340
|
+
label: method,
|
|
1341
|
+
invert,
|
|
1342
|
+
source: `mask_generate:${method}`,
|
|
1343
|
+
preOps: [{
|
|
1344
|
+
op: "duplicateLayer",
|
|
1345
|
+
id,
|
|
1346
|
+
newId: copyId
|
|
1347
|
+
}],
|
|
1348
|
+
maskId: copyId
|
|
1349
|
+
}),
|
|
1350
|
+
layer: copyId
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
return writeMask(ctx, doc, id, img, {
|
|
1354
|
+
label: method,
|
|
1355
|
+
invert,
|
|
1356
|
+
source: `mask_generate:${method}`
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
];
|
|
1361
|
+
// src/ai/tools.ts
|
|
1362
|
+
var TOOL_ORDER = [
|
|
1363
|
+
"project_info",
|
|
1364
|
+
"project_create",
|
|
1365
|
+
"doc_summary",
|
|
1366
|
+
"doc_get",
|
|
1367
|
+
"doc_patch",
|
|
1368
|
+
"doc_apply",
|
|
1369
|
+
"doc_validate",
|
|
1370
|
+
"layer_list",
|
|
1371
|
+
"layer_add",
|
|
1372
|
+
"layer_update",
|
|
1373
|
+
"layer_remove",
|
|
1374
|
+
"layer_move",
|
|
1375
|
+
"filter_add",
|
|
1376
|
+
"mask_set",
|
|
1377
|
+
"remove_background",
|
|
1378
|
+
"mask_generate",
|
|
1379
|
+
"image_analyze",
|
|
1380
|
+
"render_preview",
|
|
1381
|
+
"catalog_get",
|
|
1382
|
+
"templates_list",
|
|
1383
|
+
"library_list",
|
|
1384
|
+
"assets_list",
|
|
1385
|
+
"assets_add",
|
|
1386
|
+
"assets_remove",
|
|
1387
|
+
"check",
|
|
1388
|
+
"export"
|
|
1389
|
+
];
|
|
1390
|
+
var BY_NAME = new Map([...DOC_TOOLS, ...IMAGE_TOOLS].map((t) => [t.name, t]));
|
|
1391
|
+
var AI_TOOLS = TOOL_ORDER.map((n) => {
|
|
1392
|
+
const t = BY_NAME.get(n);
|
|
1393
|
+
if (!t) throw new Error(`AI tool ${n} is not implemented`);
|
|
1394
|
+
return t;
|
|
1395
|
+
});
|
|
1396
|
+
function listAiTools(host, opts = {}) {
|
|
1397
|
+
const list = AI_TOOLS.filter((t) => !t.needsEngine || host?.engine);
|
|
1398
|
+
return opts.readOnly ? list.filter((t) => !t.mutating) : list;
|
|
1399
|
+
}
|
|
1400
|
+
function findAiTool(name, host) {
|
|
1401
|
+
const tools = listAiTools(host);
|
|
1402
|
+
const t = tools.find((x) => x.name === name);
|
|
1403
|
+
if (!t) {
|
|
1404
|
+
if (BY_NAME.get(name)?.needsEngine) throw new OpsError("unavailable", `Tool "${name}" needs an image engine this host does not have.`, "Use the CLI (needs Google Chrome / Chromium) or the Studio.");
|
|
1405
|
+
const s = closest(name, tools.map((x) => x.name));
|
|
1406
|
+
throw new OpsError("notFound", `Unknown tool "${name}".`, `${s ? `Did you mean "${s}"? ` : ""}Tools: ${tools.map((x) => x.name).join(", ")}.`);
|
|
1407
|
+
}
|
|
1408
|
+
return t;
|
|
1409
|
+
}
|
|
1410
|
+
async function callAiTool(ctx, name, input) {
|
|
1411
|
+
const tool = findAiTool(name, ctx.host);
|
|
1412
|
+
return tool.handler(ctx, checkInput(tool.inputSchema, input, name));
|
|
1413
|
+
}
|
|
1414
|
+
function resultImages(name, result) {
|
|
1415
|
+
if (name !== "render_preview" || !result || typeof result !== "object") return [];
|
|
1416
|
+
const r = result;
|
|
1417
|
+
return r.images?.length ? r.images : r.preview ? [r.preview] : [];
|
|
1418
|
+
}
|
|
1419
|
+
export { callAiTool, listAiTools, resultImages };
|