@logictan/dsh-stitch-designer 2.1.1 → 2.1.3
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/lib/guidance.js +1 -3
- package/lib/index.js +49 -11
- package/lib/tools.js +58 -0
- package/package.json +1 -1
package/lib/guidance.js
CHANGED
|
@@ -40,9 +40,7 @@ export const SECTION_ORDER = 150;
|
|
|
40
40
|
export const STITCH_GUIDANCE = [
|
|
41
41
|
'本机已安装 dsh-stitch-designer 插件(Google Stitch AI 界面设计):提供 Stitch 原生工具(数量随上游变化,含建/删/列项目、列/取界面、生成界面、改界面、生成变体、设计系统、设计稿上传与文件下载),以及 1 个本插件自建的 `mcp__stitch__remove_screens_from_canvas`(把一张或多张 screen 从项目画布上移除;传一个 id 就是删一张——Stitch 没有删除单张 screen 的接口)。',
|
|
42
42
|
'',
|
|
43
|
-
'
|
|
44
|
-
'',
|
|
45
|
-
'参数结构复杂的那几只(`create_design_system`、`update_design_system`、`apply_design_system`、`generate_screen_from_text`、`edit_screens`、`generate_variants`、`upload_design_md`、`create_design_system_from_design_md`)参数表保持完整,按提示词里的说明填即可。',
|
|
43
|
+
'Stitch 全部工具的参数表都不在提示词里(由 dsh-tool-surface 撤下),参数按 dsh-tool-surface 的 stitch 组说明填,直接按名调用即可。其中 `create_design_system`、`update_design_system` 结构复杂(`$defs` 三层嵌套、字体枚举 68 个),先读插件 README 或用 `mcp__stitch__get_project` 读项目里已存的设计系统资产,不要凭空猜字段。',
|
|
46
44
|
'',
|
|
47
45
|
'用 Stitch 设计稿实现界面时,遵守以下规范:',
|
|
48
46
|
'1. 一个代码项目只对应一个 Stitch 项目,对应关系记录在该代码项目根目录的 `.stitch.json`。继续沿用已记录的那个项目,不要为同一个代码项目再调 `create_project`——插件会直接报错拒绝,并告知已绑定的项目 id。记录指向的 Stitch 项目已被删除时,插件会自动清掉记录,可重新创建。',
|
package/lib/index.js
CHANGED
|
@@ -41,7 +41,7 @@ import { createCanvasClient } from './canvas.js';
|
|
|
41
41
|
import { createStitchClient } from './stitch.js';
|
|
42
42
|
import { createInvoker } from './conventions.js';
|
|
43
43
|
import { SECTION_NAME, SECTION_ORDER, STITCH_GUIDANCE } from './guidance.js';
|
|
44
|
-
import { createOutput, createToolDefinition, publicToolName, withoutWithdrawn } from './tools.js';
|
|
44
|
+
import { createOutput, createToolDefinition, publicToolName, retryWithBackoff, withoutWithdrawn } from './tools.js';
|
|
45
45
|
|
|
46
46
|
/** Plugin row id; must equal the row id in `cordis.patch.yml`. */
|
|
47
47
|
export const name = 'stitch-designer';
|
|
@@ -61,6 +61,26 @@ export const SERVER_NAME = 'stitch';
|
|
|
61
61
|
/** The prefix every tool this plugin registers carries, announcement filter included. */
|
|
62
62
|
const PUBLIC_PREFIX = `mcp__${SERVER_NAME}__`;
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* How the boot-time `tools/list` is retried before the tools are given up on.
|
|
66
|
+
*
|
|
67
|
+
* The definitions are not shipped statically — they ARE the upstream catalogs —
|
|
68
|
+
* so registration happens only after a successful `tools/list`, and that fetch
|
|
69
|
+
* is the one thing that can fail transiently at boot: the credential store may
|
|
70
|
+
* not be live yet on a cold start, or the network hiccups. The credential-update
|
|
71
|
+
* event covers the "key set later" case, but a key already present at boot never
|
|
72
|
+
* fires it, so without a self-retry a single failed fetch left the tools
|
|
73
|
+
* unregistered for the whole process lifetime while the system prompt still
|
|
74
|
+
* claimed they existed — the model then called `mcp__stitch__*` and got
|
|
75
|
+
* "unknown tool" (observed live 2026-11-13: 18 tools reachable via the API, yet
|
|
76
|
+
* none registered because the one boot fetch failed).
|
|
77
|
+
*
|
|
78
|
+
* Bounded exponential backoff, then give up with a warning: an unrecoverable
|
|
79
|
+
* failure (wrong key, Stitch down) must not spin forever. The credential-update
|
|
80
|
+
* event remains the recovery path after that point.
|
|
81
|
+
*/
|
|
82
|
+
const SYNC_RETRY_DELAYS_MS = [500, 1000, 2000, 4000, 8000];
|
|
83
|
+
|
|
64
84
|
/**
|
|
65
85
|
* Register the Stitch tools and announce the design conventions.
|
|
66
86
|
*
|
|
@@ -109,8 +129,9 @@ export function apply(ctx) {
|
|
|
109
129
|
toolsCtx.effect(() => {
|
|
110
130
|
let disposed = false;
|
|
111
131
|
let disposers = [];
|
|
132
|
+
let cancelRetry;
|
|
112
133
|
|
|
113
|
-
const
|
|
134
|
+
const registerListed = async () => {
|
|
114
135
|
const listed = await stitch.listTools();
|
|
115
136
|
if (disposed) return;
|
|
116
137
|
const next = listed.map((tool) =>
|
|
@@ -127,17 +148,32 @@ export function apply(ctx) {
|
|
|
127
148
|
toolsCtx.logger.info(`stitch-designer: registered ${disposers.length} mcp__${SERVER_NAME}__* tools`);
|
|
128
149
|
};
|
|
129
150
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
151
|
+
// One attempt plus bounded backoff, so the boot fetch that failed
|
|
152
|
+
// transiently (credential store not live yet, network hiccup) heals
|
|
153
|
+
// itself instead of leaving the tools missing for the process lifetime.
|
|
154
|
+
// A failure that persists through every retry is logged once and left to
|
|
155
|
+
// the credential-update event; an unrecoverable one (wrong key) stops
|
|
156
|
+
// here rather than spinning. Each new run cancels any pending backoff so
|
|
157
|
+
// a mid-flight credential update cannot race an in-progress retry.
|
|
158
|
+
const syncWithRetry = () => {
|
|
159
|
+
cancelRetry?.();
|
|
160
|
+
cancelRetry = retryWithBackoff(registerListed, SYNC_RETRY_DELAYS_MS, {
|
|
161
|
+
shouldStop: () => disposed,
|
|
162
|
+
onRetry: (error, delay) =>
|
|
163
|
+
toolsCtx.logger.warn(
|
|
164
|
+
`stitch-designer: the mcp__${SERVER_NAME}__* tools are not registered yet (${error?.message ?? error}); retrying in ${delay}ms`,
|
|
165
|
+
),
|
|
166
|
+
onGiveUp: (error, attempts) =>
|
|
167
|
+
toolsCtx.logger.warn(
|
|
168
|
+
`stitch-designer: the mcp__${SERVER_NAME}__* tools are not registered after ${attempts} attempts (${error?.message ?? error}); set or fix the API key to retry.`,
|
|
169
|
+
),
|
|
170
|
+
});
|
|
135
171
|
};
|
|
136
172
|
|
|
137
|
-
|
|
173
|
+
syncWithRetry();
|
|
138
174
|
const offCredential = toolsCtx.on('credentials/reference-updated', (ref) => {
|
|
139
175
|
if (ref !== API_KEY_REF || disposers.length > 0) return;
|
|
140
|
-
|
|
176
|
+
syncWithRetry();
|
|
141
177
|
});
|
|
142
178
|
|
|
143
179
|
// `remove_screens_from_canvas` is local, not upstream: Stitch has no
|
|
@@ -189,6 +225,7 @@ export function apply(ctx) {
|
|
|
189
225
|
|
|
190
226
|
return () => {
|
|
191
227
|
disposed = true;
|
|
228
|
+
cancelRetry?.();
|
|
192
229
|
offCredential();
|
|
193
230
|
removeScreenDispose();
|
|
194
231
|
for (const dispose of disposers) dispose();
|
|
@@ -201,8 +238,9 @@ export function apply(ctx) {
|
|
|
201
238
|
// The withdrawn half of the toolset (see `ANNOUNCED_TOOLS`): registered and
|
|
202
239
|
// executable, absent from every request's tool list. Filtering by the public
|
|
203
240
|
// name prefix rather than by a captured registration set keeps this correct
|
|
204
|
-
// when `tools/list` fails at boot and the tools appear later
|
|
205
|
-
//
|
|
241
|
+
// when `tools/list` fails at boot and the tools appear later — either
|
|
242
|
+
// through the boot retry or a credential update — the names are the
|
|
243
|
+
// contract, the set of them is not.
|
|
206
244
|
ctx.on('system-prompt/assemble', async (_assembly, _context, next) => {
|
|
207
245
|
const assembly = await next();
|
|
208
246
|
const tools = withoutWithdrawn(assembly.tools, PUBLIC_PREFIX);
|
package/lib/tools.js
CHANGED
|
@@ -267,3 +267,61 @@ export function createToolDefinition(options) {
|
|
|
267
267
|
},
|
|
268
268
|
};
|
|
269
269
|
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Run one async attempt, and on failure schedule bounded retries.
|
|
273
|
+
*
|
|
274
|
+
* The tool list this plugin registers comes from Stitch's own `tools/list`, so
|
|
275
|
+
* registration is only possible after that fetch succeeds — and that fetch is
|
|
276
|
+
* the one thing that can fail transiently at boot (credential store not live
|
|
277
|
+
* yet, network hiccup). A single failed fetch used to leave the tools missing
|
|
278
|
+
* for the whole process lifetime, even though the system prompt still announced
|
|
279
|
+
* them, so the model called `mcp__stitch__*` and got "unknown tool". This is the
|
|
280
|
+
* bounded self-heal: attempt now, and on rejection wait `delays[0]`, retry, wait
|
|
281
|
+
* `delays[1]`, and so on, giving up after the delays run out.
|
|
282
|
+
*
|
|
283
|
+
* Exported and dependency-injected (`schedule`) so the behavior is testable
|
|
284
|
+
* without a live endpoint or real timers.
|
|
285
|
+
*
|
|
286
|
+
* @param attempt - the work; its rejection is what triggers a retry.
|
|
287
|
+
* @param delays - backoff in milliseconds before each retry, in order.
|
|
288
|
+
* @param options - `shouldStop` checked before every retry; `onRetry`/`onGiveUp`
|
|
289
|
+
* notifications; `schedule` timer function that MUST return a canceller.
|
|
290
|
+
* @returns a canceller that stops any pending retry.
|
|
291
|
+
*/
|
|
292
|
+
export function retryWithBackoff(attempt, delays, options = {}) {
|
|
293
|
+
const {
|
|
294
|
+
shouldStop = () => false,
|
|
295
|
+
onRetry,
|
|
296
|
+
onGiveUp,
|
|
297
|
+
// Normalized to return a canceller. Bare `setTimeout` returns a Timeout
|
|
298
|
+
// object, and the teardown path calls the return value — returning the raw
|
|
299
|
+
// timeout would throw "cancelScheduled is not a function" when the effect is
|
|
300
|
+
// unloaded during a backoff window.
|
|
301
|
+
schedule = (fn, delay) => {
|
|
302
|
+
const timeout = setTimeout(fn, delay);
|
|
303
|
+
return () => clearTimeout(timeout);
|
|
304
|
+
},
|
|
305
|
+
} = options;
|
|
306
|
+
let cancelled = false;
|
|
307
|
+
let cancelScheduled;
|
|
308
|
+
|
|
309
|
+
const run = (index) => {
|
|
310
|
+
attempt().catch((error) => {
|
|
311
|
+
if (cancelled || shouldStop()) return;
|
|
312
|
+
if (index >= delays.length) {
|
|
313
|
+
onGiveUp?.(error, index + 1);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const delay = delays[index];
|
|
317
|
+
onRetry?.(error, delay);
|
|
318
|
+
cancelScheduled = schedule(() => run(index + 1), delay);
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
run(0);
|
|
323
|
+
return () => {
|
|
324
|
+
cancelled = true;
|
|
325
|
+
cancelScheduled?.();
|
|
326
|
+
};
|
|
327
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-stitch-designer",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
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",
|