feat(server): adapt context model (#11028)

expose more field in listContextObject
This commit is contained in:
darkskygit
2025-03-21 05:36:45 +00:00
parent a5b975ac46
commit 5acba9d5a0
25 changed files with 537 additions and 377 deletions
@@ -34,6 +34,13 @@ export enum ContextCategories {
export const ContextDocSchema = z.object({
id: z.string(),
createdAt: z.number(),
status: z
.enum([
ContextEmbedStatus.processing,
ContextEmbedStatus.finished,
ContextEmbedStatus.failed,
])
.nullable(),
});
export const ContextFileSchema = z.object({
@@ -6,7 +6,6 @@ import { Prisma } from '@prisma/client';
import { CopilotSessionNotFound } from '../base';
import { BaseModel } from './base';
import {
ChunkSimilarity,
ContextConfigSchema,
ContextDoc,
ContextEmbedStatus,
@@ -24,7 +23,7 @@ type UpdateCopilotContextInput = Pick<CopilotContext, 'config'>;
*/
@Injectable()
export class CopilotContextModel extends BaseModel {
// contexts
// ================ contexts ================
async create(sessionId: string) {
const session = await this.db.aiSession.findFirst({
@@ -113,7 +112,7 @@ export class CopilotContextModel extends BaseModel {
return ret.count > 0;
}
// embeddings
// ================ embeddings ================
async checkEmbeddingAvailable(): Promise<boolean> {
const [{ count }] = await this.db.$queryRaw<
@@ -157,7 +156,7 @@ export class CopilotContextModel extends BaseModel {
return Prisma.join(groups.map(row => Prisma.sql`(${Prisma.join(row)})`));
}
async insertEmbedding(
async insertContentEmbedding(
contextId: string,
fileId: string,
embeddings: Embedding[]
@@ -172,12 +171,12 @@ export class CopilotContextModel extends BaseModel {
`;
}
async matchEmbedding(
async matchContentEmbedding(
embedding: number[],
contextId: string,
topK: number,
threshold: number
): Promise<ChunkSimilarity[]> {
): Promise<FileChunkSimilarity[]> {
const similarityChunks = await this.db.$queryRaw<
Array<FileChunkSimilarity>
>`
@@ -214,7 +213,7 @@ export class CopilotContextModel extends BaseModel {
workspaceId: string,
topK: number,
threshold: number
): Promise<ChunkSimilarity[]> {
): Promise<DocChunkSimilarity[]> {
const similarityChunks = await this.db.$queryRaw<Array<DocChunkSimilarity>>`
SELECT "doc_id" as "docId", "chunk", "content", "embedding" <=> ${embedding}::vector as "distance"
FROM "ai_workspace_embeddings"
+17
View File
@@ -185,6 +185,23 @@ export class DocModel extends BaseModel {
});
}
/**
* Check if all doc exists in the workspace.
* Ignore pending updates.
*/
async existsAll(workspaceId: string, docIds: string[]) {
const count = await this.db.snapshot.count({
where: {
workspaceId,
id: { in: docIds },
},
});
if (count === docIds.length) {
return true;
}
return false;
}
/**
* Detect a doc exists or not, including updates
*/