forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaitSkill.cs
61 lines (52 loc) · 1.76 KB
/
WaitSkill.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.SkillDefinition;
namespace Microsoft.SemanticKernel.CoreSkills;
/// <summary>
/// WaitSkill provides a set of functions to wait before making the rest of operations.
/// </summary>
/// <example>
/// Usage: kernel.ImportSkill("wait", new WaitSkill());
/// Examples:
/// {{wait.seconds 10}} => Wait 10 seconds
/// </example>
public class WaitSkill
{
private readonly IWaitProvider _waitProvider;
public interface IWaitProvider
{
Task DelayAsync(int milliSeconds);
}
private sealed class WaitProvider : IWaitProvider
{
public Task DelayAsync(int milliSeconds)
{
return Task.Delay(milliSeconds);
}
}
public WaitSkill(IWaitProvider? waitProvider = null)
{
this._waitProvider = waitProvider ?? new WaitProvider();
}
/// <summary>
/// Wait a given amount of seconds
/// </summary>
/// <example>
/// {{wait.seconds 10}} (Wait 10 seconds)
/// </example>
[SKFunction("Wait a given amount of seconds")]
[SKFunctionName("Seconds")]
[SKFunctionInput(DefaultValue = "0", Description = "The number of seconds to wait")]
public async Task SecondsAsync(string secondsText)
{
if (!decimal.TryParse(secondsText, NumberStyles.Any, CultureInfo.InvariantCulture, out var seconds))
{
throw new ArgumentException("Seconds provided is not in numeric format", nameof(secondsText));
}
var milliseconds = seconds * 1000;
milliseconds = (milliseconds > 0) ? milliseconds : 0;
await this._waitProvider.DelayAsync((int)milliseconds).ConfigureAwait(false);
}
}