File size: 6,767 Bytes
8e438ab
 
 
 
 
 
691dc7c
8e438ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b8ca
 
8e438ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b8ca
 
 
 
 
 
 
 
8e438ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
691dc7c
8e438ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
691dc7c
8e438ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
(() => {
  "use strict";
  function node(tag, className = "", text = "") {
    const element = document.createElement(tag);
    if (className) element.className = className;
    element.textContent = text;
    return window.FccFormControls.configure(element);
  }
  function button(label, className, action) {
    const element = node("button", className, label);
    element.type = "button";
    element.addEventListener("click", action);
    return element;
  }
  function libraryHeader(title, description, create) {
    const header = node("header", "session-library-header"),
      copy = node("div");
    copy.append(node("h2", "", title), node("p", "", description));
    header.append(copy, create);
    return header;
  }
  function search(label, value, onChange) {
    const input = node("input", "session-search");
    input.type = "search";
    input.placeholder = label;
    input.value = value;
    input.setAttribute("aria-label", label);
    let timer;
    input.addEventListener("input", () => {
      clearTimeout(timer);
      timer = setTimeout(() => {
        if (input.isConnected) onChange(input.value);
      }, 200);
    });
    return input;
  }
  function card(title, preview, meta, action) {
    const element = button("", "session-card", action);
    element.append(
      node("strong", "", title),
      node("p", "", preview),
      node("span", "", meta),
    );
    return element;
  }
  function shell(header, notice, transcript, composer) {
    const element = node("div", "session-shell");
    element.append(header, notice, transcript, composer);
    return element;
  }
  function header(
    backLabel,
    back,
    titleValue,
    titleLabel,
    onTitle,
    actions,
    controls,
  ) {
    const element = node("header", "session-header"),
      row = node("div", "session-header-row");
    const title = node("input", "session-title");
    title.value = titleValue;
    title.maxLength = 200;
    title.setAttribute("aria-label", titleLabel);
    title.addEventListener("blur", () => {
      if (title.isConnected) onTitle(title);
    });
    title.addEventListener("keydown", (event) => {
      if (event.key === "Enter" && !event.isComposing) title.blur();
    });
    row.append(
      button(backLabel, "session-back-button", back),
      title,
      ...actions,
    );
    element.append(row, controls);
    return element;
  }
  function modelControl(id, value, values, registry, onSelect) {
    const group = node("div", "session-control session-model-control"),
      label = node("label", "", "Model");
    label.htmlFor = id;
    const input = node("input", "session-model-input");
    input.id = id;
    input.type = "text";
    input.autocomplete = "off";
    input.value = value;
    input.setAttribute("aria-label", "Selected model");
    let committed = value,
      optionsSignature = JSON.stringify(values());
    const combobox = new window.FccModelCombobox(input, {
      listboxId: `${id}-options`,
      label: "model",
      values,
      registry,
      emptyMessage: () =>
        values().length ? "No matching models." : "No models available.",
      onSelect: (model) => {
        committed = model;
        onSelect(model);
      },
      onClose: () => {
        input.value = committed;
      },
    });
    group.append(label, combobox.element);
    return {
      group,
      input,
      combobox,
      update(model, disabled) {
        committed = model;
        if (!combobox.isOpen) input.value = model;
        input.disabled = disabled;
        combobox.toggle.disabled = disabled;
        if (disabled) combobox.close();
        const signature = JSON.stringify(values());
        if (combobox.isOpen && signature !== optionsSignature) {
          const active = combobox.visibleOptions[combobox.activeIndex]?.dataset.value;
          combobox.render(combobox.query);
          const index = combobox.visibleOptions.findIndex((option) => option.dataset.value === active);
          if (index >= 0) combobox.setActive(index, false);
        }
        optionsSignature = signature;
      },
    };
  }
  function selectControl(id, title, options, value, onChange) {
    const group = node("label", "session-control"),
      select = node("select");
    select.id = id;
    const update = (choices, selected) => {
      const signature = JSON.stringify(choices);
      if (select.dataset.options !== signature) {
        select.replaceChildren(
          ...choices.map(([value, label]) => new Option(label, value)),
        );
        select.dataset.options = signature;
      }
      select.value = selected;
    };
    update(options, value);
    select.addEventListener("change", () => onChange(select.value));
    group.append(node("span", "", title), select);
    return { group, select, update };
  }
  function composer(id, draft, placeholder, onInput, onSend, onStop) {
    const element = node("div", "session-composer"),
      textarea = node("textarea");
    textarea.id = `${id}Composer`;
    textarea.rows = 2;
    textarea.value = draft;
    textarea.placeholder = placeholder;
    textarea.setAttribute("aria-label", "Message");
    const actions = node("div", "session-composer-actions");
    const send = button("Send", "primary-button", onSend),
      stop = button("Stop", "danger-button", onStop);
    send.id = `${id}Send`;
    stop.id = `${id}Stop`;
    stop.hidden = true;
    textarea.addEventListener("input", () => {
      resizeComposer(textarea);
      onInput(textarea.value);
    });
    textarea.addEventListener("keydown", (event) => {
      if (event.key === "Enter" && !event.shiftKey && !event.isComposing) {
        event.preventDefault();
        if (!send.disabled && !send.hidden) onSend();
      }
    });
    actions.append(send, stop);
    element.append(textarea, actions);
    return element;
  }
  function message(role, label) {
    const element = node("article", `session-message ${role}-message`);
    element.append(node("div", "session-message-label", label));
    return element;
  }
  function thinking() {
    const element = node("details", "session-thinking");
    element.append(node("summary", "", "Thinking"));
    return element;
  }
  function resizeComposer(textarea) {
    textarea.style.height = "auto";
    textarea.style.height = `${textarea.scrollHeight}px`;
    textarea.style.overflowY =
      textarea.scrollHeight > textarea.clientHeight ? "auto" : "hidden";
  }
  function nearBottom(scroller) {
    return (
      scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < 80
    );
  }
  window.SessionUI = {
    libraryHeader,
    search,
    card,
    shell,
    header,
    modelControl,
    selectControl,
    composer,
    message,
    thinking,
    resizeComposer,
    nearBottom,
  };
})();