// panes/lang.js — language pipelines (LangChain-ish sidecar).
import { registerPane } from '../app.js';
import { jget, jpost, escapeHtml } from '../lib/api.js';
import { fmtJSON, err } from '../lib/ui.js';
const TPL = `
Lang
Run language pipelines.
—
Pipelines
…
`;
async function refresh(host) {
try {
const r = await jget('/api/lang/pipelines');
const items = r.pipelines || [];
host.querySelector('#lang-pipelines').innerHTML = items.map(p =>
`${escapeHtml(p.name)}
${escapeHtml(p.description || '')}
`
).join('') || 'no pipelines
';
const sel = host.querySelector('#lang-pipeline');
if (sel && sel.children.length === 0) {
sel.innerHTML = items.map(p => ``).join('');
}
host.querySelector('#lang-status').textContent = 'ok';
} catch (e) {
host.querySelector('#lang-status').textContent = 'upstream unavailable: ' + e.message;
}
}
registerPane('lang', {
label: 'Lang',
init(host) {
host.innerHTML = TPL;
host.querySelector('#lang-run').addEventListener('click', async () => {
const name = host.querySelector('#lang-pipeline').value;
let input;
try { input = JSON.parse(host.querySelector('#lang-input').value || '{}'); }
catch (e) { return err(`bad JSON: ${e.message}`); }
host.querySelector('#lang-status').textContent = 'running…';
try {
const r = await jpost(`/api/lang/pipelines/${encodeURIComponent(name)}`, { input });
host.querySelector('#lang-result').textContent = fmtJSON(r);
host.querySelector('#lang-status').textContent = `ok · ${r.duration_ms ?? '?'}ms`;
} catch (e) {
host.querySelector('#lang-result').textContent = fmtJSON(e.body ?? { error: e.message });
host.querySelector('#lang-status').textContent = `error ${e.status ?? ''}`;
}
});
host.querySelector('#lang-health').addEventListener('click', async () => {
try { host.querySelector('#lang-result').textContent = fmtJSON(await jget('/api/lang/healthz')); }
catch (e) { host.querySelector('#lang-result').textContent = e.message; }
});
refresh(host);
},
refresh,
});