feat: add blob upload support for copilot (#6584)

This commit is contained in:
DarkSky
2024-04-17 22:05:38 +08:00
committed by GitHub
parent e806169f60
commit ccb3bed91e
10 changed files with 260 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
import { Logger } from '@nestjs/common';
import { BadRequestException, Logger } from '@nestjs/common';
import {
Args,
Field,
@@ -12,12 +12,18 @@ import {
Resolver,
} from '@nestjs/graphql';
import { GraphQLJSON, SafeIntResolver } from 'graphql-scalars';
import GraphQLUpload from 'graphql-upload/GraphQLUpload.mjs';
import { CurrentUser } from '../../core/auth';
import { UserType } from '../../core/user';
import { PermissionService } from '../../core/workspaces/permission';
import { MutexService, TooManyRequestsException } from '../../fundamentals';
import {
FileUpload,
MutexService,
TooManyRequestsException,
} from '../../fundamentals';
import { ChatSessionService } from './session';
import { CopilotStorage } from './storage';
import {
AvailableModels,
type ChatHistory,
@@ -28,7 +34,7 @@ import {
registerEnumType(AvailableModels, { name: 'CopilotModel' });
const COPILOT_LOCKER = 'copilot';
export const COPILOT_LOCKER = 'copilot';
// ================== Input Types ==================
@@ -57,6 +63,9 @@ class CreateChatMessageInput implements Omit<SubmittedMessage, 'content'> {
@Field(() => [String], { nullable: true })
attachments!: string[] | undefined;
@Field(() => [GraphQLUpload], { nullable: true })
blobs!: FileUpload[] | undefined;
@Field(() => GraphQLJSON, { nullable: true })
params!: Record<string, string> | undefined;
}
@@ -140,7 +149,8 @@ export class CopilotResolver {
constructor(
private readonly permissions: PermissionService,
private readonly mutex: MutexService,
private readonly chatSession: ChatSessionService
private readonly chatSession: ChatSessionService,
private readonly storage: CopilotStorage
) {}
@ResolveField(() => CopilotQuotaType, {
@@ -260,6 +270,25 @@ export class CopilotResolver {
if (!lock) {
return new TooManyRequestsException('Server is busy');
}
const session = await this.chatSession.get(options.sessionId);
if (!session) return new BadRequestException('Session not found');
if (options.blobs) {
options.attachments = options.attachments || [];
const { workspaceId } = session.config;
for (const blob of options.blobs) {
const uploaded = await this.storage.handleUpload(user.id, blob);
const link = await this.storage.put(
user.id,
workspaceId,
uploaded.filename,
uploaded.buffer
);
options.attachments.push(link);
}
}
try {
return await this.chatSession.createMessage(options);
} catch (e: any) {