fix(agent): stabilize large tool results
Generic Container CI/CD / test-build-publish (push) Successful in 1m53s
Agent CI/CD v2 / build-test-publish-and-deploy (push) Successful in 1m53s

This commit is contained in:
2026-08-25 12:02:48 +08:00
parent 774f39cbbe
commit 004c9bb72d
17 changed files with 721 additions and 62 deletions
+94
View File
@@ -76,6 +76,100 @@ export async function requestJson(ctx: RuntimeContext, request: RequestOptions):
return [payload, durationMs];
}
export async function requestAllPages(
ctx: RuntimeContext,
request: RequestOptions,
pageSize: number,
): Promise<[unknown[], number]> {
const items: unknown[] = [];
let durationMs = 0;
let offset = 0;
let expectedTotal: number | null = null;
while (expectedTotal === null || offset < expectedTotal) {
const [payload, pageDurationMs] = await requestJson(ctx, {
...request,
params: {
...request.params,
limit: pageSize,
offset,
},
});
durationMs += pageDurationMs;
const page = normalizePage(payload);
if (!page) {
throw new CliError(
"服务端错误",
"INVALID_PAGINATION_RESPONSE",
"backend collection response must contain items, total, limit, and offset",
7,
false,
payload,
);
}
if (expectedTotal === null) {
expectedTotal = page.total;
} else if (page.total !== expectedTotal) {
throw new CliError(
"服务端错误",
"PAGINATION_TOTAL_CHANGED",
`backend collection total changed from ${expectedTotal} to ${page.total}`,
7,
true,
);
}
if (page.offset !== offset) {
throw new CliError(
"服务端错误",
"PAGINATION_OFFSET_MISMATCH",
`backend collection returned offset ${page.offset}, expected ${offset}`,
7,
true,
);
}
if (page.items.length === 0 && offset < expectedTotal) {
throw new CliError(
"服务端错误",
"PAGINATION_STALLED",
`backend collection returned an empty page at offset ${offset} before total ${expectedTotal}`,
7,
true,
);
}
items.push(...page.items);
offset += page.items.length;
}
return [items.slice(0, expectedTotal ?? 0), durationMs];
}
function normalizePage(
payload: unknown,
): { items: unknown[]; limit: number; offset: number; total: number } | null {
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return null;
const page = payload as Record<string, unknown>;
if (
!Array.isArray(page.items) ||
typeof page.limit !== "number" ||
!Number.isInteger(page.limit) ||
typeof page.offset !== "number" ||
!Number.isInteger(page.offset) ||
typeof page.total !== "number" ||
!Number.isInteger(page.total) ||
page.limit <= 0 ||
page.offset < 0 ||
page.total < 0
) {
return null;
}
return {
items: page.items,
limit: page.limit,
offset: page.offset,
total: page.total,
};
}
function mapStatus(status: number): number {
if (status === 400 || status === 422) return 2;
if (status === 401) return 3;