swarm-ui: let a closed stream reconnect and a failed caps read retry
When the server answers an event stream with an error status the browser gives up and the badge stayed on disconnected with nothing to press. The terminal, its state header and the messages page now offer a reconnect, and the one rule for the state after an error moved into a tested helper. The resource caps panel gets a retry beside its error, and its loading state announces itself.
This commit is contained in:
parent
9b1fe85706
commit
31b7452e56
9 changed files with 118 additions and 34 deletions
|
|
@ -17,7 +17,12 @@
|
|||
// `status_text`/`running`).
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
|
||||
export type ConnectionState = "connecting" | "open" | "reconnecting" | "closed";
|
||||
import {
|
||||
connectionAfterError,
|
||||
type ConnectionState,
|
||||
} from "../../ui/stream/connection.js";
|
||||
|
||||
export type { ConnectionState };
|
||||
|
||||
export interface TokenUsage {
|
||||
input_tokens?: number;
|
||||
|
|
@ -53,6 +58,8 @@ export interface AgentStateHeader {
|
|||
export interface UseSwarmAgentStateStreamResult {
|
||||
header: AgentStateHeader | null;
|
||||
connection: ConnectionState;
|
||||
/** Opens the stream again, for one the browser gave up on. */
|
||||
reconnect: () => void;
|
||||
}
|
||||
|
||||
export function useSwarmAgentStateStream(
|
||||
|
|
@ -60,6 +67,8 @@ export function useSwarmAgentStateStream(
|
|||
): UseSwarmAgentStateStreamResult {
|
||||
const [header, setHeader] = useState<AgentStateHeader | null>(null);
|
||||
const [connection, setConnection] = useState<ConnectionState>("connecting");
|
||||
// Bumped by `reconnect`, which reruns the effect below.
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
setHeader(null);
|
||||
|
|
@ -78,13 +87,9 @@ export function useSwarmAgentStateStream(
|
|||
// badge cluster over — the next valid header replaces it.
|
||||
}
|
||||
};
|
||||
es.onerror = () => {
|
||||
setConnection(
|
||||
es.readyState === 0 /* CONNECTING */ ? "reconnecting" : "closed",
|
||||
);
|
||||
};
|
||||
es.onerror = () => setConnection(connectionAfterError(es.readyState));
|
||||
return () => es.close();
|
||||
}, [streamUrl]);
|
||||
}, [streamUrl, attempt]);
|
||||
|
||||
return { header, connection };
|
||||
return { header, connection, reconnect: () => setAttempt((n) => n + 1) };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue