@ohmykreee/dsh-remote-control 0.0.0-stage → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +556 -0
- package/INSTALLATION.md +310 -0
- package/LICENSE +21 -0
- package/README.md +195 -2
- package/client.js +3 -0
- package/components.json +20 -0
- package/cordis.patch.yml +3 -0
- package/docs/README.en.md +196 -0
- package/docs/screenshots/en/overview.png +0 -0
- package/docs/screenshots/en/question.png +0 -0
- package/docs/screenshots/zh/overview.png +0 -0
- package/docs/screenshots/zh/question.png +0 -0
- package/icon.svg +24 -0
- package/index.js +2 -0
- package/lib/account.js +1 -0
- package/lib/app-mapping.js +1 -0
- package/lib/i18n.js +1 -0
- package/lib/media-enrich.js +1 -0
- package/lib/notifications.js +1 -0
- package/lib/performance.js +1 -0
- package/lib/platform/index.js +1 -0
- package/lib/probe-adapter.js +1 -0
- package/lib/probe-daemon.js +1 -0
- package/lib/probe.js +1 -0
- package/lib/qr.js +1 -0
- package/lib/runtime.js +1 -0
- package/lib/service.js +1 -0
- package/lib/sessions.js +1 -0
- package/lib/settings.js +1 -0
- package/lib/types.js +1 -0
- package/lib/util.js +1 -0
- package/lib/weather.js +1 -0
- package/lib/ws.js +1 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +118 -4
- package/scripts/browser-shot.mjs +217 -0
- package/scripts/build-dsh.mjs +228 -0
- package/scripts/build-rust.mjs +319 -0
- package/scripts/build-web.mjs +17 -0
- package/scripts/check-npm-package.mjs +322 -0
- package/scripts/mock-preview.py +574 -0
- package/scripts/screenshot-compose.py +89 -0
- package/scripts/screenshot-device.sh +75 -0
- package/src/dsh/client/api.ts +68 -0
- package/src/dsh/client/i18n.ts +573 -0
- package/src/dsh/client/icons.ts +56 -0
- package/src/dsh/client/index.ts +95 -0
- package/src/dsh/client/logic.ts +310 -0
- package/src/dsh/client/panel.ts +483 -0
- package/src/dsh/client/sections/apps.ts +40 -0
- package/src/dsh/client/sections/balance.ts +38 -0
- package/src/dsh/client/sections/connection.ts +97 -0
- package/src/dsh/client/sections/notifications.ts +43 -0
- package/src/dsh/client/sections/pomodoro.ts +184 -0
- package/src/dsh/client/sections/preferences.ts +138 -0
- package/src/dsh/client/sections/system.ts +120 -0
- package/src/dsh/client/sections/weather.ts +276 -0
- package/src/dsh/client/styles.ts +1119 -0
- package/src/dsh/client/types.ts +366 -0
- package/src/dsh/client/ui.ts +142 -0
- package/src/dsh/index.ts +108 -0
- package/src/dsh/lib/account.ts +520 -0
- package/src/dsh/lib/app-mapping.ts +366 -0
- package/src/dsh/lib/i18n.ts +82 -0
- package/src/dsh/lib/media-enrich.ts +202 -0
- package/src/dsh/lib/notifications.ts +322 -0
- package/src/dsh/lib/performance.ts +279 -0
- package/src/dsh/lib/platform/index.ts +43 -0
- package/src/dsh/lib/probe-adapter.ts +275 -0
- package/src/dsh/lib/probe-daemon.ts +359 -0
- package/src/dsh/lib/probe.ts +395 -0
- package/src/dsh/lib/qr.ts +42 -0
- package/src/dsh/lib/runtime.ts +126 -0
- package/src/dsh/lib/service.ts +1645 -0
- package/src/dsh/lib/sessions.ts +907 -0
- package/src/dsh/lib/settings.ts +468 -0
- package/src/dsh/lib/types.ts +231 -0
- package/src/dsh/lib/util.ts +115 -0
- package/src/dsh/lib/weather.ts +532 -0
- package/src/dsh/lib/ws.ts +328 -0
- package/src/dsh/types/qrcode-generator.d.ts +16 -0
- package/src/rust/Cargo.lock +1431 -0
- package/src/rust/Cargo.toml +68 -0
- package/src/rust/src/cli.rs +233 -0
- package/src/rust/src/daemon.rs +140 -0
- package/src/rust/src/location/linux.rs +118 -0
- package/src/rust/src/location/macos.rs +26 -0
- package/src/rust/src/location/mod.rs +170 -0
- package/src/rust/src/location/windows.rs +205 -0
- package/src/rust/src/main.rs +42 -0
- package/src/rust/src/media/catalog.rs +454 -0
- package/src/rust/src/media/linux.rs +350 -0
- package/src/rust/src/media/macos/accessibility.rs +59 -0
- package/src/rust/src/media/macos.rs +366 -0
- package/src/rust/src/media/mod.rs +298 -0
- package/src/rust/src/media/vendor/apple_music.rs +90 -0
- package/src/rust/src/media/vendor/common.rs +186 -0
- package/src/rust/src/media/vendor/kugou.rs +52 -0
- package/src/rust/src/media/vendor/kuwo.rs +49 -0
- package/src/rust/src/media/vendor/mod.rs +75 -0
- package/src/rust/src/media/vendor/netease.rs +1715 -0
- package/src/rust/src/media/vendor/qishui.rs +36 -0
- package/src/rust/src/media/vendor/qqmusic.rs +33 -0
- package/src/rust/src/media/vendor/spotify.rs +46 -0
- package/src/rust/src/media/vendor/wesing.rs +39 -0
- package/src/rust/src/media/vendor/youtube_music.rs +49 -0
- package/src/rust/src/media/windows/accessibility.rs +85 -0
- package/src/rust/src/media/windows/audio.rs +116 -0
- package/src/rust/src/media/windows/private.rs +291 -0
- package/src/rust/src/media/windows.rs +775 -0
- package/src/rust/src/notifications/linux.rs +276 -0
- package/src/rust/src/notifications/macos.rs +382 -0
- package/src/rust/src/notifications/mod.rs +244 -0
- package/src/rust/src/notifications/windows.rs +565 -0
- package/src/rust/src/perf/common.rs +109 -0
- package/src/rust/src/perf/linux.rs +282 -0
- package/src/rust/src/perf/macos.rs +28 -0
- package/src/rust/src/perf/mod.rs +157 -0
- package/src/rust/src/perf/windows.rs +981 -0
- package/src/rust/src/util.rs +360 -0
- package/src/rust/src/volume/linux.rs +195 -0
- package/src/rust/src/volume/macos.rs +54 -0
- package/src/rust/src/volume/mod.rs +116 -0
- package/src/rust/src/volume/windows.rs +75 -0
- package/src/rust/tests/cli.rs +79 -0
- package/src/rust/tests/daemon.rs +107 -0
- package/src/svelte/App.svelte +385 -0
- package/src/svelte/index.html +18 -0
- package/src/svelte/lib/billing.svelte.ts +228 -0
- package/src/svelte/lib/components/BalanceCard.svelte +173 -0
- package/src/svelte/lib/components/ClockCard.svelte +183 -0
- package/src/svelte/lib/components/CountdownCard.svelte +261 -0
- package/src/svelte/lib/components/CpuMemoryCard.svelte +238 -0
- package/src/svelte/lib/components/EmptyState.svelte +34 -0
- package/src/svelte/lib/components/GpuCard.svelte +245 -0
- package/src/svelte/lib/components/HomePage.svelte +34 -0
- package/src/svelte/lib/components/MusicControls.svelte +256 -0
- package/src/svelte/lib/components/MusicCover.svelte +88 -0
- package/src/svelte/lib/components/MusicPage.svelte +116 -0
- package/src/svelte/lib/components/NotificationsCard.svelte +109 -0
- package/src/svelte/lib/components/Panel.svelte +22 -0
- package/src/svelte/lib/components/PerformancePage.svelte +55 -0
- package/src/svelte/lib/components/PomodoroCard.svelte +250 -0
- package/src/svelte/lib/components/QuestionSheet.svelte +354 -0
- package/src/svelte/lib/components/RadialGauge.svelte +99 -0
- package/src/svelte/lib/components/Rail.svelte +217 -0
- package/src/svelte/lib/components/SectionHeader.svelte +40 -0
- package/src/svelte/lib/components/SeekBar.svelte +421 -0
- package/src/svelte/lib/components/SessionList.svelte +364 -0
- package/src/svelte/lib/components/SessionStats.svelte +98 -0
- package/src/svelte/lib/components/SessionsPage.svelte +61 -0
- package/src/svelte/lib/components/SpendCard.svelte +105 -0
- package/src/svelte/lib/components/ThermalCard.svelte +249 -0
- package/src/svelte/lib/components/Toast.svelte +32 -0
- package/src/svelte/lib/components/WeatherIcon.svelte +40 -0
- package/src/svelte/lib/components/WeatherPanel.svelte +420 -0
- package/src/svelte/lib/components/ui/alert/alert-action.svelte +20 -0
- package/src/svelte/lib/components/ui/alert/alert-description.svelte +23 -0
- package/src/svelte/lib/components/ui/alert/alert-title.svelte +23 -0
- package/src/svelte/lib/components/ui/alert/alert.svelte +43 -0
- package/src/svelte/lib/components/ui/alert/index.ts +17 -0
- package/src/svelte/lib/components/ui/avatar/avatar-badge.svelte +26 -0
- package/src/svelte/lib/components/ui/avatar/avatar-fallback.svelte +20 -0
- package/src/svelte/lib/components/ui/avatar/avatar-group-count.svelte +23 -0
- package/src/svelte/lib/components/ui/avatar/avatar-group.svelte +23 -0
- package/src/svelte/lib/components/ui/avatar/avatar-image.svelte +17 -0
- package/src/svelte/lib/components/ui/avatar/avatar.svelte +26 -0
- package/src/svelte/lib/components/ui/avatar/index.ts +22 -0
- package/src/svelte/lib/components/ui/badge/badge.svelte +49 -0
- package/src/svelte/lib/components/ui/badge/index.ts +2 -0
- package/src/svelte/lib/components/ui/button/button.svelte +82 -0
- package/src/svelte/lib/components/ui/button/index.ts +17 -0
- package/src/svelte/lib/components/ui/card/card-action.svelte +23 -0
- package/src/svelte/lib/components/ui/card/card-content.svelte +20 -0
- package/src/svelte/lib/components/ui/card/card-description.svelte +20 -0
- package/src/svelte/lib/components/ui/card/card-footer.svelte +20 -0
- package/src/svelte/lib/components/ui/card/card-header.svelte +23 -0
- package/src/svelte/lib/components/ui/card/card-title.svelte +20 -0
- package/src/svelte/lib/components/ui/card/card.svelte +22 -0
- package/src/svelte/lib/components/ui/card/index.ts +25 -0
- package/src/svelte/lib/components/ui/dialog/dialog-close.svelte +11 -0
- package/src/svelte/lib/components/ui/dialog/dialog-content.svelte +48 -0
- package/src/svelte/lib/components/ui/dialog/dialog-description.svelte +17 -0
- package/src/svelte/lib/components/ui/dialog/dialog-footer.svelte +32 -0
- package/src/svelte/lib/components/ui/dialog/dialog-header.svelte +20 -0
- package/src/svelte/lib/components/ui/dialog/dialog-overlay.svelte +17 -0
- package/src/svelte/lib/components/ui/dialog/dialog-portal.svelte +7 -0
- package/src/svelte/lib/components/ui/dialog/dialog-title.svelte +17 -0
- package/src/svelte/lib/components/ui/dialog/dialog-trigger.svelte +11 -0
- package/src/svelte/lib/components/ui/dialog/dialog.svelte +7 -0
- package/src/svelte/lib/components/ui/dialog/index.ts +34 -0
- package/src/svelte/lib/components/ui/empty/empty-content.svelte +23 -0
- package/src/svelte/lib/components/ui/empty/empty-description.svelte +23 -0
- package/src/svelte/lib/components/ui/empty/empty-header.svelte +20 -0
- package/src/svelte/lib/components/ui/empty/empty-media.svelte +41 -0
- package/src/svelte/lib/components/ui/empty/empty-title.svelte +20 -0
- package/src/svelte/lib/components/ui/empty/empty.svelte +23 -0
- package/src/svelte/lib/components/ui/empty/index.ts +22 -0
- package/src/svelte/lib/components/ui/input/index.ts +7 -0
- package/src/svelte/lib/components/ui/input/input.svelte +48 -0
- package/src/svelte/lib/components/ui/progress/index.ts +7 -0
- package/src/svelte/lib/components/ui/progress/progress.svelte +27 -0
- package/src/svelte/lib/components/ui/scroll-area/index.ts +10 -0
- package/src/svelte/lib/components/ui/scroll-area/scroll-area-scrollbar.svelte +30 -0
- package/src/svelte/lib/components/ui/scroll-area/scroll-area.svelte +43 -0
- package/src/svelte/lib/components/ui/separator/index.ts +7 -0
- package/src/svelte/lib/components/ui/separator/separator.svelte +21 -0
- package/src/svelte/lib/components/ui/sheet/index.ts +34 -0
- package/src/svelte/lib/components/ui/sheet/sheet-close.svelte +7 -0
- package/src/svelte/lib/components/ui/sheet/sheet-content.svelte +55 -0
- package/src/svelte/lib/components/ui/sheet/sheet-description.svelte +17 -0
- package/src/svelte/lib/components/ui/sheet/sheet-footer.svelte +20 -0
- package/src/svelte/lib/components/ui/sheet/sheet-header.svelte +20 -0
- package/src/svelte/lib/components/ui/sheet/sheet-overlay.svelte +20 -0
- package/src/svelte/lib/components/ui/sheet/sheet-portal.svelte +7 -0
- package/src/svelte/lib/components/ui/sheet/sheet-title.svelte +17 -0
- package/src/svelte/lib/components/ui/sheet/sheet-trigger.svelte +7 -0
- package/src/svelte/lib/components/ui/sheet/sheet.svelte +7 -0
- package/src/svelte/lib/components/ui/skeleton/index.ts +7 -0
- package/src/svelte/lib/components/ui/skeleton/skeleton.svelte +17 -0
- package/src/svelte/lib/components/ui/slider/index.ts +7 -0
- package/src/svelte/lib/components/ui/slider/slider.svelte +52 -0
- package/src/svelte/lib/components/ui/tooltip/index.ts +19 -0
- package/src/svelte/lib/components/ui/tooltip/tooltip-content.svelte +51 -0
- package/src/svelte/lib/components/ui/tooltip/tooltip-portal.svelte +7 -0
- package/src/svelte/lib/components/ui/tooltip/tooltip-provider.svelte +7 -0
- package/src/svelte/lib/components/ui/tooltip/tooltip-trigger.svelte +7 -0
- package/src/svelte/lib/components/ui/tooltip/tooltip.svelte +7 -0
- package/src/svelte/lib/countdown.svelte.ts +252 -0
- package/src/svelte/lib/format.ts +199 -0
- package/src/svelte/lib/i18n.svelte.ts +583 -0
- package/src/svelte/lib/media.ts +32 -0
- package/src/svelte/lib/pages.ts +18 -0
- package/src/svelte/lib/pomodoro.svelte.ts +223 -0
- package/src/svelte/lib/session-tree.ts +80 -0
- package/src/svelte/lib/state.svelte.ts +359 -0
- package/src/svelte/lib/timer-alarm.svelte.ts +319 -0
- package/src/svelte/lib/types.ts +306 -0
- package/src/svelte/lib/utils.ts +8 -0
- package/src/svelte/lib/weather.ts +84 -0
- package/src/svelte/lib/ws.ts +104 -0
- package/src/svelte/main.ts +13 -0
- package/src/svelte/styles/global.css +315 -0
- package/src/svelte/vite-env.d.ts +12 -0
- package/static/fonts/LICENSE.txt +287 -0
- package/static/icon.svg +24 -0
- package/static/manifest.webmanifest +18 -0
- package/svelte.config.js +5 -0
- package/tsconfig.dsh-client.json +18 -0
- package/tsconfig.dsh.json +21 -0
- package/tsconfig.json +20 -0
- package/vite.config.js +31 -0
- package/web/assets/index-DLdSfnJ3.css +1 -0
- package/web/assets/index-YNE8X0Uy.js +6 -0
- package/web/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/web/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/web/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/web/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/web/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/web/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/web/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/web/assets/jetbrains-mono-cyrillic-ext-wght-normal-EocZY2iu.woff2 +0 -0
- package/web/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/web/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/web/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/web/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/web/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/web/assets/noto-sans-sc-100-wght-normal-DluiXYzi.woff2 +0 -0
- package/web/assets/noto-sans-sc-101-wght-normal-CPkZHnw0.woff2 +0 -0
- package/web/assets/noto-sans-sc-102-wght-normal-gzIulUH6.woff2 +0 -0
- package/web/assets/noto-sans-sc-103-wght-normal-CJfSBxFo.woff2 +0 -0
- package/web/assets/noto-sans-sc-104-wght-normal-B3QMglgE.woff2 +0 -0
- package/web/assets/noto-sans-sc-105-wght-normal-xXh_OMfk.woff2 +0 -0
- package/web/assets/noto-sans-sc-106-wght-normal-B9n3jMjP.woff2 +0 -0
- package/web/assets/noto-sans-sc-107-wght-normal-D7fvVprN.woff2 +0 -0
- package/web/assets/noto-sans-sc-108-wght-normal-J0_LhxVo.woff2 +0 -0
- package/web/assets/noto-sans-sc-109-wght-normal-Dsp-De_0.woff2 +0 -0
- package/web/assets/noto-sans-sc-110-wght-normal-DE_6NUrb.woff2 +0 -0
- package/web/assets/noto-sans-sc-111-wght-normal-C4S-TlTn.woff2 +0 -0
- package/web/assets/noto-sans-sc-112-wght-normal-BcFuP3w6.woff2 +0 -0
- package/web/assets/noto-sans-sc-113-wght-normal-Dk932kmj.woff2 +0 -0
- package/web/assets/noto-sans-sc-114-wght-normal-CO_5Xo-i.woff2 +0 -0
- package/web/assets/noto-sans-sc-115-wght-normal-BWy07H14.woff2 +0 -0
- package/web/assets/noto-sans-sc-116-wght-normal-B4HVAfHo.woff2 +0 -0
- package/web/assets/noto-sans-sc-117-wght-normal-siZzFqgK.woff2 +0 -0
- package/web/assets/noto-sans-sc-118-wght-normal-xhBB7f6V.woff2 +0 -0
- package/web/assets/noto-sans-sc-119-wght-normal-Ck_g2FGz.woff2 +0 -0
- package/web/assets/noto-sans-sc-21-wght-normal-D0x9-yBt.woff2 +0 -0
- package/web/assets/noto-sans-sc-22-wght-normal-BBUVTShg.woff2 +0 -0
- package/web/assets/noto-sans-sc-23-wght-normal-Du1oYWkE.woff2 +0 -0
- package/web/assets/noto-sans-sc-24-wght-normal-CmWjJ0gt.woff2 +0 -0
- package/web/assets/noto-sans-sc-25-wght-normal-BFHiqilg.woff2 +0 -0
- package/web/assets/noto-sans-sc-26-wght-normal-C0sF2Z2f.woff2 +0 -0
- package/web/assets/noto-sans-sc-27-wght-normal-COFdrquJ.woff2 +0 -0
- package/web/assets/noto-sans-sc-28-wght-normal-DMgdNu1o.woff2 +0 -0
- package/web/assets/noto-sans-sc-29-wght-normal-Cmq8em30.woff2 +0 -0
- package/web/assets/noto-sans-sc-30-wght-normal-CZWqZ3yU.woff2 +0 -0
- package/web/assets/noto-sans-sc-31-wght-normal-Wfi_lutW.woff2 +0 -0
- package/web/assets/noto-sans-sc-32-wght-normal-BRJQ4PY5.woff2 +0 -0
- package/web/assets/noto-sans-sc-33-wght-normal-CUVQXNAJ.woff2 +0 -0
- package/web/assets/noto-sans-sc-34-wght-normal-Bspba0wC.woff2 +0 -0
- package/web/assets/noto-sans-sc-35-wght-normal-K8fX_YPG.woff2 +0 -0
- package/web/assets/noto-sans-sc-36-wght-normal-CkITyjoT.woff2 +0 -0
- package/web/assets/noto-sans-sc-37-wght-normal-WCZ2zfEe.woff2 +0 -0
- package/web/assets/noto-sans-sc-38-wght-normal-DeM7CUce.woff2 +0 -0
- package/web/assets/noto-sans-sc-39-wght-normal-BMZNLWQU.woff2 +0 -0
- package/web/assets/noto-sans-sc-4-wght-normal-AR5bdDjz.woff2 +0 -0
- package/web/assets/noto-sans-sc-40-wght-normal-BQ5xpS5j.woff2 +0 -0
- package/web/assets/noto-sans-sc-41-wght-normal-Hthm7FFi.woff2 +0 -0
- package/web/assets/noto-sans-sc-42-wght-normal-eaS2gKds.woff2 +0 -0
- package/web/assets/noto-sans-sc-43-wght-normal-o-T53XXV.woff2 +0 -0
- package/web/assets/noto-sans-sc-44-wght-normal-BJH5J4Ez.woff2 +0 -0
- package/web/assets/noto-sans-sc-45-wght-normal-BKuxSCOJ.woff2 +0 -0
- package/web/assets/noto-sans-sc-46-wght-normal-BR_5Mu6e.woff2 +0 -0
- package/web/assets/noto-sans-sc-47-wght-normal-CyY7gKNX.woff2 +0 -0
- package/web/assets/noto-sans-sc-48-wght-normal-BNMfTR6k.woff2 +0 -0
- package/web/assets/noto-sans-sc-49-wght-normal-DX-0y7wg.woff2 +0 -0
- package/web/assets/noto-sans-sc-5-wght-normal-CfWugllB.woff2 +0 -0
- package/web/assets/noto-sans-sc-50-wght-normal-DAOGW-Dr.woff2 +0 -0
- package/web/assets/noto-sans-sc-51-wght-normal-BlFLOjcP.woff2 +0 -0
- package/web/assets/noto-sans-sc-52-wght-normal-OZjrybnw.woff2 +0 -0
- package/web/assets/noto-sans-sc-53-wght-normal-CwyWNT6x.woff2 +0 -0
- package/web/assets/noto-sans-sc-54-wght-normal-DM0nRe18.woff2 +0 -0
- package/web/assets/noto-sans-sc-55-wght-normal-DBK2wUOR.woff2 +0 -0
- package/web/assets/noto-sans-sc-56-wght-normal-C-VKK1LS.woff2 +0 -0
- package/web/assets/noto-sans-sc-57-wght-normal-Cdbfh5kq.woff2 +0 -0
- package/web/assets/noto-sans-sc-58-wght-normal-CflMKARZ.woff2 +0 -0
- package/web/assets/noto-sans-sc-59-wght-normal-BS4Kwzxx.woff2 +0 -0
- package/web/assets/noto-sans-sc-6-wght-normal-ChnZIQfQ.woff2 +0 -0
- package/web/assets/noto-sans-sc-60-wght-normal-B7QOYsXJ.woff2 +0 -0
- package/web/assets/noto-sans-sc-61-wght-normal-C8JjQS7w.woff2 +0 -0
- package/web/assets/noto-sans-sc-62-wght-normal-bS4wq1Gg.woff2 +0 -0
- package/web/assets/noto-sans-sc-63-wght-normal-BJr8P-OB.woff2 +0 -0
- package/web/assets/noto-sans-sc-64-wght-normal-DVB11t7j.woff2 +0 -0
- package/web/assets/noto-sans-sc-65-wght-normal-DMPCXBvK.woff2 +0 -0
- package/web/assets/noto-sans-sc-66-wght-normal-BobaG10V.woff2 +0 -0
- package/web/assets/noto-sans-sc-67-wght-normal-Dzk7WhCR.woff2 +0 -0
- package/web/assets/noto-sans-sc-68-wght-normal-DGbCD9xu.woff2 +0 -0
- package/web/assets/noto-sans-sc-69-wght-normal-Ctrv1EqA.woff2 +0 -0
- package/web/assets/noto-sans-sc-70-wght-normal-RgTO941D.woff2 +0 -0
- package/web/assets/noto-sans-sc-71-wght-normal-D9sG-CWv.woff2 +0 -0
- package/web/assets/noto-sans-sc-72-wght-normal-C4oWy3ul.woff2 +0 -0
- package/web/assets/noto-sans-sc-73-wght-normal-B1ZoOTgM.woff2 +0 -0
- package/web/assets/noto-sans-sc-74-wght-normal-F7dH_rZJ.woff2 +0 -0
- package/web/assets/noto-sans-sc-75-wght-normal-Cp2wp4si.woff2 +0 -0
- package/web/assets/noto-sans-sc-76-wght-normal-B494D7xS.woff2 +0 -0
- package/web/assets/noto-sans-sc-77-wght-normal-B3MVY9Ax.woff2 +0 -0
- package/web/assets/noto-sans-sc-78-wght-normal-5jX7iNMC.woff2 +0 -0
- package/web/assets/noto-sans-sc-79-wght-normal-BIoZy8fr.woff2 +0 -0
- package/web/assets/noto-sans-sc-80-wght-normal-Bje1Fxy1.woff2 +0 -0
- package/web/assets/noto-sans-sc-81-wght-normal-B2NN7Qi3.woff2 +0 -0
- package/web/assets/noto-sans-sc-82-wght-normal-CUggNZi4.woff2 +0 -0
- package/web/assets/noto-sans-sc-83-wght-normal-99rD8YO-.woff2 +0 -0
- package/web/assets/noto-sans-sc-84-wght-normal-zMBmUocm.woff2 +0 -0
- package/web/assets/noto-sans-sc-85-wght-normal-CjI3qdxG.woff2 +0 -0
- package/web/assets/noto-sans-sc-86-wght-normal-BHY8TVEH.woff2 +0 -0
- package/web/assets/noto-sans-sc-87-wght-normal-DioZSHnU.woff2 +0 -0
- package/web/assets/noto-sans-sc-88-wght-normal-BXYxwf2t.woff2 +0 -0
- package/web/assets/noto-sans-sc-89-wght-normal-ClMFrFaL.woff2 +0 -0
- package/web/assets/noto-sans-sc-90-wght-normal-BnOlPreP.woff2 +0 -0
- package/web/assets/noto-sans-sc-91-wght-normal-DTr9RQcS.woff2 +0 -0
- package/web/assets/noto-sans-sc-97-wght-normal-9u6Aiav-.woff2 +0 -0
- package/web/assets/noto-sans-sc-98-wght-normal-BR-H2C0V.woff2 +0 -0
- package/web/assets/noto-sans-sc-99-wght-normal-BI6XPoxp.woff2 +0 -0
- package/web/assets/noto-sans-sc-cyrillic-wght-normal-BGmsQlw-.woff2 +0 -0
- package/web/assets/noto-sans-sc-latin-ext-wght-normal-DuJTpTg_.woff2 +0 -0
- package/web/assets/noto-sans-sc-latin-wght-normal-DDW8mKAR.woff2 +0 -0
- package/web/assets/noto-sans-sc-vietnamese-wght-normal-Bi3ATJ76.woff2 +0 -0
- package/web/fonts/LICENSE.txt +287 -0
- package/web/icon.svg +24 -0
- package/web/index.html +19 -0
- package/web/manifest.webmanifest +18 -0
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
/** Balance and Platform usage (today / current week) for the active DSH account. */
|
|
2
|
+
|
|
3
|
+
import { isRecord } from './types.js';
|
|
4
|
+
import type { DshContext, JsonRecord } from './types.js';
|
|
5
|
+
import {
|
|
6
|
+
finiteNumber,
|
|
7
|
+
localTimezoneOffsetSeconds,
|
|
8
|
+
serializeError,
|
|
9
|
+
startOfLocalDay,
|
|
10
|
+
startOfLocalWeek,
|
|
11
|
+
VERSION,
|
|
12
|
+
} from './util.js';
|
|
13
|
+
|
|
14
|
+
const BALANCE_TTL_MS = 60_000;
|
|
15
|
+
const BALANCE_ERROR_TTL_MS = 20_000;
|
|
16
|
+
const USAGE_TTL_MS = 90_000;
|
|
17
|
+
const USAGE_ERROR_TTL_MS = 30_000;
|
|
18
|
+
const REQUEST_TIMEOUT_MS = 9000;
|
|
19
|
+
|
|
20
|
+
/** Spend window selected in the console settings. */
|
|
21
|
+
export type SpendWindow = 'calendar' | 'rolling';
|
|
22
|
+
|
|
23
|
+
/** States emitted by `readBalance`. */
|
|
24
|
+
type AccountStatus = 'unavailable' | 'error' | 'signed-out' | 'failed' | 'ready';
|
|
25
|
+
|
|
26
|
+
/** States emitted by `readUsage`; account states are reused as-is. */
|
|
27
|
+
type UsageStatus = AccountStatus | 'expired' | 'empty';
|
|
28
|
+
|
|
29
|
+
/** Balance snapshot for the active account. */
|
|
30
|
+
export interface BalanceState {
|
|
31
|
+
signedIn: boolean;
|
|
32
|
+
state: AccountStatus;
|
|
33
|
+
currency?: string | null;
|
|
34
|
+
balance?: number | null;
|
|
35
|
+
paid?: number | null;
|
|
36
|
+
bonus?: number | null;
|
|
37
|
+
error?: string;
|
|
38
|
+
updatedAt?: number;
|
|
39
|
+
cached?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** One bar of the seven-day spend chart. */
|
|
43
|
+
interface UsageDay {
|
|
44
|
+
date: string;
|
|
45
|
+
label: string;
|
|
46
|
+
cost: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Spend snapshot for the active account. */
|
|
50
|
+
export interface UsageSnapshot {
|
|
51
|
+
state: UsageStatus;
|
|
52
|
+
currency: string | null;
|
|
53
|
+
today: number | null;
|
|
54
|
+
spend: number | null;
|
|
55
|
+
week?: number | null;
|
|
56
|
+
spendWindow: SpendWindow;
|
|
57
|
+
spendLabel: string;
|
|
58
|
+
periodStart?: string;
|
|
59
|
+
daily?: UsageDay[];
|
|
60
|
+
error?: string;
|
|
61
|
+
updatedAt?: number;
|
|
62
|
+
cached?: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Balance plus usage, as returned by `getAccount`. */
|
|
66
|
+
export interface AccountSnapshot {
|
|
67
|
+
available: boolean;
|
|
68
|
+
signedIn: boolean;
|
|
69
|
+
balance: BalanceState;
|
|
70
|
+
usage: UsageSnapshot;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Settings slice `createAccountService` reads. */
|
|
74
|
+
type AccountSettings = {
|
|
75
|
+
balance?: {
|
|
76
|
+
spendWindow?: string;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
interface AccountOptions {
|
|
81
|
+
locale?: string;
|
|
82
|
+
getSettings?: () => AccountSettings;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Balance request metadata, also the `getBalance` argument. */
|
|
86
|
+
type ClientMetadata = {
|
|
87
|
+
version: string;
|
|
88
|
+
locale: string;
|
|
89
|
+
timezoneOffsetSeconds: number;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** Minimal `deepseekAccount` service surface. */
|
|
93
|
+
interface DeepseekAccountService {
|
|
94
|
+
getState(): Promise<unknown>;
|
|
95
|
+
getBalance(metadata: JsonRecord): Promise<unknown>;
|
|
96
|
+
getPlatformSession?(): Promise<unknown>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Narrow an untrusted JSON value to an array. */
|
|
100
|
+
function asList(value: unknown): unknown[] {
|
|
101
|
+
return Array.isArray(value) ? value : [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function clientMetadata(locale = 'zh'): ClientMetadata {
|
|
105
|
+
return {
|
|
106
|
+
version: VERSION,
|
|
107
|
+
locale,
|
|
108
|
+
timezoneOffsetSeconds: localTimezoneOffsetSeconds(),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function timeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {
|
|
113
|
+
return new Promise<T>((resolve, reject) => {
|
|
114
|
+
const timer = setTimeout(() => reject(new Error(`${label} timed out`)), ms);
|
|
115
|
+
promise.then(
|
|
116
|
+
(value) => {
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
resolve(value);
|
|
119
|
+
},
|
|
120
|
+
(error: unknown) => {
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
reject(error);
|
|
123
|
+
},
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function currencyOf(wallets: unknown): string | null {
|
|
129
|
+
const list = asList(wallets);
|
|
130
|
+
const normalized = list
|
|
131
|
+
.map((wallet): { currency: string; balance: number | null } => {
|
|
132
|
+
const record: JsonRecord = isRecord(wallet) ? wallet : {};
|
|
133
|
+
return {
|
|
134
|
+
currency: String(record.currency || '').toUpperCase(),
|
|
135
|
+
balance: finiteNumber(record.balance, null),
|
|
136
|
+
};
|
|
137
|
+
})
|
|
138
|
+
.filter((wallet) => wallet.currency && wallet.balance !== null);
|
|
139
|
+
if (normalized.length === 0) return null;
|
|
140
|
+
const cny = normalized.find((wallet) => wallet.currency === 'CNY');
|
|
141
|
+
if (cny) return cny.currency;
|
|
142
|
+
const usd = normalized.find((wallet) => wallet.currency === 'USD');
|
|
143
|
+
if (usd) return usd.currency;
|
|
144
|
+
return normalized[0].currency;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function sumWallets(wallets: unknown, currency: string): number {
|
|
148
|
+
const list = asList(wallets);
|
|
149
|
+
return list
|
|
150
|
+
.filter((wallet) => String((isRecord(wallet) && wallet.currency) || '').toUpperCase() === currency)
|
|
151
|
+
.reduce<number>((sum, wallet) => sum + (finiteNumber(isRecord(wallet) ? wallet.balance : 0, 0) ?? 0), 0);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function pickCostBlock(blocks: unknown, preferredCurrency: string | null): JsonRecord | null {
|
|
155
|
+
const list: JsonRecord[] = asList(blocks).filter(isRecord);
|
|
156
|
+
if (list.length === 0) return null;
|
|
157
|
+
if (preferredCurrency) {
|
|
158
|
+
const exact = list.find((block) => String(block.currency || '').toUpperCase() === preferredCurrency);
|
|
159
|
+
if (exact) return exact;
|
|
160
|
+
}
|
|
161
|
+
const funded = list.find((block) =>
|
|
162
|
+
[...asList(block.series), ...asList(block.function_series)].some((series) => {
|
|
163
|
+
const seriesRecord: JsonRecord = isRecord(series) ? series : {};
|
|
164
|
+
return asList(seriesRecord.buckets).some((bucket) => {
|
|
165
|
+
const bucketRecord: JsonRecord = isRecord(bucket) ? bucket : {};
|
|
166
|
+
return (finiteNumber(costOf(bucketRecord.cost), 0) ?? 0) > 0;
|
|
167
|
+
});
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
return funded || list[0];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function costOf(value: unknown): number {
|
|
174
|
+
if (value && typeof value === 'object') return finiteNumber(isRecord(value) ? value.value : null, 0) ?? 0;
|
|
175
|
+
return finiteNumber(value, 0) ?? 0;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function costBlocks(payload: unknown): JsonRecord[] {
|
|
179
|
+
const root: JsonRecord = isRecord(payload) ? payload : {};
|
|
180
|
+
const data: JsonRecord = isRecord(root.data) ? root.data : {};
|
|
181
|
+
const bizData = data.biz_data;
|
|
182
|
+
if (Array.isArray(bizData)) return bizData.filter(isRecord);
|
|
183
|
+
if (isRecord(bizData) && Array.isArray(bizData.data)) return bizData.data.filter(isRecord);
|
|
184
|
+
if (isRecord(bizData)) return [bizData];
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function localDateKey(date: Date): string {
|
|
189
|
+
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function lastSevenDays(now: Date): UsageDay[] {
|
|
193
|
+
const start = startOfLocalDay(now);
|
|
194
|
+
const days: UsageDay[] = [];
|
|
195
|
+
for (let offset = 6; offset >= 0; offset -= 1) {
|
|
196
|
+
const date = new Date(start);
|
|
197
|
+
date.setDate(date.getDate() - offset);
|
|
198
|
+
days.push({
|
|
199
|
+
date: localDateKey(date),
|
|
200
|
+
label: offset === 0 ? '今天' : `${date.getMonth() + 1}/${date.getDate()}`,
|
|
201
|
+
cost: 0,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
return days;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function parseUsage(
|
|
208
|
+
costPayload: unknown,
|
|
209
|
+
currency: string | null,
|
|
210
|
+
now: Date = new Date(),
|
|
211
|
+
periodStartMs: number | null = null,
|
|
212
|
+
mode: SpendWindow = 'calendar',
|
|
213
|
+
): UsageSnapshot {
|
|
214
|
+
const blocks = costBlocks(costPayload);
|
|
215
|
+
const block = pickCostBlock(blocks, currency);
|
|
216
|
+
const dayStart: number = startOfLocalDay(now).getTime();
|
|
217
|
+
const startMs: number = finiteNumber(periodStartMs, startOfLocalWeek(now).getTime()) ?? startOfLocalWeek(now).getTime();
|
|
218
|
+
const end = new Date(now);
|
|
219
|
+
end.setHours(24, 0, 0, 0);
|
|
220
|
+
const endMs = end.getTime();
|
|
221
|
+
const daily = lastSevenDays(now);
|
|
222
|
+
|
|
223
|
+
if (!block) {
|
|
224
|
+
return {
|
|
225
|
+
state: 'empty',
|
|
226
|
+
currency,
|
|
227
|
+
today: 0,
|
|
228
|
+
spend: 0,
|
|
229
|
+
spendWindow: mode,
|
|
230
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
231
|
+
periodStart: new Date(startMs).toISOString(),
|
|
232
|
+
daily,
|
|
233
|
+
updatedAt: Date.now(),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
const effectiveCurrency = String(block.currency || currency || 'CNY').toUpperCase();
|
|
237
|
+
|
|
238
|
+
let today = 0;
|
|
239
|
+
let spend = 0;
|
|
240
|
+
for (const series of [...asList(block.series), ...asList(block.function_series)]) {
|
|
241
|
+
const seriesRecord: JsonRecord = isRecord(series) ? series : {};
|
|
242
|
+
for (const bucket of asList(seriesRecord.buckets)) {
|
|
243
|
+
const bucketRecord: JsonRecord = isRecord(bucket) ? bucket : {};
|
|
244
|
+
const time: number | null = finiteNumber(bucketRecord.time, null);
|
|
245
|
+
if (time === null) continue;
|
|
246
|
+
const timeMs = time * 1000;
|
|
247
|
+
const cost = costOf(bucketRecord.cost);
|
|
248
|
+
const bucketDate = localDateKey(new Date(timeMs));
|
|
249
|
+
const day = daily.find((entry) => entry.date === bucketDate);
|
|
250
|
+
if (day) day.cost += cost;
|
|
251
|
+
if (timeMs < startMs || timeMs >= endMs) continue;
|
|
252
|
+
spend += cost;
|
|
253
|
+
if (timeMs >= dayStart) today += cost;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
state: 'ready',
|
|
258
|
+
currency: effectiveCurrency,
|
|
259
|
+
today,
|
|
260
|
+
spend,
|
|
261
|
+
spendWindow: mode,
|
|
262
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
263
|
+
periodStart: new Date(startMs).toISOString(),
|
|
264
|
+
daily,
|
|
265
|
+
updatedAt: Date.now(),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function createAccountService(ctx: DshContext, options: AccountOptions = {}): {
|
|
270
|
+
getAccount(force?: boolean): Promise<AccountSnapshot>;
|
|
271
|
+
readBalance(force?: boolean): Promise<BalanceState>;
|
|
272
|
+
readUsage(force?: boolean): Promise<UsageSnapshot>;
|
|
273
|
+
} {
|
|
274
|
+
const locale = options.locale || 'zh';
|
|
275
|
+
const getSettings: () => AccountSettings =
|
|
276
|
+
options.getSettings || (() => ({ balance: { spendWindow: 'calendar' } }));
|
|
277
|
+
const resolveAccount = (): DeepseekAccountService | undefined =>
|
|
278
|
+
ctx.get?.('deepseekAccount') as DeepseekAccountService | undefined;
|
|
279
|
+
let balanceCache: BalanceState | null = null;
|
|
280
|
+
let balanceAt = 0;
|
|
281
|
+
let usageCache: UsageSnapshot | null = null;
|
|
282
|
+
let usageAt = 0;
|
|
283
|
+
|
|
284
|
+
async function readBalance(force = false): Promise<BalanceState> {
|
|
285
|
+
const account = resolveAccount();
|
|
286
|
+
if (!account) return { signedIn: false, state: 'unavailable', error: 'Account service is not mounted' };
|
|
287
|
+
const now = Date.now();
|
|
288
|
+
const ttl = balanceCache?.state === 'ready' ? BALANCE_TTL_MS : BALANCE_ERROR_TTL_MS;
|
|
289
|
+
if (!force && balanceCache && now - balanceAt < ttl) return { ...balanceCache, cached: true };
|
|
290
|
+
|
|
291
|
+
let state: unknown;
|
|
292
|
+
try {
|
|
293
|
+
state = await account.getState();
|
|
294
|
+
} catch (error) {
|
|
295
|
+
balanceCache = { signedIn: false, state: 'error', error: serializeError(error) };
|
|
296
|
+
balanceAt = now;
|
|
297
|
+
return balanceCache;
|
|
298
|
+
}
|
|
299
|
+
const stateRecord: JsonRecord | null = isRecord(state) ? state : null;
|
|
300
|
+
if (!state || stateRecord?.status === 'signed-out') {
|
|
301
|
+
balanceCache = {
|
|
302
|
+
signedIn: false,
|
|
303
|
+
state: 'signed-out',
|
|
304
|
+
currency: null,
|
|
305
|
+
balance: null,
|
|
306
|
+
paid: null,
|
|
307
|
+
bonus: null,
|
|
308
|
+
updatedAt: now,
|
|
309
|
+
};
|
|
310
|
+
balanceAt = now;
|
|
311
|
+
return balanceCache;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
try {
|
|
315
|
+
const details = await timeout(account.getBalance(clientMetadata(locale)), REQUEST_TIMEOUT_MS, 'Balance');
|
|
316
|
+
const detailsRecord: JsonRecord | null = isRecord(details) ? details : null;
|
|
317
|
+
if (!detailsRecord || detailsRecord.status !== 'ready') {
|
|
318
|
+
balanceCache = {
|
|
319
|
+
signedIn: true,
|
|
320
|
+
state: 'failed',
|
|
321
|
+
currency: null,
|
|
322
|
+
balance: null,
|
|
323
|
+
paid: null,
|
|
324
|
+
bonus: null,
|
|
325
|
+
error: 'Balance request failed',
|
|
326
|
+
updatedAt: now,
|
|
327
|
+
};
|
|
328
|
+
balanceAt = now;
|
|
329
|
+
return balanceCache;
|
|
330
|
+
}
|
|
331
|
+
const currency = currencyOf(detailsRecord.value) || currencyOf(detailsRecord.bonusWallets) || 'CNY';
|
|
332
|
+
const paid = sumWallets(detailsRecord.value, currency);
|
|
333
|
+
const bonus = sumWallets(detailsRecord.bonusWallets, currency);
|
|
334
|
+
balanceCache = {
|
|
335
|
+
signedIn: true,
|
|
336
|
+
state: 'ready',
|
|
337
|
+
currency,
|
|
338
|
+
balance: paid + bonus,
|
|
339
|
+
paid,
|
|
340
|
+
bonus,
|
|
341
|
+
updatedAt: now,
|
|
342
|
+
};
|
|
343
|
+
} catch (error) {
|
|
344
|
+
balanceCache = {
|
|
345
|
+
signedIn: true,
|
|
346
|
+
state: 'failed',
|
|
347
|
+
currency: null,
|
|
348
|
+
balance: null,
|
|
349
|
+
paid: null,
|
|
350
|
+
bonus: null,
|
|
351
|
+
error: serializeError(error),
|
|
352
|
+
updatedAt: now,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
balanceAt = now;
|
|
356
|
+
return balanceCache;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
async function readUsage(force = false): Promise<UsageSnapshot> {
|
|
360
|
+
const account = resolveAccount();
|
|
361
|
+
const mode: SpendWindow = getSettings()?.balance?.spendWindow === 'rolling' ? 'rolling' : 'calendar';
|
|
362
|
+
if (!account?.getPlatformSession) {
|
|
363
|
+
return {
|
|
364
|
+
state: 'unavailable',
|
|
365
|
+
currency: null,
|
|
366
|
+
today: null,
|
|
367
|
+
spend: null,
|
|
368
|
+
week: null,
|
|
369
|
+
spendWindow: mode,
|
|
370
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
371
|
+
error: 'Usage is unavailable',
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
const now = Date.now();
|
|
375
|
+
const ttl = usageCache?.state === 'ready' ? USAGE_TTL_MS : USAGE_ERROR_TTL_MS;
|
|
376
|
+
if (!force && usageCache && usageCache.spendWindow === mode && now - usageAt < ttl) {
|
|
377
|
+
return { ...usageCache, cached: true };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
try {
|
|
381
|
+
const platform = await timeout(account.getPlatformSession(), 4000, 'Platform session');
|
|
382
|
+
const origin = isRecord(platform) && typeof platform.origin === 'string' ? platform.origin : '';
|
|
383
|
+
const token = isRecord(platform) && typeof platform.token === 'string' ? platform.token : '';
|
|
384
|
+
if (!origin || !token) {
|
|
385
|
+
usageCache = {
|
|
386
|
+
state: 'signed-out',
|
|
387
|
+
currency: null,
|
|
388
|
+
today: null,
|
|
389
|
+
spend: null,
|
|
390
|
+
week: null,
|
|
391
|
+
spendWindow: mode,
|
|
392
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
393
|
+
updatedAt: now,
|
|
394
|
+
};
|
|
395
|
+
usageAt = now;
|
|
396
|
+
return usageCache;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const current = new Date();
|
|
400
|
+
const todayStart = startOfLocalDay(current);
|
|
401
|
+
const periodStart =
|
|
402
|
+
mode === 'rolling'
|
|
403
|
+
? new Date(todayStart.getTime() - 6 * 24 * 60 * 60 * 1000)
|
|
404
|
+
: startOfLocalWeek(current);
|
|
405
|
+
const tomorrow = new Date(current);
|
|
406
|
+
tomorrow.setHours(24, 0, 0, 0);
|
|
407
|
+
const tz = localTimezoneOffsetSeconds(current);
|
|
408
|
+
const url = new URL('/api/v0/usage/by_api_key/cost', origin);
|
|
409
|
+
url.searchParams.set('start', String(Math.floor(periodStart.getTime() / 1000)));
|
|
410
|
+
url.searchParams.set('end', String(Math.floor(tomorrow.getTime() / 1000)));
|
|
411
|
+
url.searchParams.set('tz', String(tz));
|
|
412
|
+
|
|
413
|
+
const controller = new AbortController();
|
|
414
|
+
const timer = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
|
|
415
|
+
let response: Response;
|
|
416
|
+
try {
|
|
417
|
+
response = await fetch(url, {
|
|
418
|
+
signal: controller.signal,
|
|
419
|
+
headers: {
|
|
420
|
+
authorization: `Bearer ${token}`,
|
|
421
|
+
'x-dsh-auth-token': token,
|
|
422
|
+
accept: 'application/json',
|
|
423
|
+
'x-client-platform': 'web',
|
|
424
|
+
'x-client-locale': locale === 'zh' ? 'zh_CN' : 'en_US',
|
|
425
|
+
'x-client-version': VERSION,
|
|
426
|
+
},
|
|
427
|
+
});
|
|
428
|
+
} finally {
|
|
429
|
+
clearTimeout(timer);
|
|
430
|
+
}
|
|
431
|
+
if (response.status === 401 || response.status === 403) {
|
|
432
|
+
usageCache = {
|
|
433
|
+
state: 'expired',
|
|
434
|
+
currency: null,
|
|
435
|
+
today: null,
|
|
436
|
+
spend: null,
|
|
437
|
+
week: null,
|
|
438
|
+
spendWindow: mode,
|
|
439
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
440
|
+
error: `HTTP ${response.status}`,
|
|
441
|
+
updatedAt: now,
|
|
442
|
+
};
|
|
443
|
+
usageAt = now;
|
|
444
|
+
return usageCache;
|
|
445
|
+
}
|
|
446
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
447
|
+
const payload: unknown = await response.json();
|
|
448
|
+
const payloadRecord: JsonRecord = isRecord(payload) ? payload : {};
|
|
449
|
+
const data: JsonRecord = isRecord(payloadRecord.data) ? payloadRecord.data : {};
|
|
450
|
+
const code: number = finiteNumber(payloadRecord.code, 0);
|
|
451
|
+
const bizCode: number = finiteNumber(data.biz_code, 0);
|
|
452
|
+
if (code !== 0 || bizCode !== 0) {
|
|
453
|
+
if (code === 40002 || code === 40003 || bizCode === 40002 || bizCode === 40003) {
|
|
454
|
+
usageCache = {
|
|
455
|
+
state: 'expired',
|
|
456
|
+
currency: null,
|
|
457
|
+
today: null,
|
|
458
|
+
spend: null,
|
|
459
|
+
week: null,
|
|
460
|
+
spendWindow: mode,
|
|
461
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
462
|
+
error: `code ${code || bizCode}`,
|
|
463
|
+
updatedAt: now,
|
|
464
|
+
};
|
|
465
|
+
usageAt = now;
|
|
466
|
+
return usageCache;
|
|
467
|
+
}
|
|
468
|
+
throw new Error(`Platform code ${code || bizCode}`);
|
|
469
|
+
}
|
|
470
|
+
usageCache = parseUsage(payload, balanceCache?.currency || null, current, periodStart.getTime(), mode);
|
|
471
|
+
usageCache.week = usageCache.spend;
|
|
472
|
+
usageAt = now;
|
|
473
|
+
return usageCache;
|
|
474
|
+
} catch (error) {
|
|
475
|
+
usageCache = {
|
|
476
|
+
state: 'error',
|
|
477
|
+
currency: balanceCache?.currency || null,
|
|
478
|
+
today: null,
|
|
479
|
+
spend: null,
|
|
480
|
+
week: null,
|
|
481
|
+
spendWindow: mode,
|
|
482
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
483
|
+
error: serializeError(error),
|
|
484
|
+
updatedAt: now,
|
|
485
|
+
};
|
|
486
|
+
usageAt = now;
|
|
487
|
+
return usageCache;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async function getAccount(force = false): Promise<AccountSnapshot> {
|
|
492
|
+
const balance = await readBalance(force);
|
|
493
|
+
if (!balance.signedIn) {
|
|
494
|
+
const mode: SpendWindow = getSettings()?.balance?.spendWindow === 'rolling' ? 'rolling' : 'calendar';
|
|
495
|
+
return {
|
|
496
|
+
available: true,
|
|
497
|
+
signedIn: false,
|
|
498
|
+
balance,
|
|
499
|
+
usage: {
|
|
500
|
+
state: balance.state,
|
|
501
|
+
currency: null,
|
|
502
|
+
today: null,
|
|
503
|
+
spend: null,
|
|
504
|
+
week: null,
|
|
505
|
+
spendWindow: mode,
|
|
506
|
+
spendLabel: mode === 'rolling' ? '最近 7 天' : '本周',
|
|
507
|
+
},
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
const usage = await readUsage(force);
|
|
511
|
+
return {
|
|
512
|
+
available: true,
|
|
513
|
+
signedIn: true,
|
|
514
|
+
balance,
|
|
515
|
+
usage,
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
return { getAccount, readBalance, readUsage };
|
|
520
|
+
}
|