@minded-ai/mindedjs 1.0.63 → 1.0.64-patch1

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/cli/index.js CHANGED
@@ -37,6 +37,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
37
37
  const fs = __importStar(require("fs"));
38
38
  const path = __importStar(require("path"));
39
39
  const logger_1 = require("../utils/logger");
40
+ const child_process_1 = require("child_process");
40
41
  const ENV_FILE = '.env';
41
42
  function getEnvFilePath() {
42
43
  return path.join(process.cwd(), ENV_FILE);
@@ -68,6 +69,102 @@ function setToken(token) {
68
69
  }
69
70
  }
70
71
  }
72
+ function generateLambdaHandler() {
73
+ const mindedConfigPath = path.join(process.cwd(), 'minded.json');
74
+ // Check if minded.json exists
75
+ if (!fs.existsSync(mindedConfigPath)) {
76
+ logger_1.logger.error('minded.json not found in the current directory');
77
+ process.exit(1);
78
+ }
79
+ // Read and parse minded.json
80
+ let mindedConfig;
81
+ try {
82
+ const configContent = fs.readFileSync(mindedConfigPath, 'utf8');
83
+ mindedConfig = JSON.parse(configContent);
84
+ }
85
+ catch (error) {
86
+ logger_1.logger.error('Failed to read or parse minded.json:', error);
87
+ process.exit(1);
88
+ }
89
+ // Get the agent path
90
+ const agentPath = mindedConfig.agent;
91
+ if (!agentPath) {
92
+ logger_1.logger.error('No agent path found in minded.json');
93
+ process.exit(1);
94
+ }
95
+ // Remove .ts or .js extension if present and convert to relative path
96
+ const modulePath = agentPath.replace(/\.(ts|js)$/, '');
97
+ // Get the path to the template file
98
+ // Try multiple locations to support both development and npm package scenarios
99
+ const templatePaths = [
100
+ path.join(__dirname, 'lambdaHandlerTemplate.ts'), // For npm package
101
+ path.join(__dirname, '..', '..', 'src', 'cli', 'lambdaHandlerTemplate.ts'), // For development
102
+ ];
103
+ let templateContent = null;
104
+ // Try each path until we find the template
105
+ for (const templatePath of templatePaths) {
106
+ try {
107
+ if (fs.existsSync(templatePath)) {
108
+ templateContent = fs.readFileSync(templatePath, 'utf8');
109
+ logger_1.logger.debug(`Found template at ${templatePath}`);
110
+ break;
111
+ }
112
+ }
113
+ catch (_a) {
114
+ // Continue to next path
115
+ }
116
+ }
117
+ if (!templateContent) {
118
+ logger_1.logger.error('Could not find Lambda handler template file');
119
+ process.exit(1);
120
+ }
121
+ try {
122
+ // Replace the placeholder with the actual module path
123
+ templateContent = templateContent.replace(/{MODULE_PATH}/g, modulePath);
124
+ // Write the Lambda handler to index.ts at the root
125
+ const outputPath = path.join(process.cwd(), 'index.ts');
126
+ fs.writeFileSync(outputPath, templateContent);
127
+ logger_1.logger.info(`Generated Lambda handler at ${outputPath}`);
128
+ // Compile the index.ts file using TypeScript
129
+ logger_1.logger.info('Compiling the Lambda handler...');
130
+ try {
131
+ // Read tsconfig.json to get the outDir
132
+ let outDir = 'dist'; // Default value
133
+ const tsconfigPath = path.join(process.cwd(), 'tsconfig.json');
134
+ if (fs.existsSync(tsconfigPath)) {
135
+ try {
136
+ const tsconfigContent = fs.readFileSync(tsconfigPath, 'utf8');
137
+ const tsconfig = JSON.parse(tsconfigContent);
138
+ if (tsconfig.compilerOptions && tsconfig.compilerOptions.outDir) {
139
+ outDir = tsconfig.compilerOptions.outDir;
140
+ logger_1.logger.debug(`Using outDir from tsconfig.json: ${outDir}`);
141
+ }
142
+ }
143
+ catch (error) {
144
+ logger_1.logger.warn('Failed to parse tsconfig.json, using default outDir: dist');
145
+ }
146
+ }
147
+ // Check if output directory exists, create if it doesn't
148
+ const distDir = path.join(process.cwd(), outDir);
149
+ if (!fs.existsSync(distDir)) {
150
+ fs.mkdirSync(distDir, { recursive: true });
151
+ }
152
+ // Compile index.ts to the configured output directory
153
+ (0, child_process_1.execSync)(`npx tsc ${outputPath} --outDir ${outDir}`, {
154
+ stdio: 'pipe',
155
+ cwd: process.cwd(),
156
+ });
157
+ logger_1.logger.info(`Successfully compiled Lambda handler to ${outDir}/index.js`);
158
+ }
159
+ catch (compileError) {
160
+ logger_1.logger.error({ message: 'Failed to compile Lambda handler', error: compileError });
161
+ }
162
+ }
163
+ catch (error) {
164
+ logger_1.logger.error({ message: 'Failed to generate Lambda handler', error: error });
165
+ process.exit(1);
166
+ }
167
+ }
71
168
  function main() {
72
169
  const args = process.argv.slice(2);
73
170
  const command = args[0];
@@ -79,8 +176,11 @@ function main() {
79
176
  }
80
177
  setToken(token);
81
178
  }
179
+ else if (command === 'generate-lambda-ts-handler') {
180
+ generateLambdaHandler();
181
+ }
82
182
  else {
83
- logger_1.logger.error('Unknown command. Available commands: token');
183
+ logger_1.logger.error('Unknown command. Available commands: token, generate-lambda-ts-handler');
84
184
  process.exit(1);
85
185
  }
86
186
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cli/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,uCAAyB;AACzB,2CAA6B;AAC7B,4CAAyC;AAEzC,MAAM,QAAQ,GAAG,MAAM,CAAC;AAExB,SAAS,cAAc;IACrB,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,QAAQ,CAAC,KAAa;IAC7B,MAAM,OAAO,GAAG,cAAc,EAAE,CAAC;IACjC,MAAM,SAAS,GAAG,2BAA2B,KAAK,EAAE,CAAC;IAErD,4BAA4B;IAC5B,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5B,kCAAkC;QAClC,EAAE,CAAC,aAAa,CAAC,OAAO,EAAE,SAAS,GAAG,IAAI,CAAC,CAAC;QAC5C,eAAM,CAAC,IAAI,CAAC,WAAW,OAAO,kBAAkB,CAAC,CAAC;IACpD,CAAC;SAAM,CAAC;QACN,0BAA0B;QAC1B,MAAM,UAAU,GAAG,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAEpD,kDAAkD;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C5E,CAAC;QAAC,OAAO,YAAY,EAAE,CAAC;YACtB,eAAM,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,kCAAkC,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAC;QACrF,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,eAAM,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,mCAAmC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QAC7E,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,SAAS,IAAI;IACX,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IAExB,IAAI,OAAO,KAAK,OAAO,EAAE,CAAC;QACxB,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,eAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;YACvC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;QACD,QAAQ,CAAC,KAAK,CAAC,CAAC;IAClB,CAAC;SAAM,IAAI,OAAO,KAAK,4BAA4B,EAAE,CAAC;QACpD,qBAAqB,EAAE,CAAC;IAC1B,CAAC;SAAM,CAAC;QACN,eAAM,CAAC,KAAK,CAAC,wEAAwE,CAAC,CAAC;QACvF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC"}
@@ -0,0 +1,10 @@
1
+ export declare const handler: (event: any, context: any) => Promise<{
2
+ statusCode: number;
3
+ headers: {
4
+ 'Content-Type': string;
5
+ 'Access-Control-Allow-Origin': string;
6
+ 'Access-Control-Allow-Credentials': boolean;
7
+ };
8
+ body: string;
9
+ }>;
10
+ //# sourceMappingURL=lambdaHandlerTemplate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lambdaHandlerTemplate.d.ts","sourceRoot":"","sources":["../../src/cli/lambdaHandlerTemplate.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,OAAO,GAAU,OAAO,GAAG,EAAE,SAAS,GAAG;;;;;;;;EA0CrD,CAAC"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ // This is the template for the Lambda handler
3
+ // The {MODULE_PATH} placeholder will be replaced with the actual path
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.handler = void 0;
6
+ const handler = async (event, context) => {
7
+ const modulePath = '{MODULE_PATH}'; // Module path placeholder - DO NOT CHANGE THE {MODULE_PATH} TOKEN
8
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
9
+ const resolvedPath = require.resolve(modulePath);
10
+ delete require.cache[resolvedPath];
11
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
12
+ const agent = require(modulePath).default;
13
+ const { agentFunctionName, agentFunctionBody } = event.body ? JSON.parse(event.body) : event;
14
+ if (!agent[agentFunctionName]) {
15
+ throw new Error('Unknown agentFunctionName: ' + agentFunctionName);
16
+ }
17
+ const response = await agent[agentFunctionName](agentFunctionBody);
18
+ // Safely stringify the response. If it fails (e.g., due to circular refs
19
+ // or non-serialisable class instances), fall back to null.
20
+ let responseBody = null;
21
+ try {
22
+ JSON.stringify(response);
23
+ responseBody = response;
24
+ }
25
+ catch (_a) {
26
+ // ignored - responseBody stays null
27
+ }
28
+ if (agentFunctionName === 'startVoiceSession') {
29
+ const wait = (ms) => new Promise((r) => setTimeout(r, ms));
30
+ while (context.getRemainingTimeInMillis() > 1000) {
31
+ console.log('Lambda remaining time in seconds: ', context.getRemainingTimeInMillis() / 1000);
32
+ await wait(1000); // sleep 1 s
33
+ }
34
+ }
35
+ return {
36
+ statusCode: 200,
37
+ headers: {
38
+ 'Content-Type': 'application/json',
39
+ 'Access-Control-Allow-Origin': '*',
40
+ 'Access-Control-Allow-Credentials': true,
41
+ },
42
+ body: JSON.stringify(responseBody),
43
+ };
44
+ };
45
+ exports.handler = handler;
46
+ //# sourceMappingURL=lambdaHandlerTemplate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lambdaHandlerTemplate.js","sourceRoot":"","sources":["../../src/cli/lambdaHandlerTemplate.ts"],"names":[],"mappings":";AAAA,8CAA8C;AAC9C,sEAAsE;;;AAE/D,MAAM,OAAO,GAAG,KAAK,EAAE,KAAU,EAAE,OAAY,EAAE,EAAE;IACxD,MAAM,UAAU,GAAG,eAAe,CAAC,CAAC,kEAAkE;IACtG,iEAAiE;IACjE,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACjD,OAAO,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACnC,iEAAiE;IACjE,MAAM,KAAK,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC;IAC1C,MAAM,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IAE7F,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,6BAA6B,GAAG,iBAAiB,CAAC,CAAC;IACrE,CAAC;IAED,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,iBAAiB,CAAC,CAAC,iBAAiB,CAAC,CAAC;IAEnE,yEAAyE;IACzE,2DAA2D;IAC3D,IAAI,YAAY,GAAG,IAAI,CAAC;IACxB,IAAI,CAAC;QACH,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QACzB,YAAY,GAAG,QAAQ,CAAC;IAC1B,CAAC;IAAC,WAAM,CAAC;QACP,oCAAoC;IACtC,CAAC;IAED,IAAI,iBAAiB,KAAK,mBAAmB,EAAE,CAAC;QAC9C,MAAM,IAAI,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QACnE,OAAO,OAAO,CAAC,wBAAwB,EAAE,GAAG,IAAI,EAAE,CAAC;YACjD,OAAO,CAAC,GAAG,CAAC,oCAAoC,EAAE,OAAO,CAAC,wBAAwB,EAAE,GAAG,IAAI,CAAC,CAAC;YAC7F,MAAM,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY;QAChC,CAAC;IACH,CAAC;IAED,OAAO;QACL,UAAU,EAAE,GAAG;QACf,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,6BAA6B,EAAE,GAAG;YAClC,kCAAkC,EAAE,IAAI;SACzC;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC;KACnC,CAAC;AACJ,CAAC,CAAC;AA1CW,QAAA,OAAO,WA0ClB"}
@@ -0,0 +1,46 @@
1
+ // This is the template for the Lambda handler
2
+ // The {MODULE_PATH} placeholder will be replaced with the actual path
3
+
4
+ export const handler = async (event: any, context: any) => {
5
+ const modulePath = '{MODULE_PATH}'; // Module path placeholder - DO NOT CHANGE THE {MODULE_PATH} TOKEN
6
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
7
+ const resolvedPath = require.resolve(modulePath);
8
+ delete require.cache[resolvedPath];
9
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
10
+ const agent = require(modulePath).default;
11
+ const { agentFunctionName, agentFunctionBody } = event.body ? JSON.parse(event.body) : event;
12
+
13
+ if (!agent[agentFunctionName]) {
14
+ throw new Error('Unknown agentFunctionName: ' + agentFunctionName);
15
+ }
16
+
17
+ const response = await agent[agentFunctionName](agentFunctionBody);
18
+
19
+ // Safely stringify the response. If it fails (e.g., due to circular refs
20
+ // or non-serialisable class instances), fall back to null.
21
+ let responseBody = null;
22
+ try {
23
+ JSON.stringify(response);
24
+ responseBody = response;
25
+ } catch {
26
+ // ignored - responseBody stays null
27
+ }
28
+
29
+ if (agentFunctionName === 'startVoiceSession') {
30
+ const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
31
+ while (context.getRemainingTimeInMillis() > 1000) {
32
+ console.log('Lambda remaining time in seconds: ', context.getRemainingTimeInMillis() / 1000);
33
+ await wait(1000); // sleep 1 s
34
+ }
35
+ }
36
+
37
+ return {
38
+ statusCode: 200,
39
+ headers: {
40
+ 'Content-Type': 'application/json',
41
+ 'Access-Control-Allow-Origin': '*',
42
+ 'Access-Control-Allow-Credentials': true,
43
+ },
44
+ body: JSON.stringify(responseBody),
45
+ };
46
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minded-ai/mindedjs",
3
- "version": "1.0.63",
3
+ "version": "1.0.64-patch1",
4
4
  "description": "MindedJS is a TypeScript library for building agents.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -12,7 +12,8 @@
12
12
  "src"
13
13
  ],
14
14
  "scripts": {
15
- "build": "tsc",
15
+ "build": "tsc && npm run copy-templates",
16
+ "copy-templates": "cp -r src/cli/lambdaHandlerTemplate.ts dist/cli/",
16
17
  "watch": "tsc -w",
17
18
  "test": "NODE_ENV=test mocha --parallel --jobs auto --retries 1 -r ts-node/register -r ./test/setup.ts \"test/**/*.test.ts\"",
18
19
  "testFile": "NODE_ENV=test mocha -r ts-node/register -r ./test/setup.ts",
@@ -26,6 +27,7 @@
26
27
  "license": "ISC",
27
28
  "devDependencies": {
28
29
  "@eslint/js": "^9.27.0",
30
+ "@types/aws-lambda": "^8.10.150",
29
31
  "@types/chai": "^4.3.11",
30
32
  "@types/mocha": "^10.0.6",
31
33
  "@types/node": "^20.11.19",
package/src/cli/index.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  import * as fs from 'fs';
4
4
  import * as path from 'path';
5
5
  import { logger } from '../utils/logger';
6
+ import { execSync } from 'child_process';
6
7
 
7
8
  const ENV_FILE = '.env';
8
9
 
@@ -38,6 +39,115 @@ function setToken(token: string): void {
38
39
  }
39
40
  }
40
41
 
42
+ function generateLambdaHandler(): void {
43
+ const mindedConfigPath = path.join(process.cwd(), 'minded.json');
44
+
45
+ // Check if minded.json exists
46
+ if (!fs.existsSync(mindedConfigPath)) {
47
+ logger.error('minded.json not found in the current directory');
48
+ process.exit(1);
49
+ }
50
+
51
+ // Read and parse minded.json
52
+ let mindedConfig;
53
+ try {
54
+ const configContent = fs.readFileSync(mindedConfigPath, 'utf8');
55
+ mindedConfig = JSON.parse(configContent);
56
+ } catch (error) {
57
+ logger.error('Failed to read or parse minded.json:', error);
58
+ process.exit(1);
59
+ }
60
+
61
+ // Get the agent path
62
+ const agentPath = mindedConfig.agent;
63
+ if (!agentPath) {
64
+ logger.error('No agent path found in minded.json');
65
+ process.exit(1);
66
+ }
67
+
68
+ // Remove .ts or .js extension if present and convert to relative path
69
+ const modulePath = agentPath.replace(/\.(ts|js)$/, '');
70
+
71
+ // Get the path to the template file
72
+ // Try multiple locations to support both development and npm package scenarios
73
+ const templatePaths = [
74
+ path.join(__dirname, 'lambdaHandlerTemplate.ts'), // For npm package
75
+ path.join(__dirname, '..', '..', 'src', 'cli', 'lambdaHandlerTemplate.ts'), // For development
76
+ ];
77
+
78
+ let templateContent: string | null = null;
79
+
80
+ // Try each path until we find the template
81
+ for (const templatePath of templatePaths) {
82
+ try {
83
+ if (fs.existsSync(templatePath)) {
84
+ templateContent = fs.readFileSync(templatePath, 'utf8');
85
+ logger.debug(`Found template at ${templatePath}`);
86
+ break;
87
+ }
88
+ } catch {
89
+ // Continue to next path
90
+ }
91
+ }
92
+
93
+ if (!templateContent) {
94
+ logger.error('Could not find Lambda handler template file');
95
+ process.exit(1);
96
+ }
97
+
98
+ try {
99
+ // Replace the placeholder with the actual module path
100
+ templateContent = templateContent.replace(/{MODULE_PATH}/g, modulePath);
101
+
102
+ // Write the Lambda handler to index.ts at the root
103
+ const outputPath = path.join(process.cwd(), 'index.ts');
104
+ fs.writeFileSync(outputPath, templateContent);
105
+ logger.info(`Generated Lambda handler at ${outputPath}`);
106
+
107
+ // Compile the index.ts file using TypeScript
108
+ logger.info('Compiling the Lambda handler...');
109
+
110
+ try {
111
+ // Read tsconfig.json to get the outDir
112
+ let outDir = 'dist'; // Default value
113
+ const tsconfigPath = path.join(process.cwd(), 'tsconfig.json');
114
+
115
+ if (fs.existsSync(tsconfigPath)) {
116
+ try {
117
+ const tsconfigContent = fs.readFileSync(tsconfigPath, 'utf8');
118
+ const tsconfig = JSON.parse(tsconfigContent);
119
+
120
+ if (tsconfig.compilerOptions && tsconfig.compilerOptions.outDir) {
121
+ outDir = tsconfig.compilerOptions.outDir;
122
+ logger.debug(`Using outDir from tsconfig.json: ${outDir}`);
123
+ }
124
+ } catch (error) {
125
+ logger.warn('Failed to parse tsconfig.json, using default outDir: dist');
126
+ }
127
+ }
128
+
129
+ // Check if output directory exists, create if it doesn't
130
+ const distDir = path.join(process.cwd(), outDir);
131
+ if (!fs.existsSync(distDir)) {
132
+ fs.mkdirSync(distDir, { recursive: true });
133
+ }
134
+
135
+ // Compile index.ts to the configured output directory
136
+ execSync(`npx tsc ${outputPath} --outDir ${outDir}`, {
137
+ stdio: 'pipe',
138
+ cwd: process.cwd(),
139
+ });
140
+
141
+ logger.info(`Successfully compiled Lambda handler to ${outDir}/index.js`);
142
+ } catch (compileError) {
143
+ logger.error({ message: 'Failed to compile Lambda handler', error: compileError });
144
+ }
145
+ } catch (error) {
146
+ logger.error({ message: 'Failed to generate Lambda handler', error: error });
147
+ process.exit(1);
148
+ }
149
+ }
150
+
41
151
  function main() {
42
152
  const args = process.argv.slice(2);
43
153
  const command = args[0];
@@ -49,8 +159,10 @@ function main() {
49
159
  process.exit(1);
50
160
  }
51
161
  setToken(token);
162
+ } else if (command === 'generate-lambda-ts-handler') {
163
+ generateLambdaHandler();
52
164
  } else {
53
- logger.error('Unknown command. Available commands: token');
165
+ logger.error('Unknown command. Available commands: token, generate-lambda-ts-handler');
54
166
  process.exit(1);
55
167
  }
56
168
  }
@@ -0,0 +1,46 @@
1
+ // This is the template for the Lambda handler
2
+ // The {MODULE_PATH} placeholder will be replaced with the actual path
3
+
4
+ export const handler = async (event: any, context: any) => {
5
+ const modulePath = '{MODULE_PATH}'; // Module path placeholder - DO NOT CHANGE THE {MODULE_PATH} TOKEN
6
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
7
+ const resolvedPath = require.resolve(modulePath);
8
+ delete require.cache[resolvedPath];
9
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
10
+ const agent = require(modulePath).default;
11
+ const { agentFunctionName, agentFunctionBody } = event.body ? JSON.parse(event.body) : event;
12
+
13
+ if (!agent[agentFunctionName]) {
14
+ throw new Error('Unknown agentFunctionName: ' + agentFunctionName);
15
+ }
16
+
17
+ const response = await agent[agentFunctionName](agentFunctionBody);
18
+
19
+ // Safely stringify the response. If it fails (e.g., due to circular refs
20
+ // or non-serialisable class instances), fall back to null.
21
+ let responseBody = null;
22
+ try {
23
+ JSON.stringify(response);
24
+ responseBody = response;
25
+ } catch {
26
+ // ignored - responseBody stays null
27
+ }
28
+
29
+ if (agentFunctionName === 'startVoiceSession') {
30
+ const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
31
+ while (context.getRemainingTimeInMillis() > 1000) {
32
+ console.log('Lambda remaining time in seconds: ', context.getRemainingTimeInMillis() / 1000);
33
+ await wait(1000); // sleep 1 s
34
+ }
35
+ }
36
+
37
+ return {
38
+ statusCode: 200,
39
+ headers: {
40
+ 'Content-Type': 'application/json',
41
+ 'Access-Control-Allow-Origin': '*',
42
+ 'Access-Control-Allow-Credentials': true,
43
+ },
44
+ body: JSON.stringify(responseBody),
45
+ };
46
+ };