移除了所有Hermes相关的命名引用,包括: - 从.gitignore中清理相关构建缓存文件 - 将README中的beaver-home路径配置更新 - 完善backend/README.md文档说明Beaver后端主线实现 - 移除Hermes风格的相关注释和兼容性代码 - 清理nanobot环境变量兼容性处理 - 删除技能迁移和服务迁移相关功能代码 - 更新测试用例中相关命名和函数名 BREAKING CHANGE: 移除了Hermes迁移相关API和CLI命令,不再支持nanobot环境变量兼容性
122 lines
3.6 KiB
TypeScript
122 lines
3.6 KiB
TypeScript
'use client';
|
|
|
|
import React from 'react';
|
|
|
|
import { getStatus, listSessions, wsManager } from '@/lib/api';
|
|
import { useChatStore } from '@/lib/store';
|
|
import type { ProcessWsEvent, SessionUpdatedEvent, WsEvent } from '@/types';
|
|
|
|
function scheduleWhenIdle(task: () => void, timeout = 1200): () => void {
|
|
if (typeof window === 'undefined') {
|
|
task();
|
|
return () => {};
|
|
}
|
|
|
|
const idleWindow = window as Window &
|
|
typeof globalThis & {
|
|
requestIdleCallback?: (callback: IdleRequestCallback, options?: IdleRequestOptions) => number;
|
|
cancelIdleCallback?: (handle: number) => void;
|
|
};
|
|
|
|
if (typeof idleWindow.requestIdleCallback === 'function') {
|
|
const id = idleWindow.requestIdleCallback(() => task(), { timeout });
|
|
return () => idleWindow.cancelIdleCallback?.(id);
|
|
}
|
|
|
|
const id = globalThis.setTimeout(task, 250);
|
|
return () => globalThis.clearTimeout(id);
|
|
}
|
|
|
|
function isProcessEvent(data: WsEvent | Record<string, unknown>): data is ProcessWsEvent {
|
|
const type = typeof data.type === 'string' ? data.type : '';
|
|
return type.startsWith('process_') || type === 'process_cancel_ack';
|
|
}
|
|
|
|
function isSessionUpdatedEvent(data: WsEvent | Record<string, unknown>): data is SessionUpdatedEvent {
|
|
return data.type === 'session_updated' && typeof data.session_id === 'string';
|
|
}
|
|
|
|
export function AppRuntimeBridge() {
|
|
const sessionId = useChatStore((state) => state.sessionId);
|
|
const setSessions = useChatStore((state) => state.setSessions);
|
|
const setWsStatus = useChatStore((state) => state.setWsStatus);
|
|
const setBeaverReady = useChatStore((state) => state.setBeaverReady);
|
|
const resetProcessState = useChatStore((state) => state.resetProcessState);
|
|
const ingestProcessEvent = useChatStore((state) => state.ingestProcessEvent);
|
|
const statusCheckCleanupRef = React.useRef<(() => void) | null>(null);
|
|
const statusCheckInFlightRef = React.useRef(false);
|
|
|
|
const loadSessions = React.useCallback(async () => {
|
|
try {
|
|
const sessions = await listSessions();
|
|
setSessions(sessions);
|
|
} catch {
|
|
// backend may still be offline during first render
|
|
}
|
|
}, [setSessions]);
|
|
|
|
const scheduleStatusCheck = React.useCallback(() => {
|
|
if (statusCheckInFlightRef.current) return;
|
|
|
|
statusCheckCleanupRef.current?.();
|
|
statusCheckCleanupRef.current = scheduleWhenIdle(async () => {
|
|
statusCheckInFlightRef.current = true;
|
|
try {
|
|
await getStatus();
|
|
setBeaverReady(true);
|
|
} catch {
|
|
setBeaverReady(false);
|
|
} finally {
|
|
statusCheckInFlightRef.current = false;
|
|
}
|
|
});
|
|
}, [setBeaverReady]);
|
|
|
|
React.useEffect(() => {
|
|
void loadSessions();
|
|
}, [loadSessions]);
|
|
|
|
React.useEffect(() => {
|
|
resetProcessState();
|
|
wsManager.connect(sessionId);
|
|
}, [resetProcessState, sessionId]);
|
|
|
|
React.useEffect(() => {
|
|
const unsubStatus = wsManager.onStatusChange((status) => {
|
|
setWsStatus(status);
|
|
if (status === 'connected') {
|
|
scheduleStatusCheck();
|
|
} else {
|
|
statusCheckCleanupRef.current?.();
|
|
statusCheckCleanupRef.current = null;
|
|
setBeaverReady(null);
|
|
}
|
|
});
|
|
|
|
return () => {
|
|
statusCheckCleanupRef.current?.();
|
|
statusCheckCleanupRef.current = null;
|
|
unsubStatus();
|
|
};
|
|
}, [scheduleStatusCheck, setBeaverReady, setWsStatus]);
|
|
|
|
React.useEffect(() => {
|
|
const unsubMessage = wsManager.onMessage((data) => {
|
|
if (isSessionUpdatedEvent(data)) {
|
|
void loadSessions();
|
|
return;
|
|
}
|
|
|
|
if (isProcessEvent(data)) {
|
|
ingestProcessEvent(data);
|
|
}
|
|
});
|
|
|
|
return () => {
|
|
unsubMessage();
|
|
};
|
|
}, [ingestProcessEvent, loadSessions]);
|
|
|
|
return null;
|
|
}
|