runChildren
SUPPORT · function · Runtime value
Run child tasks concurrently and return their values in declaration order.
The first failure aborts the remaining group, but an already in-flight commit is still awaited because cancellation does not mean the write was rolled back.
Import
import { runChildren } from '@jangts/agent-core/support';import { runChildren } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/model.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
Call
runChildren<Value>(parent: Pick<ModelRunner, "createChild" | "childLimits" | "signal">, tasks: readonly ModelChildTask<Value>[], options?: ModelChildrenOptions<Value>): Promise<readonly Value[]>Run child tasks concurrently and return their values in declaration order.
The first failure aborts the remaining group, but an already in-flight commit is still awaited because cancellation does not mean the write was rolled back.
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
parent | Pick<ModelRunner<string, readonly ModelTransportDefinition<unknown>[]>, "signal" | "childLimits" | "createChild"> | Required | — |
tasks | readonly ModelChildTask<Value>[] | Required | — |
options | ModelChildrenOptions<Value> | Optional | — |
Fields of parent
| Field | Type | Required | Description |
|---|---|---|---|
signal | AbortSignal | Required | Read-only. See the type definition and usage context. |
childLimits | Readonly<Required<ModelChildPolicy>> | Required | Read-only. Limits shared by every runner of one request; children only read them. |
createChild | (options: import("../structs/model/runtime.js").ModelChildOptions) => ModelRunner | Required | Create a fresh runner for a nested stage. The child never inherits conversation, journal or sinks: only the provider, fixed limits, the parent budget and the child's own lifetime. All ancestors keep their budgets, and the child is charged through them, so nested work cannot overspend. |
Fields of options
| Field | Type | Required | Description |
|---|---|---|---|
concurrency | number | undefined | Optional | Read-only. Parallel workers; defaults to, and may not exceed, the parent's concurrency limit. |
signal | AbortSignal | undefined | Optional | Read-only. Optional caller cancellation, combined with the parent signal and group failure. |
accept | ((value: Value, id: string) => MaybePromise<void>) | undefined | Optional | Called as each task finishes, before commit; must settle with void. |
commit | ((value: Value, id: string) => MaybePromise<void>) | undefined | Optional | Called in declaration order after every result is accepted; App owns atomicity/receipts. |
Returns: Promise<readonly Value[]>.
Awaited value: readonly Value[].
Examples
- Child task composition: sequential execution and controlled parallelism: Reuse createChild and the shared budget, check every result, and commit in declaration order.
Analyze two documents in sequence
Beginner · Node.js
import { ModelRunner, Support } from '@jangts/agent-core';
import type { ChatProvider } from '@jangts/agent-core';
export async function analyze(provider: ChatProvider, inputs: readonly [string, string], signal: AbortSignal) {
const model = new ModelRunner({ provider, maxCalls: 5, execution: { signal } });
return Support.runChildren(model, inputs.map((input, index) => ({
id: 'analysis-' + index, maxCalls: 2, reserveCalls: 1, timeoutMs: 30000, transports: [],
async run(child: ModelRunner) { return (await child.invoke(input)).value; },
})), {
accept(value) { if (!value.trim()) throw new Error('分析结果为空'); },
});
}import { ModelRunner, Support } from '@jangts/agent-core';
export async function analyze(provider, inputs, signal) {
const model = new ModelRunner({ provider, maxCalls: 5, execution: { signal } });
return Support.runChildren(model, inputs.map((input, index) => ({
id: 'analysis-' + index, maxCalls: 2, reserveCalls: 1, timeoutMs: 30000, transports: [],
async run(child) { return (await child.invoke(input)).value; },
})), {
accept(value) { if (!value.trim())
throw new Error('分析结果为空'); },
});
}Add concurrent capacity to the same set of analyses
Configuration · Node.js
The same function and task list, adding only the root Runner capacity setting. Local budgets and the parent reservation still apply.
import { ModelRunner, Support } from '@jangts/agent-core';
import type { ChatProvider } from '@jangts/agent-core';
export async function analyze(provider: ChatProvider, inputs: readonly [string, string], signal: AbortSignal) {
const model = new ModelRunner({ provider, maxCalls: 5, execution: { signal }, children: { maxConcurrentCalls: 2 } });
return Support.runChildren(model, inputs.map((input, index) => ({
id: 'analysis-' + index, maxCalls: 2, reserveCalls: 1, timeoutMs: 30000, transports: [],
async run(child: ModelRunner) { return (await child.invoke(input)).value; },
})), {
accept(value) { if (!value.trim()) throw new Error('分析结果为空'); },
});
}import { ModelRunner, Support } from '@jangts/agent-core';
export async function analyze(provider, inputs, signal) {
const model = new ModelRunner({ provider, maxCalls: 5, execution: { signal }, children: { maxConcurrentCalls: 2 } });
return Support.runChildren(model, inputs.map((input, index) => ({
id: 'analysis-' + index, maxCalls: 2, reserveCalls: 1, timeoutMs: 30000, transports: [],
async run(child) { return (await child.invoke(input)).value; },
})), {
accept(value) { if (!value.trim())
throw new Error('分析结果为空'); },
});
}