Files
zn-ai/src-react/lib/runtime.ts
duanshuwen b1dea9a5c2 feat: implement task management store with IPC integration
- Added a new task store in `src-react/stores/task.ts` to manage tasks and their statuses.
- Implemented functions for creating, executing, and retrying tasks, along with handling task progress and completion.
- Introduced persistence for tasks using IPC.
- Created utility functions for normalizing room types and building subtasks.
- Added a new CSS file for global styles in `src-react/styles.css`.
- Created runtime types in `src-react/types/runtime.ts` and exported them.
- Updated the main entry points for Vue and React applications to support dynamic framework loading.
- Refactored chat model interfaces and utility functions into `src/shared/chat-model.ts`.
- Updated TypeScript configuration to include paths for React components and types.
- Enhanced Vite configuration to support both Vue and React frameworks.
2026-04-17 07:09:56 +08:00

77 lines
2.4 KiB
TypeScript

import { IPC_EVENTS, WINDOW_NAMES } from './constants';
import { invokeIpc } from './host-api';
import type { RuntimePlatform, WindowIdentity, WindowName } from '../types/runtime';
function normalizePlatform(platform: string | undefined | null): RuntimePlatform {
const value = platform?.toLowerCase() ?? '';
if (value === 'win32' || value.includes('win')) return 'win32';
if (value === 'darwin' || value.includes('mac')) return 'darwin';
if (value === 'linux') return 'linux';
if (value) return 'unknown';
return 'web';
}
export function hasIpcBridge(): boolean {
return typeof window !== 'undefined' && Boolean(window.api?.invoke);
}
export function detectRuntimePlatform(): RuntimePlatform {
if (typeof window === 'undefined') return 'unknown';
const exposedPlatform = window.api?.platform;
if (exposedPlatform) return normalizePlatform(exposedPlatform);
if (typeof navigator !== 'undefined') {
const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
return normalizePlatform(nav.platform || nav.userAgentData?.platform);
}
return window.api ? 'unknown' : 'web';
}
function readWindowIdFromBridge(): string | number | null {
if (typeof window.api?.getCurrentWindowId === 'function') {
return window.api.getCurrentWindowId();
}
return null;
}
export function detectWindowName(windowId?: string | number | null): WindowName {
if (typeof windowId === 'string') {
const normalized = windowId.toLowerCase();
if (normalized.includes('setting')) return WINDOW_NAMES.SETTING;
if (normalized.includes('dialog')) return WINDOW_NAMES.DIALOG;
if (normalized.includes('loading')) return WINDOW_NAMES.LOADING;
}
return WINDOW_NAMES.MAIN;
}
export async function resolveWindowIdentity(): Promise<WindowIdentity> {
const platform = detectRuntimePlatform();
const isElectron = platform !== 'web' && platform !== 'unknown';
if (!hasIpcBridge()) {
return {
platform,
windowId: null,
windowName: WINDOW_NAMES.MAIN,
isElectron,
};
}
let windowId: string | number | null = null;
try {
windowId = readWindowIdFromBridge();
if (windowId === null) {
windowId = await invokeIpc<string | number | null>(IPC_EVENTS.GET_WINDOW_ID);
}
} catch {
windowId = null;
}
return {
platform,
windowId,
windowName: detectWindowName(windowId),
isElectron,
};
}