Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog/unreleased/account-bundle-export-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### English

- Added full account export/import on the Accounts page. Export all providers' accounts and their settings to one JSON file, copy it to another computer, and import it there to restore them.
- The export file includes stored sign-in credentials; keep it private.

### 中文

- 账号页新增全量导出/导入:可把所有供应商的账号及配置导出为一个 JSON 文件,复制到另一台电脑后导入即可恢复。
- 导出文件包含登录凭证,请妥善保管。
49 changes: 49 additions & 0 deletions frontend/src/api/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,52 @@ export function importAccount(bundle: Record<string, unknown>) {
export function exportAccount(accountId: string) {
return api<Record<string, unknown>>(`/api/accounts/${encodeURIComponent(accountId)}/export`)
}

export type AccountBundle = {
format: string
version: number
exported_at: string
count: number
accounts: AccountBundleEntry[]
}

export type AccountBundleEntry = {
name: string
provider: string
region: string
auth_type?: string
enabled: boolean
max_inflight: number
priority: number
drop_system_prompt?: boolean
model_aliases?: Record<string, string>
enabled_models?: string[]
models_restricted?: boolean
auto_checkin?: boolean
checkin_time?: string
workbuddy_auto_checkin?: boolean
workbuddy_checkin_time?: string
proxy_url?: string
credential?: {
format: string
credential?: unknown
user_blob?: string
machine_id?: string
}
}

export type AccountImportResult = {
object?: string
succeeded?: number
failed?: number
data?: unknown[]
errors?: Array<{ index?: number; name?: string; message?: string }>
}

export function exportAccountBundle() {
return api<AccountBundle>('/api/accounts/export')
}

export function importAccountBundle(bundle: AccountBundle) {
return api<AccountImportResult>('/api/accounts/import/bundle', { method: 'POST', body: JSON.stringify(bundle) })
}
81 changes: 81 additions & 0 deletions frontend/src/components/account/AccountBundleImportModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Button, Modal } from '@heroui/react'
import { X } from '@phosphor-icons/react'
import type { AccountBundle } from '@/api/overview'

type Props = {
isOpen: boolean
bundle: AccountBundle | null
fileName?: string
isPending?: boolean
t: (key: string, vars?: Record<string, string | number>) => string
onClose: () => void
onConfirm: () => void
}

/** Confirmation dialog for a full console account migration bundle. */
export function AccountBundleImportModal({
isOpen,
bundle,
fileName,
isPending,
t,
onClose,
onConfirm,
}: Props) {
const count = bundle?.accounts?.length ?? 0
const withCredentials = bundle?.accounts?.filter((entry) => Boolean(entry.credential)).length ?? 0

return (
<Modal.Root
isOpen={isOpen}
onOpenChange={(open: boolean) => {
if (!open && !isPending) onClose()
}}
>
<Modal.Backdrop variant="blur" isDismissable={!isPending}>
<Modal.Container placement="center" size="md" scroll="inside" className="mx-3 w-full max-w-[min(100%,32rem)] sm:mx-0">
<Modal.Dialog>
<Modal.Header className="items-start justify-between gap-4 px-6 pt-6">
<div className="min-w-0">
<Modal.Heading className="text-section font-semibold">{t('bundleImportConfirmTitle')}</Modal.Heading>
<p className="mt-1 break-all text-caption leading-5 text-muted">
{fileName ? t('bundleImportConfirmHint', { file: fileName }) : t('bundleImportConfirmHintNoFile')}
</p>
</div>
<Modal.CloseTrigger
isDisabled={isPending}
aria-label={t('close')}
className="grid size-8 shrink-0 place-items-center rounded-lg text-muted transition-colors hover:bg-surface-secondary hover:text-foreground"
>
<X size={16} />
</Modal.CloseTrigger>
</Modal.Header>
<Modal.Body className="space-y-3 px-6 pb-2">
<div className="rounded-xl bg-surface-secondary px-3.5 py-3 text-body leading-6 text-foreground">
<div className="mono text-lg font-semibold">{count}</div>
<div className="text-muted">{t('bundleImportAccountCount')}</div>
</div>
<p className="text-body leading-6 text-muted">{t('bundleImportCountDetail', { credentials: withCredentials })}</p>
<p className="text-body leading-6 text-muted">{t('bundleImportAdditiveHint')}</p>
<p className="text-caption leading-5 text-muted">{t('bundleExportHint')}</p>
</Modal.Body>
<Modal.Footer className="flex-col-reverse gap-2 px-6 pb-6 sm:flex-row sm:justify-end">
<Button size="sm" variant="ghost" className="w-full sm:w-auto" isDisabled={isPending} onPress={onClose}>
{t('cancel')}
</Button>
<Button
size="sm"
className="w-full sm:w-auto"
isPending={isPending}
isDisabled={count === 0}
onPress={onConfirm}
>
{t('bundleImportConfirmAction')}
</Button>
</Modal.Footer>
</Modal.Dialog>
</Modal.Container>
</Modal.Backdrop>
</Modal.Root>
)
}
28 changes: 28 additions & 0 deletions frontend/src/i18n/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ export const messages: Record<Lang, Dict> = {
rewarmDone: 'Rewarm completed',
accountsSigned: '{n} / {total} accounts ready',
addAccount: 'Add account',
exportAll: 'Export all accounts',
importAll: 'Import all accounts',
bundleExportHint: 'Keep this file private — it contains sign-in credentials.',
toastAccountsExporting: 'Preparing account export…',
toastAccountsExported: 'Exported {n} accounts to a file.',
batchImport: 'Batch import',
batchImportLoading: 'Importing WorkBuddy accounts…',
batchImportUnrecognized: 'Could not recognize this credential file format.',
Expand All @@ -781,6 +786,15 @@ export const messages: Record<Lang, Dict> = {
batchImportConfirmAction: 'Import',
batchImportFormatUnknown: 'Unknown format',
batchImportProviderUnknown: 'Unknown provider',
bundleImportConfirmTitle: 'Import all accounts?',
bundleImportConfirmHint: 'From {file}. This restores the accounts stored in the file.',
bundleImportConfirmHintNoFile: 'This restores the accounts stored in the selected file.',
bundleImportConfirmAction: 'Import all',
bundleImportAccountCount: 'accounts in this file',
bundleImportCountDetail: '{credentials} of them include a stored sign-in credential.',
bundleImportAdditiveHint: 'Import adds new accounts with fresh IDs. Existing accounts are not changed.',
bundleImportLoading: 'Importing accounts…',
bundleImportUnrecognized: 'This is not a valid full account export file.',
accountName: 'Account name',
accountNameRequired: 'Enter an account name.',
accountCreated: 'Account created. Session is starting.',
Expand Down Expand Up @@ -1774,6 +1788,11 @@ export const messages: Record<Lang, Dict> = {
rewarmDone: 'Rewarm 完成',
accountsSigned: '{n} / {total} 个账号就绪',
addAccount: '添加账号',
exportAll: '导出全部账号',
importAll: '导入全部账号',
bundleExportHint: '请妥善保管此文件——其中包含登录凭证。',
toastAccountsExporting: '正在准备账号导出…',
toastAccountsExported: '已将 {n} 个账号导出到文件。',
batchImport: '批量导入',
batchImportLoading: '正在导入 WorkBuddy 账号…',
batchImportUnrecognized: '无法识别此凭证文件格式。',
Expand All @@ -1789,6 +1808,15 @@ export const messages: Record<Lang, Dict> = {
batchImportConfirmAction: '导入',
batchImportFormatUnknown: '未知格式',
batchImportProviderUnknown: '未知供应商',
bundleImportConfirmTitle: '导入全部账号?',
bundleImportConfirmHint: '来自 {file}。将恢复文件中保存的账号。',
bundleImportConfirmHintNoFile: '将恢复所选文件中保存的账号。',
bundleImportConfirmAction: '全部导入',
bundleImportAccountCount: '个账号(此文件中)',
bundleImportCountDetail: '其中 {credentials} 个包含已保存的登录凭证。',
bundleImportAdditiveHint: '导入会创建新账号并分配新 ID,不会修改已有账号。',
bundleImportLoading: '正在导入账号…',
bundleImportUnrecognized: '这不是有效的全量账号导出文件。',
accountName: '账号名称',
accountNameRequired: '请输入账号名称。',
accountCreated: '账号已创建,会话正在启动。',
Expand Down
40 changes: 40 additions & 0 deletions frontend/src/lib/accountBundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { AccountBundle } from '@/api/overview'

export const ACCOUNT_BUNDLE_FORMAT = 'cli2api-accounts'

export function isAccountBundle(value: unknown): value is AccountBundle {
if (!value || typeof value !== 'object' || Array.isArray(value)) return false
const record = value as Record<string, unknown>
return (
typeof record.format === 'string' &&
record.format.trim().toLowerCase() === ACCOUNT_BUNDLE_FORMAT &&
Array.isArray(record.accounts)
)
}

export function parseAccountBundleText(text: string): AccountBundle {
let parsed: unknown
try {
parsed = JSON.parse(text)
} catch {
throw new Error('bad_json')
}
if (!isAccountBundle(parsed)) throw new Error('bad_bundle')
return parsed as AccountBundle
}

export function downloadAccountBundle(bundle: AccountBundle) {
const text = JSON.stringify(bundle, null, 2)
const blob = new Blob([text], { type: 'application/json' })
const url = URL.createObjectURL(blob)
const stamp = (bundle.exported_at || new Date().toISOString())
.replace(/[:.]/g, '-')
.slice(0, 19)
const anchor = document.createElement('a')
anchor.href = url
anchor.download = `cli2api-accounts-${stamp}.json`
document.body.appendChild(anchor)
anchor.click()
anchor.remove()
URL.revokeObjectURL(url)
}
Loading
Loading