fix(agent): stabilize large tool results
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user