@solidstarters/solid-core 1.2.179 → 1.2.180
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controllers/service.controller.d.ts +8 -1
- package/dist/controllers/service.controller.d.ts.map +1 -1
- package/dist/controllers/service.controller.js +39 -2
- package/dist/controllers/service.controller.js.map +1 -1
- package/dist/helpers/date.helper.d.ts +2 -0
- package/dist/helpers/date.helper.d.ts.map +1 -0
- package/dist/helpers/date.helper.js +18 -0
- package/dist/helpers/date.helper.js.map +1 -0
- package/dist/jobs/database/trigger-mcp-client-subscriber-database.service.d.ts.map +1 -1
- package/dist/jobs/database/trigger-mcp-client-subscriber-database.service.js +18 -14
- package/dist/jobs/database/trigger-mcp-client-subscriber-database.service.js.map +1 -1
- package/dist/services/ai-interaction.service.d.ts.map +1 -1
- package/dist/services/ai-interaction.service.js +11 -4
- package/dist/services/ai-interaction.service.js.map +1 -1
- package/dist/services/excel.service.js +2 -2
- package/dist/services/excel.service.js.map +1 -1
- package/dist/services/import-transaction.service.d.ts.map +1 -1
- package/dist/services/import-transaction.service.js +6 -3
- package/dist/services/import-transaction.service.js.map +1 -1
- package/dist/services/mcp-tool-response-handlers/solid-create-model-with-fields-mcp-tool-response-handler.service.d.ts.map +1 -1
- package/dist/services/mcp-tool-response-handlers/solid-create-model-with-fields-mcp-tool-response-handler.service.js +2 -2
- package/dist/services/mcp-tool-response-handlers/solid-create-model-with-fields-mcp-tool-response-handler.service.js.map +1 -1
- package/dist/services/mcp-tool-response-handlers/solid-create-module-mcp-tool-response-handler.service.js +2 -2
- package/dist/services/mcp-tool-response-handlers/solid-create-module-mcp-tool-response-handler.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -2
- package/src/controllers/service.controller.ts +40 -2
- package/src/helpers/date.helper.ts +13 -0
- package/src/jobs/database/trigger-mcp-client-subscriber-database.service.ts +18 -14
- package/src/services/ai-interaction.service.ts +14 -8
- package/src/services/excel.service.ts +2 -2
- package/src/services/import-transaction.service.ts +12 -4
- package/src/services/mcp-tool-response-handlers/solid-create-model-with-fields-mcp-tool-response-handler.service.ts +3 -2
- package/src/services/mcp-tool-response-handlers/solid-create-module-mcp-tool-response-handler.service.ts +2 -2
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { ErrorMapperService } from 'src/helpers/error-mapper.service';
|
|
2
2
|
import { ActiveUserData } from 'src/interfaces/active-user-data.interface';
|
|
3
3
|
import { AiInteractionService } from 'src/services/ai-interaction.service';
|
|
4
|
+
import { IngestMetadataService } from 'src/services/genai/ingest-metadata.service';
|
|
4
5
|
import { MqMessageService } from 'src/services/mq-message.service';
|
|
5
6
|
import { SolidRegistry } from '../helpers/solid-registry';
|
|
7
|
+
export interface PostProcessCodeGenConfig {
|
|
8
|
+
runModuleMetadataSeeder?: boolean;
|
|
9
|
+
runSolidIngestion?: boolean;
|
|
10
|
+
}
|
|
6
11
|
export declare class ServiceController {
|
|
7
12
|
private readonly solidRegistry;
|
|
8
13
|
private readonly aiInteractionService;
|
|
9
14
|
private readonly mqMessageService;
|
|
10
15
|
private readonly errorMapper;
|
|
16
|
+
private readonly ingestMetadataService;
|
|
11
17
|
private readonly logger;
|
|
12
|
-
constructor(solidRegistry: SolidRegistry, aiInteractionService: AiInteractionService, mqMessageService: MqMessageService, errorMapper: ErrorMapperService);
|
|
18
|
+
constructor(solidRegistry: SolidRegistry, aiInteractionService: AiInteractionService, mqMessageService: MqMessageService, errorMapper: ErrorMapperService, ingestMetadataService: IngestMetadataService);
|
|
13
19
|
pingPong(): {
|
|
14
20
|
pong: string;
|
|
15
21
|
};
|
|
@@ -24,5 +30,6 @@ export declare class ServiceController {
|
|
|
24
30
|
seedData(seedData: any): Promise<{
|
|
25
31
|
message: string;
|
|
26
32
|
}>;
|
|
33
|
+
postProcessCodeGeneration(config: PostProcessCodeGenConfig): Promise<void>;
|
|
27
34
|
}
|
|
28
35
|
//# sourceMappingURL=service.controller.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.controller.d.ts","sourceRoot":"","sources":["../../src/controllers/service.controller.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,2CAA2C,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"service.controller.d.ts","sourceRoot":"","sources":["../../src/controllers/service.controller.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,2CAA2C,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,qBAAqB,EAAE,MAAM,4CAA4C,CAAC;AACnF,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D,MAAM,WAAW,wBAAwB;IACrC,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC/B;AACD,qBAIa,iBAAiB;IAItB,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,oBAAoB;IACrC,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW;IAC5B,OAAO,CAAC,QAAQ,CAAC,qBAAqB;IAP1C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;gBAGxC,aAAa,EAAE,aAAa,EAC5B,oBAAoB,EAAE,oBAAoB,EAC1C,gBAAgB,EAAE,gBAAgB,EAClC,WAAW,EAAE,kBAAkB,EAC/B,qBAAqB,EAAE,qBAAqB;IAMjE,QAAQ;;;IAMF,WAAW,CAAe,UAAU,EAAE,cAAc;;;;;;;;IAyDpD,QAAQ,CAAS,QAAQ,EAAE,GAAG;;;IAiB9B,yBAAyB,CAAS,MAAM,EAAG,wBAAwB;CAgD5E"}
|
|
@@ -21,14 +21,16 @@ const active_user_decorator_1 = require("../decorators/active-user.decorator");
|
|
|
21
21
|
const public_decorator_1 = require("../decorators/public.decorator");
|
|
22
22
|
const error_mapper_service_1 = require("../helpers/error-mapper.service");
|
|
23
23
|
const ai_interaction_service_1 = require("../services/ai-interaction.service");
|
|
24
|
+
const ingest_metadata_service_1 = require("../services/genai/ingest-metadata.service");
|
|
24
25
|
const mq_message_service_1 = require("../services/mq-message.service");
|
|
25
26
|
const solid_registry_1 = require("../helpers/solid-registry");
|
|
26
27
|
let ServiceController = ServiceController_1 = class ServiceController {
|
|
27
|
-
constructor(solidRegistry, aiInteractionService, mqMessageService, errorMapper) {
|
|
28
|
+
constructor(solidRegistry, aiInteractionService, mqMessageService, errorMapper, ingestMetadataService) {
|
|
28
29
|
this.solidRegistry = solidRegistry;
|
|
29
30
|
this.aiInteractionService = aiInteractionService;
|
|
30
31
|
this.mqMessageService = mqMessageService;
|
|
31
32
|
this.errorMapper = errorMapper;
|
|
33
|
+
this.ingestMetadataService = ingestMetadataService;
|
|
32
34
|
this.logger = new common_1.Logger(ServiceController_1.name);
|
|
33
35
|
}
|
|
34
36
|
pingPong() {
|
|
@@ -83,6 +85,31 @@ let ServiceController = ServiceController_1 = class ServiceController {
|
|
|
83
85
|
await seeder.seed();
|
|
84
86
|
return { message: `seed data for ${seedData.seeder}` };
|
|
85
87
|
}
|
|
88
|
+
async postProcessCodeGeneration(config) {
|
|
89
|
+
config.runModuleMetadataSeeder = config.runModuleMetadataSeeder ?? true;
|
|
90
|
+
config.runSolidIngestion = config.runSolidIngestion ?? true;
|
|
91
|
+
if (config.runModuleMetadataSeeder) {
|
|
92
|
+
this.logger.debug(`Running the Module Metadata Seeder Service as part of post-process code generation`);
|
|
93
|
+
const seeder = this.solidRegistry
|
|
94
|
+
.getSeeders()
|
|
95
|
+
.filter((seeder) => seeder.name === 'ModuleMetadataSeederService')
|
|
96
|
+
.map((seeder) => seeder.instance)
|
|
97
|
+
.pop();
|
|
98
|
+
if (!seeder) {
|
|
99
|
+
this.logger.error(`Seeder service ModuleMetadataSeederService not found. Does your service have a seed() method?`);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
await seeder.seed();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this.logger.debug(`Skipping the Module Metadata Seeder Service as part of post-process code generation`);
|
|
107
|
+
}
|
|
108
|
+
if (config.runSolidIngestion) {
|
|
109
|
+
this.logger.debug(`Running the Solid ingestion command as part of post-process code generation`);
|
|
110
|
+
await this.ingestMetadataService.ingest();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
86
113
|
};
|
|
87
114
|
exports.ServiceController = ServiceController;
|
|
88
115
|
__decorate([
|
|
@@ -111,12 +138,22 @@ __decorate([
|
|
|
111
138
|
__metadata("design:paramtypes", [Object]),
|
|
112
139
|
__metadata("design:returntype", Promise)
|
|
113
140
|
], ServiceController.prototype, "seedData", null);
|
|
141
|
+
__decorate([
|
|
142
|
+
(0, swagger_1.ApiBearerAuth)("jwt"),
|
|
143
|
+
(0, common_1.Post)('code-generation/post-process'),
|
|
144
|
+
openapi.ApiResponse({ status: 201 }),
|
|
145
|
+
__param(0, (0, common_1.Body)()),
|
|
146
|
+
__metadata("design:type", Function),
|
|
147
|
+
__metadata("design:paramtypes", [Object]),
|
|
148
|
+
__metadata("design:returntype", Promise)
|
|
149
|
+
], ServiceController.prototype, "postProcessCodeGeneration", null);
|
|
114
150
|
exports.ServiceController = ServiceController = ServiceController_1 = __decorate([
|
|
115
151
|
(0, common_1.Controller)(''),
|
|
116
152
|
(0, swagger_1.ApiTags)("Common"),
|
|
117
153
|
__metadata("design:paramtypes", [solid_registry_1.SolidRegistry,
|
|
118
154
|
ai_interaction_service_1.AiInteractionService,
|
|
119
155
|
mq_message_service_1.MqMessageService,
|
|
120
|
-
error_mapper_service_1.ErrorMapperService
|
|
156
|
+
error_mapper_service_1.ErrorMapperService,
|
|
157
|
+
ingest_metadata_service_1.IngestMetadataService])
|
|
121
158
|
], ServiceController);
|
|
122
159
|
//# sourceMappingURL=service.controller.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.controller.js","sourceRoot":"","sources":["../../src/controllers/service.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAqE;AACrE,6CAAyD;AACzD,+EAAkE;AAClE,qEAAyD;AACzD,0EAAsE;AAEtE,+EAA2E;AAC3E,uEAAmE;AACnE,8DAA0D;AAOnD,IAAM,iBAAiB,yBAAvB,MAAM,iBAAiB;IAG1B,YACqB,aAA4B,EAC5B,oBAA0C,EAC1C,gBAAkC,EAClC,WAA+B;QAH/B,kBAAa,GAAb,aAAa,CAAe;QAC5B,yBAAoB,GAApB,oBAAoB,CAAsB;QAC1C,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,gBAAW,GAAX,WAAW,CAAoB;QANnC,WAAM,GAAG,IAAI,eAAM,CAAC,mBAAiB,CAAC,IAAI,CAAC,CAAC;IAOzD,CAAC;IAIL,QAAQ;QACJ,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC;IAC9B,CAAC;IAIK,AAAN,KAAK,CAAC,WAAW,CAAe,UAA0B;QAItD,MAAM,QAAQ,GAAG,eAAe,UAAU,CAAC,GAAG,EAAE,CAAC;QACjD,MAAM,GAAG,GAAE,EAAC,MAAM,EAAC,kBAAkB,EAAE,UAAU,EAAC,iBAAiB,EAAC,CAAA;QACpE,MAAM,EAAE,cAAc,EAAE,eAAe,EAAE,GAAG,MAAM,IAAI,CAAC,oBAAoB,CAAC,mBAAmB,CAC3F,G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AE,CAAC;IACd,IAAA,iBAAO,EAAC,QAAQ,CAAC;qCAOsB,8BAAa;QACN,6CAAoB;QACxB,qCAAgB;QACrB,yCAAkB;GAP3C,iBAAiB,CA4G7B","sourcesContent":["import { Body, Controller, Get, Logger, Post } from '@nestjs/common';\nimport { ApiBearerAuth, ApiTags } from '@nestjs/swagger';\nimport { ActiveUser } from 'src/decorators/active-user.decorator';\nimport { Public } from 'src/decorators/public.decorator';\nimport { ErrorMapperService } from 'src/helpers/error-mapper.service';\nimport { ActiveUserData } from 'src/interfaces/active-user-data.interface';\nimport { AiInteractionService } from 'src/services/ai-interaction.service';\nimport { MqMessageService } from 'src/services/mq-message.service';\nimport { SolidRegistry } from '../helpers/solid-registry';\n\n\n@Controller('')\n@ApiTags(\"Common\")\n// @UseGuards(ThrottlerGuard)\n// @SkipThrottle({ short: true, login: true, burst: true, sustained: true }) // Skip all\nexport class ServiceController {\n private readonly logger = new Logger(ServiceController.name);\n\n constructor(\n private readonly solidRegistry: SolidRegistry,\n private readonly aiInteractionService: AiInteractionService,\n private readonly mqMessageService: MqMessageService,\n private readonly errorMapper: ErrorMapperService\n ) { }\n\n @Public()\n @Get('ping')\n pingPong() {\n return { pong: 'v1.0.2' };\n }\n\n @ApiBearerAuth(\"jwt\")\n @Get('mcp/ping')\n async mcpPingPong(@ActiveUser() activeUser: ActiveUserData) {\n // TODO: do a MCP client invocation, wait for response and return.\n // If failure then decide shape to return.\n\n const threadId = `pingPongTxn-${activeUser.sub}`;\n const dto ={prompt:\"Can you do 1 + 1\", moduleName:\"solidCoreModule\"}\n const { queueMessageId, aiInteractionId } = await this.aiInteractionService.triggerMcpClientJob(\n dto,\n activeUser.sub,\n true,\n threadId\n );\n\n this.logger.debug(`mcp ping pong job triggered: queueMessageId=${queueMessageId}, aiInteractionId=${aiInteractionId}`);\n\n // Wait up to 2 minutes, start at 500ms poll, back off to max 2s, throw if failed:\n const result = await this.mqMessageService.waitForTerminalStatus(queueMessageId, {\n timeoutMs: 2 * 60 * 1000,\n intervalMs: 500,\n maxIntervalMs: 2000,\n throwOnFailure: false,\n });\n\n this.logger.debug(`mcp ping pong job finished with stage=${result.stage}`)\n\n this.logger.debug(`mcp ping pong trying to find genai (child) interaction for aiInteraction for id=${aiInteractionId}`)\n\n // @ts-ignore\n const genAiInteractions = await this.aiInteractionService.find({\n filters: {\n parentInteraction: {\n id: {\n $eq: aiInteractionId\n }\n }\n }\n });\n\n const genAiInteraction = genAiInteractions['records'][0];\n this.logger.debug(genAiInteraction.message);\n\n this.logger.debug(`identified gen-ai interaction with id=${genAiInteraction.id}`);\n this.logger.debug(`proceeding with applying the gen-ai interaction`)\n\n return {\n mcpPong: 'v1.0.2',\n genAiInteraction: {\n status: genAiInteraction.status,\n errorCode: genAiInteraction.status === 'failed' ? this.errorMapper.mapMessage(genAiInteraction.errorMessage, genAiInteraction.metadata) : '',\n errorMessage: genAiInteraction.errorMessage,\n }\n };\n }\n\n @Public()\n // @SkipThrottle({ short: false, login: true, burst: true, sustained: true }) //Enable the short throttle only\n @Post('seed')\n async seedData(@Body() seedData: any) {\n const seeder = this.solidRegistry\n .getSeeders()\n .filter((seeder) => seeder.name === seedData.seeder)\n .map((seeder) => seeder.instance)\n .pop();\n if (!seeder) {\n this.logger.error(`Seeder service ${seedData.seeder} not found. Does your service have a seed() method?`);\n return;\n }\n this.logger.log(`Running the seed() method for seeder :${seeder.constructor.name}`);\n await seeder.seed();\n return { message: `seed data for ${seedData.seeder}` };\n }\n\n // @Public()\n // @Get('play')\n // play() {\n // return this.solidRegistry.getControllers();\n // }\n\n // //This method identifies a provider as a seeder if it has a seed method i.e duck typing\n // private isSeeder(provider: InstanceWrapper) {\n // const { instance } = provider;\n // if (!instance) return false;\n\n // const seedMethod = this.metadataScanner\n // .getAllMethodNames(Object.getPrototypeOf(instance))\n // .find((methodName) => methodName === 'seed');\n // if (!seedMethod) return false;\n // return true;\n // }\n\n}\n"]}
|
|
1
|
+
{"version":3,"file":"service.controller.js","sourceRoot":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,CAAC,IAAI,EAAE,CAAC;YACxB,CAAC;QACL,CAAC;aAAM,CAAC;YACJ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,qFAAqF,CAAC,CAAC;QAC7G,CAAC;QAGD,IAAI,MAAM,CAAC,iBAAiB,EAAE,CAAC;YAC3B,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,6EAA6E,CAAC,CAAC;YACjG,MAAM,IAAI,CAAC,qBAAqB,CAAC,MAAM,EAAE,CAAC;QAC9C,CAAC;IACL,CAAC;CAqBJ,CAAA;AA9IY,8CAAiB;AAc1B;IAFC,IAAA,yBAAM,GAAE;IACR,IAAA,YAAG,EAAC,MAAM,CAAC;;;;;iDAGX;AAIK;IAFL,IAAA,uBAAa,EAAC,KAAK,CAAC;IACpB,IAAA,YAAG,EAAC,UAAU,CAAC;;IACG,WAAA,IAAA,kCAAU,GAAE,CAAA;;;;oDAoD9B;AAKK;IAHL,IAAA,yBAAM,GAAE;IAER,IAAA,aAAI,EAAC,MAAM,CAAC;;IACG,WAAA,IAAA,aAAI,GAAE,CAAA;;;;iDAarB;AAIK;IAFL,IAAA,uBAAa,EAAC,KAAK,CAAC;IACpB,IAAA,aAAI,EAAC,8BAA8B,CAAC;;IACJ,WAAA,IAAA,aAAI,GAAE,CAAA;;;;kEA2BtC;4BAzHQ,iBAAiB;IAJ7B,IAAA,mBAAU,EAAC,EAAE,CAAC;IACd,IAAA,iBAAO,EAAC,QAAQ,CAAC;qCAOsB,8BAAa;QACN,6CAAoB;QACxB,qCAAgB;QACrB,yCAAkB;QACR,+CAAqB;GARxD,iBAAiB,CA8I7B","sourcesContent":["import { Body, Controller, Get, Logger, Post } from '@nestjs/common';\nimport { ApiBearerAuth, ApiTags } from '@nestjs/swagger';\nimport { ActiveUser } from 'src/decorators/active-user.decorator';\nimport { Public } from 'src/decorators/public.decorator';\nimport { ErrorMapperService } from 'src/helpers/error-mapper.service';\nimport { ActiveUserData } from 'src/interfaces/active-user-data.interface';\nimport { AiInteractionService } from 'src/services/ai-interaction.service';\nimport { IngestMetadataService } from 'src/services/genai/ingest-metadata.service';\nimport { MqMessageService } from 'src/services/mq-message.service';\nimport { SolidRegistry } from '../helpers/solid-registry';\n\nexport interface PostProcessCodeGenConfig {\n runModuleMetadataSeeder?: boolean; // If true, regenerate module metadata\n runSolidIngestion?: boolean; // If true, run solid ingestion command\n}\n@Controller('')\n@ApiTags(\"Common\")\n// @UseGuards(ThrottlerGuard)\n// @SkipThrottle({ short: true, login: true, burst: true, sustained: true }) // Skip all\nexport class ServiceController {\n private readonly logger = new Logger(ServiceController.name);\n\n constructor(\n private readonly solidRegistry: SolidRegistry,\n private readonly aiInteractionService: AiInteractionService,\n private readonly mqMessageService: MqMessageService,\n private readonly errorMapper: ErrorMapperService,\n private readonly ingestMetadataService: IngestMetadataService,\n \n ) { }\n\n @Public()\n @Get('ping')\n pingPong() {\n return { pong: 'v1.0.2' };\n }\n\n @ApiBearerAuth(\"jwt\")\n @Get('mcp/ping')\n async mcpPingPong(@ActiveUser() activeUser: ActiveUserData) {\n // TODO: do a MCP client invocation, wait for response and return.\n // If failure then decide shape to return.\n\n const threadId = `pingPongTxn-${activeUser.sub}`;\n const dto ={prompt:\"Can you do 1 + 1\", moduleName:\"solidCoreModule\"}\n const { queueMessageId, aiInteractionId } = await this.aiInteractionService.triggerMcpClientJob(\n dto,\n activeUser.sub,\n true,\n threadId\n );\n\n this.logger.debug(`mcp ping pong job triggered: queueMessageId=${queueMessageId}, aiInteractionId=${aiInteractionId}`);\n\n // Wait up to 2 minutes, start at 500ms poll, back off to max 2s, throw if failed:\n const result = await this.mqMessageService.waitForTerminalStatus(queueMessageId, {\n timeoutMs: 2 * 60 * 1000,\n intervalMs: 500,\n maxIntervalMs: 2000,\n throwOnFailure: false,\n });\n\n this.logger.debug(`mcp ping pong job finished with stage=${result.stage}`)\n\n this.logger.debug(`mcp ping pong trying to find genai (child) interaction for aiInteraction for id=${aiInteractionId}`)\n\n // @ts-ignore\n const genAiInteractions = await this.aiInteractionService.find({\n filters: {\n parentInteraction: {\n id: {\n $eq: aiInteractionId\n }\n }\n }\n });\n\n const genAiInteraction = genAiInteractions['records'][0];\n this.logger.debug(genAiInteraction.message);\n\n this.logger.debug(`identified gen-ai interaction with id=${genAiInteraction.id}`);\n this.logger.debug(`proceeding with applying the gen-ai interaction`)\n\n return {\n mcpPong: 'v1.0.2',\n genAiInteraction: {\n status: genAiInteraction.status,\n errorCode: genAiInteraction.status === 'failed' ? this.errorMapper.mapMessage(genAiInteraction.errorMessage, genAiInteraction.metadata) : '',\n errorMessage: genAiInteraction.errorMessage,\n }\n };\n }\n\n @Public()\n // @SkipThrottle({ short: false, login: true, burst: true, sustained: true }) //Enable the short throttle only\n @Post('seed')\n async seedData(@Body() seedData: any) {\n const seeder = this.solidRegistry\n .getSeeders()\n .filter((seeder) => seeder.name === seedData.seeder)\n .map((seeder) => seeder.instance)\n .pop();\n if (!seeder) {\n this.logger.error(`Seeder service ${seedData.seeder} not found. Does your service have a seed() method?`);\n return;\n }\n this.logger.log(`Running the seed() method for seeder :${seeder.constructor.name}`);\n await seeder.seed();\n return { message: `seed data for ${seedData.seeder}` };\n }\n\n @ApiBearerAuth(\"jwt\")\n @Post('code-generation/post-process')\n async postProcessCodeGeneration(@Body() config : PostProcessCodeGenConfig) {\n // Set defaults if not provided\n config.runModuleMetadataSeeder = config.runModuleMetadataSeeder ?? true;\n config.runSolidIngestion = config.runSolidIngestion ?? true;\n\n // Run the Module Metadata Seeder Service\n if (config.runModuleMetadataSeeder) {\n this.logger.debug(`Running the Module Metadata Seeder Service as part of post-process code generation`);\n const seeder = this.solidRegistry\n .getSeeders()\n .filter((seeder) => seeder.name === 'ModuleMetadataSeederService')\n .map((seeder) => seeder.instance)\n .pop();\n if (!seeder) {\n this.logger.error(`Seeder service ModuleMetadataSeederService not found. Does your service have a seed() method?`);\n } else {\n await seeder.seed();\n }\n } else {\n this.logger.debug(`Skipping the Module Metadata Seeder Service as part of post-process code generation`);\n }\n \n // Run the Solid ingestion command\n if (config.runSolidIngestion) {\n this.logger.debug(`Running the Solid ingestion command as part of post-process code generation`);\n await this.ingestMetadataService.ingest();\n }\n }\n\n\n // @Public()\n // @Get('play')\n // play() {\n // return this.solidRegistry.getControllers();\n // }\n\n // //This method identifies a provider as a seeder if it has a seed method i.e duck typing\n // private isSeeder(provider: InstanceWrapper) {\n // const { instance } = provider;\n // if (!instance) return false;\n\n // const seedMethod = this.metadataScanner\n // .getAllMethodNames(Object.getPrototypeOf(instance))\n // .find((methodName) => methodName === 'seed');\n // if (!seedMethod) return false;\n // return true;\n // }\n\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date.helper.d.ts","sourceRoot":"","sources":["../../src/helpers/date.helper.ts"],"names":[],"mappings":"AAGA,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAQ1D"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.parseFlexibleDate = parseFlexibleDate;
|
|
7
|
+
const dayjs_1 = __importDefault(require("dayjs"));
|
|
8
|
+
function parseFlexibleDate(value) {
|
|
9
|
+
if (!value)
|
|
10
|
+
return null;
|
|
11
|
+
const formats = ['DD-MM-YYYY', 'YYYY-MM-DD'];
|
|
12
|
+
const parsed = (0, dayjs_1.default)(value, formats, true);
|
|
13
|
+
if (!parsed.isValid()) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return parsed.toDate();
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=date.helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date.helper.js","sourceRoot":"","sources":["../../src/helpers/date.helper.ts"],"names":[],"mappings":";;;;;AAGA,8CAQG;AAXH,kDAA0B;AAG1B,SAAgB,iBAAiB,CAAC,KAAa;IAC3C,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IACxB,MAAM,OAAO,GAAG,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;IAC7C,MAAM,MAAM,GAAG,IAAA,eAAK,EAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IAC3C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC;AACzB,CAAC","sourcesContent":["import dayjs from 'dayjs';\n\n\nexport function parseFlexibleDate(value: string): Date | null {\n if (!value) return null;\n const formats = ['DD-MM-YYYY', 'YYYY-MM-DD'];\n const parsed = dayjs(value, formats, true); // true = strict parsing\n if (!parsed.isValid()) {\n return null;\n }\n return parsed.toDate();\n }\n "]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trigger-mcp-client-subscriber-database.service.d.ts","sourceRoot":"","sources":["../../../src/jobs/database/trigger-mcp-client-subscriber-database.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,MAAM,yCAAyC,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC;AAErF,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,qBACa,kCAAmC,SAAQ,kBAAkB,CAAC,uBAAuB,CAAC;IAI3F,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB;IAC3C,QAAQ,CAAC,qBAAqB,EAAE,qBAAqB;IACrD,QAAQ,CAAC,MAAM,EAAE,aAAa;IAC9B,QAAQ,CAAC,oBAAoB,EAAE,oBAAoB;IANvD,OAAO,CAAC,QAAQ,CAAC,gCAAgC,CAAuD;gBAG3F,gBAAgB,EAAE,gBAAgB,EAClC,qBAAqB,EAAE,qBAAqB,EAC5C,MAAM,EAAE,aAAa,EACrB,oBAAoB,EAAE,oBAAoB;IAKvD,OAAO,IAAI,mBAAmB;IAM9B,mBAAmB,CAAC,UAAU,EAAE,WAAW;IA8BrC,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"trigger-mcp-client-subscriber-database.service.d.ts","sourceRoot":"","sources":["../../../src/jobs/database/trigger-mcp-client-subscriber-database.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,MAAM,yCAAyC,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EAAE,kBAAkB,EAAE,MAAM,iDAAiD,CAAC;AAErF,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,qBACa,kCAAmC,SAAQ,kBAAkB,CAAC,uBAAuB,CAAC;IAI3F,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB;IAC3C,QAAQ,CAAC,qBAAqB,EAAE,qBAAqB;IACrD,QAAQ,CAAC,MAAM,EAAE,aAAa;IAC9B,QAAQ,CAAC,oBAAoB,EAAE,oBAAoB;IANvD,OAAO,CAAC,QAAQ,CAAC,gCAAgC,CAAuD;gBAG3F,gBAAgB,EAAE,gBAAgB,EAClC,qBAAqB,EAAE,qBAAqB,EAC5C,MAAM,EAAE,aAAa,EACrB,oBAAoB,EAAE,oBAAoB;IAKvD,OAAO,IAAI,mBAAmB;IAM9B,mBAAmB,CAAC,UAAU,EAAE,WAAW;IA8BrC,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC,uBAAuB,CAAC;CAuIjE"}
|
|
@@ -88,25 +88,29 @@ let TriggerMcpClientSubscriberDatabase = TriggerMcpClientSubscriberDatabase_1 =
|
|
|
88
88
|
status: 'pending'
|
|
89
89
|
});
|
|
90
90
|
const finalPrompt = `
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
91
|
+
# User Prompt:
|
|
92
|
+
${prompt}
|
|
93
|
+
|
|
94
|
+
# System Instructions:
|
|
95
|
+
- aiInteractionId: ${genAiInteraction.id}
|
|
96
|
+
- moduleName:${message.payload.moduleName}
|
|
97
|
+
- You will be invoking tools if needed.
|
|
98
|
+
- If a tool is invoked, you must return **exactly** the raw output from the tool, without any json envelopes, additional formatting, commentary, or text.
|
|
99
|
+
- Do not wrap the result in quotes, JSON, or markdown fences.
|
|
100
|
+
- Do not explain what the result means.
|
|
101
|
+
|
|
102
|
+
# Past Interactions:
|
|
103
|
+
This section contains the last 10 interactions done between the human and LLM. These are sorted by oldest first.
|
|
104
|
+
Use these interactions to further identify concerns based on the current User Prompt.
|
|
105
|
+
|
|
106
|
+
`;
|
|
103
107
|
const aiResponse = await this.aiInteractionService.runMcpPrompt(finalPrompt);
|
|
104
108
|
this.triggerMcpClientSubscriberLogger.log(`aiResponse: `);
|
|
105
109
|
this.triggerMcpClientSubscriberLogger.log(JSON.stringify(aiResponse));
|
|
106
110
|
if (!aiResponse.success) {
|
|
107
111
|
this.triggerMcpClientSubscriberLogger.log(`Gen ai has returned with a false status code`);
|
|
108
|
-
const errorsStr = aiResponse.errors
|
|
109
|
-
const errorTrace = aiResponse.error_trace
|
|
112
|
+
const errorsStr = aiResponse.errors?.join('\n ');
|
|
113
|
+
const errorTrace = aiResponse.error_trace?.join('\n');
|
|
110
114
|
await this.aiInteractionService.update(genAiInteraction.id, {
|
|
111
115
|
errorMessage: `${errorsStr}\n\n${errorTrace}`,
|
|
112
116
|
modelUsed: aiResponse.model,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trigger-mcp-client-subscriber-database.service.js","sourceRoot":"","sources":["../../../src/jobs/database/trigger-mcp-client-subscriber-database.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAoD;AAGpD,0EAAqE;AACrE,sFAAgF;AAEhF,mGAAqF;AACrF,0GAA8E;AAC9E,kFAA2E;AAC3E,kEAA4D;AAGrD,IAAM,kCAAkC,0CAAxC,MAAM,kCAAmC,SAAQ,gDAA2C;IAG/F,YACa,gBAAkC,EAClC,qBAA4C,EAC5C,MAAqB,EACrB,oBAA0C;QAEnD,KAAK,CAAC,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,CAAC,CAAC;QAL9C,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,0BAAqB,GAArB,qBAAqB,CAAuB;QAC5C,WAAM,GAAN,MAAM,CAAe;QACrB,yBAAoB,GAApB,oBAAoB,CAAsB;QANtC,qCAAgC,GAAG,IAAI,eAAM,CAAC,oCAAkC,CAAC,IAAI,CAAC,CAAC;IASxG,CAAC;IAED,OAAO;QACH,OAAO;YACH,GAAG,0CAA4B;SAClC,CAAA;IACL,CAAC;IAED,mBAAmB,CAAC,UAAuB;QACvC,IAAI,cAAmB,CAAC;QAExB,IAAI,CAAC;YACD,IAAI,GAAG,GAAG,UAAU,CAAC,QAAQ,CAAC;YAE9B,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;gBAC1B,GAAG,GAAG,GAAG,CAAC,IAAI,EAA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pC,8BAAa;QACC,6CAAoB;GAP9C,kCAAkC,CAmL9C","sourcesContent":["import { Injectable, Logger } from '@nestjs/common';\n\nimport { QueueMessage } from 'src/interfaces/mq';\nimport { MqMessageService } from '../../services/mq-message.service';\nimport { MqMessageQueueService } from '../../services/mq-message-queue.service';\nimport { McpResponse, QueuesModuleOptions, TriggerMcpClientOptions } from \"../../interfaces\";\nimport { DatabaseSubscriber } from 'src/services/queues/database-subscriber.service';\nimport triggerMcpClientQueueOptions from \"./trigger-mcp-client-queue-options\";\nimport { AiInteractionService } from 'src/services/ai-interaction.service';\nimport { PollerService } from 'src/services/poller.service';\n\n@Injectable()\nexport class TriggerMcpClientSubscriberDatabase extends DatabaseSubscriber<TriggerMcpClientOptions> {\n private readonly triggerMcpClientSubscriberLogger = new Logger(TriggerMcpClientSubscriberDatabase.name);\n\n constructor(\n readonly mqMessageService: MqMessageService,\n readonly mqMessageQueueService: MqMessageQueueService,\n readonly poller: PollerService,\n readonly aiInteractionService: AiInteractionService,\n ) {\n super(mqMessageService, mqMessageQueueService, poller);\n }\n\n options(): QueuesModuleOptions {\n return {\n ...triggerMcpClientQueueOptions\n }\n }\n\n cleanNestedResponse(aiResponse: McpResponse) {\n let nestedResponse: any;\n\n try {\n let raw = aiResponse.response;\n\n if (typeof raw === \"string\") {\n raw = raw.trim();\n try {\n // Try to parse as JSON\n nestedResponse = JSON.parse(raw);\n } catch {\n // Not JSON, just keep as string\n nestedResponse = raw;\n }\n } else if (typeof raw === \"object\" && raw !== null) {\n // Already JSON\n nestedResponse = raw;\n } else {\n // Fallback\n nestedResponse = String(raw);\n }\n } catch (err) {\n this.triggerMcpClientSubscriberLogger.error(\"Error processing AI response:\", err);\n nestedResponse = `Error handling response: ${err?.message || String(err)}`;\n }\n\n return nestedResponse;\n }\n\n async subscribe(message: QueueMessage<TriggerMcpClientOptions>) {\n this.triggerMcpClientSubscriberLogger.debug(`Received message: ${JSON.stringify(message)}`);\n\n const codeGnerationOptions = message.payload;\n\n const aiInteraction = await this.aiInteractionService.findOne(codeGnerationOptions.aiInteractionId, {\n populate: ['user']\n });\n if (!aiInteraction) {\n const m = `Unable to identified the aiInteraction entry that triggered this job... using id: ${codeGnerationOptions.aiInteractionId}`\n this.triggerMcpClientSubscriberLogger.log(m);\n throw new Error(m);\n }\n\n // The message contains the users prompt.\n const prompt = aiInteraction.message;\n\n // Use this to invoke our mcp client\n // TODO: try / catch ... \n // Handle the rejection gracefully...\n\n // We create the aiInteraction entry first \n const genAiInteraction = await this.aiInteractionService.create({\n userId: aiInteraction.user.id,\n threadId: aiInteraction.threadId,\n parentInteractionId: aiInteraction.id,\n role: 'gen-ai',\n message: '...', // Updated in the tool\n contentType: '', // Updated in the tool\n errorMessage: '', // Updated after we receive the response\n modelUsed: '', // Updated after we receive the response\n responseTimeMs: 0, // Updated after we receive the response\n metadata: '', // Updated in the tool\n isApplied: false, // Updated after we receive the response\n status: 'pending' // Updated after we receive the response\n });\n\n const finalPrompt = `\n # User Prompt: \n ${prompt}\n \n # System Instructions:\n - aiInteractionId: ${genAiInteraction.id}\n - moduleName:${message.payload.moduleName}\n - You will be invoking tools if needed.\n - If a tool is invoked, you must return **exactly** the raw output from the tool, without any additional formatting, commentary, or text.\n - Do not wrap the result in quotes, JSON, or markdown fences.\n - Do not explain what the result means.\n - Your final response must be identical to the tool output.\n `\n\n const aiResponse = await this.aiInteractionService.runMcpPrompt(finalPrompt);\n this.triggerMcpClientSubscriberLogger.log(`aiResponse: `);\n this.triggerMcpClientSubscriberLogger.log(JSON.stringify(aiResponse));\n\n if (!aiResponse.success) {\n this.triggerMcpClientSubscriberLogger.log(`Gen ai has returned with a false status code`);\n\n const errorsStr = aiResponse.errors.join('\\n ');\n const errorTrace = aiResponse.error_trace.join('\\n');\n\n // await this.aiInteractionService.create({\n // userId: aiInteraction.user.id,\n // threadId: aiInteraction.threadId,\n // parentInteractionId: aiInteraction.id,\n // role: 'gen-ai',\n // message: '-',\n // contentType: aiResponse.content_type,\n // errorMessage: errorsStr,\n // modelUsed: aiResponse.model,\n // responseTimeMs: aiResponse.duration_ms,\n // metadata: JSON.stringify(aiResponse, null, 2),\n // isApplied: aiInteraction.isApplied,\n // status: aiResponse.success ? 'succeeded' : 'failed'\n // });\n\n // TODO: Update the previously created genAiInteraction record with the respective error fields and save to DB\n await this.aiInteractionService.update(genAiInteraction.id, {\n // contentType: aiResponse.content_type,\n errorMessage: `${errorsStr}\\n\\n${errorTrace}`,\n modelUsed: aiResponse.model,\n responseTimeMs: aiResponse.duration_ms,\n isApplied: aiInteraction.isApplied,\n status: aiResponse.success ? 'succeeded' : 'failed'\n }, [], true);\n\n // update the job entry with failure... raising an error will lead the job to be marked as failed...\n throw new Error(errorsStr);\n }\n else {\n let nestedResponse = this.cleanNestedResponse(aiResponse);\n\n // const genAiInteraction = await this.aiInteractionService.create({\n // userId: aiInteraction.user.id,\n // threadId: aiInteraction.threadId,\n // parentInteractionId: aiInteraction.id,\n // role: 'gen-ai',\n // message: nestedResponse,\n // contentType: aiResponse.content_type,\n // errorMessage: '',\n // modelUsed: aiResponse.model,\n // responseTimeMs: aiResponse.duration_ms,\n // metadata: JSON.stringify(aiResponse, null, 2),\n // isApplied: aiInteraction.isApplied,\n // status: aiResponse.success ? 'succeeded' : 'failed'\n // });\n\n // TODO: Update the previously created genAiInteraction record with the respective success fields and save to DB\n const errorsStr = nestedResponse.status == \"error\" && nestedResponse.errors.join('\\n ');\n\n await this.aiInteractionService.update(genAiInteraction.id, {\n errorMessage: nestedResponse.status == \"error\" ? `${errorsStr}` : \"\",\n // errorMessage:\"\",\n // contentType: aiResponse.content_type,\n // message: nestedResponse,\n modelUsed: aiResponse.model,\n responseTimeMs: aiResponse.duration_ms,\n isApplied: aiInteraction.isApplied,\n\n // status: aiResponse.success ? 'succeeded' : 'failed'\n status: aiResponse.success && nestedResponse.status == \"success\" ? 'succeeded' : 'failed'\n }, [], true);\n\n // If the human interaction was with isAutoApply=true, then we can go ahead and autoApply.\n if (aiInteraction.isAutoApply) {\n this.aiInteractionService.applySolidAiInteraction(genAiInteraction.id);\n }\n }\n\n return aiResponse;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"trigger-mcp-client-subscriber-database.service.js","sourceRoot":"","sources":["../../../src/jobs/database/trigger-mcp-client-subscriber-database.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAoD;AAGpD,0EAAqE;AACrE,sFAAgF;AAEhF,mGAAqF;AACrF,0GAA8E;AAC9E,kFAA2E;AAC3E,kEAA4D;AAGrD,IAAM,kCAAkC,0CAAxC,MAAM,kCAAmC,SAAQ,gDAA2C;IAG/F,YACa,gBAAkC,EAClC,qBAA4C,EAC5C,MAAqB,EACrB,oBAA0C;QAEnD,KAAK,CAAC,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,CAAC,CAAC;QAL9C,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,0BAAqB,GAArB,qBAAqB,CAAuB;QAC5C,WAAM,GAAN,MAAM,CAAe;QACrB,yBAAoB,GAApB,oBAAoB,CAAsB;QANtC,qCAAgC,GAAG,IAAI,eAAM,CAAC,oCAAkC,CAAC,IAAI,CAAC,CAAC;IASxG,CAAC;IAED,OAAO;QACH,OAAO;YACH,GAAG,0CAA4B;SAClC,CAAA;IACL,CAAC;IAED,mBAAmB,CAAC,UAAuB;QACvC,IAAI,cAAmB,CAAC;QAExB,IAAI,CAAC;YACD,IAAI,GAAG,GAAG,UAAU,CAAC,QAAQ,CAAC;YAE9B,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;gBAC1B,GAAG,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;gBACjB,IAAI,CAAC;oBAED,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACrC,CAAC;gBAAC,MAAM,CAAC;oBAEL,cAAc,GAAG,GAAG,CAAC;gBACzB,CAAC;YACL,CAAC;iBAAM,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI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C,CAAC;YAGb,MAAM,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC;QAC/B,CAAC;aACI,CAAC;YACF,IAAI,cAAc,GAAG,IAAI,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC;YAkB1D,MAAM,SAAS,GAAG,cAAc,CAAC,MAAM,IAAI,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAExF,MAAM,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE,EAAE;gBACxD,YAAY,EAAE,cAAc,CAAC,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC,CAAC,CAAC,EAAE;gBAIpE,SAAS,EAAE,UAAU,CAAC,KAAK;gBAC3B,cAAc,EAAE,UAAU,CAAC,WAAW;gBACtC,SAAS,EAAE,aAAa,CAAC,SAAS;gBAGlC,MAAM,EAAE,UAAU,CAAC,OAAO,IAAI,cAAc,CAAC,MAAM,IAAI,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,QAAQ;aAC5F,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;YAGb,IAAI,aAAa,CAAC,WAAW,EAAE,CAAC;gBAC5B,IAAI,CAAC,oBAAoB,CAAC,uBAAuB,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC;YAC3E,CAAC;QACL,CAAC;QAED,OAAO,UAAU,CAAC;IACtB,CAAC;CACJ,CAAA;AAvLY,gFAAkC;6CAAlC,kCAAkC;IAD9C,IAAA,mBAAU,GAAE;qCAKsB,qCAAgB;QACX,gDAAqB;QACpC,8BAAa;QACC,6CAAoB;GAP9C,kCAAkC,CAuL9C","sourcesContent":["import { Injectable, Logger } from '@nestjs/common';\n\nimport { QueueMessage } from 'src/interfaces/mq';\nimport { MqMessageService } from '../../services/mq-message.service';\nimport { MqMessageQueueService } from '../../services/mq-message-queue.service';\nimport { McpResponse, QueuesModuleOptions, TriggerMcpClientOptions } from \"../../interfaces\";\nimport { DatabaseSubscriber } from 'src/services/queues/database-subscriber.service';\nimport triggerMcpClientQueueOptions from \"./trigger-mcp-client-queue-options\";\nimport { AiInteractionService } from 'src/services/ai-interaction.service';\nimport { PollerService } from 'src/services/poller.service';\n\n@Injectable()\nexport class TriggerMcpClientSubscriberDatabase extends DatabaseSubscriber<TriggerMcpClientOptions> {\n private readonly triggerMcpClientSubscriberLogger = new Logger(TriggerMcpClientSubscriberDatabase.name);\n\n constructor(\n readonly mqMessageService: MqMessageService,\n readonly mqMessageQueueService: MqMessageQueueService,\n readonly poller: PollerService,\n readonly aiInteractionService: AiInteractionService,\n ) {\n super(mqMessageService, mqMessageQueueService, poller);\n }\n\n options(): QueuesModuleOptions {\n return {\n ...triggerMcpClientQueueOptions\n }\n }\n\n cleanNestedResponse(aiResponse: McpResponse) {\n let nestedResponse: any;\n\n try {\n let raw = aiResponse.response;\n\n if (typeof raw === \"string\") {\n raw = raw.trim();\n try {\n // Try to parse as JSON\n nestedResponse = JSON.parse(raw);\n } catch {\n // Not JSON, just keep as string\n nestedResponse = raw;\n }\n } else if (typeof raw === \"object\" && raw !== null) {\n // Already JSON\n nestedResponse = raw;\n } else {\n // Fallback\n nestedResponse = String(raw);\n }\n } catch (err) {\n this.triggerMcpClientSubscriberLogger.error(\"Error processing AI response:\", err);\n nestedResponse = `Error handling response: ${err?.message || String(err)}`;\n }\n\n return nestedResponse;\n }\n\n async subscribe(message: QueueMessage<TriggerMcpClientOptions>) {\n this.triggerMcpClientSubscriberLogger.debug(`Received message: ${JSON.stringify(message)}`);\n\n const codeGnerationOptions = message.payload;\n\n const aiInteraction = await this.aiInteractionService.findOne(codeGnerationOptions.aiInteractionId, {\n populate: ['user']\n });\n if (!aiInteraction) {\n const m = `Unable to identified the aiInteraction entry that triggered this job... using id: ${codeGnerationOptions.aiInteractionId}`\n this.triggerMcpClientSubscriberLogger.log(m);\n throw new Error(m);\n }\n\n // The message contains the users prompt.\n const prompt = aiInteraction.message;\n\n // Use this to invoke our mcp client\n // TODO: try / catch ... \n // Handle the rejection gracefully...\n\n // We create the aiInteraction entry first \n const genAiInteraction = await this.aiInteractionService.create({\n userId: aiInteraction.user.id,\n threadId: aiInteraction.threadId,\n parentInteractionId: aiInteraction.id,\n role: 'gen-ai',\n message: '...', // Updated in the tool\n contentType: '', // Updated in the tool\n errorMessage: '', // Updated after we receive the response\n modelUsed: '', // Updated after we receive the response\n responseTimeMs: 0, // Updated after we receive the response\n metadata: '', // Updated in the tool\n isApplied: false, // Updated after we receive the response\n status: 'pending' // Updated after we receive the response\n });\n\n const finalPrompt = `\n# User Prompt: \n${prompt}\n\n# System Instructions:\n- aiInteractionId: ${genAiInteraction.id}\n- moduleName:${message.payload.moduleName}\n- You will be invoking tools if needed.\n- If a tool is invoked, you must return **exactly** the raw output from the tool, without any json envelopes, additional formatting, commentary, or text.\n- Do not wrap the result in quotes, JSON, or markdown fences.\n- Do not explain what the result means.\n\n# Past Interactions: \nThis section contains the last 10 interactions done between the human and LLM. These are sorted by oldest first. \nUse these interactions to further identify concerns based on the current User Prompt.\n\n`\n\n const aiResponse = await this.aiInteractionService.runMcpPrompt(finalPrompt);\n this.triggerMcpClientSubscriberLogger.log(`aiResponse: `);\n this.triggerMcpClientSubscriberLogger.log(JSON.stringify(aiResponse));\n\n if (!aiResponse.success) {\n this.triggerMcpClientSubscriberLogger.log(`Gen ai has returned with a false status code`);\n\n const errorsStr = aiResponse.errors?.join('\\n ');\n const errorTrace = aiResponse.error_trace?.join('\\n');\n\n // await this.aiInteractionService.create({\n // userId: aiInteraction.user.id,\n // threadId: aiInteraction.threadId,\n // parentInteractionId: aiInteraction.id,\n // role: 'gen-ai',\n // message: '-',\n // contentType: aiResponse.content_type,\n // errorMessage: errorsStr,\n // modelUsed: aiResponse.model,\n // responseTimeMs: aiResponse.duration_ms,\n // metadata: JSON.stringify(aiResponse, null, 2),\n // isApplied: aiInteraction.isApplied,\n // status: aiResponse.success ? 'succeeded' : 'failed'\n // });\n\n // TODO: Update the previously created genAiInteraction record with the respective error fields and save to DB\n await this.aiInteractionService.update(genAiInteraction.id, {\n // contentType: aiResponse.content_type,\n errorMessage: `${errorsStr}\\n\\n${errorTrace}`,\n modelUsed: aiResponse.model,\n responseTimeMs: aiResponse.duration_ms,\n isApplied: aiInteraction.isApplied,\n status: aiResponse.success ? 'succeeded' : 'failed'\n }, [], true);\n\n // update the job entry with failure... raising an error will lead the job to be marked as failed...\n throw new Error(errorsStr);\n }\n else {\n let nestedResponse = this.cleanNestedResponse(aiResponse);\n\n // const genAiInteraction = await this.aiInteractionService.create({\n // userId: aiInteraction.user.id,\n // threadId: aiInteraction.threadId,\n // parentInteractionId: aiInteraction.id,\n // role: 'gen-ai',\n // message: nestedResponse,\n // contentType: aiResponse.content_type,\n // errorMessage: '',\n // modelUsed: aiResponse.model,\n // responseTimeMs: aiResponse.duration_ms,\n // metadata: JSON.stringify(aiResponse, null, 2),\n // isApplied: aiInteraction.isApplied,\n // status: aiResponse.success ? 'succeeded' : 'failed'\n // });\n\n // TODO: Update the previously created genAiInteraction record with the respective success fields and save to DB\n const errorsStr = nestedResponse.status == \"error\" && nestedResponse.errors.join('\\n ');\n\n await this.aiInteractionService.update(genAiInteraction.id, {\n errorMessage: nestedResponse.status == \"error\" ? `${errorsStr}` : \"\",\n // errorMessage:\"\",\n // contentType: aiResponse.content_type,\n // message: nestedResponse,\n modelUsed: aiResponse.model,\n responseTimeMs: aiResponse.duration_ms,\n isApplied: aiInteraction.isApplied,\n\n // status: aiResponse.success ? 'succeeded' : 'failed'\n status: aiResponse.success && nestedResponse.status == \"success\" ? 'succeeded' : 'failed'\n }, [], true);\n\n // If the human interaction was with isAutoApply=true, then we can go ahead and autoApply.\n if (aiInteraction.isAutoApply) {\n this.aiInteractionService.applySolidAiInteraction(genAiInteraction.id);\n }\n }\n\n return aiResponse;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-interaction.service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EAE3D,6BAA6B,EAAE,6BAA6B;IAMjE,mBAAmB,CAAC,GAAG,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAE,OAAe,EAAE,QAAQ,GAAE,MAAa,GAAG,OAAO,CAAC,GAAG,CAAC;IAqChI,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IA+FxD,aAAa,CAAC,QAAQ,EAAE,MAAM;IAexB,uBAAuB,CAAC,EAAE,EAAE,MAAM;CAuDzC"}
|
|
@@ -154,7 +154,6 @@ let AiInteractionService = AiInteractionService_1 = class AiInteractionService e
|
|
|
154
154
|
}
|
|
155
155
|
catch (ex) {
|
|
156
156
|
this.logger.warn(`Attempting to parse mcp client response assuming it is JSON, however it is not: ${parsedResponse}`);
|
|
157
|
-
raw.success = false;
|
|
158
157
|
}
|
|
159
158
|
const enrichedRaw = {
|
|
160
159
|
...raw,
|
|
@@ -189,12 +188,20 @@ let AiInteractionService = AiInteractionService_1 = class AiInteractionService e
|
|
|
189
188
|
}
|
|
190
189
|
let metadata = {};
|
|
191
190
|
try {
|
|
192
|
-
|
|
191
|
+
if (typeof aiInteraction.metadata === "string") {
|
|
192
|
+
metadata = JSON.parse(aiInteraction.metadata);
|
|
193
|
+
}
|
|
194
|
+
else if (typeof aiInteraction.metadata === "object" && aiInteraction.metadata !== null) {
|
|
195
|
+
metadata = aiInteraction.metadata;
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
metadata = {};
|
|
199
|
+
}
|
|
193
200
|
}
|
|
194
201
|
catch (e) {
|
|
195
|
-
throw new Error(e);
|
|
202
|
+
throw new Error(`Invalid metadata JSON: ${e}`);
|
|
196
203
|
}
|
|
197
|
-
const toolsInvoked = metadata['
|
|
204
|
+
const toolsInvoked = metadata['toolsInvoked'];
|
|
198
205
|
if (!toolsInvoked) {
|
|
199
206
|
throw new Error(error_messages_1.ERROR_MESSAGES.UNABLE_TO_RESOLVE_SOLID_COMMAND);
|
|
200
207
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-interaction.service.js","sourceRoot":"","sources":["../../src/services/ai-interaction.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAyE;AACzE,6CAAwE;AACxE,uCAA2D;AAC3D,qCAAoD;AAEpD,iDAAwD;AACxD,qEAA2E;AAC3E,uEAA6E;AAC7E,2CAA+C;AAC/C,iDAAwD;AACxD,+DAAqE;AACrE,iDAAsC;AACtC,6EAAkE;AAClE,gDAAkC;AAElC,kFAAsE;AAGtE,sIAAuH;AACvH,gEAA8D;AAIvD,IAAM,oBAAoB,4BAA1B,MAAM,oBAAqB,SAAQ,0BAA0B;IAGlE,YACW,oBAA0C,EAC1C,qBAA4C,EAC5C,aAA4B,EAC5B,WAAwB,EACxB,gBAAkC,EAClC,iBAAoC,EAE7C,aAAqC,EAErC,IAAwC,EAC/B,SAAoB,EACpB,gBAA2D,EAE3D,6BAA4D;QAGrE,KAAK,CAAC,oBAAoB,EAAE,qBAAqB,EAAE,aAAa,EAAE,WAAW,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,aAAa,EAAE,IAAI,EAAE,eAAe,EAAE,YAAY,EAAE,SAAS,CAAC,CAAC;QAhB1K,yBAAoB,GAApB,oBAAoB,CAAsB;QAC1C,0BAAqB,GAArB,qBAAqB,CAAuB;QAC5C,kBAAa,GAAb,aAAa,CAAe;QAC5B,gBAAW,GAAX,WAAW,CAAa;QACxB,qBAAgB,GAAhB,gBAAgB,CAAkB;QAClC,sBAAiB,GAAjB,iBAAiB,CAAmB;QAEpC,kBAAa,GAAb,aAAa,CAAe;QAE5B,SAAI,GAAJ,IAAI,CAA2B;QAC/B,cAAS,GAAT,SAAS,CAAW;QACpB,qBAAgB,GAAhB,gBAAgB,CAA2C;QAE3D,kCAA6B,GAA7B,6BAA6B,CAA+B;QAhBtD,WAAM,GAAG,IAAI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c,CAAC,+BAA+B,CAAC,CAAC;QAClE,CAAC;QAGD,MAAM,WAAW,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;QAIpC,MAAM,cAAc,GAAG,IAAI,CAAC,6BAA6B,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;QACnF,IAAI,CAAC,cAAc,EAAE,CAAC;YAEpB,MAAM,IAAI,KAAK,CAAC,+BAAc,CAAC,6BAA6B,CAAC,CAAC;QAChE,CAAC;QAED,MAAM,0BAA0B,GAAG,MAAM,cAAc,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;QAM7E,MAAM,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;QAEnE,OAAO,0BAA0B,CAAC;IACpC,CAAC;CACF,CAAA;AA3NY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,GAAE;IAWR,WAAA,IAAA,6BAAmB,GAAE,CAAA;IAErB,WAAA,IAAA,0BAAgB,EAAC,qCAAa,EAAE,SAAS,CAAC,CAAA;qCARZ,6CAAoB;QACnB,+CAAqB;QAC7B,sBAAa;QACf,0BAAW;QACN,uBAAgB;QACf,uCAAiB;QAErB,uBAAa;QAEtB,oBAAU;QACL,gBAAS;QACF,4CAAgB;QAEH,yEAA6B;GAjB5D,oBAAoB,CA2NhC","sourcesContent":["import { BadRequestException, Logger, Injectable } from '@nestjs/common';\nimport { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';\nimport { DiscoveryService, ModuleRef } from \"@nestjs/core\";\nimport { EntityManager, Repository } from 'typeorm';\n\nimport { CRUDService } from 'src/services/crud.service';\nimport { ModelMetadataService } from 'src/services/model-metadata.service';\nimport { ModuleMetadataService } from 'src/services/module-metadata.service';\nimport { ConfigService } from '@nestjs/config';\nimport { FileService } from 'src/services/file.service';\nimport { CrudHelperService } from 'src/services/crud-helper.service';\nimport { spawn } from 'child_process';\nimport { AiInteraction } from '../entities/ai-interaction.entity';\nimport * as fs from 'fs/promises';\nimport { McpResponse, TriggerMcpClientOptions } from 'src/interfaces';\nimport { PublisherFactory } from './queues/publisher-factory.service';\nimport { RequestContextService } from './request-context.service';\nimport { ActiveUserData } from 'src/interfaces/active-user-data.interface';\nimport { McpToolResponseHandlerFactory } from './mcp-tool-response-handlers/mcp-tool-response-handler-factory.service';\nimport { ERROR_MESSAGES } from 'src/constants/error-messages';\nimport { InvokeAiPromptDto } from 'src/dtos/invoke-ai-prompt.dto';\n\n@Injectable()\nexport class AiInteractionService extends CRUDService<AiInteraction> {\n private readonly logger = new Logger(AiInteractionService.name);\n\n constructor(\n readonly modelMetadataService: ModelMetadataService,\n readonly moduleMetadataService: ModuleMetadataService,\n readonly configService: ConfigService,\n readonly fileService: FileService,\n readonly discoveryService: DiscoveryService,\n readonly crudHelperService: CrudHelperService,\n @InjectEntityManager()\n readonly entityManager: EntityManager,\n @InjectRepository(AiInteraction, 'default')\n readonly repo: Repository<AiInteraction>,\n readonly moduleRef: ModuleRef,\n readonly publisherFactory: PublisherFactory<TriggerMcpClientOptions>,\n // readonly requestContextService: RequestContextService,\n readonly mcpToolResponseHandlerFactory: McpToolResponseHandlerFactory,\n\n ) {\n super(modelMetadataService, moduleMetadataService, configService, fileService, discoveryService, crudHelperService, entityManager, repo, 'aiInteraction', 'solid-core', moduleRef);\n }\n\n async triggerMcpClientJob(dto: InvokeAiPromptDto, userId: number, isAutoApply: boolean = false, threadId: string = null): Promise<any> {\n // const activeUser: ActiveUserData = this.requestContextService.getActiveUser();\n\n const aiInteraction = await this.create({\n userId: userId,\n threadId: threadId ? threadId : `thread-${userId}`,\n role: 'human',\n message: dto.prompt,\n contentType: '',\n errorMessage: '',\n modelUsed: '',\n responseTimeMs: 0,\n metadata: '',\n isAutoApply: isAutoApply\n });\n const m = {\n payload: {\n aiInteractionId: aiInteraction.id,\n moduleName:dto.moduleName\n },\n parentEntity: 'aiInteraction',\n parentEntityId: aiInteraction.id,\n };\n\n const queueMessageId = await this.publisherFactory.publish(m, 'TriggerMcpClientPublisher');\n\n return {\n queueMessageId: queueMessageId,\n aiInteractionId: aiInteraction.id\n }\n }\n\n /**\n * Runs the Python MCP client with a prompt and returns the parsed JSON embedded in the 'response'.\n * @param prompt - The question or instruction to send to the MCP client.\n * @returns The parsed object inside the 'response' field of the JSON output.\n */\n async runMcpPrompt(prompt: string): Promise<McpResponse> {\n const pythonExecutable = process.env.MCP_PYTHON_EXECUTABLE;\n const mcpClient = process.env.MCP_CLIENT;\n\n // TODO: We can return an error if the above env variables are not properly setup...\n if (!pythonExecutable || !mcpClient) {\n throw new BadRequestException(ERROR_MESSAGES.PYTHON_EXECUTABLE_NOT_CONFIGURED);\n }\n\n // Check if both paths are valid and accessible\n try {\n const [pyStat, clientStat] = await Promise.all([\n fs.stat(pythonExecutable),\n fs.stat(mcpClient),\n ]);\n\n if (!pyStat.isFile()) {\n throw new BadRequestException(`MCP_PYTHON_EXECUTABLE path is not a file: ${pythonExecutable}`);\n }\n\n if (!clientStat.isFile()) {\n throw new BadRequestException(`MCP_CLIENT path is not a file: ${mcpClient}`);\n }\n\n } catch (err: any) {\n throw new BadRequestException(`Invalid MCP executable or client path: ${err.message}`);\n }\n\n // TODO: Refactor to use the command.service.ts instead...\n return new Promise((resolve, reject) => {\n this.logger.log(`Attempting to run command:`)\n this.logger.log(`${pythonExecutable} ${mcpClient} \"${prompt}\"`);\n\n const python = spawn(pythonExecutable, [mcpClient, `\"${prompt}\"`]);\n\n let stdout = '';\n let stderr = '';\n\n python.stdout.on('data', (data) => {\n stdout += data.toString();\n });\n\n python.stderr.on('data', (data) => {\n stderr += data.toString();\n });\n\n python.on('close', (code) => {\n this.logger.log(`Python script exited with code ${code}`);\n\n if (code !== 0) {\n this.logger.error(`Python script exited with a non-zero exit code: ${stderr}`);\n return reject(new Error(`Python script exited with a non-zero exit code: ${stderr}`));\n }\n\n try {\n this.logger.log(`Python script exited with zero exit code: ${stdout}`);\n const raw: McpResponse = JSON.parse(stdout);\n\n // Sometimes the raw.response might not be a valid json\n // TODO: examine the content type of the raw response..\n // if (raw.content_type==='json') {\n // }\n let parsedResponse = raw.response;\n try {\n parsedResponse = JSON.parse(raw.response);\n }\n catch (ex) {\n this.logger.warn(`Attempting to parse mcp client response assuming it is JSON, however it is not: ${parsedResponse}`);\n raw.success = false\n }\n // Parse the response string into an object\n // const parsedResponse = JSON.parse(raw.response);\n\n // Replace the string with the parsed object\n const enrichedRaw = {\n ...raw,\n response: parsedResponse,\n };\n // if (!raw.success) {\n // return reject(new Error(`MCP error: ${raw.errors?.join(', ')}`));\n // }\n // let cleaned = raw.response.trim();\n\n // Don't need to re-parse this...\n // const parsed = JSON.parse(cleaned);\n // resolve(cleaned);\n\n resolve(enrichedRaw);\n } catch (err: any) {\n reject(new Error(`Mcp Invocation Failed: ${err.message}`));\n }\n });\n });\n }\n\n cleanResponse(response: string) {\n this.logger.log(`mcp server response is: ${response}`);\n\n // Remove markdown-style code block wrapper\n if (response.startsWith('```json')) {\n response = response.replace(/^```json/, '').trim();\n }\n if (response.endsWith('```')) {\n response = response.replace(/```$/, '').trim();\n }\n this.logger.log(`mcp server response after removing doc tags is: ${response}`);\n\n return response;\n }\n\n async applySolidAiInteraction(id: number) {\n // Fetch the aiInteraction\n const aiInteraction = await this.findOne(id, {\n populate: ['user']\n });\n if (!aiInteraction) {\n const m = `Unable to identified the aiInteraction entry that triggered this job... using id: ${id}`\n\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(m);\n }\n\n // TODO: Validation: Check if JSON.parse(metadata).tools_invoked starts with solid_\n let metadata = {};\n try {\n metadata = JSON.parse(aiInteraction.metadata);\n }\n catch (e) {\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(e);\n }\n\n const toolsInvoked = metadata['tools_invoked'];\n if (!toolsInvoked) {\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(ERROR_MESSAGES.UNABLE_TO_RESOLVE_SOLID_COMMAND);\n }\n\n // TODO: OPTIMISATION for chained tool invocation, for now we are assuming only 1 tool was used.\n const toolInvoked = toolsInvoked[0];\n\n // TODO: use the toolInvoked to identify a service using some convention.\n // TODO: Eg. if toolInvoked is solid_create_module <> SolidCreateModuleMcpToolHandler ... create a factory class to do this mapping and identify the relevant provider. \n const mcpToolHandler = this.mcpToolResponseHandlerFactory.getInstance(toolInvoked);\n if (!mcpToolHandler) {\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(ERROR_MESSAGES.UNABLE_TO_RESOLVE_MCP_HANDLER);\n }\n\n const handlerApplicationResponse = await mcpToolHandler.apply(aiInteraction);\n\n // TODO: This provider to implement an interface - IMcpToolResponseHandler ... apply(aiInteraction: AiInteraction)\n // throw new Error('Method not implemented.');\n\n // Mark the interaction as applied\n await this.update(aiInteraction.id, { isApplied: true }, [], true);\n\n return handlerApplicationResponse;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"ai-interaction.service.js","sourceRoot":"","sources":["../../src/services/ai-interaction.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAyE;AACzE,6CAAwE;AACxE,uCAA2D;AAC3D,qCAAoD;AAEpD,iDAAwD;AACxD,qEAA2E;AAC3E,uEAA6E;AAC7E,2CAA+C;AAC/C,iDAAwD;AACxD,+DAAqE;AACrE,iDAAsC;AACtC,6EAAkE;AAClE,gDAAkC;AAElC,kFAAsE;AAGtE,sIAAuH;AACvH,gEAA8D;AAIvD,IAAM,oBAAoB,4BAA1B,MAAM,oBAAqB,SAAQ,0BAA0B;IAGlE,YACW,oBAA0C,EAC1C,qBAA4C,EAC5C,aAA4B,EAC5B,WAAwB,EACxB,gBAAkC,EAClC,iBAAoC,EAE7C,aAAqC,EAErC,IAAwC,EAC/B,SAAoB,EACpB,gBAA2D,EAE3D,6BAA4D;QA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C,MAAM,CAAC,aAAa,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;QAEnE,OAAO,0BAA0B,CAAC;IACpC,CAAC;CACF,CAAA;AAjOY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,GAAE;IAWR,WAAA,IAAA,6BAAmB,GAAE,CAAA;IAErB,WAAA,IAAA,0BAAgB,EAAC,qCAAa,EAAE,SAAS,CAAC,CAAA;qCARZ,6CAAoB;QACnB,+CAAqB;QAC7B,sBAAa;QACf,0BAAW;QACN,uBAAgB;QACf,uCAAiB;QAErB,uBAAa;QAEtB,oBAAU;QACL,gBAAS;QACF,4CAAgB;QAEH,yEAA6B;GAjB5D,oBAAoB,CAiOhC","sourcesContent":["import { BadRequestException, Logger, Injectable } from '@nestjs/common';\nimport { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';\nimport { DiscoveryService, ModuleRef } from \"@nestjs/core\";\nimport { EntityManager, Repository } from 'typeorm';\n\nimport { CRUDService } from 'src/services/crud.service';\nimport { ModelMetadataService } from 'src/services/model-metadata.service';\nimport { ModuleMetadataService } from 'src/services/module-metadata.service';\nimport { ConfigService } from '@nestjs/config';\nimport { FileService } from 'src/services/file.service';\nimport { CrudHelperService } from 'src/services/crud-helper.service';\nimport { spawn } from 'child_process';\nimport { AiInteraction } from '../entities/ai-interaction.entity';\nimport * as fs from 'fs/promises';\nimport { McpResponse, TriggerMcpClientOptions } from 'src/interfaces';\nimport { PublisherFactory } from './queues/publisher-factory.service';\nimport { RequestContextService } from './request-context.service';\nimport { ActiveUserData } from 'src/interfaces/active-user-data.interface';\nimport { McpToolResponseHandlerFactory } from './mcp-tool-response-handlers/mcp-tool-response-handler-factory.service';\nimport { ERROR_MESSAGES } from 'src/constants/error-messages';\nimport { InvokeAiPromptDto } from 'src/dtos/invoke-ai-prompt.dto';\n\n@Injectable()\nexport class AiInteractionService extends CRUDService<AiInteraction> {\n private readonly logger = new Logger(AiInteractionService.name);\n\n constructor(\n readonly modelMetadataService: ModelMetadataService,\n readonly moduleMetadataService: ModuleMetadataService,\n readonly configService: ConfigService,\n readonly fileService: FileService,\n readonly discoveryService: DiscoveryService,\n readonly crudHelperService: CrudHelperService,\n @InjectEntityManager()\n readonly entityManager: EntityManager,\n @InjectRepository(AiInteraction, 'default')\n readonly repo: Repository<AiInteraction>,\n readonly moduleRef: ModuleRef,\n readonly publisherFactory: PublisherFactory<TriggerMcpClientOptions>,\n // readonly requestContextService: RequestContextService,\n readonly mcpToolResponseHandlerFactory: McpToolResponseHandlerFactory,\n\n ) {\n super(modelMetadataService, moduleMetadataService, configService, fileService, discoveryService, crudHelperService, entityManager, repo, 'aiInteraction', 'solid-core', moduleRef);\n }\n\n async triggerMcpClientJob(dto: InvokeAiPromptDto, userId: number, isAutoApply: boolean = false, threadId: string = null): Promise<any> {\n // const activeUser: ActiveUserData = this.requestContextService.getActiveUser();\n\n const aiInteraction = await this.create({\n userId: userId,\n threadId: threadId ? threadId : `thread-${userId}`,\n role: 'human',\n message: dto.prompt,\n contentType: '',\n errorMessage: '',\n modelUsed: '',\n responseTimeMs: 0,\n metadata: '',\n isAutoApply: isAutoApply\n });\n const m = {\n payload: {\n aiInteractionId: aiInteraction.id,\n moduleName: dto.moduleName\n },\n parentEntity: 'aiInteraction',\n parentEntityId: aiInteraction.id,\n };\n\n const queueMessageId = await this.publisherFactory.publish(m, 'TriggerMcpClientPublisher');\n\n return {\n queueMessageId: queueMessageId,\n aiInteractionId: aiInteraction.id\n }\n }\n\n /**\n * Runs the Python MCP client with a prompt and returns the parsed JSON embedded in the 'response'.\n * @param prompt - The question or instruction to send to the MCP client.\n * @returns The parsed object inside the 'response' field of the JSON output.\n */\n async runMcpPrompt(prompt: string): Promise<McpResponse> {\n const pythonExecutable = process.env.MCP_PYTHON_EXECUTABLE;\n const mcpClient = process.env.MCP_CLIENT;\n\n // TODO: We can return an error if the above env variables are not properly setup...\n if (!pythonExecutable || !mcpClient) {\n throw new BadRequestException(ERROR_MESSAGES.PYTHON_EXECUTABLE_NOT_CONFIGURED);\n }\n\n // Check if both paths are valid and accessible\n try {\n const [pyStat, clientStat] = await Promise.all([\n fs.stat(pythonExecutable),\n fs.stat(mcpClient),\n ]);\n\n if (!pyStat.isFile()) {\n throw new BadRequestException(`MCP_PYTHON_EXECUTABLE path is not a file: ${pythonExecutable}`);\n }\n\n if (!clientStat.isFile()) {\n throw new BadRequestException(`MCP_CLIENT path is not a file: ${mcpClient}`);\n }\n\n } catch (err: any) {\n throw new BadRequestException(`Invalid MCP executable or client path: ${err.message}`);\n }\n\n // TODO: Refactor to use the command.service.ts instead...\n return new Promise((resolve, reject) => {\n this.logger.log(`Attempting to run command:`)\n this.logger.log(`${pythonExecutable} ${mcpClient} \"${prompt}\"`);\n\n const python = spawn(pythonExecutable, [mcpClient, `\"${prompt}\"`]);\n\n let stdout = '';\n let stderr = '';\n\n python.stdout.on('data', (data) => {\n stdout += data.toString();\n });\n\n python.stderr.on('data', (data) => {\n stderr += data.toString();\n });\n\n python.on('close', (code) => {\n this.logger.log(`Python script exited with code ${code}`);\n\n if (code !== 0) {\n this.logger.error(`Python script exited with a non-zero exit code: ${stderr}`);\n return reject(new Error(`Python script exited with a non-zero exit code: ${stderr}`));\n }\n\n try {\n this.logger.log(`Python script exited with zero exit code: ${stdout}`);\n const raw: McpResponse = JSON.parse(stdout);\n\n // Sometimes the raw.response might not be a valid json\n // TODO: examine the content type of the raw response..\n // if (raw.content_type==='json') {\n // }\n let parsedResponse = raw.response;\n try {\n parsedResponse = JSON.parse(raw.response);\n }\n catch (ex) {\n this.logger.warn(`Attempting to parse mcp client response assuming it is JSON, however it is not: ${parsedResponse}`);\n // raw.success = false\n }\n // Parse the response string into an object\n // const parsedResponse = JSON.parse(raw.response);\n\n // Replace the string with the parsed object\n const enrichedRaw = {\n ...raw,\n response: parsedResponse,\n };\n // if (!raw.success) {\n // return reject(new Error(`MCP error: ${raw.errors?.join(', ')}`));\n // }\n // let cleaned = raw.response.trim();\n\n // Don't need to re-parse this...\n // const parsed = JSON.parse(cleaned);\n // resolve(cleaned);\n\n resolve(enrichedRaw);\n } catch (err: any) {\n reject(new Error(`Mcp Invocation Failed: ${err.message}`));\n }\n });\n });\n }\n\n cleanResponse(response: string) {\n this.logger.log(`mcp server response is: ${response}`);\n\n // Remove markdown-style code block wrapper\n if (response.startsWith('```json')) {\n response = response.replace(/^```json/, '').trim();\n }\n if (response.endsWith('```')) {\n response = response.replace(/```$/, '').trim();\n }\n this.logger.log(`mcp server response after removing doc tags is: ${response}`);\n\n return response;\n }\n\n async applySolidAiInteraction(id: number) {\n // Fetch the aiInteraction\n const aiInteraction = await this.findOne(id, {\n populate: ['user']\n });\n if (!aiInteraction) {\n const m = `Unable to identified the aiInteraction entry that triggered this job... using id: ${id}`\n\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(m);\n }\n\n // TODO: Validation: Check if JSON.parse(metadata).tools_invoked starts with solid_\n let metadata: any = {};\n try {\n if (typeof aiInteraction.metadata === \"string\") {\n metadata = JSON.parse(aiInteraction.metadata);\n } else if (typeof aiInteraction.metadata === \"object\" && aiInteraction.metadata !== null) {\n metadata = aiInteraction.metadata;\n } else {\n // optional fallback\n metadata = {};\n }\n } catch (e) {\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(`Invalid metadata JSON: ${e}`);\n }\n\n const toolsInvoked = metadata['toolsInvoked'];\n if (!toolsInvoked) {\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(ERROR_MESSAGES.UNABLE_TO_RESOLVE_SOLID_COMMAND);\n }\n\n // TODO: OPTIMISATION for chained tool invocation, for now we are assuming only 1 tool was used.\n const toolInvoked = toolsInvoked[0];\n\n // TODO: use the toolInvoked to identify a service using some convention.\n // TODO: Eg. if toolInvoked is solid_create_module <> SolidCreateModuleMcpToolHandler ... create a factory class to do this mapping and identify the relevant provider. \n const mcpToolHandler = this.mcpToolResponseHandlerFactory.getInstance(toolInvoked);\n if (!mcpToolHandler) {\n // TODO: RESPONSE SHAPE ALERT Check if we want to control the shape of the response....\n throw new Error(ERROR_MESSAGES.UNABLE_TO_RESOLVE_MCP_HANDLER);\n }\n\n const handlerApplicationResponse = await mcpToolHandler.apply(aiInteraction);\n\n // TODO: This provider to implement an interface - IMcpToolResponseHandler ... apply(aiInteraction: AiInteraction)\n // throw new Error('Method not implemented.');\n\n // Mark the interaction as applied\n await this.update(aiInteraction.id, { isApplied: true }, [], true);\n\n return handlerApplicationResponse;\n }\n}\n"]}
|
|
@@ -71,7 +71,7 @@ let ExcelService = ExcelService_1 = class ExcelService {
|
|
|
71
71
|
isHeaderWritten = true;
|
|
72
72
|
}
|
|
73
73
|
if (typeof getDataRecords !== 'function') {
|
|
74
|
-
|
|
74
|
+
workbook.commit();
|
|
75
75
|
return passThrough;
|
|
76
76
|
}
|
|
77
77
|
let chunkIndex = 0;
|
|
@@ -93,7 +93,7 @@ let ExcelService = ExcelService_1 = class ExcelService {
|
|
|
93
93
|
chunkIndex++;
|
|
94
94
|
this.logger.debug(`✅ Chunk ${chunkIndex} written to Excel`);
|
|
95
95
|
}
|
|
96
|
-
|
|
96
|
+
workbook.commit();
|
|
97
97
|
}
|
|
98
98
|
catch (error) {
|
|
99
99
|
this.logger.error(`❌ Error writing Excel: ${error.message}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"excel.service.js","sourceRoot":"","sources":["../../src/services/excel.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAAoD;AACpD,iDAAmC;AACnC,gEAA8D;AAC9D,mCAA+C;AAS/C,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAQvB,IAAM,YAAY,oBAAlB,MAAM,YAAY;IAAlB;QACG,WAAM,GAAG,IAAI,eAAM,CAAC,cAAY,CAAC,IAAI,CAAC,CAAC;IA4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M,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;gBAEnE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE;oBAC5C,GAAG,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;oBAC7B,OAAO,GAAG,CAAC;gBACb,CAAC,EAAE,EAAyB,CAAC,CAAC;gBAE9B,IAAI,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;oBAAE,SAAS;gBAEvE,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;gBAElB,IAAI,IAAI,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;oBAC7B,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;oBAC9B,cAAc,GAAG,IAAI,CAAC;oBACtB,IAAI,GAAG,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC;QAIH,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpB,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;YAC9B,cAAc,GAAG,IAAI,CAAC;QACxB,CAAC;QAGD,IAAI,CAAC,cAAc,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1C,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QAC9B,CAAC;IACH,CAAC;CAEF,CAAA;AA7IY,oCAAY;uBAAZ,YAAY;IADxB,IAAA,mBAAU,GAAE;GACA,YAAY,CA6IxB","sourcesContent":["import { Injectable, Logger } from '@nestjs/common';\nimport * as ExcelJS from 'exceljs';\nimport { ERROR_MESSAGES } from 'src/constants/error-messages';\nimport { PassThrough, Readable } from 'stream';\n\n\nexport interface ExcelReadOptions {\n pageSize?: number; // Number of records per page\n hasHeaderRow?: boolean; // Whether the first row contains headers\n providedHeaders?: string[]; // Custom headers if hasHeaderRow is false\n}\n\nconst DEFAULT_PAGE_SIZE = 100; // Default page size if not provided\n\nexport interface ExcelReadResult {\n headers: string[]; // Headers of the Excel file\n data: Record<string, any>[]; // Data records in the current page\n}\n\n@Injectable()\nexport class ExcelService {\n private logger = new Logger(ExcelService.name);\n\n public async createExcelStream(\n getDataRecords: (chunkIndex: number, chunkSize: number) => Promise<any[]>,\n chunkSize: number = 100,\n headers: string[] = []\n ): Promise<Readable> {\n // Validations\n // If neither headers nor data records function is provided, throw an error\n if (headers.length === 0 && typeof getDataRecords !== 'function') {\n throw new Error(ERROR_MESSAGES.MISSING_HEADERS_OR_FUNCTION);\n }\n\n // If data records function is provided, chunkSize must be greater than 0\n if (getDataRecords && chunkSize <= 0) {\n throw new Error(ERROR_MESSAGES.INVALID_CHUNK_SIZE);\n }\n\n const passThrough = new PassThrough(); // Create streaming pipe\n try {\n const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({ stream: passThrough });\n const worksheet = workbook.addWorksheet('Data');\n\n // If headers are provided, use them;\n let isHeaderWritten = false;\n if (headers.length > 0) {\n worksheet.columns = headers.map((header) => ({\n header: header, // Convert header names to uppercase\n key: header,\n width: 20, // Set column width\n }));\n isHeaderWritten = true; // Mark headers as written\n }\n\n // ✅ If no data loader provided, write only headers and finish\n if (typeof getDataRecords !== 'function') {\n // worksheet.addRow(\n // headers.reduce((acc, header) => ({ ...acc, [header]: '' }), {})\n // ).commit(); // Write a dummy record with headers\n\n workbook.commit();\n return passThrough;\n }\n\n // Write the data records in chunks\n let chunkIndex = 0;\n while (true) {\n const records = await getDataRecords(chunkIndex, chunkSize); // Fetch chunked data\n if (records.length === 0) break; // Stop if no more records\n\n if (!isHeaderWritten) { // Falback because without columns being set, ExcelJS won't write data correctly\n worksheet.columns = Object.keys(records[0]).map((key) => ({\n header: key.toUpperCase(),\n key: key,\n width: 20,\n }));\n isHeaderWritten = true;\n }\n\n records.forEach((item) => {\n worksheet.addRow(item).commit(); // Commit each row immediately\n });\n\n chunkIndex++; // Fetch next chunk\n this.logger.debug(`✅ Chunk ${chunkIndex} written to Excel`);\n }\n\n workbook.commit();\n // passThrough.end(); // ✅ Properly close the stream\n } catch (error) {\n this.logger.error(`❌ Error writing Excel: ${error.message}`);\n passThrough.destroy(error); // Destroy stream\n throw error;\n }\n return passThrough; // Return streaming response\n }\n\n public async *readExcelInPagesFromStream(\n stream: Readable,\n options?: ExcelReadOptions\n ): AsyncGenerator<ExcelReadResult> {\n const { pageSize = DEFAULT_PAGE_SIZE, hasHeaderRow = true, providedHeaders = [] } = options || {};\n const workbookReader = new ExcelJS.stream.xlsx.WorkbookReader(stream, {});\n\n let headers: string[] = [];\n let page: Record<string, any>[] = [];\n let isFirstRow = true;\n let hasYieldedData = false;\n\n for await (const worksheet of workbookReader) {\n for await (const row of worksheet) {\n const values = Array.isArray(row.values) ? row.values.slice(1) : [];\n\n if (isFirstRow) {\n isFirstRow = false;\n\n if (hasHeaderRow) {\n headers = values.map(v => v?.toString().trim() || '');\n continue;\n } else if (providedHeaders.length) {\n headers = providedHeaders;\n } else {\n headers = values.map((_, idx) => `${idx}`);\n }\n }\n\n while (values.length < headers.length) values.push(null);\n if (values.length > headers.length) values.length = headers.length;\n\n const record = headers.reduce((acc, key, i) => {\n acc[key] = values[i] ?? null;\n return acc;\n }, {} as Record<string, any>);\n\n if (Object.values(record).every(v => v === null || v === '')) continue;\n\n page.push(record);\n\n if (page.length === pageSize) {\n yield { headers, data: page };\n hasYieldedData = true;\n page = [];\n }\n }\n\n // Optional: break if only processing first worksheet\n // break;\n }\n\n if (page.length > 0) {\n yield { headers, data: page };\n hasYieldedData = true;\n }\n\n // ✅ Yield headers with empty data if only headers were found\n if (!hasYieldedData && headers.length > 0) {\n yield { headers, data: [] };\n }\n }\n\n}"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"import-transaction.service.d.ts","sourceRoot":"","sources":["../../src/services/import-transaction.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE3D,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAI7E,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,6BAA6B,EAAyC,MAAM,kCAAkC,CAAC;AAExH,OAAO,EAAE,yBAAyB,EAAE,MAAM,kDAAkD,CAAC;AAK7F,OAAO,EAAE,iBAAiB,EAAE,MAAM,uCAAuC,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"import-transaction.service.d.ts","sourceRoot":"","sources":["../../src/services/import-transaction.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE3D,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,WAAW,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AAE,UAAU,EACtB,WAAW,EAAE,WAAW,EACxB,iBAAiB,EAAE,sBAAsB;IAMpD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA6C;IAO9D,iBAAiB,CAAC,eAAe,EAAE,MAAM,EAAE,MAAM,GAAE,YAA+B,GAAG,OAAO,CAAC,sBAAsB,CAAC;IAsCpH,qBAAqB,CAAC,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,6BAA6B,CAAC;IA8DtF,oBAAoB,CAAC,mBAAmB,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAuC7E,eAAe,CAAC,mBAAmB,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC;IA4B7E,gBAAgB,CAAC,mBAAmB,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAItD,2BAA2B,CAAC,mBAAmB,EAAE,MAAM;;;;;YA+D/C,sBAAsB;YAgBtB,qBAAqB;IAWnC,OAAO,CAAC,sBAAsB;YAchB,oBAAoB;IAkBlC,OAAO,CAAC,mBAAmB;YAQb,mBAAmB;YAkBnB,kBAAkB;YAuClB,aAAa;IAyBrB,mBAAmB,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,GAAG;YAoBzF,YAAY;IAQ1B,OAAO,CAAC,eAAe;YAUT,0BAA0B;YAuB1B,mBAAmB;IAmCjC,OAAO,CAAC,6BAA6B;IAyBrC,OAAO,CAAC,qBAAqB;IAgB7B,OAAO,CAAC,oBAAoB;IAc5B,OAAO,CAAC,kBAAkB;YAwBZ,uBAAuB;YAoBvB,+BAA+B;CA2B9C"}
|
|
@@ -34,6 +34,7 @@ const csv_service_1 = require("./csv.service");
|
|
|
34
34
|
const excel_service_1 = require("./excel.service");
|
|
35
35
|
const solid_introspect_service_1 = require("./solid-introspect.service");
|
|
36
36
|
const error_messages_1 = require("../constants/error-messages");
|
|
37
|
+
const date_helper_1 = require("../helpers/date.helper");
|
|
37
38
|
var ImportFormat;
|
|
38
39
|
(function (ImportFormat) {
|
|
39
40
|
ImportFormat["CSV"] = "csv";
|
|
@@ -490,9 +491,11 @@ let ImportTransactionService = ImportTransactionService_1 = class ImportTransact
|
|
|
490
491
|
dtoRecord[fieldMetadata.name] = null;
|
|
491
492
|
return dtoRecord;
|
|
492
493
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
494
|
+
console.log(cellValue, 'cellValue');
|
|
495
|
+
const dateValue = (0, date_helper_1.parseFlexibleDate)(cellValue);
|
|
496
|
+
console.log(dateValue, 'dateValue');
|
|
497
|
+
if (!dateValue) {
|
|
498
|
+
throw new Error(`Invalid date value for cell ${key} with value ${cellValue}`);
|
|
496
499
|
}
|
|
497
500
|
dtoRecord[fieldMetadata.name] = dateValue;
|
|
498
501
|
return dtoRecord;
|