@insmind/cli 2.1.0 → 2.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.
Files changed (95) hide show
  1. package/contracts/operations/agent.send/input.schema.json +1 -77
  2. package/contracts/operations/dam.folder.list/input.schema.json +0 -1
  3. package/contracts/operations/dam.folder.list/output.schema.json +1 -2
  4. package/contracts/operations/dam.list/input.schema.json +3 -6
  5. package/contracts/operations/dam.list/output.schema.json +1 -2
  6. package/contracts/operations/dam.search/input.schema.json +3 -6
  7. package/contracts/operations/dam.search/output.schema.json +1 -2
  8. package/contracts/operations/tool.call/models/image-edit-smart.schema.json +1 -2
  9. package/contracts/operations/tool.call/models/image-expand-smart.schema.json +1 -2
  10. package/contracts/operations/tool.call/models/image-layers-smart.schema.json +1 -3
  11. package/contracts/operations/tool.call/models/video-edit-smart.schema.json +0 -3
  12. package/contracts/operations/tool.call/ocr-payload.schema.json +4 -5
  13. package/dist/bin/postinstall.js +22 -6
  14. package/dist/bin/task-worker.js +2 -2
  15. package/dist/src/bootstrap/create-cli.js +13 -6
  16. package/dist/src/bootstrap/create-runtime.js +98 -28
  17. package/dist/src/bootstrap/run-task-worker.js +6 -2
  18. package/dist/src/bootstrap/validators.js +1 -1
  19. package/dist/src/cli/action-binding.js +14 -37
  20. package/dist/src/cli/agent-commands.js +11 -11
  21. package/dist/src/cli/auth-commands.js +2 -1
  22. package/dist/src/cli/creation-help.js +9 -0
  23. package/dist/src/cli/dam-commands.js +10 -14
  24. package/dist/src/cli/editor-commands.js +7 -5
  25. package/dist/src/cli/errors.js +81 -22
  26. package/dist/src/cli/host-input.js +64 -5
  27. package/dist/src/cli/model-commands.js +9 -3
  28. package/dist/src/cli/observed-errors.js +7 -3
  29. package/dist/src/cli/org-commands.js +3 -1
  30. package/dist/src/cli/task-commands.js +6 -2
  31. package/dist/src/cli/tool-commands.js +12 -11
  32. package/dist/src/features/agent/creative-agent-adapter.js +6 -2
  33. package/dist/src/features/agent/creative-protocol.js +8 -29
  34. package/dist/src/features/agent/resource-authorizer.js +25 -17
  35. package/dist/src/features/agent/result-reader.js +31 -14
  36. package/dist/src/features/agent/use-cases.js +6 -31
  37. package/dist/src/features/credits/use-cases.js +55 -28
  38. package/dist/src/features/dam/asset-projection.js +3 -6
  39. package/dist/src/features/dam/dam-api-adapter.js +30 -15
  40. package/dist/src/features/dam/repository-catalog.js +30 -21
  41. package/dist/src/features/dam/use-cases.js +12 -7
  42. package/dist/src/features/editor/bridge-client.js +0 -2
  43. package/dist/src/features/editor/public-url.js +1 -36
  44. package/dist/src/features/editor/session.js +3 -2
  45. package/dist/src/features/editor/use-cases.js +27 -2
  46. package/dist/src/features/org/org-service.js +10 -1
  47. package/dist/src/features/org/use-cases.js +18 -4
  48. package/dist/src/features/task/process-owner.js +54 -0
  49. package/dist/src/features/task/store.js +37 -10
  50. package/dist/src/features/task/task-background.js +5 -2
  51. package/dist/src/features/task/use-cases.js +184 -84
  52. package/dist/src/features/tool/argument-paths.js +14 -0
  53. package/dist/src/features/tool/canvas-copy.js +28 -15
  54. package/dist/src/features/tool/canvas-matting.js +123 -0
  55. package/dist/src/features/tool/canvas-media.js +100 -52
  56. package/dist/src/features/tool/canvas-models.js +66 -33
  57. package/dist/src/features/tool/canvas-pricing.js +12 -0
  58. package/dist/src/features/tool/canvas-recovery.js +13 -30
  59. package/dist/src/features/tool/canvas-results.js +79 -87
  60. package/dist/src/features/tool/canvas-tool-adapter.js +134 -157
  61. package/dist/src/features/tool/canvas-video.js +44 -37
  62. package/dist/src/features/tool/dynamic-schema.js +39 -40
  63. package/dist/src/features/tool/ffprobe-executable.js +21 -0
  64. package/dist/src/features/tool/mns-catalog-adapter.js +61 -24
  65. package/dist/src/features/tool/resource-authorizer.js +11 -3
  66. package/dist/src/features/tool/tool-api-adapter.js +73 -82
  67. package/dist/src/features/tool/tool-errors.js +25 -0
  68. package/dist/src/features/tool/tool-output.js +15 -0
  69. package/dist/src/features/tool/use-cases.js +74 -98
  70. package/dist/src/features/update/update-service.js +95 -21
  71. package/dist/src/platform/media-type.js +46 -0
  72. package/dist/src/platform/public-url.js +35 -0
  73. package/dist/src/platform/remote-error-evidence.js +33 -6
  74. package/dist/src/platform/resource-auth-key.js +35 -1
  75. package/dist/src/platform/safe-upload-file.js +36 -41
  76. package/dist/src/platform/signature.js +2 -2
  77. package/dist/src/platform/signed-http-transport.js +4 -1
  78. package/dist/src/platform/url-safety.js +6 -1
  79. package/dist/src/platform/webm-header.js +41 -0
  80. package/dist/src/telemetry/command-route.js +38 -0
  81. package/dist/src/telemetry/command-routes.json +1 -0
  82. package/dist/src/telemetry/failure-summary.js +1 -1
  83. package/dist/src/telemetry/invocation.js +1 -2
  84. package/dist/src/telemetry/process-errors.js +17 -33
  85. package/dist/src/telemetry/sanitize.js +19 -4
  86. package/dist/src/telemetry/sls-sink.js +8 -0
  87. package/package.json +4 -4
  88. package/skills/insmind-cli/SKILL.md +18 -5
  89. package/skills/insmind-cli/references/agent.md +9 -4
  90. package/skills/insmind-cli/references/creation.md +3 -1
  91. package/skills/insmind-cli/references/editor.md +7 -5
  92. package/skills/insmind-cli/references/errors.md +20 -9
  93. package/skills/insmind-cli/references/matting.md +3 -5
  94. package/skills/insmind-cli/references/media-tools.md +10 -4
  95. package/skills/insmind-cli/references/tool.md +9 -15
@@ -2,8 +2,10 @@ import { randomUUID } from "node:crypto";
2
2
  import { Ajv2020 } from "ajv/dist/2020.js";
3
3
  import addFormatsImport, {} from "ajv-formats";
4
4
  import { contractDocument } from "../../contracts/schema.js";
5
+ import { parseHttpAssetUrl, parseSafeRemoteAssetUrl } from "../../platform/url-safety.js";
5
6
  import { RemoteProtocolError } from "../../platform/remote-protocol.js";
6
7
  import { normalizeToolContent } from "./tool-api-adapter.js";
8
+ import { readOutputMedia } from "./canvas-media.js";
7
9
  export function record(value) {
8
10
  return value !== null && typeof value === "object" && !Array.isArray(value);
9
11
  }
@@ -12,20 +14,27 @@ export function canvasContent(value) {
12
14
  const resource = value.resource;
13
15
  // Real RPC resources include nullable text/image/task slots. URI wins for media;
14
16
  // OCR's inline JSON has text rather than a usable media URI.
15
- if (typeof resource.uri === "string" && resource.uri.trim() && resource.mimeType !== "application/json") {
16
- return normalizeToolContent({ type: "resource", resource: { uri: resource.uri,
17
- ...(typeof resource.mimeType === "string" && resource.mimeType ? { mimeType: resource.mimeType } : {}) } });
18
- }
17
+ if (typeof resource.uri === "string" && resource.uri.trim())
18
+ return normalizeToolContent(value);
19
19
  if (typeof resource.text === "string" && resource.text.trim())
20
20
  return { type: "text", text: resource.text };
21
21
  }
22
22
  return normalizeToolContent(value);
23
23
  }
24
+ function outputUri(value) {
25
+ try {
26
+ parseSafeRemoteAssetUrl(value);
27
+ }
28
+ catch (cause) {
29
+ throw new RemoteProtocolError({}, cause, "TOOL_RESOURCE_URI_INVALID");
30
+ }
31
+ return value;
32
+ }
24
33
  export function oneResource(content) {
25
34
  const resources = content.filter(item => item.type === "resource_link");
26
35
  if (resources.length !== 1)
27
36
  throw new RemoteProtocolError({}, undefined, "TOOL_SINGLE_RESOURCE_EXPECTED");
28
- return resources[0].uri;
37
+ return outputUri(resources[0].uri);
29
38
  }
30
39
  export function link(uri, name, mimeType) {
31
40
  return [{ type: "resource_link", uri, name, mimeType }];
@@ -37,11 +46,6 @@ const validateOcr = ajv.compile(contractDocument("tool.call/ocr-payload.schema.j
37
46
  function assertOcrPayload(payload) {
38
47
  if (!validateOcr(payload))
39
48
  throw new RemoteProtocolError({}, undefined, "OCR_PAYLOAD_INVALID");
40
- for (const block of payload.blocks) {
41
- const [l, t, r, b] = block.bbox;
42
- if (l > r || t > b || r > payload.image.width || b > payload.image.height)
43
- throw new RemoteProtocolError({}, undefined, "OCR_COORDINATES_INVALID");
44
- }
45
49
  }
46
50
  export function readOcrPayload(text) {
47
51
  let payload;
@@ -55,90 +59,78 @@ export function readOcrPayload(text) {
55
59
  return payload;
56
60
  }
57
61
  export function ocrResult(content, image, id = randomUUID()) {
58
- const texts = content.filter(item => item.type === "text");
59
- if (texts.length !== 1)
60
- throw new RemoteProtocolError({}, undefined, "OCR_PAYLOAD_MISSING");
61
- let blocks;
62
- try {
63
- blocks = JSON.parse(texts[0].text);
64
- }
65
- catch (cause) {
66
- throw new RemoteProtocolError({}, cause, "OCR_JSON_INVALID");
67
- }
68
- const payload = { image, blocks };
69
- assertOcrPayload(payload);
70
- return [{ type: "resource", resource: { uri: `urn:uuid:${id}`, mimeType: "application/json", text: JSON.stringify(payload) } }];
71
- }
72
- export async function imageResult(content, media, signal) {
73
- const { default: sharp } = await import("sharp");
74
- const uri = oneResource(content);
75
- const bytes = await media.read(uri, signal);
76
- const metadata = await sharp(bytes).metadata();
77
- if (!metadata.width || !metadata.height || !["jpeg", "png", "webp", "avif"].includes(metadata.format ?? "")) {
78
- throw new RemoteProtocolError({}, undefined, "TOOL_IMAGE_INVALID");
62
+ const result = [];
63
+ for (const part of content) {
64
+ if (part.type === "resource_link") {
65
+ result.push(part);
66
+ continue;
67
+ }
68
+ let blocks;
69
+ try {
70
+ blocks = JSON.parse(part.type === "text" ? part.text : part.resource.text);
71
+ }
72
+ catch {
73
+ result.push(part);
74
+ continue;
75
+ }
76
+ const payload = { image, blocks };
77
+ if (!Array.isArray(blocks) || !validateOcr(payload)) {
78
+ result.push(part);
79
+ continue;
80
+ }
81
+ result.push({ type: "resource", resource: { uri: part.type === "resource" ? part.resource.uri : `urn:uuid:${result.some(item => item.type === "resource") ? randomUUID() : id}`,
82
+ mimeType: "application/json", text: JSON.stringify(payload) } });
79
83
  }
80
- // Decode the pixels as well: a valid header is insufficient evidence of a complete resource.
81
- await sharp(bytes).stats();
82
- return link(uri, `result.${metadata.format === "jpeg" ? "jpg" : metadata.format}`, `image/${metadata.format}`);
83
- }
84
- export async function vectorResult(content, media, signal) {
85
- const { default: sharp } = await import("sharp");
86
- const uri = oneResource(content);
87
- const bytes = await media.read(uri, signal);
88
- if ((await sharp(bytes).metadata()).format !== "svg")
89
- throw new RemoteProtocolError({}, undefined, "SVG_INVALID");
90
- return link(uri, "result.svg", "image/svg+xml");
84
+ return result;
91
85
  }
92
86
  export async function templateResult(content, media, signal, english = false) {
93
- const text = content.find(item => item.type === "text");
94
- if (!text || text.type !== "text")
95
- throw new RemoteProtocolError({}, undefined, "TEMPLATE_URL_MISSING");
96
- const uri = text.text.trim();
97
- let template;
87
+ const explicit = content.flatMap((part, i) => part.type === "resource_link" &&
88
+ (part.mimeType === "application/json" || part.name.toLowerCase().endsWith(".json")) ? [i] : []);
89
+ const candidates = explicit.length ? explicit : content.flatMap((part, i) => {
90
+ if (part.type !== "text")
91
+ return [];
92
+ try {
93
+ parseHttpAssetUrl(part.text.trim());
94
+ return /\s/u.test(part.text.trim()) ? [] : [i];
95
+ }
96
+ catch {
97
+ return [];
98
+ }
99
+ });
100
+ if (candidates.length !== 1)
101
+ return content;
102
+ const index = candidates[0];
103
+ const selected = content[index];
104
+ const uri = selected.type === "text" ? selected.text.trim() : selected.type === "resource_link" ? selected.uri : "";
105
+ const result = [...content];
106
+ if (selected.type === "text")
107
+ result[index] = link(uri, "template.json", "application/json")[0];
98
108
  try {
99
- template = JSON.parse((await media.read(uri, signal)).toString("utf8"));
100
- }
101
- catch (cause) {
102
- throw new RemoteProtocolError({}, cause, "TEMPLATE_JSON_INVALID");
103
- }
104
- if (!validateTemplate(template) || !record(template) || !Array.isArray(template.pages) || template.pages.length === 0)
105
- throw new RemoteProtocolError({}, undefined, "TEMPLATE_PAGES_MISSING");
106
- const types = new Map();
107
- const assets = new Set();
108
- function inspectElements(elements) {
109
- for (const element of elements) {
109
+ const bytes = await readOutputMedia(media, outputUri(uri), signal);
110
+ const resolved = media.deliveryUri?.(uri) ?? uri;
111
+ const part = result[index];
112
+ if (part?.type === "resource_link" && resolved !== uri)
113
+ result[index] = { ...part, uri: resolved };
114
+ const template = JSON.parse(bytes.toString("utf8"));
115
+ if (!validateTemplate(template) || !record(template) || !Array.isArray(template.pages))
116
+ throw new RemoteProtocolError();
117
+ const types = new Map();
118
+ const stack = template.pages.flatMap((page) => page.elements);
119
+ while (stack.length) {
120
+ const element = stack.pop();
110
121
  if (!record(element) || typeof element.type !== "string")
111
- throw new RemoteProtocolError({}, undefined, "TEMPLATE_ELEMENT_INVALID");
122
+ continue;
112
123
  types.set(element.type, (types.get(element.type) ?? 0) + 1);
113
- if (typeof element.url === "string" && element.url)
114
- assets.add(element.url);
115
- if (record(element.background) && record(element.background.image) && typeof element.background.image.url === "string" && element.background.image.url)
116
- assets.add(element.background.image.url);
117
- if (element.elements !== undefined) {
118
- if (!Array.isArray(element.elements))
119
- throw new RemoteProtocolError();
120
- inspectElements(element.elements);
121
- }
124
+ if (Array.isArray(element.elements))
125
+ stack.push(...element.elements);
122
126
  }
127
+ const counts = [...types].map(([type, count]) => `${type}: ${count}`).join("; ");
128
+ result.push({ type: "text", text: english ? `Native template, ${counts}. The editor matches fonts and may substitute missing fonts.`
129
+ : `原生模板,${counts}。字体由编辑器匹配,缺失字体可能替换。` });
123
130
  }
124
- for (const page of template.pages) {
125
- if (!record(page) || !Array.isArray(page.elements) || page.elements.length === 0)
126
- throw new RemoteProtocolError();
127
- for (const layout of page.elements) {
128
- if (!record(layout) || typeof layout.width !== "number" || layout.width <= 0
129
- || typeof layout.height !== "number" || layout.height <= 0 || !record(layout.background)
130
- || !record(layout.background.image) || !Array.isArray(layout.elements))
131
- throw new RemoteProtocolError({}, undefined, "TEMPLATE_LAYOUT_INVALID");
132
- }
133
- inspectElements(page.elements);
134
- }
135
- for (const asset of assets) {
136
- const bytes = await media.read(asset, signal);
137
- if (bytes.length === 0)
138
- throw new RemoteProtocolError();
131
+ catch {
132
+ signal.throwIfAborted();
133
+ result.push({ type: "text", text: english ? "The template link is included. Its layer summary is unavailable." : "已返回模板链接,暂未取得图层摘要。" });
139
134
  }
140
- const counts = [...types].map(([type, count]) => `${type}: ${count}`).join("; ");
141
- return [...link(uri, "template.json", "application/json"), { type: "text", text: english
142
- ? `Native template, ${counts}. The editor matches fonts and may substitute missing fonts.`
143
- : `原生模板,${counts}。字体由编辑器匹配,缺失字体可能替换。` }];
135
+ return result;
144
136
  }