tabby-ai-assistant 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/chat/ai-sidebar.component.d.ts +147 -0
- package/dist/components/chat/chat-interface.component.d.ts +38 -6
- package/dist/components/settings/general-settings.component.d.ts +6 -3
- package/dist/components/settings/provider-config.component.d.ts +25 -12
- package/dist/components/terminal/command-preview.component.d.ts +38 -0
- package/dist/index-full.d.ts +8 -0
- package/dist/index-minimal.d.ts +3 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +1 -2
- package/dist/providers/tabby/ai-config.provider.d.ts +57 -5
- package/dist/providers/tabby/ai-hotkey.provider.d.ts +8 -14
- package/dist/providers/tabby/ai-toolbar-button.provider.d.ts +8 -9
- package/dist/services/chat/ai-sidebar.service.d.ts +89 -0
- package/dist/services/chat/chat-history.service.d.ts +78 -0
- package/dist/services/chat/chat-session.service.d.ts +57 -2
- package/dist/services/context/compaction.d.ts +90 -0
- package/dist/services/context/manager.d.ts +69 -0
- package/dist/services/context/memory.d.ts +116 -0
- package/dist/services/context/token-budget.d.ts +105 -0
- package/dist/services/core/ai-assistant.service.d.ts +40 -1
- package/dist/services/core/checkpoint.service.d.ts +130 -0
- package/dist/services/platform/escape-sequence.service.d.ts +132 -0
- package/dist/services/platform/platform-detection.service.d.ts +146 -0
- package/dist/services/providers/anthropic-provider.service.d.ts +5 -0
- package/dist/services/providers/base-provider.service.d.ts +6 -1
- package/dist/services/providers/glm-provider.service.d.ts +5 -0
- package/dist/services/providers/minimax-provider.service.d.ts +10 -1
- package/dist/services/providers/ollama-provider.service.d.ts +76 -0
- package/dist/services/providers/openai-compatible.service.d.ts +5 -0
- package/dist/services/providers/openai-provider.service.d.ts +5 -0
- package/dist/services/providers/vllm-provider.service.d.ts +82 -0
- package/dist/services/terminal/buffer-analyzer.service.d.ts +128 -0
- package/dist/services/terminal/terminal-manager.service.d.ts +185 -0
- package/dist/services/terminal/terminal-tools.service.d.ts +79 -0
- package/dist/types/ai.types.d.ts +92 -0
- package/dist/types/provider.types.d.ts +1 -1
- package/package.json +7 -10
- package/src/components/chat/ai-sidebar.component.ts +945 -0
- package/src/components/chat/chat-input.component.html +9 -24
- package/src/components/chat/chat-input.component.scss +3 -2
- package/src/components/chat/chat-interface.component.html +77 -69
- package/src/components/chat/chat-interface.component.scss +54 -4
- package/src/components/chat/chat-interface.component.ts +250 -34
- package/src/components/chat/chat-settings.component.scss +4 -4
- package/src/components/chat/chat-settings.component.ts +22 -11
- package/src/components/common/error-message.component.html +15 -0
- package/src/components/common/error-message.component.scss +77 -0
- package/src/components/common/error-message.component.ts +2 -96
- package/src/components/common/loading-spinner.component.html +4 -0
- package/src/components/common/loading-spinner.component.scss +57 -0
- package/src/components/common/loading-spinner.component.ts +2 -63
- package/src/components/security/consent-dialog.component.html +22 -0
- package/src/components/security/consent-dialog.component.scss +34 -0
- package/src/components/security/consent-dialog.component.ts +2 -55
- package/src/components/security/password-prompt.component.html +19 -0
- package/src/components/security/password-prompt.component.scss +30 -0
- package/src/components/security/password-prompt.component.ts +2 -54
- package/src/components/security/risk-confirm-dialog.component.html +8 -12
- package/src/components/security/risk-confirm-dialog.component.scss +8 -5
- package/src/components/security/risk-confirm-dialog.component.ts +6 -6
- package/src/components/settings/ai-settings-tab.component.html +16 -20
- package/src/components/settings/ai-settings-tab.component.scss +8 -5
- package/src/components/settings/ai-settings-tab.component.ts +12 -12
- package/src/components/settings/general-settings.component.html +8 -17
- package/src/components/settings/general-settings.component.scss +6 -3
- package/src/components/settings/general-settings.component.ts +62 -22
- package/src/components/settings/provider-config.component.html +19 -39
- package/src/components/settings/provider-config.component.scss +182 -39
- package/src/components/settings/provider-config.component.ts +119 -7
- package/src/components/settings/security-settings.component.scss +1 -1
- package/src/components/terminal/ai-toolbar-button.component.html +8 -0
- package/src/components/terminal/ai-toolbar-button.component.scss +20 -0
- package/src/components/terminal/ai-toolbar-button.component.ts +2 -30
- package/src/components/terminal/command-preview.component.html +61 -0
- package/src/components/terminal/command-preview.component.scss +72 -0
- package/src/components/terminal/command-preview.component.ts +127 -140
- package/src/components/terminal/command-suggestion.component.html +23 -0
- package/src/components/terminal/command-suggestion.component.scss +55 -0
- package/src/components/terminal/command-suggestion.component.ts +2 -77
- package/src/index-minimal.ts +32 -0
- package/src/index.ts +94 -11
- package/src/index.ts.backup +165 -0
- package/src/providers/tabby/ai-config.provider.ts +60 -51
- package/src/providers/tabby/ai-hotkey.provider.ts +23 -39
- package/src/providers/tabby/ai-settings-tab.provider.ts +2 -2
- package/src/providers/tabby/ai-toolbar-button.provider.ts +29 -24
- package/src/services/chat/ai-sidebar.service.ts +258 -0
- package/src/services/chat/chat-history.service.ts +308 -0
- package/src/services/chat/chat-history.service.ts.backup +239 -0
- package/src/services/chat/chat-session.service.ts +276 -3
- package/src/services/context/compaction.ts +483 -0
- package/src/services/context/manager.ts +442 -0
- package/src/services/context/memory.ts +519 -0
- package/src/services/context/token-budget.ts +422 -0
- package/src/services/core/ai-assistant.service.ts +280 -5
- package/src/services/core/ai-provider-manager.service.ts +2 -2
- package/src/services/core/checkpoint.service.ts +619 -0
- package/src/services/platform/escape-sequence.service.ts +499 -0
- package/src/services/platform/platform-detection.service.ts +494 -0
- package/src/services/providers/anthropic-provider.service.ts +28 -1
- package/src/services/providers/base-provider.service.ts +7 -1
- package/src/services/providers/glm-provider.service.ts +28 -1
- package/src/services/providers/minimax-provider.service.ts +209 -11
- package/src/services/providers/ollama-provider.service.ts +445 -0
- package/src/services/providers/openai-compatible.service.ts +9 -0
- package/src/services/providers/openai-provider.service.ts +9 -0
- package/src/services/providers/vllm-provider.service.ts +463 -0
- package/src/services/security/risk-assessment.service.ts +6 -2
- package/src/services/terminal/buffer-analyzer.service.ts +594 -0
- package/src/services/terminal/terminal-manager.service.ts +748 -0
- package/src/services/terminal/terminal-tools.service.ts +441 -0
- package/src/styles/ai-assistant.scss +78 -6
- package/src/types/ai.types.ts +144 -0
- package/src/types/provider.types.ts +1 -1
- package/tsconfig.json +9 -9
- package/webpack.config.js +28 -6
|
@@ -6,13 +6,65 @@ import { ConfigProviderService } from '../../services/core/config-provider.servi
|
|
|
6
6
|
*/
|
|
7
7
|
export declare class AiConfigProvider extends ConfigProvider {
|
|
8
8
|
private configService;
|
|
9
|
-
constructor(configService: ConfigProviderService);
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
10
|
+
* 默认配置
|
|
12
11
|
*/
|
|
13
|
-
|
|
12
|
+
defaults: {
|
|
13
|
+
hotkeys: {
|
|
14
|
+
'ai-assistant-toggle': string[];
|
|
15
|
+
'ai-command-generation': string[];
|
|
16
|
+
'ai-explain-command': string[];
|
|
17
|
+
};
|
|
18
|
+
aiAssistant: {
|
|
19
|
+
enabled: boolean;
|
|
20
|
+
defaultProvider: string;
|
|
21
|
+
autoSuggestCommands: boolean;
|
|
22
|
+
enableSecurityChecks: boolean;
|
|
23
|
+
providers: {
|
|
24
|
+
openai: {
|
|
25
|
+
apiKey: string;
|
|
26
|
+
model: string;
|
|
27
|
+
baseURL: string;
|
|
28
|
+
};
|
|
29
|
+
anthropic: {
|
|
30
|
+
apiKey: string;
|
|
31
|
+
model: string;
|
|
32
|
+
baseURL: string;
|
|
33
|
+
};
|
|
34
|
+
minimax: {
|
|
35
|
+
apiKey: string;
|
|
36
|
+
model: string;
|
|
37
|
+
baseURL: string;
|
|
38
|
+
};
|
|
39
|
+
glm: {
|
|
40
|
+
apiKey: string;
|
|
41
|
+
model: string;
|
|
42
|
+
baseURL: string;
|
|
43
|
+
};
|
|
44
|
+
openaiCompatible: {
|
|
45
|
+
apiKey: string;
|
|
46
|
+
model: string;
|
|
47
|
+
baseURL: string;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
security: {
|
|
51
|
+
passwordProtection: boolean;
|
|
52
|
+
riskAssessmentLevel: string;
|
|
53
|
+
consentPersistenceDays: number;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
};
|
|
14
57
|
/**
|
|
15
|
-
*
|
|
58
|
+
* 平台特定默认配置
|
|
16
59
|
*/
|
|
17
|
-
|
|
60
|
+
platformDefaults: {
|
|
61
|
+
macOS: {
|
|
62
|
+
hotkeys: {
|
|
63
|
+
'ai-assistant-toggle': string[];
|
|
64
|
+
'ai-command-generation': string[];
|
|
65
|
+
'ai-explain-command': string[];
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
constructor(configService: ConfigProviderService);
|
|
18
70
|
}
|
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
import { HotkeyProvider, HotkeyDescription } from 'tabby-core';
|
|
2
|
-
import {
|
|
3
|
-
import { AiAssistantService } from '../../services/core/ai-assistant.service';
|
|
1
|
+
import { HotkeyProvider, HotkeyDescription, TranslateService } from 'tabby-core';
|
|
2
|
+
import { AiSidebarService } from '../../services/chat/ai-sidebar.service';
|
|
4
3
|
/**
|
|
5
4
|
* Tabby热键提供者
|
|
6
5
|
* 为Tabby添加AI助手热键支持
|
|
6
|
+
*
|
|
7
|
+
* 注意:热键 ID 必须与 AiConfigProvider.defaults.hotkeys 中定义的一致
|
|
7
8
|
*/
|
|
8
9
|
export declare class AiHotkeyProvider extends HotkeyProvider {
|
|
9
|
-
private
|
|
10
|
-
private
|
|
11
|
-
|
|
10
|
+
private sidebarService;
|
|
11
|
+
private translate;
|
|
12
|
+
hotkeys: HotkeyDescription[];
|
|
13
|
+
constructor(sidebarService: AiSidebarService, translate: TranslateService);
|
|
12
14
|
provide(): Promise<HotkeyDescription[]>;
|
|
13
|
-
/**
|
|
14
|
-
* 打开AI聊天界面
|
|
15
|
-
*/
|
|
16
|
-
private openAiChat;
|
|
17
|
-
/**
|
|
18
|
-
* 打开命令生成功能
|
|
19
|
-
*/
|
|
20
|
-
private openCommandGeneration;
|
|
21
15
|
}
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { ToolbarButtonProvider, ToolbarButton } from 'tabby-core';
|
|
2
|
-
import {
|
|
3
|
-
import { AiAssistantService } from '../../services/core/ai-assistant.service';
|
|
1
|
+
import { ToolbarButtonProvider, ToolbarButton, AppService } from 'tabby-core';
|
|
2
|
+
import { AiSidebarService } from '../../services/chat/ai-sidebar.service';
|
|
4
3
|
/**
|
|
5
4
|
* Tabby工具栏按钮提供者
|
|
6
|
-
* 为Tabby工具栏添加AI
|
|
5
|
+
* 为Tabby工具栏添加AI助手按钮,使用侧边栏方式
|
|
7
6
|
*/
|
|
8
7
|
export declare class AiToolbarButtonProvider extends ToolbarButtonProvider {
|
|
9
|
-
private
|
|
10
|
-
private
|
|
11
|
-
constructor(
|
|
8
|
+
private app;
|
|
9
|
+
private sidebarService;
|
|
10
|
+
constructor(app: AppService, sidebarService: AiSidebarService);
|
|
12
11
|
provide(): ToolbarButton[];
|
|
13
12
|
/**
|
|
14
|
-
*
|
|
13
|
+
* 打开设置页面
|
|
15
14
|
*/
|
|
16
|
-
private
|
|
15
|
+
private openSettings;
|
|
17
16
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ComponentFactoryResolver, ApplicationRef, Injector } from '@angular/core';
|
|
2
|
+
import { ConfigService } from 'tabby-core';
|
|
3
|
+
/**
|
|
4
|
+
* AI Sidebar 配置接口
|
|
5
|
+
*/
|
|
6
|
+
export interface AiSidebarConfig {
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
position?: 'left' | 'right';
|
|
9
|
+
showInToolbar?: boolean;
|
|
10
|
+
sidebarVisible?: boolean;
|
|
11
|
+
sidebarCollapsed?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* AI Sidebar 服务 - 管理 AI 聊天侧边栏的生命周期
|
|
15
|
+
*
|
|
16
|
+
* 采用 Flexbox 布局方式,将 sidebar 插入到 app-root 作为第一个子元素,
|
|
17
|
+
* app-root 变为水平 flex 容器,sidebar 在左侧
|
|
18
|
+
*/
|
|
19
|
+
export declare class AiSidebarService {
|
|
20
|
+
private componentFactoryResolver;
|
|
21
|
+
private appRef;
|
|
22
|
+
private injector;
|
|
23
|
+
private config;
|
|
24
|
+
private sidebarComponentRef;
|
|
25
|
+
private sidebarElement;
|
|
26
|
+
private styleElement;
|
|
27
|
+
private _isVisible;
|
|
28
|
+
private readonly SIDEBAR_WIDTH;
|
|
29
|
+
/**
|
|
30
|
+
* 侧边栏是否可见
|
|
31
|
+
*/
|
|
32
|
+
get sidebarVisible(): boolean;
|
|
33
|
+
constructor(componentFactoryResolver: ComponentFactoryResolver, appRef: ApplicationRef, injector: Injector, config: ConfigService);
|
|
34
|
+
/**
|
|
35
|
+
* 显示 sidebar
|
|
36
|
+
*/
|
|
37
|
+
show(): void;
|
|
38
|
+
/**
|
|
39
|
+
* 隐藏 sidebar
|
|
40
|
+
*/
|
|
41
|
+
hide(): void;
|
|
42
|
+
/**
|
|
43
|
+
* 切换 sidebar 显示状态
|
|
44
|
+
*/
|
|
45
|
+
toggle(): void;
|
|
46
|
+
/**
|
|
47
|
+
* 获取当前显示状态
|
|
48
|
+
*/
|
|
49
|
+
get visible(): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* 初始化 - 应用启动时调用
|
|
52
|
+
*/
|
|
53
|
+
initialize(): void;
|
|
54
|
+
/**
|
|
55
|
+
* 创建 sidebar 组件
|
|
56
|
+
*
|
|
57
|
+
* 使用固定定位方案:
|
|
58
|
+
* 1. 侧边栏 position: fixed,固定在左侧
|
|
59
|
+
* 2. 主内容区通过 margin-left 推开
|
|
60
|
+
* 这样不改变任何现有元素的 flex 布局
|
|
61
|
+
*/
|
|
62
|
+
private createSidebar;
|
|
63
|
+
/**
|
|
64
|
+
* 销毁 sidebar 组件
|
|
65
|
+
*/
|
|
66
|
+
private destroySidebar;
|
|
67
|
+
/**
|
|
68
|
+
* 调整 .content 元素样式 - 只处理第二个(更深层的).content
|
|
69
|
+
*/
|
|
70
|
+
private adjustContentStyles;
|
|
71
|
+
/**
|
|
72
|
+
* 注入布局 CSS - 使用 margin-left 把主内容推开
|
|
73
|
+
*
|
|
74
|
+
* 固定定位方案:侧边栏 fixed,主内容区 margin-left
|
|
75
|
+
*/
|
|
76
|
+
private injectLayoutCSS;
|
|
77
|
+
/**
|
|
78
|
+
* 移除布局 CSS
|
|
79
|
+
*/
|
|
80
|
+
private removeLayoutCSS;
|
|
81
|
+
/**
|
|
82
|
+
* 获取插件配置
|
|
83
|
+
*/
|
|
84
|
+
private getPluginConfig;
|
|
85
|
+
/**
|
|
86
|
+
* 保存插件配置
|
|
87
|
+
*/
|
|
88
|
+
private savePluginConfig;
|
|
89
|
+
}
|
|
@@ -8,6 +8,22 @@ export interface SavedSession {
|
|
|
8
8
|
createdAt: Date;
|
|
9
9
|
updatedAt: Date;
|
|
10
10
|
messageCount: number;
|
|
11
|
+
contextInfo?: {
|
|
12
|
+
tokenUsage?: {
|
|
13
|
+
input: number;
|
|
14
|
+
output: number;
|
|
15
|
+
cacheRead: number;
|
|
16
|
+
cacheWrite: number;
|
|
17
|
+
};
|
|
18
|
+
compactionHistory?: Array<{
|
|
19
|
+
timestamp: Date;
|
|
20
|
+
type: 'prune' | 'compact' | 'truncate';
|
|
21
|
+
tokensSaved: number;
|
|
22
|
+
condenseId?: string;
|
|
23
|
+
}>;
|
|
24
|
+
hasCompression?: boolean;
|
|
25
|
+
lastCompactionAt?: Date;
|
|
26
|
+
};
|
|
11
27
|
}
|
|
12
28
|
/**
|
|
13
29
|
* 聊天历史服务
|
|
@@ -64,4 +80,66 @@ export declare class ChatHistoryService {
|
|
|
64
80
|
private saveToStorage;
|
|
65
81
|
private generateSessionTitle;
|
|
66
82
|
private trimMessages;
|
|
83
|
+
/**
|
|
84
|
+
* 更新会话的上下文信息(压缩标记支持)
|
|
85
|
+
*/
|
|
86
|
+
updateContextInfo(sessionId: string, contextInfo: SavedSession['contextInfo']): void;
|
|
87
|
+
/**
|
|
88
|
+
* 导出会话(包含压缩状态)
|
|
89
|
+
*/
|
|
90
|
+
exportSessionWithContext(sessionId: string): string | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* 导出会话历史(包含所有压缩状态)
|
|
93
|
+
*/
|
|
94
|
+
exportAllHistoryWithContext(): string;
|
|
95
|
+
/**
|
|
96
|
+
* 导入会话(包含压缩状态)
|
|
97
|
+
*/
|
|
98
|
+
importSessionWithContext(data: string): void;
|
|
99
|
+
/**
|
|
100
|
+
* 清理压缩数据
|
|
101
|
+
*/
|
|
102
|
+
cleanupCompressedData(sessionId?: string): void;
|
|
103
|
+
/**
|
|
104
|
+
* 获取压缩统计信息
|
|
105
|
+
*/
|
|
106
|
+
getCompressionStatistics(): {
|
|
107
|
+
totalSessions: number;
|
|
108
|
+
sessionsWithCompression: number;
|
|
109
|
+
totalTokensSaved: number;
|
|
110
|
+
averageTokensSaved: number;
|
|
111
|
+
lastCompactionAt?: Date;
|
|
112
|
+
compactionHistory: Array<{
|
|
113
|
+
sessionId: string;
|
|
114
|
+
type: 'prune' | 'compact' | 'truncate';
|
|
115
|
+
timestamp: Date;
|
|
116
|
+
tokensSaved: number;
|
|
117
|
+
}>;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* 记录压缩事件
|
|
121
|
+
*/
|
|
122
|
+
recordCompactionEvent(sessionId: string, type: 'prune' | 'compact' | 'truncate', tokensSaved: number, condenseId?: string): void;
|
|
123
|
+
/**
|
|
124
|
+
* 检查会话是否有压缩标记
|
|
125
|
+
*/
|
|
126
|
+
hasCompressionMarkers(sessionId: string): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* 获取会话的压缩历史
|
|
129
|
+
*/
|
|
130
|
+
getCompactionHistory(sessionId: string): Array<{
|
|
131
|
+
timestamp: Date;
|
|
132
|
+
type: 'prune' | 'compact' | 'truncate';
|
|
133
|
+
tokensSaved: number;
|
|
134
|
+
condenseId?: string;
|
|
135
|
+
}>;
|
|
136
|
+
/**
|
|
137
|
+
* 更新Token使用统计
|
|
138
|
+
*/
|
|
139
|
+
updateTokenUsage(sessionId: string, tokenUsage?: {
|
|
140
|
+
input: number;
|
|
141
|
+
output: number;
|
|
142
|
+
cacheRead: number;
|
|
143
|
+
cacheWrite: number;
|
|
144
|
+
}): void;
|
|
67
145
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
-
import { ChatMessage } from '../../types/ai.types';
|
|
2
|
+
import { ChatMessage, Checkpoint } from '../../types/ai.types';
|
|
3
3
|
import { AiAssistantService } from '../core/ai-assistant.service';
|
|
4
4
|
import { LoggerService } from '../core/logger.service';
|
|
5
|
+
import { ChatHistoryService } from './chat-history.service';
|
|
5
6
|
/**
|
|
6
7
|
* 聊天会话服务
|
|
7
8
|
* 管理聊天会话的生命周期、消息历史和状态
|
|
@@ -9,14 +10,17 @@ import { LoggerService } from '../core/logger.service';
|
|
|
9
10
|
export declare class ChatSessionService {
|
|
10
11
|
private aiService;
|
|
11
12
|
private logger;
|
|
13
|
+
private chatHistoryService;
|
|
12
14
|
private currentSessionId;
|
|
13
15
|
private messagesSubject;
|
|
14
16
|
private isTypingSubject;
|
|
15
17
|
private errorSubject;
|
|
18
|
+
private checkpointsSubject;
|
|
16
19
|
messages$: Observable<ChatMessage[]>;
|
|
17
20
|
isTyping$: Observable<boolean>;
|
|
18
21
|
error$: Observable<string>;
|
|
19
|
-
|
|
22
|
+
checkpoints$: Observable<Checkpoint[]>;
|
|
23
|
+
constructor(aiService: AiAssistantService, logger: LoggerService, chatHistoryService: ChatHistoryService);
|
|
20
24
|
/**
|
|
21
25
|
* 创建新会话
|
|
22
26
|
*/
|
|
@@ -55,4 +59,55 @@ export declare class ChatSessionService {
|
|
|
55
59
|
importSession(sessionData: string): void;
|
|
56
60
|
private generateSessionId;
|
|
57
61
|
private generateMessageId;
|
|
62
|
+
/**
|
|
63
|
+
* 创建检查点
|
|
64
|
+
*/
|
|
65
|
+
createCheckpoint(label?: string): Checkpoint;
|
|
66
|
+
/**
|
|
67
|
+
* 恢复检查点
|
|
68
|
+
*/
|
|
69
|
+
restoreCheckpoint(checkpointId: string): void;
|
|
70
|
+
/**
|
|
71
|
+
* 删除检查点
|
|
72
|
+
*/
|
|
73
|
+
deleteCheckpoint(checkpointId: string): void;
|
|
74
|
+
/**
|
|
75
|
+
* 获取指定检查点
|
|
76
|
+
*/
|
|
77
|
+
getCheckpoint(checkpointId: string): Checkpoint | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* 列出所有检查点
|
|
80
|
+
*/
|
|
81
|
+
listCheckpoints(sessionId?: string): Checkpoint[];
|
|
82
|
+
/**
|
|
83
|
+
* 清空检查点
|
|
84
|
+
*/
|
|
85
|
+
clearCheckpoints(sessionId?: string): void;
|
|
86
|
+
/**
|
|
87
|
+
* 压缩存储检查点
|
|
88
|
+
*/
|
|
89
|
+
compressCheckpoint(checkpointId: string): Checkpoint;
|
|
90
|
+
/**
|
|
91
|
+
* 导出会话(包含检查点)
|
|
92
|
+
*/
|
|
93
|
+
exportSessionWithCheckpoints(): string;
|
|
94
|
+
/**
|
|
95
|
+
* 导入会话(包含检查点)
|
|
96
|
+
*/
|
|
97
|
+
importSessionWithCheckpoints(sessionData: string): void;
|
|
98
|
+
/**
|
|
99
|
+
* 获取检查点统计信息
|
|
100
|
+
*/
|
|
101
|
+
getCheckpointStatistics(): {
|
|
102
|
+
totalCheckpoints: number;
|
|
103
|
+
averageMessagesPerCheckpoint: number;
|
|
104
|
+
totalTokenUsage: number;
|
|
105
|
+
oldestCheckpoint?: Date;
|
|
106
|
+
newestCheckpoint?: Date;
|
|
107
|
+
};
|
|
108
|
+
private generateCheckpointId;
|
|
109
|
+
private generateCheckpointSummary;
|
|
110
|
+
private truncateText;
|
|
111
|
+
private calculateInputTokens;
|
|
112
|
+
private calculateOutputTokens;
|
|
58
113
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { LoggerService } from '../core/logger.service';
|
|
2
|
+
import { ApiMessage, ContextConfig, CompactionResult, PruneResult, TruncationResult } from '../../types/ai.types';
|
|
3
|
+
/**
|
|
4
|
+
* 压缩算法实现类
|
|
5
|
+
* 提供Prune、Compact、Truncate三种压缩算法的具体实现
|
|
6
|
+
*/
|
|
7
|
+
export declare class Compaction {
|
|
8
|
+
private logger;
|
|
9
|
+
private config;
|
|
10
|
+
constructor(logger: LoggerService);
|
|
11
|
+
/**
|
|
12
|
+
* Prune算法 - 移除工具输出中的冗余信息
|
|
13
|
+
* 保留关键信息,移除重复、冗长或无关的内容
|
|
14
|
+
*/
|
|
15
|
+
prune(messages: ApiMessage[]): Promise<PruneResult>;
|
|
16
|
+
/**
|
|
17
|
+
* Compact算法 - 使用AI生成摘要压缩上下文
|
|
18
|
+
* 将多个消息合并为一个摘要,保留核心信息
|
|
19
|
+
*/
|
|
20
|
+
compact(messages: ApiMessage[]): Promise<CompactionResult>;
|
|
21
|
+
/**
|
|
22
|
+
* Truncate算法 - 滑动窗口截断
|
|
23
|
+
* 保留最近的消息,移除较早的消息
|
|
24
|
+
*/
|
|
25
|
+
truncate(messages: ApiMessage[]): TruncationResult;
|
|
26
|
+
/**
|
|
27
|
+
* 智能压缩 - 根据消息类型和重要性选择最佳压缩策略
|
|
28
|
+
*/
|
|
29
|
+
smartCompact(messages: ApiMessage[], tokenBudget: number): Promise<{
|
|
30
|
+
strategy: 'prune' | 'compact' | 'truncate';
|
|
31
|
+
result: PruneResult | CompactionResult | TruncationResult;
|
|
32
|
+
messages: ApiMessage[];
|
|
33
|
+
}>;
|
|
34
|
+
/**
|
|
35
|
+
* 更新配置
|
|
36
|
+
*/
|
|
37
|
+
updateConfig(newConfig: Partial<ContextConfig>): void;
|
|
38
|
+
/**
|
|
39
|
+
* 获取当前配置
|
|
40
|
+
*/
|
|
41
|
+
getConfig(): ContextConfig;
|
|
42
|
+
/**
|
|
43
|
+
* 裁剪单条消息
|
|
44
|
+
*/
|
|
45
|
+
private pruneMessage;
|
|
46
|
+
/**
|
|
47
|
+
* 裁剪字符串内容
|
|
48
|
+
*/
|
|
49
|
+
private pruneStringContent;
|
|
50
|
+
/**
|
|
51
|
+
* 裁剪复杂内容(工具调用结果等)
|
|
52
|
+
*/
|
|
53
|
+
private pruneComplexContent;
|
|
54
|
+
/**
|
|
55
|
+
* 应用Prune结果到消息
|
|
56
|
+
*/
|
|
57
|
+
private applyPruneResult;
|
|
58
|
+
/**
|
|
59
|
+
* 格式化消息用于摘要
|
|
60
|
+
*/
|
|
61
|
+
private formatMessagesForSummary;
|
|
62
|
+
/**
|
|
63
|
+
* 提取文本内容
|
|
64
|
+
*/
|
|
65
|
+
private extractTextContent;
|
|
66
|
+
/**
|
|
67
|
+
* 生成摘要(集成AI API)
|
|
68
|
+
*/
|
|
69
|
+
private generateSummary;
|
|
70
|
+
/**
|
|
71
|
+
* 创建简单摘要(适用于短对话)
|
|
72
|
+
*/
|
|
73
|
+
private createSimpleSummary;
|
|
74
|
+
/**
|
|
75
|
+
* 创建降级摘要(当AI API不可用时)
|
|
76
|
+
*/
|
|
77
|
+
private createFallbackSummary;
|
|
78
|
+
/**
|
|
79
|
+
* 创建智能摘要(简化版)
|
|
80
|
+
*/
|
|
81
|
+
private createIntelligentSummary;
|
|
82
|
+
/**
|
|
83
|
+
* 计算总Token数
|
|
84
|
+
*/
|
|
85
|
+
private calculateTotalTokens;
|
|
86
|
+
/**
|
|
87
|
+
* 估算Token数量
|
|
88
|
+
*/
|
|
89
|
+
private estimateTokens;
|
|
90
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { LoggerService } from '../core/logger.service';
|
|
2
|
+
import { ChatHistoryService } from '../chat/chat-history.service';
|
|
3
|
+
import { ApiMessage, ContextConfig, TokenUsage, CompactionResult, PruneResult, TruncationResult } from '../../types/ai.types';
|
|
4
|
+
/**
|
|
5
|
+
* 上下文管理器 - 上下文工程的核心组件
|
|
6
|
+
* 负责Token预算管理、压缩触发决策、历史过滤等核心功能
|
|
7
|
+
*/
|
|
8
|
+
export declare class ContextManager {
|
|
9
|
+
private logger;
|
|
10
|
+
private chatHistoryService;
|
|
11
|
+
private config;
|
|
12
|
+
constructor(logger: LoggerService, chatHistoryService: ChatHistoryService);
|
|
13
|
+
/**
|
|
14
|
+
* 计算Token使用量
|
|
15
|
+
*/
|
|
16
|
+
calculateTokenUsage(messages: ApiMessage[]): TokenUsage;
|
|
17
|
+
/**
|
|
18
|
+
* 计算使用率
|
|
19
|
+
*/
|
|
20
|
+
calculateUsageRate(tokenUsage: TokenUsage): number;
|
|
21
|
+
/**
|
|
22
|
+
* 判断是否需要管理上下文
|
|
23
|
+
*/
|
|
24
|
+
shouldManageContext(sessionId: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* 统一管理上下文入口
|
|
27
|
+
*/
|
|
28
|
+
manageContext(sessionId: string): Promise<{
|
|
29
|
+
compactionResult?: CompactionResult;
|
|
30
|
+
pruneResult?: PruneResult;
|
|
31
|
+
truncationResult?: TruncationResult;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* 获取有效历史(过滤被压缩/截断的消息)
|
|
35
|
+
*/
|
|
36
|
+
getEffectiveHistory(sessionId: string): ApiMessage[];
|
|
37
|
+
/**
|
|
38
|
+
* 滑动窗口截断
|
|
39
|
+
*/
|
|
40
|
+
truncate(sessionId: string, messages: ApiMessage[]): Promise<TruncationResult>;
|
|
41
|
+
/**
|
|
42
|
+
* 清理孤儿标记
|
|
43
|
+
*/
|
|
44
|
+
cleanupOrphanedTags(sessionId: string): number;
|
|
45
|
+
/**
|
|
46
|
+
* 更新配置
|
|
47
|
+
*/
|
|
48
|
+
updateConfig(newConfig: Partial<ContextConfig>): void;
|
|
49
|
+
/**
|
|
50
|
+
* 获取当前配置
|
|
51
|
+
*/
|
|
52
|
+
getConfig(): ContextConfig;
|
|
53
|
+
/**
|
|
54
|
+
* 压缩(Compact)- 使用AI生成摘要
|
|
55
|
+
*/
|
|
56
|
+
private compact;
|
|
57
|
+
/**
|
|
58
|
+
* 裁剪(Prune)- 移除工具输出中的冗余信息
|
|
59
|
+
*/
|
|
60
|
+
private prune;
|
|
61
|
+
/**
|
|
62
|
+
* 格式化消息用于摘要
|
|
63
|
+
*/
|
|
64
|
+
private formatMessagesForSummary;
|
|
65
|
+
/**
|
|
66
|
+
* 估算Token数量(简化版)
|
|
67
|
+
*/
|
|
68
|
+
private estimateTokens;
|
|
69
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { LoggerService } from '../core/logger.service';
|
|
2
|
+
import { ApiMessage } from '../../types/ai.types';
|
|
3
|
+
/**
|
|
4
|
+
* 记忆层类型
|
|
5
|
+
*/
|
|
6
|
+
export declare enum MemoryLayer {
|
|
7
|
+
SHORT_TERM = "short_term",
|
|
8
|
+
MID_TERM = "mid_term",
|
|
9
|
+
LONG_TERM = "long_term"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 记忆项接口
|
|
13
|
+
*/
|
|
14
|
+
export interface MemoryItem {
|
|
15
|
+
id: string;
|
|
16
|
+
layer: MemoryLayer;
|
|
17
|
+
content: string;
|
|
18
|
+
metadata: {
|
|
19
|
+
timestamp: number;
|
|
20
|
+
sessionId?: string;
|
|
21
|
+
importance: number;
|
|
22
|
+
tags?: string[];
|
|
23
|
+
source?: string;
|
|
24
|
+
};
|
|
25
|
+
accessCount: number;
|
|
26
|
+
lastAccessed: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 记忆摘要
|
|
30
|
+
*/
|
|
31
|
+
export interface MemorySummary {
|
|
32
|
+
id: string;
|
|
33
|
+
layer: MemoryLayer;
|
|
34
|
+
summary: string;
|
|
35
|
+
keyPoints: string[];
|
|
36
|
+
createdAt: number;
|
|
37
|
+
relatedMemories: string[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 记忆统计信息
|
|
41
|
+
*/
|
|
42
|
+
export interface MemoryStatistics {
|
|
43
|
+
totalItems: number;
|
|
44
|
+
shortTermCount: number;
|
|
45
|
+
midTermCount: number;
|
|
46
|
+
longTermCount: number;
|
|
47
|
+
totalAccessCount: number;
|
|
48
|
+
averageImportance: number;
|
|
49
|
+
oldestMemory?: Date;
|
|
50
|
+
newestMemory?: Date;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 三层记忆系统
|
|
54
|
+
* 实现 Claude Code 架构的记忆管理
|
|
55
|
+
*/
|
|
56
|
+
export declare class Memory {
|
|
57
|
+
private logger;
|
|
58
|
+
private shortTermMemories;
|
|
59
|
+
private midTermMemories;
|
|
60
|
+
private longTermMemories;
|
|
61
|
+
private readonly MAX_SHORT_TERM_ITEMS;
|
|
62
|
+
private readonly MAX_MID_TERM_ITEMS;
|
|
63
|
+
private readonly MAX_LONG_TERM_ITEMS;
|
|
64
|
+
private readonly IMPORTANCE_THRESHOLD;
|
|
65
|
+
constructor(logger: LoggerService);
|
|
66
|
+
/**
|
|
67
|
+
* 存储记忆
|
|
68
|
+
*/
|
|
69
|
+
store(content: string, layer: MemoryLayer, metadata?: Partial<MemoryItem['metadata']>): string;
|
|
70
|
+
/**
|
|
71
|
+
* 检索记忆
|
|
72
|
+
*/
|
|
73
|
+
retrieve(memoryId: string): MemoryItem | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* 搜索记忆
|
|
76
|
+
*/
|
|
77
|
+
search(query: string, layer?: MemoryLayer): MemoryItem[];
|
|
78
|
+
/**
|
|
79
|
+
* 从消息创建记忆
|
|
80
|
+
*/
|
|
81
|
+
createFromMessages(messages: ApiMessage[], layer: MemoryLayer): string[];
|
|
82
|
+
/**
|
|
83
|
+
* 生成中期记忆摘要
|
|
84
|
+
*/
|
|
85
|
+
createMidTermSummary(sessionId: string): Promise<string>;
|
|
86
|
+
/**
|
|
87
|
+
* 保存到长期记忆(项目级)
|
|
88
|
+
*/
|
|
89
|
+
saveToLongTerm(content: string, metadata?: Partial<MemoryItem['metadata']>): string;
|
|
90
|
+
/**
|
|
91
|
+
* 从长期记忆加载
|
|
92
|
+
*/
|
|
93
|
+
loadFromLongTerm(query?: string): MemoryItem[];
|
|
94
|
+
/**
|
|
95
|
+
* 获取记忆统计
|
|
96
|
+
*/
|
|
97
|
+
getStatistics(): MemoryStatistics;
|
|
98
|
+
/**
|
|
99
|
+
* 清理过期记忆
|
|
100
|
+
*/
|
|
101
|
+
cleanup(): number;
|
|
102
|
+
/**
|
|
103
|
+
* 清空指定层级
|
|
104
|
+
*/
|
|
105
|
+
clearLayer(layer: MemoryLayer): void;
|
|
106
|
+
/**
|
|
107
|
+
* 获取相关记忆
|
|
108
|
+
*/
|
|
109
|
+
getRelatedMemories(memoryId: string): MemoryItem[];
|
|
110
|
+
private generateMemoryId;
|
|
111
|
+
private enforceLimit;
|
|
112
|
+
private generateIntelligentSummary;
|
|
113
|
+
private calculateSimilarity;
|
|
114
|
+
private saveToStorage;
|
|
115
|
+
private loadFromStorage;
|
|
116
|
+
}
|