Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(core): always create message for ai (#6620)

pengx17c3438fde2192
diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts
index 1982e64364..9bcbadc7ac 100644
--- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts
+++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/copilot-client.ts
@@ -87,32 +87,12 @@ export class CopilotClient {
async chatText({
sessionId,
messageId,
- message,
- params,
}: {
sessionId: string;
- messageId?: string;
- message?: string;
- params?: Record<string, string>;
+ messageId: string;
}) {
- if (messageId && message) {
- throw new Error('Only one of messageId or message can be provided');
- } else if (!messageId && !message) {
- throw new Error('Either messageId or message must be provided');
- }
const url = new URL(`${this.backendUrl}/api/copilot/chat/${sessionId}`);
- if (messageId) {
- url.searchParams.set('messageId', messageId);
- }
- if (message) {
- url.searchParams.set('message', message);
- }
- if (!messageId && params) {
- Object.entries(params).forEach(([key, value]) => {
- url.searchParams.set(key, value);
- });
- }
-
+ url.searchParams.set('messageId', messageId);
const response = await fetch(url.toString());
return response.text();
}
@@ -121,33 +101,14 @@ export class CopilotClient {
chatTextStream({
sessionId,
messageId,
- message,
- params,
}: {
sessionId: string;
- messageId?: string;
- message?: string;
- params?: Record<string, string>;
+ messageId: string;
}) {
- if (messageId && message) {
- throw new Error('Only one of messageId or message can be provided');
- } else if (!messageId && !message) {
- throw new Error('Either messageId or message must be provided');
- }
const url = new URL(
`${this.backendUrl}/api/copilot/chat/${sessionId}/stream`
);
- if (messageId) {
- url.searchParams.set('messageId', messageId);
- }
- if (message) {
- url.searchParams.set('message', message);
- }
- if (!messageId && params) {
- Object.entries(params).forEach(([key, value]) => {
- url.searchParams.set(key, value);
- });
- }
+ url.searchParams.set('messageId', messageId);
return new EventSource(url.toString());
}
diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts
index efcea9c8b3..1565c61d7c 100644
--- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts
+++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/provider.ts
@@ -263,7 +263,6 @@ export function setupAIProvider() {
return toImage({
...options,
promptName,
- forceCreate: true,
});
});
diff --git a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts
index c114985e59..af41b8b9b3 100644
--- a/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts
+++ b/packages/frontend/core/src/components/blocksuite/block-suite-editor/ai/request.ts
@@ -28,7 +28,6 @@ export type TextToTextOptions = {
params?: Record<string, string>;
timeout?: number;
stream?: boolean;
- forceCreate?: boolean; // force to create a message
};
export function createChatSession({
@@ -53,7 +52,6 @@ async function createSessionMessage({
sessionId: providedSessionId,
attachments,
params,
- forceCreate,
}: TextToTextOptions) {
if (!promptName && !providedSessionId) {
throw new Error('promptName or sessionId is required');
@@ -66,41 +64,33 @@ async function createSessionMessage({
promptName: promptName as string,
}));
- if (forceCreate || hasAttachments) {
- const options: Parameters<CopilotClient['createMessage']>[0] = {
- sessionId,
- content,
- params,
- };
- if (hasAttachments) {
- const [stringAttachments, blobs] = partition(
- attachments,
- attachment => typeof attachment === 'string'
- ) as [string[], (Blob | File)[]];
- options.attachments = stringAttachments;
- options.blobs = await Promise.all(
- blobs.map(async blob => {
- if (blob instanceof File) {
- return blob;
- } else {
- return new File([blob], await calculateBlobHash(blob));
- }
- })
- );
- }
- const messageId = await client.createMessage(options);
- return {
- messageId,
- sessionId,
- };
- } else if (content) {
- return {
- message: content,
- sessionId,
- };
- } else {
- throw new Error('No content or attachments provided');
+ const options: Parameters<CopilotClient['createMessage']>[0] = {
+ sessionId,
+ content,
+ params,
+ };
+
+ if (hasAttachments) {
+ const [stringAttachments, blobs] = partition(
+ attachments,
+ attachment => typeof attachment === 'string'
+ ) as [string[], (Blob | File)[]];
+ options.attachments = stringAttachments;
+ options.blobs = await Promise.all(
+ blobs.map(async blob => {
+ if (blob instanceof File) {
+ return blob;
+ } else {
+ return new File([blob], await calculateBlobHash(blob));
+ }
+ })
+ );
}
+ const messageId = await client.createMessage(options);
+ return {
+ messageId,
+ sessionId,
+ };
}
export function textToText({
@@ -130,8 +120,6 @@ export function textToText({
const eventSource = client.chatTextStream({
sessionId: message.sessionId,
messageId: message.messageId,
- message: message.message,
- params,
});
yield* toTextStream(eventSource, { timeout });
},
@@ -157,8 +145,6 @@ export function textToText({
return await client.chatText({
sessionId: message.sessionId,
messageId: message.messageId,
- message: message.message,
- params,
});
}),
]);
@@ -175,7 +161,6 @@ export function toImage({
content,
attachments,
params,
- forceCreate,
timeout = TIMEOUT,
}: TextToTextOptions) {
return {
@@ -187,14 +172,9 @@ export function toImage({
content,
attachments,
params,
- forceCreate,
});
- const eventSource = client.imagesStream(
- // @ts-expect-error: messageId should exist
- messageId,
- sessionId
- );
+ const eventSource = client.imagesStream(messageId, sessionId);
yield* toTextStream(eventSource, { timeout, type: 'attachment' });
},
};