@kabmo-labs/kabmo 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -421
- package/dist/client/index.html +1 -1
- package/dist/client/index.js +426 -413
- package/dist/client/static/js/{index-amss8pge.js → index-0smc9gm2.js} +1123 -747
- package/dist/client/static/js/index-shexbk5f.css +1 -0
- package/marketplace/marketplace.json +13 -3
- package/marketplace/plugins/command-runner/package.json +4 -2
- package/marketplace/plugins/git-forge/README.md +219 -0
- package/marketplace/plugins/git-forge/icon.svg +20 -0
- package/marketplace/plugins/git-forge/package.json +56 -0
- package/marketplace/plugins/git-forge/src/app.tsx +82 -0
- package/marketplace/plugins/git-forge/src/command.test.ts +181 -0
- package/marketplace/plugins/git-forge/src/command.ts +119 -0
- package/marketplace/plugins/git-forge/src/header.tsx +87 -0
- package/marketplace/plugins/git-forge/src/panel/Badge.tsx +76 -0
- package/marketplace/plugins/git-forge/src/panel/Change.tsx +150 -0
- package/marketplace/plugins/git-forge/src/panel/CiBadge.tsx +51 -0
- package/marketplace/plugins/git-forge/src/panel/Credential.tsx +107 -0
- package/marketplace/plugins/git-forge/src/panel/Glyph.tsx +70 -0
- package/marketplace/plugins/git-forge/src/panel/Header.tsx +255 -0
- package/marketplace/plugins/git-forge/src/panel/ItemView.tsx +198 -0
- package/marketplace/plugins/git-forge/src/panel/List.tsx +89 -0
- package/marketplace/plugins/git-forge/src/panel/Row.tsx +68 -0
- package/marketplace/plugins/git-forge/src/panel/Sidebar.tsx +94 -0
- package/marketplace/plugins/git-forge/src/panel/Write.tsx +236 -0
- package/marketplace/plugins/git-forge/src/panel/format.ts +78 -0
- package/marketplace/plugins/git-forge/src/panel/index.test.tsx +344 -0
- package/marketplace/plugins/git-forge/src/panel/index.tsx +193 -0
- package/marketplace/plugins/git-forge/src/panel/use-forge.ts +250 -0
- package/marketplace/plugins/git-forge/src/panel/write.test.tsx +269 -0
- package/marketplace/plugins/git-forge/src/protocol.ts +308 -0
- package/marketplace/plugins/git-forge/src/server/adapters.test.ts +274 -0
- package/marketplace/plugins/git-forge/src/server/bitbucket.ts +307 -0
- package/marketplace/plugins/git-forge/src/server/capabilities.test.ts +116 -0
- package/marketplace/plugins/git-forge/src/server/capabilities.ts +105 -0
- package/marketplace/plugins/git-forge/src/server/ci.test.ts +88 -0
- package/marketplace/plugins/git-forge/src/server/ci.ts +118 -0
- package/marketplace/plugins/git-forge/src/server/context.ts +76 -0
- package/marketplace/plugins/git-forge/src/server/diff.test.ts +127 -0
- package/marketplace/plugins/git-forge/src/server/diff.ts +120 -0
- package/marketplace/plugins/git-forge/src/server/filters.test.ts +138 -0
- package/marketplace/plugins/git-forge/src/server/filters.ts +138 -0
- package/marketplace/plugins/git-forge/src/server/forges.test.ts +235 -0
- package/marketplace/plugins/git-forge/src/server/gitea.ts +296 -0
- package/marketplace/plugins/git-forge/src/server/github-extras.ts +92 -0
- package/marketplace/plugins/git-forge/src/server/github.ts +320 -0
- package/marketplace/plugins/git-forge/src/server/gitlab-detail.ts +207 -0
- package/marketplace/plugins/git-forge/src/server/gitlab.test.ts +195 -0
- package/marketplace/plugins/git-forge/src/server/gitlab.ts +176 -0
- package/marketplace/plugins/git-forge/src/server/http.test.ts +270 -0
- package/marketplace/plugins/git-forge/src/server/http.ts +329 -0
- package/marketplace/plugins/git-forge/src/server/map.ts +120 -0
- package/marketplace/plugins/git-forge/src/server/registry.ts +47 -0
- package/marketplace/plugins/git-forge/src/server/remote.test.ts +146 -0
- package/marketplace/plugins/git-forge/src/server/remote.ts +188 -0
- package/marketplace/plugins/git-forge/src/server/scope-probe.test.ts +98 -0
- package/marketplace/plugins/git-forge/src/server/scope-probe.ts +128 -0
- package/marketplace/plugins/git-forge/src/server/scope.ts +66 -0
- package/marketplace/plugins/git-forge/src/server/test-context.ts +67 -0
- package/marketplace/plugins/git-forge/src/server/test-host.ts +51 -0
- package/marketplace/plugins/git-forge/src/server/token.test.ts +107 -0
- package/marketplace/plugins/git-forge/src/server/token.ts +139 -0
- package/marketplace/plugins/git-forge/src/server/write-http.test.ts +127 -0
- package/marketplace/plugins/git-forge/src/server/write-http.ts +110 -0
- package/marketplace/plugins/git-forge/src/server/writes.test.ts +218 -0
- package/marketplace/plugins/git-forge/src/server/writes.ts +313 -0
- package/marketplace/plugins/git-forge/src/server.ts +284 -0
- package/marketplace/plugins/git-forge/tsconfig.json +16 -0
- package/marketplace/plugins/session-info/package.json +4 -2
- package/marketplace/plugins/stats/package.json +4 -2
- package/package.json +6 -1
- package/src/client/components/common/code-surface/blame-hover.ts +188 -0
- package/src/client/components/common/code-surface/blame-panel.test.ts +304 -0
- package/src/client/components/common/code-surface/blame-panel.ts +306 -0
- package/src/client/components/common/code-surface/blame.test.ts +303 -0
- package/src/client/components/common/code-surface/blame.ts +227 -0
- package/src/client/components/common/code-surface/conflict-markers.test.ts +135 -0
- package/src/client/components/common/code-surface/conflict-markers.ts +144 -0
- package/src/client/components/common/code-surface/documents.ts +48 -1
- package/src/client/components/common/code-surface/extensions.test.ts +4 -0
- package/src/client/components/common/code-surface/extensions.ts +44 -0
- package/src/client/components/common/code-surface/fold-stack.test.ts +4 -0
- package/src/client/components/common/code-surface/indent.test.ts +5 -0
- package/src/client/components/common/code-surface/index.tsx +16 -2
- package/src/client/components/common/code-surface/lint-parsers.test.ts +109 -0
- package/src/client/components/common/code-surface/lint-parsers.ts +134 -0
- package/src/client/components/common/code-surface/lint.test.ts +101 -0
- package/src/client/components/common/code-surface/lint.ts +85 -0
- package/src/client/components/common/code-surface/props.ts +17 -0
- package/src/client/components/common/code-surface/surface-failures.test.ts +132 -0
- package/src/client/components/common/code-surface/theme.ts +71 -0
- package/src/client/components/common/header-controls/index.test.tsx +1 -1
- package/src/client/components/common/header-controls/plugin-entry.test.tsx +1 -1
- package/src/client/components/common/header-controls/shell-chrome.test.tsx +1 -1
- package/src/client/components/common/provider-icon/Plate.tsx +88 -0
- package/src/client/components/common/provider-icon/index.tsx +55 -11
- package/src/client/components/layout/session-sidebar/SessionsView.tsx +5 -2
- package/src/client/components/mobile/SessionSidebar.tsx +2 -10
- package/src/client/components/mobile/mobile-right-sidebar/FullEditor.tsx +6 -0
- package/src/client/components/mobile/mobile-right-sidebar/StatusBar.tsx +8 -0
- package/src/client/components/settings/categories/AppearanceSettings.tsx +3 -0
- package/src/client/components/settings/categories/ProviderSettings.tsx +43 -41
- package/src/client/components/settings/categories/appearance-settings/EditorBlameSection.tsx +97 -0
- package/src/client/components/settings/categories/panel-plugins/AvailableList.tsx +106 -54
- package/src/client/components/settings/categories/panel-plugins/GitFreshness.tsx +14 -1
- package/src/client/components/settings/categories/panel-plugins/InstalledList.tsx +39 -150
- package/src/client/components/settings/categories/panel-plugins/Meta.test.tsx +143 -0
- package/src/client/components/settings/categories/panel-plugins/Meta.tsx +157 -0
- package/src/client/components/settings/categories/panel-plugins/Placement.test.tsx +149 -0
- package/src/client/components/settings/categories/panel-plugins/Placement.tsx +99 -0
- package/src/client/components/settings/categories/panel-plugins/PluginCard.tsx +270 -0
- package/src/client/components/settings/categories/panel-plugins/Section.tsx +11 -3
- package/src/client/components/settings/categories/provider-settings/AddModelModal.tsx +25 -0
- package/src/client/components/settings/categories/provider-settings/AuthSection.tsx +135 -80
- package/src/client/components/settings/categories/provider-settings/DeleteProviderModal.tsx +51 -21
- package/src/client/components/settings/categories/provider-settings/EntryAdvanced.tsx +175 -0
- package/src/client/components/settings/categories/provider-settings/EntryModal.tsx +275 -0
- package/src/client/components/settings/categories/provider-settings/Header.tsx +40 -9
- package/src/client/components/settings/categories/provider-settings/LoginModal.tsx +142 -160
- package/src/client/components/settings/categories/provider-settings/LogoutProviderModal.tsx +2 -2
- package/src/client/components/settings/categories/provider-settings/ModelCapabilitiesModal.tsx +84 -46
- package/src/client/components/settings/categories/provider-settings/ModelsList.tsx +217 -102
- package/src/client/components/settings/categories/provider-settings/PresetPicker.tsx +5 -2
- package/src/client/components/settings/categories/provider-settings/SidebarList.plugin.test.ts +3 -3
- package/src/client/components/settings/categories/provider-settings/SidebarList.tsx +158 -102
- package/src/client/components/settings/categories/provider-settings/auth-section.test.tsx +135 -0
- package/src/client/components/settings/categories/provider-settings/groups.test.ts +98 -0
- package/src/client/components/settings/categories/provider-settings/groups.ts +108 -0
- package/src/client/components/settings/categories/provider-settings/layout.test.tsx +325 -0
- package/src/client/components/settings/categories/provider-settings/model-dialogs.test.tsx +135 -0
- package/src/client/components/settings/categories/provider-settings/preset-picker.test.tsx +73 -0
- package/src/client/components/settings/categories/usage-settings/ProviderCard.tsx +22 -2
- package/src/client/components/settings/categories/usage-settings/ProviderDetail.tsx +2 -0
- package/src/client/components/settings/categories/usage-settings/SidebarList.tsx +16 -6
- package/src/client/components/settings/categories/usage-settings/providers.ts +3 -0
- package/src/client/components/settings/categories/usage-settings/usage-and-picker.test.ts +19 -0
- package/src/client/components/settings/categories/usage-settings/usage-icons.test.tsx +143 -0
- package/src/client/components/workspace/chat-timeline/MinimapShortcuts.test.ts +2 -1
- package/src/client/components/workspace/chat-timeline/chat-input/GoalBanner.test.ts +23 -19
- package/src/client/components/workspace/chat-timeline/chat-input/ModeToggles.test.ts +23 -19
- package/src/client/components/workspace/chat-timeline/chat-input/Toolbar.test.ts +20 -18
- package/src/client/components/workspace/chat-timeline/chat-input/compact.test.tsx +19 -6
- package/src/client/components/workspace/chat-timeline/index.tsx +7 -16
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.missing.test.ts +100 -0
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.test.ts +3 -47
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/Wait.tsx +8 -1
- package/src/client/components/workspace/chat-timeline/tool-renderers/panels/wait-test-util.ts +62 -0
- package/src/client/components/workspace/diff-panel/HunkStepper.tsx +62 -0
- package/src/client/components/workspace/diff-panel/MergeDiffView.tsx +29 -0
- package/src/client/components/workspace/diff-panel/Toolbar.tsx +32 -1
- package/src/client/components/workspace/diff-panel/changes.ts +69 -0
- package/src/client/components/workspace/diff-panel/chunks.ts +85 -0
- package/src/client/components/workspace/diff-panel/index.tsx +21 -1
- package/src/client/components/workspace/diff-panel/merge.test.ts +145 -0
- package/src/client/components/workspace/diff-panel/merge.ts +63 -68
- package/src/client/components/workspace/editor/EditorDropOverlay.tsx +6 -1
- package/src/client/components/workspace/editor/FileDocument.tsx +51 -1
- package/src/client/components/workspace/editor/FilePane.tsx +25 -1
- package/src/client/components/workspace/editor/StatusBar.tsx +19 -0
- package/src/client/components/workspace/editor/Toolbar.tsx +29 -5
- package/src/client/components/workspace/editor/toolbar-clusters.test.ts +86 -0
- package/src/client/components/workspace/editor/toolbar-clusters.tsx +48 -16
- package/src/client/components/workspace/editor/viewers/bytes.ts +16 -0
- package/src/client/components/workspace/git-panel/index.test.ts +46 -1
- package/src/client/components/workspace/git-panel/index.tsx +11 -38
- package/src/client/components/workspace/model-dropdown/Item.plugin.test.ts +66 -0
- package/src/client/components/workspace/model-dropdown/Item.tsx +71 -62
- package/src/client/components/workspace/model-dropdown/Section.tsx +31 -14
- package/src/client/components/workspace/model-dropdown/collapsed.ts +72 -0
- package/src/client/components/workspace/model-dropdown/index.tsx +42 -19
- package/src/client/components/workspace/model-dropdown/refresh.test.tsx +260 -0
- package/src/client/components/workspace/model-dropdown/trigger.test.ts +34 -0
- package/src/client/components/workspace/model-dropdown/use-catalog.ts +13 -2
- package/src/client/components/workspace/usage-panel/ProviderSelect.tsx +19 -3
- package/src/client/hooks/chat/omp/folder-auto-expand.ts +113 -0
- package/src/client/hooks/chat/omp/index.ts +20 -0
- package/src/client/hooks/chat/omp/pending-rows.ts +21 -0
- package/src/client/hooks/chat/omp/pending-sessions.test.ts +16 -1
- package/src/client/hooks/chat/omp/session-sidebar-controller.test.ts +102 -0
- package/src/client/hooks/chat/omp/session-sidebar-controller.ts +21 -1
- package/src/client/hooks/chat/omp/stream.ts +8 -1
- package/src/client/hooks/chat/timeline/index.ts +1 -1
- package/src/client/hooks/chat/timeline/new-chat-open.ts +60 -0
- package/src/client/hooks/editor/blame-settings.ts +67 -0
- package/src/client/hooks/editor/blame.test.ts +272 -0
- package/src/client/hooks/editor/blame.ts +232 -0
- package/src/client/hooks/editor/file-editor-types.ts +23 -0
- package/src/client/hooks/editor/save-file.test.ts +20 -0
- package/src/client/hooks/editor/use-editor-drop.test-util.ts +198 -0
- package/src/client/hooks/editor/use-editor-drop.test.ts +70 -175
- package/src/client/hooks/editor/use-editor-drop.ts +19 -36
- package/src/client/hooks/editor/use-file-editor.ts +7 -1
- package/src/client/hooks/settings/providers/actions.ts +90 -194
- package/src/client/hooks/settings/providers/api.ts +7 -1
- package/src/client/hooks/settings/providers/index.ts +171 -59
- package/src/client/hooks/settings/providers/login.test.ts +79 -0
- package/src/client/hooks/settings/providers/login.ts +276 -0
- package/src/client/hooks/settings/providers/messages.ts +28 -0
- package/src/client/hooks/settings/providers/model-actions.ts +178 -0
- package/src/client/hooks/ui/editor-error-toast.ts +47 -0
- package/src/client/hooks/ui/open-file.ts +7 -0
- package/src/client/hooks/ui/toasts.ts +7 -0
- package/src/client/hooks/workspace/file-tabs.ts +4 -1
- package/src/client/hooks/workspace/folder-expansion.test.ts +62 -0
- package/src/client/hooks/workspace/folder-expansion.ts +33 -0
- package/src/client/hooks/workspace/git-action-outcome.ts +86 -0
- package/src/client/hooks/workspace/git-actions.ts +29 -2
- package/src/client/hooks/workspace/plugin-contributions.test.ts +117 -0
- package/src/client/hooks/workspace/plugin-contributions.ts +146 -0
- package/src/client/lib/editor-errors.test.ts +85 -0
- package/src/client/lib/editor-errors.ts +71 -0
- package/src/client/lib/fs/resolve-open.test.ts +125 -0
- package/src/client/lib/fs/resolve-open.ts +90 -0
- package/src/client/lib/git/action-message.test.ts +82 -0
- package/src/client/lib/git/action-message.ts +160 -0
- package/src/client/lib/launch/device-files.test.ts +160 -0
- package/src/client/lib/launch/device-files.ts +197 -0
- package/src/client/lib/native/open-paths.test.ts +140 -0
- package/src/client/lib/native/open-paths.ts +157 -0
- package/src/client/lib/plugins/bootstrap.test.ts +1 -1
- package/src/client/main.tsx +20 -0
- package/src/server/db.server.test.ts +39 -0
- package/src/server/db.server.ts +21 -2
- package/src/server/lib/db/schema.ts +2 -0
- package/src/server/lib/fs/external-roots.test.ts +100 -0
- package/src/server/lib/fs/external-roots.ts +101 -0
- package/src/server/lib/fs/git-actions-branch.ts +15 -2
- package/src/server/lib/fs/git-actions-shared.ts +24 -0
- package/src/server/lib/fs/git-actions-sync.test.ts +18 -7
- package/src/server/lib/fs/git-actions-worktree.ts +5 -1
- package/src/server/lib/fs/git-blame.test.ts +206 -0
- package/src/server/lib/fs/git-blame.ts +189 -0
- package/src/server/lib/fs/root.ts +14 -0
- package/src/server/lib/http/same-origin.ts +28 -7
- package/src/server/lib/models/catalog.server.ts +230 -0
- package/src/server/lib/models/catalog.test.ts +83 -0
- package/src/server/lib/models/credential-origin.server.ts +108 -0
- package/src/server/lib/models/credential-origin.test.ts +132 -0
- package/src/server/lib/models/provider-icon-fetch.ts +115 -0
- package/src/server/lib/models/provider-icon-parse.test.ts +134 -0
- package/src/server/lib/models/provider-icon-parse.ts +139 -0
- package/src/server/lib/models/provider-icon-store.server.ts +118 -0
- package/src/server/lib/models/provider-icon-store.test.ts +107 -0
- package/src/server/lib/models/provider-icon.server.ts +209 -0
- package/src/server/lib/models/provider-items.server.ts +211 -37
- package/src/server/lib/models/provider-items.test.ts +201 -0
- package/src/server/lib/models/provider-registry.server.test.ts +27 -9
- package/src/server/lib/models/provider-registry.server.ts +160 -67
- package/src/server/lib/models/registry.server.test.ts +3 -3
- package/src/server/lib/models/registry.server.ts +8 -0
- package/src/server/lib/omp/config/definitions.test.ts +5 -4
- package/src/server/lib/omp/config/models-config.ts +42 -0
- package/src/server/lib/omp/config/provider-entry.test.ts +205 -0
- package/src/server/lib/omp/config/provider-entry.ts +197 -0
- package/src/server/lib/omp/config/provider-seeds.ts +10 -0
- package/src/server/lib/omp/core/models-json.ts +69 -0
- package/src/server/lib/omp/session/messages.ts +17 -3
- package/src/server/lib/omp/session/sidebar-data.canonical.test.ts +13 -3
- package/src/server/lib/panels/bundled-catalog.server.ts +7 -0
- package/src/server/lib/panels/catalog-offers.server.ts +68 -0
- package/src/server/lib/panels/git-update.server.test.ts +14 -0
- package/src/server/lib/panels/git-update.server.ts +63 -1
- package/src/server/lib/panels/manifest.test.ts +65 -0
- package/src/server/lib/panels/manifest.ts +55 -1
- package/src/server/lib/panels/offers.server.test.ts +191 -0
- package/src/server/lib/panels/offers.server.ts +108 -0
- package/src/server/lib/panels/registry.server.test.ts +35 -1
- package/src/server/lib/panels/registry.server.ts +51 -142
- package/src/server/lib/panels/scan.server.ts +162 -0
- package/src/server/lib/route-adapter.ts +13 -2
- package/src/server/lib/tray/tray.test.ts +2 -1
- package/src/server/lib/usage/providers.server.ts +4 -1
- package/src/server/routes/chat/session.ts +7 -1
- package/src/server/routes/fs/git-blame-loader.test.ts +146 -0
- package/src/server/routes/fs/git.ts +17 -0
- package/src/server/routes/fs/grant.test.ts +135 -0
- package/src/server/routes/fs/grant.ts +79 -0
- package/src/server/routes/fs/index.ts +2 -0
- package/src/server/routes/fs/scope.ts +34 -17
- package/src/server/routes/omp/login.ts +41 -14
- package/src/server/routes/omp/routes.test.ts +23 -10
- package/src/server/routes/settings/index.ts +8 -0
- package/src/server/routes/settings/provider-entry.ts +108 -0
- package/src/server/routes/settings/provider-icon.ts +99 -0
- package/src/server/routes/settings/provider-model.ts +4 -1
- package/src/server/routes/settings/provider-refresh.ts +87 -0
- package/src/server/routes/settings/providers.ts +31 -2
- package/src/shared/lib/chat/omp/fold.test.ts +34 -0
- package/src/shared/lib/chat/omp/tool-frames.test.ts +20 -0
- package/src/shared/lib/chat/omp/tool-frames.ts +5 -1
- package/src/shared/lib/chat/omp/tool-results.ts +29 -3
- package/src/shared/lib/chat/tool/wait-facts.ts +4 -0
- package/src/shared/lib/fs/git-blame.test.ts +266 -0
- package/src/shared/lib/fs/git-blame.ts +246 -0
- package/src/shared/lib/git/url.ts +18 -0
- package/src/shared/lib/markdown/sanitize.test.ts +57 -0
- package/src/shared/lib/markdown/sanitize.ts +30 -7
- package/src/shared/lib/models/provider/connection.ts +135 -0
- package/src/shared/lib/models/provider/dialect.test.ts +4 -2
- package/src/shared/lib/models/provider/dialect.ts +76 -6
- package/src/shared/lib/models/provider/models.ts +2 -0
- package/src/shared/lib/models/provider/origin.ts +94 -0
- package/src/shared/lib/models/provider/provider-ui.test.ts +7 -0
- package/src/shared/lib/models/server-cache.ts +8 -3
- package/src/shared/lib/omp/session/messages-map-pending.test.ts +110 -0
- package/src/shared/lib/omp/session/messages-map.ts +34 -2
- package/src/shared/lib/omp/session/prewarm-misc.test.ts +7 -90
- package/src/shared/lib/omp/session/prewarm.test.ts +133 -0
- package/src/shared/lib/omp/session/prewarm.ts +23 -1
- package/src/shared/lib/omp/session/wait.test.ts +28 -0
- package/src/shared/lib/omp/session/wait.ts +38 -3
- package/src/shared/lib/panels/slot-labels.test.ts +61 -0
- package/src/shared/lib/panels/slot-labels.ts +133 -0
- package/src/shared/lib/workspace/panel-ids.ts +11 -0
- package/src/shared/types/fs.ts +6 -0
- package/src/shared/types/index.ts +1 -0
- package/src/shared/types/models.ts +12 -0
- package/src/shared/types/panels-registry.ts +277 -0
- package/src/shared/types/panels.ts +46 -213
- package/src/shared/types/settings/provider.ts +58 -0
- package/src/shared/types/settings/usage.ts +10 -0
- package/src/test-support/pristine-globals.ts +7 -0
- package/dist/client/static/js/index-bq6qeayr.css +0 -1
- package/src/client/components/settings/categories/provider-settings/ReconnectModal.tsx +0 -184
|
@@ -1,39 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* State wiring for Settings → Providers: the merged registry, the selection,
|
|
8
|
+
* the dialog flags, and the actions (`providers/actions.ts`).
|
|
9
|
+
*
|
|
10
|
+
* Two rules this file carries:
|
|
11
|
+
*
|
|
12
|
+
* - the sidebar lists EVERY provider omp knows — including one with no
|
|
13
|
+
* credential, which is the row you click to sign it in. omp's own `/models`
|
|
14
|
+
* hub draws them the same way, and filtering them out made an OAuth provider
|
|
15
|
+
* that had never been signed in unreachable from the panel that signs it in;
|
|
16
|
+
* - `disabled` is kept separate from `status`, because a provider hidden on
|
|
17
|
+
* purpose is not the same as one whose credential is missing, and the switch
|
|
18
|
+
* that hid it has to stay reachable.
|
|
19
|
+
*/
|
|
20
|
+
|
|
1
21
|
import { useEffect, useMemo, useState } from 'preact/hooks';
|
|
2
|
-
import type { PresetProviderOption, ProviderItem
|
|
3
|
-
import {
|
|
4
|
-
import { buildAvailableProviderPresets } from '@/shared/lib/models/provider/presets';
|
|
22
|
+
import type { PresetProviderOption, ProviderItem } from '@/shared/types';
|
|
23
|
+
import { buildAvailableProviderPresets, configuredProviderSlugs } from '@/shared/lib/models/provider/presets';
|
|
5
24
|
import { useToasts } from '@/client/hooks/ui/toasts';
|
|
6
|
-
import { loadProvidersFromApi } from '@/client/hooks/settings/providers/api';
|
|
7
25
|
import { createProviderActions } from '@/client/hooks/settings/providers/actions';
|
|
26
|
+
import { createModelActions } from '@/client/hooks/settings/providers/model-actions';
|
|
27
|
+
import { loadProvidersFromApi } from '@/client/hooks/settings/providers/api';
|
|
28
|
+
import { refreshProviderIcon } from '@/shared/lib/models/provider/connection';
|
|
29
|
+
import { subscribeModelsUpdated } from '@/shared/lib/models/client';
|
|
8
30
|
|
|
9
|
-
interface UseProviderSettingsOptions {
|
|
31
|
+
export interface UseProviderSettingsOptions {
|
|
10
32
|
autoOpenAdd?: boolean;
|
|
11
33
|
onAddModalClose?: () => void;
|
|
12
34
|
}
|
|
13
35
|
|
|
14
|
-
/**
|
|
15
|
-
* State wiring behind Settings → Providers.
|
|
16
|
-
*
|
|
17
|
-
* The mutations themselves live in `providers/actions.ts`, which receives this
|
|
18
|
-
* hook's setters as one `deps` record — the split keeps both files under the
|
|
19
|
-
* repo's size ceiling without changing closure semantics, since every action
|
|
20
|
-
* still closes over the same state.
|
|
21
|
-
*
|
|
22
|
-
* Every mutation lands in one of two stores: Kabmo-local overlay (per-model
|
|
23
|
-
* visibility and sampling config) or omp's own config.yml `disabledProviders`
|
|
24
|
-
* (connect / disconnect). Disconnect has to use the latter — the overlay is
|
|
25
|
-
* re-merged away on load, which is why a disconnected provider used to come back
|
|
26
|
-
* as connected.
|
|
27
|
-
*/
|
|
28
36
|
export function useProviderSettings({
|
|
29
37
|
autoOpenAdd = false,
|
|
30
38
|
onAddModalClose,
|
|
31
39
|
}: UseProviderSettingsOptions = {}) {
|
|
32
40
|
const [providers, setProviders] = useState<ProviderItem[]>([]);
|
|
33
|
-
const [presetProviders, setPresetProviders] = useState<PresetProviderOption[]>(
|
|
34
|
-
const [selectedProviderId, setSelectedProviderId] = useState<string>('
|
|
35
|
-
const [
|
|
41
|
+
const [presetProviders, setPresetProviders] = useState<PresetProviderOption[]>([]);
|
|
42
|
+
const [selectedProviderId, setSelectedProviderId] = useState<string>('');
|
|
43
|
+
const [catalogError, setCatalogError] = useState<string | null>(null);
|
|
44
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
45
|
+
const [isRefreshingList, setIsRefreshingList] = useState(false);
|
|
36
46
|
const [isFetchingModels, setIsFetchingModels] = useState(false);
|
|
47
|
+
const [isRefreshingIcon, setIsRefreshingIcon] = useState(false);
|
|
37
48
|
const { toasts, pushToast, dismissToast } = useToasts();
|
|
38
49
|
|
|
39
50
|
const [isAddModalOpen, setIsAddModalOpen] = useState(autoOpenAdd);
|
|
@@ -41,10 +52,10 @@ export function useProviderSettings({
|
|
|
41
52
|
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
|
42
53
|
const [isLogoutModalOpen, setIsLogoutModalOpen] = useState(false);
|
|
43
54
|
const [isDeletingProvider, setIsDeletingProvider] = useState(false);
|
|
44
|
-
const [
|
|
55
|
+
const [isEntryModalOpen, setIsEntryModalOpen] = useState(false);
|
|
45
56
|
const [isLoginModalOpen, setIsLoginModalOpen] = useState(false);
|
|
46
|
-
const [configModel, setConfigModel] = useState<
|
|
47
|
-
const [capabilitiesModel, setCapabilitiesModel] = useState<
|
|
57
|
+
const [configModel, setConfigModel] = useState<ProviderItem['models'][number] | null>(null);
|
|
58
|
+
const [capabilitiesModel, setCapabilitiesModel] = useState<ProviderItem['models'][number] | null>(null);
|
|
48
59
|
|
|
49
60
|
useEffect(() => {
|
|
50
61
|
if (autoOpenAdd) setIsAddModalOpen(true);
|
|
@@ -53,35 +64,59 @@ export function useProviderSettings({
|
|
|
53
64
|
useEffect(() => {
|
|
54
65
|
let active = true;
|
|
55
66
|
void loadProvidersFromApi().then((data) => {
|
|
56
|
-
if (!active
|
|
67
|
+
if (!active) return;
|
|
68
|
+
setIsLoading(false);
|
|
69
|
+
if (!data) return;
|
|
57
70
|
setProviders(data.providers);
|
|
71
|
+
setPresetProviders(data.presetProviders ?? []);
|
|
72
|
+
setCatalogError(data.catalogError ?? null);
|
|
58
73
|
const connected = data.providers.filter((provider) => provider.status === 'connected');
|
|
59
|
-
setSelectedProviderId(connected
|
|
60
|
-
if (data.presetProviders) setPresetProviders(data.presetProviders);
|
|
74
|
+
setSelectedProviderId(connected[0]?.id ?? data.providers[0]?.id ?? '');
|
|
61
75
|
});
|
|
62
76
|
return () => { active = false; };
|
|
63
77
|
}, []);
|
|
64
78
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
/**
|
|
80
|
+
* A provider's web icon is fetched in the BACKGROUND (the registry never makes
|
|
81
|
+
* a request wait on a third-party host), and the server republishes the
|
|
82
|
+
* `models` topic when it stored one. Without this subscription the icons
|
|
83
|
+
* arrived only on the next manual open — the panel had already drawn its
|
|
84
|
+
* initials fallbacks.
|
|
85
|
+
*
|
|
86
|
+
* Debounced: the pass stores several icons in a row, and each publish is one
|
|
87
|
+
* full registry read.
|
|
88
|
+
*/
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
91
|
+
const unsubscribe = subscribeModelsUpdated(() => {
|
|
92
|
+
if (timer) clearTimeout(timer);
|
|
93
|
+
timer = setTimeout(() => {
|
|
94
|
+
timer = null;
|
|
95
|
+
void loadProvidersFromApi().then((data) => {
|
|
96
|
+
if (data) setProviders(data.providers);
|
|
97
|
+
});
|
|
98
|
+
}, 400);
|
|
99
|
+
});
|
|
100
|
+
return () => {
|
|
101
|
+
unsubscribe();
|
|
102
|
+
if (timer) clearTimeout(timer);
|
|
103
|
+
};
|
|
104
|
+
}, []);
|
|
105
|
+
|
|
74
106
|
const availablePresetProviders = useMemo(
|
|
75
107
|
() => buildAvailableProviderPresets(providers, presetProviders),
|
|
76
108
|
[providers, presetProviders],
|
|
77
109
|
);
|
|
78
|
-
const
|
|
110
|
+
const existingSlugs = useMemo(() => configuredProviderSlugs(providers), [providers]);
|
|
111
|
+
// The selection is resolved against the FULL list: a provider with no
|
|
112
|
+
// credential is selectable, because its detail pane is where the sign-in
|
|
113
|
+
// control lives.
|
|
114
|
+
const selectedProvider = providers.find((provider) => provider.id === selectedProviderId)
|
|
115
|
+
?? providers[0];
|
|
79
116
|
|
|
80
|
-
const
|
|
117
|
+
const providerActions = createProviderActions({
|
|
81
118
|
providers,
|
|
82
119
|
selectedProvider,
|
|
83
|
-
connectedProviders,
|
|
84
|
-
isFetchingModels,
|
|
85
120
|
isDeletingProvider,
|
|
86
121
|
pushToast,
|
|
87
122
|
setProviders,
|
|
@@ -89,17 +124,21 @@ export function useProviderSettings({
|
|
|
89
124
|
setIsAddModalOpen,
|
|
90
125
|
setIsDeleteModalOpen,
|
|
91
126
|
setIsDeletingProvider,
|
|
92
|
-
|
|
127
|
+
openLogin: () => setIsLoginModalOpen(true),
|
|
128
|
+
openEntryEditor: () => setIsEntryModalOpen(true),
|
|
93
129
|
...(onAddModalClose ? { onAddModalClose } : {}),
|
|
94
130
|
});
|
|
131
|
+
const modelActions = createModelActions({
|
|
132
|
+
providers,
|
|
133
|
+
selectedProvider,
|
|
134
|
+
isFetchingModels,
|
|
135
|
+
pushToast,
|
|
136
|
+
setProviders,
|
|
137
|
+
setIsFetchingModels,
|
|
138
|
+
persistProviders: providerActions.persistProviders,
|
|
139
|
+
reloadProviders: providerActions.reloadProviders,
|
|
140
|
+
});
|
|
95
141
|
|
|
96
|
-
/**
|
|
97
|
-
* After a hand-registered model lands in models.yml, the registry has to be
|
|
98
|
-
* re-read: the new row exists only in omp's file, so nothing in Kabmo's
|
|
99
|
-
* own overlay knows about it yet. Re-fetching the MERGED list (rather than
|
|
100
|
-
* re-saving the overlay, whose response is the overlay alone) is what puts
|
|
101
|
-
* the model in the sidebar count and the chat picker.
|
|
102
|
-
*/
|
|
103
142
|
/**
|
|
104
143
|
* After a stored credential is removed the merged registry has to be re-READ
|
|
105
144
|
* rather than patched: the provider may drop out of the list entirely, and a
|
|
@@ -108,24 +147,96 @@ export function useProviderSettings({
|
|
|
108
147
|
const handleSignedOut = async (message: string) => {
|
|
109
148
|
setIsLogoutModalOpen(false);
|
|
110
149
|
pushToast(message, 'success');
|
|
111
|
-
|
|
112
|
-
|
|
150
|
+
await providerActions.reloadProviders();
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Refetch this provider's icon from its own site.
|
|
155
|
+
*
|
|
156
|
+
* Separate from the registry's background pass: that one only runs when the
|
|
157
|
+
* stored icon is missing or older than a week, and this is the user saying
|
|
158
|
+
* "look again now" after the site changed.
|
|
159
|
+
*/
|
|
160
|
+
const handleRefreshIcon = async () => {
|
|
161
|
+
const target = selectedProvider;
|
|
162
|
+
if (!target || isRefreshingIcon) return;
|
|
163
|
+
if (!target.baseUrl) {
|
|
164
|
+
pushToast('This provider has no endpoint, so there is no site to read an icon from.', 'error');
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
setIsRefreshingIcon(true);
|
|
168
|
+
try {
|
|
169
|
+
const result = await refreshProviderIcon(target.slug, target.baseUrl);
|
|
170
|
+
if (!result.ok) {
|
|
171
|
+
pushToast(result.error || 'No usable icon on that host — keeping the current mark.', 'error');
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const refreshed = await providerActions.reloadProviders();
|
|
175
|
+
const next = refreshed.find((provider) => provider.slug === target.slug);
|
|
176
|
+
if (next) setSelectedProviderId(next.id);
|
|
177
|
+
pushToast(`${target.name}: icon updated from ${target.baseUrl}.`, 'success');
|
|
178
|
+
} finally {
|
|
179
|
+
setIsRefreshingIcon(false);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Re-read the merged provider list.
|
|
185
|
+
*
|
|
186
|
+
* The list is a snapshot of four sources (omp's login providers, models.yml,
|
|
187
|
+
* config.yml's disabled set and the model catalog), and any of them can move
|
|
188
|
+
* without this tab doing anything — a second instance, a hand edit of
|
|
189
|
+
* models.yml, a login finished in another tab. The panel already re-reads on
|
|
190
|
+
* the `models` signal; this is the explicit "now" for everything that signal
|
|
191
|
+
* does not cover, and it is the only control that says what it did.
|
|
192
|
+
*
|
|
193
|
+
* The current selection SURVIVES when it still exists: a refresh that jumped
|
|
194
|
+
* back to the first provider would throw away the row the user was reading.
|
|
195
|
+
*/
|
|
196
|
+
const handleRefreshList = async () => {
|
|
197
|
+
if (isRefreshingList) return;
|
|
198
|
+
setIsRefreshingList(true);
|
|
199
|
+
try {
|
|
200
|
+
const data = await loadProvidersFromApi();
|
|
201
|
+
if (!data) {
|
|
202
|
+
pushToast('Could not read the provider list — omp may still be starting.', 'error');
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
setProviders(data.providers);
|
|
206
|
+
setPresetProviders(data.presetProviders ?? []);
|
|
207
|
+
setCatalogError(data.catalogError ?? null);
|
|
208
|
+
setSelectedProviderId((current) => (
|
|
209
|
+
data.providers.some((provider) => provider.id === current)
|
|
210
|
+
? current
|
|
211
|
+
: (data.providers.find((provider) => provider.status === 'connected')?.id ?? data.providers[0]?.id ?? '')
|
|
212
|
+
));
|
|
213
|
+
const count = data.providers.length;
|
|
214
|
+
pushToast(
|
|
215
|
+
`Provider list refreshed — ${count} provider${count === 1 ? '' : 's'}.`,
|
|
216
|
+
'success',
|
|
217
|
+
);
|
|
218
|
+
} finally {
|
|
219
|
+
setIsRefreshingList(false);
|
|
220
|
+
}
|
|
113
221
|
};
|
|
114
222
|
|
|
115
223
|
const handleModelAdded = async (modelId: string) => {
|
|
116
224
|
pushToast(`${modelId} registered in models.yml.`, 'success');
|
|
117
|
-
|
|
118
|
-
if (data) setProviders(data.providers);
|
|
225
|
+
await providerActions.reloadProviders();
|
|
119
226
|
};
|
|
120
227
|
|
|
121
228
|
return {
|
|
122
229
|
providers,
|
|
123
|
-
connectedProviders,
|
|
124
230
|
availablePresetProviders,
|
|
231
|
+
existingSlugs,
|
|
125
232
|
selectedProvider,
|
|
126
|
-
|
|
127
|
-
|
|
233
|
+
catalogError,
|
|
234
|
+
isLoading,
|
|
235
|
+
isRefreshingList,
|
|
236
|
+
handleRefreshList,
|
|
128
237
|
isFetchingModels,
|
|
238
|
+
isRefreshingIcon,
|
|
239
|
+
handleRefreshIcon,
|
|
129
240
|
toasts,
|
|
130
241
|
pushToast,
|
|
131
242
|
dismissToast,
|
|
@@ -140,8 +251,8 @@ export function useProviderSettings({
|
|
|
140
251
|
isDeletingProvider,
|
|
141
252
|
isAddModalOpen,
|
|
142
253
|
setIsAddModalOpen,
|
|
143
|
-
|
|
144
|
-
|
|
254
|
+
isEntryModalOpen,
|
|
255
|
+
setIsEntryModalOpen,
|
|
145
256
|
isLoginModalOpen,
|
|
146
257
|
setIsLoginModalOpen,
|
|
147
258
|
configModel,
|
|
@@ -149,6 +260,7 @@ export function useProviderSettings({
|
|
|
149
260
|
capabilitiesModel,
|
|
150
261
|
setCapabilitiesModel,
|
|
151
262
|
setSelectedProviderId,
|
|
152
|
-
...
|
|
263
|
+
...providerActions,
|
|
264
|
+
...modelActions,
|
|
153
265
|
};
|
|
154
266
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The login dialog's state rules, and the two shapes that made every device
|
|
8
|
+
* flow fail.
|
|
9
|
+
*
|
|
10
|
+
* `open_url` ALWAYS carries `url`; `launchUrl` is an optional loopback shortcut
|
|
11
|
+
* omp omits for manual-input-only flows and for non-loopback redirect URIs. A
|
|
12
|
+
* client that keys on `launchUrl` cancels the login the moment it is absent —
|
|
13
|
+
* and `instructions` is the only place a device flow says which code to type.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { afterAll, afterEach, beforeEach, describe, expect, test } from 'bun:test';
|
|
17
|
+
import { Window } from 'happy-dom';
|
|
18
|
+
import { canOpenLocally, deviceCodeOf } from '@/client/hooks/settings/providers/login';
|
|
19
|
+
import { installDomGlobals, restoreDomGlobals } from '@/test-support/pristine-globals';
|
|
20
|
+
|
|
21
|
+
describe('deviceCodeOf', () => {
|
|
22
|
+
test('lifts the code out of the instruction sentence', () => {
|
|
23
|
+
expect(deviceCodeOf('Enter code: ABCD-1234')).toBe('ABCD-1234');
|
|
24
|
+
expect(deviceCodeOf('To sign in, open the page and enter the code: WXYZ-9876')).toBe('WXYZ-9876');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('a code keeps its hyphens and digits', () => {
|
|
28
|
+
expect(deviceCodeOf('code: A1B2-C3D4')).toBe('A1B2-C3D4');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('prose without a code yields nothing', () => {
|
|
32
|
+
expect(deviceCodeOf('Waiting for the browser…')).toBeNull();
|
|
33
|
+
expect(deviceCodeOf('')).toBeNull();
|
|
34
|
+
expect(deviceCodeOf(null)).toBeNull();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('canOpenLocally', () => {
|
|
39
|
+
beforeEach(() => installDomGlobals(new Window({ url: 'http://127.0.0.1:3000/settings' })));
|
|
40
|
+
afterEach(() => restoreDomGlobals());
|
|
41
|
+
afterAll(() => restoreDomGlobals());
|
|
42
|
+
|
|
43
|
+
/** The console's own host, which decides whether a loopback URL is usable. */
|
|
44
|
+
function setHost(host: string): void {
|
|
45
|
+
Object.defineProperty(window.location, 'hostname', { value: host, configurable: true });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
test('a public URL is always openable', () => {
|
|
50
|
+
setHost('kabmo.example.com');
|
|
51
|
+
expect(canOpenLocally('https://github.com/login/device')).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('a loopback URL is openable only from a loopback console', () => {
|
|
55
|
+
// omp's callback server runs on the SERVER, so its launch URL is
|
|
56
|
+
// 127.0.0.1 there. Opening it from a console on another machine reaches the
|
|
57
|
+
// reader's localhost — a dead tab that looks like a broken login.
|
|
58
|
+
setHost('127.0.0.1');
|
|
59
|
+
expect(canOpenLocally('http://127.0.0.1:54545/launch')).toBe(true);
|
|
60
|
+
setHost('kabmo.example.com');
|
|
61
|
+
expect(canOpenLocally('http://127.0.0.1:54545/launch')).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('localhost and its subdomains count as loopback', () => {
|
|
65
|
+
setHost('app.localhost');
|
|
66
|
+
expect(canOpenLocally('http://localhost:3000/launch')).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('a non-http scheme is left to the browser', () => {
|
|
70
|
+
setHost('kabmo.example.com');
|
|
71
|
+
expect(canOpenLocally('vscode://callback?code=abc')).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('an unparsable value is not opened', () => {
|
|
75
|
+
setHost('127.0.0.1');
|
|
76
|
+
expect(canOpenLocally('not a url')).toBe(false);
|
|
77
|
+
expect(canOpenLocally(null)).toBe(false);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One provider sign-in flow, as the dialog sees it.
|
|
8
|
+
*
|
|
9
|
+
* omp's `login` command answers with `extension_ui_request` frames on a
|
|
10
|
+
* throwaway RPC child (`POST /api/omp/login`):
|
|
11
|
+
*
|
|
12
|
+
* - `open_url` — the authorization URL. `url` is ALWAYS present; `launchUrl` is
|
|
13
|
+
* an optional loopback shortcut omp omits for manual-input-only flows and for
|
|
14
|
+
* non-loopback redirect URIs. Keying on `launchUrl` cancelled the login the
|
|
15
|
+
* moment it was absent, which is what device flows send.
|
|
16
|
+
* - `input` — a pasted authorization code, or a device code the user has to
|
|
17
|
+
* type on another machine.
|
|
18
|
+
* - `notify` — progress. It must be ACKNOWLEDGED, or omp waits on it.
|
|
19
|
+
*
|
|
20
|
+
* `instructions` is the only place a device flow says WHICH code to type, so it
|
|
21
|
+
* is part of the state rather than a tooltip.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
|
25
|
+
|
|
26
|
+
export interface LoginUiFrame {
|
|
27
|
+
id: string;
|
|
28
|
+
method?: string;
|
|
29
|
+
title?: string;
|
|
30
|
+
message?: string;
|
|
31
|
+
url?: string;
|
|
32
|
+
launchUrl?: string;
|
|
33
|
+
instructions?: string;
|
|
34
|
+
placeholder?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type LoginStatus = 'connecting' | 'waiting' | 'success' | 'failed';
|
|
38
|
+
|
|
39
|
+
export interface LoginFlowState {
|
|
40
|
+
status: LoginStatus;
|
|
41
|
+
/** The authorization URL omp asked us to open (always present on `open_url`). */
|
|
42
|
+
authUrl: string | null;
|
|
43
|
+
/** omp's loopback shortcut, when it offered one. */
|
|
44
|
+
launchUrl: string | null;
|
|
45
|
+
/** Device-flow text (`Enter code: ABCD-1234`), when the provider sends one. */
|
|
46
|
+
instructions: string | null;
|
|
47
|
+
prompt: LoginUiFrame | null;
|
|
48
|
+
notices: string[];
|
|
49
|
+
error: string | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const INITIAL: LoginFlowState = {
|
|
53
|
+
status: 'connecting',
|
|
54
|
+
authUrl: null,
|
|
55
|
+
launchUrl: null,
|
|
56
|
+
instructions: null,
|
|
57
|
+
prompt: null,
|
|
58
|
+
notices: [],
|
|
59
|
+
error: null,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The device code inside omp's instructions, when there is one.
|
|
64
|
+
*
|
|
65
|
+
* Mirrors omp's own extraction (`login-dialog.ts`): the lead-in and the code.
|
|
66
|
+
* A code is what a user copies to another device, so it is lifted out of the
|
|
67
|
+
* sentence rather than left buried in it.
|
|
68
|
+
*/
|
|
69
|
+
export function deviceCodeOf(instructions: string | null): string | null {
|
|
70
|
+
if (!instructions) return null;
|
|
71
|
+
const match = /^(.*\bcode:?\s+)([A-Za-z0-9][A-Za-z0-9-]{3,})\s*$/s.exec(instructions.trim());
|
|
72
|
+
return match?.[2] ?? null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A host only the machine running it can reach. */
|
|
76
|
+
function isLoopbackHost(host: string): boolean {
|
|
77
|
+
const bare = host.replace(/^\[|\]$/g, '');
|
|
78
|
+
return bare === 'localhost' || bare === '127.0.0.1' || bare === '::1' || bare.endsWith('.localhost');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Whether this browser can usefully open `url`.
|
|
83
|
+
*
|
|
84
|
+
* omp's callback server runs on the SERVER, so its `launchUrl` is
|
|
85
|
+
* `http://127.0.0.1:<port>/launch`. Opening that from a console served on
|
|
86
|
+
* another machine reaches the READER's localhost — a dead tab that looks like
|
|
87
|
+
* a broken login. The same address IS correct when the console itself is on
|
|
88
|
+
* loopback (the usual local install), which is why the page's own host decides
|
|
89
|
+
* rather than a hard-coded refusal.
|
|
90
|
+
*/
|
|
91
|
+
export function canOpenLocally(url: string | null): boolean {
|
|
92
|
+
if (!url) return false;
|
|
93
|
+
let parsed: URL;
|
|
94
|
+
try {
|
|
95
|
+
parsed = new URL(url);
|
|
96
|
+
} catch {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return true;
|
|
100
|
+
if (!isLoopbackHost(parsed.hostname)) return true;
|
|
101
|
+
return typeof window !== 'undefined' && isLoopbackHost(window.location.hostname);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface UseProviderLoginOptions {
|
|
105
|
+
isOpen: boolean;
|
|
106
|
+
providerId: string;
|
|
107
|
+
onAuthenticated: () => void;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Drive one login flow while `isOpen`; abort it when the dialog closes. */
|
|
111
|
+
export function useProviderLogin({ isOpen, providerId, onAuthenticated }: UseProviderLoginOptions) {
|
|
112
|
+
const [state, setState] = useState<LoginFlowState>(INITIAL);
|
|
113
|
+
const [inputValue, setInputValue] = useState('');
|
|
114
|
+
const respondRef = useRef<((payload: Record<string, unknown>) => void) | null>(null);
|
|
115
|
+
// Read through a ref: the parent recreates this handler on every render, and
|
|
116
|
+
// a changing dependency would abort + restart the login stream mid-flow.
|
|
117
|
+
const onAuthenticatedRef = useRef(onAuthenticated);
|
|
118
|
+
onAuthenticatedRef.current = onAuthenticated;
|
|
119
|
+
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (!isOpen) return;
|
|
122
|
+
setState(INITIAL);
|
|
123
|
+
setInputValue('');
|
|
124
|
+
|
|
125
|
+
const controller = new AbortController();
|
|
126
|
+
let loginId: string | null = null;
|
|
127
|
+
const respond = (payload: Record<string, unknown>) => {
|
|
128
|
+
if (!loginId) return;
|
|
129
|
+
// The server routes a frame by `type`, so a reply that omits it falls
|
|
130
|
+
// through to the "start a login" branch and is answered 400 — the reply
|
|
131
|
+
// then never reaches omp and the flow waits forever.
|
|
132
|
+
void fetch('/api/omp/login', {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: { 'Content-Type': 'application/json' },
|
|
135
|
+
body: JSON.stringify({ ...payload, type: 'extension_ui_response', loginId }),
|
|
136
|
+
signal: controller.signal,
|
|
137
|
+
}).catch(() => {});
|
|
138
|
+
};
|
|
139
|
+
respondRef.current = respond;
|
|
140
|
+
|
|
141
|
+
void fetch('/api/omp/login', {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
headers: { 'Content-Type': 'application/json' },
|
|
144
|
+
body: JSON.stringify({ providerId }),
|
|
145
|
+
signal: controller.signal,
|
|
146
|
+
})
|
|
147
|
+
.then(async (response) => {
|
|
148
|
+
if (!response.ok) {
|
|
149
|
+
const body = await response.json().catch(() => null);
|
|
150
|
+
const reason = body && typeof body.error === 'string' ? body.error : `HTTP ${response.status}`;
|
|
151
|
+
throw new Error(reason);
|
|
152
|
+
}
|
|
153
|
+
if (!response.body) throw new Error('Streaming is not supported by this browser');
|
|
154
|
+
const reader = response.body.getReader();
|
|
155
|
+
const decoder = new TextDecoder();
|
|
156
|
+
let buffer = '';
|
|
157
|
+
for (;;) {
|
|
158
|
+
const { done, value } = await reader.read();
|
|
159
|
+
if (done) break;
|
|
160
|
+
buffer += decoder.decode(value, { stream: true });
|
|
161
|
+
const parts = buffer.split('\n\n');
|
|
162
|
+
buffer = parts.pop() ?? '';
|
|
163
|
+
for (const part of parts) {
|
|
164
|
+
const line = part.split('\n').find((entry) => entry.startsWith('data: '));
|
|
165
|
+
if (!line) continue;
|
|
166
|
+
let frame: Record<string, unknown>;
|
|
167
|
+
try {
|
|
168
|
+
frame = JSON.parse(line.slice(6)) as Record<string, unknown>;
|
|
169
|
+
} catch {
|
|
170
|
+
continue; // malformed frame — skip
|
|
171
|
+
}
|
|
172
|
+
const type = frame.type;
|
|
173
|
+
if (type === 'login_started' && typeof frame.loginId === 'string') {
|
|
174
|
+
loginId = frame.loginId;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (type === 'extension_ui_request') {
|
|
178
|
+
const ui: LoginUiFrame = {
|
|
179
|
+
id: typeof frame.id === 'string' ? frame.id : '',
|
|
180
|
+
...(typeof frame.method === 'string' ? { method: frame.method } : {}),
|
|
181
|
+
...(typeof frame.title === 'string' ? { title: frame.title } : {}),
|
|
182
|
+
...(typeof frame.message === 'string' ? { message: frame.message } : {}),
|
|
183
|
+
...(typeof frame.url === 'string' ? { url: frame.url } : {}),
|
|
184
|
+
...(typeof frame.launchUrl === 'string' ? { launchUrl: frame.launchUrl } : {}),
|
|
185
|
+
...(typeof frame.instructions === 'string' ? { instructions: frame.instructions } : {}),
|
|
186
|
+
...(typeof frame.placeholder === 'string' ? { placeholder: frame.placeholder } : {}),
|
|
187
|
+
};
|
|
188
|
+
const method = ui.method;
|
|
189
|
+
if (method === 'open_url') {
|
|
190
|
+
// `url` is the authorization target; `launchUrl` is a shortcut
|
|
191
|
+
// that only exists for a loopback flow on THIS machine.
|
|
192
|
+
const authUrl = ui.url ?? null;
|
|
193
|
+
setState((prev) => ({
|
|
194
|
+
...prev,
|
|
195
|
+
status: 'waiting',
|
|
196
|
+
authUrl,
|
|
197
|
+
launchUrl: ui.launchUrl ?? null,
|
|
198
|
+
instructions: ui.instructions ?? null,
|
|
199
|
+
}));
|
|
200
|
+
// Auto-open only when the target is reachable from here. A
|
|
201
|
+
// server-side loopback URL opened on the user's machine is a
|
|
202
|
+
// dead tab, and a popup blocker eats the attempt silently.
|
|
203
|
+
const target = ui.launchUrl ?? authUrl;
|
|
204
|
+
if (canOpenLocally(target)) {
|
|
205
|
+
window.open(target as string, '_blank', 'noopener');
|
|
206
|
+
}
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (method === 'input') {
|
|
210
|
+
setState((prev) => ({ ...prev, status: 'waiting', prompt: ui }));
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (method === 'notify') {
|
|
214
|
+
const text = typeof ui.title === 'string' ? ui.title : '';
|
|
215
|
+
setState((prev) => ({
|
|
216
|
+
...prev,
|
|
217
|
+
status: 'waiting',
|
|
218
|
+
notices: text ? [...prev.notices, text] : prev.notices,
|
|
219
|
+
}));
|
|
220
|
+
// omp blocks on this acknowledgement; without it the flow
|
|
221
|
+
// stalls with the progress line on screen and nothing behind it.
|
|
222
|
+
respond({ id: ui.id, accepted: true });
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
// Any other dialog method has no answer this dialog can give.
|
|
226
|
+
respond({ id: ui.id, cancelled: true });
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (type === 'login_result') {
|
|
230
|
+
const ok = frame.success === true;
|
|
231
|
+
setState((prev) => ({
|
|
232
|
+
...prev,
|
|
233
|
+
status: ok ? 'success' : 'failed',
|
|
234
|
+
error: ok ? null : (typeof frame.error === 'string' ? frame.error : 'Login failed'),
|
|
235
|
+
}));
|
|
236
|
+
if (ok) onAuthenticatedRef.current();
|
|
237
|
+
controller.abort();
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
})
|
|
243
|
+
.catch((error: unknown) => {
|
|
244
|
+
if (controller.signal.aborted) return;
|
|
245
|
+
setState((prev) => ({
|
|
246
|
+
...prev,
|
|
247
|
+
status: 'failed',
|
|
248
|
+
error: error instanceof Error ? error.message : String(error),
|
|
249
|
+
}));
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
return () => {
|
|
253
|
+
controller.abort();
|
|
254
|
+
respondRef.current = null;
|
|
255
|
+
};
|
|
256
|
+
}, [isOpen, providerId]);
|
|
257
|
+
|
|
258
|
+
const submitInput = useCallback((cancelled: boolean) => {
|
|
259
|
+
const prompt = state.prompt;
|
|
260
|
+
if (!prompt) return;
|
|
261
|
+
respondRef.current?.(cancelled
|
|
262
|
+
? { id: prompt.id, cancelled: true }
|
|
263
|
+
: { id: prompt.id, value: inputValue });
|
|
264
|
+
// The flow is now WAITING on omp, not starting: reporting `connecting` put
|
|
265
|
+
// "Starting the login flow…" over a request that was already answered.
|
|
266
|
+
setState((prev) => ({
|
|
267
|
+
...prev,
|
|
268
|
+
prompt: null,
|
|
269
|
+
status: 'waiting',
|
|
270
|
+
notices: cancelled ? prev.notices : [...prev.notices, 'Sent to omp — waiting for the provider…'],
|
|
271
|
+
}));
|
|
272
|
+
setInputValue('');
|
|
273
|
+
}, [state.prompt, inputValue]);
|
|
274
|
+
|
|
275
|
+
return { state, inputValue, setInputValue, submitInput };
|
|
276
|
+
}
|