@logictan/dsh-stitch-designer 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +97 -2
- package/cordis.patch.yml +22 -0
- package/lib/client.js +227 -0
- package/lib/conventions.js +116 -0
- package/lib/guidance.js +40 -0
- package/lib/index.js +167 -0
- package/lib/project.js +77 -0
- package/lib/stitch.js +113 -0
- package/lib/tools.js +165 -0
- package/package.json +70 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Logic Tan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,98 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @logictan/dsh-stitch-designer
|
|
2
2
|
|
|
3
|
-
|
|
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.
|
|
5
|
+
|
|
6
|
+
Ported from [liuxinlongwa-hue/dsh-stitch-designer](https://github.com/liuxinlongwa-hue/dsh-stitch-designer)
|
|
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.
|
|
10
|
+
|
|
11
|
+
## What it gives the model
|
|
12
|
+
|
|
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:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
mcp__stitch__create_project mcp__stitch__get_project
|
|
18
|
+
mcp__stitch__delete_project mcp__stitch__list_projects
|
|
19
|
+
mcp__stitch__list_screens mcp__stitch__get_screen
|
|
20
|
+
mcp__stitch__generate_screen_from_text mcp__stitch__edit_screens
|
|
21
|
+
mcp__stitch__generate_variants mcp__stitch__upload_design_md
|
|
22
|
+
mcp__stitch__create_design_system mcp__stitch__list_design_systems
|
|
23
|
+
mcp__stitch__create_design_system_from_design_md
|
|
24
|
+
mcp__stitch__update_design_system mcp__stitch__apply_design_system
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The tool list is read from Stitch at activation, not copied into this package,
|
|
28
|
+
so a tool Stitch adds or renames shows up without a code change. A call budget
|
|
29
|
+
of 180s covers `generate_screen_from_text`, which is long-running.
|
|
30
|
+
|
|
31
|
+
## Settings
|
|
32
|
+
|
|
33
|
+
`Settings → Plugins → stitch-designer`:
|
|
34
|
+
|
|
35
|
+
| Field | Meaning |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| Stitch API Key | The `STITCH_API_KEY` credential; write-only in the UI and never read back |
|
|
38
|
+
|
|
39
|
+
Get the key from <https://stitch.withgoogle.com> (avatar → Stitch Settings → API
|
|
40
|
+
key). It is stored in `$DSH_HOME/.credentials.yaml` as `STITCH_API_KEY` — **not**
|
|
41
|
+
in a plugin config row, so it never appears in plaintext in
|
|
42
|
+
`cordis.patch.yml`. Config sync carries it in its credentials section.
|
|
43
|
+
|
|
44
|
+
Setting the key registers the tools immediately; no restart is needed.
|
|
45
|
+
|
|
46
|
+
## Design conventions
|
|
47
|
+
|
|
48
|
+
Three rules apply when a Stitch design is implemented as code in a repository.
|
|
49
|
+
|
|
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
|
+
```
|
|
71
|
+
|
|
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.
|
|
75
|
+
|
|
76
|
+
## Local files
|
|
77
|
+
|
|
78
|
+
| Path | Contents |
|
|
79
|
+
| --- | --- |
|
|
80
|
+
| `$DSH_HOME/.credentials.yaml` | The API key, under `STITCH_API_KEY` |
|
|
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.
|
|
88
|
+
|
|
89
|
+
## Replacing the `mcp-stitch` row
|
|
90
|
+
|
|
91
|
+
This plugin registers the 15 `mcp__stitch__*` names itself, so it **replaces**
|
|
92
|
+
the `mcp-stitch` MCP row rather than coexisting with it: two registrations of one
|
|
93
|
+
tool name throw. Remove that row from `$DSH_HOME/cordis.patch.yml` before
|
|
94
|
+
installing, and set the key on the Plugins page.
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT. See [LICENSE](./LICENSE).
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Patch row of @logictan/dsh-stitch-designer.
|
|
2
|
+
#
|
|
3
|
+
# Two rules this file must keep, both enforced elsewhere:
|
|
4
|
+
#
|
|
5
|
+
# - `name` is the BARE package name. A subpath (`@logictan/dsh-stitch-designer/client`)
|
|
6
|
+
# resolves to no manifest, so the loader cannot read `dsh.bundle.patch` and the row
|
|
7
|
+
# never mounts. The browser half is reached through the package's `./client` export,
|
|
8
|
+
# which `dsh-client-modules` reads from the manifest — never from this row.
|
|
9
|
+
# - `id` equals the host half's `export const name` ('stitch-designer' in
|
|
10
|
+
# src/index.js). `node scripts/aggregate.mjs --check` does NOT validate row-id
|
|
11
|
+
# uniqueness; `pnpm run check:host` does, and a duplicate hard-crashes the host
|
|
12
|
+
# with `duplicate loader entry id`. The "equals `export const name`" half has no
|
|
13
|
+
# automated check at all and is verified by hand.
|
|
14
|
+
#
|
|
15
|
+
# The row carries no `config:` — this plugin has no configuration of its own. The
|
|
16
|
+
# Stitch API key is NOT a patch value: it lives in the credentials file
|
|
17
|
+
# (`$DSH_HOME/.credentials.yaml`, ref `STITCH_API_KEY`) and is edited from the
|
|
18
|
+
# Plugins page, which is what makes it survive config sync without ever appearing
|
|
19
|
+
# in a plaintext snapshot as a patch row.
|
|
20
|
+
- insert:
|
|
21
|
+
- id: stitch-designer
|
|
22
|
+
name: '@logictan/dsh-stitch-designer'
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser half: the API-key card on the plugin's Plugins-page row.
|
|
3
|
+
*
|
|
4
|
+
* This file is already in the client module loader's protocol — it is not a
|
|
5
|
+
* module and imports nothing. `build.mjs` copies it to `lib/client.js`
|
|
6
|
+
* verbatim, which is why `package.json` exports `./client` at that path.
|
|
7
|
+
*
|
|
8
|
+
* `remote.credentials` is injected NESTED inside the card registration rather
|
|
9
|
+
* than declared in this plugin's `inject` list. A plugin that waits on a
|
|
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".
|
|
13
|
+
*/
|
|
14
|
+
window.__ModuleLoader__.load({
|
|
15
|
+
id: "@logictan/dsh-stitch-designer",
|
|
16
|
+
factory: function (require) {
|
|
17
|
+
var React = require("react");
|
|
18
|
+
|
|
19
|
+
/** Loader entry id this card's row is keyed by; equals the host half's `export const name`. */
|
|
20
|
+
var ROW_ID = "stitch-designer";
|
|
21
|
+
/**
|
|
22
|
+
* Bundle package names whose row `ROW_ID` this card configures.
|
|
23
|
+
*
|
|
24
|
+
* The Plugins page keys a row's configuration by the package that declares
|
|
25
|
+
* the row, and this plugin reaches a profile in one of two shapes — as a
|
|
26
|
+
* dependency of this repository's aggregate bundle, or installed on its
|
|
27
|
+
* own. Both keys are registered; the one whose bundle is not installed
|
|
28
|
+
* never renders, because the page dispatches only the keys its own bundles
|
|
29
|
+
* declare.
|
|
30
|
+
*/
|
|
31
|
+
var BUNDLE_NAMES = ["@logictan/dsh-plugins-all", "@logictan/dsh-stitch-designer"];
|
|
32
|
+
/**
|
|
33
|
+
* Credential reference holding the Stitch API key.
|
|
34
|
+
*
|
|
35
|
+
* The key is NOT a plugin config field: `role('secret')` config values are
|
|
36
|
+
* written into the profile's `cordis.patch.yml` in plaintext, which is how
|
|
37
|
+
* the previous generation leaked keys into config-sync snapshots. A
|
|
38
|
+
* credential lives in `$DSH_HOME/.credentials.yaml` and is carried by the
|
|
39
|
+
* `credentialsStatus` sync section instead.
|
|
40
|
+
*/
|
|
41
|
+
var API_KEY_REF = "STITCH_API_KEY";
|
|
42
|
+
|
|
43
|
+
var CSS_ID = "stitch-designer/key-card.css";
|
|
44
|
+
var css = [
|
|
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}",
|
|
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}",
|
|
48
|
+
".std-btn:hover{color:var(--dsw-alias-label-primary, inherit)}",
|
|
49
|
+
".std-btn:disabled{opacity:.5;cursor:default}",
|
|
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}",
|
|
51
|
+
".std-note{font-size:12px;line-height:1.6;color:var(--dsw-alias-label-tertiary, inherit)}",
|
|
52
|
+
".std-ok{color:var(--dsw-alias-success-fg, #22c55e)}",
|
|
53
|
+
".std-bad{color:var(--dsw-alias-danger-fg, #ef4444)}"
|
|
54
|
+
].join("");
|
|
55
|
+
|
|
56
|
+
if (typeof document !== "undefined" && document.querySelector('style[data-plugin-css="' + CSS_ID + '"]') === null) {
|
|
57
|
+
var tag = document.createElement("style");
|
|
58
|
+
tag.dataset.plugin = "stitch-designer";
|
|
59
|
+
tag.dataset.pluginCss = CSS_ID;
|
|
60
|
+
tag.textContent = css;
|
|
61
|
+
document.head.appendChild(tag);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The Stitch API-key card on the plugin's Plugins-page row.
|
|
66
|
+
*
|
|
67
|
+
* The key is write-only across the wire: `describe` reports whether a value
|
|
68
|
+
* exists and never returns it, so the input is never pre-filled. That is a
|
|
69
|
+
* property of the credential seam, not a UI choice — and it is why this
|
|
70
|
+
* card needs no `configForms`: the only setting this plugin has is a
|
|
71
|
+
* credential, which the credential API already owns.
|
|
72
|
+
*/
|
|
73
|
+
function StitchKeyCard(props) {
|
|
74
|
+
var ctx = props.ctx;
|
|
75
|
+
var st = React.useState(null);
|
|
76
|
+
var status = st[0];
|
|
77
|
+
var setStatus = st[1];
|
|
78
|
+
var val = React.useState("");
|
|
79
|
+
var value = val[0];
|
|
80
|
+
var setValue = val[1];
|
|
81
|
+
var msg = React.useState("");
|
|
82
|
+
var message = msg[0];
|
|
83
|
+
var setMessage = msg[1];
|
|
84
|
+
var busy = React.useState(false);
|
|
85
|
+
var pending = busy[0];
|
|
86
|
+
var setPending = busy[1];
|
|
87
|
+
|
|
88
|
+
function read() {
|
|
89
|
+
return ctx.remote.credentials
|
|
90
|
+
.describe([API_KEY_REF])
|
|
91
|
+
.then(function (response) {
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
setMessage("读取失败: " + ((response.error && response.error.message) || "未知错误"));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
setStatus(response.value[API_KEY_REF] || null);
|
|
97
|
+
})
|
|
98
|
+
.catch(function (e) {
|
|
99
|
+
setMessage("读取失败: " + ((e && e.message) || e));
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
React.useEffect(function () {
|
|
104
|
+
read();
|
|
105
|
+
var off = ctx.remote.$on("credentials/reference-updated", function (ref) {
|
|
106
|
+
if (ref === API_KEY_REF) read();
|
|
107
|
+
});
|
|
108
|
+
return function () {
|
|
109
|
+
if (typeof off === "function") off();
|
|
110
|
+
};
|
|
111
|
+
}, []);
|
|
112
|
+
|
|
113
|
+
function save() {
|
|
114
|
+
if (value === "") return;
|
|
115
|
+
setPending(true);
|
|
116
|
+
setMessage("");
|
|
117
|
+
ctx.remote.credentials
|
|
118
|
+
.set(API_KEY_REF, value)
|
|
119
|
+
.then(function (response) {
|
|
120
|
+
if (response && response.ok === false) {
|
|
121
|
+
setMessage("保存失败: " + ((response.error && response.error.message) || "未知错误"));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
setValue("");
|
|
125
|
+
setMessage("已保存。密钥只存本机,随配置同步走 credentials 分区。");
|
|
126
|
+
return read();
|
|
127
|
+
})
|
|
128
|
+
.catch(function (e) {
|
|
129
|
+
setMessage("保存失败: " + ((e && e.message) || e));
|
|
130
|
+
})
|
|
131
|
+
.then(function () {
|
|
132
|
+
setPending(false);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function clear() {
|
|
137
|
+
setPending(true);
|
|
138
|
+
setMessage("");
|
|
139
|
+
ctx.remote.credentials
|
|
140
|
+
.unset(API_KEY_REF)
|
|
141
|
+
.then(function (response) {
|
|
142
|
+
if (response && response.ok === false) {
|
|
143
|
+
setMessage("清除失败: " + ((response.error && response.error.message) || "未知错误"));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
setMessage("已清除。");
|
|
147
|
+
return read();
|
|
148
|
+
})
|
|
149
|
+
.catch(function (e) {
|
|
150
|
+
setMessage("清除失败: " + ((e && e.message) || e));
|
|
151
|
+
})
|
|
152
|
+
.then(function () {
|
|
153
|
+
setPending(false);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
var configured = status && status.configured === true;
|
|
158
|
+
var statusLine = configured
|
|
159
|
+
? React.createElement("div", { className: "std-note std-ok" }, "已配置(值不回读)。来源: " + (status.source || "file"))
|
|
160
|
+
: React.createElement("div", { className: "std-note std-bad" }, "未配置。设置后 15 个 mcp__stitch__* 工具即可用。");
|
|
161
|
+
|
|
162
|
+
return React.createElement(
|
|
163
|
+
"div",
|
|
164
|
+
{ className: "std-card" },
|
|
165
|
+
React.createElement("div", { className: "std-note" }, "Stitch API Key"),
|
|
166
|
+
statusLine,
|
|
167
|
+
React.createElement(
|
|
168
|
+
"div",
|
|
169
|
+
{ className: "std-card-row" },
|
|
170
|
+
React.createElement("input", {
|
|
171
|
+
className: "std-input",
|
|
172
|
+
type: "password",
|
|
173
|
+
value: value,
|
|
174
|
+
placeholder: configured ? "已设置(留空则不改)" : "粘贴 AQ. 开头的 key",
|
|
175
|
+
autoComplete: "off",
|
|
176
|
+
disabled: pending,
|
|
177
|
+
onChange: function (e) {
|
|
178
|
+
setValue(e.target.value);
|
|
179
|
+
},
|
|
180
|
+
}),
|
|
181
|
+
React.createElement(
|
|
182
|
+
"button",
|
|
183
|
+
{ className: "std-btn", disabled: pending || value === "", onClick: save },
|
|
184
|
+
pending ? "保存中…" : "保存",
|
|
185
|
+
),
|
|
186
|
+
React.createElement(
|
|
187
|
+
"button",
|
|
188
|
+
{ className: "std-btn", disabled: pending || !configured, onClick: clear },
|
|
189
|
+
"清除",
|
|
190
|
+
),
|
|
191
|
+
),
|
|
192
|
+
React.createElement(
|
|
193
|
+
"div",
|
|
194
|
+
{ className: "std-note" },
|
|
195
|
+
"key 来自 https://stitch.withgoogle.com(头像 → Stitch Settings → API key)。存于 $DSH_HOME/.credentials.yaml 的 STITCH_API_KEY,不写入插件配置行,因此不会以明文出现在 cordis.patch.yml。",
|
|
196
|
+
),
|
|
197
|
+
message ? React.createElement("div", { className: "std-note" }, message) : null,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function apply(ctx) {
|
|
202
|
+
// Nested: a host without a credential provider loses only this card.
|
|
203
|
+
ctx.inject(["remote.credentials"], function (scoped) {
|
|
204
|
+
BUNDLE_NAMES.forEach(function (bundle) {
|
|
205
|
+
scoped.slots.inject("plugins.row.config", function () {
|
|
206
|
+
return scoped.slots.register(
|
|
207
|
+
{
|
|
208
|
+
name: "plugins.row.config",
|
|
209
|
+
key: bundle + "#" + ROW_ID,
|
|
210
|
+
inject: function () {
|
|
211
|
+
return { ctx: scoped };
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
StitchKeyCard,
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
name: "dsh-stitch-designer",
|
|
223
|
+
inject: ["slots", "remote"],
|
|
224
|
+
apply: apply,
|
|
225
|
+
};
|
|
226
|
+
},
|
|
227
|
+
});
|
|
@@ -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
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-stitch-designer — host half.
|
|
3
|
+
*
|
|
4
|
+
* Google Stitch (AI UI generation) as a first-class part of the harness. It
|
|
5
|
+
* registers the same 15 `mcp__stitch__*` tools the retired `mcp-stitch` patch
|
|
6
|
+
* row used to provide.
|
|
7
|
+
*
|
|
8
|
+
* Three design choices are load-bearing, and each replaces something that was
|
|
9
|
+
* tried and failed elsewhere in this repository:
|
|
10
|
+
*
|
|
11
|
+
* - **The Stitch transport lives here, not in `@deepseek-ai/dsh-mcp-client`.**
|
|
12
|
+
* Stitch's tool schemas are outside the subset `dsh-tools` accepts — 11 of
|
|
13
|
+
* 15 input schemas and 13 of 15 output schemas are rejected by
|
|
14
|
+
* `assertSupportedJsonSchema` — so the bridge has to decide per field what
|
|
15
|
+
* to keep. Owning the transport is also what keeps the API key out of a
|
|
16
|
+
* patch row (see below).
|
|
17
|
+
*
|
|
18
|
+
* - **The API key is a credential, not configuration.** It lives in
|
|
19
|
+
* `$DSH_HOME/.credentials.yaml` under the ref `STITCH_API_KEY`, resolved
|
|
20
|
+
* through `ctx.credentials` on every call. A `role('secret')` Config field
|
|
21
|
+
* would have been the obvious alternative, but it stores the value in the
|
|
22
|
+
* profile's `cordis.patch.yml` as plaintext — which is exactly how the
|
|
23
|
+
* previous generation leaked secrets into config-sync snapshots. As a
|
|
24
|
+
* credential it is still carried by config sync (the `credentialsStatus`
|
|
25
|
+
* section exports refs from the credentials file), and it never appears in a
|
|
26
|
+
* patch row.
|
|
27
|
+
*
|
|
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.
|
|
37
|
+
*
|
|
38
|
+
* @module @logictan/dsh-stitch-designer
|
|
39
|
+
*/
|
|
40
|
+
import { createStitchClient } from './stitch.js';
|
|
41
|
+
import { createInvoker } from './conventions.js';
|
|
42
|
+
import { SECTION_NAME, SECTION_ORDER, STITCH_GUIDANCE } from './guidance.js';
|
|
43
|
+
import { createToolDefinition, publicToolName } from './tools.js';
|
|
44
|
+
|
|
45
|
+
/** Plugin row id; must equal the row id in `cordis.patch.yml`. */
|
|
46
|
+
export const name = 'stitch-designer';
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Credential reference holding the Stitch API key.
|
|
50
|
+
*
|
|
51
|
+
* The reference grammar is `/^[A-Za-z_][A-Za-z0-9_]*$/`, so a POSIX-shell-style
|
|
52
|
+
* name is required; `STITCH_API_KEY` also matches the convention the other
|
|
53
|
+
* providers in this repository use for their keys.
|
|
54
|
+
*/
|
|
55
|
+
export const API_KEY_REF = 'STITCH_API_KEY';
|
|
56
|
+
|
|
57
|
+
/** Server namespace embedded in every public tool name (`mcp__stitch__*`). */
|
|
58
|
+
export const SERVER_NAME = 'stitch';
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Register the Stitch tools and announce the design conventions.
|
|
62
|
+
*
|
|
63
|
+
* @param ctx - the plugin context.
|
|
64
|
+
*/
|
|
65
|
+
export function apply(ctx) {
|
|
66
|
+
/**
|
|
67
|
+
* Resolve the Stitch API key at each call.
|
|
68
|
+
*
|
|
69
|
+
* Read per call rather than captured: the credential can be set, replaced or
|
|
70
|
+
* removed from the Plugins page while the process runs, and a captured value
|
|
71
|
+
* would keep signing with a revoked key until the next restart.
|
|
72
|
+
*
|
|
73
|
+
* @returns the key, or `''` when unset.
|
|
74
|
+
*/
|
|
75
|
+
const resolveApiKey = async () => {
|
|
76
|
+
const credentials = ctx.get('credentials');
|
|
77
|
+
if (credentials === undefined) return '';
|
|
78
|
+
const resolved = await credentials.resolve(API_KEY_REF);
|
|
79
|
+
return resolved?.value ?? '';
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const stitch = createStitchClient({ resolveApiKey });
|
|
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
|
+
|
|
91
|
+
// ---------------------------------------------------------------- tools
|
|
92
|
+
// The upstream descriptors ARE the tool contract — names, descriptions and
|
|
93
|
+
// input schemas all come from Stitch — so registration follows a successful
|
|
94
|
+
// `tools/list` rather than a copy of the list kept in this repository.
|
|
95
|
+
//
|
|
96
|
+
// That fetch needs the API key, which means a freshly installed plugin has
|
|
97
|
+
// no tools until a key exists. The credential-change event is what closes
|
|
98
|
+
// that gap: setting the key in the Plugins page registers the tools in the
|
|
99
|
+
// running process, with no restart. Without it the tools would appear only
|
|
100
|
+
// after the user happened to restart, which reads as "the plugin is broken".
|
|
101
|
+
ctx.inject(['tools', 'credentials'], (toolsCtx) => {
|
|
102
|
+
const tools = toolsCtx.get('tools');
|
|
103
|
+
|
|
104
|
+
toolsCtx.effect(() => {
|
|
105
|
+
let disposed = false;
|
|
106
|
+
let disposers = [];
|
|
107
|
+
|
|
108
|
+
const sync = async () => {
|
|
109
|
+
const listed = await stitch.listTools();
|
|
110
|
+
if (disposed) return;
|
|
111
|
+
const next = listed.map((tool) =>
|
|
112
|
+
createToolDefinition({
|
|
113
|
+
name: publicToolName(SERVER_NAME, tool.name),
|
|
114
|
+
description: tool.description ?? '',
|
|
115
|
+
inputSchema: tool.inputSchema,
|
|
116
|
+
outputSchema: tool.outputSchema,
|
|
117
|
+
call: (args, exec) => invoke(tool.name, args, exec),
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
for (const dispose of disposers) dispose();
|
|
121
|
+
disposers = next.map((definition) => tools.register(definition));
|
|
122
|
+
toolsCtx.logger.info(`stitch-designer: registered ${disposers.length} mcp__${SERVER_NAME}__* tools`);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const report = (error) => {
|
|
126
|
+
if (disposed) return;
|
|
127
|
+
toolsCtx.logger.warn(
|
|
128
|
+
`stitch-designer: the mcp__${SERVER_NAME}__* tools are not registered yet (${error?.message ?? error})`,
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
sync().catch(report);
|
|
133
|
+
const offCredential = toolsCtx.on('credentials/reference-updated', (ref) => {
|
|
134
|
+
if (ref !== API_KEY_REF || disposers.length > 0) return;
|
|
135
|
+
sync().catch(report);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return () => {
|
|
139
|
+
disposed = true;
|
|
140
|
+
offCredential();
|
|
141
|
+
for (const dispose of disposers) dispose();
|
|
142
|
+
disposers = [];
|
|
143
|
+
};
|
|
144
|
+
}, 'stitch-designer: Stitch tools');
|
|
145
|
+
});
|
|
146
|
+
|
|
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
|
+
);
|
|
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/lib/stitch.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport to Google Stitch's MCP endpoint.
|
|
3
|
+
*
|
|
4
|
+
* Stitch speaks stateless Streamable HTTP JSON-RPC: every request carries its
|
|
5
|
+
* own `X-Goog-Api-Key`, no session id is issued or echoed, and the response is
|
|
6
|
+
* always `application/json` — never an SSE stream (measured 2026-10-08 for
|
|
7
|
+
* `initialize`, `tools/list` and `tools/call`). A plain `fetch` is therefore
|
|
8
|
+
* the entire client; there is no handshake to maintain and no session to
|
|
9
|
+
* resume, which is also why `tools/list` works without a preceding
|
|
10
|
+
* `initialize`.
|
|
11
|
+
*
|
|
12
|
+
* @module @logictan/dsh-stitch-designer/stitch
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Stitch's MCP endpoint. */
|
|
16
|
+
export const MCP_URL = 'https://stitch.googleapis.com/mcp';
|
|
17
|
+
|
|
18
|
+
/** Default per-call budget: `generate_screen_from_text` is long-running. */
|
|
19
|
+
export const DEFAULT_TOOL_CALL_TIMEOUT_MS = 180_000;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* One JSON-RPC round trip.
|
|
23
|
+
*
|
|
24
|
+
* @param options - endpoint, API key, method, params, timeout and abort signal.
|
|
25
|
+
* @returns the JSON-RPC envelope.
|
|
26
|
+
* @throws when the HTTP request fails or the response is not a JSON-RPC envelope.
|
|
27
|
+
*/
|
|
28
|
+
async function rpc(options) {
|
|
29
|
+
const { url, apiKey, method, params, timeoutMs, signal } = options;
|
|
30
|
+
const response = await fetch(url, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: {
|
|
33
|
+
'X-Goog-Api-Key': apiKey,
|
|
34
|
+
'Content-Type': 'application/json',
|
|
35
|
+
Accept: 'application/json, text/event-stream',
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({ jsonrpc: '2.0', id: Date.now(), method, params }),
|
|
38
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(timeoutMs)].filter(Boolean)),
|
|
39
|
+
});
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
const detail = await response.text().catch(() => '');
|
|
42
|
+
throw new Error(`Stitch ${method} failed: HTTP ${response.status}${detail ? ` ${detail.slice(0, 300)}` : ''}`);
|
|
43
|
+
}
|
|
44
|
+
const envelope = await response.json();
|
|
45
|
+
if (envelope?.error !== undefined) {
|
|
46
|
+
const message = envelope.error?.message ?? JSON.stringify(envelope.error);
|
|
47
|
+
throw new Error(`Stitch ${method} failed: ${message}`);
|
|
48
|
+
}
|
|
49
|
+
if (envelope?.result === undefined) {
|
|
50
|
+
throw new Error(`Stitch ${method} returned no result`);
|
|
51
|
+
}
|
|
52
|
+
return envelope.result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Create a Stitch client bound to one key-resolution callback.
|
|
57
|
+
*
|
|
58
|
+
* The key is read through a callback rather than captured, because the
|
|
59
|
+
* credential can be set or replaced while the process runs — a captured string
|
|
60
|
+
* would keep signing with a revoked key until the next restart.
|
|
61
|
+
*
|
|
62
|
+
* @param options - key resolver, endpoint override and per-call timeout.
|
|
63
|
+
* @returns `listTools` and `callTool`.
|
|
64
|
+
*/
|
|
65
|
+
export function createStitchClient(options = {}) {
|
|
66
|
+
const {
|
|
67
|
+
resolveApiKey,
|
|
68
|
+
url = MCP_URL,
|
|
69
|
+
timeoutMs = DEFAULT_TOOL_CALL_TIMEOUT_MS,
|
|
70
|
+
} = options;
|
|
71
|
+
|
|
72
|
+
const requireKey = async () => {
|
|
73
|
+
const apiKey = await resolveApiKey();
|
|
74
|
+
if (typeof apiKey !== 'string' || apiKey === '') {
|
|
75
|
+
throw new Error(
|
|
76
|
+
'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).',
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return apiKey;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
/**
|
|
84
|
+
* List the server's tools.
|
|
85
|
+
*
|
|
86
|
+
* @param signal - optional abort signal.
|
|
87
|
+
* @returns the upstream tool descriptors.
|
|
88
|
+
*/
|
|
89
|
+
async listTools(signal) {
|
|
90
|
+
const result = await rpc({ url, apiKey: await requireKey(), method: 'tools/list', params: {}, timeoutMs, signal });
|
|
91
|
+
return Array.isArray(result?.tools) ? result.tools : [];
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Invoke one Stitch tool by its raw name.
|
|
96
|
+
*
|
|
97
|
+
* @param name - Stitch's own tool name.
|
|
98
|
+
* @param args - the tool arguments.
|
|
99
|
+
* @param exec - the tool execution context, whose signal is honoured.
|
|
100
|
+
* @returns the raw MCP result (`content`, `structuredContent`, `isError`).
|
|
101
|
+
*/
|
|
102
|
+
async callTool(name, args, exec) {
|
|
103
|
+
return rpc({
|
|
104
|
+
url,
|
|
105
|
+
apiKey: await requireKey(),
|
|
106
|
+
method: 'tools/call',
|
|
107
|
+
params: { name, arguments: args },
|
|
108
|
+
timeoutMs,
|
|
109
|
+
signal: exec?.signal,
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
package/lib/tools.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adaptation of Google Stitch's own MCP tool descriptors to DSH ToolRuntime
|
|
3
|
+
* definitions.
|
|
4
|
+
*
|
|
5
|
+
* This module is the whole reason the plugin exists as a host-side MCP host
|
|
6
|
+
* rather than a dependency on `@deepseek-ai/dsh-mcp-client`: Stitch's tool
|
|
7
|
+
* vocabulary is not the closed subset `dsh-tools` accepts, and the bridge has
|
|
8
|
+
* to decide field by field what to keep and what to drop.
|
|
9
|
+
*
|
|
10
|
+
* Two measured facts drive every choice here (probed against the live
|
|
11
|
+
* `tools/list` of 2026-10-08, all 15 tools):
|
|
12
|
+
*
|
|
13
|
+
* - `parameters` is passed through VERBATIM. `ToolRuntime.register` validates
|
|
14
|
+
* only `output.schema`; it never inspects `definition.parameters`. Stitch's
|
|
15
|
+
* input schemas carry `$defs` / `$ref` / `x-google-*` (11 of 15 fail
|
|
16
|
+
* `assertSupportedJsonSchema`), so routing them through `defineTool` — which
|
|
17
|
+
* validates its `parameters` at construction — would throw for 11 tools.
|
|
18
|
+
* Registering the raw schema is therefore not a shortcut, it is the only
|
|
19
|
+
* shape that keeps the upstream contract intact for the model.
|
|
20
|
+
* - `output.schema` IS validated, both at registration and again on every
|
|
21
|
+
* result (`createSuccessResult`). 13 of Stitch's 15 advertised
|
|
22
|
+
* `outputSchema`s are outside the accepted subset, so they must be dropped
|
|
23
|
+
* to the loose envelope below — the same fallback
|
|
24
|
+
* `@deepseek-ai/dsh-mcp-client`'s `supportedOutputSchema` performs.
|
|
25
|
+
*
|
|
26
|
+
* @module @logictan/dsh-stitch-designer/tools
|
|
27
|
+
*/
|
|
28
|
+
import { createHash } from 'node:crypto';
|
|
29
|
+
|
|
30
|
+
import { assertSupportedJsonSchema } from '@deepseek-ai/dsh-tools';
|
|
31
|
+
|
|
32
|
+
/** DeepSeek function-name contract: at most 64 characters. */
|
|
33
|
+
const MAX_PUBLIC_NAME_LENGTH = 64;
|
|
34
|
+
/** DeepSeek function-name contract: only `[A-Za-z0-9_-]` is allowed. */
|
|
35
|
+
const INVALID_NAME_CHARS = /[^A-Za-z0-9_-]/g;
|
|
36
|
+
/** Hex chars of the SHA-256 identity hash appended on lossy normalization. */
|
|
37
|
+
const HASH_LENGTH = 12;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Derive the model-facing public name for one Stitch tool.
|
|
41
|
+
*
|
|
42
|
+
* Byte-for-byte the same rule `dsh-mcp-client` applies, because the names are
|
|
43
|
+
* the model's existing vocabulary: this plugin replaces the retired
|
|
44
|
+
* `mcp-stitch` row, and every prompt, note and habit that names
|
|
45
|
+
* `mcp__stitch__generate_screen_from_text` has to keep working. The clean case
|
|
46
|
+
* is `mcp__<serverName>__<rawName>` verbatim; when character replacement or
|
|
47
|
+
* truncation changes it, a 12-hex-char SHA-256 of the identity is appended so
|
|
48
|
+
* two distinct Stitch tools can never collapse onto one public name.
|
|
49
|
+
*
|
|
50
|
+
* @param serverName - stable local namespace; `stitch` reproduces the retired row.
|
|
51
|
+
* @param rawName - Stitch's own tool name.
|
|
52
|
+
* @returns the globally unique, model-facing ToolRuntime name.
|
|
53
|
+
*/
|
|
54
|
+
export function publicToolName(serverName, rawName) {
|
|
55
|
+
const joined = `mcp__${serverName}__${rawName}`;
|
|
56
|
+
const normalized = joined.replace(INVALID_NAME_CHARS, '_');
|
|
57
|
+
if (normalized === joined && normalized.length <= MAX_PUBLIC_NAME_LENGTH) return normalized;
|
|
58
|
+
const hash = createHash('sha256').update(`${serverName}\0${rawName}`).digest('hex').slice(0, HASH_LENGTH);
|
|
59
|
+
return `${normalized.slice(0, MAX_PUBLIC_NAME_LENGTH - HASH_LENGTH - 1)}_${hash}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Keep an advertised output schema only if the runtime accepts it.
|
|
64
|
+
*
|
|
65
|
+
* Stitch's richer schemas (`$defs`, `x-google-*`, `not`) are rejected by
|
|
66
|
+
* `assertSupportedJsonSchema`; the loose envelope in {@link createOutput} is
|
|
67
|
+
* what the tool falls back to. Dropping the schema loses no information the
|
|
68
|
+
* model can act on — the text projection carries the payload either way — but
|
|
69
|
+
* keeping an unsupported one would make registration throw.
|
|
70
|
+
*
|
|
71
|
+
* @param candidate - the upstream `outputSchema`, if any.
|
|
72
|
+
* @returns the schema when supported, otherwise `undefined`.
|
|
73
|
+
*/
|
|
74
|
+
export function supportedOutputSchema(candidate) {
|
|
75
|
+
if (candidate === undefined) return undefined;
|
|
76
|
+
try {
|
|
77
|
+
assertSupportedJsonSchema(candidate);
|
|
78
|
+
return candidate;
|
|
79
|
+
} catch {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Project ordered MCP content blocks into one plain-text string.
|
|
86
|
+
*
|
|
87
|
+
* Stitch returns only `text` blocks (measured across all 15 tools), so the
|
|
88
|
+
* text run is simply joined. A non-text block is reported positionally rather
|
|
89
|
+
* than silently dropped, which is what makes a future image-returning tool
|
|
90
|
+
* visible instead of empty.
|
|
91
|
+
*
|
|
92
|
+
* @param content - the MCP `content` array.
|
|
93
|
+
* @returns the joined text.
|
|
94
|
+
*/
|
|
95
|
+
export function extractText(content) {
|
|
96
|
+
if (!Array.isArray(content)) return '';
|
|
97
|
+
return content
|
|
98
|
+
.map((block) => {
|
|
99
|
+
if (block !== null && typeof block === 'object' && block.type === 'text' && typeof block.text === 'string') {
|
|
100
|
+
return block.text;
|
|
101
|
+
}
|
|
102
|
+
const type = block !== null && typeof block === 'object' && typeof block.type === 'string' ? block.type : 'unknown';
|
|
103
|
+
return `[${type} content is not projected to text]`;
|
|
104
|
+
})
|
|
105
|
+
.join('\n');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Build the canonical result schema and its text projection.
|
|
110
|
+
*
|
|
111
|
+
* The envelope is fixed because `createSuccessResult` validates every returned
|
|
112
|
+
* value against it: `content` is always present, and `structuredContent` is
|
|
113
|
+
* required exactly when a structured schema was kept. With no structured
|
|
114
|
+
* schema the value carries `content` alone and the text projection is the whole
|
|
115
|
+
* model-visible result.
|
|
116
|
+
*
|
|
117
|
+
* @param structuredSchema - an accepted upstream schema, or `undefined`.
|
|
118
|
+
* @returns the `output` field of a ToolRuntime definition.
|
|
119
|
+
*/
|
|
120
|
+
export function createOutput(structuredSchema) {
|
|
121
|
+
return {
|
|
122
|
+
schema: {
|
|
123
|
+
type: 'object',
|
|
124
|
+
properties: {
|
|
125
|
+
content: { type: 'array', items: {} },
|
|
126
|
+
structuredContent: structuredSchema ?? {},
|
|
127
|
+
},
|
|
128
|
+
required: structuredSchema === undefined ? ['content'] : ['content', 'structuredContent'],
|
|
129
|
+
additionalProperties: false,
|
|
130
|
+
},
|
|
131
|
+
render(_args, value) {
|
|
132
|
+
return [{ type: 'text', text: extractText(value.content) }];
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Adapt one upstream Stitch tool descriptor to a ToolRuntime definition.
|
|
139
|
+
*
|
|
140
|
+
* `parameters` is the raw upstream `inputSchema` (see the module note). The
|
|
141
|
+
* result is unregistered; the caller owns registration lifetime.
|
|
142
|
+
*
|
|
143
|
+
* @param options - upstream descriptor fields plus the raw-result callback.
|
|
144
|
+
* @returns the ToolRuntime definition.
|
|
145
|
+
*/
|
|
146
|
+
export function createToolDefinition(options) {
|
|
147
|
+
const { name, description, inputSchema, outputSchema, call, timeoutMs } = options;
|
|
148
|
+
return {
|
|
149
|
+
name,
|
|
150
|
+
description,
|
|
151
|
+
parameters: inputSchema,
|
|
152
|
+
output: createOutput(supportedOutputSchema(outputSchema)),
|
|
153
|
+
...(timeoutMs === undefined ? {} : { timeoutMs }),
|
|
154
|
+
async execute(args, exec) {
|
|
155
|
+
const result = await call(typeof args === 'object' && args !== null ? args : {}, exec);
|
|
156
|
+
const content = Array.isArray(result?.content) ? result.content : [];
|
|
157
|
+
const text = extractText(content);
|
|
158
|
+
if (result?.isError === true) throw new Error(text);
|
|
159
|
+
return {
|
|
160
|
+
content,
|
|
161
|
+
...(result?.structuredContent !== undefined ? { structuredContent: result.structuredContent } : {}),
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logictan/dsh-stitch-designer",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
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
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./client": "./lib/client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"dsh": {
|
|
13
|
+
"bundle": {
|
|
14
|
+
"patch": "./cordis.patch.yml"
|
|
15
|
+
},
|
|
16
|
+
"client": {
|
|
17
|
+
"inject": [
|
|
18
|
+
"slots",
|
|
19
|
+
"remote",
|
|
20
|
+
"remote.credentials"
|
|
21
|
+
],
|
|
22
|
+
"platform": "web"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"lib",
|
|
27
|
+
"cordis.patch.yml",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test": "node --test",
|
|
33
|
+
"build": "node build.mjs",
|
|
34
|
+
"prepare": "node build.mjs",
|
|
35
|
+
"prepack": "node build.mjs"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"dsh",
|
|
39
|
+
"deepseek-harness",
|
|
40
|
+
"dsh-plugin",
|
|
41
|
+
"google-stitch",
|
|
42
|
+
"ai-design",
|
|
43
|
+
"ui-generation",
|
|
44
|
+
"generative-ui",
|
|
45
|
+
"mcp"
|
|
46
|
+
],
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/dale0525/dsh-plugins.git",
|
|
51
|
+
"directory": "packages/dsh-stitch-designer"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@deepseek-ai/cordis": "^4.0.2",
|
|
58
|
+
"@deepseek-ai/dsh-tools": "^0.1.5-alpha.1 || ^0.1.6-alpha.1 || ^0.1.7-alpha.1 || ^0.1.7-rc.1 || ^0.2.0-rc.1 || ^0.2.0-rc.2"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"@deepseek-ai/cordis": {
|
|
62
|
+
"optional": true
|
|
63
|
+
},
|
|
64
|
+
"@deepseek-ai/dsh-tools": {
|
|
65
|
+
"optional": true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@deepseek-ai/cordis": "^4.0.2",
|
|
70
|
+
"@deepseek-ai/dsh-tools": "^0.2.0-rc.2"
|
|
71
|
+
}
|
|
72
|
+
}
|