54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
|
|
namespace Toak.Core.Skills;
|
|
|
|
public class DynamicSkill : ISkill
|
|
{
|
|
private readonly SkillDefinition _def;
|
|
|
|
public string Name => _def.Name;
|
|
public string Description => _def.Description;
|
|
public string[] Hotwords => _def.Hotwords;
|
|
|
|
public bool HandlesExecution => _def.Action.ToLowerInvariant() == "script";
|
|
|
|
public DynamicSkill(SkillDefinition def)
|
|
{
|
|
_def = def;
|
|
}
|
|
|
|
public string GetSystemPrompt(string rawTranscript)
|
|
{
|
|
return _def.SystemPrompt.Replace("{transcript}", rawTranscript);
|
|
}
|
|
|
|
public void Execute(string llmResult)
|
|
{
|
|
if (HandlesExecution && !string.IsNullOrWhiteSpace(_def.ScriptPath))
|
|
{
|
|
var expandedPath = Environment.ExpandEnvironmentVariables(_def.ScriptPath);
|
|
if (expandedPath.StartsWith("~"))
|
|
{
|
|
expandedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + expandedPath.Substring(1);
|
|
}
|
|
|
|
try
|
|
{
|
|
var process = Process.Start(new ProcessStartInfo
|
|
{
|
|
FileName = expandedPath,
|
|
Arguments = $"\"{llmResult.Replace("\"", "\\\"")}\"",
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
});
|
|
process?.WaitForExit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"[DynamicSkill] Error executing script '{expandedPath}': {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|