mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-07-28 01:42:57 +00:00
63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using AIStudio.Settings.DataModel;
|
|
|
|
namespace AIStudio.Tools.Services;
|
|
|
|
public sealed partial class RustService
|
|
{
|
|
public async Task<string> GetPDFText(string filePath)
|
|
{
|
|
var response = await this.http.GetAsync($"/retrieval/fs/read/pdf?file_path={filePath}");
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
this.logger!.LogError($"Failed to read the PDF file due to an network error: '{response.StatusCode}'");
|
|
return string.Empty;
|
|
}
|
|
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
|
|
public async Task<string> ReadArbitraryFileData(string path, int maxEvents)
|
|
{
|
|
var requestUri = $"/retrieval/fs/extract?path={Uri.EscapeDataString(path)}";
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
|
var response = await this.http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
|
|
if (!response.IsSuccessStatusCode) { return string.Empty; }
|
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync();
|
|
using var reader = new StreamReader(stream);
|
|
|
|
var resultBuilder = new StringBuilder();
|
|
var eventCount = 0;
|
|
|
|
while (!reader.EndOfStream && eventCount < maxEvents)
|
|
{
|
|
var line = await reader.ReadLineAsync();
|
|
|
|
if (string.IsNullOrEmpty(line)) continue;
|
|
if (!line.StartsWith("data:")) continue;
|
|
|
|
var jsonContent = line[5..];
|
|
|
|
try
|
|
{
|
|
var sseEvent = JsonSerializer.Deserialize<SseEvent>(jsonContent);
|
|
if (sseEvent != null)
|
|
{
|
|
var content = await SseHandler.ProcessEventAsync(sseEvent, false);
|
|
resultBuilder.Append(content);
|
|
eventCount++;
|
|
}
|
|
}
|
|
catch (JsonException) { resultBuilder.Append(string.Empty); }
|
|
|
|
}
|
|
var result = resultBuilder.ToString();
|
|
|
|
return result;
|
|
}
|
|
} |