refactor(core,server): 统一模块职责并消除类型重复
- 统一 ToolStatus 类型(Core 导出,Server 引用) - 重命名 Server SessionManager 为 SessionMetadataManager - 扩展 Core PermissionContext 添加结构化字段 - 统一 Part 类型导出(Core 定义存储格式,Server 定义展示格式) - 简化 Message 格式(移除 MergedMessage,统一使用 Message) - 添加向后兼容的类型别名和 @deprecated 注释
This commit is contained in:
@@ -6,12 +6,11 @@
|
||||
import { randomUUID } from 'crypto';
|
||||
import { broadcastToSession } from '../ws.js';
|
||||
import type {
|
||||
PermissionType,
|
||||
PermissionRequestPayload,
|
||||
PermissionRequestContext,
|
||||
ServerMessage,
|
||||
} from '../types.js';
|
||||
import type { PermissionDecision, PermissionContext } from '@ai-assistant/core';
|
||||
import type { PermissionDecision, PermissionContext, PermissionType } from '@ai-assistant/core';
|
||||
|
||||
// 等待中的权限请求
|
||||
interface PendingRequest {
|
||||
@@ -57,14 +56,21 @@ function isAutoApproved(sessionId: string, ctx: PermissionContext): boolean {
|
||||
const config = sessionAutoApprove.get(sessionId);
|
||||
if (!config) return false;
|
||||
|
||||
const command = ctx.command.toLowerCase();
|
||||
// 优先使用结构化字段判断
|
||||
if (ctx.permissionType === 'file') {
|
||||
if (ctx.fileOperation === 'write' && config.file?.write === 'allow') {
|
||||
return true;
|
||||
}
|
||||
if (ctx.fileOperation === 'edit' && config.file?.edit === 'allow') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否为文件写入操作
|
||||
// 向后兼容:解析 command 字符串
|
||||
const command = ctx.command.toLowerCase();
|
||||
if (command.startsWith('write ') && config.file?.write === 'allow') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 检查是否为文件编辑操作
|
||||
if (command.startsWith('edit ') && config.file?.edit === 'allow') {
|
||||
return true;
|
||||
}
|
||||
@@ -73,17 +79,22 @@ function isAutoApproved(sessionId: string, ctx: PermissionContext): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从命令或上下文检测权限类型
|
||||
* 从上下文获取权限类型
|
||||
* 优先使用结构化的 permissionType 字段,否则解析 command 字符串
|
||||
*/
|
||||
function detectPermissionType(ctx: PermissionContext): PermissionType {
|
||||
function getPermissionType(ctx: PermissionContext): PermissionType {
|
||||
// 优先使用结构化字段
|
||||
if (ctx.permissionType) {
|
||||
return ctx.permissionType;
|
||||
}
|
||||
|
||||
// 向后兼容:解析 command 字符串
|
||||
const command = ctx.command.toLowerCase();
|
||||
|
||||
// 检测 git 操作
|
||||
if (command.startsWith('git ')) {
|
||||
return 'git';
|
||||
}
|
||||
|
||||
// 检测文件操作
|
||||
const fileOps = ['read', 'write', 'edit', 'delete', 'move', 'copy', 'mkdir'];
|
||||
for (const op of fileOps) {
|
||||
if (command.startsWith(`${op} `)) {
|
||||
@@ -91,46 +102,42 @@ function detectPermissionType(ctx: PermissionContext): PermissionType {
|
||||
}
|
||||
}
|
||||
|
||||
// 检测 web 操作
|
||||
if (command.includes('fetch') || command.includes('http')) {
|
||||
if (command.includes('fetch') || command.includes('http') || command.startsWith('web_search')) {
|
||||
return 'web';
|
||||
}
|
||||
|
||||
// 默认为 bash
|
||||
return 'bash';
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建权限请求上下文
|
||||
* 使用 Core 传递的结构化字段,减少字符串解析
|
||||
*/
|
||||
function buildRequestContext(ctx: PermissionContext): PermissionRequestContext {
|
||||
const permType = detectPermissionType(ctx);
|
||||
const permType = getPermissionType(ctx);
|
||||
|
||||
switch (permType) {
|
||||
case 'file': {
|
||||
const parts = ctx.command.split(' ');
|
||||
const operation = parts[0];
|
||||
const path = parts.slice(1).join(' ');
|
||||
case 'file':
|
||||
return {
|
||||
operation,
|
||||
path,
|
||||
command: ctx.command,
|
||||
operation: ctx.fileOperation || ctx.command.split(' ')[0],
|
||||
path: ctx.filePath || ctx.command.split(' ').slice(1).join(' '),
|
||||
patterns: ctx.patterns,
|
||||
externalPaths: ctx.externalPaths,
|
||||
};
|
||||
}
|
||||
case 'git': {
|
||||
const gitOp = ctx.command.replace(/^git\s+/, '').split(' ')[0];
|
||||
|
||||
case 'git':
|
||||
return {
|
||||
command: ctx.command,
|
||||
gitOperation: gitOp,
|
||||
gitOperation: ctx.gitOperation || ctx.command.replace(/^git\s+/, '').split(' ')[0],
|
||||
};
|
||||
}
|
||||
case 'web': {
|
||||
|
||||
case 'web':
|
||||
return {
|
||||
command: ctx.command,
|
||||
query: ctx.command,
|
||||
query: ctx.webQuery || ctx.command,
|
||||
};
|
||||
}
|
||||
|
||||
default:
|
||||
return {
|
||||
command: ctx.command,
|
||||
@@ -155,7 +162,7 @@ export function createServerPermissionCallback(sessionId: string) {
|
||||
}
|
||||
|
||||
const requestId = randomUUID();
|
||||
const permissionType = detectPermissionType(permCtx);
|
||||
const permissionType = getPermissionType(permCtx);
|
||||
const context = buildRequestContext(permCtx);
|
||||
|
||||
// 构建请求 payload
|
||||
|
||||
Reference in New Issue
Block a user