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

This commit is contained in:
pengx17
2024-04-18 15:09:36 +00:00
parent 08cd940e6b
commit c3438fde21
3 changed files with 31 additions and 91 deletions

View File

@@ -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());
}

View File

@@ -263,7 +263,6 @@ export function setupAIProvider() {
return toImage({
...options,
promptName,
forceCreate: true,
});
});

View File

@@ -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' });
},
};