Files
DeepTutor/web/lib/workspaces-api.ts
Botong dd29ff5a0a fix(i18n): sync frontend output language and remove zh-only UI fallbacks
Align frontend model-output language propagation and eliminate Simplified-Chinese-only UI shortcuts across chat, settings, books, reading, memory, and workspace surfaces:

- Preserve Traditional Chinese (zh-tw/zh-hk/zh-mo/zh-hant) and native aliases in resolveResponseLanguage(), and introduce shared effectiveResponseLanguage() and subscribeResponseLanguage() helpers.
- Sync response_language with language in SettingsStore when both were aligned, while preserving intentional divergence and clearing auto-synced pending response_language if an external desktop menu overrides language.
- Preserve per-conversation replyLanguageOverride when regenerating without a replay snapshot and subscribe ChatWorkspace's default reply-language label to live response_language changes.
- Route Book Creator, Book Chat fallback, Memory evolution/update, and Reading extension actions through effectiveResponseLanguage() instead of the UI language, and preserve zh-Hant in translationTarget().
- Route remaining zh ? ... : t(...) branches, hardcoded Memory labels, and binary zh/en date formatting through i18next and normalizeLanguage() so Traditional Chinese and all non-Simplified locales render through catalogs without modifying locale JSON files.

Related to #2933
Related to #684
2026-11-29 02:13:55 -06:00

217 lines
6.3 KiB
TypeScript

import { apiFetch, apiUrl } from '@/lib/api'
import { invalidateClientCache, withClientCache } from '@/lib/client-cache'
import { notifySessionsChanged } from '@/lib/session-events'
export interface WorkspaceResources {
skills: string[] | null
mcp: string[] | null
knowledge_bases: string[] | null
}
export const inheritedWorkspaceResources = (): WorkspaceResources => ({
skills: null,
mcp: null,
knowledge_bases: null,
})
export interface WorkspaceResourceOption {
id: string
name: string
description?: string
source?: string
provenance_label?: string
available?: boolean
}
export type WorkspaceResourceCatalog = Record<keyof WorkspaceResources, WorkspaceResourceOption[]>
export function getWorkspaceResources(workspaceId = ''): Promise<WorkspaceResourceCatalog> {
return request(
`/api/settings/workspace/resources?workspace_id=${encodeURIComponent(workspaceId)}`
)
}
export interface ChatWorkspaceRegistration {
instructions?: string
resources?: WorkspaceResources
workspace_id: string
kind: 'system' | 'general' | 'workspace'
follows_root: boolean
display_name: string
path: string
archived: boolean
created_at: string
status: 'ready' | 'invalid'
error: string
}
const endpoint = '/api/settings/workspace/registrations'
export interface ServerFolder {
name: string
path: string
}
export interface ServerFolderListing extends ServerFolder {
roots: ServerFolder[]
parent: string | null
folders: ServerFolder[]
truncated: boolean
}
export function browseServerFolders(path = ''): Promise<ServerFolderListing> {
return request(`/api/settings/workspace/folders?path=${encodeURIComponent(path)}`)
}
async function request<T>(url: string, init?: RequestInit): Promise<T> {
const response = await apiFetch(apiUrl(url), init)
const payload = await response.json()
if (!response.ok) throw new Error(payload.detail || `Request failed (${response.status})`)
return payload as T
}
export function listWorkspaces(force = false): Promise<ChatWorkspaceRegistration[]> {
return withClientCache(
'workspaces:list',
async () => {
const result = await request<{ workspaces: ChatWorkspaceRegistration[] }>(endpoint)
return result.workspaces
},
{ force, ttlMs: 15_000 }
)
}
export async function saveWorkspace(
input: { name?: string; path?: string; archived?: boolean; resources?: WorkspaceResources; instructions?: string },
workspaceId?: string
): Promise<ChatWorkspaceRegistration> {
const result = await request<{ workspace: ChatWorkspaceRegistration }>(
workspaceId ? `${endpoint}/${encodeURIComponent(workspaceId)}` : endpoint,
{
method: workspaceId ? 'PATCH' : 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(input),
}
)
invalidateClientCache('workspaces:')
invalidateClientCache('knowledge:')
notifySessionsChanged()
return result.workspace
}
export function workspaceChatHref(workspaceId: string): string {
return `/chat?dt_workspace=${encodeURIComponent(workspaceId)}`
}
export interface WorkspaceCatalog {
root: string
workspaces: ChatWorkspaceRegistration[]
migration?: { id: string } | null
}
export interface SystemWorkspaceSnapshot {
generated_at: string
models: Record<
string,
{
active_profile_id?: string
active_model_id?: string
profiles: {
id?: string
name?: string
binding?: string
endpoint_origin?: string
models: { id?: string; name?: string; model?: string }[]
}[]
}
>
mcp: { name: string; enabled: boolean; transport: string; endpoint_origin: string }[]
}
export function getWorkspaceCatalog(): Promise<WorkspaceCatalog> {
return request<WorkspaceCatalog>(endpoint)
}
export async function migrateWorkspace(
path: string,
workspaceId?: string
): Promise<WorkspaceCatalog> {
const result = await request<WorkspaceCatalog>(
workspaceId
? `${endpoint}/${encodeURIComponent(workspaceId)}/migrate`
: `${endpoint}/migrate-root`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path }),
}
)
invalidateClientCache('workspaces:')
invalidateClientCache('knowledge:')
notifySessionsChanged()
return result
}
export function getSystemWorkspaceSnapshot(): Promise<SystemWorkspaceSnapshot> {
return request<SystemWorkspaceSnapshot>(`${endpoint}/system-snapshot`, { method: 'POST' })
}
export function workspaceLabel(
row: ChatWorkspaceRegistration,
_zh: boolean,
translate: (key: string) => string = key => key,
): string {
if (row.kind === 'system') return translate('System workspace')
if (row.kind === 'general') return translate('General workspace')
return row.display_name
}
export async function resourceUsage(
kind: keyof WorkspaceResources,
resourceId: string,
skillWorkspace = ''
): Promise<string[]> {
const params = new URLSearchParams({
kind,
resource_id: resourceId,
skill_workspace: skillWorkspace,
})
const result = await request<{ workspaces: { display_name: string }[] }>(
`/api/settings/workspace/resource-usage?${params}`
)
return result.workspaces.map(row => row.display_name)
}
export interface KnowledgeMovePreview {
source_id: string
target_id: string
source_workspace_id: string
target_workspace_id: string
name: string
files: number
bytes: number
assignments: { workspace_id: string; display_name: string }[]
blockers: string[]
status?: string
cleanup_path?: string
}
export function previewKnowledgeMove(sourceId: string, targetWorkspaceId: string): Promise<KnowledgeMovePreview> {
return request('/api/settings/workspace/knowledge-bases/preview-move', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ source_id: sourceId, target_workspace_id: targetWorkspaceId }),
})
}
export async function moveKnowledgeBase(sourceId: string, targetWorkspaceId: string): Promise<KnowledgeMovePreview> {
const result = await request<KnowledgeMovePreview>('/api/settings/workspace/knowledge-bases/move', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ source_id: sourceId, target_workspace_id: targetWorkspaceId }),
})
invalidateClientCache('workspaces:')
invalidateClientCache('knowledge:')
notifySessionsChanged()
return result
}