File size: 11,387 Bytes
2671e04
adb34c2
 
 
 
 
2671e04
adb34c2
 
 
2671e04
adb34c2
 
 
 
 
 
 
 
 
 
 
 
 
2671e04
adb34c2
 
740e55f
2671e04
 
adb34c2
2671e04
 
adb34c2
 
 
 
 
 
 
 
 
 
 
 
 
 
740e55f
adb34c2
 
 
 
 
 
 
 
 
740e55f
adb34c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2671e04
adb34c2
 
 
2671e04
 
adb34c2
 
 
 
 
 
 
 
 
740e55f
 
 
 
 
 
 
adb34c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2671e04
 
adb34c2
 
 
 
 
 
 
740e55f
adb34c2
740e55f
adb34c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2671e04
adb34c2
 
2671e04
 
adb34c2
 
2671e04
 
 
 
 
 
 
 
 
adb34c2
2671e04
 
 
adb34c2
 
2671e04
 
adb34c2
2671e04
adb34c2
2671e04
 
 
adb34c2
 
2671e04
740e55f
2671e04
adb34c2
 
2671e04
 
adb34c2
2671e04
adb34c2
2671e04
 
 
adb34c2
2671e04
 
 
740e55f
 
 
 
adb34c2
 
 
 
 
 
 
 
 
2671e04
 
adb34c2
 
 
 
2671e04
adb34c2
2671e04
adb34c2
2671e04
 
 
 
adb34c2
2671e04
 
 
adb34c2
 
 
 
2671e04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
adb34c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
740e55f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import { registerServiceWorker } from "/sw-register.js";
import { buildAttachmentParts, createAttachments, createLinkAttachment, releaseAttachment } from "/chatclient/media.js";
import { createAttachmentPreviewController } from "/chatclient/preview.js";
import { loadDraft, saveDraft } from "/chatclient/draft.js";
import { renderAttachments, renderResponse, setStatus, showError } from "/chatclient/render.js";
import { loadSettings, saveSettings } from "/chatclient/settings.js";

const state = { attachments: [] };

const editor = document.querySelector("#editor");
const endpointInput = document.querySelector("#endpoint");
const modelInput = document.querySelector("#model");
const systemPromptInput = document.querySelector("#system-prompt");
const audioOutputInput = document.querySelector("#audio-output");
const voiceInput = document.querySelector("#voice");
const attachmentInput = document.querySelector("#attachment-input");
const attachmentPicker = document.querySelector("#attachment-picker");
const attachmentLinkType = document.querySelector("#attachment-link-type");
const attachmentLinkUrl = document.querySelector("#attachment-link-url");
const attachmentsCard = document.querySelector(".attachments-card");
const attachmentList = document.querySelector("#attachment-list");
const attachmentSummary = document.querySelector("#attachment-summary");
const settingsPanel = document.querySelector("#settings-panel");
const settingsToggle = document.querySelector("#settings-toggle");
const statusLine = document.querySelector("#status-line");
const sendButton = document.querySelector("#send-button");
const responseOutput = document.querySelector("#response-output");
const rawCopyButton = document.querySelector("#raw-copy-button");
const rawJson = document.querySelector("#raw-json");
const errorToast = document.querySelector("#error-toast");
const previewController = createAttachmentPreviewController();

registerServiceWorker();
loadSettings({ endpointInput, modelInput, systemPromptInput, audioOutputInput, voiceInput });
previewController.close();
restoreDraft();
renderAttachments(attachmentsCard, attachmentList, attachmentSummary, state.attachments, removeAttachment, previewController.open);
bindEvents();

function bindEvents() {
  settingsToggle.addEventListener("click", () => toggleSettings());
  audioOutputInput.addEventListener("change", syncAudioFields);
  attachmentInput.addEventListener("change", handleAttachmentSelect);
  document.querySelector("#attachment-trigger").addEventListener("click", () => toggleAttachmentPicker());
  document.querySelector("#attachment-link-add").addEventListener("click", handleLinkAdd);
  attachmentLinkUrl.addEventListener("keydown", handleLinkKeyDown);
  document.querySelector("#send-button").addEventListener("click", handleSend);
  rawCopyButton.addEventListener("click", handleRawCopy);
  document.querySelector(".editor-toolbar").addEventListener("click", handleFormatClick);
  editor.addEventListener("keydown", handleEditorKeyDown);
  editor.addEventListener("input", persistDraft);

  for (const button of document.querySelectorAll(".tab-button")) {
    button.addEventListener("click", () => setActiveTab(button.dataset.tab));
  }

  for (const button of document.querySelectorAll(".picker-mode-button")) {
    button.addEventListener("click", () => handleAttachmentModeSelect(button.dataset.mode));
  }

  for (const element of [endpointInput, modelInput, systemPromptInput, attachmentLinkType]) {
    element.addEventListener("input", handleInputPersistence);
  }

  for (const element of [audioOutputInput, voiceInput]) {
    element.addEventListener("change", handleInputPersistence);
  }

  attachmentLinkUrl.addEventListener("input", persistDraft);
  syncAudioFields();
}

function restoreDraft() {
  const draft = loadDraft();
  editor.innerHTML = draft.editorHtml;
  attachmentLinkType.value = draft.attachmentLinkType;
  attachmentLinkUrl.value = draft.attachmentLinkUrl;
  state.attachments = draft.attachments;
  setAttachmentMode(draft.attachmentMode);
}

function syncAudioFields() {
  voiceInput.disabled = !audioOutputInput.checked;
}

function toggleSettings(forceOpen) {
  const shouldShow = typeof forceOpen === "boolean" ? forceOpen : settingsPanel.hidden;
  settingsPanel.hidden = !shouldShow;
  settingsToggle.classList.toggle("is-active", shouldShow);
}

function toggleAttachmentPicker(forceOpen) {
  const shouldShow = typeof forceOpen === "boolean" ? forceOpen : attachmentPicker.hidden;
  attachmentPicker.hidden = !shouldShow;
  document.querySelector("#attachment-trigger").classList.toggle("is-active", shouldShow);
}

function setAttachmentMode(mode) {
  for (const button of document.querySelectorAll(".picker-mode-button")) {
    button.classList.toggle("is-active", button.dataset.mode === mode);
  }

  for (const panel of document.querySelectorAll(".picker-panel")) {
    const isActive = panel.id === `attachment-picker-${mode}`;
    panel.hidden = !isActive;
    panel.classList.toggle("is-active", isActive);
  }

  persistDraft();
}

function handleAttachmentModeSelect(mode) {
  setAttachmentMode(mode);
  if (mode === "upload") {
    attachmentInput.click();
  }
}

function setActiveTab(tabName) {
  for (const button of document.querySelectorAll(".tab-button")) {
    const isActive = button.dataset.tab === tabName;
    button.classList.toggle("is-active", isActive);
    button.setAttribute("aria-selected", String(isActive));
  }

  for (const panel of document.querySelectorAll(".tab-panel")) {
    const isActive = panel.dataset.panel === tabName;
    panel.hidden = !isActive;
    panel.classList.toggle("is-active", isActive);
  }
}

function handleFormatClick(event) {
  const button = event.target.closest(".format-button");
  if (!button) {
    return;
  }

  editor.focus();
  document.execCommand(button.dataset.command);
  persistDraft();
}

function handleEditorKeyDown(event) {
  if ((event.ctrlKey || event.metaKey) && event.key === "Enter") {
    event.preventDefault();
    handleSend();
  }
}

async function handleAttachmentSelect(event) {
  try {
    const nextItems = await createAttachments(event.target.files);
    if (nextItems.length === 0) {
      return;
    }

    addAttachments(nextItems);
    toggleAttachmentPicker(false);
  } catch (error) {
    showError(errorToast, error.message);
  } finally {
    attachmentInput.value = "";
  }
}

function handleLinkAdd() {
  try {
    addAttachments([createLinkAttachment(attachmentLinkType.value, attachmentLinkUrl.value)]);
    attachmentLinkUrl.value = "";
    toggleAttachmentPicker(false);
    persistDraft();
  } catch (error) {
    showError(errorToast, error.message);
  }
}

function handleLinkKeyDown(event) {
  if (event.key === "Enter") {
    event.preventDefault();
    handleLinkAdd();
  }
}

function removeAttachment(id) {
  const index = state.attachments.findIndex((attachment) => attachment.id === id);
  if (index === -1) {
    return;
  }

  releaseAttachment(state.attachments[index]);
  state.attachments.splice(index, 1);
  renderAttachments(attachmentsCard, attachmentList, attachmentSummary, state.attachments, removeAttachment, previewController.open);
  setStatus(statusLine, state.attachments.length === 0 ? "Ready." : `${state.attachments.length} attachment(s) ready.`);
  persistDraft();
}

function addAttachments(nextItems) {
  state.attachments.push(...nextItems);
  renderAttachments(attachmentsCard, attachmentList, attachmentSummary, state.attachments, removeAttachment, previewController.open);
  setStatus(statusLine, `${state.attachments.length} attachment(s) ready.`);
  persistDraft();
}

async function handleSend() {
  if (sendButton.disabled) {
    return;
  }

  sendButton.disabled = true;
  setStatus(statusLine, "Sending request...");
  showPendingOutput();
  setActiveTab("output");

  try {
    const payload = await buildPayload();
    const response = await fetch(endpointInput.value.trim(), {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify(payload)
    });

    const data = await readResponseBody(response);
    rawJson.textContent = JSON.stringify(data, null, 2);

    if (!response.ok) {
      setActiveTab("raw");
      throw new Error(data?.error?.message ?? `HTTP ${response.status}`);
    }

    renderResponse(responseOutput, payload, data);
    setActiveTab("output");
    setStatus(statusLine, "Response received.", true);
  } catch (error) {
    renderRequestError(error, "request");
  } finally {
    sendButton.disabled = false;
  }
}

async function buildPayload() {
  const text = readEditorText();
  const content = [];
  const hasAudioAttachment = state.attachments.some((attachment) => attachment.kind === "audio");

  if (text) {
    content.push({ type: "text", text });
  }

  content.push(...await buildAttachmentParts(state.attachments));
  if (content.length === 0) {
    throw new Error("Add text or at least one image/audio file before sending.");
  }

  const payload = {
    model: modelInput.value.trim(),
    messages: []
  };

  if (hasAudioAttachment) {
    payload.modalities = ["text", "audio"];
  }

  if (!payload.model) {
    throw new Error("Enter a model name in settings.");
  }

  if (systemPromptInput.value.trim()) {
    payload.messages.push({
      role: "system",
      content: systemPromptInput.value.trim()
    });
  }

  payload.messages.push({
    role: "user",
    content
  });

  if (audioOutputInput.checked) {
    payload.audio = {
      voice: voiceInput.value,
      format: "mp3"
    };
  }

  persistSettings();
  return payload;
}

function readEditorText() {
  return editor.innerText.replaceAll("\u00A0", " ").trim();
}

async function readResponseBody(response) {
  const text = await response.text();
  if (!text) {
    return {};
  }

  try {
    return JSON.parse(text);
  } catch (_error) {
    return {
      error: {
        message: text
      }
    };
  }
}

function handleInputPersistence() {
  persistSettings();
  persistDraft();
}

function persistSettings() {
  saveSettings({
    endpointInput,
    modelInput,
    systemPromptInput,
    audioOutputInput,
    voiceInput
  });
}

function persistDraft() {
  saveDraft({
    editor,
    attachmentMode: document.querySelector(".picker-mode-button.is-active")?.dataset.mode || "upload",
    attachmentLinkType: attachmentLinkType.value,
    attachmentLinkUrl: attachmentLinkUrl.value,
    attachments: state.attachments
  });
}

function renderRequestError(error, stage) {
  rawJson.textContent = JSON.stringify({
    error: {
      stage,
      name: error.name,
      message: error.message
    }
  }, null, 2);
  setActiveTab("raw");
  setStatus(statusLine, "Request failed.");
  showError(errorToast, error.message);
}

function showPendingOutput() {
  responseOutput.innerHTML = '<p class="empty-state">Sending request...</p>';
}

async function handleRawCopy() {
  try {
    await navigator.clipboard.writeText(rawJson.textContent);
    setStatus(statusLine, "Raw response copied.", true);
    rawCopyButton.textContent = "Copied";
  } catch (_error) {
    showError(errorToast, "Failed to copy raw response.");
    rawCopyButton.textContent = "Copy Failed";
  }

  window.clearTimeout(handleRawCopy.timer);
  handleRawCopy.timer = window.setTimeout(() => {
    rawCopyButton.textContent = "Copy Raw";
  }, 1600);
}

handleRawCopy.timer = 0;