import { InvalidArgumentError } from '../error/invalid-argument-error';
/**
* Validates the maximum number of tool calls that may execute concurrently.
*/
export function prepareToolCallConcurrency(
toolCallConcurrency: number | undefined,
): number | undefined {
if (toolCallConcurrency == null) {
return undefined;
}
if (!Number.isInteger(toolCallConcurrency)) {
throw new InvalidArgumentError({
parameter: 'toolCallConcurrency',
value: toolCallConcurrency,
message: 'toolCallConcurrency must be an integer',
});
}
if (toolCallConcurrency < 1) {
throw new InvalidArgumentError({
parameter: 'toolCallConcurrency',
value: toolCallConcurrency,
message: 'toolCallConcurrency must be >= 1',
});
}
return toolCallConcurrency;
}
/**
* Maps items with an optional concurrency limit while preserving result order.
*
* Workers claim items in input order. When the signal is aborted or an
* execution rejects, no additional queued items are started.
*/
export async function mapWithConcurrency({
items,
concurrency,
abortSignal,
execute,
}: {
items: Array;
concurrency: number | undefined;
abortSignal?: AbortSignal;
execute: (item: INPUT, index: number) => Promise