runChildren
SUPPORT · function · 运行时值
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.
导入
ts
import { runChildren } from '@jangts/agent-core/support';js
import { runChildren } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
调用
ts
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.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Value | — | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
parent | Pick<ModelRunner<string, readonly ModelTransportDefinition<unknown>[]>, "signal" | "childLimits" | "createChild"> | 必需 | — |
tasks | readonly ModelChildTask<Value>[] | 必需 | — |
options | ModelChildrenOptions<Value> | 可省略 | — |
parent 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
signal | AbortSignal | 必需 | 只读。见类型定义及调用场景。 |
childLimits | Readonly<Required<ModelChildPolicy>> | 必需 | 只读。Limits shared by every runner of one request; children only read them. |
createChild | (options: import("../structs/model/runtime.js").ModelChildOptions) => ModelRunner | 必需 | 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. |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
concurrency | number | undefined | 可省略 | 只读。Parallel workers; defaults to, and may not exceed, the parent's concurrency limit. |
signal | AbortSignal | undefined | 可省略 | 只读。Optional caller cancellation, combined with the parent signal and group failure. |
accept | ((value: Value, id: string) => MaybePromise<void>) | undefined | 可省略 | Called as each task finishes, before commit; must settle with void. |
commit | ((value: Value, id: string) => MaybePromise<void>) | undefined | 可省略 | Called in declaration order after every result is accepted; App owns atomicity/receipts. |
返回: Promise<readonly Value[]>。
等待后的值: readonly Value[]。
使用示例
- 子任务组合:顺序执行与受控并行:复用 createChild 和共享预算,检查全部结果后按声明顺序提交。
顺序分析两段资料
入门 · Node.js
ts
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('分析结果为空'); },
});
}js
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('分析结果为空'); },
});
}同一组分析增加并发容量
配置 · Node.js
同一个函数与任务清单,只增加根 Runner 的容量配置。局部预算和父级预留仍生效。
ts
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('分析结果为空'); },
});
}js
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('分析结果为空'); },
});
}