feat: allow retry with new message (#10307)

fix AF-1630
This commit is contained in:
darkskygit
2025-02-20 06:07:53 +00:00
parent 50820482df
commit 13f1859cdf
9 changed files with 313 additions and 44 deletions

View File

@@ -137,20 +137,23 @@ export class CopilotController implements BeforeApplicationShutdown {
private async appendSessionMessage(
sessionId: string,
messageId?: string
messageId?: string,
retry = false
): Promise<ChatSession> {
const session = await this.chatSession.get(sessionId);
if (!session) {
throw new CopilotSessionNotFound();
}
if (!messageId || retry) {
// revert the latest message generated by the assistant
// if messageId is provided, we will also revert latest user message
await this.chatSession.revertLatestMessage(sessionId, !messageId);
session.revertLatestMessage(!messageId);
}
if (messageId) {
await session.pushByMessageId(messageId);
} else {
// revert the latest message generated by the assistant
// if messageId is not provided, then we can retry the action
await this.chatSession.revertLatestMessage(sessionId);
session.revertLatestMessage();
}
return session;
@@ -160,8 +163,12 @@ export class CopilotController implements BeforeApplicationShutdown {
const messageId = Array.isArray(params.messageId)
? params.messageId[0]
: params.messageId;
const retry = Array.isArray(params.retry)
? Boolean(params.retry[0])
: Boolean(params.retry);
delete params.messageId;
return { messageId, params };
delete params.retry;
return { messageId, retry, params };
}
private getSignal(req: Request) {
@@ -202,7 +209,7 @@ export class CopilotController implements BeforeApplicationShutdown {
@Param('sessionId') sessionId: string,
@Query() params: Record<string, string | string[]>
): Promise<string> {
const { messageId } = this.prepareParams(params);
const { messageId, retry } = this.prepareParams(params);
const provider = await this.chooseTextProvider(
user.id,
@@ -210,7 +217,11 @@ export class CopilotController implements BeforeApplicationShutdown {
messageId
);
const session = await this.appendSessionMessage(sessionId, messageId);
const session = await this.appendSessionMessage(
sessionId,
messageId,
retry
);
try {
metrics.ai.counter('chat_calls').add(1, { model: session.model });
const content = await provider.generateText(
@@ -248,7 +259,7 @@ export class CopilotController implements BeforeApplicationShutdown {
const info: any = { sessionId, params, throwInStream: false };
try {
const { messageId } = this.prepareParams(params);
const { messageId, retry } = this.prepareParams(params);
const provider = await this.chooseTextProvider(
user.id,
@@ -256,7 +267,11 @@ export class CopilotController implements BeforeApplicationShutdown {
messageId
);
const session = await this.appendSessionMessage(sessionId, messageId);
const session = await this.appendSessionMessage(
sessionId,
messageId,
retry
);
info.model = session.model;
metrics.ai.counter('chat_stream_calls').add(1, { model: session.model });