Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions hub/client/application/hub/learnings-contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { recordIdentifierSchema, repositorySchema } from "@application/hub/contr

import type {
AcceptedLearningResponse,
EvaluationSettingsResponse,
LearningActivityDayResponse,
LearningDecisionInput,
LearningEvidenceResponse,
Expand Down Expand Up @@ -274,3 +275,26 @@ export const learningShareInputSchema = z
export const learningMutationResponseSchema = z
.object({ notice: z.string().min(1) })
.strict() satisfies z.ZodType<LearningMutationResponse>

export const evaluationSettingsResponseSchema = z
.object({
repository: z
.object({
id: recordIdentifierSchema,
nameWithOwner: z.string().min(1),
defaultBranch: z.string().min(1),
})
.strict(),
monthlyBudgetCredits: credits,
usedCredits: credits,
enabled: z.boolean(),
screenshots: z
.object({
allowed: z.boolean(),
changedBy: z.string().min(1).nullable(),
changedAt: timestamp.nullable(),
})
.strict(),
permissions: z.object({ changeBudget: z.boolean(), changeRepository: z.boolean() }).strict(),
})
.strict() satisfies z.ZodType<EvaluationSettingsResponse>
9 changes: 9 additions & 0 deletions hub/client/application/hub/learnings-contracts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,12 @@ export interface LearningShareInput {
export interface LearningMutationResponse {
notice: string
}

export interface EvaluationSettingsResponse {
repository: { id: string; nameWithOwner: string; defaultBranch: string }
monthlyBudgetCredits: string
usedCredits: string
enabled: boolean
screenshots: { allowed: boolean; changedBy: string | null; changedAt: string | null }
permissions: { changeBudget: boolean; changeRepository: boolean }
}
128 changes: 128 additions & 0 deletions hub/client/application/hub/routes/learning-evaluations/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { useFetcher, useLocation } from "react-router"

import { canvasActionError } from "@application/hub/canvas-connections/contract"
import { recordIdentifierSchema } from "@application/hub/contracts"
import { FormBoundary } from "@application/hub/form-boundary"
import {
evaluationSettingsResponseSchema,
learningMutationResponseSchema,
} from "@application/hub/learnings-contracts"
import { requestJson } from "@application/hub/requests/request-json"
import { EvaluationSettingsPage } from "@pages/evaluation-settings-page"

import type { Route } from "./+types/index"
import type { WorkspaceRouteHandle } from "../workspace/types"

export const handle = { workspaceSection: "learnings" } satisfies WorkspaceRouteHandle

function repositoryId(value: string | undefined) {
const parsed = recordIdentifierSchema.safeParse(value)
if (!parsed.success) throw new Response("Invalid repository identifier.", { status: 400 })
return parsed.data
}

export async function clientLoader({ request, params }: Route.ClientLoaderArgs) {
const id = repositoryId(params.repositoryId)
return requestJson(
new URL(`/api/repositories/${id}/learnings/evaluations`, request.url),
evaluationSettingsResponseSchema,
{ signal: request.signal },
)
}

/** Reads an optional on/off field the switches send as text. */
function readSwitch(form: FormData, name: string) {
const value = form.get(name)
if (value === null) return undefined
if (value !== "true" && value !== "false") {
throw new Response(`A valid ${name} value is required.`, { status: 422 })
}
return value === "true"
}

export async function clientAction({ request, params }: Route.ClientActionArgs) {
const id = repositoryId(params.repositoryId)
if (request.method !== "POST") throw new Response("Method not allowed.", { status: 405 })
const form = await request.formData()
const budget = form.get("monthlyBudgetCredits")
try {
if (typeof budget === "string") {
return await requestJson(
new URL("/api/learnings/evaluation-budget", request.url),
learningMutationResponseSchema,
{
method: "PATCH",
body: JSON.stringify({ monthlyBudgetCredits: budget.trim() }),
signal: request.signal,
},
)
}
return await requestJson(
new URL(`/api/repositories/${id}/learnings/evaluations`, request.url),
learningMutationResponseSchema,
{
method: "PATCH",
body: JSON.stringify({
enabled: readSwitch(form, "enabled"),
screenshots: readSwitch(form, "screenshots"),
}),
signal: request.signal,
},
)
} catch (error) {
return canvasActionError(error)
}
}

export function meta() {
return [{ title: "Automated evaluations - Replica Hub" }]
}

function dateLabel(value: string) {
return new Date(value).toLocaleDateString()
}

export default function LearningEvaluations({ loaderData, params }: Route.ComponentProps) {
const fetcher = useFetcher<{ error?: string; notice?: string }>()
const { pathname } = useLocation()
const busy = fetcher.state !== "idle"
const { screenshots } = loaderData
function submit(form: FormData) {
void fetcher.submit(form, { method: "post", action: pathname })
}
return (
<FormBoundary
busy={busy}
error={busy ? null : fetcher.data?.error}
notice={busy ? null : fetcher.data?.notice}
onSubmit={submit}
>
<EvaluationSettingsPage
busy={busy}
data={{
repositoryName: loaderData.repository.nameWithOwner,
learningsPath: `/repositories/${params.repositoryId}/learnings`,
budget: {
limitCredits: loaderData.monthlyBudgetCredits,
usedCredits: loaderData.usedCredits,
editable: loaderData.permissions.changeBudget,
},
evaluations: {
editable: loaderData.permissions.changeRepository,
enabled: loaderData.enabled,
screenshotsAllowed: screenshots.allowed,
screenshotsRecord:
screenshots.changedBy && screenshots.changedAt
? `${screenshots.allowed ? "Allowed" : "Turned off"} by @${screenshots.changedBy} on ${dateLabel(screenshots.changedAt)}`
: null,
},
}}
onChange={(change) => {
const form = new FormData()
for (const [name, value] of Object.entries(change)) form.set(name, String(value))
submit(form)
}}
/>
</FormBoundary>
)
}
1 change: 1 addition & 0 deletions hub/client/application/hub/routes/product-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const repositoryProductRoutes: RouteConfigEntry[] = [
route("analytics", "routes/settings-analytics/index.tsx"),
route("learnings", "routes/settings-learnings/index.tsx"),
route("learnings/agents", "routes/learning-agents/index.tsx"),
route("learnings/evaluations", "routes/learning-evaluations/index.tsx"),
]

export const workspaceProductRoutes: RouteConfigEntry[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ export function settingsLearnings(
agentsPath: workspace.repositories.find(({ id }) => id === repositoryId)?.capabilities?.local
? null
: `/repositories/${repositoryId}/learnings/agents`,
evaluationsPath:
state.capabilities.administerHub || state.capabilities.administerRepository
? `/repositories/${repositoryId}/learnings/evaluations`
: null,
run: {
path: `/repositories/${repositoryId}/learnings/runs`,
available: state.permissions.requestRun && state.runAvailable,
Expand Down
35 changes: 35 additions & 0 deletions hub/client/pages/evaluation-settings-page/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { fireEvent, render, screen } from "@testing-library/react"
import { describe, expect, it, vi } from "vitest"

import { HubThemeProvider } from "@hooks/use-hub-theme"

import { EvaluationSettingsPage } from "."

describe("EvaluationSettingsPage()", () => {
it("allows screenshots only once the repository is evaluated", () => {
const onChange = vi.fn()
render(
<EvaluationSettingsPage
busy={false}
data={{
repositoryName: "github/prototype",
learningsPath: "/repositories/2001/learnings",
budget: { limitCredits: "100", usedCredits: "0", editable: false },
evaluations: {
editable: true,
enabled: false,
screenshotsAllowed: false,
screenshotsRecord: null,
},
}}
onChange={onChange}
/>,
{ wrapper: HubThemeProvider },
)

fireEvent.click(screen.getByRole("switch", { name: "Evaluate github/prototype" }))

expect(screen.getByRole("switch", { name: "Allow screenshots" })).toBeDisabled()
expect(onChange).toHaveBeenCalledWith({ enabled: true })
})
})
Loading
Loading