feat: text to image impl (#6437)

fix CLOUD-18
fix CLOUD-28
fix CLOUD-29
This commit is contained in:
darkskygit
2024-04-10 12:13:39 +00:00
parent 7c38a54f81
commit 9f349a2300
19 changed files with 601 additions and 99 deletions

View File

@@ -1,3 +1,4 @@
import { Logger } from '@nestjs/common';
import {
Args,
Field,
@@ -12,7 +13,7 @@ import {
} from '@nestjs/graphql';
import { SafeIntResolver } from 'graphql-scalars';
import { CurrentUser, Public } from '../../core/auth';
import { CurrentUser } from '../../core/auth';
import { QuotaService } from '../../core/quota';
import { UserType } from '../../core/user';
import { PermissionService } from '../../core/workspaces/permission';
@@ -21,11 +22,19 @@ import {
PaymentRequiredException,
TooManyRequestsException,
} from '../../fundamentals';
import { ChatSessionService, ListHistoriesOptions } from './session';
import { AvailableModels, type ChatHistory, type ChatMessage } from './types';
import { ChatSessionService } from './session';
import {
AvailableModels,
type ChatHistory,
type ChatMessage,
type ListHistoriesOptions,
SubmittedMessage,
} from './types';
registerEnumType(AvailableModels, { name: 'CopilotModel' });
const COPILOT_LOCKER = 'copilot';
// ================== Input Types ==================
@InputType()
@@ -48,6 +57,21 @@ class CreateChatSessionInput {
promptName!: string;
}
@InputType()
class CreateChatMessageInput implements Omit<SubmittedMessage, 'params'> {
@Field(() => String)
sessionId!: string;
@Field(() => String)
content!: string;
@Field(() => [String], { nullable: true })
attachments!: string[] | undefined;
@Field(() => String, { nullable: true })
params!: string | undefined;
}
@InputType()
class QueryChatHistoriesInput implements Partial<ListHistoriesOptions> {
@Field(() => Boolean, { nullable: true })
@@ -118,6 +142,8 @@ export class CopilotType {
@Resolver(() => CopilotType)
export class CopilotResolver {
private readonly logger = new Logger(CopilotResolver.name);
constructor(
private readonly permissions: PermissionService,
private readonly quota: QuotaService,
@@ -208,7 +234,6 @@ export class CopilotResolver {
);
}
@Public()
@Mutation(() => String, {
description: 'Create a chat session',
})
@@ -222,7 +247,7 @@ export class CopilotResolver {
options.docId,
user.id
);
const lockFlag = `session:${user.id}:${options.workspaceId}`;
const lockFlag = `${COPILOT_LOCKER}:session:${user.id}:${options.workspaceId}`;
await using lock = await this.mutex.lock(lockFlag);
if (!lock) {
return new TooManyRequestsException('Server is busy');
@@ -241,6 +266,32 @@ export class CopilotResolver {
});
return session;
}
@Mutation(() => String, {
description: 'Create a chat message',
})
async createCopilotMessage(
@CurrentUser() user: CurrentUser,
@Args({ name: 'options', type: () => CreateChatMessageInput })
options: CreateChatMessageInput
) {
const lockFlag = `${COPILOT_LOCKER}:message:${user?.id}:${options.sessionId}`;
await using lock = await this.mutex.lock(lockFlag);
if (!lock) {
return new TooManyRequestsException('Server is busy');
}
try {
const { params, ...rest } = options;
const record: SubmittedMessage['params'] = {};
new URLSearchParams(params).forEach((value, key) => {
record[key] = value;
});
return await this.chatSession.createMessage({ ...rest, params: record });
} catch (e: any) {
this.logger.error(`Failed to create chat message: ${e.message}`);
throw new Error('Failed to create chat message');
}
}
}
@Resolver(() => UserType)