Import apps/android/src/services/storage.ts
This commit is contained in:
parent
a6dbcf8ce8
commit
818c99fcbf
|
|
@ -6,9 +6,19 @@ import * as SecureStore from 'expo-secure-store'
|
||||||
import { isLanguagePreference, messageCatalogs, strings, type LanguagePreference } from '../locales/i18n'
|
import { isLanguagePreference, messageCatalogs, strings, type LanguagePreference } from '../locales/i18n'
|
||||||
import type { Messages } from '../locales/types'
|
import type { Messages } from '../locales/types'
|
||||||
import zhCN from '../locales/zh-CN'
|
import zhCN from '../locales/zh-CN'
|
||||||
import { isActivityDockMode, isThemePreference, isTypographyDensity, type ActivityDockMode, type ThemePreference, type TypographyDensity } from '../ui/theme'
|
import { isActivityDockMode, isDrawerLayoutMode, isThemePreference, isTypographyDensity, normalizeChatAppearance, type ActivityDockMode, type ChatAppearance, type DrawerLayoutMode, type ThemePreference, type TypographyDensity } from '../ui/theme'
|
||||||
|
import { isQuickPromptActionMode, type QuickPromptActionMode } from '../screens/chat/composer-gate'
|
||||||
import type { AgentBackend, CodexPermissionPreset, DefaultPermissionPreset, DeviceCredentials, DeviceIdentity, RemoteDevice, ServerConfig, WorkspaceShortcut } from '../types'
|
import type { AgentBackend, CodexPermissionPreset, DefaultPermissionPreset, DeviceCredentials, DeviceIdentity, RemoteDevice, ServerConfig, WorkspaceShortcut } from '../types'
|
||||||
|
|
||||||
|
/** Durable compaction attempt shape shared with `compaction-store`. */
|
||||||
|
export interface StoredCompactionRecord {
|
||||||
|
sessionId: string
|
||||||
|
requestId: string
|
||||||
|
operationId?: string
|
||||||
|
state: string
|
||||||
|
updatedAt: number
|
||||||
|
}
|
||||||
|
|
||||||
const KEYS = {
|
const KEYS = {
|
||||||
config: 'dshremote.server.v1',
|
config: 'dshremote.server.v1',
|
||||||
identity: 'dshremote.identity.v1',
|
identity: 'dshremote.identity.v1',
|
||||||
|
|
@ -20,7 +30,10 @@ const KEYS = {
|
||||||
languagePreference: 'dshremote.language-preference.v1',
|
languagePreference: 'dshremote.language-preference.v1',
|
||||||
themePreference: 'dshremote.theme-preference.v1',
|
themePreference: 'dshremote.theme-preference.v1',
|
||||||
typographyDensity: 'dshremote.typography-density.v1',
|
typographyDensity: 'dshremote.typography-density.v1',
|
||||||
|
chatAppearance: 'dshremote.chat-appearance.v1',
|
||||||
activityDockMode: 'dshremote.activity-dock-mode.v1',
|
activityDockMode: 'dshremote.activity-dock-mode.v1',
|
||||||
|
drawerLayoutMode: 'dshremote.drawer-layout-mode.v1',
|
||||||
|
quickPromptActionMode: 'dshremote.quick-prompt-action-mode.v1',
|
||||||
compactHistory: 'dshremote.compact-history.v1',
|
compactHistory: 'dshremote.compact-history.v1',
|
||||||
statusBarVisible: 'dshremote.status-bar-visible.v1',
|
statusBarVisible: 'dshremote.status-bar-visible.v1',
|
||||||
defaultPermissionPreset: 'dshremote.default-permission-preset.v1',
|
defaultPermissionPreset: 'dshremote.default-permission-preset.v1',
|
||||||
|
|
@ -31,6 +44,7 @@ const KEYS = {
|
||||||
favoriteWorkspaces: 'dshremote.favorite-workspaces.v1',
|
favoriteWorkspaces: 'dshremote.favorite-workspaces.v1',
|
||||||
recentWorkspaces: 'dshremote.recent-workspaces.v1',
|
recentWorkspaces: 'dshremote.recent-workspaces.v1',
|
||||||
customPrompts: 'dshremote.custom-prompts.v1',
|
customPrompts: 'dshremote.custom-prompts.v1',
|
||||||
|
compactions: 'dshremote.compactions.v1',
|
||||||
} as const
|
} as const
|
||||||
|
|
||||||
const secureOptions: SecureStore.SecureStoreOptions = {
|
const secureOptions: SecureStore.SecureStoreOptions = {
|
||||||
|
|
@ -390,6 +404,31 @@ export async function clearLocalData(): Promise<void> {
|
||||||
await Promise.all(Object.values(KEYS).map(key => SecureStore.deleteItemAsync(key, secureOptions)))
|
await Promise.all(Object.values(KEYS).map(key => SecureStore.deleteItemAsync(key, secureOptions)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Persisted compaction attempts keyed by Host scope and Session id. */
|
||||||
|
export async function loadCompactionRecords(): Promise<Record<string, Record<string, StoredCompactionRecord>>> {
|
||||||
|
const stored = await readJson<Record<string, Record<string, StoredCompactionRecord>>>(KEYS.compactions)
|
||||||
|
if (stored === undefined || !isRecord(stored)) return {}
|
||||||
|
const scopes: Record<string, Record<string, StoredCompactionRecord>> = {}
|
||||||
|
for (const [scope, records] of Object.entries(stored)) {
|
||||||
|
if (!isRecord(records)) continue
|
||||||
|
const valid: Record<string, StoredCompactionRecord> = {}
|
||||||
|
for (const [sessionId, record] of Object.entries(records)) {
|
||||||
|
if (!isRecord(record)) continue
|
||||||
|
if (typeof record.sessionId !== 'string' || typeof record.requestId !== 'string'
|
||||||
|
|| typeof record.state !== 'string' || typeof record.updatedAt !== 'number') continue
|
||||||
|
valid[sessionId] = record as unknown as StoredCompactionRecord
|
||||||
|
}
|
||||||
|
if (Object.keys(valid).length > 0) scopes[scope] = valid
|
||||||
|
}
|
||||||
|
return scopes
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveCompactionRecords(
|
||||||
|
scopes: Record<string, Record<string, StoredCompactionRecord>>,
|
||||||
|
): Promise<void> {
|
||||||
|
await writeJson(KEYS.compactions, scopes)
|
||||||
|
}
|
||||||
|
|
||||||
async function readJson<T>(key: string): Promise<T | undefined> {
|
async function readJson<T>(key: string): Promise<T | undefined> {
|
||||||
const value = await SecureStore.getItemAsync(key, secureOptions)
|
const value = await SecureStore.getItemAsync(key, secureOptions)
|
||||||
if (value === null) return undefined
|
if (value === null) return undefined
|
||||||
|
|
@ -559,3 +598,32 @@ export async function loadActivityDockMode(): Promise<ActivityDockMode> {
|
||||||
export async function saveActivityDockMode(value: ActivityDockMode): Promise<void> {
|
export async function saveActivityDockMode(value: ActivityDockMode): Promise<void> {
|
||||||
await writeJson(KEYS.activityDockMode, { value })
|
await writeJson(KEYS.activityDockMode, { value })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function loadDrawerLayoutMode(): Promise<DrawerLayoutMode> {
|
||||||
|
const stored = await readJson<{ value: unknown }>(KEYS.drawerLayoutMode)
|
||||||
|
return isDrawerLayoutMode(stored?.value) ? stored.value : 'compact'
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveDrawerLayoutMode(value: DrawerLayoutMode): Promise<void> {
|
||||||
|
await writeJson(KEYS.drawerLayoutMode, { value })
|
||||||
|
}
|
||||||
|
|
||||||
|
export { isQuickPromptActionMode, type QuickPromptActionMode }
|
||||||
|
|
||||||
|
export async function loadQuickPromptActionMode(): Promise<QuickPromptActionMode> {
|
||||||
|
const stored = await readJson<{ value: unknown }>(KEYS.quickPromptActionMode)
|
||||||
|
return isQuickPromptActionMode(stored?.value) ? stored.value : 'fill'
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveQuickPromptActionMode(value: QuickPromptActionMode): Promise<void> {
|
||||||
|
await writeJson(KEYS.quickPromptActionMode, { value })
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadChatAppearance(): Promise<ChatAppearance> {
|
||||||
|
const stored = await readJson<{ value?: unknown }>(KEYS.chatAppearance)
|
||||||
|
return normalizeChatAppearance(stored?.value ?? stored)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveChatAppearance(value: ChatAppearance): Promise<void> {
|
||||||
|
await writeJson(KEYS.chatAppearance, { value })
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue