docs/archives/123-advanced-features-implementation/experience.md
核心原则: 使用可选字段扩展而非重写接口
// ✅ 正确的扩展方式
interface OptimizationRequest {
// 现有字段保持不变
optimizationMode: OptimizationMode;
targetPrompt: string;
// 新功能作为可选字段
advancedContext?: {
variables?: Record<string, string>;
messages?: ConversationMessage[];
};
}
// ❌ 错误的方式:创建新接口
interface AdvancedOptimizationRequest extends OptimizationRequest {
// 这会破坏现有代码
}
适用场景: 任何需要扩展现有功能的情况 关键价值: 确保现有用户无感升级,新用户可选择使用高级功能
设计思想: 通过导航菜单实现功能的渐进式暴露
<!-- 高级模式按钮 - 始终可见,引导用户发现 -->
<ActionButtonUI
icon="🚀"
:text="$t('nav.advancedMode')"
@click="toggleAdvancedMode"
:class="{ 'active-button': advancedModeEnabled }"
/>
<!-- 变量管理按钮 - 仅在高级模式下显示 -->
<ActionButtonUI
v-if="advancedModeEnabled"
icon="📊"
:text="$t('nav.variableManager')"
@click="showVariableManager = true"
/>
核心价值: 既保持简洁性,又提供高级功能的可发现性
架构策略: 抽象统一接口,各提供商适配转换
// 统一的工具调用结果格式
export interface ToolCall {
id: string;
type: 'function';
function: {
name: string;
arguments: string;
};
}
// OpenAI直接映射
const openaiToolCall = chunk.choices[0]?.delta?.tool_calls?.[0];
// Gemini需要转换
const geminiToolCall: ToolCall = {
id: `call_${Date.now()}`,
type: 'function' as const,
function: {
name: functionCall.name,
arguments: JSON.stringify(functionCall.args)
}
};
关键优势: 新增LLM提供商时只需实现转换逻辑,业务代码无需修改
模式: 组合式API + 服务注入
// ✅ 推荐的状态管理方式
export function useVariableManager() {
const customVariables = ref<Record<string, string>>({});
// 响应式计算属性
const allVariables = computed(() => {
return { ...predefinedVariables.value, ...customVariables.value };
});
// 方法封装
const setVariable = (name: string, value: string) => {
customVariables.value[name] = value;
saveToStorage(); // 自动持久化
};
return { allVariables, setVariable };
}
避坑指南: 不要在computed中进行副作用操作,保持纯函数特性
关键技巧: 使用字面量类型和断言确保类型安全
// 问题:string类型不能赋值给字面量类型
const toolCall = {
type: 'function' // TypeScript推断为string
};
// 解决方案1:使用as const断言
const toolCall = {
type: 'function' as const // 推断为'function'
};
// 解决方案2:显式类型声明
const toolCall: ToolCall = {
type: 'function' // 符合ToolCall类型定义
};
问题: 多个组件实例导致状态不一致 解决方案: 统一实例管理
// App.vue 创建统一实例
const variableManager = new VariableManager();
// 子组件优先使用传入实例
const activeVariableManager = computed(() => {
return props.variableManager || localVariableManager;
});
关键原则: 单一数据源,避免状态分散
策略: 使用语义化CSS类而非硬编码样式
<!-- ❌ 硬编码样式 -->
<div class="bg-white dark:bg-gray-800 border rounded-lg p-4">
<!-- ✅ 使用主题系统 -->
<div class="theme-manager-card theme-manager-padding">
优势: 自动适配主题切换,减少维护成本
错误做法: 过早创建新接口
// ❌ 不要过早抽象
interface BasicRequest { /* ... */ }
interface AdvancedRequest { /* ... */ }
interface SuperAdvancedRequest { /* ... */ }
正确做法: 基于可选字段渐进式扩展
// ✅ 渐进式扩展
interface Request {
// 核心字段
basic: string;
// 第一次扩展
advanced?: AdvancedOptions;
// 第二次扩展
superAdvanced?: SuperAdvancedOptions;
}
反模式: 深层props传递
<!-- ❌ 避免深层传递 -->
<GrandParent>
<Parent :data="data">
<Child :data="data">
<GrandChild :data="data" />
</Child>
</Parent>
</GrandParent>
推荐模式: 服务层解耦
// ✅ 通过服务层通信
const variableService = inject('variableService');
// 任何组件都可以直接使用服务
错误时机: 功能未完成就开始优化 正确时机: 功能完整后针对性优化
// 功能完成后的性能优化
const debouncedSave = debounce(saveVariables, 300);
const virtualizedList = useVirtualList(largeVariableList);
错误方式: 只测试正常流程 正确方式: 重点测试边界情况
describe('VariableManager', () => {
it('should handle invalid variable names', () => {
// 测试特殊字符、空字符串、保留字等
});
it('should recover from storage corruption', () => {
// 测试存储数据损坏的恢复机制
});
});
UI层职责: 用户交互、状态展示、本地状态管理
// UI层:变量管理UI逻辑
export class VariableManagerUI {
private customVariables = ref({});
// UI相关方法:验证、格式化、本地存储
validateAndSave(name: string, value: string) { /* ... */ }
}
Core层职责: 业务逻辑、数据处理、API调用
// Core层:模板处理逻辑
export class TemplateProcessor {
// 纯业务逻辑:变量替换、模板渲染
replaceVariables(template: string, variables: Record<string, string>) { /* ... */ }
}
策略: 预留扩展接口,支持插件化
export interface IVariableProvider {
getVariables(): Record<string, string>;
}
export class VariableManager {
private providers: IVariableProvider[] = [];
// 支持插件注册
addProvider(provider: IVariableProvider) {
this.providers.push(provider);
}
}
// 层次1:业务逻辑错误
class VariableValidationError extends Error {
constructor(variableName: string) {
super(`Invalid variable name: ${variableName}`);
}
}
// 层次2:系统级错误
class StorageError extends Error {
constructor(operation: string) {
super(`Storage ${operation} failed`);
}
}
// 层次3:用户友好提示
const handleError = (error: Error) => {
if (error instanceof VariableValidationError) {
toast.warning('变量名格式不正确');
} else {
toast.error('操作失败,请重试');
}
};
创新点: 根据优化模式自动生成合适的测试环境
// 传统方式:用户手动配置测试环境
// 创新方式:智能模板生成
if (optimizationMode === 'system') {
// 系统提示词:创建系统+用户消息对
conversationMessages = [
{ role: 'system', content: '{{currentPrompt}}' },
{ role: 'user', content: '请展示你的能力并与我互动' }
];
} else {
// 用户提示词:创建用户消息
conversationMessages = [
{ role: 'user', content: '{{currentPrompt}}' }
];
}
设计决策: 变量系统和工具系统完全分离 理由: 避免概念混淆,简化用户理解
// 变量:用于内容模板化
const variables = { userName: 'Alice', task: 'coding' };
const template = 'Hello {{userName}}, let\'s start {{task}}';
// 工具:用于LLM功能调用
const tools = [{
function: { name: 'get_weather', parameters: { ... } }
}];
创新: 通过UI状态控制功能可见性
const featureVisibility = computed(() => ({
basicMode: true,
advancedMode: advancedModeEnabled.value,
variableManager: advancedModeEnabled.value,
toolManager: advancedModeEnabled.value && hasTools.value
}));
适用于任何需要向后兼容的功能增强场景:
适用于复杂状态管理场景:
适用于需要集成多个第三方服务的场景:
这些经验和模式可以应用到任何大型功能迭代过程中,特别是需要保持向后兼容性和用户体验平滑升级的场景。