@logictan/dsh-stitch-designer 1.0.0 → 2.0.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 +37 -31
- package/lib/client.js +7 -313
- package/lib/conventions.js +116 -0
- package/lib/guidance.js +40 -0
- package/lib/index.js +42 -28
- package/lib/project.js +77 -0
- package/package.json +3 -4
- package/lib/routes.js +0 -250
- package/lib/store.js +0 -229
package/README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
# @logictan/dsh-stitch-designer
|
|
2
2
|
|
|
3
3
|
Google Stitch (AI UI generation) inside DSH: the 15 `mcp__stitch__*` tools, plus
|
|
4
|
-
|
|
5
|
-
screenshot/HTML cache, and exports the 1:1 HTML source.
|
|
4
|
+
the design conventions that keep one code project bound to one Stitch project.
|
|
6
5
|
|
|
7
6
|
Ported from [liuxinlongwa-hue/dsh-stitch-designer](https://github.com/liuxinlongwa-hue/dsh-stitch-designer)
|
|
8
|
-
(MIT, © 2026 liuxinlongwa-hue)
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
(MIT, © 2026 liuxinlongwa-hue). What changed here is the tool surface, the key
|
|
8
|
+
storage, and the scope — that project's preview panel, screenshot cache and
|
|
9
|
+
1:1 HTML export endpoint have been removed; see below.
|
|
11
10
|
|
|
12
11
|
## What it gives the model
|
|
13
12
|
|
|
@@ -44,41 +43,48 @@ in a plugin config row, so it never appears in plaintext in
|
|
|
44
43
|
|
|
45
44
|
Setting the key registers the tools immediately; no restart is needed.
|
|
46
45
|
|
|
47
|
-
##
|
|
46
|
+
## Design conventions
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
design for the current project: a project picker, the full-size screenshot, a
|
|
51
|
-
thumbnail strip, and **导出 1:1 HTML 源码**.
|
|
48
|
+
Three rules apply when a Stitch design is implemented as code in a repository.
|
|
52
49
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
50
|
+
| Rule | How it reaches the model |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| One code project owns exactly one Stitch project | **Enforced.** The id is recorded in the code project's `.stitch.json`; a second `create_project` from an already-bound directory is refused with the existing id. |
|
|
53
|
+
| Implement with TailwindCSS, the way Stitch builds its screens | Announced in the system prompt. Unless the repository already has an architecture that cannot accommodate TailwindCSS, in which case the model is told to explain that first. |
|
|
54
|
+
| Implement from Stitch's exported HTML for pixel-level fidelity | Announced in the system prompt. The export goes to `<project>/docs/design/<screenId>.html`; anywhere the implementation would differ from the design, the model is told to ask first and give the reason. |
|
|
55
|
+
|
|
56
|
+
The first rule is enforced in code and the other two are announced, because only
|
|
57
|
+
the first has a computable predicate. "Did you use TailwindCSS?" and "is this
|
|
58
|
+
pixel-perfect?" cannot be checked, so a validator for them would be a lie — they
|
|
59
|
+
are delivered the way this repository delivers every other non-computable rule,
|
|
60
|
+
as a system-prompt section the plugin registers itself (`src/guidance.js`).
|
|
61
|
+
|
|
62
|
+
`.stitch.json` is the project's own file — commit it. It is small and
|
|
63
|
+
hand-editable on purpose: it is the escape hatch when a Stitch project was
|
|
64
|
+
deleted, or when the wrong one was recorded. A record pointing at a project
|
|
65
|
+
that no longer exists is cleared automatically rather than obeyed, so the
|
|
66
|
+
refusal can never become a permanent dead end; `delete_project` clears it too.
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{ "projectId": "15445722684773602632" }
|
|
70
|
+
```
|
|
66
71
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
| GET | `/stitch/api/html/<screenId>` | Fetch and cache a screen's 1:1 HTML, returning its local URL |
|
|
71
|
-
| GET | `/stitch/screens/<screenId>.png` | Cached screenshot |
|
|
72
|
-
| GET | `/stitch/html/<screenId>.html` | Exported HTML source |
|
|
72
|
+
Only the id is stored. The title is not, because it is a second copy of a fact
|
|
73
|
+
Stitch already owns and would go stale as soon as the project is renamed
|
|
74
|
+
upstream — while the binding rule reads the id alone.
|
|
73
75
|
|
|
74
76
|
## Local files
|
|
75
77
|
|
|
76
78
|
| Path | Contents |
|
|
77
79
|
| --- | --- |
|
|
78
80
|
| `$DSH_HOME/.credentials.yaml` | The API key, under `STITCH_API_KEY` |
|
|
79
|
-
|
|
|
80
|
-
|
|
81
|
-
|
|
81
|
+
| `<project>/.stitch.json` | The Stitch project this code project owns (see above) |
|
|
82
|
+
|
|
83
|
+
`~/.stitch` — the previous generation's screenshot/HTML cache under the user's
|
|
84
|
+
home directory — is no longer written or read by this plugin. Exporting a
|
|
85
|
+
design to `<project>/docs/design/` is the model following the announced
|
|
86
|
+
convention, not a plugin action; it travels with the repository, so a later
|
|
87
|
+
session or a teammate can read the design the code was built from.
|
|
82
88
|
|
|
83
89
|
## Replacing the `mcp-stitch` row
|
|
84
90
|
|
package/lib/client.js
CHANGED
|
@@ -1,25 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Browser half: the
|
|
2
|
+
* Browser half: the API-key card on the plugin's Plugins-page row.
|
|
3
3
|
*
|
|
4
4
|
* This file is already in the client module loader's protocol — it is not a
|
|
5
5
|
* module and imports nothing. `build.mjs` copies it to `lib/client.js`
|
|
6
6
|
* verbatim, which is why `package.json` exports `./client` at that path.
|
|
7
7
|
*
|
|
8
|
-
* Two independent surfaces share this bundle:
|
|
9
|
-
*
|
|
10
|
-
* - the preview panel, registered into `conversation.session.header.actions`;
|
|
11
|
-
* - the API-key card, registered into `plugins.row.config` under
|
|
12
|
-
* `<bundle>#stitch-designer`.
|
|
13
|
-
*
|
|
14
8
|
* `remote.credentials` is injected NESTED inside the card registration rather
|
|
15
9
|
* than declared in this plugin's `inject` list. A plugin that waits on a
|
|
16
|
-
* service its host does not provide never activates at all,
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* The panel's HTTP contract is `src/routes.js`: `GET /stitch/api/state` and
|
|
21
|
-
* `GET /stitch/api/html/<id>`, plus the cached `/stitch/screens/*.png` and
|
|
22
|
-
* `/stitch/html/*.html` files.
|
|
10
|
+
* service its host does not provide never activates at all, so a declared
|
|
11
|
+
* dependency would turn "this host has no credential provider" into "the whole
|
|
12
|
+
* browser half is gone".
|
|
23
13
|
*/
|
|
24
14
|
window.__ModuleLoader__.load({
|
|
25
15
|
id: "@logictan/dsh-stitch-designer",
|
|
@@ -50,33 +40,13 @@ window.__ModuleLoader__.load({
|
|
|
50
40
|
*/
|
|
51
41
|
var API_KEY_REF = "STITCH_API_KEY";
|
|
52
42
|
|
|
53
|
-
var CSS_ID = "stitch-designer/
|
|
43
|
+
var CSS_ID = "stitch-designer/key-card.css";
|
|
54
44
|
var css = [
|
|
55
|
-
".std-
|
|
56
|
-
".std-
|
|
57
|
-
".std-trigger:hover,.std-trigger:focus-visible{color:var(--dsw-alias-label-primary, inherit);background:var(--dsw-alias-fill-l2, rgba(128,128,128,.08))}",
|
|
58
|
-
".std-dot{width:8px;height:8px;border-radius:50%;background:linear-gradient(135deg,#4285F4,#34A853);flex:none}",
|
|
59
|
-
".std-panel{position:fixed;top:calc(var(--dsw-header-h, 48px) + 8px);right:16px;z-index:1000;width:min(460px, calc(100vw - 32px));max-height:min(680px, calc(100vh - 96px));display:flex;flex-direction:column;background:var(--dsw-specific-menu, #1b1b1f);border:1px solid var(--dsw-alias-border-l2, rgba(128,128,128,.35));border-radius:12px;box-shadow:var(--dsw-shadow-lv3, 0 8px 30px rgba(0,0,0,.35));overflow:hidden}",
|
|
60
|
-
".std-head{display:flex;align-items:center;justify-content:space-between;gap:8px;padding:8px 12px;border-bottom:1px solid var(--dsw-alias-border-l1, rgba(128,128,128,.2));font-size:13px;font-weight:600}",
|
|
61
|
-
".std-head-actions{display:flex;gap:6px;align-items:center}",
|
|
45
|
+
".std-card{display:flex;flex-direction:column;gap:10px;padding:4px 0;font-size:13px}",
|
|
46
|
+
".std-card-row{display:flex;align-items:center;gap:8px}",
|
|
62
47
|
".std-btn{padding:2px 8px;border-radius:6px;border:1px solid var(--dsw-alias-border-l2, rgba(128,128,128,.3));background:transparent;color:var(--dsw-alias-label-tertiary, inherit);font-size:12px;cursor:pointer}",
|
|
63
48
|
".std-btn:hover{color:var(--dsw-alias-label-primary, inherit)}",
|
|
64
49
|
".std-btn:disabled{opacity:.5;cursor:default}",
|
|
65
|
-
".std-select{padding:3px 8px;border-radius:6px;border:1px solid var(--dsw-alias-border-l2, rgba(128,128,128,.3));background:var(--dsw-specific-menu, #1b1b1f);color:var(--dsw-alias-label-primary, inherit);font-size:12px;line-height:18px;max-width:100%}",
|
|
66
|
-
".std-body{overflow:auto;padding:10px 12px;display:flex;flex-direction:column;gap:10px;--dsh-scrollbar-thumb:var(--dsw-alias-scrollbar-bg-l2, rgba(128,128,128,.4))}",
|
|
67
|
-
".std-meta{font-size:11px;color:var(--dsw-alias-label-tertiary, inherit);display:flex;gap:10px;flex-wrap:wrap;padding:0 2px}",
|
|
68
|
-
".std-tag{padding:1px 8px;border-radius:99px;background:var(--dsw-alias-fill-l2, rgba(128,128,128,.1));font-size:11px;color:var(--dsw-alias-label-secondary, inherit)}",
|
|
69
|
-
".std-img-wrap{display:flex;justify-content:center;background:var(--dsw-alias-fill-l2, rgba(128,128,128,.06));border-radius:10px;padding:8px;min-height:120px}",
|
|
70
|
-
".std-img{max-width:100%;max-height:min(480px, calc(100vh - 240px));border-radius:6px;box-shadow:0 4px 16px rgba(0,0,0,.25)}",
|
|
71
|
-
".std-thumbs{display:flex;gap:8px;overflow-x:auto;padding:2px 0;--dsh-scrollbar-thumb:var(--dsw-alias-scrollbar-bg-l2, rgba(128,128,128,.4))}",
|
|
72
|
-
".std-thumb{width:56px;height:96px;object-fit:cover;border-radius:6px;border:2px solid transparent;cursor:pointer;flex:none;opacity:.75;transition:opacity .15s}",
|
|
73
|
-
".std-thumb:hover{opacity:1}",
|
|
74
|
-
".std-thumb-on{border-color:var(--dsw-alias-accent, #4285F4);opacity:1}",
|
|
75
|
-
".std-empty{padding:26px 12px;text-align:center;color:var(--dsw-alias-label-tertiary, inherit);font-size:13px;line-height:1.6}",
|
|
76
|
-
".std-hint{padding:8px 10px;border-radius:8px;background:var(--dsw-alias-fill-l2, rgba(128,128,128,.07));font-size:12px;color:var(--dsw-alias-label-secondary, inherit);line-height:1.6}",
|
|
77
|
-
".std-err{padding:10px 12px;text-align:center;color:var(--dsw-alias-danger-fg, #ef4444);font-size:12px}",
|
|
78
|
-
".std-card{display:flex;flex-direction:column;gap:10px;padding:4px 0;font-size:13px}",
|
|
79
|
-
".std-card-row{display:flex;align-items:center;gap:8px}",
|
|
80
50
|
".std-input{flex:1;min-width:0;padding:5px 8px;border-radius:6px;border:1px solid var(--dsw-alias-border-l2, rgba(128,128,128,.35));background:var(--dsw-alias-fill-l1, transparent);color:var(--dsw-alias-label-primary, inherit);font-size:12px;line-height:18px}",
|
|
81
51
|
".std-note{font-size:12px;line-height:1.6;color:var(--dsw-alias-label-tertiary, inherit)}",
|
|
82
52
|
".std-ok{color:var(--dsw-alias-success-fg, #22c55e)}",
|
|
@@ -91,267 +61,6 @@ window.__ModuleLoader__.load({
|
|
|
91
61
|
document.head.appendChild(tag);
|
|
92
62
|
}
|
|
93
63
|
|
|
94
|
-
//#region preview panel
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* The session-header Stitch preview panel.
|
|
98
|
-
*
|
|
99
|
-
* Reads `GET /stitch/api/state` and renders the newest cached screenshot,
|
|
100
|
-
* a project picker, and a thumbnail strip. The Host does the caching and the
|
|
101
|
-
* index-lag fallback; this half only presents what it is given.
|
|
102
|
-
*/
|
|
103
|
-
function DesignerPanel() {
|
|
104
|
-
var open = React.useState(false);
|
|
105
|
-
var opened = open[0];
|
|
106
|
-
var setOpened = open[1];
|
|
107
|
-
var state = React.useState(null);
|
|
108
|
-
var data = state[0];
|
|
109
|
-
var setData = state[1];
|
|
110
|
-
var err = React.useState("");
|
|
111
|
-
var errorText = err[0];
|
|
112
|
-
var setErr = err[1];
|
|
113
|
-
var sel = React.useState("");
|
|
114
|
-
var selectedId = sel[0];
|
|
115
|
-
var setSelectedId = sel[1];
|
|
116
|
-
var idx = React.useState(0);
|
|
117
|
-
var currentIdx = idx[0];
|
|
118
|
-
var setCurrentIdx = idx[1];
|
|
119
|
-
var busy = React.useState(false);
|
|
120
|
-
var loading = busy[0];
|
|
121
|
-
var setLoading = busy[1];
|
|
122
|
-
|
|
123
|
-
function refresh(projectId) {
|
|
124
|
-
setErr("");
|
|
125
|
-
setLoading(true);
|
|
126
|
-
var q = projectId ? "?projectId=" + encodeURIComponent(projectId) : "";
|
|
127
|
-
fetch("/stitch/api/state" + q, { cache: "no-store" })
|
|
128
|
-
.then(function (r) {
|
|
129
|
-
if (!r.ok) throw new Error("HTTP " + r.status);
|
|
130
|
-
return r.json();
|
|
131
|
-
})
|
|
132
|
-
.then(function (d) {
|
|
133
|
-
if (d.ok) {
|
|
134
|
-
setData(d);
|
|
135
|
-
setCurrentIdx(0);
|
|
136
|
-
} else {
|
|
137
|
-
setErr(d.error || "未知错误");
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
.catch(function (e) {
|
|
141
|
-
setErr((e && e.message) || String(e));
|
|
142
|
-
})
|
|
143
|
-
.then(function () {
|
|
144
|
-
setLoading(false);
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function toggle() {
|
|
149
|
-
var next = !opened;
|
|
150
|
-
setOpened(next);
|
|
151
|
-
if (next) refresh(selectedId || (data && data.projectId));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function onProjectChange(e) {
|
|
155
|
-
var id = e.target.value;
|
|
156
|
-
setSelectedId(id);
|
|
157
|
-
setCurrentIdx(0);
|
|
158
|
-
refresh(id);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function exportHtml(screenId) {
|
|
162
|
-
fetch("/stitch/api/html/" + screenId, { cache: "no-store" })
|
|
163
|
-
.then(function (r) {
|
|
164
|
-
if (!r.ok) throw new Error("HTTP " + r.status);
|
|
165
|
-
return r.json();
|
|
166
|
-
})
|
|
167
|
-
.then(function (d) {
|
|
168
|
-
if (d.ok && d.html) window.open(d.html, "_blank");
|
|
169
|
-
else alert("导出失败: " + (d.error || "未知错误"));
|
|
170
|
-
})
|
|
171
|
-
.catch(function (e) {
|
|
172
|
-
alert("导出失败: " + ((e && e.message) || e));
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
var trigger = React.createElement(
|
|
177
|
-
"button",
|
|
178
|
-
{
|
|
179
|
-
className: "std-trigger",
|
|
180
|
-
title: "AI 设计(Stitch 预览)",
|
|
181
|
-
"aria-label": "AI 设计预览",
|
|
182
|
-
"aria-expanded": opened,
|
|
183
|
-
onClick: toggle,
|
|
184
|
-
},
|
|
185
|
-
React.createElement("span", { className: "std-dot" }),
|
|
186
|
-
"AI 设计",
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
var body;
|
|
190
|
-
if (opened) {
|
|
191
|
-
if (errorText) {
|
|
192
|
-
body = React.createElement("div", { className: "std-err" }, "加载失败: " + errorText);
|
|
193
|
-
} else if (data === null) {
|
|
194
|
-
body = React.createElement("div", { className: "std-empty" }, "加载中…");
|
|
195
|
-
} else {
|
|
196
|
-
var projectOptions = (data.projects || []).map(function (p) {
|
|
197
|
-
return React.createElement(
|
|
198
|
-
"option",
|
|
199
|
-
{ key: p.id, value: p.id },
|
|
200
|
-
(p.title || "项目 " + String(p.id).slice(-6)) + (p.id === data.projectId ? " ✓" : ""),
|
|
201
|
-
);
|
|
202
|
-
});
|
|
203
|
-
var projectPicker = React.createElement(
|
|
204
|
-
"div",
|
|
205
|
-
{ className: "std-meta", style: { alignItems: "center", gap: 6 } },
|
|
206
|
-
React.createElement("span", { className: "std-tag" }, "项目"),
|
|
207
|
-
React.createElement(
|
|
208
|
-
"select",
|
|
209
|
-
{
|
|
210
|
-
className: "std-select",
|
|
211
|
-
value: selectedId || data.projectId || "",
|
|
212
|
-
onChange: onProjectChange,
|
|
213
|
-
style: { flex: 1, minWidth: 0 },
|
|
214
|
-
},
|
|
215
|
-
React.createElement("option", { value: "" }, "选择项目…"),
|
|
216
|
-
projectOptions,
|
|
217
|
-
),
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
var screens = data.screens || [];
|
|
221
|
-
var total = screens.length;
|
|
222
|
-
var cur = currentIdx < total ? screens[currentIdx] : screens[0];
|
|
223
|
-
var meta = React.createElement(
|
|
224
|
-
"div",
|
|
225
|
-
{ className: "std-meta" },
|
|
226
|
-
React.createElement("span", { className: "std-tag" }, data.projectTitle || "项目 " + String(data.projectId || "").slice(-6)),
|
|
227
|
-
total > 0 ? React.createElement("span", { className: "std-tag" }, "共 " + total + " 张") : null,
|
|
228
|
-
cur ? React.createElement("span", { className: "std-tag" }, cur.deviceType || "MOBILE") : null,
|
|
229
|
-
cur ? React.createElement("span", { className: "std-tag" }, (cur.width || "") + "×" + (cur.height || "")) : null,
|
|
230
|
-
cur && cur.title ? React.createElement("span", { className: "std-tag" }, cur.title) : null,
|
|
231
|
-
);
|
|
232
|
-
|
|
233
|
-
var preview;
|
|
234
|
-
if (cur && cur.preview) {
|
|
235
|
-
preview = React.createElement(
|
|
236
|
-
"div",
|
|
237
|
-
{ className: "std-img-wrap" },
|
|
238
|
-
React.createElement("img", {
|
|
239
|
-
className: "std-img",
|
|
240
|
-
src: cur.preview + "?t=" + Date.now(),
|
|
241
|
-
alt: "设计预览",
|
|
242
|
-
onClick: function () {
|
|
243
|
-
window.open(cur.preview, "_blank");
|
|
244
|
-
},
|
|
245
|
-
}),
|
|
246
|
-
);
|
|
247
|
-
} else {
|
|
248
|
-
preview = React.createElement(
|
|
249
|
-
"div",
|
|
250
|
-
{ className: "std-empty" },
|
|
251
|
-
"该项目还没有设计。在对话里告诉我需求(如:帮我设计一个回收小程序首页,APP 端)→ 生成后这里实时预览。或在上方切换查看其他项目。",
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
var thumbs = null;
|
|
256
|
-
if (total > 1) {
|
|
257
|
-
thumbs = React.createElement(
|
|
258
|
-
"div",
|
|
259
|
-
{ className: "std-thumbs" },
|
|
260
|
-
screens.map(function (s, i) {
|
|
261
|
-
return React.createElement("img", {
|
|
262
|
-
key: s.id || i,
|
|
263
|
-
className: "std-thumb" + (i === currentIdx ? " std-thumb-on" : ""),
|
|
264
|
-
src: s.preview + "?t=" + Date.now(),
|
|
265
|
-
alt: s.title || "设计 " + (i + 1),
|
|
266
|
-
onClick: function () {
|
|
267
|
-
setCurrentIdx(i);
|
|
268
|
-
},
|
|
269
|
-
});
|
|
270
|
-
}),
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
var actions = null;
|
|
275
|
-
if (cur) {
|
|
276
|
-
actions = React.createElement(
|
|
277
|
-
"div",
|
|
278
|
-
{ className: "std-meta", style: { marginTop: 4 } },
|
|
279
|
-
React.createElement(
|
|
280
|
-
"button",
|
|
281
|
-
{
|
|
282
|
-
className: "std-btn",
|
|
283
|
-
onClick: function () {
|
|
284
|
-
exportHtml(cur.id);
|
|
285
|
-
},
|
|
286
|
-
},
|
|
287
|
-
"📄 导出 1:1 HTML 源码",
|
|
288
|
-
),
|
|
289
|
-
React.createElement(
|
|
290
|
-
"button",
|
|
291
|
-
{
|
|
292
|
-
className: "std-btn",
|
|
293
|
-
onClick: function () {
|
|
294
|
-
window.open(cur.preview, "_blank");
|
|
295
|
-
},
|
|
296
|
-
},
|
|
297
|
-
"🔍 查看大图",
|
|
298
|
-
),
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
var hint = React.createElement(
|
|
303
|
-
"div",
|
|
304
|
-
{ className: "std-hint" },
|
|
305
|
-
"💡 怎么用:在对话里告诉我设计需求(APP / Web / 软件界面均可)→ 生成后这里实时预览。不满意就在对话里说哪里要改(如「颜色换浅一点、品类改成四个」)→ 我改完点刷新看新版。每个设计都自带 1:1 HTML 源码,点上方「导出 1:1 HTML 源码」即可获取。",
|
|
306
|
-
);
|
|
307
|
-
|
|
308
|
-
body = React.createElement(React.Fragment, null, projectPicker, meta, preview, thumbs, actions, hint);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return React.createElement(
|
|
313
|
-
"div",
|
|
314
|
-
{ className: "std-root" },
|
|
315
|
-
trigger,
|
|
316
|
-
opened
|
|
317
|
-
? React.createElement(
|
|
318
|
-
"div",
|
|
319
|
-
{ className: "std-panel", role: "dialog", "aria-label": "AI 设计预览" },
|
|
320
|
-
React.createElement(
|
|
321
|
-
"div",
|
|
322
|
-
{ className: "std-head" },
|
|
323
|
-
React.createElement("span", null, "AI 设计 · Stitch 预览"),
|
|
324
|
-
React.createElement(
|
|
325
|
-
"span",
|
|
326
|
-
{ className: "std-head-actions" },
|
|
327
|
-
React.createElement(
|
|
328
|
-
"button",
|
|
329
|
-
{
|
|
330
|
-
className: "std-btn",
|
|
331
|
-
disabled: loading,
|
|
332
|
-
onClick: function () {
|
|
333
|
-
refresh(selectedId || (data && data.projectId));
|
|
334
|
-
},
|
|
335
|
-
},
|
|
336
|
-
loading ? "刷新中…" : "刷新",
|
|
337
|
-
),
|
|
338
|
-
React.createElement(
|
|
339
|
-
"button",
|
|
340
|
-
{ className: "std-btn", onClick: function () { setOpened(false); } },
|
|
341
|
-
"关闭",
|
|
342
|
-
),
|
|
343
|
-
),
|
|
344
|
-
),
|
|
345
|
-
React.createElement("div", { className: "std-body" }, body),
|
|
346
|
-
)
|
|
347
|
-
: null,
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
//#endregion
|
|
352
|
-
|
|
353
|
-
//#region API key card
|
|
354
|
-
|
|
355
64
|
/**
|
|
356
65
|
* The Stitch API-key card on the plugin's Plugins-page row.
|
|
357
66
|
*
|
|
@@ -489,22 +198,7 @@ window.__ModuleLoader__.load({
|
|
|
489
198
|
);
|
|
490
199
|
}
|
|
491
200
|
|
|
492
|
-
//#endregion
|
|
493
|
-
|
|
494
201
|
function apply(ctx) {
|
|
495
|
-
ctx.effect(function () {
|
|
496
|
-
return ctx.slots.inject("conversation.session.header.actions", function () {
|
|
497
|
-
return ctx.slots.register(
|
|
498
|
-
{
|
|
499
|
-
name: "conversation.session.header.actions",
|
|
500
|
-
id: "stitch-designer",
|
|
501
|
-
order: 40,
|
|
502
|
-
},
|
|
503
|
-
DesignerPanel,
|
|
504
|
-
);
|
|
505
|
-
});
|
|
506
|
-
}, "stitch-designer: preview panel");
|
|
507
|
-
|
|
508
202
|
// Nested: a host without a credential provider loses only this card.
|
|
509
203
|
ctx.inject(["remote.credentials"], function (scoped) {
|
|
510
204
|
BUNDLE_NAMES.forEach(function (bundle) {
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one design convention that is enforced rather than announced.
|
|
3
|
+
*
|
|
4
|
+
* "One code project owns exactly one Stitch project" is the only one of the
|
|
5
|
+
* three with a computable predicate, so it is the only one implemented here.
|
|
6
|
+
* The other two (TailwindCSS; implement from the exported HTML and ask before
|
|
7
|
+
* deviating) are prose, and live in `src/guidance.js` as the plugin's
|
|
8
|
+
* system-prompt announcement — a validator for them would be a lie.
|
|
9
|
+
*
|
|
10
|
+
* The rule is enforced rather than stated because a convention the model can
|
|
11
|
+
* silently break is not a convention. The id is recorded in the code project's
|
|
12
|
+
* own `.stitch.json`, and a second `create_project` from an already-bound
|
|
13
|
+
* directory is refused with the existing id — a refusal the model does not
|
|
14
|
+
* control. A recorded project that no longer exists upstream is cleared rather
|
|
15
|
+
* than obeyed, so the refusal can never become a permanent dead end.
|
|
16
|
+
*
|
|
17
|
+
* @module @logictan/dsh-stitch-designer/conventions
|
|
18
|
+
*/
|
|
19
|
+
import { clearBinding, readBinding, writeBinding } from './project.js';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the code project a tool call is running on behalf of.
|
|
23
|
+
*
|
|
24
|
+
* The session's working directory is the code project's identity here. A call
|
|
25
|
+
* with no agent (a probe, a nested dispatch) has no directory, so it gets no
|
|
26
|
+
* enforcement — that is not an error, it just has no project to bind.
|
|
27
|
+
*
|
|
28
|
+
* @param exec - the tool execution context.
|
|
29
|
+
* @returns the absolute directory, or `undefined`.
|
|
30
|
+
*/
|
|
31
|
+
function codeDirectory(exec) {
|
|
32
|
+
const cwd = exec?.agent?.session?.header?.cwd;
|
|
33
|
+
return typeof cwd === 'string' && cwd !== '' ? cwd : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The Stitch project id inside a `projects/<id>` resource name.
|
|
38
|
+
*
|
|
39
|
+
* Stitch is inconsistent about this: `create_project`'s result carries the full
|
|
40
|
+
* resource name, while other payloads carry the bare id. Both shapes are
|
|
41
|
+
* accepted here rather than at each call site.
|
|
42
|
+
*
|
|
43
|
+
* @param value - a resource name, a bare id, or anything else.
|
|
44
|
+
* @returns the bare id, or `undefined`.
|
|
45
|
+
*/
|
|
46
|
+
function projectIdOf(value) {
|
|
47
|
+
const text = String(value ?? '');
|
|
48
|
+
if (text === '') return undefined;
|
|
49
|
+
return text.startsWith('projects/') ? text.split('/').pop() : text;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether a recorded project still exists upstream.
|
|
54
|
+
*
|
|
55
|
+
* Measured against the live server: an unknown or deleted project is answered
|
|
56
|
+
* with `content: [{ text: 'Requested entity was not found.' }]` and NO
|
|
57
|
+
* `structuredContent` — it is not an error response. That absence is the whole
|
|
58
|
+
* liveness signal.
|
|
59
|
+
*
|
|
60
|
+
* @param stitch - the Stitch client.
|
|
61
|
+
* @param projectId - the recorded project id.
|
|
62
|
+
* @returns whether the project is still there.
|
|
63
|
+
*/
|
|
64
|
+
async function projectExists(stitch, projectId) {
|
|
65
|
+
const result = await stitch.callTool('get_project', { name: `projects/${projectId}` }, undefined);
|
|
66
|
+
return result?.structuredContent?.name !== undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Build the wrapper that keeps one code project on one Stitch project.
|
|
71
|
+
*
|
|
72
|
+
* The wrapper is the seam that sees the code project a call belongs to, which
|
|
73
|
+
* is what makes the refusal possible at all — the Stitch tools themselves have
|
|
74
|
+
* no idea which repository is calling them.
|
|
75
|
+
*
|
|
76
|
+
* @param options - the Stitch client.
|
|
77
|
+
* @returns the `(rawName, args, exec)` invoker the tool definitions use.
|
|
78
|
+
*/
|
|
79
|
+
export function createInvoker(options) {
|
|
80
|
+
const { stitch } = options;
|
|
81
|
+
|
|
82
|
+
return async function invoke(rawName, args, exec) {
|
|
83
|
+
const directory = codeDirectory(exec);
|
|
84
|
+
const recorded = directory === undefined ? undefined : await readBinding(directory);
|
|
85
|
+
|
|
86
|
+
if (rawName === 'create_project' && directory !== undefined && recorded !== undefined) {
|
|
87
|
+
// A record pointing at a project that is gone must not be a dead end.
|
|
88
|
+
if (await projectExists(stitch, recorded.projectId)) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`This code project is already bound to Stitch project ${recorded.projectId} ` +
|
|
91
|
+
`(projects/${recorded.projectId}), recorded in ${directory}/.stitch.json. ` +
|
|
92
|
+
'One code project owns exactly one Stitch project, so no second one may be created for it. ' +
|
|
93
|
+
'Continue the existing project instead — list its screens, or generate into it with that projectId.',
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
await clearBinding(directory);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const result = await stitch.callTool(rawName, args, exec);
|
|
100
|
+
|
|
101
|
+
if (rawName === 'create_project') {
|
|
102
|
+
const created = projectIdOf(result?.structuredContent?.name);
|
|
103
|
+
if (created !== undefined && directory !== undefined) {
|
|
104
|
+
await writeBinding(directory, created);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (rawName === 'delete_project') {
|
|
110
|
+
// The record is cleared only when it points at the project being deleted.
|
|
111
|
+
if (recorded !== undefined && projectIdOf(args?.name) === recorded.projectId) await clearBinding(directory);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return result;
|
|
115
|
+
};
|
|
116
|
+
}
|
package/lib/guidance.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Stitch design conventions, as the plugin announces them.
|
|
3
|
+
*
|
|
4
|
+
* These are **prose**, and deliberately so. "Did you use TailwindCSS?" and "is
|
|
5
|
+
* this pixel-perfect?" have no computable predicate, so a validator for them
|
|
6
|
+
* would be a lie — it would either pass everything or block correct work. They
|
|
7
|
+
* are delivered the way this repository delivers every other non-computable
|
|
8
|
+
* rule: as an announcement in the system prompt, registered by the plugin that
|
|
9
|
+
* owns the subject, so the model reads it in every session without anyone
|
|
10
|
+
* having to remember to ask.
|
|
11
|
+
*
|
|
12
|
+
* The one convention with a computable predicate — one code project owns one
|
|
13
|
+
* Stitch project — is **not** here. It is enforced in `src/conventions.js`,
|
|
14
|
+
* because a rule the model can silently break is not a rule. This file is only
|
|
15
|
+
* the text.
|
|
16
|
+
*
|
|
17
|
+
* @module @logictan/dsh-stitch-designer/guidance
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Section name in the system prompt; unique across plugins. */
|
|
21
|
+
export const SECTION_NAME = 'plugin:dsh-stitch-designer';
|
|
22
|
+
|
|
23
|
+
/** Order within the tool-guidance band. */
|
|
24
|
+
export const SECTION_ORDER = 150;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* What the model is told about Stitch, and about implementing its designs.
|
|
28
|
+
*
|
|
29
|
+
* Written for a reader who has just been handed a design and is about to write
|
|
30
|
+
* code from it — the moment these rules matter and the only moment they can be
|
|
31
|
+
* acted on.
|
|
32
|
+
*/
|
|
33
|
+
export const STITCH_GUIDANCE = [
|
|
34
|
+
'本机已安装 dsh-stitch-designer 插件(Google Stitch AI 界面设计):提供 15 个 `mcp__stitch__*` 工具(建/删/列项目、列/取界面、生成界面、改界面、生成变体、设计系统与设计稿上传)。',
|
|
35
|
+
'',
|
|
36
|
+
'用 Stitch 设计稿实现界面时,遵守以下规范:',
|
|
37
|
+
'1. 一个代码项目只对应一个 Stitch 项目,对应关系记录在该代码项目根目录的 `.stitch.json`。继续沿用已记录的那个项目,不要为同一个代码项目再调 `create_project`——插件会直接报错拒绝,并告知已绑定的项目 id。记录指向的 Stitch 项目已被删除时,插件会自动清掉记录,可重新创建。',
|
|
38
|
+
'2. 实现时使用 TailwindCSS,与 Stitch 生成设计稿的方式保持一致;除非该项目已有架构无法兼容 TailwindCSS,此时先说明是哪一处不兼容、为什么不兼容,再动手。',
|
|
39
|
+
'3. 先导出 Stitch 的设计稿 HTML,照着它实现,尽可能做到像素级还原。设计稿 HTML 落在 `<项目>/docs/design/<screenId>.html`,应提交到版本库。凡实现需要与设计稿不一致的地方,先问用户并给出理由,不要自行偏离。',
|
|
40
|
+
].join('\n');
|
package/lib/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Google Stitch (AI UI generation) as a first-class part of the harness. It
|
|
5
5
|
* registers the same 15 `mcp__stitch__*` tools the retired `mcp-stitch` patch
|
|
6
|
-
* row used to provide
|
|
6
|
+
* row used to provide.
|
|
7
7
|
*
|
|
8
8
|
* Three design choices are load-bearing, and each replaces something that was
|
|
9
9
|
* tried and failed elsewhere in this repository:
|
|
@@ -13,8 +13,7 @@
|
|
|
13
13
|
* 15 input schemas and 13 of 15 output schemas are rejected by
|
|
14
14
|
* `assertSupportedJsonSchema` — so the bridge has to decide per field what
|
|
15
15
|
* to keep. Owning the transport is also what keeps the API key out of a
|
|
16
|
-
* patch row (see below)
|
|
17
|
-
* client.
|
|
16
|
+
* patch row (see below).
|
|
18
17
|
*
|
|
19
18
|
* - **The API key is a credential, not configuration.** It lives in
|
|
20
19
|
* `$DSH_HOME/.credentials.yaml` under the ref `STITCH_API_KEY`, resolved
|
|
@@ -26,22 +25,21 @@
|
|
|
26
25
|
* section exports refs from the credentials file), and it never appears in a
|
|
27
26
|
* patch row.
|
|
28
27
|
*
|
|
29
|
-
* - **
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* before the credential store was live and failed as "key not configured").
|
|
39
|
-
* The key itself is read at call time, so setting it later still works.
|
|
28
|
+
* - **The tools half waits for the services it needs.** `ctx.get(name)`
|
|
29
|
+
* returns only a service whose providing fiber is already ACTIVE, and
|
|
30
|
+
* providers come up asynchronously — so a service read once in `apply` can
|
|
31
|
+
* silently be `undefined`. The registration therefore nests under
|
|
32
|
+
* `ctx.inject` so it waits for its own providers; it waits for `credentials`
|
|
33
|
+
* in particular, because its first `tools/list` needs the key and nothing
|
|
34
|
+
* re-fires that fetch at boot (the initial attempt ran before the credential
|
|
35
|
+
* store was live and failed as "key not configured"). The key itself is read
|
|
36
|
+
* at call time, so setting it later still works.
|
|
40
37
|
*
|
|
41
38
|
* @module @logictan/dsh-stitch-designer
|
|
42
39
|
*/
|
|
43
|
-
import { makeRoutes } from './routes.js';
|
|
44
40
|
import { createStitchClient } from './stitch.js';
|
|
41
|
+
import { createInvoker } from './conventions.js';
|
|
42
|
+
import { SECTION_NAME, SECTION_ORDER, STITCH_GUIDANCE } from './guidance.js';
|
|
45
43
|
import { createToolDefinition, publicToolName } from './tools.js';
|
|
46
44
|
|
|
47
45
|
/** Plugin row id; must equal the row id in `cordis.patch.yml`. */
|
|
@@ -60,7 +58,7 @@ export const API_KEY_REF = 'STITCH_API_KEY';
|
|
|
60
58
|
export const SERVER_NAME = 'stitch';
|
|
61
59
|
|
|
62
60
|
/**
|
|
63
|
-
* Register the Stitch tools and the
|
|
61
|
+
* Register the Stitch tools and announce the design conventions.
|
|
64
62
|
*
|
|
65
63
|
* @param ctx - the plugin context.
|
|
66
64
|
*/
|
|
@@ -83,6 +81,13 @@ export function apply(ctx) {
|
|
|
83
81
|
|
|
84
82
|
const stitch = createStitchClient({ resolveApiKey });
|
|
85
83
|
|
|
84
|
+
// One code project ↔ one Stitch project is enforced here rather than merely
|
|
85
|
+
// announced: this wrapper is the only seam that sees which code project a
|
|
86
|
+
// call belongs to, and therefore the only one that can refuse a second
|
|
87
|
+
// `create_project`. The other two conventions are prose and live in the
|
|
88
|
+
// system-prompt section below. See `src/conventions.js`.
|
|
89
|
+
const invoke = createInvoker({ stitch });
|
|
90
|
+
|
|
86
91
|
// ---------------------------------------------------------------- tools
|
|
87
92
|
// The upstream descriptors ARE the tool contract — names, descriptions and
|
|
88
93
|
// input schemas all come from Stitch — so registration follows a successful
|
|
@@ -109,7 +114,7 @@ export function apply(ctx) {
|
|
|
109
114
|
description: tool.description ?? '',
|
|
110
115
|
inputSchema: tool.inputSchema,
|
|
111
116
|
outputSchema: tool.outputSchema,
|
|
112
|
-
call: (args, exec) =>
|
|
117
|
+
call: (args, exec) => invoke(tool.name, args, exec),
|
|
113
118
|
}),
|
|
114
119
|
);
|
|
115
120
|
for (const dispose of disposers) dispose();
|
|
@@ -139,15 +144,24 @@ export function apply(ctx) {
|
|
|
139
144
|
}, 'stitch-designer: Stitch tools');
|
|
140
145
|
});
|
|
141
146
|
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
147
|
+
// ------------------------------------------------------------- guidance
|
|
148
|
+
// The design conventions that cannot be enforced are announced instead, the
|
|
149
|
+
// way this repository delivers every other non-computable rule. Read through
|
|
150
|
+
// `ctx.get` rather than `inject`: `inject` treats a service as REQUIRED and
|
|
151
|
+
// silently skips the whole plugin when it is missing, which would turn "no
|
|
152
|
+
// system prompt" into "no Stitch tools at all".
|
|
153
|
+
const systemPrompt = ctx.get('systemPrompt');
|
|
154
|
+
if (systemPrompt === undefined || systemPrompt === null) {
|
|
155
|
+
ctx.logger?.warn?.('stitch-designer: the systemPrompt service is unavailable; the design conventions were not announced.');
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
ctx.effect(
|
|
159
|
+
() =>
|
|
160
|
+
systemPrompt.section({
|
|
161
|
+
name: SECTION_NAME,
|
|
162
|
+
order: SECTION_ORDER,
|
|
163
|
+
text: STITCH_GUIDANCE,
|
|
164
|
+
}),
|
|
165
|
+
'stitch-designer: design conventions',
|
|
166
|
+
);
|
|
153
167
|
}
|
package/lib/project.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The binding file this plugin keeps inside a code project.
|
|
3
|
+
*
|
|
4
|
+
* `.stitch.json` records which Stitch project a code project owns. It is the
|
|
5
|
+
* project's own file, not plugin state: it belongs to the repository, travels
|
|
6
|
+
* with it through git, and is read by whoever implements the design — this
|
|
7
|
+
* session, a later one, or a teammate on another machine. That is why it is
|
|
8
|
+
* kept per repository rather than in any machine-local cache under `$HOME`.
|
|
9
|
+
*
|
|
10
|
+
* It is deliberately small and hand-editable: it is the escape hatch when a
|
|
11
|
+
* Stitch project was deleted, or when the wrong one was recorded.
|
|
12
|
+
*
|
|
13
|
+
* @module @logictan/dsh-stitch-designer/project
|
|
14
|
+
*/
|
|
15
|
+
import { readFile, rm, writeFile } from 'node:fs/promises';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
|
|
18
|
+
/** The binding file, at the code project's root. */
|
|
19
|
+
export const BINDING_FILE = '.stitch.json';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Read the code project's binding.
|
|
23
|
+
*
|
|
24
|
+
* A missing or hand-broken file means "not bound yet", which is the normal
|
|
25
|
+
* first-run state — not a failure worth surfacing. The file is user-editable
|
|
26
|
+
* by design, so a bad edit has to degrade to "unbound" rather than break every
|
|
27
|
+
* Stitch tool call in the project.
|
|
28
|
+
*
|
|
29
|
+
* Only the id is kept. The title is not: it would be a second copy of a fact
|
|
30
|
+
* Stitch already owns, and it goes stale the moment the project is renamed
|
|
31
|
+
* upstream — while the binding rule reads the id alone.
|
|
32
|
+
*
|
|
33
|
+
* @param directory - the code project's root.
|
|
34
|
+
* @returns `{ projectId }`, or `undefined` when unbound.
|
|
35
|
+
*/
|
|
36
|
+
export async function readBinding(directory) {
|
|
37
|
+
try {
|
|
38
|
+
const parsed = JSON.parse(await readFile(join(directory, BINDING_FILE), 'utf8'));
|
|
39
|
+
const projectId = parsed?.projectId;
|
|
40
|
+
if (typeof projectId !== 'string' || projectId === '') return undefined;
|
|
41
|
+
return { projectId };
|
|
42
|
+
} catch {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Record the Stitch project this code project owns.
|
|
49
|
+
*
|
|
50
|
+
* First writer wins. Overwriting would let a stray design silently re-point a
|
|
51
|
+
* code project at a different Stitch project — the exact 1:1 rule this file
|
|
52
|
+
* exists to enforce — so a caller that finds a different project already
|
|
53
|
+
* recorded must report that instead of replacing it.
|
|
54
|
+
*
|
|
55
|
+
* @param directory - the code project's root.
|
|
56
|
+
* @param projectId - the Stitch project id.
|
|
57
|
+
* @returns whether the file was written.
|
|
58
|
+
*/
|
|
59
|
+
export async function writeBinding(directory, projectId) {
|
|
60
|
+
if (await readBinding(directory)) return false;
|
|
61
|
+
await writeFile(join(directory, BINDING_FILE), `${JSON.stringify({ projectId }, null, 2)}\n`);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Drop the binding, so the code project can bind again.
|
|
67
|
+
*
|
|
68
|
+
* Called when the recorded Stitch project is gone, or when it is deleted
|
|
69
|
+
* through this plugin. Without it a binding to a deleted project would block
|
|
70
|
+
* `create_project` from that directory forever, with no way back short of
|
|
71
|
+
* hand-editing `.stitch.json`.
|
|
72
|
+
*
|
|
73
|
+
* @param directory - the code project's root.
|
|
74
|
+
*/
|
|
75
|
+
export async function clearBinding(directory) {
|
|
76
|
+
await rm(join(directory, BINDING_FILE), { force: true });
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-stitch-designer",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "DSH Stitch designer: Google Stitch AI UI generation as native mcp__stitch__* tools, plus
|
|
3
|
+
"version": "2.0.0",
|
|
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",
|
|
7
7
|
"exports": {
|
|
@@ -42,8 +42,7 @@
|
|
|
42
42
|
"ai-design",
|
|
43
43
|
"ui-generation",
|
|
44
44
|
"generative-ui",
|
|
45
|
-
"mcp"
|
|
46
|
-
"preview"
|
|
45
|
+
"mcp"
|
|
47
46
|
],
|
|
48
47
|
"license": "MIT",
|
|
49
48
|
"repository": {
|
package/lib/routes.js
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP routes behind the preview panel.
|
|
3
|
-
*
|
|
4
|
-
* Four routes, and only the four the panel actually calls (the reference
|
|
5
|
-
* plugin's `POST /stitch/api/generate` and `POST /stitch/api/edit` are
|
|
6
|
-
* deliberately not ported: they existed so an agent could drive Stitch over
|
|
7
|
-
* HTTP, and in this plugin the agent calls `mcp__stitch__*` tools directly, so
|
|
8
|
-
* those two routes would have no caller at all).
|
|
9
|
-
*
|
|
10
|
-
* Two of the four are plain file servers over the local cache, and both take
|
|
11
|
-
* their filename from the request path — the only genuinely untrusted input in
|
|
12
|
-
* this plugin. Each therefore validates the segment against a strict
|
|
13
|
-
* character-class pattern before touching the filesystem, so `..` and any other
|
|
14
|
-
* traversal attempt fails as a 404 rather than resolving a path outside
|
|
15
|
-
* `~/.stitch`.
|
|
16
|
-
*
|
|
17
|
-
* @module @logictan/dsh-stitch-designer/routes
|
|
18
|
-
*/
|
|
19
|
-
import { readFile } from 'node:fs/promises';
|
|
20
|
-
import { join } from 'node:path';
|
|
21
|
-
|
|
22
|
-
import {
|
|
23
|
-
HTML_DIR,
|
|
24
|
-
SCREENS_DIR,
|
|
25
|
-
cacheScreenAssets,
|
|
26
|
-
downloadTo,
|
|
27
|
-
knownScreenIds,
|
|
28
|
-
loadState,
|
|
29
|
-
rememberScreenId,
|
|
30
|
-
saveState,
|
|
31
|
-
toPanelScreen,
|
|
32
|
-
} from './store.js';
|
|
33
|
-
|
|
34
|
-
/** Path prefix of the panel's JSON API. */
|
|
35
|
-
export const STATE_API = '/stitch/api/state';
|
|
36
|
-
/** Path prefix of the 1:1 HTML export endpoint. */
|
|
37
|
-
export const HTML_API = '/stitch/api/html';
|
|
38
|
-
/** Path prefix of the cached screenshot files. */
|
|
39
|
-
export const SCREENS_PATH = '/stitch/screens';
|
|
40
|
-
/** Path prefix of the exported HTML files. */
|
|
41
|
-
export const HTML_PATH = '/stitch/html';
|
|
42
|
-
|
|
43
|
-
/** A screen id or file stem: no dots, no slashes, so it cannot escape its directory. */
|
|
44
|
-
const SAFE_ID = /^[A-Za-z0-9_-]+$/;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Write a JSON response.
|
|
48
|
-
*
|
|
49
|
-
* `no-store` matters: the panel polls this while a design is being generated,
|
|
50
|
-
* and a cached response would show a stale project.
|
|
51
|
-
*
|
|
52
|
-
* @param res - the response.
|
|
53
|
-
* @param status - HTTP status.
|
|
54
|
-
* @param value - the JSON body.
|
|
55
|
-
*/
|
|
56
|
-
function json(res, status, value) {
|
|
57
|
-
res.writeHead(status, {
|
|
58
|
-
'Content-Type': 'application/json; charset=utf-8',
|
|
59
|
-
'Cache-Control': 'no-store',
|
|
60
|
-
});
|
|
61
|
-
res.end(JSON.stringify(value));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Write a file from the local cache.
|
|
66
|
-
*
|
|
67
|
-
* @param res - the response.
|
|
68
|
-
* @param dir - the cache directory the file must live in.
|
|
69
|
-
* @param file - the validated file name.
|
|
70
|
-
* @param contentType - the response content type.
|
|
71
|
-
*/
|
|
72
|
-
async function serveFile(res, dir, file, contentType) {
|
|
73
|
-
try {
|
|
74
|
-
const body = await readFile(join(dir, file));
|
|
75
|
-
res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': 'no-store' });
|
|
76
|
-
res.end(body);
|
|
77
|
-
} catch {
|
|
78
|
-
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
79
|
-
res.end('not found');
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Extract the last path segment after a prefix. */
|
|
84
|
-
function segmentAfter(url, prefix) {
|
|
85
|
-
const path = String(url ?? '').split('?')[0];
|
|
86
|
-
if (!path.startsWith(`${prefix}/`)) return undefined;
|
|
87
|
-
return path.slice(prefix.length + 1);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Build the plugin's routes.
|
|
92
|
-
*
|
|
93
|
-
* @param options - the Stitch client.
|
|
94
|
-
* @returns the route list.
|
|
95
|
-
*/
|
|
96
|
-
export function makeRoutes(options) {
|
|
97
|
-
const { stitch } = options;
|
|
98
|
-
|
|
99
|
-
/** List every project the account can see, flattened for the picker. */
|
|
100
|
-
const listProjects = async () => {
|
|
101
|
-
const result = await stitch.callTool('list_projects', {}, undefined);
|
|
102
|
-
const projects = result?.structuredContent?.projects;
|
|
103
|
-
return Array.isArray(projects) ? projects : [];
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/** List one project's screens. */
|
|
107
|
-
const listScreens = async (projectId) => {
|
|
108
|
-
const result = await stitch.callTool('list_screens', { projectId }, undefined);
|
|
109
|
-
const screens = result?.structuredContent?.screens;
|
|
110
|
-
return Array.isArray(screens) ? screens : [];
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
/** Fetch one screen directly, which works before the index catches up. */
|
|
114
|
-
const getScreen = async (projectId, screenId) => {
|
|
115
|
-
const result = await stitch.callTool('get_screen', { name: `projects/${projectId}/screens/${screenId}` }, undefined);
|
|
116
|
-
return result?.structuredContent?.screen ?? result?.structuredContent;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
return [
|
|
120
|
-
// ------------------------------------------------- state (prefix)
|
|
121
|
-
{
|
|
122
|
-
kind: 'prefix',
|
|
123
|
-
path: STATE_API,
|
|
124
|
-
handler: async (req, res) => {
|
|
125
|
-
try {
|
|
126
|
-
const url = new URL(String(req.url ?? ''), 'http://localhost');
|
|
127
|
-
const wanted = url.searchParams.get('projectId') ?? '';
|
|
128
|
-
|
|
129
|
-
const projects = (await listProjects().catch(() => []))
|
|
130
|
-
.map((project) => ({
|
|
131
|
-
id: String(project?.name ?? '').split('/').pop(),
|
|
132
|
-
name: project?.name ?? '',
|
|
133
|
-
title: project?.title ?? '',
|
|
134
|
-
deviceType: project?.deviceType ?? '',
|
|
135
|
-
updateTime: project?.updateTime ?? '',
|
|
136
|
-
}))
|
|
137
|
-
.filter((project) => project.id !== '');
|
|
138
|
-
|
|
139
|
-
const state = await loadState();
|
|
140
|
-
const targetId = wanted || state.projectId || projects[0]?.id || '';
|
|
141
|
-
|
|
142
|
-
const seen = new Set();
|
|
143
|
-
const items = [];
|
|
144
|
-
const adopt = async (screen) => {
|
|
145
|
-
const row = toPanelScreen(screen);
|
|
146
|
-
if (row === undefined || seen.has(row.id)) return;
|
|
147
|
-
seen.add(row.id);
|
|
148
|
-
await rememberScreenId(targetId, row.id);
|
|
149
|
-
await cacheScreenAssets(screen);
|
|
150
|
-
items.push(row);
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
for (const screen of await listScreens(targetId).catch(() => [])) await adopt(screen);
|
|
154
|
-
|
|
155
|
-
// `list_screens` lags generation by up to a couple of minutes, so any
|
|
156
|
-
// id this machine has already seen is re-fetched directly. Without
|
|
157
|
-
// this the panel shows an empty project right after a design lands.
|
|
158
|
-
const fallbackIds = knownScreenIds(state, targetId);
|
|
159
|
-
if (state.lastScreenId && !fallbackIds.includes(state.lastScreenId)) fallbackIds.unshift(state.lastScreenId);
|
|
160
|
-
for (const screenId of fallbackIds) {
|
|
161
|
-
if (seen.has(screenId) || !SAFE_ID.test(screenId)) continue;
|
|
162
|
-
await getScreen(targetId, screenId)
|
|
163
|
-
.then((screen) => adopt(screen))
|
|
164
|
-
.catch(() => {});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
await saveState({ projectId: targetId });
|
|
168
|
-
const target = projects.find((project) => project.id === targetId);
|
|
169
|
-
json(res, 200, {
|
|
170
|
-
ok: true,
|
|
171
|
-
projectId: targetId,
|
|
172
|
-
projectTitle: target?.title ?? '',
|
|
173
|
-
lastScreenId: state.lastScreenId ?? '',
|
|
174
|
-
projects,
|
|
175
|
-
screens: items,
|
|
176
|
-
});
|
|
177
|
-
} catch (error) {
|
|
178
|
-
json(res, 500, { ok: false, error: String(error?.message ?? error) });
|
|
179
|
-
}
|
|
180
|
-
},
|
|
181
|
-
},
|
|
182
|
-
|
|
183
|
-
// ------------------------------------------------- HTML export (prefix)
|
|
184
|
-
{
|
|
185
|
-
kind: 'prefix',
|
|
186
|
-
path: HTML_API,
|
|
187
|
-
handler: async (req, res) => {
|
|
188
|
-
try {
|
|
189
|
-
const screenId = segmentAfter(req.url, HTML_API);
|
|
190
|
-
if (screenId === undefined || !SAFE_ID.test(screenId)) {
|
|
191
|
-
json(res, 404, { ok: false, error: 'screen 不存在' });
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
// The panel can ask for a screen from any project, so the owning
|
|
195
|
-
// project is resolved from the state and then from the project list
|
|
196
|
-
// rather than assumed to be the currently selected one.
|
|
197
|
-
const state = await loadState();
|
|
198
|
-
const candidates = [state.projectId, ...(await listProjects().catch(() => [])).map((p) => String(p?.name ?? '').split('/').pop())];
|
|
199
|
-
let screen;
|
|
200
|
-
for (const projectId of candidates) {
|
|
201
|
-
if (!projectId) continue;
|
|
202
|
-
screen = await getScreen(projectId, screenId).catch(() => undefined);
|
|
203
|
-
if (screen?.htmlCode?.downloadUrl) break;
|
|
204
|
-
screen = undefined;
|
|
205
|
-
}
|
|
206
|
-
if (screen?.htmlCode?.downloadUrl === undefined) {
|
|
207
|
-
json(res, 404, { ok: false, error: 'screen 不存在' });
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
await downloadTo(screen.htmlCode.downloadUrl, join(HTML_DIR, `${screenId}.html`));
|
|
211
|
-
json(res, 200, { ok: true, html: `${HTML_PATH}/${screenId}.html` });
|
|
212
|
-
} catch (error) {
|
|
213
|
-
json(res, 500, { ok: false, error: String(error?.message ?? error) });
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
},
|
|
217
|
-
|
|
218
|
-
// ------------------------------------------------- screenshots (prefix)
|
|
219
|
-
{
|
|
220
|
-
kind: 'prefix',
|
|
221
|
-
path: SCREENS_PATH,
|
|
222
|
-
handler: async (req, res) => {
|
|
223
|
-
const file = segmentAfter(req.url, SCREENS_PATH);
|
|
224
|
-
const match = /^([A-Za-z0-9_-]+)\.png$/.exec(file ?? '');
|
|
225
|
-
if (match === null) {
|
|
226
|
-
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
227
|
-
res.end('not found');
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
await serveFile(res, SCREENS_DIR, `${match[1]}.png`, 'image/png');
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
|
|
234
|
-
// ------------------------------------------------- exported HTML (prefix)
|
|
235
|
-
{
|
|
236
|
-
kind: 'prefix',
|
|
237
|
-
path: HTML_PATH,
|
|
238
|
-
handler: async (req, res) => {
|
|
239
|
-
const file = segmentAfter(req.url, HTML_PATH);
|
|
240
|
-
const match = /^([A-Za-z0-9_-]+)\.html$/.exec(file ?? '');
|
|
241
|
-
if (match === null) {
|
|
242
|
-
res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
|
|
243
|
-
res.end('not found');
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
await serveFile(res, HTML_DIR, `${match[1]}.html`, 'text/html; charset=utf-8');
|
|
247
|
-
},
|
|
248
|
-
},
|
|
249
|
-
];
|
|
250
|
-
}
|
package/lib/store.js
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Local asset store behind the preview panel: the screenshot/HTML cache, the
|
|
3
|
-
* "last used project" state, and the per-project screen registry.
|
|
4
|
-
*
|
|
5
|
-
* Stitch's `list_screens` index lags behind generation by up to a couple of
|
|
6
|
-
* minutes, and `generate_screen_from_text` returns the finished screen in its
|
|
7
|
-
* own payload long before that index catches up. The cache and registry here
|
|
8
|
-
* are what let the panel show a design the moment it exists instead of showing
|
|
9
|
-
* an empty project — the failure the reference implementation was written to
|
|
10
|
-
* fix, and the reason `state.json` is persisted rather than held in memory
|
|
11
|
-
* (an in-memory `lastScreenId` is lost on every restart, blanking the panel).
|
|
12
|
-
*
|
|
13
|
-
* Everything lives under `~/.stitch`, the same location the reference plugin
|
|
14
|
-
* used, so an existing cache keeps working. The API key is deliberately NOT
|
|
15
|
-
* here: it belongs to the credentials file (see `src/index.js`).
|
|
16
|
-
*
|
|
17
|
-
* @module @logictan/dsh-stitch-designer/store
|
|
18
|
-
*/
|
|
19
|
-
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
20
|
-
import { homedir } from 'node:os';
|
|
21
|
-
import { dirname, join } from 'node:path';
|
|
22
|
-
|
|
23
|
-
/** Root of the local cache. */
|
|
24
|
-
export const STITCH_HOME = join(homedir(), '.stitch');
|
|
25
|
-
/** Cached screenshots, served at `/stitch/screens/<id>.png`. */
|
|
26
|
-
export const SCREENS_DIR = join(STITCH_HOME, 'screens');
|
|
27
|
-
/** Exported 1:1 HTML sources, served at `/stitch/html/<id>.html`. */
|
|
28
|
-
export const HTML_DIR = join(STITCH_HOME, 'html');
|
|
29
|
-
/** Persisted project/screen state. */
|
|
30
|
-
export const STATE_FILE = join(STITCH_HOME, 'state.json');
|
|
31
|
-
/** Cap on remembered screen ids per project. */
|
|
32
|
-
const MAX_REMEMBERED_SCREENS = 50;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Read `state.json`, tolerating absence and corruption.
|
|
36
|
-
*
|
|
37
|
-
* A missing or malformed file means "no memory yet", which is a normal first
|
|
38
|
-
* run — not a failure worth surfacing, and definitely not one worth letting
|
|
39
|
-
* abort a panel refresh.
|
|
40
|
-
*
|
|
41
|
-
* @returns the parsed state, or an empty object.
|
|
42
|
-
*/
|
|
43
|
-
export async function loadState() {
|
|
44
|
-
try {
|
|
45
|
-
const parsed = JSON.parse(await readFile(STATE_FILE, 'utf8'));
|
|
46
|
-
return parsed !== null && typeof parsed === 'object' ? parsed : {};
|
|
47
|
-
} catch {
|
|
48
|
-
return {};
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Merge a patch into `state.json`.
|
|
54
|
-
*
|
|
55
|
-
* Empty values never overwrite a stored one: callers pass whatever they happen
|
|
56
|
-
* to have in hand, and an uninitialised variable would otherwise erase the
|
|
57
|
-
* project the panel is supposed to remember.
|
|
58
|
-
*
|
|
59
|
-
* @param patch - fields to merge.
|
|
60
|
-
* @returns the merged state.
|
|
61
|
-
*/
|
|
62
|
-
export async function saveState(patch) {
|
|
63
|
-
const previous = await loadState();
|
|
64
|
-
const next = { ...previous, ...patch };
|
|
65
|
-
if (!patch.projectId && previous.projectId) next.projectId = previous.projectId;
|
|
66
|
-
if (!patch.lastScreenId && previous.lastScreenId) next.lastScreenId = previous.lastScreenId;
|
|
67
|
-
try {
|
|
68
|
-
await mkdir(dirname(STATE_FILE), { recursive: true });
|
|
69
|
-
await writeFile(STATE_FILE, JSON.stringify(next, null, 2));
|
|
70
|
-
} catch {
|
|
71
|
-
// The state file is an optimisation; losing it degrades the panel to
|
|
72
|
-
// "whatever Stitch's index reports" and must not fail the request.
|
|
73
|
-
}
|
|
74
|
-
return next;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Screen ids this machine has already seen for one project.
|
|
79
|
-
*
|
|
80
|
-
* @param state - a loaded state object.
|
|
81
|
-
* @param projectId - the project id.
|
|
82
|
-
* @returns the remembered ids, oldest first.
|
|
83
|
-
*/
|
|
84
|
-
export function knownScreenIds(state, projectId) {
|
|
85
|
-
const registry = state?.screensByProject?.[projectId];
|
|
86
|
-
return Array.isArray(registry) ? registry : [];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Record one screen id against its project.
|
|
91
|
-
*
|
|
92
|
-
* @param projectId - the project id.
|
|
93
|
-
* @param screenId - the screen id to remember.
|
|
94
|
-
*/
|
|
95
|
-
export async function rememberScreenId(projectId, screenId) {
|
|
96
|
-
if (!projectId || !screenId) return;
|
|
97
|
-
const state = await loadState();
|
|
98
|
-
const registry = { ...(state.screensByProject ?? {}) };
|
|
99
|
-
const list = Array.isArray(registry[projectId]) ? [...registry[projectId]] : [];
|
|
100
|
-
if (!list.includes(screenId)) list.push(screenId);
|
|
101
|
-
registry[projectId] = list.slice(-MAX_REMEMBERED_SCREENS);
|
|
102
|
-
try {
|
|
103
|
-
await mkdir(dirname(STATE_FILE), { recursive: true });
|
|
104
|
-
await writeFile(STATE_FILE, JSON.stringify({ ...state, screensByProject: registry }, null, 2));
|
|
105
|
-
} catch {
|
|
106
|
-
// See saveState: a lost registry only costs the index-lag fallback.
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Upgrade a Google image URL to full resolution.
|
|
112
|
-
*
|
|
113
|
-
* Stitch hands out thumbnail URLs; `=s0` asks the image service for the
|
|
114
|
-
* original. Applied only when no size directive is already present, so a URL
|
|
115
|
-
* that already carries one is left alone.
|
|
116
|
-
*
|
|
117
|
-
* @param url - the download URL.
|
|
118
|
-
* @returns the URL to fetch.
|
|
119
|
-
*/
|
|
120
|
-
export function hdImageUrl(url) {
|
|
121
|
-
const value = String(url ?? '');
|
|
122
|
-
return value.includes('googleusercontent.com') && !/=[sw]\d+/.test(value) ? `${value}=s0` : value;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Download a URL to a local path, creating parent directories.
|
|
127
|
-
*
|
|
128
|
-
* @param url - source URL.
|
|
129
|
-
* @param dest - absolute destination path.
|
|
130
|
-
* @returns the destination path.
|
|
131
|
-
* @throws when the response is not OK or the write fails.
|
|
132
|
-
*/
|
|
133
|
-
export async function downloadTo(url, dest) {
|
|
134
|
-
const response = await fetch(hdImageUrl(url));
|
|
135
|
-
if (!response.ok) throw new Error(`download failed: HTTP ${response.status}`);
|
|
136
|
-
await mkdir(dirname(dest), { recursive: true });
|
|
137
|
-
await writeFile(dest, Buffer.from(await response.arrayBuffer()));
|
|
138
|
-
return dest;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Cache one screen's screenshot and HTML source, best effort.
|
|
143
|
-
*
|
|
144
|
-
* A failed download is not an error: the panel falls back to the remote
|
|
145
|
-
* `downloadUrl`, and the next refresh retries. Only the returned record's
|
|
146
|
-
* shape matters to callers.
|
|
147
|
-
*
|
|
148
|
-
* @param screen - a Stitch screen object.
|
|
149
|
-
* @returns the screen's id, or `undefined` when it carries none.
|
|
150
|
-
*/
|
|
151
|
-
export async function cacheScreenAssets(screen) {
|
|
152
|
-
const screenId = String(screen?.name ?? '').split('/').pop();
|
|
153
|
-
if (!screenId) return undefined;
|
|
154
|
-
if (screen?.screenshot?.downloadUrl) {
|
|
155
|
-
await downloadTo(screen.screenshot.downloadUrl, join(SCREENS_DIR, `${screenId}.png`)).catch(() => {});
|
|
156
|
-
}
|
|
157
|
-
if (screen?.htmlCode?.downloadUrl) {
|
|
158
|
-
await downloadTo(screen.htmlCode.downloadUrl, join(HTML_DIR, `${screenId}.html`)).catch(() => {});
|
|
159
|
-
}
|
|
160
|
-
return screenId;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Project one Stitch screen into the panel's row shape.
|
|
165
|
-
*
|
|
166
|
-
* @param screen - a Stitch screen object.
|
|
167
|
-
* @returns the panel row, or `undefined` when the screen carries no id.
|
|
168
|
-
*/
|
|
169
|
-
export function toPanelScreen(screen) {
|
|
170
|
-
const screenId = String(screen?.name ?? '').split('/').pop();
|
|
171
|
-
if (!screenId) return undefined;
|
|
172
|
-
return {
|
|
173
|
-
name: screen.name,
|
|
174
|
-
id: screenId,
|
|
175
|
-
title: screen.title ?? '',
|
|
176
|
-
deviceType: screen.deviceType ?? '',
|
|
177
|
-
width: screen.width ?? '',
|
|
178
|
-
height: screen.height ?? '',
|
|
179
|
-
preview: `/stitch/screens/${screenId}.png`,
|
|
180
|
-
screenshotUrl: screen.screenshot?.downloadUrl ?? '',
|
|
181
|
-
htmlUrl: screen.htmlCode?.downloadUrl ?? '',
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Pull the freshly generated screen out of a `generate`/`edit` result.
|
|
187
|
-
*
|
|
188
|
-
* Stitch returns the finished screen inside
|
|
189
|
-
* `structuredContent.outputComponents[].design.screens[]` immediately, which is
|
|
190
|
-
* strictly earlier than `list_screens` will report it.
|
|
191
|
-
*
|
|
192
|
-
* @param result - the MCP result of a generate/edit call.
|
|
193
|
-
* @returns the first screen found, or `undefined`.
|
|
194
|
-
*/
|
|
195
|
-
export function extractScreenFromResult(result) {
|
|
196
|
-
const components = result?.structuredContent?.outputComponents;
|
|
197
|
-
if (!Array.isArray(components)) return undefined;
|
|
198
|
-
for (const component of components) {
|
|
199
|
-
const screens = component?.design?.screens;
|
|
200
|
-
if (Array.isArray(screens) && screens.length > 0) return screens[0];
|
|
201
|
-
}
|
|
202
|
-
return undefined;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Adopt a screen into the cache and the persisted state.
|
|
207
|
-
*
|
|
208
|
-
* @param projectId - the project the screen belongs to.
|
|
209
|
-
* @param screen - the Stitch screen object.
|
|
210
|
-
* @param source - provenance tag recorded for the panel (`list`, `generate`, ...).
|
|
211
|
-
* @returns the panel response, or `undefined` when the screen carries no id.
|
|
212
|
-
*/
|
|
213
|
-
export async function adoptScreen(projectId, screen, source = 'list') {
|
|
214
|
-
const screenId = await cacheScreenAssets(screen);
|
|
215
|
-
if (screenId === undefined) return undefined;
|
|
216
|
-
await rememberScreenId(projectId, screenId);
|
|
217
|
-
await saveState({ projectId, lastScreenId: screenId });
|
|
218
|
-
return {
|
|
219
|
-
ok: true,
|
|
220
|
-
screenId,
|
|
221
|
-
title: screen.title ?? '',
|
|
222
|
-
deviceType: screen.deviceType ?? '',
|
|
223
|
-
width: screen.width ?? '',
|
|
224
|
-
height: screen.height ?? '',
|
|
225
|
-
preview: `/stitch/screens/${screenId}.png`,
|
|
226
|
-
html: `/stitch/html/${screenId}.html`,
|
|
227
|
-
source,
|
|
228
|
-
};
|
|
229
|
-
}
|