-
Notifications
You must be signed in to change notification settings - Fork 16
feat: add Altimate as a provider via /connect #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
packages/opencode/src/cli/cmd/tui/component/dialog-altimate-login.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| import { createSignal, Show, onMount } from "solid-js" | ||
| import { createStore } from "solid-js/store" | ||
| import { TextareaRenderable, TextAttributes } from "@opentui/core" | ||
| import { useKeyboard } from "@opentui/solid" | ||
| import { useDialog } from "@tui/ui/dialog" | ||
| import { useSDK } from "../context/sdk" | ||
| import { useSync } from "@tui/context/sync" | ||
| import { useTheme } from "../context/theme" | ||
| import { AltimateApi } from "@/altimate/api/client" | ||
| import { Filesystem } from "@/util/filesystem" | ||
|
|
||
| export function DialogAltimateLogin() { | ||
| const dialog = useDialog() | ||
| const sdk = useSDK() | ||
| const sync = useSync() | ||
| const { theme } = useTheme() | ||
| const [error, setError] = createSignal("") | ||
| const [validating, setValidating] = createSignal(false) | ||
| const [store, setStore] = createStore({ | ||
| active: "instance" as "instance" | "key" | "url", | ||
| }) | ||
|
|
||
| let instanceRef: TextareaRenderable | ||
| let keyRef: TextareaRenderable | ||
| let urlRef: TextareaRenderable | ||
|
|
||
| const fields = ["instance", "key", "url"] as const | ||
|
|
||
| function focusActive() { | ||
| setTimeout(() => { | ||
| const ref = { instance: instanceRef, key: keyRef, url: urlRef }[store.active] | ||
| if (ref && !ref.isDestroyed) ref.focus() | ||
| }, 1) | ||
| } | ||
|
|
||
| useKeyboard((evt) => { | ||
| if (evt.name === "tab") { | ||
| const idx = fields.indexOf(store.active) | ||
| const next = fields[(idx + 1) % fields.length] | ||
| setStore("active", next) | ||
| focusActive() | ||
| evt.preventDefault() | ||
| } | ||
| if (evt.name === "return") { | ||
| if (validating()) return | ||
| void submit().catch((e) => setError(`Unexpected error: ${e?.message ?? e}`)) | ||
| } | ||
| }) | ||
|
|
||
| onMount(() => { | ||
| dialog.setSize("medium") | ||
| focusActive() | ||
| }) | ||
|
|
||
| async function submit() { | ||
| const instance = instanceRef.plainText.trim() | ||
| const key = keyRef.plainText.trim() | ||
| const url = urlRef.plainText.trim().replace(/\/+$/, "") | ||
|
|
||
| if (!instance) { | ||
| setError("Instance name is required") | ||
| setStore("active", "instance") | ||
| focusActive() | ||
| return | ||
| } | ||
| if (!key) { | ||
| setError("API key is required") | ||
| setStore("active", "key") | ||
| focusActive() | ||
| return | ||
| } | ||
| if (!url) { | ||
| setError("URL is required") | ||
| setStore("active", "url") | ||
| focusActive() | ||
| return | ||
| } | ||
|
|
||
| setError("") | ||
| setValidating(true) | ||
| try { | ||
| const res = await fetch(`${url}/auth_health`, { | ||
| method: "GET", | ||
| headers: { | ||
| Authorization: `Bearer ${key}`, | ||
| "x-tenant": instance, | ||
| }, | ||
| }) | ||
| if (!res.ok) { | ||
| setError("Invalid credentials — check your instance name, API key, and URL") | ||
| setValidating(false) | ||
| return | ||
| } | ||
| const data = await res.json() | ||
| if (data.status !== "auth_valid") { | ||
| setError("Unexpected response from server") | ||
| setValidating(false) | ||
| return | ||
| } | ||
| } catch { | ||
| setError(`Connection failed — could not reach ${url}`) | ||
| setValidating(false) | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| const creds = { | ||
| altimateUrl: url, | ||
| altimateInstanceName: instance, | ||
| altimateApiKey: key, | ||
| } | ||
| await Filesystem.writeJson(AltimateApi.credentialsPath(), creds, 0o600) | ||
| await sdk.client.instance.dispose() | ||
| await sync.bootstrap() | ||
| dialog.clear() | ||
| } finally { | ||
| setValidating(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <box paddingLeft={2} paddingRight={2} gap={1}> | ||
| <box flexDirection="row" justifyContent="space-between"> | ||
| <text attributes={TextAttributes.BOLD} fg={theme.text}> | ||
| Connect to Altimate | ||
| </text> | ||
| <text fg={theme.textMuted} onMouseUp={() => dialog.clear()}> | ||
| esc | ||
| </text> | ||
| </box> | ||
|
|
||
| <text fg={theme.textMuted}>Find these in Settings > API Keys in your Altimate dashboard</text> | ||
|
|
||
| <box> | ||
| <text fg={store.active === "instance" ? theme.text : theme.textMuted}>Instance Name:</text> | ||
| <text fg={theme.textMuted}> From your URL: https://<instance>.app.myaltimate.com</text> | ||
| <textarea | ||
| height={3} | ||
| ref={(val: TextareaRenderable) => (instanceRef = val)} | ||
| placeholder="your-instance" | ||
| textColor={theme.text} | ||
| focusedTextColor={theme.text} | ||
| cursorColor={theme.text} | ||
| onMouseUp={() => { | ||
| setStore("active", "instance") | ||
| focusActive() | ||
| }} | ||
| /> | ||
| </box> | ||
|
|
||
| <box> | ||
| <text fg={store.active === "key" ? theme.text : theme.textMuted}>API Key:</text> | ||
| <text fg={theme.textMuted}> Settings > API Keys > Copy</text> | ||
| <textarea | ||
| height={3} | ||
| ref={(val: TextareaRenderable) => (keyRef = val)} | ||
| placeholder="your-api-key" | ||
| textColor={theme.text} | ||
| focusedTextColor={theme.text} | ||
| cursorColor={theme.text} | ||
| onMouseUp={() => { | ||
| setStore("active", "key") | ||
| focusActive() | ||
| }} | ||
| /> | ||
| </box> | ||
|
|
||
| <box> | ||
| <text fg={store.active === "url" ? theme.text : theme.textMuted}>URL:</text> | ||
| <textarea | ||
| height={3} | ||
| ref={(val: TextareaRenderable) => (urlRef = val)} | ||
| initialValue="https://api.myaltimate.com" | ||
| placeholder="https://api.myaltimate.com" | ||
| textColor={theme.text} | ||
| focusedTextColor={theme.text} | ||
| cursorColor={theme.text} | ||
| onMouseUp={() => { | ||
| setStore("active", "url") | ||
| focusActive() | ||
| }} | ||
| /> | ||
| </box> | ||
|
|
||
| <Show when={error()}> | ||
| <text fg={theme.error}>{error()}</text> | ||
| </Show> | ||
| <Show when={validating()}> | ||
| <text fg={theme.textMuted}>Validating credentials...</text> | ||
| </Show> | ||
|
|
||
| <text fg={theme.textMuted} paddingBottom={1}> | ||
| <span style={{ fg: theme.text }}>tab</span> next field{" "} | ||
| <span style={{ fg: theme.text }}>enter</span> submit | ||
| </text> | ||
| </box> | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.