-
Notifications
You must be signed in to change notification settings - Fork 86
chore: left-shift insight filtering and pagination into EvalClient #2092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nborges-aws
wants to merge
1
commit into
refactor
Choose a base branch
from
batch-insights-polish
base: refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import type { BatchEvaluationSummary } from "@aws-sdk/client-bedrock-agentcore"; | ||
| import { useNavigate } from "react-router"; | ||
| import type { ScreenProps } from "../handlers/types"; | ||
| import { coreOptsFromCtx } from "../handlers/utils"; | ||
| import { formatTimestamp } from "./formatTimestamp"; | ||
| import { PaginatedTablePicker } from "./PaginatedTablePicker"; | ||
| import type { DataTableColumn } from "./ui/data-table"; | ||
|
|
||
| interface BatchInsightsRow extends Record<string, unknown> { | ||
| batchEvaluationId: string; | ||
| name: string; | ||
| status: string; | ||
| updatedAt: string; | ||
| } | ||
|
|
||
| const batchInsightsColumns = [ | ||
| { key: "name", header: "name", flex: true }, | ||
| { key: "status", header: "status", width: 22 }, | ||
| { | ||
| key: "updatedAt", | ||
| header: "updated UTC", | ||
| width: 16, | ||
| render: formatTimestamp, | ||
| }, | ||
| ] satisfies DataTableColumn<BatchInsightsRow>[]; | ||
|
|
||
| function toRow(summary: BatchEvaluationSummary): BatchInsightsRow { | ||
| const id = summary.batchEvaluationId ?? ""; | ||
| return { | ||
| batchEvaluationId: id, | ||
| name: summary.batchEvaluationName ?? id, | ||
| status: summary.status ?? "-", | ||
| updatedAt: summary.updatedAt?.toISOString() ?? "-", | ||
| }; | ||
| } | ||
|
|
||
| export interface BatchInsightsPickerProps extends ScreenProps { | ||
| breadcrumb: string[]; | ||
| description?: string; | ||
| onSelect: (batchEvaluationId: string) => void; | ||
| onEscape?: () => void; | ||
| } | ||
|
|
||
| export function BatchInsightsPicker({ | ||
| ctx, | ||
| core, | ||
| breadcrumb, | ||
| description, | ||
| onSelect, | ||
| onEscape, | ||
| }: BatchInsightsPickerProps) { | ||
| const opts = coreOptsFromCtx(ctx); | ||
| const navigate = useNavigate(); | ||
| const goBack = onEscape ?? (() => navigate("/" + breadcrumb.slice(0, -1).join("/"))); | ||
|
|
||
| return ( | ||
| <PaginatedTablePicker | ||
| breadcrumb={breadcrumb} | ||
| description={description} | ||
| queryKey={["batch-insights", opts.region]} | ||
| loadPage={async (token, pageSize) => { | ||
| const response = await core.eval.listBatchInsights(token, pageSize, opts); | ||
| return { | ||
| items: response.batchEvaluations ?? [], | ||
| nextToken: response.nextToken, | ||
| }; | ||
| }} | ||
| toRow={toRow} | ||
| columns={batchInsightsColumns} | ||
| getValue={(row) => row.batchEvaluationId} | ||
| onSelect={onSelect} | ||
| onBack={goBack} | ||
| loadingMessage="Loading batch insights…" | ||
| errorMessage={(error) => `Error: ${error.message}`} | ||
| emptyMessage="No batch insights found in this Region." | ||
| emptyPageMessage="No batch insights on this page." | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { describe, expect, mock, test } from "bun:test"; | ||
| import { | ||
| ListBatchEvaluationsCommand, | ||
| type BatchEvaluationSummary, | ||
| } from "@aws-sdk/client-bedrock-agentcore"; | ||
| import { ResultTruncationError } from "../errors"; | ||
| import type { AwsClients } from "./types"; | ||
| import { EvalClient } from "./eval"; | ||
|
|
||
| const options = { region: "us-west-2", endpointUrl: "https://agentcore.example.test" }; | ||
|
|
||
| function insight(id: string): BatchEvaluationSummary { | ||
| return { | ||
| batchEvaluationId: id, | ||
| insights: [{ insightId: "Builtin.Insight.FailureAnalysis" }], | ||
| } as BatchEvaluationSummary; | ||
| } | ||
|
|
||
| function evaluation(id: string): BatchEvaluationSummary { | ||
| return { | ||
| batchEvaluationId: id, | ||
| evaluators: [{ evaluatorId: "Builtin.Correctness" }], | ||
| } as BatchEvaluationSummary; | ||
| } | ||
|
|
||
| function evalClient(send: (command: ListBatchEvaluationsCommand) => Promise<unknown>): EvalClient { | ||
| return new EvalClient({ | ||
| data: () => ({ send: mock(send) }) as never, | ||
| } as unknown as AwsClients); | ||
| } | ||
|
|
||
| describe("EvalClient.listBatchInsights", () => { | ||
| test("rejects an invalid logical page size before calling the service", async () => { | ||
| const send = mock(async () => ({ batchEvaluations: [] })); | ||
| const client = evalClient(send); | ||
|
|
||
| await expect(client.listBatchInsights(undefined, 0, options)).rejects.toThrow( | ||
| "maxResults must be a positive integer", | ||
| ); | ||
| expect(send).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("filters the final Batch Evaluation page", async () => { | ||
| const insightsJob = insight("insights-1"); | ||
| const client = evalClient(async (command) => { | ||
| expect(command).toBeInstanceOf(ListBatchEvaluationsCommand); | ||
| expect(command.input).toEqual({ nextToken: "page-2", maxResults: undefined }); | ||
| return { batchEvaluations: [evaluation("evaluation-1"), insightsJob] }; | ||
| }); | ||
|
|
||
| await expect(client.listBatchInsights("page-2", 10, options)).resolves.toEqual({ | ||
| batchEvaluations: [insightsJob], | ||
| nextToken: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| test("scans sparse service pages to fill one logical Insights page", async () => { | ||
| const insights = [insight("insights-1"), insight("insights-2")]; | ||
| const requests: unknown[] = []; | ||
| const client = evalClient(async (command) => { | ||
| requests.push(command.input); | ||
| switch (command.input.nextToken) { | ||
| case undefined: | ||
| return { | ||
| batchEvaluations: [evaluation("evaluation-1")], | ||
| nextToken: "page-2", | ||
| }; | ||
| case "page-2": | ||
| return { | ||
| batchEvaluations: [insights[0], evaluation("evaluation-2")], | ||
| nextToken: "page-3", | ||
| }; | ||
| default: | ||
| return { | ||
| batchEvaluations: [evaluation("evaluation-3"), insights[1]], | ||
| }; | ||
| } | ||
| }); | ||
|
|
||
| await expect(client.listBatchInsights(undefined, 2, options)).resolves.toEqual({ | ||
| batchEvaluations: insights, | ||
| nextToken: undefined, | ||
| }); | ||
| expect(requests).toEqual([ | ||
| { nextToken: undefined, maxResults: undefined }, | ||
| { nextToken: "page-2", maxResults: undefined }, | ||
| { nextToken: "page-3", maxResults: undefined }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("replays the exact Batch Evaluation prefix without skipping the next Insight", async () => { | ||
| const firstInsight = insight("insights-1"); | ||
| const secondInsight = insight("insights-2"); | ||
| const requests: unknown[] = []; | ||
| const client = evalClient(async (command) => { | ||
| requests.push(command.input); | ||
|
|
||
| if (command.input.nextToken === "after-insights-1") { | ||
| return { | ||
| batchEvaluations: [evaluation("evaluation-2"), secondInsight], | ||
| }; | ||
| } | ||
| if (command.input.maxResults === 2) { | ||
| return { | ||
| batchEvaluations: [evaluation("evaluation-1"), firstInsight], | ||
| nextToken: "after-insights-1", | ||
| }; | ||
| } | ||
| return { | ||
| batchEvaluations: [ | ||
| evaluation("evaluation-1"), | ||
| firstInsight, | ||
| evaluation("evaluation-2"), | ||
| secondInsight, | ||
| ], | ||
| }; | ||
| }); | ||
|
|
||
| const first = await client.listBatchInsights(undefined, 1, options); | ||
| const second = await client.listBatchInsights(first.nextToken, 1, options); | ||
|
|
||
| expect(first).toEqual({ | ||
| batchEvaluations: [firstInsight], | ||
| nextToken: "after-insights-1", | ||
| }); | ||
| expect(second).toEqual({ | ||
| batchEvaluations: [secondInsight], | ||
| nextToken: undefined, | ||
| }); | ||
| expect(requests).toEqual([ | ||
| { nextToken: undefined, maxResults: undefined }, | ||
| { nextToken: undefined, maxResults: 2 }, | ||
| { nextToken: "after-insights-1", maxResults: undefined }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("returns a token only when it leads to another Insights job", async () => { | ||
| const firstInsight = insight("insights-1"); | ||
| const secondInsight = insight("insights-2"); | ||
| const client = evalClient(async (command) => { | ||
| if (command.input.nextToken === "page-2") { | ||
| return { batchEvaluations: [evaluation("evaluation-2"), secondInsight] }; | ||
| } | ||
| return { | ||
| batchEvaluations: [firstInsight, evaluation("evaluation-1")], | ||
| nextToken: "page-2", | ||
| }; | ||
| }); | ||
|
|
||
| await expect(client.listBatchInsights(undefined, 1, options)).resolves.toEqual({ | ||
| batchEvaluations: [firstInsight], | ||
| nextToken: "page-2", | ||
| }); | ||
| }); | ||
|
|
||
| test("throws when Insights discovery exceeds the Batch Evaluation scan cap", async () => { | ||
| let calls = 0; | ||
| const client = evalClient(async () => { | ||
| calls += 1; | ||
| return { batchEvaluations: [], nextToken: `page-${calls}` }; | ||
| }); | ||
|
|
||
| await expect(client.listBatchInsights(undefined, 1, options)).rejects.toThrow( | ||
| ResultTruncationError, | ||
| ); | ||
| expect(calls).toBe(101); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does gateway have a seperate unit test for this? Can this be tested within the handler tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gateway tests this via unit tests also: src/core/gateway.test.ts:65