Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

feat(web-studio): redesign connection & identity settings (#2896)

Zayn Jarvisc65a74569e83
diff --git a/web-studio/src/hooks/use-app-connection.tsx b/web-studio/src/hooks/use-app-connection.tsx
index 0ae7b8cef..d053c1d2f 100644
--- a/web-studio/src/hooks/use-app-connection.tsx
+++ b/web-studio/src/hooks/use-app-connection.tsx
@@ -122,7 +122,12 @@ function normalizeConnectionDraft(
accountId: connection.accountId.trim(),
adminApiKey: connection.adminApiKey.trim(),
apiKey: connection.apiKey.trim(),
- baseUrl: normalizeBaseUrl(connection.baseUrl),
+ // Keep the URL as typed (whitespace trimmed only). Stripping the trailing
+ // slash here ran on every keystroke and fought the input: typing the "//"
+ // of "http://" kept collapsing back to "http:". Trailing slashes are
+ // stripped where the URL is actually used instead (ovClient.setOptions,
+ // detectServerMode, detectConnectionRole, and the admin client).
+ baseUrl: connection.baseUrl.trim(),
userId: connection.userId.trim(),
}
}
@@ -175,7 +180,10 @@ async function detectConnectionRole(
headers['X-API-Key'] = apiKey
}
- const response = await fetch(`${connection.baseUrl}/health`, { headers })
+ const response = await fetch(
+ `${normalizeBaseUrl(connection.baseUrl)}/health`,
+ { headers },
+ )
if (!response.ok) {
return 'unknown'
}
diff --git a/web-studio/src/i18n/locales/en.ts b/web-studio/src/i18n/locales/en.ts
index 36881fa74..e6e4d18a8 100644
--- a/web-studio/src/i18n/locales/en.ts
+++ b/web-studio/src/i18n/locales/en.ts
@@ -111,17 +111,21 @@ const en = {
regenerate: 'Regenerate',
save: 'Save',
use: 'Use',
- useForData: 'Use as user key',
+ useForData: 'Use as User API Key',
},
connection: {
accountListLimited:
'This key cannot list all accounts, but it can still manage the selected account if it has account-admin access.',
- adminError: 'Could not load admin identities: {{message}}',
+ adminError: 'Could not verify the Root API Key: {{message}}',
description:
- 'Use a user API key for tenant data APIs and an optional root or account-admin key for control APIs.',
+ 'Use a User API Key for tenant data APIs and an optional Root or account-admin key for control APIs.',
+ devMode:
+ 'Development mode is active — identity is automatic and no API key is required.',
noKey:
- 'Enter a root or account-admin API key to load account and user choices.',
+ 'Add a Root API Key to manage accounts and mint user keys. The User API Key alone powers the Playground and data APIs.',
+ rootHint: 'Lists accounts and users, and mints or rotates keys.',
title: 'Connection settings',
+ userHint: 'Used by the Playground and tenant data APIs.',
},
dialogs: {
addAccount: {
@@ -154,7 +158,8 @@ const en = {
apiKey: 'API key',
baseUrl: 'Server URL',
dataApiKey: 'User API key',
- userApiKey: 'User API key',
+ rootApiKey: 'Root API Key',
+ userApiKey: 'User API Key',
role: 'Role',
user: 'User',
},
@@ -893,7 +898,8 @@ const en = {
},
messageContent: {
name: 'content',
- description: 'For the message subcommand. Text to append to the session.',
+ description:
+ 'For the message subcommand. Text to append to the session.',
},
contexts: {
name: '--context uri',
@@ -920,7 +926,8 @@ const en = {
},
toolResultId: {
name: 'tool_result_id',
- description: 'Required when reading or searching an externalized tool result.',
+ description:
+ 'Required when reading or searching an externalized tool result.',
},
limit: {
name: '--limit count',
@@ -932,7 +939,8 @@ const en = {
},
contextChars: {
name: '--context-chars count',
- description: 'For tool-search. Controls context length around matches.',
+ description:
+ 'For tool-search. Controls context length around matches.',
},
timeout: {
name: '--timeout seconds',
diff --git a/web-studio/src/i18n/locales/zh-CN.ts b/web-studio/src/i18n/locales/zh-CN.ts
index 6d86998f5..f3910ed82 100644
--- a/web-studio/src/i18n/locales/zh-CN.ts
+++ b/web-studio/src/i18n/locales/zh-CN.ts
@@ -111,17 +111,20 @@ const zhCN = {
regenerate: '重新生成',
save: '保存',
use: '使用',
- useForData: '用作 User key',
+ useForData: '用作 User API Key',
},
connection: {
accountListLimited:
'当前 key 不能列出所有 account;如果它有 account-admin 权限,仍可管理选中的 account。',
- adminError: '加载 admin 身份失败:{{message}}',
+ adminError: '校验 Root API Key 失败:{{message}}',
description:
- '租户数据 API 使用 User API key;控制 API 可单独使用 root 或 account-admin key。',
+ '租户数据 API 使用 User API Key;控制 API 可单独使用 Root 或 account-admin key。',
+ devMode: '当前为开发模式 — 身份自动确定,无需 API key。',
noKey:
- '输入 root 或 account-admin API key 后,可以加载 account 和 user 可选项。',
+ '填入 Root API Key 即可管理 account / user 并生成 user key。仅凭 User API Key 即可使用 Playground 和数据 API。',
+ rootHint: '用于列出 account / user,以及生成或轮换 key。',
title: '连接设置',
+ userHint: '供 Playground 和租户数据 API 使用。',
},
dialogs: {
addAccount: {
@@ -154,7 +157,8 @@ const zhCN = {
apiKey: 'API key',
baseUrl: '服务地址',
dataApiKey: 'User API key',
- userApiKey: 'User API key',
+ rootApiKey: 'Root API Key',
+ userApiKey: 'User API Key',
role: '角色',
user: 'User',
},
@@ -796,7 +800,8 @@ const zhCN = {
sessionUsage:
'用法:/session [current|list|create|switch|get|context|messages|archive|commit|extract|message|used|tool-results|tool-result|tool-search|delete] ...',
sessionDeleteUsage: '用法:/session delete <session_id>',
- sessionMissing: '当前没有 active session,请先打开 Agent 面板创建会话,或指定 session_id。',
+ sessionMissing:
+ '当前没有 active session,请先打开 Agent 面板创建会话,或指定 session_id。',
sessionCurrentBody: '当前 active session:{{id}}',
sessionListBody: '共有 {{count}} 个 session。',
sessionCreatedBody: '已创建并切换到 session:{{id}}',
@@ -843,7 +848,8 @@ const zhCN = {
},
scope: {
name: '--scope <.|uri>',
- description: '可选。不填则全局搜索;传 . 使用当前目录;传 uri 使用指定目录。',
+ description:
+ '可选。不填则全局搜索;传 . 使用当前目录;传 uri 使用指定目录。',
},
sessionAction: {
name: '子命令',
@@ -852,7 +858,8 @@ const zhCN = {
},
sessionId: {
name: 'session_id',
- description: '可选。省略时多数子命令使用当前 Agent session;delete 必须显式指定。',
+ description:
+ '可选。省略时多数子命令使用当前 Agent session;delete 必须显式指定。',
},
archiveId: {
name: 'archive_id',
diff --git a/web-studio/src/lib/admin.ts b/web-studio/src/lib/admin.ts
index bbc59d045..bdb139fba 100644
--- a/web-studio/src/lib/admin.ts
+++ b/web-studio/src/lib/admin.ts
@@ -157,7 +157,10 @@ async function probeAdminAccess(
}
}
- const controlKey = input.adminApiKey || input.apiKey
+ // The Root API key is the only control-plane credential. The User API key is
+ // never promoted to admin access, so management stays gated behind the root
+ // or account-admin key alone.
+ const controlKey = input.adminApiKey.trim()
if (input.serverMode === 'api_key' && !controlKey) {
return {
detail: 'A root or account-admin API key is required',
diff --git a/web-studio/src/routes/settings/route.tsx b/web-studio/src/routes/settings/route.tsx
index 333ab1473..3ccb5493c 100644
--- a/web-studio/src/routes/settings/route.tsx
+++ b/web-studio/src/routes/settings/route.tsx
@@ -1,11 +1,15 @@
import * as React from 'react'
-import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
+import {
+ keepPreviousData,
+ useMutation,
+ useQuery,
+ useQueryClient,
+} from '@tanstack/react-query'
import { createFileRoute } from '@tanstack/react-router'
import {
CircleAlertIcon,
CircleDashedIcon,
CopyIcon,
- DatabaseIcon,
KeyRoundIcon,
PlusIcon,
RefreshCwIcon,
@@ -35,7 +39,12 @@ import {
DialogHeader,
DialogTitle,
} from '#/components/ui/dialog'
-import { Field, FieldContent, FieldLabel } from '#/components/ui/field'
+import {
+ Field,
+ FieldContent,
+ FieldDescription,
+ FieldLabel,
+} from '#/components/ui/field'
import { Input } from '#/components/ui/input'
import {
Select,
@@ -67,7 +76,7 @@ import { copyTextToClipboard } from '#/lib/clipboard'
import { cn } from '#/lib/utils'
import type { ConnectionDraft } from '#/hooks/use-app-connection'
-import { AccountSelect, UserSelect } from '#/components/identity-select'
+import { AccountSelect } from '#/components/identity-select'
import {
DEFAULT_ACCOUNT_ID,
DEFAULT_USER_ID,
@@ -101,6 +110,17 @@ const USER_ROLES = ['user', 'admin'] as const
type AddAccountDraft = CreateAccountInput
type AddUserDraft = CreateUserInput
+// Server URLs, API keys, and account/user ids are case-sensitive freeform
+// values. Browsers (and mobile keyboards) otherwise auto-capitalize the first
+// letter — turning "http://" into "Http://" — and run autocorrect/spellcheck.
+// Disable all of it on these inputs.
+const PLAIN_INPUT_PROPS = {
+ autoCapitalize: 'none',
+ autoComplete: 'off',
+ autoCorrect: 'off',
+ spellCheck: false,
+} as const
+
function getErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error)
}
@@ -147,11 +167,11 @@ function StatCard({
function KeyResultCard({
onClear,
- onUseForData,
+ onUseAsUserKey,
result,
}: {
onClear: () => void
- onUseForData: () => void
+ onUseAsUserKey: () => void
result: KeyResult
}) {
const { t } = useTranslation('settings')
@@ -195,9 +215,9 @@ function KeyResultCard({
type="button"
variant="default"
size="sm"
- onClick={onUseForData}
+ onClick={onUseAsUserKey}
>
- <DatabaseIcon />
+ <KeyRoundIcon />
{t('actions.useForData')}
</Button>
</div>
@@ -360,6 +380,7 @@ function UserApiKeyInput({
onChange={(event) => onChange(event.target.value)}
placeholder={placeholder}
className="h-full min-w-0 flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed"
+ {...PLAIN_INPUT_PROPS}
/>
</div>
)
@@ -420,6 +441,7 @@ function AddAccountDialog({
}
placeholder={t('placeholders.account')}
required
+ {...PLAIN_INPUT_PROPS}
/>
</FieldContent>
</Field>
@@ -439,6 +461,7 @@ function AddAccountDialog({
}
placeholder={DEFAULT_USER_ID}
required
+ {...PLAIN_INPUT_PROPS}
/>
</FieldContent>
</Field>
@@ -538,6 +561,7 @@ function AddUserDialog({
}
placeholder={t('placeholders.user')}
required
+ {...PLAIN_INPUT_PROPS}
/>
</FieldContent>
</Field>
@@ -602,54 +626,121 @@ function SettingsRoute() {
const [pendingRegenerateUser, setPendingRegenerateUser] =
React.useState<AdminUser | null>(null)
+ // Debounced persistence. `draft` is the live, editable buffer behind the
+ // inputs; `connection` is the committed value everything else keys off (the
+ // probe, the admin queries, applyConnection, server-mode detection). Typing
+ // only updates draft and schedules a commit, so we don't re-probe / re-detect
+ // / refetch on every keystroke. Programmatic changes (adopting or rotating a
+ // key) commit immediately.
+ const saveConnectionRef = React.useRef(saveConnection)
React.useEffect(() => {
+ saveConnectionRef.current = saveConnection
+ }, [saveConnection])
+ const pendingDraftRef = React.useRef<ConnectionDraft | null>(null)
+ const saveTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null)
+
+ const commitConnection = React.useCallback((next: ConnectionDraft) => {
+ if (saveTimerRef.current) {
+ clearTimeout(saveTimerRef.current)
+ saveTimerRef.current = null
+ }
+ pendingDraftRef.current = null
+ saveConnectionRef.current(next)
+ }, [])
+
+ const commitConnectionDebounced = React.useCallback(
+ (next: ConnectionDraft) => {
+ pendingDraftRef.current = next
+ if (saveTimerRef.current) {
+ clearTimeout(saveTimerRef.current)
+ }
+ saveTimerRef.current = setTimeout(() => {
+ saveTimerRef.current = null
+ const pending = pendingDraftRef.current
+ pendingDraftRef.current = null
+ if (pending) {
+ saveConnectionRef.current(pending)
+ }
+ }, 350)
+ },
+ [],
+ )
+
+ // Flush a still-pending edit when leaving the page so the last keystrokes are
+ // not lost. Empty deps -> the cleanup runs only on unmount.
+ React.useEffect(() => {
+ return () => {
+ if (saveTimerRef.current) {
+ clearTimeout(saveTimerRef.current)
+ saveTimerRef.current = null
+ }
+ const pending = pendingDraftRef.current
+ pendingDraftRef.current = null
+ if (pending) {
+ saveConnectionRef.current(pending)
+ }
+ }
+ }, [])
+
+ // Mirror committed connection changes back into the buffer, but never while an
+ // edit is still pending (that would clobber what the user is typing).
+ React.useEffect(() => {
+ if (pendingDraftRef.current) {
+ return
+ }
setDraft(connection)
}, [connection])
- const controlApiKey = draft.adminApiKey.trim() || draft.apiKey.trim()
+ const isDevMode = serverMode === 'dev'
+ // The Root API key is the only control-plane credential and drives every
+ // admin call below. The User API key powers tenant data APIs (Playground,
+ // retrieval, ...). Keeping the two keys cleanly separated is the whole point
+ // of this screen. These derive from the committed connection (not the live
+ // draft) so the heavy work waits until typing settles.
+ const rootApiKey = connection.adminApiKey.trim()
const adminConnection = React.useMemo<AdminConnection>(
() => ({
- accountId: draft.accountId || DEFAULT_ACCOUNT_ID,
- apiKey: controlApiKey,
- baseUrl: draft.baseUrl,
- userId: draft.userId || DEFAULT_USER_ID,
+ accountId: connection.accountId || DEFAULT_ACCOUNT_ID,
+ apiKey: rootApiKey,
+ baseUrl: connection.baseUrl,
+ userId: connection.userId || DEFAULT_USER_ID,
}),
- [controlApiKey, draft.accountId, draft.baseUrl, draft.userId],
+ [rootApiKey, connection.accountId, connection.baseUrl, connection.userId],
)
const probeQuery = useQuery({
- enabled: Boolean(draft.baseUrl) && serverMode !== 'checking',
+ enabled: Boolean(connection.baseUrl) && serverMode !== 'checking',
queryFn: () =>
probeStudioConnection({
- accountId: draft.accountId || DEFAULT_ACCOUNT_ID,
- adminApiKey: draft.adminApiKey,
- apiKey: draft.apiKey,
- baseUrl: draft.baseUrl,
+ accountId: connection.accountId || DEFAULT_ACCOUNT_ID,
+ adminApiKey: connection.adminApiKey,
+ apiKey: connection.apiKey,
+ baseUrl: connection.baseUrl,
serverMode,
- userId: draft.userId || DEFAULT_USER_ID,
+ userId: connection.userId || DEFAULT_USER_ID,
}),
queryKey: [
'studio-connection-probe',
- draft.baseUrl,
- draft.adminApiKey,
- draft.apiKey,
- draft.accountId,
- draft.userId,
+ connection.baseUrl,
+ connection.adminApiKey,
+ connection.apiKey,
+ connection.accountId,
+ connection.userId,
serverMode,
],
+ // Keep showing the last probe result while a new identity/key is being
+ // checked, so adopting a user key (or editing a key) does not blank out the
+ // Admin/Data status and the whole User Management panel mid-refetch.
+ placeholderData: keepPreviousData,
retry: false,
staleTime: 15_000,
})
- const hasAdminAccess =
- serverMode !== 'dev' && probeQuery.data?.admin.state === 'ok'
+ const adminProbe = probeQuery.data?.admin
+ const hasAdminAccess = !isDevMode && adminProbe?.state === 'ok'
const showAdminDescription =
hasAdminAccess || connectionRole === 'admin' || connectionRole === 'root'
- const hasSavedAdminApiKey = Boolean(draft.adminApiKey.trim())
- const showAdminCredentialFields =
- serverMode !== 'dev' && (hasAdminAccess || hasSavedAdminApiKey)
- const canQueryAdmin = Boolean(controlApiKey) && hasAdminAccess
- const showDevApiKeyPlaceholder = serverMode === 'dev'
+ const canQueryAdmin = Boolean(rootApiKey) && hasAdminAccess
const accountsQuery = useQuery({
enabled: canQueryAdmin,
@@ -664,7 +755,7 @@ function SettingsRoute() {
retry: false,
})
- const selectedAccountId = draft.accountId || DEFAULT_ACCOUNT_ID
+ const selectedAccountId = connection.accountId || DEFAULT_ACCOUNT_ID
const selectedManagedAccountIds = React.useMemo(() => {
const selected = sortedAccountIds(managedAccountIds, '')
return selected.length > 0
@@ -672,20 +763,6 @@ function SettingsRoute() {
: sortedAccountIds([selectedAccountId], '')
}, [managedAccountIds, selectedAccountId])
- const usersQuery = useQuery({
- enabled: canQueryAdmin && Boolean(selectedAccountId),
- queryFn: () => fetchAdminUsers(adminConnection, selectedAccountId),
- queryKey: [
- 'admin-users',
- adminConnection.baseUrl,
- adminConnection.apiKey,
- adminConnection.accountId,
- adminConnection.userId,
- selectedAccountId,
- ],
- retry: false,
- })
-
const managedUsersQuery = useQuery({
enabled: canQueryAdmin && selectedManagedAccountIds.length > 0,
queryFn: async () => {
@@ -723,83 +800,72 @@ function SettingsRoute() {
)
}, [accountsQuery.data, managedUsersQuery.data, selectedManagedAccountIds])
+ // Keep the management account filter pointed at an account that still exists.
+ // This never mutates the active identity — that only changes when the user
+ // explicitly adopts a key via "Use as User API Key".
React.useEffect(() => {
if (!accountOptions.length) {
return
}
-
const accountIds = accountOptions.map((account) => account.accountId)
const preferred = accountIds.includes(DEFAULT_ACCOUNT_ID)
? DEFAULT_ACCOUNT_ID
: accountIds[0]
-
- setDraft((current) => {
- if (current.accountId && accountIds.includes(current.accountId)) {
- return current
- }
-
- const next = { ...current, accountId: preferred }
- saveConnection(next)
- return next
- })
setManagedAccountIds((current) => {
const next = current.filter((accountId) => accountIds.includes(accountId))
- return next.length > 0 ? next : [preferred]
+ const resolved = next.length > 0 ? next : [preferred]
+ // Return the SAME reference when nothing actually changed. filter() always
+ // builds a fresh array, and handing a new reference back would re-key the
+ // selectedManagedAccountIds -> accountOptions memos and re-fire this
+ // effect on every render — an unbounded loop.
+ const unchanged =
+ resolved.length === current.length &&
+ resolved.every((accountId, index) => accountId === current[index])
+ return unchanged ? current : resolved
})
- }, [accountOptions, saveConnection])
+ }, [accountOptions])
- React.useEffect(() => {
- const users = usersQuery.data
- if (!users?.length) {
- return
+ function buildUserKeyConnection(
+ current: ConnectionDraft,
+ result: KeyResult,
+ ): ConnectionDraft {
+ return {
+ ...current,
+ accountId: result.accountId || current.accountId || DEFAULT_ACCOUNT_ID,
+ apiKey: result.apiKey || current.apiKey,
+ userId: result.userId || current.userId || DEFAULT_USER_ID,
}
+ }
- const userIds = users.map((user) => user.userId)
- const preferred = userIds.includes(DEFAULT_USER_ID)
- ? DEFAULT_USER_ID
- : userIds[0]
- setDraft((current) => {
- const hasCurrentUser =
- Boolean(current.userId) && userIds.includes(current.userId)
- const userId = hasCurrentUser ? current.userId : preferred
- const selectedUser = users.find((user) => user.userId === userId)
- const apiKey = selectedUser?.apiKey || ''
- const next = { ...current, apiKey, userId }
-
- if (next.apiKey !== current.apiKey || next.userId !== current.userId) {
- saveConnection(next)
- return next
- }
+ // Field edits debounce the commit (no probe/refetch per keystroke).
+ function updateDraft(next: Partial<ConnectionDraft>): void {
+ const updated = { ...draft, ...next }
+ setDraft(updated)
+ commitConnectionDebounced(updated)
+ }
- return current
- })
- }, [saveConnection, usersQuery.data])
+ function useUserKey(result: KeyResult): void {
+ if (!result.apiKey) {
+ return
+ }
+ // Adopting a key is an explicit action — commit it right away.
+ const next = buildUserKeyConnection(draft, result)
+ setDraft(next)
+ commitConnection(next)
+ toast.success(t('toast.dataKeySelected'))
+ }
const createAccountMutation = useMutation({
mutationFn: (input: CreateAccountInput) =>
createAdminAccount(adminConnection, input),
onError: (error) => toast.error(getErrorMessage(error)),
onSuccess: async (result, variables) => {
- const keyResultWithIdentity = {
+ setKeyResult({
...result,
accountId: result.accountId || variables.accountId,
userId: result.userId || variables.adminUserId,
- }
- setKeyResult(keyResultWithIdentity)
+ })
setManagedAccountIds([variables.accountId])
- if (keyResultWithIdentity.apiKey) {
- const next = buildDataKeyConnection(draft, keyResultWithIdentity)
- setDraft(next)
- saveConnection(next)
- } else {
- const next = {
- ...draft,
- accountId: variables.accountId,
- userId: variables.adminUserId,
- }
- setDraft(next)
- saveConnection(next)
- }
setAddAccountOpen(false)
toast.success(t('toast.accountCreated'))
await queryClient.invalidateQueries({ queryKey: ['admin-accounts'] })
@@ -812,28 +878,14 @@ function SettingsRoute() {
createAdminUser(adminConnection, input),
onError: (error) => toast.error(getErrorMessage(error)),
onSuccess: async (result, variables) => {
- const keyResultWithIdentity = {
+ setKeyResult({
...result,
accountId: result.accountId || variables.accountId,
userId: result.userId || variables.userId,
- }
- setKeyResult(keyResultWithIdentity)
+ })
setManagedAccountIds((current) =>
sortedAccountIds([...current, variables.accountId], ''),
)
- if (keyResultWithIdentity.apiKey) {
- const next = buildDataKeyConnection(draft, keyResultWithIdentity)
- setDraft(next)
- saveConnection(next)
- } else {
- const next = {
- ...draft,
- accountId: variables.accountId,
- userId: variables.userId,
- }
- setDraft(next)
- saveConnection(next)
- }
setAddUserOpen(false)
toast.success(t('toast.userCreated'))
await queryClient.invalidateQueries({ queryKey: ['admin-accounts'] })
@@ -847,14 +899,16 @@ function SettingsRoute() {
onError: (error) => toast.error(getErrorMessage(error)),
onSuccess: async (result, user) => {
setKeyResult(result)
+ // If the rotated user is the one currently powering data APIs, keep the
+ // active User API key in sync so the session does not silently break.
if (
result.apiKey &&
user.accountId === draft.accountId &&
user.userId === draft.userId
) {
- const next = buildDataKeyConnection(draft, result)
+ const next = buildUserKeyConnection(draft, result)
setDraft(next)
- saveConnection(next)
+ commitConnection(next)
}
setPendingRegenerateUser(null)
toast.success(t('toast.keyRegenerated'))
@@ -862,7 +916,6 @@ function SettingsRoute() {
},
})
- const users = usersQuery.data ?? []
const managedUsers = managedUsersQuery.data ?? []
const totalAccounts = accountOptions.length
const totalUsers =
@@ -873,81 +926,9 @@ function SettingsRoute() {
).length
const adminUnavailable = !canQueryAdmin || managedUsersQuery.isError
- function findSelectedUser(
- accountId: string,
- preferredUserId: string,
- ): AdminUser | undefined {
- const normalizedAccountId = accountId || DEFAULT_ACCOUNT_ID
- const candidates = [...users, ...managedUsers]
- .filter((user) => user.accountId === normalizedAccountId)
- .filter(
- (user, index, list) =>
- list.findIndex((item) => item.userId === user.userId) === index,
- )
-
- return (
- candidates.find((user) => user.userId === preferredUserId) ||
- candidates.find((user) => user.userId === DEFAULT_USER_ID) ||
- candidates[0]
- )
- }
-
- function updateDraft(next: Partial<ConnectionDraft>): void {
- const updated = { ...draft, ...next }
- setDraft(updated)
- saveConnection(updated)
- }
-
- function updateConnectionAccount(accountId: string): void {
- const selectedUser = findSelectedUser(accountId, draft.userId)
- setManagedAccountIds((current) =>
- sortedAccountIds([...current, accountId], ''),
- )
- updateDraft({
- accountId,
- apiKey: selectedUser?.apiKey || '',
- userId: selectedUser?.userId || DEFAULT_USER_ID,
- })
- }
-
- function updateConnectionUser(userId: string): void {
- const selectedUser = findSelectedUser(draft.accountId, userId)
- updateDraft({
- apiKey: selectedUser?.apiKey || '',
- userId,
- })
- }
-
- function buildDataKeyConnection(
- current: ConnectionDraft,
- result: KeyResult,
- ): ConnectionDraft {
- const nextApiKey = result.apiKey || current.apiKey
- return {
- ...current,
- accountId: result.accountId || current.accountId || DEFAULT_ACCOUNT_ID,
- adminApiKey:
- current.adminApiKey ||
- (current.apiKey && current.apiKey !== nextApiKey ? current.apiKey : ''),
- apiKey: nextApiKey,
- userId: result.userId || current.userId || DEFAULT_USER_ID,
- }
- }
-
- function useKeyForData(result: KeyResult): void {
- if (!result.apiKey) {
- return
- }
- const next = buildDataKeyConnection(draft, result)
- setDraft(next)
- saveConnection(next)
- toast.success(t('toast.dataKeySelected'))
- }
-
async function refreshAdmin(): Promise<void> {
await Promise.all([
accountsQuery.refetch(),
- usersQuery.refetch(),
managedUsersQuery.refetch(),
probeQuery.refetch(),
])
@@ -980,118 +961,62 @@ function SettingsRoute() {
<Card className="gap-0 overflow-hidden border-primary/25 bg-primary/[0.025] py-0 shadow-sm ring-1 ring-primary/10">
<CardHeader className="gap-2 border-b border-primary/15 bg-primary/[0.07] px-5 py-3.5">
- <div className="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
- <div className="min-w-0">
- <div className="flex flex-wrap items-center gap-2">
- <div className="flex size-7 items-center justify-center rounded-md bg-primary text-primary-foreground">
- <KeyRoundIcon className="size-4" />
- </div>
- <CardTitle>{t('connection.title')}</CardTitle>
+ <div className="flex flex-wrap items-center justify-between gap-2">
+ <div className="flex min-w-0 items-center gap-2">
+ <div className="flex size-7 items-center justify-center rounded-md bg-primary text-primary-foreground">
+ <KeyRoundIcon className="size-4" />
</div>
+ <CardTitle>{t('connection.title')}</CardTitle>
</div>
+ <Badge variant="outline" className="shrink-0 font-normal">
+ {t(`serverMode.${serverMode}`)}
+ </Badge>
</div>
</CardHeader>
- <CardContent className="grid gap-3 px-5 py-4">
- <div
- className={cn(
- 'grid gap-3',
- hasAdminAccess
- ? 'xl:grid-cols-[minmax(16rem,1.2fr)_minmax(12rem,0.8fr)_minmax(12rem,0.8fr)]'
- : 'xl:grid-cols-[minmax(16rem,1.2fr)_minmax(16rem,1fr)]',
- )}
- >
- <Field>
- <FieldLabel htmlFor="settings-base-url">
- {t('fields.baseUrl')}
- </FieldLabel>
- <FieldContent>
- <Input
- id="settings-base-url"
- value={draft.baseUrl}
- onChange={(event) =>
- updateDraft({ baseUrl: event.target.value })
- }
- placeholder={t('placeholders.baseUrl')}
- />
- </FieldContent>
- </Field>
- {hasAdminAccess ? (
- <>
- <Field>
- <FieldLabel>{t('fields.account')}</FieldLabel>
- <FieldContent>
- <AccountSelect
- accounts={accountOptions}
- disabled={!canQueryAdmin || accountsQuery.isLoading}
- label={t('fields.account')}
- value={draft.accountId || DEFAULT_ACCOUNT_ID}
- onChange={updateConnectionAccount}
- />
- </FieldContent>
- </Field>
- <Field>
- <FieldLabel>{t('fields.user')}</FieldLabel>
- <FieldContent>
- <UserSelect
- disabled={!canQueryAdmin || usersQuery.isLoading}
- label={t('fields.user')}
- users={users}
- value={draft.userId || DEFAULT_USER_ID}
- onChange={updateConnectionUser}
- />
- </FieldContent>
- </Field>
- </>
- ) : showDevApiKeyPlaceholder ? (
- <Field>
- <FieldLabel htmlFor="settings-api-key-dev">
- {t('fields.apiKey')}
- </FieldLabel>
- <FieldContent>
- <Input
- id="settings-api-key-dev"
- value={t('placeholders.devModeApiKey')}
- disabled
- readOnly
- />
- </FieldContent>
- </Field>
- ) : showAdminCredentialFields ? null : (
- <Field>
- <FieldLabel htmlFor="settings-api-key">
- {t('fields.apiKey')}
- </FieldLabel>
- <FieldContent>
- <Input
- id="settings-api-key"
- type="password"
- value={draft.apiKey}
- onChange={(event) =>
- updateDraft({ apiKey: event.target.value })
- }
- placeholder={t('placeholders.apiKey')}
- />
- </FieldContent>
- </Field>
- )}
- </div>
- {showAdminCredentialFields ? (
+ <CardContent className="grid gap-4 px-5 py-4">
+ <Field>
+ <FieldLabel htmlFor="settings-base-url">
+ {t('fields.baseUrl')}
+ </FieldLabel>
+ <FieldContent>
+ <Input
+ id="settings-base-url"
+ value={draft.baseUrl}
+ onChange={(event) =>
+ updateDraft({ baseUrl: event.target.value })
+ }
+ placeholder={t('placeholders.baseUrl')}
+ inputMode="url"
+ {...PLAIN_INPUT_PROPS}
+ />
+ </FieldContent>
+ </Field>
+
+ {isDevMode ? (
+ <p className="rounded-md border border-dashed bg-muted/30 px-3 py-2 text-sm text-muted-foreground">
+ {t('connection.devMode')}
+ </p>
+ ) : (
<>
- <div className="grid gap-3">
+ <div className="grid gap-4 md:grid-cols-2">
<Field>
- <FieldLabel htmlFor="settings-admin-api-key">
- {t('fields.adminApiKey')}
+ <FieldLabel htmlFor="settings-root-api-key">
+ {t('fields.rootApiKey')}
</FieldLabel>
<FieldContent>
<Input
- id="settings-admin-api-key"
+ id="settings-root-api-key"
type="password"
value={draft.adminApiKey}
onChange={(event) =>
updateDraft({ adminApiKey: event.target.value })
}
placeholder={t('placeholders.adminApiKey')}
+ {...PLAIN_INPUT_PROPS}
/>
+ <FieldDescription>
+ {t('connection.rootHint')}
+ </FieldDescription>
</FieldContent>
</Field>
<Field>
@@ -1107,14 +1032,18 @@ function SettingsRoute() {
onChange={(apiKey) => updateDraft({ apiKey })}
placeholder={t('placeholders.userApiKey')}
/>
+ <FieldDescription>
+ {t('connection.userHint')}
+ </FieldDescription>
</FieldContent>
</Field>
</div>
+
<div className="grid gap-2 md:grid-cols-2">
<CapabilityStatus
isLoading={probeQuery.isFetching}
label={t('health.admin')}
- result={probeQuery.data?.admin}
+ result={adminProbe}
/>
<CapabilityStatus
isLoading={probeQuery.isFetching}
@@ -1122,19 +1051,22 @@ function SettingsRoute() {
result={probeQuery.data?.data}
/>
</div>
- {accountsQuery.isError && managedUsersQuery.isError ? (
+
+ {!hasAdminAccess && !rootApiKey ? (
+ <p className="text-sm text-muted-foreground">
+ {t('connection.noKey')}
+ </p>
+ ) : !hasAdminAccess &&
+ rootApiKey &&
+ adminProbe?.state === 'error' ? (
<p className="text-sm text-destructive">
{t('connection.adminError', {
- message: getErrorMessage(accountsQuery.error),
+ message: adminProbe.detail || '',
})}
</p>
- ) : accountsQuery.isError ? (
- <p className="text-sm text-muted-foreground">
- {t('connection.accountListLimited')}
- </p>
) : null}
</>
- ) : null}
+ )}
</CardContent>
</Card>
@@ -1162,7 +1094,7 @@ function SettingsRoute() {
<KeyResultCard
result={keyResult}
onClear={() => setKeyResult(null)}
- onUseForData={() => useKeyForData(keyResult)}
+ onUseAsUserKey={() => useUserKey(keyResult)}
/>
) : null}
@@ -1305,14 +1237,14 @@ function SettingsRoute() {
variant="secondary"
size="sm"
onClick={() =>
- useKeyForData({
+ useUserKey({
accountId: user.accountId,
apiKey: user.apiKey || '',
userId: user.userId,
})
}
>
- <DatabaseIcon />
+ <KeyRoundIcon />
{t('actions.useForData')}
</Button>
) : null}