@credal/actions 0.1.43 → 0.1.45

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.
@@ -6,3 +6,4 @@ export declare function getSnowflakeConnection(snowflakeData: {
6
6
  warehouse: string;
7
7
  database: string;
8
8
  }, authParams: AuthParamsType): Connection;
9
+ export declare function connectToSnowflakeAndWarehouse(connection: Connection, warehouse?: string): Promise<void>;
@@ -1,16 +1,71 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
2
44
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
45
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
46
  };
5
47
  Object.defineProperty(exports, "__esModule", { value: true });
6
48
  exports.getSnowflakeConnection = getSnowflakeConnection;
7
- const crypto_1 = __importDefault(require("crypto"));
49
+ exports.connectToSnowflakeAndWarehouse = connectToSnowflakeAndWarehouse;
8
50
  const snowflake_sdk_1 = __importDefault(require("snowflake-sdk"));
51
+ const forge = __importStar(require("node-forge"));
52
+ const getPrivateKeyCorrectFormat = (privateKey) => {
53
+ try {
54
+ // Parse the private key
55
+ const pemKey = forge.pem.decode(privateKey)[0];
56
+ // Re-encode it properly with correct formatting
57
+ return forge.pem.encode(pemKey);
58
+ }
59
+ catch (error) {
60
+ console.error("Error processing private key:", error);
61
+ throw new Error("Invalid private key format. Please check the key format and try again.");
62
+ }
63
+ };
9
64
  function getSnowflakeConnection(snowflakeData, authParams) {
10
65
  const { authToken, apiKey } = authParams;
11
66
  const { account, username, warehouse, database } = snowflakeData;
12
67
  if (authToken) {
13
- // Always try to use Nango-Snowflake OAuth
68
+ // Always try to use Nango-Snowflake OAuth (unused for now)
14
69
  return snowflake_sdk_1.default.createConnection({
15
70
  account: account,
16
71
  username: username,
@@ -22,20 +77,6 @@ function getSnowflakeConnection(snowflakeData, authParams) {
22
77
  });
23
78
  }
24
79
  else if (apiKey) {
25
- // Use the apiKey for authentication (one off)
26
- const getPrivateKeyCorrectFormat = (privateKey) => {
27
- const buffer = Buffer.from(privateKey);
28
- const privateKeyObject = crypto_1.default.createPrivateKey({
29
- key: buffer,
30
- format: "pem",
31
- passphrase: "password",
32
- });
33
- const privateKeyCorrectFormat = privateKeyObject.export({
34
- format: "pem",
35
- type: "pkcs8",
36
- });
37
- return privateKeyCorrectFormat.toString();
38
- };
39
80
  const privateKeyCorrectFormatString = getPrivateKeyCorrectFormat(apiKey);
40
81
  return snowflake_sdk_1.default.createConnection({
41
82
  account: account,
@@ -51,3 +92,30 @@ function getSnowflakeConnection(snowflakeData, authParams) {
51
92
  throw new Error("Snowflake authToken or apiKey is required");
52
93
  }
53
94
  }
95
+ function connectToSnowflakeAndWarehouse(connection, warehouse) {
96
+ return __awaiter(this, void 0, void 0, function* () {
97
+ yield new Promise((resolve, reject) => {
98
+ connection.connect((err, conn) => {
99
+ if (err) {
100
+ console.error("Unable to connect to Snowflake:", err.message);
101
+ return reject(err);
102
+ }
103
+ resolve(conn);
104
+ });
105
+ });
106
+ if (warehouse) {
107
+ yield new Promise((resolve, reject) => {
108
+ connection.execute({
109
+ sqlText: `USE WAREHOUSE ${warehouse}`,
110
+ complete: (err, stmt, rows) => {
111
+ if (err) {
112
+ console.error("Unable to use warehouse:", err.message);
113
+ return reject(err);
114
+ }
115
+ resolve(rows);
116
+ },
117
+ });
118
+ });
119
+ }
120
+ });
121
+ }
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const snowflake_sdk_1 = __importDefault(require("snowflake-sdk"));
16
16
  const getSnowflakeConnection_1 = require("./auth/getSnowflakeConnection");
17
+ const formatDataForCodeInterpreter_1 = require("../../util/formatDataForCodeInterpreter");
17
18
  snowflake_sdk_1.default.configure({ logLevel: "ERROR" });
18
19
  const runSnowflakeQuery = (_a) => __awaiter(void 0, [_a], void 0, function* ({ params, authParams, }) {
19
20
  const { databaseName, warehouse, query, user, accountName, outputFormat = "json" } = params;
@@ -34,24 +35,8 @@ const runSnowflakeQuery = (_a) => __awaiter(void 0, [_a], void 0, function* ({ p
34
35
  });
35
36
  });
36
37
  // Format the results based on the output format
37
- let formattedData;
38
- if (outputFormat.toLowerCase() === "csv") {
39
- if (queryResults.length === 0) {
40
- formattedData = "";
41
- }
42
- else {
43
- const headers = Object.keys(queryResults[0]).join(",");
44
- const rows = queryResults.map(row => Object.values(row)
45
- .map(value => (typeof value === "object" && value !== null ? JSON.stringify(value) : value))
46
- .join(","));
47
- formattedData = [headers, ...rows].join("\n");
48
- }
49
- }
50
- else {
51
- // Default to JSON
52
- formattedData = JSON.stringify(queryResults).replace(/\s+/g, "");
53
- }
54
- return { formattedData, resultsLength: queryResults.length };
38
+ const { formattedData, resultsLength } = (0, formatDataForCodeInterpreter_1.formatDataForCodeInterpreter)(queryResults, outputFormat);
39
+ return { formattedData, resultsLength };
55
40
  });
56
41
  // Set up a connection using snowflake-sdk
57
42
  const connection = (0, getSnowflakeConnection_1.getSnowflakeConnection)({
@@ -62,15 +47,7 @@ const runSnowflakeQuery = (_a) => __awaiter(void 0, [_a], void 0, function* ({ p
62
47
  }, { authToken: authParams.authToken, apiKey: authParams.apiKey });
63
48
  try {
64
49
  // Connect to Snowflake
65
- yield new Promise((resolve, reject) => {
66
- connection.connect((err, conn) => {
67
- if (err) {
68
- console.error("Unable to connect to Snowflake:", err.message);
69
- return reject(err);
70
- }
71
- resolve(conn);
72
- });
73
- });
50
+ yield (0, getSnowflakeConnection_1.connectToSnowflakeAndWarehouse)(connection, warehouse);
74
51
  const { formattedData, resultsLength } = yield executeQueryAndFormatData();
75
52
  // Return fields to match schema definition
76
53
  connection.destroy(err => {
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Formats query results into either CSV or JSON format for code interpreter
3
+ * @param queryResults The results from a database query
4
+ * @param outputFormat The desired output format ("csv" or "json")
5
+ * @returns Object containing formatted data and result count
6
+ */
7
+ export declare function formatDataForCodeInterpreter(queryResults: any[], outputFormat?: string): {
8
+ formattedData: string;
9
+ resultsLength: number;
10
+ };
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.formatDataForCodeInterpreter = formatDataForCodeInterpreter;
5
+ /**
6
+ * Formats query results into either CSV or JSON format for code interpreter
7
+ * @param queryResults The results from a database query
8
+ * @param outputFormat The desired output format ("csv" or "json")
9
+ * @returns Object containing formatted data and result count
10
+ */
11
+ function formatDataForCodeInterpreter(queryResults, outputFormat = "json") {
12
+ let formattedData;
13
+ if (outputFormat.toLowerCase() === "csv") {
14
+ if (queryResults.length === 0) {
15
+ formattedData = "";
16
+ }
17
+ else {
18
+ const headers = Object.keys(queryResults[0]).join(",");
19
+ // Helper function to properly escape CSV fields
20
+ const escapeCsvField = (field) => {
21
+ if (field === null || field === undefined)
22
+ return "";
23
+ const stringValue = typeof field === "object" ? JSON.stringify(field) : String(field);
24
+ // If the field contains commas, quotes, or newlines, it needs to be quoted
25
+ if (stringValue.includes(",") ||
26
+ stringValue.includes('"') ||
27
+ stringValue.includes("\n") ||
28
+ stringValue.includes("\r")) {
29
+ // Escape quotes by doubling them and wrap in quotes
30
+ return '"' + stringValue.replace(/"/g, '""') + '"';
31
+ }
32
+ return stringValue;
33
+ };
34
+ const rows = queryResults.map(row => Object.values(row).map(escapeCsvField).join(","));
35
+ formattedData = [headers, ...rows].join("\n");
36
+ }
37
+ }
38
+ else {
39
+ // Default to JSON - keep it formatted for readability
40
+ formattedData = JSON.stringify(queryResults, null, 2);
41
+ }
42
+ return { formattedData, resultsLength: queryResults.length };
43
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@credal/actions",
3
- "version": "0.1.43",
3
+ "version": "0.1.45",
4
4
  "description": "AI Actions by Credal AI",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -27,6 +27,7 @@
27
27
  "@types/js-yaml": "^4.0.9",
28
28
  "@types/jsonwebtoken": "^9.0.9",
29
29
  "@types/node": "^22.10.1",
30
+ "@types/node-forge": "^1.3.11",
30
31
  "@typescript-eslint/eslint-plugin": "^8.18.0",
31
32
  "@typescript-eslint/parser": "^8.18.0",
32
33
  "eslint": "^9.16.0",
@@ -52,6 +53,7 @@
52
53
  "json-schema-to-zod": "^2.5.0",
53
54
  "jsonwebtoken": "^9.0.2",
54
55
  "mongodb": "^6.13.1",
56
+ "node-forge": "^1.3.1",
55
57
  "octokit": "^4.1.2",
56
58
  "resend": "^4.1.2",
57
59
  "snowflake-sdk": "^2.0.2",