Skip to content

Commit

Permalink
Add support for externalUserId for chats.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoseley committed Jul 9, 2024
1 parent d1b070b commit 1115d5b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
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");

Check failure on line 62 in chat.test.ts

View workflow job for this annotation

GitHub Actions / test

chat.test.ts > e2e catalog, cortex, and sync chat

AssertionError: expected undefined to be '123' // Object.is equality - Expected: "123" + Received: undefined ❯ chat.test.ts:62:37

// 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 | null;
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 | null;
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

0 comments on commit 1115d5b

Please sign in to comment.