dsh-chat-comprehension 0.2.0 → 0.3.0
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 +30 -9
- package/lib/engines.mjs +136 -0
- package/lib/index.js +37 -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 +3278 -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,125 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { jsonObject, knownHost, readBody, sendJson, type JsonResult } from '../http.ts';
|
|
3
|
+
import { PERSONA_LAYERS, type Persona } from '../persona.ts';
|
|
4
|
+
import { validTurns, validWechatBuffer, validWechatContext, MAX_WECHAT_BUFFER_SECONDS, MAX_WECHAT_CONTEXT } from '../worldSwitch.ts';
|
|
5
|
+
import { validArchiveScope, type MemoryArchiveScope } from '../archiveSelection.ts';
|
|
6
|
+
|
|
7
|
+
export const CONSOLE_PREFIX = '/dsh-world';
|
|
8
|
+
/** A write must carry this header; a page on another site cannot add it without a CORS preflight, which is never answered. */
|
|
9
|
+
export const WRITE_HEADER = 'x-dsh-world';
|
|
10
|
+
const MAX_BODY_BYTES = 64 * 1024;
|
|
11
|
+
export interface ConsoleSources {
|
|
12
|
+
snapshot(): Promise<unknown>;
|
|
13
|
+
activity(day: string, outsideOnly: boolean): unknown;
|
|
14
|
+
diary(date: string): string | undefined;
|
|
15
|
+
/** The pictures she kept with `keep_photo`, newest first. */
|
|
16
|
+
}
|
|
17
|
+
/** What the console can change: whether her world runs, and her persona and world prompt. */
|
|
18
|
+
export interface ConsoleActions {
|
|
19
|
+
start(): Promise<void>;
|
|
20
|
+
stop(): Promise<void>;
|
|
21
|
+
prompts(): { persona: Persona; worldPrompt: { text: string; custom: boolean } };
|
|
22
|
+
savePersona(persona: Partial<Persona>): void;
|
|
23
|
+
/** An empty text goes back to the shipped template. */
|
|
24
|
+
saveWorldPrompt(text: string): void;
|
|
25
|
+
/** CNY a day for her home; undefined removes the limit. */
|
|
26
|
+
setDailyBudget(yuan: number | undefined): void;
|
|
27
|
+
/** Turns in her memory block; undefined goes back to the default. */
|
|
28
|
+
setMemoryTurns(turns: number | undefined): void;
|
|
29
|
+
setArchiveScope(scope: MemoryArchiveScope): boolean;
|
|
30
|
+
/** A field left out stays as it is; null goes back to its default. */
|
|
31
|
+
/** Turns the WeChat and QQ modules on or off now (their tools and routes come and go with the switch). Returns why it cannot, or nothing. */
|
|
32
|
+
setModules(modules: { wechat?: boolean; qq?: boolean }): Promise<string | undefined>;
|
|
33
|
+
/** Restarts the adapter the plugin manages; false when it does not manage one. */
|
|
34
|
+
restartWechatAdapter(): Promise<boolean>;
|
|
35
|
+
setWechatSettings(settings: { context?: number | null; bufferSeconds?: number | null }): void;
|
|
36
|
+
}
|
|
37
|
+
export interface ConsoleRequest { method: string; url: string; write: boolean; body?: Record<string, unknown> | undefined }
|
|
38
|
+
const ok = (body: unknown = { ok: true }): JsonResult => ({ status: 200, body });
|
|
39
|
+
const bad = (error: string): JsonResult => ({ status: 400, body: { error } });
|
|
40
|
+
|
|
41
|
+
function persona(body: Record<string, unknown> | undefined): Partial<Persona> | undefined {
|
|
42
|
+
if (!body) return undefined;
|
|
43
|
+
const entries = Object.entries(body);
|
|
44
|
+
if (!entries.every(([key, value]) => (PERSONA_LAYERS as readonly string[]).includes(key) && typeof value === 'string')) return undefined;
|
|
45
|
+
return Object.fromEntries(entries) as Partial<Persona>;
|
|
46
|
+
}
|
|
47
|
+
async function write(actions: ConsoleActions, path: string, body: Record<string, unknown> | undefined): Promise<JsonResult> {
|
|
48
|
+
switch (path) {
|
|
49
|
+
case '/console/start': await actions.start(); return ok();
|
|
50
|
+
case '/console/stop': await actions.stop(); return ok();
|
|
51
|
+
case '/console/persona': { const layers = persona(body); if (!layers) return bad('persona layers must be strings'); actions.savePersona(layers); return ok(); }
|
|
52
|
+
case '/console/budget': {
|
|
53
|
+
const daily = body?.daily;
|
|
54
|
+
if (daily !== null && (typeof daily !== 'number' || !Number.isFinite(daily) || daily < 0)) return bad('daily must be a non-negative number or null');
|
|
55
|
+
actions.setDailyBudget(daily === null ? undefined : daily); return ok();
|
|
56
|
+
}
|
|
57
|
+
case '/console/memory': {
|
|
58
|
+
const turns = body?.turns;
|
|
59
|
+
if (turns !== null && !validTurns(turns)) return bad('turns must be a whole number of at least 1, or null');
|
|
60
|
+
actions.setMemoryTurns(turns === null ? undefined : turns); return ok();
|
|
61
|
+
}
|
|
62
|
+
case '/console/wechat': {
|
|
63
|
+
const context = body?.contextMessages, bufferSeconds = body?.bufferSeconds;
|
|
64
|
+
if (context !== undefined && context !== null && !validWechatContext(context)) return bad(`contextMessages must be a whole number from 1 to ${MAX_WECHAT_CONTEXT}, or null`);
|
|
65
|
+
if (bufferSeconds !== undefined && bufferSeconds !== null && !validWechatBuffer(bufferSeconds)) return bad(`bufferSeconds must be a number from 0 to ${MAX_WECHAT_BUFFER_SECONDS}, or null`);
|
|
66
|
+
if (context === undefined && bufferSeconds === undefined) return bad('nothing to change');
|
|
67
|
+
actions.setWechatSettings({ ...(context === undefined ? {} : { context }), ...(bufferSeconds === undefined ? {} : { bufferSeconds }) }); return ok();
|
|
68
|
+
}
|
|
69
|
+
case '/console/modules': {
|
|
70
|
+
const wechat = body?.wechat, qq = body?.qq;
|
|
71
|
+
if ((wechat !== undefined && typeof wechat !== 'boolean') || (qq !== undefined && typeof qq !== 'boolean')) return bad('wechat 和 qq 必须是 true 或 false');
|
|
72
|
+
if (wechat === undefined && qq === undefined) return bad('nothing to change');
|
|
73
|
+
const refused = await actions.setModules({ ...(wechat === undefined ? {} : { wechat }), ...(qq === undefined ? {} : { qq }) });
|
|
74
|
+
return refused ? bad(refused) : ok();
|
|
75
|
+
}
|
|
76
|
+
case '/console/wechat-adapter': return await actions.restartWechatAdapter() ? ok() : bad('适配器不是由插件管理的,请在配置里写 wechatAdapter');
|
|
77
|
+
case '/console/archive-scope': {
|
|
78
|
+
if (!validArchiveScope(body?.scope)) return bad('scope must be home or workspace');
|
|
79
|
+
return ok({ ok: true, restartRequired: actions.setArchiveScope(body.scope) });
|
|
80
|
+
}
|
|
81
|
+
case '/console/world-prompt': { if (typeof body?.text !== 'string') return bad('text must be a string'); actions.saveWorldPrompt(body.text); return ok(); }
|
|
82
|
+
default: return { status: 404, body: { error: 'not found' } };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Reads are open to the console page; writes need POST, the write header and a JSON body. */
|
|
87
|
+
export async function handleConsole(sources: ConsoleSources, actions: ConsoleActions, request: ConsoleRequest): Promise<JsonResult> {
|
|
88
|
+
const url = new URL(request.url, 'http://localhost');
|
|
89
|
+
if (url.pathname !== CONSOLE_PREFIX && !url.pathname.startsWith(CONSOLE_PREFIX + '/')) return { status: 404, body: { error: 'not found' } };
|
|
90
|
+
const path = url.pathname.slice(CONSOLE_PREFIX.length);
|
|
91
|
+
if (request.method === 'POST') {
|
|
92
|
+
if (!request.write) return { status: 403, body: { error: 'forbidden' } };
|
|
93
|
+
return write(actions, path, request.body);
|
|
94
|
+
}
|
|
95
|
+
if (request.method !== 'GET') return { status: 405, body: { error: 'method not allowed' }, headers: { allow: 'GET, POST' } };
|
|
96
|
+
switch (path) {
|
|
97
|
+
case '/console/snapshot': return ok(await sources.snapshot());
|
|
98
|
+
case '/console/activity': return ok(sources.activity(url.searchParams.get('day') ?? '', url.searchParams.get('outside') === '1'));
|
|
99
|
+
case '/console/diary': {
|
|
100
|
+
const date = url.searchParams.get('date') ?? '';
|
|
101
|
+
const text = sources.diary(date);
|
|
102
|
+
return text === undefined ? { status: 404, body: { error: 'no diary for this date' } } : ok({ date, text });
|
|
103
|
+
}
|
|
104
|
+
case '/console/prompts': return ok(actions.prompts());
|
|
105
|
+
default: return { status: 404, body: { error: 'not found' } };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export function consoleRoute(sources: ConsoleSources, actions: ConsoleActions, allowedHosts: readonly string[] = []) {
|
|
109
|
+
return {
|
|
110
|
+
kind: 'prefix' as const, path: CONSOLE_PREFIX,
|
|
111
|
+
handler: async (req: IncomingMessage, res: ServerResponse) => {
|
|
112
|
+
let result: JsonResult;
|
|
113
|
+
try {
|
|
114
|
+
if (req.headers['sec-fetch-site'] === 'cross-site' || !knownHost(req.headers.host, allowedHosts)) result = { status: 403, body: { error: 'cross-site requests are not allowed' } };
|
|
115
|
+
else {
|
|
116
|
+
const method = req.method ?? 'GET';
|
|
117
|
+
const write = method === 'POST' && req.headers[WRITE_HEADER] === '1' && String(req.headers['content-type'] ?? '').startsWith('application/json');
|
|
118
|
+
const body = write ? jsonObject(await readBody(req, MAX_BODY_BYTES)) : undefined;
|
|
119
|
+
result = write && body === undefined ? bad('a JSON object body is required') : await handleConsole(sources, actions, { method, url: req.url ?? '/', write, body });
|
|
120
|
+
}
|
|
121
|
+
} catch { result = { status: 500, body: { error: 'internal error' } }; }
|
|
122
|
+
sendJson(res, result);
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { SessionId } from '@deepseek-ai/dsh-session';
|
|
3
|
+
import { createHistory, renderHistory } from '../history.ts';
|
|
4
|
+
import { createDiaryReader } from '../life/diary.ts';
|
|
5
|
+
import type { LifeStore } from '../life/lifeStore.ts';
|
|
6
|
+
import type { LifeRuntime } from '../life/runtime.ts';
|
|
7
|
+
import type { ArchiveStore } from '../store.ts';
|
|
8
|
+
import type { WorldConfig } from '../types.ts';
|
|
9
|
+
import type { WorldNames } from '../worldPrompt.ts';
|
|
10
|
+
import type { MemoryArchiveScope } from '../archiveSelection.ts';
|
|
11
|
+
import type { ConsoleSources } from './routes.ts';
|
|
12
|
+
|
|
13
|
+
const SNAPSHOT_TTL_MS = 5000;
|
|
14
|
+
export function estimateTokens(text: string): number {
|
|
15
|
+
const cjk = (text.match(/[㐀-鿿豈- -〿-]/g) ?? []).length;
|
|
16
|
+
return Math.round(cjk * 0.6 + (text.length - cjk) / 3.5);
|
|
17
|
+
}
|
|
18
|
+
export interface SnapshotDeps {
|
|
19
|
+
ctx: Context;
|
|
20
|
+
config: WorldConfig;
|
|
21
|
+
archive: ArchiveStore;
|
|
22
|
+
names(): WorldNames;
|
|
23
|
+
/** Absent when the life line could not be set up. */
|
|
24
|
+
lifeStore?: LifeStore;
|
|
25
|
+
/** The running life line; undefined while her world is stopped. */
|
|
26
|
+
life(): LifeRuntime | undefined;
|
|
27
|
+
diaryDir: string;
|
|
28
|
+
running(): boolean;
|
|
29
|
+
/** CNY a day for her home; undefined is no limit. */
|
|
30
|
+
dailyBudget(): number | undefined;
|
|
31
|
+
/** Turns her memory block carries. */
|
|
32
|
+
memoryTurns(): number;
|
|
33
|
+
selectedArchiveScope?(): MemoryArchiveScope;
|
|
34
|
+
/** The WeChat module's state; undefined while it is off or her world is stopped. */
|
|
35
|
+
wechat?(): unknown;
|
|
36
|
+
includesSession?(id: string): boolean;
|
|
37
|
+
/** Faults outside the life line: the memory line, the bridge, the QQ bridge, starting and stopping. */
|
|
38
|
+
faults(): string[];
|
|
39
|
+
/** Which modules the config allows, whether their console switch is on, and whether they are running now. */
|
|
40
|
+
modules?(): unknown;
|
|
41
|
+
/** The QQ bridge, when the module is on; absent leaves that card out of the console. */
|
|
42
|
+
qq?: () => Promise<unknown>;
|
|
43
|
+
}
|
|
44
|
+
/** Read-only views for the console; repeated polling within a few seconds reuses one snapshot until `invalidate`. */
|
|
45
|
+
export function consoleSources(deps: SnapshotDeps): ConsoleSources & { invalidate(): void } {
|
|
46
|
+
const { config, archive, lifeStore } = deps;
|
|
47
|
+
const history = createHistory(archive);
|
|
48
|
+
const diary = createDiaryReader(deps.diaryDir);
|
|
49
|
+
let cached: { at: number; value: Promise<unknown> } | undefined;
|
|
50
|
+
async function compute(): Promise<unknown> {
|
|
51
|
+
const life = deps.life()?.snapshot();
|
|
52
|
+
const home = life?.homeSessionId ? deps.ctx.agents.get(SessionId(life.homeSessionId)) : undefined;
|
|
53
|
+
const window = deps.memoryTurns();
|
|
54
|
+
const turns = await history.loadRecentTurns(config.worldId, window, deps.includesSession);
|
|
55
|
+
const archiveScope = config.archiveScope ?? 'workspace';
|
|
56
|
+
const selectedArchiveScope = deps.selectedArchiveScope?.() ?? archiveScope;
|
|
57
|
+
// Never let a dead bridge take the whole console down with it: a broken QQ module shows as "not running".
|
|
58
|
+
const qq = deps.qq ? await deps.qq().catch(() => null) : null;
|
|
59
|
+
return {
|
|
60
|
+
worldId: config.worldId, timeZone: config.timeZone, now: new Date().toISOString(), running: deps.running(), name: deps.names().companion,
|
|
61
|
+
budget: { daily: deps.dailyBudget() ?? null, today: life?.spend ?? null },
|
|
62
|
+
life: life ? { homeSessionId: life.homeSessionId, status: home ? home.status : 'cold', alarm: life.alarm, review: life.review, recentWakes: lifeStore?.wakes().slice(-5).reverse() ?? [] } : null,
|
|
63
|
+
health: [...(life?.faults ?? []), ...deps.faults()],
|
|
64
|
+
memory: { turns: turns.length, windowTurns: window, archiveScope, selectedArchiveScope, restartRequired: archiveScope !== selectedArchiveScope, approxTokens: estimateTokens(renderHistory(turns, config.timeZone, deps.names(), archiveScope === 'home')), oldest: turns.at(-1)?.messages[0]?.occurredAt, ...archive.stats() },
|
|
65
|
+
wechat: deps.wechat?.() ?? null,
|
|
66
|
+
compactions: lifeStore?.compactions().slice(-5).reverse() ?? [],
|
|
67
|
+
diaryDates: diary.dates(), activityDays: lifeStore?.activityDays() ?? [],
|
|
68
|
+
qq: deps.qq ? qq : null,
|
|
69
|
+
modules: deps.modules?.() ?? null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
snapshot() {
|
|
74
|
+
if (cached && Date.now() - cached.at < SNAPSHOT_TTL_MS) return cached.value;
|
|
75
|
+
const entry = { at: Date.now(), value: compute() };
|
|
76
|
+
cached = entry;
|
|
77
|
+
entry.value.catch(() => { if (cached === entry) cached = undefined; });
|
|
78
|
+
return entry.value;
|
|
79
|
+
},
|
|
80
|
+
invalidate() { cached = undefined; },
|
|
81
|
+
activity: (day, outsideOnly) => lifeStore?.activity(day).filter(record => !outsideOnly || record.outside).reverse() ?? [],
|
|
82
|
+
diary: date => diary.read(date),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# 外部生态接入
|
|
2
|
+
|
|
3
|
+
外部平台、桌宠等程序的接入代码统一放在此目录,核心记忆归档、人设和生活线保留在 `src/` 原有模块中。
|
|
4
|
+
|
|
5
|
+
新增模块的目录约定、公共协议、内核变更说明和提交要求见 [贡献协议](../../CONTRIBUTING.md)。
|
|
6
|
+
|
|
7
|
+
- `wechat/`:微信宿主接口、回复编排、图片接收和发送回执;`windows/` 放 Python 收发适配器及 `engine/` 原生桥。启动步骤见 [微信模块](wechat/windows/README.md)。
|
|
8
|
+
- `qq/`:外部 `qq-bridge` 进程的管理:探活、拉起与停止、把 DSH 当前地址写回桥的配置、会话绑定、巡检自动重拉、SnowLuma 控制台入口,以及主动聊天开关。QQ 消息由桥通过会话接口送入家,这里不实现收发。
|
|
9
|
+
- `shared/`:通用来源身份、群上下文、平台会话路由和消息出口。
|
|
10
|
+
- `bridge/`:桌宠及其他本地程序向家的通用桥。
|
|
11
|
+
|
|
12
|
+
各平台自身逻辑放在各自目录;后续 B 站接入时新增 `bilibili/`。平台适配器使用共同的身份和会话协议,通过现有归档服务保存正文,不各建一份记忆。
|
|
13
|
+
|
|
14
|
+
微信本机配置、账号状态、依赖环境和原生构建产物继续保存在项目根目录的 `.host/wechat/`,不会打入发布包。运行入口为 `src/ecosystem/wechat/windows/wechat.ps1`。
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { readFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
4
|
+
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
5
|
+
import { createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
6
|
+
import type {} from '@deepseek-ai/dsh-session';
|
|
7
|
+
import { durableWrite } from '../../life/lifeStore.ts';
|
|
8
|
+
import { errorText, Faults } from '../../faults.ts';
|
|
9
|
+
import { realTimers, type LifeTimers } from '../../life/alarmTimer.ts';
|
|
10
|
+
import { busyText, pushOf, type Outlet, type Push, type PushKind } from '../shared/outlets.ts';
|
|
11
|
+
|
|
12
|
+
export const REPLY_TIMEOUT_MS = 3 * 60_000;
|
|
13
|
+
/** The words could not be handed to her home; the route answers 503. */
|
|
14
|
+
export class HomeUnavailable extends Error {}
|
|
15
|
+
export interface BridgeDeps {
|
|
16
|
+
ctx: Context;
|
|
17
|
+
/** The current home, resolving or rebuilding it (the life line's own). */
|
|
18
|
+
home(): Promise<Agent>;
|
|
19
|
+
/** Where her replies and the busy notice go; each takes the kinds it lists. */
|
|
20
|
+
outlets: readonly Outlet[];
|
|
21
|
+
origin(): string;
|
|
22
|
+
/** `<world>/bridge.json`, where a local app finds the bridge. */
|
|
23
|
+
file: string;
|
|
24
|
+
timers?: LifeTimers;
|
|
25
|
+
clock?: () => Date;
|
|
26
|
+
/** Her name, in the busy notice and the console's faults; TA when unnamed. */
|
|
27
|
+
name?: string;
|
|
28
|
+
}
|
|
29
|
+
interface Tracked { id: string; sessionId: string; admitted: boolean; timer?: unknown }
|
|
30
|
+
const labels = (name: string): Record<PushKind, string> => ({ reply: `${name}的回复`, busy: `「${name}在忙」`, reach: `${name}找你的话` });
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A local app's way into her home: its words arrive as her partner's own (source `user`), and the first
|
|
34
|
+
* visible text she says after they are admitted goes to the outlets. Only the latest words are tracked.
|
|
35
|
+
*/
|
|
36
|
+
export function createBridge(deps: BridgeDeps) {
|
|
37
|
+
const { ctx } = deps;
|
|
38
|
+
const name = deps.name ?? 'TA';
|
|
39
|
+
const clock = deps.clock ?? (() => new Date());
|
|
40
|
+
const timers = deps.timers ?? realTimers;
|
|
41
|
+
const faults = new Faults();
|
|
42
|
+
const disposers: (() => void)[] = [];
|
|
43
|
+
const token = randomBytes(32).toString('hex');
|
|
44
|
+
let tracked: Tracked | undefined;
|
|
45
|
+
let stopped = false;
|
|
46
|
+
|
|
47
|
+
function send(push: Push): void {
|
|
48
|
+
for (const outlet of deps.outlets) {
|
|
49
|
+
if (!outlet.kinds.includes(push.kind)) continue;
|
|
50
|
+
const key = `push:${outlet.name}`;
|
|
51
|
+
void outlet.send(push).then(() => faults.clear(key), error => {
|
|
52
|
+
if (stopped) return;
|
|
53
|
+
faults.set(key, `${outlet.name}没收到${labels(name)[push.kind]}:${errorText(error)}`);
|
|
54
|
+
ctx.logger.warn(`dsh-world: the push to ${outlet.name} failed:`, error);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function untrack(): void {
|
|
59
|
+
if (tracked?.timer !== undefined) timers.clear(tracked.timer);
|
|
60
|
+
tracked = undefined;
|
|
61
|
+
}
|
|
62
|
+
function track(id: string, sessionId: string): void {
|
|
63
|
+
untrack();
|
|
64
|
+
const entry: Tracked = { id, sessionId, admitted: false };
|
|
65
|
+
// The reply stays tracked after "busy": it is still pushed when it comes.
|
|
66
|
+
entry.timer = timers.set(() => { entry.timer = undefined; if (tracked === entry && !stopped) send({ text: busyText(name), kind: 'busy', name }); }, REPLY_TIMEOUT_MS);
|
|
67
|
+
tracked = entry;
|
|
68
|
+
}
|
|
69
|
+
function start(): void {
|
|
70
|
+
disposers.push(ctx.on('session/event', (session, event) => {
|
|
71
|
+
if (!tracked || session.id !== tracked.sessionId) return;
|
|
72
|
+
if (event.type === 'user/message' && event.data.id === tracked.id) { tracked.admitted = true; return; }
|
|
73
|
+
if (event.type !== 'assistant/message' || !tracked.admitted) return;
|
|
74
|
+
const visible = event.data.message.content.flatMap(block => block.type === 'text' ? [block.text] : []).join('\n');
|
|
75
|
+
const reply = pushOf(visible, 'reply', name, safeOrigin());
|
|
76
|
+
if (!reply.text && !reply.images) return;
|
|
77
|
+
untrack();
|
|
78
|
+
send(reply);
|
|
79
|
+
}));
|
|
80
|
+
try {
|
|
81
|
+
durableWrite(deps.file, JSON.stringify({ origin: deps.origin(), token, pid: process.pid, startedAt: clock().toISOString(), name }, null, 2));
|
|
82
|
+
faults.clear('file');
|
|
83
|
+
} catch (error) { faults.set('file', `bridge.json 没能写入,外面的程序找不到家:${errorText(error)}`); ctx.logger.error(error); }
|
|
84
|
+
}
|
|
85
|
+
function safeOrigin(): string | undefined { try { return deps.origin(); } catch { return undefined; } }
|
|
86
|
+
function stop(): void {
|
|
87
|
+
if (stopped) return;
|
|
88
|
+
stopped = true;
|
|
89
|
+
untrack();
|
|
90
|
+
for (const dispose of disposers.splice(0).reverse()) dispose();
|
|
91
|
+
// A newer start (a reload) may already have written its own token; that file is not ours to remove.
|
|
92
|
+
try { if (JSON.parse(readFileSync(deps.file, 'utf8')).token === token) rmSync(deps.file, { force: true }); } catch { /* gone or unreadable */ }
|
|
93
|
+
}
|
|
94
|
+
function checkToken(value: string | undefined): boolean {
|
|
95
|
+
if (typeof value !== 'string') return false;
|
|
96
|
+
const given = Buffer.from(value); const expected = Buffer.from(token);
|
|
97
|
+
return given.length === expected.length && timingSafeEqual(given, expected);
|
|
98
|
+
}
|
|
99
|
+
async function say(text: string): Promise<{ id: string }> {
|
|
100
|
+
if (stopped) throw new HomeUnavailable('the bridge is stopped');
|
|
101
|
+
let agent: Agent;
|
|
102
|
+
try { agent = await deps.home(); }
|
|
103
|
+
catch (error) { faults.set('home', `从外面送来的话没进家:${errorText(error)}`); ctx.logger.error(error); throw new HomeUnavailable(errorText(error), { cause: error }); }
|
|
104
|
+
if (stopped) throw new HomeUnavailable('the bridge is stopped');
|
|
105
|
+
const words = createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text }] });
|
|
106
|
+
// Tracked first: steering can admit the words before it returns.
|
|
107
|
+
track(words.id, agent.id);
|
|
108
|
+
try {
|
|
109
|
+
// Like her partner typing in the home: into the running turn at its next step, or a new turn.
|
|
110
|
+
if (agent.status === 'running') agent.steer(words); else agent.followup(words);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
untrack();
|
|
113
|
+
faults.set('home', `从外面送来的话没进家:${errorText(error)}`); ctx.logger.error(error);
|
|
114
|
+
throw new HomeUnavailable(errorText(error), { cause: error });
|
|
115
|
+
}
|
|
116
|
+
faults.clear('home');
|
|
117
|
+
return { id: words.id };
|
|
118
|
+
}
|
|
119
|
+
return { start, stop, say, checkToken, faults: () => faults.list() };
|
|
120
|
+
}
|
|
121
|
+
export type Bridge = ReturnType<typeof createBridge>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { jsonObject, knownHost, readBody, sendJson, type JsonResult } from '../../http.ts';
|
|
3
|
+
import { HomeUnavailable, type Bridge } from './bridge.ts';
|
|
4
|
+
|
|
5
|
+
export const BRIDGE_PATH = '/dsh-world/bridge/say';
|
|
6
|
+
const MAX_BODY_BYTES = 32 * 1024;
|
|
7
|
+
const MAX_TEXT = 2000;
|
|
8
|
+
const bad: JsonResult = { status: 400, body: { error: 'bad request' } };
|
|
9
|
+
const unavailable: JsonResult = { status: 503, body: { error: 'home unavailable' } };
|
|
10
|
+
|
|
11
|
+
async function handleSay(bridge: Bridge | undefined, req: IncomingMessage, allowedHosts: readonly string[]): Promise<JsonResult> {
|
|
12
|
+
if (req.method !== 'POST') return { status: 405, body: { error: 'method not allowed' }, headers: { allow: 'POST' } };
|
|
13
|
+
const token = req.headers['x-dsh-world-token'];
|
|
14
|
+
if (req.headers['sec-fetch-site'] === 'cross-site' || !knownHost(req.headers.host, allowedHosts)) return { status: 403, body: { error: 'forbidden' } };
|
|
15
|
+
// While her world is stopped there is no bridge and no token to check against.
|
|
16
|
+
if (!bridge) return unavailable;
|
|
17
|
+
if (!bridge.checkToken(Array.isArray(token) ? undefined : token)) return { status: 403, body: { error: 'forbidden' } };
|
|
18
|
+
const body = jsonObject(await readBody(req, MAX_BODY_BYTES));
|
|
19
|
+
const text = typeof body?.text === 'string' ? body.text.trim() : '';
|
|
20
|
+
const length = [...text].length;
|
|
21
|
+
if (length < 1 || length > MAX_TEXT) return bad;
|
|
22
|
+
try { return { status: 202, body: { accepted: true, id: (await bridge.say(text)).id } }; }
|
|
23
|
+
catch (error) { return error instanceof HomeUnavailable ? unavailable : { status: 500, body: { error: 'internal error' } }; }
|
|
24
|
+
}
|
|
25
|
+
/** The one outside route that changes something: a local app hands her partner's words to her home. */
|
|
26
|
+
export function bridgeRoute(bridge: () => Bridge | undefined, allowedHosts: readonly string[] = []) {
|
|
27
|
+
return {
|
|
28
|
+
kind: 'exact' as const, path: BRIDGE_PATH,
|
|
29
|
+
handler: async (req: IncomingMessage, res: ServerResponse) => {
|
|
30
|
+
let result: JsonResult;
|
|
31
|
+
try { result = await handleSay(bridge(), req, allowedHosts); } catch { result = { status: 500, body: { error: 'internal error' } }; }
|
|
32
|
+
sendJson(res, result);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|