feat(*): add RAG support
This commit is contained in:
102
frontend/src/lib/convex/ragConnections.ts
Normal file
102
frontend/src/lib/convex/ragConnections.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { v } from 'convex/values';
|
||||
import { mutation, query } from './_generated/server';
|
||||
|
||||
export const connect = mutation({
|
||||
args: {
|
||||
userId: v.id('users'),
|
||||
ragDatabaseId: v.id('ragDatabases'),
|
||||
isGlobal: v.optional(v.boolean())
|
||||
},
|
||||
returns: v.id('ragConnections'),
|
||||
handler: async (ctx, args) => {
|
||||
const existing = await ctx.db
|
||||
.query('ragConnections')
|
||||
.withIndex('by_user_id_and_rag_database_id', (q) =>
|
||||
q.eq('userId', args.userId).eq('ragDatabaseId', args.ragDatabaseId)
|
||||
)
|
||||
.unique();
|
||||
|
||||
if (existing) {
|
||||
return existing._id;
|
||||
}
|
||||
|
||||
return await ctx.db.insert('ragConnections', {
|
||||
userId: args.userId,
|
||||
ragDatabaseId: args.ragDatabaseId,
|
||||
isGlobal: args.isGlobal ?? true,
|
||||
createdAt: Date.now()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const disconnect = mutation({
|
||||
args: {
|
||||
userId: v.id('users'),
|
||||
ragDatabaseId: v.id('ragDatabases')
|
||||
},
|
||||
returns: v.boolean(),
|
||||
handler: async (ctx, args) => {
|
||||
const existing = await ctx.db
|
||||
.query('ragConnections')
|
||||
.withIndex('by_user_id_and_rag_database_id', (q) =>
|
||||
q.eq('userId', args.userId).eq('ragDatabaseId', args.ragDatabaseId)
|
||||
)
|
||||
.unique();
|
||||
|
||||
if (!existing) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await ctx.db.delete(existing._id);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
export const getActiveForUser = query({
|
||||
args: { userId: v.id('users') },
|
||||
returns: v.array(
|
||||
v.object({
|
||||
_id: v.id('ragConnections'),
|
||||
_creationTime: v.number(),
|
||||
userId: v.id('users'),
|
||||
ragDatabaseId: v.id('ragDatabases'),
|
||||
isGlobal: v.boolean(),
|
||||
createdAt: v.number()
|
||||
})
|
||||
),
|
||||
handler: async (ctx, args) => {
|
||||
return await ctx.db
|
||||
.query('ragConnections')
|
||||
.withIndex('by_user_id', (q) => q.eq('userId', args.userId))
|
||||
.collect();
|
||||
}
|
||||
});
|
||||
|
||||
export const getByRagDatabaseId = query({
|
||||
args: { ragDatabaseId: v.id('ragDatabases') },
|
||||
returns: v.array(
|
||||
v.object({
|
||||
_id: v.id('ragConnections'),
|
||||
_creationTime: v.number(),
|
||||
userId: v.id('users'),
|
||||
ragDatabaseId: v.id('ragDatabases'),
|
||||
isGlobal: v.boolean(),
|
||||
createdAt: v.number()
|
||||
})
|
||||
),
|
||||
handler: async (ctx, args) => {
|
||||
return await ctx.db
|
||||
.query('ragConnections')
|
||||
.withIndex('by_rag_database_id', (q) => q.eq('ragDatabaseId', args.ragDatabaseId))
|
||||
.collect();
|
||||
}
|
||||
});
|
||||
|
||||
export const deleteConnection = mutation({
|
||||
args: { connectionId: v.id('ragConnections') },
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
await ctx.db.delete(args.connectionId);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user