assistan-ts
Version:
A typesafe and code-first library to define and run OpenAI assistants
106 lines • 4.5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const buffer_1 = require("buffer");
const fs_1 = require("fs");
const path = __importStar(require("path"));
/**
* Mimics the File interface in a Node.js environment.
*/
class FileLike extends buffer_1.Blob {
constructor(fileContent, fileName, mime) {
super([fileContent], { type: mime });
this.name = fileName;
this.lastModified = Date.now(); // This will be updated upon reading the actual file modification date
this.webkitRelativePath = ""; // Not applicable in Node.js context
}
}
/**
* Reads a file from the filesystem and returns an object mimicking the File interface.
*
* @param filePath - The path to the file to read.
* @returns A promise that resolves with the NodeFile object.
*/
function getFile(filePath) {
return __awaiter(this, void 0, void 0, function* () {
try {
const data = yield fs_1.promises.readFile(filePath);
const stats = yield fs_1.promises.stat(filePath);
const fileName = path.basename(filePath);
const mimeType = "application/octet-stream"; // Default MIME type, consider using a library like `mime-types` for accurate types
const file = new FileLike(data, fileName, mimeType);
file.lastModified = stats.mtimeMs;
return file;
}
catch (err) {
throw err;
}
});
}
// Usage example:
/*
(async () => {
try {
const file = await getFile('/path/to/your/file.txt');
console.log(await file.text());
} catch (error) {
console.error(error);
}
})();
*/
// import * as fs from 'fs/promises';
// import * as path from 'path';
// import { NodeFile } from './NodeFile'; // Assuming NodeFile is exported from another file
// import * as glob from 'glob';
// import { promisify } from 'util';
// const globAsync = promisify(glob);
// /**
// * Reads files matching a glob pattern from the filesystem and returns an array of objects mimicking the File interface.
// *
// * @param {string} pattern - The glob pattern to match files.
// * @returns {Promise<NodeFile[]>} A promise that resolves with an array of NodeFile objects.
// */
// async function getFiles(pattern: string): Promise<NodeFile[]> {
// try {
// const filePaths = await globAsync(pattern);
// const filesPromises = filePaths.map(async (filePath) => {
// return getFile(filePath); // reusing the previously defined getFile function
// });
// return await Promise.all(filesPromises);
// } catch (err) {
// throw new Error(`Error reading files: ${(err as Error).message}`);
// }
// }
// export { getFile, NodeFile };
//# sourceMappingURL=files.js.map