polydev-ai 1.4.0 → 1.4.2

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.
@@ -1,189 +0,0 @@
1
- /**
2
- * Smart Cache for CLI Status Management
3
- * Provides intelligent caching based on CLI reliability and status
4
- */
5
-
6
- export class SmartCliCache {
7
- private supabase: any;
8
-
9
- constructor(supabase: any) {
10
- this.supabase = supabase;
11
- }
12
-
13
- /**
14
- * Get smart timeout based on CLI configuration
15
- * Returns timeout in minutes
16
- */
17
- getSmartTimeout(cliConfig: any): number {
18
- if (!cliConfig.available) {
19
- return 2; // 2 minutes - check frequently for new installs
20
- }
21
-
22
- if (!cliConfig.authenticated) {
23
- return 3; // 3 minutes - check for authentication
24
- }
25
-
26
- if (cliConfig.model_detection_method === 'fallback') {
27
- return 5; // 5 minutes - retry interactive detection
28
- }
29
-
30
- return 10; // 10 minutes - stable, working CLI
31
- }
32
-
33
- /**
34
- * Check if CLI configuration is stale
35
- */
36
- isStale(cliConfig: any): boolean {
37
- if (!cliConfig.last_checked_at) return true;
38
-
39
- const now = new Date();
40
- const lastChecked = new Date(cliConfig.last_checked_at);
41
- const minutesOld = (now.getTime() - lastChecked.getTime()) / (1000 * 60);
42
- const timeout = this.getSmartTimeout(cliConfig);
43
-
44
- return minutesOld > timeout;
45
- }
46
-
47
- /**
48
- * Get CLI status with smart caching
49
- * Checks if data is stale and triggers refresh if needed
50
- */
51
- async getCliStatusWithCache(userId: string): Promise<any[]> {
52
- // 1. Get current CLI configurations from database
53
- const { data: cliConfigs, error } = await this.supabase
54
- .from('cli_provider_configurations')
55
- .select('*')
56
- .eq('user_id', userId);
57
-
58
- if (error || !cliConfigs) {
59
- console.error('[Smart Cache] Failed to get CLI configs:', error);
60
- return [];
61
- }
62
-
63
- if (cliConfigs.length === 0) {
64
- return [];
65
- }
66
-
67
- // 2. Check which configurations are stale
68
- const staleConfigs = cliConfigs.filter((config: any) => this.isStale(config));
69
-
70
- // 3. If any are stale, trigger refresh (async, don't wait)
71
- if (staleConfigs.length > 0) {
72
- console.log(`[Smart Cache] ${staleConfigs.length} CLI configs are stale, triggering refresh for user ${userId}`);
73
- this.refreshStaleConfigs(userId, staleConfigs).catch((error: any) => {
74
- console.error('[Smart Cache] Refresh failed:', error);
75
- });
76
- }
77
-
78
- // 4. Return current data (will be fresh after refresh)
79
- return cliConfigs;
80
- }
81
-
82
- /**
83
- * Refresh stale CLI configurations (called asynchronously)
84
- */
85
- private async refreshStaleConfigs(userId: string, staleConfigs: any[]): Promise<void> {
86
- console.log(`[Smart Cache] Starting refresh for ${staleConfigs.length} stale CLI configs`);
87
-
88
- try {
89
- // Trigger forced CLI detection via stdio-wrapper
90
- for (const config of staleConfigs) {
91
- await this.updateCliStatus(userId, config.provider);
92
- }
93
-
94
- console.log('[Smart Cache] Successfully triggered refresh for stale CLI configs');
95
- } catch (error) {
96
- console.error('[Smart Cache] Failed to trigger refresh:', error);
97
- }
98
- }
99
-
100
- /**
101
- * Update CLI status for a specific provider
102
- */
103
- private async updateCliStatus(userId: string, provider: string): Promise<void> {
104
- try {
105
- const response = await fetch('/api/cli-status/refresh', {
106
- method: 'POST',
107
- headers: {
108
- 'Content-Type': 'application/json',
109
- 'Authorization': `Bearer ${this.getUserToken()}`
110
- },
111
- body: JSON.stringify({
112
- user_id: userId,
113
- provider: provider
114
- })
115
- });
116
-
117
- if (!response.ok) {
118
- console.error(`[Smart Cache] Failed to refresh ${provider}:`, response.status);
119
- }
120
- } catch (error) {
121
- console.error(`[Smart Cache] Network error refreshing ${provider}:`, error);
122
- }
123
- }
124
-
125
- /**
126
- * Get current user token (helper method)
127
- */
128
- private getUserToken(): string {
129
- // Try to get from environment or context
130
- return process.env.POLYDEV_USER_TOKEN || '';
131
- }
132
-
133
- /**
134
- * Format last checked time for display
135
- */
136
- formatLastCheckedTime(lastChecked: string): string {
137
- if (!lastChecked) return 'Unknown';
138
-
139
- const lastCheckedDate = new Date(lastChecked);
140
- const now = new Date();
141
- const minutes = Math.floor((now.getTime() - lastCheckedDate.getTime()) / (1000 * 60));
142
-
143
- if (minutes < 1) return 'Just now';
144
- if (minutes < 60) return `${minutes} min ago`;
145
- if (minutes < 1440) return `${Math.floor(minutes / 60)} hours ago`;
146
- return `${Math.floor(minutes / 1440)} days ago`;
147
- }
148
-
149
- /**
150
- * Check if CLI should use local tools based on database status
151
- */
152
- shouldUseLocalCli(model: string, cliConfigs: any[]): boolean {
153
- // Find CLI that supports this model
154
- const cliMatch = cliConfigs.find((cli: any) =>
155
- cli.status === 'available' &&
156
- cli.authenticated &&
157
- (cli.available_models?.includes(model) || cli.default_model === model)
158
- );
159
-
160
- return !!cliMatch;
161
- }
162
-
163
- /**
164
- * Get summary statistics for dashboard
165
- */
166
- getClimiStatusSummary(cliConfigs: any[]): {
167
- total: number;
168
- available: number;
169
- authenticated: number;
170
- stale: number;
171
- } {
172
- const now = new Date();
173
- let staleCount = 0;
174
-
175
- // Count stale configs
176
- cliConfigs.forEach(config => {
177
- if (this.isStale(config)) {
178
- staleCount++;
179
- }
180
- });
181
-
182
- return {
183
- total: cliConfigs.length,
184
- available: cliConfigs.filter(cli => cli.status === 'available').length,
185
- authenticated: cliConfigs.filter(cli => cli.authenticated).length,
186
- stale: staleCount
187
- };
188
- }
189
- }