@autobe/agent
Version:
AI backend server code generator
41 lines • 2.13 kB
JavaScript
;
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 });
exports.executeCachedBatch = void 0;
const tstl_1 = require("tstl");
const uuid_1 = require("uuid");
const executeCachedBatch = (ctx, taskList, promptCacheKey) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
if (taskList.length === 0)
return [];
promptCacheKey !== null && promptCacheKey !== void 0 ? promptCacheKey : (promptCacheKey = (0, uuid_1.v7)());
const first = yield taskList[0](promptCacheKey);
const semaphore = ctx.vendor.semaphore && ctx.vendor.semaphore instanceof tstl_1.Semaphore
? ctx.vendor.semaphore.max()
: ((_a = ctx.vendor.semaphore) !== null && _a !== void 0 ? _a : 16 /* AutoBeConfigConstant.SEMAPHORE */);
const remained = taskList
.slice(1)
.map((task, index) => new tstl_1.Pair(task, index));
const tail = [];
yield Promise.all(new Array(semaphore).fill(0).map(() => __awaiter(void 0, void 0, void 0, function* () {
while (remained.length !== 0) {
const batch = remained.splice(0, 1)[0];
const result = yield batch.first(promptCacheKey);
tail.push(new tstl_1.Pair(result, batch.second));
}
})));
return [
first,
...tail.sort((x, y) => x.second - y.second).map((p) => p.first),
];
});
exports.executeCachedBatch = executeCachedBatch;
//# sourceMappingURL=executeCachedBatch.js.map