ChatCompletion.svelte 2.78 KB
Newer Older
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
1
<script lang="ts">
2
3
4
	import { onMount, getContext } from 'svelte';

	const i18n = getContext('i18n');
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

	export let messages = [];

	onMount(() => {
		messages.forEach((message, idx) => {
			let textareaElement = document.getElementById(`${message.role}-${idx}-textarea`);
			textareaElement.style.height = '';
			textareaElement.style.height = textareaElement.scrollHeight + 'px';
		});
	});
</script>

<div class="py-3 space-y-3">
	{#each messages as message, idx}
		<div class="flex gap-2 group">
			<div class="flex items-start pt-1">
				<button
					class="px-2 py-1 text-sm font-semibold uppercase min-w-[6rem] text-left dark:group-hover:bg-gray-800 rounded-lg transition"
					on:click={() => {
						message.role = message.role === 'user' ? 'assistant' : 'user';
25
					}}>{$i18n.t(message.role)}</button
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
26
27
28
29
30
31
32
				>
			</div>

			<div class="flex-1">
				<textarea
					id="{message.role}-{idx}-textarea"
					class="w-full bg-transparent outline-none rounded-lg p-2 text-sm resize-none overflow-hidden"
33
34
35
					placeholder={$i18n.t(
						`Enter ${message.role === 'user' ? 'a user' : 'an assistant'} message here`
					)}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
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
					rows="1"
					on:input={(e) => {
						e.target.style.height = '';
						e.target.style.height = e.target.scrollHeight + 'px';
					}}
					on:focus={(e) => {
						e.target.style.height = '';
						e.target.style.height = e.target.scrollHeight + 'px';

						// e.target.style.height = Math.min(e.target.scrollHeight, 200) + 'px';
					}}
					bind:value={message.content}
				/>
			</div>

			<div class=" pt-1">
				<button
					class=" group-hover:text-gray-500 dark:text-gray-900 dark:hover:text-gray-300 transition"
					on:click={() => {
						messages = messages.filter((message, messageIdx) => messageIdx !== idx);
					}}
				>
					<svg
						xmlns="http://www.w3.org/2000/svg"
						fill="none"
						viewBox="0 0 24 24"
						stroke-width="2"
						stroke="currentColor"
						class="w-5 h-5"
					>
						<path
							stroke-linecap="round"
							stroke-linejoin="round"
							d="M15 12H9m12 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
						/>
					</svg>
				</button>
			</div>
		</div>

		<hr class=" dark:border-gray-800" />
	{/each}

	<button
		class="flex items-center gap-2 px-2 py-1"
		on:click={() => {
			console.log(messages.at(-1));
			messages.push({
				role: (messages.at(-1)?.role ?? 'assistant') === 'user' ? 'assistant' : 'user',
				content: ''
			});
			messages = messages;
		}}
	>
		<div>
			<svg
				xmlns="http://www.w3.org/2000/svg"
				fill="none"
				viewBox="0 0 24 24"
				stroke-width="1.5"
				stroke="currentColor"
				class="w-5 h-5"
			>
				<path
					stroke-linecap="round"
					stroke-linejoin="round"
					d="M12 9v6m3-3H9m12 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"
				/>
			</svg>
		</div>

107
		<div class=" text-sm font-medium">{$i18n.t('Add message')}</div>
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
108
109
	</button>
</div>