spice-js 2.6.48 → 2.6.49
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/build/cache/providers/SpiceCache.js +1 -1
- package/build/nova/Ai.js +13 -8
- package/build/utility/RestHelper.js +1 -0
- package/package.json +1 -1
- package/src/cache/providers/SpiceCache.js +1 -1
- package/src/nova/Ai.js +12 -6
- package/src/utility/RestHelper.js +1 -0
- package/build/artificial_intelligence/AI.js +0 -299
package/build/nova/Ai.js
CHANGED
|
@@ -57,7 +57,7 @@ class AI {
|
|
|
57
57
|
return uniqueDir;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
extractOrStoreFile(
|
|
60
|
+
extractOrStoreFile(fileData, outputDir, supportedFileTypes) {
|
|
61
61
|
var _this = this;
|
|
62
62
|
|
|
63
63
|
return _asyncToGenerator(function* () {
|
|
@@ -69,8 +69,15 @@ class AI {
|
|
|
69
69
|
} // Create a temporary file to store the input
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
var tempFilePath = path.join(outputDir, "temp_input");
|
|
73
|
-
|
|
72
|
+
var tempFilePath = path.join(outputDir, "temp_input"); // Check if fileData is a buffer or a stream
|
|
73
|
+
|
|
74
|
+
if (Buffer.isBuffer(fileData)) {
|
|
75
|
+
// It's a buffer, write it directly to the temp file
|
|
76
|
+
yield _fs.default.promises.writeFile(tempFilePath, fileData);
|
|
77
|
+
} else {
|
|
78
|
+
// Assume it's a stream and pipe it to the temp file
|
|
79
|
+
yield (0, _promises.pipeline)(fileData, _fs.default.createWriteStream(tempFilePath));
|
|
80
|
+
}
|
|
74
81
|
|
|
75
82
|
try {
|
|
76
83
|
// Check file type
|
|
@@ -231,9 +238,8 @@ class AI {
|
|
|
231
238
|
return (yield Promise.all(files.map(
|
|
232
239
|
/*#__PURE__*/
|
|
233
240
|
function () {
|
|
234
|
-
var _ref = _asyncToGenerator(function* (
|
|
235
|
-
|
|
236
|
-
var imagePaths = yield _this3.extractOrStoreFile(fileStream, outputDir, types);
|
|
241
|
+
var _ref = _asyncToGenerator(function* (fileData) {
|
|
242
|
+
var imagePaths = yield _this3.extractOrStoreFile(fileData, outputDir, types);
|
|
237
243
|
return imagePaths;
|
|
238
244
|
});
|
|
239
245
|
|
|
@@ -267,8 +273,7 @@ class AI {
|
|
|
267
273
|
try {
|
|
268
274
|
if (options) {
|
|
269
275
|
_this4.options = _lodash.default.merge(_this4.options, options);
|
|
270
|
-
}
|
|
271
|
-
|
|
276
|
+
}
|
|
272
277
|
|
|
273
278
|
if (_this4.Provider === undefined) {
|
|
274
279
|
throw new Error("AI provider " + _this4.provider + " is not defined");
|
package/package.json
CHANGED
package/src/nova/Ai.js
CHANGED
|
@@ -33,7 +33,7 @@ export default class AI {
|
|
|
33
33
|
return uniqueDir;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
async extractOrStoreFile(
|
|
36
|
+
async extractOrStoreFile(fileData, outputDir, supportedFileTypes) {
|
|
37
37
|
// Ensure the output directory exists
|
|
38
38
|
if (!fs.existsSync(outputDir)) {
|
|
39
39
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
@@ -41,7 +41,15 @@ export default class AI {
|
|
|
41
41
|
|
|
42
42
|
// Create a temporary file to store the input
|
|
43
43
|
const tempFilePath = path.join(outputDir, "temp_input");
|
|
44
|
-
|
|
44
|
+
|
|
45
|
+
// Check if fileData is a buffer or a stream
|
|
46
|
+
if (Buffer.isBuffer(fileData)) {
|
|
47
|
+
// It's a buffer, write it directly to the temp file
|
|
48
|
+
await fs.promises.writeFile(tempFilePath, fileData);
|
|
49
|
+
} else {
|
|
50
|
+
// Assume it's a stream and pipe it to the temp file
|
|
51
|
+
await pipeline(fileData, fs.createWriteStream(tempFilePath));
|
|
52
|
+
}
|
|
45
53
|
|
|
46
54
|
try {
|
|
47
55
|
// Check file type
|
|
@@ -196,10 +204,9 @@ export default class AI {
|
|
|
196
204
|
|
|
197
205
|
return (
|
|
198
206
|
await Promise.all(
|
|
199
|
-
files.map(async (
|
|
200
|
-
//let type = await this.getFileType(fileStream);
|
|
207
|
+
files.map(async (fileData) => {
|
|
201
208
|
const imagePaths = await this.extractOrStoreFile(
|
|
202
|
-
|
|
209
|
+
fileData,
|
|
203
210
|
outputDir,
|
|
204
211
|
types
|
|
205
212
|
);
|
|
@@ -223,7 +230,6 @@ export default class AI {
|
|
|
223
230
|
if (options) {
|
|
224
231
|
this.options = _.merge(this.options, options);
|
|
225
232
|
}
|
|
226
|
-
//console.log(this.Provider);
|
|
227
233
|
if (this.Provider === undefined) {
|
|
228
234
|
throw new Error(`AI provider ${this.provider} is not defined`);
|
|
229
235
|
}
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.default = void 0;
|
|
5
|
-
|
|
6
|
-
var _lodash = _interopRequireDefault(require("lodash"));
|
|
7
|
-
|
|
8
|
-
var _fs = _interopRequireWildcard(require("fs"));
|
|
9
|
-
|
|
10
|
-
var _promises = require("stream/promises");
|
|
11
|
-
|
|
12
|
-
var _child_process = require("child_process");
|
|
13
|
-
|
|
14
|
-
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
|
15
|
-
|
|
16
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
17
|
-
|
|
18
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
21
|
-
|
|
22
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
23
|
-
|
|
24
|
-
var crypto = require("crypto");
|
|
25
|
-
|
|
26
|
-
var path = require("path");
|
|
27
|
-
|
|
28
|
-
class AI {
|
|
29
|
-
constructor(args) {
|
|
30
|
-
if (args === void 0) {
|
|
31
|
-
args = {};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.provider = args.provider || spice.config.ai.default_driver;
|
|
35
|
-
console.log(this.provider, args.provider, spice.config.ai.default_driver);
|
|
36
|
-
this.options = _lodash.default.merge(spice.config.ai.drivers[this.provider], args.options); //console.log(this.provider_name, spice.config.ai.providers)
|
|
37
|
-
|
|
38
|
-
this.Provider = spice.config.ai.providers[this.provider];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
createUniqueDirectory() {
|
|
42
|
-
var timestamp = new Date().toISOString().replace(/[:.-]/g, "");
|
|
43
|
-
var randomString = crypto.randomBytes(4).toString("hex");
|
|
44
|
-
var uniqueDirName = "job_" + timestamp + "_" + randomString;
|
|
45
|
-
var project_root = path.resolve(spice.root_path, "..");
|
|
46
|
-
var storageDir = path.join(project_root, "storage/jobs");
|
|
47
|
-
var uniqueDir = path.join(storageDir, uniqueDirName);
|
|
48
|
-
|
|
49
|
-
if (!_fs.default.existsSync(storageDir)) {
|
|
50
|
-
_fs.default.mkdirSync(storageDir, {
|
|
51
|
-
recursive: true
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
_fs.default.mkdirSync(uniqueDir);
|
|
56
|
-
|
|
57
|
-
return uniqueDir;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
extractOrStoreFile(fileStream, outputDir, supportedFileTypes) {
|
|
61
|
-
var _this = this;
|
|
62
|
-
|
|
63
|
-
return _asyncToGenerator(function* () {
|
|
64
|
-
// Ensure the output directory exists
|
|
65
|
-
if (!_fs.default.existsSync(outputDir)) {
|
|
66
|
-
_fs.default.mkdirSync(outputDir, {
|
|
67
|
-
recursive: true
|
|
68
|
-
});
|
|
69
|
-
} // Create a temporary file to store the input
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
var tempFilePath = path.join(outputDir, "temp_input");
|
|
73
|
-
yield (0, _promises.pipeline)(fileStream, _fs.default.createWriteStream(tempFilePath));
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
// Check file type
|
|
77
|
-
var fileType = yield _this.getFileType(tempFilePath);
|
|
78
|
-
|
|
79
|
-
if (_lodash.default.includes(supportedFileTypes, fileType)) {
|
|
80
|
-
// If it's a JPEG or PNG, just move it to the output directory
|
|
81
|
-
var timestamp = new Date().toISOString().replace(/[:.-]/g, "");
|
|
82
|
-
var extension = fileType === "jpeg" ? "jpg" : fileType;
|
|
83
|
-
var outputPath = path.join(outputDir, timestamp + "." + extension);
|
|
84
|
-
|
|
85
|
-
_fs.default.renameSync(tempFilePath, outputPath);
|
|
86
|
-
|
|
87
|
-
return [{
|
|
88
|
-
type: fileType,
|
|
89
|
-
media_type: _this.getMimeType(fileType),
|
|
90
|
-
path: outputPath
|
|
91
|
-
}];
|
|
92
|
-
} else {
|
|
93
|
-
// If it's a PDF, extract pages
|
|
94
|
-
if (fileType === "pdf") {
|
|
95
|
-
var outputPrefix = path.join(outputDir, "page"); // Use pdftoppm (part of poppler-utils) to convert PDF to images
|
|
96
|
-
|
|
97
|
-
yield new Promise((resolve, reject) => {
|
|
98
|
-
var process = (0, _child_process.spawn)("pdftoppm", ["-jpeg", tempFilePath, outputPrefix]);
|
|
99
|
-
process.on("close", code => {
|
|
100
|
-
if (code === 0) resolve();else reject(new Error("pdftoppm process exited with code " + code));
|
|
101
|
-
});
|
|
102
|
-
}); // Get a list of extracted images
|
|
103
|
-
|
|
104
|
-
var files = _fs.default.readdirSync(outputDir);
|
|
105
|
-
|
|
106
|
-
var images = files.filter(file => file.startsWith("page") && file.endsWith(".jpg"));
|
|
107
|
-
return images.map(image => {
|
|
108
|
-
return {
|
|
109
|
-
type: "jpeg",
|
|
110
|
-
media_type: _this.getMimeType("jpeg"),
|
|
111
|
-
path: path.join(outputDir, image)
|
|
112
|
-
};
|
|
113
|
-
});
|
|
114
|
-
} else {
|
|
115
|
-
throw new Error("Unsupported file type. Only PDF, JPEG, and PNG are supported.");
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
} catch (error) {
|
|
119
|
-
console.error("Error processing file:", error);
|
|
120
|
-
throw error;
|
|
121
|
-
} finally {
|
|
122
|
-
// Clean up the temporary input file
|
|
123
|
-
console.log("Cleaning up temporary file:", tempFilePath);
|
|
124
|
-
|
|
125
|
-
if (_fs.default.existsSync(tempFilePath)) {
|
|
126
|
-
_fs.default.unlinkSync(tempFilePath);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
})();
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
getMimeType(fileType) {
|
|
133
|
-
// Mapping of file extensions to MIME types
|
|
134
|
-
var mimeTypes = {
|
|
135
|
-
jpeg: "image/jpeg",
|
|
136
|
-
jpg: "image/jpeg",
|
|
137
|
-
// Adding 'jpg' as it's a common alias for 'jpeg'
|
|
138
|
-
png: "image/png",
|
|
139
|
-
gif: "image/gif",
|
|
140
|
-
bmp: "image/bmp",
|
|
141
|
-
webp: "image/webp",
|
|
142
|
-
svg: "image/svg+xml",
|
|
143
|
-
pdf: "application/pdf",
|
|
144
|
-
txt: "text/plain",
|
|
145
|
-
html: "text/html",
|
|
146
|
-
css: "text/css",
|
|
147
|
-
js: "application/javascript",
|
|
148
|
-
json: "application/json",
|
|
149
|
-
xml: "application/xml",
|
|
150
|
-
csv: "text/csv",
|
|
151
|
-
mp3: "audio/mpeg",
|
|
152
|
-
wav: "audio/wav",
|
|
153
|
-
mp4: "video/mp4",
|
|
154
|
-
avi: "video/x-msvideo",
|
|
155
|
-
mov: "video/quicktime",
|
|
156
|
-
zip: "application/zip",
|
|
157
|
-
rar: "application/vnd.rar",
|
|
158
|
-
"7z": "application/x-7z-compressed" // Add more MIME types as needed
|
|
159
|
-
|
|
160
|
-
}; // Ensure the fileType is a string and convert it to lowercase
|
|
161
|
-
|
|
162
|
-
if (typeof fileType !== "string") {
|
|
163
|
-
throw new TypeError("fileType must be a string");
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
var ext = fileType.toLowerCase(); // Return the MIME type if found, otherwise return the default
|
|
167
|
-
|
|
168
|
-
return mimeTypes[ext] || "application/octet-stream";
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
getFileType(filePath) {
|
|
172
|
-
return _asyncToGenerator(function* () {
|
|
173
|
-
return new Promise((resolve, reject) => {
|
|
174
|
-
var fileSignature = Buffer.alloc(8);
|
|
175
|
-
|
|
176
|
-
_fs.default.open(filePath, "r", (err, fd) => {
|
|
177
|
-
if (err) reject(err);
|
|
178
|
-
|
|
179
|
-
_fs.default.read(fd, fileSignature, 0, 8, 0, err => {
|
|
180
|
-
if (err) reject(err);
|
|
181
|
-
|
|
182
|
-
_fs.default.close(fd, err => {
|
|
183
|
-
if (err) console.error("Error closing file:", err);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
var hex = fileSignature.toString("hex");
|
|
187
|
-
|
|
188
|
-
if (hex.startsWith("ffd8")) {
|
|
189
|
-
resolve("jpeg");
|
|
190
|
-
} else if (hex.startsWith("89504e47")) {
|
|
191
|
-
resolve("png");
|
|
192
|
-
} else if (fileSignature.toString("ascii").startsWith("%PDF")) {
|
|
193
|
-
resolve("pdf");
|
|
194
|
-
} else {
|
|
195
|
-
resolve("unknown");
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
})();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
deleteFolderRecursive(dirPath) {
|
|
204
|
-
var _this2 = this;
|
|
205
|
-
|
|
206
|
-
return _asyncToGenerator(function* () {
|
|
207
|
-
if (_fs.default.existsSync(dirPath)) {
|
|
208
|
-
for (var entry of _fs.default.readdirSync(dirPath)) {
|
|
209
|
-
var entryPath = path.join(dirPath, entry);
|
|
210
|
-
|
|
211
|
-
if (_fs.default.lstatSync(entryPath).isDirectory()) {
|
|
212
|
-
yield _this2.deleteFolderRecursive(entryPath);
|
|
213
|
-
} else {
|
|
214
|
-
yield _fs.default.promises.unlink(entryPath);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
yield _fs.default.promises.rmdir(dirPath);
|
|
219
|
-
}
|
|
220
|
-
})();
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
processFiles(files) {
|
|
224
|
-
var _this3 = this;
|
|
225
|
-
|
|
226
|
-
return _asyncToGenerator(function* () {
|
|
227
|
-
var types = _this3.getSupportedFileTypes();
|
|
228
|
-
|
|
229
|
-
var outputDir = _this3.createUniqueDirectory();
|
|
230
|
-
|
|
231
|
-
return (yield Promise.all(files.map(
|
|
232
|
-
/*#__PURE__*/
|
|
233
|
-
function () {
|
|
234
|
-
var _ref = _asyncToGenerator(function* (fileStream) {
|
|
235
|
-
//let type = await this.getFileType(fileStream);
|
|
236
|
-
var imagePaths = yield _this3.extractOrStoreFile(fileStream, outputDir, types);
|
|
237
|
-
return imagePaths;
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
return function (_x) {
|
|
241
|
-
return _ref.apply(this, arguments);
|
|
242
|
-
};
|
|
243
|
-
}()))).flat();
|
|
244
|
-
})();
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
getSupportedFileTypes(options) {
|
|
248
|
-
if (this.Provider === undefined) {
|
|
249
|
-
throw new Error("AI provider " + this.provider + " is not defined");
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
var provider = new this.Provider(this.options);
|
|
253
|
-
return provider.getSupportedTypes();
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
prompt(prompt, _ref2, options) {
|
|
257
|
-
var _this4 = this;
|
|
258
|
-
|
|
259
|
-
return _asyncToGenerator(function* () {
|
|
260
|
-
var {
|
|
261
|
-
files,
|
|
262
|
-
system_message,
|
|
263
|
-
prompt_options
|
|
264
|
-
} = _ref2;
|
|
265
|
-
var context_files = yield _this4.processFiles(files);
|
|
266
|
-
|
|
267
|
-
try {
|
|
268
|
-
if (options) {
|
|
269
|
-
_this4.options = _lodash.default.merge(_this4.options, options);
|
|
270
|
-
} //console.log(this.Provider);
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
if (_this4.Provider === undefined) {
|
|
274
|
-
throw new Error("AI provider " + _this4.provider + " is not defined");
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
var provider = new _this4.Provider(_this4.options);
|
|
278
|
-
return yield provider.prompt(prompt, {
|
|
279
|
-
prompt_options,
|
|
280
|
-
system_message,
|
|
281
|
-
files: context_files
|
|
282
|
-
});
|
|
283
|
-
} catch (e) {
|
|
284
|
-
console.error(e);
|
|
285
|
-
return {
|
|
286
|
-
error: e.message
|
|
287
|
-
};
|
|
288
|
-
} finally {
|
|
289
|
-
for (var context_file of context_files) {
|
|
290
|
-
var directory = path.dirname(context_file.path);
|
|
291
|
-
yield _this4.deleteFolderRecursive(directory);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
})();
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
exports.default = AI;
|