mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 03:14:32 +03:00
#### PR Dependency Tree * **PR #14959** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Rewired realtime and copilot services to require their runtime dependencies, improving reliability and removing nullable/optional runtime paths. * **Tests** * Centralized service creation in tests with helper factories and added checks ensuring realtime dependency injection is configured as expected. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/toeverything/AFFiNE/pull/14959) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import { Injectable, OnModuleInit } from '@nestjs/common';
|
|
import { z } from 'zod';
|
|
|
|
import { CopilotTranscriptionJobNotFound } from '../../../base';
|
|
import { AccessController } from '../../../core/permission';
|
|
import {
|
|
RealtimeRegistry,
|
|
realtimeTranscriptTaskRoom,
|
|
registerRealtimeLiveQuery,
|
|
} from '../../../core/realtime';
|
|
import { CopilotTranscriptionReader } from './reader';
|
|
|
|
@Injectable()
|
|
export class CopilotTranscriptRealtimeProvider implements OnModuleInit {
|
|
constructor(
|
|
private readonly ac: AccessController,
|
|
private readonly transcript: CopilotTranscriptionReader,
|
|
private readonly registry: RealtimeRegistry
|
|
) {}
|
|
|
|
onModuleInit() {
|
|
const requestInput = z
|
|
.object({
|
|
workspaceId: z.string(),
|
|
blobId: z.string().optional(),
|
|
taskId: z.string().optional(),
|
|
})
|
|
.refine(input => input.blobId || input.taskId);
|
|
const topicInput = z.object({
|
|
workspaceId: z.string(),
|
|
taskId: z.string(),
|
|
});
|
|
|
|
registerRealtimeLiveQuery(this.registry, {
|
|
request: {
|
|
name: 'copilot.transcript.task.get',
|
|
input: requestInput,
|
|
handle: async (user, input) => {
|
|
await this.assertCopilot(user.id, input.workspaceId);
|
|
return {
|
|
task: await this.transcript.queryTask(
|
|
user.id,
|
|
input.workspaceId,
|
|
input.taskId,
|
|
input.blobId
|
|
),
|
|
};
|
|
},
|
|
},
|
|
topic: {
|
|
name: 'copilot.transcript.task.changed',
|
|
input: topicInput,
|
|
authorize: async (user, input) => {
|
|
await this.assertCopilot(user.id, input.workspaceId);
|
|
const task = await this.transcript.queryTask(
|
|
user.id,
|
|
input.workspaceId,
|
|
input.taskId
|
|
);
|
|
if (!task) {
|
|
throw new CopilotTranscriptionJobNotFound();
|
|
}
|
|
},
|
|
room: (_user, input) =>
|
|
realtimeTranscriptTaskRoom(input.workspaceId, input.taskId),
|
|
},
|
|
});
|
|
}
|
|
|
|
private async assertCopilot(userId: string, workspaceId: string) {
|
|
await this.ac
|
|
.user(userId)
|
|
.workspace(workspaceId)
|
|
.allowLocal()
|
|
.assert('Workspace.Copilot');
|
|
}
|
|
}
|