@usewhisper/mcp-server 0.2.3 → 0.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 +23 -23
- package/dist/autosubscribe-GHO6YR5A.js +4068 -0
- package/dist/chunk-52VJYCZ7.js +455 -0
- package/dist/chunk-5KBZQHDL.js +189 -0
- package/dist/chunk-7SN3CKDK.js +1076 -0
- package/dist/chunk-EI5CE3EY.js +616 -0
- package/dist/chunk-JO3ORBZD.js +616 -0
- package/dist/chunk-LMEYV4JD.js +368 -0
- package/dist/chunk-MEFLJ4PV.js +8385 -0
- package/dist/chunk-PPGYJJED.js +271 -0
- package/dist/chunk-T7KMSTWP.js +399 -0
- package/dist/chunk-TWEIYHI6.js +399 -0
- package/dist/consolidation-2GCKI4RE.js +220 -0
- package/dist/consolidation-4JOPW6BG.js +220 -0
- package/dist/context-sharing-4ITCNKG4.js +307 -0
- package/dist/context-sharing-GYKLXHZA.js +307 -0
- package/dist/context-sharing-Y6LTZZOF.js +307 -0
- package/dist/cost-optimization-7DVSTL6R.js +307 -0
- package/dist/ingest-7T5FAZNC.js +15 -0
- package/dist/ingest-EBNIE7XB.js +15 -0
- package/dist/ingest-FSHT5BCS.js +15 -0
- package/dist/oracle-3RLQF3DP.js +259 -0
- package/dist/oracle-FKRTQUUG.js +282 -0
- package/dist/search-EG6TYWWW.js +13 -0
- package/dist/search-I22QQA7T.js +13 -0
- package/dist/search-T7H5G6DW.js +13 -0
- package/dist/server.js +1124 -1094
- package/package.json +2 -6
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSessionMemories
|
|
3
|
+
} from "./chunk-PPGYJJED.js";
|
|
4
|
+
import "./chunk-52VJYCZ7.js";
|
|
5
|
+
import "./chunk-LMEYV4JD.js";
|
|
6
|
+
import {
|
|
7
|
+
db
|
|
8
|
+
} from "./chunk-3WGYBAYR.js";
|
|
9
|
+
import "./chunk-QGM4M3NI.js";
|
|
10
|
+
|
|
11
|
+
// ../node_modules/nanoid/index.js
|
|
12
|
+
import { webcrypto as crypto } from "crypto";
|
|
13
|
+
|
|
14
|
+
// ../node_modules/nanoid/url-alphabet/index.js
|
|
15
|
+
var urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
16
|
+
|
|
17
|
+
// ../node_modules/nanoid/index.js
|
|
18
|
+
var POOL_SIZE_MULTIPLIER = 128;
|
|
19
|
+
var pool;
|
|
20
|
+
var poolOffset;
|
|
21
|
+
function fillPool(bytes) {
|
|
22
|
+
if (!pool || pool.length < bytes) {
|
|
23
|
+
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER);
|
|
24
|
+
crypto.getRandomValues(pool);
|
|
25
|
+
poolOffset = 0;
|
|
26
|
+
} else if (poolOffset + bytes > pool.length) {
|
|
27
|
+
crypto.getRandomValues(pool);
|
|
28
|
+
poolOffset = 0;
|
|
29
|
+
}
|
|
30
|
+
poolOffset += bytes;
|
|
31
|
+
}
|
|
32
|
+
function nanoid(size = 21) {
|
|
33
|
+
fillPool(size |= 0);
|
|
34
|
+
let id = "";
|
|
35
|
+
for (let i = poolOffset - size; i < poolOffset; i++) {
|
|
36
|
+
id += urlAlphabet[pool[i] & 63];
|
|
37
|
+
}
|
|
38
|
+
return id;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ../src/engine/context-sharing.ts
|
|
42
|
+
async function createSharedContext(params) {
|
|
43
|
+
const {
|
|
44
|
+
sessionId,
|
|
45
|
+
projectId,
|
|
46
|
+
orgId,
|
|
47
|
+
userId,
|
|
48
|
+
includeMemories = true,
|
|
49
|
+
includeMessages = true,
|
|
50
|
+
includeChunks = false,
|
|
51
|
+
expiryDays = 7,
|
|
52
|
+
metadata = {}
|
|
53
|
+
} = params;
|
|
54
|
+
const shareId = nanoid(12);
|
|
55
|
+
const session = await db.session.findUnique({
|
|
56
|
+
where: { id: sessionId }
|
|
57
|
+
});
|
|
58
|
+
if (!session) {
|
|
59
|
+
throw new Error("Session not found");
|
|
60
|
+
}
|
|
61
|
+
const shareData = {
|
|
62
|
+
sessionId,
|
|
63
|
+
projectId,
|
|
64
|
+
userId,
|
|
65
|
+
metadata
|
|
66
|
+
};
|
|
67
|
+
if (includeMemories) {
|
|
68
|
+
const memories = await getSessionMemories({
|
|
69
|
+
sessionId,
|
|
70
|
+
projectId,
|
|
71
|
+
limit: 200
|
|
72
|
+
});
|
|
73
|
+
shareData.memories = memories.map((m) => ({
|
|
74
|
+
id: m.id,
|
|
75
|
+
content: m.content,
|
|
76
|
+
type: m.memoryType,
|
|
77
|
+
entities: m.entityMentions,
|
|
78
|
+
confidence: m.confidence,
|
|
79
|
+
documentDate: m.documentDate?.toISOString(),
|
|
80
|
+
eventDate: m.eventDate?.toISOString()
|
|
81
|
+
}));
|
|
82
|
+
} else {
|
|
83
|
+
shareData.memories = [];
|
|
84
|
+
}
|
|
85
|
+
if (includeMessages) {
|
|
86
|
+
const messages = await db.message.findMany({
|
|
87
|
+
where: { sessionId },
|
|
88
|
+
orderBy: { createdAt: "asc" },
|
|
89
|
+
take: 500
|
|
90
|
+
});
|
|
91
|
+
shareData.messages = messages.map((m) => ({
|
|
92
|
+
role: m.role,
|
|
93
|
+
content: m.content,
|
|
94
|
+
createdAt: m.createdAt.toISOString()
|
|
95
|
+
}));
|
|
96
|
+
} else {
|
|
97
|
+
shareData.messages = [];
|
|
98
|
+
}
|
|
99
|
+
if (includeChunks) {
|
|
100
|
+
const chunkIds = shareData.memories.map((m) => m.sourceChunkId).filter(Boolean).slice(0, 50);
|
|
101
|
+
if (chunkIds.length > 0) {
|
|
102
|
+
const chunks = await db.chunk.findMany({
|
|
103
|
+
where: {
|
|
104
|
+
id: { in: chunkIds }
|
|
105
|
+
},
|
|
106
|
+
select: {
|
|
107
|
+
id: true,
|
|
108
|
+
content: true,
|
|
109
|
+
metadata: true,
|
|
110
|
+
chunkType: true
|
|
111
|
+
},
|
|
112
|
+
take: 50
|
|
113
|
+
});
|
|
114
|
+
shareData.chunks = chunks;
|
|
115
|
+
} else {
|
|
116
|
+
shareData.chunks = [];
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
shareData.chunks = [];
|
|
120
|
+
}
|
|
121
|
+
const expiresAt = expiryDays ? new Date(Date.now() + expiryDays * 24 * 60 * 60 * 1e3) : null;
|
|
122
|
+
await db.$executeRaw`
|
|
123
|
+
INSERT INTO shared_contexts (
|
|
124
|
+
id, session_id, project_id, org_id, user_id,
|
|
125
|
+
share_data, expires_at, created_at, access_count
|
|
126
|
+
) VALUES (
|
|
127
|
+
${shareId}, ${sessionId}, ${projectId}, ${orgId}, ${userId || null},
|
|
128
|
+
${JSON.stringify(shareData)}::jsonb, ${expiresAt}, NOW(), 0
|
|
129
|
+
)
|
|
130
|
+
ON CONFLICT (id) DO NOTHING
|
|
131
|
+
`;
|
|
132
|
+
const baseUrl = process.env.BASE_URL || "http://localhost:4000";
|
|
133
|
+
const shareUrl = `${baseUrl}/shared/${shareId}`;
|
|
134
|
+
return {
|
|
135
|
+
id: shareId,
|
|
136
|
+
sessionId,
|
|
137
|
+
projectId,
|
|
138
|
+
userId,
|
|
139
|
+
shareUrl,
|
|
140
|
+
memories: shareData.memories,
|
|
141
|
+
messages: shareData.messages,
|
|
142
|
+
chunks: shareData.chunks,
|
|
143
|
+
metadata,
|
|
144
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
145
|
+
expiresAt,
|
|
146
|
+
accessCount: 0
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async function loadSharedContext(shareId) {
|
|
150
|
+
const result = await db.$queryRaw`
|
|
151
|
+
SELECT * FROM shared_contexts
|
|
152
|
+
WHERE id = ${shareId}
|
|
153
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
154
|
+
`;
|
|
155
|
+
if (result.length === 0) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
const row = result[0];
|
|
159
|
+
await db.$executeRaw`
|
|
160
|
+
UPDATE shared_contexts
|
|
161
|
+
SET access_count = access_count + 1,
|
|
162
|
+
last_accessed_at = NOW()
|
|
163
|
+
WHERE id = ${shareId}
|
|
164
|
+
`;
|
|
165
|
+
const baseUrl = process.env.BASE_URL || "http://localhost:4000";
|
|
166
|
+
return {
|
|
167
|
+
id: row.id,
|
|
168
|
+
sessionId: row.session_id,
|
|
169
|
+
projectId: row.project_id,
|
|
170
|
+
userId: row.user_id,
|
|
171
|
+
shareUrl: `${baseUrl}/shared/${shareId}`,
|
|
172
|
+
memories: row.share_data.memories || [],
|
|
173
|
+
messages: row.share_data.messages || [],
|
|
174
|
+
chunks: row.share_data.chunks || [],
|
|
175
|
+
metadata: row.share_data.metadata || {},
|
|
176
|
+
createdAt: row.created_at,
|
|
177
|
+
expiresAt: row.expires_at,
|
|
178
|
+
accessCount: row.access_count
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
async function resumeFromSharedContext(params) {
|
|
182
|
+
const { shareId, projectId, orgId, userId, newSessionId } = params;
|
|
183
|
+
const sharedContext = await loadSharedContext(shareId);
|
|
184
|
+
if (!sharedContext) {
|
|
185
|
+
throw new Error("Shared context not found or expired");
|
|
186
|
+
}
|
|
187
|
+
const sessionId = newSessionId || nanoid();
|
|
188
|
+
await db.session.create({
|
|
189
|
+
data: {
|
|
190
|
+
id: sessionId,
|
|
191
|
+
projectId,
|
|
192
|
+
orgId,
|
|
193
|
+
userId,
|
|
194
|
+
title: `Resumed from ${sharedContext.id}`,
|
|
195
|
+
metadata: {
|
|
196
|
+
resumedFrom: shareId,
|
|
197
|
+
originalSessionId: sharedContext.sessionId,
|
|
198
|
+
...sharedContext.metadata
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
let memoriesRestored = 0;
|
|
203
|
+
for (const memory of sharedContext.memories) {
|
|
204
|
+
try {
|
|
205
|
+
await db.memory.create({
|
|
206
|
+
data: {
|
|
207
|
+
projectId,
|
|
208
|
+
orgId,
|
|
209
|
+
userId,
|
|
210
|
+
sessionId,
|
|
211
|
+
memoryType: memory.type,
|
|
212
|
+
content: memory.content,
|
|
213
|
+
entityMentions: memory.entities || [],
|
|
214
|
+
confidence: memory.confidence || 0.8,
|
|
215
|
+
documentDate: memory.documentDate ? new Date(memory.documentDate) : null,
|
|
216
|
+
eventDate: memory.eventDate ? new Date(memory.eventDate) : null,
|
|
217
|
+
validFrom: /* @__PURE__ */ new Date(),
|
|
218
|
+
metadata: {
|
|
219
|
+
restoredFrom: shareId
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
memoriesRestored++;
|
|
224
|
+
} catch (error) {
|
|
225
|
+
console.error("Failed to restore memory:", error);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const msg of sharedContext.messages) {
|
|
229
|
+
try {
|
|
230
|
+
await db.message.create({
|
|
231
|
+
data: {
|
|
232
|
+
sessionId,
|
|
233
|
+
role: msg.role,
|
|
234
|
+
content: msg.content,
|
|
235
|
+
metadata: {
|
|
236
|
+
restoredFrom: shareId,
|
|
237
|
+
originalTimestamp: msg.createdAt
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error("Failed to restore message:", error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
sessionId,
|
|
247
|
+
memoriesRestored
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async function cleanupExpiredContexts() {
|
|
251
|
+
const result = await db.$executeRaw`
|
|
252
|
+
DELETE FROM shared_contexts
|
|
253
|
+
WHERE expires_at IS NOT NULL
|
|
254
|
+
AND expires_at < NOW()
|
|
255
|
+
`;
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
async function listSharedContexts(params) {
|
|
259
|
+
const { userId, projectId, orgId, limit = 50 } = params;
|
|
260
|
+
const maxLimit = Math.min(limit, 100);
|
|
261
|
+
const whereClauses = [`org_id = ${orgId}`];
|
|
262
|
+
if (userId) whereClauses.push(`user_id = ${userId}`);
|
|
263
|
+
if (projectId) whereClauses.push(`project_id = ${projectId}`);
|
|
264
|
+
const results = await db.$queryRaw`
|
|
265
|
+
SELECT id, session_id, created_at, expires_at, access_count, share_data
|
|
266
|
+
FROM shared_contexts
|
|
267
|
+
WHERE ${db.Prisma.raw(whereClauses.join(" AND "))}
|
|
268
|
+
ORDER BY created_at DESC
|
|
269
|
+
LIMIT ${maxLimit}
|
|
270
|
+
`;
|
|
271
|
+
const baseUrl = process.env.BASE_URL || "http://localhost:4000";
|
|
272
|
+
return results.map((row) => ({
|
|
273
|
+
id: row.id,
|
|
274
|
+
shareUrl: `${baseUrl}/shared/${row.id}`,
|
|
275
|
+
createdAt: row.created_at,
|
|
276
|
+
expiresAt: row.expires_at,
|
|
277
|
+
accessCount: row.access_count,
|
|
278
|
+
metadata: row.share_data?.metadata || {}
|
|
279
|
+
}));
|
|
280
|
+
}
|
|
281
|
+
var SHARED_CONTEXTS_MIGRATION = `
|
|
282
|
+
CREATE TABLE IF NOT EXISTS shared_contexts (
|
|
283
|
+
id TEXT PRIMARY KEY,
|
|
284
|
+
session_id TEXT NOT NULL,
|
|
285
|
+
project_id TEXT NOT NULL,
|
|
286
|
+
org_id TEXT NOT NULL,
|
|
287
|
+
user_id TEXT,
|
|
288
|
+
share_data JSONB NOT NULL DEFAULT '{}',
|
|
289
|
+
expires_at TIMESTAMPTZ,
|
|
290
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
291
|
+
access_count INTEGER NOT NULL DEFAULT 0,
|
|
292
|
+
last_accessed_at TIMESTAMPTZ
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_org ON shared_contexts(org_id);
|
|
296
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_user ON shared_contexts(user_id);
|
|
297
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_expires ON shared_contexts(expires_at);
|
|
298
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_session ON shared_contexts(session_id);
|
|
299
|
+
`;
|
|
300
|
+
export {
|
|
301
|
+
SHARED_CONTEXTS_MIGRATION,
|
|
302
|
+
cleanupExpiredContexts,
|
|
303
|
+
createSharedContext,
|
|
304
|
+
listSharedContexts,
|
|
305
|
+
loadSharedContext,
|
|
306
|
+
resumeFromSharedContext
|
|
307
|
+
};
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSessionMemories
|
|
3
|
+
} from "./chunk-TWEIYHI6.js";
|
|
4
|
+
import "./chunk-JO3ORBZD.js";
|
|
5
|
+
import "./chunk-5KBZQHDL.js";
|
|
6
|
+
import {
|
|
7
|
+
db
|
|
8
|
+
} from "./chunk-MEFLJ4PV.js";
|
|
9
|
+
import "./chunk-QGM4M3NI.js";
|
|
10
|
+
|
|
11
|
+
// ../node_modules/nanoid/index.js
|
|
12
|
+
import { webcrypto as crypto } from "crypto";
|
|
13
|
+
|
|
14
|
+
// ../node_modules/nanoid/url-alphabet/index.js
|
|
15
|
+
var urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
16
|
+
|
|
17
|
+
// ../node_modules/nanoid/index.js
|
|
18
|
+
var POOL_SIZE_MULTIPLIER = 128;
|
|
19
|
+
var pool;
|
|
20
|
+
var poolOffset;
|
|
21
|
+
function fillPool(bytes) {
|
|
22
|
+
if (!pool || pool.length < bytes) {
|
|
23
|
+
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER);
|
|
24
|
+
crypto.getRandomValues(pool);
|
|
25
|
+
poolOffset = 0;
|
|
26
|
+
} else if (poolOffset + bytes > pool.length) {
|
|
27
|
+
crypto.getRandomValues(pool);
|
|
28
|
+
poolOffset = 0;
|
|
29
|
+
}
|
|
30
|
+
poolOffset += bytes;
|
|
31
|
+
}
|
|
32
|
+
function nanoid(size = 21) {
|
|
33
|
+
fillPool(size |= 0);
|
|
34
|
+
let id = "";
|
|
35
|
+
for (let i = poolOffset - size; i < poolOffset; i++) {
|
|
36
|
+
id += urlAlphabet[pool[i] & 63];
|
|
37
|
+
}
|
|
38
|
+
return id;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// ../src/engine/context-sharing.ts
|
|
42
|
+
async function createSharedContext(params) {
|
|
43
|
+
const {
|
|
44
|
+
sessionId,
|
|
45
|
+
projectId,
|
|
46
|
+
orgId,
|
|
47
|
+
userId,
|
|
48
|
+
includeMemories = true,
|
|
49
|
+
includeMessages = true,
|
|
50
|
+
includeChunks = false,
|
|
51
|
+
expiryDays = 7,
|
|
52
|
+
metadata = {}
|
|
53
|
+
} = params;
|
|
54
|
+
const shareId = nanoid(12);
|
|
55
|
+
const session = await db.session.findUnique({
|
|
56
|
+
where: { id: sessionId }
|
|
57
|
+
});
|
|
58
|
+
if (!session) {
|
|
59
|
+
throw new Error("Session not found");
|
|
60
|
+
}
|
|
61
|
+
const shareData = {
|
|
62
|
+
sessionId,
|
|
63
|
+
projectId,
|
|
64
|
+
userId,
|
|
65
|
+
metadata
|
|
66
|
+
};
|
|
67
|
+
if (includeMemories) {
|
|
68
|
+
const memories = await getSessionMemories({
|
|
69
|
+
sessionId,
|
|
70
|
+
projectId,
|
|
71
|
+
limit: 200
|
|
72
|
+
});
|
|
73
|
+
shareData.memories = memories.map((m) => ({
|
|
74
|
+
id: m.id,
|
|
75
|
+
content: m.content,
|
|
76
|
+
type: m.memoryType,
|
|
77
|
+
entities: m.entityMentions,
|
|
78
|
+
confidence: m.confidence,
|
|
79
|
+
documentDate: m.documentDate?.toISOString(),
|
|
80
|
+
eventDate: m.eventDate?.toISOString()
|
|
81
|
+
}));
|
|
82
|
+
} else {
|
|
83
|
+
shareData.memories = [];
|
|
84
|
+
}
|
|
85
|
+
if (includeMessages) {
|
|
86
|
+
const messages = await db.message.findMany({
|
|
87
|
+
where: { sessionId },
|
|
88
|
+
orderBy: { createdAt: "asc" },
|
|
89
|
+
take: 500
|
|
90
|
+
});
|
|
91
|
+
shareData.messages = messages.map((m) => ({
|
|
92
|
+
role: m.role,
|
|
93
|
+
content: m.content,
|
|
94
|
+
createdAt: m.createdAt.toISOString()
|
|
95
|
+
}));
|
|
96
|
+
} else {
|
|
97
|
+
shareData.messages = [];
|
|
98
|
+
}
|
|
99
|
+
if (includeChunks) {
|
|
100
|
+
const chunkIds = shareData.memories.map((m) => m.sourceChunkId).filter(Boolean).slice(0, 50);
|
|
101
|
+
if (chunkIds.length > 0) {
|
|
102
|
+
const chunks = await db.chunk.findMany({
|
|
103
|
+
where: {
|
|
104
|
+
id: { in: chunkIds }
|
|
105
|
+
},
|
|
106
|
+
select: {
|
|
107
|
+
id: true,
|
|
108
|
+
content: true,
|
|
109
|
+
metadata: true,
|
|
110
|
+
chunkType: true
|
|
111
|
+
},
|
|
112
|
+
take: 50
|
|
113
|
+
});
|
|
114
|
+
shareData.chunks = chunks;
|
|
115
|
+
} else {
|
|
116
|
+
shareData.chunks = [];
|
|
117
|
+
}
|
|
118
|
+
} else {
|
|
119
|
+
shareData.chunks = [];
|
|
120
|
+
}
|
|
121
|
+
const expiresAt = expiryDays ? new Date(Date.now() + expiryDays * 24 * 60 * 60 * 1e3) : null;
|
|
122
|
+
await db.$executeRaw`
|
|
123
|
+
INSERT INTO shared_contexts (
|
|
124
|
+
id, session_id, project_id, org_id, user_id,
|
|
125
|
+
share_data, expires_at, created_at, access_count
|
|
126
|
+
) VALUES (
|
|
127
|
+
${shareId}, ${sessionId}, ${projectId}, ${orgId}, ${userId || null},
|
|
128
|
+
${JSON.stringify(shareData)}::jsonb, ${expiresAt}, NOW(), 0
|
|
129
|
+
)
|
|
130
|
+
ON CONFLICT (id) DO NOTHING
|
|
131
|
+
`;
|
|
132
|
+
const baseUrl = process.env.BASE_URL || "http://localhost:4000";
|
|
133
|
+
const shareUrl = `${baseUrl}/shared/${shareId}`;
|
|
134
|
+
return {
|
|
135
|
+
id: shareId,
|
|
136
|
+
sessionId,
|
|
137
|
+
projectId,
|
|
138
|
+
userId,
|
|
139
|
+
shareUrl,
|
|
140
|
+
memories: shareData.memories,
|
|
141
|
+
messages: shareData.messages,
|
|
142
|
+
chunks: shareData.chunks,
|
|
143
|
+
metadata,
|
|
144
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
145
|
+
expiresAt,
|
|
146
|
+
accessCount: 0
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async function loadSharedContext(shareId) {
|
|
150
|
+
const result = await db.$queryRaw`
|
|
151
|
+
SELECT * FROM shared_contexts
|
|
152
|
+
WHERE id = ${shareId}
|
|
153
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
154
|
+
`;
|
|
155
|
+
if (result.length === 0) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
const row = result[0];
|
|
159
|
+
await db.$executeRaw`
|
|
160
|
+
UPDATE shared_contexts
|
|
161
|
+
SET access_count = access_count + 1,
|
|
162
|
+
last_accessed_at = NOW()
|
|
163
|
+
WHERE id = ${shareId}
|
|
164
|
+
`;
|
|
165
|
+
const baseUrl = process.env.BASE_URL || "http://localhost:4000";
|
|
166
|
+
return {
|
|
167
|
+
id: row.id,
|
|
168
|
+
sessionId: row.session_id,
|
|
169
|
+
projectId: row.project_id,
|
|
170
|
+
userId: row.user_id,
|
|
171
|
+
shareUrl: `${baseUrl}/shared/${shareId}`,
|
|
172
|
+
memories: row.share_data.memories || [],
|
|
173
|
+
messages: row.share_data.messages || [],
|
|
174
|
+
chunks: row.share_data.chunks || [],
|
|
175
|
+
metadata: row.share_data.metadata || {},
|
|
176
|
+
createdAt: row.created_at,
|
|
177
|
+
expiresAt: row.expires_at,
|
|
178
|
+
accessCount: row.access_count
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
async function resumeFromSharedContext(params) {
|
|
182
|
+
const { shareId, projectId, orgId, userId, newSessionId } = params;
|
|
183
|
+
const sharedContext = await loadSharedContext(shareId);
|
|
184
|
+
if (!sharedContext) {
|
|
185
|
+
throw new Error("Shared context not found or expired");
|
|
186
|
+
}
|
|
187
|
+
const sessionId = newSessionId || nanoid();
|
|
188
|
+
await db.session.create({
|
|
189
|
+
data: {
|
|
190
|
+
id: sessionId,
|
|
191
|
+
projectId,
|
|
192
|
+
orgId,
|
|
193
|
+
userId,
|
|
194
|
+
title: `Resumed from ${sharedContext.id}`,
|
|
195
|
+
metadata: {
|
|
196
|
+
resumedFrom: shareId,
|
|
197
|
+
originalSessionId: sharedContext.sessionId,
|
|
198
|
+
...sharedContext.metadata
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
let memoriesRestored = 0;
|
|
203
|
+
for (const memory of sharedContext.memories) {
|
|
204
|
+
try {
|
|
205
|
+
await db.memory.create({
|
|
206
|
+
data: {
|
|
207
|
+
projectId,
|
|
208
|
+
orgId,
|
|
209
|
+
userId,
|
|
210
|
+
sessionId,
|
|
211
|
+
memoryType: memory.type,
|
|
212
|
+
content: memory.content,
|
|
213
|
+
entityMentions: memory.entities || [],
|
|
214
|
+
confidence: memory.confidence || 0.8,
|
|
215
|
+
documentDate: memory.documentDate ? new Date(memory.documentDate) : null,
|
|
216
|
+
eventDate: memory.eventDate ? new Date(memory.eventDate) : null,
|
|
217
|
+
validFrom: /* @__PURE__ */ new Date(),
|
|
218
|
+
metadata: {
|
|
219
|
+
restoredFrom: shareId
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
memoriesRestored++;
|
|
224
|
+
} catch (error) {
|
|
225
|
+
console.error("Failed to restore memory:", error);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const msg of sharedContext.messages) {
|
|
229
|
+
try {
|
|
230
|
+
await db.message.create({
|
|
231
|
+
data: {
|
|
232
|
+
sessionId,
|
|
233
|
+
role: msg.role,
|
|
234
|
+
content: msg.content,
|
|
235
|
+
metadata: {
|
|
236
|
+
restoredFrom: shareId,
|
|
237
|
+
originalTimestamp: msg.createdAt
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.error("Failed to restore message:", error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
sessionId,
|
|
247
|
+
memoriesRestored
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async function cleanupExpiredContexts() {
|
|
251
|
+
const result = await db.$executeRaw`
|
|
252
|
+
DELETE FROM shared_contexts
|
|
253
|
+
WHERE expires_at IS NOT NULL
|
|
254
|
+
AND expires_at < NOW()
|
|
255
|
+
`;
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
async function listSharedContexts(params) {
|
|
259
|
+
const { userId, projectId, orgId, limit = 50 } = params;
|
|
260
|
+
const maxLimit = Math.min(limit, 100);
|
|
261
|
+
const whereClauses = [`org_id = ${orgId}`];
|
|
262
|
+
if (userId) whereClauses.push(`user_id = ${userId}`);
|
|
263
|
+
if (projectId) whereClauses.push(`project_id = ${projectId}`);
|
|
264
|
+
const results = await db.$queryRaw`
|
|
265
|
+
SELECT id, session_id, created_at, expires_at, access_count, share_data
|
|
266
|
+
FROM shared_contexts
|
|
267
|
+
WHERE ${db.Prisma.raw(whereClauses.join(" AND "))}
|
|
268
|
+
ORDER BY created_at DESC
|
|
269
|
+
LIMIT ${maxLimit}
|
|
270
|
+
`;
|
|
271
|
+
const baseUrl = process.env.BASE_URL || "http://localhost:4000";
|
|
272
|
+
return results.map((row) => ({
|
|
273
|
+
id: row.id,
|
|
274
|
+
shareUrl: `${baseUrl}/shared/${row.id}`,
|
|
275
|
+
createdAt: row.created_at,
|
|
276
|
+
expiresAt: row.expires_at,
|
|
277
|
+
accessCount: row.access_count,
|
|
278
|
+
metadata: row.share_data?.metadata || {}
|
|
279
|
+
}));
|
|
280
|
+
}
|
|
281
|
+
var SHARED_CONTEXTS_MIGRATION = `
|
|
282
|
+
CREATE TABLE IF NOT EXISTS shared_contexts (
|
|
283
|
+
id TEXT PRIMARY KEY,
|
|
284
|
+
session_id TEXT NOT NULL,
|
|
285
|
+
project_id TEXT NOT NULL,
|
|
286
|
+
org_id TEXT NOT NULL,
|
|
287
|
+
user_id TEXT,
|
|
288
|
+
share_data JSONB NOT NULL DEFAULT '{}',
|
|
289
|
+
expires_at TIMESTAMPTZ,
|
|
290
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
291
|
+
access_count INTEGER NOT NULL DEFAULT 0,
|
|
292
|
+
last_accessed_at TIMESTAMPTZ
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_org ON shared_contexts(org_id);
|
|
296
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_user ON shared_contexts(user_id);
|
|
297
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_expires ON shared_contexts(expires_at);
|
|
298
|
+
CREATE INDEX IF NOT EXISTS idx_shared_contexts_session ON shared_contexts(session_id);
|
|
299
|
+
`;
|
|
300
|
+
export {
|
|
301
|
+
SHARED_CONTEXTS_MIGRATION,
|
|
302
|
+
cleanupExpiredContexts,
|
|
303
|
+
createSharedContext,
|
|
304
|
+
listSharedContexts,
|
|
305
|
+
loadSharedContext,
|
|
306
|
+
resumeFromSharedContext
|
|
307
|
+
};
|