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({ async chatText({
sessionId, sessionId,
messageId, messageId,
message,
params,
}: { }: {
sessionId: string; sessionId: string;
messageId?: string; messageId: string;
message?: string;
params?: Record<string, 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}`); const url = new URL(`${this.backendUrl}/api/copilot/chat/${sessionId}`);
if (messageId) { url.searchParams.set('messageId', 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);
});
}
const response = await fetch(url.toString()); const response = await fetch(url.toString());
return response.text(); return response.text();
} }
@@ -121,33 +101,14 @@ export class CopilotClient {
chatTextStream({ chatTextStream({
sessionId, sessionId,
messageId, messageId,
message,
params,
}: { }: {
sessionId: string; sessionId: string;
messageId?: string; messageId: string;
message?: string;
params?: Record<string, 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( const url = new URL(
`${this.backendUrl}/api/copilot/chat/${sessionId}/stream` `${this.backendUrl}/api/copilot/chat/${sessionId}/stream`
); );
if (messageId) { url.searchParams.set('messageId', 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);
});
}
return new EventSource(url.toString()); return new EventSource(url.toString());
} }

View File

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

View File

@@ -28,7 +28,6 @@ export type TextToTextOptions = {
params?: Record<string, string>; params?: Record<string, string>;
timeout?: number; timeout?: number;
stream?: boolean; stream?: boolean;
forceCreate?: boolean; // force to create a message
}; };
export function createChatSession({ export function createChatSession({
@@ -53,7 +52,6 @@ async function createSessionMessage({
sessionId: providedSessionId, sessionId: providedSessionId,
attachments, attachments,
params, params,
forceCreate,
}: TextToTextOptions) { }: TextToTextOptions) {
if (!promptName && !providedSessionId) { if (!promptName && !providedSessionId) {
throw new Error('promptName or sessionId is required'); throw new Error('promptName or sessionId is required');
@@ -66,41 +64,33 @@ async function createSessionMessage({
promptName: promptName as string, promptName: promptName as string,
})); }));
if (forceCreate || hasAttachments) { const options: Parameters<CopilotClient['createMessage']>[0] = {
const options: Parameters<CopilotClient['createMessage']>[0] = { sessionId,
sessionId, content,
content, params,
params, };
};
if (hasAttachments) { if (hasAttachments) {
const [stringAttachments, blobs] = partition( const [stringAttachments, blobs] = partition(
attachments, attachments,
attachment => typeof attachment === 'string' attachment => typeof attachment === 'string'
) as [string[], (Blob | File)[]]; ) as [string[], (Blob | File)[]];
options.attachments = stringAttachments; options.attachments = stringAttachments;
options.blobs = await Promise.all( options.blobs = await Promise.all(
blobs.map(async blob => { blobs.map(async blob => {
if (blob instanceof File) { if (blob instanceof File) {
return blob; return blob;
} else { } else {
return new File([blob], await calculateBlobHash(blob)); 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 messageId = await client.createMessage(options);
return {
messageId,
sessionId,
};
} }
export function textToText({ export function textToText({
@@ -130,8 +120,6 @@ export function textToText({
const eventSource = client.chatTextStream({ const eventSource = client.chatTextStream({
sessionId: message.sessionId, sessionId: message.sessionId,
messageId: message.messageId, messageId: message.messageId,
message: message.message,
params,
}); });
yield* toTextStream(eventSource, { timeout }); yield* toTextStream(eventSource, { timeout });
}, },
@@ -157,8 +145,6 @@ export function textToText({
return await client.chatText({ return await client.chatText({
sessionId: message.sessionId, sessionId: message.sessionId,
messageId: message.messageId, messageId: message.messageId,
message: message.message,
params,
}); });
}), }),
]); ]);
@@ -175,7 +161,6 @@ export function toImage({
content, content,
attachments, attachments,
params, params,
forceCreate,
timeout = TIMEOUT, timeout = TIMEOUT,
}: TextToTextOptions) { }: TextToTextOptions) {
return { return {
@@ -187,14 +172,9 @@ export function toImage({
content, content,
attachments, attachments,
params, params,
forceCreate,
}); });
const eventSource = client.imagesStream( const eventSource = client.imagesStream(messageId, sessionId);
// @ts-expect-error: messageId should exist
messageId,
sessionId
);
yield* toTextStream(eventSource, { timeout, type: 'attachment' }); yield* toTextStream(eventSource, { timeout, type: 'attachment' });
}, },
}; };