File size: 6,371 Bytes
04f192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!--
SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.

SPDX-License-Identifier: Apache-2.0
-->

<script lang="ts" setup>
import { IconCirclePlus2, IconLoader2, IconPlayerStopFilled, IconSend, IconWand } from "@tabler/icons-vue";
import { useVModel } from "@vueuse/core";
import { useConfirm } from "primevue/useconfirm";
import { useTemplateRef, watch } from "vue";
import { useConfig, useSystemState } from "../store";

const props = defineProps<{
  modelValue: string;
  sendable: boolean;
  loading: boolean;
  optimizing: boolean;
}>();

const emit = defineEmits<{
  (event: "update:modelValue", value: string): void;
  (event: "send", value: string): void;
  (event: "reset"): void;
  (event: "optimizePrompt", value: string): void;
  (event: "stop"): void;
}>();

const textareaRef = useTemplateRef("textarea");

const modelValue = useVModel(props, "modelValue", emit);

const confirm = useConfirm();

const config = useConfig();
const state = useSystemState();

watch(
  () => props.loading,
  (newVal) => {
    if (!newVal) {
      confirm.close();
    }
  },
);

defineExpose({
  reset,
});

function handleKeyDown(event: KeyboardEvent) {
  if (event.key === "Enter" && !event.shiftKey) {
    event.preventDefault();
    send();
  }
}

function optimizePrompt() {
  if (!props.sendable || !modelValue.value) return;

  emit("optimizePrompt", modelValue.value);
}

function send() {
  if (!config.data.userAgreement || !props.sendable || !modelValue.value) return;

  emit("send", modelValue.value);
}

function stop(event: Event) {
  confirm.require({
    target: event.currentTarget as HTMLElement,
    message: "Are you sure you want to stop the conversation? This action cannot be undone.",
    rejectProps: {
      label: "Cancel",
      severity: "secondary",
      outlined: true,
    },
    acceptProps: {
      label: "Stop",
      severity: "danger",
    },
    accept() {
      emit("stop");
    },
  });
}

function adjustTextareaHeight(event: Event) {
  const textarea = event.target as HTMLTextAreaElement;
  textarea.style.height = "auto";

  const minHeight = parseInt(getComputedStyle(textarea).lineHeight) || 30;

  const newHeight = Math.max(textarea.scrollHeight, minHeight);
  textarea.style.height = `${newHeight}px`;
}

function reset() {
  modelValue.value = ""; // Clear input

  // Reset textarea height after clearing
  const textarea = textareaRef.value as HTMLTextAreaElement;
  if (textarea) {
    textarea.style.height = "auto";
    const minHeight = parseInt(getComputedStyle(textarea).lineHeight) || 30;
    textarea.style.height = `${minHeight}px`;
  }
}
</script>

<template>
  <div class="w-full max-w-4xl mx-auto px-4 py-2">
    <!-- Read-only banner until the User Agreement is accepted -->
    <div
      v-if="!config.data.userAgreement"
      class="flex items-center justify-between gap-3 rounded-lg border border-amber-400 bg-amber-50 px-4 py-2 text-amber-700 dark:border-amber-600 dark:bg-amber-950/40 dark:text-amber-300"
    >
      <p class="text-sm">Accept the <strong>User Agreement</strong> before sending messages.</p>
      <Button label="Open Settings" size="small" severity="secondary" @click="state.showSettings = true" />
    </div>

    <template v-else>
      <template v-if="props.sendable || props.loading || props.optimizing">
        <div class="flex gap-2 mb-1">
          <div
            :class="[
              'flex-1 flex rounded-lg px-2 py-1 border transition-colors  border-gray-500',
              props.sendable ? 'hover:border-violet-500 focus-within:border-violet-500' : '',
            ]"
          >
            <textarea
              ref="textarea"
              v-model="modelValue"
              :class="[
                'flex-1 px-2 py-1 focus:outline-none resize-none',
                props.sendable ? 'opacity-100' : 'opacity-50 cursor-not-allowed',
              ]"
              placeholder="Ask the agent to do anything related to ImageJ or image processing..."
              rows="1"
              :disabled="!props.sendable"
              @keydown="handleKeyDown"
              @input="adjustTextareaHeight"
            />

            <div class="flex items-center gap-2">
              <!-- Show loading while optimizing -->
              <IconLoader2 v-if="props.optimizing" size="20" class="animate-spin text-violet-500" />
              <!-- Show wand button in normal state -->
              <component
                v-else
                :is="IconWand"
                :class="[
                  'text-slate-500 transition-colors',
                  props.sendable ? 'hover:text-violet-500 cursor-pointer' : 'cursor-not-allowed',
                ]"
                @click="optimizePrompt"
              />
            </div>
          </div>

          <!-- Send button (shown when not loading) -->
          <div
            v-if="!props.loading"
            :class="[
              'px-4 py-2 rounded-lg flex items-center text-white transition-colors',
              props.sendable
                ? 'cursor-pointer bg-violet-500 hover:bg-violet-600 dark:hover:bg-violet-400'
                : 'cursor-not-allowed bg-violet-300 dark:bg-violet-700',
            ]"
            @click="send"
          >
            <IconSend size="28" />
          </div>

          <!-- Loading state with abort button -->
          <div v-else class="flex gap-2">
            <!-- Abort button -->
            <div
              class="relative px-4 py-2 rounded-lg flex items-center text-white bg-orange-400 hover:bg-orange-500 dark:hover:bg-orange-300 transition cursor-pointer"
              title="Stop conversation"
              @click="stop($event)"
            >
              <IconLoader2 size="28" class="animate-spin" />

              <IconPlayerStopFilled size="12" class="absolute top-1/2 left-1/2 translate-[-50%]" />
            </div>
          </div>
        </div>
      </template>

      <div v-else>
        <div
          :class="[
            'px-4 py-2 border rounded-lg flex justify-center items-center cursor-pointer transition-colors',
            'border-violet-500 text-violet-500 hover:text-white hover:bg-violet-500',
          ]"
          @click="emit('reset')"
        >
          <IconCirclePlus2 size="24" />

          <p class="mx-4">New Thread</p>
        </div>
      </div>
    </template>
  </div>
</template>