﻿================================================================================
 Commit Message - xSaherelmWorkspace
 تاریخ: ۱۴۰۵/۰۸/۱۱ (2026-11-02)
 نسخه مستند: v01
 گزارش کامل: Documents/Docs/ChangeLogs/1405-08-11-v01.html
================================================================================

هدف: بازآرایی لایهٔ گفتگو در xAiApi (استخراج PrepareChatRequirementsAsync و
مدل XPreparedChatContext)، مقاوم‌سازی XAiRAGService و XInMemoryVectorStore،
Nullable شدن projectId در لایهٔ داده xAiModels و انتقال XAiApiConfiguration
به سطح ریشهٔ Workspace.

ترتیب Commit: [1] xAiModels -> [2] xAiApi -> [3] Root

[1] Modules/xAiModels
Make projectId Nullable in Data Provider and Remove Api Configuration
- IXAiDataProvider.GetDefaultConversation: projectId changed from Guid to Guid?;
- XAiDataProvider.GetDefaultConversation / HasDefaultConversation /
  CreateDefaultConversation: adapt to Guid? (use !projectId.HasValue instead of
  projectId.IsNull(); resolve project via projectId.Value);
- XAiDataProvider: conversation lookup predicate fixed to
  x.ProjectId == project.Id (was x.ProjectId == projectId);
- XEmbeddingExtensions: add ToEmbedding(this ReadOnlyMemory<float>) to persist
  embeddings as JSON;
- XAiDataProvider: use item.Embedding.ToEmbedding() instead of ToJSON() when
  upserting vectors;
- XAiModelsExtensions: rename GetDafualtProject -> GetDafaultProject (typo fix);
- Remove Configurations/XAiApiConfiguration.cs and
  Extensions/XAiApiConfigurationExtensions.cs (Api configuration does not belong
  to the models module).
BREAKING: XAiApiConfiguration / XAiApiConfigurationExtensions removed from
          xAiModels; GetDafualtProject renamed to GetDafaultProject.
Files: D Configurations/XAiApiConfiguration.cs;
       D Extensions/XAiApiConfigurationExtensions.cs;
       M Extensions/XAiModelsExtensions.cs;
       M Extensions/XEmbeddingExtensions.cs;
       M Interfaces/IXAiDataProvider.cs;
       M Providers/XAiDataProvider.cs


[2] xAiApi
Refactor Chat Pipeline into PrepareChatRequirementsAsync and Harden RAG Services
- XAIServiceBase: add PrepareChatRequirementsAsync as the single entry point
  for chat prerequisites (normalize RAG params, resolve default
  project/conversation, ingest attachments, retrieve RAG context, build history,
  persist prompt message) and return the new XPreparedChatContext;
- XAIServiceBase: add NormalizeRagParams (topK / minScore / type) and use it in
  all Ask* methods instead of duplicated inline normalization;
- XAIServiceBase: rework GetHistory - the single
  GetHistory(ChatMessage, bool) signature is replaced by two overloads
  GetHistory(prompt, forcePrompt) and GetHistory(prompt, context); the latter is
  used by the non-persisted chat path;
- XAIServiceBase: remove AskLLMAsync; LLM invocation is now inlined in each
  Ask* method over prep.History;
- XAIServiceBase: rewrite AskAsync and AskAsEnumerable on top of
  PrepareChatRequirementsAsync; streamed responses now use Role = Assistant,
  RespondTo = prep.PersistedPromptMessage.Id and Sequence++;
- XAIServiceBase: use XAiApiConstants.XAiApiRagPromptName instead of
  configuration.RAGDescriptor.PromptName;
- XAIServiceBase.LoadConversations: call the renamed GetDafaultProject;
- Add Models/XPreparedChatContext.cs (Project, Conversation, History, Context,
  PersistedPromptMessage) as the shared context between actions;
- XAiRAGService: wrap each file ingest in its own try/catch with
  logger.LogError so one bad file no longer aborts the whole ingest;
- XAiRAGService: upload the file and add a scope reference
  (providedFor / forProvidedId) for the ingested scope;
- XAiRAGService: remove stale vectors of the same document via
  RemoveVectorsAsync(identifierPrefix) when replaceExisting is set;
- XInMemoryVectorStore: make session retrieval thread-safe via
  lock (found.SyncRoot) and ReferenceEquals check against concurrent removal;
- XAiServiceControllerBase.Ask: pass the attached files to the AI service
  (was files: null) so RAG works on attachments;
- XModelsExtensions: relax ChatWithDocuments ask validation - TopK and
  MinScore are optional again (defaults come from RAGDescriptor);
- XAiModelProxyMiddleware: use logical OR instead of bitwise OR in the
  authentication check;
- XAiEmbeddingServiceBase: do not dispose the embedding generator
  (removed `using` to avoid premature disposal);
- ChatWithDocumentsController: reorder EmbedChunks parameters;
- Remove Interfaces/IXAiEmbeddingServiceControllerBase.cs.
Files: A Models/XPreparedChatContext.cs;
       D Interfaces/IXAiEmbeddingServiceControllerBase.cs;
       M Controllers/ChatWithDocumentsController.cs;
       M Controllers/XAiServiceControllerBase.cs;
       M Extensions/XModelsExtensions.cs;
       M Middlewares/XAiModelProxyMiddleware.cs;
       M Providers/XAIServiceBase.cs;
       M Providers/XAiEmbeddingServiceBase.cs;
       M Providers/XAiRAGService.cs;
       M Providers/XInMemoryVectorStore.cs


[3] Workspace (root)
- 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.

Validation:
- dotnet build xAiApi/xAiApi.csproj --nologo -v q -> 0 Error(s),
  64 pre-existing warnings (MSB3277);
- Runtime / end-to-end tests not performed.

Known Issues (to fix before release):
- Removing XAiApiConfiguration from xAiModels is a breaking change for any
  other consumer of that module;
- GetDafaultProject rename is breaking for out-of-module consumers;
- XAiDataProvider now relies on `project` being loaded before the
  `x.ProjectId == project.Id` predicate;
- SSE response Role changed from System to Assistant - verify SSE consumers;
- The "ChatWithDocuments" prompt must exist in production appsettings.

Suggested root commit title:
Refactor Chat Pipeline, Harden RAG Services, Nullable projectId (۱۴۰۵/۰۸/۱۱ v01)

For additional documentation use v02:
1405-08-11-v02.html
CommitMessage-1405-08-11-v02.txt
================================================================================

