forked from Azure-Samples/azure-search-openai-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCosmosDB.ts
51 lines (43 loc) · 1.72 KB
/
CosmosDB.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { IHistoryProvider, Answers, HistoryProviderOptions, HistoryMetaData } from "./IProvider";
import { deleteChatHistoryApi, getChatHistoryApi, getChatHistoryListApi, postChatHistoryApi } from "../../api";
export class CosmosDBProvider implements IHistoryProvider {
getProviderName = () => HistoryProviderOptions.CosmosDB;
private continuationToken: string | undefined;
private isItemEnd: boolean = false;
resetContinuationToken() {
this.continuationToken = undefined;
this.isItemEnd = false;
}
async getNextItems(count: number, idToken?: string): Promise<HistoryMetaData[]> {
if (this.isItemEnd) {
return [];
}
try {
const response = await getChatHistoryListApi(count, this.continuationToken, idToken || "");
this.continuationToken = response.continuation_token;
if (!this.continuationToken) {
this.isItemEnd = true;
}
return response.sessions.map(session => ({
id: session.id,
title: session.title,
timestamp: session.timestamp
}));
} catch (e) {
console.error(e);
return [];
}
}
async addItem(id: string, answers: Answers, idToken?: string): Promise<void> {
await postChatHistoryApi({ id, answers }, idToken || "");
return;
}
async getItem(id: string, idToken?: string): Promise<Answers | null> {
const response = await getChatHistoryApi(id, idToken || "");
return response.answers || null;
}
async deleteItem(id: string, idToken?: string): Promise<void> {
await deleteChatHistoryApi(id, idToken || "");
return;
}
}