dsh-chat-comprehension 0.2.0 → 0.3.1
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/NOTICE.md +20 -0
- package/README.md +20 -9
- package/client.js +41 -9
- package/lib/engines.mjs +136 -0
- package/lib/index.js +54 -4
- package/lib/qqbot-0.5.3-patches.json +75 -70
- package/lib/qqbot-patch.mjs +12 -7
- package/lib/video.mjs +11 -2
- package/package.json +7 -23
- package/vendor/dsh-agent-md/LICENSE +21 -0
- package/vendor/dsh-agent-md/cordis.patch.yml +8 -0
- package/vendor/dsh-agent-md/lib/index.js +775 -0
- package/vendor/dsh-agent-md/lib/persona.js +634 -0
- package/vendor/dsh-agent-md/lib/qq.js +527 -0
- package/vendor/dsh-agent-md/package.json +50 -0
- package/vendor/dsh-bili-scroll/LICENSE +21 -0
- package/vendor/dsh-bili-scroll/NOTICE.md +18 -0
- package/vendor/dsh-bili-scroll/cordis.patch.yml +20 -0
- package/vendor/dsh-bili-scroll/index.js +3293 -0
- package/vendor/dsh-bili-scroll/lib/asr.py +141 -0
- package/vendor/dsh-bili-scroll/lib/diagnose.mjs +95 -0
- package/vendor/dsh-bili-scroll/lib/dm.mjs +110 -0
- package/vendor/dsh-bili-scroll/lib/favops.py +90 -0
- package/vendor/dsh-bili-scroll/lib/hostdeps.mjs +148 -0
- package/vendor/dsh-bili-scroll/lib/login.mjs +141 -0
- package/vendor/dsh-bili-scroll/lib/mcp.mjs +144 -0
- package/vendor/dsh-bili-scroll/lib/state.mjs +236 -0
- package/vendor/dsh-bili-scroll/lib/uid.mjs +61 -0
- package/vendor/dsh-bili-scroll/lib/uvfetch.mjs +217 -0
- package/vendor/dsh-bili-scroll/lib/verify.mjs +184 -0
- package/vendor/dsh-bili-scroll/package.json +84 -0
- package/vendor/dsh-bili-scroll/profile-template/bili-duty/cordis.patch.yml +116 -0
- package/vendor/dsh-bili-scroll/profile-template/bili-duty/cordis.yml +4 -0
- package/vendor/dsh-bili-scroll/profile-template/bili-duty/package.json +13 -0
- package/vendor/dsh-bili-scroll/prompts/duty.md +296 -0
- package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/LICENSE +21 -0
- package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/README.md +176 -0
- package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/mcp_server.py +2015 -0
- package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/requirements.txt +10 -0
- package/vendor/dsh-meme/.dsh/skills/dsh-meme-maintenance/SKILL.md +50 -0
- package/vendor/dsh-meme/LICENSE +21 -0
- package/vendor/dsh-meme/anim.js +588 -0
- package/vendor/dsh-meme/cordis.patch.yml +13 -0
- package/vendor/dsh-meme/icon.jpg +0 -0
- package/vendor/dsh-meme/index.js +2616 -0
- package/vendor/dsh-meme/locale/en.json +6 -0
- package/vendor/dsh-meme/locale/zh.json +6 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/index.db +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/manifest.json +13 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/angry/shengqi.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/baka/xianqi.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/color/huachi.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/confused/jiazai.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/confused/wenhao.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/cpu/jiaban.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/daily/ganfan.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/fool/buyao.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/givemoney/yaoqian.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/happy/deyi.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/happy/ok.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/happy/piaole.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/like/bixin.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/meow/maozhua.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/morning/zaoshanghao.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/sad/qiurao.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/sad/yun.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/see/zhenjing.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/shy/haixiu.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/shy/toukan.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/sigh/wuyu.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/sleep/shuijiao.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/surprised/xia.jpg +0 -0
- package/vendor/dsh-meme/memes/dafeiyu-001/memes/work/tangping.jpg +0 -0
- package/vendor/dsh-meme/memes.js +998 -0
- package/vendor/dsh-meme/package.json +67 -0
- package/vendor/dsh-meme/panel.html +210 -0
- package/vendor/dsh-video-storyboard/LICENSE +21 -0
- package/vendor/dsh-video-storyboard/NOTICE +12 -0
- package/vendor/dsh-video-storyboard/bin/_args.mjs +16 -0
- package/vendor/dsh-video-storyboard/bin/asr-check.mjs +85 -0
- package/vendor/dsh-video-storyboard/bin/scan-stats.mjs +43 -0
- package/vendor/dsh-video-storyboard/bin/scan-video.mjs +38 -0
- package/vendor/dsh-video-storyboard/bin/storyboard.mjs +195 -0
- package/vendor/dsh-video-storyboard/cordis.patch.yml +3 -0
- package/vendor/dsh-video-storyboard/dsh.plugin.json +12 -0
- package/vendor/dsh-video-storyboard/package.json +88 -0
- package/vendor/dsh-video-storyboard/scripts/check-syntax.mjs +56 -0
- package/vendor/dsh-video-storyboard/scripts/install-dev.mjs +104 -0
- package/vendor/dsh-video-storyboard/src/flicker.js +104 -0
- package/vendor/dsh-video-storyboard/src/index.js +82 -0
- package/vendor/dsh-video-storyboard/src/ocr_bridge.py +103 -0
- package/vendor/dsh-video-storyboard/src/storyboard/asr-lifecycle.js +262 -0
- package/vendor/dsh-video-storyboard/src/storyboard/asr.js +488 -0
- package/vendor/dsh-video-storyboard/src/storyboard/audio.js +168 -0
- package/vendor/dsh-video-storyboard/src/storyboard/binaries.js +69 -0
- package/vendor/dsh-video-storyboard/src/storyboard/board.js +121 -0
- package/vendor/dsh-video-storyboard/src/storyboard/browser-time.js +504 -0
- package/vendor/dsh-video-storyboard/src/storyboard/cache.js +143 -0
- package/vendor/dsh-video-storyboard/src/storyboard/cells.js +70 -0
- package/vendor/dsh-video-storyboard/src/storyboard/density.js +545 -0
- package/vendor/dsh-video-storyboard/src/storyboard/direct-url.js +130 -0
- package/vendor/dsh-video-storyboard/src/storyboard/fetch.js +373 -0
- package/vendor/dsh-video-storyboard/src/storyboard/flash.js +180 -0
- package/vendor/dsh-video-storyboard/src/storyboard/format.js +573 -0
- package/vendor/dsh-video-storyboard/src/storyboard/frames.js +212 -0
- package/vendor/dsh-video-storyboard/src/storyboard/index.js +32 -0
- package/vendor/dsh-video-storyboard/src/storyboard/injection.js +76 -0
- package/vendor/dsh-video-storyboard/src/storyboard/layout-order.js +176 -0
- package/vendor/dsh-video-storyboard/src/storyboard/locate.js +113 -0
- package/vendor/dsh-video-storyboard/src/storyboard/media-store.js +106 -0
- package/vendor/dsh-video-storyboard/src/storyboard/memory.js +94 -0
- package/vendor/dsh-video-storyboard/src/storyboard/ocr-subs.js +107 -0
- package/vendor/dsh-video-storyboard/src/storyboard/ocr.js +167 -0
- package/vendor/dsh-video-storyboard/src/storyboard/pipeline.js +853 -0
- package/vendor/dsh-video-storyboard/src/storyboard/plan.js +400 -0
- package/vendor/dsh-video-storyboard/src/storyboard/probe.js +132 -0
- package/vendor/dsh-video-storyboard/src/storyboard/provenance.js +44 -0
- package/vendor/dsh-video-storyboard/src/storyboard/python.js +84 -0
- package/vendor/dsh-video-storyboard/src/storyboard/register.js +32 -0
- package/vendor/dsh-video-storyboard/src/storyboard/scan.js +486 -0
- package/vendor/dsh-video-storyboard/src/storyboard/segments.js +117 -0
- package/vendor/dsh-video-storyboard/src/storyboard/sheet.js +507 -0
- package/vendor/dsh-video-storyboard/src/storyboard/storage.js +93 -0
- package/vendor/dsh-video-storyboard/src/storyboard/subtitles.js +181 -0
- package/vendor/dsh-video-storyboard/src/storyboard/tool-spec.js +544 -0
- package/vendor/dsh-video-storyboard/src/storyboard/transcript-file.js +146 -0
- package/vendor/dsh-video-storyboard/src/storyboard/url.js +69 -0
- package/vendor/dsh-video-storyboard/src/storyboard/ytdlp.js +289 -0
- package/vendor/dsh-video-storyboard/src/tool-spec.js +17 -0
- package/vendor/dsh-video-storyboard/src/tools/locate.js +39 -0
- package/vendor/dsh-video-storyboard/src/tools/probe.js +23 -0
- package/vendor/dsh-video-storyboard/src/tools/shared.js +39 -0
- package/vendor/dsh-video-storyboard/src/tools/storyboard.js +20 -0
- package/vendor/dsh-video-storyboard/src/tools/zoom.js +56 -0
- package/vendor/dsh-world/LICENSE +21 -0
- package/vendor/dsh-world/assets/qq-card.png +0 -0
- package/vendor/dsh-world/cordis.patch.yml +6 -0
- package/vendor/dsh-world/lib/archive.d.ts +6 -0
- package/vendor/dsh-world/lib/archive.js +49 -0
- package/vendor/dsh-world/lib/archiveSelection.d.ts +8 -0
- package/vendor/dsh-world/lib/archiveSelection.js +20 -0
- package/vendor/dsh-world/lib/compaction.d.ts +124 -0
- package/vendor/dsh-world/lib/compaction.js +273 -0
- package/vendor/dsh-world/lib/compactionRegion.d.ts +42 -0
- package/vendor/dsh-world/lib/compactionRegion.js +210 -0
- package/vendor/dsh-world/lib/console/routes.d.ts +57 -0
- package/vendor/dsh-world/lib/console/routes.js +129 -0
- package/vendor/dsh-world/lib/console/snapshot.d.ts +39 -0
- package/vendor/dsh-world/lib/console/snapshot.js +51 -0
- package/vendor/dsh-world/lib/ecosystem/bridge/bridge.d.ts +36 -0
- package/vendor/dsh-world/lib/ecosystem/bridge/bridge.js +140 -0
- package/vendor/dsh-world/lib/ecosystem/bridge/route.d.ts +9 -0
- package/vendor/dsh-world/lib/ecosystem/bridge/route.js +46 -0
- package/vendor/dsh-world/lib/ecosystem/qq/bridge.d.ts +123 -0
- package/vendor/dsh-world/lib/ecosystem/qq/bridge.js +380 -0
- package/vendor/dsh-world/lib/ecosystem/qq/panel.d.ts +75 -0
- package/vendor/dsh-world/lib/ecosystem/qq/panel.js +187 -0
- package/vendor/dsh-world/lib/ecosystem/qq/route.d.ts +32 -0
- package/vendor/dsh-world/lib/ecosystem/qq/route.js +134 -0
- package/vendor/dsh-world/lib/ecosystem/qq/supervisor.d.ts +57 -0
- package/vendor/dsh-world/lib/ecosystem/qq/supervisor.js +188 -0
- package/vendor/dsh-world/lib/ecosystem/shared/context.d.ts +14 -0
- package/vendor/dsh-world/lib/ecosystem/shared/context.js +42 -0
- package/vendor/dsh-world/lib/ecosystem/shared/identity.d.ts +15 -0
- package/vendor/dsh-world/lib/ecosystem/shared/identity.js +36 -0
- package/vendor/dsh-world/lib/ecosystem/shared/outlets.d.ts +43 -0
- package/vendor/dsh-world/lib/ecosystem/shared/outlets.js +70 -0
- package/vendor/dsh-world/lib/ecosystem/shared/sessions.d.ts +16 -0
- package/vendor/dsh-world/lib/ecosystem/shared/sessions.js +52 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/archive.d.ts +24 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/archive.js +62 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/home.d.ts +21 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/home.js +111 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/images.d.ts +5 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/images.js +43 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/index.d.ts +41 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/index.js +166 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/messages.d.ts +24 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/messages.js +35 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/sending.d.ts +30 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/sending.js +216 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/supervisor.d.ts +39 -0
- package/vendor/dsh-world/lib/ecosystem/wechat/supervisor.js +138 -0
- package/vendor/dsh-world/lib/faults.d.ts +10 -0
- package/vendor/dsh-world/lib/faults.js +13 -0
- package/vendor/dsh-world/lib/history.d.ts +16 -0
- package/vendor/dsh-world/lib/history.js +80 -0
- package/vendor/dsh-world/lib/hostContract.d.ts +19 -0
- package/vendor/dsh-world/lib/hostContract.js +1 -0
- package/vendor/dsh-world/lib/http.d.ts +18 -0
- package/vendor/dsh-world/lib/http.js +39 -0
- package/vendor/dsh-world/lib/index.d.ts +108 -0
- package/vendor/dsh-world/lib/index.js +356 -0
- package/vendor/dsh-world/lib/life/activity.d.ts +14 -0
- package/vendor/dsh-world/lib/life/activity.js +58 -0
- package/vendor/dsh-world/lib/life/alarm.d.ts +39 -0
- package/vendor/dsh-world/lib/life/alarm.js +132 -0
- package/vendor/dsh-world/lib/life/alarmTimer.d.ts +22 -0
- package/vendor/dsh-world/lib/life/alarmTimer.js +49 -0
- package/vendor/dsh-world/lib/life/diary.d.ts +4 -0
- package/vendor/dsh-world/lib/life/diary.js +9 -0
- package/vendor/dsh-world/lib/life/earlyStop.d.ts +19 -0
- package/vendor/dsh-world/lib/life/earlyStop.js +25 -0
- package/vendor/dsh-world/lib/life/home.d.ts +34 -0
- package/vendor/dsh-world/lib/life/home.js +91 -0
- package/vendor/dsh-world/lib/life/host.d.ts +22 -0
- package/vendor/dsh-world/lib/life/host.js +24 -0
- package/vendor/dsh-world/lib/life/journal.d.ts +20 -0
- package/vendor/dsh-world/lib/life/journal.js +59 -0
- package/vendor/dsh-world/lib/life/lifeStore.d.ts +16 -0
- package/vendor/dsh-world/lib/life/lifeStore.js +105 -0
- package/vendor/dsh-world/lib/life/messages.d.ts +17 -0
- package/vendor/dsh-world/lib/life/messages.js +53 -0
- package/vendor/dsh-world/lib/life/notify.d.ts +15 -0
- package/vendor/dsh-world/lib/life/notify.js +40 -0
- package/vendor/dsh-world/lib/life/pricing.d.ts +4 -0
- package/vendor/dsh-world/lib/life/pricing.js +30 -0
- package/vendor/dsh-world/lib/life/runtime.d.ts +52 -0
- package/vendor/dsh-world/lib/life/runtime.js +376 -0
- package/vendor/dsh-world/lib/life/toolRegistry.d.ts +22 -0
- package/vendor/dsh-world/lib/life/toolRegistry.js +64 -0
- package/vendor/dsh-world/lib/life/tools.d.ts +24 -0
- package/vendor/dsh-world/lib/life/tools.js +47 -0
- package/vendor/dsh-world/lib/life/types.d.ts +67 -0
- package/vendor/dsh-world/lib/life/types.js +1 -0
- package/vendor/dsh-world/lib/persona.d.ts +10 -0
- package/vendor/dsh-world/lib/persona.js +29 -0
- package/vendor/dsh-world/lib/promptSection.d.ts +30 -0
- package/vendor/dsh-world/lib/promptSection.js +81 -0
- package/vendor/dsh-world/lib/qqScope.d.ts +11 -0
- package/vendor/dsh-world/lib/qqScope.js +39 -0
- package/vendor/dsh-world/lib/runtime.d.ts +23 -0
- package/vendor/dsh-world/lib/runtime.js +206 -0
- package/vendor/dsh-world/lib/self.d.ts +10 -0
- package/vendor/dsh-world/lib/self.js +36 -0
- package/vendor/dsh-world/lib/sourceArchive.d.ts +28 -0
- package/vendor/dsh-world/lib/sourceArchive.js +156 -0
- package/vendor/dsh-world/lib/store.d.ts +30 -0
- package/vendor/dsh-world/lib/store.js +191 -0
- package/vendor/dsh-world/lib/timeContext.d.ts +3 -0
- package/vendor/dsh-world/lib/timeContext.js +13 -0
- package/vendor/dsh-world/lib/types.d.ts +118 -0
- package/vendor/dsh-world/lib/types.js +1 -0
- package/vendor/dsh-world/lib/worldPrompt.d.ts +47 -0
- package/vendor/dsh-world/lib/worldPrompt.js +49 -0
- package/vendor/dsh-world/lib/worldSwitch.d.ts +44 -0
- package/vendor/dsh-world/lib/worldSwitch.js +78 -0
- package/vendor/dsh-world/package-lock.json +1088 -0
- package/vendor/dsh-world/package.json +88 -0
- package/vendor/dsh-world/scripts/enable-tool-free-compaction.ps1 +33 -0
- package/vendor/dsh-world/src/archive.ts +45 -0
- package/vendor/dsh-world/src/archiveSelection.ts +27 -0
- package/vendor/dsh-world/src/compaction.ts +260 -0
- package/vendor/dsh-world/src/compactionRegion.ts +172 -0
- package/vendor/dsh-world/src/console/routes.ts +125 -0
- package/vendor/dsh-world/src/console/snapshot.ts +84 -0
- package/vendor/dsh-world/src/ecosystem/README.md +14 -0
- package/vendor/dsh-world/src/ecosystem/bridge/bridge.ts +121 -0
- package/vendor/dsh-world/src/ecosystem/bridge/route.ts +35 -0
- package/vendor/dsh-world/src/ecosystem/qq/bridge.ts +456 -0
- package/vendor/dsh-world/src/ecosystem/qq/panel.ts +178 -0
- package/vendor/dsh-world/src/ecosystem/qq/route.ts +130 -0
- package/vendor/dsh-world/src/ecosystem/qq/supervisor.ts +262 -0
- package/vendor/dsh-world/src/ecosystem/shared/context.ts +38 -0
- package/vendor/dsh-world/src/ecosystem/shared/identity.ts +42 -0
- package/vendor/dsh-world/src/ecosystem/shared/outlets.ts +75 -0
- package/vendor/dsh-world/src/ecosystem/shared/sessions.ts +47 -0
- package/vendor/dsh-world/src/ecosystem/wechat/archive.ts +66 -0
- package/vendor/dsh-world/src/ecosystem/wechat/home.ts +94 -0
- package/vendor/dsh-world/src/ecosystem/wechat/images.ts +38 -0
- package/vendor/dsh-world/src/ecosystem/wechat/index.ts +101 -0
- package/vendor/dsh-world/src/ecosystem/wechat/messages.ts +43 -0
- package/vendor/dsh-world/src/ecosystem/wechat/sending.ts +177 -0
- package/vendor/dsh-world/src/ecosystem/wechat/supervisor.ts +116 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/README.md +117 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/adapter.py +572 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/binding.py +52 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/LICENSE +43 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/NOTICE.md +10 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/bridge.cpp +229 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/build.ps1 +19 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/profile.json +28 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/groups.py +42 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/incoming.py +157 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/native.py +168 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/outgoing.py +95 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/requirements.txt +1 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/sender.py +157 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/snapshot.py +32 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/stickers.py +60 -0
- package/vendor/dsh-world/src/ecosystem/wechat/windows/wechat.ps1 +75 -0
- package/vendor/dsh-world/src/faults.ts +12 -0
- package/vendor/dsh-world/src/history.ts +77 -0
- package/vendor/dsh-world/src/hostContract.ts +9 -0
- package/vendor/dsh-world/src/http.ts +34 -0
- package/vendor/dsh-world/src/index.ts +286 -0
- package/vendor/dsh-world/src/life/activity.ts +48 -0
- package/vendor/dsh-world/src/life/alarm.ts +119 -0
- package/vendor/dsh-world/src/life/alarmTimer.ts +45 -0
- package/vendor/dsh-world/src/life/diary.ts +10 -0
- package/vendor/dsh-world/src/life/earlyStop.ts +27 -0
- package/vendor/dsh-world/src/life/home.ts +85 -0
- package/vendor/dsh-world/src/life/host.ts +50 -0
- package/vendor/dsh-world/src/life/journal.ts +44 -0
- package/vendor/dsh-world/src/life/lifeStore.ts +70 -0
- package/vendor/dsh-world/src/life/messages.ts +44 -0
- package/vendor/dsh-world/src/life/notify.ts +47 -0
- package/vendor/dsh-world/src/life/pricing.ts +37 -0
- package/vendor/dsh-world/src/life/runtime.ts +318 -0
- package/vendor/dsh-world/src/life/toolRegistry.ts +48 -0
- package/vendor/dsh-world/src/life/tools.ts +61 -0
- package/vendor/dsh-world/src/life/types.ts +19 -0
- package/vendor/dsh-world/src/persona.ts +26 -0
- package/vendor/dsh-world/src/promptSection.ts +71 -0
- package/vendor/dsh-world/src/qqScope.ts +51 -0
- package/vendor/dsh-world/src/runtime.ts +172 -0
- package/vendor/dsh-world/src/self.ts +32 -0
- package/vendor/dsh-world/src/sourceArchive.ts +132 -0
- package/vendor/dsh-world/src/store.ts +170 -0
- package/vendor/dsh-world/src/timeContext.ts +13 -0
- package/vendor/dsh-world/src/types.ts +112 -0
- package/vendor/dsh-world/src/worldPrompt.ts +64 -0
- package/vendor/dsh-world/src/worldSwitch.ts +82 -0
- package/vendor/dsh-world/templates/alarm.md +49 -0
- package/vendor/dsh-world/templates/world-prompt.md +7 -0
- package/vendor/dsh-world/tests/archive.test.ts +57 -0
- package/vendor/dsh-world/tests/archiveScope.test.ts +86 -0
- package/vendor/dsh-world/tests/bridge.test.ts +211 -0
- package/vendor/dsh-world/tests/channelSessions.test.ts +79 -0
- package/vendor/dsh-world/tests/clientHooks.test.ts +103 -0
- package/vendor/dsh-world/tests/compaction.test.ts +141 -0
- package/vendor/dsh-world/tests/console.test.ts +202 -0
- package/vendor/dsh-world/tests/consoleClient.test.ts +101 -0
- package/vendor/dsh-world/tests/enableCompaction.test.ts +19 -0
- package/vendor/dsh-world/tests/fixtures.ts +6 -0
- package/vendor/dsh-world/tests/history.test.ts +100 -0
- package/vendor/dsh-world/tests/hostFixture.ts +46 -0
- package/vendor/dsh-world/tests/lifeActivity.test.ts +70 -0
- package/vendor/dsh-world/tests/lifeAlarm.test.ts +87 -0
- package/vendor/dsh-world/tests/lifeDiary.test.ts +33 -0
- package/vendor/dsh-world/tests/lifeMessages.test.ts +61 -0
- package/vendor/dsh-world/tests/lifeNotify.test.ts +46 -0
- package/vendor/dsh-world/tests/lifeParts.test.ts +45 -0
- package/vendor/dsh-world/tests/lifeRuntime.test.ts +660 -0
- package/vendor/dsh-world/tests/lifeStore.test.ts +90 -0
- package/vendor/dsh-world/tests/lifeTools.test.ts +53 -0
- package/vendor/dsh-world/tests/outlets.test.ts +85 -0
- package/vendor/dsh-world/tests/panel.test.ts +85 -0
- package/vendor/dsh-world/tests/plugin.test.ts +614 -0
- package/vendor/dsh-world/tests/pricing.test.ts +21 -0
- package/vendor/dsh-world/tests/qq.test.ts +121 -0
- package/vendor/dsh-world/tests/qqSessionMode.test.ts +97 -0
- package/vendor/dsh-world/tests/qqSupervisor.test.ts +149 -0
- package/vendor/dsh-world/tests/sourceArchive.test.ts +58 -0
- package/vendor/dsh-world/tests/store.test.ts +82 -0
- package/vendor/dsh-world/tests/storeWriter.ts +5 -0
- package/vendor/dsh-world/tests/timeContext.test.ts +15 -0
- package/vendor/dsh-world/tests/wechat.test.ts +254 -0
- package/vendor/dsh-world/tests/wechatImages.test.ts +130 -0
- package/vendor/dsh-world/tests/wechatSending.test.ts +142 -0
- package/vendor/dsh-world/tests/wechatSupervisor.test.ts +91 -0
- package/vendor/dsh-world/tests/worldPrompt.test.ts +201 -0
- package/vendor/dsh-world/tsconfig.build.json +5 -0
- package/vendor/dsh-world/tsconfig.json +8 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { readFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { jsonObject, knownHost, readBody, sendJson } from "../../http.js";
|
|
4
|
+
import { speakerIdentity } from "../shared/identity.js";
|
|
5
|
+
import { durableWrite } from "../../life/lifeStore.js";
|
|
6
|
+
import { createWechatHome } from "./home.js";
|
|
7
|
+
import { channelContext, channelMedia } from "../shared/context.js";
|
|
8
|
+
import { admitContextImages } from "./images.js";
|
|
9
|
+
import { dirname, join } from 'node:path';
|
|
10
|
+
import { createWechatSending } from "./sending.js";
|
|
11
|
+
export const WECHAT_PATH = '/dsh-world/wechat/ask';
|
|
12
|
+
export function createWechatModule(deps) {
|
|
13
|
+
const token = randomBytes(32).toString('hex');
|
|
14
|
+
const sending = createWechatSending({ ...deps, file: join(dirname(deps.file), 'wechat-outbox.sqlite'), worldId: deps.worldId ?? 'default' });
|
|
15
|
+
const attached = new Map();
|
|
16
|
+
const requests = new Set();
|
|
17
|
+
let closing;
|
|
18
|
+
let stopped = false;
|
|
19
|
+
let lastContactAt;
|
|
20
|
+
/** What the adapter last said about the WeChat account itself; only the adapter knows whether it is logged in. */
|
|
21
|
+
let accountState;
|
|
22
|
+
const settings = () => deps.settings?.() ?? { contextMessages: 8, bufferSeconds: 3 };
|
|
23
|
+
function attach(agent) {
|
|
24
|
+
if (stopped || attached.has(agent))
|
|
25
|
+
return;
|
|
26
|
+
const disposers = [];
|
|
27
|
+
try {
|
|
28
|
+
for (const tool of sending.tools)
|
|
29
|
+
disposers.push(agent.ctx.tools.register(tool));
|
|
30
|
+
attached.set(agent, disposers);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
for (const dispose of disposers.reverse())
|
|
34
|
+
dispose();
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const detachCreate = deps.ctx.on('agent/created', ({ agent }) => { if (deps.acceptAgent?.(agent))
|
|
39
|
+
attach(agent); return undefined; });
|
|
40
|
+
const detachDispose = deps.ctx.on('agent/disposed', ({ agent }) => { attached.delete(agent); });
|
|
41
|
+
const home = createWechatHome({ ...deps, home: async (identity) => { const agent = await deps.home(identity); attach(agent); return agent; }, images: async (identity, context) => {
|
|
42
|
+
if (!context.some(message => message.image))
|
|
43
|
+
return [];
|
|
44
|
+
if (!deps.incomingDir)
|
|
45
|
+
throw new Error('Group image directory is unavailable');
|
|
46
|
+
return admitContextImages(deps.ctx, deps.incomingDir, identity, context);
|
|
47
|
+
} });
|
|
48
|
+
async function handle(req) {
|
|
49
|
+
const given = req.headers['x-dsh-world-token'];
|
|
50
|
+
if (!knownHost(req.headers.host, deps.allowedHosts) || req.headers['sec-fetch-site'] === 'cross-site' || typeof given !== 'string')
|
|
51
|
+
return { status: 403, body: { error: 'forbidden' } };
|
|
52
|
+
const bytes = Buffer.from(given), expected = Buffer.from(token);
|
|
53
|
+
if (bytes.length !== expected.length || !timingSafeEqual(bytes, expected))
|
|
54
|
+
return { status: 403, body: { error: 'forbidden' } };
|
|
55
|
+
if (stopped)
|
|
56
|
+
return { status: 503, body: { error: 'wechat unavailable' } };
|
|
57
|
+
lastContactAt = new Date().toISOString();
|
|
58
|
+
if (req.method !== 'POST')
|
|
59
|
+
return { status: 405, body: { error: 'method not allowed' }, headers: { allow: 'POST' } };
|
|
60
|
+
const body = jsonObject(await readBody(req, 32 * 1024));
|
|
61
|
+
if (body && 'status' in body) {
|
|
62
|
+
const reported = body.status?.wechat;
|
|
63
|
+
if (reported !== 'online' && reported !== 'offline')
|
|
64
|
+
return { status: 400, body: { error: 'bad status' } };
|
|
65
|
+
accountState = reported;
|
|
66
|
+
return { status: 200, body: { accepted: true } };
|
|
67
|
+
}
|
|
68
|
+
if (body && 'outbox' in body) {
|
|
69
|
+
try {
|
|
70
|
+
const job = await sending.poll(body.outbox);
|
|
71
|
+
accountState = 'online';
|
|
72
|
+
return { status: 200, body: { accepted: true, settings: settings(), ...(job ? { job } : {}) } };
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
deps.ctx.logger.warn('dsh-world WeChat outbox failed:', error);
|
|
76
|
+
return { status: 400, body: { error: 'invalid WeChat outbox binding or pending job; check adapter configuration' } };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (body && 'receipt' in body) {
|
|
80
|
+
const receipt = body.receipt;
|
|
81
|
+
if (!receipt || typeof receipt !== 'object' || typeof receipt.id !== 'string' || !/^[a-f0-9]{64}$/.test(receipt.id)
|
|
82
|
+
|| !Number.isSafeInteger(receipt.confirmed) || receipt.confirmed < 0 || receipt.confirmed > 16
|
|
83
|
+
|| !['sent', 'uncertain', 'failed'].includes(receipt.status ?? ''))
|
|
84
|
+
return { status: 400, body: { error: 'bad receipt' } };
|
|
85
|
+
try {
|
|
86
|
+
await sending.receipt(receipt);
|
|
87
|
+
return { status: 200, body: { accepted: true } };
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
deps.ctx.logger.warn('dsh-world WeChat receipt failed:', error);
|
|
91
|
+
return { status: 503, body: { error: 'receipt not recorded; retry receipt only' } };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const text = typeof body?.text === 'string' ? body.text.trim() : '';
|
|
95
|
+
let identity;
|
|
96
|
+
let context;
|
|
97
|
+
let media;
|
|
98
|
+
try {
|
|
99
|
+
identity = speakerIdentity(body?.identity);
|
|
100
|
+
context = channelContext(body?.context, identity, settings().contextMessages);
|
|
101
|
+
media = channelMedia(body?.media, identity);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return { status: 400, body: { error: 'bad identity or context' } };
|
|
105
|
+
}
|
|
106
|
+
if ((!text && !media.length) || [...text].length > 2000)
|
|
107
|
+
return { status: 400, body: { error: 'bad request' } };
|
|
108
|
+
if (identity.channel !== 'wechat')
|
|
109
|
+
return { status: 400, body: { error: 'bad channel' } };
|
|
110
|
+
if ((identity.conversationType === 'group') !== identity.conversationId.endsWith('@chatroom'))
|
|
111
|
+
return { status: 400, body: { error: 'bad conversation type' } };
|
|
112
|
+
try {
|
|
113
|
+
return { status: 200, body: { accepted: true, ...(await home.ask(text, identity, context, media)) } };
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
deps.ctx.logger.warn('dsh-world WeChat request failed:', error);
|
|
117
|
+
return { status: 503, body: { error: 'home request failed; delivery must be checked before retrying' } };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
status() { return { ...sending.status(), lastContactAt: lastContactAt ?? null, account: accountState ?? null, ...settings() }; },
|
|
122
|
+
start() {
|
|
123
|
+
durableWrite(deps.file, JSON.stringify({ origin: deps.origin(), token, path: WECHAT_PATH, pid: process.pid, protocol: 3 }, null, 2));
|
|
124
|
+
for (const agent of deps.ctx.agents.list())
|
|
125
|
+
if (deps.acceptAgent?.(agent))
|
|
126
|
+
attach(agent);
|
|
127
|
+
},
|
|
128
|
+
stop() {
|
|
129
|
+
if (stopped)
|
|
130
|
+
return closing;
|
|
131
|
+
stopped = true;
|
|
132
|
+
home.stop();
|
|
133
|
+
detachCreate();
|
|
134
|
+
detachDispose();
|
|
135
|
+
for (const disposers of attached.values())
|
|
136
|
+
for (const dispose of disposers.reverse()) {
|
|
137
|
+
try {
|
|
138
|
+
dispose();
|
|
139
|
+
}
|
|
140
|
+
catch { /* agent already disposed */ }
|
|
141
|
+
}
|
|
142
|
+
attached.clear();
|
|
143
|
+
try {
|
|
144
|
+
if (JSON.parse(readFileSync(deps.file, 'utf8')).token === token)
|
|
145
|
+
rmSync(deps.file, { force: true });
|
|
146
|
+
}
|
|
147
|
+
catch { /* already absent, or a newer module owns the file */ }
|
|
148
|
+
return closing = Promise.allSettled([...requests]).then(() => sending.stop());
|
|
149
|
+
},
|
|
150
|
+
route: { kind: 'exact', path: WECHAT_PATH, handler: async (req, res) => {
|
|
151
|
+
let result;
|
|
152
|
+
const request = handle(req);
|
|
153
|
+
requests.add(request);
|
|
154
|
+
try {
|
|
155
|
+
result = await request;
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
result = { status: 500, body: { error: 'internal error' } };
|
|
159
|
+
}
|
|
160
|
+
finally {
|
|
161
|
+
requests.delete(request);
|
|
162
|
+
}
|
|
163
|
+
sendJson(res, result);
|
|
164
|
+
} },
|
|
165
|
+
};
|
|
166
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { SpeakerIdentity } from '../shared/identity.ts';
|
|
2
|
+
declare module '@deepseek-ai/dsh-llm' {
|
|
3
|
+
interface MessageSourceMap {
|
|
4
|
+
'dsh-world-wechat-outgoing': {
|
|
5
|
+
kind: 'dsh-world-wechat-outgoing';
|
|
6
|
+
form: 'recall';
|
|
7
|
+
identity: SpeakerIdentity;
|
|
8
|
+
messages: WechatMessage[];
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export type WechatMessage = {
|
|
13
|
+
type: 'text';
|
|
14
|
+
text: string;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'image' | 'sticker';
|
|
17
|
+
path: string;
|
|
18
|
+
} | {
|
|
19
|
+
type: 'sticker';
|
|
20
|
+
stickerId: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function replyText(messages: readonly WechatMessage[]): string;
|
|
23
|
+
export declare function replyMessages(value: unknown): WechatMessage[];
|
|
24
|
+
export declare function wechatSendTool(send: (chatId: string, messages: WechatMessage[], context: import('@deepseek-ai/dsh-tools').ToolRunContext) => Promise<string>): import("@deepseek-ai/dsh-tools").ToolDefinition;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
export function replyText(messages) {
|
|
3
|
+
return messages.map(message => message.type === 'text' ? message.text : 'stickerId' in message
|
|
4
|
+
? `【表情包 ${message.stickerId}】` : `【${message.type === 'image' ? '图片' : '表情包'} ${message.path}】`).join('\n\n');
|
|
5
|
+
}
|
|
6
|
+
export function replyMessages(value) {
|
|
7
|
+
if (!Array.isArray(value) || !value.length || value.length > 16)
|
|
8
|
+
throw new Error('一次需要 1–16 条微信消息');
|
|
9
|
+
return value.map(item => {
|
|
10
|
+
if (!item || typeof item !== 'object' || Array.isArray(item))
|
|
11
|
+
throw new Error('无效的微信消息');
|
|
12
|
+
const input = item;
|
|
13
|
+
if (input.type === 'text' && typeof input.text === 'string' && input.text.trim() && !input.text.includes('\0') && Buffer.byteLength(input.text) < 4095)
|
|
14
|
+
return { type: 'text', text: input.text };
|
|
15
|
+
if (input.type === 'sticker' && typeof input.stickerId === 'string' && /^st_[A-Za-z0-9_-]+$/.test(input.stickerId))
|
|
16
|
+
return { type: 'sticker', stickerId: input.stickerId };
|
|
17
|
+
if ((input.type === 'image' || input.type === 'sticker') && typeof input.path === 'string' && input.path && input.path.length < 1024 && !input.path.includes('\0'))
|
|
18
|
+
return { type: input.type, path: input.path };
|
|
19
|
+
throw new Error('文字超长,或图片/表情缺少有效路径或表情 ID');
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export function wechatSendTool(send) {
|
|
23
|
+
return defineTool({
|
|
24
|
+
name: 'wechat_send',
|
|
25
|
+
description: '主动联系微信中的人或群,或者回复当前微信消息,都用同一个工具。先用 wechat_chats 查询已配置目标,chatId 使用准确 ID,不能按昵称猜测。messages 每项是独立消息,可连续短句并穿插图片或表情,按数组顺序发送。text 放文字;image 用允许目录内的本地绝对路径;sticker 用现有 stickerId(send_sticker 的 list 可查询)或本地图片/GIF 路径。这里只排队,尚未确认发送;用返回的 ID 调用 wechat_delivery 查询结果,结果不明时不要重复发送。同一聊天的一批话尽量一次排好。普通最终回答不会自动转发到微信。',
|
|
26
|
+
parameters: { chatId: { type: 'string', required: true }, messages: { type: 'array', required: true, items: { oneOf: [
|
|
27
|
+
{ type: 'object', additionalProperties: false, properties: { type: { type: 'string', const: 'text', required: true }, text: { type: 'string', required: true } } },
|
|
28
|
+
{ type: 'object', additionalProperties: false, properties: { type: { type: 'string', const: 'image', required: true }, path: { type: 'string', required: true } } },
|
|
29
|
+
{ type: 'object', additionalProperties: false, properties: { type: { type: 'string', const: 'sticker', required: true }, stickerId: { type: 'string', required: true } } },
|
|
30
|
+
{ type: 'object', additionalProperties: false, properties: { type: { type: 'string', const: 'sticker', required: true }, path: { type: 'string', required: true } } },
|
|
31
|
+
] } } },
|
|
32
|
+
output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] },
|
|
33
|
+
execute: async ({ chatId, messages }, context) => send(chatId, replyMessages(messages), context),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { replyMessages } from './messages.ts';
|
|
3
|
+
import type { PreparedReply, WechatReceipt } from './archive.ts';
|
|
4
|
+
interface Job {
|
|
5
|
+
id: string;
|
|
6
|
+
accountId: string;
|
|
7
|
+
chatId: string;
|
|
8
|
+
messages: ReturnType<typeof replyMessages>;
|
|
9
|
+
}
|
|
10
|
+
/** The existing adapter owns all sending; a claim survives disconnects until its receipt is recorded. */
|
|
11
|
+
export declare function createWechatSending(deps: {
|
|
12
|
+
ctx: Context;
|
|
13
|
+
file: string;
|
|
14
|
+
worldId: string;
|
|
15
|
+
record?(reply: PreparedReply): Promise<string>;
|
|
16
|
+
receipt?(receipt: WechatReceipt): Promise<void>;
|
|
17
|
+
}): {
|
|
18
|
+
tools: import("@deepseek-ai/dsh-tools").ToolDefinition[];
|
|
19
|
+
/** Bound account, chats the adapter reported, and jobs waiting to go out or needing a human check. */
|
|
20
|
+
status(): {
|
|
21
|
+
accountId: string | null;
|
|
22
|
+
chats: number;
|
|
23
|
+
pending: number;
|
|
24
|
+
uncertain: number;
|
|
25
|
+
};
|
|
26
|
+
poll(value: unknown): Promise<Job | undefined>;
|
|
27
|
+
receipt(receipt: WechatReceipt): Promise<void>;
|
|
28
|
+
stop(): Promise<void>;
|
|
29
|
+
};
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { SessionId } from '@deepseek-ai/dsh-session';
|
|
3
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
4
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
5
|
+
import { conversationRecordId } from "../../archive.js";
|
|
6
|
+
import { speakerIdentity } from "../shared/identity.js";
|
|
7
|
+
import { replyMessages, replyText, wechatSendTool } from "./messages.js";
|
|
8
|
+
const output = { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] };
|
|
9
|
+
/** The existing adapter owns all sending; a claim survives disconnects until its receipt is recorded. */
|
|
10
|
+
export function createWechatSending(deps) {
|
|
11
|
+
const db = new DatabaseSync(deps.file);
|
|
12
|
+
db.exec(`PRAGMA busy_timeout=10000; PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;
|
|
13
|
+
CREATE TABLE IF NOT EXISTS binding (accountId TEXT PRIMARY KEY);
|
|
14
|
+
CREATE TABLE IF NOT EXISTS jobs (id TEXT PRIMARY KEY, sessionId TEXT NOT NULL, messageId TEXT NOT NULL,
|
|
15
|
+
status TEXT NOT NULL, body TEXT NOT NULL, confirmed INTEGER NOT NULL DEFAULT 0, error TEXT);
|
|
16
|
+
CREATE INDEX IF NOT EXISTS queueOrder ON jobs(status);`);
|
|
17
|
+
let accountId;
|
|
18
|
+
let chats = [];
|
|
19
|
+
let stopped = false;
|
|
20
|
+
const pending = new Set();
|
|
21
|
+
const rowOf = (id) => db.prepare('SELECT * FROM jobs WHERE id=? LIMIT 1').get(id);
|
|
22
|
+
function transaction(run) {
|
|
23
|
+
db.exec('BEGIN IMMEDIATE');
|
|
24
|
+
try {
|
|
25
|
+
const result = run();
|
|
26
|
+
db.exec('COMMIT');
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
db.exec('ROLLBACK');
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async function release(sessionId, event, turn) {
|
|
35
|
+
if (event.type !== 'user/message' || event.data.source.kind !== 'dsh-world-wechat-outgoing')
|
|
36
|
+
return;
|
|
37
|
+
const id = conversationRecordId(deps.worldId, sessionId, event.data.id);
|
|
38
|
+
const row = rowOf(id);
|
|
39
|
+
if (!row || row.status !== 'staged')
|
|
40
|
+
return;
|
|
41
|
+
const source = event.data.source;
|
|
42
|
+
const job = JSON.parse(row.body);
|
|
43
|
+
const identity = speakerIdentity(source.identity);
|
|
44
|
+
const messages = replyMessages(source.messages);
|
|
45
|
+
if (job.accountId !== identity.accountId || job.chatId !== identity.conversationId || JSON.stringify(job.messages) !== JSON.stringify(messages))
|
|
46
|
+
throw new Error('微信发送正文与持久化队列不一致');
|
|
47
|
+
const recorded = await deps.record?.({ sessionId, messageId: event.data.id, seq: event.seq, turn, time: event.time, identity, messages });
|
|
48
|
+
if (recorded && recorded !== id)
|
|
49
|
+
throw new Error('微信发送记录 ID 不一致');
|
|
50
|
+
db.prepare("UPDATE jobs SET status='queued' WHERE id=? AND status='staged'").run(id);
|
|
51
|
+
}
|
|
52
|
+
async function ready(session, event) {
|
|
53
|
+
await deps.ctx.sessions.flush(session);
|
|
54
|
+
const turn = session.snapshotEvents().findLast(item => item.type === 'turn/start' && item.seq < event.seq);
|
|
55
|
+
if (turn?.type !== 'turn/start')
|
|
56
|
+
throw new Error('微信发送记录缺少来源轮次');
|
|
57
|
+
await release(session.id, event, turn.data.turn);
|
|
58
|
+
}
|
|
59
|
+
const dispose = deps.ctx.on('session/event', (session, event) => {
|
|
60
|
+
if (stopped || event.type !== 'user/message' || event.data.source.kind !== 'dsh-world-wechat-outgoing')
|
|
61
|
+
return;
|
|
62
|
+
const job = Promise.resolve().then(() => ready(session, event)).catch(error => {
|
|
63
|
+
db.prepare("UPDATE jobs SET status='failed',error=? WHERE id=? AND status='staged'").run(String(error), conversationRecordId(deps.worldId, session.id, event.data.id));
|
|
64
|
+
deps.ctx.logger.error(error);
|
|
65
|
+
}).finally(() => pending.delete(job));
|
|
66
|
+
pending.add(job);
|
|
67
|
+
});
|
|
68
|
+
async function settle() { while (pending.size)
|
|
69
|
+
await Promise.all(pending); }
|
|
70
|
+
async function recover() {
|
|
71
|
+
await settle();
|
|
72
|
+
let after = '';
|
|
73
|
+
while (true) {
|
|
74
|
+
const rows = db.prepare("SELECT * FROM jobs WHERE status='staged' AND id>? ORDER BY id LIMIT 50").all(after);
|
|
75
|
+
if (!rows.length)
|
|
76
|
+
return;
|
|
77
|
+
for (const row of rows) {
|
|
78
|
+
after = row.id;
|
|
79
|
+
const live = deps.ctx.sessions.get(SessionId(row.sessionId));
|
|
80
|
+
if (deps.ctx.agents.get(SessionId(row.sessionId))?.status === 'running')
|
|
81
|
+
continue;
|
|
82
|
+
if (live)
|
|
83
|
+
await deps.ctx.sessions.flush(live);
|
|
84
|
+
const handle = await deps.ctx.sessionPersistence.open(SessionId(row.sessionId), 'read');
|
|
85
|
+
try {
|
|
86
|
+
let next = 0, found = false, turn = 0;
|
|
87
|
+
for (;;) {
|
|
88
|
+
const { events } = await handle.read(next, 128);
|
|
89
|
+
if (!events.length)
|
|
90
|
+
break;
|
|
91
|
+
for (const event of events) {
|
|
92
|
+
if (event.type === 'turn/start')
|
|
93
|
+
turn = event.data.turn;
|
|
94
|
+
if (event.type === 'user/message' && event.data.id === row.messageId && event.data.source.kind === 'dsh-world-wechat-outgoing') {
|
|
95
|
+
await release(row.sessionId, event, turn);
|
|
96
|
+
found = true;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (found)
|
|
101
|
+
break;
|
|
102
|
+
next = events.at(-1).seq + 1;
|
|
103
|
+
}
|
|
104
|
+
if (!found)
|
|
105
|
+
db.prepare("UPDATE jobs SET status='failed',error='发送内容尚未写入会话,未发送' WHERE id=?").run(row.id);
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
await handle.close();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const tools = [defineTool({
|
|
114
|
+
name: 'wechat_chats', description: '分页查看已绑定微信账号允许发送的私聊和群,返回准确 chatId;不能向列表外发送。',
|
|
115
|
+
parameters: { limit: { type: 'integer' }, offset: { type: 'integer' } }, output,
|
|
116
|
+
execute: async ({ limit = 20, offset = 0 }) => {
|
|
117
|
+
if (stopped || !accountId)
|
|
118
|
+
throw new Error('微信适配器尚未连接');
|
|
119
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 100 || !Number.isSafeInteger(offset) || offset < 0)
|
|
120
|
+
throw new Error('分页参数无效');
|
|
121
|
+
return JSON.stringify({ accountId, chats: chats.slice(offset, offset + limit).map(chat => ({ chatId: chat.id, name: chat.name, type: chat.id.endsWith('@chatroom') ? 'group' : 'private' })), hasMore: offset + limit < chats.length });
|
|
122
|
+
},
|
|
123
|
+
}), wechatSendTool(async (chatId, messages, context) => {
|
|
124
|
+
if (stopped || !accountId)
|
|
125
|
+
throw new Error('微信适配器尚未连接,未安排发送');
|
|
126
|
+
const boundAccount = accountId;
|
|
127
|
+
const chat = chats.find(chat => chat.id === chatId);
|
|
128
|
+
if (!chat)
|
|
129
|
+
throw new Error('只能向 wechat_chats 列出的已配置聊天发送');
|
|
130
|
+
const agent = context.agent;
|
|
131
|
+
if (!agent)
|
|
132
|
+
throw new Error('微信发送需要来源会话');
|
|
133
|
+
context.signal.throwIfAborted();
|
|
134
|
+
const identity = speakerIdentity({ channel: 'wechat', accountId, speakerId: `wechat:${accountId}`, conversationId: chat.id, conversationName: chat.name, conversationType: chat.id.endsWith('@chatroom') ? 'group' : 'private' });
|
|
135
|
+
const message = createUserMessage({ source: { kind: 'dsh-world-wechat-outgoing', form: 'recall', identity, messages }, content: [{ type: 'text', text: `微信待发送给 ${chat.name}(${chat.id}),以下是我已安排的正文,尚未确认发送,不要重复发送:\n${replyText(messages)}` }] });
|
|
136
|
+
const id = conversationRecordId(deps.worldId, agent.id, message.id);
|
|
137
|
+
transaction(() => db.prepare("INSERT INTO jobs(id,sessionId,messageId,status,body) VALUES (?,?,?,'staged',?)").run(id, agent.id, message.id, JSON.stringify({ id, accountId: boundAccount, chatId, messages })));
|
|
138
|
+
context.deferContext(message);
|
|
139
|
+
return JSON.stringify({ id, status: 'queued', total: messages.length, note: '会话记录写入后由适配器发送;尚未确认送达,勿重复调用。' });
|
|
140
|
+
}), defineTool({
|
|
141
|
+
name: 'wechat_delivery', description: '按 wechat_send 返回的 ID 查询排队或发送结果。sent 只代表微信服务器已确认发送,不代表对方已读;uncertain 不得自动重发。',
|
|
142
|
+
parameters: { id: { type: 'string', required: true } }, output,
|
|
143
|
+
execute: async ({ id }) => {
|
|
144
|
+
if (stopped)
|
|
145
|
+
throw new Error('微信模块已停止');
|
|
146
|
+
if (!/^[a-f0-9]{64}$/.test(id))
|
|
147
|
+
throw new Error('无效的发送 ID');
|
|
148
|
+
const row = rowOf(id);
|
|
149
|
+
if (!row)
|
|
150
|
+
throw new Error('没有这条微信发送记录');
|
|
151
|
+
const job = JSON.parse(row.body);
|
|
152
|
+
return JSON.stringify({ id, chatId: job.chatId, status: row.status, confirmed: row.confirmed, total: job.messages.length, ...(row.error ? { error: row.error } : {}) });
|
|
153
|
+
},
|
|
154
|
+
})];
|
|
155
|
+
return {
|
|
156
|
+
tools,
|
|
157
|
+
/** Bound account, chats the adapter reported, and jobs waiting to go out or needing a human check. */
|
|
158
|
+
status() {
|
|
159
|
+
const count = (...states) => db.prepare(`SELECT COUNT(*) AS n FROM jobs WHERE status IN (${states.map(() => '?').join(',')})`).get(...states).n;
|
|
160
|
+
return { accountId: accountId ?? null, chats: chats.length, pending: count('staged', 'queued', 'claimed'), uncertain: count('uncertain') };
|
|
161
|
+
},
|
|
162
|
+
async poll(value) {
|
|
163
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
164
|
+
throw new Error('无效的适配器绑定');
|
|
165
|
+
const input = value;
|
|
166
|
+
speakerIdentity({ channel: 'wechat', accountId: input.accountId, speakerId: `wechat:${input.accountId}`, conversationId: 'binding' });
|
|
167
|
+
if (!Array.isArray(input.chats) || !input.chats.length || input.chats.length > 1000)
|
|
168
|
+
throw new Error('无效的目标列表');
|
|
169
|
+
const targets = input.chats.map((value) => {
|
|
170
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
171
|
+
throw new Error('无效的聊天');
|
|
172
|
+
const chat = value;
|
|
173
|
+
speakerIdentity({ channel: 'wechat', accountId: input.accountId, speakerId: `wechat:${input.accountId}`, conversationId: chat.id, conversationName: chat.name });
|
|
174
|
+
if (typeof chat.name !== 'string' || !chat.name.trim())
|
|
175
|
+
throw new Error('聊天缺少名称');
|
|
176
|
+
return { id: chat.id, name: chat.name };
|
|
177
|
+
});
|
|
178
|
+
if (new Set(targets.map(chat => chat.id)).size !== targets.length)
|
|
179
|
+
throw new Error('聊天 ID 不能重复');
|
|
180
|
+
transaction(() => {
|
|
181
|
+
const previous = db.prepare('SELECT accountId FROM binding LIMIT 1').get()?.accountId;
|
|
182
|
+
if (previous && previous !== input.accountId)
|
|
183
|
+
throw new Error('微信发送队列已绑定其他账号,拒绝切换');
|
|
184
|
+
db.prepare('INSERT OR IGNORE INTO binding VALUES (?)').run(input.accountId);
|
|
185
|
+
});
|
|
186
|
+
accountId = input.accountId;
|
|
187
|
+
chats = targets;
|
|
188
|
+
await recover();
|
|
189
|
+
return transaction(() => {
|
|
190
|
+
const row = db.prepare("SELECT * FROM jobs WHERE status IN ('claimed','queued') ORDER BY CASE status WHEN 'claimed' THEN 0 ELSE 1 END,rowid LIMIT 1").get();
|
|
191
|
+
if (!row)
|
|
192
|
+
return;
|
|
193
|
+
const job = JSON.parse(row.body);
|
|
194
|
+
if (job.accountId !== accountId || !chats.some(chat => chat.id === job.chatId))
|
|
195
|
+
throw new Error('待发送任务的账号或聊天已不在配置中,需人工核对');
|
|
196
|
+
db.prepare("UPDATE jobs SET status='claimed' WHERE id=?").run(job.id);
|
|
197
|
+
return job;
|
|
198
|
+
});
|
|
199
|
+
},
|
|
200
|
+
async receipt(receipt) {
|
|
201
|
+
const row = rowOf(receipt.id);
|
|
202
|
+
if (!row && !deps.receipt)
|
|
203
|
+
throw new Error('回执存储不可用');
|
|
204
|
+
if (row) {
|
|
205
|
+
const job = JSON.parse(row.body);
|
|
206
|
+
if (!['claimed', 'sent', 'uncertain', 'failed'].includes(row.status) || receipt.confirmed < row.confirmed || receipt.confirmed > job.messages.length || (receipt.status === 'sent') !== (receipt.confirmed === job.messages.length) || (receipt.status === 'failed' && receipt.confirmed !== 0) || (row.status === 'sent' && receipt.status !== 'sent'))
|
|
207
|
+
throw new Error('无效或过时的微信回执');
|
|
208
|
+
}
|
|
209
|
+
await deps.receipt?.(receipt);
|
|
210
|
+
if (row)
|
|
211
|
+
transaction(() => db.prepare('UPDATE jobs SET status=?,confirmed=? WHERE id=?').run(receipt.status, receipt.confirmed, receipt.id));
|
|
212
|
+
},
|
|
213
|
+
async stop() { if (stopped)
|
|
214
|
+
return; stopped = true; dispose(); await settle(); db.close(); },
|
|
215
|
+
};
|
|
216
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { spawn, type ChildProcess } from 'node:child_process';
|
|
2
|
+
/** The adapter script ships next to the sources; this resolves from both `src/` and the built `lib/`. */
|
|
3
|
+
export declare const ADAPTER_SCRIPT: string;
|
|
4
|
+
/** The adapter exits with this when a person must check WeChat and run `acknowledge`; restarting cannot help. */
|
|
5
|
+
export declare const EXIT_NEEDS_HUMAN = 2;
|
|
6
|
+
export interface AdapterStatus {
|
|
7
|
+
running: boolean;
|
|
8
|
+
pid: number | null;
|
|
9
|
+
restarts: number;
|
|
10
|
+
lastExit: string | null;
|
|
11
|
+
error: string | null;
|
|
12
|
+
needsHuman: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface AdapterSupervisorDeps {
|
|
15
|
+
python: string;
|
|
16
|
+
config: string;
|
|
17
|
+
logFile: string;
|
|
18
|
+
script?: string;
|
|
19
|
+
/** Delay before the first restart and the cap it doubles up to. */
|
|
20
|
+
minDelayMs?: number;
|
|
21
|
+
maxDelayMs?: number;
|
|
22
|
+
spawnProcess?: typeof spawn;
|
|
23
|
+
killTree?(child: ChildProcess): void;
|
|
24
|
+
log(message: string): void;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Runs the Windows WeChat adapter for as long as the world's WeChat module lives: started with the module,
|
|
28
|
+
* restarted with a growing delay when it exits or cannot start (a file missing during an upgrade comes back),
|
|
29
|
+
* and stopped (its whole process tree) with the module. An exit that says a person must act is not retried.
|
|
30
|
+
* The adapter itself waits for the same WeChat account to log in again, so a WeChat restart needs nothing from here.
|
|
31
|
+
*/
|
|
32
|
+
export declare function createAdapterSupervisor(deps: AdapterSupervisorDeps): {
|
|
33
|
+
start(): void;
|
|
34
|
+
/** The console's restart: also the way out of "a person must check" once the person has. */
|
|
35
|
+
restart(): Promise<void>;
|
|
36
|
+
stop(): Promise<void>;
|
|
37
|
+
status(): AdapterStatus;
|
|
38
|
+
};
|
|
39
|
+
export type AdapterSupervisor = ReturnType<typeof createAdapterSupervisor>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { closeSync, existsSync, openSync, statSync, truncateSync } from 'node:fs';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
/** The adapter script ships next to the sources; this resolves from both `src/` and the built `lib/`. */
|
|
6
|
+
export const ADAPTER_SCRIPT = fileURLToPath(new URL('../../../src/ecosystem/wechat/windows/adapter.py', import.meta.url));
|
|
7
|
+
/** The adapter exits with this when a person must check WeChat and run `acknowledge`; restarting cannot help. */
|
|
8
|
+
export const EXIT_NEEDS_HUMAN = 2;
|
|
9
|
+
const MAX_LOG_BYTES = 1024 * 1024;
|
|
10
|
+
const HEALTHY_MS = 60_000;
|
|
11
|
+
/**
|
|
12
|
+
* Runs the Windows WeChat adapter for as long as the world's WeChat module lives: started with the module,
|
|
13
|
+
* restarted with a growing delay when it exits or cannot start (a file missing during an upgrade comes back),
|
|
14
|
+
* and stopped (its whole process tree) with the module. An exit that says a person must act is not retried.
|
|
15
|
+
* The adapter itself waits for the same WeChat account to log in again, so a WeChat restart needs nothing from here.
|
|
16
|
+
*/
|
|
17
|
+
export function createAdapterSupervisor(deps) {
|
|
18
|
+
const script = deps.script ?? ADAPTER_SCRIPT;
|
|
19
|
+
const spawnProcess = deps.spawnProcess ?? spawn;
|
|
20
|
+
const minDelay = deps.minDelayMs ?? 1000, maxDelay = deps.maxDelayMs ?? 30_000;
|
|
21
|
+
let child;
|
|
22
|
+
let timer;
|
|
23
|
+
let stopped = false, replacing = false, needsHuman = false, started = false, failures = 0, restarts = 0;
|
|
24
|
+
let lastExit = null, error = null;
|
|
25
|
+
let exited = Promise.resolve();
|
|
26
|
+
const killTree = deps.killTree ?? ((target) => {
|
|
27
|
+
// The Windows venv launcher starts a child interpreter; killing only the launcher would leave it running.
|
|
28
|
+
if (process.platform === 'win32' && target.pid)
|
|
29
|
+
spawn('taskkill', ['/PID', String(target.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true });
|
|
30
|
+
else
|
|
31
|
+
target.kill();
|
|
32
|
+
});
|
|
33
|
+
function fail(message) {
|
|
34
|
+
error = message;
|
|
35
|
+
deps.log(`dsh-world WeChat adapter not started: ${message}`);
|
|
36
|
+
schedule();
|
|
37
|
+
}
|
|
38
|
+
function launch() {
|
|
39
|
+
if (stopped)
|
|
40
|
+
return;
|
|
41
|
+
for (const [what, path] of [['Python', deps.python], ['适配器配置', deps.config], ['适配器脚本', script]]) {
|
|
42
|
+
if (!existsSync(path))
|
|
43
|
+
return fail(`${what}不存在:${path}`);
|
|
44
|
+
}
|
|
45
|
+
let fd;
|
|
46
|
+
try {
|
|
47
|
+
if (existsSync(deps.logFile) && statSync(deps.logFile).size > MAX_LOG_BYTES)
|
|
48
|
+
truncateSync(deps.logFile, 0);
|
|
49
|
+
fd = openSync(deps.logFile, 'a');
|
|
50
|
+
}
|
|
51
|
+
catch (cause) {
|
|
52
|
+
return fail(`无法写适配器日志:${String(cause)}`);
|
|
53
|
+
}
|
|
54
|
+
const startedAt = Date.now();
|
|
55
|
+
let current;
|
|
56
|
+
try {
|
|
57
|
+
current = spawnProcess(deps.python, ['-X', 'utf8', '-u', script, 'watch', '--config', deps.config, '--allow-send'], { cwd: dirname(script), stdio: ['ignore', fd, fd], windowsHide: true });
|
|
58
|
+
}
|
|
59
|
+
catch (cause) {
|
|
60
|
+
return fail(`适配器启动失败:${String(cause)}`);
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
closeSync(fd);
|
|
64
|
+
} // the child owns its copy of the descriptor
|
|
65
|
+
error = null;
|
|
66
|
+
if (started)
|
|
67
|
+
restarts++;
|
|
68
|
+
started = true;
|
|
69
|
+
child = current;
|
|
70
|
+
exited = new Promise(resolve => {
|
|
71
|
+
let done = false;
|
|
72
|
+
const finish = (text, code) => {
|
|
73
|
+
if (done)
|
|
74
|
+
return;
|
|
75
|
+
done = true;
|
|
76
|
+
if (child === current)
|
|
77
|
+
child = undefined;
|
|
78
|
+
lastExit = text;
|
|
79
|
+
if (Date.now() - startedAt > HEALTHY_MS)
|
|
80
|
+
failures = 0;
|
|
81
|
+
if (!stopped && !replacing) {
|
|
82
|
+
if (code === EXIT_NEEDS_HUMAN) {
|
|
83
|
+
needsHuman = true;
|
|
84
|
+
deps.log('dsh-world WeChat adapter stopped: a message needs checking by a person (run acknowledge, then restart it from the console)');
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
deps.log(`dsh-world WeChat adapter exited (${text}); restarting`);
|
|
88
|
+
schedule();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
resolve();
|
|
92
|
+
};
|
|
93
|
+
current.once('exit', (code, signal) => finish(signal ? `signal ${signal}` : `code ${code}`, signal ? null : code));
|
|
94
|
+
current.once('error', cause => finish(`error ${String(cause)}`, null));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function schedule() {
|
|
98
|
+
if (stopped)
|
|
99
|
+
return;
|
|
100
|
+
const delay = Math.min(maxDelay, minDelay * 2 ** failures++);
|
|
101
|
+
timer = setTimeout(() => { timer = undefined; launch(); }, delay);
|
|
102
|
+
timer.unref?.();
|
|
103
|
+
}
|
|
104
|
+
async function halt() {
|
|
105
|
+
if (timer) {
|
|
106
|
+
clearTimeout(timer);
|
|
107
|
+
timer = undefined;
|
|
108
|
+
}
|
|
109
|
+
const running = child;
|
|
110
|
+
if (!running)
|
|
111
|
+
return;
|
|
112
|
+
killTree(running);
|
|
113
|
+
await Promise.race([exited, new Promise(resolve => { setTimeout(resolve, 5000).unref?.(); })]);
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
start() { stopped = false; launch(); },
|
|
117
|
+
/** The console's restart: also the way out of "a person must check" once the person has. */
|
|
118
|
+
async restart() {
|
|
119
|
+
if (stopped)
|
|
120
|
+
return;
|
|
121
|
+
replacing = true;
|
|
122
|
+
try {
|
|
123
|
+
await halt();
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
replacing = false;
|
|
127
|
+
}
|
|
128
|
+
needsHuman = false;
|
|
129
|
+
failures = 0;
|
|
130
|
+
launch();
|
|
131
|
+
},
|
|
132
|
+
async stop() {
|
|
133
|
+
stopped = true;
|
|
134
|
+
await halt();
|
|
135
|
+
},
|
|
136
|
+
status() { return { running: child !== undefined, pid: child?.pid ?? null, restarts, lastExit, error, needsHuman }; },
|
|
137
|
+
};
|
|
138
|
+
}
|