The IExtensionResult interface is a fundamental component in Assistant .NET extensions that represents the output of your extension. It provides a versatile structure for communicating the result of your extension's task.
The IExtensionResult interface includes the following key components:
Result: A property that indicates the execution result of your extension task.AsText(): A method that returns a text representation of the result. This is presented in the result window of Assistant.By implementing this interface, you can create custom result classes that fit your specific requirements. This allows you to tailor the output of your extensions precisely to your task's objectives.
When implementing your extension, you can choose between built-in result classes or custom result classes that implement the IExtensionResult interface to define your extension's result. These classes allow you to encapsulate the data you wish to provide as the output of your extension's execution.
For example, here's how you can create a custom result class based on the IExtensionResult interface in your extension's RunAsync method:
public async Task<MyCustomResult> RunAsync(AssistantExtensionContext context, MyCustomArgs args, CancellationToken cancellationToken)
{
// Your extension logic here
if (someCondition)
{
return MyCustomResult.Succeeded("Task completed successfully.");
}
else
{
return MyCustomResult.Failed("Task failed due to a specific reason.");
}
}
Both built-in result classes and custom result classes that implement the IExtensionResult interface play a crucial role in conveying the outcome of your extension's task to users in a clear and structured manner.
In addition to using built-in result classes, Assistant extensions allow you to create custom result classes based on the IExtensionResult interface tailored to your specific needs. These custom result classes can include additional properties to convey detailed information about your extension's task.
Here's a guide on how to create your own custom result class:
Define a New Class: In your extension project, define a new class that implements the IExtensionResult interface. This class should represent your custom result and provide the Result property.
Add Custom Properties: Inside your custom result class, add properties that represent the data you want to return as the result of your extension task. These properties can be of various data types, depending on the information you want to convey.
For example:
public class MyCustomResult : IExtensionResult
{
public ExecutionResult Result { get; set; }
public string CustomProperty1 { get; set; }
public int CustomProperty2 { get; set; }
// ... add more properties as needed
public string? AsText()
{
// Implement the AsText method to return a text representation of the result.
}
}
ExecutionResult statuses.For example:
public class MyCustomResult : IExtensionResult
{
public ExecutionResult Result { get; set; }
public string CustomProperty1 { get; set; }
public int CustomProperty2 { get; set; }
// ... add more properties as needed
public MyCustomResult(ExecutionResult result, string customProperty1, int customProperty2)
{
Result = result;
CustomProperty1 = customProperty1;
CustomProperty2 = customProperty2;
}
public string? AsText()
{
// Implement the AsText method to return a text representation of the result.
return $"Result: {Result}, CustomProperty1: {CustomProperty1}, CustomProperty2: {CustomProperty2}";
}
public static MyCustomResult Succeeded(string customProperty1, int customProperty2)
{
return new MyCustomResult(ExecutionResult.Succeeded, customProperty1, customProperty2);
}
public static MyCustomResult PartiallySucceeded(string customProperty1, int customProperty2)
{
return new MyCustomResult(ExecutionResult.PartiallySucceeded, customProperty1, customProperty2);
}
public static MyCustomResult Failed(string customProperty1, int customProperty2)
{
return new MyCustomResult(ExecutionResult.Failed, customProperty1, customProperty2);
}
}
RunAsync method, you can use the static methods to create instances of your custom result class, setting the custom properties with the specific data generated by your extension's logic and specifying the ExecutionResult status.By creating custom result classes based on the IExtensionResult interface, you have the flexibility to convey complex and detailed information as the result of your extension's task. These custom result classes allow you to tailor the result output precisely to your extension's requirements.