| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- import type {
- ChatAttachment,
- ChatMessage,
- CurrentUser,
- FileKind,
- FileLibraryItem,
- GroupAgent,
- GroupCapabilities,
- GroupInteraction,
- GroupMessage,
- GroupRoomDetail,
- GroupRoomSummary,
- JsonValue,
- ModelOption,
- Profile,
- SessionSummary,
- ToolCall,
- } from '@shared/types'
- export type UnknownRecord = Record<string, unknown>
- export function record(value: unknown): UnknownRecord {
- return value && typeof value === 'object' && !Array.isArray(value) ? value as UnknownRecord : {}
- }
- export function string(value: unknown, fallback = ''): string {
- if (typeof value === 'string') return value
- if (typeof value === 'number' && Number.isFinite(value)) return String(value)
- return fallback
- }
- export function number(value: unknown, fallback = 0): number {
- const parsed = typeof value === 'number' ? value : Number(value)
- return Number.isFinite(parsed) ? parsed : fallback
- }
- export function bool(value: unknown, fallback = false): boolean {
- if (typeof value === 'boolean') return value
- if (value === 1 || value === '1' || value === 'true') return true
- if (value === 0 || value === '0' || value === 'false') return false
- return fallback
- }
- export function values(value: unknown): unknown[] {
- return Array.isArray(value) ? value : []
- }
- function pick(source: UnknownRecord, ...keys: string[]): unknown {
- for (const key of keys) if (source[key] !== undefined && source[key] !== null) return source[key]
- return undefined
- }
- export function normalizeUser(value: unknown): CurrentUser {
- const source = record(value)
- const id = string(pick(source, 'id', 'user_id', 'userId')) || string(pick(source, 'email', 'username')) || 'local'
- return {
- id,
- username: string(pick(source, 'username', 'display_name', 'displayName', 'email', 'user_id', 'userId'), id),
- displayName: string(pick(source, 'display_name', 'displayName')) || undefined,
- email: string(source.email) || undefined,
- provider: string(source.provider) || undefined,
- role: string(source.role) || undefined,
- }
- }
- export function normalizeProfile(value: unknown): Profile {
- const source = record(value)
- const name = string(pick(source, 'name', 'profile', 'id'), 'default')
- return {
- name,
- agentName: string(pick(source, 'agentName', 'agent_name')) || undefined,
- displayName: string(pick(source, 'display_name', 'displayName', 'alias', 'description')) || undefined,
- description: string(source.description) || undefined,
- isDefault: bool(pick(source, 'is_default', 'isDefault', 'active')),
- model: string(source.model) || undefined,
- provider: string(source.provider) || undefined,
- gatewayRunning: source.gateway_running === undefined && source.gatewayRunning === undefined
- ? undefined
- : bool(pick(source, 'gateway_running', 'gatewayRunning')),
- avatar: string(source.avatar) || undefined,
- }
- }
- export function normalizeModel(value: unknown): ModelOption {
- const source = record(value)
- const provider = string(source.provider)
- const id = string(pick(source, 'id', 'model', 'value'))
- return {
- id,
- name: string(pick(source, 'name', 'display_name', 'displayName'), id),
- provider,
- supportsReasoning: source.supports_reasoning === undefined && source.supportsReasoning === undefined
- ? undefined
- : bool(pick(source, 'supports_reasoning', 'supportsReasoning')),
- reasoningEfforts: values(pick(source, 'reasoning_efforts', 'reasoningEfforts')).map(value => string(value)).filter(Boolean),
- isDefault: bool(pick(source, 'is_default', 'isDefault')),
- }
- }
- export function normalizeSession(value: unknown, fallbackProfile?: string): SessionSummary {
- const source = record(value)
- const startedAt = number(pick(source, 'started_at', 'startedAt', 'created_at', 'createdAt'))
- const updatedAt = number(pick(source, 'last_active', 'lastActive', 'updated_at', 'updatedAt'), startedAt)
- const id = string(pick(source, 'id', 'session_id', 'sessionId', 'stored_session_id', 'storedSessionId'))
- return {
- id,
- profile: string(source.profile, fallbackProfile) || undefined,
- source: string(source.source, 'cli'),
- title: string(source.title, '未命名会话'),
- preview: string(source.preview) || undefined,
- model: string(source.model) || undefined,
- provider: string(source.provider) || undefined,
- agent: string(source.agent) || undefined,
- messageCount: number(pick(source, 'message_count', 'messageCount')),
- toolCallCount: number(pick(source, 'tool_call_count', 'toolCallCount')),
- inputTokens: number(pick(source, 'input_tokens', 'inputTokens')) || undefined,
- outputTokens: number(pick(source, 'output_tokens', 'outputTokens')) || undefined,
- startedAt,
- updatedAt,
- endedAt: pick(source, 'ended_at', 'endedAt') == null ? null : number(pick(source, 'ended_at', 'endedAt')),
- parentSessionId: string(pick(source, 'parent_session_id', 'parentSessionId')) || null,
- forkPointMessageId: string(pick(source, 'fork_point_message_id', 'forkPointMessageId')) || null,
- workspace: string(source.workspace) || null,
- archived: bool(pick(source, 'is_archived', 'isArchived', 'archived')),
- pinned: bool(pick(source, 'is_pinned', 'isPinned', 'pinned')),
- }
- }
- function normalizeToolCall(value: unknown, index: number): ToolCall {
- const source = record(value)
- const functionCall = record(source.function)
- const error = string(source.error)
- const result = pick(source, 'result', 'output') as JsonValue | undefined
- return {
- id: string(pick(source, 'id', 'tool_call_id', 'toolCallId', 'tool_id'), `tool-${index}`),
- name: string(pick(source, 'name', 'tool_name', 'toolName') ?? functionCall.name, '工具'),
- status: error ? 'failed' : result !== undefined ? 'completed' : 'running',
- arguments: (pick(source, 'arguments', 'args', 'context') ?? functionCall.arguments) as JsonValue | undefined,
- result,
- preview: string(pick(source, 'preview', 'summary', 'args_text')) || undefined,
- error: error || undefined,
- durationSeconds: number(pick(source, 'duration_seconds', 'durationSeconds', 'duration_s')) || undefined,
- }
- }
- export function normalizeAttachment(value: unknown, index = 0): ChatAttachment {
- const source = record(value)
- const mimeType = string(pick(source, 'mime_type', 'mimeType', 'media_type', 'mediaType'), 'application/octet-stream')
- const name = string(pick(source, 'name', 'filename'), `附件 ${index + 1}`)
- return {
- id: string(source.id, `${name}:${index}`),
- name,
- mimeType,
- size: number(source.size),
- path: string(source.path) || undefined,
- url: string(source.url) || undefined,
- kind: mimeType.startsWith('image/') ? 'image' : mimeType === 'application/pdf' ? 'pdf' : 'file',
- }
- }
- export function normalizeChatMessage(value: unknown, sessionId: string, fallbackProfile?: string): ChatMessage {
- const source = record(value)
- const contentValue = pick(source, 'content', 'text', 'output')
- let content = typeof contentValue === 'string' ? contentValue : ''
- let attachments: ChatAttachment[] = values(pick(source, 'attachments', 'files')).map(normalizeAttachment)
- if (Array.isArray(contentValue)) {
- const textBlocks: string[] = []
- const attachmentBlocks: ChatAttachment[] = []
- contentValue.forEach((blockValue, index) => {
- const block = record(blockValue)
- const type = string(block.type)
- if (type === 'text') textBlocks.push(string(block.text))
- if (type === 'image' || type === 'file') attachmentBlocks.push(normalizeAttachment(block, index))
- })
- content = textBlocks.filter(Boolean).join('\n\n')
- attachments = [...attachments, ...attachmentBlocks]
- }
- const roleValue = string(source.role, 'assistant')
- const role = ['user', 'assistant', 'tool', 'system'].includes(roleValue) ? roleValue as ChatMessage['role'] : 'system'
- const id = string(pick(source, 'id', 'message_id', 'messageId')) || `history-${sessionId}-${number(source.timestamp)}-${Math.random()}`
- const status = string(pick(source, 'status', 'finish_reason', 'finishReason'))
- return {
- id,
- serverMessageId: id,
- clientMessageId: string(pick(source, 'client_message_id', 'clientMessageId')) || undefined,
- sessionId,
- profile: string(source.profile, fallbackProfile) || undefined,
- role,
- content,
- reasoning: string(pick(source, 'reasoning', 'thinking')) || undefined,
- timestamp: number(pick(source, 'timestamp', 'created_at', 'createdAt', 'updated_at', 'updatedAt'), Date.now() / 1000),
- sequence: number(pick(source, 'seq', 'sequence')) || undefined,
- stage: status === 'error' || source.error ? 'failed' : 'settled',
- error: string(source.error) || undefined,
- attachments: attachments.length ? attachments : undefined,
- toolCalls: values(typeof pick(source, 'tool_calls', 'toolCalls', 'tools') === 'string'
- ? (() => { try { return JSON.parse(String(pick(source, 'tool_calls', 'toolCalls', 'tools'))) } catch { return [] } })()
- : pick(source, 'tool_calls', 'toolCalls', 'tools')).map(normalizeToolCall),
- toolCallId: string(pick(source, 'tool_call_id', 'toolCallId')) || undefined,
- toolName: string(pick(source, 'tool_name', 'toolName')) || undefined,
- raw: value as JsonValue,
- }
- }
- export function normalizeGroupCapabilities(value: unknown): GroupCapabilities {
- const source = record(value)
- const limits = record(source.limits)
- return {
- protocolVersion: number(pick(source, 'protocolVersion', 'protocol_version')),
- journalEpoch: string(pick(source, 'journalEpoch', 'journal_epoch')),
- latestCursor: number(pick(source, 'latestCursor', 'latest_cursor')),
- limits: {
- maxAgentsPerRoom: number(pick(limits, 'maxAgentsPerRoom', 'max_agents_per_room'), 8),
- maxMessageBytes: number(pick(limits, 'maxMessageBytes', 'max_message_bytes'), 64 * 1024),
- maxToolStateBytes: number(pick(limits, 'maxToolStateBytes', 'max_tool_state_bytes')) || undefined,
- maxInteractionPayloadBytes: number(pick(limits, 'maxInteractionPayloadBytes', 'max_interaction_payload_bytes')) || undefined,
- maxMessagePageSize: number(pick(limits, 'maxMessagePageSize', 'max_message_page_size'), 100),
- maxEventBatchSize: number(pick(limits, 'maxEventBatchSize', 'max_event_batch_size')) || undefined,
- maxAgentDepth: number(pick(limits, 'maxAgentDepth', 'max_agent_depth')) || undefined,
- maxRoomConcurrency: number(pick(limits, 'maxRoomConcurrency', 'max_room_concurrency')) || undefined,
- maxPluginConcurrency: number(pick(limits, 'maxPluginConcurrency', 'max_plugin_concurrency')) || undefined,
- defaultMaxReplyRounds: number(pick(limits, 'defaultMaxReplyRounds', 'default_max_reply_rounds'), 3),
- unlimitedReplyRoundsValue: number(pick(limits, 'unlimitedReplyRoundsValue', 'unlimited_reply_rounds_value'), -1),
- maxAgentDisplayNameLength: number(pick(limits, 'maxAgentDisplayNameLength', 'max_agent_display_name_length'), 100),
- },
- eventTypes: values(pick(source, 'eventTypes', 'event_types')).map(value => string(value)).filter(Boolean),
- }
- }
- export function normalizeGroupAgent(value: unknown): GroupAgent {
- const source = record(value)
- const status = string(source.status, 'unknown') as GroupAgent['status']
- return {
- id: string(source.id), roomId: string(pick(source, 'roomId', 'room_id')),
- profile: string(source.profile), displayName: string(pick(source, 'displayName', 'display_name'), string(source.profile)),
- description: string(source.description), storedSessionId: string(pick(source, 'storedSessionId', 'stored_session_id')) || null,
- lastContextMessageSeq: number(pick(source, 'lastContextMessageSeq', 'last_context_message_seq')),
- enabled: bool(source.enabled, true), replyWithoutMention: bool(pick(source, 'replyWithoutMention', 'reply_without_mention')),
- createdAt: number(pick(source, 'createdAt', 'created_at')), updatedAt: number(pick(source, 'updatedAt', 'updated_at')),
- status: ['idle', 'queued', 'running', 'awaiting_input'].includes(status) ? status : 'unknown',
- }
- }
- export function normalizeGroupMessage(value: unknown): GroupMessage {
- const source = record(value)
- const senderKind = string(pick(source, 'senderKind', 'sender_kind'), 'unknown') as GroupMessage['senderKind']
- const status = string(source.status, 'unknown') as GroupMessage['status']
- return {
- seq: number(source.seq), id: string(source.id), roomId: string(pick(source, 'roomId', 'room_id')),
- senderKind: ['human', 'agent', 'system'].includes(senderKind) ? senderKind : 'unknown',
- senderId: string(pick(source, 'senderId', 'sender_id')), senderName: string(pick(source, 'senderName', 'sender_name')),
- rootMessageId: string(pick(source, 'rootMessageId', 'root_message_id')), replyToMessageId: string(pick(source, 'replyToMessageId', 'reply_to_message_id')) || null,
- clientMessageId: string(pick(source, 'clientMessageId', 'client_message_id')) || null,
- content: string(source.content), reasoning: string(source.reasoning), toolState: values(pick(source, 'toolState', 'tool_state')) as JsonValue[],
- status: ['queued', 'streaming', 'completed', 'failed', 'interrupted'].includes(status) ? status : 'unknown',
- error: string(source.error), createdAt: number(pick(source, 'createdAt', 'created_at')), updatedAt: number(pick(source, 'updatedAt', 'updated_at')),
- }
- }
- export function normalizeGroupInteraction(value: unknown): GroupInteraction {
- const source = record(value)
- return {
- id: string(source.id), roomId: string(pick(source, 'roomId', 'room_id')), agentId: string(pick(source, 'agentId', 'agent_id')),
- runId: string(pick(source, 'runId', 'run_id')), kind: string(source.kind, 'unknown') as GroupInteraction['kind'],
- payload: (source.payload ?? null) as JsonValue, status: string(source.status, 'unknown') as GroupInteraction['status'],
- createdAt: number(pick(source, 'createdAt', 'created_at')), resolvedAt: pick(source, 'resolvedAt', 'resolved_at') == null ? null : number(pick(source, 'resolvedAt', 'resolved_at')),
- }
- }
- export function normalizeGroupRoom(value: unknown): GroupRoomSummary {
- const source = record(value)
- const rawAgents = source.agents
- return {
- id: string(source.id), name: string(source.name, '未命名群聊'), cwd: string(source.cwd),
- createdAt: number(pick(source, 'createdAt', 'created_at')), updatedAt: number(pick(source, 'updatedAt', 'updated_at')),
- archived: bool(source.archived), agentCount: Array.isArray(rawAgents) ? rawAgents.length : number(pick(source, 'agentCount', 'agent_count')),
- lastMessage: source.lastMessage || source.last_message ? normalizeGroupMessage(pick(source, 'lastMessage', 'last_message')) : null,
- unreadCount: number(pick(source, 'unreadCount', 'unread_count')), maxReplyRounds: number(pick(source, 'maxReplyRounds', 'max_reply_rounds'), 3),
- }
- }
- export function normalizeGroupRoomDetail(value: unknown): GroupRoomDetail {
- const source = record(value)
- const summary = normalizeGroupRoom(source)
- return {
- id: summary.id, name: summary.name, cwd: summary.cwd, createdAt: summary.createdAt, updatedAt: summary.updatedAt,
- archived: summary.archived, maxReplyRounds: summary.maxReplyRounds,
- agents: values(source.agents).map(normalizeGroupAgent), runs: values(source.runs).map(run => record(run) as unknown as GroupRoomDetail['runs'][number]),
- pendingInteractions: values(pick(source, 'pendingInteractions', 'pending_interactions')).map(normalizeGroupInteraction),
- latestCursor: number(pick(source, 'latestCursor', 'latest_cursor')),
- }
- }
- export function inferFileKind(mimeType: string, name: string): FileKind {
- const mime = mimeType.toLowerCase().split(';')[0]
- if (mime.startsWith('image/')) return 'image'
- if (mime.startsWith('video/')) return 'video'
- if (mime.startsWith('audio/')) return 'audio'
- if (mime.startsWith('text/') || /\.(pdf|docx?|xlsx?|pptx?|md|txt|csv|json|rtf|pages|numbers|key)$/i.test(name)) return 'document'
- return 'other'
- }
- export function normalizeFile(value: unknown, fallbackProfile?: string): FileLibraryItem {
- const source = record(value)
- const id = string(pick(source, 'id', 'fileId', 'file_id'))
- const mimeType = string(pick(source, 'mimeType', 'mime_type'), 'application/octet-stream')
- const name = string(source.name)
- const origins = values(source.origins).map(item => {
- const origin = record(item)
- return {
- id: origin.id as number | string | undefined,
- profile: string(origin.profile) || fallbackProfile || undefined,
- sessionId: string(pick(origin, 'sessionId', 'session_id')) || undefined,
- sessionTitle: string(pick(origin, 'sessionTitle', 'session_title')) || undefined,
- messageId: string(pick(origin, 'messageId', 'message_id')) || undefined,
- authorKind: string(pick(origin, 'authorKind', 'author_kind')) as FileLibraryItem['origins'][number]['authorKind'] || undefined,
- authorName: string(pick(origin, 'authorName', 'author_name')) || undefined,
- observedAt: number(pick(origin, 'observedAt', 'observed_at')) || undefined,
- originalPath: string(pick(origin, 'originalPath', 'original_path')) || undefined,
- referencePath: string(pick(origin, 'referencePath', 'reference_path')) || undefined,
- }
- })
- const profile = origins.find(origin => origin.profile)?.profile
- const profileQuery = profile ? `?profile=${encodeURIComponent(profile)}` : ''
- return {
- id, path: string(source.path), name, extension: string(pick(source, 'extension', 'extensionName', 'extension_name')),
- mimeType, size: number(source.size), modifiedAt: number(pick(source, 'modifiedAt', 'modified_at')),
- exists: bool(source.exists, true), availability: string(source.availability) as FileLibraryItem['availability'] || undefined,
- archiveStatus: string(pick(source, 'archiveStatus', 'archive_status')) || undefined,
- firstSeenAt: number(pick(source, 'firstSeenAt', 'first_seen_at')) || undefined,
- lastSeenAt: number(pick(source, 'lastSeenAt', 'last_seen_at')) || undefined,
- messageTimestamp: pick(source, 'messageTimestamp', 'message_timestamp') == null ? null : number(pick(source, 'messageTimestamp', 'message_timestamp')),
- origins, kind: inferFileKind(mimeType, name),
- previewUrl: string(pick(source, 'previewUrl', 'preview_url'))
- || `/api/app/files/${encodeURIComponent(id)}/preview${profileQuery}`,
- downloadUrl: string(pick(source, 'downloadUrl', 'download_url'))
- || `/api/app/files/${encodeURIComponent(id)}/download${profileQuery}`,
- }
- }
|