Skip to content

Commit

Permalink
feat: remove message query (#6622)
Browse files Browse the repository at this point in the history
related #6620
  • Loading branch information
darkskygit committed Apr 18, 2024
1 parent 7970d9b commit 3cc3af8
Showing 1 changed file with 15 additions and 46 deletions.
61 changes: 15 additions & 46 deletions packages/backend/server/src/plugins/copilot/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,43 +50,30 @@ export class CopilotController {
private readonly storage: CopilotStorage
) {}

private async hasAttachment(sessionId: string, messageId?: string) {
private async hasAttachment(sessionId: string, messageId: string) {
const session = await this.chatSession.get(sessionId);
if (!session) {
throw new BadRequestException('Session not found');
}

if (messageId) {
const message = await session.getMessageById(messageId);
if (Array.isArray(message.attachments) && message.attachments.length) {
return true;
}
const message = await session.getMessageById(messageId);
if (Array.isArray(message.attachments) && message.attachments.length) {
return true;
}
return false;
}

private async appendSessionMessage(
sessionId: string,
message?: string,
messageId?: string
messageId: string
): Promise<ChatSession> {
const session = await this.chatSession.get(sessionId);
if (!session) {
throw new BadRequestException('Session not found');
}

if (messageId) {
await session.pushByMessageId(messageId);
} else {
if (!message || !message.trim()) {
throw new BadRequestException('Message is empty');
}
session.push({
role: 'user',
content: decodeURIComponent(message),
createdAt: new Date(),
});
}
await session.pushByMessageId(messageId);

return session;
}

Expand All @@ -101,8 +88,7 @@ export class CopilotController {
@CurrentUser() user: CurrentUser,
@Req() req: Request,
@Param('sessionId') sessionId: string,
@Query('message') message: string | undefined,
@Query('messageId') messageId: string | undefined,
@Query('messageId') messageId: string,
@Query() params: Record<string, string | string[]>
): Promise<string> {
await this.chatSession.checkQuota(user.id);
Expand All @@ -116,14 +102,9 @@ export class CopilotController {
throw new InternalServerErrorException('No provider available');
}

const session = await this.appendSessionMessage(
sessionId,
message,
messageId
);
const session = await this.appendSessionMessage(sessionId, messageId);

try {
delete params.message;
delete params.messageId;
const content = await provider.generateText(
session.finish(params),
Expand Down Expand Up @@ -154,8 +135,7 @@ export class CopilotController {
@CurrentUser() user: CurrentUser,
@Req() req: Request,
@Param('sessionId') sessionId: string,
@Query('message') message: string | undefined,
@Query('messageId') messageId: string | undefined,
@Query('messageId') messageId: string,
@Query() params: Record<string, string>
): Promise<Observable<ChatEvent>> {
await this.chatSession.checkQuota(user.id);
Expand All @@ -169,14 +149,9 @@ export class CopilotController {
throw new InternalServerErrorException('No provider available');
}

const session = await this.appendSessionMessage(
sessionId,
message,
messageId
);

delete params.message;
const session = await this.appendSessionMessage(sessionId, messageId);
delete params.messageId;

return from(
provider.generateTextStream(session.finish(params), session.model, {
signal: this.getSignal(req),
Expand Down Expand Up @@ -212,8 +187,7 @@ export class CopilotController {
@CurrentUser() user: CurrentUser,
@Req() req: Request,
@Param('sessionId') sessionId: string,
@Query('message') message: string | undefined,
@Query('messageId') messageId: string | undefined,
@Query('messageId') messageId: string,
@Query() params: Record<string, string>
): Promise<Observable<ChatEvent>> {
await this.chatSession.checkQuota(user.id);
Expand All @@ -230,14 +204,9 @@ export class CopilotController {
throw new InternalServerErrorException('No provider available');
}

const session = await this.appendSessionMessage(
sessionId,
message,
messageId
);

delete params.message;
const session = await this.appendSessionMessage(sessionId, messageId);
delete params.messageId;

const handleRemoteLink = this.storage.handleRemoteLink.bind(
this.storage,
user.id,
Expand Down

0 comments on commit 3cc3af8

Please sign in to comment.