diff --git a/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.md b/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.md index 1f1840ff6..49f5c10e7 100644 --- a/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.md +++ b/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.md @@ -42,7 +42,7 @@ Generated by [AVA](https://avajs.dev). [ { - blobId: 'fileId1', + blobId: 'Ip3vuwzubwJnOlzeKQ0Gc-daDcMc7EOYnIqypOyn4bs', chunkSize: 0, name: 'sample.pdf', status: 'processing', diff --git a/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.snap b/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.snap index a668f573a..9239165a9 100644 Binary files a/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.snap and b/packages/backend/server/src/__tests__/__snapshots__/copilot.e2e.ts.snap differ diff --git a/packages/backend/server/src/plugins/copilot/context/resolver.ts b/packages/backend/server/src/plugins/copilot/context/resolver.ts index fa15f8e06..0585d10a3 100644 --- a/packages/backend/server/src/plugins/copilot/context/resolver.ts +++ b/packages/backend/server/src/plugins/copilot/context/resolver.ts @@ -1,3 +1,5 @@ +import { createHash } from 'node:crypto'; + import { Args, Context, @@ -102,8 +104,9 @@ class AddContextFileInput { @Field(() => String) contextId!: string; - @Field(() => String) - blobId!: string; + // @TODO(@darkskygit): remove this after client lower then 0.22 has been disconnected + @Field(() => String, { nullable: true, deprecationReason: 'Never used' }) + blobId!: string | undefined; } @InputType() @@ -611,8 +614,9 @@ export class CopilotContextResolver { if (!this.context.canEmbedding) { throw new CopilotEmbeddingUnavailable(); } + const { contextId } = options; - const lockFlag = `${COPILOT_LOCKER}:context:${options.contextId}`; + const lockFlag = `${COPILOT_LOCKER}:context:${contextId}`; await using lock = await this.mutex.acquire(lockFlag); if (!lock) { throw new TooManyRequest('Server is busy'); @@ -623,22 +627,15 @@ export class CopilotContextResolver { throw new BlobQuotaExceeded(); } - const session = await this.context.get(options.contextId); + const session = await this.context.get(contextId); try { - const file = await session.addFile( - options.blobId, - content.filename, - content.mimetype - ); - const buffer = await readStream(content.createReadStream()); - await this.storage.put( - user.id, - session.workspaceId, - options.blobId, - buffer - ); + const blobId = createHash('sha256').update(buffer).digest('base64url'); + const { filename, mimetype } = content; + + await this.storage.put(user.id, session.workspaceId, blobId, buffer); + const file = await session.addFile(blobId, filename, mimetype); await this.jobs.addFileEmbeddingQueue({ userId: user.id, @@ -655,10 +652,7 @@ export class CopilotContextResolver { if (e instanceof UserFriendlyError) { throw e; } - throw new CopilotFailedToModifyContext({ - contextId: options.contextId, - message: e.message, - }); + throw new CopilotFailedToModifyContext({ contextId, message: e.message }); } } diff --git a/packages/backend/server/src/schema.gql b/packages/backend/server/src/schema.gql index 36f0a5cc5..1e3258a3e 100644 --- a/packages/backend/server/src/schema.gql +++ b/packages/backend/server/src/schema.gql @@ -15,7 +15,7 @@ input AddContextDocInput { } input AddContextFileInput { - blobId: String! + blobId: String contextId: String! } diff --git a/packages/common/graphql/src/schema.ts b/packages/common/graphql/src/schema.ts index bec90e146..218b14ed2 100644 --- a/packages/common/graphql/src/schema.ts +++ b/packages/common/graphql/src/schema.ts @@ -50,7 +50,7 @@ export interface AddContextDocInput { } export interface AddContextFileInput { - blobId: Scalars['String']['input']; + blobId?: InputMaybe; contextId: Scalars['String']['input']; }