Files
xSaherElmAiApi/Providers/Extractors/XCodeFileContentExtractor.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

131 lines
3.7 KiB
C#

using System;
using System.Linq;
using xCommons.Extensions;
using System.Threading.Tasks;
using System.Collections.Generic;
using xAiApi.Interfaces.Extractors;
using System.IO;
using System.Threading;
using xAiModels.Models;
using System.Text;
namespace xAiApi.Providers.Extractors
{
/// <summary>
/// Extractor for source code files ...
/// (C#, Python, JavaScript, TypeScript, Java, Go, Rust, ...)
/// </summary>
public class XCodeFileContentExtractor : IXCodeFileContentExtractor
{
/// <summary>
/// Supported MIME Types ...
/// </summary>
private static readonly string[] SupportedMimeTypes =
[
"text/x-c",
"text/x-go",
"text/yaml",
"text/x-php",
"text/x-c++",
"text/x-sql",
"text/x-java",
"text/x-rust",
"text/x-ruby",
"text/x-csharp",
"text/x-python",
"text/javascript",
"text/typescript",
"text/x-java-source",
"application/x-yaml",
"application/x-csharp",
"application/x-python",
"application/javascript",
"application/typescript",
];
/// <summary>
/// Check if this extractor supports the specified MIME type ...
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
public bool CanExtract(string mimeType)
{
//
return SupportedMimeTypes.Contains(
mimeType?.ToLowerInvariant() ?? string.Empty
);
}
/// <summary>
/// Extract text content from file stream ...
/// </summary>
/// <param name="fileStream"></param>
/// <param name="mimeType"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<string> ExtractAsync(
Stream fileStream,
string mimeType,
CancellationToken cancellationToken = default
)
{
//
var result = await ExtractRichAsync(
fileStream,
string.Empty,
mimeType,
cancellationToken
);
//
return result.Text;
}
/// <summary>
/// Extract content from stream as Rich Result ...
/// </summary>
/// <param name="fileStream"></param>
/// <param name="fileName"></param>
/// <param name="mimeType"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<XFileExtractionResult> ExtractRichAsync(
Stream fileStream,
string fileName,
string mimeType,
CancellationToken cancellationToken = default
)
{
//
var result = new XFileExtractionResult
{
FileName = fileName,
MimeType = mimeType
};
//
// Read entire source code as UTF-8 (with BOM detection) ...
using var reader = new StreamReader(
fileStream,
Encoding.UTF8,
leaveOpen: false,
detectEncodingFromByteOrderMarks: true
);
//
var content = await reader.ReadToEndAsync();
//
// Normalize line endings for consistent downstream processing ...
content = content?
.Replace("\r\n", "\n")
.Replace("\r", "\n");
//
result.Text = content;
//
return result;
}
}
}