chat.agi 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. AI Chat — chat backend
  3. Relays a conversation to the configured AI endpoint via llm.request().
  4. POST parameters:
  5. messages : JSON array of {role, content}
  6. options : JSON object { model, system, temperature, max_tokens }
  7. files : (optional) JSON array of virtual file paths to attach to the
  8. latest user message (images for vision, text documents inlined)
  9. Response (JSON):
  10. { ok:true, content:"...", reasoning:"...", usage:{...}, model:"..." }
  11. { ok:false, error:"..." }
  12. "reasoning" carries the model's chain-of-thought / "thinking" output when
  13. the endpoint exposes it separately (DeepSeek reasoning_content, OpenRouter
  14. reasoning, Anthropic thinking blocks); it is an empty string otherwise.
  15. */
  16. requirelib("llm");
  17. //Read POST parameters safely (they are injected as VM globals when present)
  18. var rawMessages = (typeof messages !== "undefined") ? messages : "[]";
  19. var rawOptions = (typeof options !== "undefined") ? options : "{}";
  20. var rawFiles = (typeof files !== "undefined") ? files : "[]";
  21. var msgs = [];
  22. var opts = {};
  23. var fileList = [];
  24. try { msgs = JSON.parse(rawMessages) || []; } catch (e) { msgs = []; }
  25. try { opts = JSON.parse(rawOptions) || {}; } catch (e) { opts = {}; }
  26. try { fileList = JSON.parse(rawFiles) || []; } catch (e) { fileList = []; }
  27. //Build the final message list, prepending the system prompt if provided
  28. var finalMessages = [];
  29. if (opts.system && ("" + opts.system).trim() != "") {
  30. finalMessages.push({ role: "system", content: "" + opts.system });
  31. }
  32. for (var i = 0; i < msgs.length; i++) {
  33. if (msgs[i] && msgs[i].role && typeof msgs[i].content != "undefined") {
  34. finalMessages.push({ role: msgs[i].role, content: msgs[i].content });
  35. }
  36. }
  37. var handled = false;
  38. //If files are attached, convert them into content parts and merge them into
  39. //the latest user message (turning its content into a multimodal array).
  40. if (fileList.length > 0 && finalMessages.length > 0) {
  41. try {
  42. var parts = llm.fileParts(fileList);
  43. var lastUserIdx = -1;
  44. for (var j = finalMessages.length - 1; j >= 0; j--) {
  45. if (finalMessages[j].role == "user") { lastUserIdx = j; break; }
  46. }
  47. if (lastUserIdx >= 0) {
  48. var content = [];
  49. var existing = finalMessages[lastUserIdx].content;
  50. if (existing && ("" + existing).trim() != "") {
  51. content.push({ type: "text", text: "" + existing });
  52. }
  53. for (var k = 0; k < parts.length; k++) { content.push(parts[k]); }
  54. finalMessages[lastUserIdx].content = content;
  55. }
  56. } catch (e) {
  57. sendJSONResp(JSON.stringify({ ok: false, error: "" + e }));
  58. handled = true;
  59. }
  60. }
  61. if (!handled) {
  62. if (finalMessages.length == 0) {
  63. sendJSONResp(JSON.stringify({ ok: false, error: "No messages to send" }));
  64. } else {
  65. try {
  66. var res = llm.request(finalMessages, opts);
  67. var replyText = "";
  68. var reasoning = "";
  69. if (res && res.choices && res.choices.length > 0 && res.choices[0].message) {
  70. replyText = res.choices[0].message.content;
  71. reasoning = res.choices[0].message.reasoning_content || "";
  72. }
  73. sendJSONResp(JSON.stringify({
  74. ok: true,
  75. content: replyText,
  76. reasoning: reasoning,
  77. usage: (res && res.usage) ? res.usage : null,
  78. model: (res && res.model) ? res.model : (opts.model || "")
  79. }));
  80. } catch (e) {
  81. sendJSONResp(JSON.stringify({ ok: false, error: "" + e }));
  82. }
  83. }
  84. }