feat(server): integrate blob to context (#13491)
Some checks failed
Build & Test / Analyze (javascript, affine) (push) Has been cancelled
Build & Test / Analyze (javascript, blocksuite) (push) Has been cancelled
Build & Test / Analyze (typescript, affine) (push) Has been cancelled
Build & Test / Analyze (typescript, blocksuite) (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Lint Rust (push) Has been cancelled
Build & Test / Check yarn binary (push) Has been cancelled
Build & Test / E2E BlockSuite Test (1) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (10) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (2) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (3) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (4) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (5) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (6) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (7) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (8) (push) Has been cancelled
Build & Test / E2E BlockSuite Test (9) (push) Has been cancelled
Build & Test / E2E BlockSuite Cross Browser Test (chromium, 1) (push) Has been cancelled
Build & Test / E2E BlockSuite Cross Browser Test (chromium, 2) (push) Has been cancelled
Build & Test / E2E BlockSuite Cross Browser Test (firefox, 1) (push) Has been cancelled
Build & Test / E2E BlockSuite Cross Browser Test (firefox, 2) (push) Has been cancelled
Build & Test / E2E BlockSuite Cross Browser Test (webkit, 1) (push) Has been cancelled
Build & Test / E2E BlockSuite Cross Browser Test (webkit, 2) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (10) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / E2E Test (6) (push) Has been cancelled
Build & Test / E2E Test (7) (push) Has been cancelled
Build & Test / E2E Test (8) (push) Has been cancelled
Build & Test / E2E Test (9) (push) Has been cancelled
Build & Test / E2E Mobile Test (1) (push) Has been cancelled
Build & Test / E2E Mobile Test (2) (push) Has been cancelled
Build & Test / E2E Mobile Test (3) (push) Has been cancelled
Build & Test / E2E Mobile Test (4) (push) Has been cancelled
Build & Test / E2E Mobile Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (${{ matrix.spec.target }}) (map[os:macos-latest target:aarch64-apple-darwin]) (push) Has been cancelled
Build & Test / Build AFFiNE native (${{ matrix.spec.target }}) (map[os:macos-latest target:x86_64-apple-darwin]) (push) Has been cancelled
Build & Test / Build AFFiNE native (${{ matrix.spec.target }}) (map[os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Has been cancelled
Build & Test / Build AFFiNE native (${{ matrix.spec.target }}) (map[os:windows-latest target:aarch64-pc-windows-msvc]) (push) Has been cancelled
Build & Test / Build AFFiNE native (${{ matrix.spec.target }}) (map[os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
Build & Test / Build Server native (push) Has been cancelled
Build & Test / Build @affine/electron renderer (push) Has been cancelled
Build & Test / miri code check (push) Has been cancelled
Build & Test / loom thread test (push) Has been cancelled
Build & Test / fuzzing (push) Has been cancelled
Build & Test / y-octo binding test on ${{ matrix.settings.target }} (map[os:macos-13 target:x86_64-apple-darwin]) (push) Has been cancelled
Build & Test / y-octo binding test on ${{ matrix.settings.target }} (map[os:macos-latest target:aarch64-apple-darwin]) (push) Has been cancelled
Build & Test / y-octo binding test on ${{ matrix.settings.target }} (map[os:ubuntu-24.04-arm target:aarch64-unknown-linux-gnu]) (push) Has been cancelled
Build & Test / y-octo binding test on ${{ matrix.settings.target }} (map[os:ubuntu-latest target:x86_64-unknown-linux-gnu]) (push) Has been cancelled
Build & Test / y-octo binding test on ${{ matrix.settings.target }} (map[os:windows-11-arm target:aarch64-pc-windows-msvc]) (push) Has been cancelled
Build & Test / y-octo binding test on ${{ matrix.settings.target }} (map[os:windows-latest target:x86_64-pc-windows-msvc]) (push) Has been cancelled
Build & Test / Run native tests (push) Has been cancelled
Build & Test / Check Git Status (push) Has been cancelled
Build & Test / Unit Test (1) (push) Has been cancelled
Build & Test / Unit Test (2) (push) Has been cancelled
Build & Test / Unit Test (3) (push) Has been cancelled
Build & Test / Unit Test (4) (push) Has been cancelled
Build & Test / Unit Test (5) (push) Has been cancelled
Build & Test / Native Unit Test (push) Has been cancelled
Build & Test / Server Test (0, 8) (push) Has been cancelled
Build & Test / Server Test (1, 8) (push) Has been cancelled
Build & Test / Server Test (2, 8) (push) Has been cancelled
Build & Test / Server Test (3, 8) (push) Has been cancelled
Build & Test / Server Test (4, 8) (push) Has been cancelled
Build & Test / Server Test (5, 8) (push) Has been cancelled
Build & Test / Server Test (6, 8) (push) Has been cancelled
Build & Test / Server Test (7, 8) (push) Has been cancelled
Build & Test / Server Test with Elasticsearch (push) Has been cancelled
Build & Test / Server E2E Test (push) Has been cancelled
Build & Test / Server Copilot Api Test (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (1, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (10, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (2, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (3, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (4, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (5, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (6, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (7, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (8, 10) (push) Has been cancelled
Build & Test / Frontend Copilot E2E Test (9, 10) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud Desktop E2E Test script:yarn affine @affine/electron build:dev # Workaround for Electron apps failing to initialize on Ubuntu 24.04 due to AppArmor restrictions # Disables unprivileged user namespaces restrictio… (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 1/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=1/10 shard:1]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 10/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=10/10 shard:10]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 2/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=2/10 shard:2]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 3/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=3/10 shard:3]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 4/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=4/10 shard:4]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 5/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=5/10 shard:5]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 6/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=6/10 shard:6]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 7/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=7/10 shard:7]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 8/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=8/10 shard:8]) (push) Has been cancelled
Build & Test / ${{ matrix.tests.name }} (map[name:Cloud E2E Test 9/10 script:yarn affine @affine-test/affine-cloud e2e --forbid-only --shard=9/10 shard:9]) (push) Has been cancelled
Build & Test / Desktop Test (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:arm64 os:macos-latest platform:macos target:aarch64-apple-darwin test:true]) (push) Has been cancelled
Build & Test / Desktop Test (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:x64 os:macos-latest platform:macos target:x86_64-apple-darwin test:false]) (push) Has been cancelled
Build & Test / Desktop Test (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:x64 os:ubuntu-latest platform:linux target:x86_64-unknown-linux-gnu test:true]) (push) Has been cancelled
Build & Test / Desktop Test (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:x64 os:windows-latest platform:windows target:x86_64-pc-windows-msvc test:true]) (push) Has been cancelled
Build & Test / Desktop bundle check (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:arm64 os:macos-latest platform:macos target:aarch64-apple-darwin test:true]) (push) Has been cancelled
Build & Test / Desktop bundle check (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:x64 os:macos-latest platform:macos target:x86_64-apple-darwin test:false]) (push) Has been cancelled
Build & Test / Desktop bundle check (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:x64 os:ubuntu-latest platform:linux target:x86_64-unknown-linux-gnu test:true]) (push) Has been cancelled
Build & Test / Desktop bundle check (${{ matrix.spec.os }}, ${{ matrix.spec.platform }}, ${{ matrix.spec.arch }}, ${{ matrix.spec.target }}, ${{ matrix.spec.test }}) (map[arch:x64 os:windows-latest platform:windows target:x86_64-pc-windows-msvc test:true]) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled

This commit is contained in:
DarkSky 2025-08-15 17:35:45 +08:00 committed by GitHub
parent 795bfb2f95
commit e2156ea135
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 85 additions and 12 deletions

View File

@ -67,12 +67,17 @@ export class BlobModel extends BaseModel {
});
}
async list(workspaceId: string) {
async list(
workspaceId: string,
options?: { where: Prisma.BlobWhereInput; select?: Prisma.BlobSelect }
) {
return await this.db.blob.findMany({
where: {
...options?.where,
workspaceId,
deletedAt: null,
},
select: options?.select,
});
}

View File

@ -6,13 +6,14 @@ import { Prisma, PrismaClient } from '@prisma/client';
import { PaginationInput } from '../base';
import { BaseModel } from './base';
import type {
BlobChunkSimilarity,
CopilotWorkspaceFile,
CopilotWorkspaceFileMetadata,
Embedding,
FileChunkSimilarity,
IgnoredDoc,
import {
type BlobChunkSimilarity,
clearEmbeddingContent,
type CopilotWorkspaceFile,
type CopilotWorkspaceFileMetadata,
type Embedding,
type FileChunkSimilarity,
type IgnoredDoc,
} from './common';
@Injectable()
@ -413,6 +414,33 @@ export class CopilotWorkspaceConfigModel extends BaseModel {
return similarityChunks.filter(c => Number(c.distance) <= threshold);
}
async getBlobContent(
workspaceId: string,
blobId: string,
chunk?: number
): Promise<string | undefined> {
const blob = await this.db.aiWorkspaceBlobEmbedding.findMany({
where: { workspaceId, blobId, chunk },
select: { content: true },
orderBy: { chunk: 'asc' },
});
return blob?.map(f => clearEmbeddingContent(f.content)).join('\n');
}
async getBlobChunkSizes(workspaceId: string, blobIds: string[]) {
const sizes = await this.db.aiWorkspaceBlobEmbedding.groupBy({
by: ['blobId'],
_count: { chunk: true },
where: { workspaceId, blobId: { in: blobIds } },
});
return sizes.reduce((acc, cur) => {
if (cur._count.chunk) {
acc.set(cur.blobId, cur._count.chunk);
}
return acc;
}, new Map<string, number>());
}
@Transactional()
async insertBlobEmbeddings(
workspaceId: string,

View File

@ -55,7 +55,7 @@ export class ContextSession implements AsyncDisposable {
return this.config.docs.map(d => ({ ...d }));
}
get files() {
get files(): Required<ContextFile>[] {
return this.config.files.map(f => this.fulfillFile(f));
}
@ -135,6 +135,36 @@ export class ContextSession implements AsyncDisposable {
return record;
}
async getBlobMetadata() {
const blobIds = this.blobs.map(b => b.id);
const blobs = await this.models.blob.list(this.config.workspaceId, {
where: { key: { in: blobIds } },
select: { key: true, mime: true },
});
const blobChunkSizes = await this.models.copilotWorkspace.getBlobChunkSizes(
this.config.workspaceId,
blobIds
);
return blobs
.filter(b => !!blobChunkSizes.get(b.key))
.map(b => ({
id: b.key,
mimeType: b.mime,
chunkSize: blobChunkSizes.get(b.key),
}));
}
async getBlobContent(
blobId: string,
chunk?: number
): Promise<string | undefined> {
return this.models.copilotWorkspace.getBlobContent(
this.config.workspaceId,
blobId,
chunk
);
}
async removeBlobRecord(blobId: string): Promise<boolean> {
const index = this.config.blobs.findIndex(b => b.id === blobId);
if (index >= 0) {

View File

@ -208,8 +208,14 @@ export class CopilotController implements BeforeApplicationShutdown {
const context = await this.context.getBySessionId(sessionId);
const contextParams =
Array.isArray(context?.files) && context.files.length > 0
? { contextFiles: context.files }
(Array.isArray(context?.files) && context.files.length > 0) ||
(Array.isArray(context?.blobs) && context.blobs.length > 0)
? {
contextFiles: [
...context.files,
...(await context.getBlobMetadata()),
],
}
: {};
const lastParams = latestMessage
? {

View File

@ -33,7 +33,11 @@ export const buildBlobContentGetter = (
return;
}
const content = await context?.getFileContent(blobId, chunk);
const [file, blob] = await Promise.all([
context?.getFileContent(blobId, chunk),
context?.getBlobContent(blobId, chunk),
]);
const content = file?.trim() || blob?.trim();
if (!content) {
return;
}