mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-09-27 01:33:37 +00:00
Added the .NET side of the document open endpoint
This commit is contained in:
parent
994473adf7
commit
9e9efb80e4
8
app/MindWork AI Studio/Tools/Rust/OpenDocumentRequest.cs
Normal file
8
app/MindWork AI Studio/Tools/Rust/OpenDocumentRequest.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace AIStudio.Tools.Rust;
|
||||
|
||||
/// <summary>
|
||||
/// Asks the runtime to open a document in the program the system uses for it.
|
||||
/// </summary>
|
||||
/// <param name="Path">The document to open.</param>
|
||||
/// <param name="Page">The page to show, counted from one, or null when the document has none.</param>
|
||||
public readonly record struct OpenDocumentRequest(string Path, int? Page);
|
||||
14
app/MindWork AI Studio/Tools/Rust/OpenDocumentResponse.cs
Normal file
14
app/MindWork AI Studio/Tools/Rust/OpenDocumentResponse.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace AIStudio.Tools.Rust;
|
||||
|
||||
/// <summary>
|
||||
/// Says how opening a document went.
|
||||
/// </summary>
|
||||
/// <param name="Success">Whether the document was opened at all.</param>
|
||||
/// <param name="PageApplied">
|
||||
/// Whether the document was handed to its program together with the page. False means it opens on
|
||||
/// its first page: no page was asked for, the system uses a program which cannot be told one, or
|
||||
/// starting that program failed. None of these is an error, so this belongs in the log rather than
|
||||
/// in front of the user, who is told the page by the source itself.
|
||||
/// </param>
|
||||
/// <param name="Issue">Why the document could not be opened, or an empty text when it was.</param>
|
||||
public readonly record struct OpenDocumentResponse(bool Success, bool PageApplied, string Issue);
|
||||
@ -168,4 +168,66 @@ public sealed partial class RustService
|
||||
result.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a document in the program the system uses for it, on the given page where possible.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The page is best effort and never decides whether this succeeded. Which programs can be
|
||||
/// told a page is the runtime's business, and it says afterwards whether it managed to.
|
||||
/// </remarks>
|
||||
/// <param name="path">The document to open.</param>
|
||||
/// <param name="pageNumber">The page to show, counted from one, or null when there is none.</param>
|
||||
/// <returns>Whether the document was opened, whether the page was applied, and what went wrong.</returns>
|
||||
public async Task<OpenDocumentResponse> TryOpenDocumentInSystemViewer(string path, int? pageNumber)
|
||||
{
|
||||
HttpResponseMessage result;
|
||||
try
|
||||
{
|
||||
result = await this.http.PostAsJsonAsync("/open/document", new OpenDocumentRequest(path, pageNumber), this.jsonRustSerializerOptions);
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
this.logger!.LogWarning(e, "Failed to reach the Rust runtime document endpoint.");
|
||||
return new OpenDocumentResponse(false, false, TB("The runtime document endpoint is not available."));
|
||||
}
|
||||
catch (TaskCanceledException e)
|
||||
{
|
||||
this.logger!.LogWarning(e, "Timed out while reaching the Rust runtime document endpoint.");
|
||||
return new OpenDocumentResponse(false, false, TB("The runtime document endpoint is not available."));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger!.LogWarning("Failed to open a document through the Rust runtime: '{StatusCode}'", result.StatusCode);
|
||||
return new OpenDocumentResponse(false, false, string.Format(TB("The runtime document endpoint returned '{0}'."), result.StatusCode));
|
||||
}
|
||||
|
||||
var response = await result.Content.ReadFromJsonAsync<OpenDocumentResponse>(this.jsonRustSerializerOptions);
|
||||
if (response.Success)
|
||||
{
|
||||
//
|
||||
// A page which was asked for but not applied is noted here and nowhere else: the
|
||||
// document is open, and the source the user clicked names the page anyway.
|
||||
//
|
||||
if (pageNumber is > 0 && !response.PageApplied)
|
||||
this.logger!.LogInformation("Opened a document without the requested page {PageNumber}, because the system uses a program which cannot be told one.", pageNumber);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
return new OpenDocumentResponse(false, false, string.IsNullOrWhiteSpace(response.Issue) ? TB("The runtime document endpoint failed without details.") : response.Issue);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.logger!.LogWarning(e, "Failed to process the Rust runtime document endpoint response.");
|
||||
return new OpenDocumentResponse(false, false, TB("The runtime document endpoint failed without details."));
|
||||
}
|
||||
finally
|
||||
{
|
||||
result.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user