import * as vscode from 'vscode'; import { Collector, UserBehaviorCollector } from '@ide-collector/core-sdk'; import { debounce } from '@ide-collector/shared'; /** * 编辑器适配器 * 用于捕获编辑器活动和用户行为 */ export class EditorAdapter { private collector: Collector; private behaviorCollector: UserBehaviorCollector; private lastActivityTime: number = 0; private inactivityTimeout: ReturnType | null = null; private readonly INACTIVITY_THRESHOLD = 300000; // 5分钟 constructor(collector: Collector) { this.collector = collector; this.behaviorCollector = new UserBehaviorCollector(collector); } /** * 编辑器切换时调用 */ public onEditorChange(editor: vscode.TextEditor): void { this.recordActivity(); // 记录当前编辑的语言 const language = editor.document.languageId; this.behaviorCollector.trackAIUsage(`editor_language_${language}`); } /** * 文档变化时调用 */ public onDocumentChange = debounce( (event: vscode.TextDocumentChangeEvent): void => { this.recordActivity(); // 检测是否可能是 AI 生成的代码(大量文本一次性插入) for (const change of event.contentChanges) { if (change.text.length > 50 && change.rangeLength === 0) { this.behaviorCollector.trackAIUsage('possible_ai_insertion'); } } }, 500 ); /** * 选择变化时调用 */ public onSelectionChange(event: vscode.TextEditorSelectionChangeEvent): void { this.recordActivity(); } /** * 记录活动 */ private recordActivity(): void { const now = Date.now(); // 如果之前处于不活跃状态,开始新的活动周期 if (now - this.lastActivityTime > this.INACTIVITY_THRESHOLD) { this.behaviorCollector.startActivity(); } this.lastActivityTime = now; // 重置不活跃计时器 if (this.inactivityTimeout) { clearTimeout(this.inactivityTimeout); } this.inactivityTimeout = setTimeout(() => { this.onInactive(); }, this.INACTIVITY_THRESHOLD); } /** * 不活跃时调用 */ private async onInactive(): Promise { await this.behaviorCollector.flushStats(); } /** * 获取当前活动状态 */ public getActivityStatus(): { isActive: boolean; lastActivityTime: number; aiUsageStats: Record; } { return { isActive: Date.now() - this.lastActivityTime < this.INACTIVITY_THRESHOLD, lastActivityTime: this.lastActivityTime, aiUsageStats: this.behaviorCollector.getAIUsageStats(), }; } }