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

Add support for externalUserId for chats. #25

Merged
merged 3 commits into from
Jul 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ test("e2e catalog, cortex, and sync chat", { timeout: 60000 }, async () => {

// create chat
const chatInput = "what customers does cortex click have?";
const chat = await cortex.chat({ message: chatInput });
const chat = await cortex.chat({ message: chatInput, externalUserId: "123" });
expect(chat.messages[1].message.length).toBeGreaterThan(0);

// get chat
const getChatRes = await testClient.getChat(chat.id);
expect(getChatRes.messages.length).toBe(2);
expect(getChatRes.title).toBe(chatInput);
expect(getChatRes.externalUserId).toBe("123");

// respond to chat
await chat.respond({ message: "what about customer verticals" });
Expand Down Expand Up @@ -131,6 +132,7 @@ test("streaming chat", { timeout: 60000 }, async () => {
message: chatInput,
stream: true,
statusStream,
externalUserId: "123",
});

let fullMessage = "";
Expand Down Expand Up @@ -158,6 +160,7 @@ test("streaming chat", { timeout: 60000 }, async () => {
chatResult.messages[chatResult.messages.length - 1].message,
);
expect(chatResult.messages.length).toBe(2);
expect(chatResult.externalUserId).toBe("123");
expect(sawChat).toBe(true);

// respond to chat
Expand Down
31 changes: 27 additions & 4 deletions chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface CreateChatOptsBase {
message: string;
stream?: boolean;
statusStream?: Readable;
externalUserId?: string;
}

export interface CreateChatOptsStreaming extends CreateChatOptsBase {
Expand Down Expand Up @@ -46,6 +47,7 @@ export interface ChatListItem {
userEmail?: string;
cortexName: string;
createdAt: string;
externalUserId?: string;
Chat(): Promise<Chat>;
}
export interface ChatListResult {
Expand All @@ -56,6 +58,7 @@ export interface ChatListOpts {
cursor?: string;
pageSize?: number;
userEmail?: string | null;
externalUserId?: string;
cortexName?: string;
}

Expand All @@ -72,6 +75,7 @@ export class Chat {
readonly messages: Message[],
readonly createdAt: string,
readonly userEmail?: string,
readonly externalUserId?: string,
) {}

static async create(opts: CreateChatOptsSync): Promise<Chat>;
Expand All @@ -91,8 +95,12 @@ export class Chat {
private static async createContentSync(
opts: CreateChatOptsSync,
): Promise<Chat> {
const { client, cortex, message } = opts;
const res = await client.POST(`/chats`, { cortex: cortex.name, message });
const { client, cortex, message, externalUserId } = opts;
const res = await client.POST(`/chats`, {
cortex: cortex.name,
message,
externalUserId,
});
const body = await res.json();
const messages: Message[] = [
{
Expand All @@ -111,17 +119,19 @@ export class Chat {
messages,
body.createdAt,
body.userEmail,
body.externalUserId,
);
}

private static async createContentStreaming(
opts: CreateChatOptsStreaming,
): Promise<StreamingChatResult> {
const { client, cortex, message } = opts;
const { client, cortex, message, externalUserId } = opts;
const res = await client.POST(`/chats`, {
cortex: cortex.name,
message,
stream: true,
externalUserId,
});
const reader = res.body!.getReader();
const decoder = new TextDecoder("utf-8");
Expand Down Expand Up @@ -151,7 +161,15 @@ export class Chat {
message: content,
},
];
return new Chat(client, id, title, messages, createdAt, userEmail);
return new Chat(
client,
id,
title,
messages,
createdAt,
userEmail,
externalUserId,
);
});

return { responseStream: readableStream, chat: chatPromise };
Expand All @@ -170,6 +188,7 @@ export class Chat {
body.messages,
body.createdAt,
body.userEmail,
body.externalUserId,
);
}

Expand All @@ -191,6 +210,9 @@ export class Chat {
if (opts?.cortexName) {
query.set("cortexName", opts.cortexName);
}
if (opts?.externalUserId) {
query.set("externalUserId", opts.externalUserId);
}
query.set("pageSize", (opts?.pageSize || 50).toString());
const res = await client.GET(`/chats?${query.toString()}`);
if (res.status !== 200) {
Expand All @@ -205,6 +227,7 @@ export class Chat {
userEmail: chat.userEmail,
cortexName: chat.cortexName,
createdAt: chat.createdAt,
externalUserId: chat.externalUserId,
Chat: () => {
return Chat.get(client, chat.chatId);
},
Expand Down
1 change: 1 addition & 0 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface ClientListChatOpts {
pageSize?: number;
cursor?: string;
userEmail?: string | null;
externalUserId?: string;
cortexName?: string;
}

Expand Down
3 changes: 3 additions & 0 deletions cortex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface CortexCreateChatOptsBase {
message: string;
stream?: boolean;
statusStream?: Readable;
externalUserId?: string;
}

export interface CortexCreateChatOptsStreaming
Expand Down Expand Up @@ -192,6 +193,7 @@ export class Cortex {
message: opts.message,
statusStream: opts.statusStream,
stream: true,
externalUserId: opts.externalUserId,
});
} else {
return Chat.create({
Expand All @@ -200,6 +202,7 @@ export class Cortex {
message: opts.message,
statusStream: opts.statusStream,
stream: false,
externalUserId: opts.externalUserId,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion indexers/shopify-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class ShopifyIndexer {
}

this.documents.push({
id: item.id,
id: item.id.toString(),
title: item.title,
description: this.stripHTML(item.body_html),
productType: item.product_type,
Expand Down