Skip to content

Commit

Permalink
Include cortexName and userEmail with results for listing chats/conte…
Browse files Browse the repository at this point in the history
…nt (#18)

* Include cortexName and userEmail with results for listing chats/content

* Enable filtering by email and cortex name

* update opts type for client
  • Loading branch information
jmoseley authored Jun 27, 2024
1 parent 6be3868 commit df1f447
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
32 changes: 23 additions & 9 deletions chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,20 @@ export interface ChatListItem {
title: string;
id: string;
messageCount: number;
userEmail?: string;
cortexName: string;
createdAt: string;
Chat(): Promise<Chat>;
}
export interface ChatListResult {
chats: ChatListItem[];
nextPage: () => Promise<ChatListResult>;
}
export interface ChatListPaginationOpts {
export interface ChatListOpts {
cursor?: string;
pageSize?: number;
userEmail?: string | null;
cortexName?: string;
}

export type Message = {
Expand Down Expand Up @@ -152,15 +157,23 @@ export class Chat {

static async list(
client: CortexApiClient,
paginationOpts?: ChatListPaginationOpts,
opts?: ChatListOpts,
): Promise<ChatListResult> {
const chats: ChatListItem[] = [];

const query = new URLSearchParams();
if (paginationOpts?.cursor) {
query.set("cursor", paginationOpts.cursor);
if (opts?.cursor) {
query.set("cursor", opts.cursor);
}
query.set("pageSize", (paginationOpts?.pageSize || 50).toString());
if (opts?.userEmail) {
query.set("userEmail", opts.userEmail);
} else if (opts?.userEmail === null) {
query.set("userEmail", "null");
}
if (opts?.cortexName) {
query.set("cortexName", opts.cortexName);
}
query.set("pageSize", (opts?.pageSize || 50).toString());
const res = await client.GET(`/chats?${query.toString()}`);
if (res.status !== 200) {
throw new Error(`Failed to list chats: ${res.statusText}`);
Expand All @@ -171,19 +184,20 @@ export class Chat {
title: chat.title,
id: chat.chatId,
messageCount: chat.messageCount,
userEmail: chat.userEmail,
cortexName: chat.cortexName,
createdAt: chat.createdAt,
Chat: () => {
return Chat.get(client, chat.chatId);
},
});
}

const cursor = body.cursor;
const pageSize = paginationOpts?.pageSize;
const pageSize = opts?.pageSize;
return {
chats,
nextPage: async () => {
return Chat.list(client, { cursor, pageSize });
},
nextPage: async () => Chat.list(client, { cursor, pageSize }),
};
}

Expand Down
16 changes: 10 additions & 6 deletions client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ export interface ClientCreateChatOptsSync extends ClientCreateChatOptsBase {
stream?: false;
}

export interface ClientListContentPaginationOpts {
export interface ClientListContentOpts {
pageSize?: number;
cursor?: string;
userEmail?: string | null;
cortexName?: string;
}
export interface ClientListChatPaginationOpts {
export interface ClientListChatOpts {
pageSize?: number;
cursor?: string;
userEmail?: string | null;
cortexName?: string;
}

const apiUrl = process.env.CORTEX_API_URL || "https://api.cortexclick.com";
Expand Down Expand Up @@ -123,11 +127,11 @@ export class CortexClient {
return Content.get(this.apiClient, id, version);
}

async listContent(paginationOptions?: ClientListContentPaginationOpts) {
return Content.list(this.apiClient, paginationOptions);
async listContent(options?: ClientListContentOpts) {
return Content.list(this.apiClient, options);
}
async listChats(paginationOptions?: ClientListChatPaginationOpts) {
return Chat.list(this.apiClient, paginationOptions);
async listChats(options?: ClientListChatOpts) {
return Chat.list(this.apiClient, options);
}

async getCortex(name: string): Promise<Cortex> {
Expand Down
32 changes: 23 additions & 9 deletions content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,21 @@ export type ContentListItem = {
title: string;
latestVersion: number;
id: string;
userEmail?: string;
cortexName: string;
createdAt: string;
Content(): Promise<Content>;
};

export type ContentListResult = {
nextPage: () => Promise<ContentListResult>;
content: ContentListItem[];
};
export type ContentListPaginationOptions = {
export type ContentListOptions = {
cursor?: string;
pageSize?: number;
userEmail?: string | null;
cortexName?: string;
};

export class Content {
Expand Down Expand Up @@ -322,15 +327,23 @@ export class Content {

static async list(
client: CortexApiClient,
paginationOpts?: ContentListPaginationOptions,
opts?: ContentListOptions,
): Promise<ContentListResult> {
const contentList: ContentListItem[] = [];

const query = new URLSearchParams();
if (paginationOpts?.cursor) {
query.set("cursor", paginationOpts.cursor);
if (opts?.cursor) {
query.set("cursor", opts.cursor);
}
query.set("pageSize", (paginationOpts?.pageSize || 50).toString());
if (opts?.userEmail) {
query.set("userEmail", opts.userEmail);
} else if (opts?.userEmail === null) {
query.set("userEmail", "null");
}
if (opts?.cortexName) {
query.set("cortexName", opts.cortexName);
}
query.set("pageSize", (opts?.pageSize || 50).toString());
const res = await client.GET(`/content?${query.toString()}`);
if (res.status !== 200) {
throw new Error(`Failed to list content: ${res.statusText}`);
Expand All @@ -341,19 +354,20 @@ export class Content {
title: content.title,
latestVersion: content.latestVersion,
id: content.contentId,
userEmail: content.userEmail,
cortexName: content.cortexName,
createdAt: content.createdAt,
Content: () => {
return Content.get(client, content.contentId);
},
});
}

const cursor = body.cursor;
const pageSize = paginationOpts?.pageSize;
const pageSize = opts?.pageSize;
return {
content: contentList,
nextPage: async () => {
return Content.list(client, { cursor, pageSize });
},
nextPage: async () => Content.list(client, { cursor, pageSize }),
};
}
}
Expand Down

0 comments on commit df1f447

Please sign in to comment.