@autobe/agent
Version:
AI backend server code generator
63 lines • 3.03 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");
/**
* Executes task list with semaphore-controlled parallelization.
*
* All tasks are dispatched immediately into a worker pool, with concurrency
* limited by the semaphore to prevent overwhelming LLM APIs. Results are
* returned in original order.
*
* @param ctx Execution context providing vendor semaphore configuration
* @param taskList List of async tasks to execute, each receiving cache key
* @param promptCacheKey Optional cache key (generates UUID if not provided)
* @returns Array of task results in original order
*/
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 semaphore = typeof ctx === "number"
? ctx
: ctx.vendor.semaphore && ctx.vendor.semaphore instanceof tstl_1.Semaphore
? ctx.vendor.semaphore.max()
: ((_a = ctx.vendor.semaphore) !== null && _a !== void 0 ? _a : 8 /* AutoBeConfigConstant.SEMAPHORE */);
const queue = taskList.map((task, index) => new tstl_1.Pair(task, index));
const results = [];
let aborted = false;
let firstError = null;
yield Promise.allSettled(new Array(Math.min(semaphore, queue.length)).fill(0).map(() => __awaiter(void 0, void 0, void 0, function* () {
while (queue.length !== 0 && !aborted) {
const item = queue.splice(0, 1)[0];
try {
const result = yield item.first(promptCacheKey);
if (!aborted)
results.push(new tstl_1.Pair(result, item.second));
}
catch (error) {
if (!aborted) {
aborted = true;
queue.length = 0;
firstError = error;
}
}
}
})));
if (firstError !== null)
throw firstError;
return results.sort((x, y) => x.second - y.second).map((p) => p.first);
});
exports.executeCachedBatch = executeCachedBatch;
//# sourceMappingURL=executeCachedBatch.js.map