File size: 4,417 Bytes
4670a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<title>ChatDocs</title>

<script>
  const auth = new URLSearchParams(window.location.search).get('auth');
  const ws = new WebSocket(`ws://${location.host}/ws`);

  ws.addEventListener('message', (event) => {
    data = JSON.parse(event.data);
    onReceive(data);
  });

  const send = (req) => {
    if (auth) {
      req['auth'] = auth;
    }
    req = JSON.stringify(req);
    ws.send(req);
  };

  const el = (tag) => document.createElement(tag);

  const onReceive = (res) => {
    const { id } = res;
    const answer = document.getElementById('answer-' + id);
    const sources = document.getElementById('sources-' + id);
    if (res.chunk) {
      answer.innerText += res.chunk;
    } else {
      answer.innerText = res.result;
      for (const { source, content } of res.sources) {
        const summary = el('summary');
        summary.innerText = source;

        const details = el('details');
        details.innerText = content;
        details.appendChild(summary);

        sources.appendChild(details);
      }
      form.reset();
      form.elements.submit.disabled = false;
      form.elements.query.disabled = false;
    }
    document.getElementById('end').scrollIntoView();
  };

  function onSubmit(event) {
    event.preventDefault();
    event.target.elements.submit.disabled = true;
    const query = new FormData(event.target).get('query');
    event.target.elements.query.disabled = true;
    event.target.elements.query.value = 'Processing your query. Please wait...';
    const id = 'mid-' + performance.now();
    const messages = document.getElementById('messages');

    const me = el('div');
    me.classList.add('message', 'message-me');
    messages.appendChild(me);

    const q = el('div');
    q.innerText = query;
    me.appendChild(q);

    const ai = el('div');
    ai.classList.add('message', 'message-ai');
    messages.appendChild(ai);

    const answer = el('div');
    answer.classList.add('answer');
    answer.setAttribute('id', 'answer-' + id);
    ai.appendChild(answer);

    const sources = el('div');
    sources.classList.add('sources');
    sources.setAttribute('id', 'sources-' + id);
    ai.appendChild(sources);

    document.getElementById('end').scrollIntoView();

    send({ id, query });
  }
</script>

<style>
  *,
  ::before,
  ::after {
    box-sizing: border-box;
  }

  html {
    line-height: 1.5;
  }

  body {
    margin: 0;
    font-family: system-ui, sans-serif;
  }

  button,
  input,
  optgroup,
  select,
  textarea {
    font-family: inherit;
    font-size: 100%;
    line-height: 1.5;
    margin: 0;
  }

  button,
  select {
    text-transform: none;
  }

  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    -webkit-clip-path: inset(50%);
    clip-path: inset(50%);
    border: 0;
  }

  body {
    background-color: #343541;
    color: #ececf1;
  }

  main {
    padding-bottom: 100px;
  }

  .message {
    padding: 1.5rem 0;
    line-height: 1.75;
    border-bottom: 1px solid rgba(32, 33, 35, 0.5);
  }

  .message br {
    line-height: 1;
  }

  .message > div {
    max-width: 48rem;
    margin-left: auto;
    margin-right: auto;
  }

  .message-ai {
    background-color: #444654;
    color: #d1d5db;
  }

  .answer {
    margin-bottom: 1.25em;
  }

  .sources details {
    margin-bottom: 0.625em;
  }

  .sources summary {
    cursor: pointer;
    font-weight: 500;
    color: #fff;
  }

  footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100px;
    display: flex;
    align-items: center;
    background-image: linear-gradient(
      180deg,
      rgba(53, 55, 64, 0),
      #353740 58.85%
    );
  }

  footer form {
    flex-grow: 1;
    max-width: 48rem;
    margin: 0 auto;
  }

  footer input {
    width: 100%;
    padding: 1rem;
    background-color: #202123;
    color: #fff;
    border-radius: 0.75rem;
    border: 0;
    outline: 0;
  }
</style>

<main id="messages"></main>
<div id="end"></div>
<footer>
  <form id="form">
    <input
      type="text"
      name="query"
      required
      placeholder="Type your query and press Enter."
      autofocus
    />
    <button type="submit" name="submit" class="sr-only">Send</button>
  </form>
</footer>

<script>
  const form = document.getElementById('form');
  form.addEventListener('submit', onSubmit);
</script>