-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e44d857
commit a27edfd
Showing
6 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Text.Json; | ||
using ChatRPG.Data.Models; | ||
using LangChain.Chains.StackableChains.Agents.Tools; | ||
|
||
namespace ChatRPG.API.Tools; | ||
|
||
public class HealCharacterTool( | ||
IConfiguration configuration, | ||
Campaign campaign, | ||
ToolUtilities utilities, | ||
string name, | ||
string? description = null) : AgentTool(name, description) | ||
{ | ||
private static readonly JsonSerializerOptions JsonOptions = new() | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
private static readonly Dictionary<string, (int, int)> HealingRanges = new() | ||
{ | ||
{ "low", (5, 10) }, | ||
{ "medium", (10, 20) }, | ||
{ "high", (15, 25) }, | ||
{ "extraordinary", (25, 80) } | ||
}; | ||
|
||
public override async Task<string> ToolTask(string input, CancellationToken token = new CancellationToken()) | ||
{ | ||
try | ||
{ | ||
var effectInput = JsonSerializer.Deserialize<HealInput>(input, JsonOptions) ?? | ||
throw new JsonException("Failed to deserialize"); | ||
|
||
var instruction = configuration.GetSection("SystemPrompts").GetValue<string>("HealCharacterInstruction")!; | ||
var character = await utilities.FindCharacter(campaign, effectInput.Input!, instruction); | ||
|
||
if (character is null) | ||
{ | ||
return "Could not determine the character to heal. The character does not exist in the game. " + | ||
"Consider creating the character before wounding it."; | ||
} | ||
|
||
// Determine damage | ||
Random rand = new Random(); | ||
var (minHealing, maxHealing) = HealingRanges[effectInput.Magnitude!]; | ||
var healing = rand.Next(minHealing, maxHealing); | ||
|
||
character.AdjustHealth(healing); | ||
|
||
return $"The character {character.Name} is healed for {healing} health points. They now have {character.CurrentHealth} health points out of a total of {character.MaxHealth}."; | ||
|
||
} | ||
catch (Exception) | ||
{ | ||
return "Could not determine the character to heal. Tool input format was invalid. " + | ||
"Please provide a valid character name, description, and magnitude level in valid JSON without markdown."; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace ChatRPG.API.Tools; | ||
|
||
public class HealInput | ||
{ | ||
public string? Input { get; set; } | ||
public string? Magnitude { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ChatRPG/API/Tools/EffectInput.cs → ChatRPG/API/Tools/WoundInput.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters