@xiaobuyu/nodesign 0.0.1 → 0.0.2
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 +156 -106
- package/bin/nodesign.js +0 -0
- package/package.json +1 -1
- package/server/api/README.md +45 -45
- package/server/api/_guard.js +75 -75
- package/server/api/admin.js +187 -187
- package/server/api/assets/docx-page.js +103 -103
- package/server/api/assets/helpers.js +22 -22
- package/server/api/assets/notes.js +144 -144
- package/server/api/assets.js +896 -896
- package/server/api/board.js +57 -57
- package/server/api/browse.js +138 -138
- package/server/api/canvas.js +444 -444
- package/server/api/chatai.js +324 -324
- package/server/api/exports/build-standalone.js +980 -980
- package/server/api/exports/cards.js +118 -118
- package/server/api/exports/docx-pdf.js +46 -46
- package/server/api/exports/export-page.js +142 -142
- package/server/api/exports/handoff.js +147 -147
- package/server/api/exports.js +690 -690
- package/server/api/instruction.js +75 -75
- package/server/api/me.js +116 -116
- package/server/api/memory.js +185 -185
- package/server/api/pending-changes.js +366 -366
- package/server/api/plugins.js +164 -164
- package/server/api/projects.js +220 -220
- package/server/api/publish.js +64 -64
- package/server/api/recent.js +73 -73
- package/server/api/sessions.js +589 -589
- package/server/api/skills.js +77 -77
- package/server/api/standalone-fit.js +168 -168
- package/server/api/turn-compose.js +180 -180
- package/server/api/turn-inflight.js +44 -44
- package/server/auth/README.md +17 -17
- package/server/auth/middleware.js +138 -138
- package/server/auth/origin-guard.js +71 -71
- package/server/auth/session.js +108 -108
- package/server/auth/tier.js +113 -113
- package/server/auth/users-store.js +348 -348
- package/server/edit/README.md +29 -29
- package/server/engine/README.md +113 -113
- package/server/engine/agent/agent-shared.js +550 -550
- package/server/engine/agent/auto-mode-default-hard-deny.txt +4 -4
- package/server/engine/agent/auto-mode-rules.js +78 -78
- package/server/engine/agent/context.js +418 -418
- package/server/engine/agent/events.js +464 -464
- package/server/engine/agent/hooks/canvas-validate.js +199 -199
- package/server/engine/agent/hooks/failure.js +120 -120
- package/server/engine/agent/hooks/file-events.js +75 -75
- package/server/engine/agent/hooks/lifecycle.js +233 -233
- package/server/engine/agent/hooks/post-canvas-focus.js +80 -80
- package/server/engine/agent/hooks/post-guidance.js +206 -206
- package/server/engine/agent/hooks/post-subagent-report.js +104 -104
- package/server/engine/agent/hooks/post-trim.js +58 -58
- package/server/engine/agent/hooks/pre-board-neighborhood.js +34 -34
- package/server/engine/agent/hooks/pre-defaults.js +84 -84
- package/server/engine/agent/hooks/pre-injectors.js +277 -277
- package/server/engine/agent/hooks/pre-performance-log-guard.js +78 -78
- package/server/engine/agent/hooks/pre-starter-files.js +61 -61
- package/server/engine/agent/hooks/pre-workspace-scope-guard.js +78 -78
- package/server/engine/agent/hooks/site-validate.js +94 -94
- package/server/engine/agent/hooks/tool-prompts.js +31 -31
- package/server/engine/agent/hooks/turn-state-memory.js +56 -56
- package/server/engine/agent/hooks/user-prompt-submit.js +215 -215
- package/server/engine/agent/hooks.js +354 -354
- package/server/engine/agent/init-contract.js +69 -69
- package/server/engine/agent/isolation.js +159 -159
- package/server/engine/agent/model-table.js +292 -292
- package/server/engine/agent/plugin-loader.js +266 -266
- package/server/engine/agent/prompts/nodesign-prelude.md +336 -336
- package/server/engine/agent/prompts/tools/ask-user-question-protocol.md +71 -71
- package/server/engine/agent/prompts/tools/direct-edit-protocol.md +98 -98
- package/server/engine/agent/prompts/tools/generate-image-cookbook.gemini-gateway.md +583 -583
- package/server/engine/agent/prompts/tools/generate-image-cookbook.md +483 -483
- package/server/engine/agent/prompts/tools/hybrid-reference.md +100 -100
- package/server/engine/agent/prompts/tools/paint-still-cookbook.md +270 -270
- package/server/engine/agent/prompts/tools/roll-film-cookbook.md +70 -70
- package/server/engine/agent/prompts/tools/site-reference.md +106 -106
- package/server/engine/agent/prompts/tools/tweaks-syntax.md +125 -125
- package/server/engine/agent/prompts/tools/vision-checker-dispatch.md +48 -48
- package/server/engine/agent/session-loop.js +1024 -1024
- package/server/engine/agent/session-model.js +164 -164
- package/server/engine/agent/skill.js +204 -204
- package/server/engine/agent/task-events.js +78 -78
- package/server/engine/agents/ds-extractor.md +132 -132
- package/server/engine/agents/explorer.md +190 -190
- package/server/engine/agents/index.js +230 -230
- package/server/engine/agents/schemas/design-system.json +156 -156
- package/server/engine/agents/schemas/tweak-schema.json +113 -113
- package/server/engine/agents/tweak-proposer.md +127 -127
- package/server/engine/agents/vision-checker.md +254 -254
- package/server/engine/browse/capture.js +422 -422
- package/server/engine/browse/card.js +137 -137
- package/server/engine/browse/handover.js +59 -59
- package/server/engine/browse/page-digest.js +119 -119
- package/server/engine/browse/refs.js +196 -196
- package/server/engine/browse/registry.js +307 -307
- package/server/engine/browse/screencast.js +170 -170
- package/server/engine/browse/state.js +121 -121
- package/server/engine/chatai/chat-log.js +79 -79
- package/server/engine/chatai/index.js +193 -193
- package/server/engine/chatai/openai-compat.js +152 -152
- package/server/engine/chatai/orchestrate.js +309 -309
- package/server/engine/chatai/perform.js +40 -40
- package/server/engine/chatai/summarize.js +79 -79
- package/server/engine/mcp/capability-gate.js +52 -52
- package/server/engine/mcp/index.js +339 -339
- package/server/engine/mcp/param-sanitizer.js +142 -142
- package/server/engine/mcp/tools/arrange-on-board.js +124 -124
- package/server/engine/mcp/tools/artifact-session.js +278 -278
- package/server/engine/mcp/tools/browse-computer.js +421 -421
- package/server/engine/mcp/tools/browse-find-batch.js +183 -183
- package/server/engine/mcp/tools/browse-screenshot.js +157 -157
- package/server/engine/mcp/tools/browse.js +480 -480
- package/server/engine/mcp/tools/build-docx.js +101 -101
- package/server/engine/mcp/tools/clear-pending-changes.js +99 -99
- package/server/engine/mcp/tools/create-on-board.js +155 -155
- package/server/engine/mcp/tools/crystallize-skill.js +168 -168
- package/server/engine/mcp/tools/explain-style.js +237 -237
- package/server/engine/mcp/tools/export-handoff.js +133 -133
- package/server/engine/mcp/tools/expose-tweaks.js +149 -149
- package/server/engine/mcp/tools/generate-image.js +842 -842
- package/server/engine/mcp/tools/get-computed-styles.js +164 -164
- package/server/engine/mcp/tools/get-pending-changes.js +205 -205
- package/server/engine/mcp/tools/h3box-ssh.js +84 -84
- package/server/engine/mcp/tools/helpers/acquire-page.js +82 -82
- package/server/engine/mcp/tools/helpers/motion-lab.js +554 -554
- package/server/engine/mcp/tools/helpers/motion-scroll.js +112 -112
- package/server/engine/mcp/tools/helpers/perception-page.js +215 -215
- package/server/engine/mcp/tools/helpers/reference-download.js +103 -103
- package/server/engine/mcp/tools/helpers/rembg-bridge.py +80 -80
- package/server/engine/mcp/tools/helpers/shot-pipeline.js +280 -280
- package/server/engine/mcp/tools/highlight.js +68 -68
- package/server/engine/mcp/tools/list-pages.js +222 -222
- package/server/engine/mcp/tools/lookup-tags.js +59 -59
- package/server/engine/mcp/tools/navigate-to-page.js +60 -60
- package/server/engine/mcp/tools/organize-board.js +64 -64
- package/server/engine/mcp/tools/paint-still.js +434 -434
- package/server/engine/mcp/tools/pin-to-board.js +130 -130
- package/server/engine/mcp/tools/preview-deck.js +130 -130
- package/server/engine/mcp/tools/profile-scroll.js +305 -305
- package/server/engine/mcp/tools/publish-site.js +102 -102
- package/server/engine/mcp/tools/query-elements.js +195 -195
- package/server/engine/mcp/tools/read-board.js +103 -103
- package/server/engine/mcp/tools/read-document.js +93 -93
- package/server/engine/mcp/tools/read-page.js +264 -264
- package/server/engine/mcp/tools/read-tavern-json.js +143 -143
- package/server/engine/mcp/tools/record-decision.js +140 -140
- package/server/engine/mcp/tools/relate-on-board.js +110 -110
- package/server/engine/mcp/tools/report-issue.js +115 -115
- package/server/engine/mcp/tools/roll-film.js +266 -266
- package/server/engine/mcp/tools/screenshot-docx.js +138 -138
- package/server/engine/mcp/tools/screenshot-url.js +195 -195
- package/server/engine/mcp/tools/screenshot.js +583 -583
- package/server/engine/mcp/tools/tier-gate.js +96 -96
- package/server/engine/mcp/tools/trace-motion.js +215 -215
- package/server/engine/mcp/tools/web-search.js +548 -548
- package/server/engine/motion/inventory.js +349 -349
- package/server/engine/perception/session.js +235 -235
- package/server/engine/plugins/nodesign/.claude-plugin/plugin.json +5 -5
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/SKILL.md +192 -192
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/canvas.template.html +667 -667
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/hybrid-grid.md +22 -22
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/image-led-cover.md +25 -25
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/portrait.md +23 -23
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/quote-backdrop.md +21 -21
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/section-divider.md +23 -23
- package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/text-led.md +21 -21
- package/server/engine/plugins/nodesign/skills/docx-craft/SKILL.md +162 -162
- package/server/engine/plugins/nodesign/skills/docx-craft/references/token-schema.md +407 -407
- package/server/engine/plugins/nodesign/skills/docx-craft//346/226/207/346/241/243.template.json +80 -80
- package/server/engine/plugins/nodesign/skills/rp-craft/SKILL.md +228 -228
- package/server/engine/plugins/nodesign/skills/rp-craft/patterns//346/274/224/345/207/272/351/241/265-/346/234/200/345/260/217/345/256/236/347/216/260.html +78 -78
- package/server/engine/plugins/nodesign/skills/rp-craft//346/274/224/345/207/272.template.js +268 -268
- package/server/engine/plugins/nodesign/skills/site-craft/SKILL.md +323 -323
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/build-lane.md +134 -134
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/cutout-collage.md +61 -61
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/mock-app.md +163 -163
- package/server/engine/plugins/nodesign/skills/site-craft/patterns/page-transitions.md +86 -86
- package/server/engine/runs/active-runs.js +698 -698
- package/server/engine/runs/live-turn.js +252 -252
- package/server/engine/runs/store.js +364 -364
- package/server/engine/runs/turn-relay.js +217 -217
- package/server/engine/runtime/workspace.js +164 -164
- package/server/lib/artifact-file-path.js +80 -80
- package/server/lib/artifact-target.js +284 -284
- package/server/lib/asset-refs.js +239 -239
- package/server/lib/async-queue.js +104 -104
- package/server/lib/auto-relations.js +130 -130
- package/server/lib/binding-types.js +60 -60
- package/server/lib/board-kind-sizes.js +62 -62
- package/server/lib/board-relations.js +133 -133
- package/server/lib/browse-proxy.js +180 -180
- package/server/lib/canvas-id.js +36 -36
- package/server/lib/cover.js +227 -227
- package/server/lib/danbooru-tags.js +291 -291
- package/server/lib/doc-extract.js +156 -156
- package/server/lib/docx/build-from-source.js +260 -260
- package/server/lib/docx/build.js +490 -490
- package/server/lib/docx/dump-styles.js +287 -287
- package/server/lib/docx/fonts/nodesign-cjk.conf +164 -164
- package/server/lib/docx/merge-runs.js +129 -129
- package/server/lib/docx/numbering.js +218 -218
- package/server/lib/docx/order.js +152 -152
- package/server/lib/docx/rawzip.js +172 -172
- package/server/lib/docx/render.js +92 -92
- package/server/lib/docx/text-lint.js +225 -225
- package/server/lib/docx/tokens.js +335 -335
- package/server/lib/docx/units.js +29 -29
- package/server/lib/docx/xml.js +291 -291
- package/server/lib/docx-pages.js +187 -187
- package/server/lib/export-collect.js +289 -289
- package/server/lib/export-package.js +210 -210
- package/server/lib/html-srcset.js +145 -145
- package/server/lib/image-variant.js +460 -460
- package/server/lib/ingress/forward-openai-chat.js +305 -305
- package/server/lib/ingress/openai-chat.js +491 -491
- package/server/lib/ingress/session-notice.js +66 -66
- package/server/lib/ingress/session-routes.js +68 -68
- package/server/lib/ingress/slot-probe.js +130 -130
- package/server/lib/ingress/upstream-billing.js +52 -52
- package/server/lib/ingress/upstream-fail-streak.js +72 -72
- package/server/lib/ingress/upstream-truncation.js +48 -48
- package/server/lib/issues-store.js +182 -182
- package/server/lib/kinds/deck.js +72 -72
- package/server/lib/kinds/docx.js +283 -283
- package/server/lib/kinds/file-kinds.js +113 -113
- package/server/lib/kinds/index.js +202 -202
- package/server/lib/kinds/site.js +161 -161
- package/server/lib/model-ingress.js +543 -543
- package/server/lib/moderation.js +255 -255
- package/server/lib/notice-store.js +103 -103
- package/server/lib/plugin-install.js +104 -104
- package/server/lib/plugin-validator.js +721 -721
- package/server/lib/publish-store.js +115 -115
- package/server/lib/quick-summary.js +52 -52
- package/server/lib/quota.js +270 -270
- package/server/lib/rate-window.js +29 -29
- package/server/lib/reference-assets.js +92 -92
- package/server/lib/region-shot.js +135 -135
- package/server/lib/safe-path.js +73 -73
- package/server/lib/sdk-session.js +33 -33
- package/server/lib/showcase-store.js +92 -92
- package/server/lib/site-publish.js +465 -465
- package/server/lib/ssrf-guard.js +432 -432
- package/server/lib/task-scan.js +158 -158
- package/server/lib/tavern-json.js +154 -154
- package/server/lib/video-variant.js +237 -237
- package/server/lib/workspace-path.js +33 -33
- package/server/ops/fix-sdk-musl.mjs +40 -40
- package/server/ops/install-macos-fonts.sh +114 -114
- package/server/ops/macos-fonts.conf +336 -336
- package/server/ops/sandbox-shim/bwrap +57 -57
- package/server/projects/assets-summary.js +119 -119
- package/server/projects/auto-name.js +53 -53
- package/server/projects/board-store.js +640 -640
- package/server/projects/move-entry.js +103 -103
- package/server/projects/store.js +270 -270
- package/server/projects/ui-config.js +54 -54
- package/server/projects/workspace-templates.js +53 -53
- package/server/projects/workspace.js +1025 -1025
- package/server/runtime/local-config.js +177 -177
- package/server/runtime/profile.js +72 -72
- package/server/services/rembg-service.py +334 -334
- package/server/shared/README.md +25 -25
- package/server/shared/deck.js +54 -54
- package/server/shared/time.js +49 -49
- package/server/style-pipeline/README.md +53 -53
- package/server/ws/broker.js +36 -36
- package/server/ws/browse-channel.js +177 -177
- package/server/ws/index.js +386 -386
- package/web/README.md +89 -89
- package/web/dist/assets/BrowserWindow-BYCgizHw.js +11 -0
- package/web/dist/assets/{DeckWindow-4G0xX7P3.js → DeckWindow-pNydEM4L.js} +1 -1
- package/web/dist/assets/{DocxWindow-BXQsoPkh.js → DocxWindow-PPgzh3Gq.js} +1 -1
- package/web/dist/assets/{SiteWindow-D6eS-2gM.js → SiteWindow-DM0LQPCO.js} +1 -1
- package/web/dist/assets/{index-Cbl3ATvp.js → index-BHTBJlpY.js} +32 -32
- package/web/dist/assets/{pending-edit-apply-DTjo2geW.js → pending-edit-apply-C0I13CbD.js} +1 -1
- package/web/dist/assets/{rotate-cw-LMT3x1Zj.js → rotate-cw-B1UQ8Hld.js} +1 -1
- package/web/dist/assets/{square-dashed-mouse-pointer-CBoAkfhi.js → square-dashed-mouse-pointer-8lrgqmfv.js} +1 -1
- package/web/dist/index.html +17 -17
- package/web/dist/assets/BrowserWindow-Dc7J7CYc.js +0 -11
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mcp/tools/create-on-board.js —— create_on_board(2026-08-14,agent 建元素批)
|
|
3
|
-
*
|
|
4
|
-
* 让 agent 落**用户级的画布原生物件**。v1 只开手写字(text):便签是留档不是
|
|
5
|
-
* 聊天,agent 值得写下来的是"这一版为什么这么改"这类一句话,不是回复的搬运。
|
|
6
|
-
* 涂鸦不开 —— agent 画矢量笔迹没有意义,它要画图走生图。
|
|
7
|
-
*
|
|
8
|
-
* 设计对齐用户侧:
|
|
9
|
-
* - near 锚定 = 用户"标注"的同构(落在锚右侧空白,关系线可选一步带上)——
|
|
10
|
-
* 一次调用 = 一段字 + 一条线,跟 keepAnnotation 落的东西一模一样
|
|
11
|
-
* - 字体/字号走 board-store 同一份白名单(TEXT_FONTS)
|
|
12
|
-
* - 尺寸估算抄前端 handleCreateText 同款公式(约 26 全角字/行)
|
|
13
|
-
* - by:'agent' 出处,用户一眼分得出谁写的
|
|
14
|
-
*
|
|
15
|
-
* 护栏:每回合上限 4 条 —— 画布是版面不是弹幕区。
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { tool } from '@anthropic-ai/claude-agent-sdk';
|
|
19
|
-
import { z } from 'zod';
|
|
20
|
-
import { readBoard, patchBoard, TEXT_FONTS } from '../../../projects/board-store.js';
|
|
21
|
-
import { estimateSize } from '../../../lib/board-kind-sizes.js';
|
|
22
|
-
import { normalizeCanvasId, layerOf } from '../../../lib/canvas-id.js';
|
|
23
|
-
|
|
24
|
-
const MAX_PER_TURN = 4;
|
|
25
|
-
const SIZE_PX = { sm: 13, md: 16, lg: 22, xl: 30 };
|
|
26
|
-
|
|
27
|
-
let seq = 0;
|
|
28
|
-
const nextTextId = () => `text:a${Date.now().toString(36)}${(seq++ % 1000).toString(36)}`;
|
|
29
|
-
const nextBindingId = () => `b:a${Date.now().toString(36)}n${(seq++ % 1000).toString(36)}`;
|
|
30
|
-
|
|
31
|
-
/** 前端 handleCreateText 同款身位估算(约 26 全角字/行,行高 1.6) */
|
|
32
|
-
function textBox(t, sizeKey) {
|
|
33
|
-
const px = SIZE_PX[sizeKey] || 16;
|
|
34
|
-
const cols = Math.min(26, Math.max(6, t.length));
|
|
35
|
-
const lines = Math.ceil(t.length / cols) + (t.match(/\n/g)?.length || 0);
|
|
36
|
-
return { w: Math.round(cols * px * 1.05) + 12, h: Math.round(lines * px * 1.6) + 10 };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function makeCreateOnBoardTool({ projectId, ctx }) {
|
|
40
|
-
const turnStamp = { turn: -1, count: 0 };
|
|
41
|
-
return tool(
|
|
42
|
-
'create_on_board',
|
|
43
|
-
`Write a handwritten note directly on the canvas — the same kind the user writes.
|
|
44
|
-
|
|
45
|
-
Use it for remarks worth KEEPING on the board: why a version went this way, what
|
|
46
|
-
to compare, a caption for a group. Not for conversation (that goes in your reply)
|
|
47
|
-
and not for documents (write a .md instead). Hard cap: ${MAX_PER_TURN} notes per turn.
|
|
48
|
-
|
|
49
|
-
- near: anchor it beside a thing (and usually pass relation to draw the line —
|
|
50
|
-
that is exactly the user's "annotation" gesture: a note + an annotates edge)
|
|
51
|
-
- omit near: the note lands under the desktop's current content`,
|
|
52
|
-
{
|
|
53
|
-
text: z.string().min(1).max(200).describe('The note text (≤200 chars, one thought)'),
|
|
54
|
-
near: z.string().max(300).optional()
|
|
55
|
-
.describe('Canvas id to place the note next to (right side, auto-avoids overlap)'),
|
|
56
|
-
relation: z.object({
|
|
57
|
-
type: z.enum(['annotates', 'link']).describe('annotates = the note is ABOUT it; link = loosely related'),
|
|
58
|
-
to: z.string().min(1).max(300).describe('Canvas id the line connects to (usually = near)'),
|
|
59
|
-
label: z.string().max(60).optional().describe('Optional words on the line (link only usually)'),
|
|
60
|
-
}).optional().describe('Draw a relation line from the note in the same call'),
|
|
61
|
-
font: z.enum(['pen', 'kai', 'sans', 'serif', 'mono']).optional()
|
|
62
|
-
.describe('Handwriting style (default pen — same as the user\'s default)'),
|
|
63
|
-
size: z.enum(['sm', 'md', 'lg', 'xl']).optional().describe('Text size (default md)'),
|
|
64
|
-
},
|
|
65
|
-
async ({ text, near: rawNear, relation, font, size }) => {
|
|
66
|
-
if (!projectId) {
|
|
67
|
-
return { content: [{ type: 'text', text: 'No project bound.' }], isError: true };
|
|
68
|
-
}
|
|
69
|
-
const err = (t) => ({ content: [{ type: 'text', text: t }], isError: true });
|
|
70
|
-
|
|
71
|
-
const turn = ctx?.counters?.turns ?? -1;
|
|
72
|
-
if (turnStamp.turn !== turn) { turnStamp.turn = turn; turnStamp.count = 0; }
|
|
73
|
-
if (turnStamp.count >= MAX_PER_TURN) {
|
|
74
|
-
return err(`本回合便签额度用完(${MAX_PER_TURN} 条)—— 画布是版面不是弹幕区,要说的话说给用户听。`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const t = String(text).trim();
|
|
78
|
-
if (!t) return err('空字不落板。');
|
|
79
|
-
const sizeKey = SIZE_PX[size] ? size : 'md';
|
|
80
|
-
const box = textBox(t, sizeKey);
|
|
81
|
-
|
|
82
|
-
const board = await readBoard(projectId);
|
|
83
|
-
const known = new Set(Object.keys(board.zones || {}));
|
|
84
|
-
let pos; let zone = '';
|
|
85
|
-
if (rawNear) {
|
|
86
|
-
const near = normalizeCanvasId(rawNear);
|
|
87
|
-
const anchorEntry = near ? board.objects?.[near] : null;
|
|
88
|
-
if (!anchorEntry || !Number.isFinite(anchorEntry.x)) {
|
|
89
|
-
return err(`锚点 ${rawNear} 还没有座位(read_board 里看不到就锚不上)。`);
|
|
90
|
-
}
|
|
91
|
-
zone = layerOf(near, anchorEntry, known);
|
|
92
|
-
const aSize = estimateSize(near, anchorEntry);
|
|
93
|
-
pos = { x: Math.round(anchorEntry.x + aSize.w + 24), y: Math.round(anchorEntry.y) };
|
|
94
|
-
// 撞卡往下让(同 arrange 的避让)
|
|
95
|
-
const obstacles = Object.entries(board.objects || {})
|
|
96
|
-
.filter(([id, e]) => Number.isFinite(e?.x) && layerOf(id, e, known) === zone)
|
|
97
|
-
.map(([id, e]) => ({ ...estimateSize(id, e), x: e.x, y: e.y }));
|
|
98
|
-
for (let g = 0; g < 40; g += 1) {
|
|
99
|
-
const blocker = obstacles.find(o =>
|
|
100
|
-
!(pos.x + box.w <= o.x || o.x + o.w <= pos.x || pos.y + box.h <= o.y || o.y + o.h <= pos.y));
|
|
101
|
-
if (!blocker) break;
|
|
102
|
-
pos.y = blocker.y + blocker.h + 16;
|
|
103
|
-
}
|
|
104
|
-
} else {
|
|
105
|
-
// 桌面根层内容最低边下面(跟新物件入座同一条起排线精神)
|
|
106
|
-
let bottom = 0;
|
|
107
|
-
for (const [id, e] of Object.entries(board.objects || {})) {
|
|
108
|
-
if (!Number.isFinite(e?.y) || layerOf(id, e, known) !== '') continue;
|
|
109
|
-
bottom = Math.max(bottom, e.y + estimateSize(id, e).h);
|
|
110
|
-
}
|
|
111
|
-
for (const zz of Object.values(board.zones || {})) {
|
|
112
|
-
if (Number.isFinite(zz?.y)) bottom = Math.max(bottom, zz.y + 150);
|
|
113
|
-
}
|
|
114
|
-
pos = { x: 10, y: Math.round(bottom) + 16 };
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const textId = nextTextId();
|
|
118
|
-
const patch = {
|
|
119
|
-
objects: {
|
|
120
|
-
[textId]: {
|
|
121
|
-
x: pos.x, y: pos.y, z: 1, w: box.w, h: box.h,
|
|
122
|
-
kind: 'text',
|
|
123
|
-
data: { t, font: TEXT_FONTS.includes(font) ? font : 'pen', size: sizeKey, color: 'ink' },
|
|
124
|
-
zone, by: 'agent',
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
if (relation) {
|
|
129
|
-
const to = normalizeCanvasId(relation.to);
|
|
130
|
-
if (to && to !== textId) {
|
|
131
|
-
patch.bindings = {
|
|
132
|
-
[nextBindingId()]: {
|
|
133
|
-
type: relation.type, from: textId, to, by: 'agent',
|
|
134
|
-
...(relation.label ? { label: relation.label } : {}),
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const saved = await patchBoard(projectId, patch);
|
|
141
|
-
if (!saved.objects?.[textId]) return err('便签被 board 拒了(内容或字段不合法)。');
|
|
142
|
-
turnStamp.count += 1;
|
|
143
|
-
try {
|
|
144
|
-
ctx?.emit?.({ type: 'board.updated', sessionId: null, summary: '写了一条画布便签' });
|
|
145
|
-
} catch { /* fail-soft */ }
|
|
146
|
-
return {
|
|
147
|
-
content: [{
|
|
148
|
-
type: 'text',
|
|
149
|
-
text: `Note on board at (${pos.x},${pos.y})${relation ? ' with a line' : ''} (id: ${textId}).`
|
|
150
|
-
+ ` ${MAX_PER_TURN - turnStamp.count} left this turn.`,
|
|
151
|
-
}],
|
|
152
|
-
};
|
|
153
|
-
},
|
|
154
|
-
);
|
|
155
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* mcp/tools/create-on-board.js —— create_on_board(2026-08-14,agent 建元素批)
|
|
3
|
+
*
|
|
4
|
+
* 让 agent 落**用户级的画布原生物件**。v1 只开手写字(text):便签是留档不是
|
|
5
|
+
* 聊天,agent 值得写下来的是"这一版为什么这么改"这类一句话,不是回复的搬运。
|
|
6
|
+
* 涂鸦不开 —— agent 画矢量笔迹没有意义,它要画图走生图。
|
|
7
|
+
*
|
|
8
|
+
* 设计对齐用户侧:
|
|
9
|
+
* - near 锚定 = 用户"标注"的同构(落在锚右侧空白,关系线可选一步带上)——
|
|
10
|
+
* 一次调用 = 一段字 + 一条线,跟 keepAnnotation 落的东西一模一样
|
|
11
|
+
* - 字体/字号走 board-store 同一份白名单(TEXT_FONTS)
|
|
12
|
+
* - 尺寸估算抄前端 handleCreateText 同款公式(约 26 全角字/行)
|
|
13
|
+
* - by:'agent' 出处,用户一眼分得出谁写的
|
|
14
|
+
*
|
|
15
|
+
* 护栏:每回合上限 4 条 —— 画布是版面不是弹幕区。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { tool } from '@anthropic-ai/claude-agent-sdk';
|
|
19
|
+
import { z } from 'zod';
|
|
20
|
+
import { readBoard, patchBoard, TEXT_FONTS } from '../../../projects/board-store.js';
|
|
21
|
+
import { estimateSize } from '../../../lib/board-kind-sizes.js';
|
|
22
|
+
import { normalizeCanvasId, layerOf } from '../../../lib/canvas-id.js';
|
|
23
|
+
|
|
24
|
+
const MAX_PER_TURN = 4;
|
|
25
|
+
const SIZE_PX = { sm: 13, md: 16, lg: 22, xl: 30 };
|
|
26
|
+
|
|
27
|
+
let seq = 0;
|
|
28
|
+
const nextTextId = () => `text:a${Date.now().toString(36)}${(seq++ % 1000).toString(36)}`;
|
|
29
|
+
const nextBindingId = () => `b:a${Date.now().toString(36)}n${(seq++ % 1000).toString(36)}`;
|
|
30
|
+
|
|
31
|
+
/** 前端 handleCreateText 同款身位估算(约 26 全角字/行,行高 1.6) */
|
|
32
|
+
function textBox(t, sizeKey) {
|
|
33
|
+
const px = SIZE_PX[sizeKey] || 16;
|
|
34
|
+
const cols = Math.min(26, Math.max(6, t.length));
|
|
35
|
+
const lines = Math.ceil(t.length / cols) + (t.match(/\n/g)?.length || 0);
|
|
36
|
+
return { w: Math.round(cols * px * 1.05) + 12, h: Math.round(lines * px * 1.6) + 10 };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function makeCreateOnBoardTool({ projectId, ctx }) {
|
|
40
|
+
const turnStamp = { turn: -1, count: 0 };
|
|
41
|
+
return tool(
|
|
42
|
+
'create_on_board',
|
|
43
|
+
`Write a handwritten note directly on the canvas — the same kind the user writes.
|
|
44
|
+
|
|
45
|
+
Use it for remarks worth KEEPING on the board: why a version went this way, what
|
|
46
|
+
to compare, a caption for a group. Not for conversation (that goes in your reply)
|
|
47
|
+
and not for documents (write a .md instead). Hard cap: ${MAX_PER_TURN} notes per turn.
|
|
48
|
+
|
|
49
|
+
- near: anchor it beside a thing (and usually pass relation to draw the line —
|
|
50
|
+
that is exactly the user's "annotation" gesture: a note + an annotates edge)
|
|
51
|
+
- omit near: the note lands under the desktop's current content`,
|
|
52
|
+
{
|
|
53
|
+
text: z.string().min(1).max(200).describe('The note text (≤200 chars, one thought)'),
|
|
54
|
+
near: z.string().max(300).optional()
|
|
55
|
+
.describe('Canvas id to place the note next to (right side, auto-avoids overlap)'),
|
|
56
|
+
relation: z.object({
|
|
57
|
+
type: z.enum(['annotates', 'link']).describe('annotates = the note is ABOUT it; link = loosely related'),
|
|
58
|
+
to: z.string().min(1).max(300).describe('Canvas id the line connects to (usually = near)'),
|
|
59
|
+
label: z.string().max(60).optional().describe('Optional words on the line (link only usually)'),
|
|
60
|
+
}).optional().describe('Draw a relation line from the note in the same call'),
|
|
61
|
+
font: z.enum(['pen', 'kai', 'sans', 'serif', 'mono']).optional()
|
|
62
|
+
.describe('Handwriting style (default pen — same as the user\'s default)'),
|
|
63
|
+
size: z.enum(['sm', 'md', 'lg', 'xl']).optional().describe('Text size (default md)'),
|
|
64
|
+
},
|
|
65
|
+
async ({ text, near: rawNear, relation, font, size }) => {
|
|
66
|
+
if (!projectId) {
|
|
67
|
+
return { content: [{ type: 'text', text: 'No project bound.' }], isError: true };
|
|
68
|
+
}
|
|
69
|
+
const err = (t) => ({ content: [{ type: 'text', text: t }], isError: true });
|
|
70
|
+
|
|
71
|
+
const turn = ctx?.counters?.turns ?? -1;
|
|
72
|
+
if (turnStamp.turn !== turn) { turnStamp.turn = turn; turnStamp.count = 0; }
|
|
73
|
+
if (turnStamp.count >= MAX_PER_TURN) {
|
|
74
|
+
return err(`本回合便签额度用完(${MAX_PER_TURN} 条)—— 画布是版面不是弹幕区,要说的话说给用户听。`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const t = String(text).trim();
|
|
78
|
+
if (!t) return err('空字不落板。');
|
|
79
|
+
const sizeKey = SIZE_PX[size] ? size : 'md';
|
|
80
|
+
const box = textBox(t, sizeKey);
|
|
81
|
+
|
|
82
|
+
const board = await readBoard(projectId);
|
|
83
|
+
const known = new Set(Object.keys(board.zones || {}));
|
|
84
|
+
let pos; let zone = '';
|
|
85
|
+
if (rawNear) {
|
|
86
|
+
const near = normalizeCanvasId(rawNear);
|
|
87
|
+
const anchorEntry = near ? board.objects?.[near] : null;
|
|
88
|
+
if (!anchorEntry || !Number.isFinite(anchorEntry.x)) {
|
|
89
|
+
return err(`锚点 ${rawNear} 还没有座位(read_board 里看不到就锚不上)。`);
|
|
90
|
+
}
|
|
91
|
+
zone = layerOf(near, anchorEntry, known);
|
|
92
|
+
const aSize = estimateSize(near, anchorEntry);
|
|
93
|
+
pos = { x: Math.round(anchorEntry.x + aSize.w + 24), y: Math.round(anchorEntry.y) };
|
|
94
|
+
// 撞卡往下让(同 arrange 的避让)
|
|
95
|
+
const obstacles = Object.entries(board.objects || {})
|
|
96
|
+
.filter(([id, e]) => Number.isFinite(e?.x) && layerOf(id, e, known) === zone)
|
|
97
|
+
.map(([id, e]) => ({ ...estimateSize(id, e), x: e.x, y: e.y }));
|
|
98
|
+
for (let g = 0; g < 40; g += 1) {
|
|
99
|
+
const blocker = obstacles.find(o =>
|
|
100
|
+
!(pos.x + box.w <= o.x || o.x + o.w <= pos.x || pos.y + box.h <= o.y || o.y + o.h <= pos.y));
|
|
101
|
+
if (!blocker) break;
|
|
102
|
+
pos.y = blocker.y + blocker.h + 16;
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
// 桌面根层内容最低边下面(跟新物件入座同一条起排线精神)
|
|
106
|
+
let bottom = 0;
|
|
107
|
+
for (const [id, e] of Object.entries(board.objects || {})) {
|
|
108
|
+
if (!Number.isFinite(e?.y) || layerOf(id, e, known) !== '') continue;
|
|
109
|
+
bottom = Math.max(bottom, e.y + estimateSize(id, e).h);
|
|
110
|
+
}
|
|
111
|
+
for (const zz of Object.values(board.zones || {})) {
|
|
112
|
+
if (Number.isFinite(zz?.y)) bottom = Math.max(bottom, zz.y + 150);
|
|
113
|
+
}
|
|
114
|
+
pos = { x: 10, y: Math.round(bottom) + 16 };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const textId = nextTextId();
|
|
118
|
+
const patch = {
|
|
119
|
+
objects: {
|
|
120
|
+
[textId]: {
|
|
121
|
+
x: pos.x, y: pos.y, z: 1, w: box.w, h: box.h,
|
|
122
|
+
kind: 'text',
|
|
123
|
+
data: { t, font: TEXT_FONTS.includes(font) ? font : 'pen', size: sizeKey, color: 'ink' },
|
|
124
|
+
zone, by: 'agent',
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
if (relation) {
|
|
129
|
+
const to = normalizeCanvasId(relation.to);
|
|
130
|
+
if (to && to !== textId) {
|
|
131
|
+
patch.bindings = {
|
|
132
|
+
[nextBindingId()]: {
|
|
133
|
+
type: relation.type, from: textId, to, by: 'agent',
|
|
134
|
+
...(relation.label ? { label: relation.label } : {}),
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const saved = await patchBoard(projectId, patch);
|
|
141
|
+
if (!saved.objects?.[textId]) return err('便签被 board 拒了(内容或字段不合法)。');
|
|
142
|
+
turnStamp.count += 1;
|
|
143
|
+
try {
|
|
144
|
+
ctx?.emit?.({ type: 'board.updated', sessionId: null, summary: '写了一条画布便签' });
|
|
145
|
+
} catch { /* fail-soft */ }
|
|
146
|
+
return {
|
|
147
|
+
content: [{
|
|
148
|
+
type: 'text',
|
|
149
|
+
text: `Note on board at (${pos.x},${pos.y})${relation ? ' with a line' : ''} (id: ${textId}).`
|
|
150
|
+
+ ` ${MAX_PER_TURN - turnStamp.count} left this turn.`,
|
|
151
|
+
}],
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
}
|
|
@@ -1,168 +1,168 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mcp/tools/crystallize-skill.js — crystallize_skill MCP tool
|
|
3
|
-
*
|
|
4
|
-
* 把一次真实探索的**结论**固化成用户自己的 skill,并把作品收进个人橱窗。
|
|
5
|
-
*
|
|
6
|
-
* 为什么不是"存模板":模板是把成品存下来,换个主题就崩——真正能复用的是判断依据
|
|
7
|
-
* (为什么这个字号阶梯、为什么这个场合压住动效、哪些默认做法在这个气质里必须反掉)。
|
|
8
|
-
* 所以这个工具收的是方法论,不是 HTML。产物写进用户级 plugin 根
|
|
9
|
-
* (~/.nodesign/plugins/<userId>/<name>/),下次开新会话在**这个用户的所有项目**里
|
|
10
|
-
* 都能用(plugin 发现是 startup-time,当前会话不生效)。
|
|
11
|
-
*
|
|
12
|
-
* 跟 record_decision 的分工:决策贴是**这一件作品**的过程档案,随任务走;
|
|
13
|
-
* 这里是跨项目复用的方法论,随用户走。一个是日志,一个是沉淀。
|
|
14
|
-
*
|
|
15
|
-
* 归属:写到**项目 owner** 的目录,不是"当前请求者"——同一个项目谁跑都该产出同一
|
|
16
|
-
* 套资产。owner 查不到就报错,不往共享根写(那是刚修掉的跨用户污染路径)。
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { tool } from '@anthropic-ai/claude-agent-sdk';
|
|
20
|
-
import { z } from 'zod';
|
|
21
|
-
import { getProject } from '../../../projects/store.js';
|
|
22
|
-
import { getUserPluginsRoot } from '../../agent/plugin-loader.js';
|
|
23
|
-
import { installPluginToRoot } from '../../../lib/plugin-install.js';
|
|
24
|
-
import { upsertEntry } from '../../../lib/showcase-store.js';
|
|
25
|
-
import { getActiveArtifact } from '../../../lib/artifact-target.js';
|
|
26
|
-
import { Events } from '../../agent/events.js';
|
|
27
|
-
|
|
28
|
-
const NAME_RE = /^[a-z0-9][a-z0-9-]{2,39}$/;
|
|
29
|
-
|
|
30
|
-
function composeSkillMd({ name, title, description, body }) {
|
|
31
|
-
return [
|
|
32
|
-
'---',
|
|
33
|
-
`name: ${name}`,
|
|
34
|
-
`description: ${description.replace(/\n+/g, ' ').trim()}`,
|
|
35
|
-
'version: 0.1.0',
|
|
36
|
-
'---',
|
|
37
|
-
'',
|
|
38
|
-
`# ${title.trim()}`,
|
|
39
|
-
'',
|
|
40
|
-
body.trim(),
|
|
41
|
-
'',
|
|
42
|
-
].join('\n');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* @param {object} deps
|
|
47
|
-
* @param {string} [deps.projectId]
|
|
48
|
-
* @param {string} [deps.sessionId]
|
|
49
|
-
* @param {import('../../agent/context.js').AgentContext} [deps.ctx]
|
|
50
|
-
*/
|
|
51
|
-
export function makeCrystallizeSkillTool({ projectId, sessionId, ctx }) {
|
|
52
|
-
return tool(
|
|
53
|
-
'crystallize_skill',
|
|
54
|
-
`Distill a style/approach the user and you worked out together into a REUSABLE
|
|
55
|
-
SKILL owned by this user, and put the finished work in their personal showcase.
|
|
56
|
-
|
|
57
|
-
Call this ONLY when the user asks to keep a style, or explicitly confirms they
|
|
58
|
-
want this approach reusable. Never call it unprompted — a skill the user did not
|
|
59
|
-
ask for is clutter in every future session.
|
|
60
|
-
|
|
61
|
-
What belongs in \`body\` is the METHODOLOGY, not the artifact:
|
|
62
|
-
- The reasoning behind the choices (why this type scale, why this palette works
|
|
63
|
-
for this kind of room, why motion was held back here)
|
|
64
|
-
- What the user rejected along the way and why — the negative space is the most
|
|
65
|
-
reusable part
|
|
66
|
-
- The anti-default list for this style: what the obvious move would have been,
|
|
67
|
-
and why it was wrong here
|
|
68
|
-
- Where this style BREAKS DOWN — occasions it does not suit. A skill that cannot
|
|
69
|
-
state its own boundary is a template wearing a methodology costume, and it will
|
|
70
|
-
produce bad work the first time it is applied off-target.
|
|
71
|
-
|
|
72
|
-
Do NOT paste HTML/CSS of the finished piece. Concrete values (a specific hex, a
|
|
73
|
-
specific scale) belong in only as illustrations of the reasoning.
|
|
74
|
-
|
|
75
|
-
The skill lands in the user's personal plugin dir and becomes available in NEW
|
|
76
|
-
sessions across all their projects (plugin discovery happens at session start,
|
|
77
|
-
so it does not apply to the current session).`,
|
|
78
|
-
{
|
|
79
|
-
name: z.string()
|
|
80
|
-
.describe('Skill id, kebab-case, 3-40 chars (e.g. "quiet-editorial-deck"). Becomes the plugin/skill name.'),
|
|
81
|
-
title: z.string().min(2).max(80)
|
|
82
|
-
.describe('Human-readable name of the style (e.g. "安静的编辑气质 · 长文型 deck")'),
|
|
83
|
-
description: z.string().min(20).max(600)
|
|
84
|
-
.describe('Frontmatter description — the routing signal. MUST say when to use it AND when not to.'),
|
|
85
|
-
body: z.string().min(200)
|
|
86
|
-
.describe('The methodology in markdown: reasoning, rejected alternatives, anti-default list, and where the style breaks down.'),
|
|
87
|
-
showcaseTitle: z.string().max(80).optional()
|
|
88
|
-
.describe('Title for the showcase card. Defaults to the style title.'),
|
|
89
|
-
showcaseNote: z.string().max(400).optional()
|
|
90
|
-
.describe('One line for the showcase card: what occasion this piece was for.'),
|
|
91
|
-
artifactPath: z.string().optional()
|
|
92
|
-
.describe('Work to show on the card, relative to workspace (e.g. "canvas.html" or "稿件/主稿.html"). Defaults to the active artifact.'),
|
|
93
|
-
overwrite: z.boolean().optional()
|
|
94
|
-
.describe('Replace an existing skill of the same name. Ask the user before setting this.'),
|
|
95
|
-
},
|
|
96
|
-
async ({ name, title, description, body, showcaseTitle, showcaseNote, artifactPath, overwrite }) => {
|
|
97
|
-
const fail = (text) => ({ content: [{ type: 'text', text }], isError: true });
|
|
98
|
-
try {
|
|
99
|
-
if (!NAME_RE.test(String(name || ''))) {
|
|
100
|
-
return fail(`Invalid skill name "${name}" — use kebab-case, 3-40 chars, e.g. "quiet-editorial-deck".`);
|
|
101
|
-
}
|
|
102
|
-
if (!projectId) return fail('No project bound; cannot resolve who owns this skill.');
|
|
103
|
-
const ownerId = getProject(projectId)?.ownerId || null;
|
|
104
|
-
const root = getUserPluginsRoot(ownerId);
|
|
105
|
-
if (!root) {
|
|
106
|
-
return fail('This project has no owner on record, so there is no personal skill library to write to.');
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const md = composeSkillMd({ name, title, description, body });
|
|
110
|
-
const result = await installPluginToRoot(Buffer.from(md, 'utf8'), root, { force: !!overwrite });
|
|
111
|
-
if (result.status === 409) {
|
|
112
|
-
return fail(`A skill named "${name}" already exists in this user's library `
|
|
113
|
-
+ `(${result.body.existing?.description || 'no description'}). `
|
|
114
|
-
+ 'Ask the user whether to replace it, then call again with overwrite: true.');
|
|
115
|
-
}
|
|
116
|
-
if (result.status >= 400) {
|
|
117
|
-
const errs = (result.body.errors || []).join('; ') || result.body.error;
|
|
118
|
-
return fail(`Skill rejected by validator: ${errs}`);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// 橱窗条目:作品 + 它沉淀出来的 skill
|
|
122
|
-
// 橱窗条目指向的是**产物的相对路径**。扁平化前还要顺带记一个 taskId
|
|
123
|
-
// (产物住在哪个任务文件夹里),任务层没了之后这个字段永远是 null ——
|
|
124
|
-
// 留着列是因为 showcase 表里有存量数据,读的时候还认它。
|
|
125
|
-
const rel = String(artifactPath || getActiveArtifact(sessionId)?.path || '').replace(/\\/g, '/');
|
|
126
|
-
const artifactRel = rel || null;
|
|
127
|
-
let entry = null;
|
|
128
|
-
try {
|
|
129
|
-
entry = upsertEntry({
|
|
130
|
-
userId: ownerId,
|
|
131
|
-
projectId,
|
|
132
|
-
taskId: null,
|
|
133
|
-
artifactRel,
|
|
134
|
-
skillName: name,
|
|
135
|
-
title: (showcaseTitle || title).trim(),
|
|
136
|
-
note: showcaseNote?.trim() || null,
|
|
137
|
-
});
|
|
138
|
-
} catch (err) {
|
|
139
|
-
console.warn('[crystallize_skill] showcase entry failed:', err.message);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
try {
|
|
143
|
-
ctx?.emit?.({
|
|
144
|
-
type: 'run.skill_crystallized',
|
|
145
|
-
skillName: name,
|
|
146
|
-
title: title.trim(),
|
|
147
|
-
showcaseId: entry?.id || null,
|
|
148
|
-
});
|
|
149
|
-
} catch { /* emit fail-safe */ }
|
|
150
|
-
|
|
151
|
-
const warn = (result.body.warnings || []).length
|
|
152
|
-
? ` Warnings: ${result.body.warnings.join('; ')}.` : '';
|
|
153
|
-
return {
|
|
154
|
-
content: [{
|
|
155
|
-
type: 'text',
|
|
156
|
-
text: `Skill "${name}" saved to the user's personal library`
|
|
157
|
-
+ `${artifactRel ? ` and the work added to their showcase` : ''}.`
|
|
158
|
-
+ ` It becomes available in NEW sessions (not this one).${warn}`
|
|
159
|
-
+ ` Tell the user plainly what you captured and what boundary you wrote,`
|
|
160
|
-
+ ` so they can correct it while it is fresh.`,
|
|
161
|
-
}],
|
|
162
|
-
};
|
|
163
|
-
} catch (err) {
|
|
164
|
-
return fail(`crystallize_skill failed: ${err?.message || String(err)}`);
|
|
165
|
-
}
|
|
166
|
-
},
|
|
167
|
-
);
|
|
168
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* mcp/tools/crystallize-skill.js — crystallize_skill MCP tool
|
|
3
|
+
*
|
|
4
|
+
* 把一次真实探索的**结论**固化成用户自己的 skill,并把作品收进个人橱窗。
|
|
5
|
+
*
|
|
6
|
+
* 为什么不是"存模板":模板是把成品存下来,换个主题就崩——真正能复用的是判断依据
|
|
7
|
+
* (为什么这个字号阶梯、为什么这个场合压住动效、哪些默认做法在这个气质里必须反掉)。
|
|
8
|
+
* 所以这个工具收的是方法论,不是 HTML。产物写进用户级 plugin 根
|
|
9
|
+
* (~/.nodesign/plugins/<userId>/<name>/),下次开新会话在**这个用户的所有项目**里
|
|
10
|
+
* 都能用(plugin 发现是 startup-time,当前会话不生效)。
|
|
11
|
+
*
|
|
12
|
+
* 跟 record_decision 的分工:决策贴是**这一件作品**的过程档案,随任务走;
|
|
13
|
+
* 这里是跨项目复用的方法论,随用户走。一个是日志,一个是沉淀。
|
|
14
|
+
*
|
|
15
|
+
* 归属:写到**项目 owner** 的目录,不是"当前请求者"——同一个项目谁跑都该产出同一
|
|
16
|
+
* 套资产。owner 查不到就报错,不往共享根写(那是刚修掉的跨用户污染路径)。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { tool } from '@anthropic-ai/claude-agent-sdk';
|
|
20
|
+
import { z } from 'zod';
|
|
21
|
+
import { getProject } from '../../../projects/store.js';
|
|
22
|
+
import { getUserPluginsRoot } from '../../agent/plugin-loader.js';
|
|
23
|
+
import { installPluginToRoot } from '../../../lib/plugin-install.js';
|
|
24
|
+
import { upsertEntry } from '../../../lib/showcase-store.js';
|
|
25
|
+
import { getActiveArtifact } from '../../../lib/artifact-target.js';
|
|
26
|
+
import { Events } from '../../agent/events.js';
|
|
27
|
+
|
|
28
|
+
const NAME_RE = /^[a-z0-9][a-z0-9-]{2,39}$/;
|
|
29
|
+
|
|
30
|
+
function composeSkillMd({ name, title, description, body }) {
|
|
31
|
+
return [
|
|
32
|
+
'---',
|
|
33
|
+
`name: ${name}`,
|
|
34
|
+
`description: ${description.replace(/\n+/g, ' ').trim()}`,
|
|
35
|
+
'version: 0.1.0',
|
|
36
|
+
'---',
|
|
37
|
+
'',
|
|
38
|
+
`# ${title.trim()}`,
|
|
39
|
+
'',
|
|
40
|
+
body.trim(),
|
|
41
|
+
'',
|
|
42
|
+
].join('\n');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {object} deps
|
|
47
|
+
* @param {string} [deps.projectId]
|
|
48
|
+
* @param {string} [deps.sessionId]
|
|
49
|
+
* @param {import('../../agent/context.js').AgentContext} [deps.ctx]
|
|
50
|
+
*/
|
|
51
|
+
export function makeCrystallizeSkillTool({ projectId, sessionId, ctx }) {
|
|
52
|
+
return tool(
|
|
53
|
+
'crystallize_skill',
|
|
54
|
+
`Distill a style/approach the user and you worked out together into a REUSABLE
|
|
55
|
+
SKILL owned by this user, and put the finished work in their personal showcase.
|
|
56
|
+
|
|
57
|
+
Call this ONLY when the user asks to keep a style, or explicitly confirms they
|
|
58
|
+
want this approach reusable. Never call it unprompted — a skill the user did not
|
|
59
|
+
ask for is clutter in every future session.
|
|
60
|
+
|
|
61
|
+
What belongs in \`body\` is the METHODOLOGY, not the artifact:
|
|
62
|
+
- The reasoning behind the choices (why this type scale, why this palette works
|
|
63
|
+
for this kind of room, why motion was held back here)
|
|
64
|
+
- What the user rejected along the way and why — the negative space is the most
|
|
65
|
+
reusable part
|
|
66
|
+
- The anti-default list for this style: what the obvious move would have been,
|
|
67
|
+
and why it was wrong here
|
|
68
|
+
- Where this style BREAKS DOWN — occasions it does not suit. A skill that cannot
|
|
69
|
+
state its own boundary is a template wearing a methodology costume, and it will
|
|
70
|
+
produce bad work the first time it is applied off-target.
|
|
71
|
+
|
|
72
|
+
Do NOT paste HTML/CSS of the finished piece. Concrete values (a specific hex, a
|
|
73
|
+
specific scale) belong in only as illustrations of the reasoning.
|
|
74
|
+
|
|
75
|
+
The skill lands in the user's personal plugin dir and becomes available in NEW
|
|
76
|
+
sessions across all their projects (plugin discovery happens at session start,
|
|
77
|
+
so it does not apply to the current session).`,
|
|
78
|
+
{
|
|
79
|
+
name: z.string()
|
|
80
|
+
.describe('Skill id, kebab-case, 3-40 chars (e.g. "quiet-editorial-deck"). Becomes the plugin/skill name.'),
|
|
81
|
+
title: z.string().min(2).max(80)
|
|
82
|
+
.describe('Human-readable name of the style (e.g. "安静的编辑气质 · 长文型 deck")'),
|
|
83
|
+
description: z.string().min(20).max(600)
|
|
84
|
+
.describe('Frontmatter description — the routing signal. MUST say when to use it AND when not to.'),
|
|
85
|
+
body: z.string().min(200)
|
|
86
|
+
.describe('The methodology in markdown: reasoning, rejected alternatives, anti-default list, and where the style breaks down.'),
|
|
87
|
+
showcaseTitle: z.string().max(80).optional()
|
|
88
|
+
.describe('Title for the showcase card. Defaults to the style title.'),
|
|
89
|
+
showcaseNote: z.string().max(400).optional()
|
|
90
|
+
.describe('One line for the showcase card: what occasion this piece was for.'),
|
|
91
|
+
artifactPath: z.string().optional()
|
|
92
|
+
.describe('Work to show on the card, relative to workspace (e.g. "canvas.html" or "稿件/主稿.html"). Defaults to the active artifact.'),
|
|
93
|
+
overwrite: z.boolean().optional()
|
|
94
|
+
.describe('Replace an existing skill of the same name. Ask the user before setting this.'),
|
|
95
|
+
},
|
|
96
|
+
async ({ name, title, description, body, showcaseTitle, showcaseNote, artifactPath, overwrite }) => {
|
|
97
|
+
const fail = (text) => ({ content: [{ type: 'text', text }], isError: true });
|
|
98
|
+
try {
|
|
99
|
+
if (!NAME_RE.test(String(name || ''))) {
|
|
100
|
+
return fail(`Invalid skill name "${name}" — use kebab-case, 3-40 chars, e.g. "quiet-editorial-deck".`);
|
|
101
|
+
}
|
|
102
|
+
if (!projectId) return fail('No project bound; cannot resolve who owns this skill.');
|
|
103
|
+
const ownerId = getProject(projectId)?.ownerId || null;
|
|
104
|
+
const root = getUserPluginsRoot(ownerId);
|
|
105
|
+
if (!root) {
|
|
106
|
+
return fail('This project has no owner on record, so there is no personal skill library to write to.');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const md = composeSkillMd({ name, title, description, body });
|
|
110
|
+
const result = await installPluginToRoot(Buffer.from(md, 'utf8'), root, { force: !!overwrite });
|
|
111
|
+
if (result.status === 409) {
|
|
112
|
+
return fail(`A skill named "${name}" already exists in this user's library `
|
|
113
|
+
+ `(${result.body.existing?.description || 'no description'}). `
|
|
114
|
+
+ 'Ask the user whether to replace it, then call again with overwrite: true.');
|
|
115
|
+
}
|
|
116
|
+
if (result.status >= 400) {
|
|
117
|
+
const errs = (result.body.errors || []).join('; ') || result.body.error;
|
|
118
|
+
return fail(`Skill rejected by validator: ${errs}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 橱窗条目:作品 + 它沉淀出来的 skill
|
|
122
|
+
// 橱窗条目指向的是**产物的相对路径**。扁平化前还要顺带记一个 taskId
|
|
123
|
+
// (产物住在哪个任务文件夹里),任务层没了之后这个字段永远是 null ——
|
|
124
|
+
// 留着列是因为 showcase 表里有存量数据,读的时候还认它。
|
|
125
|
+
const rel = String(artifactPath || getActiveArtifact(sessionId)?.path || '').replace(/\\/g, '/');
|
|
126
|
+
const artifactRel = rel || null;
|
|
127
|
+
let entry = null;
|
|
128
|
+
try {
|
|
129
|
+
entry = upsertEntry({
|
|
130
|
+
userId: ownerId,
|
|
131
|
+
projectId,
|
|
132
|
+
taskId: null,
|
|
133
|
+
artifactRel,
|
|
134
|
+
skillName: name,
|
|
135
|
+
title: (showcaseTitle || title).trim(),
|
|
136
|
+
note: showcaseNote?.trim() || null,
|
|
137
|
+
});
|
|
138
|
+
} catch (err) {
|
|
139
|
+
console.warn('[crystallize_skill] showcase entry failed:', err.message);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
ctx?.emit?.({
|
|
144
|
+
type: 'run.skill_crystallized',
|
|
145
|
+
skillName: name,
|
|
146
|
+
title: title.trim(),
|
|
147
|
+
showcaseId: entry?.id || null,
|
|
148
|
+
});
|
|
149
|
+
} catch { /* emit fail-safe */ }
|
|
150
|
+
|
|
151
|
+
const warn = (result.body.warnings || []).length
|
|
152
|
+
? ` Warnings: ${result.body.warnings.join('; ')}.` : '';
|
|
153
|
+
return {
|
|
154
|
+
content: [{
|
|
155
|
+
type: 'text',
|
|
156
|
+
text: `Skill "${name}" saved to the user's personal library`
|
|
157
|
+
+ `${artifactRel ? ` and the work added to their showcase` : ''}.`
|
|
158
|
+
+ ` It becomes available in NEW sessions (not this one).${warn}`
|
|
159
|
+
+ ` Tell the user plainly what you captured and what boundary you wrote,`
|
|
160
|
+
+ ` so they can correct it while it is fresh.`,
|
|
161
|
+
}],
|
|
162
|
+
};
|
|
163
|
+
} catch (err) {
|
|
164
|
+
return fail(`crystallize_skill failed: ${err?.message || String(err)}`);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
}
|