refactor: reorganize imports and update type references across services

- Updated import paths for types and utilities in config-service, menu-service, script-execution-service, script-store-service, and window-service to reflect new structure.
- Moved utility functions like debounce and cloneDeep to shared utilities.
- Created new runtime and script types files to centralize type definitions.
- Removed deprecated task-types and locale messages files.
- Updated constants to use shared definitions and added new placeholders for providers.
- Enhanced provider type information with additional placeholders for different languages.
This commit is contained in:
duanshuwen
2026-04-21 23:25:51 +08:00
parent 97a956ffe8
commit 721344883f
24 changed files with 181 additions and 670 deletions

View File

@@ -1,4 +1,4 @@
import type { ConfigKeys, IConfig } from '@runtime/lib/types';
import type { ConfigKeys, IConfig } from '@electron/types/runtime';
import configManager from '@electron/service/config-service';
import type { HostApiResult } from '@src/types/runtime';
import type { HostApiContext } from '../context';

View File

@@ -0,0 +1,99 @@
export type RuntimeMessageTree = {
[key: string]: string | number | RuntimeMessageTree;
};
export const runtimeLocaleMessages: Record<'en' | 'zh' | 'th', RuntimeMessageTree> = {
en: {
settings: {
title: 'Settings',
},
menu: {
conversation: {
newConversation: 'New Conversation',
sortBy: 'Sort By',
sortByCreateTime: 'Sort by Creation Time',
sortByUpdateTime: 'Sort by Update Time',
sortByName: 'Sort by Name',
sortByModel: 'Sort by Model',
sortAscending: 'Ascending',
sortDescending: 'Descending',
pinConversation: 'Pin Conversation',
renameConversation: 'Rename Conversation',
delConversation: 'Delete Conversation',
batchOperations: 'Batch Operations',
},
message: {
copyMessage: 'Copy Message',
deleteMessage: 'Delete Message',
selectMessage: 'Select Message',
},
},
tray: {
tooltip: 'ZN-AI',
showWindow: 'Show Window',
exit: 'Exit',
},
},
zh: {
settings: {
title: '设置',
},
menu: {
conversation: {
newConversation: '新建对话',
sortBy: '排序方式',
sortByCreateTime: '按创建时间排序',
sortByUpdateTime: '按更新时间排序',
sortByName: '按名称排序',
sortByModel: '按模型排序',
sortAscending: '递增',
sortDescending: '递减',
pinConversation: '置顶对话',
renameConversation: '重命名对话',
delConversation: '删除对话',
batchOperations: '批量操作',
},
message: {
copyMessage: '复制消息',
deleteMessage: '删除消息',
selectMessage: '选择消息',
},
},
tray: {
tooltip: 'ZN-AI',
showWindow: '显示窗口',
exit: '退出',
},
},
th: {
settings: {
title: 'ตั้งค่า',
},
menu: {
conversation: {
newConversation: 'การสนทนาใหม่',
sortBy: 'จัดเรียงตาม',
sortByCreateTime: 'จัดเรียงตามเวลาสร้าง',
sortByUpdateTime: 'จัดเรียงตามเวลาอัปเดต',
sortByName: 'จัดเรียงตามชื่อ',
sortByModel: 'จัดเรียงตามโมเดล',
sortAscending: 'เรียงจากน้อยไปมาก',
sortDescending: 'เรียงจากมากไปน้อย',
pinConversation: 'ปักหมุดการสนทนา',
renameConversation: 'เปลี่ยนชื่อการสนทนา',
delConversation: 'ลบการสนทนา',
batchOperations: 'ดำเนินการแบบกลุ่ม',
},
message: {
copyMessage: 'คัดลอกข้อความ',
deleteMessage: 'ลบข้อความ',
selectMessage: 'เลือกข้อความ',
},
},
tray: {
tooltip: 'ZN-AI',
showWindow: 'แสดงหน้าต่าง',
exit: 'ออก',
},
},
};

View File

@@ -1,5 +1,5 @@
import { app, BrowserWindow, ipcMain } from 'electron'
import { CONFIG_KEYS } from '@runtime/lib/constants'
import { CONFIG_KEYS, IPC_EVENTS } from '@runtime/lib/constants'
import { setupMainWindow } from './wins';
import started from 'electron-squirrel-startup'
import configManager from '@electron/service/config-service'
@@ -75,7 +75,7 @@ async function requestUpstreamHostApi(path: string, method: string, headers: Rec
}
}
ipcMain.handle('hostapi:fetch', async (_event, { path, method, headers, body }) => {
ipcMain.handle(IPC_EVENTS.HOST_API_FETCH, async (_event, { path, method, headers, body }) => {
const normalizedMethod = method || 'GET';
// 1. 优先本地处理 Host API 路由(逐步对齐 ClawX
@@ -92,7 +92,7 @@ ipcMain.handle('hostapi:fetch', async (_event, { path, method, headers, body })
});
// Gateway RPC IPC handler
ipcMain.handle('gateway:rpc', async (_event, method: string, params: any) => {
ipcMain.handle(IPC_EVENTS.GATEWAY_RPC, async (_event, method: string, params: any) => {
return gatewayManager.rpc(method, params);
});

View File

@@ -1,10 +1,10 @@
import { BrowserWindow, ipcMain } from 'electron'
import type { ConfigKeys, IConfig } from '@runtime/lib/types'
import { CONFIG_KEYS, IPC_EVENTS } from '@runtime/lib/constants'
import { debounce } from '@runtime/lib/utils'
import logManager from '@electron/service/logger'
import type { ConfigKeys, IConfig } from '@electron/types/runtime'
import { getUserDataDir } from '@electron/utils/paths'
import { debounce } from '@electron/utils/shared'
type AppConfig = IConfig & {
[CONFIG_KEYS.GATEWAY_AUTO_START]: boolean;

View File

@@ -1,7 +1,7 @@
import { ipcMain, Menu, type MenuItemConstructorOptions } from 'electron';
import { CONFIG_KEYS, IPC_EVENTS } from '@runtime/lib/constants';
import { cloneDeep } from '@runtime/lib/utils';
import { createTranslator } from '@electron/utils'
import { cloneDeep } from '@electron/utils/shared';
import logManager from '@electron/service/logger'
import configManager from '@electron/service/config-service'

View File

@@ -3,7 +3,7 @@ import {
getScriptPathById,
updateLastRun,
} from '@electron/service/script-store-service';
import type { ScriptExecutionResult } from '@runtime/lib/script-types';
import type { ScriptExecutionResult } from '../../types/script-types';
const executor = new executeScriptService();

View File

@@ -7,7 +7,7 @@ import type {
ScriptMetaItem,
ScriptsMeta,
ScriptSaveInput,
} from '@runtime/lib/script-types';
} from '../../types/script-types';
const META_FILENAME = 'scripts.meta.json';

View File

@@ -1,4 +1,4 @@
import type { WindowNames } from '@runtime/lib/types'
import type { WindowNames } from '@electron/types/runtime'
import { CONFIG_KEYS, IPC_EVENTS, WINDOW_NAMES } from '@runtime/lib/constants'
import { app, BrowserWindow, BrowserWindowConstructorOptions, ipcMain, IpcMainInvokeEvent, type IpcMainEvent } from 'electron'

40
electron/types/runtime.ts Normal file
View File

@@ -0,0 +1,40 @@
import type { Task } from '@src/lib/task-types';
import { CONFIG_KEYS, WINDOW_NAMES } from '@runtime/lib/constants';
export type ThemeMode = 'dark' | 'light' | 'system';
export type WindowNames = `${WINDOW_NAMES}`;
export type ConfigKeys = `${CONFIG_KEYS}`;
export interface IConfig {
[CONFIG_KEYS.THEME_MODE]: ThemeMode;
[CONFIG_KEYS.PRIMARY_COLOR]: string;
[CONFIG_KEYS.LANGUAGE]: 'zh' | 'en' | 'th';
[CONFIG_KEYS.FONT_SIZE]: number;
[CONFIG_KEYS.MINIMIZE_TO_TRAY]: boolean;
[CONFIG_KEYS.LAUNCH_AT_STARTUP]: boolean;
[CONFIG_KEYS.PROVIDER]?: string;
[CONFIG_KEYS.DEFAULT_MODEL]?: string | null;
[CONFIG_KEYS.AUTO_CHECK_UPDATE]?: boolean;
[CONFIG_KEYS.AUTO_DOWNLOAD_UPDATE]?: boolean;
[CONFIG_KEYS.GATEWAY_AUTO_START]?: boolean;
[CONFIG_KEYS.SELECTED_CHANNELS]: Array<{ id: string; channelName: string; channelUrl: string }>;
[CONFIG_KEYS.IMAGE_CACHE]: Array<[string, any]>;
[CONFIG_KEYS.TASK_LIST]?: Task[];
}
export interface Provider {
id: number;
name: string;
visible?: boolean;
title?: string;
type?: 'OpenAI';
openAISetting?: string;
createdAt: number;
updatedAt: number;
models: string[];
}
export interface OpenAISetting {
baseURL?: string;
apiKey?: string;
}

View File

@@ -0,0 +1,53 @@
export interface ScriptLastRun {
time: string;
success: boolean;
error?: string;
}
export interface AutomationScript {
id: string;
name: string;
description: string;
filename: string;
enabled: boolean;
channel: string;
createdAt: string;
updatedAt: string;
code?: string;
lastRun?: ScriptLastRun;
}
export interface ScriptSaveInput {
id?: string;
name: string;
description: string;
code: string;
channel: string;
enabled: boolean;
}
export interface ScriptExecutionResult {
success: boolean;
exitCode: number | null;
stdoutTail: string;
stderrTail: string;
error?: string;
}
export type ScriptRecordingStatus = 'idle' | 'recording' | 'stopped';
export interface ScriptMetaItem {
id: string;
name: string;
description: string;
filename: string;
enabled: boolean;
channel: string;
createdAt: string;
updatedAt: string;
lastRun?: ScriptLastRun;
}
export interface ScriptsMeta {
scripts: ScriptMetaItem[];
}

View File

@@ -3,7 +3,7 @@ import logManager from '@electron/service/logger'
import configManager from '@electron/service/config-service'
import path from 'node:path'
import { app } from 'electron'
import { runtimeLocaleMessages, type RuntimeMessageTree } from '@runtime/locales/messages'
import { runtimeLocaleMessages, type RuntimeMessageTree } from '@electron/locales/messages'
const messages: Record<string, RuntimeMessageTree> = runtimeLocaleMessages

33
electron/utils/shared.ts Normal file
View File

@@ -0,0 +1,33 @@
export function debounce<T extends (...args: any[]) => any>(
fn: T,
delay: number,
): (...args: Parameters<T>) => void {
let timer: NodeJS.Timeout | null = null;
return function debounced(this: unknown, ...args: Parameters<T>) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
export function cloneDeep<T>(obj: T): T {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item) => cloneDeep(item)) as T;
}
const clone = Object.assign({}, obj);
for (const key in clone) {
if (Object.prototype.hasOwnProperty.call(clone, key)) {
clone[key] = cloneDeep(clone[key]);
}
}
return clone;
}