diff --git a/backend/package.json b/backend/package.json index 36ba56027..a12dac473 100644 --- a/backend/package.json +++ b/backend/package.json @@ -46,6 +46,7 @@ "@rocketadmin/shared-code": "workspace:*", "@sentry/minimal": "^6.19.7", "@sentry/node": "10.39.0", + "@toon-format/toon": "^2.1.0", "@types/crypto-js": "^4.2.2", "@types/jsonwebtoken": "^9.0.10", "@types/multer": "^2.0.0", diff --git a/backend/src/ai-core/tools/prompts.ts b/backend/src/ai-core/tools/prompts.ts index 9e8a9b13b..62ce9778b 100644 --- a/backend/src/ai-core/tools/prompts.ts +++ b/backend/src/ai-core/tools/prompts.ts @@ -14,6 +14,8 @@ Table name: "${tableName}". ${schema ? `Schema: "${schema}".` : ''} Current date and time: ${currentDatetime} +Tool responses are encoded in TOON (Token-Oriented Object Notation) format - a compact, human-readable format similar to YAML with CSV-style tabular arrays. Parse it naturally. + Please follow these steps EXACTLY: 1. First, always use the getTableStructure tool to analyze the table schema and understand available columns 2. If the question requires data from related tables, note their relationships diff --git a/backend/src/ai-core/utils/index.ts b/backend/src/ai-core/utils/index.ts index e4be3d96f..1768a7c89 100644 --- a/backend/src/ai-core/utils/index.ts +++ b/backend/src/ai-core/utils/index.ts @@ -1 +1,2 @@ export * from './message-builder.js'; +export * from './toon-encoder.js'; diff --git a/backend/src/ai-core/utils/toon-encoder.ts b/backend/src/ai-core/utils/toon-encoder.ts new file mode 100644 index 000000000..84b6c8f16 --- /dev/null +++ b/backend/src/ai-core/utils/toon-encoder.ts @@ -0,0 +1,9 @@ +import { encode as toonEncode } from '@toon-format/toon'; + +export function encodeToToon(data: unknown): string { + return toonEncode(data); +} + +export function encodeError(error: { error: string }): string { + return JSON.stringify(error); +} diff --git a/backend/src/entities/ai/use-cases/request-info-from-table-with-ai-v7.use.case.ts b/backend/src/entities/ai/use-cases/request-info-from-table-with-ai-v7.use.case.ts index 4d4d21e88..92c79f03e 100644 --- a/backend/src/entities/ai/use-cases/request-info-from-table-with-ai-v7.use.case.ts +++ b/backend/src/entities/ai/use-cases/request-info-from-table-with-ai-v7.use.case.ts @@ -14,6 +14,8 @@ import { AIToolDefinition, createDatabaseQuerySystemPrompt, createDatabaseTools, + encodeError, + encodeToToon, isValidMongoDbCommand, isValidSQLQuery, MessageBuilder, @@ -243,7 +245,7 @@ export class RequestInfoFromTableWithAIUseCaseV7 userEmail, foundConnection, ); - result = JSON.stringify(structureInfo); + result = encodeToToon(structureInfo); break; } @@ -259,7 +261,7 @@ export class RequestInfoFromTableWithAIUseCaseV7 } const wrappedQuery = wrapQueryWithLimit(query, foundConnection.type as ConnectionTypesEnum); const queryResult = await dataAccessObject.executeRawQuery(wrappedQuery, inputTableName, userEmail); - result = JSON.stringify(queryResult); + result = encodeToToon(queryResult); break; } @@ -274,15 +276,15 @@ export class RequestInfoFromTableWithAIUseCaseV7 ); } const pipelineResult = await dataAccessObject.executeRawQuery(pipeline, inputTableName, userEmail); - result = JSON.stringify(pipelineResult); + result = encodeToToon(pipelineResult); break; } default: - result = JSON.stringify({ error: `Unknown tool: ${toolCall.name}` }); + result = encodeError({ error: `Unknown tool: ${toolCall.name}` }); } } catch (error) { - result = JSON.stringify({ error: error.message }); + result = encodeError({ error: error.message }); } results.push({ toolCallId: toolCall.id, result }); diff --git a/yarn.lock b/yarn.lock index 9843578ee..5cdcaedb8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5231,6 +5231,13 @@ __metadata: languageName: node linkType: hard +"@toon-format/toon@npm:^2.1.0": + version: 2.1.0 + resolution: "@toon-format/toon@npm:2.1.0" + checksum: 79d3595d4806cf16ce7a4961fdcbb8a9afe4583ecd6297e0d4cbcd07c5ef2a7ee60665a5d82ce07cbe3dd36c22ee54fa515968b12afa04c74df449baee28fb6a + languageName: node + linkType: hard + "@tsconfig/node10@npm:^1.0.7": version: 1.0.12 resolution: "@tsconfig/node10@npm:1.0.12" @@ -6382,6 +6389,7 @@ __metadata: "@rocketadmin/shared-code": "workspace:*" "@sentry/minimal": ^6.19.7 "@sentry/node": 10.39.0 + "@toon-format/toon": ^2.1.0 "@types/bcrypt": ^6.0.0 "@types/body-parser": ^1.19.6 "@types/cookie-parser": ^1.4.10