@logictan/dsh-stitch-designer 2.0.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,8 @@
1
1
  # @logictan/dsh-stitch-designer
2
2
 
3
- Google Stitch (AI UI generation) inside DSH: the 15 `mcp__stitch__*` tools, plus
4
- the design conventions that keep one code project bound to one Stitch project.
3
+ Google Stitch (AI UI generation) inside DSH: Stitch's own `mcp__stitch__*` tools,
4
+ one local addition (`remove_screens_from_canvas`), plus the design conventions
5
+ that keep one code project bound to one Stitch project.
5
6
 
6
7
  Ported from [liuxinlongwa-hue/dsh-stitch-designer](https://github.com/liuxinlongwa-hue/dsh-stitch-designer)
7
8
  (MIT, © 2026 liuxinlongwa-hue). What changed here is the tool surface, the key
@@ -10,8 +11,9 @@ storage, and the scope — that project's preview panel, screenshot cache and
10
11
 
11
12
  ## What it gives the model
12
13
 
13
- The same 15 tools the `mcp-stitch` MCP row used to expose, under the same
14
- public names, so existing prompts and habits keep working:
14
+ The Stitch tools the `mcp-stitch` MCP row used to expose (and whatever Stitch
15
+ has added since — `download_file` among them), under the same public names, so
16
+ existing prompts and habits keep working:
15
17
 
16
18
  ```
17
19
  mcp__stitch__create_project mcp__stitch__get_project
@@ -28,6 +30,38 @@ The tool list is read from Stitch at activation, not copied into this package,
28
30
  so a tool Stitch adds or renames shows up without a code change. A call budget
29
31
  of 180s covers `generate_screen_from_text`, which is long-running.
30
32
 
33
+ ### `remove_screens_from_canvas` — the one tool Stitch does not offer
34
+
35
+ Stitch has no screen-delete API. `stitch delete screen` answers
36
+ `UNSUPPORTED_RESOURCE / "Stitch screens do not support delete."`, and
37
+ `DELETE /projects/<p>/screens/<s>` answers 404 in every spelling including
38
+ `:delete` (measured 2026-11-08). The MCP tool list has no `delete_screen`
39
+ either, so this is not a capability the bridge failed to expose.
40
+
41
+ What the service does accept is a project-level rewrite of the canvas:
42
+ `PATCH /v1/projects/<id>?updateMask=screenInstances` with the instance list
43
+ minus the unwanted entries. That is what this tool does, and it is why the
44
+ plugin carries a small REST transport (`src/canvas.js`) alongside the MCP one.
45
+
46
+ Removal is **batch by construction, not by looping**: the canvas is a single
47
+ field, so N ids cost one GET and one PATCH. Looping would mean N
48
+ read-modify-write cycles and a lost removal whenever two of them raced. Passing
49
+ a single id in the list is simply the one-element case.
50
+
51
+ Ids that are not on the canvas do not fail the call: the ones that are get
52
+ removed, and the leftovers come back in the result so the caller can see them.
53
+ When *none* of the ids is on the canvas the tool raises instead of writing.
54
+
55
+ The screen object is not destroyed — it stays addressable at
56
+ `GET /v1/projects/<p>/screens/<s>` — but it leaves the project, which is what
57
+ clears abandoned screens out of a project. Renaming a screen's title has no API
58
+ route at all (the canvas `label` is a separate field and does not change the
59
+ screen `title`), so this plugin offers no rename tool.
60
+
61
+ ```
62
+ mcp__stitch__remove_screens_from_canvas { projectId, screenIds: [screenId, ...] }
63
+ ```
64
+
31
65
  ## Settings
32
66
 
33
67
  `Settings → Plugins → stitch-designer`:
package/lib/canvas.js ADDED
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Transport to Stitch's Canvas REST surface, for the one operation its MCP
3
+ * endpoint does not expose: taking a screen off a project's canvas.
4
+ *
5
+ * Stitch has no screen-delete route — `DELETE /projects/<p>/screens/<s>` answers
6
+ * 404 for every spelling including `:delete`, and the `@google/stitch` CLI
7
+ * rejects the command with `UNSUPPORTED_RESOURCE / "Stitch screens do not
8
+ * support delete."` (measured 2026-11-08). The MCP tool list has no
9
+ * `delete_screen` either, so this is not a field the bridge dropped.
10
+ *
11
+ * What the service DOES accept is a project-level PATCH of `screenInstances`:
12
+ * a project's canvas is a list of instances, each pointing at a `sourceScreen`,
13
+ * and writing that list back without one instance removes that screen from the
14
+ * canvas. Measured against project 4697019860827468919: the PATCH returned 200
15
+ * and the project went from five screens to three.
16
+ *
17
+ * Two properties of that endpoint shape this module:
18
+ *
19
+ * - **The list is replaced whole** (`updateMask=screenInstances`), so every
20
+ * surviving instance is sent back verbatim. A field dropped here is a lost
21
+ * canvas position, and the API will not complain.
22
+ * - **The instance id is not the screen id.** A generated screen reuses its id
23
+ * as the instance id, but an uploaded image gets a fresh UUID instance whose
24
+ * `sourceScreen` ends in the screen id. Matching goes through `sourceScreen`,
25
+ * which is correct for both shapes.
26
+ *
27
+ * The screen object itself survives the removal: `GET .../screens/<id>` still
28
+ * answers afterwards. That is the service's model, not a shortfall here — the
29
+ * complaint this addresses is a project canvas full of abandoned screens.
30
+ *
31
+ * @module @logictan/dsh-stitch-designer/canvas
32
+ */
33
+
34
+ /** Stitch's Canvas REST base. Distinct from the MCP endpoint in `stitch.js`. */
35
+ export const CANVAS_API_URL = 'https://stitch.googleapis.com/v1';
36
+
37
+ /** Budget for one Canvas round trip; both calls here are small. */
38
+ export const DEFAULT_CANVAS_TIMEOUT_MS = 60_000;
39
+
40
+ /**
41
+ * One Canvas REST round trip.
42
+ *
43
+ * @param options - method, url, per-call key, body, timeout and abort signal.
44
+ * @returns the parsed JSON response.
45
+ * @throws when the request fails or the response is not JSON.
46
+ */
47
+ async function request(options) {
48
+ const { method, url, apiKey, body, timeoutMs, signal } = options;
49
+ const response = await fetch(url, {
50
+ method,
51
+ headers: {
52
+ 'X-Goog-Api-Key': apiKey,
53
+ ...(body === undefined ? {} : { 'Content-Type': 'application/json' }),
54
+ },
55
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
56
+ signal: AbortSignal.any([signal, AbortSignal.timeout(timeoutMs)].filter(Boolean)),
57
+ });
58
+ if (!response.ok) {
59
+ const detail = await response.text().catch(() => '');
60
+ throw new Error(`Stitch ${method} ${url} failed: HTTP ${response.status}${detail ? ` ${detail.slice(0, 300)}` : ''}`);
61
+ }
62
+ return response.json();
63
+ }
64
+
65
+ /**
66
+ * Build a Canvas client bound to one key-resolution callback.
67
+ *
68
+ * The key is resolved per call for the same reason as in `stitch.js`: it can be
69
+ * replaced from the Plugins page while the process runs.
70
+ *
71
+ * @param options - key resolver, base URL override and per-call timeout.
72
+ * @returns `removeScreen`.
73
+ */
74
+ export function createCanvasClient(options = {}) {
75
+ const { resolveApiKey, url = CANVAS_API_URL, timeoutMs = DEFAULT_CANVAS_TIMEOUT_MS } = options;
76
+
77
+ const requireKey = async () => {
78
+ const apiKey = await resolveApiKey();
79
+ if (typeof apiKey !== 'string' || apiKey === '') {
80
+ throw new Error(
81
+ 'Stitch API key is not configured. Open Settings → Plugins → stitch-designer and paste a key from https://stitch.withgoogle.com (avatar → Stitch Settings → API key).',
82
+ );
83
+ }
84
+ return apiKey;
85
+ };
86
+
87
+ return {
88
+ /**
89
+ * Take one or more screens off a project's canvas.
90
+ *
91
+ * Batch is the primitive: the canvas is a single field, so every removal is
92
+ * absorbed into ONE read-modify-write cycle and therefore one PATCH, however
93
+ * many ids are given. Removing one screen is simply the one-element case.
94
+ *
95
+ * @param projectId - the project id (bare, without the `projects/` prefix).
96
+ * @param screenIds - screen ids to remove; the bare ids from `list_screens`.
97
+ * @param exec - the tool execution context, whose signal is honoured.
98
+ * @returns the project after the write, plus which ids were removed and
99
+ * which were not on the canvas.
100
+ * @throws when no id is given, or when none of them is on the canvas.
101
+ */
102
+ async removeScreens(projectId, screenIds, exec) {
103
+ const requested = Array.isArray(screenIds) ? screenIds.filter((id) => typeof id === 'string' && id !== '') : [];
104
+ if (requested.length === 0) {
105
+ throw new Error(
106
+ 'No screen id was given: pass at least one screen id in screenIds. ' +
107
+ 'Run list_screens to see the screens this project currently holds.',
108
+ );
109
+ }
110
+
111
+ const apiKey = await requireKey();
112
+ const projectUrl = `${url}/projects/${projectId}`;
113
+
114
+ const project = await request({ method: 'GET', url: projectUrl, apiKey, timeoutMs, signal: exec?.signal });
115
+ const instances = Array.isArray(project?.screenInstances) ? project.screenInstances : [];
116
+ const screenIdOf = (instance) => String(instance?.sourceScreen ?? '').split('/screens/').pop() ?? '';
117
+
118
+ const kept = instances.filter((instance) => !requested.includes(screenIdOf(instance)));
119
+ const present = instances.map(screenIdOf);
120
+ const removed = requested.filter((id) => present.includes(id));
121
+ const missing = requested.filter((id) => !present.includes(id));
122
+
123
+ if (removed.length === 0) {
124
+ throw new Error(
125
+ `None of the given screen ids is on the canvas of project ${projectId}, so there is nothing to remove. ` +
126
+ 'Run list_screens to see the screens this project currently holds.',
127
+ );
128
+ }
129
+
130
+ const updated = await request({
131
+ method: 'PATCH',
132
+ url: `${projectUrl}?updateMask=screenInstances`,
133
+ apiKey,
134
+ body: { screenInstances: kept },
135
+ timeoutMs,
136
+ signal: exec?.signal,
137
+ });
138
+
139
+ return { ...updated, removed, missing };
140
+ },
141
+ };
142
+ }
package/lib/guidance.js CHANGED
@@ -38,7 +38,7 @@ export const SECTION_ORDER = 150;
38
38
  * acted on.
39
39
  */
40
40
  export const STITCH_GUIDANCE = [
41
- '本机已安装 dsh-stitch-designer 插件(Google Stitch AI 界面设计):提供 15 个 `mcp__stitch__*` 工具(建/删/列项目、列/取界面、生成界面、改界面、生成变体、设计系统与设计稿上传)。',
41
+ '本机已安装 dsh-stitch-designer 插件(Google Stitch AI 界面设计):提供 Stitch 原生工具(数量随上游变化,含建/删/列项目、列/取界面、生成界面、改界面、生成变体、设计系统、设计稿上传与文件下载),以及 1 个本插件自建的 `mcp__stitch__remove_screens_from_canvas`(把一张或多张 screen 从项目画布上移除;传一个 id 就是删一张——Stitch 没有删除单张 screen 的接口)。',
42
42
  '',
43
43
  '用 Stitch 设计稿实现界面时,遵守以下规范:',
44
44
  '1. 一个代码项目只对应一个 Stitch 项目,对应关系记录在该代码项目根目录的 `.stitch.json`。继续沿用已记录的那个项目,不要为同一个代码项目再调 `create_project`——插件会直接报错拒绝,并告知已绑定的项目 id。记录指向的 Stitch 项目已被删除时,插件会自动清掉记录,可重新创建。',
package/lib/index.js CHANGED
@@ -37,10 +37,11 @@
37
37
  *
38
38
  * @module @logictan/dsh-stitch-designer
39
39
  */
40
+ import { createCanvasClient } from './canvas.js';
40
41
  import { createStitchClient } from './stitch.js';
41
42
  import { createInvoker } from './conventions.js';
42
43
  import { SECTION_NAME, SECTION_ORDER, STITCH_GUIDANCE } from './guidance.js';
43
- import { createToolDefinition, publicToolName } from './tools.js';
44
+ import { createOutput, createToolDefinition, publicToolName } from './tools.js';
44
45
 
45
46
  /** Plugin row id; must equal the row id in `cordis.patch.yml`. */
46
47
  export const name = 'stitch-designer';
@@ -80,6 +81,7 @@ export function apply(ctx) {
80
81
  };
81
82
 
82
83
  const stitch = createStitchClient({ resolveApiKey });
84
+ const canvas = createCanvasClient({ resolveApiKey });
83
85
 
84
86
  // One code project ↔ one Stitch project is enforced here rather than merely
85
87
  // announced: this wrapper is the only seam that sees which code project a
@@ -135,9 +137,57 @@ export function apply(ctx) {
135
137
  sync().catch(report);
136
138
  });
137
139
 
140
+ // `remove_screens_from_canvas` is local, not upstream: Stitch has no
141
+ // screen-delete route (`stitch delete screen` answers
142
+ // `UNSUPPORTED_RESOURCE`, `DELETE .../screens/<id>` answers 404), so the
143
+ // bridge performs the removal itself through one project-level PATCH. The
144
+ // tool takes a list because the canvas is one field: N removals are one
145
+ // write, and a single removal is just the one-element list. It is
146
+ // registered here unconditionally because it needs no `tools/list` to
147
+ // describe it — its contract is this file's.
148
+ const removeScreenDispose = tools.register({
149
+ name: publicToolName(SERVER_NAME, 'remove_screens_from_canvas'),
150
+ description:
151
+ 'Remove one or more screens from a Stitch project canvas. Stitch has no screen-delete API, so '
152
+ + 'this rewrites the project\'s canvas without those screens — always in a single write, '
153
+ + 'however many ids you pass. The screen objects themselves are not destroyed; they disappear '
154
+ + 'from the project, which is what clears abandoned screens out. Use '
155
+ + 'mcp__stitch__list_screens to find screen ids first. Ids that are not on the canvas are '
156
+ + 'reported back rather than failing the whole call.',
157
+ parameters: {
158
+ type: 'object',
159
+ properties: {
160
+ projectId: {
161
+ type: 'string',
162
+ description: 'Stitch project id (bare id, not "projects/<id>").',
163
+ },
164
+ screenIds: {
165
+ type: 'array',
166
+ items: { type: 'string' },
167
+ description: 'Ids of the screens to remove from the canvas. One id is the normal single-removal case.',
168
+ },
169
+ },
170
+ required: ['projectId', 'screenIds'],
171
+ },
172
+ output: createOutput(undefined),
173
+ async execute(args, exec) {
174
+ const result = await canvas.removeScreens(args.projectId, args.screenIds, exec);
175
+ const remaining = Array.isArray(result?.screenInstances) ? result.screenInstances.length : 0;
176
+ const lines = [
177
+ `Removed ${result.removed.length} screen(s) from project ${args.projectId}: ${result.removed.join(', ')}.`,
178
+ `The canvas now holds ${remaining} screen(s).`,
179
+ ];
180
+ if (result.missing.length > 0) {
181
+ lines.push(`Not on the canvas (left alone): ${result.missing.join(', ')}.`);
182
+ }
183
+ return { content: [{ type: 'text', text: lines.join(' ') }] };
184
+ },
185
+ });
186
+
138
187
  return () => {
139
188
  disposed = true;
140
189
  offCredential();
190
+ removeScreenDispose();
141
191
  for (const dispose of disposers) dispose();
142
192
  disposers = [];
143
193
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logictan/dsh-stitch-designer",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "DSH Stitch designer: Google Stitch AI UI generation as native mcp__stitch__* tools, plus the design conventions that keep one code project bound to one Stitch project. DSH Stitch 界面设计子插件。",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",