wezard 2.9.0 → 3.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/CLAUDE.md +43 -21
- package/README.md +37 -50
- package/cli/wezard.sh +59 -20
- package/config.example.jsonc +5 -8
- package/dist/cli/audit.js +11 -13
- package/dist/cli/audit.js.map +1 -1
- package/dist/cli/codex-hook.js +20 -17
- package/dist/cli/codex-hook.js.map +1 -1
- package/dist/cli/ext.js +271 -0
- package/dist/cli/ext.js.map +1 -0
- package/dist/cli/init.js +44 -13
- package/dist/cli/init.js.map +1 -1
- package/dist/cli/skills.js +113 -0
- package/dist/cli/skills.js.map +1 -0
- package/dist/cli/sync.js +3 -2
- package/dist/cli/sync.js.map +1 -1
- package/dist/cli/wx.js +7 -6
- package/dist/cli/wx.js.map +1 -1
- package/dist/daemon/aibot-media.js +28 -20
- package/dist/daemon/aibot-media.js.map +1 -1
- package/dist/daemon/apprentice.js +75 -0
- package/dist/daemon/apprentice.js.map +1 -0
- package/dist/daemon/approval.js +75 -33
- package/dist/daemon/approval.js.map +1 -1
- package/dist/daemon/channel-context.js +7 -0
- package/dist/daemon/channel-context.js.map +1 -1
- package/dist/daemon/channel-onboarding.js +61 -0
- package/dist/daemon/channel-onboarding.js.map +1 -0
- package/dist/daemon/channel-posts.js +93 -0
- package/dist/daemon/channel-posts.js.map +1 -0
- package/dist/daemon/channel-rebind.js +100 -0
- package/dist/daemon/channel-rebind.js.map +1 -0
- package/dist/daemon/channel-store.js.map +1 -1
- package/dist/daemon/channel.js +273 -409
- package/dist/daemon/channel.js.map +1 -1
- package/dist/daemon/chat-name.js +24 -161
- package/dist/daemon/chat-name.js.map +1 -1
- package/dist/daemon/claim.js +1 -45
- package/dist/daemon/claim.js.map +1 -1
- package/dist/daemon/classes.js +416 -0
- package/dist/daemon/classes.js.map +1 -0
- package/dist/daemon/config-api.js +6 -30
- package/dist/daemon/config-api.js.map +1 -1
- package/dist/daemon/control-socket.js +93 -0
- package/dist/daemon/control-socket.js.map +1 -0
- package/dist/daemon/danger-scrub.js +315 -0
- package/dist/daemon/danger-scrub.js.map +1 -0
- package/dist/daemon/danger.js +146 -16
- package/dist/daemon/danger.js.map +1 -1
- package/dist/daemon/detail.js +50 -22
- package/dist/daemon/detail.js.map +1 -1
- package/dist/daemon/exec-surface.js +108 -0
- package/dist/daemon/exec-surface.js.map +1 -0
- package/dist/daemon/external.js +595 -0
- package/dist/daemon/external.js.map +1 -0
- package/dist/daemon/handoff.js +51 -50
- package/dist/daemon/handoff.js.map +1 -1
- package/dist/daemon/http.js +119 -31
- package/dist/daemon/http.js.map +1 -1
- package/dist/daemon/idle-reap.js +30 -0
- package/dist/daemon/idle-reap.js.map +1 -0
- package/dist/daemon/inbound.js +436 -207
- package/dist/daemon/inbound.js.map +1 -1
- package/dist/daemon/index.js +3353 -2760
- package/dist/daemon/index.js.map +1 -1
- package/dist/daemon/last-response.js +51 -25
- package/dist/daemon/last-response.js.map +1 -1
- package/dist/daemon/legacy-panes.js +67 -0
- package/dist/daemon/legacy-panes.js.map +1 -0
- package/dist/daemon/mcp-http.js.map +1 -1
- package/dist/daemon/me-room.js +137 -0
- package/dist/daemon/me-room.js.map +1 -0
- package/dist/daemon/media.js +14 -0
- package/dist/daemon/media.js.map +1 -1
- package/dist/daemon/memory-steward.js +1 -1
- package/dist/daemon/memory-steward.js.map +1 -1
- package/dist/daemon/message-format.js +15 -2
- package/dist/daemon/message-format.js.map +1 -1
- package/dist/daemon/mirror-bridge.js +583 -488
- package/dist/daemon/mirror-bridge.js.map +1 -1
- package/dist/daemon/mirror-store.js +24 -0
- package/dist/daemon/mirror-store.js.map +1 -1
- package/dist/daemon/notices.js +5 -6
- package/dist/daemon/notices.js.map +1 -1
- package/dist/daemon/onboarding.js +459 -0
- package/dist/daemon/onboarding.js.map +1 -0
- package/dist/daemon/outbound.js +21 -11
- package/dist/daemon/outbound.js.map +1 -1
- package/dist/daemon/peers.js +39 -231
- package/dist/daemon/peers.js.map +1 -1
- package/dist/daemon/plan-http.js +2 -3
- package/dist/daemon/plan-http.js.map +1 -1
- package/dist/daemon/plans.js.map +1 -1
- package/dist/daemon/reach.js +1 -5
- package/dist/daemon/reach.js.map +1 -1
- package/dist/daemon/room-auto-adhoc.js +18 -0
- package/dist/daemon/room-auto-adhoc.js.map +1 -0
- package/dist/daemon/room-bind.js +297 -0
- package/dist/daemon/room-bind.js.map +1 -0
- package/dist/daemon/room-facts.js +35 -0
- package/dist/daemon/room-facts.js.map +1 -0
- package/dist/daemon/room-hub.js +569 -0
- package/dist/daemon/room-hub.js.map +1 -0
- package/dist/daemon/room-overdue.js +160 -0
- package/dist/daemon/room-overdue.js.map +1 -0
- package/dist/daemon/room-prune.js +74 -0
- package/dist/daemon/room-prune.js.map +1 -0
- package/dist/daemon/room-spawn.js +20 -0
- package/dist/daemon/room-spawn.js.map +1 -0
- package/dist/daemon/room-watchdog.js +175 -0
- package/dist/daemon/room-watchdog.js.map +1 -0
- package/dist/daemon/rooms.js +204 -0
- package/dist/daemon/rooms.js.map +1 -0
- package/dist/daemon/route.js +7 -18
- package/dist/daemon/route.js.map +1 -1
- package/dist/daemon/session-scan.js +1 -1
- package/dist/daemon/session-scan.js.map +1 -1
- package/dist/daemon/short-id-log.js +43 -0
- package/dist/daemon/short-id-log.js.map +1 -0
- package/dist/daemon/spawn-tmux.js +60 -45
- package/dist/daemon/spawn-tmux.js.map +1 -1
- package/dist/daemon/steward-guard.js +3 -3
- package/dist/daemon/steward-guard.js.map +1 -1
- package/dist/daemon/steward-model.js +17 -3
- package/dist/daemon/steward-model.js.map +1 -1
- package/dist/daemon/steward-repair.js +171 -0
- package/dist/daemon/steward-repair.js.map +1 -0
- package/dist/daemon/steward.js +171 -0
- package/dist/daemon/steward.js.map +1 -0
- package/dist/daemon/tasks.js +4 -14
- package/dist/daemon/tasks.js.map +1 -1
- package/dist/daemon/tool-face.js +31 -0
- package/dist/daemon/tool-face.js.map +1 -0
- package/dist/daemon/turn-opening.js +30 -0
- package/dist/daemon/turn-opening.js.map +1 -0
- package/dist/daemon/wake-tail.js +55 -0
- package/dist/daemon/wake-tail.js.map +1 -0
- package/dist/daemon/weixin-cmd.js +10 -14
- package/dist/daemon/weixin-cmd.js.map +1 -1
- package/dist/daemon/wezard-session.js +6 -2
- package/dist/daemon/wezard-session.js.map +1 -1
- package/dist/daemon/wizard-memory.js +4 -10
- package/dist/daemon/wizard-memory.js.map +1 -1
- package/dist/daemon/wizard-names.js +0 -22
- package/dist/daemon/wizard-names.js.map +1 -1
- package/dist/daemon/wizard.js +683 -159
- package/dist/daemon/wizard.js.map +1 -1
- package/dist/daemon/ws.js +1 -1
- package/dist/daemon/ws.js.map +1 -1
- package/dist/mcp/output.js +30 -7
- package/dist/mcp/output.js.map +1 -1
- package/dist/mcp/relay.js +152 -0
- package/dist/mcp/relay.js.map +1 -0
- package/dist/mcp/tools.js +318 -265
- package/dist/mcp/tools.js.map +1 -1
- package/dist/shared/allow-rules.js +1 -7
- package/dist/shared/allow-rules.js.map +1 -1
- package/dist/shared/charter-rearm.js +179 -0
- package/dist/shared/charter-rearm.js.map +1 -0
- package/dist/shared/chat-http.js +412 -480
- package/dist/shared/chat-http.js.map +1 -1
- package/dist/shared/chat-record.js +55 -0
- package/dist/shared/chat-record.js.map +1 -0
- package/dist/shared/chat-render.js +7 -4
- package/dist/shared/chat-render.js.map +1 -1
- package/dist/shared/chat-view.js +2 -41
- package/dist/shared/chat-view.js.map +1 -1
- package/dist/shared/cli-backends.js +13 -5
- package/dist/shared/cli-backends.js.map +1 -1
- package/dist/shared/cli-local-status.js +28 -0
- package/dist/shared/cli-local-status.js.map +1 -0
- package/dist/shared/cli-socket.js +14 -0
- package/dist/shared/cli-socket.js.map +1 -0
- package/dist/shared/codex-hooks.js +5 -2
- package/dist/shared/codex-hooks.js.map +1 -1
- package/dist/shared/config.js +90 -66
- package/dist/shared/config.js.map +1 -1
- package/dist/shared/daemon-addr.js +23 -0
- package/dist/shared/daemon-addr.js.map +1 -0
- package/dist/shared/daemon-client.js +41 -0
- package/dist/shared/daemon-client.js.map +1 -0
- package/dist/shared/daemon-token.js +2 -2
- package/dist/shared/daemon-token.js.map +1 -1
- package/dist/shared/detail-render.js +98 -21
- package/dist/shared/detail-render.js.map +1 -1
- package/dist/shared/detail-store.js.map +1 -1
- package/dist/shared/external.js +402 -0
- package/dist/shared/external.js.map +1 -0
- package/dist/shared/group-members.js +35 -0
- package/dist/shared/group-members.js.map +1 -0
- package/dist/shared/inject-view.js +143 -0
- package/dist/shared/inject-view.js.map +1 -0
- package/dist/shared/instance-id.js +21 -0
- package/dist/shared/instance-id.js.map +1 -0
- package/dist/shared/legacy-routes.js +199 -0
- package/dist/shared/legacy-routes.js.map +1 -0
- package/dist/shared/mem-guard.js +32 -0
- package/dist/shared/mem-guard.js.map +1 -0
- package/dist/shared/message-footer.js +11 -3
- package/dist/shared/message-footer.js.map +1 -1
- package/dist/shared/model-tiers.js +39 -1
- package/dist/shared/model-tiers.js.map +1 -1
- package/dist/shared/ports.js +56 -0
- package/dist/shared/ports.js.map +1 -0
- package/dist/shared/reminder.js +30 -38
- package/dist/shared/reminder.js.map +1 -1
- package/dist/shared/role-render.js +210 -210
- package/dist/shared/role-render.js.map +1 -1
- package/dist/shared/role-view.js +65 -300
- package/dist/shared/role-view.js.map +1 -1
- package/dist/shared/room-label.js +13 -0
- package/dist/shared/room-label.js.map +1 -0
- package/dist/shared/room-migrate.js +404 -0
- package/dist/shared/room-migrate.js.map +1 -0
- package/dist/shared/room-overdue.js +153 -0
- package/dist/shared/room-overdue.js.map +1 -0
- package/dist/shared/room-search.js +102 -0
- package/dist/shared/room-search.js.map +1 -0
- package/dist/shared/room-view.js +275 -0
- package/dist/shared/room-view.js.map +1 -0
- package/dist/shared/room.js +827 -0
- package/dist/shared/room.js.map +1 -0
- package/dist/shared/session-hooks.js +20 -8
- package/dist/shared/session-hooks.js.map +1 -1
- package/dist/shared/session-label.js +82 -84
- package/dist/shared/session-label.js.map +1 -1
- package/dist/shared/session-mcp.js +36 -40
- package/dist/shared/session-mcp.js.map +1 -1
- package/dist/shared/session-skills.js +8 -0
- package/dist/shared/session-skills.js.map +1 -1
- package/dist/shared/skill-sync.js +229 -0
- package/dist/shared/skill-sync.js.map +1 -0
- package/dist/shared/smarts.js +24 -0
- package/dist/shared/smarts.js.map +1 -0
- package/dist/shared/stream-page.js +22 -0
- package/dist/shared/stream-page.js.map +1 -0
- package/dist/shared/task-file.js +0 -5
- package/dist/shared/task-file.js.map +1 -1
- package/dist/shared/task-template.js +1 -1
- package/dist/shared/task-template.js.map +1 -1
- package/dist/shared/tool-hints.js +20 -11
- package/dist/shared/tool-hints.js.map +1 -1
- package/dist/shared/workspace-trust.js +8 -0
- package/dist/shared/workspace-trust.js.map +1 -1
- package/dist/shared/world.js +101 -166
- package/dist/shared/world.js.map +1 -1
- package/dist/svr/facts-store.js +26 -0
- package/dist/svr/facts-store.js.map +1 -0
- package/dist/svr/index.js +114 -16
- package/dist/svr/index.js.map +1 -1
- package/hooks/pre-tool-use.sh +14 -10
- package/hooks/stop.sh +28 -0
- package/launchd/com.wezard.svr.plist.template +2 -0
- package/package.json +9 -2
- package/scripts/install.sh +23 -0
- package/skills/LICENSE.wecom-unified.txt +21 -0
- package/skills/wecom-unified/SKILL.md +151 -0
- package/skills/wecom-unified/assets/templates/broadside/DESIGN.preset.md +172 -0
- package/skills/wecom-unified/assets/templates/broadside/shell/content.jsx +31 -0
- package/skills/wecom-unified/assets/templates/broadside/slides/01_cover.slide +48 -0
- package/skills/wecom-unified/assets/templates/broadside/slides/02_section.slide +58 -0
- package/skills/wecom-unified/assets/templates/broadside/slides/03_cards.slide +53 -0
- package/skills/wecom-unified/assets/templates/broadside/slides/04_image.slide +64 -0
- package/skills/wecom-unified/assets/templates/cartesian/DESIGN.preset.md +139 -0
- package/skills/wecom-unified/assets/templates/cartesian/shell/content.jsx +28 -0
- package/skills/wecom-unified/assets/templates/cartesian/slides/01_cover.slide +44 -0
- package/skills/wecom-unified/assets/templates/cartesian/slides/02_compare.slide +79 -0
- package/skills/wecom-unified/assets/templates/cartesian/slides/03_table.slide +79 -0
- package/skills/wecom-unified/assets/templates/cobalt-grid/DESIGN.preset.md +118 -0
- package/skills/wecom-unified/assets/templates/cobalt-grid/shell/content.jsx +24 -0
- package/skills/wecom-unified/assets/templates/cobalt-grid/slides/01_cover.slide +61 -0
- package/skills/wecom-unified/assets/templates/cobalt-grid/slides/02_structure.slide +85 -0
- package/skills/wecom-unified/assets/templates/cobalt-grid/slides/03_data.slide +60 -0
- package/skills/wecom-unified/assets/templates/editorial-forest/DESIGN.preset.md +135 -0
- package/skills/wecom-unified/assets/templates/editorial-forest/shell/content.jsx +19 -0
- package/skills/wecom-unified/assets/templates/editorial-forest/slides/01_cover.slide +29 -0
- package/skills/wecom-unified/assets/templates/editorial-forest/slides/02_cards.slide +60 -0
- package/skills/wecom-unified/assets/templates/editorial-forest/slides/03_data.slide +49 -0
- package/skills/wecom-unified/assets/templates/editorial-forest/slides/04_image.slide +46 -0
- package/skills/wecom-unified/assets/templates/emerald-editorial/DESIGN.preset.md +119 -0
- package/skills/wecom-unified/assets/templates/emerald-editorial/shell/content.jsx +18 -0
- package/skills/wecom-unified/assets/templates/emerald-editorial/slides/01_cover.slide +36 -0
- package/skills/wecom-unified/assets/templates/emerald-editorial/slides/02_section.slide +39 -0
- package/skills/wecom-unified/assets/templates/emerald-editorial/slides/03_data.slide +79 -0
- package/skills/wecom-unified/assets/templates/emerald-editorial/slides/04_steps.slide +53 -0
- package/skills/wecom-unified/assets/templates/grove/DESIGN.preset.md +159 -0
- package/skills/wecom-unified/assets/templates/grove/shell/content.jsx +18 -0
- package/skills/wecom-unified/assets/templates/grove/shell/content_light.jsx +18 -0
- package/skills/wecom-unified/assets/templates/grove/slides/01_cover.slide +26 -0
- package/skills/wecom-unified/assets/templates/grove/slides/02_compare.slide +92 -0
- package/skills/wecom-unified/assets/templates/grove/slides/03_table.slide +68 -0
- package/skills/wecom-unified/assets/templates/index.json +280 -0
- package/skills/wecom-unified/assets/templates/long-table/DESIGN.preset.md +135 -0
- package/skills/wecom-unified/assets/templates/long-table/shell/content.jsx +36 -0
- package/skills/wecom-unified/assets/templates/long-table/slides/01_cover.slide +71 -0
- package/skills/wecom-unified/assets/templates/long-table/slides/02_cards.slide +72 -0
- package/skills/wecom-unified/assets/templates/long-table/slides/03_timeline.slide +81 -0
- package/skills/wecom-unified/assets/templates/long-table/slides/04_image.slide +60 -0
- package/skills/wecom-unified/assets/templates/sakura-chroma/DESIGN.preset.md +121 -0
- package/skills/wecom-unified/assets/templates/sakura-chroma/shell/content.jsx +13 -0
- package/skills/wecom-unified/assets/templates/sakura-chroma/slides/01_cover.slide +54 -0
- package/skills/wecom-unified/assets/templates/sakura-chroma/slides/02_products.slide +134 -0
- package/skills/wecom-unified/assets/templates/sakura-chroma/slides/03_section.slide +30 -0
- package/skills/wecom-unified/assets/templates/sakura-chroma/slides/04_schedule.slide +65 -0
- package/skills/wecom-unified/assets/templates/signal/DESIGN.preset.md +121 -0
- package/skills/wecom-unified/assets/templates/signal/shell/content.jsx +18 -0
- package/skills/wecom-unified/assets/templates/signal/shell/content_dark.jsx +23 -0
- package/skills/wecom-unified/assets/templates/signal/slides/01_cover.slide +48 -0
- package/skills/wecom-unified/assets/templates/signal/slides/02_cards.slide +67 -0
- package/skills/wecom-unified/assets/templates/signal/slides/03_data.slide +68 -0
- package/skills/wecom-unified/references/wecomcli-calendar-agenda.md +227 -0
- package/skills/wecom-unified/references/wecomcli-calendar-cancel.md +108 -0
- package/skills/wecom-unified/references/wecomcli-calendar-create.md +238 -0
- package/skills/wecom-unified/references/wecomcli-calendar-freebusy.md +207 -0
- package/skills/wecom-unified/references/wecomcli-calendar-meeting-room.md +170 -0
- package/skills/wecom-unified/references/wecomcli-calendar-search.md +209 -0
- package/skills/wecom-unified/references/wecomcli-calendar-update.md +272 -0
- package/skills/wecom-unified/references/wecomcli-calendar.md +296 -0
- package/skills/wecom-unified/references/wecomcli-contact.md +48 -0
- package/skills/wecom-unified/references/wecomcli-disk.md +380 -0
- package/skills/wecom-unified/references/wecomcli-doc-contents-append.md +20 -0
- package/skills/wecom-unified/references/wecomcli-doc-contents-overwrite.md +27 -0
- package/skills/wecom-unified/references/wecomcli-doc-create.md +162 -0
- package/skills/wecom-unified/references/wecomcli-doc-manage-doc-members-update.md +24 -0
- package/skills/wecom-unified/references/wecomcli-doc-manage-doc-names-update.md +20 -0
- package/skills/wecom-unified/references/wecomcli-doc-manage-doc-rules-update.md +22 -0
- package/skills/wecom-unified/references/wecomcli-doc-manage.md +127 -0
- package/skills/wecom-unified/references/wecomcli-doc.md +135 -0
- package/skills/wecom-unified/references/wecomcli-email-forward-mail.md +131 -0
- package/skills/wecom-unified/references/wecomcli-email-get-mail.md +187 -0
- package/skills/wecom-unified/references/wecomcli-email-reply-mail.md +138 -0
- package/skills/wecom-unified/references/wecomcli-email-search-mail.md +114 -0
- package/skills/wecom-unified/references/wecomcli-email-security.md +52 -0
- package/skills/wecom-unified/references/wecomcli-email-send-mail.md +186 -0
- package/skills/wecom-unified/references/wecomcli-email-send-schedule.md +83 -0
- package/skills/wecom-unified/references/wecomcli-email.md +206 -0
- package/skills/wecom-unified/references/wecomcli-media.md +89 -0
- package/skills/wecom-unified/references/wecomcli-meeting-cancel.md +113 -0
- package/skills/wecom-unified/references/wecomcli-meeting-create.md +167 -0
- package/skills/wecom-unified/references/wecomcli-meeting-list.md +228 -0
- package/skills/wecom-unified/references/wecomcli-meeting-original-get.md +101 -0
- package/skills/wecom-unified/references/wecomcli-meeting-search.md +176 -0
- package/skills/wecom-unified/references/wecomcli-meeting-update.md +217 -0
- package/skills/wecom-unified/references/wecomcli-meeting.md +366 -0
- package/skills/wecom-unified/references/wecomcli-message.md +190 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-animation.md +62 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-box.md +102 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-chart.md +261 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-codeblock.md +70 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-diagram.md +54 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-faicon.md +71 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-hyperlink.md +55 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-image.md +107 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-math.md +65 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-qrcode.md +22 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-slide.md +74 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-svg.md +95 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-table.md +72 -0
- package/skills/wecom-unified/references/wecomcli-pptx-component-text.md +96 -0
- package/skills/wecom-unified/references/wecomcli-pptx-content-contract.md +73 -0
- package/skills/wecom-unified/references/wecomcli-pptx-create-from-material.md +67 -0
- package/skills/wecom-unified/references/wecomcli-pptx-create-from-scratch.md +178 -0
- package/skills/wecom-unified/references/wecomcli-pptx-create-from-template.md +123 -0
- package/skills/wecom-unified/references/wecomcli-pptx-design-principle.md +65 -0
- package/skills/wecom-unified/references/wecomcli-pptx-government-contract.md +40 -0
- package/skills/wecom-unified/references/wecomcli-pptx-human-alignment.md +140 -0
- package/skills/wecom-unified/references/wecomcli-pptx-pptx-api.md +161 -0
- package/skills/wecom-unified/references/wecomcli-pptx-pptx-edit.md +37 -0
- package/skills/wecom-unified/references/wecomcli-pptx-story-principle.md +184 -0
- package/skills/wecom-unified/references/wecomcli-pptx-templates-README.md +165 -0
- package/skills/wecom-unified/references/wecomcli-pptx-templates-anti-ai-slop.md +52 -0
- package/skills/wecom-unified/references/wecomcli-pptx.md +158 -0
- package/skills/wecom-unified/references/wecomcli-sheet-conditional-formatting.md +203 -0
- package/skills/wecom-unified/references/wecomcli-sheet-filter.md +179 -0
- package/skills/wecom-unified/references/wecomcli-sheet-float-image.md +235 -0
- package/skills/wecom-unified/references/wecomcli-sheet-formulas.md +71 -0
- package/skills/wecom-unified/references/wecomcli-sheet-history.md +118 -0
- package/skills/wecom-unified/references/wecomcli-sheet-protection.md +170 -0
- package/skills/wecom-unified/references/wecomcli-sheet-range-operations.md +306 -0
- package/skills/wecom-unified/references/wecomcli-sheet-read-data.md +151 -0
- package/skills/wecom-unified/references/wecomcli-sheet-search-replace.md +121 -0
- package/skills/wecom-unified/references/wecomcli-sheet-structure-operations.md +394 -0
- package/skills/wecom-unified/references/wecomcli-sheet-style.md +246 -0
- package/skills/wecom-unified/references/wecomcli-sheet-table-style.md +199 -0
- package/skills/wecom-unified/references/wecomcli-sheet-validation.md +217 -0
- package/skills/wecom-unified/references/wecomcli-sheet-visual-standards.md +166 -0
- package/skills/wecom-unified/references/wecomcli-sheet-workbook.md +264 -0
- package/skills/wecom-unified/references/wecomcli-sheet-write-cells.md +320 -0
- package/skills/wecom-unified/references/wecomcli-sheet-xlsx-process.md +324 -0
- package/skills/wecom-unified/references/wecomcli-sheet.md +96 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-data-driven-pages.md +50 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-edit.md +526 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-arraylist.md +369 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-datetime.md +283 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-logic.md +247 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-math.md +362 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-operators.md +246 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-pageblock.md +76 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-reference.md +192 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-templates.md +410 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-text.md +377 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-formula-user.md +22 -0
- package/skills/wecom-unified/references/wecomcli-smartpage-mdx-syntax.md +739 -0
- package/skills/wecom-unified/references/wecomcli-smartpage.md +179 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-chart-types.md +95 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-common.md +160 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-edit.md +609 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-field-types.md +438 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-formula.md +845 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-read.md +411 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-record-values.md +218 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-ai_efficiency.md +709 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-connect_to_app.md +380 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-financial_accounting.md +369 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-hr_and_administration.md +475 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-ledger_records.md +156 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-manufacturing.md +395 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-marketing.md +186 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-office_essentials.md +299 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-personal_efficiency.md +70 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-procurement_logistics.md +325 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-project_management.md +564 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_customer.md +222 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_ops.md +105 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_project.md +109 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_research.md +92 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-sales_and_operations.md +446 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-store_management.md +431 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-team_tasks.md +274 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-wechat_customer.md +384 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-template-work_report.md +100 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-templates.md +53 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-view-types.md +373 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-webhook-examples.md +176 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet-webhook.md +186 -0
- package/skills/wecom-unified/references/wecomcli-smartsheet.md +147 -0
- package/skills/wecom-unified/references/wecomcli-todo-create.md +137 -0
- package/skills/wecom-unified/references/wecomcli-todo-delete.md +63 -0
- package/skills/wecom-unified/references/wecomcli-todo-finish.md +72 -0
- package/skills/wecom-unified/references/wecomcli-todo-get.md +69 -0
- package/skills/wecom-unified/references/wecomcli-todo-list.md +136 -0
- package/skills/wecom-unified/references/wecomcli-todo-update.md +112 -0
- package/skills/wecom-unified/references/wecomcli-todo.md +68 -0
- package/skills/wecom-unified/scripts/build_docx.py +1274 -0
- package/skills/wecom-unified/scripts/build_page.py +2449 -0
- package/skills/wecom-unified/scripts/collect.py +868 -0
- package/skills/wecom-unified/scripts/doc_image_extractor.py +498 -0
- package/skills/wecom-unified/scripts/pptx2image.py +268 -0
- package/skills/wecom-unified/scripts/recalc.py +403 -0
- package/systemd/wezard-svr.service.template +2 -0
- package/systemd/wezard.service.template +6 -0
- package/web/chat.css +70 -273
- package/web/chat.js +2269 -2190
- package/dist/daemon/bot-mux.js +0 -161
- package/dist/daemon/bot-mux.js.map +0 -1
- package/dist/daemon/chat-log.js +0 -130
- package/dist/daemon/chat-log.js.map +0 -1
- package/dist/daemon/dispatch.js +0 -6
- package/dist/daemon/dispatch.js.map +0 -1
- package/dist/daemon/idle-nudge.js +0 -90
- package/dist/daemon/idle-nudge.js.map +0 -1
- package/dist/daemon/message-events.js +0 -1155
- package/dist/daemon/message-events.js.map +0 -1
- package/dist/daemon/pending-items.js +0 -160
- package/dist/daemon/pending-items.js.map +0 -1
- package/dist/daemon/router.js +0 -742
- package/dist/daemon/router.js.map +0 -1
- package/dist/daemon/tips.js +0 -25
- package/dist/daemon/tips.js.map +0 -1
- package/dist/daemon/wedoc.js +0 -210
- package/dist/daemon/wedoc.js.map +0 -1
- package/dist/shared/role-search.js +0 -100
- package/dist/shared/role-search.js.map +0 -1
- package/dist/shared/startup-notice.js +0 -21
- package/dist/shared/startup-notice.js.map +0 -1
- package/dist/shared/turn-state.js +0 -27
- package/dist/shared/turn-state.js.map +0 -1
|
@@ -0,0 +1,827 @@
|
|
|
1
|
+
// room 模型的纯核心 (契约: docs/room/contract.md, 已拍板)。
|
|
2
|
+
//
|
|
3
|
+
// 一条总原则: **完成信号、出处、发言权都是账本行上的显式字段**, 不从 transcript / 终句 /
|
|
4
|
+
// 信封里推断。room 是唯一的沟通对象 —— 企微群 / 单聊 / topic 帖是绑定了传输的 room,
|
|
5
|
+
// wizard 之间的两人对话是不绑定传输的 dm room。人和 wizard 走同一个 append 口子。
|
|
6
|
+
//
|
|
7
|
+
// 本文件无 IO: 账本行的类型、fold (行 → 状态)、append 前的判定 (ACL / status 写入权 /
|
|
8
|
+
// 覆盖检查 / 去重 / 唤醒 / 预算)、host 求值、镜像规则、uid 与 room id 的派生。落盘在
|
|
9
|
+
// daemon/rooms.ts, 接线在 daemon/room-hub.ts。
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { expandHome } from "./paths.js";
|
|
13
|
+
import { stripLegacyTail } from "./reminder.js";
|
|
14
|
+
export const whoKey = (w) => w.kind + ":" + w.id;
|
|
15
|
+
export const sameWho = (a, b) => !!a && !!b && a.kind === b.kind && a.id === b.id;
|
|
16
|
+
export const DAEMON = { kind: "daemon", id: "wezard" };
|
|
17
|
+
const UID_RE = /^w[0-9a-f]{12}$/;
|
|
18
|
+
export const isUid = (s) => !!s && UID_RE.test(s);
|
|
19
|
+
const sha1 = (s) => createHash("sha1").update(s).digest("hex");
|
|
20
|
+
/** 实例的稳定身份 id。已落盘的 uid 永远优先 (只读); 没有的按 target + bornAt 现算 ——
|
|
21
|
+
* 迁移脚本与 daemon boot 回填走同一个函数, 算出同一个值。bornAt 缺失 (或非有限数) 时
|
|
22
|
+
* 用确定的回退 \`target|-\`, 绝不把 undefined 拼进去。 */
|
|
23
|
+
export const uidOf = (rec) => {
|
|
24
|
+
if (isUid(rec.uid))
|
|
25
|
+
return rec.uid;
|
|
26
|
+
if (!rec.target)
|
|
27
|
+
throw new Error("uidOf: target required");
|
|
28
|
+
const born = typeof rec.bornAt === "number" && Number.isFinite(rec.bornAt) ? String(rec.bornAt) : "-";
|
|
29
|
+
const uid = "w" + sha1(rec.target + "|" + born).slice(0, 12);
|
|
30
|
+
if (!isUid(uid))
|
|
31
|
+
throw new Error("uidOf: bad uid " + uid);
|
|
32
|
+
return uid;
|
|
33
|
+
};
|
|
34
|
+
/** 新建实例时铸一个 uid (调用方给随机源, 保持本文件纯)。 */
|
|
35
|
+
export const mintUid = (randomHex) => {
|
|
36
|
+
const uid = "w" + randomHex.toLowerCase().replace(/[^0-9a-f]/g, "").slice(0, 12);
|
|
37
|
+
if (!isUid(uid))
|
|
38
|
+
throw new Error("mintUid: need ≥12 hex chars");
|
|
39
|
+
return uid;
|
|
40
|
+
};
|
|
41
|
+
// ── 账本目录 ────────────────────────────────────────────────────────────
|
|
42
|
+
/** room 账本目录 = <stateDir>/rooms, 唯一推导。daemon 读、迁移写、observe 重放都必须
|
|
43
|
+
* 从这里取 —— 2026-11-19 切换失败就是迁移自己拼了 <home>/rooms 而 daemon 读 <stateDir>/rooms,
|
|
44
|
+
* 新 daemon 起来账本为空 (内存 1 / 账本重放 17)。谁都不许再本地 join 一个 rooms 出来。 */
|
|
45
|
+
export const roomsDirOf = (stateDir) => join(expandHome(stateDir), "rooms");
|
|
46
|
+
/** 两个 wizard 的 dm room: uid 排序后 \`|\` 拼接, 同一对唯一。名字 / target 绝不进 id。 */
|
|
47
|
+
export const dmRoomId = (a, b) => {
|
|
48
|
+
if (!isUid(a) || !isUid(b))
|
|
49
|
+
throw new Error("dmRoomId: both sides must be uids (got " + a + ", " + b + ")");
|
|
50
|
+
if (a === b)
|
|
51
|
+
throw new Error("dmRoomId: a wizard has no dm with itself");
|
|
52
|
+
return "dm:" + [a, b].sort().join("|");
|
|
53
|
+
};
|
|
54
|
+
export const adhocRoomId = (randomHex) => {
|
|
55
|
+
const h = randomHex.toLowerCase().replace(/[^0-9a-f]/g, "").slice(0, 8);
|
|
56
|
+
if (h.length !== 8)
|
|
57
|
+
throw new Error("adhocRoomId: need ≥8 hex chars");
|
|
58
|
+
return "adhoc:" + h;
|
|
59
|
+
};
|
|
60
|
+
/** 绑定传输的 room = base principal 原样 (\`chat:…\` / \`user:…\`, 含 \`~bot\`); 无绑定的是 dm: / adhoc:。 */
|
|
61
|
+
export const isBoundRoom = (room) => !!room && !room.startsWith("dm:") && !room.startsWith("adhoc:");
|
|
62
|
+
export const dmPeers = (room) => {
|
|
63
|
+
if (!room.startsWith("dm:"))
|
|
64
|
+
return undefined;
|
|
65
|
+
const [a, b] = room.slice(3).split("|");
|
|
66
|
+
return isUid(a) && isUid(b) ? [a, b] : undefined;
|
|
67
|
+
};
|
|
68
|
+
/** room 的 8 位 hash —— 消息 id 的前缀, 使 id 全机唯一且能反查 room。 */
|
|
69
|
+
export const roomHash = (room) => sha1("room|" + room).slice(0, 8);
|
|
70
|
+
export const msgIdOf = (room, seq) => roomHash(room) + "." + seq;
|
|
71
|
+
export const parseMsgId = (id) => {
|
|
72
|
+
const m = /^([0-9a-f]{8})\.(\d+)$/.exec((id ?? "").trim());
|
|
73
|
+
return m ? { hash: m[1], seq: Number(m[2]) } : undefined;
|
|
74
|
+
};
|
|
75
|
+
/** 注入头里的出站等级标记 (一个字): 群 / 单 = 绑定企微出站, 私 / 临 = 只在 rolepage 里。 */
|
|
76
|
+
export const ROOM_MARKS = ["群", "单", "私", "临"];
|
|
77
|
+
export const isRoomMark = (s) => !!s && ROOM_MARKS.includes(s);
|
|
78
|
+
export const roomMark = (room) => room.startsWith("dm:") ? "私"
|
|
79
|
+
: room.startsWith("adhoc:") ? "临"
|
|
80
|
+
: room.startsWith("user:") || room.startsWith("me:") ? "单"
|
|
81
|
+
: "群";
|
|
82
|
+
/** 标记 → 这一行真的进不进企微 (与 isBoundRoom 同口径, 但读的是注入头里的标记)。 */
|
|
83
|
+
export const markIsBound = (m) => m === "群" || m === "单";
|
|
84
|
+
/** 工具侧短 id 补全: 注入块里行 id 只写 seq (hash 段在块头), 模型照抄回来时按「最近投递过
|
|
85
|
+
* 这个 seq 的 room」补成全 id (候选由调用方从持久投递记录取 —— 轮终 / reload 后仍有效)。
|
|
86
|
+
* 纯 seq 且恰好一个候选 room 才补; 多个 = 歧义; 0 个 = 可行动报错 (模型手里有块头 #hash8 和行号)。 */
|
|
87
|
+
export const expandMsgRef = (id, rooms) => {
|
|
88
|
+
const raw = (id ?? "").trim();
|
|
89
|
+
if (!/^\d+$/.test(raw))
|
|
90
|
+
return { id: raw };
|
|
91
|
+
if (rooms.length > 1)
|
|
92
|
+
return { error: "这个短 id 在多个 room 都投递过, 认不出是哪一条 —— 请用完整 id (hash8.seq): " + raw };
|
|
93
|
+
if (!rooms.length) {
|
|
94
|
+
return { error: "短 id " + raw + " 认不出: 最近的投递记录里没有它 (批次更早 / 已出窗) —— 请用完整 id = 注入块头的 #hash8 拼行号, 例: <hash8>." + raw };
|
|
95
|
+
}
|
|
96
|
+
return { id: msgIdOf(rooms[0], Number(raw)) };
|
|
97
|
+
};
|
|
98
|
+
/** 每条 id 的候选各算各的 (不同 seq 可能投递在不同 room)。 */
|
|
99
|
+
export const expandMsgRefs = (ids, roomsOf) => {
|
|
100
|
+
const out = [];
|
|
101
|
+
for (const id of ids) {
|
|
102
|
+
const r = expandMsgRef(id, roomsOf(id));
|
|
103
|
+
if (r.error)
|
|
104
|
+
return { error: r.error };
|
|
105
|
+
out.push(r.id);
|
|
106
|
+
}
|
|
107
|
+
return { ids: out };
|
|
108
|
+
};
|
|
109
|
+
/** 关 duty 的终态。need / progress / error 不关。 */
|
|
110
|
+
export const CLOSING = new Set(["done", "passed", "canceled"]);
|
|
111
|
+
/** 公告正文上限 (字)。 */
|
|
112
|
+
export const NOTICE_MAX = 500;
|
|
113
|
+
export const HOST_SLOT = "host";
|
|
114
|
+
export const dutyKey = (msg, slot) => msg + "@" + slot;
|
|
115
|
+
export const emptyRoom = (id) => ({
|
|
116
|
+
id, seq: 0, nextOrd: 1, members: new Map(), msgs: [], byId: new Map(), duties: new Map(), everMembers: new Map(), memberLog: [], bindings: new Map(), verdicts: new Map(),
|
|
117
|
+
});
|
|
118
|
+
const slotOfWho = (w) => whoKey(w);
|
|
119
|
+
/** 一条 msg 行开出的 duty (fold 与 judge 共用)。reAuthorOf = 按 msgId 查作者 (applyOp 传 byId 查询),
|
|
120
|
+
* 只给 activate 判决行定 asker 用: 那条 duty 的源头是 re 的原话人, 不是发判决的 monitor (查不到回落 row.author)。 */
|
|
121
|
+
export const dutiesOpenedBy = (row, reAuthorOf) => {
|
|
122
|
+
const base = { msg: row.id, seq: row.seq, asker: row.author, at: row.at,
|
|
123
|
+
...(row.after?.length ? { after: row.after } : {}), ...(row.planId ? { planId: row.planId } : {}), ...(row.for ? { for: row.for } : {}) };
|
|
124
|
+
if (row.kind === "ask" || row.kind === "task")
|
|
125
|
+
// 点名的 wizard 与人 (人名下开的 ask / task 靠人的下一句话关账, 见 judge 的 humanCloses)。
|
|
126
|
+
return (row.to ?? []).filter((t) => t.kind === "wizard" || t.kind === "human").map((t) => ({ ...base, slot: slotOfWho(t), kind: row.kind }));
|
|
127
|
+
// wizard 的 need 回复: judge 把 re 里 duty 的人提问者补进 to —— 球传到人那边, 人名下开一条 need。
|
|
128
|
+
if (row.kind === "reply" && row.status === "need" && row.author.kind === "wizard")
|
|
129
|
+
return (row.to ?? []).filter((t) => t.kind === "human").map((t) => ({ ...base, slot: slotOfWho(t), kind: "need" }));
|
|
130
|
+
if (row.kind === "join-req")
|
|
131
|
+
return [{ ...base, slot: HOST_SLOT, kind: "join-req" }];
|
|
132
|
+
// monitor 的 activate: 为 re 里那条人话在 to 名下开 duty (人话自己不再开 host 槽位)。to 为空 = 判定不回应。
|
|
133
|
+
if (row.kind === "trigger" && row.sys === "activate") {
|
|
134
|
+
const target = row.to?.[0];
|
|
135
|
+
if (!(target && target.kind === "wizard"))
|
|
136
|
+
return [];
|
|
137
|
+
const asker = (row.re?.[0] ? reAuthorOf?.(row.re[0]) : undefined) ?? row.author;
|
|
138
|
+
return [{ ...base, asker, msg: row.re?.[0] ?? row.id, slot: slotOfWho(target), kind: "human" }];
|
|
139
|
+
}
|
|
140
|
+
// 绑定 room 里的人话: 点名开给被点名者; 交给 monitor 的不开 duty; 其余开给 host。
|
|
141
|
+
if (row.author.kind === "human" && row.kind === "say" && isBoundRoom(row.room) && row.origin !== "migrate") {
|
|
142
|
+
if (row.monitor)
|
|
143
|
+
return [];
|
|
144
|
+
const named = (row.to ?? []).filter((t) => t.kind === "wizard");
|
|
145
|
+
return named.length ? named.map((t) => ({ ...base, slot: slotOfWho(t), kind: "human" })) : [{ ...base, slot: HOST_SLOT, kind: "human" }];
|
|
146
|
+
}
|
|
147
|
+
return [];
|
|
148
|
+
};
|
|
149
|
+
/** 单步 fold —— 就地推进 (调用方持有状态; 纯度在「同一串行 = 同一状态」上)。 */
|
|
150
|
+
export const BATCH_KEEP = 64;
|
|
151
|
+
export const applyOp = (s, op) => {
|
|
152
|
+
switch (op.op) {
|
|
153
|
+
case "open":
|
|
154
|
+
s.kind = op.kind;
|
|
155
|
+
if (op.title)
|
|
156
|
+
s.title = op.title;
|
|
157
|
+
s.openedAt ??= op.at;
|
|
158
|
+
break;
|
|
159
|
+
case "name":
|
|
160
|
+
if (op.title)
|
|
161
|
+
s.title = op.title;
|
|
162
|
+
break;
|
|
163
|
+
case "notice":
|
|
164
|
+
if (op.text.trim())
|
|
165
|
+
s.notice = op.text;
|
|
166
|
+
else
|
|
167
|
+
delete s.notice;
|
|
168
|
+
break;
|
|
169
|
+
case "join": {
|
|
170
|
+
const k = whoKey(op.who);
|
|
171
|
+
const prev = s.members.get(k);
|
|
172
|
+
if (!prev)
|
|
173
|
+
s.memberLog.push({ at: op.at, op: "join", who: op.who, by: op.by, via: op.via });
|
|
174
|
+
s.members.set(k, prev
|
|
175
|
+
? { ...prev, via: op.via, by: op.by, ...(op.role ? { role: op.role } : {}) }
|
|
176
|
+
: { who: op.who, ord: op.ord, via: op.via, since: op.at, by: op.by, seen: 0, watch: false, batches: [], ...(op.role ? { role: op.role } : {}) });
|
|
177
|
+
s.everMembers.set(k, op.who);
|
|
178
|
+
s.nextOrd = Math.max(s.nextOrd, op.ord + 1);
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
case "leave":
|
|
182
|
+
if (s.members.has(whoKey(op.who)))
|
|
183
|
+
s.memberLog.push({ at: op.at, op: "leave", who: op.who, by: op.by, ...(op.why ? { why: op.why } : {}) });
|
|
184
|
+
s.members.delete(whoKey(op.who));
|
|
185
|
+
break;
|
|
186
|
+
case "seen": {
|
|
187
|
+
const m = s.members.get(whoKey(op.who));
|
|
188
|
+
if (m && op.seq > m.seen)
|
|
189
|
+
m.seen = op.seq;
|
|
190
|
+
if (m && op.batch && m.batches[m.batches.length - 1] !== op.at)
|
|
191
|
+
m.batches = [...m.batches, op.at].slice(-BATCH_KEEP);
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case "watch": {
|
|
195
|
+
const m = s.members.get(whoKey(op.who));
|
|
196
|
+
if (m)
|
|
197
|
+
m.watch = op.on;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
// bind 设 / 覆盖条目, unbind 删条目 —— 判定在 append 时已算死, 重放只照抄。
|
|
201
|
+
case "bind":
|
|
202
|
+
s.bindings.set(op.source, { at: op.at, by: op.by, ...(op.why ? { why: op.why } : {}) });
|
|
203
|
+
break;
|
|
204
|
+
case "unbind":
|
|
205
|
+
s.bindings.delete(op.source);
|
|
206
|
+
break;
|
|
207
|
+
case "move": {
|
|
208
|
+
const from = s.duties.get(dutyKey(op.duty, slotOfWho(op.from))) ?? s.duties.get(dutyKey(op.duty, HOST_SLOT));
|
|
209
|
+
if (!from || from.closed)
|
|
210
|
+
break;
|
|
211
|
+
s.duties.delete(from.key);
|
|
212
|
+
const key = dutyKey(op.duty, slotOfWho(op.to));
|
|
213
|
+
s.duties.set(key, { ...from, key, slot: slotOfWho(op.to), movedAt: op.at });
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case "assign": {
|
|
217
|
+
// 旧 monitor 判决: 不占 seq、不写消息行, 只开 duty (重放一致)。
|
|
218
|
+
const key = dutyKey(op.duty, slotOfWho(op.to));
|
|
219
|
+
if (!s.duties.has(key)) {
|
|
220
|
+
const row = s.byId.get(op.duty);
|
|
221
|
+
s.duties.set(key, { key, msg: op.duty, seq: row?.seq ?? 0, slot: slotOfWho(op.to), asker: op.asker, kind: "human", at: op.at, via: "assign" });
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case "perceive": {
|
|
226
|
+
// 感知判决 (2026-11-24): 一条 op 开一串 duty (名单逐个, via:"assign" 语义不变) + 记一条 verdict。
|
|
227
|
+
// 不占 seq、不写消息行; 目标重复 / 非成员只跳过 (写入前 judgeActivate 已校验, 这里防旧账本脏数据)。
|
|
228
|
+
const row = s.byId.get(op.duty);
|
|
229
|
+
for (const t of op.to) {
|
|
230
|
+
if (t.kind !== "wizard")
|
|
231
|
+
continue;
|
|
232
|
+
const key = dutyKey(op.duty, slotOfWho(t));
|
|
233
|
+
if (!s.duties.has(key))
|
|
234
|
+
s.duties.set(key, { key, msg: op.duty, seq: row?.seq ?? 0, slot: slotOfWho(t), asker: op.asker, kind: "human", at: op.at, via: "assign" });
|
|
235
|
+
}
|
|
236
|
+
s.verdicts.set(op.duty, { msg: op.duty, at: op.at, to: op.to, by: op.by, ...(op.why ? { why: op.why } : {}) });
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
case "msg": {
|
|
240
|
+
s.seq = Math.max(s.seq, op.seq);
|
|
241
|
+
s.msgs.push(op);
|
|
242
|
+
s.byId.set(op.id, op);
|
|
243
|
+
s.everMembers.set(whoKey(op.author), op.author);
|
|
244
|
+
for (const d of dutiesOpenedBy(op, (id) => s.byId.get(id)?.author)) {
|
|
245
|
+
const key = dutyKey(d.msg, d.slot);
|
|
246
|
+
if (!s.duties.has(key))
|
|
247
|
+
s.duties.set(key, { ...d, key });
|
|
248
|
+
}
|
|
249
|
+
for (const key of op.closes ?? []) {
|
|
250
|
+
const d = s.duties.get(key);
|
|
251
|
+
if (d && !d.closed)
|
|
252
|
+
d.closed = { by: op.id, status: op.status ?? "done", at: op.at };
|
|
253
|
+
}
|
|
254
|
+
// 非终态 status 记在 holder 名下的那条 duty 上 (need 反向唤醒提问者的依据)。
|
|
255
|
+
if (op.kind === "reply" && op.status && !CLOSING.has(op.status))
|
|
256
|
+
for (const re of op.re ?? [])
|
|
257
|
+
for (const d of s.duties.values())
|
|
258
|
+
if (d.msg === re && !d.closed && holderMatches(s, d, op.author))
|
|
259
|
+
d.last = { status: op.status, seq: op.seq };
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return s;
|
|
264
|
+
};
|
|
265
|
+
export const foldRoom = (id, ops) => ops.reduce(applyOp, emptyRoom(id));
|
|
266
|
+
// ── host ───────────────────────────────────────────────────────────────
|
|
267
|
+
/** host = 当前 wizard 成员里加入序最小的那个; 人不参与, monitor 也不当 host (它的活是激活, 不是回答)。
|
|
268
|
+
* 每次读时算, 不落盘。undefined = 没有 wizard 成员 (人兜底 / 绑定 room 下一句人话触发 onboarding)。 */
|
|
269
|
+
export const hostOf = (members) => {
|
|
270
|
+
let best;
|
|
271
|
+
for (const m of members)
|
|
272
|
+
if (m.who.kind === "wizard" && m.role !== "monitor" && (!best || m.ord < best.ord))
|
|
273
|
+
best = m;
|
|
274
|
+
return best?.who;
|
|
275
|
+
};
|
|
276
|
+
export const roomHost = (s) => hostOf(s.members.values());
|
|
277
|
+
/** 本 room 的 monitor 成员 (不发言、不当 host、不持有 duty)。 */
|
|
278
|
+
export const monitorsOf = (s) => [...s.members.values()].filter((m) => m.role === "monitor" && m.who.kind === "wizard").map((m) => m.who);
|
|
279
|
+
export const hasMonitor = (s) => [...s.members.values()].some((m) => m.role === "monitor" && m.who.kind === "wizard");
|
|
280
|
+
/** 感知候选: 本 room 的 wizard 成员里, 除 perceptron 自己之外的每一个 —— 判决只对这些人的职责做分析。 */
|
|
281
|
+
export const perceptronCandidates = (s) => [...s.members.values()].filter((m) => m.who.kind === "wizard" && m.role !== "monitor").map((m) => m.who);
|
|
282
|
+
/** 感知门控 (2026-11-24 人二次拍板 9e7dbc47.9): 存在 wizard 成员 (不含 perceptron 自己) 就需要感知 ——
|
|
283
|
+
* 含单管家 room / 频道: 它也要感知「没点名给它的消息是否需要它回应」, 不再回落 host 兜底。
|
|
284
|
+
* 单聊 / .me / 引导中的 room 由 daemon 侧不建 perceptron (那几类人话有明确去处)。 */
|
|
285
|
+
export const perceptronWanted = (s) => perceptronCandidates(s).length >= 1;
|
|
286
|
+
/** 起名 / 改名只有当前 host 能做 (人也不行 —— 人的意见走人话)。 */
|
|
287
|
+
export const canRename = (s, who) => sameWho(roomHost(s), who);
|
|
288
|
+
/** 改公告的权限同起名: 只有当前 host。人不受此限 (人的授权在 inbound 的 allowFrom 门)。 */
|
|
289
|
+
export const canNotice = (s, who) => who.kind === "human" || canRename(s, who);
|
|
290
|
+
/** 一次改公告的裁决: 权限 + 归一化 (空白折成单空格) + 长度。空串 = 清除, 与现有相同 = changed:false。 */
|
|
291
|
+
export const judgeNotice = (s, who, raw) => {
|
|
292
|
+
if (!canNotice(s, who))
|
|
293
|
+
return { ok: false, status: 403, reason: "只有本 room 的 host 能改公告" };
|
|
294
|
+
const notice = raw.replace(/\s+/g, " ").trim();
|
|
295
|
+
if (notice.length > NOTICE_MAX)
|
|
296
|
+
return { ok: false, status: 400, reason: "公告太长 (最多 " + NOTICE_MAX + " 字, 现在 " + notice.length + ")" };
|
|
297
|
+
return { ok: true, notice, changed: notice !== (s.notice ?? "") };
|
|
298
|
+
};
|
|
299
|
+
/** duty 此刻的处理人 (host 槽位读时求值)。 */
|
|
300
|
+
export const holderOf = (s, d) => {
|
|
301
|
+
if (d.slot === HOST_SLOT)
|
|
302
|
+
return roomHost(s);
|
|
303
|
+
const i = d.slot.indexOf(":");
|
|
304
|
+
return { kind: d.slot.slice(0, i), id: d.slot.slice(i + 1) };
|
|
305
|
+
};
|
|
306
|
+
const holderMatches = (s, d, who) => sameWho(holderOf(s, d), who);
|
|
307
|
+
export const openDuties = (s) => [...s.duties.values()].filter((d) => !d.closed);
|
|
308
|
+
/** who 在这个 room 手上未关的 duty。 */
|
|
309
|
+
export const dutiesHeldBy = (s, who) => openDuties(s).filter((d) => holderMatches(s, d, who));
|
|
310
|
+
/** 人 who 的下一句话关哪些 duty (key): 它在本 room 名下全部未关的; `re` (企微引用) 命中其中几条就只关那几条 ——
|
|
311
|
+
* 引用的不是它名下的账 (引了别的消息 / 引用没对上行) 不算「指定了哪一条」, 照全关, 免得一句答话挂着账不落。 */
|
|
312
|
+
export const humanCloses = (s, who, re) => {
|
|
313
|
+
const mine = dutiesHeldBy(s, who);
|
|
314
|
+
const quoted = mine.filter((x) => re.includes(x.msg));
|
|
315
|
+
return (quoted.length ? quoted : mine).map((x) => x.key);
|
|
316
|
+
};
|
|
317
|
+
const uniqWho = (ws) => ws.filter((w, i) => ws.findIndex((x) => sameWho(x, w)) === i);
|
|
318
|
+
/** 企微的引用只带文字、不带消息 id: 把引用正文对回本 room 里的一行 (别人 / daemon 说的、正文互相包含、取最近一条)。
|
|
319
|
+
* 只比字母数字 (镜像出去的气泡带名字头、markdown 会被企微改样), 太短 (< 4 字) 不认 —— 对不上就是没引用,
|
|
320
|
+
* 人话关账按「没指定哪一条」处理 (全关)。 */
|
|
321
|
+
export const quotedRowIds = (s, quoted, author) => {
|
|
322
|
+
const canon = (t) => t.replace(/[^\p{L}\p{N}]/gu, "");
|
|
323
|
+
const q = canon(quoted);
|
|
324
|
+
if (q.length < 4)
|
|
325
|
+
return [];
|
|
326
|
+
const hit = [...s.msgs].reverse().find((m) => !sameWho(m.author, author) && m.kind !== "trigger" && ((c) => c.length >= 4 && (c.includes(q) || q.includes(c)))(canon(m.text)));
|
|
327
|
+
return hit ? [hit.id] : [];
|
|
328
|
+
};
|
|
329
|
+
/** judge 替这一行补的收话人 (进行里的 `to`, 重放不再推断):
|
|
330
|
+
* wizard 的 need 回复 → re 里它所持 duty 的**人**提问者 (fold 据此在人名下开 need);
|
|
331
|
+
* 人的话关了谁的账 → 那些账的 wizard 提问者 (仍在 room 里的): 这句就是答给它们的, 与点名同义 —— 唤醒它们、
|
|
332
|
+
* 且不再当「没点名的人话」落给 host。 */
|
|
333
|
+
const judgeAddressees = (s, d, re, status, closes) => {
|
|
334
|
+
if (d.kind === "reply" && status === "need" && d.author.kind === "wizard")
|
|
335
|
+
return uniqWho(openDuties(s).filter((x) => re.includes(x.msg) && holderMatches(s, x, d.author)).map((x) => x.asker).filter((a) => a.kind === "human"));
|
|
336
|
+
if (d.kind === "say" && d.author.kind === "human")
|
|
337
|
+
return uniqWho([
|
|
338
|
+
...closes.flatMap((k) => { const a = s.duties.get(k)?.asker; return a?.kind === "wizard" && s.members.has(whoKey(a)) ? [a] : []; }),
|
|
339
|
+
// 人引用 / 回复了某个 wizard 的一行 (帖子 / 交代 / 回话): 这句就是答给它的, 与点名同义 ——
|
|
340
|
+
// 直接开给它, 不再交给 host / perceptron 猜 (2026-11-24 人报: 回复谁就该投给谁)。
|
|
341
|
+
...re.flatMap((id) => {
|
|
342
|
+
const a = s.byId.get(id)?.author;
|
|
343
|
+
const m = a ? s.members.get(whoKey(a)) : undefined;
|
|
344
|
+
return a?.kind === "wizard" && m && m.role !== "monitor" && !sameWho(a, d.author) ? [a] : [];
|
|
345
|
+
}),
|
|
346
|
+
]);
|
|
347
|
+
return [];
|
|
348
|
+
};
|
|
349
|
+
/** holder 在最近一次被这条 duty 唤醒之后, 是否已用 chat_reply 交代过 —— 任何 status 都算
|
|
350
|
+
* (progress / need 不关账, 但已经「交代过这一轮」, 不该再被提醒、更不该给提问者写系统行)。
|
|
351
|
+
* 按唤醒算而不按「曾经回过」: need 之后提问者补了信息再唤醒它, 那一轮仍要重新交代。 */
|
|
352
|
+
export const dutyAnswered = (s, d, who) => {
|
|
353
|
+
const touches = (r) => r.id === d.msg || (r.re ?? []).includes(d.msg);
|
|
354
|
+
const wokeAt = s.msgs.reduce((at, r) => (touches(r) && r.wake.includes(who.id) ? Math.max(at, r.seq) : at), 0);
|
|
355
|
+
return s.msgs.some((r) => r.kind === "reply" && r.seq > wokeAt && sameWho(r.author, who) && (r.re ?? []).includes(d.msg));
|
|
356
|
+
};
|
|
357
|
+
/** 2026-11-20 起 idList 修好之前, 没给 after 的 chat_ask 落成了 after:["undefined"] —— 这个假 id
|
|
358
|
+
* 不指向任何消息, 视为「没有前置」(否则前置永不满足, 被点名者永不唤醒)。 */
|
|
359
|
+
export const FAKE_AFTER_ID = "undefined";
|
|
360
|
+
/** after 前置是否全部以 done 关闭 (跨 room 的前置由调用方给查询函数)。 */
|
|
361
|
+
export const afterSatisfied = (after, closedDone) => !after?.length || after.every((id) => id === FAKE_AFTER_ID || closedDone(id));
|
|
362
|
+
/** 这条 after 只由假 id 构成 (旧 bug 的受害行, 不是真在等谁)。 */
|
|
363
|
+
export const afterIsFake = (after) => !!after?.length && after.every((id) => id === FAKE_AFTER_ID);
|
|
364
|
+
/** 带 after 的 duty 是否欠一次补唤醒 —— 欠则返回该叫的 holder。
|
|
365
|
+
* judge 在前置未满足时把 wake 落成 [] 存进行里, 之后前置关了没人重算; 而 holder 的 seen 游标可能早已
|
|
366
|
+
* 越过那一行, pending() 现算也救不回来, 所以由 daemon 追加一条 trigger 行 (新 seq, 必在游标之后)。
|
|
367
|
+
* 欠 ⇔ 前置此刻满足 ∧ duty 仍开 ∧ holder 是 wizard 成员 ∧ 账本里没有任何一行 (原 ask / re 它的行)
|
|
368
|
+
* 唤醒过 holder ∧ 没有已写给 holder 的 trigger (幂等: 预算降级也不重写) ∧ holder 从没 re 过它
|
|
369
|
+
* (它已经从游标里读到并交代过, 不必再叫)。 */
|
|
370
|
+
export const afterWakeDue = (s, d, closedDone) => {
|
|
371
|
+
if (d.closed || !d.after?.length || !afterSatisfied(d.after, closedDone))
|
|
372
|
+
return undefined;
|
|
373
|
+
const holder = holderOf(s, d);
|
|
374
|
+
if (holder?.kind !== "wizard" || !s.members.has(whoKey(holder)))
|
|
375
|
+
return undefined;
|
|
376
|
+
const touches = (r) => r.id === d.msg || (r.re ?? []).includes(d.msg);
|
|
377
|
+
const handled = s.msgs.some((r) => touches(r) && (r.wake.includes(holder.id)
|
|
378
|
+
|| (r.kind === "trigger" && (r.to ?? []).some((t) => sameWho(t, holder)))
|
|
379
|
+
|| (r.id !== d.msg && sameWho(r.author, holder))));
|
|
380
|
+
return handled ? undefined : holder;
|
|
381
|
+
};
|
|
382
|
+
/** 补唤醒 trigger 行的正文: 原 ask 可能已在 holder 的游标之前 (渲染时不会再列), 所以带上原文。 */
|
|
383
|
+
export const afterTriggerText = (ask, askerName) => (afterIsFake(ask.after)
|
|
384
|
+
? "[wezard] 补投: " + ask.id + " 当时因 after 写坏 (假 id \"undefined\") 没有唤醒你, 它其实没有前置, 现在补上。"
|
|
385
|
+
: "[wezard] 前置 " + (ask.after ?? []).join(", ") + " 已完成, " + ask.id + " 现在轮到你了。")
|
|
386
|
+
+ "\n原话 (" + ask.kind + ", 来自 " + askerName + "): " + ask.text;
|
|
387
|
+
export const msgClosedDone = (s, msg) => {
|
|
388
|
+
const ds = [...s.duties.values()].filter((d) => d.msg === msg);
|
|
389
|
+
return ds.length > 0 && ds.every((d) => d.closed?.status === "done");
|
|
390
|
+
};
|
|
391
|
+
// ── 镜像规则 (§3a) ─────────────────────────────────────────────────────
|
|
392
|
+
/** 一行进企微 ⇔ room 绑定了传输 ∧ 是 msg ∧ origin ∉ {inbound, migrate} ∧ kind ≠ trigger
|
|
393
|
+
* ∧ 有内容 (正文或文件至少一样)。空正文无文件的行 (纯 status 关账, 2026-11-23 人报: 空交代被发成
|
|
394
|
+
* 只有身份头的空评论) 不出站 —— 账本里它照常存在并关账, 只是没东西可给人看。 */
|
|
395
|
+
export const mirrors = (row) => row.op === "msg" && isBoundRoom(row.room) && row.origin !== "inbound" && row.origin !== "migrate" && row.kind !== "trigger"
|
|
396
|
+
&& (String(row.text ?? "").trim() !== "" || (row.files?.length ?? 0) > 0);
|
|
397
|
+
// ── 出站: 一行的目标源 (§4.2, 纯判定; 传输句柄由 daemon 注入) ──────────
|
|
398
|
+
/** 这一行的目标源: reply 跟它所 re 那一行的 src (缺省 = 主源 = room id), 其余 (say / file / …) 回主源。
|
|
399
|
+
* re 一条都查不到 (不在本 room 账本) → 回落主源并置 fellBack, 调用方记日志 —— 不炸也不丢话。 */
|
|
400
|
+
export const outboundSourceOf = (row, rowOf) => {
|
|
401
|
+
if (row.kind !== "reply")
|
|
402
|
+
return { source: row.room, fellBack: false };
|
|
403
|
+
const hit = (row.re ?? []).map(rowOf).find((r) => !!r);
|
|
404
|
+
if (!hit)
|
|
405
|
+
return { source: row.room, fellBack: true };
|
|
406
|
+
return { source: hit.src || row.room, fellBack: false };
|
|
407
|
+
};
|
|
408
|
+
/** 人话行的发生源 (src, 缺省主源): 占位气泡盖在哪条流上。cover 机制本身不动, 只拿它确认
|
|
409
|
+
* 盖的流与这行的目标源是同一个 —— 不是就别盖, 免得把回复塞进另一个源的流里。 */
|
|
410
|
+
export const bubbleSourceOf = (row) => row.src || row.room;
|
|
411
|
+
/** 目标源 → 传输 (设计稿 §4.2 表): wx > 频道 > 帖子 (有配对) > 群。
|
|
412
|
+
* 帖子源仍查 channel-posts 的 {频道, 根帖} 拿企微侧地址; 绑定本身不改企微地址。 */
|
|
413
|
+
export const planOutbound = (source, row, d) => {
|
|
414
|
+
const chatId = source.replace(/^(user|chat|group):/, "");
|
|
415
|
+
if (d.wxOwns(chatId))
|
|
416
|
+
return { via: "wx", chatId };
|
|
417
|
+
if (d.isTopic(source)) {
|
|
418
|
+
const topicId = source.replace(/^chat:/, "");
|
|
419
|
+
const a = d.channelAction?.(row) ?? { action: "post" };
|
|
420
|
+
return a.action === "reply" ? { via: "channel", topicId, action: "reply", post: a.post } : { via: "channel", topicId, action: "post" };
|
|
421
|
+
}
|
|
422
|
+
const ref = d.postRefOf(source);
|
|
423
|
+
if (ref)
|
|
424
|
+
return { via: "post", channel: ref.channel, post: ref.post };
|
|
425
|
+
return { via: "group", chatId };
|
|
426
|
+
};
|
|
427
|
+
/** 人话行的占位气泡由**第一条** re 它且带正文的 reply 覆盖; 之后的作为新消息。 */
|
|
428
|
+
export const coversBubble = (s, row) => {
|
|
429
|
+
if (row.kind !== "reply" || !row.text.trim())
|
|
430
|
+
return undefined;
|
|
431
|
+
for (const re of row.re ?? []) {
|
|
432
|
+
const target = s.byId.get(re);
|
|
433
|
+
if (!target?.bubble)
|
|
434
|
+
continue;
|
|
435
|
+
const earlier = s.msgs.some((m) => m !== row && m.kind === "reply" && m.text.trim() && m.seq < row.seq && (m.re ?? []).includes(re));
|
|
436
|
+
if (!earlier)
|
|
437
|
+
return re;
|
|
438
|
+
}
|
|
439
|
+
return undefined;
|
|
440
|
+
};
|
|
441
|
+
export const WAKE_BUDGET = 20;
|
|
442
|
+
export const WAKE_WINDOW_MS = 10 * 60_000;
|
|
443
|
+
export const DEDUP_WINDOW_MS = 10 * 60_000;
|
|
444
|
+
const fail = (status, reason) => ({ ok: false, status, reason });
|
|
445
|
+
/** 去重键: (author, kind, text, to, re, status) —— 10 分钟内同键视为同一次发言, 返回已有行。 */
|
|
446
|
+
const dedupKey = (author, kind, text, to, re, status) => [whoKey(author), kind, sha1(text).slice(0, 12), (to ?? []).map(whoKey).sort().join(","), (re ?? []).join(","), status ?? ""].join("|");
|
|
447
|
+
/** 请求体里的 id 列表: 缺省 = []。别写成 [x].map(String) —— undefined 会变成假 id "undefined"
|
|
448
|
+
* (2026-11-20: chat_ask 没给 after 时 after=["undefined"], 前置永不算满足, 被点名的人永不唤醒)。 */
|
|
449
|
+
export const idList = (x) => x == null ? [] : (Array.isArray(x) ? x : [x]).map((v) => String(v).trim()).filter(Boolean);
|
|
450
|
+
/** 这条人话是否已被 monitor 判过 (存量 activate trigger 行 / 新的 assign duty 都算)。 */
|
|
451
|
+
export const wasActivated = (s, msgId) => s.msgs.some((m) => m.kind === "trigger" && m.sys === "activate" && (m.re ?? []).includes(msgId))
|
|
452
|
+
|| [...s.duties.values()].some((d) => d.msg === msgId && d.via === "assign");
|
|
453
|
+
/** room_activate (感知判决) 的判定 (判决不写消息行): re 必须本 room 最后一条人话, to = 应回应名单
|
|
454
|
+
* (0..N, 逐个必须是本 room 的 wizard 成员; 去重后的空名单 = 判不回应, 调用方不落任何记录),
|
|
455
|
+
* 同一条人话只判一次 (与旧 judge 的 activate 校验同口径)。ok 之后由调用方落一条 perceive op。 */
|
|
456
|
+
export const judgeActivate = (s, actor, re, to) => {
|
|
457
|
+
if (s.members.get(whoKey(actor))?.role !== "monitor")
|
|
458
|
+
return { ok: false, status: 403, reason: "只有本 room 的 perceptron 能写感知判决" };
|
|
459
|
+
if (re.length !== 1)
|
|
460
|
+
return { ok: false, status: 400, reason: "re 只收一条 (最后一条人话的消息 id)" };
|
|
461
|
+
const target = s.byId.get(re[0]);
|
|
462
|
+
if (!target)
|
|
463
|
+
return { ok: false, status: 400, reason: "re 不在这个 room: " + re[0] };
|
|
464
|
+
if (target.author.kind !== "human" || target.kind !== "say")
|
|
465
|
+
return { ok: false, status: 400, reason: "activate 的 re 必须是本 room 的人话: " + target.id };
|
|
466
|
+
if (s.msgs.some((m) => m.seq > target.seq && m.author.kind === "human" && m.kind === "say"))
|
|
467
|
+
return { ok: false, status: 409, reason: "activate 只能激活本 room 最后一条人话 (它之后还有人说话)" };
|
|
468
|
+
if (wasActivated(s, target.id))
|
|
469
|
+
return { ok: false, status: 409, reason: "这条人话已经被激活过: " + target.id };
|
|
470
|
+
const bad = to.find((t) => t.kind !== "wizard" || !s.members.has(whoKey(t)));
|
|
471
|
+
if (bad)
|
|
472
|
+
return { ok: false, status: 400, reason: "只能激活本 room 的 wizard 成员: " + bad.id };
|
|
473
|
+
return { ok: true };
|
|
474
|
+
};
|
|
475
|
+
/** 10 分钟窗内这个 room 已经花掉的唤醒数 (从账本算, 重启不丢)。 */
|
|
476
|
+
export const wakesInWindow = (s, now, windowMs = WAKE_WINDOW_MS) => s.msgs.reduce((n, m) => (now - m.at < windowMs ? n + m.wake.length + (m.interrupt ? 1 : 0) : n), 0);
|
|
477
|
+
/** reply 的 status: 显式给的原样; 没给且调用方是 re 里某条 duty 的 holder → text ? done : passed;
|
|
478
|
+
* 没给且不是 holder → undefined (普通线程回复)。 */
|
|
479
|
+
const resolveStatus = (d, holdsAny) => d.statusGiven && d.status ? d.status : holdsAny ? (d.text.trim() ? "done" : "passed") : undefined;
|
|
480
|
+
/** 判定一条发言能不能落成行; 能 → 算好 seq / id / ord / speak / closes / wake。纯函数。 */
|
|
481
|
+
export const judge = (s, d, ctx) => {
|
|
482
|
+
const member = s.members.get(whoKey(d.author));
|
|
483
|
+
const isHuman = d.author.kind === "human";
|
|
484
|
+
const isDaemon = d.author.kind === "daemon";
|
|
485
|
+
// ① 发言权 (落盘时判, 被拒不成行)
|
|
486
|
+
let speak;
|
|
487
|
+
if (isDaemon)
|
|
488
|
+
speak = "daemon";
|
|
489
|
+
else if (isHuman)
|
|
490
|
+
speak = "human";
|
|
491
|
+
else if (member)
|
|
492
|
+
speak = "member";
|
|
493
|
+
else if (d.kind === "join-req")
|
|
494
|
+
speak = "join-req";
|
|
495
|
+
else
|
|
496
|
+
return fail(403, "非成员: 先 room_join 申请入群 (或请 host / 人邀请你)");
|
|
497
|
+
// activate 是 monitor 唯一能写的 trigger 行 (它的判决), 其余 trigger / system 只有 daemon 能写。
|
|
498
|
+
const activate = d.kind === "trigger" && d.sys === "activate";
|
|
499
|
+
if (!isDaemon && (d.kind === "system" || (d.kind === "trigger" && !activate)))
|
|
500
|
+
return fail(403, d.kind + " 行只有 daemon 能写");
|
|
501
|
+
if (activate && member?.role !== "monitor")
|
|
502
|
+
return fail(403, "activate 行只有本 room 的 monitor 能写");
|
|
503
|
+
if (d.kind === "join-req" && member)
|
|
504
|
+
return fail(409, "你已经是成员");
|
|
505
|
+
// activate 可以不带正文 (判决 "不回应" 就是一行空话)。
|
|
506
|
+
if (!d.text.trim() && d.kind !== "reply" && !d.files?.length && !activate)
|
|
507
|
+
return fail(400, "text required");
|
|
508
|
+
// ② re 必须都在本 room
|
|
509
|
+
const re = [...new Set(d.re ?? [])];
|
|
510
|
+
const missing = re.filter((id) => !s.byId.has(id));
|
|
511
|
+
if (missing.length)
|
|
512
|
+
return fail(400, "re 不在这个 room: " + missing.join(", "));
|
|
513
|
+
if (d.kind === "reply" && !re.length)
|
|
514
|
+
return fail(400, "reply 必须带 re");
|
|
515
|
+
// activate 的合法形状: re 恰一条本 room 最后一条人话; to 至多一个 wizard; 同一条人话只激活一次。
|
|
516
|
+
if (activate) {
|
|
517
|
+
if (re.length !== 1)
|
|
518
|
+
return fail(400, "activate 必须且只能带一条 re (最后一条人话的 id)");
|
|
519
|
+
const target = s.byId.get(re[0]);
|
|
520
|
+
if (target.author.kind !== "human" || target.kind !== "say")
|
|
521
|
+
return fail(400, "activate 的 re 必须是本 room 的人话: " + re[0]);
|
|
522
|
+
if (s.msgs.some((m) => m.seq > target.seq && m.author.kind === "human" && m.kind === "say"))
|
|
523
|
+
return fail(409, "activate 只能激活本 room 最后一条人话 (它之后还有人说话)");
|
|
524
|
+
if (wasActivated(s, target.id))
|
|
525
|
+
return fail(409, "这条人话已经被激活过: " + target.id);
|
|
526
|
+
if ((d.to ?? []).length > 1)
|
|
527
|
+
return fail(400, "activate 至多激活一个 wizard");
|
|
528
|
+
if ((d.to ?? []).some((t) => t.kind !== "wizard"))
|
|
529
|
+
return fail(400, "activate 只能激活 wizard");
|
|
530
|
+
}
|
|
531
|
+
// ③ status 写入权: 只有 holder (人超集; error 只有 daemon)
|
|
532
|
+
let closes = [];
|
|
533
|
+
let status;
|
|
534
|
+
if (d.kind === "reply") {
|
|
535
|
+
if (d.statusGiven && d.status === "error" && !isDaemon)
|
|
536
|
+
return fail(403, "error 只有 daemon 能写");
|
|
537
|
+
// 提问方可以撤回自己开在人名下的账 (不再需要了): 只认显式的 canceled / passed, done 仍只有人 (或人话关账) 能写。
|
|
538
|
+
const retire = !!d.statusGiven && (d.status === "canceled" || d.status === "passed");
|
|
539
|
+
const retirable = (x) => retire && holderOf(s, x)?.kind === "human" && sameWho(x.asker, d.author);
|
|
540
|
+
const mine = (id) => openDuties(s).filter((x) => x.msg === id && (isHuman || isDaemon ? true : holderMatches(s, x, d.author) || retirable(x)));
|
|
541
|
+
const holds = re.map((id) => ({ id, duties: mine(id) }));
|
|
542
|
+
const holdsAny = holds.some((h) => h.duties.length);
|
|
543
|
+
status = resolveStatus(d, holdsAny);
|
|
544
|
+
if (status && !isHuman && !isDaemon) {
|
|
545
|
+
const notMine = holds.filter((h) => !h.duties.length).map((h) => h.id);
|
|
546
|
+
if (notMine.length)
|
|
547
|
+
return fail(403, "status 只有 duty 的处理人能写; 这几条不归你 (或已关): " + notMine.join(", "));
|
|
548
|
+
}
|
|
549
|
+
if (status && CLOSING.has(status)) {
|
|
550
|
+
// ④ 覆盖检查: 提问者在线程里有 holder 还没读到的补充 → 先读
|
|
551
|
+
const holderSeen = member?.seen ?? Number.MAX_SAFE_INTEGER;
|
|
552
|
+
for (const h of holds)
|
|
553
|
+
for (const duty of h.duties) {
|
|
554
|
+
// 提问者自己撤回: 没有「提问者的补充」要先读。
|
|
555
|
+
const newer = sameWho(duty.asker, d.author) ? [] : s.msgs.filter((m) => sameWho(m.author, duty.asker) && m.seq > holderSeen && (m.re ?? []).includes(duty.msg));
|
|
556
|
+
if (newer.length && !isHuman && !isDaemon)
|
|
557
|
+
return fail(409, "先读 seq " + newer[newer.length - 1].seq + ": 提问者对 " + duty.msg + " 有你还没读到的补充");
|
|
558
|
+
closes.push(duty.key);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
else if (d.statusGiven)
|
|
563
|
+
return fail(400, "status 只用于 reply");
|
|
564
|
+
// 人话关账: 人的下一句话关掉它名下 (本 room) 未关的 duty —— 带 re 且 re 命中其中几条就只关被引用的,
|
|
565
|
+
// 否则 (没引用 / 引用的不是它的账) 全关。被关账的提问 wizard 就是这句话的收话人 (judge 补进 to, 下面)。
|
|
566
|
+
if (isHuman && d.kind === "say")
|
|
567
|
+
closes = humanCloses(s, d.author, re);
|
|
568
|
+
// ⑤ to 只认成员 (人话的 .name 点名由调用方先拉进来)
|
|
569
|
+
const given = (d.to ?? []).filter((t, i, a) => a.findIndex((x) => sameWho(x, t)) === i);
|
|
570
|
+
if (!isDaemon) {
|
|
571
|
+
const outsiders = given.filter((t) => !s.members.has(whoKey(t)));
|
|
572
|
+
if (outsiders.length)
|
|
573
|
+
return fail(409, "这些不是本 room 成员: " + outsiders.map((t) => t.id).join(", ") + " — 先邀请, 或改去 dm room");
|
|
574
|
+
}
|
|
575
|
+
if ((d.kind === "ask" || d.kind === "task") && !given.some((t) => t.kind === "wizard" || t.kind === "human"))
|
|
576
|
+
return fail(400, d.kind + " 至少点名一个 wizard 或人成员");
|
|
577
|
+
// judge 自己补的收话人 (落进行里, 重放不再推断): need → 被反问的人 (开 need duty); 人话关账 → 提问的 wizard。
|
|
578
|
+
const addressees = judgeAddressees(s, d, re, status, closes);
|
|
579
|
+
const to = [...given, ...addressees.filter((a) => !given.some((t) => sameWho(t, a)))];
|
|
580
|
+
// ⑥ interrupt 限权: 直属 parent / host / 人
|
|
581
|
+
if (d.interrupt) {
|
|
582
|
+
if (d.kind !== "ask" && d.kind !== "task" && d.kind !== "say")
|
|
583
|
+
return fail(400, "interrupt 只用于 chat_ask / chat_say");
|
|
584
|
+
const targets = to.filter((t) => t.kind === "wizard");
|
|
585
|
+
if (!targets.length)
|
|
586
|
+
return fail(400, "interrupt 要点名被打断的 wizard");
|
|
587
|
+
const host = roomHost(s);
|
|
588
|
+
const allowed = (t) => isHuman || sameWho(host, d.author) || !!ctx.isParent?.(d.author, t);
|
|
589
|
+
const denied = targets.filter((t) => !allowed(t));
|
|
590
|
+
if (denied.length)
|
|
591
|
+
return fail(403, "interrupt 只有直属 parent / 本 room 的 host / 人能用: " + denied.map((t) => t.id).join(", "));
|
|
592
|
+
}
|
|
593
|
+
// ⑦ 去重 (幂等: 返回已有行)
|
|
594
|
+
const key = dedupKey(d.author, d.kind, d.text, to, re, status);
|
|
595
|
+
const dup = [...s.msgs].reverse().find((m) => ctx.now - m.at < DEDUP_WINDOW_MS && dedupKey(m.author, m.kind, m.text, m.to, m.re, m.status) === key);
|
|
596
|
+
if (dup && d.origin !== "inbound")
|
|
597
|
+
return { ok: true, row: dup, duplicate: true, degraded: [] };
|
|
598
|
+
// ⑧ 唤醒 + 回合预算
|
|
599
|
+
// 绑定 room 里没点名、也没 @ 机器人的人话: 有 perceptron 且存在候选 (≥1, 2026-11-24 人二次拍板:
|
|
600
|
+
// 单管家也要感知) 时交给它处置 (只唤醒它, 不在 host 槽位开 duty, 不回落 host); 否则照旧落 host
|
|
601
|
+
// (没有 perceptron 的 room: 单聊 / .me / 引导中)。迁移重放的行不进感知。
|
|
602
|
+
const toMonitor = isHuman && d.kind === "say" && isBoundRoom(s.id) && d.mention !== "bot"
|
|
603
|
+
&& d.origin !== "migrate" && !to.some((t) => t.kind === "wizard") && hasMonitor(s) && perceptronWanted(s);
|
|
604
|
+
const seq = s.seq + 1;
|
|
605
|
+
const draftRow = {
|
|
606
|
+
op: "msg", at: ctx.now, room: s.id, id: msgIdOf(s.id, seq), seq, author: d.author, ord: member?.ord ?? 0, speak,
|
|
607
|
+
kind: d.kind, text: d.text, ...(to.length ? { to } : {}), ...(re.length ? { re } : {}), ...(status ? { status } : {}),
|
|
608
|
+
...(d.mention ? { mention: d.mention } : {}), ...(d.post ? { post: d.post } : {}), ...(d.src ? { src: d.src } : {}), ...(toMonitor ? { monitor: true } : {}),
|
|
609
|
+
...(closes.length ? { closes } : {}), wake: [], ...(d.interrupt ? { interrupt: true } : {}),
|
|
610
|
+
...(d.for ? { for: d.for } : {}), ...(d.after?.length ? { after: d.after } : {}), ...(d.planId ? { planId: d.planId } : {}),
|
|
611
|
+
...(d.files?.length ? { files: d.files } : {}), origin: d.origin, ...(d.ext ? { ext: d.ext } : {}),
|
|
612
|
+
...(d.bubble ? { bubble: d.bubble } : {}), ...(d.sys ? { sys: d.sys } : {}),
|
|
613
|
+
};
|
|
614
|
+
const want = wakeTargets(s, draftRow, ctx);
|
|
615
|
+
const budget = ctx.budget ?? WAKE_BUDGET;
|
|
616
|
+
const used = wakesInWindow(s, ctx.now);
|
|
617
|
+
if (d.interrupt && used + want.length + 1 > budget)
|
|
618
|
+
return fail(409, "本 room 10 分钟内的唤醒预算 (" + budget + ") 已用完, interrupt 不放行");
|
|
619
|
+
const room = Math.max(0, budget - used);
|
|
620
|
+
const wake = want.slice(0, room);
|
|
621
|
+
const degraded = want.slice(room);
|
|
622
|
+
return { ok: true, row: { ...draftRow, wake }, degraded };
|
|
623
|
+
};
|
|
624
|
+
/** 这一行该唤醒谁 (wizard uid, 不含作者自己)。B-3: 点名 + host 兜底 + 订阅, 其余只推游标。
|
|
625
|
+
* 人话关了谁名下的账, 谁 (提问者) 就在 `to` 里 (judge 补的, 见 judgeAddressees) —— 走 named, 与 host 兜底天然去重。 */
|
|
626
|
+
export const wakeTargets = (s, row, ctx = {}) => {
|
|
627
|
+
const out = [];
|
|
628
|
+
const add = (w) => {
|
|
629
|
+
if (w?.kind === "wizard" && !sameWho(w, row.author) && !out.includes(w.id))
|
|
630
|
+
out.push(w.id);
|
|
631
|
+
};
|
|
632
|
+
const named = (row.to ?? []).filter((t) => t.kind === "wizard");
|
|
633
|
+
const closedDone = ctx.closedDone ?? ((m) => msgClosedDone(s, m));
|
|
634
|
+
switch (row.kind) {
|
|
635
|
+
case "ask":
|
|
636
|
+
case "task":
|
|
637
|
+
if (afterSatisfied(row.after, closedDone))
|
|
638
|
+
named.forEach(add);
|
|
639
|
+
break;
|
|
640
|
+
case "join-req":
|
|
641
|
+
add(roomHost(s));
|
|
642
|
+
break;
|
|
643
|
+
case "reply": {
|
|
644
|
+
if (row.status === "progress")
|
|
645
|
+
break;
|
|
646
|
+
for (const id of row.re ?? [])
|
|
647
|
+
add(s.byId.get(id)?.author);
|
|
648
|
+
break;
|
|
649
|
+
}
|
|
650
|
+
case "trigger":
|
|
651
|
+
named.forEach(add);
|
|
652
|
+
break;
|
|
653
|
+
default:
|
|
654
|
+
named.forEach(add);
|
|
655
|
+
// 绑定 room 里没点名的人话 → monitor (有的话, 不开 duty) / host。@ 了机器人的直达 host。
|
|
656
|
+
if (row.author.kind === "human" && !named.length && isBoundRoom(row.room)) {
|
|
657
|
+
if (row.monitor)
|
|
658
|
+
monitorsOf(s).forEach(add);
|
|
659
|
+
else
|
|
660
|
+
add(roomHost(s));
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
// 订阅 (B-3): 只在人话 / 带终态的 reply 时唤醒订阅者
|
|
664
|
+
const watchable = (row.author.kind === "human" && row.kind === "say") || (row.kind === "reply" && !!row.status && CLOSING.has(row.status));
|
|
665
|
+
if (watchable)
|
|
666
|
+
for (const m of s.members.values())
|
|
667
|
+
if (m.watch)
|
|
668
|
+
add(m.who);
|
|
669
|
+
return out;
|
|
670
|
+
};
|
|
671
|
+
// ── 离开 (§4): 人提的账交给下一位 host, 其余由 daemon 写 canceled ──────
|
|
672
|
+
/** 成员离开要落的行。每条它名下 (静态槽位) 未关的 duty:
|
|
673
|
+
* - 提问者是人 → 不取消, move 给下一位 host + 一条 trigger 唤醒它 (人问的话不许以「没有答案了」收场,
|
|
674
|
+
* 2026-11-24 人报: 人回复某 wizard 后它退出, 两条话当场被取消; 应转给 host);
|
|
675
|
+
* - 否则 (wizard 提问) → 一行 canceled reply, 唤醒提问者。
|
|
676
|
+
* host 槽位的 duty 不取消 —— 它们随 host 求值自然落到下一位。 */
|
|
677
|
+
export const leaveOps = (s, who, by, now, why = "") => {
|
|
678
|
+
const mine = openDuties(s).filter((d) => d.slot !== HOST_SLOT && d.slot === whoKey(who));
|
|
679
|
+
const byMsg = new Map();
|
|
680
|
+
for (const d of mine)
|
|
681
|
+
byMsg.set(d.msg, [...(byMsg.get(d.msg) ?? []), d]);
|
|
682
|
+
// 下一位 host = 去掉它之后加入序最小的 wizard 成员 (monitor 不当 host)。
|
|
683
|
+
const next = hostOf([...s.members.values()].filter((m) => !sameWho(m.who, who)));
|
|
684
|
+
let seq = s.seq;
|
|
685
|
+
const rows = [];
|
|
686
|
+
const moves = [];
|
|
687
|
+
for (const [msg, ds] of byMsg.entries()) {
|
|
688
|
+
seq += 1;
|
|
689
|
+
const asker = ds[0].asker;
|
|
690
|
+
const ask = s.byId.get(msg);
|
|
691
|
+
if (next && ask && asker.kind === "human") {
|
|
692
|
+
rows.push({
|
|
693
|
+
op: "msg", at: now, room: s.id, id: msgIdOf(s.id, seq), seq, author: DAEMON, ord: 0, speak: "daemon",
|
|
694
|
+
kind: "trigger", text: "[wezard] " + who.id + " 已离开" + (why ? " (" + why + ")" : "") + ", 人问的这条转给本 room 的 host。\n原话 (来自 " + asker.id + "): " + ask.text,
|
|
695
|
+
to: [next], re: [msg], wake: [next.id], origin: "daemon", sys: "left-handoff",
|
|
696
|
+
});
|
|
697
|
+
moves.push(...ds.map((d) => ({ op: "move", at: now, room: s.id, duty: d.msg, from: who, to: next, by })));
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
rows.push({
|
|
701
|
+
op: "msg", at: now, room: s.id, id: msgIdOf(s.id, seq), seq, author: DAEMON, ord: 0, speak: "daemon",
|
|
702
|
+
kind: "reply", text: "[wezard] " + who.id + " 已离开" + (why ? " (" + why + ")" : "") + ", 这条没有答案了", re: [msg],
|
|
703
|
+
status: "canceled", closes: ds.map((d) => d.key), wake: asker.kind === "wizard" && !sameWho(asker, who) ? [asker.id] : [],
|
|
704
|
+
origin: "daemon", sys: "left",
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
return [...rows, ...moves, { op: "leave", at: now, room: s.id, who, by, ...(why ? { why } : {}) }];
|
|
708
|
+
};
|
|
709
|
+
const statusTag = (m) => (m.status ? " [" + m.status + "]" : "");
|
|
710
|
+
/** 行正文的读法: 人话行 (origin inbound) 曾把提醒尾巴一起写进 text, 老行不改写、读侧剥掉 (见 stripLegacyTail);
|
|
711
|
+
* 新行只存原话, 剥一遍是空操作。只有 inbound 行带过尾巴, 别的来源的 text 原样返回。 */
|
|
712
|
+
export const rowText = (m) => (m.origin === "inbound" ? stripLegacyTail(m.text) : m.text);
|
|
713
|
+
/** 行 id 的短形: 同一个 room 的块里 hash 段每行都重复, 只留 seq (块头已有 #hash8)。
|
|
714
|
+
* `hash` = 本块的 roomHash; 给了且 id 的前缀正好是它才短 —— 老路径 / 跨 room 一律全形。 */
|
|
715
|
+
const shortId = (hash, id) => hash && id.startsWith(hash + ".") ? id.slice(hash.length + 1) : undefined;
|
|
716
|
+
/** id 列表的紧凑形: 每一个都在本块 → `re=118,119`; 有一个不在 → 退回全形 `(re a,b)` (可解析)。 */
|
|
717
|
+
const reTag = (hash, re) => {
|
|
718
|
+
if (!re?.length)
|
|
719
|
+
return "";
|
|
720
|
+
const shorts = re.map((id) => shortId(hash, id));
|
|
721
|
+
return shorts.every((x) => !!x) ? " re=" + shorts.join(",") : " (re " + re.join(",") + ")";
|
|
722
|
+
};
|
|
723
|
+
export const renderMsgLine = (m, n, max = 500, hash) => {
|
|
724
|
+
const id = shortId(hash, m.id) ?? m.id;
|
|
725
|
+
// monitor 的 activate 判决是系统行为, 不是 monitor 发言 (§2.2): 输出系统形态, 不以 monitor 名义出现。
|
|
726
|
+
// 只精确匹配 sys:"activate"; 其他 trigger (after-wake / overdue / 定时任务) 照旧按消息行渲染 (author 是 wezard, 带正文)。
|
|
727
|
+
if (m.kind === "trigger" && m.sys === "activate") {
|
|
728
|
+
const targets = (m.to ?? []).filter((t) => t.kind === "wizard").map(n.nameOf).join(", ");
|
|
729
|
+
return "[" + id + "] [" + n.nameOf(DAEMON) + "] monitor 判决: " + (targets ? "交给 " + targets + " 回应" : "不回应") + reTag(hash, m.re);
|
|
730
|
+
}
|
|
731
|
+
const to = m.to?.length ? " → " + m.to.map(n.nameOf).join(", ") : "";
|
|
732
|
+
const body = rowText(m).replace(/\s+/g, " ").trim();
|
|
733
|
+
const files = m.files?.length ? " 📎" + m.files.map((f) => f.name).join(",") : "";
|
|
734
|
+
return "[" + id + "] " + n.nameOf(m.author) + to + reTag(hash, m.re) + statusTag(m) + ": " + (body.length > max ? body.slice(0, max) + "…(+" + (body.length - max) + ")" : body) + files;
|
|
735
|
+
};
|
|
736
|
+
/** who 的游标之后、别人说的行数 (不含自己) —— rolepage 的未读角标; 与 unreadFor 同一口径, 不装预算。 */
|
|
737
|
+
export const unreadCount = (s, who) => {
|
|
738
|
+
const seen = s.members.get(whoKey(who))?.seen ?? 0;
|
|
739
|
+
return s.msgs.reduce((n, m) => (m.seq > seen && !sameWho(m.author, who) ? n + 1 : n), 0);
|
|
740
|
+
};
|
|
741
|
+
export const UNREAD_BUDGET = 3000;
|
|
742
|
+
/** 游标之后、who 没看过的行 (不含自己说的), 触发行全文, 其余从新到旧装到预算为止。 */
|
|
743
|
+
export const unreadFor = (s, who, triggers = [], budget = UNREAD_BUDGET) => {
|
|
744
|
+
const seen = s.members.get(whoKey(who))?.seen ?? 0;
|
|
745
|
+
const eligible = s.msgs.filter((m) => m.seq > seen && !sameWho(m.author, who));
|
|
746
|
+
const picked = [];
|
|
747
|
+
let used = 0;
|
|
748
|
+
for (let i = eligible.length - 1; i >= 0; i--) {
|
|
749
|
+
const m = eligible[i];
|
|
750
|
+
const cost = triggers.includes(m.id) ? 0 : Math.min(rowText(m).length, 500);
|
|
751
|
+
if (picked.length && used + cost > budget && !triggers.includes(m.id))
|
|
752
|
+
continue;
|
|
753
|
+
picked.unshift(m);
|
|
754
|
+
used += cost;
|
|
755
|
+
}
|
|
756
|
+
return { rows: picked, more: eligible.length - picked.length, to: s.seq };
|
|
757
|
+
};
|
|
758
|
+
/** 入座时的初始 seen 游标 (null = 不写, 保持 0 = 全部可读); 输入不合法 → {error}。纯函数。
|
|
759
|
+
* seen 只往前推, 所以新成员 (从 0 起) 写这条 seen 就等于「只看游标之后」。 */
|
|
760
|
+
export const historyCursor = (s, history) => {
|
|
761
|
+
if (history === undefined || history === null || history === "" || history === "all")
|
|
762
|
+
return null;
|
|
763
|
+
if (history === "none")
|
|
764
|
+
return s.seq;
|
|
765
|
+
const asNum = typeof history === "number" ? history : typeof history === "string" && /^\d+$/.test(history.trim()) ? Number(history) : NaN;
|
|
766
|
+
if (Number.isFinite(asNum)) {
|
|
767
|
+
if (!Number.isInteger(asNum) || asNum < 1)
|
|
768
|
+
return { error: "history 的数字要是正整数 (得到 " + asNum + ")" };
|
|
769
|
+
return s.msgs.length <= asNum ? null : s.msgs[s.msgs.length - asNum].seq - 1;
|
|
770
|
+
}
|
|
771
|
+
if (typeof history === "string" && history.startsWith("from:")) {
|
|
772
|
+
const row = s.byId.get(history.slice(5).trim());
|
|
773
|
+
return row ? row.seq - 1 : { error: "history 的 from: 指向的消息不在本 room: " + history.slice(5) };
|
|
774
|
+
}
|
|
775
|
+
return { error: 'history 取值: "none" | 正整数 N | "from:<msgId>" | "all"' };
|
|
776
|
+
};
|
|
777
|
+
/** 入座那一批 op 后面跟的 seen (游标 ≤ 0 或 null 时没有)。 */
|
|
778
|
+
export const seatSeenOps = (s, who, at, history) => {
|
|
779
|
+
const c = historyCursor(s, history);
|
|
780
|
+
if (c !== null && typeof c === "object")
|
|
781
|
+
return c;
|
|
782
|
+
return c ? [{ op: "seen", at, room: s.id, who, seq: c }] : [];
|
|
783
|
+
};
|
|
784
|
+
/** dm:A|B 现状 → 怎么用: 不存在 → 建; 只有 A/B → 用 (缺人先补回); 混进第三人 → 不再落进它, 另找 / 另开 adhoc。 */
|
|
785
|
+
export const planDm = (s, a, b) => {
|
|
786
|
+
if (!s)
|
|
787
|
+
return { kind: "create" };
|
|
788
|
+
const ms = [...s.members.values()].map((m) => m.who);
|
|
789
|
+
if (ms.some((w) => !(w.kind === "wizard" && (w.id === a || w.id === b))))
|
|
790
|
+
return { kind: "pair-adhoc" };
|
|
791
|
+
const missing = [a, b].filter((u) => !s.members.has(whoKey({ kind: "wizard", id: u })));
|
|
792
|
+
return missing.length ? { kind: "rejoin", missing } : { kind: "use" };
|
|
793
|
+
};
|
|
794
|
+
/** 在 adhoc room 里找当前成员恰好等于 uids (wizard uid 集合, 顺序无关) 的那个; 多个取最近活动 (最后一条消息, 无则 open) 的。 */
|
|
795
|
+
export const findSetAdhoc = (states, uids) => {
|
|
796
|
+
const want = new Set(uids.map((u) => whoKey({ kind: "wizard", id: u })));
|
|
797
|
+
return states
|
|
798
|
+
.filter((s) => s.id.startsWith("adhoc:") && s.members.size === want.size && [...want].every((k) => s.members.has(k)))
|
|
799
|
+
.map((s) => ({ id: s.id, at: s.msgs.length ? s.msgs[s.msgs.length - 1].at : s.openedAt ?? 0 }))
|
|
800
|
+
.sort((x, y) => y.at - x.at)[0]?.id;
|
|
801
|
+
};
|
|
802
|
+
/** 两人的特例。 */
|
|
803
|
+
export const findPairAdhoc = (states, a, b) => findSetAdhoc(states, [a, b]);
|
|
804
|
+
/** parent 是不是 child 的直属 parent (judge ⑥ interrupt 限权的判据; hub 与自动 adhoc 落点共用)。 */
|
|
805
|
+
export const isDirectParent = (parentUidOf, parent, child) => parent.kind === "wizard" && child.kind === "wizard" && parentUidOf(child.id) === parent.id;
|
|
806
|
+
/** 「你要交代」的用法提示这次该不该附: 它最近 window 次成功唤醒批次 (跨 room 按 at 去重) 里写过任何 chat_reply 行 → 不附; 批次不足 window / 窗口内没写过 / window ≤ 0 → 附。
|
|
807
|
+
* states = 它所在的各 room; 只从尾往前扫到窗口起点为止。 */
|
|
808
|
+
export const replyHintDue = (states, uid, window) => {
|
|
809
|
+
if (window <= 0)
|
|
810
|
+
return true;
|
|
811
|
+
const me = whoKey({ kind: "wizard", id: uid });
|
|
812
|
+
const ats = [...new Set(states.flatMap((s) => s.members.get(me)?.batches ?? []))].sort((a, b) => b - a);
|
|
813
|
+
if (ats.length < window)
|
|
814
|
+
return true;
|
|
815
|
+
const cutoff = ats[window - 1];
|
|
816
|
+
return !states.some((s) => {
|
|
817
|
+
for (let i = s.msgs.length - 1; i >= 0; i--) {
|
|
818
|
+
const r = s.msgs[i];
|
|
819
|
+
if (r.at < cutoff)
|
|
820
|
+
return false;
|
|
821
|
+
if (r.kind === "reply" && r.author.kind === "wizard" && r.author.id === uid)
|
|
822
|
+
return true;
|
|
823
|
+
}
|
|
824
|
+
return false;
|
|
825
|
+
});
|
|
826
|
+
};
|
|
827
|
+
//# sourceMappingURL=room.js.map
|