-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhtmx-ai.js
More file actions
16 lines (16 loc) · 737 Bytes
/
htmx-ai.js
File metadata and controls
16 lines (16 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
htmx.defineExtension('ai', {
onEvent: function (name, evt) {
if (name === 'htmx:beforeProcessNode') {
evt.target.querySelectorAll('[hx-ai]').forEach(el => {
const nextEndpoint = el.closest(['hx-ai-endpoint'])?.getAttribute('hx-ai-endpoint');
el.setAttribute('hx-post', nextEndpoint ?? 'http://htmx-ai.test/');
if(el.getAttribute('hx-ai').startsWith('js:')){
el.setAttribute('hx-vals', 'js:{prompt: ' + el.getAttribute('hx-ai').replace('js:', '') + '}');
}
else {
el.setAttribute('hx-vals', '{"prompt": "' + el.getAttribute('hx-ai') + '"}');
}
});
}
}
})