diff --git a/packages/app/src/components/provider-installation.tsx b/packages/app/src/components/provider-installation.tsx index 8d28356e40..483917d935 100644 --- a/packages/app/src/components/provider-installation.tsx +++ b/packages/app/src/components/provider-installation.tsx @@ -1,9 +1,7 @@ import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useCallback, useEffect, useState } from "react"; -import type { - ProviderInstallationUpdate, - ProviderInstallationUpdateProgress, -} from "@getpaseo/protocol/messages"; +import { useTranslation } from "react-i18next"; +import type { ProviderInstallationUpdate } from "@getpaseo/protocol/messages"; import { useFetchQuery } from "@/data/query"; import { useHostFeature } from "@/runtime/host-features"; import { useHostRuntimeClient } from "@/runtime/host-runtime"; @@ -12,6 +10,7 @@ import type { ProviderUpdateAction, ProviderUpdatesState, } from "./provider-installation-section"; +import { applyProviderUpdateProgress } from "./provider-update-progress"; export interface ProviderInstallationRead { state: ProviderInstallationState; @@ -22,19 +21,6 @@ export interface ProviderInstallationRead { refresh: () => void; } -/** Folds a progress message into the update it belongs to; a new operation starts empty. */ -function applyProgress( - current: ProviderInstallationUpdate | null, - progress: ProviderInstallationUpdateProgress, -): ProviderInstallationUpdate { - const previous = current?.operation.id === progress.operation.id ? current : null; - return { - operation: progress.operation, - output: [...(previous?.output ?? []), ...progress.output], - ...(previous?.truncated ? { truncated: true } : {}), - }; -} - /** * Reads a provider's installation from hosts that advertise `providerInstallation`. The update * check is a separate request that starts once the provider reports itself installed, so the @@ -49,27 +35,28 @@ export function useProviderInstallation(input: { const supported = useHostFeature(serverId, "providerInstallation"); const client = useHostRuntimeClient(serverId); const queryClient = useQueryClient(); + const { t } = useTranslation(); const enabled = supported && visible && Boolean(client); const installationKey = ["provider-installation", serverId, provider] as const; const updatesKey = ["provider-updates", serverId, provider] as const; const inspect = useCallback( async (refresh: boolean) => { - if (!client) throw new Error("Host is not connected"); + if (!client) throw new Error(t("workspace.terminal.hostDisconnected")); const result = await client.inspectProviderInstallation(provider, { refresh }); if (result.error) throw new Error(result.error); return result.installation; }, - [client, provider], + [client, provider, t], ); const checkForUpdates = useCallback( async (refresh: boolean) => { - if (!client) throw new Error("Host is not connected"); + if (!client) throw new Error(t("workspace.terminal.hostDisconnected")); const result = await client.checkProviderUpdates(provider, { refresh }); if (result.error) throw new Error(result.error); return result.updateCheck; }, - [client, provider], + [client, provider, t], ); // The host caches both answers, so reading on every open is cheap. @@ -89,13 +76,23 @@ export function useProviderInstallation(input: { staleTimeMs: 0, }); + // A refresh runs through the queries so its answer or its failure is what the screen shows. + // `staleTime: 0` overrides the app default so the request always goes out. const refreshInstallation = useMutation({ - mutationFn: () => inspect(true), - onSuccess: (installation) => queryClient.setQueryData(installationKey, installation), + mutationFn: () => + queryClient.fetchQuery({ + queryKey: installationKey, + queryFn: () => inspect(true), + staleTime: 0, + }), }); const refreshUpdates = useMutation({ - mutationFn: () => checkForUpdates(true), - onSuccess: (updateCheck) => queryClient.setQueryData(updatesKey, updateCheck), + mutationFn: () => + queryClient.fetchQuery({ + queryKey: updatesKey, + queryFn: () => checkForUpdates(true), + staleTime: 0, + }), }); // The host owns the update; this view follows it while open and catches up on reopen. const [currentUpdate, setCurrentUpdate] = useState(null); @@ -106,7 +103,7 @@ export function useProviderInstallation(input: { snapshot: (response) => setCurrentUpdate(response.update), update: (message) => { if (message.type !== "provider.installation.update.progress") return; - setCurrentUpdate((current) => applyProgress(current, message.payload)); + setCurrentUpdate((current) => applyProviderUpdateProgress(current, message.payload)); // The host dropped its cached answers; read the new version and update state. if (message.payload.operation.status === "succeeded") { void queryClient.invalidateQueries({ @@ -128,7 +125,7 @@ export function useProviderInstallation(input: { }, [client, enabled, provider, queryClient, serverId]); const startUpdate = useMutation({ mutationFn: async () => { - if (!client) throw new Error("Host is not connected"); + if (!client) throw new Error(t("workspace.terminal.hostDisconnected")); const result = await client.updateProvider(provider); if (result.error) throw new Error(result.error); return result.update; diff --git a/packages/app/src/components/provider-update-progress.test.ts b/packages/app/src/components/provider-update-progress.test.ts new file mode 100644 index 0000000000..db6a53aa3b --- /dev/null +++ b/packages/app/src/components/provider-update-progress.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "vitest"; +import type { ProviderInstallationUpdateProgress } from "@getpaseo/protocol/messages"; +import { applyProviderUpdateProgress } from "./provider-update-progress"; + +const running = { + status: "running", + id: "update-1", + startedAt: "2026-11-13T00:00:00.000Z", +} as const; + +function progress(...texts: string[]): ProviderInstallationUpdateProgress { + return { + provider: "codex", + operation: running, + output: texts.map((text) => ({ type: "stdout", text })), + }; +} + +describe("applyProviderUpdateProgress", () => { + it("appends output for the same update", () => { + const first = applyProviderUpdateProgress(null, progress("a"), { outputLimit: 10 }); + const second = applyProviderUpdateProgress(first, progress("b"), { outputLimit: 10 }); + expect(second.output.map((entry) => entry.text)).toEqual(["a", "b"]); + expect(second.truncated).toBeUndefined(); + }); + + it("drops the oldest output beyond the limit and marks the update truncated", () => { + let update = applyProviderUpdateProgress(null, progress("12345"), { outputLimit: 8 }); + update = applyProviderUpdateProgress(update, progress("6789"), { outputLimit: 8 }); + expect(update.output.map((entry) => entry.text)).toEqual(["6789"]); + expect(update.truncated).toBe(true); + + update = applyProviderUpdateProgress(update, progress("abcdefghij"), { outputLimit: 8 }); + expect(update.output.map((entry) => entry.text)).toEqual(["cdefghij"]); + expect(update.truncated).toBe(true); + }); + + it("starts a new update empty", () => { + const first = applyProviderUpdateProgress(null, progress("123456789"), { outputLimit: 4 }); + const next = applyProviderUpdateProgress( + first, + { ...progress("x"), operation: { ...running, id: "update-2" } }, + { outputLimit: 4 }, + ); + expect(next.output.map((entry) => entry.text)).toEqual(["x"]); + expect(next.truncated).toBeUndefined(); + }); +}); diff --git a/packages/app/src/components/provider-update-progress.ts b/packages/app/src/components/provider-update-progress.ts new file mode 100644 index 0000000000..ad72471d4c --- /dev/null +++ b/packages/app/src/components/provider-update-progress.ts @@ -0,0 +1,38 @@ +import type { + ProviderInstallationUpdate, + ProviderInstallationUpdateOutput, + ProviderInstallationUpdateProgress, +} from "@getpaseo/protocol/messages"; + +/** Characters of update output the app keeps, matching what the daemon retains. */ +export const PROVIDER_UPDATE_OUTPUT_LIMIT = 64 * 1024; + +/** + * Folds a progress message into the update it belongs to; a new operation starts empty. Output + * beyond `outputLimit` characters drops oldest first and marks the update truncated, so a noisy + * updater cannot grow the screen's memory while it stays open. + */ +export function applyProviderUpdateProgress( + current: ProviderInstallationUpdate | null, + progress: ProviderInstallationUpdateProgress, + options: { outputLimit: number } = { outputLimit: PROVIDER_UPDATE_OUTPUT_LIMIT }, +): ProviderInstallationUpdate { + const previous = current?.operation.id === progress.operation.id ? current : null; + const limit = options.outputLimit; + const output: ProviderInstallationUpdateOutput[] = [ + ...(previous?.output ?? []), + ...progress.output, + ]; + let length = output.reduce((total, entry) => total + entry.text.length, 0); + let truncated = previous?.truncated ?? false; + while (length > limit && output.length > 1) { + length -= output.shift()!.text.length; + truncated = true; + } + const last = output.at(-1); + if (last && last.text.length > limit) { + output[output.length - 1] = { type: last.type, text: last.text.slice(-limit) }; + truncated = true; + } + return { operation: progress.operation, output, ...(truncated ? { truncated: true } : {}) }; +}