File size: 4,361 Bytes
74411f9 75e0099 74411f9 75e0099 74411f9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | /**
* Bayan Chrome Extension — API Client
*
* Reused from: src/js/api.js (L2-20)
* Adaptation: Relative paths replaced with CONFIG.API_BASE.
*
* Endpoints: analyze, summarize, dialect, quran, autocomplete, health.
*/
/**
* Send text to the unified analysis pipeline.
* Backend: /api/analyze (Spelling → Grammar → Punctuation)
*
* @param {string} text - Arabic text to analyze
* @param {AbortSignal} [signal] - Optional abort signal
* @returns {Promise<Object>} { original, corrected, suggestions[], timing_ms, status }
*/
async function bayanAnalyze(text, signal) {
const response = await fetch(`${CONFIG.API_BASE}/api/analyze`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text }),
signal,
});
if (!response.ok) {
throw new Error(`Analyze API error: ${response.status}`);
}
return await response.json();
}
/**
* Summarize Arabic text.
* Backend: /api/summarize
*
* @param {string} text - Arabic text to summarize
* @param {number} [length=2] - Summary length (1=short, 2=medium, 3=long)
* @param {boolean} [fullText=true] - Summarize full text or first paragraph
* @returns {Promise<Object>} { summary, status, original_length, summary_length }
*/
async function bayanSummarize(text, length = 2, fullText = true) {
const response = await fetch(`${CONFIG.API_BASE}/api/summarize`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text, length, full_text: fullText }),
});
if (!response.ok) {
throw new Error(`Summarize API error: ${response.status}`);
}
return await response.json();
}
/**
* Convert dialectal Arabic to Modern Standard Arabic (MSA).
* Backend: /api/dialect
*
* @param {string} text - Dialectal Arabic text
* @param {AbortSignal} [signal] - Optional abort signal
* @returns {Promise<Object>} { original_text, converted_text, status }
*/
async function bayanDialect(text, signal) {
const response = await fetch(`${CONFIG.API_BASE}/api/dialect`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text }),
signal,
});
if (!response.ok) {
throw new Error(`Dialect API error: ${response.status}`);
}
return await response.json();
}
/**
* Verify / search Quranic text.
* Backend: /api/quran
*
* @param {string} text - Text to verify against the Quran
* @param {string} [language='تدقيق الايات'] - Target operation type
* @param {AbortSignal} [signal] - Optional abort signal
* @returns {Promise<Object>} { matched_segment, full_verse, ... } or { error }
*/
async function bayanQuran(text, language = 'تدقيق الايات', signal, topN = 1) {
const body = { text, language };
if (topN > 1) body.top_n = topN;
const response = await fetch(`${CONFIG.API_BASE}/api/quran`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
signal,
});
// Quran endpoint returns 404 with a JSON {error} body on "no match" —
// treat that as a normal (non-throwing) result so the UI can show it.
if (!response.ok && response.status !== 404) {
throw new Error(`Quran API error: ${response.status}`);
}
return await response.json();
}
/**
* Get autocomplete suggestions for Arabic text.
* Backend: /api/autocomplete
*
* @param {string} context - Text before the cursor
* @param {number} [n=3] - Number of suggestions to return
* @param {AbortSignal} [signal] - Optional abort signal
* @returns {Promise<Object>} { suggestions: string[], status }
*/
async function bayanAutocomplete(context, n = 3, signal) {
const response = await fetch(`${CONFIG.API_BASE}/api/autocomplete`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ context, n }),
signal,
});
if (!response.ok) {
throw new Error(`Autocomplete API error: ${response.status}`);
}
return await response.json();
}
/**
* Check backend health.
* Backend: /api/health
*
* @returns {Promise<Object>} { status, mode, models }
*/
async function bayanHealthCheck() {
const response = await fetch(`${CONFIG.API_BASE}/api/health`, {
method: 'GET',
});
if (!response.ok) {
throw new Error(`Health check error: ${response.status}`);
}
return await response.json();
}
|