tangweijie 7b40485f60 feat: 初始化 Monorepo 项目结构
- 添加 pnpm workspace 和 Turborepo 配置
- 创建 packages/shared 共享类型和工具
- 创建 packages/core-sdk 核心 SDK
- 创建 packages/vscode-extension VSCode 插件
- 创建 packages/jetbrains-plugin JetBrains 插件基础结构
- 添加 README 文档
2026-01-05 18:08:18 +08:00

102 lines
2.6 KiB
TypeScript

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<typeof setTimeout> | 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<void> {
await this.behaviorCollector.flushStats();
}
/**
* 获取当前活动状态
*/
public getActivityStatus(): {
isActive: boolean;
lastActivityTime: number;
aiUsageStats: Record<string, number>;
} {
return {
isActive: Date.now() - this.lastActivityTime < this.INACTIVITY_THRESHOLD,
lastActivityTime: this.lastActivityTime,
aiUsageStats: this.behaviorCollector.getAIUsageStats(),
};
}
}