hostlet 0.0.0-stage → 0.1.0-alpha.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/LICENSE +21 -0
- package/README.md +233 -2
- package/bin/hostlet.js +61 -0
- package/console/app/api.js +1223 -0
- package/console/app/boot.js +165 -0
- package/console/app/cards.js +222 -0
- package/console/app/compose-quote.js +127 -0
- package/console/app/config-form.js +727 -0
- package/console/app/document-drafts.js +62 -0
- package/console/app/document-tools.mjs +67 -0
- package/console/app/documents.js +106 -0
- package/console/app/pages.js +6483 -0
- package/console/app/plugins.js +509 -0
- package/console/app/preview-tools.js +172 -0
- package/console/app/reference-labels.js +118 -0
- package/console/app/selection.js +372 -0
- package/console/app/store.js +57 -0
- package/console/app/text-anchor.js +26 -0
- package/console/app/topic-local.js +322 -0
- package/console/app/topic-nav.js +414 -0
- package/console/app/topic-status.js +109 -0
- package/console/app/views.js +43 -0
- package/console/apple-touch-icon.png +0 -0
- package/console/avatars/agy.svg +34 -0
- package/console/avatars/alhena.svg +36 -0
- package/console/avatars/castor.svg +38 -0
- package/console/avatars/claude.svg +34 -0
- package/console/avatars/codex.svg +34 -0
- package/console/avatars/devin.svg +34 -0
- package/console/avatars/echo.svg +34 -0
- package/console/avatars/kimi.svg +33 -0
- package/console/avatars/opencode.svg +33 -0
- package/console/avatars/pinion.svg +34 -0
- package/console/avatars/sprocket.svg +34 -0
- package/console/avatars/vela.svg +44 -0
- package/console/avatars/wasat.svg +38 -0
- package/console/clients/agy.svg +1 -0
- package/console/clients/claude.svg +1 -0
- package/console/clients/codex.svg +1 -0
- package/console/clients/devin.svg +1 -0
- package/console/clients/kimi.svg +1 -0
- package/console/clients/opencode.svg +1 -0
- package/console/favicon.ico +0 -0
- package/console/favicon.svg +16 -0
- package/console/index.html +99 -0
- package/console/pdl/engine/README.md +13 -0
- package/console/pdl/engine/anchor.js +195 -0
- package/console/pdl/engine/core.js +1340 -0
- package/console/pdl/engine/frame.js +1252 -0
- package/console/pdl/engine/i18n.js +67 -0
- package/console/pdl/engine/pdl-check.js +102 -0
- package/console/pdl/engine/pdl-doc.js +353 -0
- package/console/pdl/engine/pdl-view.js +280 -0
- package/console/pdl/engine/pdl.js +1064 -0
- package/console/pdl/engine/platform.js +18 -0
- package/console/pdl/engine/proj.js +279 -0
- package/console/pdl/engine/theme.js +176 -0
- package/console/pdl/engine/tree.js +171 -0
- package/console/pdl/engine/units.js +546 -0
- package/console/pdl/library/README.md +28 -0
- package/console/pdl/library/blocks.js +1512 -0
- package/console/pdl/library/buttons.js +86 -0
- package/console/pdl/library/capsules.js +130 -0
- package/console/pdl/library/charts.js +287 -0
- package/console/pdl/library/code.js +98 -0
- package/console/pdl/library/css/base.css +12 -0
- package/console/pdl/library/css/blocks.css +98 -0
- package/console/pdl/library/css/capsules.css +42 -0
- package/console/pdl/library/css/catalog-page.css +24 -0
- package/console/pdl/library/css/chat.css +100 -0
- package/console/pdl/library/css/composer-top.css +145 -0
- package/console/pdl/library/css/controls.css +195 -0
- package/console/pdl/library/css/drag.css +29 -0
- package/console/pdl/library/css/edit-layout.css +93 -0
- package/console/pdl/library/css/floating-window.css +30 -0
- package/console/pdl/library/css/hover.css +50 -0
- package/console/pdl/library/css/label-search.css +37 -0
- package/console/pdl/library/css/layers.css +43 -0
- package/console/pdl/library/css/library-parts.css +33 -0
- package/console/pdl/library/css/library.css +117 -0
- package/console/pdl/library/css/member-select.css +34 -0
- package/console/pdl/library/css/message.css +72 -0
- package/console/pdl/library/css/page.css +104 -0
- package/console/pdl/library/css/parts.css +338 -0
- package/console/pdl/library/css/responsive.css +40 -0
- package/console/pdl/library/css/roles.css +28 -0
- package/console/pdl/library/css/rows.css +231 -0
- package/console/pdl/library/css/settings-rows.css +37 -0
- package/console/pdl/library/css/shell.css +59 -0
- package/console/pdl/library/css/stream.css +296 -0
- package/console/pdl/library/css/theme-calculator.css +18 -0
- package/console/pdl/library/css/thumb-choice.css +69 -0
- package/console/pdl/library/css/tweaks.css +16 -0
- package/console/pdl/library/css/widgets.css +211 -0
- package/console/pdl/library/css/workbench.css +59 -0
- package/console/pdl/library/feedback.js +83 -0
- package/console/pdl/library/floating-window.mjs +100 -0
- package/console/pdl/library/fold.js +238 -0
- package/console/pdl/library/format.js +53 -0
- package/console/pdl/library/frames.js +123 -0
- package/console/pdl/library/icons.js +165 -0
- package/console/pdl/library/identity.js +149 -0
- package/console/pdl/library/inputs.js +801 -0
- package/console/pdl/library/label-search.js +214 -0
- package/console/pdl/library/markdown-text.js +115 -0
- package/console/pdl/library/markdown.js +150 -0
- package/console/pdl/library/media-viewer.js +293 -0
- package/console/pdl/library/member-select.mjs +32 -0
- package/console/pdl/library/message.js +105 -0
- package/console/pdl/library/motion.js +56 -0
- package/console/pdl/library/overlays.js +165 -0
- package/console/pdl/library/page.js +142 -0
- package/console/pdl/library/plugin-form.js +154 -0
- package/console/pdl/library/registry.js +198 -0
- package/console/pdl/library/rows.js +523 -0
- package/console/pdl/library/shell.js +151 -0
- package/console/pdl/library/stream.js +58 -0
- package/console/pdl/library/tabs.js +259 -0
- package/console/pdl/library/theme-calculator.js +300 -0
- package/console/pdl/library/widgets.js +114 -0
- package/console/pdl/load-order.json +74 -0
- package/console/pdl/platform/README.md +3 -0
- package/console/pdl/platform/rich-markdown.mjs +17 -0
- package/console/pdl/platform/web-links.js +26 -0
- package/console/pdl/platform/web.js +80 -0
- package/console/sdk-browser/mentions.d.mts +16 -0
- package/console/sdk-browser/mentions.mjs +84 -0
- package/console/theme/assets.js +21 -0
- package/console/theme/theme.json +9417 -0
- package/console/theme/tokens.json +444 -0
- package/dist/core/a2a.d.ts +288 -0
- package/dist/core/a2a.js +2197 -0
- package/dist/core/address.d.ts +34 -0
- package/dist/core/address.js +33 -0
- package/dist/core/approval-inputs.d.ts +17 -0
- package/dist/core/approval-inputs.js +70 -0
- package/dist/core/archive.d.ts +19 -0
- package/dist/core/archive.js +135 -0
- package/dist/core/attachments.d.ts +17 -0
- package/dist/core/attachments.js +143 -0
- package/dist/core/blocked-handoffs.d.ts +132 -0
- package/dist/core/blocked-handoffs.js +349 -0
- package/dist/core/config-schema.d.ts +124 -0
- package/dist/core/config-schema.js +192 -0
- package/dist/core/config.d.ts +117 -0
- package/dist/core/config.js +638 -0
- package/dist/core/disk-usage.d.ts +26 -0
- package/dist/core/disk-usage.js +83 -0
- package/dist/core/documents.d.ts +9 -0
- package/dist/core/documents.js +48 -0
- package/dist/core/entry.d.ts +316 -0
- package/dist/core/entry.js +711 -0
- package/dist/core/home.d.ts +76 -0
- package/dist/core/home.js +449 -0
- package/dist/core/i18n.d.ts +36 -0
- package/dist/core/i18n.js +458 -0
- package/dist/core/instances.d.ts +224 -0
- package/dist/core/instances.js +1272 -0
- package/dist/core/members.d.ts +190 -0
- package/dist/core/members.js +358 -0
- package/dist/core/mentions.d.ts +12 -0
- package/dist/core/mentions.js +49 -0
- package/dist/core/messages.d.ts +876 -0
- package/dist/core/messages.js +916 -0
- package/dist/core/notices.d.ts +88 -0
- package/dist/core/notices.js +54 -0
- package/dist/core/ops.d.ts +117 -0
- package/dist/core/ops.js +146 -0
- package/dist/core/package.d.ts +2 -0
- package/dist/core/package.js +14 -0
- package/dist/core/parts.d.ts +25 -0
- package/dist/core/parts.js +119 -0
- package/dist/core/plugins/catalog.d.ts +14 -0
- package/dist/core/plugins/catalog.js +54 -0
- package/dist/core/plugins/external.d.ts +7 -0
- package/dist/core/plugins/external.js +921 -0
- package/dist/core/plugins/index.d.ts +5 -0
- package/dist/core/plugins/index.js +51 -0
- package/dist/core/plugins/npm.d.ts +12 -0
- package/dist/core/plugins/npm.js +99 -0
- package/dist/core/plugins/plugin-manifest.d.ts +12 -0
- package/dist/core/plugins/plugin-manifest.js +231 -0
- package/dist/core/push.d.ts +22 -0
- package/dist/core/push.js +63 -0
- package/dist/core/resource.d.ts +77 -0
- package/dist/core/resource.js +48 -0
- package/dist/core/spawn.d.ts +12 -0
- package/dist/core/spawn.js +30 -0
- package/dist/core/storage-inventory.d.ts +118 -0
- package/dist/core/storage-inventory.js +279 -0
- package/dist/core/storage.d.ts +45 -0
- package/dist/core/storage.js +602 -0
- package/dist/core/store.d.ts +15 -0
- package/dist/core/store.js +33 -0
- package/dist/core/topic-index.d.ts +19 -0
- package/dist/core/topic-index.js +70 -0
- package/dist/core/topic-ops.d.ts +139 -0
- package/dist/core/topic-ops.js +507 -0
- package/dist/core/topic-status.d.ts +32 -0
- package/dist/core/topic-status.js +105 -0
- package/dist/core/topics.d.ts +327 -0
- package/dist/core/topics.js +786 -0
- package/dist/core/turn-clients.d.ts +1 -0
- package/dist/core/turn-clients.js +5 -0
- package/dist/core/types.d.ts +2866 -0
- package/dist/core/types.js +166 -0
- package/dist/core/util.d.ts +10 -0
- package/dist/core/util.js +47 -0
- package/dist/main.d.ts +12 -0
- package/dist/main.js +82 -0
- package/dist/plugins/adapter-acp/index.d.ts +3 -0
- package/dist/plugins/adapter-acp/index.js +57 -0
- package/dist/plugins/adapter-acp/plugin.yaml +100 -0
- package/dist/plugins/adapter-agy/command.d.ts +9 -0
- package/dist/plugins/adapter-agy/command.js +34 -0
- package/dist/plugins/adapter-agy/index.d.ts +5 -0
- package/dist/plugins/adapter-agy/index.js +65 -0
- package/dist/plugins/adapter-agy/quota.d.ts +2 -0
- package/dist/plugins/adapter-agy/quota.js +155 -0
- package/dist/plugins/adapter-agy/terminal.d.ts +6 -0
- package/dist/plugins/adapter-agy/terminal.js +132 -0
- package/dist/plugins/adapter-agy/version.d.ts +9 -0
- package/dist/plugins/adapter-agy/version.js +17 -0
- package/dist/plugins/adapter-claude/index.d.ts +43 -0
- package/dist/plugins/adapter-claude/index.js +412 -0
- package/dist/plugins/adapter-claude/quota.d.ts +14 -0
- package/dist/plugins/adapter-claude/quota.js +103 -0
- package/dist/plugins/adapter-codex/index.d.ts +4 -0
- package/dist/plugins/adapter-codex/index.js +688 -0
- package/dist/plugins/adapter-codex/responses-model.d.ts +9 -0
- package/dist/plugins/adapter-codex/responses-model.js +82 -0
- package/dist/plugins/adapter-kimi/index.d.ts +4 -0
- package/dist/plugins/adapter-kimi/index.js +51 -0
- package/dist/plugins/adapter-kimi/quota.d.ts +3 -0
- package/dist/plugins/adapter-kimi/quota.js +171 -0
- package/dist/plugins/adapter-opencode/index.d.ts +12 -0
- package/dist/plugins/adapter-opencode/index.js +64 -0
- package/dist/plugins/annotations/console/cards.css +189 -0
- package/dist/plugins/annotations/console/cards.mjs +439 -0
- package/dist/plugins/annotations/console/pdl.json +52 -0
- package/dist/plugins/annotations/console/view.mjs +241 -0
- package/dist/plugins/annotations/console/window.mjs +219 -0
- package/dist/plugins/annotations/followups.d.ts +51 -0
- package/dist/plugins/annotations/followups.js +112 -0
- package/dist/plugins/annotations/index.d.ts +3 -0
- package/dist/plugins/annotations/index.js +298 -0
- package/dist/plugins/annotations/plugin.yaml +3 -0
- package/dist/plugins/ask-choice/index.d.ts +9 -0
- package/dist/plugins/ask-choice/index.js +24 -0
- package/dist/plugins/ask-choice/plugin.yaml +2 -0
- package/dist/plugins/background/index.d.ts +2 -0
- package/dist/plugins/background/index.js +398 -0
- package/dist/plugins/background/plugin.yaml +7 -0
- package/dist/plugins/builtin.json +37 -0
- package/dist/plugins/cards/console/pdl.json +516 -0
- package/dist/plugins/cards/console/view.mjs +67 -0
- package/dist/plugins/cards/index.d.ts +8 -0
- package/dist/plugins/cards/index.js +627 -0
- package/dist/plugins/cards/plugin.yaml +3 -0
- package/dist/plugins/console/browser.d.ts +2 -0
- package/dist/plugins/console/browser.js +115 -0
- package/dist/plugins/console/console/pdl.json +38 -0
- package/dist/plugins/console/console/view.mjs +26 -0
- package/dist/plugins/console/index.d.ts +4 -0
- package/dist/plugins/console/index.js +94 -0
- package/dist/plugins/console/picker.d.ts +2 -0
- package/dist/plugins/console/picker.js +48 -0
- package/dist/plugins/console/plugin.yaml +3 -0
- package/dist/plugins/console/topic-view.d.ts +3 -0
- package/dist/plugins/console/topic-view.js +388 -0
- package/dist/plugins/handoff/index.d.ts +2 -0
- package/dist/plugins/handoff/index.js +147 -0
- package/dist/plugins/handoff/plugin.yaml +8 -0
- package/dist/plugins/host-api/index.d.ts +7 -0
- package/dist/plugins/host-api/index.js +310 -0
- package/dist/plugins/host-api/plugin.yaml +2 -0
- package/dist/plugins/host-api/signing.d.ts +63 -0
- package/dist/plugins/host-api/signing.js +427 -0
- package/dist/plugins/host-api/skills/host-ops/SKILL.md +28 -0
- package/dist/plugins/host-api/skills/hostlet-plugin-dev/SKILL.md +38 -0
- package/dist/plugins/inbox/index.d.ts +15 -0
- package/dist/plugins/inbox/index.js +161 -0
- package/dist/plugins/inbox/plugin.yaml +2 -0
- package/dist/plugins/keys/account-directories.d.ts +18 -0
- package/dist/plugins/keys/account-directories.js +86 -0
- package/dist/plugins/keys/account-storage.d.ts +18 -0
- package/dist/plugins/keys/account-storage.js +112 -0
- package/dist/plugins/keys/console/pdl.json +710 -0
- package/dist/plugins/keys/console/view.mjs +619 -0
- package/dist/plugins/keys/index.d.ts +7 -0
- package/dist/plugins/keys/index.js +842 -0
- package/dist/plugins/keys/login-link.d.ts +2 -0
- package/dist/plugins/keys/login-link.js +15 -0
- package/dist/plugins/keys/plugin.yaml +3 -0
- package/dist/plugins/keys/quota-history.d.ts +91 -0
- package/dist/plugins/keys/quota-history.js +420 -0
- package/dist/plugins/mcp/console/pdl.json +412 -0
- package/dist/plugins/mcp/console/view.mjs +207 -0
- package/dist/plugins/mcp/index.d.ts +7 -0
- package/dist/plugins/mcp/index.js +494 -0
- package/dist/plugins/mcp/plugin.yaml +3 -0
- package/dist/plugins/member-health/index.d.ts +2 -0
- package/dist/plugins/member-health/index.js +478 -0
- package/dist/plugins/member-health/plugin.yaml +2 -0
- package/dist/plugins/memory/capabilities.d.ts +10 -0
- package/dist/plugins/memory/capabilities.js +70 -0
- package/dist/plugins/memory/console/pdl.json +459 -0
- package/dist/plugins/memory/console/view.mjs +254 -0
- package/dist/plugins/memory/corpus.d.ts +60 -0
- package/dist/plugins/memory/corpus.js +521 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.d.ts +8 -0
- package/dist/plugins/memory/engines/fts-tree-diversity.js +49 -0
- package/dist/plugins/memory/engines/fts-tree-text.d.ts +12 -0
- package/dist/plugins/memory/engines/fts-tree-text.js +93 -0
- package/dist/plugins/memory/engines/fts-tree.d.ts +4 -0
- package/dist/plugins/memory/engines/fts-tree.js +387 -0
- package/dist/plugins/memory/engines/search.d.ts +9 -0
- package/dist/plugins/memory/engines/search.js +35 -0
- package/dist/plugins/memory/index.d.ts +3 -0
- package/dist/plugins/memory/index.js +465 -0
- package/dist/plugins/memory/notes.d.ts +22 -0
- package/dist/plugins/memory/notes.js +140 -0
- package/dist/plugins/memory/plugin.yaml +23 -0
- package/dist/plugins/model-profiles/index.d.ts +2 -0
- package/dist/plugins/model-profiles/index.js +916 -0
- package/dist/plugins/model-profiles/plugin.yaml +13 -0
- package/dist/plugins/model-profiles/review-source.d.ts +11 -0
- package/dist/plugins/model-profiles/review-source.js +640 -0
- package/dist/plugins/model-profiles/rules.d.ts +130 -0
- package/dist/plugins/model-profiles/rules.js +729 -0
- package/dist/plugins/model-profiles/skills/model-profile/SKILL.md +59 -0
- package/dist/plugins/model-profiles/sources.d.ts +61 -0
- package/dist/plugins/model-profiles/sources.js +375 -0
- package/dist/plugins/model-profiles/storage.d.ts +84 -0
- package/dist/plugins/model-profiles/storage.js +315 -0
- package/dist/plugins/model-profiles/types.d.ts +179 -0
- package/dist/plugins/model-profiles/types.js +12 -0
- package/dist/plugins/model-profiles/wakeup.d.ts +29 -0
- package/dist/plugins/model-profiles/wakeup.js +390 -0
- package/dist/plugins/models/index.d.ts +2 -0
- package/dist/plugins/models/index.js +63 -0
- package/dist/plugins/models/plugin.yaml +8 -0
- package/dist/plugins/ops/collector.d.ts +22 -0
- package/dist/plugins/ops/collector.js +48 -0
- package/dist/plugins/ops/config.d.ts +11 -0
- package/dist/plugins/ops/config.js +13 -0
- package/dist/plugins/ops/console/pdl.json +676 -0
- package/dist/plugins/ops/console/view.mjs +700 -0
- package/dist/plugins/ops/index.d.ts +3 -0
- package/dist/plugins/ops/index.js +20 -0
- package/dist/plugins/ops/journal.d.ts +7 -0
- package/dist/plugins/ops/journal.js +109 -0
- package/dist/plugins/ops/metrics.d.ts +3 -0
- package/dist/plugins/ops/metrics.js +81 -0
- package/dist/plugins/ops/plugin.yaml +63 -0
- package/dist/plugins/ops/samples.d.ts +54 -0
- package/dist/plugins/ops/samples.js +100 -0
- package/dist/plugins/ops/trace.d.ts +3 -0
- package/dist/plugins/ops/trace.js +104 -0
- package/dist/plugins/ops/usage.d.ts +2 -0
- package/dist/plugins/ops/usage.js +152 -0
- package/dist/plugins/plans/acceptance.d.ts +284 -0
- package/dist/plugins/plans/acceptance.js +247 -0
- package/dist/plugins/plans/blocked.d.ts +25 -0
- package/dist/plugins/plans/blocked.js +66 -0
- package/dist/plugins/plans/console/pdl.json +639 -0
- package/dist/plugins/plans/console/view.d.mts +2 -0
- package/dist/plugins/plans/console/view.mjs +855 -0
- package/dist/plugins/plans/dependencies.d.ts +5 -0
- package/dist/plugins/plans/dependencies.js +21 -0
- package/dist/plugins/plans/index.d.ts +72 -0
- package/dist/plugins/plans/index.js +2057 -0
- package/dist/plugins/plans/plugin.yaml +44 -0
- package/dist/plugins/plans/progress.d.ts +82 -0
- package/dist/plugins/plans/progress.js +516 -0
- package/dist/plugins/prompt/index.d.ts +8 -0
- package/dist/plugins/prompt/index.js +361 -0
- package/dist/plugins/prompt/plugin.yaml +2 -0
- package/dist/plugins/recovery/console/pdl.json +50 -0
- package/dist/plugins/recovery/console/view.mjs +46 -0
- package/dist/plugins/recovery/index.d.ts +9 -0
- package/dist/plugins/recovery/index.js +454 -0
- package/dist/plugins/recovery/plugin.yaml +9 -0
- package/dist/plugins/recovery/probe.d.ts +11 -0
- package/dist/plugins/recovery/probe.js +128 -0
- package/dist/plugins/restart/console/pdl.json +112 -0
- package/dist/plugins/restart/console/view.mjs +85 -0
- package/dist/plugins/restart/index.d.ts +10 -0
- package/dist/plugins/restart/index.js +669 -0
- package/dist/plugins/restart/plugin.yaml +9 -0
- package/dist/plugins/restart/scope.d.ts +6 -0
- package/dist/plugins/restart/scope.js +44 -0
- package/dist/plugins/schedule/console/pdl.json +696 -0
- package/dist/plugins/schedule/console/view.mjs +398 -0
- package/dist/plugins/schedule/events.d.ts +76 -0
- package/dist/plugins/schedule/events.js +158 -0
- package/dist/plugins/schedule/index.d.ts +32 -0
- package/dist/plugins/schedule/index.js +1199 -0
- package/dist/plugins/schedule/plugin.yaml +26 -0
- package/dist/plugins/schedule/when.d.ts +14 -0
- package/dist/plugins/schedule/when.js +83 -0
- package/dist/plugins/sessions/console/pdl.json +259 -0
- package/dist/plugins/sessions/console/view.mjs +355 -0
- package/dist/plugins/sessions/index.d.ts +3 -0
- package/dist/plugins/sessions/index.js +126 -0
- package/dist/plugins/sessions/participants.d.ts +28 -0
- package/dist/plugins/sessions/participants.js +83 -0
- package/dist/plugins/sessions/plugin.yaml +3 -0
- package/dist/plugins/signals/console/pdl.json +802 -0
- package/dist/plugins/signals/console/view.mjs +284 -0
- package/dist/plugins/signals/content.d.ts +31 -0
- package/dist/plugins/signals/content.js +151 -0
- package/dist/plugins/signals/index.d.ts +2 -0
- package/dist/plugins/signals/index.js +601 -0
- package/dist/plugins/signals/plugin.yaml +32 -0
- package/dist/plugins/signals/skills/signal-sources/SKILL.md +27 -0
- package/dist/plugins/skills/console/pdl.json +254 -0
- package/dist/plugins/skills/console/view.mjs +204 -0
- package/dist/plugins/skills/index.d.ts +11 -0
- package/dist/plugins/skills/index.js +567 -0
- package/dist/plugins/skills/install.d.ts +69 -0
- package/dist/plugins/skills/install.js +265 -0
- package/dist/plugins/skills/plugin.yaml +24 -0
- package/dist/plugins/skills/safety-scan.d.ts +17 -0
- package/dist/plugins/skills/safety-scan.js +78 -0
- package/dist/plugins/skills/skills/skill-ops/SKILL.md +29 -0
- package/dist/plugins/terminal/console/pdl.json +115 -0
- package/dist/plugins/terminal/console/view.mjs +178 -0
- package/dist/plugins/terminal/index.d.ts +3 -0
- package/dist/plugins/terminal/index.js +204 -0
- package/dist/plugins/terminal/plugin.yaml +2 -0
- package/dist/plugins/tools/index.d.ts +3 -0
- package/dist/plugins/tools/index.js +455 -0
- package/dist/plugins/tools/plugin.yaml +2 -0
- package/dist/plugins/tools/token.d.ts +61 -0
- package/dist/plugins/tools/token.js +179 -0
- package/dist/plugins/topic-tools/index.d.ts +9 -0
- package/dist/plugins/topic-tools/index.js +29 -0
- package/dist/plugins/topic-tools/plugin.yaml +2 -0
- package/dist/plugins/topic-tools/skills/topic-tidy/SKILL.md +108 -0
- package/dist/plugins/works/console/pdl.json +120 -0
- package/dist/plugins/works/console/view.mjs +91 -0
- package/dist/plugins/works/document-words.d.ts +50 -0
- package/dist/plugins/works/document-words.js +27 -0
- package/dist/plugins/works/editors.d.ts +3 -0
- package/dist/plugins/works/editors.js +162 -0
- package/dist/plugins/works/events.d.ts +58 -0
- package/dist/plugins/works/events.js +82 -0
- package/dist/plugins/works/files.d.ts +23 -0
- package/dist/plugins/works/files.js +53 -0
- package/dist/plugins/works/index.d.ts +9 -0
- package/dist/plugins/works/index.js +1088 -0
- package/dist/plugins/works/plugin.yaml +9 -0
- package/dist/plugins/works/preview.d.ts +10 -0
- package/dist/plugins/works/preview.js +30 -0
- package/dist/plugins/works/project.d.ts +3 -0
- package/dist/plugins/works/project.js +245 -0
- package/dist/plugins/works/resource.d.ts +28 -0
- package/dist/plugins/works/resource.js +71 -0
- package/dist/plugins/works/retention.d.ts +36 -0
- package/dist/plugins/works/retention.js +295 -0
- package/dist/plugins/works/skills/works-ops/SKILL.md +14 -0
- package/dist/plugins/works/viewers.d.ts +13 -0
- package/dist/plugins/works/viewers.js +87 -0
- package/dist/plugins/works/web/anchors.js +94 -0
- package/dist/plugins/works/web/document-edit.js +245 -0
- package/dist/plugins/works/web/preview-bridge.js +142 -0
- package/dist/plugins/works/web/reading-position.js +74 -0
- package/dist/plugins/works/web/view.html +1118 -0
- package/dist/runtime/runtime.d.ts +210 -0
- package/dist/runtime/runtime.js +338 -0
- package/dist/sdk/adapter-kit.d.ts +1 -0
- package/dist/sdk/adapter-kit.js +1 -0
- package/dist/sdk/client.d.ts +163 -0
- package/dist/sdk/client.js +43 -0
- package/dist/sdk/contracts.d.ts +1263 -0
- package/dist/sdk/contracts.js +1 -0
- package/dist/sdk/index.d.ts +57 -0
- package/dist/sdk/index.js +27 -0
- package/dist/sdk/source-choice.d.ts +40 -0
- package/dist/sdk/source-choice.js +217 -0
- package/dist/sdk/topic-text.d.ts +5 -0
- package/dist/sdk/topic-text.js +26 -0
- package/dist/sdk/transport.d.ts +12 -0
- package/dist/sdk/transport.js +34 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.d.ts +88 -0
- package/node_modules/@hostlet/adapter-kit/dist/acp.js +674 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.d.ts +18 -0
- package/node_modules/@hostlet/adapter-kit/dist/command.js +39 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.d.ts +8 -0
- package/node_modules/@hostlet/adapter-kit/dist/index.js +6 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/models.js +108 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.d.ts +3 -0
- package/node_modules/@hostlet/adapter-kit/dist/quota.js +4 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.d.ts +29 -0
- package/node_modules/@hostlet/adapter-kit/dist/rpc.js +84 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.d.ts +5 -0
- package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.js +37 -0
- package/node_modules/@hostlet/adapter-kit/package.json +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.d.ts +16 -0
- package/node_modules/@hostlet/platform/dist/desktop.js +270 -0
- package/node_modules/@hostlet/platform/dist/files.d.ts +24 -0
- package/node_modules/@hostlet/platform/dist/files.js +113 -0
- package/node_modules/@hostlet/platform/dist/index.d.ts +6 -0
- package/node_modules/@hostlet/platform/dist/index.js +4 -0
- package/node_modules/@hostlet/platform/dist/paths.d.ts +4 -0
- package/node_modules/@hostlet/platform/dist/paths.js +9 -0
- package/node_modules/@hostlet/platform/dist/process.d.ts +45 -0
- package/node_modules/@hostlet/platform/dist/process.js +297 -0
- package/node_modules/@hostlet/platform/package.json +14 -0
- package/package.json +60 -4
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, statSync } from 'node:fs';
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import { listModels } from '@hostlet/adapter-kit';
|
|
5
|
+
import { address, errorText, HttpError, newId, object, readJson, TurnFailureError, writeDocuments, writeJson } from 'hostlet/sdk';
|
|
6
|
+
import { openLoginLink } from "./login-link.js";
|
|
7
|
+
import { accountDirectories } from "./account-directories.js";
|
|
8
|
+
import { accountUsage, cleanAccount } from "./account-storage.js";
|
|
9
|
+
import { addUsageTotals, computeWindowBurn, emptyUsageTotals, finalizeUsageTotals, parseTierMultiplier, quotaSampleStore, RANGE_MS, RETENTION_DAYS, RETENTION_MS, sanitizeQuotaSample } from "./quota-history.js";
|
|
10
|
+
const runCommand = promisify(execFile);
|
|
11
|
+
const PROTOCOLS = ['openai', 'openai-responses', 'anthropic', 'gemini'];
|
|
12
|
+
async function runLogout(logout, directory, env) {
|
|
13
|
+
try {
|
|
14
|
+
const [command, ...args] = typeof logout === 'function' ? logout(env) : logout;
|
|
15
|
+
await runCommand(command, args, { cwd: directory, env: { ...process.env, ...env }, timeout: 15_000 });
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
throw new HttpError(400, '客户端注销失败,请检查客户端后重试');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* API keys, kept once: a name, the protocol the key speaks (OpenAI chat, OpenAI responses, Anthropic or Gemini), an endpoint when it is
|
|
23
|
+
* not the usual one, and the key. A member names the key its client runs with; how that reaches the client
|
|
24
|
+
* (which variables, which config) is the adapter's business, and so is listing the models the key can use.
|
|
25
|
+
*/
|
|
26
|
+
export default function keysPlugin(home) {
|
|
27
|
+
return {
|
|
28
|
+
name: 'keys',
|
|
29
|
+
title: '密钥',
|
|
30
|
+
about: '集中管理 API 密钥和客户端登录账号。成员选择凭据,适配器负责接入、模型和额度。',
|
|
31
|
+
group: '客户端',
|
|
32
|
+
needs: ['entry', 'a2a', 'members', 'instances', 'push'],
|
|
33
|
+
start(ctx) {
|
|
34
|
+
const entry = ctx.get('entry');
|
|
35
|
+
const a2a = ctx.get('a2a');
|
|
36
|
+
const members = ctx.get('members');
|
|
37
|
+
const instances = ctx.get('instances');
|
|
38
|
+
const push = ctx.get('push');
|
|
39
|
+
const listFile = home.path('plugins', 'keys', 'keys.json');
|
|
40
|
+
const secretsFile = home.path('secrets', 'keys.json');
|
|
41
|
+
const list = () => readJson(listFile, []);
|
|
42
|
+
const secrets = () => (existsSync(secretsFile) ? JSON.parse(readFileSync(secretsFile, 'utf8')) : {});
|
|
43
|
+
const writeSecrets = (all) => {
|
|
44
|
+
writeDocuments(home, [{ path: secretsFile, value: all, mode: 0o600 }]);
|
|
45
|
+
};
|
|
46
|
+
const clearSecret = (id) => {
|
|
47
|
+
const all = secrets();
|
|
48
|
+
if (id in all) {
|
|
49
|
+
delete all[id];
|
|
50
|
+
writeSecrets(all);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const apiKeyOf = (key) => {
|
|
54
|
+
const apiKey = secrets()[key.id];
|
|
55
|
+
return apiKey ? { protocol: key.protocol, ...(key.endpoint ? { endpoint: key.endpoint } : {}), apiKey } : undefined;
|
|
56
|
+
};
|
|
57
|
+
const users = (id) => members.list().filter((m) => m.key === id);
|
|
58
|
+
function checkUnused(id, status) {
|
|
59
|
+
const using = users(id);
|
|
60
|
+
if (using.length)
|
|
61
|
+
throw new HttpError(status, `${using.map((m) => m.name).join('、')} 还在用它:先在成员的「编辑」里换掉`);
|
|
62
|
+
}
|
|
63
|
+
/** An instance keeps the key it started with: idle ones of the members using it are stopped so the next turn starts with the new one. */
|
|
64
|
+
const restart = (id) => {
|
|
65
|
+
ctx.runtime.emit('key.changed', { id });
|
|
66
|
+
for (const member of users(id))
|
|
67
|
+
void instances.stop({ memberId: member.id }, { force: false, reason: '它用的密钥变了' });
|
|
68
|
+
};
|
|
69
|
+
const directories = accountDirectories(home);
|
|
70
|
+
const dir = directories.path;
|
|
71
|
+
function removalDirectory(account, expected) {
|
|
72
|
+
checkUnused(account.id, 409);
|
|
73
|
+
return directories.forRemoval(account, list().filter((item) => 'adapter' in item), expected);
|
|
74
|
+
}
|
|
75
|
+
function unregister(id) {
|
|
76
|
+
clearSecret(id);
|
|
77
|
+
writeJson(listFile, list().filter((item) => item.id !== id));
|
|
78
|
+
const tiers = readTiers();
|
|
79
|
+
if (id in tiers) {
|
|
80
|
+
delete tiers[id];
|
|
81
|
+
writeTiers(tiers);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const tiersFile = home.path('plugins', 'keys', 'account-tiers.json');
|
|
85
|
+
const readTiers = () => readJson(tiersFile, {});
|
|
86
|
+
const writeTiers = (tiers) => writeJson(tiersFile, tiers);
|
|
87
|
+
const sampleStore = quotaSampleStore(home.path('plugins', 'keys', 'quota-samples.jsonl'));
|
|
88
|
+
let inFlightSample = null;
|
|
89
|
+
let unloading = false;
|
|
90
|
+
ctx.effect(async () => {
|
|
91
|
+
unloading = true;
|
|
92
|
+
if (inFlightSample)
|
|
93
|
+
await inFlightSample.catch(() => { });
|
|
94
|
+
});
|
|
95
|
+
const capability = (adapter) => instances.adapters().find((item) => item.name === adapter)?.adapter.account;
|
|
96
|
+
const getAccount = (id) => {
|
|
97
|
+
const found = list().find((item) => item.id === id);
|
|
98
|
+
if (!found || !('adapter' in found))
|
|
99
|
+
throw new HttpError(404, '没有这个客户端账号');
|
|
100
|
+
return found;
|
|
101
|
+
};
|
|
102
|
+
const clientOf = (account) => {
|
|
103
|
+
const client = capability(account.adapter);
|
|
104
|
+
if (!client)
|
|
105
|
+
throw new HttpError(409, `${account.adapter} 的账号管理不可用`);
|
|
106
|
+
return client;
|
|
107
|
+
};
|
|
108
|
+
const isTokenAccount = (account) => account.authMode === 'token';
|
|
109
|
+
const signedIn = (file) => {
|
|
110
|
+
try {
|
|
111
|
+
const stat = statSync(file, { throwIfNoEntry: false });
|
|
112
|
+
return !!stat?.isFile() && stat.size > 0;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
const code = error.code;
|
|
116
|
+
if (code === 'EACCES' || code === 'EPERM')
|
|
117
|
+
return null;
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const accountSignedIn = async (account, client, directory, precomputedEnv) => {
|
|
122
|
+
if (isTokenAccount(account)) {
|
|
123
|
+
if (!client.token)
|
|
124
|
+
return null;
|
|
125
|
+
return Boolean(secrets()[account.id]);
|
|
126
|
+
}
|
|
127
|
+
if (client.checkLogin) {
|
|
128
|
+
try {
|
|
129
|
+
const env = precomputedEnv ?? await client.env(directory);
|
|
130
|
+
return await client.checkLogin(directory, env);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return client.credentialFile ? signedIn(client.credentialFile(directory)) : false;
|
|
137
|
+
};
|
|
138
|
+
const publicItem = async (item) => {
|
|
139
|
+
if (!('adapter' in item))
|
|
140
|
+
return item;
|
|
141
|
+
const client = capability(item.adapter);
|
|
142
|
+
const loggedIn = client ? await accountSignedIn(item, client, dir(item)) : null;
|
|
143
|
+
const tier = readTiers()[item.id] || '';
|
|
144
|
+
return { ...item, dir: dir(item), managedDir: directories.managed(item), loggedIn, ...(tier ? { tier } : {}) };
|
|
145
|
+
};
|
|
146
|
+
const terminals = new Map();
|
|
147
|
+
ctx.effect(async () => { await Promise.all([...terminals.values()].map((terminal) => terminal.close())); });
|
|
148
|
+
const changing = new Set();
|
|
149
|
+
/** Accounts whose directory Storage is cleaning: a turn that needs one waits until it is done. */
|
|
150
|
+
const cleaning = new Map();
|
|
151
|
+
async function change(id, work) {
|
|
152
|
+
if (changing.has(id) || cleaning.has(id))
|
|
153
|
+
throw new HttpError(409, '这个账号正在处理另一项操作');
|
|
154
|
+
changing.add(id);
|
|
155
|
+
try {
|
|
156
|
+
return await work();
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
changing.delete(id);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
ctx.hook('key.resolve', async ({ id }) => {
|
|
163
|
+
await cleaning.get(id);
|
|
164
|
+
const item = list().find((k) => k.id === id);
|
|
165
|
+
if (!item)
|
|
166
|
+
return;
|
|
167
|
+
if (!('adapter' in item))
|
|
168
|
+
return apiKeyOf(item);
|
|
169
|
+
const client = clientOf(item);
|
|
170
|
+
const directory = dir(item);
|
|
171
|
+
if (isTokenAccount(item)) {
|
|
172
|
+
if (!client.token)
|
|
173
|
+
throw new HttpError(409, `${item.adapter} 不支持订阅 Token 账号`);
|
|
174
|
+
const token = secrets()[item.id];
|
|
175
|
+
if (changing.has(id) || !token) {
|
|
176
|
+
throw new TurnFailureError('account', new Error(`账号「${item.name}」未配置订阅 Token 或已清除,请到密钥页重新配置`));
|
|
177
|
+
}
|
|
178
|
+
const env = await client.token.env(directory, token);
|
|
179
|
+
return { adapter: item.adapter, env };
|
|
180
|
+
}
|
|
181
|
+
const env = await client.env(directory);
|
|
182
|
+
const saved = await accountSignedIn(item, client, directory, env);
|
|
183
|
+
if (saved === null)
|
|
184
|
+
throw new TurnFailureError('account', new Error(`无法读取账号「${item.name}」的登录状态,请检查 ${directory} 的访问权限或客户端安装`));
|
|
185
|
+
if (changing.has(id) || !saved) {
|
|
186
|
+
throw new TurnFailureError('account', new Error(`账号「${item.name}」未登录或登录已失效,请到密钥页重新登录`));
|
|
187
|
+
}
|
|
188
|
+
return { adapter: item.adapter, env };
|
|
189
|
+
});
|
|
190
|
+
// Storage: what each account directory holds and what of it its client says may go. Only directories under
|
|
191
|
+
// the accounts root are offered; an account a member is running on (or that is being changed) is left alone.
|
|
192
|
+
const storageAccounts = () => list().filter((item) => 'adapter' in item && directories.managed(item));
|
|
193
|
+
const busyAccount = (id) => {
|
|
194
|
+
if (changing.has(id))
|
|
195
|
+
return true;
|
|
196
|
+
const live = new Set(instances.list().map((instance) => instance.memberId));
|
|
197
|
+
return users(id).some((member) => live.has(member.id));
|
|
198
|
+
};
|
|
199
|
+
const sessionsAllowed = (id) => !users(id).some((member) => instances.hasAdopted(member.id));
|
|
200
|
+
ctx.provide('storage.accounts', {
|
|
201
|
+
async inventory() {
|
|
202
|
+
const out = [];
|
|
203
|
+
for (const account of storageAccounts()) {
|
|
204
|
+
const usage = await accountUsage(dir(account), capability(account.adapter), sessionsAllowed(account.id));
|
|
205
|
+
if (!usage)
|
|
206
|
+
continue;
|
|
207
|
+
const busy = cleaning.has(account.id) || busyAccount(account.id);
|
|
208
|
+
const title = instances.adapters().find((item) => item.name === account.adapter)?.adapter.title || account.adapter;
|
|
209
|
+
out.push({
|
|
210
|
+
accountId: account.id, name: account.name, adapter: account.adapter, adapterTitle: title,
|
|
211
|
+
files: usage.files, bytes: usage.bytes, busy, sessionsKept: !sessionsAllowed(account.id), items: busy ? [] : usage.items,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return out;
|
|
215
|
+
},
|
|
216
|
+
async clean(accountId, items) {
|
|
217
|
+
const account = storageAccounts().find((item) => item.id === accountId);
|
|
218
|
+
if (!account || !items.length || cleaning.has(accountId) || busyAccount(accountId))
|
|
219
|
+
return;
|
|
220
|
+
let done;
|
|
221
|
+
cleaning.set(accountId, new Promise((resolve) => { done = resolve; }));
|
|
222
|
+
try {
|
|
223
|
+
return await cleanAccount(dir(account), capability(account.adapter), sessionsAllowed(accountId), items);
|
|
224
|
+
}
|
|
225
|
+
finally {
|
|
226
|
+
cleaning.delete(accountId);
|
|
227
|
+
done();
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
ctx.effect(entry.group('keys', 'API 密钥、客户端账号与额度消耗'));
|
|
232
|
+
const id = { type: 'string', description: '密钥的编号' };
|
|
233
|
+
ctx.effect(entry.op({ name: 'keys.list', about: '列出 API 密钥的协议和地址,以及客户端账号的登录状态。', http: ['GET', '/api/keys'], run: () => Promise.all(list().map(publicItem)) }));
|
|
234
|
+
function listLocalAccounts() {
|
|
235
|
+
const order = new Map(instances.adapters().map(({ name }, index) => [name, index]));
|
|
236
|
+
const groups = new Map();
|
|
237
|
+
for (const member of members.list()) {
|
|
238
|
+
const account = instances.localQuotaAccount(member.id);
|
|
239
|
+
if (!account)
|
|
240
|
+
continue;
|
|
241
|
+
let group = groups.get(account.key);
|
|
242
|
+
if (!group) {
|
|
243
|
+
group = { ...account, adapter: member.adapter, members: [] };
|
|
244
|
+
groups.set(account.key, group);
|
|
245
|
+
}
|
|
246
|
+
group.members.push(member);
|
|
247
|
+
}
|
|
248
|
+
return [...groups.values()].sort((a, b) => (order.get(a.adapter) ?? order.size) - (order.get(b.adapter) ?? order.size) ||
|
|
249
|
+
a.adapter.localeCompare(b.adapter) || Number(a.configured) - Number(b.configured) || a.customCmd.localeCompare(b.customCmd)).map((group) => ({
|
|
250
|
+
...group,
|
|
251
|
+
rep: (group.members.find((member) => member.enabled) ?? group.members[0]).id,
|
|
252
|
+
members: group.members.map((member) => member.id),
|
|
253
|
+
}));
|
|
254
|
+
}
|
|
255
|
+
ctx.effect(entry.op({
|
|
256
|
+
name: 'keys.local_accounts', about: '列出成员使用的本机客户端登录及对应成员。',
|
|
257
|
+
http: ['GET', '/api/keys/accounts/local'], workload: 'local', run: listLocalAccounts,
|
|
258
|
+
}));
|
|
259
|
+
ctx.effect(entry.op({
|
|
260
|
+
name: 'keys.account_draft', about: '按当前数据目录预览新账号的编号和目录。',
|
|
261
|
+
http: ['POST', '/api/keys/accounts/draft'], localPage: true, run: directories.draft,
|
|
262
|
+
}));
|
|
263
|
+
ctx.effect(entry.op({
|
|
264
|
+
name: 'keys.account_add', about: '为客户端创建一个独立的登录目录。',
|
|
265
|
+
input: object({
|
|
266
|
+
name: { type: 'string' }, adapter: { type: 'string' }, id, dir: { type: 'string' },
|
|
267
|
+
authMode: { type: 'string', enum: ['login', 'token'] },
|
|
268
|
+
token: { type: 'string' },
|
|
269
|
+
}, ['name', 'adapter']),
|
|
270
|
+
http: ['POST', '/api/keys/accounts'], localPage: true,
|
|
271
|
+
async run({ name, adapter, id = newId('account'), dir: requested, authMode, token }) {
|
|
272
|
+
const account = { id, name: name.trim(), adapter, ...(authMode === 'token' ? { authMode: 'token' } : {}) };
|
|
273
|
+
if (!account.name)
|
|
274
|
+
throw new HttpError(400, '请填写账号名称');
|
|
275
|
+
if (!/^account_[a-z0-9]{1,40}$/.test(id))
|
|
276
|
+
throw new HttpError(400, '账号编号无效');
|
|
277
|
+
const client = clientOf(account);
|
|
278
|
+
const secret = authMode === 'token' ? (token ?? '').trim() : '';
|
|
279
|
+
if (authMode === 'token') {
|
|
280
|
+
if (!client.token)
|
|
281
|
+
throw new HttpError(409, `${adapter} 不支持订阅 Token 账号`);
|
|
282
|
+
if (!secret)
|
|
283
|
+
throw new HttpError(400, '请填写订阅 Token');
|
|
284
|
+
}
|
|
285
|
+
const all = list();
|
|
286
|
+
if (all.some((item) => item.id === id))
|
|
287
|
+
throw new HttpError(409, '账号编号已被使用');
|
|
288
|
+
directories.create(account, requested, all.filter((item) => 'adapter' in item), () => {
|
|
289
|
+
if (authMode === 'token')
|
|
290
|
+
writeSecrets({ ...secrets(), [account.id]: secret });
|
|
291
|
+
try {
|
|
292
|
+
writeJson(listFile, [...all, account]);
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
if (authMode === 'token')
|
|
296
|
+
clearSecret(account.id);
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
return await publicItem(account);
|
|
301
|
+
},
|
|
302
|
+
}));
|
|
303
|
+
ctx.effect(entry.op({
|
|
304
|
+
name: 'keys.account_token', about: '更新订阅 Token 账号保存的 Token。',
|
|
305
|
+
input: object({ id, token: { type: 'string' } }, ['id', 'token']),
|
|
306
|
+
http: ['POST', '/api/keys/:id/token'], localPage: true,
|
|
307
|
+
run: ({ id, token }) => change(id, async () => {
|
|
308
|
+
const account = getAccount(id);
|
|
309
|
+
if (!isTokenAccount(account))
|
|
310
|
+
throw new HttpError(409, '该账号使用终端登录,请打开登录终端操作');
|
|
311
|
+
const client = clientOf(account);
|
|
312
|
+
if (!client.token)
|
|
313
|
+
throw new HttpError(409, `${account.adapter} 不支持订阅 Token 账号`);
|
|
314
|
+
const secret = token.trim();
|
|
315
|
+
if (!secret)
|
|
316
|
+
throw new HttpError(400, '请填写订阅 Token');
|
|
317
|
+
writeSecrets({ ...secrets(), [account.id]: secret });
|
|
318
|
+
ctx.runtime.emit('key.changed', { id }); // as soon as it is written: what was read for the old token no longer holds
|
|
319
|
+
await Promise.all(users(id).map((member) => instances.stop({ memberId: member.id }, { force: false, reason: `账号「${account.name}」的订阅 Token 已更新` })));
|
|
320
|
+
push.send('keys.login_changed', { id });
|
|
321
|
+
return await publicItem(account);
|
|
322
|
+
}),
|
|
323
|
+
}));
|
|
324
|
+
ctx.effect(entry.op({
|
|
325
|
+
name: 'keys.account_login', about: '在本机终端用客户端自己的登录命令打开浏览器授权。',
|
|
326
|
+
input: object({ id }, ['id']), http: ['POST', '/api/keys/:id/login'], localPage: true,
|
|
327
|
+
run: ({ id }) => change(id, async () => {
|
|
328
|
+
const account = getAccount(id);
|
|
329
|
+
if (isTokenAccount(account))
|
|
330
|
+
throw new HttpError(409, '该账号使用长效订阅 Token,请在密钥页直接更新 Token');
|
|
331
|
+
const prior = terminals.get(id);
|
|
332
|
+
if (prior)
|
|
333
|
+
return { path: prior.path };
|
|
334
|
+
if (!ctx.runtime.has('terminal'))
|
|
335
|
+
throw new HttpError(409, '请先启用工作台终端插件');
|
|
336
|
+
const client = clientOf(account);
|
|
337
|
+
const env = await client.env(dir(account));
|
|
338
|
+
const browser = client.loginBrowser;
|
|
339
|
+
const open = browser && openLoginLink(browser.endpoint, (url) => entry.invoke('console.open_browser', { url }));
|
|
340
|
+
const login = typeof client.login === 'function' ? client.login(env) : client.login;
|
|
341
|
+
const terminal = ctx.get('terminal').command(login, dir(account), { ...env, ...browser?.env }, open);
|
|
342
|
+
terminals.set(id, terminal);
|
|
343
|
+
void terminal.exited.then(() => {
|
|
344
|
+
terminals.delete(id);
|
|
345
|
+
ctx.runtime.emit('key.changed', { id });
|
|
346
|
+
push.send('keys.login_changed', { id });
|
|
347
|
+
});
|
|
348
|
+
return { path: terminal.path };
|
|
349
|
+
}),
|
|
350
|
+
}));
|
|
351
|
+
ctx.effect(entry.op({
|
|
352
|
+
name: 'keys.account_logout', about: '注销这个账号并停止使用它的实例,成员仍保留账号选择。',
|
|
353
|
+
input: object({ id }, ['id']), http: ['POST', '/api/keys/:id/logout'], localPage: true,
|
|
354
|
+
run: ({ id }) => change(id, async () => {
|
|
355
|
+
const account = getAccount(id);
|
|
356
|
+
const client = clientOf(account);
|
|
357
|
+
const directory = dir(account);
|
|
358
|
+
ctx.runtime.emit('key.changed', { id }); // signing out starts now: reads for this account no longer hold
|
|
359
|
+
await terminals.get(id)?.close();
|
|
360
|
+
await Promise.all(users(id).map((member) => instances.stop({ memberId: member.id }, { force: true, reason: `账号「${account.name}」已注销` })));
|
|
361
|
+
if (isTokenAccount(account)) {
|
|
362
|
+
clearSecret(id);
|
|
363
|
+
ctx.runtime.emit('key.changed', { id });
|
|
364
|
+
push.send('keys.login_changed', { id });
|
|
365
|
+
return await publicItem(account);
|
|
366
|
+
}
|
|
367
|
+
const env = await client.env(directory);
|
|
368
|
+
const currentlySignedIn = await accountSignedIn(account, client, directory, env);
|
|
369
|
+
if (client.logout && currentlySignedIn) {
|
|
370
|
+
await runLogout(client.logout, directory, env);
|
|
371
|
+
}
|
|
372
|
+
else if (client.credentialFile) {
|
|
373
|
+
rmSync(client.credentialFile(directory), { force: true });
|
|
374
|
+
}
|
|
375
|
+
if (await accountSignedIn(account, client, directory, env))
|
|
376
|
+
throw new HttpError(409, '客户端仍保存着这个账号的登录凭据');
|
|
377
|
+
ctx.runtime.emit('key.changed', { id });
|
|
378
|
+
push.send('keys.login_changed', { id });
|
|
379
|
+
return await publicItem(account);
|
|
380
|
+
}),
|
|
381
|
+
}));
|
|
382
|
+
ctx.effect(entry.op({
|
|
383
|
+
name: 'keys.save', about: '添加或编辑一条 API 密钥。',
|
|
384
|
+
input: object({
|
|
385
|
+
id, name: { type: 'string', description: '密钥的显示名' },
|
|
386
|
+
protocol: { type: 'string', enum: PROTOCOLS },
|
|
387
|
+
endpoint: { type: 'string', description: '留空使用该协议的通常地址' },
|
|
388
|
+
apiKey: { type: 'string', description: '编辑已有密钥时可省略,沿用已存的值' },
|
|
389
|
+
}, ['name', 'protocol']),
|
|
390
|
+
http: ['POST', '/api/keys'],
|
|
391
|
+
run({ id, name: label, protocol, endpoint: address = '', apiKey: secret = '' }) {
|
|
392
|
+
const all = list();
|
|
393
|
+
const existing = all.find((key) => key.id === id);
|
|
394
|
+
if (existing && 'adapter' in existing)
|
|
395
|
+
throw new HttpError(400, '该编号属于客户端账号');
|
|
396
|
+
const name = label.trim();
|
|
397
|
+
const endpoint = address.trim();
|
|
398
|
+
const apiKey = secret.trim() || (existing ? secrets()[existing.id] : '');
|
|
399
|
+
if (!name)
|
|
400
|
+
throw new Error('给它起个名字');
|
|
401
|
+
if (endpoint && !/^https?:\/\//.test(endpoint))
|
|
402
|
+
throw new Error('地址要以 http:// 或 https:// 开头;用这个协议通常的地址就留空');
|
|
403
|
+
if (!apiKey)
|
|
404
|
+
throw new Error('填上 API key');
|
|
405
|
+
const key = { id: existing?.id ?? newId('key'), name, protocol, ...(endpoint ? { endpoint } : {}) };
|
|
406
|
+
writeJson(listFile, existing ? all.map((k) => (k.id === key.id ? key : k)) : [...all, key]);
|
|
407
|
+
writeSecrets({ ...secrets(), [key.id]: apiKey });
|
|
408
|
+
restart(key.id);
|
|
409
|
+
return key;
|
|
410
|
+
},
|
|
411
|
+
}));
|
|
412
|
+
ctx.effect(entry.op({
|
|
413
|
+
name: 'keys.remove', about: '删除未被成员引用的凭据。客户端账号先按声明注销,再删除其独立目录和登记。',
|
|
414
|
+
input: object({ id }, ['id']), http: ['DELETE', '/api/keys/:id'],
|
|
415
|
+
async run({ id }) {
|
|
416
|
+
const item = list().find((key) => key.id === id);
|
|
417
|
+
if (item && 'adapter' in item) {
|
|
418
|
+
return change(id, async () => {
|
|
419
|
+
const directory = removalDirectory(item);
|
|
420
|
+
const client = clientOf(item);
|
|
421
|
+
await terminals.get(id)?.close();
|
|
422
|
+
if (client.logout) {
|
|
423
|
+
const token = isTokenAccount(item) && secrets()[id];
|
|
424
|
+
const env = token && client.token ? await client.token.env(directory, token) : await client.env(directory);
|
|
425
|
+
removalDirectory(item, directory);
|
|
426
|
+
// A retry may find the directory already gone but a keychain entry or registration still present.
|
|
427
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
428
|
+
await runLogout(client.logout, directory, env);
|
|
429
|
+
}
|
|
430
|
+
rmSync(removalDirectory(item, directory), { recursive: true, force: true });
|
|
431
|
+
unregister(id);
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
checkUnused(id, 400);
|
|
435
|
+
unregister(id);
|
|
436
|
+
},
|
|
437
|
+
}));
|
|
438
|
+
function listClientAccounts(locals = listLocalAccounts()) {
|
|
439
|
+
const adapterEntries = instances.adapters();
|
|
440
|
+
const out = [];
|
|
441
|
+
for (const account of list().filter((item) => 'adapter' in item)) {
|
|
442
|
+
const entry = adapterEntries.find((item) => item.name === account.adapter);
|
|
443
|
+
if (!entry?.adapter.quota)
|
|
444
|
+
continue;
|
|
445
|
+
out.push({
|
|
446
|
+
key: account.id,
|
|
447
|
+
id: account.id,
|
|
448
|
+
name: account.name,
|
|
449
|
+
adapter: account.adapter,
|
|
450
|
+
adapterTitle: entry.adapter.title || account.adapter,
|
|
451
|
+
local: false,
|
|
452
|
+
members: users(account.id).map((member) => member.id),
|
|
453
|
+
account,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
for (const group of locals) {
|
|
457
|
+
const entry = adapterEntries.find((item) => item.name === group.adapter);
|
|
458
|
+
if (!entry?.adapter.quota)
|
|
459
|
+
continue;
|
|
460
|
+
const title = entry.adapter.title || group.adapter;
|
|
461
|
+
const label = group.configured ? members.get(group.rep)?.name : group.customCmd;
|
|
462
|
+
out.push({
|
|
463
|
+
key: group.key,
|
|
464
|
+
name: label ? `${title} · ${label}` : title,
|
|
465
|
+
adapter: group.adapter,
|
|
466
|
+
adapterTitle: title,
|
|
467
|
+
local: true,
|
|
468
|
+
members: group.members,
|
|
469
|
+
rep: group.rep,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
for (const key of list().filter((item) => !('adapter' in item))) {
|
|
473
|
+
const usersOfKey = members.list().filter((member) => member.kind === 'agent' && member.key === key.id)
|
|
474
|
+
.filter((member) => adapterEntries.find((item) => item.name === member.adapter)?.adapter.quota);
|
|
475
|
+
const rep = usersOfKey.find((member) => member.enabled) ?? usersOfKey[0];
|
|
476
|
+
if (!rep)
|
|
477
|
+
continue;
|
|
478
|
+
const entry = adapterEntries.find((item) => item.name === rep.adapter);
|
|
479
|
+
out.push({
|
|
480
|
+
key: key.id,
|
|
481
|
+
id: key.id,
|
|
482
|
+
name: key.name,
|
|
483
|
+
adapter: rep.adapter,
|
|
484
|
+
adapterTitle: entry?.adapter.title || rep.adapter,
|
|
485
|
+
local: false,
|
|
486
|
+
apiKey: true,
|
|
487
|
+
members: usersOfKey.map((member) => member.id),
|
|
488
|
+
rep: rep.id,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
async function sampleQuotas() {
|
|
494
|
+
if (inFlightSample)
|
|
495
|
+
return inFlightSample;
|
|
496
|
+
const work = (async () => {
|
|
497
|
+
const at = Date.now();
|
|
498
|
+
const accounts = listClientAccounts();
|
|
499
|
+
const errors = [];
|
|
500
|
+
const settled = await Promise.allSettled(accounts.map(async (target) => {
|
|
501
|
+
try {
|
|
502
|
+
const adapterEntry = instances.adapters().find((item) => item.name === target.adapter);
|
|
503
|
+
if (!adapterEntry?.adapter.quota)
|
|
504
|
+
return { target, windows: [] };
|
|
505
|
+
if (target.account) {
|
|
506
|
+
const client = clientOf(target.account);
|
|
507
|
+
const signed = await accountSignedIn(target.account, client, dir(target.account));
|
|
508
|
+
if (signed !== true)
|
|
509
|
+
return { target, windows: [] };
|
|
510
|
+
const windows = await entry.invoke('members.adapter_quota', { name: target.adapter, key: target.account.id });
|
|
511
|
+
return { target, windows };
|
|
512
|
+
}
|
|
513
|
+
if (!target.rep)
|
|
514
|
+
return { target, windows: [] };
|
|
515
|
+
const windows = await entry.invoke('members.adapter_quota', { name: target.adapter, member: target.rep });
|
|
516
|
+
return { target, windows };
|
|
517
|
+
}
|
|
518
|
+
catch (error) {
|
|
519
|
+
return { target, error: errorText(error) };
|
|
520
|
+
}
|
|
521
|
+
}));
|
|
522
|
+
const samples = [];
|
|
523
|
+
let sampledAccounts = 0;
|
|
524
|
+
for (const result of settled) {
|
|
525
|
+
if (result.status === 'rejected') {
|
|
526
|
+
errors.push({ account: '', name: '', error: errorText(result.reason) });
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
const { target, windows, error } = result.value;
|
|
530
|
+
if (error) {
|
|
531
|
+
errors.push({ account: target.key, name: target.name, error });
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (!Array.isArray(windows) || !windows.length)
|
|
535
|
+
continue;
|
|
536
|
+
sampledAccounts++;
|
|
537
|
+
for (const window of windows) {
|
|
538
|
+
if (typeof window.label !== 'string' || !window.label)
|
|
539
|
+
continue;
|
|
540
|
+
const hasPercent = typeof window.usedPercent === 'number' && Number.isFinite(window.usedPercent);
|
|
541
|
+
const hasReset = typeof window.resetsAt === 'number' && Number.isFinite(window.resetsAt) && window.resetsAt > 0;
|
|
542
|
+
if (!hasPercent && !hasReset)
|
|
543
|
+
continue;
|
|
544
|
+
const usedPercent = window.usedPercent;
|
|
545
|
+
const resetsAt = window.resetsAt;
|
|
546
|
+
samples.push({
|
|
547
|
+
at,
|
|
548
|
+
account: target.key,
|
|
549
|
+
window: window.label,
|
|
550
|
+
...(hasPercent ? { usedPercent: usedPercent } : {}),
|
|
551
|
+
...(hasReset ? { resetsAt: resetsAt } : {}),
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
const cleanSamples = samples.map(sanitizeQuotaSample).filter((sample) => !!sample);
|
|
556
|
+
const writtenSamples = sampleStore.append(cleanSamples, at);
|
|
557
|
+
if (writtenSamples > 0 && !unloading)
|
|
558
|
+
push.send('keys.quota_changed', { at, samples: cleanSamples });
|
|
559
|
+
return { at, sampledAccounts, writtenSamples, errors };
|
|
560
|
+
})();
|
|
561
|
+
inFlightSample = work;
|
|
562
|
+
try {
|
|
563
|
+
return await work;
|
|
564
|
+
}
|
|
565
|
+
finally {
|
|
566
|
+
inFlightSample = null;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
ctx.effect(entry.op({
|
|
570
|
+
name: 'keys.tiers', about: '查看各账号手工登记的订阅档位。',
|
|
571
|
+
http: ['GET', '/api/keys/tiers'], agent: true, workload: 'local',
|
|
572
|
+
run: () => readTiers(),
|
|
573
|
+
}));
|
|
574
|
+
ctx.effect(entry.op({
|
|
575
|
+
name: 'keys.tier_set', about: '设置或清除一个账号的订阅档位,用于换算 1x 基准消耗速度。',
|
|
576
|
+
input: object({ key: { type: 'string', minLength: 1 }, tier: { type: 'string' } }, ['key', 'tier']),
|
|
577
|
+
http: ['POST', '/api/keys/tiers'],
|
|
578
|
+
run({ key: rawKey, tier: rawTier }) {
|
|
579
|
+
const key = rawKey.trim();
|
|
580
|
+
const clean = rawTier.trim();
|
|
581
|
+
if (!key || key.length > 200)
|
|
582
|
+
throw new HttpError(400, '账号编号无效');
|
|
583
|
+
if (clean.length > 40)
|
|
584
|
+
throw new HttpError(400, '档位说明最多 40 个字符');
|
|
585
|
+
const tiers = readTiers();
|
|
586
|
+
if (clean)
|
|
587
|
+
tiers[key] = clean;
|
|
588
|
+
else
|
|
589
|
+
delete tiers[key];
|
|
590
|
+
writeTiers(tiers);
|
|
591
|
+
push.send('keys.tiers_changed', { key, tier: clean || null });
|
|
592
|
+
return { key, tier: clean || null, multiplier: parseTierMultiplier(clean) };
|
|
593
|
+
},
|
|
594
|
+
}));
|
|
595
|
+
ctx.effect(entry.op({
|
|
596
|
+
name: 'keys.quota_sample', about: '立即采样所有支持额度查询的账号,并保存消耗速度历史。',
|
|
597
|
+
input: object({}),
|
|
598
|
+
http: ['POST', '/api/keys/quotas/sample'], agent: true, workload: 'external',
|
|
599
|
+
run: () => sampleQuotas(),
|
|
600
|
+
}));
|
|
601
|
+
function quotaUsage(accounts, locals, quotaRange, now) {
|
|
602
|
+
const since = now - RANGE_MS[quotaRange];
|
|
603
|
+
const adapterAccountBuckets = new Map();
|
|
604
|
+
const addBucket = (adapter, bucket) => {
|
|
605
|
+
if (!adapter || !bucket)
|
|
606
|
+
return;
|
|
607
|
+
let buckets = adapterAccountBuckets.get(adapter);
|
|
608
|
+
if (!buckets)
|
|
609
|
+
adapterAccountBuckets.set(adapter, buckets = new Set());
|
|
610
|
+
buckets.add(bucket);
|
|
611
|
+
};
|
|
612
|
+
for (const item of list()) {
|
|
613
|
+
if ('adapter' in item)
|
|
614
|
+
addBucket(item.adapter, item.id);
|
|
615
|
+
}
|
|
616
|
+
for (const member of members.list()) {
|
|
617
|
+
if (member.kind !== 'agent')
|
|
618
|
+
continue;
|
|
619
|
+
if (member.key)
|
|
620
|
+
addBucket(member.adapter, member.key);
|
|
621
|
+
}
|
|
622
|
+
for (const account of locals)
|
|
623
|
+
addBucket(account.adapter, account.key);
|
|
624
|
+
for (const account of accounts)
|
|
625
|
+
addBucket(account.adapter, account.key);
|
|
626
|
+
const totalUsage = emptyUsageTotals();
|
|
627
|
+
const unrecordedClient = emptyUsageTotals();
|
|
628
|
+
const usageByClient = new Map();
|
|
629
|
+
for (const turn of a2a.settledUsage(since, now + 1)) {
|
|
630
|
+
let client = turn.client;
|
|
631
|
+
if (client == null && turn.sessionId)
|
|
632
|
+
client = instances.storedSessionAdapter(turn.topicId, turn.memberId, turn.sessionId) ?? null;
|
|
633
|
+
addUsageTotals(totalUsage, turn.usage);
|
|
634
|
+
if (!client) {
|
|
635
|
+
addUsageTotals(unrecordedClient, turn.usage);
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
let totals = usageByClient.get(client);
|
|
639
|
+
if (!totals)
|
|
640
|
+
usageByClient.set(client, totals = emptyUsageTotals());
|
|
641
|
+
addUsageTotals(totals, turn.usage);
|
|
642
|
+
}
|
|
643
|
+
finalizeUsageTotals(totalUsage);
|
|
644
|
+
finalizeUsageTotals(unrecordedClient);
|
|
645
|
+
for (const totals of usageByClient.values())
|
|
646
|
+
finalizeUsageTotals(totals);
|
|
647
|
+
const accountKeysByClient = new Map();
|
|
648
|
+
for (const account of accounts) {
|
|
649
|
+
if (!account.adapter)
|
|
650
|
+
continue;
|
|
651
|
+
const keys = accountKeysByClient.get(account.adapter) ?? [];
|
|
652
|
+
if (!keys.includes(account.key))
|
|
653
|
+
keys.push(account.key);
|
|
654
|
+
accountKeysByClient.set(account.adapter, keys);
|
|
655
|
+
}
|
|
656
|
+
const clientNames = new Set([...usageByClient.keys(), ...accountKeysByClient.keys()]);
|
|
657
|
+
const byClient = {};
|
|
658
|
+
for (const client of [...clientNames].sort()) {
|
|
659
|
+
const totals = usageByClient.get(client) ?? emptyUsageTotals();
|
|
660
|
+
const bucketCount = adapterAccountBuckets.get(client)?.size ?? 0;
|
|
661
|
+
const adapterEntry = instances.adapters().find((item) => item.name === client);
|
|
662
|
+
byClient[client] = {
|
|
663
|
+
...totals,
|
|
664
|
+
coverage: { ...totals.coverage },
|
|
665
|
+
adapter: client,
|
|
666
|
+
adapterTitle: adapterEntry?.adapter.title || client,
|
|
667
|
+
accounts: accountKeysByClient.get(client) ?? [],
|
|
668
|
+
attribution: bucketCount === 1 ? 'client_single_account' : bucketCount > 1 ? 'unattributed_multi_account' : 'client_only',
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
const unattributedUsage = (attribution, client, clientAccountCount) => ({
|
|
672
|
+
attribution,
|
|
673
|
+
scope: 'client_only',
|
|
674
|
+
client,
|
|
675
|
+
clientAccountCount,
|
|
676
|
+
turns: null,
|
|
677
|
+
inputTokens: null,
|
|
678
|
+
outputTokens: null,
|
|
679
|
+
cachedReadTokens: null,
|
|
680
|
+
cachedWriteTokens: null,
|
|
681
|
+
cacheHitRate: null,
|
|
682
|
+
coverage: null,
|
|
683
|
+
});
|
|
684
|
+
const accountUsage = (account) => {
|
|
685
|
+
if (!account.adapter)
|
|
686
|
+
return unattributedUsage('unattributed', null, 0);
|
|
687
|
+
const clientAccountCount = adapterAccountBuckets.get(account.adapter)?.size ?? 0;
|
|
688
|
+
if (clientAccountCount !== 1)
|
|
689
|
+
return unattributedUsage('unattributed_multi_account', account.adapter, clientAccountCount);
|
|
690
|
+
const totals = byClient[account.adapter];
|
|
691
|
+
return {
|
|
692
|
+
attribution: 'client_single_account',
|
|
693
|
+
scope: 'client_only',
|
|
694
|
+
client: account.adapter,
|
|
695
|
+
clientAccountCount,
|
|
696
|
+
turns: totals?.turns ?? 0,
|
|
697
|
+
inputTokens: totals?.inputTokens ?? null,
|
|
698
|
+
outputTokens: totals?.outputTokens ?? null,
|
|
699
|
+
cachedReadTokens: totals?.cachedReadTokens ?? null,
|
|
700
|
+
cachedWriteTokens: totals?.cachedWriteTokens ?? null,
|
|
701
|
+
cacheHitRate: totals?.cacheHitRate ?? null,
|
|
702
|
+
coverage: totals ? { ...totals.coverage } : emptyUsageTotals().coverage,
|
|
703
|
+
};
|
|
704
|
+
};
|
|
705
|
+
return {
|
|
706
|
+
accountUsage,
|
|
707
|
+
summary: {
|
|
708
|
+
range: quotaRange,
|
|
709
|
+
since,
|
|
710
|
+
until: now,
|
|
711
|
+
attributionScope: 'client_only',
|
|
712
|
+
externalConsumption: 'cannot_isolate',
|
|
713
|
+
total: totalUsage,
|
|
714
|
+
unrecordedClient,
|
|
715
|
+
byClient,
|
|
716
|
+
},
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
ctx.effect(entry.op({
|
|
720
|
+
name: 'keys.quota_burn',
|
|
721
|
+
about: '按账号汇总额度消耗速度、同服务同窗口 1x 基准速度、预计用完与离重置余量,可附同时间范围的 Hostlet 客户端实记对照供成员调度参考。',
|
|
722
|
+
input: object({
|
|
723
|
+
range: { type: 'string', enum: ['24h', '7d', '14d'], description: 'Hostlet 实记账单对照的时间范围(以及 includePoints 为 true 时曲线点的时间范围),默认 24h。' },
|
|
724
|
+
includePoints: { type: 'boolean', description: '是否返回时间序列曲线点,默认 false。' },
|
|
725
|
+
includeUsage: { type: 'boolean', description: '是否汇总并返回 Hostlet 实记账单对照,默认 true。' },
|
|
726
|
+
now: { type: 'integer', description: '查询基准时间(Unix 毫秒),默认当前时间。' },
|
|
727
|
+
}),
|
|
728
|
+
http: ['GET', '/api/keys/quotas/burn'], agent: true, workload: 'local',
|
|
729
|
+
async run({ range = '24h', includePoints = false, includeUsage = true, now = Date.now() }) {
|
|
730
|
+
const quotaRange = range;
|
|
731
|
+
const locals = listLocalAccounts();
|
|
732
|
+
const accounts = listClientAccounts(locals);
|
|
733
|
+
const tiers = readTiers();
|
|
734
|
+
const cutoff = now - RETENTION_MS;
|
|
735
|
+
const histories = new Map();
|
|
736
|
+
for (const sample of sampleStore.samples(cutoff, now)) {
|
|
737
|
+
let windows = histories.get(sample.account);
|
|
738
|
+
if (!windows)
|
|
739
|
+
histories.set(sample.account, windows = new Map());
|
|
740
|
+
const rows = windows.get(sample.window) ?? [];
|
|
741
|
+
rows.push(sample);
|
|
742
|
+
windows.set(sample.window, rows);
|
|
743
|
+
}
|
|
744
|
+
for (const key of histories.keys()) {
|
|
745
|
+
if (!accounts.some((account) => account.key === key))
|
|
746
|
+
accounts.push({ key, name: key, adapter: '', adapterTitle: '', local: false, members: [] });
|
|
747
|
+
}
|
|
748
|
+
const usage = includeUsage ? quotaUsage(accounts, locals, quotaRange, now) : undefined;
|
|
749
|
+
const rows = accounts.map((account) => {
|
|
750
|
+
const tier = tiers[account.key] || '';
|
|
751
|
+
const multiplier = parseTierMultiplier(tier);
|
|
752
|
+
const windows = {};
|
|
753
|
+
for (const [label, samples] of histories.get(account.key) ?? new Map()) {
|
|
754
|
+
windows[label] = computeWindowBurn(samples, quotaRange, multiplier, now, includePoints === true);
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
key: account.key,
|
|
758
|
+
id: account.id,
|
|
759
|
+
name: account.name,
|
|
760
|
+
adapter: account.adapter,
|
|
761
|
+
adapterTitle: account.adapterTitle,
|
|
762
|
+
local: account.local,
|
|
763
|
+
apiKey: account.apiKey === true,
|
|
764
|
+
members: account.members,
|
|
765
|
+
...(tier ? { tier } : {}),
|
|
766
|
+
multiplier,
|
|
767
|
+
windows,
|
|
768
|
+
...(usage ? { hostletUsage: usage.accountUsage(account) } : {}),
|
|
769
|
+
};
|
|
770
|
+
});
|
|
771
|
+
let schedule = null;
|
|
772
|
+
if (ctx.runtime.has('schedule')) {
|
|
773
|
+
try {
|
|
774
|
+
const tasks = await entry.invoke('schedule.tasks', {});
|
|
775
|
+
const task = tasks.find((item) => item.key === 'keys.quota_sample');
|
|
776
|
+
if (task)
|
|
777
|
+
schedule = { id: task.id, enabled: task.enabled, rule: task.rule, next: task.next };
|
|
778
|
+
}
|
|
779
|
+
catch {
|
|
780
|
+
schedule = null;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
return {
|
|
784
|
+
range: quotaRange,
|
|
785
|
+
now,
|
|
786
|
+
retentionDays: RETENTION_DAYS,
|
|
787
|
+
normalizationScope: 'same-provider-window',
|
|
788
|
+
externalConsumption: 'cannot_isolate',
|
|
789
|
+
...(usage ? { hostletUsage: usage.summary } : {}),
|
|
790
|
+
samples: sampleStore.stats(now),
|
|
791
|
+
schedule,
|
|
792
|
+
accounts: rows,
|
|
793
|
+
};
|
|
794
|
+
},
|
|
795
|
+
}));
|
|
796
|
+
let scheduleService = null;
|
|
797
|
+
let disposeSchedule = null;
|
|
798
|
+
const syncSchedule = () => {
|
|
799
|
+
if (!ctx.runtime.has('schedule')) {
|
|
800
|
+
scheduleService = null;
|
|
801
|
+
disposeSchedule = null;
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
const svc = ctx.get('schedule');
|
|
805
|
+
if (svc === scheduleService)
|
|
806
|
+
return;
|
|
807
|
+
void disposeSchedule?.();
|
|
808
|
+
disposeSchedule = svc.register({
|
|
809
|
+
key: 'keys.quota_sample',
|
|
810
|
+
when: { kind: 'every', minutes: 10 },
|
|
811
|
+
op: 'keys.quota_sample',
|
|
812
|
+
args: {},
|
|
813
|
+
about: '定时采样各账号额度',
|
|
814
|
+
ref: address.page('ops'),
|
|
815
|
+
}, 'plugin:keys');
|
|
816
|
+
scheduleService = svc;
|
|
817
|
+
};
|
|
818
|
+
syncSchedule();
|
|
819
|
+
ctx.hook('host.ready', syncSchedule);
|
|
820
|
+
ctx.on('plugin.changed', ({ name }) => { if (name === 'schedule')
|
|
821
|
+
syncSchedule(); });
|
|
822
|
+
ctx.effect(() => { void disposeSchedule?.(); });
|
|
823
|
+
// Trying a key: the models its endpoint answers with.
|
|
824
|
+
ctx.effect(entry.op({
|
|
825
|
+
name: 'keys.models', about: '向密钥的服务查询可用模型。',
|
|
826
|
+
input: object({ id }, ['id']), http: ['GET', '/api/keys/:id/models'],
|
|
827
|
+
async run({ id }) {
|
|
828
|
+
const item = list().find((one) => one.id === id);
|
|
829
|
+
const key = item && 'protocol' in item ? apiKeyOf(item) : undefined;
|
|
830
|
+
if (!key)
|
|
831
|
+
throw new HttpError(404, '没有这条密钥');
|
|
832
|
+
try {
|
|
833
|
+
return { models: await listModels(key) };
|
|
834
|
+
}
|
|
835
|
+
catch (problem) {
|
|
836
|
+
throw new HttpError(400, errorText(problem));
|
|
837
|
+
}
|
|
838
|
+
},
|
|
839
|
+
}));
|
|
840
|
+
},
|
|
841
|
+
};
|
|
842
|
+
}
|