mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-24 13:17:10 +08:00
fix(core): audio block job
This commit is contained in:
@@ -1065,7 +1065,6 @@ test('copilot transcript realtime provider registers task live query handlers',
|
|||||||
return { id: taskId, status: 'running', userId, workspaceId };
|
return { id: taskId, status: 'running', userId, workspaceId };
|
||||||
},
|
},
|
||||||
} as unknown as CopilotTranscriptionRetryService;
|
} as unknown as CopilotTranscriptionRetryService;
|
||||||
|
|
||||||
new CopilotTranscriptRealtimeProvider(ac, transcript, retry, registry, {
|
new CopilotTranscriptRealtimeProvider(ac, transcript, retry, registry, {
|
||||||
copilot: { enabled: true },
|
copilot: { enabled: true },
|
||||||
} as never).onModuleInit();
|
} as never).onModuleInit();
|
||||||
|
|||||||
@@ -115,9 +115,10 @@ const AttachmentAudioPlayer = ({ block }: { block: AudioAttachmentBlock }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setPreflightChecking(true);
|
setPreflightChecking(true);
|
||||||
const result = await block.transcriptionJob.preflightCheck();
|
const result = await block.transcribe().finally(() => {
|
||||||
setPreflightChecking(false);
|
setPreflightChecking(false);
|
||||||
if (result?.error === 'created-by-others') {
|
});
|
||||||
|
if (result?.status === 'blocked') {
|
||||||
confirmModal.openConfirmModal({
|
confirmModal.openConfirmModal({
|
||||||
title: t['com.affine.audio.transcribe.non-owner.confirm.title'](),
|
title: t['com.affine.audio.transcribe.non-owner.confirm.title'](),
|
||||||
description: (
|
description: (
|
||||||
@@ -137,7 +138,6 @@ const AttachmentAudioPlayer = ({ block }: { block: AudioAttachmentBlock }) => {
|
|||||||
method: 'not owner',
|
method: 'not owner',
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
await block.transcribe();
|
|
||||||
track.doc.editor.audioBlock.transcribeRecording({
|
track.doc.editor.audioBlock.transcribeRecording({
|
||||||
type: 'Meeting record',
|
type: 'Meeting record',
|
||||||
method: 'success',
|
method: 'success',
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ export class AudioAttachmentBlock extends Entity<AttachmentBlockModel> {
|
|||||||
this.transcriptionJob.status$.value.status === 'waiting-for-job' &&
|
this.transcriptionJob.status$.value.status === 'waiting-for-job' &&
|
||||||
!this.hasTranscription$.value
|
!this.hasTranscription$.value
|
||||||
) {
|
) {
|
||||||
this.transcribe().catch(error => {
|
this.resumeTranscription().catch(error => {
|
||||||
logger.error('Error transcribing audio:', error);
|
logger.error('Error transcribing audio:', error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -167,16 +167,20 @@ export class AudioAttachmentBlock extends Entity<AttachmentBlockModel> {
|
|||||||
return job;
|
return job;
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly transcribe = async () => {
|
private readonly runTranscription = async (retryFailed: boolean) => {
|
||||||
try {
|
try {
|
||||||
const initialStatus = this.transcriptionJob.status$.value.status;
|
const initialStatus = this.transcriptionJob.status$.value.status;
|
||||||
if (initialStatus !== 'waiting-for-job' && initialStatus !== 'failed') {
|
if (initialStatus !== 'waiting-for-job' && initialStatus !== 'failed') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const status = await this.transcriptionJob.start();
|
const status = await this.transcriptionJob.start(retryFailed);
|
||||||
|
if (status.status === 'blocked') {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
if (status.status === 'settled') {
|
if (status.status === 'settled') {
|
||||||
await this.fillTranscriptionResult(status.result);
|
await this.fillTranscriptionResult(status.result);
|
||||||
}
|
}
|
||||||
|
return status;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
track.doc.editor.audioBlock.transcribeRecording({
|
track.doc.editor.audioBlock.transcribeRecording({
|
||||||
type: 'Meeting record',
|
type: 'Meeting record',
|
||||||
@@ -187,6 +191,10 @@ export class AudioAttachmentBlock extends Entity<AttachmentBlockModel> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
readonly resumeTranscription = () => this.runTranscription(false);
|
||||||
|
|
||||||
|
readonly transcribe = () => this.runTranscription(true);
|
||||||
|
|
||||||
private readonly fillTranscriptionResult = async (
|
private readonly fillTranscriptionResult = async (
|
||||||
result: TranscriptionResult
|
result: TranscriptionResult
|
||||||
) => {
|
) => {
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/** @vitest-environment happy-dom */
|
||||||
|
|
||||||
|
import type { TranscriptionBlockProps } from '@affine/core/blocksuite/ai/blocks/transcription-block/model';
|
||||||
|
import { AiJobStatus } from '@affine/graphql';
|
||||||
|
import { Framework } from '@toeverything/infra';
|
||||||
|
import { describe, expect, test, vi } from 'vitest';
|
||||||
|
|
||||||
|
import { AuthService } from '../../cloud/services/auth';
|
||||||
|
import { DefaultServerService } from '../../cloud/services/default-server';
|
||||||
|
import { WorkspaceServerService } from '../../cloud/services/workspace-server';
|
||||||
|
import { NbstoreService } from '../../storage';
|
||||||
|
import { WorkspaceService } from '../../workspace';
|
||||||
|
import { AudioTranscriptionJob } from './audio-transcription-job';
|
||||||
|
import { AudioTranscriptionJobStore } from './audio-transcription-job-store';
|
||||||
|
|
||||||
|
describe('AudioTranscriptionJob', () => {
|
||||||
|
test('only retries a failed task after explicit user intent', async () => {
|
||||||
|
const request = vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValueOnce({
|
||||||
|
task: { id: 'task-1', status: AiJobStatus.failed },
|
||||||
|
})
|
||||||
|
.mockResolvedValueOnce({
|
||||||
|
task: { id: 'task-1', status: AiJobStatus.failed },
|
||||||
|
})
|
||||||
|
.mockResolvedValueOnce({
|
||||||
|
task: { id: 'task-1', status: AiJobStatus.failed },
|
||||||
|
});
|
||||||
|
const server = {
|
||||||
|
scope: {
|
||||||
|
get: () => null,
|
||||||
|
getOptional: (key: unknown) =>
|
||||||
|
key === AuthService
|
||||||
|
? { session: { account$: { value: { id: 'user-1' } } } }
|
||||||
|
: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const framework = new Framework();
|
||||||
|
framework
|
||||||
|
.service(WorkspaceService, {
|
||||||
|
workspace: { id: 'workspace-1' },
|
||||||
|
} as WorkspaceService)
|
||||||
|
.service(WorkspaceServerService, {
|
||||||
|
server,
|
||||||
|
} as unknown as WorkspaceServerService)
|
||||||
|
.service(DefaultServerService, {
|
||||||
|
server: null,
|
||||||
|
} as unknown as DefaultServerService)
|
||||||
|
.service(NbstoreService, {
|
||||||
|
realtime: { request },
|
||||||
|
} as unknown as NbstoreService)
|
||||||
|
.entity(AudioTranscriptionJobStore, [
|
||||||
|
WorkspaceService,
|
||||||
|
WorkspaceServerService,
|
||||||
|
DefaultServerService,
|
||||||
|
NbstoreService,
|
||||||
|
])
|
||||||
|
.entity(AudioTranscriptionJob, [
|
||||||
|
WorkspaceServerService,
|
||||||
|
DefaultServerService,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const job = framework.provider().createEntity(AudioTranscriptionJob, {
|
||||||
|
blobId: 'blob-1',
|
||||||
|
blockProps: {
|
||||||
|
jobId: 'task-1',
|
||||||
|
createdBy: 'user-1',
|
||||||
|
} as TranscriptionBlockProps,
|
||||||
|
getAudioTranscriptionInput: async () => ({ files: [] }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const resumed = await job.start(false);
|
||||||
|
|
||||||
|
expect(resumed.status).toBe(AiJobStatus.failed);
|
||||||
|
expect(request).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
const [first, second] = await Promise.all([
|
||||||
|
job.start(true),
|
||||||
|
job.start(true),
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(first.status).toBe(AiJobStatus.failed);
|
||||||
|
expect(second).toBe(first);
|
||||||
|
expect(request).toHaveBeenCalledTimes(3);
|
||||||
|
expect(request).toHaveBeenNthCalledWith(
|
||||||
|
1,
|
||||||
|
'copilot.transcript.task.get',
|
||||||
|
{
|
||||||
|
workspaceId: 'workspace-1',
|
||||||
|
taskId: 'task-1',
|
||||||
|
blobId: 'blob-1',
|
||||||
|
},
|
||||||
|
{ timeoutMs: 10000 }
|
||||||
|
);
|
||||||
|
expect(request).toHaveBeenNthCalledWith(
|
||||||
|
2,
|
||||||
|
'copilot.transcript.task.get',
|
||||||
|
{
|
||||||
|
workspaceId: 'workspace-1',
|
||||||
|
taskId: 'task-1',
|
||||||
|
blobId: 'blob-1',
|
||||||
|
},
|
||||||
|
{ timeoutMs: 10000 }
|
||||||
|
);
|
||||||
|
expect(request).toHaveBeenNthCalledWith(
|
||||||
|
3,
|
||||||
|
'copilot.transcript.task.retry',
|
||||||
|
{ workspaceId: 'workspace-1', taskId: 'task-1' }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -26,6 +26,14 @@ export type TranscriptionStatus =
|
|||||||
| { status: AiJobStatus.finished }
|
| { status: AiJobStatus.finished }
|
||||||
| { status: 'settled'; result: TranscriptionResult };
|
| { status: 'settled'; result: TranscriptionResult };
|
||||||
|
|
||||||
|
export type TranscriptionStartResult =
|
||||||
|
| TranscriptionStatus
|
||||||
|
| {
|
||||||
|
status: 'blocked';
|
||||||
|
error: 'created-by-others';
|
||||||
|
userId: string;
|
||||||
|
};
|
||||||
|
|
||||||
const logger = new DebugLogger('audio-transcription-job');
|
const logger = new DebugLogger('audio-transcription-job');
|
||||||
|
|
||||||
function hasSettledTranscriptResult(
|
function hasSettledTranscriptResult(
|
||||||
@@ -71,6 +79,8 @@ export class AudioTranscriptionJob extends Entity<{
|
|||||||
RealtimeTopicEventOf<'copilot.transcript.task.changed'>
|
RealtimeTopicEventOf<'copilot.transcript.task.changed'>
|
||||||
>;
|
>;
|
||||||
private taskWaitReject?: (error: unknown) => void;
|
private taskWaitReject?: (error: unknown) => void;
|
||||||
|
private startPromise?: Promise<TranscriptionStartResult>;
|
||||||
|
private retryFailedRequested = false;
|
||||||
|
|
||||||
private readonly _status$ = new LiveData<TranscriptionStatus>({
|
private readonly _status$ = new LiveData<TranscriptionStatus>({
|
||||||
status: 'waiting-for-job',
|
status: 'waiting-for-job',
|
||||||
@@ -101,46 +111,36 @@ export class AudioTranscriptionJob extends Entity<{
|
|||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
// check if we can kick start the transcription job
|
start(retryFailed: boolean): Promise<TranscriptionStartResult> {
|
||||||
readonly preflightCheck = async () => {
|
this.retryFailedRequested ||= retryFailed;
|
||||||
// if the job id is given, check if the job exists
|
if (this.startPromise) {
|
||||||
if (this.props.blockProps.jobId) {
|
return this.startPromise;
|
||||||
const existingJob = await this.store.getTranscriptTask(
|
|
||||||
this.props.blobId,
|
|
||||||
this.props.blockProps.jobId
|
|
||||||
);
|
|
||||||
|
|
||||||
if (hasSettledTranscriptResult(existingJob)) {
|
|
||||||
// if job exists, anyone can query it
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!existingJob &&
|
|
||||||
this.props.blockProps.createdBy &&
|
|
||||||
this.props.blockProps.createdBy !== this.currentUserId
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
error: 'created-by-others',
|
|
||||||
userId: this.props.blockProps.createdBy,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
const promise = this.runStart();
|
||||||
|
this.startPromise = promise;
|
||||||
|
void promise.then(
|
||||||
|
() => {
|
||||||
|
if (this.startPromise === promise) {
|
||||||
|
this.startPromise = undefined;
|
||||||
|
this.retryFailedRequested = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
if (this.startPromise === promise) {
|
||||||
|
this.startPromise = undefined;
|
||||||
|
this.retryFailedRequested = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return promise;
|
||||||
|
}
|
||||||
|
|
||||||
// if no job id, anyone can start a new job
|
private async runStart(): Promise<TranscriptionStartResult> {
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
async start() {
|
|
||||||
if (this.disposed) {
|
if (this.disposed) {
|
||||||
logger.debug('Job already disposed, cannot start');
|
logger.debug('Job already disposed, cannot start');
|
||||||
throw new Error('Job already disposed');
|
throw new Error('Job already disposed');
|
||||||
}
|
}
|
||||||
|
|
||||||
this._status$.value = {
|
|
||||||
status: 'started',
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// firstly check if there is a job already
|
// firstly check if there is a job already
|
||||||
logger.debug('Checking for existing transcription job', {
|
logger.debug('Checking for existing transcription job', {
|
||||||
@@ -150,15 +150,38 @@ export class AudioTranscriptionJob extends Entity<{
|
|||||||
let job: {
|
let job: {
|
||||||
id: string;
|
id: string;
|
||||||
status: AiJobStatus;
|
status: AiJobStatus;
|
||||||
|
normalizedTranscript?: string | null;
|
||||||
|
transcription?: unknown[] | null;
|
||||||
} | null = await this.store.getTranscriptTask(
|
} | null = await this.store.getTranscriptTask(
|
||||||
this.props.blobId,
|
this.props.blobId,
|
||||||
this.props.blockProps.jobId
|
this.props.blockProps.jobId
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.props.blockProps.jobId &&
|
||||||
|
!hasSettledTranscriptResult(job) &&
|
||||||
|
!job &&
|
||||||
|
this.props.blockProps.createdBy &&
|
||||||
|
this.props.blockProps.createdBy !== this.currentUserId
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
status: 'blocked',
|
||||||
|
error: 'created-by-others',
|
||||||
|
userId: this.props.blockProps.createdBy,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
this._status$.value = {
|
||||||
|
status: 'started',
|
||||||
|
};
|
||||||
|
|
||||||
if (!job) {
|
if (!job) {
|
||||||
logger.debug('No existing job found, submitting new transcription job');
|
logger.debug('No existing job found, submitting new transcription job');
|
||||||
job = await this.store.submitTranscriptTask();
|
job = await this.store.submitTranscriptTask();
|
||||||
} else if (job.status === AiJobStatus.failed) {
|
} else if (job.status === AiJobStatus.failed) {
|
||||||
|
if (!this.retryFailedRequested) {
|
||||||
|
throw UserFriendlyError.fromAny('Transcription job failed');
|
||||||
|
}
|
||||||
logger.debug('Found existing failed job, retrying', {
|
logger.debug('Found existing failed job, retrying', {
|
||||||
jobId: job.id,
|
jobId: job.id,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user