Skip to content

Add Name property to InteractionInput and enable name-based access in results #10835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 5, 2025

This PR adds support for accessing interaction inputs by name instead of just by index, as requested in the issue.

Changes Made

1. Added optional Name property to InteractionInput

Users can now specify a name for each input that will be used for lookup:

var inputs = new List<InteractionInput>
{
    new InteractionInput { Name = "Username", Label = "Username", InputType = InputType.Text },
    new InteractionInput { Name = "Password", Label = "Password", InputType = InputType.SecretText }
};

2. Created InteractionInputCollection class

A new collection type that supports both indexed and name-based access:

var result = await interactionService.PromptInputsAsync("Login", "Enter credentials", inputs);
if (!result.Canceled)
{
    var collection = result.Data;
    
    // New: Access by name
    var username = collection["Username"].Value;
    var password = collection["Password"].Value;
    
    // Existing: Access by index (backward compatibility)
    var firstInput = collection[0].Value;
    var secondInput = collection[1].Value;
}

3. Automatic name generation

When no name is specified, names are automatically generated from labels:

  • "User Name" becomes "User_Name"
  • "Email Address" becomes "Email_Address"
  • Conflicts are resolved with numeric suffixes ("Input", "Input_1", "Input_2")

4. Duplicate name validation

Clear error messages are thrown when duplicate names are provided:

InvalidOperationException: Duplicate input name 'Username' found. Input names must be unique.

5. Enhanced validation context

InputsDialogValidationContext.Inputs now supports name-based access:

ValidationCallback = context =>
{
    var emailInput = context.Inputs["Email"];
    var ageInput = context.Inputs["Age"];
    // Validate inputs by name instead of remembering indices
    return Task.CompletedTask;
}

Backward Compatibility

All existing code continues to work unchanged:

  • PromptInputsAsync results still support indexed access
  • InputsDialogValidationContext.Inputs still supports enumeration and indexing
  • No breaking changes to existing APIs
  • All existing tests pass without modification

Additional Features

  • TryGetByName(name, out input) for safe name-based lookup
  • ContainsName(name) to check if a name exists
  • Names property to enumerate all available names
  • Case-insensitive name lookup

Fixes #10834.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] InteractionInputs should be accessiable by name in results Add Name property to InteractionInput and enable name-based access in results Aug 5, 2025
@Copilot Copilot AI requested a review from JamesNK August 5, 2025 06:57
Copilot finished work on behalf of JamesNK August 5, 2025 06:57
@JamesNK
Copy link
Member

JamesNK commented Aug 6, 2025

@davidfowl @mitchdenny InteractionInput.Name is optional. What do you think of generating the name from the label?

Alternatively, if you don't specify a name then the input isn't accessible by name, just using the index (current behavior).

I'm not a fan of generating the name, but curious what you all think.

@JamesNK JamesNK marked this pull request as ready for review August 6, 2025 07:04
@Copilot Copilot AI review requested due to automatic review settings August 6, 2025 07:04
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for accessing interaction inputs by name instead of just by index, implementing name-based access functionality for the interaction service. The changes enable users to specify optional names for inputs and access them through a new InteractionInputCollection class.

  • Adds an optional Name property to InteractionInput for explicit naming
  • Introduces InteractionInputCollection that supports both indexed and name-based access with automatic name generation
  • Updates the interaction service API to return InteractionInputCollection instead of IReadOnlyList<InteractionInput>

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Aspire.Hosting/IInteractionService.cs Adds InteractionInputCollection class with name-based access capabilities and updates InteractionInput with Name property
src/Aspire.Hosting/InteractionService.cs Updates method signatures to use InteractionInputCollection and handles early validation/name generation
src/Aspire.Hosting/api/Aspire.Hosting.cs Updates public API signatures to reflect the new collection type
tests/Aspire.Hosting.Tests/InteractionServiceTests.cs Comprehensive test coverage for name-based access, validation, and edge cases
tests/Shared/TestInteractionService.cs Updates test service implementation to work with new collection type
Comments suppressed due to low confidence (1)

tests/Aspire.Hosting.Tests/InteractionServiceTests.cs:45

  • This line creates a nested InteractionInputCollection by wrapping result.Data (which is already an InteractionInputCollection) in another collection. This could cause issues with the wrapped collection's functionality.
        // Act 1

@mitchdenny
Copy link
Member

@JamesNK the API is experimental. We could make the Name required at this early stage? Better to be more explicit than less?

@JamesNK
Copy link
Member

JamesNK commented Aug 7, 2025

There are scenarios where you don't want name to be required. For example:

var result = await interactionService.PromptInputAsync(
    title: "Hello world",
    message: "What is your name?",
    input: new InteractionInput { Label = "Name", InputType = InputType.Text });

You wouldn't want to require a name here because it's never used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

InteractionInputs should be accessiable by name in results
3 participants