From 783cbd9ea006f5ce906520fcd7d7d9bc7b3ef33f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 31 Dec 2025 10:49:59 +0000 Subject: [PATCH] Implement WaitCommand and update agent prompt - Added `WaitCommand` logic (Command, Definition, Handler, Event, Formatter) to allow players to pass turns. - Updated `SystemPrompt.hbs` to instruct AI agents to use `wait` instead of ``. - Added unit tests for `WaitCommand` behavior. --- .../Commands/Meta/WaitCommandTests.cs | 62 +++++++++++++++++++ MooSharp/Agents/SystemPrompt.hbs | 2 +- .../Commands/Commands/Meta/WaitCommand.cs | 51 +++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 MooSharp.Tests/Commands/Meta/WaitCommandTests.cs create mode 100644 MooSharp/Commands/Commands/Meta/WaitCommand.cs diff --git a/MooSharp.Tests/Commands/Meta/WaitCommandTests.cs b/MooSharp.Tests/Commands/Meta/WaitCommandTests.cs new file mode 100644 index 0000000..db89d18 --- /dev/null +++ b/MooSharp.Tests/Commands/Meta/WaitCommandTests.cs @@ -0,0 +1,62 @@ +using MooSharp.Actors.Players; +using MooSharp.Commands.Commands.Meta; +using MooSharp.Commands.Machinery; +using MooSharp.Commands.Presentation; +using MooSharp.Tests.Handlers; +using MooSharp.Tests.TestDoubles; +using NSubstitute; + +namespace MooSharp.Tests.Commands.Meta; + +public class WaitCommandTests +{ + private readonly World.World _world; + private readonly Player _player; + private readonly Player _observer; + private readonly WaitHandler _handler; + + public WaitCommandTests() + { + var origin = HandlerTestHelpers.CreateRoom("origin"); + + var repository = new InMemoryWorldRepository(); + _world = new World.World(repository, Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance); + _world.Initialize([origin]); + + _player = HandlerTestHelpers.CreatePlayer("Tester"); + _world.RegisterPlayer(_player); + _world.MovePlayer(_player, origin); + + _observer = HandlerTestHelpers.CreatePlayer("Observer"); + _world.RegisterPlayer(_observer); + _world.MovePlayer(_observer, origin); + + _handler = new WaitHandler(_world); + } + + [Fact] + public async Task Handle_ReturnsTimePassesForActor() + { + var cmd = new WaitCommand { Player = _player }; + var result = await _handler.Handle(cmd); + + var actorEvents = result.Messages.Where(m => m.Player == _player).Select(m => m.Event).ToList(); + Assert.Single(actorEvents); + + var formatter = new PlayerWaitsEventFormatter(); + Assert.Equal("Time passes...", formatter.FormatForActor((PlayerWaitsEvent)actorEvents[0])); + } + + [Fact] + public async Task Handle_ReturnsWaitMessageForObserver() + { + var cmd = new WaitCommand { Player = _player }; + var result = await _handler.Handle(cmd); + + var observerEvents = result.Messages.Where(m => m.Player == _observer).Select(m => m.Event).ToList(); + Assert.Single(observerEvents); + + var formatter = new PlayerWaitsEventFormatter(); + Assert.Equal("Tester waits.", formatter.FormatForObserver((PlayerWaitsEvent)observerEvents[0])); + } +} diff --git a/MooSharp/Agents/SystemPrompt.hbs b/MooSharp/Agents/SystemPrompt.hbs index f3ac844..21258b8 100644 --- a/MooSharp/Agents/SystemPrompt.hbs +++ b/MooSharp/Agents/SystemPrompt.hbs @@ -9,7 +9,7 @@ You will be fed a feed of the game's output. Respond to it as you see fit. If nothing happens in the game for a while, you will be prompted to act autonomously by the system. -You may emit the special command `` to indicate you don't want to do anything. +You may emit the command `wait` to indicate you don't want to do anything. Besides that, you will be given no special tools beyond what a human player has access to. diff --git a/MooSharp/Commands/Commands/Meta/WaitCommand.cs b/MooSharp/Commands/Commands/Meta/WaitCommand.cs new file mode 100644 index 0000000..fb7efda --- /dev/null +++ b/MooSharp/Commands/Commands/Meta/WaitCommand.cs @@ -0,0 +1,51 @@ +using MooSharp.Actors.Players; +using MooSharp.Commands.Machinery; +using MooSharp.Commands.Parsing; +using MooSharp.Commands.Presentation; + +namespace MooSharp.Commands.Commands.Meta; + +public class WaitCommand : CommandBase +{ + public required Player Player { get; init; } +} + +public class WaitCommandDefinition : ICommandDefinition +{ + public IReadOnlyCollection Verbs { get; } = ["wait", "z"]; + public CommandCategory Category => CommandCategory.Meta; + public string Description => "Pass a turn without acting."; + + public string? TryCreateCommand(ParsingContext ctx, ArgumentBinder binder, out ICommand? command) + { + command = new WaitCommand + { + Player = ctx.Player + }; + + return null; + } +} + +public class WaitHandler(World.World world) : IHandler +{ + public Task Handle(WaitCommand cmd, CancellationToken cancellationToken = default) + { + var result = new CommandResult(); + var room = world.GetLocationOrThrow(cmd.Player); + var gameEvent = new PlayerWaitsEvent(cmd.Player); + + result.Add(cmd.Player, gameEvent); + result.BroadcastToAllButPlayer(room, cmd.Player, gameEvent); + + return Task.FromResult(result); + } +} + +public record PlayerWaitsEvent(Player Player) : IGameEvent; + +public class PlayerWaitsEventFormatter : IGameEventFormatter +{ + public string FormatForActor(PlayerWaitsEvent gameEvent) => "Time passes..."; + public string FormatForObserver(PlayerWaitsEvent gameEvent) => $"{gameEvent.Player.Username} waits."; +}