neoagent 2.3.1-beta.98 → 2.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/.env.example +6 -3
- package/flutter_app/lib/main.dart +1 -0
- package/flutter_app/lib/main_integrations.dart +21 -2
- package/flutter_app/lib/main_models.dart +60 -0
- package/flutter_app/lib/main_theme.dart +31 -2
- package/flutter_app/macos/Runner/AppDelegate.swift +11 -1
- package/flutter_app/macos/Runner/DebugProfile.entitlements +4 -0
- package/flutter_app/macos/Runner/Release.entitlements +4 -0
- package/flutter_app/pubspec.lock +5 -5
- package/lib/manager.js +164 -2
- package/package.json +1 -1
- package/server/db/database.js +85 -0
- package/server/public/.last_build_id +1 -1
- package/server/public/assets/NOTICES +971 -1066
- package/server/public/assets/fonts/MaterialIcons-Regular.otf +0 -0
- package/server/public/assets/shaders/ink_sparkle.frag +1 -1
- package/server/public/assets/shaders/stretch_effect.frag +1 -1
- package/server/public/canvaskit/canvaskit.js +2 -2
- package/server/public/canvaskit/canvaskit.js.symbols +11796 -11733
- package/server/public/canvaskit/canvaskit.wasm +0 -0
- package/server/public/canvaskit/chromium/canvaskit.js +2 -2
- package/server/public/canvaskit/chromium/canvaskit.js.symbols +10706 -10643
- package/server/public/canvaskit/chromium/canvaskit.wasm +0 -0
- package/server/public/canvaskit/experimental_webparagraph/canvaskit.js +171 -0
- package/server/public/canvaskit/experimental_webparagraph/canvaskit.js.symbols +9134 -0
- package/server/public/canvaskit/experimental_webparagraph/canvaskit.wasm +0 -0
- package/server/public/canvaskit/skwasm.js +14 -14
- package/server/public/canvaskit/skwasm.js.symbols +12787 -12676
- package/server/public/canvaskit/skwasm.wasm +0 -0
- package/server/public/canvaskit/skwasm_heavy.js +14 -14
- package/server/public/canvaskit/skwasm_heavy.js.symbols +14400 -14286
- package/server/public/canvaskit/skwasm_heavy.wasm +0 -0
- package/server/public/canvaskit/wimp.js +94 -95
- package/server/public/canvaskit/wimp.js.symbols +11325 -11177
- package/server/public/canvaskit/wimp.wasm +0 -0
- package/server/public/flutter_bootstrap.js +2 -2
- package/server/public/main.dart.js +83866 -82074
- package/server/routes/integrations.js +2 -2
- package/server/routes/memory.js +73 -0
- package/server/services/ai/engine.js +65 -26
- package/server/services/ai/models.js +21 -0
- package/server/services/ai/preModelCompaction.js +191 -0
- package/server/services/ai/providers/claudeCode.js +273 -0
- package/server/services/ai/providers/openaiCodex.js +226 -41
- package/server/services/ai/settings.js +11 -1
- package/server/services/integrations/google/provider.js +78 -0
- package/server/services/integrations/manager.js +29 -13
- package/server/services/manager.js +25 -0
- package/server/services/memory/ingestion.js +486 -0
- package/server/services/memory/manager.js +422 -0
- package/server/services/memory/openhuman_uplift.test.js +98 -0
- package/server/services/widgets/focus_widget.js +45 -4
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { v4: uuidv4 } = require('uuid');
|
|
4
|
+
const db = require('../../db/database');
|
|
5
|
+
const { resolveAgentId } = require('../agents/manager');
|
|
6
|
+
const { compactTextPayload } = require('../ai/preModelCompaction');
|
|
7
|
+
|
|
8
|
+
const SOURCE_TYPES = Object.freeze([
|
|
9
|
+
'email',
|
|
10
|
+
'calendar',
|
|
11
|
+
'chat',
|
|
12
|
+
'docs',
|
|
13
|
+
'tickets',
|
|
14
|
+
'repos',
|
|
15
|
+
'files',
|
|
16
|
+
'crm',
|
|
17
|
+
'payments',
|
|
18
|
+
'notes',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
const FRESHNESS_POLICIES = Object.freeze({
|
|
22
|
+
email: Object.freeze({ intervalMinutes: 60, staleAfterDays: 14 }),
|
|
23
|
+
calendar: Object.freeze({ intervalMinutes: 180, staleAfterDays: 30 }),
|
|
24
|
+
chat: Object.freeze({ intervalMinutes: 60, staleAfterDays: 14 }),
|
|
25
|
+
docs: Object.freeze({ intervalMinutes: 360, staleAfterDays: 60 }),
|
|
26
|
+
tickets: Object.freeze({ intervalMinutes: 120, staleAfterDays: 30 }),
|
|
27
|
+
repos: Object.freeze({ intervalMinutes: 180, staleAfterDays: 45 }),
|
|
28
|
+
files: Object.freeze({ intervalMinutes: 360, staleAfterDays: 60 }),
|
|
29
|
+
crm: Object.freeze({ intervalMinutes: 240, staleAfterDays: 45 }),
|
|
30
|
+
payments: Object.freeze({ intervalMinutes: 360, staleAfterDays: 90 }),
|
|
31
|
+
notes: Object.freeze({ intervalMinutes: 360, staleAfterDays: 90 }),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const SOURCE_MEMORY_CATEGORIES = Object.freeze({
|
|
35
|
+
email: 'episodic',
|
|
36
|
+
calendar: 'events',
|
|
37
|
+
chat: 'episodic',
|
|
38
|
+
docs: 'projects',
|
|
39
|
+
tickets: 'tasks',
|
|
40
|
+
repos: 'projects',
|
|
41
|
+
files: 'episodic',
|
|
42
|
+
crm: 'contacts',
|
|
43
|
+
payments: 'events',
|
|
44
|
+
notes: 'episodic',
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const INTEGRATION_SOURCE_TYPES = Object.freeze({
|
|
48
|
+
google_workspace: Object.freeze({
|
|
49
|
+
gmail: Object.freeze(['email']),
|
|
50
|
+
calendar: Object.freeze(['calendar']),
|
|
51
|
+
drive: Object.freeze(['files']),
|
|
52
|
+
docs: Object.freeze(['docs']),
|
|
53
|
+
sheets: Object.freeze(['docs', 'files']),
|
|
54
|
+
}),
|
|
55
|
+
microsoft_365: Object.freeze({
|
|
56
|
+
outlook: Object.freeze(['email']),
|
|
57
|
+
calendar: Object.freeze(['calendar']),
|
|
58
|
+
onedrive: Object.freeze(['files']),
|
|
59
|
+
teams: Object.freeze(['chat']),
|
|
60
|
+
}),
|
|
61
|
+
github: Object.freeze({
|
|
62
|
+
repos: Object.freeze(['repos', 'tickets']),
|
|
63
|
+
}),
|
|
64
|
+
slack: Object.freeze({
|
|
65
|
+
slack: Object.freeze(['chat']),
|
|
66
|
+
}),
|
|
67
|
+
whatsapp: Object.freeze({
|
|
68
|
+
personal: Object.freeze(['chat']),
|
|
69
|
+
}),
|
|
70
|
+
notion: Object.freeze({
|
|
71
|
+
notion: Object.freeze(['notes', 'docs']),
|
|
72
|
+
}),
|
|
73
|
+
trello: Object.freeze({
|
|
74
|
+
trello: Object.freeze(['tickets']),
|
|
75
|
+
}),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
function safeTrim(value, maxLength = 240) {
|
|
79
|
+
return String(value || '').trim().slice(0, maxLength);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function parseJsonObject(value, fallback = {}) {
|
|
83
|
+
if (!value) return { ...fallback };
|
|
84
|
+
if (typeof value === 'object' && !Array.isArray(value)) return { ...value };
|
|
85
|
+
try {
|
|
86
|
+
const parsed = JSON.parse(String(value));
|
|
87
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
|
88
|
+
? parsed
|
|
89
|
+
: { ...fallback };
|
|
90
|
+
} catch {
|
|
91
|
+
return { ...fallback };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function normalizeSourceType(value) {
|
|
96
|
+
const normalized = String(value || '').trim().toLowerCase();
|
|
97
|
+
return SOURCE_TYPES.includes(normalized) ? normalized : 'notes';
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function getFreshnessPolicy(sourceType) {
|
|
101
|
+
const normalized = normalizeSourceType(sourceType);
|
|
102
|
+
return { ...FRESHNESS_POLICIES[normalized] };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function nextSyncFromPolicy(policy, now = Date.now()) {
|
|
106
|
+
const intervalMinutes = Math.max(15, Number(policy?.intervalMinutes) || 360);
|
|
107
|
+
return new Date(now + intervalMinutes * 60 * 1000).toISOString();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function normalizeDocument(raw = {}, defaults = {}) {
|
|
111
|
+
const sourceType = normalizeSourceType(raw.sourceType || defaults.sourceType);
|
|
112
|
+
const externalObjectId = safeTrim(raw.externalObjectId || raw.id || raw.sourceId, 180);
|
|
113
|
+
const contentSource = raw.content ?? raw.text ?? raw.body ?? '';
|
|
114
|
+
const content = safeTrim(contentSource, 12000);
|
|
115
|
+
if (!externalObjectId || !content) {
|
|
116
|
+
throw new Error('Normalized memory documents require externalObjectId and content.');
|
|
117
|
+
}
|
|
118
|
+
const compacted = compactTextPayload(content, { maxChars: 2400, maxLines: 50 });
|
|
119
|
+
return {
|
|
120
|
+
sourceType,
|
|
121
|
+
normalizedType: normalizeSourceType(raw.normalizedType || raw.memoryType || sourceType),
|
|
122
|
+
providerKey: safeTrim(raw.providerKey || defaults.providerKey, 80),
|
|
123
|
+
connectionId: Number.isInteger(Number(raw.connectionId ?? defaults.connectionId))
|
|
124
|
+
? Number(raw.connectionId ?? defaults.connectionId)
|
|
125
|
+
: 0,
|
|
126
|
+
externalObjectId,
|
|
127
|
+
sourceAccount: safeTrim(raw.sourceAccount || defaults.sourceAccount, 180),
|
|
128
|
+
title: safeTrim(raw.title || raw.subject || raw.name || externalObjectId, 220),
|
|
129
|
+
content,
|
|
130
|
+
summary: safeTrim(raw.summary || compacted.text, 1200),
|
|
131
|
+
salience: Math.max(1, Math.min(10, Number(raw.salience) || 5)),
|
|
132
|
+
sourceTimestamp: safeTrim(raw.sourceTimestamp || raw.updatedAt || raw.createdAt, 80) || null,
|
|
133
|
+
metadata: {
|
|
134
|
+
...parseJsonObject(raw.metadata, {}),
|
|
135
|
+
compaction: compacted.metrics,
|
|
136
|
+
},
|
|
137
|
+
payload: parseJsonObject(raw.payload, {}),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function sourceTypesForConnection(providerKey, appKey) {
|
|
142
|
+
const providerMap = INTEGRATION_SOURCE_TYPES[String(providerKey || '').trim()] || {};
|
|
143
|
+
return Array.from(providerMap[String(appKey || '').trim()] || []);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function buildCoverageForConnection(connection, latestJob = null) {
|
|
147
|
+
const dataDomains = sourceTypesForConnection(connection.provider_key, connection.app_key);
|
|
148
|
+
const supported = dataDomains.length > 0;
|
|
149
|
+
return {
|
|
150
|
+
supported,
|
|
151
|
+
contributesToMemory: supported,
|
|
152
|
+
contributesToTaskExecution: String(connection.status || '') === 'connected',
|
|
153
|
+
status: latestJob?.status || (supported ? 'ready' : 'not_supported'),
|
|
154
|
+
dataDomains,
|
|
155
|
+
lastRefreshAt: latestJob?.completedAt || latestJob?.updatedAt || null,
|
|
156
|
+
nextRefreshAt: latestJob?.nextSyncAt || null,
|
|
157
|
+
documentCount: Number(latestJob?.documentCount || 0),
|
|
158
|
+
error: latestJob?.error || null,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
class MemoryIngestionService {
|
|
163
|
+
constructor({ memoryManager, integrationManager, intervalMs = 10 * 60 * 1000 } = {}) {
|
|
164
|
+
this.memoryManager = memoryManager;
|
|
165
|
+
this.integrationManager = integrationManager;
|
|
166
|
+
this.intervalMs = intervalMs;
|
|
167
|
+
this.timer = null;
|
|
168
|
+
this.running = false;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
start() {
|
|
172
|
+
if (this.timer) return;
|
|
173
|
+
this.timer = setInterval(() => {
|
|
174
|
+
this.refreshDueConnections().catch((err) => {
|
|
175
|
+
console.warn('[MemoryIngestion] Background refresh failed:', err.message);
|
|
176
|
+
});
|
|
177
|
+
}, this.intervalMs);
|
|
178
|
+
this.timer.unref?.();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
stop() {
|
|
182
|
+
if (this.timer) clearInterval(this.timer);
|
|
183
|
+
this.timer = null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
getFreshnessPolicy(sourceType) {
|
|
187
|
+
return getFreshnessPolicy(sourceType);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async ingestDocuments(userId, documents = [], options = {}) {
|
|
191
|
+
const agentId = resolveAgentId(userId, options.agentId || options.agent_id || null);
|
|
192
|
+
const normalizedDocs = (Array.isArray(documents) ? documents : [])
|
|
193
|
+
.map((document) => normalizeDocument(document, {
|
|
194
|
+
sourceType: options.sourceType,
|
|
195
|
+
providerKey: options.providerKey,
|
|
196
|
+
connectionId: options.connectionId,
|
|
197
|
+
sourceAccount: options.sourceAccount,
|
|
198
|
+
}));
|
|
199
|
+
const sourceType = normalizeSourceType(options.sourceType || normalizedDocs[0]?.sourceType);
|
|
200
|
+
const policy = getFreshnessPolicy(sourceType);
|
|
201
|
+
const jobId = uuidv4();
|
|
202
|
+
|
|
203
|
+
this.memoryManager.recordIngestionJob(userId, {
|
|
204
|
+
id: jobId,
|
|
205
|
+
sourceType,
|
|
206
|
+
providerKey: safeTrim(options.providerKey, 80),
|
|
207
|
+
connectionId: Number.isInteger(Number(options.connectionId)) ? Number(options.connectionId) : 0,
|
|
208
|
+
status: 'running',
|
|
209
|
+
freshnessPolicy: policy,
|
|
210
|
+
metadata: parseJsonObject(options.metadata, {}),
|
|
211
|
+
documentCount: 0,
|
|
212
|
+
nextSyncAt: nextSyncFromPolicy(policy),
|
|
213
|
+
}, { agentId });
|
|
214
|
+
|
|
215
|
+
const documentIds = [];
|
|
216
|
+
const memoryIds = [];
|
|
217
|
+
try {
|
|
218
|
+
for (const document of normalizedDocs) {
|
|
219
|
+
const documentId = this.memoryManager.upsertIngestionDocument(userId, document, { agentId });
|
|
220
|
+
documentIds.push(documentId);
|
|
221
|
+
const memoryId = await this.memoryManager.saveMemory(
|
|
222
|
+
userId,
|
|
223
|
+
`${document.title}: ${document.summary || document.content}`,
|
|
224
|
+
SOURCE_MEMORY_CATEGORIES[document.normalizedType] || SOURCE_MEMORY_CATEGORIES[document.sourceType] || 'episodic',
|
|
225
|
+
document.salience,
|
|
226
|
+
{
|
|
227
|
+
agentId,
|
|
228
|
+
staleAfterDays: getFreshnessPolicy(document.sourceType).staleAfterDays,
|
|
229
|
+
sourceRef: {
|
|
230
|
+
sourceType: 'memory_ingestion',
|
|
231
|
+
sourceId: document.externalObjectId,
|
|
232
|
+
sourceLabel: document.title,
|
|
233
|
+
},
|
|
234
|
+
scope: {
|
|
235
|
+
scopeType: 'agent',
|
|
236
|
+
scopeId: agentId,
|
|
237
|
+
},
|
|
238
|
+
metadata: {
|
|
239
|
+
ingestionJobId: jobId,
|
|
240
|
+
ingestionDocumentId: documentId,
|
|
241
|
+
providerKey: document.providerKey || null,
|
|
242
|
+
connectionId: document.connectionId || null,
|
|
243
|
+
sourceType: document.sourceType,
|
|
244
|
+
},
|
|
245
|
+
},
|
|
246
|
+
);
|
|
247
|
+
if (memoryId) memoryIds.push(memoryId);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
this.memoryManager.recordIngestionJob(userId, {
|
|
251
|
+
id: jobId,
|
|
252
|
+
sourceType,
|
|
253
|
+
providerKey: safeTrim(options.providerKey, 80),
|
|
254
|
+
connectionId: Number.isInteger(Number(options.connectionId)) ? Number(options.connectionId) : 0,
|
|
255
|
+
status: 'completed',
|
|
256
|
+
freshnessPolicy: policy,
|
|
257
|
+
summary: { documentIds, memoryIds },
|
|
258
|
+
metadata: parseJsonObject(options.metadata, {}),
|
|
259
|
+
documentCount: documentIds.length,
|
|
260
|
+
completedAt: new Date().toISOString(),
|
|
261
|
+
nextSyncAt: nextSyncFromPolicy(policy),
|
|
262
|
+
}, { agentId });
|
|
263
|
+
|
|
264
|
+
const knowledgeViews = this.memoryManager.materializeKnowledgeViews(userId, { agentId });
|
|
265
|
+
return {
|
|
266
|
+
jobId,
|
|
267
|
+
status: 'completed',
|
|
268
|
+
documentIds,
|
|
269
|
+
memoryIds,
|
|
270
|
+
knowledgeViews,
|
|
271
|
+
};
|
|
272
|
+
} catch (err) {
|
|
273
|
+
this.memoryManager.recordIngestionJob(userId, {
|
|
274
|
+
id: jobId,
|
|
275
|
+
sourceType,
|
|
276
|
+
providerKey: safeTrim(options.providerKey, 80),
|
|
277
|
+
connectionId: Number.isInteger(Number(options.connectionId)) ? Number(options.connectionId) : 0,
|
|
278
|
+
status: 'failed',
|
|
279
|
+
freshnessPolicy: policy,
|
|
280
|
+
documentCount: documentIds.length,
|
|
281
|
+
error: err.message,
|
|
282
|
+
completedAt: new Date().toISOString(),
|
|
283
|
+
nextSyncAt: nextSyncFromPolicy(policy),
|
|
284
|
+
}, { agentId });
|
|
285
|
+
throw err;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async refreshDueConnections(userId = null) {
|
|
290
|
+
if (this.running) return { skipped: true };
|
|
291
|
+
this.running = true;
|
|
292
|
+
try {
|
|
293
|
+
const params = [];
|
|
294
|
+
let sql = `SELECT *
|
|
295
|
+
FROM integration_connections
|
|
296
|
+
WHERE status = 'connected'`;
|
|
297
|
+
if (userId != null) {
|
|
298
|
+
sql += ' AND user_id = ?';
|
|
299
|
+
params.push(userId);
|
|
300
|
+
}
|
|
301
|
+
const connections = db.prepare(sql).all(...params);
|
|
302
|
+
const results = [];
|
|
303
|
+
for (const connection of connections) {
|
|
304
|
+
results.push(await this.refreshConnection(connection));
|
|
305
|
+
}
|
|
306
|
+
return { refreshed: results.length, results };
|
|
307
|
+
} finally {
|
|
308
|
+
this.running = false;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async refreshConnection(connection) {
|
|
313
|
+
const sourceTypes = sourceTypesForConnection(connection.provider_key, connection.app_key);
|
|
314
|
+
if (sourceTypes.length === 0) {
|
|
315
|
+
return { connectionId: connection.id, status: 'not_supported' };
|
|
316
|
+
}
|
|
317
|
+
const provider = this.integrationManager?.getProvider?.(connection.provider_key);
|
|
318
|
+
const agentId = connection.agent_id || resolveAgentId(connection.user_id, null);
|
|
319
|
+
const primarySource = sourceTypes[0];
|
|
320
|
+
const policy = getFreshnessPolicy(primarySource);
|
|
321
|
+
const latestJob = this.memoryManager
|
|
322
|
+
.listIngestionJobs(connection.user_id, {
|
|
323
|
+
agentId,
|
|
324
|
+
providerKey: connection.provider_key,
|
|
325
|
+
limit: 1,
|
|
326
|
+
})
|
|
327
|
+
.find((job) => Number(job.connectionId || 0) === Number(connection.id));
|
|
328
|
+
|
|
329
|
+
if (latestJob?.nextSyncAt && Date.parse(latestJob.nextSyncAt) > Date.now()) {
|
|
330
|
+
return { connectionId: connection.id, status: 'fresh' };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (typeof provider?.collectMemoryDocuments === 'function') {
|
|
334
|
+
const collected = await provider.collectMemoryDocuments({
|
|
335
|
+
connection,
|
|
336
|
+
sourceTypes,
|
|
337
|
+
cursor: latestJob?.cursor || {},
|
|
338
|
+
});
|
|
339
|
+
return this.ingestDocuments(connection.user_id, collected.documents || [], {
|
|
340
|
+
agentId,
|
|
341
|
+
sourceType: primarySource,
|
|
342
|
+
providerKey: connection.provider_key,
|
|
343
|
+
connectionId: connection.id,
|
|
344
|
+
sourceAccount: connection.account_email,
|
|
345
|
+
metadata: {
|
|
346
|
+
appKey: connection.app_key,
|
|
347
|
+
sourceTypes,
|
|
348
|
+
cursor: collected.cursor || null,
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const jobId = this.memoryManager.recordIngestionJob(connection.user_id, {
|
|
354
|
+
id: uuidv4(),
|
|
355
|
+
sourceType: primarySource,
|
|
356
|
+
providerKey: connection.provider_key,
|
|
357
|
+
connectionId: connection.id,
|
|
358
|
+
status: 'ready',
|
|
359
|
+
freshnessPolicy: policy,
|
|
360
|
+
summary: {
|
|
361
|
+
appKey: connection.app_key,
|
|
362
|
+
sourceTypes,
|
|
363
|
+
collectorAvailable: false,
|
|
364
|
+
},
|
|
365
|
+
metadata: {
|
|
366
|
+
accountEmail: connection.account_email || null,
|
|
367
|
+
},
|
|
368
|
+
documentCount: 0,
|
|
369
|
+
completedAt: new Date().toISOString(),
|
|
370
|
+
nextSyncAt: nextSyncFromPolicy(policy),
|
|
371
|
+
}, { agentId });
|
|
372
|
+
return { connectionId: connection.id, status: 'ready', jobId };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
listConnectionStatuses(userId, { agentId = null } = {}) {
|
|
376
|
+
const scopedAgentId = resolveAgentId(userId, agentId);
|
|
377
|
+
const connections = db.prepare(
|
|
378
|
+
`SELECT *
|
|
379
|
+
FROM integration_connections
|
|
380
|
+
WHERE user_id = ? AND agent_id = ?
|
|
381
|
+
ORDER BY updated_at DESC, id DESC`,
|
|
382
|
+
).all(userId, scopedAgentId);
|
|
383
|
+
const jobs = this.memoryManager.listIngestionJobs(userId, { agentId: scopedAgentId, limit: 100 });
|
|
384
|
+
return connections.map((connection) => {
|
|
385
|
+
const latestJob = jobs.find((job) => Number(job.connectionId || 0) === Number(connection.id));
|
|
386
|
+
return {
|
|
387
|
+
connectionId: connection.id,
|
|
388
|
+
providerKey: connection.provider_key,
|
|
389
|
+
appKey: connection.app_key,
|
|
390
|
+
accountEmail: connection.account_email || null,
|
|
391
|
+
...buildCoverageForConnection(connection, latestJob),
|
|
392
|
+
};
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
decorateProviderSnapshot(snapshot, userId, agentId = null) {
|
|
397
|
+
if (!snapshot || typeof snapshot !== 'object') return snapshot;
|
|
398
|
+
const scopedAgentId = resolveAgentId(userId, agentId);
|
|
399
|
+
const connections = db.prepare(
|
|
400
|
+
`SELECT *
|
|
401
|
+
FROM integration_connections
|
|
402
|
+
WHERE user_id = ? AND agent_id = ? AND provider_key = ?`,
|
|
403
|
+
).all(userId, scopedAgentId, snapshot.provider);
|
|
404
|
+
const jobs = this.memoryManager.listIngestionJobs(userId, {
|
|
405
|
+
agentId: scopedAgentId,
|
|
406
|
+
providerKey: snapshot.provider,
|
|
407
|
+
limit: 100,
|
|
408
|
+
});
|
|
409
|
+
const latestJobForConnection = (connectionId) =>
|
|
410
|
+
jobs.find((job) => Number(job.connectionId || 0) === Number(connectionId));
|
|
411
|
+
|
|
412
|
+
const connectionCoverage = connections.map((connection) => ({
|
|
413
|
+
connectionId: connection.id,
|
|
414
|
+
appKey: connection.app_key,
|
|
415
|
+
accountEmail: connection.account_email || null,
|
|
416
|
+
...buildCoverageForConnection(connection, latestJobForConnection(connection.id)),
|
|
417
|
+
}));
|
|
418
|
+
const providerDomains = Array.from(new Set(connectionCoverage.flatMap((item) => item.dataDomains || [])));
|
|
419
|
+
const providerJob = jobs[0] || null;
|
|
420
|
+
const decorated = {
|
|
421
|
+
...snapshot,
|
|
422
|
+
memoryCoverage: {
|
|
423
|
+
supported: providerDomains.length > 0,
|
|
424
|
+
contributesToMemory: providerDomains.length > 0,
|
|
425
|
+
contributesToTaskExecution: Boolean(snapshot.connection?.connected),
|
|
426
|
+
status: providerJob?.status || (providerDomains.length > 0 ? 'ready' : 'not_supported'),
|
|
427
|
+
dataDomains: providerDomains,
|
|
428
|
+
documentCount: connectionCoverage.reduce((sum, item) => sum + Number(item.documentCount || 0), 0),
|
|
429
|
+
lastRefreshAt: providerJob?.completedAt || providerJob?.updatedAt || null,
|
|
430
|
+
nextRefreshAt: providerJob?.nextSyncAt || null,
|
|
431
|
+
error: providerJob?.error || null,
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
decorated.apps = (snapshot.apps || []).map((app) => {
|
|
436
|
+
const appConnections = connectionCoverage.filter((item) => item.appKey === app.id);
|
|
437
|
+
const dataDomains = Array.from(new Set([
|
|
438
|
+
...sourceTypesForConnection(snapshot.provider, app.id),
|
|
439
|
+
...appConnections.flatMap((item) => item.dataDomains || []),
|
|
440
|
+
]));
|
|
441
|
+
const latest = appConnections[0] || null;
|
|
442
|
+
return {
|
|
443
|
+
...app,
|
|
444
|
+
memoryCoverage: {
|
|
445
|
+
supported: dataDomains.length > 0,
|
|
446
|
+
contributesToMemory: dataDomains.length > 0 && app.connection?.connected === true,
|
|
447
|
+
contributesToTaskExecution: app.connection?.connected === true,
|
|
448
|
+
status: latest?.status || (dataDomains.length > 0 ? 'ready' : 'not_supported'),
|
|
449
|
+
dataDomains,
|
|
450
|
+
documentCount: appConnections.reduce((sum, item) => sum + Number(item.documentCount || 0), 0),
|
|
451
|
+
lastRefreshAt: latest?.lastRefreshAt || null,
|
|
452
|
+
nextRefreshAt: latest?.nextRefreshAt || null,
|
|
453
|
+
error: latest?.error || null,
|
|
454
|
+
},
|
|
455
|
+
accounts: (app.accounts || []).map((account) => {
|
|
456
|
+
const accountCoverage = appConnections.find((item) =>
|
|
457
|
+
Number(item.connectionId) === Number(account.connectionId),
|
|
458
|
+
);
|
|
459
|
+
return {
|
|
460
|
+
...account,
|
|
461
|
+
memoryCoverage: accountCoverage || {
|
|
462
|
+
supported: dataDomains.length > 0,
|
|
463
|
+
contributesToMemory: false,
|
|
464
|
+
contributesToTaskExecution: app.connection?.connected === true,
|
|
465
|
+
status: dataDomains.length > 0 ? 'ready' : 'not_supported',
|
|
466
|
+
dataDomains,
|
|
467
|
+
documentCount: 0,
|
|
468
|
+
lastRefreshAt: null,
|
|
469
|
+
nextRefreshAt: null,
|
|
470
|
+
error: null,
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
}),
|
|
474
|
+
};
|
|
475
|
+
});
|
|
476
|
+
return decorated;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
module.exports = {
|
|
481
|
+
MemoryIngestionService,
|
|
482
|
+
SOURCE_TYPES,
|
|
483
|
+
FRESHNESS_POLICIES,
|
|
484
|
+
normalizeSourceType,
|
|
485
|
+
sourceTypesForConnection,
|
|
486
|
+
};
|