欢迎页面支持多语言

This commit is contained in:
xintaofei
2026-03-07 12:17:57 +08:00
parent aeecc4769c
commit 6c48be023c
15 changed files with 685 additions and 72 deletions

69
src/lib/app-error.ts Normal file
View File

@@ -0,0 +1,69 @@
import type { AppCommandError } from "@/lib/types"
type ObjectLike = Record<string, unknown>
function asObject(value: unknown): ObjectLike | null {
return value !== null && typeof value === "object"
? (value as ObjectLike)
: null
}
function normalizeString(value: unknown): string | null {
return typeof value === "string" && value.trim() ? value.trim() : null
}
function parseErrorObject(value: unknown): AppCommandError | null {
const obj = asObject(value)
if (!obj) return null
const code = normalizeString(obj.code)
const message = normalizeString(obj.message)
const detailRaw = normalizeString(obj.detail)
const detail = detailRaw ?? null
if (!code || !message) return null
return {
code,
message,
detail,
}
}
export function extractAppCommandError(error: unknown): AppCommandError | null {
const direct = parseErrorObject(error)
if (direct) return direct
const errorObject = asObject(error)
const message = normalizeString(errorObject?.message)
if (!message) return null
try {
const parsed = JSON.parse(message)
return parseErrorObject(parsed)
} catch {
return null
}
}
export function toErrorMessage(error: unknown): string {
const appError = extractAppCommandError(error)
if (appError) {
return appError.detail?.trim() || appError.message
}
if (error instanceof Error) {
return error.message.trim()
}
if (typeof error === "string") {
return error.trim()
}
try {
const serialized = JSON.stringify(error)
return serialized ? serialized.trim() : String(error)
} catch {
return String(error)
}
}

View File

@@ -20,6 +20,27 @@ export type AgentType =
| "open_claw"
| "stakpak"
export type AppErrorCode =
| "unknown"
| "invalid_input"
| "not_found"
| "already_exists"
| "permission_denied"
| "dependency_missing"
| "network_error"
| "authentication_failed"
| "database_error"
| "io_error"
| "external_command_failed"
| "window_operation_failed"
| (string & {})
export interface AppCommandError {
code: AppErrorCode
message: string
detail?: string | null
}
export interface ConversationSummary {
id: string
agent_type: AgentType