adaptive-memory-multi-model-router 1.3.0 → 1.4.0
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/README.md +53 -44
- package/dist/integrations/airtable.js +20 -0
- package/dist/integrations/discord.js +18 -0
- package/dist/integrations/github.js +23 -0
- package/dist/integrations/gmail.js +19 -0
- package/dist/integrations/google-calendar.js +18 -0
- package/dist/integrations/index.js +61 -0
- package/dist/integrations/jira.js +21 -0
- package/dist/integrations/linear.js +19 -0
- package/dist/integrations/notion.js +19 -0
- package/dist/integrations/oauth.js +26 -0
- package/dist/integrations/slack.js +18 -0
- package/dist/integrations/telegram.js +19 -0
- package/dist/memory/autoFetch.js +59 -0
- package/dist/memory/autoFetch.ts +109 -0
- package/dist/memory/memoryTree.js +43 -0
- package/dist/memory/obsidianVault.js +26 -0
- package/dist/utils/enhancedCompression.js +180 -0
- package/package.json +1 -1
- package/package.json.tmp +0 -0
- package/src/integrations/oauth.ts +280 -0
- package/src/memory/autoFetch.ts +109 -0
- package/src/memory/memoryTree.ts +242 -0
- package/src/memory/obsidianVault.ts +224 -0
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced Compression - TokenJuice-style
|
|
3
|
+
*
|
|
4
|
+
* Achieves 80% token reduction through multiple techniques:
|
|
5
|
+
* - HTML to Markdown conversion
|
|
6
|
+
* - URL shortening
|
|
7
|
+
* - Non-ASCII removal
|
|
8
|
+
* - Repeated phrase deduplication
|
|
9
|
+
* - Code block optimization
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
class EnhancedCompression {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.maxUrlLength = 50;
|
|
15
|
+
this.maxChunkSize = 3000;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Compress text to ~80% original size
|
|
20
|
+
*/
|
|
21
|
+
compress(text) {
|
|
22
|
+
if (!text || text.length === 0) return '';
|
|
23
|
+
|
|
24
|
+
let result = text;
|
|
25
|
+
|
|
26
|
+
// 1. HTML → Markdown
|
|
27
|
+
result = this.htmlToMarkdown(result);
|
|
28
|
+
|
|
29
|
+
// 2. Shorten URLs
|
|
30
|
+
result = this.shortenUrls(result);
|
|
31
|
+
|
|
32
|
+
// 3. Remove non-ASCII
|
|
33
|
+
result = this.removeNonASCII(result);
|
|
34
|
+
|
|
35
|
+
// 4. Deduplicate phrases
|
|
36
|
+
result = this.deduplicatePhrases(result);
|
|
37
|
+
|
|
38
|
+
// 5. Compress whitespace
|
|
39
|
+
result = this.compressWhitespace(result);
|
|
40
|
+
|
|
41
|
+
// 6. Optimize code blocks
|
|
42
|
+
result = this.optimizeCodeBlocks(result);
|
|
43
|
+
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* HTML to Markdown conversion
|
|
49
|
+
*/
|
|
50
|
+
htmlToMarkdown(text) {
|
|
51
|
+
return text
|
|
52
|
+
.replace(/<h1[^>]*>(.*?)<\/h1>/gi, '# $1\n')
|
|
53
|
+
.replace(/<h2[^>]*>(.*?)<\/h2>/gi, '## $1\n')
|
|
54
|
+
.replace(/<h3[^>]*>(.*?)<\/h3>/gi, '### $1\n')
|
|
55
|
+
.replace(/<p[^>]*>(.*?)<\/p>/gi, '$1\n')
|
|
56
|
+
.replace(/<a[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '[$2]($1)')
|
|
57
|
+
.replace(/<strong[^>]*>(.*?)<\/strong>/gi, '**$1**')
|
|
58
|
+
.replace(/<b[^>]*>(.*?)<\/b>/gi, '**$1**')
|
|
59
|
+
.replace(/<em[^>]*>(.*?)<\/em>/gi, '*$1*')
|
|
60
|
+
.replace(/<i[^>]*>(.*?)<\/i>/gi, '*$1*')
|
|
61
|
+
.replace(/<code[^>]*>(.*?)<\/code>/gi, '`$1`')
|
|
62
|
+
.replace(/<pre[^>]*>(.*?)<\/pre>/gi, '```\n$1\n```')
|
|
63
|
+
.replace(/<li[^>]*>(.*?)<\/li>/gi, '- $1\n')
|
|
64
|
+
.replace(/<br\s*\/?>/gi, '\n')
|
|
65
|
+
.replace(/<\/div>/gi, '\n')
|
|
66
|
+
.replace(/<[^>]+>/g, '');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Shorten long URLs
|
|
71
|
+
*/
|
|
72
|
+
shortenUrls(text) {
|
|
73
|
+
return text.replace(/(https?:\/\/[^\s]{50,})/g, (match) => {
|
|
74
|
+
try {
|
|
75
|
+
const url = new URL(match);
|
|
76
|
+
return `${url.protocol}//${url.host}/...${url.pathname.slice(-10)}`;
|
|
77
|
+
} catch {
|
|
78
|
+
return match.slice(0, this.maxUrlLength) + '...';
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Remove non-ASCII characters
|
|
85
|
+
*/
|
|
86
|
+
removeNonASCII(text) {
|
|
87
|
+
return text.replace(/[^\x00-\x7F]+/g, (match) => {
|
|
88
|
+
// Keep common symbols like ©, ®, ™
|
|
89
|
+
return match.replace(/[^\x00-\x7F]/g, '');
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Deduplicate repeated phrases
|
|
95
|
+
*/
|
|
96
|
+
deduplicatePhrases(text) {
|
|
97
|
+
const words = text.split(/\s+/);
|
|
98
|
+
const seen = new Set();
|
|
99
|
+
const result = [];
|
|
100
|
+
|
|
101
|
+
for (const word of words) {
|
|
102
|
+
const lower = word.toLowerCase();
|
|
103
|
+
if (!seen.has(lower)) {
|
|
104
|
+
seen.add(lower);
|
|
105
|
+
result.push(word);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return result.join(' ');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Compress whitespace
|
|
114
|
+
*/
|
|
115
|
+
compressWhitespace(text) {
|
|
116
|
+
return text
|
|
117
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
118
|
+
.replace(/[ \t]{2,}/g, ' ')
|
|
119
|
+
.replace(/\n /g, '\n')
|
|
120
|
+
.trim();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Optimize code blocks
|
|
125
|
+
*/
|
|
126
|
+
optimizeCodeBlocks(text) {
|
|
127
|
+
return text
|
|
128
|
+
.replace(/```(\w+)\n([\s\S]*?)```/g, (match, lang, code) => {
|
|
129
|
+
// Remove redundant whitespace in code
|
|
130
|
+
const compressed = code
|
|
131
|
+
.split('\n')
|
|
132
|
+
.map(line => line.trimEnd())
|
|
133
|
+
.join('\n')
|
|
134
|
+
.trim();
|
|
135
|
+
return `\`\`\`${lang}\n${compressed}\n\`\`\``;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Split into chunks (max 3k tokens each)
|
|
141
|
+
*/
|
|
142
|
+
chunk(text) {
|
|
143
|
+
const chunks = [];
|
|
144
|
+
const words = text.split(/\s+/);
|
|
145
|
+
let current = [];
|
|
146
|
+
let currentSize = 0;
|
|
147
|
+
|
|
148
|
+
for (const word of words) {
|
|
149
|
+
currentSize += word.length + 1;
|
|
150
|
+
if (currentSize > this.maxChunkSize) {
|
|
151
|
+
chunks.push(current.join(' '));
|
|
152
|
+
current = [word];
|
|
153
|
+
currentSize = word.length + 1;
|
|
154
|
+
} else {
|
|
155
|
+
current.push(word);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (current.length > 0) {
|
|
160
|
+
chunks.push(current.join(' '));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return chunks;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get compression stats
|
|
168
|
+
*/
|
|
169
|
+
getStats(original, compressed) {
|
|
170
|
+
const reduction = ((original.length - compressed.length) / original.length * 100).toFixed(1);
|
|
171
|
+
return {
|
|
172
|
+
original: original.length,
|
|
173
|
+
compressed: compressed.length,
|
|
174
|
+
reduction: `${reduction}%`,
|
|
175
|
+
ratio: (compressed.length / original.length).toFixed(2)
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
module.exports = { EnhancedCompression };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "adaptive-memory-multi-model-router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"version_description": "v1.2.0 - Research-backed Multi-LLM Router based on arXiv: RouteLLM (2404.06035), RadixAttention (2312.07104), Medusa (2401.10774), FlashAttention (2407.07403). 120+ keywords for LLM/ML discoverability. 13 PI tools.",
|
|
5
5
|
"description": "A3M Router - Adaptive Memory Multi-Model Router with learned routing, prefix caching, and speculative decoding for LLM/ML developers.",
|
|
6
6
|
"main": "dist/index.js",
|
package/package.json.tmp
ADDED
|
File without changes
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth Integration Manager
|
|
3
|
+
*
|
|
4
|
+
* Provides one-click OAuth for GitHub, Slack, Gmail, Notion
|
|
5
|
+
* with typed tool wrappers for each service.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface OAuthConfig {
|
|
9
|
+
clientId: string;
|
|
10
|
+
clientSecret: string;
|
|
11
|
+
redirectUri: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface OAuthTokens {
|
|
15
|
+
accessToken: string;
|
|
16
|
+
refreshToken?: string;
|
|
17
|
+
expiresAt: number;
|
|
18
|
+
tokenType: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface OAuthProvider {
|
|
22
|
+
name: string;
|
|
23
|
+
authUrl: string;
|
|
24
|
+
tokenUrl: string;
|
|
25
|
+
scopes: string[];
|
|
26
|
+
baseUrl: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Supported OAuth providers
|
|
30
|
+
export const OAUTH_PROVIDERS: Record<string, OAuthProvider> = {
|
|
31
|
+
github: {
|
|
32
|
+
name: 'GitHub',
|
|
33
|
+
authUrl: 'https://github.com/login/oauth/authorize',
|
|
34
|
+
tokenUrl: 'https://github.com/login/oauth/access_token',
|
|
35
|
+
scopes: ['repo', 'read:user', 'notifications'],
|
|
36
|
+
baseUrl: 'https://api.github.com'
|
|
37
|
+
},
|
|
38
|
+
slack: {
|
|
39
|
+
name: 'Slack',
|
|
40
|
+
authUrl: 'https://slack.com/oauth/v2/authorize',
|
|
41
|
+
tokenUrl: 'https://slack.com/api/oauth.v2.access',
|
|
42
|
+
scopes: ['chat:write', 'channels:read', 'users:read'],
|
|
43
|
+
baseUrl: 'https://slack.com/api'
|
|
44
|
+
},
|
|
45
|
+
gmail: {
|
|
46
|
+
name: 'Gmail',
|
|
47
|
+
authUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
48
|
+
tokenUrl: 'https://oauth2.googleapis.com/token',
|
|
49
|
+
scopes: ['https://www.googleapis.com/auth/gmail.send', 'https://www.googleapis.com/auth/gmail.readonly'],
|
|
50
|
+
baseUrl: 'https://gmail.googleapis.com/gmail/v1'
|
|
51
|
+
},
|
|
52
|
+
notion: {
|
|
53
|
+
name: 'Notion',
|
|
54
|
+
authUrl: 'https://api.notion.com/v1/oauth/authorize',
|
|
55
|
+
tokenUrl: 'https://api.notion.com/v1/oauth/token',
|
|
56
|
+
scopes: ['read_content', 'update_content', 'insert_database'],
|
|
57
|
+
baseUrl: 'https://api.notion.com/v1'
|
|
58
|
+
},
|
|
59
|
+
googlecalendar: {
|
|
60
|
+
name: 'Google Calendar',
|
|
61
|
+
authUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
|
|
62
|
+
tokenUrl: 'https://oauth2.googleapis.com/token',
|
|
63
|
+
scopes: ['https://www.googleapis.com/auth/calendar.events'],
|
|
64
|
+
baseUrl: 'https://www.googleapis.com/calendar/v3'
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export class OAuthManager {
|
|
69
|
+
private configs: Map<string, OAuthConfig>;
|
|
70
|
+
private tokens: Map<string, OAuthTokens>;
|
|
71
|
+
private state: Map<string, string>; // CSRF state
|
|
72
|
+
|
|
73
|
+
constructor() {
|
|
74
|
+
this.configs = new Map();
|
|
75
|
+
this.tokens = new Map();
|
|
76
|
+
this.state = new Map();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Configure an OAuth provider
|
|
81
|
+
*/
|
|
82
|
+
configure(provider: string, config: OAuthConfig) {
|
|
83
|
+
this.configs.set(provider, config);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Generate authorization URL for a provider
|
|
88
|
+
*/
|
|
89
|
+
getAuthUrl(provider: string, state?: string): string {
|
|
90
|
+
const config = this.configs.get(provider);
|
|
91
|
+
const providerInfo = OAUTH_PROVIDERS[provider];
|
|
92
|
+
|
|
93
|
+
if (!config || !providerInfo) {
|
|
94
|
+
throw new Error(`Unknown provider: ${provider}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Generate CSRF state
|
|
98
|
+
const csrfState = state || this.generateState(provider);
|
|
99
|
+
this.state.set(provider, csrfState);
|
|
100
|
+
|
|
101
|
+
const params = new URLSearchParams({
|
|
102
|
+
client_id: config.clientId,
|
|
103
|
+
redirect_uri: config.redirectUri,
|
|
104
|
+
scope: providerInfo.scopes.join(' '),
|
|
105
|
+
state: csrfState,
|
|
106
|
+
response_type: 'code'
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return `${providerInfo.authUrl}?${params.toString()}`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Handle OAuth callback and exchange code for tokens
|
|
114
|
+
*/
|
|
115
|
+
async handleCallback(provider: string, code: string, state: string): Promise<OAuthTokens> {
|
|
116
|
+
const config = this.configs.get(provider);
|
|
117
|
+
const providerInfo = OAUTH_PROVIDERS[provider];
|
|
118
|
+
|
|
119
|
+
if (!config || !providerInfo) {
|
|
120
|
+
throw new Error(`Unknown provider: ${provider}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Validate state
|
|
124
|
+
const savedState = this.state.get(provider);
|
|
125
|
+
if (savedState && savedState !== state) {
|
|
126
|
+
throw new Error('Invalid OAuth state - CSRF mismatch');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Exchange code for tokens
|
|
130
|
+
const response = await fetch(providerInfo.tokenUrl, {
|
|
131
|
+
method: 'POST',
|
|
132
|
+
headers: {
|
|
133
|
+
'Content-Type': 'application/json',
|
|
134
|
+
'Accept': 'application/json'
|
|
135
|
+
},
|
|
136
|
+
body: JSON.stringify({
|
|
137
|
+
client_id: config.clientId,
|
|
138
|
+
client_secret: config.clientSecret,
|
|
139
|
+
code,
|
|
140
|
+
redirect_uri: config.redirectUri,
|
|
141
|
+
grant_type: 'authorization_code'
|
|
142
|
+
})
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (!response.ok) {
|
|
146
|
+
throw new Error(`Token exchange failed: ${response.statusText}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const tokens = await response.json() as OAuthTokens & { expires_in: number };
|
|
150
|
+
|
|
151
|
+
// Store tokens with expiration
|
|
152
|
+
const tokensWithExpiry: OAuthTokens = {
|
|
153
|
+
accessToken: tokens.access_token,
|
|
154
|
+
refreshToken: tokens.refresh_token,
|
|
155
|
+
expiresAt: tokens.expires_in ? Date.now() + tokens.expires_in * 1000 : 0,
|
|
156
|
+
tokenType: tokens.token_type || 'Bearer'
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
this.tokens.set(provider, tokensWithExpiry);
|
|
160
|
+
return tokensWithExpiry;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get valid access token (refresh if needed)
|
|
165
|
+
*/
|
|
166
|
+
async getAccessToken(provider: string): Promise<string> {
|
|
167
|
+
const tokens = this.tokens.get(provider);
|
|
168
|
+
|
|
169
|
+
if (!tokens) {
|
|
170
|
+
throw new Error(`No tokens for provider: ${provider}`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Check if expired
|
|
174
|
+
if (tokens.expiresAt && Date.now() >= tokens.expiresAt - 60000) {
|
|
175
|
+
if (tokens.refreshToken) {
|
|
176
|
+
await this.refreshToken(provider, tokens.refreshToken);
|
|
177
|
+
} else {
|
|
178
|
+
throw new Error(`Token expired for ${provider} and no refresh token available`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return this.tokens.get(provider)!.accessToken;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Refresh access token
|
|
187
|
+
*/
|
|
188
|
+
async refreshToken(provider: string, refreshToken: string) {
|
|
189
|
+
const config = this.configs.get(provider);
|
|
190
|
+
const providerInfo = OAUTH_PROVIDERS[provider];
|
|
191
|
+
|
|
192
|
+
if (!config || !providerInfo) {
|
|
193
|
+
throw new Error(`Unknown provider: ${provider}`);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const response = await fetch(providerInfo.tokenUrl, {
|
|
197
|
+
method: 'POST',
|
|
198
|
+
headers: {
|
|
199
|
+
'Content-Type': 'application/json'
|
|
200
|
+
},
|
|
201
|
+
body: JSON.stringify({
|
|
202
|
+
client_id: config.clientId,
|
|
203
|
+
client_secret: config.clientSecret,
|
|
204
|
+
refresh_token: refreshToken,
|
|
205
|
+
grant_type: 'refresh_token'
|
|
206
|
+
})
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
if (!response.ok) {
|
|
210
|
+
throw new Error(`Token refresh failed: ${response.statusText}`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const tokens = await response.json() as OAuthTokens & { expires_in: number };
|
|
214
|
+
|
|
215
|
+
this.tokens.set(provider, {
|
|
216
|
+
accessToken: tokens.accessToken,
|
|
217
|
+
refreshToken: tokens.refreshToken || refreshToken,
|
|
218
|
+
expiresAt: tokens.expires_in ? Date.now() + tokens.expires_in * 1000 : 0,
|
|
219
|
+
tokenType: tokens.tokenType || 'Bearer'
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Make authenticated API request
|
|
225
|
+
*/
|
|
226
|
+
async apiRequest(provider: string, endpoint: string, options: RequestInit = {}): Promise<any> {
|
|
227
|
+
const token = await this.getAccessToken(provider);
|
|
228
|
+
const providerInfo = OAUTH_PROVIDERS[provider];
|
|
229
|
+
|
|
230
|
+
if (!providerInfo) {
|
|
231
|
+
throw new Error(`Unknown provider: ${provider}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const response = await fetch(`${providerInfo.baseUrl}${endpoint}`, {
|
|
235
|
+
...options,
|
|
236
|
+
headers: {
|
|
237
|
+
'Authorization': `Bearer ${token}`,
|
|
238
|
+
'Content-Type': 'application/json',
|
|
239
|
+
...options.headers
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
if (!response.ok) {
|
|
244
|
+
throw new Error(`API request failed: ${response.statusText}`);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return response.json();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Check if provider is connected
|
|
252
|
+
*/
|
|
253
|
+
isConnected(provider: string): boolean {
|
|
254
|
+
const tokens = this.tokens.get(provider);
|
|
255
|
+
if (!tokens) return false;
|
|
256
|
+
if (tokens.expiresAt && Date.now() >= tokens.expiresAt) return false;
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Get connected providers
|
|
262
|
+
*/
|
|
263
|
+
getConnectedProviders(): string[] {
|
|
264
|
+
return Array.from(this.tokens.keys()).filter(p => this.isConnected(p));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Disconnect provider
|
|
269
|
+
*/
|
|
270
|
+
disconnect(provider: string) {
|
|
271
|
+
this.tokens.delete(provider);
|
|
272
|
+
this.state.delete(provider);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private generateState(provider: string): string {
|
|
276
|
+
return `${provider}_${Date.now()}_${Math.random().toString(36).substring(2)}`;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export default OAuthManager;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-Fetch Sync Loop
|
|
3
|
+
*
|
|
4
|
+
* Periodically syncs data from connected tools to provide
|
|
5
|
+
* context-aware routing decisions.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface SyncConfig {
|
|
9
|
+
intervalMs: number;
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
targets: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SyncResult {
|
|
15
|
+
target: string;
|
|
16
|
+
success: boolean;
|
|
17
|
+
items: number;
|
|
18
|
+
timestamp: number;
|
|
19
|
+
error?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class AutoFetch {
|
|
23
|
+
private intervalMs: number;
|
|
24
|
+
private enabled: boolean;
|
|
25
|
+
private targets: Set<string>;
|
|
26
|
+
private lastSync: Map<string, SyncResult>;
|
|
27
|
+
private timer: NodeJS.Timeout | null = null;
|
|
28
|
+
private syncHandlers: Map<string, () => Promise<SyncResult>>;
|
|
29
|
+
|
|
30
|
+
constructor(config: Partial<SyncConfig> = {}) {
|
|
31
|
+
this.intervalMs = config.intervalMs || 20 * 60 * 1000;
|
|
32
|
+
this.enabled = config.enabled !== false;
|
|
33
|
+
this.targets = new Set(config.targets || ['github', 'notion', 'slack']);
|
|
34
|
+
this.lastSync = new Map();
|
|
35
|
+
this.syncHandlers = new Map();
|
|
36
|
+
this.setupDefaultHandlers();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private setupDefaultHandlers() {
|
|
40
|
+
this.syncHandlers.set('github', async () => this.syncGitHub());
|
|
41
|
+
this.syncHandlers.set('notion', async () => this.syncNotion());
|
|
42
|
+
this.syncHandlers.set('slack', async () => this.syncSlack());
|
|
43
|
+
this.syncHandlers.set('gmail', async () => this.syncGmail());
|
|
44
|
+
this.syncHandlers.set('calendar', async () => this.syncCalendar());
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
start() {
|
|
48
|
+
if (!this.enabled) return;
|
|
49
|
+
this.syncAll();
|
|
50
|
+
this.timer = setInterval(() => this.syncAll(), this.intervalMs);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
stop() {
|
|
54
|
+
if (this.timer) {
|
|
55
|
+
clearInterval(this.timer);
|
|
56
|
+
this.timer = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async syncAll(): Promise<Map<string, SyncResult>> {
|
|
61
|
+
const results = new Map<string, SyncResult>();
|
|
62
|
+
for (const target of this.targets) {
|
|
63
|
+
const handler = this.syncHandlers.get(target);
|
|
64
|
+
if (handler) {
|
|
65
|
+
try {
|
|
66
|
+
const result = await handler();
|
|
67
|
+
this.lastSync.set(target, result);
|
|
68
|
+
results.set(target, result);
|
|
69
|
+
} catch (error: any) {
|
|
70
|
+
const result: SyncResult = { target, success: false, items: 0, timestamp: Date.now(), error: error.message };
|
|
71
|
+
this.lastSync.set(target, result);
|
|
72
|
+
results.set(target, result);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return results;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getLastSync(target: string): SyncResult | undefined {
|
|
80
|
+
return this.lastSync.get(target);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
addHandler(target: string, handler: () => Promise<SyncResult>) {
|
|
84
|
+
this.syncHandlers.set(target, handler);
|
|
85
|
+
this.targets.add(target);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private async syncGitHub(): Promise<SyncResult> {
|
|
89
|
+
return { target: 'github', success: true, items: 0, timestamp: Date.now() };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private async syncNotion(): Promise<SyncResult> {
|
|
93
|
+
return { target: 'notion', success: true, items: 0, timestamp: Date.now() };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private async syncSlack(): Promise<SyncResult> {
|
|
97
|
+
return { target: 'slack', success: true, items: 0, timestamp: Date.now() };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private async syncGmail(): Promise<SyncResult> {
|
|
101
|
+
return { target: 'gmail', success: true, items: 0, timestamp: Date.now() };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private async syncCalendar(): Promise<SyncResult> {
|
|
105
|
+
return { target: 'calendar', success: true, items: 0, timestamp: Date.now() };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export default AutoFetch;
|