Skip to content
Open
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
12 changes: 12 additions & 0 deletions apps/web/src/components/chat/composerSend.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { threadContextReference } from "../../lib/composerContextRecords";
import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
import {
approvalRedirectNotice,
autoBalanceLabel,
loadBalancingSendWarning,
multipleModelSendBlock,
referencedThreadContexts,
Expand All @@ -25,6 +26,17 @@ describe("referencedThreadContexts", () => {
});
});

describe("autoBalanceLabel", () => {
it.each([
["checks machines while a candidate is pending", true, 2, remote, "Checking machines…"],
["stays neutral with no candidate machine", false, 0, null, "Auto balance"],
["stays neutral once a machine is picked", false, 2, remote, "Auto balance"],
["is unavailable when every candidate is too busy", false, 2, null, "Auto balance unavailable"],
] as const)("%s", (_case, pending, candidateCount, environmentId, label) => {
expect(autoBalanceLabel({ pending, candidateCount, environmentId })).toBe(label);
});
});

describe("loadBalancingSendWarning", () => {
it("asks the user to wait while resource checks run", () => {
expect(
Expand Down
12 changes: 12 additions & 0 deletions apps/web/src/components/chat/composerSend.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export function referencedThreadContexts(draft: {
return draft.threadContexts.filter((record) => referenced.has(record.contextId));
}

/** Auto's label: unavailable once every checked candidate machine is too busy or failed. */
export function autoBalanceLabel(input: {
readonly pending: boolean;
readonly candidateCount: number;
readonly environmentId: EnvironmentId | null;
}) {
if (input.pending) return "Checking machines…";
return input.candidateCount > 0 && input.environmentId === null
? "Auto balance unavailable"
: "Auto balance";
}

/** Why a send waits for the user to pick a machine when no machine was chosen automatically. */
export function loadBalancingSendWarning(input: {
readonly pending: boolean;
Expand Down
13 changes: 6 additions & 7 deletions apps/web/src/components/chat/useDraftEnvironmentRouting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
import { useEffect, useLayoutEffect, useRef, useState } from "react";
import { toastManager } from "../ui/toast";
import { autoBalanceLabel } from "./composerSend.logic";
import {
deriveLogicalProjectKeyFromSettings,
selectProjectGroupingSettings,
Expand Down Expand Up @@ -156,13 +157,11 @@ export function useDraftEnvironmentRouting(input: DraftEnvironmentRoutingInput)
};
const onAutoEnvironmentStable = useStableCallback(onAutoEnvironment);
const autoEnvironmentLabel = automaticEnvironment
? draftThread?.loadBalancedEnvironmentId
? "Auto balance"
: loadBalancing.pending
? "Checking machines…"
: loadBalancing.failed
? "Auto balance unavailable"
: "Auto balance"
? autoBalanceLabel({
pending: loadBalancing.pending,
candidateCount: loadBalancingCandidates.length,
environmentId: loadBalancing.environmentId,
})
: undefined;

// A change in flight belongs to the draft and project it started on.
Expand Down
8 changes: 1 addition & 7 deletions apps/web/src/hooks/useLoadBalancedEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const hostResourcesAtom = Atom.family((environmentIdsKey: string) =>
resources: result._tag === "Success" ? result.value : null,
receivedAt: result._tag === "Success" ? result.timestamp : 0,
pending: result._tag === "Initial" || result.waiting,
failed: result._tag === "Failure",
};
});
// Re-read when the next fresh snapshot ages out, so a frozen evaluatedAt never selects it.
Expand Down Expand Up @@ -59,10 +58,5 @@ export function useLoadBalancedEnvironment(
})),
evaluatedAt,
) as EnvironmentId | null;
return {
refresh,
pending,
environmentId,
failed: !pending && environmentId === null && resources.some((resource) => resource.failed),
};
return { refresh, pending, environmentId };
}
Loading