refactor: consolidate utility functions into shared package

Move duplicate isObject, asString, asNumber, and safeStringify functions from multiple modules into a centralized shared/src/utils.ts module and update imports across cli, server, and web packages. This eliminates code duplication and improves maintainability.
This commit is contained in:
weishu
2026-01-23 16:04:37 +08:00
parent a41d6aa662
commit 9922588c6f
30 changed files with 34 additions and 138 deletions
+1
View File
@@ -1,4 +1,5 @@
export * from './messages'
export * from './modes'
export * from './sessionSummary'
export * from './utils'
export type * from './types'
+2 -4
View File
@@ -1,13 +1,11 @@
import { isObject } from './utils'
type RoleWrappedRecord = {
role: string
content: unknown
meta?: unknown
}
function isObject(value: unknown): value is Record<string, unknown> {
return Boolean(value) && typeof value === 'object'
}
export function isRoleWrappedRecord(value: unknown): value is RoleWrappedRecord {
if (!isObject(value)) return false
return typeof value.role === 'string' && 'content' in value
+21
View File
@@ -0,0 +1,21 @@
export function isObject(value: unknown): value is Record<string, unknown> {
return Boolean(value) && typeof value === 'object'
}
export function asString(value: unknown): string | null {
return typeof value === 'string' ? value : null
}
export function asNumber(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) ? value : null
}
export function safeStringify(value: unknown): string {
if (typeof value === 'string') return value
try {
const stringified = JSON.stringify(value, null, 2)
return typeof stringified === 'string' ? stringified : String(value)
} catch {
return String(value)
}
}