mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-21 20:16:26 +08:00
chore: cleanup legacy logic (#15072)
This commit is contained in:
@@ -2,7 +2,13 @@ import { createHash, createHmac, randomUUID } from 'node:crypto';
|
||||
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
|
||||
import { BadRequest, Cache, CryptoHelper, metrics } from '../../../base';
|
||||
import {
|
||||
BadRequest,
|
||||
Cache,
|
||||
CryptoHelper,
|
||||
metrics,
|
||||
safeFetch,
|
||||
} from '../../../base';
|
||||
import { Models } from '../../../models';
|
||||
import type { CopilotProviderProfile } from '../config';
|
||||
import { ByokEntitlementPolicy } from './policy';
|
||||
@@ -103,6 +109,8 @@ type ByokProfileMeta = {
|
||||
|
||||
@Injectable()
|
||||
export class ByokService {
|
||||
private readonly probeFetch = safeFetch;
|
||||
|
||||
constructor(
|
||||
private readonly models: Models,
|
||||
private readonly crypto: CryptoHelper,
|
||||
@@ -755,22 +763,24 @@ export class ByokService {
|
||||
apiKey: string,
|
||||
endpoint: string | null
|
||||
) {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), TEST_TIMEOUT_MS);
|
||||
try {
|
||||
const request = this.buildProbeRequest(provider, apiKey, endpoint);
|
||||
const response = await fetch(request.url, {
|
||||
const request = this.buildProbeRequest(provider, apiKey, endpoint);
|
||||
const response = await this.probeFetch(
|
||||
request.url,
|
||||
{
|
||||
method: request.method,
|
||||
headers: request.headers as unknown as Record<string, string>,
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new BadRequestException(
|
||||
this.providerProbeFailureMessage(response.status)
|
||||
);
|
||||
},
|
||||
{
|
||||
timeoutMs: TEST_TIMEOUT_MS,
|
||||
maxRedirects: 3,
|
||||
maxBytes: 1024,
|
||||
allowedHeaders: Object.keys(request.headers),
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw new BadRequestException(
|
||||
this.providerProbeFailureMessage(response.status)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,12 +66,12 @@ export class CopilotEmbeddingRealtimeProvider implements OnModuleInit {
|
||||
|
||||
@OnEvent('workspace.file.embed.finished', { suppressError: true })
|
||||
async onFileEmbedFinished(payload: Events['workspace.file.embed.finished']) {
|
||||
await this.publishContext(payload.contextId, 'finished');
|
||||
await this.publishEmbeddingProgress(payload, 'finished');
|
||||
}
|
||||
|
||||
@OnEvent('workspace.file.embed.failed', { suppressError: true })
|
||||
async onFileEmbedFailed(payload: Events['workspace.file.embed.failed']) {
|
||||
await this.publishContext(payload.contextId, 'failed');
|
||||
await this.publishEmbeddingProgress(payload, 'failed');
|
||||
}
|
||||
|
||||
@OnEvent('workspace.blob.embed.finished', { suppressError: true })
|
||||
@@ -90,11 +90,29 @@ export class CopilotEmbeddingRealtimeProvider implements OnModuleInit {
|
||||
) {
|
||||
if (!this.publisher) return;
|
||||
const context = await this.context.get(contextId);
|
||||
this.publishWorkspace(context.workspaceId, reason);
|
||||
}
|
||||
|
||||
private async publishEmbeddingProgress(
|
||||
payload:
|
||||
| Events['workspace.file.embed.finished']
|
||||
| Events['workspace.file.embed.failed'],
|
||||
reason: 'finished' | 'failed'
|
||||
) {
|
||||
if (!this.publisher) return;
|
||||
if (payload.contextId) {
|
||||
await this.publishContext(payload.contextId, reason);
|
||||
return;
|
||||
}
|
||||
this.publishWorkspace(payload.workspaceId, reason);
|
||||
}
|
||||
|
||||
private publishWorkspace(workspaceId: string, reason: 'finished' | 'failed') {
|
||||
this.publisher.publish(
|
||||
'workspace.embedding.progress.changed',
|
||||
{ workspaceId: context.workspaceId },
|
||||
{ workspaceId },
|
||||
{ reason },
|
||||
{ room: workspaceEmbeddingRoom(context.workspaceId) }
|
||||
{ room: workspaceEmbeddingRoom(workspaceId) }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -354,6 +354,7 @@ export class CopilotContextService implements OnApplicationBootstrap {
|
||||
fileId,
|
||||
chunkSize,
|
||||
}: Events['workspace.file.embed.finished']) {
|
||||
if (!contextId) return;
|
||||
const context = await this.get(contextId);
|
||||
await context.saveFileRecord(fileId, file => ({
|
||||
...(file as ContextFile),
|
||||
@@ -368,6 +369,7 @@ export class CopilotContextService implements OnApplicationBootstrap {
|
||||
fileId,
|
||||
error,
|
||||
}: Events['workspace.file.embed.failed']) {
|
||||
if (!contextId) return;
|
||||
const context = await this.get(contextId);
|
||||
await context.saveFileRecord(fileId, file => ({
|
||||
...(file as ContextFile),
|
||||
|
||||
@@ -300,21 +300,19 @@ export class CopilotEmbeddingJob {
|
||||
}
|
||||
}
|
||||
|
||||
if (contextId) {
|
||||
this.event.emit('workspace.file.embed.finished', {
|
||||
contextId,
|
||||
fileId,
|
||||
chunkSize: total,
|
||||
});
|
||||
}
|
||||
this.event.emit('workspace.file.embed.finished', {
|
||||
contextId,
|
||||
workspaceId,
|
||||
fileId,
|
||||
chunkSize: total,
|
||||
});
|
||||
} catch (error: any) {
|
||||
if (contextId) {
|
||||
this.event.emit('workspace.file.embed.failed', {
|
||||
contextId,
|
||||
fileId,
|
||||
error: mapAnyError(error).message,
|
||||
});
|
||||
}
|
||||
this.event.emit('workspace.file.embed.failed', {
|
||||
contextId,
|
||||
workspaceId,
|
||||
fileId,
|
||||
error: mapAnyError(error).message,
|
||||
});
|
||||
|
||||
// passthrough error to job queue
|
||||
throw error;
|
||||
|
||||
@@ -43,13 +43,15 @@ declare global {
|
||||
};
|
||||
|
||||
'workspace.file.embed.finished': {
|
||||
contextId: string;
|
||||
contextId?: string;
|
||||
workspaceId: string;
|
||||
fileId: string;
|
||||
chunkSize: number;
|
||||
};
|
||||
|
||||
'workspace.file.embed.failed': {
|
||||
contextId: string;
|
||||
contextId?: string;
|
||||
workspaceId: string;
|
||||
fileId: string;
|
||||
error: string;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user