File size: 12,000 Bytes
1a25b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<template>
  <div class="flex flex-col h-full bg-slate-50/30">
    <!-- Header Controls -->
    <div
      v-if="messages.length > 0"
      class="px-4 py-2 border-b border-slate-100 flex justify-end shrink-0"
    >
      <UButton
        icon="i-lucide-trash-2"
        color="gray"
        variant="ghost"
        size="xs"
        label="Clear Chat"
        @click="chatStore.clearHistory(project.id)"
        class="text-slate-400 hover:text-red-500 transition-colors"
      />
    </div>

    <div
      class="flex-1 p-4 overflow-y-auto custom-scrollbar space-y-4"
      ref="chatContainer"
    >
      <!-- Welcome message -->
      <div
        v-if="messages.length === 0"
        class="h-full flex flex-col items-center justify-center text-center p-6 animate-fade-in-up"
      >
        <div
          class="w-16 h-16 rounded-2xl bg-gradient-to-br from-green-400 to-green-600 flex items-center justify-center mb-6 shadow-lg shadow-green-500/20"
        >
          <UIcon name="i-lucide-bot" class="w-8 h-8 text-white" />
        </div>
        <h3 class="font-bold text-slate-900 mb-2">AI Tutor Ready</h3>
        <p class="text-sm text-slate-500 mb-8 max-w-[240px]">
          Ask me any questions about the video or educational concepts.
        </p>

        <!-- Vertical Suggestion Chips -->
        <div
          v-if="suggestions.length > 0"
          class="flex flex-col gap-2.5 w-full max-w-[300px]"
        >
          <p
            class="text-[10px] uppercase tracking-[0.1em] font-bold text-slate-400 mb-1 text-left px-1"
          >
            Suggested for you
          </p>
          <button
            v-for="suggestion in suggestions"
            :key="suggestion"
            class="group flex items-center gap-3 px-4 py-3 rounded-xl bg-white border border-slate-100 text-left text-sm text-slate-700 hover:border-green-500 hover:bg-green-50/30 transition-all duration-200 shadow-sm hover:shadow-md"
            @click="useSuggestion(suggestion)"
          >
            <UIcon
              name="i-lucide-sparkles"
              class="w-4 h-4 text-green-500 opacity-40 group-hover:opacity-100 transition-opacity shrink-0"
            />
            <span class="whitespace-normal leading-relaxed text-slate-700">{{
              suggestion
            }}</span>
          </button>
        </div>
      </div>

      <!-- Messages -->
      <div
        v-for="(msg, i) in messages"
        :key="i"
        class="flex animate-fade-in-up"
        :class="msg.role === 'user' ? 'justify-end' : 'justify-start'"
      >
        <!-- User Bubble -->
        <div
          v-if="msg.role === 'user'"
          class="max-w-[85%] p-4 rounded-2xl shadow-sm text-sm leading-relaxed border transition-all bg-green-50/50 border-green-100 text-slate-800 rounded-tr-sm"
        >
          <div
            class="prose prose-sm max-w-none text-slate-800 font-medium prose-p:mb-0 last:prose-p:mb-0 prose-code:bg-slate-200/60 prose-code:text-slate-800 prose-code:px-1 prose-code:py-0.5 prose-code:rounded"
            v-html="renderMarkdown(msg.content)"
          ></div>
        </div>

        <!-- AI Message - Clean text, no avatar, no bubble -->
        <div v-else class="w-full transition-all duration-200">
          <div
            class="prose prose-sm max-w-none prose-p:mb-4 last:prose-p:mb-0 prose-p:text-slate-700 prose-p:leading-7 prose-headings:text-slate-900 prose-headings:font-bold prose-headings:mb-3 prose-headings:mt-6 first:prose-headings:mt-0 prose-h2:text-base prose-h2:border-b prose-h2:border-slate-200 prose-h2:pb-2 prose-h3:text-sm prose-h3:text-slate-900 prose-h3:font-medium prose-h4:text-sm prose-h4:text-slate-800 prose-h4:font-medium prose-strong:text-slate-900 prose-strong:font-semibold prose-ul:list-disc prose-ul:pl-5 prose-ul:mb-4 prose-ul:space-y-1.5 prose-li:text-slate-700 prose-ol:list-decimal prose-ol:pl-5 prose-ol:mb-4 prose-ol:space-y-1.5 prose-li:text-slate-700 prose-code:bg-slate-100 prose-code:px-1.5 prose-code:py-0.5 prose-code:rounded prose-code:text-emerald-700 prose-code:font-mono prose-code:text-[0.85em] prose-code:before:content-none prose-code:after:content-none prose-pre:bg-[#0f172a] prose-pre:text-green-400 prose-pre:border prose-pre:border-slate-800 prose-pre:rounded-lg prose-pre:p-4 prose-pre:mt-4 prose-pre:mb-4 prose-pre:overflow-x-auto prose-blockquote:border-l-2 prose-blockquote:border-emerald-300 prose-blockquote:bg-emerald-50/30 prose-blockquote:pl-4 prose-blockquote:pr-4 prose-blockquote:py-3 prose-blockquote:rounded-r-xl prose-blockquote:italic prose-blockquote:text-slate-600 prose-blockquote:my-4 prose-table:w-full prose-table:border-collapse prose-table:my-4 prose-th:bg-slate-50 prose-th:border prose-th:border-slate-200 prose-th:px-3 prose-th:py-2 prose-th:text-left prose-th:text-xs prose-th:font-bold prose-th:text-slate-700 prose-td:border prose-td:border-slate-200 prose-td:px-3 prose-td:py-2 prose-td:text-sm prose-td:text-slate-700 prose-hr:border-slate-200 prose-hr:my-6 prose-a:text-emerald-600 prose-a:underline prose-a:decoration-emerald-600/30 prose-a:underline-offset-2 hover:prose-a:decoration-emerald-600/60 space-y-2"
            v-html="renderMarkdown(msg.content)"
          ></div>
        </div>
      </div>

      <!-- Loading Indicator -->
      <div v-if="isTyping" class="flex justify-start animate-fade-in">
        <div
          class="bg-green-50 border border-green-100 rounded-2xl rounded-tl-sm px-4 py-3 flex items-center gap-1.5 shadow-sm"
        >
          <span
            class="w-1.5 h-1.5 bg-green-400 rounded-full animate-bounce"
          ></span>
          <span
            class="w-1.5 h-1.5 bg-green-400 rounded-full animate-bounce"
            style="animation-delay: 0.15s"
          ></span>
          <span
            class="w-1.5 h-1.5 bg-green-400 rounded-full animate-bounce"
            style="animation-delay: 0.3s"
          ></span>
        </div>
      </div>
    </div>

    <!-- Input Area -->
    <div class="bg-white/80 border-t border-slate-100 shrink-0">
      <div class="p-4">
        <form @submit.prevent="sendMessage" class="relative group">
          <input
            v-model="newMessage"
            type="text"
            @keydown.enter.prevent="sendMessage"
            :placeholder="
              projectStore.loadingModels
                ? 'Loading AI models...'
                : 'Ask a question...'
            "
            class="w-full premium-input rounded-xl pl-4 pr-12 py-3 text-sm outline-none disabled:opacity-50 disabled:cursor-not-allowed"
            :disabled="isTyping || projectStore.loadingModels"
          />
          <button
            type="submit"
            :disabled="
              !newMessage.trim() || isTyping || projectStore.loadingModels
            "
            class="absolute right-2 top-1/2 -translate-y-1/2 w-8 h-8 flex items-center justify-center rounded-lg bg-green-500 text-white hover:bg-green-600 disabled:opacity-50 disabled:bg-slate-300 transition-colors"
            :title="
              projectStore.loadingModels ? 'Models loading' : 'Send message'
            "
          >
            <UIcon
              :name="
                projectStore.loadingModels
                  ? 'i-lucide-loader-2'
                  : 'i-lucide-send'
              "
              class="w-4 h-4"
              :class="
                projectStore.loadingModels
                  ? 'animate-spin'
                  : '-translate-x-[1px] translate-y-[1px]'
              "
            />
          </button>
        </form>
        <div class="mt-2 text-[10px] text-center text-slate-400">
          AlgoVision can make mistakes. Check important information.
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, nextTick, computed } from "vue";
import { marked } from "marked";
import markedKatex from "marked-katex-extension";
import "katex/dist/katex.min.css";
import api from "~/utils/api";
import { useProjectStore } from "~/stores/projects";
import { useChatStore } from "~/stores/chat";

marked.use(
  markedKatex({
    throwOnError: false,
    output: "html",
  }),
);

const props = defineProps({
  project: { type: Object, required: true },
});

const projectStore = useProjectStore();
const chatStore = useChatStore();

const messages = computed(() => chatStore.getHistory(props.project.id));
const suggestions = computed(() => chatStore.getSuggestions(props.project.id));
const isTyping = ref(false);
const newMessage = ref("");
const chatContainer = ref(null);

const fetchSuggestions = async () => {
  try {
    await chatStore.fetchSuggestions(props.project.id, projectStore.apiKeys);
  } catch (err) {
    console.error("Error fetching suggestions:", err);
  }
};

const useSuggestion = (text) => {
  newMessage.value = text;
  sendMessage();
};

const scrollToBottom = async () => {
  await nextTick();
  if (chatContainer.value) {
    chatContainer.value.scrollTop = chatContainer.value.scrollHeight;
  }
};

onMounted(() => {
  scrollToBottom();
  if (!projectStore.loadingModels) {
    fetchSuggestions();
  }
});

watch(
  () => projectStore.loadingModels,
  (loading) => {
    if (!loading && suggestions.value.length === 0) {
      fetchSuggestions();
    }
  },
);

watch(messages, scrollToBottom, { deep: true });
watch(isTyping, scrollToBottom);

const renderMarkdown = (text) => {
  if (!text) return "";
  try {
    return marked.parse(text);
  } catch (e) {
    return text;
  }
};

const sendMessage = async () => {
  if (!newMessage.value.trim() || isTyping.value) return;

  const userText = newMessage.value;
  chatStore.addMessage(props.project.id, { role: "user", content: userText });
  newMessage.value = "";
  isTyping.value = true;

  try {
    const res = await api.chat(props.project.id, {
      message: userText,
      history: messages.value.slice(0, -1),
      model:
        projectStore.planningModel ||
        projectStore.selectedModel ||
        "gemini/gemini-3.1-flash-latest",
      openai_api_key: projectStore.apiKeys.openai,
      anthropic_api_key: projectStore.apiKeys.anthropic,
      gemini_api_key: projectStore.apiKeys.gemini,
    });

    chatStore.addMessage(props.project.id, {
      role: "assistant",
      content: res.response,
    });
  } catch (error) {
    console.error("Chat error:", error);
    chatStore.addMessage(props.project.id, {
      role: "assistant",
      content: `**Error:** Failed to connect to the AI engine. ${error.response?.data?.detail || error.message}`,
    });
  } finally {
    isTyping.value = false;
  }
};
</script>

<style scoped>
.custom-scrollbar::-webkit-scrollbar {
  width: 4px;
}
.custom-scrollbar::-webkit-scrollbar-track {
  background: transparent;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
  background: rgba(203, 213, 225, 0.5);
  border-radius: 2px;
}
.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: rgba(148, 163, 184, 0.8);
}

/* Additional prose polish for AI responses */
:deep(.prose a) {
  color: #059669;
  text-decoration: underline;
  text-decoration-color: rgba(5, 150, 105, 0.3);
  text-underline-offset: 2px;
  transition: all 0.15s ease;
}
:deep(.prose a:hover) {
  color: #047857;
  text-decoration-color: rgba(5, 150, 105, 0.6);
}

/* Inline math/latex */
:deep(.prose .katex) {
  font-size: 0.95em;
}

/* Display math blocks */
:deep(.prose .katex-display) {
  margin: 0.75rem 0;
  padding: 0.75rem;
  overflow-x: auto;
}

/* Smooth text rendering */
:deep(.prose *) {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Code block line numbers area (if any) */
:deep(.prose pre code) {
  font-family:
    ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono",
    monospace;
}

/* Prevent code wrap */
:deep(.prose pre) {
  white-space: pre;
  word-break: normal;
  overflow-wrap: normal;
}

/* Table responsiveness */
:deep(.prose table) {
  display: table;
  width: 100%;
  overflow-x: auto;
}
</style>