mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
fix(app): add explicit retry/discard recovery for failed startup settings and keep live edits independent
This commit is contained in:
@@ -26,7 +26,6 @@ import { useProvidersSnapshot } from "@/hooks/use-providers-snapshot";
|
||||
import { useDraftProviderOptions } from "@/hooks/use-draft-provider-options";
|
||||
import {
|
||||
clearStagedProviderSwitch,
|
||||
dismissRetainedProviderSwitchEditFailure,
|
||||
isStagedProviderSwitchInFlight,
|
||||
type StagedProviderSwitch,
|
||||
useStagedProviderSwitch,
|
||||
@@ -1321,7 +1320,6 @@ export const AgentControls = memo(function AgentControls({
|
||||
}
|
||||
try {
|
||||
await client.setAgentModel(agentId, modelId, { expectedProvider: agentProvider });
|
||||
dismissRetainedProviderSwitchEditFailure(serverId, agentId);
|
||||
await updatePreferences((current) =>
|
||||
mergeProviderPreferences({
|
||||
preferences: current,
|
||||
@@ -1334,7 +1332,7 @@ export const AgentControls = memo(function AgentControls({
|
||||
toast.error(toErrorMessage(error));
|
||||
}
|
||||
},
|
||||
[agentId, agentProvider, client, serverId, toast, updatePreferences],
|
||||
[agentId, agentProvider, client, toast, updatePreferences],
|
||||
);
|
||||
|
||||
const handleSelectModel = useCallback(
|
||||
@@ -1517,11 +1515,7 @@ export const AgentControls = memo(function AgentControls({
|
||||
}
|
||||
void client
|
||||
.setAgentThinkingOption(agentId, thinkingOptionId, { expectedProvider: agentProvider })
|
||||
.then((notice) => {
|
||||
dismissRetainedProviderSwitchEditFailure(serverId, agentId);
|
||||
showProviderNoticeToast(toast, notice);
|
||||
return undefined;
|
||||
})
|
||||
.then((notice) => showProviderNoticeToast(toast, notice))
|
||||
.catch((error) => {
|
||||
console.warn("[AgentControls] setAgentThinkingOption failed", error);
|
||||
toast.error(toErrorMessage(error));
|
||||
@@ -1567,10 +1561,6 @@ export const AgentControls = memo(function AgentControls({
|
||||
});
|
||||
void client
|
||||
.setAgentOptions(agentId, { [key]: value }, { expectedProvider: agentProvider })
|
||||
.then(() => {
|
||||
dismissRetainedProviderSwitchEditFailure(serverId, agentId);
|
||||
return undefined;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn("[AgentControls] setAgentOptions failed", error);
|
||||
toast.error(toErrorMessage(error));
|
||||
|
||||
@@ -13,12 +13,12 @@ import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
beginStagedProviderSwitch,
|
||||
clearStagedProviderSwitch,
|
||||
dismissRetainedProviderSwitchEditFailure,
|
||||
endStagedProviderSwitch,
|
||||
getFailedStartupSettings,
|
||||
getStagedProviderSwitch,
|
||||
hasRetainedProviderSwitchEditFailure,
|
||||
isStagedProviderSwitchInFlight,
|
||||
settleCommittedProviderSwitch,
|
||||
useFailedStartupSettings,
|
||||
useStagedProviderSwitch,
|
||||
} from "@/composer/provider-switch";
|
||||
import { useProvidersSnapshot } from "@/hooks/use-providers-snapshot";
|
||||
@@ -1384,6 +1384,7 @@ function ComposerContentImpl({
|
||||
(state) => state.sessions[serverId]?.serverInfo?.features?.providerSwitching === true,
|
||||
);
|
||||
const rawStagedSwitch = useStagedProviderSwitch(serverId, agentId);
|
||||
const failedStartupSettings = useFailedStartupSettings(serverId, agentId);
|
||||
const stagedSwitch =
|
||||
supportsProviderSwitching &&
|
||||
rawStagedSwitch &&
|
||||
@@ -1402,7 +1403,7 @@ function ComposerContentImpl({
|
||||
agentState.provider &&
|
||||
rawStagedSwitch.provider === agentState.provider &&
|
||||
!isStagedProviderSwitchInFlight(serverId, agentId) &&
|
||||
!hasRetainedProviderSwitchEditFailure(serverId, agentId)
|
||||
!getFailedStartupSettings(serverId, agentId)
|
||||
) {
|
||||
clearStagedProviderSwitch(serverId, agentId);
|
||||
}
|
||||
@@ -1414,6 +1415,16 @@ function ComposerContentImpl({
|
||||
useSessionStore.getState().setQueuedSendError(serverId, agentId, null);
|
||||
}, [agentId, serverId]);
|
||||
|
||||
const handleRetryFailedStartupSettings = useCallback(() => {
|
||||
setSendError(null);
|
||||
void getHostRuntimeStore().recoverFailedStartupSettings(serverId, agentId, "retry");
|
||||
}, [agentId, serverId]);
|
||||
|
||||
const handleDismissFailedStartupSettings = useCallback(() => {
|
||||
setSendError(null);
|
||||
void getHostRuntimeStore().recoverFailedStartupSettings(serverId, agentId, "discard");
|
||||
}, [agentId, serverId]);
|
||||
|
||||
const queuedMessagesRaw = useSessionStore((state) =>
|
||||
state.sessions[serverId]?.queuedMessages?.get(agentId),
|
||||
);
|
||||
@@ -1781,9 +1792,6 @@ function ComposerContentImpl({
|
||||
const stagedProviderSwitch =
|
||||
pendingSwitch && pendingSwitch.provider !== liveProvider ? pendingSwitch : null;
|
||||
useSessionStore.getState().setQueuedSendError(serverId, targetAgentId, null);
|
||||
if (!stagedProviderSwitch) {
|
||||
dismissRetainedProviderSwitchEditFailure(serverId, targetAgentId);
|
||||
}
|
||||
if (stagedProviderSwitch) {
|
||||
beginStagedProviderSwitch(serverId, targetAgentId);
|
||||
}
|
||||
@@ -2186,7 +2194,6 @@ function ComposerContentImpl({
|
||||
});
|
||||
if (!result) return;
|
||||
useSessionStore.getState().setQueuedSendError(serverId, agentId, null);
|
||||
dismissRetainedProviderSwitchEditFailure(serverId, agentId);
|
||||
replaceUserInput(result.text);
|
||||
setSelectedAttachments(result.attachments);
|
||||
},
|
||||
@@ -2197,7 +2204,6 @@ function ComposerContentImpl({
|
||||
async (id: string) => {
|
||||
if (!sendAgentMessageRef.current && !onSubmitMessageRef.current) return;
|
||||
useSessionStore.getState().setQueuedSendError(serverId, agentId, null);
|
||||
dismissRetainedProviderSwitchEditFailure(serverId, agentId);
|
||||
// Reuse the regular send path; server-side send atomically interrupts any active run.
|
||||
const result = await sendQueuedComposerMessageNow({
|
||||
agentId,
|
||||
@@ -2664,13 +2670,41 @@ function ComposerContentImpl({
|
||||
// Focusing the composer on a touch screen raises the on-screen keyboard over the conversation.
|
||||
const messageInputAutoFocus = autoFocus && isDesktopWebBreakpoint && hasFinePointer;
|
||||
const submitLoadingPressHandler = isAgentRunning ? handleCancelAgent : undefined;
|
||||
const displayError = externalError || sendError || queuedSendError;
|
||||
const displayError =
|
||||
externalError || sendError || failedStartupSettings?.errorMessage || queuedSendError;
|
||||
const sendErrorNode = useMemo(
|
||||
() =>
|
||||
displayError ? (
|
||||
<Alert variant="error" size="sm" description={displayError} testID="composer-error" />
|
||||
<Alert variant="error" size="sm" description={displayError} testID="composer-error">
|
||||
{failedStartupSettings ? (
|
||||
<>
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onPress={handleRetryFailedStartupSettings}
|
||||
testID="retry-failed-startup-settings"
|
||||
>
|
||||
{t("common.actions.retry")}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onPress={handleDismissFailedStartupSettings}
|
||||
testID="dismiss-failed-startup-settings"
|
||||
>
|
||||
{t("common.actions.dismiss")}
|
||||
</Button>
|
||||
</>
|
||||
) : null}
|
||||
</Alert>
|
||||
) : null,
|
||||
[displayError],
|
||||
[
|
||||
displayError,
|
||||
failedStartupSettings,
|
||||
handleDismissFailedStartupSettings,
|
||||
handleRetryFailedStartupSettings,
|
||||
t,
|
||||
],
|
||||
);
|
||||
const githubEmptyText = githubSearchResultsQuery.isFetching
|
||||
? t("composer.github.searching")
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
export {
|
||||
applyFailedStartupSettings,
|
||||
beginStagedProviderSwitch,
|
||||
clearStagedProviderSwitch,
|
||||
dismissRetainedProviderSwitchEditFailure,
|
||||
discardFailedStartupSettings,
|
||||
endStagedProviderSwitch,
|
||||
getFailedStartupSettings,
|
||||
getStagedProviderSwitch,
|
||||
hasRetainedProviderSwitchEditFailure,
|
||||
isStagedProviderSwitchInFlight,
|
||||
settleCommittedProviderSwitch,
|
||||
settleStagedProviderSwitch,
|
||||
useFailedStartupSettings,
|
||||
useStagedProviderSwitch,
|
||||
useStagedProviderSwitchStore,
|
||||
type FailedStartupSettings,
|
||||
type LiveAgentSettingsWriter,
|
||||
type SettleCommittedProviderSwitchResult,
|
||||
type StagedProviderSwitch,
|
||||
|
||||
@@ -15,7 +15,7 @@ import { useSessionStore } from "@/stores/session-store";
|
||||
import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
|
||||
import type { ProviderSwitchItem, StreamItem, UserMessageItem } from "@/types/stream";
|
||||
import {
|
||||
dismissRetainedProviderSwitchEditFailure,
|
||||
getFailedStartupSettings,
|
||||
getStagedProviderSwitch,
|
||||
useStagedProviderSwitchStore,
|
||||
} from "./index";
|
||||
@@ -1075,6 +1075,9 @@ describe("app provider switching integration", () => {
|
||||
expect(useSessionStore.getState().sessions[serverId]?.queuedSendErrors.get("agent-1")).toBe(
|
||||
"Plan mode rejected by provider",
|
||||
);
|
||||
expect(getFailedStartupSettings(serverId, "agent-1")?.bundle).toEqual({
|
||||
options: { plan_mode: true },
|
||||
});
|
||||
expect(getStagedProviderSwitch(serverId, "agent-1")?.providerOptions).toEqual({
|
||||
plan_mode: true,
|
||||
});
|
||||
@@ -1083,12 +1086,70 @@ describe("app provider switching integration", () => {
|
||||
{ id: "m4", text: "Must stay queued", attachments: [] },
|
||||
]);
|
||||
|
||||
// Once the user explicitly dismisses or re-applies the failed settings, automatic drain resumes.
|
||||
dismissRetainedProviderSwitchEditFailure(serverId, "agent-1");
|
||||
runtime.drainQueuedAgentMessage(serverId, "agent-1");
|
||||
// Retrying via the single recovery action re-applies the exact failed bundle (plan_mode: true)
|
||||
// and automatically resumes the idle queue strictly AFTER the retry succeeds!
|
||||
const retryPromise = runtime.recoverFailedStartupSettings(serverId, "agent-1", "retry");
|
||||
await vi.waitFor(() => {
|
||||
expect(callOrder).toHaveLength(3);
|
||||
});
|
||||
expect(callOrder[2]).toContain('"plan_mode":true');
|
||||
// Still waiting for applyAgentConfig response -> M4 MUST NOT send yet!
|
||||
expect(callOrder.some((entry) => entry.startsWith("send:"))).toBe(false);
|
||||
resolveApplyConfig();
|
||||
expect(await retryPromise).toBe(true);
|
||||
await vi.waitFor(() => {
|
||||
expect(callOrder).toContain("send:Must stay queued");
|
||||
});
|
||||
expect(getFailedStartupSettings(serverId, "agent-1")).toBeNull();
|
||||
expect(useSessionStore.getState().sessions[serverId]?.queuedSendErrors.get("agent-1")).toBe(
|
||||
undefined,
|
||||
);
|
||||
|
||||
// Case 3: Explicit discard clears the failed settings and resumes an idle queue immediately.
|
||||
callOrder.length = 0;
|
||||
sessionStore.setQueuedMessages(
|
||||
serverId,
|
||||
new Map([
|
||||
[
|
||||
"agent-1",
|
||||
[
|
||||
{ id: "m5", text: "Switch with discarded edit", attachments: [] },
|
||||
{ id: "m6", text: "Send after explicit discard", attachments: [] },
|
||||
],
|
||||
],
|
||||
]),
|
||||
);
|
||||
useStagedProviderSwitchStore.getState().stage(serverId, "agent-1", {
|
||||
provider: "codex",
|
||||
model: "gpt-5.3",
|
||||
thinkingOptionId: null,
|
||||
modeId: null,
|
||||
providerOptions: { plan_mode: false },
|
||||
});
|
||||
runtime.drainQueuedAgentMessage(serverId, "agent-1");
|
||||
await vi.waitFor(() => {
|
||||
expect(callOrder).toEqual(["switch:codex"]);
|
||||
});
|
||||
useStagedProviderSwitchStore.getState().patch(serverId, "agent-1", (current) => ({
|
||||
...current,
|
||||
providerOptions: { plan_mode: true },
|
||||
}));
|
||||
runtime.drainQueuedAgentMessage(serverId, "agent-1");
|
||||
resolveSwitch({ agent: { ...agentSnapshot("codex"), status: "idle" } });
|
||||
await vi.waitFor(() => {
|
||||
expect(callOrder).toHaveLength(2);
|
||||
});
|
||||
rejectApplyConfig!(new Error("Disconnected"));
|
||||
await vi.waitFor(() => {
|
||||
expect(getFailedStartupSettings(serverId, "agent-1")?.errorMessage).toBe("Disconnected");
|
||||
});
|
||||
expect(callOrder.some((entry) => entry.startsWith("send:"))).toBe(false);
|
||||
|
||||
expect(await runtime.recoverFailedStartupSettings(serverId, "agent-1", "discard")).toBe(true);
|
||||
await vi.waitFor(() => {
|
||||
expect(callOrder).toContain("send:Send after explicit discard");
|
||||
});
|
||||
expect(getFailedStartupSettings(serverId, "agent-1")).toBeNull();
|
||||
|
||||
sessionStore.clearSession(serverId);
|
||||
});
|
||||
|
||||
@@ -42,6 +42,12 @@ export interface LiveAgentSettingsWriter {
|
||||
) => Promise<unknown>;
|
||||
}
|
||||
|
||||
export interface FailedStartupSettings {
|
||||
provider: AgentProvider;
|
||||
bundle: AgentConfigApply;
|
||||
errorMessage: string;
|
||||
}
|
||||
|
||||
export type SettleCommittedProviderSwitchResult =
|
||||
| { applied: true }
|
||||
| { applied: false; errorMessage: string };
|
||||
@@ -49,7 +55,7 @@ export type SettleCommittedProviderSwitchResult =
|
||||
interface StagedProviderSwitchState {
|
||||
stagedByKey: Map<string, StagedProviderSwitch>;
|
||||
inFlightByKey: Map<string, number>;
|
||||
retainedOnEditFailureByKey: Set<string>;
|
||||
failedStartupSettingsByKey: Map<string, FailedStartupSettings>;
|
||||
stage: (serverId: string, agentId: string, draft: StagedProviderSwitch) => void;
|
||||
patch: (
|
||||
serverId: string,
|
||||
@@ -58,7 +64,11 @@ interface StagedProviderSwitchState {
|
||||
) => void;
|
||||
beginSwitch: (serverId: string, agentId: string) => void;
|
||||
endSwitch: (serverId: string, agentId: string) => void;
|
||||
markRetainedOnEditFailure: (serverId: string, agentId: string) => void;
|
||||
setFailedStartupSettings: (
|
||||
serverId: string,
|
||||
agentId: string,
|
||||
failed: FailedStartupSettings | null,
|
||||
) => void;
|
||||
settle: (serverId: string, agentId: string, sentDraft: StagedProviderSwitch) => void;
|
||||
clear: (serverId: string, agentId: string) => void;
|
||||
clearForServer: (serverId: string) => void;
|
||||
@@ -67,16 +77,16 @@ interface StagedProviderSwitchState {
|
||||
export const useStagedProviderSwitchStore = create<StagedProviderSwitchState>((set) => ({
|
||||
stagedByKey: new Map(),
|
||||
inFlightByKey: new Map(),
|
||||
retainedOnEditFailureByKey: new Set(),
|
||||
failedStartupSettingsByKey: new Map(),
|
||||
|
||||
stage: (serverId, agentId, draft) =>
|
||||
set((state) => {
|
||||
const key = stagedKey(serverId, agentId);
|
||||
const next = new Map(state.stagedByKey);
|
||||
next.set(key, draft);
|
||||
const nextRetained = new Set(state.retainedOnEditFailureByKey);
|
||||
nextRetained.delete(key);
|
||||
return { stagedByKey: next, retainedOnEditFailureByKey: nextRetained };
|
||||
const nextFailed = new Map(state.failedStartupSettingsByKey);
|
||||
nextFailed.delete(key);
|
||||
return { stagedByKey: next, failedStartupSettingsByKey: nextFailed };
|
||||
}),
|
||||
|
||||
patch: (serverId, agentId, updater) =>
|
||||
@@ -88,9 +98,7 @@ export const useStagedProviderSwitchStore = create<StagedProviderSwitchState>((s
|
||||
if (updated === current) return state;
|
||||
const next = new Map(state.stagedByKey);
|
||||
next.set(key, updated);
|
||||
const nextRetained = new Set(state.retainedOnEditFailureByKey);
|
||||
nextRetained.delete(key);
|
||||
return { stagedByKey: next, retainedOnEditFailureByKey: nextRetained };
|
||||
return { stagedByKey: next };
|
||||
}),
|
||||
|
||||
beginSwitch: (serverId, agentId) =>
|
||||
@@ -115,13 +123,17 @@ export const useStagedProviderSwitchStore = create<StagedProviderSwitchState>((s
|
||||
return { inFlightByKey: next };
|
||||
}),
|
||||
|
||||
markRetainedOnEditFailure: (serverId, agentId) =>
|
||||
setFailedStartupSettings: (serverId, agentId, failed) =>
|
||||
set((state) => {
|
||||
const key = stagedKey(serverId, agentId);
|
||||
if (state.retainedOnEditFailureByKey.has(key)) return state;
|
||||
const next = new Set(state.retainedOnEditFailureByKey);
|
||||
next.add(key);
|
||||
return { retainedOnEditFailureByKey: next };
|
||||
const next = new Map(state.failedStartupSettingsByKey);
|
||||
if (failed === null) {
|
||||
if (!next.has(key)) return state;
|
||||
next.delete(key);
|
||||
} else {
|
||||
next.set(key, failed);
|
||||
}
|
||||
return { failedStartupSettingsByKey: next };
|
||||
}),
|
||||
|
||||
settle: (serverId, agentId, sentDraft) =>
|
||||
@@ -131,20 +143,20 @@ export const useStagedProviderSwitchStore = create<StagedProviderSwitchState>((s
|
||||
if (!current || current !== sentDraft) return state;
|
||||
const next = new Map(state.stagedByKey);
|
||||
next.delete(key);
|
||||
const nextRetained = new Set(state.retainedOnEditFailureByKey);
|
||||
nextRetained.delete(key);
|
||||
return { stagedByKey: next, retainedOnEditFailureByKey: nextRetained };
|
||||
const nextFailed = new Map(state.failedStartupSettingsByKey);
|
||||
nextFailed.delete(key);
|
||||
return { stagedByKey: next, failedStartupSettingsByKey: nextFailed };
|
||||
}),
|
||||
|
||||
clear: (serverId, agentId) =>
|
||||
set((state) => {
|
||||
const key = stagedKey(serverId, agentId);
|
||||
if (!state.stagedByKey.has(key) && !state.retainedOnEditFailureByKey.has(key)) return state;
|
||||
if (!state.stagedByKey.has(key) && !state.failedStartupSettingsByKey.has(key)) return state;
|
||||
const next = new Map(state.stagedByKey);
|
||||
next.delete(key);
|
||||
const nextRetained = new Set(state.retainedOnEditFailureByKey);
|
||||
nextRetained.delete(key);
|
||||
return { stagedByKey: next, retainedOnEditFailureByKey: nextRetained };
|
||||
const nextFailed = new Map(state.failedStartupSettingsByKey);
|
||||
nextFailed.delete(key);
|
||||
return { stagedByKey: next, failedStartupSettingsByKey: nextFailed };
|
||||
}),
|
||||
|
||||
clearForServer: (serverId) =>
|
||||
@@ -165,10 +177,10 @@ export const useStagedProviderSwitchStore = create<StagedProviderSwitchState>((s
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
const nextRetained = new Set(state.retainedOnEditFailureByKey);
|
||||
for (const key of nextRetained) {
|
||||
const nextFailed = new Map(state.failedStartupSettingsByKey);
|
||||
for (const key of nextFailed.keys()) {
|
||||
if (key.startsWith(prefix)) {
|
||||
nextRetained.delete(key);
|
||||
nextFailed.delete(key);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
@@ -176,7 +188,7 @@ export const useStagedProviderSwitchStore = create<StagedProviderSwitchState>((s
|
||||
? {
|
||||
stagedByKey: next,
|
||||
inFlightByKey: nextInFlight,
|
||||
retainedOnEditFailureByKey: nextRetained,
|
||||
failedStartupSettingsByKey: nextFailed,
|
||||
}
|
||||
: state;
|
||||
}),
|
||||
@@ -206,14 +218,28 @@ export function isStagedProviderSwitchInFlight(serverId: string, agentId: string
|
||||
);
|
||||
}
|
||||
|
||||
export function hasRetainedProviderSwitchEditFailure(serverId: string, agentId: string): boolean {
|
||||
return useStagedProviderSwitchStore
|
||||
.getState()
|
||||
.retainedOnEditFailureByKey.has(stagedKey(serverId, agentId));
|
||||
export function getFailedStartupSettings(
|
||||
serverId: string,
|
||||
agentId: string,
|
||||
): FailedStartupSettings | null {
|
||||
return (
|
||||
useStagedProviderSwitchStore
|
||||
.getState()
|
||||
.failedStartupSettingsByKey.get(stagedKey(serverId, agentId)) ?? null
|
||||
);
|
||||
}
|
||||
|
||||
export function dismissRetainedProviderSwitchEditFailure(serverId: string, agentId: string): void {
|
||||
if (!hasRetainedProviderSwitchEditFailure(serverId, agentId)) return;
|
||||
export function useFailedStartupSettings(
|
||||
serverId: string,
|
||||
agentId: string,
|
||||
): FailedStartupSettings | null {
|
||||
return useStagedProviderSwitchStore(
|
||||
(state) => state.failedStartupSettingsByKey.get(stagedKey(serverId, agentId)) ?? null,
|
||||
);
|
||||
}
|
||||
|
||||
export function discardFailedStartupSettings(serverId: string, agentId: string): void {
|
||||
if (!getFailedStartupSettings(serverId, agentId)) return;
|
||||
useStagedProviderSwitchStore.getState().clear(serverId, agentId);
|
||||
}
|
||||
|
||||
@@ -333,13 +359,41 @@ export async function settleCommittedProviderSwitch(input: {
|
||||
store.settle(serverId, agentId, remaining);
|
||||
return { applied: true };
|
||||
} catch (error) {
|
||||
store.markRetainedOnEditFailure(serverId, agentId);
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
store.setFailedStartupSettings(serverId, agentId, {
|
||||
provider: liveProvider,
|
||||
bundle,
|
||||
errorMessage,
|
||||
});
|
||||
console.warn("[ProviderSwitch] failed to forward edited settings after switch", error);
|
||||
return { applied: false, errorMessage };
|
||||
}
|
||||
}
|
||||
|
||||
/** Retries applying previously failed startup settings for a committed provider switch. */
|
||||
export async function applyFailedStartupSettings(input: {
|
||||
serverId: string;
|
||||
agentId: string;
|
||||
client: LiveAgentSettingsWriter;
|
||||
}): Promise<SettleCommittedProviderSwitchResult> {
|
||||
const { serverId, agentId, client } = input;
|
||||
const failed = getFailedStartupSettings(serverId, agentId);
|
||||
if (!failed) {
|
||||
return { applied: true };
|
||||
}
|
||||
const store = useStagedProviderSwitchStore.getState();
|
||||
try {
|
||||
await applyEditedDraftSettingsToLiveAgent(agentId, failed.bundle, failed.provider, client);
|
||||
store.clear(serverId, agentId);
|
||||
return { applied: true };
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
store.setFailedStartupSettings(serverId, agentId, { ...failed, errorMessage });
|
||||
console.warn("[ProviderSwitch] failed to retry startup settings", error);
|
||||
return { applied: false, errorMessage };
|
||||
}
|
||||
}
|
||||
|
||||
export function clearStagedProviderSwitch(serverId: string, agentId: string): void {
|
||||
useStagedProviderSwitchStore.getState().clear(serverId, agentId);
|
||||
}
|
||||
|
||||
@@ -84,10 +84,12 @@ import {
|
||||
import { schedulesQueryBaseKey } from "@/schedules/aggregated-schedules";
|
||||
import { dispatchComposerAgentMessage, sendQueuedComposerMessageNow } from "@/composer/actions";
|
||||
import {
|
||||
applyFailedStartupSettings,
|
||||
beginStagedProviderSwitch,
|
||||
discardFailedStartupSettings,
|
||||
endStagedProviderSwitch,
|
||||
getFailedStartupSettings,
|
||||
getStagedProviderSwitch,
|
||||
hasRetainedProviderSwitchEditFailure,
|
||||
settleCommittedProviderSwitch,
|
||||
} from "@/composer/provider-switch";
|
||||
import { createMessageSubmissionWriter } from "@/composer/submission/writer";
|
||||
@@ -2891,8 +2893,51 @@ export class HostRuntimeStore {
|
||||
}
|
||||
}
|
||||
|
||||
private resumeIdleQueueIfReady(serverId: string, agentId: string): void {
|
||||
const latestSession = useSessionStore.getState().sessions[serverId];
|
||||
const latestAgent = latestSession?.agents.get(agentId);
|
||||
const remainingQueue = latestSession?.queuedMessages.get(agentId);
|
||||
if (
|
||||
latestAgent?.status === "idle" &&
|
||||
remainingQueue &&
|
||||
remainingQueue.length > 0 &&
|
||||
latestSession?.initializingAgents.get(agentId) !== true
|
||||
) {
|
||||
this.drainQueuedAgentMessage(serverId, agentId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicitly retries or discards startup settings that failed after a provider switch,
|
||||
* then resumes automatic draining if the chat is idle with queued messages.
|
||||
*/
|
||||
async recoverFailedStartupSettings(
|
||||
serverId: string,
|
||||
agentId: string,
|
||||
action: "retry" | "discard",
|
||||
): Promise<boolean> {
|
||||
if (action === "discard") {
|
||||
discardFailedStartupSettings(serverId, agentId);
|
||||
useSessionStore.getState().setQueuedSendError(serverId, agentId, null);
|
||||
this.resumeIdleQueueIfReady(serverId, agentId);
|
||||
return true;
|
||||
}
|
||||
const client = useSessionStore.getState().sessions[serverId]?.client;
|
||||
if (!client) {
|
||||
return false;
|
||||
}
|
||||
const result = await applyFailedStartupSettings({ serverId, agentId, client });
|
||||
if (!result.applied) {
|
||||
useSessionStore.getState().setQueuedSendError(serverId, agentId, result.errorMessage);
|
||||
return false;
|
||||
}
|
||||
useSessionStore.getState().setQueuedSendError(serverId, agentId, null);
|
||||
this.resumeIdleQueueIfReady(serverId, agentId);
|
||||
return true;
|
||||
}
|
||||
|
||||
drainQueuedAgentMessage(serverId: string, agentId: string): void {
|
||||
if (hasRetainedProviderSwitchEditFailure(serverId, agentId)) {
|
||||
if (getFailedStartupSettings(serverId, agentId)) {
|
||||
return;
|
||||
}
|
||||
const drainKey = `${serverId}:${agentId}`;
|
||||
@@ -2997,17 +3042,7 @@ export class HostRuntimeStore {
|
||||
this.queuedAgentDrainInFlight.delete(drainKey);
|
||||
const hadPendingDrain = this.queuedAgentDrainPending.delete(drainKey);
|
||||
if (!drainSent || !hadPendingDrain) return;
|
||||
const latestSession = useSessionStore.getState().sessions[serverId];
|
||||
const latestAgent = latestSession?.agents.get(agentId);
|
||||
const remainingQueue = latestSession?.queuedMessages.get(agentId);
|
||||
if (
|
||||
latestAgent?.status === "idle" &&
|
||||
remainingQueue &&
|
||||
remainingQueue.length > 0 &&
|
||||
latestSession?.initializingAgents.get(agentId) !== true
|
||||
) {
|
||||
this.drainQueuedAgentMessage(serverId, agentId);
|
||||
}
|
||||
this.resumeIdleQueueIfReady(serverId, agentId);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user