mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-02 01:49:51 +08:00
@@ -50,43 +50,30 @@ export class CopilotController {
|
|||||||
private readonly storage: CopilotStorage
|
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);
|
const session = await this.chatSession.get(sessionId);
|
||||||
if (!session) {
|
if (!session) {
|
||||||
throw new BadRequestException('Session not found');
|
throw new BadRequestException('Session not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (messageId) {
|
const message = await session.getMessageById(messageId);
|
||||||
const message = await session.getMessageById(messageId);
|
if (Array.isArray(message.attachments) && message.attachments.length) {
|
||||||
if (Array.isArray(message.attachments) && message.attachments.length) {
|
return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async appendSessionMessage(
|
private async appendSessionMessage(
|
||||||
sessionId: string,
|
sessionId: string,
|
||||||
message?: string,
|
messageId: string
|
||||||
messageId?: string
|
|
||||||
): Promise<ChatSession> {
|
): Promise<ChatSession> {
|
||||||
const session = await this.chatSession.get(sessionId);
|
const session = await this.chatSession.get(sessionId);
|
||||||
if (!session) {
|
if (!session) {
|
||||||
throw new BadRequestException('Session not found');
|
throw new BadRequestException('Session not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (messageId) {
|
await session.pushByMessageId(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(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,8 +88,7 @@ export class CopilotController {
|
|||||||
@CurrentUser() user: CurrentUser,
|
@CurrentUser() user: CurrentUser,
|
||||||
@Req() req: Request,
|
@Req() req: Request,
|
||||||
@Param('sessionId') sessionId: string,
|
@Param('sessionId') sessionId: string,
|
||||||
@Query('message') message: string | undefined,
|
@Query('messageId') messageId: string,
|
||||||
@Query('messageId') messageId: string | undefined,
|
|
||||||
@Query() params: Record<string, string | string[]>
|
@Query() params: Record<string, string | string[]>
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
await this.chatSession.checkQuota(user.id);
|
await this.chatSession.checkQuota(user.id);
|
||||||
@@ -116,14 +102,9 @@ export class CopilotController {
|
|||||||
throw new InternalServerErrorException('No provider available');
|
throw new InternalServerErrorException('No provider available');
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = await this.appendSessionMessage(
|
const session = await this.appendSessionMessage(sessionId, messageId);
|
||||||
sessionId,
|
|
||||||
message,
|
|
||||||
messageId
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
delete params.message;
|
|
||||||
delete params.messageId;
|
delete params.messageId;
|
||||||
const content = await provider.generateText(
|
const content = await provider.generateText(
|
||||||
session.finish(params),
|
session.finish(params),
|
||||||
@@ -154,8 +135,7 @@ export class CopilotController {
|
|||||||
@CurrentUser() user: CurrentUser,
|
@CurrentUser() user: CurrentUser,
|
||||||
@Req() req: Request,
|
@Req() req: Request,
|
||||||
@Param('sessionId') sessionId: string,
|
@Param('sessionId') sessionId: string,
|
||||||
@Query('message') message: string | undefined,
|
@Query('messageId') messageId: string,
|
||||||
@Query('messageId') messageId: string | undefined,
|
|
||||||
@Query() params: Record<string, string>
|
@Query() params: Record<string, string>
|
||||||
): Promise<Observable<ChatEvent>> {
|
): Promise<Observable<ChatEvent>> {
|
||||||
await this.chatSession.checkQuota(user.id);
|
await this.chatSession.checkQuota(user.id);
|
||||||
@@ -169,14 +149,9 @@ export class CopilotController {
|
|||||||
throw new InternalServerErrorException('No provider available');
|
throw new InternalServerErrorException('No provider available');
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = await this.appendSessionMessage(
|
const session = await this.appendSessionMessage(sessionId, messageId);
|
||||||
sessionId,
|
|
||||||
message,
|
|
||||||
messageId
|
|
||||||
);
|
|
||||||
|
|
||||||
delete params.message;
|
|
||||||
delete params.messageId;
|
delete params.messageId;
|
||||||
|
|
||||||
return from(
|
return from(
|
||||||
provider.generateTextStream(session.finish(params), session.model, {
|
provider.generateTextStream(session.finish(params), session.model, {
|
||||||
signal: this.getSignal(req),
|
signal: this.getSignal(req),
|
||||||
@@ -212,8 +187,7 @@ export class CopilotController {
|
|||||||
@CurrentUser() user: CurrentUser,
|
@CurrentUser() user: CurrentUser,
|
||||||
@Req() req: Request,
|
@Req() req: Request,
|
||||||
@Param('sessionId') sessionId: string,
|
@Param('sessionId') sessionId: string,
|
||||||
@Query('message') message: string | undefined,
|
@Query('messageId') messageId: string,
|
||||||
@Query('messageId') messageId: string | undefined,
|
|
||||||
@Query() params: Record<string, string>
|
@Query() params: Record<string, string>
|
||||||
): Promise<Observable<ChatEvent>> {
|
): Promise<Observable<ChatEvent>> {
|
||||||
await this.chatSession.checkQuota(user.id);
|
await this.chatSession.checkQuota(user.id);
|
||||||
@@ -230,14 +204,9 @@ export class CopilotController {
|
|||||||
throw new InternalServerErrorException('No provider available');
|
throw new InternalServerErrorException('No provider available');
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = await this.appendSessionMessage(
|
const session = await this.appendSessionMessage(sessionId, messageId);
|
||||||
sessionId,
|
|
||||||
message,
|
|
||||||
messageId
|
|
||||||
);
|
|
||||||
|
|
||||||
delete params.message;
|
|
||||||
delete params.messageId;
|
delete params.messageId;
|
||||||
|
|
||||||
const handleRemoteLink = this.storage.handleRemoteLink.bind(
|
const handleRemoteLink = this.storage.handleRemoteLink.bind(
|
||||||
this.storage,
|
this.storage,
|
||||||
user.id,
|
user.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user