feat: handle stream error (#6653)
This commit is contained in:
@@ -2,6 +2,7 @@ import {
|
|||||||
BadRequestException,
|
BadRequestException,
|
||||||
Controller,
|
Controller,
|
||||||
Get,
|
Get,
|
||||||
|
HttpException,
|
||||||
InternalServerErrorException,
|
InternalServerErrorException,
|
||||||
Logger,
|
Logger,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
@@ -13,6 +14,7 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import type { Request, Response } from 'express';
|
import type { Request, Response } from 'express';
|
||||||
import {
|
import {
|
||||||
|
catchError,
|
||||||
concatMap,
|
concatMap,
|
||||||
connect,
|
connect,
|
||||||
EMPTY,
|
EMPTY,
|
||||||
@@ -21,6 +23,7 @@ import {
|
|||||||
merge,
|
merge,
|
||||||
mergeMap,
|
mergeMap,
|
||||||
Observable,
|
Observable,
|
||||||
|
of,
|
||||||
switchMap,
|
switchMap,
|
||||||
toArray,
|
toArray,
|
||||||
} from 'rxjs';
|
} from 'rxjs';
|
||||||
@@ -34,7 +37,7 @@ import { CopilotStorage } from './storage';
|
|||||||
import { CopilotCapability } from './types';
|
import { CopilotCapability } from './types';
|
||||||
|
|
||||||
export interface ChatEvent {
|
export interface ChatEvent {
|
||||||
type: 'attachment' | 'message';
|
type: 'attachment' | 'message' | 'error';
|
||||||
id?: string;
|
id?: string;
|
||||||
data: string;
|
data: string;
|
||||||
}
|
}
|
||||||
@@ -83,6 +86,19 @@ export class CopilotController {
|
|||||||
return controller.signal;
|
return controller.signal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private handleError(err: any) {
|
||||||
|
if (err instanceof Error) {
|
||||||
|
const ret = {
|
||||||
|
message: err.message,
|
||||||
|
status: (err as any).status,
|
||||||
|
};
|
||||||
|
if (err instanceof HttpException) {
|
||||||
|
ret.status = err.getStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
@Get('/chat/:sessionId')
|
@Get('/chat/:sessionId')
|
||||||
async chat(
|
async chat(
|
||||||
@CurrentUser() user: CurrentUser,
|
@CurrentUser() user: CurrentUser,
|
||||||
@@ -138,7 +154,14 @@ export class CopilotController {
|
|||||||
@Query('messageId') messageId: string,
|
@Query('messageId') messageId: string,
|
||||||
@Query() params: Record<string, string>
|
@Query() params: Record<string, string>
|
||||||
): Promise<Observable<ChatEvent>> {
|
): Promise<Observable<ChatEvent>> {
|
||||||
await this.chatSession.checkQuota(user.id);
|
try {
|
||||||
|
await this.chatSession.checkQuota(user.id);
|
||||||
|
} catch (err) {
|
||||||
|
return of({
|
||||||
|
type: 'error' as const,
|
||||||
|
data: this.handleError(err),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const model = await this.chatSession.get(sessionId).then(s => s?.model);
|
const model = await this.chatSession.get(sessionId).then(s => s?.model);
|
||||||
const provider = this.provider.getProviderByCapability(
|
const provider = this.provider.getProviderByCapability(
|
||||||
@@ -178,6 +201,12 @@ export class CopilotController {
|
|||||||
switchMap(() => EMPTY)
|
switchMap(() => EMPTY)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
),
|
||||||
|
catchError(err =>
|
||||||
|
of({
|
||||||
|
type: 'error' as const,
|
||||||
|
data: this.handleError(err),
|
||||||
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -190,7 +219,14 @@ export class CopilotController {
|
|||||||
@Query('messageId') messageId: string,
|
@Query('messageId') messageId: string,
|
||||||
@Query() params: Record<string, string>
|
@Query() params: Record<string, string>
|
||||||
): Promise<Observable<ChatEvent>> {
|
): Promise<Observable<ChatEvent>> {
|
||||||
await this.chatSession.checkQuota(user.id);
|
try {
|
||||||
|
await this.chatSession.checkQuota(user.id);
|
||||||
|
} catch (err) {
|
||||||
|
return of({
|
||||||
|
type: 'error' as const,
|
||||||
|
data: this.handleError(err),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const hasAttachment = await this.hasAttachment(sessionId, messageId);
|
const hasAttachment = await this.hasAttachment(sessionId, messageId);
|
||||||
const model = await this.chatSession.get(sessionId).then(s => s?.model);
|
const model = await this.chatSession.get(sessionId).then(s => s?.model);
|
||||||
@@ -245,6 +281,12 @@ export class CopilotController {
|
|||||||
switchMap(() => EMPTY)
|
switchMap(() => EMPTY)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
),
|
||||||
|
catchError(err =>
|
||||||
|
of({
|
||||||
|
type: 'error' as const,
|
||||||
|
data: this.handleError(err),
|
||||||
|
})
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user