Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ export function setupAIProvider() {
return toImage({
...options,
promptName,
forceCreate: true,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -53,7 +52,6 @@ async function createSessionMessage({
sessionId: providedSessionId,
attachments,
params,
forceCreate,
}: TextToTextOptions) {
if (!promptName && !providedSessionId) {
throw new Error('promptName or sessionId is required');
Expand All @@ -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({
Expand Down Expand Up @@ -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 });
},
Expand All @@ -157,8 +145,6 @@ export function textToText({
return await client.chatText({
sessionId: message.sessionId,
messageId: message.messageId,
message: message.message,
params,
});
}),
]);
Expand All @@ -175,7 +161,6 @@ export function toImage({
content,
attachments,
params,
forceCreate,
timeout = TIMEOUT,
}: TextToTextOptions) {
return {
Expand All @@ -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' });
},
};
Expand Down