Skip to content

Commit f33ec92

Browse files
add double quotes for query when needed
1 parent 8bd6c79 commit f33ec92

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

redisinsight/api/config/production.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
join(os.homedir(), '.redisinsight-preview'),
2424
join(os.homedir(), '.redisinsight-v2'),
2525
process.env.RI_GUIDES_PATH || join(homedir, 'guides'),
26-
]
26+
],
2727
},
2828
server: {
2929
env: 'production',
@@ -40,5 +40,7 @@ export default {
4040
},
4141
ai: {
4242
convAiApiUrl: process.env.RI_AI_CONVAI_API_URL || 'https://redis.io/convai/api',
43+
querySocketUrl: process.env.RI_AI_QUERY_SOCKET_URL || 'https://app.redislabs.com',
44+
querySocketPath: process.env.RI_AI_QUERY_SOCKET_PATH || '/api/v1/cloud-copilot-service/socket.io/',
4345
},
4446
};

redisinsight/api/src/modules/ai/query/ai-query.service.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { isArray } from 'lodash';
12
import { Socket } from 'socket.io-client';
23
import { Injectable, Logger } from '@nestjs/common';
34
import { ClientContext, SessionMetadata } from 'src/common/models';
45
import { AiQueryProvider } from 'src/modules/ai/query/providers/ai-query.provider';
56
import { SendAiQueryMessageDto } from 'src/modules/ai/query/dto/send.ai-query.message.dto';
67
import { wrapAiQueryError } from 'src/modules/ai/query/exceptions';
78
import { DatabaseClientFactory } from 'src/modules/database/providers/database.client.factory';
8-
import { getFullDbContext, getIndexContext } from 'src/modules/ai/query/utils/context.util';
9+
import { getFullDbContext, getIndexContext, quotesIfNeeded } from 'src/modules/ai/query/utils/context.util';
910
import { Response } from 'express';
1011
import {
1112
AiQueryMessage,
@@ -56,6 +57,24 @@ export class AiQueryService {
5657
return steps;
5758
}
5859

60+
static prepareToolReply(toolReply: any) {
61+
try {
62+
const prepared = JSON.parse(toolReply);
63+
64+
if (prepared?.name === 'query' && prepared.content) {
65+
const query = JSON.parse(prepared.content);
66+
if (isArray(query)) {
67+
prepared.content.query = JSON.stringify(query.map(quotesIfNeeded));
68+
}
69+
return JSON.stringify(prepared);
70+
}
71+
} catch (e) {
72+
// ignore error
73+
}
74+
75+
return toolReply;
76+
}
77+
5978
static prepareHistory(messages: AiQueryMessage[]): string[][] {
6079
const history = [];
6180
messages.forEach((message) => {
@@ -178,7 +197,7 @@ export class AiQueryService {
178197
socket.on(AiQueryWsEvents.TOOL_REPLY, async (data) => {
179198
answer.steps.push(plainToClass(AiQueryIntermediateStep, {
180199
type: AiQueryIntermediateStepType.TOOL,
181-
data,
200+
data: AiQueryService.prepareToolReply(data),
182201
}));
183202
});
184203

redisinsight/api/src/modules/ai/query/utils/context.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type RedisClient = { sendCommand: (args: any, options: any) => Promise<any> };
99
const DOCUMENT_SAMPLES_PER_PREFIX = 5;
1010
const HSCAN_COUNT = 500;
1111

12-
const quotesIfNeeded = (str: string) => (str.indexOf(' ') > -1 ? JSON.stringify(str) : str);
12+
export const quotesIfNeeded = (str: string) => (str.indexOf(' ') > -1 ? JSON.stringify(str) : str);
1313

1414
// ====================================================================
1515
// Reply converter

0 commit comments

Comments
 (0)