Files
xSaherelmWorkspace/Documents/Docs/Developer/xAiApi-14050807-Overview-9.html
T
saherelm 6b1672536f - Update submodule pointers: xAiApi, Modules/xAiModels;
- Replace stale generated contexts: remove
  Documents/GeneratedContext/xDashboard-xAi{Api,Models,Service}-20261031_*.txt
  and add the 20261102_1545xx set;
- Add Documents/Codes/XAiApiConfiguration.cs,
  Documents/Codes/XAiApiConfigurationExtensions.cs,
  Documents/Codes/XAIServiceBase.cs and
  Documents/Codes/IXAiEmbeddingServiceControllerBase.cs as backups;
- Add Documents/Docs/X121KIEA/X121XKIEA_Analytics-140412.html/.pdf and
  X121XKIEA_Analytics.pdf;
- Add Documents/Docs/Developer/Fix N08 Issue.html and
  xAiApi-14050807-Overview-6/7/7.1/7.2/8/9.html;
- Add Documents/Docs/ChangeLogs/1405-08-11-v01.html and this file.
2026-11-02 16:38:13 +03:30

1103 lines
50 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>گزارش تغییرات | قابلیت‌های RAG و پردازش اسناد در xAiApi</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800&display=swap');
:root {
--primary: #1a365d;
--primary-light: #2c5282;
--accent: #c9a961;
--accent-dark: #a8893f;
--bg: #faf9f6;
--card-bg: #ffffff;
--text: #1a202c;
--text-muted: #4a5568;
--border: #e2e8f0;
--success: #38a169;
--danger: #e53e3e;
--warning: #dd6b20;
--shadow: 0 4px 6px rgba(0,0,0,0.05), 0 10px 30px rgba(26,54,93,0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Vazirmatn', Tahoma, sans-serif;
background: var(--bg);
color: var(--text);
line-height: 1.9;
font-size: 15px;
padding: 30px 15px;
}
.document-container {
max-width: 1000px;
margin: 0 auto;
background: var(--card-bg);
box-shadow: var(--shadow);
border-radius: 12px;
overflow: hidden;
}
.document-header {
background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
color: white;
padding: 50px 40px 40px;
position: relative;
border-bottom: 4px solid var(--accent);
}
.document-header::before {
content: '';
position: absolute;
top: 0; right: 0;
width: 200px; height: 200px;
background: radial-gradient(circle, rgba(201,169,97,0.15) 0%, transparent 70%);
border-radius: 50%;
transform: translate(50%, -50%);
}
.header-top {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
position: relative;
z-index: 1;
}
.company-badge { display: flex; align-items: center; gap: 12px; }
.company-logo {
width: 60px; height: 60px;
background: var(--accent);
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.doc-meta { text-align: left; font-size: 12px; opacity: 0.9; }
.doc-meta div { margin-bottom: 3px; }
.document-title { text-align: center; position: relative; z-index: 1; padding: 20px 0; }
.document-title .label {
display: inline-block;
background: rgba(201,169,97,0.2);
color: var(--accent);
padding: 4px 16px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
margin-bottom: 15px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-title h1 {
font-size: 28px;
font-weight: 800;
margin-bottom: 10px;
letter-spacing: -0.5px;
}
.document-title h2 { font-size: 16px; font-weight: 400; opacity: 0.9; }
.document-body { padding: 40px; }
.intro-text {
background: linear-gradient(to left, #f7fafc, #edf2f7);
border-right: 4px solid var(--accent);
padding: 20px 25px;
border-radius: 8px;
margin-bottom: 35px;
font-size: 14px;
color: var(--text-muted);
}
.section { margin-bottom: 40px; page-break-inside: avoid; }
.section-header {
display: flex;
align-items: center;
gap: 15px;
padding-bottom: 12px;
border-bottom: 2px solid var(--border);
margin-bottom: 20px;
}
.section-number {
width: 50px; height: 50px;
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: 20px;
flex-shrink: 0;
box-shadow: 0 4px 10px rgba(26,54,93,0.2);
}
.section-title { flex: 1; }
.section-title h3 { font-size: 19px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.section-title .subtitle { font-size: 12px; color: var(--text-muted); }
.section-content { padding-right: 10px; }
.section-content p { margin-bottom: 12px; text-align: justify; text-indent: 20px; }
.section-content p.no-indent { text-indent: 0; }
.highlight-box, .info-box, .success-box, .warning-box {
padding: 18px 22px;
border-radius: 8px;
margin: 20px 0;
font-size: 14px;
}
.highlight-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #f6e05e;
border-right: 4px solid var(--accent);
}
.highlight-box strong { color: var(--accent-dark); }
.info-box {
background: linear-gradient(to left, #ebf8ff, #e6f6ff);
border: 1px solid #90cdf4;
border-right: 4px solid var(--primary-light);
}
.success-box {
background: linear-gradient(to left, #f0fff4, #e6fffa);
border: 1px solid #9ae6b4;
border-right: 4px solid var(--success);
}
.warning-box {
background: linear-gradient(to left, #fffaf0, #fef5e7);
border: 1px solid #fbd38d;
border-right: 4px solid var(--warning);
}
.styled-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.styled-table thead {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
}
.styled-table th { padding: 14px 16px; text-align: right; font-weight: 600; font-size: 13px; }
.styled-table td { padding: 13px 16px; border-bottom: 1px solid var(--border); font-size: 13px; }
.styled-table tbody tr:last-child td { border-bottom: none; }
.styled-table tbody tr:nth-child(even) { background: #f8fafc; }
.styled-table .highlight-row { background: #fffaf0 !important; font-weight: 600; }
.file-tree {
background: #f8fafc;
border: 1px solid var(--border);
border-radius: 8px;
padding: 20px 25px;
margin: 15px 0;
font-family: 'Consolas', 'Monaco', monospace;
font-size: 12.5px;
direction: ltr;
text-align: left;
line-height: 1.9;
color: var(--text-muted);
}
.file-tree .folder { color: var(--primary); font-weight: 600; }
.file-tree .new { color: var(--success); font-weight: 600; }
.file-tree .modify { color: var(--warning); font-weight: 600; }
.endpoint-badge {
display: inline-block;
background: #edf2f7;
border: 1px solid var(--border);
border-radius: 6px;
padding: 2px 8px;
font-family: monospace;
font-size: 11px;
color: var(--primary);
direction: ltr;
margin: 2px;
}
.method-get { background: #ebf8ff; color: #2b6cb0; border-color: #90cdf4; }
.method-post { background: #f0fff4; color: #2f855a; border-color: #9ae6b4; }
.method-delete { background: #fff5f5; color: #c53030; border-color: #feb2b2; }
.conclusion-box {
background: linear-gradient(135deg, var(--primary), var(--primary-light));
color: white;
padding: 30px;
border-radius: 12px;
margin: 30px 0;
text-align: center;
box-shadow: 0 8px 20px rgba(26,54,93,0.3);
}
.conclusion-box h3 { font-size: 22px; font-weight: 800; margin-bottom: 15px; color: var(--accent); }
.conclusion-box p { font-size: 14px; line-height: 1.8; text-indent: 0 !important; margin-bottom: 15px; }
.conclusion-box .highlight {
background: rgba(201,169,97,0.2);
padding: 15px 20px;
border-radius: 8px;
margin-top: 20px;
border: 1px solid rgba(201,169,97,0.4);
}
.document-footer {
background: var(--primary);
color: white;
padding: 25px 40px;
text-align: center;
font-size: 12px;
margin-top: 40px;
}
.document-footer .footer-brand { font-weight: 700; color: var(--accent); margin-bottom: 5px; font-size: 14px; }
.footer-divider { width: 60px; height: 2px; background: var(--accent); margin: 10px auto; }
.page-info {
text-align: center;
padding: 15px;
font-size: 11px;
color: var(--text-muted);
border-top: 1px solid var(--border);
margin-top: 30px;
}
.checklist { list-style: none; padding: 0; margin: 15px 0; }
.checklist li {
padding: 10px 15px;
background: #f8fafc;
border-right: 3px solid var(--primary-light);
margin-bottom: 8px;
border-radius: 6px;
font-size: 13.5px;
}
.checklist li::before {
content: '✓';
color: var(--success);
font-weight: 700;
margin-left: 10px;
display: inline-block;
}
@media print {
body { background: white; padding: 0; }
.document-container { box-shadow: none; border-radius: 0; }
.section { page-break-inside: avoid; }
}
@media (max-width: 768px) {
.document-body { padding: 25px 20px; }
.document-header { padding: 30px 20px; }
.header-top { flex-direction: column; gap: 15px; }
.doc-meta { text-align: center; }
}
</style>
</head>
<body>
<div class="document-container">
<header class="document-header">
<div class="header-top">
<div class="company-badge">
<div class="company-logo"></div>
</div>
<div class="doc-meta">
<div>شماره مستند: CHG-1405-003</div>
<div>نسخه: ۱.۰</div>
<div>تاریخ تهیه: آبان ۱۴۰۵</div>
<div>طبقه‌بندی: فنی-داخلی</div>
</div>
</div>
<div class="document-title">
<span class="label">CHANGE REPORT</span>
<h1>گزارش تغییرات پیاده‌سازی RAG و پردازش اسناد</h1>
<h2>ماژول‌های xAiApi، xAiModels و xAiService — قابلیت‌های Chat With Documents و Knowledge</h2>
</div>
</header>
<div class="document-body">
<div class="intro-text">
<strong>خلاصه مدیریتی،</strong><br>
این مستند، گزارش تغییرات اعمال‌شده در پروژه <strong>xSaherelmWorkspace</strong> (ماژول‌های xAiApi، xAiModels و xAiService) را ارائه می‌دهد. تمرکز تغییرات بر افزودن قابلیت‌های <strong>RAG (Retrieval-Augmented Generation)</strong> در دو سطح <strong>In-Memory (Chat With Documents)</strong> و <strong>Persistent (Knowledge)</strong>، به‌همراه <strong>پردازش چندفرمتی اسناد</strong>، <strong>Text Chunking</strong>، <strong>Embedding</strong> و <strong>Vector Store</strong> است. این تغییرات، زیرساخت لازم برای پاسخ‌گویی هوشمند بر پایه اسناد سازمانی را فراهم می‌کند.
</div>
<!-- ============================================================= -->
<!-- SECTION 1: Overview -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۱</div>
<div class="section-title">
<h3>نمای کلی تغییرات</h3>
<div class="subtitle">Changes Overview</div>
</div>
</div>
<div class="section-content">
<p>تغییرات اعمال‌شده در سه محور اصلی دسته‌بندی می‌شوند:</p>
<table class="styled-table">
<thead>
<tr>
<th>محور</th>
<th>توضیح</th>
<th>فایل‌های کلیدی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Chat With Documents</strong></td>
<td>RAG در حافظه (In-Memory) با مدیریت Session و TTL — برای گفتگو با فایل‌های آپلودی</td>
<td><code>ChatWithDocumentsController.cs</code>، <code>XInMemoryVectorStore.cs</code></td>
</tr>
<tr>
<td><strong>Knowledge (Persistent RAG)</strong></td>
<td>RAG پایدار روی جدول <code>XAiVector</code> با Scope چهارسطحی (Conversation/Project/User/Global)</td>
<td><code>KnowledgeController.cs</code>، <code>XAiRAGService.cs</code></td>
</tr>
<tr>
<td><strong>Document Processing</strong></td>
<td>استخراج محتوا از ۱۳+ فرمت فایل + Text Chunking + Embedding</td>
<td><code>XFileContentExtractor.cs</code>، <code>XTextChunker.cs</code>، <code>XAiEmbeddingService.cs</code></td>
</tr>
</tbody>
</table>
<div class="highlight-box">
<strong>نکته کلیدی:</strong> این تغییرات، زیرساخت لازم برای تبدیل xAiApi از یک API ساده LLM به یک <strong>پلتفرم هوشمند سازمانی</strong> را فراهم می‌کند — جایی که کاربر می‌تواند با اسناد سازمانی خود «گفتگو» کند و پاسخ‌های دقیق با ارجاع به منابع دریافت نماید.
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 2: New Files Structure -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۲</div>
<div class="section-title">
<h3>ساختار فایل‌های جدید و تغییر یافته</h3>
<div class="subtitle">New & Modified Files</div>
</div>
</div>
<div class="section-content">
<h4 style="color: var(--primary); font-size: 15px; margin-top: 15px;">xAiApi — Controllers</h4>
<div class="file-tree">
<span class="folder">xAiApi/Controllers/</span>
├── <span class="new">ChatWithDocumentsController.cs</span> [NEW] — In-Memory RAG
├── <span class="new">KnowledgeController.cs</span> [NEW] — Persistent RAG
├── <span class="modify">XAiServiceControllerBase.cs</span> [MODIFY] — افزودن AskWithFiles و ExtractContent
└── <span class="modify">Startup.cs</span> [MODIFY] — ثبت سرویس‌های جدید
</div>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">xAiApi — Interfaces & Providers</h4>
<div class="file-tree">
<span class="folder">xAiApi/Interfaces/</span>
├── <span class="new">IXAiRAGService.cs</span> [NEW]
├── <span class="new">IXAiEmbeddingService.cs</span> [NEW]
├── <span class="new">IXInMemoryVectorStore.cs</span> [NEW]
├── <span class="new">IXTextChunker.cs</span> [NEW]
└── <span class="new">Extractors/IXFileContentExtractor.cs</span> [NEW]
<span class="folder">xAiApi/Providers/</span>
├── <span class="new">XAiRAGService.cs</span> [NEW] — Persistent RAG
├── <span class="new">XAiEmbeddingService.cs</span> [NEW]
├── <span class="new">XInMemoryVectorStore.cs</span> [NEW] — Session-based
├── <span class="new">XInMemoryVectorStoreCleanupService.cs</span> [NEW]
├── <span class="new">XTextChunker.cs</span> [NEW]
└── <span class="folder">Extractors/</span> [NEW] — ۱۳ اکسترکتور
</div>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">xAiModels — مدل‌ها و ثابت‌ها</h4>
<div class="file-tree">
<span class="folder">Modules/xAiModels/</span>
├── <span class="new">Constants/XAiKnowledgeScope.cs</span> [NEW]
├── <span class="modify">Constants/XAiModelsConstants.cs</span> [MODIFY]
├── <span class="modify">Models/Dtos/XAiVectorDto.cs</span> [MODIFY] — افزودن Type
├── <span class="modify">Models/Dtos/XAiVectorSearchResult.cs</span> [MODIFY]
├── <span class="modify">Models/Entities/XAiVector.cs</span> [MODIFY] — افزودن Type
├── <span class="modify">Interfaces/IXAiDataProvider.cs</span> [MODIFY] — متدهای Vector
└── <span class="modify">Providers/XAiDataProvider.cs</span> [MODIFY] — پیاده‌سازی Vector Search
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 3: Chat With Documents -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۳</div>
<div class="section-title">
<h3>قابلیت Chat With Documents (In-Memory RAG)</h3>
<div class="subtitle">Session-Based Document Conversation</div>
</div>
</div>
<div class="section-content">
<p>این قابلیت به کاربران اجازه می‌دهد فایل‌های خود را در یک <strong>Session موقت</strong> آپلود کرده و سپس با آن‌ها گفتگو کنند. Sessionها در حافظه نگهداری می‌شوند و پس از مدت TTL (پیش‌فرض ۳۰ دقیقه) به‌طور خودکار پاک می‌شوند.</p>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">Endpointهای جدید</h4>
<table class="styled-table">
<thead>
<tr>
<th>متد</th>
<th>مسیر</th>
<th>عملکرد</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="endpoint-badge method-post">POST</span></td>
<td><code>/ChatWithDocuments/Ingest</code></td>
<td>استخراج → Chunking → Embedding → ذخیره در Session</td>
</tr>
<tr>
<td><span class="endpoint-badge method-post">POST</span></td>
<td><code>/ChatWithDocuments/Ask</code></td>
<td>پرسش و پاسخ غیر-streaming بر اساس محتوای Session</td>
</tr>
<tr>
<td><span class="endpoint-badge method-post">POST</span></td>
<td><code>/ChatWithDocuments/AskStream</code></td>
<td>پرسش و پاسخ Streaming (SSE) با ارسال منابع در رویداد اول</td>
</tr>
<tr>
<td><span class="endpoint-badge method-get">GET</span></td>
<td><code>/ChatWithDocuments/Session/{id}</code></td>
<td>دریافت اطلاعات Session (تعداد Vector، فایل‌ها، انقضا)</td>
</tr>
<tr>
<td><span class="endpoint-badge method-delete">DELETE</span></td>
<td><code>/ChatWithDocuments/Session/{id}</code></td>
<td>حذف Session و آزادسازی حافظه</td>
</tr>
</tbody>
</table>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">اجزای پیاده‌سازی</h4>
<table class="styled-table">
<thead>
<tr>
<th>جزء</th>
<th>نقش</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>IXInMemoryVectorStore</code></td>
<td>مدیریت Sessionها با <code>ConcurrentDictionary</code>، جستجو با Cosine Similarity، پاکسازی خودکار</td>
</tr>
<tr>
<td><code>XInMemoryVectorStoreCleanupService</code></td>
<td>BackgroundService برای پاکسازی دوره‌ای Sessionهای منقضی (هر ۵ دقیقه)</td>
</tr>
<tr>
<td><code>XAiDocumentSession</code></td>
<td>مدل Session شامل <code>Vectors</code>، <code>Ttl</code>، <code>OwnerId</code>، <code>SyncRoot</code></td>
</tr>
<tr>
<td><code>XAiDocumentIngestResult</code></td>
<td>نتیجه Ingest شامل <code>SessionId</code>، <code>FilesCount</code>، <code>ChunksCount</code>، <code>SkippedFiles</code></td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 نکته امنیتی:</strong> هر Session به <code>OwnerId</code> کاربر گره خورده است. در <code>TryGet</code>، بررسی می‌شود که فقط مالک بتواند به Session دسترسی داشته باشد. همچنین Sessionها به‌صورت Thread-safe با <code>lock</code> روی <code>SyncRoot</code> مدیریت می‌شوند.
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 4: Knowledge (Persistent RAG) -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۴</div>
<div class="section-title">
<h3>قابلیت Knowledge (Persistent RAG)</h3>
<div class="subtitle">Scope-Based Persistent Knowledge Base</div>
</div>
</div>
<div class="section-content">
<p>برخلاف Chat With Documents، قابلیت Knowledge اسناد را به‌صورت <strong>پایدار</strong> در جدول <code>XAiVector</code> ذخیره می‌کند. این قابلیت از <strong>چهار سطح Scope</strong> پشتیبانی می‌کند و بازیابی به‌صورت <strong>Tiered</strong> انجام می‌شود.</p>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">سطوح Scope (XAiKnowledgeScope)</h4>
<table class="styled-table">
<thead>
<tr>
<th>Scope</th>
<th>Identifier Layout</th>
<th>دسترسی</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Conversation</strong></td>
<td><code>conversation/{conversationId}/{fileName}#{i}</code></td>
<td>فقط مالک Conversation</td>
</tr>
<tr>
<td><strong>Project</strong></td>
<td><code>project/{projectId}/{fileName}#{i}</code></td>
<td>فقط مالک Project</td>
</tr>
<tr>
<td><strong>User</strong></td>
<td><code>user/{ownerId}/{fileName}#{i}</code></td>
<td>فقط خود کاربر</td>
</tr>
<tr>
<td><strong>Global</strong></td>
<td><code>global/{fileName}#{i}</code></td>
<td>خواندن: همه — نوشتن: فقط Admin</td>
</tr>
</tbody>
</table>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">Endpointهای جدید</h4>
<table class="styled-table">
<thead>
<tr>
<th>متد</th>
<th>مسیر</th>
<th>دسترسی</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="endpoint-badge method-post">POST</span></td>
<td><code>/Knowledge/Ingest</code></td>
<td>Admin — آپلود فایل در Scope مشخص</td>
</tr>
<tr>
<td><span class="endpoint-badge method-get">GET</span></td>
<td><code>/Knowledge/Documents</code></td>
<td>Admin — لیست اسناد یک Scope</td>
</tr>
<tr>
<td><span class="endpoint-badge method-delete">DELETE</span></td>
<td><code>/Knowledge/Documents</code></td>
<td>Admin — حذف تمام Vectorهای یک سند</td>
</tr>
</tbody>
</table>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">بازیابی Tiered (XAiRAGService.RetrieveAsync)</h4>
<p>هنگام پرسش، جستجو به‌صورت پله‌ای انجام می‌شود:</p>
<ol style="padding-right: 25px; margin-top: 10px; font-size: 14px; line-height: 2;">
<li><strong>Conversation Tier</strong> — اگر ConversationId داده شده باشد</li>
<li><strong>Project Tier</strong> — اگر ProjectId داده شده یا از Conversation استخراج شود</li>
<li><strong>User Tier</strong> — دانش شخصی کاربر (اگر IncludeUser=true)</li>
<li><strong>Global Tier</strong> — دانش عمومی (اگر IncludeGlobal=true)</li>
</ol>
<p>در هر Tier، <strong>TopK باقی‌مانده</strong> پر می‌شود و Duplicateها با <code>HashSet</code> حذف می‌شوند.</p>
<div class="success-box">
<strong>✓ قابلیت‌های کلیدی XAiRAGService:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li><strong>IngestAsync</strong> — استخراج، Chunking، Embedding، Upsert در DB با Metadata کامل</li>
<li><strong>RetrieveAsync</strong> — بازیابی Tiered با رعایت Ownership</li>
<li><strong>LoadDocumentsAsync</strong> — لیست اسناد با GroupBy بر اساس FileName</li>
<li><strong>RemoveDocumentAsync</strong> — حذف بر اساس IdentifierPrefix</li>
<li><strong>MigrateLegacyVectorsAsync</strong> — مهاجرت Vectorهای قدیمی به Scope جدید (Idempotent)</li>
<li><strong>ToSources</strong> — تبدیل نتایج جستجو به منابع قابل نمایش با Snippet</li>
</ul>
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 5: File Content Extractors -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۵</div>
<div class="section-title">
<h3>اکسترکتورهای محتوای فایل</h3>
<div class="subtitle">File Content Extractors (13 Formats)</div>
</div>
</div>
<div class="section-content">
<p>برای پشتیبانی از فرمت‌های مختلف فایل، یک معماری <strong>Composite Pattern</strong> پیاده‌سازی شده است. <code>XFileContentExtractor</code> به‌عنوان Facade، بر اساس MIME Type به اکسترکتور مناسب delegate می‌کند.</p>
<table class="styled-table">
<thead>
<tr>
<th>#</th>
<th>اکسترکتور</th>
<th>فرمت‌های پشتیبانی‌شده</th>
<th>کتابخانه</th>
</tr>
</thead>
<tbody>
<tr><td>۱</td><td><code>XPdfFileContentExtractor</code></td><td>PDF (متنی + Fallback به تصویر برای PDF اسکن‌شده)</td><td>PdfPig + PDFtoImage</td></tr>
<tr><td>۲</td><td><code>XDocxFileContentExtractor</code></td><td>DOCX</td><td>DocumentFormat.OpenXml</td></tr>
<tr><td>۳</td><td><code>XExcelFileContentExtractor</code></td><td>XLSX, XLS</td><td>ExcelDataReader</td></tr>
<tr><td>۴</td><td><code>XPowerPointFileContentExtractor</code></td><td>PPTX, PPSX, POTX, PPTM + Legacy PPT</td><td>OpenXml + OfficeIMO</td></tr>
<tr><td>۵</td><td><code>XHtmlFileContentExtractor</code></td><td>HTML, XHTML</td><td>HtmlAgilityPack</td></tr>
<tr><td>۶</td><td><code>XRtfFileContentExtractor</code></td><td>RTF</td><td>RtfPipe</td></tr>
<tr><td>۷</td><td><code>XEpubFileContentExtractor</code></td><td>EPUB</td><td>VersOne.Epub</td></tr>
<tr><td>۸</td><td><code>XOpenDocumentFileContentExtractor</code></td><td>ODT, ODS, ODP</td><td>OfficeIMO.OpenDocument</td></tr>
<tr><td>۹</td><td><code>XPlainTextFileContentExtractor</code></td><td>TXT, MD, CSV, JSON, XML</td><td>—</td></tr>
<tr><td>۱۰</td><td><code>XCodeFileContentExtractor</code></td><td>C#, Python, JS, TS, Java, Go, Rust, ...</td><td>—</td></tr>
<tr><td>۱۱</td><td><code>XImageFileContentExtractor</code></td><td>PNG, JPG, GIF, WebP, BMP (با OCR Fallback)</td><td>Tesseract</td></tr>
<tr><td>۱۲</td><td><code>XAudioFileContentExtractor</code></td><td>MP3, WAV, OGG, M4A, WebM (Transcription)</td><td>Whisper.net + FFMpegCore</td></tr>
<tr><td>۱۳</td><td><code>XVisionFileContentExtractor</code></td><td>تصاویر و PDF از طریق Vision Model</td><td>Microsoft.Extensions.AI</td></tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 نکته:</strong> اکسترکتور PDF دارای <strong>Fallback هوشمند</strong> است. اگر متن استخراج‌شده کمتر از <code>MinTextLengthThreshold</code> باشد (پیش‌فرض ۵۰ کاراکتر)، PDF را به تصویر تبدیل کرده و برای Vision Model ارسال می‌کند — این برای PDFهای اسکن‌شده حیاتی است.
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 6: Text Chunker -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۶</div>
<div class="section-title">
<h3>سرویس Text Chunker</h3>
<div class="subtitle">Hierarchical Text Splitting</div>
</div>
</div>
<div class="section-content">
<p><code>XTextChunker</code> یک الگوریتم <strong>سلسله‌مراتبی</strong> برای تقسیم متن به Chunkهای همپوشان پیاده‌سازی می‌کند که برای Embedding مناسب هستند.</p>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 15px;">استراتژی تقسیم (به ترتیب اولویت)</h4>
<table class="styled-table">
<thead>
<tr>
<th>سطح</th>
<th>تقسیم‌کننده</th>
<th>توضیح</th>
</tr>
</thead>
<tbody>
<tr><td>۱</td><td>Paragraph</td><td>تقسیم بر اساس <code>\n\s*\n+</code> (خط خالی)</td></tr>
<tr><td>۲</td><td>Sentence</td><td>تقسیم بر اساس <code>. ! ? ؟ 。 ! ?</code> + خط جدید</td></tr>
<tr><td>۳</td><td>Word</td><td>تقسیم بر اساس Space/Tab/Newline با رعایت Word Boundary</td></tr>
<tr><td>۴</td><td>Hard Cut</td><td>برش سخت در صورت نبود Boundary</td></tr>
</tbody>
</table>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">پارامترهای پیکربندی (XTextChunkerDescriptor)</h4>
<table class="styled-table">
<thead>
<tr>
<th>پارامتر</th>
<th>پیش‌فرض</th>
<th>توضیح</th>
</tr>
</thead>
<tbody>
<tr><td><code>ChunkSize</code></td><td>800</td><td>حداکثر کاراکتر هر Chunk</td></tr>
<tr><td><code>ChunkOverlap</code></td><td>150</td><td>همپوشانی بین Chunkهای متوالی</td></tr>
<tr><td><code>MinChunkSize</code></td><td>50</td><td>Chunkهای کوچک‌تر با قبلی Merge می‌شوند</td></tr>
<tr><td><code>NormalizeWhitespace</code></td><td>true</td><td>نرمال‌سازی Line Ending و Whitespace</td></tr>
<tr><td><code>ChunkIdentifierSeparator</code></td><td>#</td><td>جداکننده در Identifier (مثلاً <code>file.pdf#3</code>)</td></tr>
</tbody>
</table>
<div class="success-box">
<strong>✓ ویژگی‌های کلیدی:</strong>
<ul style="padding-right: 20px; margin-top: 10px;">
<li>پشتیبانی از <strong>Persian</strong> و <strong>CJK</strong> در Sentence Splitting</li>
<li>محاسبه دقیق <strong>Offset</strong> برای هر Chunk (با <code>StartOffset</code>)</li>
<li>حفظ <strong>Overlap هوشمند</strong> با Word Boundary</li>
<li>Merge خودکار Chunkهای کوچک با Chunk قبلی</li>
</ul>
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 7: Embedding Service -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۷</div>
<div class="section-title">
<h3>سرویس Embedding</h3>
<div class="subtitle">XAiEmbeddingService</div>
</div>
</div>
<div class="section-content">
<p><code>XAiEmbeddingService</code> با استفاده از <code>Microsoft.Extensions.AI</code>، تولید Embedding را از هر Provider پشتیبانی می‌کند و خروجی را <strong>نرمال‌سازی</strong> می‌کند تا Cosine Similarity دقیق باشد.</p>
<table class="styled-table">
<thead>
<tr>
<th>متد</th>
<th>ورودی</th>
<th>خروجی</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>Embedding</code></td>
<td>یک رشته</td>
<td><code>float[]</code> نرمال‌شده</td>
</tr>
<tr>
<td><code>Embeddings</code></td>
<td>آرایه‌ای از رشته‌ها (Batch)</td>
<td><code>IReadOnlyList&lt;float[]&gt;</code></td>
</tr>
<tr>
<td><code>GetEmbeddingGenerator</code></td>
<td>—</td>
<td><code>IEmbeddingGenerator</code> برای استفاده مستقیم</td>
</tr>
</tbody>
</table>
<div class="info-box">
<strong>💡 نکته:</strong> پشتیبانی از هر دو Provider <strong>OpenAI</strong> و <strong>Ollama</strong> با تشخیص خودکار بر اساس <code>XAiModelProviderType</code>. مدل Embedding پیش‌فرض از طریق <code>XAiEmbeddingModelName</code> در <code>XAiApiConstants</code> تعیین می‌شود.
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 8: Database & Configuration Changes -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۸</div>
<div class="section-title">
<h3>تغییرات دیتابیس و پیکربندی</h3>
<div class="subtitle">Database & Configuration Changes</div>
</div>
</div>
<div class="section-content">
<h4 style="color: var(--primary); font-size: 15px;">Migration جدید</h4>
<p>یک Migration جدید با نام <code>InitialMigrationAiApi</code> ایجاد شده که شامل جداول زیر است:</p>
<table class="styled-table">
<thead>
<tr>
<th>جدول</th>
<th>توضیح</th>
<th>Indexes</th>
</tr>
</thead>
<tbody>
<tr><td><code>AiProjects</code></td><td>پروژه‌های AI کاربران</td><td>—</td></tr>
<tr><td><code>AiConversations</code></td><td>گفتگوهای هر پروژه</td><td>—</td></tr>
<tr><td><code>AiMessages</code></td><td>پیام‌های هر گفتگو</td><td>—</td></tr>
<tr class="highlight-row">
<td><code>AiVectors</code></td>
<td>Vectorهای ذخیره‌شده (Persistent RAG)</td>
<td><code>IX_AiVectors_Identifier</code>، <code>IX_AiVectors_Type</code></td>
</tr>
<tr><td><code>Files</code></td><td>فایل‌های آپلودی (xFileService)</td><td>—</td></tr>
<tr><td><code>Strings</code></td><td>منابع چندزبانه (xStringService)</td><td>—</td></tr>
<tr><td><code>Tags</code></td><td>تگ‌ها (xTagService)</td><td>—</td></tr>
</tbody>
</table>
<div class="warning-box">
<strong>⚠️ نکته مهم:</strong> جدول <code>AiVectors</code> دارای <strong>Index روی Identifier</strong> است که برای جستجوی Prefix-Based (مثلاً <code>user/{ownerId}/</code>) حیاتی است. همچنین Index روی <code>Type</code> برای فیلتر کردن بر اساس نوع دانش (مثلاً <code>XAiKnowledge</code>) استفاده می‌شود.
</div>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">تغییرات Constants</h4>
<p>در <code>XAiModelsConstants</code>، ثابت‌های زیر اضافه شده‌اند:</p>
<div class="file-tree">
// Knowledge (Persistent RAG) ...
<span class="new">XAiDefaultKnowledgeType</span> = "XAiKnowledge"
<span class="new">XAiKnowledgeSeparator</span> = "/"
<span class="new">XAiKnowledgeOwnerSeparator</span> = "/"
<span class="new">XAiKnowledgeChunkSeparator</span> = "#"
<span class="new">XAiKnowledgeUserPrefix</span> = "user"
<span class="new">XAiKnowledgeGlobalPrefix</span> = "global"
<span class="new">XAiKnowledgeProjectPrefix</span> = "project"
<span class="new">XAiKnowledgeConversationPrefix</span> = "conversation"
// Metadata Keys ...
<span class="new">XAiKnowledgeMetadataType</span> = "type"
<span class="new">XAiKnowledgeMetadataScope</span> = "scope"
<span class="new">XAiKnowledgeMetadataScopeId</span> = "scopeId"
<span class="new">XAiKnowledgeMetadataOwnerId</span> = "ownerId"
<span class="new">XAiKnowledgeMetadataIngestedAtUtc</span> = "ingestedAtUtc"
</div>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 25px;">تغییرات XAiVector Entity و DTO</h4>
<p>فیلد <code>Type</code> به <code>XAiVector</code> و <code>XAiVectorDto</code> اضافه شده است:</p>
<div class="file-tree">
<span class="folder">XAiVector.cs</span>
[StringLength(100)]
<span class="new">public string Type { get; set; }</span>
<span class="comment">// نوع دانش (e.g. "Legal", "Medical", "XAiKnowledge")</span>
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 9: DI Registration -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۹</div>
<div class="section-title">
<h3>تغییرات DI (Dependency Injection)</h3>
<div class="subtitle">Service Registration Changes</div>
</div>
</div>
<div class="section-content">
<p>در <code>XDIHelperExtension</code> (هم در xAiApi و هم xAiModels)، سرویس‌های زیر ثبت شده‌اند:</p>
<div class="file-tree">
<span class="comment">// xAiApi/DI/XDIHelperExtension.cs</span>
<span class="new">AddXAiFileContentExtractors()</span> → ثبت ۱۳ اکسترکتور (Singleton)
<span class="new">AddXAiTextChunker()</span> → ثبت XTextChunker (Singleton)
<span class="new">AddXAiInMemoryVectorStore()</span> → ثبت IXInMemoryVectorStore + Cleanup (Singleton)
<span class="new">AddXAiChatWithDocuments()</span> → فراخوانی InMemoryVectorStore
<span class="new">AddXRagService()</span> → ثبت IXAiEmbeddingService + IXAiRAGService (Scoped)
<span class="new">AddXAiServices()</span> → ثبت IXAiService + IXDefaultThinkingAiService (Scoped)
</div>
<div class="info-box">
<strong>💡 نکته درباره Lifetime:</strong> <code>IXTextChunker</code> و اکسترکتورها <strong>Singleton</strong> هستند (Stateless و Thread-safe). اما <code>IXAiRAGService</code> و <code>IXAiEmbeddingService</code> <strong>Scoped</strong> هستند (وابسته به HttpContext و DbContext).
</div>
<h4 style="color: var(--primary); font-size: 15px; margin-top: 20px;">تغییرات Startup</h4>
<p>در <code>Startup.ConfigureServices</code>، فراخوانی‌های زیر اضافه شده‌اند:</p>
<div class="file-tree">
<span class="comment">// بعد از AddXAiDataService و قبل از AddXAiApiConfiguration</span>
services.AddXAiApiConfiguration(Configuration);
services.<span class="new">AddXAiFileContentExtractors</span>();
services.<span class="new">AddXAiTextChunker</span>();
services.<span class="new">AddXAiChatWithDocuments</span>();
services.<span class="new">AddXRagService</span>();
services.<span class="new">AddXAiServices</span>();
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 10: API Endpoints Summary -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۱۰</div>
<div class="section-title">
<h3>خلاصه Endpointهای جدید</h3>
<div class="subtitle">New API Endpoints Summary</div>
</div>
</div>
<div class="section-content">
<table class="styled-table">
<thead>
<tr>
<th>Controller</th>
<th>Endpoint</th>
<th>عملکرد</th>
<th>دسترسی</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="5"><strong>ChatWithDocuments</strong></td>
<td><code>POST /Ingest</code></td>
<td>آپلود فایل در Session</td>
<td>Authorized User</td>
</tr>
<tr>
<td><code>POST /Ask</code></td>
<td>پرسش از Session</td>
<td>Authorized User</td>
</tr>
<tr>
<td><code>POST /AskStream</code></td>
<td>پرسش Streaming از Session</td>
<td>Authorized User</td>
</tr>
<tr>
<td><code>GET /Session/{id}</code></td>
<td>اطلاعات Session</td>
<td>Authorized User</td>
</tr>
<tr>
<td><code>DELETE /Session/{id}</code></td>
<td>حذف Session</td>
<td>Authorized User</td>
</tr>
<tr>
<td rowspan="3"><strong>Knowledge</strong></td>
<td><code>POST /Ingest</code></td>
<td>آپلود فایل در Scope</td>
<td>Admin</td>
</tr>
<tr>
<td><code>GET /Documents</code></td>
<td>لیست اسناد Scope</td>
<td>Admin</td>
</tr>
<tr>
<td><code>DELETE /Documents</code></td>
<td>حذف سند از Scope</td>
<td>Admin</td>
</tr>
<tr>
<td rowspan="3"><strong>XAiService (پایه)</strong></td>
<td><code>POST /AskWithFiles</code></td>
<td>پرسش با فایل (RAG یک‌مرحله‌ای)</td>
<td>Authorized User</td>
</tr>
<tr>
<td><code>POST /AskWithFilesStream</code></td>
<td>پرسش Streaming با فایل</td>
<td>Authorized User</td>
</tr>
<tr>
<td><code>POST /ExtractContent</code></td>
<td>استخراج محتوا از فایل‌ها</td>
<td>Authorized User</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 11: Key Design Decisions -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۱۱</div>
<div class="section-title">
<h3>تصمیمات کلیدی طراحی</h3>
<div class="subtitle">Key Design Decisions</div>
</div>
</div>
<div class="section-content">
<table class="styled-table">
<thead>
<tr>
<th>تصمیم</th>
<th>دلیل</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>دو حالت RAG جداگانه</strong> (In-Memory و Persistent)</td>
<td>In-Memory برای گفتگوی موقت و سبک؛ Persistent برای دانش پایدار سازمانی</td>
</tr>
<tr>
<td><strong>Identifier Prefix برای جداسازی Scope</strong></td>
<td>به جای فیلد جداگانه در دیتابیس، از Prefix در Identifier استفاده شده تا جستجو با <code>StartsWith</code> ساده و سریع باشد</td>
</tr>
<tr>
<td><strong>Prefix با Suffix «/»</strong></td>
<td>جلوگیری از تطابق ناخواسته (مثلاً <code>user/a</code> با <code>user/ab</code>)</td>
</tr>
<tr>
<td><strong>Fallback PDF به Vision</strong></td>
<td>PDFهای اسکن‌شده متن قابل استخراج ندارند؛ تبدیل به تصویر و استفاده از Vision Model ضروری است</td>
</tr>
<tr>
<td><strong>Normalize Embedding</strong></td>
<td>برای اطمینان از صحت Cosine Similarity و مقایسه‌پذیری Vectorها</td>
</tr>
<tr>
<td><strong>SSE برای Streaming</strong></td>
<td>پشتیبانی از پاسخ‌های طولانی بدون Timeout + ارسال تدریجی Tokenها به کلاینت</td>
</tr>
<tr>
<td><strong>Cleanup Service برای In-Memory</strong></td>
<td>جلوگیری از Memory Leak در صورت فراموشی حذف Session توسط کلاینت</td>
</tr>
</tbody>
</table>
<div class="success-box">
<strong>✓ نتیجه طراحی:</strong> معماری فعلی، تعادل مناسبی بین <strong>سادگی</strong> (In-Memory) و <strong>پایداری</strong> (Persistent) برقرار کرده و با رعایت اصول <strong>Separation of Concerns</strong>، قابلیت توسعه برای فرمت‌های جدید و Providerهای جدید را دارد.
</div>
</div>
</div>
<!-- ============================================================= -->
<!-- SECTION 12: Checklist -->
<!-- ============================================================= -->
<div class="section">
<div class="section-header">
<div class="section-number">۱۲</div>
<div class="section-title">
<h3>چک‌لیست تغییرات</h3>
<div class="subtitle">Changes Checklist</div>
</div>
</div>
<div class="section-content">
<ul class="checklist">
<li>افزودن <code>ChatWithDocumentsController</code> با ۵ Endpoint (Ingest، Ask، AskStream، GetSession، DeleteSession)</li>
<li>افزودن <code>KnowledgeController</code> با ۳ Endpoint (Ingest، Documents، DeleteDocument)</li>
<li>پیاده‌سازی <code>XAiRAGService</code> با Scope چهارسطحی و بازیابی Tiered</li>
<li>پیاده‌سازی <code>XInMemoryVectorStore</code> با مدیریت Session و TTL</li>
<li>پیاده‌سازی <code>XInMemoryVectorStoreCleanupService</code> برای پاکسازی دوره‌ای</li>
<li>پیاده‌سازی <code>XTextChunker</code> با استراتژی سلسله‌مراتبی</li>
<li>پیاده‌سازی <code>XAiEmbeddingService</code> با پشتیبانی OpenAI و Ollama</li>
<li>افزودن ۱۳ اکسترکتور محتوای فایل (PDF، DOCX، XLSX، PPTX، HTML، RTF، EPUB، ODF، Text، Code، Image، Audio، Vision)</li>
<li>افزودن فیلد <code>Type</code> به <code>XAiVector</code> و <code>XAiVectorDto</code></li>
<li>افزودن ثابت‌های Knowledge به <code>XAiModelsConstants</code></li>
<li>افزودن <code>XAiKnowledgeScope</code> (Enum چهارسطحی)</li>
<li>افزودن متدهای Vector (Upsert، Search، Load، Remove) به <code>IXAiDataProvider</code></li>
<li>ایجاد Migration <code>InitialMigrationAiApi</code> با جداول و Indexes جدید</li>
<li>به‌روزرسانی <code>Startup.cs</code> برای ثبت سرویس‌های جدید</li>
<li>به‌روزرسانی <code>XDIHelperExtension</code> در xAiApi و xAiModels</li>
</ul>
</div>
</div>
<!-- ============================================================= -->
<!-- CONCLUSION -->
<!-- ============================================================= -->
<div class="conclusion-box">
<h3>🎯 نتیجه نهایی</h3>
<p>تغییرات اعمال‌شده، xAiApi را از یک API ساده LLM به یک <strong>پلتفرم هوشمند سازمانی</strong> با قابلیت گفتگو با اسناد تبدیل می‌کند.</p>
<p>ترکیب <strong>In-Memory RAG</strong> (برای استفاده سریع و موقت) و <strong>Persistent RAG</strong> (برای دانش پایدار سازمانی)، به‌همراه <strong>۱۳ اکسترکتور فایل</strong> و <strong>Text Chunker سلسله‌مراتبی</strong>، زیرساخت کاملی برای پاسخ‌گویی هوشمند فراهم می‌کند.</p>
<div class="highlight">
<strong>💎 ارزش پیشنهادی:</strong><br>
Chat با اسناد + RAG پایدار با Scope چهارسطحی + پردازش ۱۳ فرمت فایل + معماری Composite قابل توسعه
</div>
</div>
<div class="page-info">
این مستند فنی-داخلی بوده و صرفاً جهت مستندسازی تغییرات تیم توسعه xSaherelmWorkspace تهیه شده است.
</div>
</div>
<footer class="document-footer">
<div class="footer-brand">تهیه و تنظیم: تیم معماری xSaherelmWorkspace</div>
<div class="footer-divider"></div>
<div>تاریخ تهیه: آبان ۱۴۰۵ &nbsp;|&nbsp; نسخه: ۱.۰ &nbsp;|&nbsp; طبقه‌بندی: فنی-داخلی</div>
</footer>
</div>
</body>
</html>