From 2ae3c3e2cdf97facb75bf359aa5bd51a17923735 Mon Sep 17 00:00:00 2001 From: DarkSky <25152247+darkskygit@users.noreply.github.com> Date: Thu, 3 Jul 2025 11:45:34 +0800 Subject: [PATCH] feat(server): deprecate blob id provide by client (#12991) fix AI-287 --- .../__tests__/__snapshots__/copilot.e2e.ts.md | 2 +- .../__snapshots__/copilot.e2e.ts.snap | Bin 1127 -> 1167 bytes .../src/plugins/copilot/context/resolver.ts | 34 ++++++++---------- packages/backend/server/src/schema.gql | 2 +- packages/common/graphql/src/schema.ts | 2 +- 5 files changed, 17 insertions(+), 23 deletions(-) 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 a668f573a699d225d4f279147f21570967b177b9..9239165a9356f4027e684914fe98809337c5d236 100644 GIT binary patch literal 1167 zcmV;A1aSL7RzVUIUnYIVp*g1?dlDaWC`@n@hI{`61 zQ{!dk86S%X00000000B+n9pw;MHI)sZ+|#Wqc*7u5=c}Ui5~}WQ=5h&MHNyJ5eXFv z2nkUThw<(>-gtLryE9JWaDxcM4^auh1tCFufdk?Y4xA7d#2IaS&+9W8G9nzkb#Cx#1V-EgNI#eyB`V1;C>KPEhGMx#yFFcZl-Q&wD3Wc7wqQ&XU$ARBay=m!9Ib;s^;Dn~+Vz9ckg5+}&Y zub1LLO5vwdYpQt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)WQ=oS@oq^3Q6`2SjCSZf*{A1Vo?) z(V5c7>jAg`;9US;md3Sb+N&b45V!Z-7hnD1waDe{mHDVspdpOPPI@4YQI6$ z41fWEuK`>IFiXIhs+5ceq9=I39{_uTtB0nYlG+~LJqr5zV84H2YA#0o^^mVXLqHplmC?i zY&yWt4)B))oOCND{zP?hg&dY#z1du9HkVF4F@`o3yeD+or?XXz#&T^d(>&g+j2h)u zUY%bSajX`YSQC1nq*$EJ&DK(@I{%oPPfg?Isnb)rxv(59Fx%y_#|Em;I!d!u6=fC4 zY*(;E2%EQ6$Onr>kv>oqX`ZmV*XaL3XZ%+Vb>^n(jQ<`19}w^<0bdZXIZ1E4obUHT zz4^V+n+DPS4oW?GK5M%96W(@!OAhdj16*~0zbC6u(*;huz)LRhnhRW>ph9)>|8#+C zE^yKVp7emwtC-ZrOg{F2&pqG=5BSvs8h*uO=1}Wyh1N|KLhE2iw{9}&*1?c&-3D)! zVz+y)9IQpAH(}K22rZ;#CfhYD$?PB%hH)9PH6BM{HYn99N%PIKv6@HHn(b}z8^!DD z&0F@m8aXW&NS+>zvWzYNqBDByqS8H+@}M#(*Ws=^XX-j6|ke)icuH({q|ynGCy^-A;@s3ELKWN)!@rqzmvurE006SvC*S}8 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']; }