adaptive-memory-multi-model-router 1.3.1 → 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/oauth.js +26 -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/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,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;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Tree Hierarchy
|
|
3
|
+
*
|
|
4
|
+
* Canonicalizes data into ≤3k-token chunks, scores them,
|
|
5
|
+
* and builds hierarchical summary trees.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface MemoryChunk {
|
|
9
|
+
id: string;
|
|
10
|
+
content: string;
|
|
11
|
+
score: number;
|
|
12
|
+
parentId?: string;
|
|
13
|
+
depth: number;
|
|
14
|
+
createdAt: number;
|
|
15
|
+
accessCount: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TreeNode {
|
|
19
|
+
id: string;
|
|
20
|
+
chunks: MemoryChunk[];
|
|
21
|
+
summary: string;
|
|
22
|
+
children: TreeNode[];
|
|
23
|
+
depth: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class MemoryTree {
|
|
27
|
+
private maxChunkSize: number;
|
|
28
|
+
private root: TreeNode;
|
|
29
|
+
private chunks: Map<string, MemoryChunk>;
|
|
30
|
+
private idCounter: number;
|
|
31
|
+
|
|
32
|
+
constructor(maxChunkSize = 3000) {
|
|
33
|
+
this.maxChunkSize = maxChunkSize;
|
|
34
|
+
this.root = this.createNode('root');
|
|
35
|
+
this.chunks = new Map();
|
|
36
|
+
this.idCounter = 0;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
private createNode(id: string, depth = 0): TreeNode {
|
|
40
|
+
return { id, chunks: [], summary: '', children: [], depth };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private generateId(): string {
|
|
44
|
+
return `chunk_${Date.now()}_${this.idCounter++}`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Add data to the memory tree
|
|
49
|
+
*/
|
|
50
|
+
async add(data: string): Promise<MemoryChunk[]> {
|
|
51
|
+
const textChunks = this.chunk(data);
|
|
52
|
+
const addedChunks: MemoryChunk[] = [];
|
|
53
|
+
|
|
54
|
+
for (const text of textChunks) {
|
|
55
|
+
const score = await this.scoreChunk(text);
|
|
56
|
+
const chunk: MemoryChunk = {
|
|
57
|
+
id: this.generateId(),
|
|
58
|
+
content: text,
|
|
59
|
+
score,
|
|
60
|
+
depth: 0,
|
|
61
|
+
createdAt: Date.now(),
|
|
62
|
+
accessCount: 0
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
this.chunks.set(chunk.id, chunk);
|
|
66
|
+
this.root.chunks.push(chunk);
|
|
67
|
+
this.insertIntoTree(chunk);
|
|
68
|
+
addedChunks.push(chunk);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
await this.updateSummaries();
|
|
72
|
+
return addedChunks;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Split text into chunks of maxChunkSize
|
|
77
|
+
*/
|
|
78
|
+
private chunk(text: string): string[] {
|
|
79
|
+
const chunks: string[] = [];
|
|
80
|
+
const words = text.split(/\s+/);
|
|
81
|
+
let current: string[] = [];
|
|
82
|
+
let currentSize = 0;
|
|
83
|
+
|
|
84
|
+
for (const word of words) {
|
|
85
|
+
currentSize += word.length + 1;
|
|
86
|
+
if (currentSize > this.maxChunkSize) {
|
|
87
|
+
chunks.push(current.join(' '));
|
|
88
|
+
current = [word];
|
|
89
|
+
currentSize = word.length + 1;
|
|
90
|
+
} else {
|
|
91
|
+
current.push(word);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (current.length > 0) {
|
|
96
|
+
chunks.push(current.join(' '));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return chunks;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Score a chunk by relevance
|
|
104
|
+
*/
|
|
105
|
+
private async scoreChunk(content: string): Promise<number> {
|
|
106
|
+
// Simple scoring: length + unique words ratio
|
|
107
|
+
const words = content.split(/\s+/);
|
|
108
|
+
const uniqueWords = new Set(words.map(w => w.toLowerCase()));
|
|
109
|
+
const uniqueRatio = uniqueWords.size / words.length;
|
|
110
|
+
|
|
111
|
+
// Base score + bonus for high unique ratio
|
|
112
|
+
return Math.min(1, (content.length / this.maxChunkSize) * uniqueRatio * 1.5);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Insert chunk into tree hierarchy
|
|
117
|
+
*/
|
|
118
|
+
private insertIntoTree(chunk: MemoryChunk) {
|
|
119
|
+
let parent = this.root;
|
|
120
|
+
|
|
121
|
+
while (parent.children.length > 0 && chunk.score < 0.5) {
|
|
122
|
+
// Find best matching child
|
|
123
|
+
let bestChild = parent.children[0];
|
|
124
|
+
let bestScore = 0;
|
|
125
|
+
|
|
126
|
+
for (const child of parent.children) {
|
|
127
|
+
const avgScore = this.getAverageScore(child);
|
|
128
|
+
if (avgScore > bestScore && avgScore >= chunk.score) {
|
|
129
|
+
bestScore = avgScore;
|
|
130
|
+
bestChild = child;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (bestScore >= chunk.score) {
|
|
135
|
+
parent = bestChild;
|
|
136
|
+
chunk.depth = parent.depth + 1;
|
|
137
|
+
chunk.parentId = parent.id;
|
|
138
|
+
} else {
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
parent.chunks.push(chunk);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private getAverageScore(node: TreeNode): number {
|
|
147
|
+
if (node.chunks.length === 0) return 0;
|
|
148
|
+
return node.chunks.reduce((sum, c) => sum + c.score, 0) / node.chunks.length;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Update summaries for tree nodes
|
|
153
|
+
*/
|
|
154
|
+
private async updateSummaries() {
|
|
155
|
+
this.summarizeNode(this.root);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private summarizeNode(node: TreeNode) {
|
|
159
|
+
const allContent = node.chunks.map(c => c.content).join(' ');
|
|
160
|
+
node.summary = allContent.slice(0, 200) + (allContent.length > 200 ? '...' : '');
|
|
161
|
+
|
|
162
|
+
for (const child of node.children) {
|
|
163
|
+
this.summarizeNode(child);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Search chunks by content
|
|
169
|
+
*/
|
|
170
|
+
search(query: string): MemoryChunk[] {
|
|
171
|
+
const results: MemoryChunk[] = [];
|
|
172
|
+
const queryLower = query.toLowerCase();
|
|
173
|
+
|
|
174
|
+
for (const chunk of this.chunks.values()) {
|
|
175
|
+
if (chunk.content.toLowerCase().includes(queryLower)) {
|
|
176
|
+
chunk.accessCount++;
|
|
177
|
+
results.push(chunk);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return results.sort((a, b) => b.score - a.score);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Get context for routing
|
|
186
|
+
*/
|
|
187
|
+
getContext(maxTokens = 3000): string {
|
|
188
|
+
const allChunks = Array.from(this.chunks.values())
|
|
189
|
+
.sort((a, b) => b.score - a.score)
|
|
190
|
+
.slice(0, 10);
|
|
191
|
+
|
|
192
|
+
let context = allChunks.map(c => c.content).join('\n\n');
|
|
193
|
+
|
|
194
|
+
if (context.length > maxTokens) {
|
|
195
|
+
context = context.slice(0, maxTokens) + '...';
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return context;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Export as markdown for Obsidian
|
|
203
|
+
*/
|
|
204
|
+
toMarkdown(): string {
|
|
205
|
+
const lines: string[] = ['# Memory Tree\n'];
|
|
206
|
+
|
|
207
|
+
const traverse = (node: TreeNode, prefix = '') => {
|
|
208
|
+
for (const chunk of node.chunks) {
|
|
209
|
+
lines.push(`${prefix}## ${chunk.id} (score: ${chunk.score.toFixed(2)})`);
|
|
210
|
+
lines.push(chunk.content);
|
|
211
|
+
lines.push('');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
for (const child of node.children) {
|
|
215
|
+
lines.push(`${prefix}### ${child.id}`);
|
|
216
|
+
traverse(child, prefix + '#');
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
traverse(this.root);
|
|
221
|
+
return lines.join('\n');
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Get tree stats
|
|
226
|
+
*/
|
|
227
|
+
getStats() {
|
|
228
|
+
return {
|
|
229
|
+
totalChunks: this.chunks.size,
|
|
230
|
+
maxDepth: this.getMaxDepth(this.root),
|
|
231
|
+
rootChunks: this.root.chunks.length,
|
|
232
|
+
treeSize: JSON.stringify(this.root).length
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private getMaxDepth(node: TreeNode): number {
|
|
237
|
+
if (node.children.length === 0) return node.depth;
|
|
238
|
+
return Math.max(...node.children.map(c => this.getMaxDepth(c)));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export default MemoryTree;
|