Files
office_translator/frontend/src/lib/webllm.ts
sepehr 05c5dfcbbb
Some checks failed
Deploy to Homelab / Deploy Wordly to 192.168.1.151 (push) Has been cancelled
Deploy to Homelab / Deploy Monitoring (if configured) (push) Has been cancelled
fix: add frontend/src/lib/ to git (was ignored by /lib/ pattern)
The root .gitignore had `lib/` which matched frontend/src/lib/,
causing Docker build to fail with "Module not found: Can't resolve
'@/lib/utils'" and '@/lib/i18n'.

Changed to `/lib/` so it only ignores the Python lib at repo root.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-10 15:17:22 +02:00

47 lines
1.0 KiB
TypeScript

import { useState, useCallback } from "react";
interface WebLLMState {
isLoaded: boolean;
loading: boolean;
error: string | null;
}
export function useWebLLM() {
const [state, setState] = useState<WebLLMState>({
isLoaded: false,
loading: false,
error: null,
});
const isWebGPUSupported = useCallback(() => {
if (typeof navigator === "undefined") return false;
return "gpu" in navigator;
}, []);
const translate = useCallback(
async (
_text: string,
_targetLang: string,
_systemPrompt?: string,
_glossary?: string,
): Promise<string> => {
setState((s) => ({ ...s, loading: true, error: null }));
try {
throw new Error("WebLLM is not available in this environment");
} catch (err) {
const message =
err instanceof Error ? err.message : "WebLLM translation failed";
setState((s) => ({ ...s, loading: false, error: message }));
throw err;
}
},
[],
);
return {
...state,
isWebGPUSupported,
translate,
};
}