Comparing version
{ | ||
"name": "oneai", | ||
"main": "lib/src/index.js", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"description": "Make your app understand language. Summarize conversations, categorize articles, and more.", | ||
@@ -6,0 +6,0 @@ "repository": "github:oneai-nlp/oneai-node", |
@@ -15,2 +15,4 @@ /// <reference types="node" /> | ||
multilingual: boolean | MultilingualParams; | ||
sync: boolean; | ||
interval: number; | ||
} | ||
@@ -17,0 +19,0 @@ export type ApiReqParams = Partial<ApiClientParams>; |
@@ -41,7 +41,7 @@ "use strict"; | ||
? __assign(__assign({}, input), { text: input.text.buffer.toString(input.encoding) }) : input; | ||
return JSON.stringify(__assign(__assign(__assign({}, (includeText && { input: fixedInput.text, encoding: fixedInput.encoding })), ((0, pipeline_1.isCSV)(fixedInput) && { | ||
return JSON.stringify(__assign(__assign(__assign({}, (includeText && { input: fixedInput.text, encoding: fixedInput.encoding })), ((0, pipeline_1.isCSV)(fixedInput.text) && { | ||
csv_params: { | ||
columns: fixedInput.columns, | ||
skip_rows: fixedInput.skipRows, | ||
max_rows: fixedInput.maxRows, | ||
columns: fixedInput.text.columns, | ||
skip_rows: fixedInput.text.skipRows, | ||
max_rows: fixedInput.text.maxRows, | ||
}, | ||
@@ -111,2 +111,8 @@ })), { input_type: fixedInput.type, output_type: 'json', content_type: fixedInput.contentType, multilingual: multilingual, steps: skills.map(function (skill) { return ({ | ||
} | ||
// handle output lists | ||
if ('outputs' in output) { | ||
var result_1 = buildOutputBase([{ utterance: '' }], output.stats, headers); | ||
result_1.outputs = output.outputs.map(function (o) { return buildOutput(steps, o, {}); }); | ||
return result_1; | ||
} | ||
var generator = (output.output[0].text_generated_by_step_id || 0) - 1; | ||
@@ -133,2 +139,3 @@ if (generator < 0) | ||
var _a, _b, _c, _d; | ||
console.log(error.response.data); | ||
if (axios_1.default.isAxiosError(error) && error.response !== undefined) { | ||
@@ -135,0 +142,0 @@ return new errors_1.httpStatusErrorType[error.response.status.toString()](((_a = error.response.data) === null || _a === void 0 ? void 0 : _a.status_code) || error.response.status, ((_b = error.response.data) === null || _b === void 0 ? void 0 : _b.message) || error.message, (_c = error.response.data) === null || _c === void 0 ? void 0 : _c.details, (_d = error.response.data) === null || _d === void 0 ? void 0 : _d.request_id); |
@@ -9,5 +9,5 @@ import { Skill, Input, File, _Input, Output, AsyncApiTask, AsyncApiResponse } from '../model/pipeline'; | ||
constructor(client: HttpApiClient, logger?: Logger); | ||
postPipeline(input: Input, skills: Skill[], params?: ApiReqParams): Promise<Output>; | ||
postAsyncFile(input: _Input<File>, skills: Skill[], params?: ApiReqParams): Promise<AsyncApiTask>; | ||
postPipeline(input: Input, skills: Skill[], params?: ApiReqParams): Promise<Output | AsyncApiTask>; | ||
postFile(input: _Input<File>, skills: Skill[], params?: ApiReqParams): Promise<Output | AsyncApiTask>; | ||
getTaskStatus(task: AsyncApiTask, params?: ApiReqParams): Promise<AsyncApiResponse>; | ||
} |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var pipeline_1 = require("../model/pipeline"); | ||
var mapping_1 = require("./mapping"); | ||
@@ -49,9 +50,19 @@ var PipelineApiClient = /** @class */ (function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var response; | ||
var sync, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.client.post(this.rootPath, (0, mapping_1.buildRequest)(input, skills, true, (params === null || params === void 0 ? void 0 : params.multilingual) || this.client.params.multilingual), params)]; | ||
case 0: | ||
sync = (params === null || params === void 0 ? void 0 : params.sync) !== false; | ||
// handle async file upload | ||
if ((0, pipeline_1.isFile)(input.text) && (params === null || params === void 0 ? void 0 : params.sync) !== true) { | ||
return [2 /*return*/, this.postFile(input, skills, params)]; | ||
} | ||
return [4 /*yield*/, this.client.post(this.rootPath + (sync ? '' : '/async'), (0, mapping_1.buildRequest)(input, skills, true, (params === null || params === void 0 ? void 0 : params.multilingual) || this.client.params.multilingual), params)]; | ||
case 1: | ||
response = _a.sent(); | ||
return [2 /*return*/, (0, mapping_1.buildOutput)(skills, response.data, response.headers)]; | ||
return [2 /*return*/, sync ? (0, mapping_1.buildOutput)(skills, response.data, response.headers) : { | ||
requestId: response.data.task_id, | ||
name: '', | ||
skills: skills, | ||
}]; | ||
} | ||
@@ -61,14 +72,20 @@ }); | ||
}; | ||
PipelineApiClient.prototype.postAsyncFile = function (input, skills, params) { | ||
PipelineApiClient.prototype.postFile = function (input, skills, params) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var request, data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
var sync, request, response; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
sync = (params === null || params === void 0 ? void 0 : params.sync) === true; | ||
request = (0, mapping_1.buildRequest)(input, skills, false, (params === null || params === void 0 ? void 0 : params.multilingual) || this.client.params.multilingual); | ||
return [4 /*yield*/, this.client.post("".concat(this.rootPath, "/async/file?pipeline=").concat(encodeURIComponent(request)), input.text.buffer, params)]; | ||
if (!sync) | ||
(_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug("Uploading file ".concat(input.text.filePath)); | ||
return [4 /*yield*/, this.client.post("".concat(this.rootPath).concat(sync ? '' : '/async', "/file?pipeline=").concat(encodeURIComponent(request)), input.text.buffer, params)]; | ||
case 1: | ||
data = (_a.sent()).data; | ||
return [2 /*return*/, { | ||
id: data.task_id, | ||
response = _c.sent(); | ||
if (!sync) | ||
(_b = this.logger) === null || _b === void 0 ? void 0 : _b.debugNoNewline("Upload of file ".concat(input.text.filePath, " complete\n")); | ||
return [2 /*return*/, sync ? (0, mapping_1.buildOutput)(skills, response.data, response.headers) : { | ||
requestId: response.data.task_id, | ||
name: input.text.filePath, | ||
@@ -86,3 +103,3 @@ skills: skills, | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.client.get("".concat(this.rootPath, "/async/tasks/").concat(task.id), params)]; | ||
case 0: return [4 /*yield*/, this.client.get("".concat(this.rootPath, "/async/tasks/").concat(task.requestId), params)]; | ||
case 1: | ||
@@ -89,0 +106,0 @@ response = _a.sent(); |
@@ -51,2 +51,3 @@ import { Conversation, Output } from './model/pipeline'; | ||
} | undefined) => import("./model/pipeline").Skill; | ||
pdfExtractText: (params?: {} | undefined) => import("./model/pipeline").Skill; | ||
}; | ||
@@ -89,2 +90,3 @@ static skills: { | ||
} | undefined) => import("./model/pipeline").Skill; | ||
pdfExtractText: (params?: {} | undefined) => import("./model/pipeline").Skill; | ||
}; | ||
@@ -105,6 +107,9 @@ private apiClientBase; | ||
run(text: import("./model/pipeline").TextContent | import("./model/pipeline").Input, params?: Partial<ApiClientParams> | undefined): Promise<Output>; | ||
runFile(filePath: string, params?: (Partial<ApiClientParams> & { | ||
sync?: boolean | undefined; | ||
interval?: number | undefined; | ||
}) | undefined): Promise<Output>; | ||
run(text: import("./model/pipeline").TextContent | import("./model/pipeline").Input, params: Partial<ApiClientParams> & { | ||
polling: false; | ||
}): Promise<import("./model/pipeline").AsyncApiTask>; | ||
runFile(filePath: string, params: Partial<ApiClientParams> & { | ||
polling: false; | ||
}): Promise<import("./model/pipeline").AsyncApiTask>; | ||
runFile(filePath: string, params?: Partial<ApiClientParams> | undefined): Promise<Output>; | ||
runBatch<T extends import("./model/pipeline").TextContent | import("./model/pipeline").Input>(inputs: Iterable<T>, params?: (Partial<ApiClientParams> & { | ||
@@ -115,2 +120,4 @@ maxConcurrentRequests?: number | undefined; | ||
}) | undefined): Promise<import("./schedule").BatchResponse<T, Output>>; | ||
taskStatus(task: string | import("./model/pipeline").AsyncApiTask): Promise<import("./model/pipeline").AsyncApiResponse>; | ||
awaitTask(task: string | import("./model/pipeline").AsyncApiTask, params?: Partial<ApiClientParams> | undefined): Promise<Output>; | ||
}; | ||
@@ -117,0 +124,0 @@ }; |
@@ -70,2 +70,4 @@ "use strict"; | ||
multilingual: false, | ||
sync: true, | ||
interval: 1, | ||
}; | ||
@@ -72,0 +74,0 @@ OneAI.OneAI = OneAI; |
@@ -12,3 +12,3 @@ /// <reference types="node" /> | ||
filePath: string; | ||
buffer: Buffer; | ||
buffer?: Buffer; | ||
}; | ||
@@ -85,3 +85,3 @@ export type Conversation = { | ||
export interface AsyncApiTask { | ||
id: string; | ||
requestId: string; | ||
name: string; | ||
@@ -88,0 +88,0 @@ skills: Skill[]; |
@@ -48,3 +48,3 @@ "use strict"; | ||
function isFile(object) { | ||
return typeof object === 'object' && 'filePath' in object && 'buffer' in object; | ||
return typeof object === 'object' && 'filePath' in object; | ||
} | ||
@@ -92,6 +92,21 @@ exports.isFile = isFile; | ||
}, | ||
'.csv': { | ||
contentType: 'text/csv', | ||
type: 'article', | ||
isBinary: false, | ||
}, | ||
'.pdf': { | ||
contentType: 'text/pdf', | ||
type: 'article', | ||
isBinary: true, | ||
}, | ||
}; | ||
function prepFileInput(input) { | ||
var ext = path.extname(input.text.filePath); | ||
if (!(ext in extensions)) | ||
throw new Error("Unsupported file type: ".concat(ext)); | ||
var _a = extensions[ext], contentType = _a.contentType, type = _a.type, isBinary = _a.isBinary; | ||
return __assign(__assign({ encoding: (isBinary) ? 'base64' : 'utf8', contentType: contentType, type: type }, input), { text: (input.text.buffer !== undefined) ? input.text : __assign(__assign({}, input.text), { buffer: fs_1.default.readFileSync(input.text.filePath) }) }); | ||
} | ||
function wrapContent(content) { | ||
if (isInput(content)) | ||
return content; | ||
if (typeof content === 'string') { | ||
@@ -103,14 +118,9 @@ return { | ||
} | ||
if (isFile(content)) { | ||
var ext = path.extname(content.filePath); | ||
if (!(ext in extensions)) | ||
throw new Error("Unsupported file type: ".concat(ext)); | ||
var _a = extensions[ext], contentType = _a.contentType, type = _a.type, isBinary = _a.isBinary; | ||
return { | ||
text: (content.buffer !== undefined) ? content : __assign(__assign({}, content), { buffer: fs_1.default.readFileSync(content.filePath) }), | ||
encoding: (isBinary) ? 'base64' : 'utf8', | ||
contentType: contentType, | ||
type: type, | ||
}; | ||
if (isInput(content)) { | ||
if (isFile(content.text)) | ||
return prepFileInput(content); | ||
return content; | ||
} | ||
if (isFile(content)) | ||
return prepFileInput({ text: content }); | ||
return { | ||
@@ -117,0 +127,0 @@ text: content, |
import { ApiReqParams } from './api/client'; | ||
import PipelineApiClient from './api/pipeline'; | ||
import { Input, Output, Skill, TextContent } from './model/pipeline'; | ||
import { BatchResponse } from './schedule'; | ||
import { AsyncApiResponse, AsyncApiTask, Input, Output, Skill, TextContent } from './model/pipeline'; | ||
import { BatchResponse, polling } from './schedule'; | ||
declare const createPipelineClass: (client: PipelineApiClient) => { | ||
@@ -10,6 +10,9 @@ new (...steps: Skill[]): { | ||
run(text: TextContent | Input, params?: ApiReqParams): Promise<Output>; | ||
runFile(filePath: string, params?: ApiReqParams & { | ||
sync?: boolean; | ||
interval?: number; | ||
}): Promise<Output>; | ||
run(text: TextContent | Input, params: ApiReqParams & { | ||
polling: false; | ||
}): Promise<AsyncApiTask>; | ||
runFile(filePath: string, params: ApiReqParams & { | ||
polling: false; | ||
}): Promise<AsyncApiTask>; | ||
runFile(filePath: string, params?: ApiReqParams): Promise<Output>; | ||
runBatch<T extends TextContent | Input>(inputs: Iterable<T>, params?: (Partial<import("./api/client").ApiClientParams> & { | ||
@@ -20,4 +23,6 @@ maxConcurrentRequests?: number | undefined; | ||
}) | undefined): Promise<BatchResponse<T, Output>>; | ||
taskStatus(task: AsyncApiTask | string): Promise<AsyncApiResponse>; | ||
awaitTask(task: AsyncApiTask | string, params?: ApiReqParams): Promise<Output>; | ||
}; | ||
}; | ||
export default createPipelineClass; |
@@ -53,7 +53,3 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fs_1 = __importDefault(require("fs")); | ||
var pipeline_1 = require("./model/pipeline"); | ||
@@ -71,4 +67,14 @@ var schedule_1 = require("./schedule"); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var input, response; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.client.postPipeline((0, pipeline_1.wrapContent)(text), this.steps, params)]; | ||
switch (_a.label) { | ||
case 0: | ||
input = (0, pipeline_1.wrapContent)(text); | ||
return [4 /*yield*/, this.client.postPipeline(input, this.steps, params)]; | ||
case 1: | ||
response = _a.sent(); | ||
return [2 /*return*/, (0, pipeline_1.isInput)(response) || (params === null || params === void 0 ? void 0 : params.polling) === false ? response | ||
: (0, schedule_1.polling)(response, function (t) { return _this.client.getTaskStatus.call(_this.client, t); }, (params === null || params === void 0 ? void 0 : params.interval) || 1, this.client.logger)]; | ||
} | ||
}); | ||
@@ -78,20 +84,5 @@ }); | ||
_Pipeline.prototype.runFile = function (filePath, params) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var input, task; | ||
var _this = this; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
input = (0, pipeline_1.wrapContent)({ filePath: filePath, buffer: fs_1.default.readFileSync(filePath) }); | ||
if (params === null || params === void 0 ? void 0 : params.sync) | ||
return [2 /*return*/, this.run(input, params)]; | ||
// todo: extend to non-file inputs | ||
(_a = this.client.logger) === null || _a === void 0 ? void 0 : _a.debug("Uploading file ".concat(input.text.filePath)); | ||
return [4 /*yield*/, this.client.postAsyncFile(input, this.steps, params)]; | ||
case 1: | ||
task = _c.sent(); | ||
(_b = this.client.logger) === null || _b === void 0 ? void 0 : _b.debugNoNewline("Upload of file ".concat(input.text.filePath, " complete\n")); | ||
return [2 /*return*/, (0, schedule_1.polling)(task, function (t) { return _this.client.getTaskStatus.call(_this.client, t); }, (params === null || params === void 0 ? void 0 : params.interval) || 1, this.client.logger)]; | ||
} | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, this.run({ filePath: filePath }, params)]; | ||
}); | ||
@@ -108,2 +99,21 @@ }); | ||
}; | ||
_Pipeline.prototype.taskStatus = function (task) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var taskObj; | ||
return __generator(this, function (_a) { | ||
taskObj = (typeof task === 'string') ? { requestId: task, name: '', skills: this.steps } : task; | ||
return [2 /*return*/, this.client.getTaskStatus(taskObj)]; | ||
}); | ||
}); | ||
}; | ||
_Pipeline.prototype.awaitTask = function (task, params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var taskObj; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
taskObj = (typeof task === 'string') ? { requestId: task, name: '', skills: this.steps } : task; | ||
return [2 /*return*/, (0, schedule_1.polling)(taskObj, function (t) { return _this.client.getTaskStatus.call(_this.client, t); }, (params === null || params === void 0 ? void 0 : params.interval) || 1, this.client.logger)]; | ||
}); | ||
}); | ||
}; | ||
return _Pipeline; | ||
@@ -110,0 +120,0 @@ }()); |
@@ -39,2 +39,3 @@ import type { Skill, Output, Label } from './model/pipeline'; | ||
}) => Skill; | ||
pdfExtractText: (params?: {}) => Skill; | ||
}; | ||
@@ -68,2 +69,4 @@ export interface OutputFields { | ||
emailInsights?: Label[]; | ||
outputs?: Output[]; | ||
pdfText?: Output; | ||
} |
@@ -106,2 +106,6 @@ "use strict"; | ||
}); }, | ||
pdfExtractText: function (params) { return ({ | ||
apiName: 'pdf-extract-text', textField: 'pdfText', | ||
params: params, | ||
}); }, | ||
}; |
{ | ||
"name": "oneai", | ||
"main": "lib/src/index.js", | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"description": "Make your app understand language. Summarize conversations, categorize articles, and more.", | ||
@@ -6,0 +6,0 @@ "repository": "github:oneai-nlp/oneai-node", |
168798
2.31%3304
2.07%4
-20%