fix: 修正遍历列表负数结束索引

This commit is contained in:
yu 2026-10-28 13:08:53 +08:00
parent aaa0f2c3c7
commit 432e038e3c
2 changed files with 24 additions and 1 deletions

View File

@ -197,6 +197,28 @@ describe("runFlow integration", () => {
expect(events.filter((event) => event.type === "node.started" && event.nodeId === "accumulate")).toHaveLength(3);
});
it("runs an array loop through the last item when end_index is -1", async () => {
const loop = node("loop", "logic_control_array_for_v3", {
array_name: "items",
reverse: false,
start_index: 0,
end_index: -1
});
const definition = flow("array-loop-flow", [
loop,
node("collect", "Basic_DebugOutput", { msg: "${item}" }, {}, {
parentId: loop.id,
slot: "array_for_fn"
})
]);
const { result, events } = await execute(definition, { items: ["glm", "mini", "other", "kimi"] });
expect(result.status).toBe("succeeded");
expect(result.nodes.find((state) => state.nodeId === "loop")?.output).toBe(4);
expect(events.filter((event) => event.type === "node.started" && event.nodeId === "collect").map((event) => event.nodeId)).toHaveLength(4);
});
it("retries a transient failure and records the successful attempt", async () => {
const component = testComponent("Test.TransientFailure");
let calls = 0;

View File

@ -409,7 +409,8 @@ async function executeContainer(
const list = asArray(value);
const reverse = String(node.values?.reverse ?? "") === "true" || String(node.values?.reverse ?? "") === "1";
const start = Math.max(0, toNumber(node.values?.start_index, 0));
const end = node.values?.end_index === "" || node.values?.end_index == null ? list.length : toNumber(node.values?.end_index, list.length);
const requestedEnd = node.values?.end_index === "" || node.values?.end_index == null ? list.length : toNumber(node.values?.end_index, list.length);
const end = requestedEnd < 0 ? Math.max(start, list.length + requestedEnd + 1) : requestedEnd;
const slice = (reverse ? [...list].reverse() : list).slice(start, end);
let iterations = 0;
for (let index = 0; index < slice.length; index += 1) {