mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
@@ -48,7 +48,7 @@ import { CurrentUser, Public } from '../../core/auth';
|
||||
import { CopilotProviderService } from './providers';
|
||||
import { ChatSession, ChatSessionService } from './session';
|
||||
import { CopilotStorage } from './storage';
|
||||
import { CopilotCapability, CopilotTextProvider } from './types';
|
||||
import { ChatMessage, CopilotCapability, CopilotTextProvider } from './types';
|
||||
import { CopilotWorkflowService, GraphExecutorState } from './workflow';
|
||||
|
||||
export interface ChatEvent {
|
||||
@@ -141,24 +141,28 @@ export class CopilotController implements BeforeApplicationShutdown {
|
||||
sessionId: string,
|
||||
messageId?: string,
|
||||
retry = false
|
||||
): Promise<ChatSession> {
|
||||
): Promise<[ChatMessage | undefined, ChatSession]> {
|
||||
const session = await this.chatSession.get(sessionId);
|
||||
if (!session) {
|
||||
throw new CopilotSessionNotFound();
|
||||
}
|
||||
|
||||
let latestMessage = undefined;
|
||||
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) {
|
||||
latestMessage = session.latestUserMessage;
|
||||
}
|
||||
}
|
||||
|
||||
if (messageId) {
|
||||
await session.pushByMessageId(messageId);
|
||||
}
|
||||
|
||||
return session;
|
||||
return [latestMessage, session];
|
||||
}
|
||||
|
||||
private prepareParams(params: Record<string, string | string[]>) {
|
||||
@@ -226,7 +230,7 @@ export class CopilotController implements BeforeApplicationShutdown {
|
||||
messageId
|
||||
);
|
||||
|
||||
const session = await this.appendSessionMessage(
|
||||
const [latestMessage, session] = await this.appendSessionMessage(
|
||||
sessionId,
|
||||
messageId,
|
||||
retry
|
||||
@@ -234,6 +238,14 @@ export class CopilotController implements BeforeApplicationShutdown {
|
||||
|
||||
info.model = session.model;
|
||||
metrics.ai.counter('chat_calls').add(1, { model: session.model });
|
||||
|
||||
if (latestMessage) {
|
||||
params = Object.assign({}, params, latestMessage.params, {
|
||||
content: latestMessage.content,
|
||||
attachments: latestMessage.attachments,
|
||||
});
|
||||
}
|
||||
|
||||
const finalMessage = session.finish(params);
|
||||
info.finalMessage = finalMessage;
|
||||
|
||||
@@ -281,14 +293,22 @@ export class CopilotController implements BeforeApplicationShutdown {
|
||||
messageId
|
||||
);
|
||||
|
||||
const session = await this.appendSessionMessage(
|
||||
const [latestMessage, session] = await this.appendSessionMessage(
|
||||
sessionId,
|
||||
messageId,
|
||||
retry
|
||||
);
|
||||
info.model = session.model;
|
||||
|
||||
info.model = session.model;
|
||||
metrics.ai.counter('chat_stream_calls').add(1, { model: session.model });
|
||||
|
||||
if (latestMessage) {
|
||||
params = Object.assign({}, params, latestMessage.params, {
|
||||
content: latestMessage.content,
|
||||
attachments: latestMessage.attachments,
|
||||
});
|
||||
}
|
||||
|
||||
this.ongoingStreamCount$.next(this.ongoingStreamCount$.value + 1);
|
||||
const finalMessage = session.finish(params);
|
||||
info.finalMessage = finalMessage;
|
||||
@@ -349,10 +369,11 @@ export class CopilotController implements BeforeApplicationShutdown {
|
||||
try {
|
||||
const { messageId } = this.prepareParams(params);
|
||||
|
||||
const session = await this.appendSessionMessage(sessionId, messageId);
|
||||
const [, session] = await this.appendSessionMessage(sessionId, messageId);
|
||||
info.model = session.model;
|
||||
|
||||
metrics.ai.counter('workflow_calls').add(1, { model: session.model });
|
||||
|
||||
const latestMessage = session.stashMessages.findLast(
|
||||
m => m.role === 'user'
|
||||
);
|
||||
@@ -463,12 +484,22 @@ export class CopilotController implements BeforeApplicationShutdown {
|
||||
throw new NoCopilotProviderAvailable();
|
||||
}
|
||||
|
||||
const session = await this.appendSessionMessage(sessionId, messageId);
|
||||
const [latestMessage, session] = await this.appendSessionMessage(
|
||||
sessionId,
|
||||
messageId
|
||||
);
|
||||
info.model = session.model;
|
||||
|
||||
metrics.ai
|
||||
.counter('images_stream_calls')
|
||||
.add(1, { model: session.model });
|
||||
|
||||
if (latestMessage) {
|
||||
params = Object.assign({}, params, latestMessage.params, {
|
||||
content: latestMessage.content,
|
||||
attachments: latestMessage.attachments,
|
||||
});
|
||||
}
|
||||
|
||||
const handleRemoteLink = this.storage.handleRemoteLink.bind(
|
||||
this.storage,
|
||||
user.id,
|
||||
|
||||
Reference in New Issue
Block a user