@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,31 +1,31 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 工具 prompt lazy 注入文件加载(首调即缓存)。仿 agents/index.js loadPrompt 模式。
|
|
3
|
-
*
|
|
4
|
-
* 用途:cookbook / tweaks-syntax / vision-checker-dispatch 这些 reference 文档
|
|
5
|
-
* 不放系统 prompt 恒驻(每 turn 拖累),改由 PreToolUse hook 在 agent 首次调对应工具时
|
|
6
|
-
* 通过 additionalContext 注入。文件存 prompts/tools/*.md,模块加载时一次性读完缓存到 map。
|
|
7
|
-
*
|
|
8
|
-
* fail-soft:缺失 / 读失败返回 stub 字符串,hook 注入也不至于崩;console.warn 让部署
|
|
9
|
-
* 日志能立刻发现。
|
|
10
|
-
*/
|
|
11
|
-
import fsSync from 'node:fs';
|
|
12
|
-
import path from 'node:path';
|
|
13
|
-
import { fileURLToPath } from 'node:url';
|
|
14
|
-
|
|
15
|
-
// prompts/ 住在 agent/ 层(本模块在 agent/hooks/ 下一层,要往上跳一级)
|
|
16
|
-
const PROMPTS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'prompts', 'tools');
|
|
17
|
-
|
|
18
|
-
const TOOL_PROMPT_CACHE = {};
|
|
19
|
-
export function loadToolPrompt(name) {
|
|
20
|
-
if (TOOL_PROMPT_CACHE[name] !== undefined) return TOOL_PROMPT_CACHE[name];
|
|
21
|
-
const file = path.join(PROMPTS_DIR, `${name}.md`);
|
|
22
|
-
try {
|
|
23
|
-
TOOL_PROMPT_CACHE[name] = fsSync.readFileSync(file, 'utf8');
|
|
24
|
-
} catch (err) {
|
|
25
|
-
console.warn(`[hooks] failed to load tool prompt ${name}.md (${err.message}); using stub`);
|
|
26
|
-
TOOL_PROMPT_CACHE[name] =
|
|
27
|
-
`(tool prompt ${name}.md not found at ${file}. PreToolUse hook will skip lazy injection — `
|
|
28
|
-
+ `agent fallbacks to whatever guidance lives in SKILL.md core.)`;
|
|
29
|
-
}
|
|
30
|
-
return TOOL_PROMPT_CACHE[name];
|
|
31
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 工具 prompt lazy 注入文件加载(首调即缓存)。仿 agents/index.js loadPrompt 模式。
|
|
3
|
+
*
|
|
4
|
+
* 用途:cookbook / tweaks-syntax / vision-checker-dispatch 这些 reference 文档
|
|
5
|
+
* 不放系统 prompt 恒驻(每 turn 拖累),改由 PreToolUse hook 在 agent 首次调对应工具时
|
|
6
|
+
* 通过 additionalContext 注入。文件存 prompts/tools/*.md,模块加载时一次性读完缓存到 map。
|
|
7
|
+
*
|
|
8
|
+
* fail-soft:缺失 / 读失败返回 stub 字符串,hook 注入也不至于崩;console.warn 让部署
|
|
9
|
+
* 日志能立刻发现。
|
|
10
|
+
*/
|
|
11
|
+
import fsSync from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
|
|
15
|
+
// prompts/ 住在 agent/ 层(本模块在 agent/hooks/ 下一层,要往上跳一级)
|
|
16
|
+
const PROMPTS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'prompts', 'tools');
|
|
17
|
+
|
|
18
|
+
const TOOL_PROMPT_CACHE = {};
|
|
19
|
+
export function loadToolPrompt(name) {
|
|
20
|
+
if (TOOL_PROMPT_CACHE[name] !== undefined) return TOOL_PROMPT_CACHE[name];
|
|
21
|
+
const file = path.join(PROMPTS_DIR, `${name}.md`);
|
|
22
|
+
try {
|
|
23
|
+
TOOL_PROMPT_CACHE[name] = fsSync.readFileSync(file, 'utf8');
|
|
24
|
+
} catch (err) {
|
|
25
|
+
console.warn(`[hooks] failed to load tool prompt ${name}.md (${err.message}); using stub`);
|
|
26
|
+
TOOL_PROMPT_CACHE[name] =
|
|
27
|
+
`(tool prompt ${name}.md not found at ${file}. PreToolUse hook will skip lazy injection — `
|
|
28
|
+
+ `agent fallbacks to whatever guidance lives in SKILL.md core.)`;
|
|
29
|
+
}
|
|
30
|
+
return TOOL_PROMPT_CACHE[name];
|
|
31
|
+
}
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* hooks/turn-state-memory.js — 每轮状态注入的"上一轮记忆"(2026-08-21)
|
|
3
|
-
*
|
|
4
|
-
* UserPromptSubmit 每轮把工作区状态(素材 / 产物 / 便利贴 / 关系线 / 决策 / tweaks)
|
|
5
|
-
* 注给模型。以前每轮全量,30 轮下来上下文里是十几份几乎一样的块(实测 540~1300
|
|
6
|
-
* token/轮)。现在**首轮全量、之后只报变化**:每节算一个指纹存在这里,下一轮对照。
|
|
7
|
-
*
|
|
8
|
-
* 键按 sessionId(一次对话一份记忆)。两种情况要清掉让下一轮重新全量:
|
|
9
|
-
* - 上下文压缩之后(PostCompact):旧的那份已经被摘要吞了,"同上轮"没有所指
|
|
10
|
-
* - 进程重启:Map 本来就空
|
|
11
|
-
* 上限 300 个会话,LRU 淘汰 —— 会话结束不另外清(多数会话没几轮,留着也就几百字节)。
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
const MAX_SESSIONS = 300;
|
|
15
|
-
/** sessionId → { sections: Map<key, {hash:string, items:string[]|null}>, turns: number } */
|
|
16
|
-
const memory = new Map();
|
|
17
|
-
|
|
18
|
-
/** djb2:够用的短指纹,不引 crypto */
|
|
19
|
-
export function fingerprint(text) {
|
|
20
|
-
let h = 5381;
|
|
21
|
-
const s = String(text);
|
|
22
|
-
for (let i = 0; i < s.length; i += 1) h = ((h << 5) + h + s.charCodeAt(i)) | 0;
|
|
23
|
-
return (h >>> 0).toString(36);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function getTurnMemory(sessionId) {
|
|
27
|
-
if (!sessionId) return null;
|
|
28
|
-
const m = memory.get(sessionId);
|
|
29
|
-
if (m) { memory.delete(sessionId); memory.set(sessionId, m); } // LRU 触碰
|
|
30
|
-
return m || null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function setTurnMemory(sessionId, sections) {
|
|
34
|
-
if (!sessionId) return;
|
|
35
|
-
const prev = memory.get(sessionId);
|
|
36
|
-
memory.delete(sessionId);
|
|
37
|
-
memory.set(sessionId, { sections, turns: (prev?.turns || 0) + 1 });
|
|
38
|
-
while (memory.size > MAX_SESSIONS) memory.delete(memory.keys().next().value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** 压缩后 / 想强制下一轮全量时调 */
|
|
42
|
-
export function resetTurnMemory(sessionId) {
|
|
43
|
-
if (sessionId) memory.delete(sessionId);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** 两个清单的差 → { added, removed }(给素材 / 便利贴这种按项报变化的节用) */
|
|
47
|
-
export function diffItems(prevItems, nextItems) {
|
|
48
|
-
const prev = new Set(prevItems || []);
|
|
49
|
-
const next = new Set(nextItems || []);
|
|
50
|
-
return {
|
|
51
|
-
added: [...next].filter(x => !prev.has(x)),
|
|
52
|
-
removed: [...prev].filter(x => !next.has(x)),
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export const _memory = memory; // 测试用
|
|
1
|
+
/**
|
|
2
|
+
* hooks/turn-state-memory.js — 每轮状态注入的"上一轮记忆"(2026-08-21)
|
|
3
|
+
*
|
|
4
|
+
* UserPromptSubmit 每轮把工作区状态(素材 / 产物 / 便利贴 / 关系线 / 决策 / tweaks)
|
|
5
|
+
* 注给模型。以前每轮全量,30 轮下来上下文里是十几份几乎一样的块(实测 540~1300
|
|
6
|
+
* token/轮)。现在**首轮全量、之后只报变化**:每节算一个指纹存在这里,下一轮对照。
|
|
7
|
+
*
|
|
8
|
+
* 键按 sessionId(一次对话一份记忆)。两种情况要清掉让下一轮重新全量:
|
|
9
|
+
* - 上下文压缩之后(PostCompact):旧的那份已经被摘要吞了,"同上轮"没有所指
|
|
10
|
+
* - 进程重启:Map 本来就空
|
|
11
|
+
* 上限 300 个会话,LRU 淘汰 —— 会话结束不另外清(多数会话没几轮,留着也就几百字节)。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const MAX_SESSIONS = 300;
|
|
15
|
+
/** sessionId → { sections: Map<key, {hash:string, items:string[]|null}>, turns: number } */
|
|
16
|
+
const memory = new Map();
|
|
17
|
+
|
|
18
|
+
/** djb2:够用的短指纹,不引 crypto */
|
|
19
|
+
export function fingerprint(text) {
|
|
20
|
+
let h = 5381;
|
|
21
|
+
const s = String(text);
|
|
22
|
+
for (let i = 0; i < s.length; i += 1) h = ((h << 5) + h + s.charCodeAt(i)) | 0;
|
|
23
|
+
return (h >>> 0).toString(36);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getTurnMemory(sessionId) {
|
|
27
|
+
if (!sessionId) return null;
|
|
28
|
+
const m = memory.get(sessionId);
|
|
29
|
+
if (m) { memory.delete(sessionId); memory.set(sessionId, m); } // LRU 触碰
|
|
30
|
+
return m || null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function setTurnMemory(sessionId, sections) {
|
|
34
|
+
if (!sessionId) return;
|
|
35
|
+
const prev = memory.get(sessionId);
|
|
36
|
+
memory.delete(sessionId);
|
|
37
|
+
memory.set(sessionId, { sections, turns: (prev?.turns || 0) + 1 });
|
|
38
|
+
while (memory.size > MAX_SESSIONS) memory.delete(memory.keys().next().value);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 压缩后 / 想强制下一轮全量时调 */
|
|
42
|
+
export function resetTurnMemory(sessionId) {
|
|
43
|
+
if (sessionId) memory.delete(sessionId);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 两个清单的差 → { added, removed }(给素材 / 便利贴这种按项报变化的节用) */
|
|
47
|
+
export function diffItems(prevItems, nextItems) {
|
|
48
|
+
const prev = new Set(prevItems || []);
|
|
49
|
+
const next = new Set(nextItems || []);
|
|
50
|
+
return {
|
|
51
|
+
added: [...next].filter(x => !prev.has(x)),
|
|
52
|
+
removed: [...prev].filter(x => !next.has(x)),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const _memory = memory; // 测试用
|
|
@@ -1,215 +1,215 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* UserPromptSubmit handler — 每次用户输入前注入工作区**状态**(2026-08-21 重做)
|
|
3
|
-
*
|
|
4
|
-
* 注的是状态不是指令:cwd、素材清单、旧决策、便利贴、画布关系线、产物清单、tweaks 开关。
|
|
5
|
-
* 怎么用这些东西(路径表、"先看有没有现成的素材"、工具怎么选)住 prelude,那是缓存的
|
|
6
|
-
* system prompt;每轮再说一遍是上下文里的 N 倍重复。
|
|
7
|
-
*
|
|
8
|
-
* ## 首轮全量,之后只报变化
|
|
9
|
-
*
|
|
10
|
-
* 以前每轮全量(实测 540~1300 token/轮),30 轮下来对话历史里是十几份几乎相同的块。
|
|
11
|
-
* 现在每节算指纹记在 turn-state-memory.js(按 sessionId):
|
|
12
|
-
* - 首轮(或压缩后 / 进程重启后):全量 + 结尾"请基于这些信息处理用户的请求"
|
|
13
|
-
* - 之后:变了的节全文(标「有变化」),素材/便利贴这种清单只报新增/移除;没变的节
|
|
14
|
-
* 只在末尾一行点名"未变:…"(让模型知道它们还在,但不重复内容);一节都没变就一句话
|
|
15
|
-
*
|
|
16
|
-
* ## 素材块从 turn-compose 搬过来(同日)
|
|
17
|
-
*
|
|
18
|
-
* 以前 assets 摘要是 turn-compose 拼进**用户消息**里的 <system> 块,跟这里的状态块是
|
|
19
|
-
* 两条线两个真相源;而且那块写着"assets/ 是 symlink 别用 Glob"—— 08-07 扁平化后早就是
|
|
20
|
-
* 真目录了。现在合成这一条线,symlink 那句删掉。
|
|
21
|
-
*
|
|
22
|
-
* input: UserPromptSubmitHookInput — output: { hookSpecificOutput: { additionalContext } }
|
|
23
|
-
*/
|
|
24
|
-
import fs from 'node:fs/promises';
|
|
25
|
-
import path from 'node:path';
|
|
26
|
-
import { readUiConfigFile } from '../../../projects/ui-config.js';
|
|
27
|
-
import { readAssetsSummary } from '../../../projects/assets-summary.js';
|
|
28
|
-
import { relationsDigest } from '../../../lib/board-relations.js';
|
|
29
|
-
import {
|
|
30
|
-
getActiveArtifact, listWorkspaceArtifacts, taskManifest, kindDef,
|
|
31
|
-
KIND_DECK, KIND_SITE, ENTRY_FILE,
|
|
32
|
-
} from '../../../lib/artifact-target.js';
|
|
33
|
-
import { getTurnMemory, setTurnMemory, fingerprint, diffItems } from './turn-state-memory.js';
|
|
34
|
-
|
|
35
|
-
/** UserPromptSubmit hook 读取 spec.json 时的最大字节数 */
|
|
36
|
-
const SPEC_JSON_MAX_BYTES = 200 * 1024;
|
|
37
|
-
/** spec.json.decisions 注入摘要时取最近 N 条 */
|
|
38
|
-
const SPEC_DECISIONS_TAIL = 5;
|
|
39
|
-
|
|
40
|
-
/** PDF/Office 文档的解法(系统自带工具;python 包没装且装不上 —— 08-19 上报实锤)。只在首次出现二进制文档时说一遍。 */
|
|
41
|
-
const BINARY_DOC_HINT = 'PDF / PPTX / DOCX / XLSX 直接 Read 拿不到结构化内容(二进制或 zip 包)。用系统自带的工具解'
|
|
42
|
-
+ '(pdfplumber/PyPDF2/python-docx/openpyxl 这些 python 包**没装且装不上**,别试):'
|
|
43
|
-
+ 'pdf 文本 `pdftotext -layout 文件.pdf -`;pdf 转页图 `pdftoppm -png -r 100 文件.pdf 页前缀` 再 Read 图片;pdf 嵌入图 `pdfimages -png`。'
|
|
44
|
-
+ 'docx/pptx/xlsx 用 `soffice --headless --convert-to txt|csv|pdf --outdir 目录 文件`(xlsx 转 csv;⚠️ soffice 吃不下中文文件名,先拷成 ASCII 名,并加 `-env:UserInstallation=file:///tmp/lo-任意名` 免得和渲染管线抢 profile);'
|
|
45
|
-
+ 'docx/pptx 里的嵌入图直接 `unzip -o 文件 "word/media/*"`(pptx 是 `ppt/media/*`)。'
|
|
46
|
-
+ '**提取出来的不只是文本,通常还包含嵌入图片** —— 提取完一定 Read 看图片,别只看 stdout 文本就以为信息齐了。';
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 采集各节。每节 { key, title, text, items? }:text 是全量文案;items 是可按项报变化的清单。
|
|
50
|
-
* 任一节采不到就不出现(跟以前一样:扫不动就不说,别拿错信息误导)。
|
|
51
|
-
*/
|
|
52
|
-
async function collectSections({ workspaceRoot, sessionId, projectId }) {
|
|
53
|
-
const sections = [];
|
|
54
|
-
|
|
55
|
-
// cwd:唯一真正动态的一行。路径表(./ notes/ assets/ .claude/agent-memory/ 各是什么)在 prelude
|
|
56
|
-
sections.push({ key: 'cwd', title: '工作区', text: `你的 cwd 是 ${workspaceRoot} —— 项目工作区,产物直接住这儿(路径表见 prelude「你跑在哪」)。` });
|
|
57
|
-
|
|
58
|
-
// 素材:顶层 assets/ + assets/references/**(逛站采回来的)
|
|
59
|
-
try {
|
|
60
|
-
const a = await readAssetsSummary(workspaceRoot);
|
|
61
|
-
if (a.count > 0) {
|
|
62
|
-
const all = Array.isArray(a.allPaths) ? a.allPaths : [];
|
|
63
|
-
const shown = Array.isArray(a.paths) ? a.paths : [];
|
|
64
|
-
const text = `${a.summary}\n完整路径(直接 Read;Glob/Grep 也能用):\n${shown.map(p => `- ${p}`).join('\n')}`
|
|
65
|
-
+ (a.hasBinaryDocs ? `\n${BINARY_DOC_HINT}` : '');
|
|
66
|
-
sections.push({ key: 'assets', title: '素材', text, items: all, hasBinaryDocs: a.hasBinaryDocs });
|
|
67
|
-
}
|
|
68
|
-
} catch { /* 素材读不到就沉默 */ }
|
|
69
|
-
|
|
70
|
-
// spec.json 遗产决策:取最近 N 条
|
|
71
|
-
try {
|
|
72
|
-
const specPath = path.join(workspaceRoot, 'spec.json');
|
|
73
|
-
const stat = await fs.stat(specPath);
|
|
74
|
-
if (stat.size <= SPEC_JSON_MAX_BYTES) {
|
|
75
|
-
const spec = JSON.parse(await fs.readFile(specPath, 'utf8'));
|
|
76
|
-
const decisions = Array.isArray(spec?.decisions) ? spec.decisions : [];
|
|
77
|
-
if (decisions.length > 0) {
|
|
78
|
-
const recent = decisions.slice(-SPEC_DECISIONS_TAIL);
|
|
79
|
-
const lines = recent.map((d, i) => {
|
|
80
|
-
const idx = decisions.length - recent.length + i + 1;
|
|
81
|
-
const title = (d?.title || '(无标题)').slice(0, 80);
|
|
82
|
-
const rationale = (d?.rationale || '').slice(0, 200);
|
|
83
|
-
return ` ${idx}. ${title}${rationale ? ` — ${rationale}` : ''}`;
|
|
84
|
-
}).join('\n');
|
|
85
|
-
sections.push({ key: 'decisions', title: '旧决策档案', text: `旧决策档案(spec.json 遗产,共 ${decisions.length} 条,最近 ${recent.length} 条;新决策一律走 record_decision → 任务便利贴):\n${lines}` });
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
} catch { /* spec.json 不存在 / 解析失败:noop */ }
|
|
89
|
-
|
|
90
|
-
// 便利贴:metadata-not-content,只列文件和首行标题
|
|
91
|
-
try {
|
|
92
|
-
const notesDir = path.join(workspaceRoot, 'notes');
|
|
93
|
-
const noteFiles = (await fs.readdir(notesDir)).filter(n => n.endsWith('.md') && !n.startsWith('.'));
|
|
94
|
-
const lines = []; const items = [];
|
|
95
|
-
for (const n of noteFiles.slice(0, 12)) {
|
|
96
|
-
let title = ''; let faces = 0;
|
|
97
|
-
try {
|
|
98
|
-
const raw = await fs.readFile(path.join(notesDir, n), 'utf8');
|
|
99
|
-
title = (raw.match(/^#\s+(.{1,60})/m)?.[1] || '').trim();
|
|
100
|
-
faces = raw.split(/\n---\n/).length;
|
|
101
|
-
} catch { /* 列出文件名就够 */ }
|
|
102
|
-
const meta = [title, faces > 1 ? `${faces} 面` : ''].filter(Boolean).join(' · ');
|
|
103
|
-
lines.push(` notes/${n}${meta ? `(${meta})` : ''}`);
|
|
104
|
-
items.push(`notes/${n}`);
|
|
105
|
-
}
|
|
106
|
-
if (lines.length) sections.push({ key: 'notes', title: '便利贴', text: `便利贴(和用户共享,他看得到也可能改过;细节 Read):\n${lines.join('\n')}`, items });
|
|
107
|
-
} catch { /* notes/ 不存在:noop */ }
|
|
108
|
-
|
|
109
|
-
// 画布关系线:用户画的排前面
|
|
110
|
-
try {
|
|
111
|
-
const digest = await relationsDigest(projectId, { limit: 12 });
|
|
112
|
-
if (digest) {
|
|
113
|
-
sections.push({ key: 'relations', title: '画布关系线', text: `画布关系线(用户和你手动画的连线,端点跟着改名走;语义看线上的词):\n${digest}\n 产出新东西后记得用 relate_on_board 把「改自/对照/接着/取材」画上去。` });
|
|
114
|
-
}
|
|
115
|
-
} catch { /* 板读不到就沉默 */ }
|
|
116
|
-
|
|
117
|
-
// 产物清单:按形态报(deck 报页数,站点报页面清单 + 产物根)
|
|
118
|
-
try {
|
|
119
|
-
const artifacts = await listWorkspaceArtifacts(workspaceRoot);
|
|
120
|
-
if (artifacts.length === 0) {
|
|
121
|
-
sections.push({ key: 'artifacts', title: '产物', text: `这个工作区还没有产物 —— 直接在工作区根上写:deck 写 ${ENTRY_FILE[KIND_DECK]},站点写 ${ENTRY_FILE[KIND_SITE]}。` });
|
|
122
|
-
} else {
|
|
123
|
-
const active = getActiveArtifact(sessionId)?.path || null;
|
|
124
|
-
const lines = [];
|
|
125
|
-
let manifest = null;
|
|
126
|
-
for (const a of artifacts.slice(0, 8)) {
|
|
127
|
-
let note = '';
|
|
128
|
-
try {
|
|
129
|
-
if (!manifest) manifest = await taskManifest(workspaceRoot);
|
|
130
|
-
const art = manifest?.artifacts?.find(x => x.entryRel === a.rel) || null;
|
|
131
|
-
note = art ? await kindDef(art.kind).describe(workspaceRoot, art) : '还判不出形态';
|
|
132
|
-
} catch { note = '读不到'; }
|
|
133
|
-
lines.push(` ${a.rel}(${note})${a.rel === active ? ' ← 画布工具默认打这份' : ''}`);
|
|
134
|
-
}
|
|
135
|
-
sections.push({ key: 'artifacts', title: '产物', text: `现有产物:\n${lines.join('\n')}` });
|
|
136
|
-
}
|
|
137
|
-
} catch { /* 扫不动就不说 */ }
|
|
138
|
-
|
|
139
|
-
// tweaks 开关:文件不存在 = 用户没碰过 = 沉默
|
|
140
|
-
try {
|
|
141
|
-
const cfg = await readUiConfigFile(workspaceRoot);
|
|
142
|
-
if (cfg) {
|
|
143
|
-
const on = cfg?.tweaks_mode_enabled !== false;
|
|
144
|
-
sections.push({ key: 'tweaks', title: 'Tweaks 开关', text: on
|
|
145
|
-
? '【Tweaks 模式:启用】用户偏好"可调产品"——deck 形态稳定后,建议调 expose_tweaks 暴露核心微调参数(颜色 / 字号 / 排版密度等)让用户拖滑杆即时改样式。'
|
|
146
|
-
: '【Tweaks 模式:禁用】用户在 toolbar 关闭了 Tweaks——这次跳过 expose_tweaks,按对话方式让用户提需求你来 Edit。已暴露的 controls 保留不动,不新增 / 不重 expose。' });
|
|
147
|
-
}
|
|
148
|
-
} catch { /* 读失败:默认行为,不注入 */ }
|
|
149
|
-
|
|
150
|
-
return sections;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* 把各节渲染成这一轮的注入文本(纯函数,可单测)。
|
|
155
|
-
* @param {Array} sections collectSections 的结果
|
|
156
|
-
* @param {Map|null} prev 上一轮记忆(null = 首轮)
|
|
157
|
-
* @returns {{ text: string|null, next: Map }}
|
|
158
|
-
*/
|
|
159
|
-
export function renderTurnState(sections, prev) {
|
|
160
|
-
const next = new Map();
|
|
161
|
-
for (const s of sections) next.set(s.key, { hash: fingerprint(s.text), items: s.items ? [...s.items] : null, hasBinaryDocs: !!s.hasBinaryDocs });
|
|
162
|
-
if (!sections.length) return { text: null, next };
|
|
163
|
-
|
|
164
|
-
if (!prev) {
|
|
165
|
-
const body = sections.map(s => s.text).join('\n\n');
|
|
166
|
-
return { text: `[NoDesign 工作台自动注入的当前状态]\n\n${body}\n\n请基于这些信息处理用户的请求。`, next };
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const changed = []; const unchanged = []; const gone = [];
|
|
170
|
-
for (const s of sections) {
|
|
171
|
-
const p = prev.get(s.key);
|
|
172
|
-
if (!p) { changed.push(`(新出现)${s.text}`); continue; }
|
|
173
|
-
if (p.hash === next.get(s.key).hash) { unchanged.push(s.title); continue; }
|
|
174
|
-
if (s.items && p.items) {
|
|
175
|
-
const d = diffItems(p.items, s.items);
|
|
176
|
-
const bits = [];
|
|
177
|
-
if (d.added.length) bits.push(`新增 ${d.added.length}:${d.added.slice(0, 8).join('、')}${d.added.length > 8 ? ' 等' : ''}`);
|
|
178
|
-
if (d.removed.length) bits.push(`移除 ${d.removed.length}:${d.removed.slice(0, 8).join('、')}${d.removed.length > 8 ? ' 等' : ''}`);
|
|
179
|
-
if (bits.length) {
|
|
180
|
-
let line = `${s.title}(有变化):${bits.join(';')}(现共 ${s.items.length} 件)`;
|
|
181
|
-
if (s.hasBinaryDocs && !p.hasBinaryDocs) line += `\n${BINARY_DOC_HINT}`;
|
|
182
|
-
changed.push(line);
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
changed.push(`(有变化)${s.text}`);
|
|
187
|
-
}
|
|
188
|
-
for (const [key] of prev) if (!next.has(key)) gone.push(key);
|
|
189
|
-
const lines = [];
|
|
190
|
-
if (changed.length) lines.push(...changed);
|
|
191
|
-
if (gone.length) lines.push(`(已不存在:${gone.join('、')})`);
|
|
192
|
-
if (!changed.length && !gone.length) {
|
|
193
|
-
return { text: `[工作台状态:与上轮相同(${unchanged.join('、')})]`, next };
|
|
194
|
-
}
|
|
195
|
-
lines.push(`未变:${unchanged.join('、') || '(无)'}`);
|
|
196
|
-
return { text: `[工作台状态 · 只报变化]\n\n${lines.join('\n\n')}`, next };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export function makeUserPromptSubmitHandler({ ctx: _ctx, workspaceRoot, sessionId, projectId }) {
|
|
200
|
-
return async (_input, _toolUseId, _options) => {
|
|
201
|
-
try {
|
|
202
|
-
if (!workspaceRoot) return {};
|
|
203
|
-
const sections = await collectSections({ workspaceRoot, sessionId, projectId });
|
|
204
|
-
const prev = getTurnMemory(sessionId)?.sections || null;
|
|
205
|
-
const { text, next } = renderTurnState(sections, prev);
|
|
206
|
-
setTurnMemory(sessionId, next);
|
|
207
|
-
if (!text) return {};
|
|
208
|
-
// 不 emit 业务事件 —— additionalContext 注入是私域提示,不需要前端展示
|
|
209
|
-
return { hookSpecificOutput: { hookEventName: 'UserPromptSubmit', additionalContext: text } };
|
|
210
|
-
} catch (err) {
|
|
211
|
-
console.warn('[hooks/UserPromptSubmit] handler threw:', err.message);
|
|
212
|
-
return {};
|
|
213
|
-
}
|
|
214
|
-
};
|
|
215
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* UserPromptSubmit handler — 每次用户输入前注入工作区**状态**(2026-08-21 重做)
|
|
3
|
+
*
|
|
4
|
+
* 注的是状态不是指令:cwd、素材清单、旧决策、便利贴、画布关系线、产物清单、tweaks 开关。
|
|
5
|
+
* 怎么用这些东西(路径表、"先看有没有现成的素材"、工具怎么选)住 prelude,那是缓存的
|
|
6
|
+
* system prompt;每轮再说一遍是上下文里的 N 倍重复。
|
|
7
|
+
*
|
|
8
|
+
* ## 首轮全量,之后只报变化
|
|
9
|
+
*
|
|
10
|
+
* 以前每轮全量(实测 540~1300 token/轮),30 轮下来对话历史里是十几份几乎相同的块。
|
|
11
|
+
* 现在每节算指纹记在 turn-state-memory.js(按 sessionId):
|
|
12
|
+
* - 首轮(或压缩后 / 进程重启后):全量 + 结尾"请基于这些信息处理用户的请求"
|
|
13
|
+
* - 之后:变了的节全文(标「有变化」),素材/便利贴这种清单只报新增/移除;没变的节
|
|
14
|
+
* 只在末尾一行点名"未变:…"(让模型知道它们还在,但不重复内容);一节都没变就一句话
|
|
15
|
+
*
|
|
16
|
+
* ## 素材块从 turn-compose 搬过来(同日)
|
|
17
|
+
*
|
|
18
|
+
* 以前 assets 摘要是 turn-compose 拼进**用户消息**里的 <system> 块,跟这里的状态块是
|
|
19
|
+
* 两条线两个真相源;而且那块写着"assets/ 是 symlink 别用 Glob"—— 08-07 扁平化后早就是
|
|
20
|
+
* 真目录了。现在合成这一条线,symlink 那句删掉。
|
|
21
|
+
*
|
|
22
|
+
* input: UserPromptSubmitHookInput — output: { hookSpecificOutput: { additionalContext } }
|
|
23
|
+
*/
|
|
24
|
+
import fs from 'node:fs/promises';
|
|
25
|
+
import path from 'node:path';
|
|
26
|
+
import { readUiConfigFile } from '../../../projects/ui-config.js';
|
|
27
|
+
import { readAssetsSummary } from '../../../projects/assets-summary.js';
|
|
28
|
+
import { relationsDigest } from '../../../lib/board-relations.js';
|
|
29
|
+
import {
|
|
30
|
+
getActiveArtifact, listWorkspaceArtifacts, taskManifest, kindDef,
|
|
31
|
+
KIND_DECK, KIND_SITE, ENTRY_FILE,
|
|
32
|
+
} from '../../../lib/artifact-target.js';
|
|
33
|
+
import { getTurnMemory, setTurnMemory, fingerprint, diffItems } from './turn-state-memory.js';
|
|
34
|
+
|
|
35
|
+
/** UserPromptSubmit hook 读取 spec.json 时的最大字节数 */
|
|
36
|
+
const SPEC_JSON_MAX_BYTES = 200 * 1024;
|
|
37
|
+
/** spec.json.decisions 注入摘要时取最近 N 条 */
|
|
38
|
+
const SPEC_DECISIONS_TAIL = 5;
|
|
39
|
+
|
|
40
|
+
/** PDF/Office 文档的解法(系统自带工具;python 包没装且装不上 —— 08-19 上报实锤)。只在首次出现二进制文档时说一遍。 */
|
|
41
|
+
const BINARY_DOC_HINT = 'PDF / PPTX / DOCX / XLSX 直接 Read 拿不到结构化内容(二进制或 zip 包)。用系统自带的工具解'
|
|
42
|
+
+ '(pdfplumber/PyPDF2/python-docx/openpyxl 这些 python 包**没装且装不上**,别试):'
|
|
43
|
+
+ 'pdf 文本 `pdftotext -layout 文件.pdf -`;pdf 转页图 `pdftoppm -png -r 100 文件.pdf 页前缀` 再 Read 图片;pdf 嵌入图 `pdfimages -png`。'
|
|
44
|
+
+ 'docx/pptx/xlsx 用 `soffice --headless --convert-to txt|csv|pdf --outdir 目录 文件`(xlsx 转 csv;⚠️ soffice 吃不下中文文件名,先拷成 ASCII 名,并加 `-env:UserInstallation=file:///tmp/lo-任意名` 免得和渲染管线抢 profile);'
|
|
45
|
+
+ 'docx/pptx 里的嵌入图直接 `unzip -o 文件 "word/media/*"`(pptx 是 `ppt/media/*`)。'
|
|
46
|
+
+ '**提取出来的不只是文本,通常还包含嵌入图片** —— 提取完一定 Read 看图片,别只看 stdout 文本就以为信息齐了。';
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 采集各节。每节 { key, title, text, items? }:text 是全量文案;items 是可按项报变化的清单。
|
|
50
|
+
* 任一节采不到就不出现(跟以前一样:扫不动就不说,别拿错信息误导)。
|
|
51
|
+
*/
|
|
52
|
+
async function collectSections({ workspaceRoot, sessionId, projectId }) {
|
|
53
|
+
const sections = [];
|
|
54
|
+
|
|
55
|
+
// cwd:唯一真正动态的一行。路径表(./ notes/ assets/ .claude/agent-memory/ 各是什么)在 prelude
|
|
56
|
+
sections.push({ key: 'cwd', title: '工作区', text: `你的 cwd 是 ${workspaceRoot} —— 项目工作区,产物直接住这儿(路径表见 prelude「你跑在哪」)。` });
|
|
57
|
+
|
|
58
|
+
// 素材:顶层 assets/ + assets/references/**(逛站采回来的)
|
|
59
|
+
try {
|
|
60
|
+
const a = await readAssetsSummary(workspaceRoot);
|
|
61
|
+
if (a.count > 0) {
|
|
62
|
+
const all = Array.isArray(a.allPaths) ? a.allPaths : [];
|
|
63
|
+
const shown = Array.isArray(a.paths) ? a.paths : [];
|
|
64
|
+
const text = `${a.summary}\n完整路径(直接 Read;Glob/Grep 也能用):\n${shown.map(p => `- ${p}`).join('\n')}`
|
|
65
|
+
+ (a.hasBinaryDocs ? `\n${BINARY_DOC_HINT}` : '');
|
|
66
|
+
sections.push({ key: 'assets', title: '素材', text, items: all, hasBinaryDocs: a.hasBinaryDocs });
|
|
67
|
+
}
|
|
68
|
+
} catch { /* 素材读不到就沉默 */ }
|
|
69
|
+
|
|
70
|
+
// spec.json 遗产决策:取最近 N 条
|
|
71
|
+
try {
|
|
72
|
+
const specPath = path.join(workspaceRoot, 'spec.json');
|
|
73
|
+
const stat = await fs.stat(specPath);
|
|
74
|
+
if (stat.size <= SPEC_JSON_MAX_BYTES) {
|
|
75
|
+
const spec = JSON.parse(await fs.readFile(specPath, 'utf8'));
|
|
76
|
+
const decisions = Array.isArray(spec?.decisions) ? spec.decisions : [];
|
|
77
|
+
if (decisions.length > 0) {
|
|
78
|
+
const recent = decisions.slice(-SPEC_DECISIONS_TAIL);
|
|
79
|
+
const lines = recent.map((d, i) => {
|
|
80
|
+
const idx = decisions.length - recent.length + i + 1;
|
|
81
|
+
const title = (d?.title || '(无标题)').slice(0, 80);
|
|
82
|
+
const rationale = (d?.rationale || '').slice(0, 200);
|
|
83
|
+
return ` ${idx}. ${title}${rationale ? ` — ${rationale}` : ''}`;
|
|
84
|
+
}).join('\n');
|
|
85
|
+
sections.push({ key: 'decisions', title: '旧决策档案', text: `旧决策档案(spec.json 遗产,共 ${decisions.length} 条,最近 ${recent.length} 条;新决策一律走 record_decision → 任务便利贴):\n${lines}` });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
} catch { /* spec.json 不存在 / 解析失败:noop */ }
|
|
89
|
+
|
|
90
|
+
// 便利贴:metadata-not-content,只列文件和首行标题
|
|
91
|
+
try {
|
|
92
|
+
const notesDir = path.join(workspaceRoot, 'notes');
|
|
93
|
+
const noteFiles = (await fs.readdir(notesDir)).filter(n => n.endsWith('.md') && !n.startsWith('.'));
|
|
94
|
+
const lines = []; const items = [];
|
|
95
|
+
for (const n of noteFiles.slice(0, 12)) {
|
|
96
|
+
let title = ''; let faces = 0;
|
|
97
|
+
try {
|
|
98
|
+
const raw = await fs.readFile(path.join(notesDir, n), 'utf8');
|
|
99
|
+
title = (raw.match(/^#\s+(.{1,60})/m)?.[1] || '').trim();
|
|
100
|
+
faces = raw.split(/\n---\n/).length;
|
|
101
|
+
} catch { /* 列出文件名就够 */ }
|
|
102
|
+
const meta = [title, faces > 1 ? `${faces} 面` : ''].filter(Boolean).join(' · ');
|
|
103
|
+
lines.push(` notes/${n}${meta ? `(${meta})` : ''}`);
|
|
104
|
+
items.push(`notes/${n}`);
|
|
105
|
+
}
|
|
106
|
+
if (lines.length) sections.push({ key: 'notes', title: '便利贴', text: `便利贴(和用户共享,他看得到也可能改过;细节 Read):\n${lines.join('\n')}`, items });
|
|
107
|
+
} catch { /* notes/ 不存在:noop */ }
|
|
108
|
+
|
|
109
|
+
// 画布关系线:用户画的排前面
|
|
110
|
+
try {
|
|
111
|
+
const digest = await relationsDigest(projectId, { limit: 12 });
|
|
112
|
+
if (digest) {
|
|
113
|
+
sections.push({ key: 'relations', title: '画布关系线', text: `画布关系线(用户和你手动画的连线,端点跟着改名走;语义看线上的词):\n${digest}\n 产出新东西后记得用 relate_on_board 把「改自/对照/接着/取材」画上去。` });
|
|
114
|
+
}
|
|
115
|
+
} catch { /* 板读不到就沉默 */ }
|
|
116
|
+
|
|
117
|
+
// 产物清单:按形态报(deck 报页数,站点报页面清单 + 产物根)
|
|
118
|
+
try {
|
|
119
|
+
const artifacts = await listWorkspaceArtifacts(workspaceRoot);
|
|
120
|
+
if (artifacts.length === 0) {
|
|
121
|
+
sections.push({ key: 'artifacts', title: '产物', text: `这个工作区还没有产物 —— 直接在工作区根上写:deck 写 ${ENTRY_FILE[KIND_DECK]},站点写 ${ENTRY_FILE[KIND_SITE]}。` });
|
|
122
|
+
} else {
|
|
123
|
+
const active = getActiveArtifact(sessionId)?.path || null;
|
|
124
|
+
const lines = [];
|
|
125
|
+
let manifest = null;
|
|
126
|
+
for (const a of artifacts.slice(0, 8)) {
|
|
127
|
+
let note = '';
|
|
128
|
+
try {
|
|
129
|
+
if (!manifest) manifest = await taskManifest(workspaceRoot);
|
|
130
|
+
const art = manifest?.artifacts?.find(x => x.entryRel === a.rel) || null;
|
|
131
|
+
note = art ? await kindDef(art.kind).describe(workspaceRoot, art) : '还判不出形态';
|
|
132
|
+
} catch { note = '读不到'; }
|
|
133
|
+
lines.push(` ${a.rel}(${note})${a.rel === active ? ' ← 画布工具默认打这份' : ''}`);
|
|
134
|
+
}
|
|
135
|
+
sections.push({ key: 'artifacts', title: '产物', text: `现有产物:\n${lines.join('\n')}` });
|
|
136
|
+
}
|
|
137
|
+
} catch { /* 扫不动就不说 */ }
|
|
138
|
+
|
|
139
|
+
// tweaks 开关:文件不存在 = 用户没碰过 = 沉默
|
|
140
|
+
try {
|
|
141
|
+
const cfg = await readUiConfigFile(workspaceRoot);
|
|
142
|
+
if (cfg) {
|
|
143
|
+
const on = cfg?.tweaks_mode_enabled !== false;
|
|
144
|
+
sections.push({ key: 'tweaks', title: 'Tweaks 开关', text: on
|
|
145
|
+
? '【Tweaks 模式:启用】用户偏好"可调产品"——deck 形态稳定后,建议调 expose_tweaks 暴露核心微调参数(颜色 / 字号 / 排版密度等)让用户拖滑杆即时改样式。'
|
|
146
|
+
: '【Tweaks 模式:禁用】用户在 toolbar 关闭了 Tweaks——这次跳过 expose_tweaks,按对话方式让用户提需求你来 Edit。已暴露的 controls 保留不动,不新增 / 不重 expose。' });
|
|
147
|
+
}
|
|
148
|
+
} catch { /* 读失败:默认行为,不注入 */ }
|
|
149
|
+
|
|
150
|
+
return sections;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 把各节渲染成这一轮的注入文本(纯函数,可单测)。
|
|
155
|
+
* @param {Array} sections collectSections 的结果
|
|
156
|
+
* @param {Map|null} prev 上一轮记忆(null = 首轮)
|
|
157
|
+
* @returns {{ text: string|null, next: Map }}
|
|
158
|
+
*/
|
|
159
|
+
export function renderTurnState(sections, prev) {
|
|
160
|
+
const next = new Map();
|
|
161
|
+
for (const s of sections) next.set(s.key, { hash: fingerprint(s.text), items: s.items ? [...s.items] : null, hasBinaryDocs: !!s.hasBinaryDocs });
|
|
162
|
+
if (!sections.length) return { text: null, next };
|
|
163
|
+
|
|
164
|
+
if (!prev) {
|
|
165
|
+
const body = sections.map(s => s.text).join('\n\n');
|
|
166
|
+
return { text: `[NoDesign 工作台自动注入的当前状态]\n\n${body}\n\n请基于这些信息处理用户的请求。`, next };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const changed = []; const unchanged = []; const gone = [];
|
|
170
|
+
for (const s of sections) {
|
|
171
|
+
const p = prev.get(s.key);
|
|
172
|
+
if (!p) { changed.push(`(新出现)${s.text}`); continue; }
|
|
173
|
+
if (p.hash === next.get(s.key).hash) { unchanged.push(s.title); continue; }
|
|
174
|
+
if (s.items && p.items) {
|
|
175
|
+
const d = diffItems(p.items, s.items);
|
|
176
|
+
const bits = [];
|
|
177
|
+
if (d.added.length) bits.push(`新增 ${d.added.length}:${d.added.slice(0, 8).join('、')}${d.added.length > 8 ? ' 等' : ''}`);
|
|
178
|
+
if (d.removed.length) bits.push(`移除 ${d.removed.length}:${d.removed.slice(0, 8).join('、')}${d.removed.length > 8 ? ' 等' : ''}`);
|
|
179
|
+
if (bits.length) {
|
|
180
|
+
let line = `${s.title}(有变化):${bits.join(';')}(现共 ${s.items.length} 件)`;
|
|
181
|
+
if (s.hasBinaryDocs && !p.hasBinaryDocs) line += `\n${BINARY_DOC_HINT}`;
|
|
182
|
+
changed.push(line);
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
changed.push(`(有变化)${s.text}`);
|
|
187
|
+
}
|
|
188
|
+
for (const [key] of prev) if (!next.has(key)) gone.push(key);
|
|
189
|
+
const lines = [];
|
|
190
|
+
if (changed.length) lines.push(...changed);
|
|
191
|
+
if (gone.length) lines.push(`(已不存在:${gone.join('、')})`);
|
|
192
|
+
if (!changed.length && !gone.length) {
|
|
193
|
+
return { text: `[工作台状态:与上轮相同(${unchanged.join('、')})]`, next };
|
|
194
|
+
}
|
|
195
|
+
lines.push(`未变:${unchanged.join('、') || '(无)'}`);
|
|
196
|
+
return { text: `[工作台状态 · 只报变化]\n\n${lines.join('\n\n')}`, next };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function makeUserPromptSubmitHandler({ ctx: _ctx, workspaceRoot, sessionId, projectId }) {
|
|
200
|
+
return async (_input, _toolUseId, _options) => {
|
|
201
|
+
try {
|
|
202
|
+
if (!workspaceRoot) return {};
|
|
203
|
+
const sections = await collectSections({ workspaceRoot, sessionId, projectId });
|
|
204
|
+
const prev = getTurnMemory(sessionId)?.sections || null;
|
|
205
|
+
const { text, next } = renderTurnState(sections, prev);
|
|
206
|
+
setTurnMemory(sessionId, next);
|
|
207
|
+
if (!text) return {};
|
|
208
|
+
// 不 emit 业务事件 —— additionalContext 注入是私域提示,不需要前端展示
|
|
209
|
+
return { hookSpecificOutput: { hookEventName: 'UserPromptSubmit', additionalContext: text } };
|
|
210
|
+
} catch (err) {
|
|
211
|
+
console.warn('[hooks/UserPromptSubmit] handler threw:', err.message);
|
|
212
|
+
return {};
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|