tabby-ai-assistant 1.0.11 → 1.0.13

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.
@@ -2,6 +2,7 @@
2
2
  * AI提供商相关类型定义
3
3
  */
4
4
 
5
+ import { Observable } from 'rxjs';
5
6
  import { ProviderCapability, HealthStatus, ValidationResult } from './ai.types';
6
7
  import { ChatRequest, ChatResponse, CommandRequest, CommandResponse, ExplainRequest, ExplainResponse, AnalysisRequest, AnalysisResponse } from './ai.types';
7
8
 
@@ -9,14 +10,18 @@ import { ChatRequest, ChatResponse, CommandRequest, CommandResponse, ExplainRequ
9
10
  export { ProviderCapability, HealthStatus, ValidationResult };
10
11
  export { ChatRequest, ChatResponse, CommandRequest, CommandResponse, ExplainRequest, ExplainResponse, AnalysisRequest, AnalysisResponse };
11
12
 
12
- // 认证配置
13
+ // ==================== 认证配置 ====================
14
+
15
+ export type AuthType = 'apiKey' | 'bearer' | 'basic' | 'oauth' | 'none';
16
+
13
17
  export interface AuthConfig {
14
- type: 'apiKey' | 'bearer' | 'basic' | 'oauth' | 'none';
18
+ type: AuthType;
15
19
  credentials: Record<string, string>;
16
20
  requiresEncryption?: boolean;
17
21
  }
18
22
 
19
- // 提供商配置
23
+ // ==================== 提供商配置 ====================
24
+
20
25
  export interface ProviderConfig {
21
26
  name: string;
22
27
  displayName: string;
@@ -32,111 +37,234 @@ export interface ProviderConfig {
32
37
  contextWindow?: number; // 供应商上下文窗口限制
33
38
  }
34
39
 
35
- // 提供商信息
36
- export interface ProviderInfo {
37
- name: string;
38
- displayName: string;
39
- version: string;
40
- description: string;
41
- capabilities: ProviderCapability[];
40
+ // 提供商默认配置
41
+ export interface ProviderDefaults {
42
+ baseURL: string;
43
+ model: string;
44
+ maxTokens: number;
45
+ temperature: number;
46
+ timeout: number;
47
+ retries: number;
48
+ contextWindow: number;
42
49
  authConfig: AuthConfig;
43
- supportedModels: string[];
44
- pricing?: {
45
- type: 'free' | 'paid' | 'freemium';
46
- currency: string;
47
- unit: string;
48
- costPerUnit?: number;
49
- };
50
- documentation?: string;
51
50
  }
52
51
 
53
- // 基础AI提供商接口
54
- export abstract class BaseAiProvider {
55
- abstract readonly name: string;
56
- abstract readonly displayName: string;
57
- abstract readonly capabilities: ProviderCapability[];
58
- abstract readonly authConfig: AuthConfig;
59
-
60
- protected config: ProviderConfig | null = null;
61
-
62
- /**
63
- * 配置提供商
64
- */
65
- abstract configure(config: ProviderConfig): void;
66
-
67
- /**
68
- * 聊天功能
69
- */
70
- abstract chat(request: ChatRequest): Promise<ChatResponse>;
71
-
72
- /**
73
- * 生成命令
74
- */
75
- abstract generateCommand(request: CommandRequest): Promise<CommandResponse>;
52
+ // 所有已知提供商及其默认配置
53
+ export const PROVIDER_DEFAULTS: Record<string, ProviderDefaults> = {
54
+ openai: {
55
+ baseURL: 'https://api.openai.com/v1',
56
+ model: 'gpt-4',
57
+ maxTokens: 1000,
58
+ temperature: 0.7,
59
+ timeout: 30000,
60
+ retries: 3,
61
+ contextWindow: 128000,
62
+ authConfig: { type: 'bearer', credentials: {} }
63
+ },
64
+ anthropic: {
65
+ baseURL: 'https://api.anthropic.com',
66
+ model: 'claude-3-sonnet',
67
+ maxTokens: 1000,
68
+ temperature: 1.0,
69
+ timeout: 30000,
70
+ retries: 3,
71
+ contextWindow: 200000,
72
+ authConfig: { type: 'bearer', credentials: {} }
73
+ },
74
+ minimax: {
75
+ baseURL: 'https://api.minimaxi.com/anthropic',
76
+ model: 'MiniMax-M2',
77
+ maxTokens: 1000,
78
+ temperature: 1.0,
79
+ timeout: 30000,
80
+ retries: 3,
81
+ contextWindow: 128000,
82
+ authConfig: { type: 'bearer', credentials: {} }
83
+ },
84
+ glm: {
85
+ baseURL: 'https://open.bigmodel.cn/api/anthropic',
86
+ model: 'glm-4.6',
87
+ maxTokens: 1000,
88
+ temperature: 0.95,
89
+ timeout: 30000,
90
+ retries: 3,
91
+ contextWindow: 128000,
92
+ authConfig: { type: 'bearer', credentials: {} }
93
+ },
94
+ ollama: {
95
+ baseURL: 'http://localhost:11434/v1',
96
+ model: 'llama3.1',
97
+ maxTokens: 1000,
98
+ temperature: 0.7,
99
+ timeout: 30000,
100
+ retries: 3,
101
+ contextWindow: 8192,
102
+ authConfig: { type: 'none', credentials: {} }
103
+ },
104
+ vllm: {
105
+ baseURL: 'http://localhost:8000/v1',
106
+ model: 'meta-llama/Llama-3.1-8B',
107
+ maxTokens: 1000,
108
+ temperature: 0.7,
109
+ timeout: 30000,
110
+ retries: 3,
111
+ contextWindow: 8192,
112
+ authConfig: { type: 'bearer', credentials: {} }
113
+ },
114
+ 'openai-compatible': {
115
+ baseURL: 'http://localhost:11434/v1',
116
+ model: 'gpt-3.5-turbo',
117
+ maxTokens: 1000,
118
+ temperature: 0.7,
119
+ timeout: 30000,
120
+ retries: 3,
121
+ contextWindow: 128000,
122
+ authConfig: { type: 'bearer', credentials: {} }
123
+ }
124
+ };
76
125
 
126
+ // 配置工具函数
127
+ export namespace ProviderConfigUtils {
77
128
  /**
78
- * 解释命令
129
+ * 使用默认值填充配置
79
130
  */
80
- abstract explainCommand(request: ExplainRequest): Promise<ExplainResponse>;
131
+ export function fillDefaults(config: Partial<ProviderConfig>, providerName: string): ProviderConfig {
132
+ const defaults = PROVIDER_DEFAULTS[providerName];
133
+ if (!defaults) {
134
+ throw new Error(`Unknown provider: ${providerName}`);
135
+ }
81
136
 
82
- /**
83
- * 分析结果
84
- */
85
- abstract analyzeResult(request: AnalysisRequest): Promise<AnalysisResponse>;
137
+ return {
138
+ name: config.name || providerName,
139
+ displayName: config.displayName || providerName,
140
+ apiKey: config.apiKey,
141
+ baseURL: config.baseURL || defaults.baseURL,
142
+ model: config.model || defaults.model,
143
+ maxTokens: config.maxTokens ?? defaults.maxTokens,
144
+ temperature: config.temperature ?? defaults.temperature,
145
+ timeout: config.timeout ?? defaults.timeout,
146
+ retries: config.retries ?? defaults.retries,
147
+ authConfig: config.authConfig || defaults.authConfig,
148
+ enabled: config.enabled ?? true,
149
+ contextWindow: config.contextWindow ?? defaults.contextWindow
150
+ };
151
+ }
86
152
 
87
153
  /**
88
- * 健康检查
154
+ * 检查配置是否完整(可用于API调用)
89
155
  */
90
- abstract healthCheck(): Promise<HealthStatus>;
156
+ export function isConfigComplete(config: ProviderConfig): boolean {
157
+ return !!(
158
+ config.name &&
159
+ config.displayName &&
160
+ // API key 不是必需的(如本地服务)
161
+ (config.apiKey || config.authConfig?.type === 'none') &&
162
+ config.baseURL
163
+ );
164
+ }
91
165
 
92
166
  /**
93
- * 验证配置
167
+ * 克隆配置(深拷贝,移除敏感信息)
94
168
  */
95
- abstract validateConfig(): ValidationResult;
169
+ export function cloneConfig(config: ProviderConfig, maskApiKey = true): ProviderConfig {
170
+ const clone = { ...config };
171
+ if (maskApiKey && clone.apiKey) {
172
+ clone.apiKey = '***MASKED***';
173
+ }
174
+ return clone;
175
+ }
96
176
 
97
177
  /**
98
- * 获取提供商信息
178
+ * 获取提供商默认配置
99
179
  */
100
- getInfo(): ProviderInfo {
101
- return {
102
- name: this.name,
103
- displayName: this.displayName,
104
- version: '1.0.0',
105
- description: `${this.displayName} AI Provider`,
106
- capabilities: this.capabilities,
107
- authConfig: this.authConfig,
108
- supportedModels: this.config?.model ? [this.config.model] : []
109
- };
180
+ export function getDefaults(providerName: string): ProviderDefaults | undefined {
181
+ return PROVIDER_DEFAULTS[providerName];
110
182
  }
111
183
 
112
184
  /**
113
- * 检查是否支持指定能力
185
+ * 获取所有已知提供商名称
114
186
  */
115
- supportsCapability(capability: ProviderCapability): boolean {
116
- return this.capabilities.includes(capability);
187
+ export function getKnownProviders(): string[] {
188
+ return Object.keys(PROVIDER_DEFAULTS);
117
189
  }
118
190
 
119
191
  /**
120
- * 获取当前配置
192
+ * 检查是否为已知提供商
121
193
  */
122
- getConfig(): ProviderConfig | null {
123
- return this.config;
194
+ export function isKnownProvider(name: string): boolean {
195
+ return name in PROVIDER_DEFAULTS;
124
196
  }
125
197
  }
126
198
 
127
- // 提供商管理器
199
+ // ==================== 提供商信息 ====================
200
+
201
+ export interface ProviderPricing {
202
+ type: 'free' | 'paid' | 'freemium';
203
+ currency: string;
204
+ unit: string;
205
+ costPerUnit?: number;
206
+ }
207
+
208
+ export interface ProviderInfo {
209
+ name: string;
210
+ displayName: string;
211
+ version: string;
212
+ description: string;
213
+ capabilities: ProviderCapability[];
214
+ authConfig: AuthConfig;
215
+ supportedModels: string[];
216
+ configured?: boolean;
217
+ lastHealthCheck?: { status: HealthStatus; timestamp: Date };
218
+ pricing?: ProviderPricing;
219
+ documentation?: string;
220
+ defaults?: ProviderDefaults;
221
+ }
222
+
223
+ // ==================== 基础AI提供商接口 ====================
224
+
225
+ export interface IBaseAiProvider {
226
+ readonly name: string;
227
+ readonly displayName: string;
228
+ readonly capabilities: ProviderCapability[];
229
+ readonly authConfig: AuthConfig;
230
+
231
+ // 配置与状态
232
+ configure(config: ProviderConfig): void;
233
+ getConfig(): ProviderConfig | null;
234
+ isConfigured(): boolean;
235
+ isEnabled(): boolean;
236
+
237
+ // 核心功能
238
+ chat(request: ChatRequest): Promise<ChatResponse>;
239
+ chatStream(request: ChatRequest): Observable<any>;
240
+ generateCommand(request: CommandRequest): Promise<CommandResponse>;
241
+ explainCommand(request: ExplainRequest): Promise<ExplainResponse>;
242
+ analyzeResult(request: AnalysisRequest): Promise<AnalysisResponse>;
243
+
244
+ // 健康与验证
245
+ healthCheck(): Promise<HealthStatus>;
246
+ validateConfig(): ValidationResult;
247
+
248
+ // 信息查询
249
+ getInfo(): ProviderInfo;
250
+ supportsCapability(capability: ProviderCapability): boolean;
251
+ }
252
+
253
+ // ==================== 提供商管理器 ====================
254
+
128
255
  export interface ProviderManager {
129
- registerProvider(provider: BaseAiProvider): void;
256
+ registerProvider(provider: IBaseAiProvider): void;
130
257
  unregisterProvider(name: string): void;
131
- getProvider(name: string): BaseAiProvider | undefined;
132
- getAllProviders(): BaseAiProvider[];
133
- getActiveProvider(): BaseAiProvider | undefined;
258
+ getProvider(name: string): IBaseAiProvider | undefined;
259
+ getAllProviders(): IBaseAiProvider[];
260
+ getActiveProvider(): IBaseAiProvider | undefined;
134
261
  setActiveProvider(name: string): boolean;
135
262
  getProviderInfo(name: string): ProviderInfo | undefined;
136
263
  getAllProviderInfo(): ProviderInfo[];
137
264
  }
138
265
 
139
- // 提供商事件
266
+ // ==================== 提供商事件 ====================
267
+
140
268
  export interface ProviderEvent {
141
269
  type: 'connected' | 'disconnected' | 'error' | 'config_changed' | 'health_changed';
142
270
  provider: string;
@@ -144,5 +272,8 @@ export interface ProviderEvent {
144
272
  data?: any;
145
273
  }
146
274
 
147
- // 提供商事件监听器
148
275
  export type ProviderEventListener = (event: ProviderEvent) => void;
276
+
277
+ // ==================== 便利类型 ====================
278
+
279
+ export type BaseAiProvider = IBaseAiProvider;
@@ -1,165 +0,0 @@
1
- import { NgModule } from '@angular/core';
2
- import { CommonModule } from '@angular/common';
3
- import { FormsModule } from '@angular/forms';
4
- import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
5
-
6
- // Tabby modules
7
- import TabbyCoreModule, { ToolbarButtonProvider, ConfigProvider, HotkeyProvider } from 'tabby-core';
8
- import TabbyTerminalModule from 'tabby-terminal';
9
- import { SettingsTabProvider } from 'tabby-settings';
10
-
11
- // Core Services
12
- import { AiAssistantService } from './services/core/ai-assistant.service';
13
- import { AiProviderManagerService } from './services/core/ai-provider-manager.service';
14
- import { ConfigProviderService } from './services/core/config-provider.service';
15
- import { LoggerService } from './services/core/logger.service';
16
-
17
- // Providers
18
- import { BaseAiProvider } from './services/providers/base-provider.service';
19
- import { OpenAiProviderService } from './services/providers/openai-provider.service';
20
- import { AnthropicProviderService } from './services/providers/anthropic-provider.service';
21
- import { MinimaxProviderService } from './services/providers/minimax-provider.service';
22
- import { GlmProviderService } from './services/providers/glm-provider.service';
23
- import { OpenAiCompatibleProviderService } from './services/providers/openai-compatible.service';
24
-
25
- // Security Services
26
- import { SecurityValidatorService } from './services/security/security-validator.service';
27
- import { RiskAssessmentService } from './services/security/risk-assessment.service';
28
- import { PasswordManagerService } from './services/security/password-manager.service';
29
- import { ConsentManagerService } from './services/security/consent-manager.service';
30
-
31
- // Chat Services
32
- import { ChatSessionService } from './services/chat/chat-session.service';
33
- import { ChatHistoryService } from './services/chat/chat-history.service';
34
- import { CommandGeneratorService } from './services/chat/command-generator.service';
35
-
36
- // Terminal Services
37
- import { CommandAnalyzerService } from './services/terminal/command-analyzer.service';
38
- import { ContextMenuService } from './services/terminal/context-menu.service';
39
- import { HotkeyService } from './services/terminal/hotkey.service';
40
-
41
- // Tabby Providers (enabled for proper integration)
42
-
43
- // Components
44
- import { ChatInterfaceComponent } from './components/chat/chat-interface.component';
45
- import { ChatMessageComponent } from './components/chat/chat-message.component';
46
- import { ChatInputComponent } from './components/chat/chat-input.component';
47
- import { ChatSettingsComponent } from './components/chat/chat-settings.component';
48
-
49
- import { AiSettingsTabComponent } from './components/settings/ai-settings-tab.component';
50
- import { ProviderConfigComponent } from './components/settings/provider-config.component';
51
- import { SecuritySettingsComponent } from './components/settings/security-settings.component';
52
- import { GeneralSettingsComponent } from './components/settings/general-settings.component';
53
-
54
- import { RiskConfirmDialogComponent } from './components/security/risk-confirm-dialog.component';
55
- import { PasswordPromptComponent } from './components/security/password-prompt.component';
56
- import { ConsentDialogComponent } from './components/security/consent-dialog.component';
57
-
58
- import { CommandSuggestionComponent } from './components/terminal/command-suggestion.component';
59
- import { CommandPreviewComponent } from './components/terminal/command-preview.component';
60
- import { AiToolbarButtonComponent } from './components/terminal/ai-toolbar-button.component';
61
-
62
- import { LoadingSpinnerComponent } from './components/common/loading-spinner.component';
63
- import { ErrorMessageComponent } from './components/common/error-message.component';
64
-
65
- // Tabby Integration Providers (enabled for proper integration)
66
- import { AiToolbarButtonProvider } from './providers/tabby/ai-toolbar-button.provider';
67
- import { AiSettingsTabProvider } from './providers/tabby/ai-settings-tab.provider';
68
- import { AiConfigProvider } from './providers/tabby/ai-config.provider';
69
- import { AiHotkeyProvider } from './providers/tabby/ai-hotkey.provider';
70
-
71
- @NgModule({
72
- imports: [
73
- CommonModule,
74
- FormsModule,
75
- TabbyCoreModule, // Enabled for Tabby integration
76
- TabbyTerminalModule, // Required for terminal integration
77
- NgbModule
78
- ],
79
- providers: [
80
- // Core Services
81
- AiAssistantService,
82
- AiProviderManagerService,
83
- ConfigProviderService,
84
- LoggerService,
85
-
86
- // AI Providers
87
- OpenAiProviderService,
88
- AnthropicProviderService,
89
- MinimaxProviderService,
90
- GlmProviderService,
91
- OpenAiCompatibleProviderService,
92
-
93
- // Security Services
94
- SecurityValidatorService,
95
- RiskAssessmentService,
96
- PasswordManagerService,
97
- ConsentManagerService,
98
-
99
- // Chat Services
100
- ChatSessionService,
101
- ChatHistoryService,
102
- CommandGeneratorService,
103
-
104
- // Terminal Services
105
- CommandAnalyzerService,
106
- ContextMenuService,
107
- HotkeyService,
108
-
109
- // Tabby Integration Providers (enabled for proper integration)
110
- { provide: ToolbarButtonProvider, useClass: AiToolbarButtonProvider, multi: true },
111
- { provide: SettingsTabProvider, useClass: AiSettingsTabProvider, multi: true },
112
- { provide: ConfigProvider, useClass: AiConfigProvider, multi: true },
113
- { provide: HotkeyProvider, useClass: AiHotkeyProvider, multi: true },
114
- // { provide: TabContextMenuItemProvider, useClass: AiContextMenuProvider, multi: true }
115
- ],
116
- declarations: [
117
- // Chat Components
118
- ChatInterfaceComponent,
119
- ChatMessageComponent,
120
- ChatInputComponent,
121
- ChatSettingsComponent,
122
-
123
- // Settings Components
124
- AiSettingsTabComponent,
125
- ProviderConfigComponent,
126
- SecuritySettingsComponent,
127
- GeneralSettingsComponent,
128
-
129
- // Security Components
130
- RiskConfirmDialogComponent,
131
- PasswordPromptComponent,
132
- ConsentDialogComponent,
133
-
134
- // Terminal Components
135
- CommandSuggestionComponent,
136
- CommandPreviewComponent,
137
- AiToolbarButtonComponent,
138
-
139
- // Common Components
140
- LoadingSpinnerComponent,
141
- ErrorMessageComponent
142
- ],
143
- entryComponents: [
144
- ChatInterfaceComponent,
145
- RiskConfirmDialogComponent,
146
- PasswordPromptComponent,
147
- ConsentDialogComponent,
148
- CommandSuggestionComponent,
149
- CommandPreviewComponent
150
- ]
151
- })
152
- export default class AiAssistantModule {
153
- constructor(
154
- private app: any,
155
- private aiService: AiAssistantService,
156
- private config: ConfigProviderService
157
- ) {
158
- // Wait for Tabby to be ready
159
- if (this.app && this.app.ready$) {
160
- this.app.ready$.subscribe(() => {
161
- this.aiService.initialize();
162
- });
163
- }
164
- }
165
- }