2026-09-01 14:29:34 +03:30
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
|
import { computed, reactive, ref, watch } from 'vue';
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
import { api } from '@/lib/api';
|
2026-09-02 21:29:00 +03:30
|
|
|
|
import JalaliDateInput from '@/components/JalaliDateInput.vue';
|
2026-09-07 11:49:54 +03:30
|
|
|
|
import { brand, type NoteAiPayloadDto, type NoteAiSuggestionDto } from '@mento/shared';
|
2026-10-13 18:04:53 +03:30
|
|
|
|
import { rewriteGregorianDates } from '@/lib/jalali';
|
2026-11-03 16:51:51 +03:30
|
|
|
|
import {
|
|
|
|
|
|
Alert as AAlert,
|
|
|
|
|
|
Button as AButton,
|
|
|
|
|
|
Card as ACard,
|
|
|
|
|
|
Checkbox as ACheckbox,
|
|
|
|
|
|
Flex as AFlex,
|
|
|
|
|
|
Input as AInput,
|
|
|
|
|
|
Space as ASpace,
|
|
|
|
|
|
Textarea as ATextarea,
|
|
|
|
|
|
Typography,
|
|
|
|
|
|
} from 'ant-design-vue';
|
|
|
|
|
|
|
|
|
|
|
|
const { Text: ATypographyText, Paragraph: ATypographyParagraph, Title: ATypographyTitle } =
|
|
|
|
|
|
Typography;
|
2026-09-01 14:29:34 +03:30
|
|
|
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
|
|
noteId: string;
|
|
|
|
|
|
hasPerson: boolean;
|
|
|
|
|
|
}>();
|
|
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
|
applied: [];
|
|
|
|
|
|
dismissed: [];
|
|
|
|
|
|
}>();
|
|
|
|
|
|
|
|
|
|
|
|
const loading = ref(false);
|
|
|
|
|
|
const applying = ref(false);
|
|
|
|
|
|
const error = ref('');
|
|
|
|
|
|
const suggestion = ref<NoteAiSuggestionDto | null>(null);
|
|
|
|
|
|
const applyNextAction = ref(true);
|
|
|
|
|
|
const selectedMemory = reactive<Record<number, boolean>>({});
|
|
|
|
|
|
const nextTitle = ref('');
|
|
|
|
|
|
const nextDueAt = ref('');
|
|
|
|
|
|
const memoryEdits = reactive<Record<number, string>>({});
|
|
|
|
|
|
|
|
|
|
|
|
const payload = computed(() => suggestion.value?.payload ?? null);
|
|
|
|
|
|
const isPending = computed(() => suggestion.value?.status === 'pending');
|
|
|
|
|
|
const memoryItems = computed(() => payload.value?.memoryItems ?? []);
|
|
|
|
|
|
|
|
|
|
|
|
watch(
|
|
|
|
|
|
() => props.noteId,
|
|
|
|
|
|
() => {
|
|
|
|
|
|
void load();
|
|
|
|
|
|
},
|
|
|
|
|
|
{ immediate: true },
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
watch(payload, (value) => {
|
|
|
|
|
|
if (!value) return;
|
|
|
|
|
|
applyNextAction.value = Boolean(value.nextAction);
|
|
|
|
|
|
nextTitle.value = value.nextAction?.title ?? '';
|
|
|
|
|
|
nextDueAt.value = value.nextAction?.dueAt
|
|
|
|
|
|
? value.nextAction.dueAt.slice(0, 16)
|
|
|
|
|
|
: '';
|
|
|
|
|
|
value.memoryItems.forEach((item, index) => {
|
|
|
|
|
|
selectedMemory[index] = true;
|
|
|
|
|
|
memoryEdits[index] = item.content;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function load() {
|
|
|
|
|
|
loading.value = true;
|
|
|
|
|
|
error.value = '';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { data } = await api.get<NoteAiSuggestionDto | null>(
|
|
|
|
|
|
`/notes/${props.noteId}/ai-suggestion`,
|
|
|
|
|
|
);
|
|
|
|
|
|
suggestion.value = data;
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
suggestion.value = null;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
loading.value = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function analyze() {
|
|
|
|
|
|
loading.value = true;
|
|
|
|
|
|
error.value = '';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { data } = await api.post<NoteAiSuggestionDto>(
|
|
|
|
|
|
`/notes/${props.noteId}/analyze`,
|
|
|
|
|
|
);
|
|
|
|
|
|
suggestion.value = data;
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
if (axios.isAxiosError(e)) {
|
|
|
|
|
|
const msg = (e.response?.data as { message?: string | string[] })?.message;
|
|
|
|
|
|
error.value = Array.isArray(msg) ? msg.join('، ') : (msg ?? 'تحلیل ناموفق بود');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
error.value = 'تحلیل ناموفق بود';
|
|
|
|
|
|
}
|
|
|
|
|
|
await load();
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
loading.value = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function apply() {
|
|
|
|
|
|
if (!payload.value || !isPending.value) return;
|
|
|
|
|
|
applying.value = true;
|
|
|
|
|
|
error.value = '';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const memoryIndices = memoryItems.value
|
|
|
|
|
|
.map((_, index) => index)
|
|
|
|
|
|
.filter((index) => selectedMemory[index]);
|
|
|
|
|
|
|
|
|
|
|
|
await api.post(`/notes/${props.noteId}/ai-suggestion/apply`, {
|
|
|
|
|
|
applyNextAction: applyNextAction.value && Boolean(nextTitle.value.trim()),
|
|
|
|
|
|
memoryIndices: props.hasPerson ? memoryIndices : [],
|
|
|
|
|
|
nextAction: applyNextAction.value
|
|
|
|
|
|
? {
|
|
|
|
|
|
title: nextTitle.value.trim(),
|
|
|
|
|
|
description: payload.value.nextAction?.description,
|
|
|
|
|
|
dueAt: nextDueAt.value
|
|
|
|
|
|
? new Date(nextDueAt.value).toISOString()
|
|
|
|
|
|
: null,
|
|
|
|
|
|
}
|
|
|
|
|
|
: undefined,
|
|
|
|
|
|
memoryItems: memoryIndices.map((index) => ({
|
|
|
|
|
|
index,
|
|
|
|
|
|
content: memoryEdits[index]?.trim() || memoryItems.value[index]?.content,
|
|
|
|
|
|
})),
|
|
|
|
|
|
});
|
|
|
|
|
|
await load();
|
|
|
|
|
|
emit('applied');
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
if (axios.isAxiosError(e)) {
|
|
|
|
|
|
const msg = (e.response?.data as { message?: string | string[] })?.message;
|
|
|
|
|
|
error.value = Array.isArray(msg) ? msg.join('، ') : (msg ?? 'اعمال پیشنهاد ناموفق بود');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
error.value = 'اعمال پیشنهاد ناموفق بود';
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
applying.value = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function dismiss() {
|
|
|
|
|
|
applying.value = true;
|
|
|
|
|
|
error.value = '';
|
|
|
|
|
|
try {
|
|
|
|
|
|
await api.post(`/notes/${props.noteId}/ai-suggestion/dismiss`);
|
|
|
|
|
|
await load();
|
|
|
|
|
|
emit('dismissed');
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
error.value = 'رد پیشنهاد ناموفق بود';
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
applying.value = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function formatPayloadSummary(value: NoteAiPayloadDto | null) {
|
|
|
|
|
|
if (!value?.summary) return '';
|
2026-10-13 18:04:53 +03:30
|
|
|
|
return rewriteGregorianDates(value.summary);
|
2026-09-01 14:29:34 +03:30
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-card :bordered="true" :style="{ marginTop: '12px' }">
|
|
|
|
|
|
<a-flex justify="space-between" align="center" wrap="wrap" :gap="8">
|
|
|
|
|
|
<a-typography-text strong>پیشنهاد {{ brand.aiName }}</a-typography-text>
|
|
|
|
|
|
<a-button
|
2026-09-01 14:29:34 +03:30
|
|
|
|
v-if="!isPending"
|
2026-11-03 16:51:51 +03:30
|
|
|
|
type="link"
|
2026-09-01 14:29:34 +03:30
|
|
|
|
:disabled="loading"
|
|
|
|
|
|
@click="analyze"
|
|
|
|
|
|
>
|
|
|
|
|
|
{{ loading ? '…' : 'تحلیل مجدد' }}
|
2026-11-03 16:51:51 +03:30
|
|
|
|
</a-button>
|
|
|
|
|
|
<a-button
|
2026-09-01 14:29:34 +03:30
|
|
|
|
v-else-if="!suggestion"
|
2026-11-03 16:51:51 +03:30
|
|
|
|
type="link"
|
2026-09-01 14:29:34 +03:30
|
|
|
|
:disabled="loading"
|
|
|
|
|
|
@click="analyze"
|
|
|
|
|
|
>
|
|
|
|
|
|
{{ loading ? 'در حال تحلیل…' : 'تحلیل با AI' }}
|
2026-11-03 16:51:51 +03:30
|
|
|
|
</a-button>
|
|
|
|
|
|
</a-flex>
|
2026-09-01 14:29:34 +03:30
|
|
|
|
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-alert v-if="error" type="error" show-icon :message="error" :style="{ marginTop: '8px' }" />
|
|
|
|
|
|
<a-typography-text v-else-if="loading && !suggestion" type="secondary">
|
|
|
|
|
|
در حال تحلیل یادداشت…
|
|
|
|
|
|
</a-typography-text>
|
|
|
|
|
|
<a-flex v-else-if="suggestion?.status === 'failed'" vertical :gap="4">
|
|
|
|
|
|
<a-alert
|
|
|
|
|
|
type="error"
|
|
|
|
|
|
show-icon
|
|
|
|
|
|
:message="suggestion.errorMessage || 'تحلیل ناموفق بود'"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<a-button type="link" @click="analyze">تلاش دوباره</a-button>
|
|
|
|
|
|
</a-flex>
|
|
|
|
|
|
<a-button
|
|
|
|
|
|
v-else-if="!suggestion"
|
|
|
|
|
|
type="link"
|
|
|
|
|
|
:disabled="loading"
|
|
|
|
|
|
@click="analyze"
|
|
|
|
|
|
>
|
|
|
|
|
|
تحلیل این یادداشت با AI
|
|
|
|
|
|
</a-button>
|
2026-09-01 14:29:34 +03:30
|
|
|
|
|
|
|
|
|
|
<template v-else-if="isPending && payload">
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-typography-paragraph
|
|
|
|
|
|
v-if="formatPayloadSummary(payload)"
|
|
|
|
|
|
type="secondary"
|
|
|
|
|
|
:style="{ marginTop: '8px' }"
|
|
|
|
|
|
>
|
|
|
|
|
|
{{ formatPayloadSummary(payload) }}
|
|
|
|
|
|
</a-typography-paragraph>
|
2026-09-01 14:29:34 +03:30
|
|
|
|
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-flex v-if="payload.nextAction" vertical :gap="8" :style="{ marginTop: '8px' }">
|
|
|
|
|
|
<a-checkbox v-model:checked="applyNextAction">کار بعدی</a-checkbox>
|
|
|
|
|
|
<a-input v-model:value="nextTitle" placeholder="عنوان کار بعدی" style="width: 100%" />
|
|
|
|
|
|
<JalaliDateInput v-model="nextDueAt" mode="datetime" style="width: 100%" />
|
|
|
|
|
|
</a-flex>
|
2026-09-01 14:29:34 +03:30
|
|
|
|
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-flex v-if="memoryItems.length" vertical :gap="8" :style="{ marginTop: '12px' }">
|
|
|
|
|
|
<a-typography-title :level="5" :style="{ margin: 0 }">
|
|
|
|
|
|
حافظه مشتری (با تأیید شما ذخیره میشود)
|
|
|
|
|
|
</a-typography-title>
|
|
|
|
|
|
<a-alert
|
|
|
|
|
|
v-if="!hasPerson"
|
|
|
|
|
|
type="warning"
|
|
|
|
|
|
show-icon
|
|
|
|
|
|
message="برای ذخیره حافظه، یادداشت باید به یک شخص متصل باشد."
|
|
|
|
|
|
/>
|
|
|
|
|
|
<a-flex
|
2026-09-01 14:29:34 +03:30
|
|
|
|
v-for="(_, index) in memoryItems"
|
|
|
|
|
|
:key="index"
|
2026-11-03 16:51:51 +03:30
|
|
|
|
:gap="8"
|
|
|
|
|
|
align="flex-start"
|
2026-09-01 14:29:34 +03:30
|
|
|
|
>
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-checkbox
|
|
|
|
|
|
v-model:checked="selectedMemory[index]"
|
2026-09-01 14:29:34 +03:30
|
|
|
|
:disabled="!hasPerson"
|
|
|
|
|
|
/>
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-textarea v-model:value="memoryEdits[index]" :rows="2" style="width: 100%" />
|
|
|
|
|
|
</a-flex>
|
|
|
|
|
|
</a-flex>
|
2026-09-01 14:29:34 +03:30
|
|
|
|
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-space :size="8" wrap :style="{ marginTop: '12px' }">
|
|
|
|
|
|
<a-button type="primary" :disabled="applying" :loading="applying" @click="apply">
|
|
|
|
|
|
اعمال انتخابشده
|
|
|
|
|
|
</a-button>
|
|
|
|
|
|
<a-button :disabled="applying" @click="dismiss">رد کردن</a-button>
|
|
|
|
|
|
</a-space>
|
2026-09-01 14:29:34 +03:30
|
|
|
|
</template>
|
|
|
|
|
|
|
2026-11-03 16:51:51 +03:30
|
|
|
|
<a-typography-text
|
|
|
|
|
|
v-else-if="suggestion?.status === 'applied'"
|
|
|
|
|
|
type="success"
|
|
|
|
|
|
:style="{ display: 'block', marginTop: '8px' }"
|
|
|
|
|
|
>
|
|
|
|
|
|
پیشنهاد اعمال شد.
|
|
|
|
|
|
</a-typography-text>
|
|
|
|
|
|
<a-typography-text
|
|
|
|
|
|
v-else-if="suggestion?.status === 'dismissed'"
|
|
|
|
|
|
type="secondary"
|
|
|
|
|
|
:style="{ display: 'block', marginTop: '8px' }"
|
|
|
|
|
|
>
|
|
|
|
|
|
پیشنهاد رد شد.
|
|
|
|
|
|
</a-typography-text>
|
|
|
|
|
|
</a-card>
|
2026-09-01 14:29:34 +03:30
|
|
|
|
</template>
|