hostlet 0.0.0-stage → 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +233 -2
- package/bin/hostlet.js +61 -0
- package/console/app/api.js +1223 -0
- package/console/app/boot.js +165 -0
- package/console/app/cards.js +222 -0
- package/console/app/compose-quote.js +127 -0
- package/console/app/config-form.js +727 -0
- package/console/app/document-drafts.js +62 -0
- package/console/app/document-tools.mjs +67 -0
- package/console/app/documents.js +106 -0
- package/console/app/pages.js +6483 -0
- package/console/app/plugins.js +509 -0
- package/console/app/preview-tools.js +172 -0
- package/console/app/reference-labels.js +118 -0
- package/console/app/selection.js +372 -0
- package/console/app/store.js +57 -0
- package/console/app/text-anchor.js +26 -0
- package/console/app/topic-local.js +322 -0
- package/console/app/topic-nav.js +414 -0
- package/console/app/topic-status.js +109 -0
- package/console/app/views.js +43 -0
- package/console/apple-touch-icon.png +0 -0
- package/console/avatars/agy.svg +34 -0
- package/console/avatars/alhena.svg +36 -0
- package/console/avatars/castor.svg +38 -0
- package/console/avatars/claude.svg +34 -0
- package/console/avatars/codex.svg +34 -0
- package/console/avatars/devin.svg +34 -0
- package/console/avatars/echo.svg +34 -0
- package/console/avatars/kimi.svg +33 -0
- package/console/avatars/opencode.svg +33 -0
- package/console/avatars/pinion.svg +34 -0
- package/console/avatars/sprocket.svg +34 -0
- package/console/avatars/vela.svg +44 -0
- package/console/avatars/wasat.svg +38 -0
- package/console/clients/agy.svg +1 -0
- package/console/clients/claude.svg +1 -0
- package/console/clients/codex.svg +1 -0
- package/console/clients/devin.svg +1 -0
- package/console/clients/kimi.svg +1 -0
- package/console/clients/opencode.svg +1 -0
- package/console/favicon.ico +0 -0
- package/console/favicon.svg +16 -0
- package/console/index.html +99 -0
- package/console/pdl/engine/README.md +13 -0
- package/console/pdl/engine/anchor.js +195 -0
- package/console/pdl/engine/core.js +1340 -0
- package/console/pdl/engine/frame.js +1252 -0
- package/console/pdl/engine/i18n.js +67 -0
- package/console/pdl/engine/pdl-check.js +102 -0
- package/console/pdl/engine/pdl-doc.js +353 -0
- package/console/pdl/engine/pdl-view.js +280 -0
- package/console/pdl/engine/pdl.js +1064 -0
- package/console/pdl/engine/platform.js +18 -0
- package/console/pdl/engine/proj.js +279 -0
- package/console/pdl/engine/theme.js +176 -0
- package/console/pdl/engine/tree.js +171 -0
- package/console/pdl/engine/units.js +546 -0
- package/console/pdl/library/README.md +28 -0
- package/console/pdl/library/blocks.js +1512 -0
- package/console/pdl/library/buttons.js +86 -0
- package/console/pdl/library/capsules.js +130 -0
- package/console/pdl/library/charts.js +287 -0
- package/console/pdl/library/code.js +98 -0
- package/console/pdl/library/css/base.css +12 -0
- package/console/pdl/library/css/blocks.css +98 -0
- package/console/pdl/library/css/capsules.css +42 -0
- package/console/pdl/library/css/catalog-page.css +24 -0
- package/console/pdl/library/css/chat.css +100 -0
- package/console/pdl/library/css/composer-top.css +145 -0
- package/console/pdl/library/css/controls.css +195 -0
- package/console/pdl/library/css/drag.css +29 -0
- package/console/pdl/library/css/edit-layout.css +93 -0
- package/console/pdl/library/css/floating-window.css +30 -0
- package/console/pdl/library/css/hover.css +50 -0
- package/console/pdl/library/css/label-search.css +37 -0
- package/console/pdl/library/css/layers.css +43 -0
- package/console/pdl/library/css/library-parts.css +33 -0
- package/console/pdl/library/css/library.css +117 -0
- package/console/pdl/library/css/member-select.css +34 -0
- package/console/pdl/library/css/message.css +72 -0
- package/console/pdl/library/css/page.css +104 -0
- package/console/pdl/library/css/parts.css +338 -0
- package/console/pdl/library/css/responsive.css +40 -0
- package/console/pdl/library/css/roles.css +28 -0
- package/console/pdl/library/css/rows.css +231 -0
- package/console/pdl/library/css/settings-rows.css +37 -0
- package/console/pdl/library/css/shell.css +59 -0
- package/console/pdl/library/css/stream.css +296 -0
- package/console/pdl/library/css/theme-calculator.css +18 -0
- package/console/pdl/library/css/thumb-choice.css +69 -0
- package/console/pdl/library/css/tweaks.css +16 -0
- package/console/pdl/library/css/widgets.css +211 -0
- package/console/pdl/library/css/workbench.css +59 -0
- package/console/pdl/library/feedback.js +83 -0
- package/console/pdl/library/floating-window.mjs +100 -0
- package/console/pdl/library/fold.js +238 -0
- package/console/pdl/library/format.js +53 -0
- package/console/pdl/library/frames.js +123 -0
- package/console/pdl/library/icons.js +165 -0
- package/console/pdl/library/identity.js +149 -0
- package/console/pdl/library/inputs.js +801 -0
- package/console/pdl/library/label-search.js +214 -0
- package/console/pdl/library/markdown-text.js +115 -0
- package/console/pdl/library/markdown.js +150 -0
- package/console/pdl/library/media-viewer.js +293 -0
- package/console/pdl/library/member-select.mjs +32 -0
- package/console/pdl/library/message.js +105 -0
- package/console/pdl/library/motion.js +56 -0
- package/console/pdl/library/overlays.js +165 -0
- package/console/pdl/library/page.js +142 -0
- package/console/pdl/library/plugin-form.js +154 -0
- package/console/pdl/library/registry.js +198 -0
- package/console/pdl/library/rows.js +523 -0
- package/console/pdl/library/shell.js +151 -0
- package/console/pdl/library/stream.js +58 -0
- package/console/pdl/library/tabs.js +259 -0
- package/console/pdl/library/theme-calculator.js +300 -0
- package/console/pdl/library/widgets.js +114 -0
- package/console/pdl/load-order.json +74 -0
- package/console/pdl/platform/README.md +3 -0
- package/console/pdl/platform/rich-markdown.mjs +17 -0
- package/console/pdl/platform/web-links.js +26 -0
- package/console/pdl/platform/web.js +80 -0
- package/console/sdk-browser/mentions.d.mts +16 -0
- package/console/sdk-browser/mentions.mjs +84 -0
- package/console/theme/assets.js +21 -0
- package/console/theme/theme.json +9417 -0
- package/console/theme/tokens.json +444 -0
- package/dist/core/a2a.d.ts +288 -0
- package/dist/core/a2a.js +2197 -0
- package/dist/core/address.d.ts +34 -0
- package/dist/core/address.js +33 -0
- package/dist/core/approval-inputs.d.ts +17 -0
- package/dist/core/approval-inputs.js +70 -0
- package/dist/core/archive.d.ts +19 -0
- package/dist/core/archive.js +135 -0
- package/dist/core/attachments.d.ts +17 -0
- package/dist/core/attachments.js +143 -0
- package/dist/core/blocked-handoffs.d.ts +132 -0
- package/dist/core/blocked-handoffs.js +349 -0
- package/dist/core/config-schema.d.ts +124 -0
- package/dist/core/config-schema.js +192 -0
- package/dist/core/config.d.ts +117 -0
- package/dist/core/config.js +638 -0
- package/dist/core/disk-usage.d.ts +26 -0
- package/dist/core/disk-usage.js +83 -0
- package/dist/core/documents.d.ts +9 -0
- package/dist/core/documents.js +48 -0
- package/dist/core/entry.d.ts +316 -0
- package/dist/core/entry.js +711 -0
- package/dist/core/home.d.ts +76 -0
- package/dist/core/home.js +449 -0
- package/dist/core/i18n.d.ts +36 -0
- package/dist/core/i18n.js +458 -0
- package/dist/core/instances.d.ts +224 -0
- package/dist/core/instances.js +1272 -0
- package/dist/core/members.d.ts +190 -0
- package/dist/core/members.js +358 -0
- package/dist/core/mentions.d.ts +12 -0
- package/dist/core/mentions.js +49 -0
- package/dist/core/messages.d.ts +876 -0
- package/dist/core/messages.js +916 -0
- package/dist/core/notices.d.ts +88 -0
- package/dist/core/notices.js +54 -0
- package/dist/core/ops.d.ts +117 -0
- package/dist/core/ops.js +146 -0
- package/dist/core/package.d.ts +2 -0
- package/dist/core/package.js +14 -0
- package/dist/core/parts.d.ts +25 -0
- package/dist/core/parts.js +119 -0
- package/dist/core/plugins/catalog.d.ts +14 -0
- package/dist/core/plugins/catalog.js +54 -0
- package/dist/core/plugins/external.d.ts +7 -0
- package/dist/core/plugins/external.js +921 -0
- package/dist/core/plugins/index.d.ts +5 -0
- package/dist/core/plugins/index.js +51 -0
- package/dist/core/plugins/npm.d.ts +12 -0
- package/dist/core/plugins/npm.js +99 -0
- package/dist/core/plugins/plugin-manifest.d.ts +12 -0
- package/dist/core/plugins/plugin-manifest.js +231 -0
- package/dist/core/push.d.ts +22 -0
- package/dist/core/push.js +63 -0
- package/dist/core/resource.d.ts +77 -0
- package/dist/core/resource.js +48 -0
- package/dist/core/spawn.d.ts +12 -0
- package/dist/core/spawn.js +30 -0
- package/dist/core/storage-inventory.d.ts +118 -0
- package/dist/core/storage-inventory.js +279 -0
- package/dist/core/storage.d.ts +45 -0
- package/dist/core/storage.js +602 -0
- package/dist/core/store.d.ts +15 -0
- package/dist/core/store.js +33 -0
- package/dist/core/topic-index.d.ts +19 -0
- package/dist/core/topic-index.js +70 -0
- package/dist/core/topic-ops.d.ts +139 -0
- package/dist/core/topic-ops.js +507 -0
- package/dist/core/topic-status.d.ts +32 -0
- package/dist/core/topic-status.js +105 -0
- package/dist/core/topics.d.ts +327 -0
- package/dist/core/topics.js +786 -0
- package/dist/core/turn-clients.d.ts +1 -0
- package/dist/core/turn-clients.js +5 -0
- package/dist/core/types.d.ts +2866 -0
- package/dist/core/types.js +166 -0
- package/dist/core/util.d.ts +10 -0
- package/dist/core/util.js +47 -0
- package/dist/main.d.ts +12 -0
- package/dist/main.js +82 -0
- package/dist/plugins/adapter-acp/index.d.ts +3 -0
- package/dist/plugins/adapter-acp/index.js +57 -0
- package/dist/plugins/adapter-acp/plugin.yaml +100 -0
- package/dist/plugins/adapter-agy/command.d.ts +9 -0
- package/dist/plugins/adapter-agy/command.js +34 -0
- package/dist/plugins/adapter-agy/index.d.ts +5 -0
- package/dist/plugins/adapter-agy/index.js +65 -0
- package/dist/plugins/adapter-agy/quota.d.ts +2 -0
- package/dist/plugins/adapter-agy/quota.js +155 -0
- package/dist/plugins/adapter-agy/terminal.d.ts +6 -0
- package/dist/plugins/adapter-agy/terminal.js +132 -0
- package/dist/plugins/adapter-agy/version.d.ts +9 -0
- package/dist/plugins/adapter-agy/version.js +17 -0
- package/dist/plugins/adapter-claude/index.d.ts +43 -0
- package/dist/plugins/adapter-claude/index.js +412 -0
- package/dist/plugins/adapter-claude/quota.d.ts +14 -0
- package/dist/plugins/adapter-claude/quota.js +103 -0
- package/dist/plugins/adapter-codex/index.d.ts +4 -0
- package/dist/plugins/adapter-codex/index.js +688 -0
- package/dist/plugins/adapter-codex/responses-model.d.ts +9 -0
- package/dist/plugins/adapter-codex/responses-model.js +82 -0
- package/dist/plugins/adapter-kimi/index.d.ts +4 -0
- package/dist/plugins/adapter-kimi/index.js +51 -0
- package/dist/plugins/adapter-kimi/quota.d.ts +3 -0
- package/dist/plugins/adapter-kimi/quota.js +171 -0
- package/dist/plugins/adapter-opencode/index.d.ts +12 -0
- package/dist/plugins/adapter-opencode/index.js +64 -0
- package/dist/plugins/annotations/console/cards.css +189 -0
- package/dist/plugins/annotations/console/cards.mjs +439 -0
- package/dist/plugins/annotations/console/pdl.json +52 -0
- package/dist/plugins/annotations/console/view.mjs +241 -0
- package/dist/plugins/annotations/console/window.mjs +219 -0
- package/dist/plugins/annotations/followups.d.ts +51 -0
- package/dist/plugins/annotations/followups.js +112 -0
- package/dist/plugins/annotations/index.d.ts +3 -0
- package/dist/plugins/annotations/index.js +298 -0
- package/dist/plugins/annotations/plugin.yaml +3 -0
- package/dist/plugins/ask-choice/index.d.ts +9 -0
- package/dist/plugins/ask-choice/index.js +24 -0
- package/dist/plugins/ask-choice/plugin.yaml +2 -0
- package/dist/plugins/background/index.d.ts +2 -0
- package/dist/plugins/background/index.js +398 -0
- package/dist/plugins/background/plugin.yaml +7 -0
- package/dist/plugins/builtin.json +37 -0
- package/dist/plugins/cards/console/pdl.json +516 -0
- package/dist/plugins/cards/console/view.mjs +67 -0
- package/dist/plugins/cards/index.d.ts +8 -0
- package/dist/plugins/cards/index.js +627 -0
- package/dist/plugins/cards/plugin.yaml +3 -0
- package/dist/plugins/console/browser.d.ts +2 -0
- package/dist/plugins/console/browser.js +115 -0
- package/dist/plugins/console/console/pdl.json +38 -0
- package/dist/plugins/console/console/view.mjs +26 -0
- package/dist/plugins/console/index.d.ts +4 -0
- package/dist/plugins/console/index.js +94 -0
- package/dist/plugins/console/picker.d.ts +2 -0
- package/dist/plugins/console/picker.js +48 -0
- package/dist/plugins/console/plugin.yaml +3 -0
- package/dist/plugins/console/topic-view.d.ts +3 -0
- package/dist/plugins/console/topic-view.js +388 -0
- package/dist/plugins/handoff/index.d.ts +2 -0
- package/dist/plugins/handoff/index.js +147 -0
- package/dist/plugins/handoff/plugin.yaml +8 -0
- package/dist/plugins/host-api/index.d.ts +7 -0
- package/dist/plugins/host-api/index.js +310 -0
- package/dist/plugins/host-api/plugin.yaml +2 -0
- package/dist/plugins/host-api/signing.d.ts +63 -0
- package/dist/plugins/host-api/signing.js +427 -0
- package/dist/plugins/host-api/skills/host-ops/SKILL.md +28 -0
- package/dist/plugins/host-api/skills/hostlet-plugin-dev/SKILL.md +38 -0
- package/dist/plugins/inbox/index.d.ts +15 -0
- package/dist/plugins/inbox/index.js +161 -0
- package/dist/plugins/inbox/plugin.yaml +2 -0
- package/dist/plugins/keys/account-directories.d.ts +18 -0
- package/dist/plugins/keys/account-directories.js +86 -0
- package/dist/plugins/keys/account-storage.d.ts +18 -0
- package/dist/plugins/keys/account-storage.js +112 -0
- package/dist/plugins/keys/console/pdl.json +710 -0
- package/dist/plugins/keys/console/view.mjs +619 -0
- package/dist/plugins/keys/index.d.ts +7 -0
- package/dist/plugins/keys/index.js +842 -0
- package/dist/plugins/keys/login-link.d.ts +2 -0
- package/dist/plugins/keys/login-link.js +15 -0
- package/dist/plugins/keys/plugin.yaml +3 -0
- package/dist/plugins/keys/quota-history.d.ts +91 -0
- package/dist/plugins/keys/quota-history.js +420 -0
- package/dist/plugins/mcp/console/pdl.json +412 -0
- package/dist/plugins/mcp/console/view.mjs +207 -0
- package/dist/plugins/mcp/index.d.ts +7 -0
- package/dist/plugins/mcp/index.js +494 -0
- package/dist/plugins/mcp/plugin.yaml +3 -0
- package/dist/plugins/member-health/index.d.ts +2 -0
- package/dist/plugins/member-health/index.js +478 -0
- package/dist/plugins/member-health/plugin.yaml +2 -0
- package/dist/plugins/memory/capabilities.d.ts +10 -0
- package/dist/plugins/memory/capabilities.js +70 -0
- package/dist/plugins/memory/console/pdl.json +459 -0
- package/dist/plugins/memory/console/view.mjs +254 -0
- package/dist/plugins/memory/corpus.d.ts +60 -0
- package/dist/plugins/memory/corpus.js +521 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.d.ts +8 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.js +49 -0
- package/dist/plugins/memory/engines/fts-tree-text.d.ts +12 -0
- package/dist/plugins/memory/engines/fts-tree-text.js +93 -0
- package/dist/plugins/memory/engines/fts-tree.d.ts +4 -0
- package/dist/plugins/memory/engines/fts-tree.js +387 -0
- package/dist/plugins/memory/engines/search.d.ts +9 -0
- package/dist/plugins/memory/engines/search.js +35 -0
- package/dist/plugins/memory/index.d.ts +3 -0
- package/dist/plugins/memory/index.js +465 -0
- package/dist/plugins/memory/notes.d.ts +22 -0
- package/dist/plugins/memory/notes.js +140 -0
- package/dist/plugins/memory/plugin.yaml +23 -0
- package/dist/plugins/model-profiles/index.d.ts +2 -0
- package/dist/plugins/model-profiles/index.js +919 -0
- package/dist/plugins/model-profiles/plugin.yaml +13 -0
- package/dist/plugins/model-profiles/review-source.d.ts +11 -0
- package/dist/plugins/model-profiles/review-source.js +640 -0
- package/dist/plugins/model-profiles/rules.d.ts +132 -0
- package/dist/plugins/model-profiles/rules.js +767 -0
- package/dist/plugins/model-profiles/skills/model-profile/SKILL.md +60 -0
- package/dist/plugins/model-profiles/sources.d.ts +61 -0
- package/dist/plugins/model-profiles/sources.js +375 -0
- package/dist/plugins/model-profiles/storage.d.ts +104 -0
- package/dist/plugins/model-profiles/storage.js +352 -0
- package/dist/plugins/model-profiles/types.d.ts +187 -0
- package/dist/plugins/model-profiles/types.js +12 -0
- package/dist/plugins/model-profiles/wakeup.d.ts +29 -0
- package/dist/plugins/model-profiles/wakeup.js +390 -0
- package/dist/plugins/models/index.d.ts +2 -0
- package/dist/plugins/models/index.js +63 -0
- package/dist/plugins/models/plugin.yaml +8 -0
- package/dist/plugins/ops/collector.d.ts +22 -0
- package/dist/plugins/ops/collector.js +48 -0
- package/dist/plugins/ops/config.d.ts +11 -0
- package/dist/plugins/ops/config.js +13 -0
- package/dist/plugins/ops/console/pdl.json +676 -0
- package/dist/plugins/ops/console/view.mjs +700 -0
- package/dist/plugins/ops/index.d.ts +3 -0
- package/dist/plugins/ops/index.js +20 -0
- package/dist/plugins/ops/journal.d.ts +7 -0
- package/dist/plugins/ops/journal.js +109 -0
- package/dist/plugins/ops/metrics.d.ts +3 -0
- package/dist/plugins/ops/metrics.js +81 -0
- package/dist/plugins/ops/plugin.yaml +63 -0
- package/dist/plugins/ops/samples.d.ts +54 -0
- package/dist/plugins/ops/samples.js +100 -0
- package/dist/plugins/ops/trace.d.ts +3 -0
- package/dist/plugins/ops/trace.js +104 -0
- package/dist/plugins/ops/usage.d.ts +2 -0
- package/dist/plugins/ops/usage.js +152 -0
- package/dist/plugins/plans/acceptance.d.ts +284 -0
- package/dist/plugins/plans/acceptance.js +247 -0
- package/dist/plugins/plans/blocked.d.ts +25 -0
- package/dist/plugins/plans/blocked.js +66 -0
- package/dist/plugins/plans/console/pdl.json +639 -0
- package/dist/plugins/plans/console/view.d.mts +2 -0
- package/dist/plugins/plans/console/view.mjs +855 -0
- package/dist/plugins/plans/dependencies.d.ts +5 -0
- package/dist/plugins/plans/dependencies.js +21 -0
- package/dist/plugins/plans/index.d.ts +72 -0
- package/dist/plugins/plans/index.js +2057 -0
- package/dist/plugins/plans/plugin.yaml +44 -0
- package/dist/plugins/plans/progress.d.ts +82 -0
- package/dist/plugins/plans/progress.js +516 -0
- package/dist/plugins/prompt/index.d.ts +8 -0
- package/dist/plugins/prompt/index.js +361 -0
- package/dist/plugins/prompt/plugin.yaml +2 -0
- package/dist/plugins/recovery/console/pdl.json +50 -0
- package/dist/plugins/recovery/console/view.mjs +46 -0
- package/dist/plugins/recovery/index.d.ts +9 -0
- package/dist/plugins/recovery/index.js +454 -0
- package/dist/plugins/recovery/plugin.yaml +9 -0
- package/dist/plugins/recovery/probe.d.ts +11 -0
- package/dist/plugins/recovery/probe.js +128 -0
- package/dist/plugins/restart/console/pdl.json +112 -0
- package/dist/plugins/restart/console/view.mjs +85 -0
- package/dist/plugins/restart/index.d.ts +10 -0
- package/dist/plugins/restart/index.js +669 -0
- package/dist/plugins/restart/plugin.yaml +9 -0
- package/dist/plugins/restart/scope.d.ts +6 -0
- package/dist/plugins/restart/scope.js +44 -0
- package/dist/plugins/schedule/console/pdl.json +696 -0
- package/dist/plugins/schedule/console/view.mjs +398 -0
- package/dist/plugins/schedule/events.d.ts +76 -0
- package/dist/plugins/schedule/events.js +158 -0
- package/dist/plugins/schedule/index.d.ts +32 -0
- package/dist/plugins/schedule/index.js +1199 -0
- package/dist/plugins/schedule/plugin.yaml +26 -0
- package/dist/plugins/schedule/when.d.ts +14 -0
- package/dist/plugins/schedule/when.js +83 -0
- package/dist/plugins/sessions/console/pdl.json +259 -0
- package/dist/plugins/sessions/console/view.mjs +355 -0
- package/dist/plugins/sessions/index.d.ts +3 -0
- package/dist/plugins/sessions/index.js +126 -0
- package/dist/plugins/sessions/participants.d.ts +28 -0
- package/dist/plugins/sessions/participants.js +83 -0
- package/dist/plugins/sessions/plugin.yaml +3 -0
- package/dist/plugins/signals/console/pdl.json +802 -0
- package/dist/plugins/signals/console/view.mjs +284 -0
- package/dist/plugins/signals/content.d.ts +31 -0
- package/dist/plugins/signals/content.js +151 -0
- package/dist/plugins/signals/index.d.ts +2 -0
- package/dist/plugins/signals/index.js +601 -0
- package/dist/plugins/signals/plugin.yaml +32 -0
- package/dist/plugins/signals/skills/signal-sources/SKILL.md +27 -0
- package/dist/plugins/skills/console/pdl.json +254 -0
- package/dist/plugins/skills/console/view.mjs +204 -0
- package/dist/plugins/skills/index.d.ts +11 -0
- package/dist/plugins/skills/index.js +567 -0
- package/dist/plugins/skills/install.d.ts +69 -0
- package/dist/plugins/skills/install.js +265 -0
- package/dist/plugins/skills/plugin.yaml +24 -0
- package/dist/plugins/skills/safety-scan.d.ts +17 -0
- package/dist/plugins/skills/safety-scan.js +78 -0
- package/dist/plugins/skills/skills/skill-ops/SKILL.md +29 -0
- package/dist/plugins/terminal/console/pdl.json +115 -0
- package/dist/plugins/terminal/console/view.mjs +178 -0
- package/dist/plugins/terminal/index.d.ts +3 -0
- package/dist/plugins/terminal/index.js +204 -0
- package/dist/plugins/terminal/plugin.yaml +2 -0
- package/dist/plugins/tools/index.d.ts +3 -0
- package/dist/plugins/tools/index.js +458 -0
- package/dist/plugins/tools/plugin.yaml +2 -0
- package/dist/plugins/tools/token.d.ts +61 -0
- package/dist/plugins/tools/token.js +179 -0
- package/dist/plugins/topic-tools/index.d.ts +9 -0
- package/dist/plugins/topic-tools/index.js +29 -0
- package/dist/plugins/topic-tools/plugin.yaml +2 -0
- package/dist/plugins/topic-tools/skills/topic-tidy/SKILL.md +108 -0
- package/dist/plugins/works/console/pdl.json +120 -0
- package/dist/plugins/works/console/view.mjs +91 -0
- package/dist/plugins/works/document-words.d.ts +50 -0
- package/dist/plugins/works/document-words.js +27 -0
- package/dist/plugins/works/editors.d.ts +3 -0
- package/dist/plugins/works/editors.js +162 -0
- package/dist/plugins/works/events.d.ts +58 -0
- package/dist/plugins/works/events.js +82 -0
- package/dist/plugins/works/files.d.ts +23 -0
- package/dist/plugins/works/files.js +53 -0
- package/dist/plugins/works/index.d.ts +9 -0
- package/dist/plugins/works/index.js +1088 -0
- package/dist/plugins/works/plugin.yaml +9 -0
- package/dist/plugins/works/preview.d.ts +10 -0
- package/dist/plugins/works/preview.js +30 -0
- package/dist/plugins/works/project.d.ts +3 -0
- package/dist/plugins/works/project.js +245 -0
- package/dist/plugins/works/resource.d.ts +28 -0
- package/dist/plugins/works/resource.js +71 -0
- package/dist/plugins/works/retention.d.ts +36 -0
- package/dist/plugins/works/retention.js +295 -0
- package/dist/plugins/works/skills/works-ops/SKILL.md +14 -0
- package/dist/plugins/works/viewers.d.ts +13 -0
- package/dist/plugins/works/viewers.js +87 -0
- package/dist/plugins/works/web/anchors.js +94 -0
- package/dist/plugins/works/web/document-edit.js +245 -0
- package/dist/plugins/works/web/preview-bridge.js +142 -0
- package/dist/plugins/works/web/reading-position.js +74 -0
- package/dist/plugins/works/web/view.html +1118 -0
- package/dist/runtime/runtime.d.ts +210 -0
- package/dist/runtime/runtime.js +338 -0
- package/dist/sdk/adapter-kit.d.ts +1 -0
- package/dist/sdk/adapter-kit.js +1 -0
- package/dist/sdk/client.d.ts +163 -0
- package/dist/sdk/client.js +43 -0
- package/dist/sdk/contracts.d.ts +1263 -0
- package/dist/sdk/contracts.js +1 -0
- package/dist/sdk/index.d.ts +57 -0
- package/dist/sdk/index.js +27 -0
- package/dist/sdk/source-choice.d.ts +40 -0
- package/dist/sdk/source-choice.js +217 -0
- package/dist/sdk/topic-text.d.ts +5 -0
- package/dist/sdk/topic-text.js +26 -0
- package/dist/sdk/transport.d.ts +12 -0
- package/dist/sdk/transport.js +34 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.d.ts +88 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.js +674 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.d.ts +18 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.js +39 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.d.ts +8 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.js +6 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.js +108 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.d.ts +3 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.js +4 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.js +84 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.d.ts +5 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.js +37 -0
- package/node_modules/@hostlet/adapter-kit/package.json +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.d.ts +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.js +276 -0
- package/node_modules/@hostlet/platform/dist/files.d.ts +24 -0
- package/node_modules/@hostlet/platform/dist/files.js +113 -0
- package/node_modules/@hostlet/platform/dist/index.d.ts +6 -0
- package/node_modules/@hostlet/platform/dist/index.js +4 -0
- package/node_modules/@hostlet/platform/dist/paths.d.ts +4 -0
- package/node_modules/@hostlet/platform/dist/paths.js +9 -0
- package/node_modules/@hostlet/platform/dist/process.d.ts +45 -0
- package/node_modules/@hostlet/platform/dist/process.js +297 -0
- package/node_modules/@hostlet/platform/package.json +14 -0
- package/package.json +60 -4
|
@@ -0,0 +1,786 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { appendFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { configOwnership } from "./config.js";
|
|
5
|
+
import { HttpError } from "./entry.js";
|
|
6
|
+
import { array, object } from "./ops.js";
|
|
7
|
+
import { topicOps } from "./topic-ops.js";
|
|
8
|
+
import { makeTopicIndex } from "./topic-index.js";
|
|
9
|
+
import { makeTopicStatus } from "./topic-status.js";
|
|
10
|
+
import { isDirRef, isTopicRef } from "./types.js";
|
|
11
|
+
import { log, newId } from "./util.js";
|
|
12
|
+
export const draftRefSchema = object({
|
|
13
|
+
id: { type: 'string' },
|
|
14
|
+
kind: { type: 'string', enum: ['item', 'selection', 'work', 'file'] },
|
|
15
|
+
label: { type: 'string' },
|
|
16
|
+
excerpt: { type: 'string' },
|
|
17
|
+
source: { type: ['object', 'null'] },
|
|
18
|
+
href: { type: 'string' },
|
|
19
|
+
}, ['id', 'kind', 'label']);
|
|
20
|
+
export const draftSchema = object({
|
|
21
|
+
revision: { type: 'integer' },
|
|
22
|
+
text: { type: 'string' },
|
|
23
|
+
replyTo: { type: ['string', 'null'] },
|
|
24
|
+
quote: { type: ['string', 'null'] },
|
|
25
|
+
refs: array(draftRefSchema),
|
|
26
|
+
}, ['revision', 'text', 'replyTo', 'quote', 'refs']);
|
|
27
|
+
/** A directory's spelling can change without changing the reference it identifies. */
|
|
28
|
+
const refPath = (path) => isAbsolute(path.trim()) ? resolve(path.trim()) : path.trim();
|
|
29
|
+
export const refKey = (ref) => isDirRef(ref) ? `dir:${refPath(ref.path)}` : `topic:${ref.topicId.trim()}`;
|
|
30
|
+
const directoryAvailable = (path) => existsSync(path) && statSync(path, { throwIfNoEntry: false })?.isDirectory() === true;
|
|
31
|
+
function checkDirectory(path) {
|
|
32
|
+
if (!isAbsolute(path))
|
|
33
|
+
throw new Error('要写目录的完整路径');
|
|
34
|
+
if (!directoryAvailable(path))
|
|
35
|
+
throw new Error(`没有这个目录:${path}`);
|
|
36
|
+
}
|
|
37
|
+
const ownerships = new WeakMap();
|
|
38
|
+
export const topicOwnership = (topics) => {
|
|
39
|
+
const ownership = ownerships.get(topics);
|
|
40
|
+
if (!ownership)
|
|
41
|
+
throw new Error('这个主题服务没有归属入口');
|
|
42
|
+
return ownership;
|
|
43
|
+
};
|
|
44
|
+
export function requireTopic(topics, id) {
|
|
45
|
+
const topic = topics.get(id);
|
|
46
|
+
if (!topic || topic.deletedAt)
|
|
47
|
+
throw new HttpError(404, '没有这个 topic');
|
|
48
|
+
return topic;
|
|
49
|
+
}
|
|
50
|
+
const UNTITLED = '新主题';
|
|
51
|
+
const SYSTEM_TITLE = '系统';
|
|
52
|
+
const SYSTEM_SUMMARY = '收纳系统运行与插件自动创建的主题。';
|
|
53
|
+
const TOPIC_PATCH_KEYS = ['title', 'parentId', 'tags', 'refs', 'pinned', 'caps', 'view', 'members', 'defaultMember'];
|
|
54
|
+
const KEEP_DAYS = 'topics.trashKeepDays';
|
|
55
|
+
export const SUMMARY_MAX_CHARS = 'topics.summaryMaxChars';
|
|
56
|
+
const DAY = 86_400_000;
|
|
57
|
+
export function topicsPlugin(home) {
|
|
58
|
+
return {
|
|
59
|
+
name: 'topics',
|
|
60
|
+
core: true,
|
|
61
|
+
needs: ['store', 'config', 'push', 'entry'],
|
|
62
|
+
start(ctx) {
|
|
63
|
+
const store = ctx.get('store');
|
|
64
|
+
const { db } = store;
|
|
65
|
+
const config = configOwnership(ctx.get('config'));
|
|
66
|
+
const push = ctx.get('push');
|
|
67
|
+
ctx.effect(config.declare({ scope: 'settings', owner: 'core', fields: [{
|
|
68
|
+
key: 'topics.maxDepth', kind: 'number', default: 5, minimum: 1, maximum: 20, unit: 'levels', presets: [2, 3, 4, 5, 6, 8, 10], section: 'runtime',
|
|
69
|
+
label: { default: '主题树最大层级', translations: { zh: '主题树最大层级', en: 'Max topic depth' } },
|
|
70
|
+
description: { default: '主题树允许嵌套的最多层数。', translations: { zh: '主题树允许嵌套的最多层数。', en: 'Maximum nesting depth allowed in the topic tree.' } },
|
|
71
|
+
}] }));
|
|
72
|
+
ctx.effect(config.declare({ scope: 'settings', owner: 'core', fields: [{
|
|
73
|
+
key: KEEP_DAYS, kind: 'number', default: 30, minimum: 0, maximum: 365, unit: 'days', presets: [0, 7, 14, 30, 60, 90], section: 'trash',
|
|
74
|
+
label: { default: '垃圾桶保留时长', translations: { zh: '垃圾桶保留时长', en: 'Trash retention' } },
|
|
75
|
+
description: { default: '主题移入垃圾桶后保留的天数,到期后彻底删除;选「仅手动清理」则不自动删除。', translations: { zh: '主题移入垃圾桶后保留的天数,到期后彻底删除;选「仅手动清理」则不自动删除。', en: 'Days to keep deleted topics in Trash before automatic permanent removal; choose Manual only to disable auto-purge.' } },
|
|
76
|
+
}] }));
|
|
77
|
+
ctx.effect(config.declare({ scope: 'settings', owner: 'core', fields: [{
|
|
78
|
+
key: SUMMARY_MAX_CHARS, kind: 'number', default: 3000, minimum: 500, maximum: 20000, unit: 'chars', presets: [1500, 2000, 3000, 5000, 10000], section: 'runtime',
|
|
79
|
+
label: { default: '主题摘要字数上限', translations: { zh: '主题摘要字数上限', en: 'Max topic summary length' } },
|
|
80
|
+
description: { default: '主题摘要允许保存的最大字数上限;超出时拒绝保存。', translations: { zh: '主题摘要允许保存的最大字数上限;超出时拒绝保存。', en: 'Maximum character count allowed when saving a topic summary; longer summaries are rejected.' } },
|
|
81
|
+
}] }));
|
|
82
|
+
db.exec(`CREATE TABLE IF NOT EXISTS drafts (
|
|
83
|
+
topic_id TEXT PRIMARY KEY,
|
|
84
|
+
revision INTEGER NOT NULL,
|
|
85
|
+
text TEXT NOT NULL,
|
|
86
|
+
reply_to TEXT,
|
|
87
|
+
quote TEXT,
|
|
88
|
+
refs TEXT NOT NULL DEFAULT '[]',
|
|
89
|
+
updated_at INTEGER NOT NULL DEFAULT 0
|
|
90
|
+
);`);
|
|
91
|
+
const root = home.topicsDir;
|
|
92
|
+
const realRoot = realpathSync(root);
|
|
93
|
+
const logFile = join(root, 'tree-log.jsonl');
|
|
94
|
+
const dirOf = (id) => join(root, id);
|
|
95
|
+
const summaryFile = (id) => join(dirOf(id), 'summary.md');
|
|
96
|
+
const writeSummary = (id, text) => writeFileSync(summaryFile(id), `${text.trim()}\n`);
|
|
97
|
+
const all = new Map();
|
|
98
|
+
const owners = new Map();
|
|
99
|
+
const systems = new Set();
|
|
100
|
+
const trashChanges = new Map();
|
|
101
|
+
const ownedTopic = (topic, ownership) => {
|
|
102
|
+
if (ownership?.plugin)
|
|
103
|
+
owners.set(topic.id, ownership.plugin);
|
|
104
|
+
if (ownership?.system)
|
|
105
|
+
systems.add(topic.id);
|
|
106
|
+
Object.defineProperties(topic, {
|
|
107
|
+
plugin: { get: () => owners.get(topic.id), enumerable: true, configurable: false },
|
|
108
|
+
system: { get: () => systems.has(topic.id) ? true : undefined, enumerable: true, configurable: false },
|
|
109
|
+
});
|
|
110
|
+
return topic;
|
|
111
|
+
};
|
|
112
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
113
|
+
const file = join(root, entry.name, 'topic.json');
|
|
114
|
+
if (entry.isDirectory() && existsSync(file)) {
|
|
115
|
+
const { trashChangeId, plugin, system, ...stored } = JSON.parse(readFileSync(file, 'utf8'));
|
|
116
|
+
if (trashChangeId)
|
|
117
|
+
trashChanges.set(entry.name, trashChangeId);
|
|
118
|
+
all.set(entry.name, ownedTopic({ ...stored, caps: stored.caps ?? {}, view: stored.view ?? null, archivedAt: stored.archivedAt ?? null,
|
|
119
|
+
deletedAt: stored.deletedAt ?? null, id: entry.name, dir: dirOf(entry.name) }, { plugin, system: system === true }));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const index = makeTopicIndex(store, all.values());
|
|
123
|
+
const normalizeView = (raw) => {
|
|
124
|
+
if (raw === null)
|
|
125
|
+
return null;
|
|
126
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
127
|
+
throw new HttpError(400, '视图声明必须是对象或 null');
|
|
128
|
+
const obj = raw;
|
|
129
|
+
const main = typeof obj.main === 'string' ? obj.main.trim() : '';
|
|
130
|
+
if (!main)
|
|
131
|
+
throw new HttpError(400, '视图声明需要非空的 main 字段');
|
|
132
|
+
const out = { main };
|
|
133
|
+
if (obj.panel !== undefined && obj.panel !== null && obj.panel !== '') {
|
|
134
|
+
if (typeof obj.panel !== 'string')
|
|
135
|
+
throw new HttpError(400, '视图声明的 panel 必须是字符串');
|
|
136
|
+
out.panel = obj.panel.trim();
|
|
137
|
+
}
|
|
138
|
+
if (obj.preset !== undefined && obj.preset !== null && obj.preset !== '') {
|
|
139
|
+
if (typeof obj.preset !== 'string')
|
|
140
|
+
throw new HttpError(400, '视图声明的 preset 必须是字符串');
|
|
141
|
+
out.preset = obj.preset.trim();
|
|
142
|
+
}
|
|
143
|
+
if (obj.tabs !== undefined) {
|
|
144
|
+
if (!Array.isArray(obj.tabs) || obj.tabs.some((t) => typeof t !== 'string' || !t.trim()))
|
|
145
|
+
throw new HttpError(400, '视图声明的 tabs 必须是字符串数组');
|
|
146
|
+
out.tabs = obj.tabs.map((t) => t.trim());
|
|
147
|
+
}
|
|
148
|
+
if (obj.args !== undefined) {
|
|
149
|
+
if (!obj.args || typeof obj.args !== 'object' || Array.isArray(obj.args))
|
|
150
|
+
throw new HttpError(400, '视图声明的 args 必须是键值对象');
|
|
151
|
+
const cleanArgs = {};
|
|
152
|
+
for (const [k, v] of Object.entries(obj.args)) {
|
|
153
|
+
if (typeof v !== 'string' && typeof v !== 'number' && typeof v !== 'boolean')
|
|
154
|
+
throw new HttpError(400, `视图参数 ${k} 只支持字符串、数字或布尔值`);
|
|
155
|
+
cleanArgs[k] = v;
|
|
156
|
+
}
|
|
157
|
+
out.args = cleanArgs;
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
};
|
|
161
|
+
const save = (topic, trashChangeId = trashChanges.get(topic.id)) => {
|
|
162
|
+
const { dir: _dir, ...stored } = topic;
|
|
163
|
+
writeFileSync(join(topic.dir, 'topic.json'), `${JSON.stringify({ ...stored, trashChangeId }, null, 2)}\n`);
|
|
164
|
+
index.write(topic);
|
|
165
|
+
};
|
|
166
|
+
const live = (topic) => !!topic && topic.deletedAt === null;
|
|
167
|
+
const pathOf = (id, includeDeleted = false) => {
|
|
168
|
+
const out = [];
|
|
169
|
+
for (let at = all.get(id); at && (includeDeleted || live(at)) && !out.includes(at); at = at.parentId ? all.get(at.parentId) : undefined)
|
|
170
|
+
out.unshift(at);
|
|
171
|
+
return out;
|
|
172
|
+
};
|
|
173
|
+
const topicPlugins = () => ctx.runtime.list().filter(({ topicSwitch }) => topicSwitch);
|
|
174
|
+
const status = makeTopicStatus(ctx, pathOf);
|
|
175
|
+
const under = (parent, includeDeleted = false) => index.descendants(parent, includeDeleted).map((id) => all.get(id));
|
|
176
|
+
const pathText = (id) => pathOf(id).map((t) => t.title).join(' › ');
|
|
177
|
+
const describeRef = (topic, note) => ({
|
|
178
|
+
topicId: topic.id, title: topic.title, path: pathText(topic.id), summary: topics.summary(topic.id), archivedAt: topic.archivedAt,
|
|
179
|
+
...(note ? { note } : {}),
|
|
180
|
+
});
|
|
181
|
+
let treeVersion = 0;
|
|
182
|
+
const announce = (topic, { before, trashChanged, prior } = {}) => {
|
|
183
|
+
const caps = before?.caps;
|
|
184
|
+
const capsChanged = caps && [...new Set([...Object.keys(caps), ...Object.keys(topic.caps)])]
|
|
185
|
+
.filter((id) => caps[id] !== topic.caps[id]).map((id) => ({ id, on: topic.caps[id] ?? null }));
|
|
186
|
+
const refsBefore = before?.refs?.filter(isDirRef);
|
|
187
|
+
const tree = prior !== undefined && {
|
|
188
|
+
version: ++treeVersion, oldParent: prior?.parentId ?? null,
|
|
189
|
+
totalDelta: Number(live(topic)) - Number(prior?.deletedAt === null),
|
|
190
|
+
archiveDelta: Number(live(topic) && topic.archivedAt !== null) - Number(prior?.deletedAt === null && prior.archivedAt !== null),
|
|
191
|
+
hasChildren: live(topic) && topic.archivedAt === null && index.hasChildren(topic.id),
|
|
192
|
+
parents: [...new Set([prior?.parentId, topic.parentId])].flatMap((id) => id ? [{ id, hasChildren: index.hasChildren(id) }] : []),
|
|
193
|
+
};
|
|
194
|
+
ctx.runtime.emit('topic.updated', {
|
|
195
|
+
topic, ...(capsChanged?.length ? { capsChanged } : {}), ...(refsBefore ? { refsBefore } : {}), ...(trashChanged ? { trashChanged } : {}),
|
|
196
|
+
...(tree ? { tree } : {}),
|
|
197
|
+
});
|
|
198
|
+
return topic;
|
|
199
|
+
};
|
|
200
|
+
const record = (topicId, before, after, by, affected, id = newId('chg')) => {
|
|
201
|
+
const entry = { id, at: Date.now(), by, topicId, before, after, affected };
|
|
202
|
+
appendFileSync(logFile, `${JSON.stringify(entry)}\n`);
|
|
203
|
+
return entry;
|
|
204
|
+
};
|
|
205
|
+
const maxDepth = () => config.get('settings', 'topics.maxDepth');
|
|
206
|
+
/** How many levels a topic takes with everything under it. */
|
|
207
|
+
const heightOf = (id) => index.children(id).reduce((height, child) => Math.max(height, 1 + heightOf(child)), 1);
|
|
208
|
+
const fits = (parentId, height) => {
|
|
209
|
+
if (pathOf(parentId).length + height > maxDepth())
|
|
210
|
+
throw new Error(`主题树最多 ${maxDepth()} 层,这里放不下。换一个浅一点的位置;层数在系统配置的 topics.maxDepth。`);
|
|
211
|
+
};
|
|
212
|
+
const checkParent = (id) => {
|
|
213
|
+
const parent = all.get(id);
|
|
214
|
+
if (!live(parent))
|
|
215
|
+
throw new Error('没有这个父主题');
|
|
216
|
+
if (parent.archivedAt !== null)
|
|
217
|
+
throw new HttpError(409, `父主题「${parent.title}」已归档`);
|
|
218
|
+
};
|
|
219
|
+
/** Apply a change to one topic and return the resulting log entry. */
|
|
220
|
+
const change = (topic, rawPatch, by) => {
|
|
221
|
+
const patch = rawPatch.view !== undefined ? { ...rawPatch, view: normalizeView(rawPatch.view) } : rawPatch;
|
|
222
|
+
if (patch.parentId !== undefined && patch.parentId !== null) {
|
|
223
|
+
checkParent(patch.parentId);
|
|
224
|
+
if (pathOf(patch.parentId).some((x) => x.id === topic.id))
|
|
225
|
+
throw new Error('不能把一个主题放到它自己的下面');
|
|
226
|
+
fits(patch.parentId, heightOf(topic.id));
|
|
227
|
+
}
|
|
228
|
+
const before = {};
|
|
229
|
+
const after = {};
|
|
230
|
+
for (const key of Object.keys(patch)) {
|
|
231
|
+
if (patch[key] === undefined || JSON.stringify(patch[key]) === JSON.stringify(topic[key]))
|
|
232
|
+
continue;
|
|
233
|
+
before[key] = topic[key] ?? null;
|
|
234
|
+
after[key] = patch[key];
|
|
235
|
+
}
|
|
236
|
+
if (!Object.keys(after).length)
|
|
237
|
+
return;
|
|
238
|
+
const orderChanged = ['title', 'tags', 'parentId', 'pinned'].some((key) => key in after);
|
|
239
|
+
const prior = orderChanged ? ownedTopic({ ...topic }) : undefined;
|
|
240
|
+
const commit = () => {
|
|
241
|
+
save(ownedTopic({ ...topic, ...after }));
|
|
242
|
+
Object.assign(topic, after);
|
|
243
|
+
const entry = record(topic.id, before, after, by);
|
|
244
|
+
announce(topic, { before, prior });
|
|
245
|
+
return entry;
|
|
246
|
+
};
|
|
247
|
+
return after.parentId === undefined ? commit() : status.topology([], topic.id, commit);
|
|
248
|
+
};
|
|
249
|
+
const need = (id) => {
|
|
250
|
+
const topic = all.get(id);
|
|
251
|
+
if (!topic)
|
|
252
|
+
throw new Error(`没有这个主题:${id}`);
|
|
253
|
+
return topic;
|
|
254
|
+
};
|
|
255
|
+
const readLog = () => (existsSync(logFile) ? readFileSync(logFile, 'utf8').split('\n').filter(Boolean).map((line) => JSON.parse(line)) : []);
|
|
256
|
+
const needActive = (id) => {
|
|
257
|
+
const topic = need(id);
|
|
258
|
+
if (!live(topic))
|
|
259
|
+
throw new HttpError(404, `没有这个主题:${id}`);
|
|
260
|
+
if (topic.archivedAt !== null)
|
|
261
|
+
throw new HttpError(409, '主题已归档');
|
|
262
|
+
return topic;
|
|
263
|
+
};
|
|
264
|
+
const needDeleted = (id) => {
|
|
265
|
+
const topic = all.get(id);
|
|
266
|
+
if (!topic)
|
|
267
|
+
throw new HttpError(404, '垃圾桶里没有这个主题');
|
|
268
|
+
if (topic.deletedAt === null)
|
|
269
|
+
throw new HttpError(409, '只能对垃圾桶里的主题执行此操作');
|
|
270
|
+
return topic;
|
|
271
|
+
};
|
|
272
|
+
// Keep references already stored on the topic, including directories temporarily offline and deleted
|
|
273
|
+
// targets that may be restored. Validate new inputs even when they name an existing reference.
|
|
274
|
+
const normalizeRefs = (id, refs, previous) => {
|
|
275
|
+
const kept = new Set(previous);
|
|
276
|
+
const previousDirs = previous.filter(isDirRef);
|
|
277
|
+
const keptDirs = new Map(previousDirs.map((ref) => [refKey(ref), ref]));
|
|
278
|
+
const edits = new Map(previousDirs.map((ref) => [ref.refId, ref]));
|
|
279
|
+
const seen = new Set();
|
|
280
|
+
const seenIds = new Set();
|
|
281
|
+
const normalized = [];
|
|
282
|
+
for (const ref of refs) {
|
|
283
|
+
const key = refKey(ref);
|
|
284
|
+
if (seen.has(key))
|
|
285
|
+
continue;
|
|
286
|
+
seen.add(key);
|
|
287
|
+
if (kept.has(ref)) {
|
|
288
|
+
const stored = ref;
|
|
289
|
+
if (isDirRef(stored))
|
|
290
|
+
seenIds.add(stored.refId);
|
|
291
|
+
normalized.push(stored);
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const note = ref.note?.trim();
|
|
295
|
+
if (isDirRef(ref)) {
|
|
296
|
+
const path = refPath(ref.path);
|
|
297
|
+
checkDirectory(path);
|
|
298
|
+
const old = ref.refId ? edits.get(ref.refId) : keptDirs.get(key);
|
|
299
|
+
// A directory keeps its number in this topic when it is removed and added again, so earlier res:ref links still open.
|
|
300
|
+
const derived = `ref_${createHash('sha256').update(`${id}\0${path}`).digest('hex').slice(0, 16)}`;
|
|
301
|
+
const refId = old?.refId ?? (seenIds.has(derived) || previousDirs.some((one) => one.refId === derived) ? newId('ref') : derived);
|
|
302
|
+
if (seenIds.has(refId))
|
|
303
|
+
throw new Error('不能在同一主题重复使用目录引用编号');
|
|
304
|
+
seenIds.add(refId);
|
|
305
|
+
normalized.push({ refId, path, ...(note ? { note } : {}) });
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
const topicId = ref.topicId.trim();
|
|
309
|
+
if (topicId === id)
|
|
310
|
+
throw new Error('不能引用主题自身');
|
|
311
|
+
if (!live(all.get(topicId)))
|
|
312
|
+
throw new Error(`没有这个主题:${topicId}`);
|
|
313
|
+
normalized.push({ topicId, ...(note ? { note } : {}) });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return normalized;
|
|
317
|
+
};
|
|
318
|
+
const currentArchives = (log = readLog()) => {
|
|
319
|
+
const latest = new Map();
|
|
320
|
+
for (const change of log) {
|
|
321
|
+
if (change.affected && change.after.archivedAt !== undefined) {
|
|
322
|
+
for (const id of change.affected)
|
|
323
|
+
latest.set(id, change);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return latest;
|
|
327
|
+
};
|
|
328
|
+
const applyArchive = (topic, affected, archivedAt, by) => {
|
|
329
|
+
return status.topology(affected.map((t) => t.id), undefined, () => {
|
|
330
|
+
const before = topic.archivedAt;
|
|
331
|
+
const prior = new Map(affected.map((one) => [one.id, ownedTopic({ ...one })]));
|
|
332
|
+
// Write ancestors first when restoring, descendants first when archiving. Notify after the batch.
|
|
333
|
+
for (const t of archivedAt === null ? affected : affected.slice().reverse()) {
|
|
334
|
+
save(ownedTopic({ ...t, archivedAt }));
|
|
335
|
+
t.archivedAt = archivedAt;
|
|
336
|
+
}
|
|
337
|
+
const entry = record(topic.id, { archivedAt: before }, { archivedAt }, by, affected.map((t) => t.id));
|
|
338
|
+
affected.forEach((topic) => announce(topic, { prior: prior.get(topic.id) }));
|
|
339
|
+
return entry;
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
const checkUnarchive = (affected) => {
|
|
343
|
+
const ids = new Set(affected.map((t) => t.id));
|
|
344
|
+
for (const t of affected) {
|
|
345
|
+
const above = pathOf(t.id).slice(0, -1).find((p) => p.archivedAt !== null && !ids.has(p.id));
|
|
346
|
+
if (above)
|
|
347
|
+
throw new HttpError(409, `上级主题「${above.title}」已归档,请先取消它的归档`);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
const sameDeletion = (topic, other) => other.deletedAt !== null && trashChanges.get(topic.id) === trashChanges.get(other.id);
|
|
351
|
+
const deletionBatch = (root) => [root, ...under(root.id, true).filter((t) => sameDeletion(root, t))];
|
|
352
|
+
const applyDeletion = (topic, affected, deletedAt, by, parentId = topic.parentId) => {
|
|
353
|
+
const moved = parentId !== topic.parentId;
|
|
354
|
+
return status.topology(affected.map((t) => t.id), moved ? topic.id : undefined, () => {
|
|
355
|
+
const before = { deletedAt: topic.deletedAt, ...(moved ? { parentId: topic.parentId } : {}) };
|
|
356
|
+
const after = { deletedAt, ...(moved ? { parentId } : {}) };
|
|
357
|
+
const changeId = newId('chg');
|
|
358
|
+
const prior = new Map(affected.map((one) => [one.id, ownedTopic({ ...one })]));
|
|
359
|
+
// The root is written last on deletion, and first on restoration.
|
|
360
|
+
for (const item of deletedAt === null ? affected : affected.slice().reverse()) {
|
|
361
|
+
const patch = item === topic ? after : { deletedAt };
|
|
362
|
+
save(ownedTopic({ ...item, ...patch }), changeId);
|
|
363
|
+
Object.assign(item, patch);
|
|
364
|
+
trashChanges.set(item.id, changeId);
|
|
365
|
+
}
|
|
366
|
+
const ids = affected.map((t) => t.id);
|
|
367
|
+
const entry = record(topic.id, before, after, by, ids, changeId);
|
|
368
|
+
for (const item of affected)
|
|
369
|
+
announce(item, { prior: prior.get(item.id), ...(item === topic ? { trashChanged: ids } : {}) });
|
|
370
|
+
return entry;
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
const restoreBatch = (topic, affected, by) => {
|
|
374
|
+
let reason;
|
|
375
|
+
if (topic.parentId) {
|
|
376
|
+
const parent = all.get(topic.parentId);
|
|
377
|
+
if (!parent)
|
|
378
|
+
reason = '原父主题已彻底删除';
|
|
379
|
+
else if (parent.deletedAt !== null)
|
|
380
|
+
reason = '原父主题仍在垃圾桶';
|
|
381
|
+
else if (topic.archivedAt === null && parent.archivedAt !== null)
|
|
382
|
+
reason = '原父主题已归档';
|
|
383
|
+
else {
|
|
384
|
+
const depth = pathOf(topic.id, true).length;
|
|
385
|
+
const height = Math.max(...affected.map((t) => pathOf(t.id, true).length - depth + 1));
|
|
386
|
+
if (pathOf(parent.id).length + height > maxDepth())
|
|
387
|
+
reason = `原位置超过主题树的 ${maxDepth()} 层上限`;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
applyDeletion(topic, affected, null, by, reason ? null : topic.parentId);
|
|
391
|
+
return { topic, restored: affected.map((t) => t.id), movedToRoot: !!reason, ...(reason ? { reason } : {}) };
|
|
392
|
+
};
|
|
393
|
+
const expiry = (deletedAt, days) => days > 0 ? deletedAt + days * DAY : null;
|
|
394
|
+
const trashRows = (deleted) => {
|
|
395
|
+
if (!deleted.length)
|
|
396
|
+
return [];
|
|
397
|
+
const counts = new Map();
|
|
398
|
+
const rows = [];
|
|
399
|
+
const days = config.get('settings', KEEP_DAYS);
|
|
400
|
+
for (const topic of deleted) {
|
|
401
|
+
const { id, title, parentId, deletedAt, archivedAt, tags } = topic;
|
|
402
|
+
const path = pathOf(id, true);
|
|
403
|
+
const batch = path.filter((t) => sameDeletion(topic, t));
|
|
404
|
+
for (const parent of batch.slice(0, -1))
|
|
405
|
+
counts.set(parent.id, (counts.get(parent.id) ?? 0) + 1);
|
|
406
|
+
rows.push({
|
|
407
|
+
id, title, parentId, deletedAt, archivedAt, tags, originalPath: path.map((t) => t.title).join(' › '),
|
|
408
|
+
batchRoot: batch[0].id, summary: topics.summary(id), expiresAt: expiry(deletedAt, days),
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
return rows.map((row) => ({ ...row, underCount: counts.get(row.id) ?? 0 }));
|
|
412
|
+
};
|
|
413
|
+
const trashed = () => [...all.values()].filter((t) => t.deletedAt !== null);
|
|
414
|
+
const erase = (items) => {
|
|
415
|
+
// Topic directories are flat, and a replaced symlink must not point a purge elsewhere.
|
|
416
|
+
for (const item of items) {
|
|
417
|
+
const stat = lstatSync(item.dir, { throwIfNoEntry: false });
|
|
418
|
+
if (dirname(resolve(item.dir)) !== root || (stat && (!stat.isDirectory() || dirname(realpathSync(item.dir)) !== realRoot))) {
|
|
419
|
+
throw new HttpError(409, `不能清理主题目录:${item.id}`);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
const removed = [];
|
|
423
|
+
try {
|
|
424
|
+
const ordered = items.map((item) => ({ item, depth: pathOf(item.id, true).length })).sort((a, b) => b.depth - a.depth);
|
|
425
|
+
for (const { item } of ordered) {
|
|
426
|
+
rmSync(item.dir, { recursive: true, force: true });
|
|
427
|
+
all.delete(item.id);
|
|
428
|
+
owners.delete(item.id);
|
|
429
|
+
systems.delete(item.id);
|
|
430
|
+
index.erase(item.id);
|
|
431
|
+
trashChanges.delete(item.id);
|
|
432
|
+
removed.push(item.id);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
finally {
|
|
436
|
+
if (removed.length)
|
|
437
|
+
ctx.runtime.emit('topic.purged', { topicIds: removed });
|
|
438
|
+
}
|
|
439
|
+
return removed;
|
|
440
|
+
};
|
|
441
|
+
const undoPlan = (id) => {
|
|
442
|
+
const history = readLog();
|
|
443
|
+
const entry = history.find((change) => change.id === id);
|
|
444
|
+
if (!entry)
|
|
445
|
+
throw new Error('没有这条改动记录');
|
|
446
|
+
const topic = need(entry.topicId);
|
|
447
|
+
let affected = [topic];
|
|
448
|
+
if (entry.after.deletedAt != null) {
|
|
449
|
+
if (trashChanges.get(topic.id) !== id) {
|
|
450
|
+
throw new HttpError(409, '删除状态已发生变化,不能撤销这条记录');
|
|
451
|
+
}
|
|
452
|
+
affected = deletionBatch(topic);
|
|
453
|
+
}
|
|
454
|
+
else if (entry.before.deletedAt != null) {
|
|
455
|
+
affected = [topic, ...under(topic.id)];
|
|
456
|
+
if (!live(topic) || (entry.affected && (affected.length !== entry.affected.length || affected.some((t) => !entry.affected.includes(t.id))
|
|
457
|
+
|| trashChanges.get(topic.id) !== id)))
|
|
458
|
+
throw new HttpError(409, '恢复的主题或子主题已发生变化,不能撤销这条记录');
|
|
459
|
+
}
|
|
460
|
+
else if (entry.affected) {
|
|
461
|
+
affected = entry.affected.map(need);
|
|
462
|
+
}
|
|
463
|
+
return { entry, topic, affected, history };
|
|
464
|
+
};
|
|
465
|
+
const createTopic = (input, by, ownership) => {
|
|
466
|
+
const id = newId('top');
|
|
467
|
+
const now = Date.now();
|
|
468
|
+
const title = input.title?.trim() ?? '';
|
|
469
|
+
const refs = normalizeRefs(id, input.refs ?? [], []);
|
|
470
|
+
const topic = {
|
|
471
|
+
id, title: title || UNTITLED, parentId: null, tags: input.tags ?? [], refs,
|
|
472
|
+
pinned: input.pinned ?? false, caps: {}, view: input.view ? normalizeView(input.view) : null, dir: dirOf(id), createdAt: now, lastActiveAt: now, archivedAt: null, deletedAt: null,
|
|
473
|
+
...(input.defaultMember ? { defaultMember: input.defaultMember } : {}),
|
|
474
|
+
};
|
|
475
|
+
if (input.parentId) {
|
|
476
|
+
checkParent(input.parentId);
|
|
477
|
+
fits(input.parentId, 1);
|
|
478
|
+
topic.parentId = input.parentId;
|
|
479
|
+
}
|
|
480
|
+
for (const sub of ['artifacts', 'tmp'])
|
|
481
|
+
mkdirSync(join(topic.dir, sub), { recursive: true });
|
|
482
|
+
writeSummary(id, input.summary ?? '');
|
|
483
|
+
ownedTopic(topic, ownership);
|
|
484
|
+
save(topic);
|
|
485
|
+
all.set(id, topic);
|
|
486
|
+
record(id, { deletedAt: now }, { deletedAt: null }, by);
|
|
487
|
+
return announce(topic, { prior: null, ...(refs.some(isDirRef) ? { before: { refs: [] } } : {}) });
|
|
488
|
+
};
|
|
489
|
+
const topics = {
|
|
490
|
+
list: () => [...all.values()].filter(live).sort((a, b) => Number(b.pinned) - Number(a.pinned) || b.lastActiveAt - a.lastActiveAt),
|
|
491
|
+
all: () => [...all.values()],
|
|
492
|
+
get: (id) => all.get(id),
|
|
493
|
+
children: (id) => index.children(id).map((child) => all.get(child)),
|
|
494
|
+
hasActiveChildren: index.hasChildren,
|
|
495
|
+
navigationCounts: index.counts,
|
|
496
|
+
tags: index.tags,
|
|
497
|
+
page: (view, parentId, q, tag, after, limit) => index.page(view, parentId, q, tag, after, limit).map((id) => all.get(id)),
|
|
498
|
+
status,
|
|
499
|
+
treeVersion: () => treeVersion,
|
|
500
|
+
path: pathOf,
|
|
501
|
+
scopeIds(id, scope) {
|
|
502
|
+
if (scope === 'all')
|
|
503
|
+
return topics.list().map((topic) => topic.id);
|
|
504
|
+
if (scope === 'lineage')
|
|
505
|
+
return pathOf(id).map((topic) => topic.id);
|
|
506
|
+
if (!live(all.get(id)))
|
|
507
|
+
return [];
|
|
508
|
+
return scope === 'subtree' ? [id, ...index.descendants(id, false)] : [id];
|
|
509
|
+
},
|
|
510
|
+
describe(id) {
|
|
511
|
+
const topic = need(id);
|
|
512
|
+
const path = pathOf(id);
|
|
513
|
+
return {
|
|
514
|
+
topic, path: path.map((t) => ({ id: t.id, title: t.title })), level: path.length, maxDepth: maxDepth(), summary: topics.summary(id),
|
|
515
|
+
above: path.slice(0, -1).map((t) => ({ id: t.id, title: t.title, dir: t.dir, summary: topics.summary(t.id), archivedAt: t.archivedAt })),
|
|
516
|
+
children: topics.children(id).sort((a, b) => a.createdAt - b.createdAt).map((t) => ({ id: t.id, title: t.title })), refs: topics.refsOf(id),
|
|
517
|
+
topicRefs: topics.topicRefsOf(id), referencedBy: topics.referencedBy(id),
|
|
518
|
+
};
|
|
519
|
+
},
|
|
520
|
+
uses(id, capId, fallback) {
|
|
521
|
+
const said = pathOf(id).reverse().find((t) => capId in t.caps);
|
|
522
|
+
return said ? said.caps[capId] : fallback;
|
|
523
|
+
},
|
|
524
|
+
pluginEnabled(topicId, plugin) {
|
|
525
|
+
return !topicId || !plugin || topics.uses(topicId, `plugin:${plugin}`, true)
|
|
526
|
+
|| !topicPlugins().some(({ name }) => name === plugin);
|
|
527
|
+
},
|
|
528
|
+
pluginCaps() {
|
|
529
|
+
return topicPlugins().map(({ name, title, about }) => ({
|
|
530
|
+
id: `plugin:${name}`, title: title ?? name, about, group: '插件', default: true,
|
|
531
|
+
}));
|
|
532
|
+
},
|
|
533
|
+
refsOf(id) {
|
|
534
|
+
const seen = new Set();
|
|
535
|
+
return pathOf(id).reverse().flatMap((t) => t.refs.filter(isDirRef)).filter((r) => !seen.has(r.path) && seen.add(r.path));
|
|
536
|
+
},
|
|
537
|
+
topicRefsOf(id) {
|
|
538
|
+
return need(id).refs.filter(isTopicRef).flatMap((ref) => {
|
|
539
|
+
const target = all.get(ref.topicId);
|
|
540
|
+
return live(target) ? [describeRef(target, ref.note)] : [];
|
|
541
|
+
});
|
|
542
|
+
},
|
|
543
|
+
referencedBy(id) {
|
|
544
|
+
return topics.list().flatMap((topic) => {
|
|
545
|
+
const ref = topic.refs.filter(isTopicRef).find((r) => r.topicId === id);
|
|
546
|
+
return ref ? [describeRef(topic, ref.note)] : [];
|
|
547
|
+
});
|
|
548
|
+
},
|
|
549
|
+
create: (input, by = 'owner') => createTopic(input, by),
|
|
550
|
+
update(id, patch, by) {
|
|
551
|
+
return topics.updateWithChange(id, patch, by).topic;
|
|
552
|
+
},
|
|
553
|
+
updateWithChange(id, input, by = 'owner') {
|
|
554
|
+
const topic = needActive(id);
|
|
555
|
+
const source = input;
|
|
556
|
+
const picked = {};
|
|
557
|
+
for (const key of TOPIC_PATCH_KEYS)
|
|
558
|
+
if (Object.hasOwn(source, key))
|
|
559
|
+
picked[key] = source[key];
|
|
560
|
+
const { refs, ...patch } = picked;
|
|
561
|
+
const title = patch.title?.trim();
|
|
562
|
+
const who = { ...(patch.members !== undefined ? { members: [...new Set((patch.members ?? []).map(String))] } : {}), ...(patch.defaultMember !== undefined ? { defaultMember: String(patch.defaultMember ?? '') } : {}) };
|
|
563
|
+
const edited = change(topic, {
|
|
564
|
+
...patch, ...who, ...(title ? { title } : {}),
|
|
565
|
+
...(refs ? { refs: normalizeRefs(id, refs, topic.refs) } : {}),
|
|
566
|
+
}, by);
|
|
567
|
+
return { topic, change: edited };
|
|
568
|
+
},
|
|
569
|
+
isArchived(id) {
|
|
570
|
+
const topic = all.get(id);
|
|
571
|
+
return live(topic) && topic.archivedAt !== null;
|
|
572
|
+
},
|
|
573
|
+
archiveTargets(id) {
|
|
574
|
+
const topic = need(id);
|
|
575
|
+
if (!live(topic))
|
|
576
|
+
throw new HttpError(404, `没有这个主题:${id}`);
|
|
577
|
+
return topic.archivedAt === null ? [topic, ...under(id).filter((t) => t.archivedAt === null)] : [];
|
|
578
|
+
},
|
|
579
|
+
archive(id, by = 'owner') {
|
|
580
|
+
const affected = topics.archiveTargets(id);
|
|
581
|
+
if (affected.length)
|
|
582
|
+
return applyArchive(affected[0], affected, Date.now(), by);
|
|
583
|
+
},
|
|
584
|
+
unarchive(id, by = 'owner') {
|
|
585
|
+
const topic = need(id);
|
|
586
|
+
if (!live(topic))
|
|
587
|
+
throw new HttpError(404, `没有这个主题:${id}`);
|
|
588
|
+
if (topic.archivedAt === null)
|
|
589
|
+
return;
|
|
590
|
+
const latest = currentArchives();
|
|
591
|
+
const batch = latest.get(id);
|
|
592
|
+
const affected = [topic, ...under(id).filter((t) => batch && t.archivedAt === topic.archivedAt && latest.get(t.id) === batch)];
|
|
593
|
+
checkUnarchive(affected);
|
|
594
|
+
return applyArchive(topic, affected, null, by);
|
|
595
|
+
},
|
|
596
|
+
remove(id, by = 'owner') {
|
|
597
|
+
const topic = need(id);
|
|
598
|
+
if (!live(topic))
|
|
599
|
+
throw new HttpError(404, `没有这个主题:${id}`);
|
|
600
|
+
return applyDeletion(topic, [topic, ...under(id)], Date.now(), by);
|
|
601
|
+
},
|
|
602
|
+
trash: () => trashRows(trashed().sort((a, b) => b.deletedAt - a.deletedAt)),
|
|
603
|
+
restore(id, by = 'owner') {
|
|
604
|
+
const topic = needDeleted(id);
|
|
605
|
+
return restoreBatch(topic, deletionBatch(topic), by);
|
|
606
|
+
},
|
|
607
|
+
purge(id) {
|
|
608
|
+
return erase([needDeleted(id), ...under(id, true).filter((t) => t.deletedAt !== null)]);
|
|
609
|
+
},
|
|
610
|
+
emptyTrash: () => erase(trashed()),
|
|
611
|
+
purgeExpired() {
|
|
612
|
+
const days = config.get('settings', KEEP_DAYS);
|
|
613
|
+
if (days === 0)
|
|
614
|
+
return;
|
|
615
|
+
const now = Date.now();
|
|
616
|
+
const expired = trashed().filter((t) => expiry(t.deletedAt, days) <= now);
|
|
617
|
+
if (!expired.length)
|
|
618
|
+
return;
|
|
619
|
+
try {
|
|
620
|
+
const ids = erase(expired);
|
|
621
|
+
log('topics', `自动清理了 ${ids.length} 个到期主题:${ids.join('、')}`);
|
|
622
|
+
}
|
|
623
|
+
catch (error) {
|
|
624
|
+
log('topics', '清理到期主题失败', error);
|
|
625
|
+
}
|
|
626
|
+
},
|
|
627
|
+
touch(id) {
|
|
628
|
+
const topic = need(id);
|
|
629
|
+
topic.lastActiveAt = Date.now();
|
|
630
|
+
save(topic);
|
|
631
|
+
announce(topic);
|
|
632
|
+
},
|
|
633
|
+
summary(id) {
|
|
634
|
+
const file = summaryFile(id);
|
|
635
|
+
return existsSync(file) ? readFileSync(file, 'utf8').trim() : '';
|
|
636
|
+
},
|
|
637
|
+
setSummary(id, text, by = 'owner') {
|
|
638
|
+
const topic = needActive(id);
|
|
639
|
+
writeSummary(id, text);
|
|
640
|
+
record(id, {}, {}, by);
|
|
641
|
+
announce(topic);
|
|
642
|
+
},
|
|
643
|
+
log: (limit = 50) => readLog().slice(-limit).reverse(),
|
|
644
|
+
undoTargets: (id) => undoPlan(id).affected.map((t) => t.id),
|
|
645
|
+
undo(changeId, by = 'owner') {
|
|
646
|
+
const { entry, topic, affected, history } = undoPlan(changeId);
|
|
647
|
+
if (entry.before.archivedAt !== undefined && entry.after.archivedAt !== undefined) {
|
|
648
|
+
const latest = currentArchives(history);
|
|
649
|
+
if (affected.some((t) => !live(t) || t.archivedAt !== entry.after.archivedAt || latest.get(t.id) !== entry)) {
|
|
650
|
+
throw new HttpError(409, '归档状态已发生变化,不能撤销这条记录');
|
|
651
|
+
}
|
|
652
|
+
if (entry.before.archivedAt === null)
|
|
653
|
+
checkUnarchive(affected);
|
|
654
|
+
else {
|
|
655
|
+
const ids = new Set(affected.map((t) => t.id));
|
|
656
|
+
if (affected.some((t) => topics.children(t.id).some((child) => child.archivedAt === null && !ids.has(child.id)))) {
|
|
657
|
+
throw new HttpError(409, '主题下已有新的活跃子主题,不能撤销这条记录');
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
applyArchive(topic, affected, entry.before.archivedAt, by);
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
if (entry.after.deletedAt != null)
|
|
664
|
+
restoreBatch(topic, affected, by);
|
|
665
|
+
// Undo creation or a restoration by returning its subtree to the trash.
|
|
666
|
+
else if (entry.before.deletedAt != null)
|
|
667
|
+
applyDeletion(topic, affected, Date.now(), by);
|
|
668
|
+
else
|
|
669
|
+
change(needActive(topic.id), entry.before, by);
|
|
670
|
+
},
|
|
671
|
+
drafts: () => db.prepare("SELECT topic_id, revision, text, reply_to, quote, refs, updated_at FROM drafts WHERE text <> '' OR refs <> '[]' ORDER BY updated_at DESC").all()
|
|
672
|
+
.filter((r) => live(all.get(r.topic_id)))
|
|
673
|
+
.map((r) => ({
|
|
674
|
+
topicId: r.topic_id,
|
|
675
|
+
revision: r.revision,
|
|
676
|
+
text: r.text,
|
|
677
|
+
replyTo: r.reply_to || null,
|
|
678
|
+
quote: r.quote || null,
|
|
679
|
+
refs: JSON.parse(r.refs || '[]'),
|
|
680
|
+
updatedAt: r.updated_at || 0,
|
|
681
|
+
})),
|
|
682
|
+
draftsOf: (id) => db.prepare("SELECT topic_id, revision, text, reply_to, quote, refs, updated_at FROM drafts WHERE topic_id = ? AND (text <> '' OR refs <> '[]')").all(id)
|
|
683
|
+
.map((r) => ({
|
|
684
|
+
topicId: r.topic_id,
|
|
685
|
+
revision: r.revision,
|
|
686
|
+
text: r.text,
|
|
687
|
+
replyTo: r.reply_to || null,
|
|
688
|
+
quote: r.quote || null,
|
|
689
|
+
refs: JSON.parse(r.refs || '[]'),
|
|
690
|
+
updatedAt: r.updated_at || 0,
|
|
691
|
+
})),
|
|
692
|
+
draft(id) {
|
|
693
|
+
const row = db.prepare('SELECT revision, text, reply_to, quote, refs FROM drafts WHERE topic_id = ?').get(id);
|
|
694
|
+
return row
|
|
695
|
+
? {
|
|
696
|
+
revision: row.revision,
|
|
697
|
+
text: row.text,
|
|
698
|
+
replyTo: row.reply_to || null,
|
|
699
|
+
quote: row.quote || null,
|
|
700
|
+
refs: JSON.parse(row.refs || '[]'),
|
|
701
|
+
}
|
|
702
|
+
: { revision: 0, text: '', replyTo: null, quote: null, refs: [] };
|
|
703
|
+
},
|
|
704
|
+
saveDraft(id, revision, text, opts = {}) {
|
|
705
|
+
const replyTo = opts.replyTo?.trim() || null;
|
|
706
|
+
const quote = opts.quote?.trim() || null;
|
|
707
|
+
if (replyTo && quote)
|
|
708
|
+
throw new HttpError(400, '草稿不能同时设置回复和引用目标');
|
|
709
|
+
const refs = opts.refs ?? [];
|
|
710
|
+
const stored = topics.draft(id);
|
|
711
|
+
if (stored.revision !== revision)
|
|
712
|
+
return { ...stored, conflict: true };
|
|
713
|
+
const now = Date.now();
|
|
714
|
+
db.prepare('INSERT INTO drafts (topic_id, revision, text, reply_to, quote, refs, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(topic_id) DO UPDATE SET revision = excluded.revision, text = excluded.text, reply_to = excluded.reply_to, quote = excluded.quote, refs = excluded.refs, updated_at = excluded.updated_at')
|
|
715
|
+
.run(id, revision + 1, text, replyTo, quote, JSON.stringify(refs), now);
|
|
716
|
+
const notify = () => {
|
|
717
|
+
push.send('drafts.changed', { topicId: id });
|
|
718
|
+
status.refresh('drafts', id);
|
|
719
|
+
};
|
|
720
|
+
if (opts.afterCommit)
|
|
721
|
+
opts.afterCommit(notify);
|
|
722
|
+
else
|
|
723
|
+
notify();
|
|
724
|
+
return { revision: revision + 1, text, replyTo, quote, refs };
|
|
725
|
+
},
|
|
726
|
+
clearDraft(id) {
|
|
727
|
+
const stored = topics.draft(id);
|
|
728
|
+
if (!stored.text && !stored.replyTo && !stored.quote && !stored.refs.length && stored.revision === 0)
|
|
729
|
+
return;
|
|
730
|
+
db.prepare('INSERT INTO drafts (topic_id, revision, text, reply_to, quote, refs, updated_at) VALUES (?, ?, ?, NULL, NULL, ?, ?) ON CONFLICT(topic_id) DO UPDATE SET revision = excluded.revision, text = excluded.text, reply_to = NULL, quote = NULL, refs = excluded.refs, updated_at = excluded.updated_at')
|
|
731
|
+
.run(id, stored.revision + 1, '', '[]', Date.now());
|
|
732
|
+
push.send('drafts.changed', { topicId: id });
|
|
733
|
+
status.refresh('drafts', id);
|
|
734
|
+
},
|
|
735
|
+
};
|
|
736
|
+
// The directories are authoritative, including after an interrupted purge.
|
|
737
|
+
db.prepare('DELETE FROM drafts WHERE topic_id NOT IN (SELECT value FROM json_each(?))').run(JSON.stringify([...all.keys()]));
|
|
738
|
+
const draftCounts = db.prepare("SELECT topic_id, count(*) AS count FROM drafts WHERE text <> '' OR refs <> '[]' GROUP BY topic_id");
|
|
739
|
+
const draftCountsFor = db.prepare("SELECT topic_id, count(*) AS count FROM drafts WHERE (text <> '' OR refs <> '[]') AND topic_id IN (SELECT value FROM json_each(?)) GROUP BY topic_id");
|
|
740
|
+
ctx.effect(status.source('drafts', (ids) => {
|
|
741
|
+
const rows = (ids ? draftCountsFor.all(JSON.stringify([...ids])) : draftCounts.all());
|
|
742
|
+
return new Map(rows.map(({ topic_id, count }) => [topic_id, count]));
|
|
743
|
+
}));
|
|
744
|
+
const deleteDrafts = db.prepare('DELETE FROM drafts WHERE topic_id IN (SELECT value FROM json_each(?))');
|
|
745
|
+
ctx.on('topic.purged', ({ topicIds }) => { deleteDrafts.run(JSON.stringify(topicIds)); });
|
|
746
|
+
const sweep = setInterval(topics.purgeExpired, 15 * 60_000).unref();
|
|
747
|
+
ctx.effect(() => clearInterval(sweep));
|
|
748
|
+
ctx.point('topic.context', { title: '主题说明的补充上下文', group: '主题页', about: '供成员查看主题说明和进入主题首轮时附加的纯文本上下文(如产物、规划)。', order: 90 });
|
|
749
|
+
ctx.point('topic.caps', { title: '主题可用的能力', group: '主题页', about: '一个主题可以启用或停用的东西:skill、MCP 服务、工具组和主题能力。', order: 91 });
|
|
750
|
+
ownerships.set(topics, {
|
|
751
|
+
create: (input, plugin) => createTopic(input, `plugin:${plugin}`, { plugin }),
|
|
752
|
+
ensureSystem() {
|
|
753
|
+
const existing = [...all.values()].find((topic) => topic.system === true && live(topic));
|
|
754
|
+
const result = existing ?? createTopic({ title: SYSTEM_TITLE, summary: SYSTEM_SUMMARY }, 'system', { system: true });
|
|
755
|
+
for (const old of all.values()) {
|
|
756
|
+
if (old.system === true && old.deletedAt !== null && old.id !== result.id) {
|
|
757
|
+
systems.delete(old.id);
|
|
758
|
+
save(old);
|
|
759
|
+
announce(old);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
return result;
|
|
763
|
+
},
|
|
764
|
+
bind(id, plugin) {
|
|
765
|
+
const topic = need(id);
|
|
766
|
+
if ((topic.plugin ?? null) === plugin)
|
|
767
|
+
return topic;
|
|
768
|
+
if (plugin === null)
|
|
769
|
+
owners.delete(id);
|
|
770
|
+
else
|
|
771
|
+
owners.set(id, plugin);
|
|
772
|
+
save(topic);
|
|
773
|
+
return announce(topic);
|
|
774
|
+
},
|
|
775
|
+
archivedBy(id) {
|
|
776
|
+
const topic = all.get(id);
|
|
777
|
+
if (!topic || topic.archivedAt === null)
|
|
778
|
+
return undefined;
|
|
779
|
+
return currentArchives().get(id)?.by;
|
|
780
|
+
},
|
|
781
|
+
});
|
|
782
|
+
ctx.provide('topics', topics);
|
|
783
|
+
topicOps(ctx);
|
|
784
|
+
},
|
|
785
|
+
};
|
|
786
|
+
}
|