|
| 1 | +import { isArray } from 'lodash'; |
1 | 2 | import { Socket } from 'socket.io-client';
|
2 | 3 | import { Injectable, Logger } from '@nestjs/common';
|
3 | 4 | import { ClientContext, SessionMetadata } from 'src/common/models';
|
4 | 5 | import { AiQueryProvider } from 'src/modules/ai/query/providers/ai-query.provider';
|
5 | 6 | import { SendAiQueryMessageDto } from 'src/modules/ai/query/dto/send.ai-query.message.dto';
|
6 | 7 | import { wrapAiQueryError } from 'src/modules/ai/query/exceptions';
|
7 | 8 | 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'; |
9 | 10 | import { Response } from 'express';
|
10 | 11 | import {
|
11 | 12 | AiQueryMessage,
|
@@ -56,6 +57,24 @@ export class AiQueryService {
|
56 | 57 | return steps;
|
57 | 58 | }
|
58 | 59 |
|
| 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 | + |
59 | 78 | static prepareHistory(messages: AiQueryMessage[]): string[][] {
|
60 | 79 | const history = [];
|
61 | 80 | messages.forEach((message) => {
|
@@ -178,7 +197,7 @@ export class AiQueryService {
|
178 | 197 | socket.on(AiQueryWsEvents.TOOL_REPLY, async (data) => {
|
179 | 198 | answer.steps.push(plainToClass(AiQueryIntermediateStep, {
|
180 | 199 | type: AiQueryIntermediateStepType.TOOL,
|
181 |
| - data, |
| 200 | + data: AiQueryService.prepareToolReply(data), |
182 | 201 | }));
|
183 | 202 | });
|
184 | 203 |
|
|
0 commit comments