/* Settings only: never enqueue work or contact the ML service from the browser. */ (() => { const ja = document.documentElement.lang.startsWith('ja'); const t = (en, jp) => ja ? jp : en; const endpoint = '/_/api/v1/ml/settings'; let loaded = false, busy = false, generation = ''; const fields = {}; const el = (tag, text) => { const n = document.createElement(tag); if (text) n.textContent = text; return n; }; const message = code => ({ ml_settings_conflict: t('Settings changed elsewhere. Your inputs are retained; reload to compare before saving.', '設定が別の操作で変更されています。入力は保持しています。再読込して比較してください。'), ml_invalid_settings: t('Check scope UUID, model, paths, dimensions and external consent.', '学習範囲UUID・モデル・Path・次元数・外部送信許可を確認してください。'), domain_admin_required: t('Domain administrator access is required.', 'ドメイン管理者権限が必要です。'), ml_settings_unavailable: t('Settings are unavailable.', '設定を読み書きできません。') }[code] || t('Request failed. Please sign in again or retry.', '操作に失敗しました。ログイン状態を確認してください。')); async function request(options) { const response = await fetch(endpoint, {credentials:'same-origin', cache:'no-store', ...options}); const data = await response.json().catch(() => ({})); if (!response.ok || response.redirected || !Object.hasOwn(data, 'enabled')) throw Error(message(data.error)); return data; } function fill(p) { generation = p.generation; for (const [name, input] of Object.entries(fields)) { if (input.type === 'checkbox') input.checked = !!p[name]; else if (Array.isArray(p[name])) input.value = p[name].join('\n'); else input.value = p[name] || ({provider:'doc2vec', dimensions:128, max_batch_bytes:6000, daily_input_bytes:0}[name] ?? ''); } } async function load() { const root = document.getElementById('ml-settings'); if (!root || loaded || busy) return; busy = true; root.replaceChildren(el('p', t('Loading…', '読込中…'))); let policy; try { policy = await request(); } catch (e) { root.replaceChildren(el('p', e.message)); const retry = el('button', t('Retry', '再試行')); retry.type='button'; retry.onclick=load; root.append(retry); busy=false; return; } root.replaceChildren(el('p', t('Configuration only. Indexing and automatic organization are not connected yet. No documents are sent when saving.', '設定のみ保存します。索引化・自動整理はまだ未接続で、保存しても文書は送信されません。'))); const form=el('form'), table=el('table'), body=el('tbody'); table.className='config-form-table'; table.append(body); form.append(table); const specs=[ ['enabled','checkbox','Enable','有効化'], ['scope_uuid','text','Learning scope UUID','学習範囲UUID'], ['provider','select','Provider','プロバイダー'], ['model','text','Model / model SHA256','モデル名/モデルSHA256'], ['dimensions','number','Dimensions','次元数'], ['prefixes','textarea','Include paths (one per line)','対象Path(1行に1つ)'], ['exclude','textarea','Exclude paths (one per line)','除外Path(1行に1つ)'], ['allow_external','checkbox','Allow external transmission','外部送信を許可'], ['max_batch_bytes','number','Input byte limit per batch','1回の入力byte上限'], ['daily_input_bytes','number','Daily input bytes (UTC; 0 blocks execution)','1日の入力byte上限(UTC・0は実行禁止)'] ]; for (const [name,type,en,jp] of specs) { const row=el('tr'), th=el('th'), td=el('td'), label=el('label',t(en,jp)); const input=el(type==='textarea'?'textarea':type==='select'?'select':'input'); if(type!=='textarea' && type!=='select') input.type=type; input.id='ml-'+name; input.name=name; label.htmlFor=input.id; if(type==='textarea') {input.rows=3;input.maxLength=16000;} if(type==='text') input.maxLength=128; if(type==='number') {input.min=name==='daily_input_bytes'?'0':'1'; input.max=name==='dimensions'?'1536':name==='daily_input_bytes'?'1000000000':'96000';input.step='1';} if(type==='select') for(const v of ['doc2vec','openai']) {const opt=el('option',v);opt.value=v;input.append(opt);} if(type==='checkbox') input.dataset.label=t(en,jp); fields[name]=input; th.append(label);td.append(input);row.append(th,td);body.append(row); } const save=el('button',t('Save','保存'));save.type='submit'; const reload=el('button',t('Reload','再読込'));reload.type='button'; const status=el('p');status.setAttribute('role','status');status.setAttribute('aria-live','polite'); reload.onclick=()=>{if(busy || !confirm(t('Discard current inputs and reload settings?','入力を破棄して設定を再読込しますか?')))return;loaded=false;load();}; form.append(save,reload,status);root.append(form);fill(policy);loaded=true;busy=false; form.addEventListener('submit', async event=>{ event.preventDefault();if(busy)return;busy=true;save.disabled=reload.disabled=true;status.textContent=''; for(const input of Object.values(fields)) input.disabled=true; const p={}; for(const [name,input] of Object.entries(fields)) p[name]=input.type==='checkbox'?input.checked:input.tagName==='TEXTAREA'?input.value.split('\n').map(x=>x.trim()).filter(Boolean):input.type==='number'?Number(input.value):input.value.trim(); // A fresh disabled form may be saved before choosing a model/scope. const next=!p.enabled&&!p.scope_uuid&&!p.model&&!p.prefixes.length&&!p.exclude.length&&!p.allow_external&&!p.daily_input_bytes?{enabled:false}:p; try {fill(await request({method:'PUT', headers:{'Content-Type':'application/json'},body:JSON.stringify({base_generation:generation,policy:next})}));status.textContent='✓';} catch(e) {status.textContent=e.message;} finally {busy=false;save.disabled=reload.disabled=false;for(const input of Object.values(fields)) input.disabled=false;} }); } window.pagesMLSettings={load}; document.addEventListener('DOMContentLoaded',()=>{if(document.getElementById('panel-machine-learning')?.classList.contains('active'))load();}); })();