| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /*
- AI Chat — chat backend
- Relays a conversation to the configured AI endpoint via llm.request().
- POST parameters:
- messages : JSON array of {role, content}
- options : JSON object { model, system, temperature, max_tokens }
- files : (optional) JSON array of virtual file paths to attach to the
- latest user message (images for vision, text documents inlined)
- Response (JSON):
- { ok:true, content:"...", reasoning:"...", usage:{...}, model:"..." }
- { ok:false, error:"..." }
- "reasoning" carries the model's chain-of-thought / "thinking" output when
- the endpoint exposes it separately (DeepSeek reasoning_content, OpenRouter
- reasoning, Anthropic thinking blocks); it is an empty string otherwise.
- */
- requirelib("llm");
- //Read POST parameters safely (they are injected as VM globals when present)
- var rawMessages = (typeof messages !== "undefined") ? messages : "[]";
- var rawOptions = (typeof options !== "undefined") ? options : "{}";
- var rawFiles = (typeof files !== "undefined") ? files : "[]";
- var msgs = [];
- var opts = {};
- var fileList = [];
- try { msgs = JSON.parse(rawMessages) || []; } catch (e) { msgs = []; }
- try { opts = JSON.parse(rawOptions) || {}; } catch (e) { opts = {}; }
- try { fileList = JSON.parse(rawFiles) || []; } catch (e) { fileList = []; }
- //Build the final message list, prepending the system prompt if provided
- var finalMessages = [];
- if (opts.system && ("" + opts.system).trim() != "") {
- finalMessages.push({ role: "system", content: "" + opts.system });
- }
- for (var i = 0; i < msgs.length; i++) {
- if (msgs[i] && msgs[i].role && typeof msgs[i].content != "undefined") {
- finalMessages.push({ role: msgs[i].role, content: msgs[i].content });
- }
- }
- var handled = false;
- //If files are attached, convert them into content parts and merge them into
- //the latest user message (turning its content into a multimodal array).
- if (fileList.length > 0 && finalMessages.length > 0) {
- try {
- var parts = llm.fileParts(fileList);
- var lastUserIdx = -1;
- for (var j = finalMessages.length - 1; j >= 0; j--) {
- if (finalMessages[j].role == "user") { lastUserIdx = j; break; }
- }
- if (lastUserIdx >= 0) {
- var content = [];
- var existing = finalMessages[lastUserIdx].content;
- if (existing && ("" + existing).trim() != "") {
- content.push({ type: "text", text: "" + existing });
- }
- for (var k = 0; k < parts.length; k++) { content.push(parts[k]); }
- finalMessages[lastUserIdx].content = content;
- }
- } catch (e) {
- sendJSONResp(JSON.stringify({ ok: false, error: "" + e }));
- handled = true;
- }
- }
- if (!handled) {
- if (finalMessages.length == 0) {
- sendJSONResp(JSON.stringify({ ok: false, error: "No messages to send" }));
- } else {
- try {
- var res = llm.request(finalMessages, opts);
- var replyText = "";
- var reasoning = "";
- if (res && res.choices && res.choices.length > 0 && res.choices[0].message) {
- replyText = res.choices[0].message.content;
- reasoning = res.choices[0].message.reasoning_content || "";
- }
- sendJSONResp(JSON.stringify({
- ok: true,
- content: replyText,
- reasoning: reasoning,
- usage: (res && res.usage) ? res.usage : null,
- model: (res && res.model) ? res.model : (opts.model || "")
- }));
- } catch (e) {
- sendJSONResp(JSON.stringify({ ok: false, error: "" + e }));
- }
- }
- }
|