Skip to content

Commit

Permalink
fix(core): always create message for ai
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Apr 18, 2024
1 parent 9cb6dcd commit fb423a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 90 deletions.
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 @@ -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

0 comments on commit fb423a5

Please sign in to comment.