Files
xSaherElmAiApi/Models/XAiKnowledgeModels.cs
T
saherelm 37d4e622e9 Implement RAG Scopes, Remove ProxyController, Fix Pipeline Order and SSE Format
- RAG: real scopes with identifiers conversation/{id}/, project/{id}/, user/{ownerId}/, global/;
- RAG: ownership checks via GetProject/GetConversation; Global write requires admin role;
- RAG: tiered retrieval Conversation -> Project -> User -> Global (single embedding, dedup, TopK fill);
- RAG: apply Type filter, sanitize file names, enforce MaxFilesPerIngest/MaxFileSizeBytes/MaxDocumentsPerScope;
- RAG: store scope/scopeId/type/ownerId metadata; return it in documents and sources;
- RAG: add MigrateLegacyVectorsAsync + POST /Knowledge/MigrateLegacy ({ownerId}/... -> user/{ownerId}/...);
- Knowledge API: scope/scopeId on Ingest, Documents, DELETE Documents;
- Remove ProxyController (root route "{modelName}/{path}" + AllowAnonymous hijacked all 2-segment paths);
- Startup: ExceptionHandler("/error") + HSTS outside Development; Swagger only in Development;
  static files before routing; CORS before model proxy; add /error endpoint;
- SSE: all stream endpoints use xHttpService XSseExtensions; validate before streaming;
  pre-stream errors return proper HTTP status; remove "Connection: keep-alive";
- Use xAiModels.Constants for XAiKnowledgeScope / Knowledge constants / XOpenAINoKey;
- Register XAiVectorEntityRegisterar in XAiApiDbContext;
- XCodeFileContentExtractor: actually read stream content; drop hard-coded "book.epub".
BREAKING: AskTextStream emits data: {"text":"..."}; AskStream emits data: {json}; both end with event: done.
2026-10-29 19:59:18 +03:30

255 lines
7.4 KiB
C#

using System;
using xAiModels.Constants;
using System.Collections.Generic;
namespace xAiApi.Models
{
/// <summary>
/// Describes Target Knowledge Base Scope of an Operation ...
/// </summary>
public class XAiKnowledgeScopeDescriptor
{
/// <summary>
/// Scope Kind ...
/// </summary>
public XAiKnowledgeScope Scope { get; set; } = XAiKnowledgeScope.User;
/// <summary>
/// Scope Identifier (ProjectId / ConversationId) - Null for Global and User ...
/// </summary>
public Guid? ScopeId { get; set; }
/// <summary>
/// Optional Knowledge Type / Domain Filter ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// Create User Scope Knowledge Descriptor ...
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor User(
string type = null
) => new()
{
Type = type,
ScopeId = null,
Scope = XAiKnowledgeScope.User,
};
/// <summary>
/// Create Global Scope Knowledge Descriptor ...
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor Global(
string type = null
) => new()
{
Type = type,
ScopeId = null,
Scope = XAiKnowledgeScope.Global,
};
/// <summary>
/// Create Project Scope Knowledge Descriptor ...
/// </summary>
/// <param name="projectId"></param>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor Project(
Guid projectId,
string type = null
) => new()
{
Type = type,
ScopeId = projectId,
Scope = XAiKnowledgeScope.Project,
};
/// <summary>
/// Create Conversational Scope Knowledge Descriptor ...
/// </summary>
/// <param name="conversationId"></param>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor Conversation(
Guid conversationId,
string type = null
) => new()
{
Type = type,
ScopeId = conversationId,
Scope = XAiKnowledgeScope.Conversation,
};
}
/// <summary>
/// Result of Ingesting Files into Persistent Knowledge Base (XAiVector Table) ...
/// </summary>
public class XAiKnowledgeIngestResult
{
/// <summary>
/// Target Scope ...
/// </summary>
public XAiKnowledgeScope Scope { get; set; }
/// <summary>
/// Target Scope Identifier (Null for Global) ...
/// </summary>
public Guid? ScopeId { get; set; }
/// <summary>
/// Knowledge Type of Ingested Vectors ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// Count of Successfully Ingested Files ...
/// </summary>
public int FilesCount { get; set; }
/// <summary>
/// Count of Stored Chunks (Vectors) in this Request ...
/// </summary>
public int ChunksCount { get; set; }
/// <summary>
/// Count of Removed Stale Vectors (Re-Ingest) ...
/// </summary>
public int ReplacedCount { get; set; }
/// <summary>
/// Skipped Files (Empty, Unsupported or Extraction Failed) ...
/// </summary>
public IList<string> SkippedFiles { get; set; } = new List<string>();
/// <summary>
/// Ingested Documents Info ...
/// </summary>
public IList<XAiKnowledgeDocumentInfo> Documents { get; set; } = new List<XAiKnowledgeDocumentInfo>();
}
/// <summary>
/// Ask Request over Persistent Knowledge Base ...
/// Retrieval is Tiered: Conversation -> Project -> User -> Global (Each Tier Fills Remaining TopK) ...
/// </summary>
public class XAiKnowledgeAskRequest
{
/// <summary>
/// Question ...
/// </summary>
public string Prompt { get; set; }
/// <summary>
/// Optional: Conversation Scope (Caller must be Conversation Owner) ...
/// When Provided, Conversation's Project is used for Second Tier Automatically ...
/// </summary>
public Guid? ConversationId { get; set; }
/// <summary>
/// Optional: Project Scope (Caller must be Project Owner) ...
/// </summary>
public Guid? ProjectId { get; set; }
/// <summary>
/// Include Personal (User) Knowledge Tier (default true) ...
/// </summary>
public bool IncludeUser { get; set; } = true;
/// <summary>
/// Include Global Knowledge Tier (default true) ...
/// </summary>
public bool IncludeGlobal { get; set; } = true;
/// <summary>
/// Optional: Restrict Retrieval to a Knowledge Type / Domain ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// Optional: Restrict Retrieval to a Specific File Name ...
/// </summary>
public string FileName { get; set; }
/// <summary>
/// Optional: Max Retrieved Chunks ...
/// </summary>
public int? TopK { get; set; }
/// <summary>
/// Optional: Minimum Cosine Similarity Score ...
/// </summary>
public float? MinScore { get; set; }
}
/// <summary>
/// Ingested Document Info ...
/// </summary>
public class XAiKnowledgeDocumentInfo
{
/// <summary>
/// Scope ...
/// </summary>
public XAiKnowledgeScope Scope { get; set; }
/// <summary>
/// Scope Identifier (Null for Global) ...
/// </summary>
public Guid? ScopeId { get; set; }
/// <summary>
/// Knowledge Type ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// File Name ...
/// </summary>
public string FileName { get; set; }
/// <summary>
/// Mime Type ...
/// </summary>
public string MimeType { get; set; }
/// <summary>
/// Ingesting User Identifier ...
/// </summary>
public string OwnerId { get; set; }
/// <summary>
/// Count of Stored Chunks ...
/// </summary>
public int ChunksCount { get; set; }
/// <summary>
/// Ingestion Time (UTC) ...
/// </summary>
public DateTime? IngestedAtUtc { get; set; }
}
/// <summary>
/// Public Vector Info (Export) ...
/// </summary>
public class XAiKnowledgeVectorInfo
{
public Guid Id { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public Guid? ScopeId { get; set; }
public string FileName { get; set; }
public int? ChunkIndex { get; set; }
public string Identifier { get; set; }
public XAiKnowledgeScope Scope { get; set; }
public Dictionary<string, string> Metadata { get; set; }
/// <summary>
/// Embedding (Only Filled when Requested) ...
/// </summary>
public float[] Embedding { get; set; }
}
}