2025-12-01 19:41:04 +01:00
|
|
|
import { activeProcessingState } from '$lib/stores/chat.svelte';
|
2025-09-17 19:29:13 +02:00
|
|
|
import { config } from '$lib/stores/settings.svelte';
|
|
|
|
|
|
|
|
|
|
export interface UseProcessingStateReturn {
|
|
|
|
|
readonly processingState: ApiProcessingState | null;
|
|
|
|
|
getProcessingDetails(): string[];
|
|
|
|
|
getProcessingMessage(): string;
|
|
|
|
|
shouldShowDetails(): boolean;
|
2025-12-01 19:41:04 +01:00
|
|
|
startMonitoring(): void;
|
2025-09-17 19:29:13 +02:00
|
|
|
stopMonitoring(): void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* useProcessingState - Reactive processing state hook
|
|
|
|
|
*
|
|
|
|
|
* This hook provides reactive access to the processing state of the server.
|
2025-12-01 19:41:04 +01:00
|
|
|
* It directly reads from chatStore's reactive state and provides
|
2025-09-17 19:29:13 +02:00
|
|
|
* formatted processing details for UI display.
|
|
|
|
|
*
|
|
|
|
|
* **Features:**
|
2025-12-01 19:41:04 +01:00
|
|
|
* - Real-time processing state via direct reactive state binding
|
2025-09-17 19:29:13 +02:00
|
|
|
* - Context and output token tracking
|
|
|
|
|
* - Tokens per second calculation
|
2025-12-01 19:41:04 +01:00
|
|
|
* - Automatic updates when streaming data arrives
|
|
|
|
|
* - Supports multiple concurrent conversations
|
2025-09-17 19:29:13 +02:00
|
|
|
*
|
|
|
|
|
* @returns Hook interface with processing state and control methods
|
|
|
|
|
*/
|
|
|
|
|
export function useProcessingState(): UseProcessingStateReturn {
|
|
|
|
|
let isMonitoring = $state(false);
|
|
|
|
|
let lastKnownState = $state<ApiProcessingState | null>(null);
|
|
|
|
|
|
2025-12-01 19:41:04 +01:00
|
|
|
// Derive processing state reactively from chatStore's direct state
|
|
|
|
|
const processingState = $derived.by(() => {
|
|
|
|
|
if (!isMonitoring) {
|
|
|
|
|
return lastKnownState;
|
2025-09-17 19:29:13 +02:00
|
|
|
}
|
2025-12-01 19:41:04 +01:00
|
|
|
// Read directly from the reactive state export
|
|
|
|
|
return activeProcessingState();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Track last known state for keepStatsVisible functionality
|
|
|
|
|
$effect(() => {
|
|
|
|
|
if (processingState && isMonitoring) {
|
|
|
|
|
lastKnownState = processingState;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function startMonitoring(): void {
|
|
|
|
|
if (isMonitoring) return;
|
|
|
|
|
isMonitoring = true;
|
2025-09-17 19:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stopMonitoring(): void {
|
|
|
|
|
if (!isMonitoring) return;
|
|
|
|
|
isMonitoring = false;
|
|
|
|
|
|
2025-12-01 19:41:04 +01:00
|
|
|
// Only clear last known state if keepStatsVisible is disabled
|
2025-09-17 19:29:13 +02:00
|
|
|
const currentConfig = config();
|
|
|
|
|
if (!currentConfig.keepStatsVisible) {
|
2025-12-01 19:41:04 +01:00
|
|
|
lastKnownState = null;
|
2025-09-17 19:29:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getProcessingMessage(): string {
|
2025-12-01 19:41:04 +01:00
|
|
|
const state = processingState;
|
|
|
|
|
if (!state) {
|
2025-09-17 19:29:13 +02:00
|
|
|
return 'Processing...';
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 19:41:04 +01:00
|
|
|
switch (state.status) {
|
2025-09-17 19:29:13 +02:00
|
|
|
case 'initializing':
|
|
|
|
|
return 'Initializing...';
|
|
|
|
|
case 'preparing':
|
2025-12-01 19:41:04 +01:00
|
|
|
if (state.progressPercent !== undefined) {
|
|
|
|
|
return `Processing (${state.progressPercent}%)`;
|
2025-09-17 19:29:13 +02:00
|
|
|
}
|
|
|
|
|
return 'Preparing response...';
|
|
|
|
|
case 'generating':
|
2025-12-01 19:41:04 +01:00
|
|
|
if (state.tokensDecoded > 0) {
|
|
|
|
|
return `Generating... (${state.tokensDecoded} tokens)`;
|
2025-09-17 19:29:13 +02:00
|
|
|
}
|
|
|
|
|
return 'Generating...';
|
|
|
|
|
default:
|
|
|
|
|
return 'Processing...';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getProcessingDetails(): string[] {
|
|
|
|
|
// Use current processing state or fall back to last known state
|
|
|
|
|
const stateToUse = processingState || lastKnownState;
|
|
|
|
|
if (!stateToUse) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const details: string[] = [];
|
|
|
|
|
|
|
|
|
|
// Always show context info when we have valid data
|
|
|
|
|
if (stateToUse.contextUsed >= 0 && stateToUse.contextTotal > 0) {
|
|
|
|
|
const contextPercent = Math.round((stateToUse.contextUsed / stateToUse.contextTotal) * 100);
|
|
|
|
|
|
|
|
|
|
details.push(
|
|
|
|
|
`Context: ${stateToUse.contextUsed}/${stateToUse.contextTotal} (${contextPercent}%)`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stateToUse.outputTokensUsed > 0) {
|
|
|
|
|
// Handle infinite max_tokens (-1) case
|
|
|
|
|
if (stateToUse.outputTokensMax <= 0) {
|
|
|
|
|
details.push(`Output: ${stateToUse.outputTokensUsed}/∞`);
|
|
|
|
|
} else {
|
|
|
|
|
const outputPercent = Math.round(
|
|
|
|
|
(stateToUse.outputTokensUsed / stateToUse.outputTokensMax) * 100
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
details.push(
|
|
|
|
|
`Output: ${stateToUse.outputTokensUsed}/${stateToUse.outputTokensMax} (${outputPercent}%)`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 19:41:04 +01:00
|
|
|
if (stateToUse.tokensPerSecond && stateToUse.tokensPerSecond > 0) {
|
2025-09-17 19:29:13 +02:00
|
|
|
details.push(`${stateToUse.tokensPerSecond.toFixed(1)} tokens/sec`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stateToUse.speculative) {
|
|
|
|
|
details.push('Speculative decoding enabled');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return details;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shouldShowDetails(): boolean {
|
2025-12-01 19:41:04 +01:00
|
|
|
const state = processingState;
|
|
|
|
|
return state !== null && state.status !== 'idle';
|
2025-09-17 19:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
get processingState() {
|
|
|
|
|
return processingState;
|
|
|
|
|
},
|
|
|
|
|
getProcessingDetails,
|
|
|
|
|
getProcessingMessage,
|
|
|
|
|
shouldShowDetails,
|
|
|
|
|
startMonitoring,
|
|
|
|
|
stopMonitoring
|
|
|
|
|
};
|
|
|
|
|
}
|