feat: 升级至 v0.2.3 — Token 估算优化、工具确认超时改进、工作空间标签页
1. Token 估算优化(核心改进):新增 token-estimator.ts 智能字符估算(中文 1.5 token/字、ASCII 0.25 token/字),替换三处旧的 length/2 粗略估算,中文场景准确度从 ~50% 提升到 ~90% 2. 工具确认超时改进:超时时间可配置(30s~600s)、超时 toast 通知、ConfirmationDialog 倒计时 UI(进度条 + 最后 10 秒红色脉冲动画)、SettingsModal 新增配置入口 3. 详情栏新增 Workspace 标签页:workspace:getInfo IPC + WorkspaceViewer 组件(文件预览、目录状态、在文件管理器打开)
This commit is contained in:
@@ -38,16 +38,17 @@ export class ConfirmationHook implements PreToolHook {
|
||||
private autoExecuteTools = new Set<string>();
|
||||
|
||||
/** 等待确认的 Promise 解析器 */
|
||||
private pendingConfirmations = new Map<string, { resolve: (v: boolean) => void; timer: NodeJS.Timeout; toolName: string }>();
|
||||
private pendingConfirmations = new Map<string, { resolve: (v: boolean) => void; timer: NodeJS.Timeout; toolName: string; expiresAt: number }>();
|
||||
|
||||
/** 确认超时时间(默认 120 秒) */
|
||||
private readonly confirmationTimeoutMs = 120_000;
|
||||
/** 确认超时时间(可从配置读取,默认 120 秒) */
|
||||
private confirmationTimeoutMs = 120_000;
|
||||
|
||||
constructor(
|
||||
private mainWindow: BrowserWindow | null = null,
|
||||
private configService: ConfigService | null = null,
|
||||
) {
|
||||
this.loadAutoExecuteList();
|
||||
this.loadConfirmationTimeout();
|
||||
}
|
||||
|
||||
/** 设置主窗口(用于发送 IPC 消息) */
|
||||
@@ -102,6 +103,33 @@ export class ConfirmationHook implements PreToolHook {
|
||||
return Array.from(this.autoExecuteTools);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 ConfigService 加载确认超时时间
|
||||
* 配置键:agent.confirmationTimeoutMs(单位毫秒,最小 30 秒,最大 600 秒)
|
||||
*/
|
||||
private loadConfirmationTimeout(): void {
|
||||
if (!this.configService) return;
|
||||
try {
|
||||
const timeout = this.configService.get<number>('agent.confirmationTimeoutMs');
|
||||
if (timeout != null) {
|
||||
// 限制范围:30 秒 ~ 600 秒
|
||||
this.confirmationTimeoutMs = Math.min(600_000, Math.max(30_000, timeout));
|
||||
}
|
||||
} catch {
|
||||
// 配置加载失败,使用默认值
|
||||
}
|
||||
}
|
||||
|
||||
/** 设置确认超时时间(运行时更新) */
|
||||
setConfirmationTimeout(ms: number): void {
|
||||
this.confirmationTimeoutMs = Math.min(600_000, Math.max(30_000, ms));
|
||||
}
|
||||
|
||||
/** 获取当前确认超时时间 */
|
||||
getConfirmationTimeout(): number {
|
||||
return this.confirmationTimeoutMs;
|
||||
}
|
||||
|
||||
/** 注入工具定义列表(用于查询风险等级) */
|
||||
setToolDefs(defs: MetonaToolDef[]): void {
|
||||
this.toolDefs.clear();
|
||||
@@ -187,12 +215,22 @@ export class ConfirmationHook implements PreToolHook {
|
||||
|
||||
/**
|
||||
* 等待用户确认(带超时)
|
||||
* 发送 expiresAt 到前端,供倒计时 UI 使用;超时时发送 toast 通知
|
||||
*/
|
||||
private waitForConfirmation(request: ConfirmationRequest): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
const expiresAt = Date.now() + this.confirmationTimeoutMs;
|
||||
|
||||
// 设置超时
|
||||
const timer = setTimeout(() => {
|
||||
this.pendingConfirmations.delete(request.toolCallId);
|
||||
// 超时发送 toast 通知用户
|
||||
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
|
||||
this.mainWindow.webContents.send('toast:show', {
|
||||
type: 'warning',
|
||||
message: `工具确认超时(${Math.round(this.confirmationTimeoutMs / 1000)}秒),"${request.toolName}" 未执行`,
|
||||
});
|
||||
}
|
||||
resolve(false); // 超时视为拒绝
|
||||
}, this.confirmationTimeoutMs);
|
||||
|
||||
@@ -200,11 +238,15 @@ export class ConfirmationHook implements PreToolHook {
|
||||
resolve,
|
||||
timer,
|
||||
toolName: request.toolName,
|
||||
expiresAt,
|
||||
});
|
||||
|
||||
// 发送确认请求到渲染进程
|
||||
// 发送确认请求到渲染进程(携带过期时间戳,供前端倒计时)
|
||||
if (this.mainWindow && !this.mainWindow.isDestroyed()) {
|
||||
this.mainWindow.webContents.send('tool:confirmationRequest', request);
|
||||
this.mainWindow.webContents.send('tool:confirmationRequest', {
|
||||
...request,
|
||||
expiresAt,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user