nsquared agents

Downloads and guides for the nsquared agents desktop characters.

View on GitHub

Building a Simple Command

In order to build a new activity for nsquared agents you will need to build a .NET 8.0 assembly that contains a class that implements the IAgentCommand interface.

The full source code for this example

Step-by-Step creating a simple nsquared agent Command

  1. Start by creating a new C# class library project named SimpleCommand.

     dotnet new classlib --name SimpleCommand
    

    This will create a new folder named SimpleCommand containing C# project named SimpleCommand, and a code file named Class1.cs.

  2. Rename the file Class1.cs to Command.cs
  3. Rename the class in the code to Command

     namespace SimpleCommand;
     public class Command
     {
     }
    
  4. In the SimpleCommand.csproj file make sure the TargetFramework is net8.0

     <TargetFramework>net8.0</TargetFramework>
    
  5. In the SimpleCommand.csproj file add a TargetExt field below the TargetFramework line

     <TargetExt>.Command</TargetExt>
    
  6. Add a package reference to the nsquared.agents NuGet package, from PowerShell you can do this with the following command.

    dotnet add package nsquared.agents.api --prerelease
    
    

    This will add the reference to the SimpleCommand.csproj file

     <ItemGroup>
       <PackageReference Include="naquared.agents.api" />
     </ItemGroup>
    
  7. In the Command.cs file add a using to import the nsquared.agents namespace

     using nsquared.agents;
    
  8. In the Command.cs file implement the IAgentCommand interface in the Command class

     public class Command : IAgentCommand
    
  9. Add the required methods to the Command class

     using nsquared.agents;
    
     namespace SimpleCommand;
     public class Command : IAgentCommand
     {
         public string Name => "SimpleCommand";
    
         public bool HasSettings => false;
         public AgentCommandType CommandType => AgentCommandType.KeywordLocal;
    
         Task<string?> IAgentCommand.Perform(string commandRequest, IAgentAnimations? animations)
         {
             if (commandRequest.Contains("simple", StringComparison.CurrentCultureIgnoreCase))
             {
                 return Task.FromResult<string?>("I am doing a simple command!");
             }
             return Task.FromResult<string?>(null);
         }
     }
    
  10. Build the SimpleCommand project. It should build the SimpleCommand.Command file in a bin folder.

  11. Run the nsquared agents application and open Settings, and then go to Manage Commands

    Manage Commands Menu in Settings

  12. In the Commands select Add

    Remove SimpleCommand from Commands

  13. Find the SimpleCommand.Command file you have built.

    Remove SimpleCommand from Commands

  14. Invoke the SimpleCommand by using the keyword simple in your request

Doing a simple command

The full source code for this example

Building a Command with Settings