diff --git a/module/genericsUtils.ts b/module/genericsUtils.ts index 15d3aa1..bb384fe 100644 --- a/module/genericsUtils.ts +++ b/module/genericsUtils.ts @@ -12,13 +12,24 @@ if ("BANANA_URL" in process.env){ } -export async function runMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ - const callID = await startAPI(apiKey, modelKey, modelInputs) +export async function runMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ + const startOut = await startAPI(apiKey, modelKey, modelInputs) + if (startOut["finished"] == true){ + const res = { + "id": startOut["id"], + "message": startOut["message"], + "created": startOut["created"], + "apiVersion": startOut["apiVersion"], + "modelOutputs": startOut["modelOutputs"] + } + return res + } + + // else it's long running, so poll for result while (true) { - const jsonOut = await checkAPI(apiKey, callID) + const jsonOut = await checkAPI(apiKey, startOut["callID"]) if (jsonOut !== undefined){ if (jsonOut.message.toLowerCase() === "success"){ - jsonOut['callID'] = callID return jsonOut } } @@ -27,28 +38,25 @@ export async function runMain(apiKey: string, modelKey: string, modelInputs: obj } export async function startMain(apiKey: string, modelKey: string, modelInputs: object = {}): Promise{ - const callID = await startAPI(apiKey, modelKey, modelInputs) - return callID + const jsonOut = await startAPI(apiKey, modelKey, modelInputs, true) + return jsonOut["callID"] } -export async function feedback(apiKey: string, callID: string, feedback: object = {}): Promise{ - const jsonOut = await feedbackAPI(apiKey, callID, feedback) - return jsonOut -} export async function checkMain(apiKey: string, callID: string): Promise{ const jsonOut = await checkAPI(apiKey, callID) return jsonOut } -const startAPI = async (apiKey: string, modelKey: string, modelInputs: object): Promise => { - const urlStart = endpoint.concat("start/v2/") +const startAPI = async (apiKey: string, modelKey: string, modelInputs: object, startOnly: boolean = false): Promise => { + const urlStart = endpoint.concat("start/v4/") const payload = { "id": uuidv4(), "created": Math.floor(new Date().getTime() / 1000), "apiKey" : apiKey, "modelKey" : modelKey, "modelInputs" : modelInputs, + "startOnly": startOnly, } const response = await axios.post(urlStart, payload).catch(err => { @@ -67,44 +75,11 @@ const startAPI = async (apiKey: string, modelKey: string, modelInputs: object): throw jsonOut.message } - const callID = jsonOut.callID - if (callID === undefined){ - throw `server error: start call failed without a message or callID` - } - - return callID -} - -const feedbackAPI = async (apiKey: string, callID: string, feedback: object): Promise => { - const url = endpoint.concat("feedback/v2/") - - const payload = { - "id": uuidv4(), - "created": Math.floor(new Date().getTime() / 1000), - "apiKey" : apiKey, - "callID" : callID, - "feedback": feedback - } - - const response = await axios.post(url, payload).catch(err => { - if (err.response) { - throw `server error: status code ${err.response.status}` - } else if (err.request) { - throw 'server error: endpoint busy or not available.' - } else { - console.log(err) - throw "Misc axios error. Please email erik@banana.dev with above error" - } - }) - const jsonOut = response.data - - if (jsonOut.message.toLowerCase().includes("error")){ - throw jsonOut.message - } return jsonOut } + const checkAPI = async (apiKey: string, callID: string): Promise => { - const urlCheck = endpoint.concat("check/v2/") + const urlCheck = endpoint.concat("check/v4/") const payload = { "id": uuidv4(), diff --git a/module/index.ts b/module/index.ts index e5c4781..259cf19 100644 --- a/module/index.ts +++ b/module/index.ts @@ -16,11 +16,6 @@ export async function start(apiKey: string, modelKey: string, modelInputs: objec return callID } -export async function feedback(apiKey: string, callID: string, feedback: object = {}): Promise{ - const jsonOut = await genericsUtils.feedback(apiKey, callID, feedback) - return jsonOut -} - export async function check(apiKey: string, callID: string): Promise{ const jsonOut = await genericsUtils.checkMain(apiKey, callID) return jsonOut diff --git a/module/package.json b/module/package.json index 56c586e..d694be4 100644 --- a/module/package.json +++ b/module/package.json @@ -1,6 +1,6 @@ { "name": "@banana-dev/banana-dev", - "version": "3.0.0", + "version": "4.0.0", "description": "A typescript-friendly node client to interact with Banana's machine learning inference APIs", "main": "dist/index.js", "types": "dist/index.d.ts",