From 1115d5b8590966241302c6814cab75cef910899e Mon Sep 17 00:00:00 2001 From: Jeremy Moseley Date: Mon, 8 Jul 2024 20:49:24 -0600 Subject: [PATCH] Add support for externalUserId for chats. --- chat.test.ts | 5 ++++- chat.ts | 31 +++++++++++++++++++++++++++---- client.ts | 1 + cortex.ts | 3 +++ 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/chat.test.ts b/chat.test.ts index 6a709f2..b83ef51 100644 --- a/chat.test.ts +++ b/chat.test.ts @@ -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" }); @@ -131,6 +132,7 @@ test("streaming chat", { timeout: 60000 }, async () => { message: chatInput, stream: true, statusStream, + externalUserId: "123", }); let fullMessage = ""; @@ -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 diff --git a/chat.ts b/chat.ts index 23f4b4d..660f1ed 100644 --- a/chat.ts +++ b/chat.ts @@ -9,6 +9,7 @@ export interface CreateChatOptsBase { message: string; stream?: boolean; statusStream?: Readable; + externalUserId?: string; } export interface CreateChatOptsStreaming extends CreateChatOptsBase { @@ -46,6 +47,7 @@ export interface ChatListItem { userEmail?: string; cortexName: string; createdAt: string; + externalUserId?: string; Chat(): Promise; } export interface ChatListResult { @@ -56,6 +58,7 @@ export interface ChatListOpts { cursor?: string; pageSize?: number; userEmail?: string | null; + externalUserId?: string | null; cortexName?: string; } @@ -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; @@ -91,8 +95,12 @@ export class Chat { private static async createContentSync( opts: CreateChatOptsSync, ): Promise { - 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[] = [ { @@ -111,17 +119,19 @@ export class Chat { messages, body.createdAt, body.userEmail, + body.externalUserId, ); } private static async createContentStreaming( opts: CreateChatOptsStreaming, ): Promise { - 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"); @@ -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 }; @@ -170,6 +188,7 @@ export class Chat { body.messages, body.createdAt, body.userEmail, + body.externalUserId, ); } @@ -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) { @@ -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); }, diff --git a/client.ts b/client.ts index 28ee0a2..6da4c46 100644 --- a/client.ts +++ b/client.ts @@ -54,6 +54,7 @@ export interface ClientListChatOpts { pageSize?: number; cursor?: string; userEmail?: string | null; + externalUserId?: string | null; cortexName?: string; } diff --git a/cortex.ts b/cortex.ts index 2a91ba0..4d5ec9b 100644 --- a/cortex.ts +++ b/cortex.ts @@ -90,6 +90,7 @@ export interface CortexCreateChatOptsBase { message: string; stream?: boolean; statusStream?: Readable; + externalUserId?: string; } export interface CortexCreateChatOptsStreaming @@ -192,6 +193,7 @@ export class Cortex { message: opts.message, statusStream: opts.statusStream, stream: true, + externalUserId: opts.externalUserId, }); } else { return Chat.create({ @@ -200,6 +202,7 @@ export class Cortex { message: opts.message, statusStream: opts.statusStream, stream: false, + externalUserId: opts.externalUserId, }); } }