From 3f76ff3513b8f7f9b290db5398425b0e93df5dd0 Mon Sep 17 00:00:00 2001 From: Lokesh Goel Date: Wed, 3 Jan 2024 14:37:45 +0530 Subject: [PATCH] update cache service to not use ts maps --- src/commands/iq/chat/intents/actionIntent.ts | 3 +- .../iq/chat/intents/collectionSchemaIntent.ts | 11 +-- src/commands/iq/chat/iqChatHandler.ts | 8 +- src/reactViews/iq/pages/chatscreen/IqChat.tsx | 2 +- src/reactViews/iq/pages/login/LoginPage.scss | 2 +- src/util/cacheService/cacheService.ts | 76 +++++++++---------- 6 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/commands/iq/chat/intents/actionIntent.ts b/src/commands/iq/chat/intents/actionIntent.ts index cbdd79cd..f6f0b14d 100644 --- a/src/commands/iq/chat/intents/actionIntent.ts +++ b/src/commands/iq/chat/intents/actionIntent.ts @@ -4,8 +4,9 @@ import { availableActions } from "../utils"; export const actionIntenthandler = async (jsonObject: any, webview: WebviewView) => { - const actions = jsonObject?.actions; + const actions = jsonObject?.actions || []; const resultingActions: string[] = []; + if(actions.length === 0){ return resultingActions; } diff --git a/src/commands/iq/chat/intents/collectionSchemaIntent.ts b/src/commands/iq/chat/intents/collectionSchemaIntent.ts index f2c99923..56871490 100644 --- a/src/commands/iq/chat/intents/collectionSchemaIntent.ts +++ b/src/commands/iq/chat/intents/collectionSchemaIntent.ts @@ -1,7 +1,8 @@ -import { ISchemaPatternCache, ISchemaCache, CacheService } from "../../../../util/cacheService/cacheService"; +import { logger } from "../../../../logger/logger"; +import { ISchemaCache, CacheService } from "../../../../util/cacheService/cacheService"; import { IAdditionalContext } from "../types"; -const schemaPatternStringify = (schemaPattern: ISchemaPatternCache, indentLevel: number = 0) => { +const schemaPatternStringify = (schemaPattern: any, indentLevel: number = 0) => { let result = ''; const indent = '-'.repeat(indentLevel * 2); for (let [key, value] of schemaPattern.schemaNode) { @@ -31,7 +32,7 @@ export const collectionSchemaHandler = async (jsonObject: any, response: IAdditi if (collections.length === 0) { // No Collections found, returning return; } - + logger.info("getting collections data intent for " + collections); let schemas = []; for (let collection of collections) { let scope = ""; @@ -42,7 +43,7 @@ export const collectionSchemaHandler = async (jsonObject: any, response: IAdditi if (schema !== undefined) { // found correct pair, we can return the schema const stringifiedSchema = { - schema: schemaStringify(schema), + schema: JSON.stringify(schema), collection: `${scope}.${col}` }; schemas.push(stringifiedSchema); @@ -53,7 +54,7 @@ export const collectionSchemaHandler = async (jsonObject: any, response: IAdditi if (schema !== undefined) { // found correct pair, we can return the schema const stringifiedSchema = { - schema: schemaStringify(schema), + schema: JSON.stringify(schema), collection: col }; schemas.push(stringifiedSchema); diff --git a/src/commands/iq/chat/iqChatHandler.ts b/src/commands/iq/chat/iqChatHandler.ts index c8c72e40..d8891735 100644 --- a/src/commands/iq/chat/iqChatHandler.ts +++ b/src/commands/iq/chat/iqChatHandler.ts @@ -37,12 +37,12 @@ const getIntentOrResponse = async (userRequest: string, jwtToken: string, orgId: 6 - If you did not extract any entities above, simply respond to the user question. Don't send any JSON in this case - `; // TODO: Update all available collections + `; let codeSelected = `The user has the following code selected: `; let codeSelectedAvailable: boolean = false; - // TODO: update based on code selected + let config = vscode.workspace.getConfiguration('couchbase'); const editor = vscode.window.activeTextEditor; @@ -60,7 +60,7 @@ const getIntentOrResponse = async (userRequest: string, jwtToken: string, orgId: ${userRequest} `; - let finalContent = basicQuestion + userQuestion + (codeSelectedAvailable ? codeSelected : ""); // TODO: update code selected as well in this + let finalContent = basicQuestion + userQuestion + (codeSelectedAvailable ? codeSelected : ""); previousMessages.allChats = [...previousMessages.allChats, { role: "user", @@ -95,7 +95,7 @@ const getFinalResponse = async (message: string, additionalContext: IAdditionalC additionalContextPrompt += "the schema for collection " + schema.collection + " is " + schema.schema + "\n"; } - const finalContent = basicPrompt + additionalContextPrompt + `Please focus now on answering the question that is: ${message}`; + const finalContent = basicPrompt + additionalContextPrompt + `Please focus now on answering the question and do not return JSON unless specified in the question. \n ${message}`; previousMessages.allChats = [...previousMessages.allChats, { role: "user", diff --git a/src/reactViews/iq/pages/chatscreen/IqChat.tsx b/src/reactViews/iq/pages/chatscreen/IqChat.tsx index c5dbe492..b05b604a 100644 --- a/src/reactViews/iq/pages/chatscreen/IqChat.tsx +++ b/src/reactViews/iq/pages/chatscreen/IqChat.tsx @@ -247,7 +247,7 @@ const IqChat = ({ org }) => { sender: "assistant", feedbackSent: false, msgDate: message.msgDate, - qaId: message.qaId, // TODO: get qaId + qaId: message.qaId, }; const updatedMessages = [...messages.userChats, newMessage]; diff --git a/src/reactViews/iq/pages/login/LoginPage.scss b/src/reactViews/iq/pages/login/LoginPage.scss index cfb4096f..bd80738a 100644 --- a/src/reactViews/iq/pages/login/LoginPage.scss +++ b/src/reactViews/iq/pages/login/LoginPage.scss @@ -114,7 +114,7 @@ .divider { display: inline-block; - width: 40%; + width: min(40%,calc(50% - 35px)); height: 1px; background-color: var( --vscode-settings-sashBorder diff --git a/src/util/cacheService/cacheService.ts b/src/util/cacheService/cacheService.ts index a3f9b7da..bcae4468 100644 --- a/src/util/cacheService/cacheService.ts +++ b/src/util/cacheService/cacheService.ts @@ -5,14 +5,14 @@ import { hasQueryService } from "../common"; import { Global } from "../util"; import { QueryIndex } from "couchbase"; -type schemaPatternType = ISchemaPatternCache | string; - -export interface ISchemaPatternCache { - schemaNode: Map; -} +// type schemaPatternType = ISchemaPatternCache | string; +// export interface ISchemaPatternCache { +// schemaNode: any; +// } +export type SchemaCacheType = { [index: string]: any }; export interface ISchemaCache { - patterns: ISchemaPatternCache[]; + patterns: SchemaCacheType[]; } export interface ICollectionCache { @@ -41,27 +41,27 @@ export class CacheService { CacheService() { } - private schemaTreeTraversal(treeNode: any): ISchemaPatternCache { + private schemaTreeTraversal(treeNode: any): SchemaCacheType { if (!treeNode) { - return { schemaNode: new Map() }; + return {}; } - const currentNodes = new Map(); + const currentNodes: SchemaCacheType = {}; Object.entries(treeNode).map(property => { const propertyValue: any = property[1]; const type = propertyValue.type; if (type === 'object') { const children = this.schemaTreeTraversal(propertyValue.properties); - currentNodes.set(`${property[0]}`, children); + currentNodes[property[0]] = children; } else if (type === 'array') { try { const items = propertyValue.items; const itemType = items.type; if (itemType === 'object') { const children = this.schemaTreeTraversal(items.properties); - currentNodes.set(`${property[0]}`, children); + currentNodes[property[0]] = children; } else { - currentNodes.set(`${property[0]}`, `array of ${itemType}`); + currentNodes[property[0]] = `array of ${itemType}`; } } catch (error) { logger.error(`Error processing array type for ${property[0]}: ${error}`); @@ -71,13 +71,13 @@ export class CacheService { try { let currentType: string = type.toString(); currentType = currentType.replace(',', " | "); - currentNodes.set(`${property[0]}`, currentType); + currentNodes[property[0]] = currentType; } catch (e) { logger.error("Type can't be stringified: " + e); } } }); - return { schemaNode: currentNodes }; + return currentNodes; } public cacheSchemaForCollection = async (connection: IConnection, collection: ICollectionCache) => { @@ -86,7 +86,7 @@ export class CacheService { const result = await connection?.cluster?.query(query); const patternCnt: number = result?.rows[0].length || 0; - const schemaPatternData: ISchemaPatternCache[] = []; + const schemaPatternData: SchemaCacheType[] = []; for (let i = 0; i < patternCnt; i++) { const row = result?.rows[0][i]; const childrenNode = this.schemaTreeTraversal(row.properties); @@ -100,7 +100,7 @@ export class CacheService { public async cacheIndexesForCollection(connection: IConnection, collection: ICollectionCache): Promise { const indexesResult = await connection?.cluster?.queryIndexes().getAllIndexes(collection.bucketName, { scopeName: collection.scopeName, collectionName: collection.name }); - collection.indexes = indexesResult?.map((index)=>{ + collection.indexes = indexesResult?.map((index) => { return JSON.stringify(index); }); return indexesResult || []; @@ -112,7 +112,7 @@ export class CacheService { for (let [_, scope] of bucket.scopes) { for (let [_, collection] of scope.collections) { const indexes = await this.cacheIndexesForCollection(connection, collection); - if(indexes.length > 0){ // Only cache schema + if (indexes.length > 0) { // Only cache schema await this.cacheSchemaForCollection(connection, collection); } }; @@ -176,7 +176,8 @@ export class CacheService { if (connection) { if (!forcedCacheUpdate) { const isCacheSuccessful = await this.loadCache(connection); - if(isCacheSuccessful){ + if (isCacheSuccessful) { + this.cacheStatus = true; return; } } @@ -212,12 +213,12 @@ export class CacheService { }; - public getCollectionSchemaWithCollectionName = async(collectionName: string):Promise => { + public getCollectionSchemaWithCollectionName = async (collectionName: string): Promise => { if (this.cacheStatus === false) { return undefined; } for await (let [_, bucketCache] of this.bucketsData) { - for await (let [_, scopeCache] of bucketCache.scopes){ + for await (let [_, scopeCache] of bucketCache.scopes) { const collections = scopeCache.collections; if (collections !== undefined) { const collectionData = collections.get(collectionName); @@ -237,11 +238,10 @@ export class CacheService { } const allCollectionList = []; for await (let [_, bucketCache] of this.bucketsData) { - for await (let [_, scopeCache] of bucketCache.scopes){ - for await (let [_, collectionCache] of scopeCache.collections){ + for await (let [_, scopeCache] of bucketCache.scopes) { + for await (let [_, collectionCache] of scopeCache.collections) { allCollectionList.push(`${collectionCache.scopeName}.${collectionCache.name}`); } - } } return allCollectionList; @@ -249,14 +249,9 @@ export class CacheService { private serializeSchema(schema: ISchemaCache): string { // Convert nested Maps to plain objects for JSON - const patternsJson = JSON.stringify(schema.patterns.map((pattern) => { - if (typeof pattern === "string") { - return pattern; - } - return { schemaNode: Object.entries(pattern.schemaNode) }; - })); - - return JSON.stringify({ patterns: patternsJson }); + const patternsJson = JSON.stringify(schema.patterns); + return patternsJson; + //return JSON.stringify({ patterns: patternsJson }); } public async storeCache(connection: IConnection): Promise { @@ -298,7 +293,7 @@ export class CacheService { if (!storedDataJson) { return false; // No existing cache } - + logger.info("Loading existing cache"); const storedData = JSON.parse(storedDataJson); this.bucketsData.clear(); for (const bucketName in storedData) { @@ -308,9 +303,10 @@ export class CacheService { scopes: new Map(), }; this.bucketsData.set(bucketName, bucket); + logger.info("loading cache from bucket: "+ bucketName); for (const scopeName in storedData[bucketName].scopes) { - const scope:IScopeCache = { + const scope: IScopeCache = { name: storedData[bucketName].scopes[scopeName].name, bucketName: bucketName, collections: new Map(), @@ -318,6 +314,7 @@ export class CacheService { bucket.scopes.set(scopeName, scope); for (const collectionName in storedData[bucketName].scopes[scopeName].collections) { + console.log(bucketName,scopeName, collectionName); const collection: ICollectionCache = { name: storedData[bucketName].scopes[scopeName].collections[collectionName].name, schema: storedData[bucketName].scopes[scopeName].collections[collectionName].schema @@ -335,17 +332,18 @@ export class CacheService { } private deserializeSchema(storedSchemaJson: string): ISchemaCache | undefined { - if (!storedSchemaJson) { + + + if (!storedSchemaJson) { return undefined; } - const storedSchema = JSON.parse(storedSchemaJson); - const patterns = JSON.parse(storedSchema.patterns); + const patterns = JSON.parse(storedSchemaJson); return { - patterns: patterns.map((pattern: ISchemaPatternCache) => { - return { schemaNode: new Map(Object.entries(pattern.schemaNode)) }; // Convert empty array to Map + patterns: patterns.map((pattern: any):SchemaCacheType => { + return pattern; // Convert empty array to Map }), - }; + }; } } \ No newline at end of file