-
Notifications
You must be signed in to change notification settings - Fork 313
Add named-pipe agent samples for DirectLineFlex #613
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b3fd9d
Add named-pipe agent samples for DirectLineFlex
web-flow 5250f0b
Enhance named-pipe agent with HTTP client and improve shutdown handling
web-flow 1688538
Merge branch 'main' into users/matthewm/namedpipes-samples
matthewmeyer e2a4cda
Docs fix for pull request finding
matthewmeyer 617ffb1
Merge branch 'main' into users/matthewm/namedpipes-samples
tracyboehrer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.Builder; | ||
| using Microsoft.Agents.Builder.App; | ||
| using Microsoft.Agents.Builder.State; | ||
| using Microsoft.Agents.Core.Models; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace NamedPipeAgent; | ||
|
|
||
| public class MyAgent : AgentApplication | ||
| { | ||
| public MyAgent(AgentApplicationOptions options) : base(options) | ||
| { | ||
| OnConversationUpdate(ConversationUpdateEvents.MembersAdded, WelcomeMessageAsync); | ||
| OnActivity(ActivityTypes.Message, OnMessageAsync, rank: RouteRank.Last); | ||
| } | ||
|
|
||
| private async Task WelcomeMessageAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) | ||
| { | ||
| foreach (ChannelAccount member in turnContext.Activity.MembersAdded) | ||
| { | ||
| if (member.Id != turnContext.Activity.Recipient.Id) | ||
| { | ||
| await turnContext.SendActivityAsync(MessageFactory.Text("Hello and Welcome! I am a named-pipe agent."), cancellationToken); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task OnMessageAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) | ||
| { | ||
| int count = turnState.Conversation.GetValue("conversation.counter", () => 0) + 1; | ||
| turnState.Conversation.SetValue("conversation.counter", count); | ||
|
|
||
| await turnContext.SendActivityAsync($"[{count}] You said: {turnContext.Activity.Text}", cancellationToken: cancellationToken); | ||
| } | ||
| } |
This file contains hidden or 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,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <ImplicitUsings>disable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Remove="wwwroot\**" /> | ||
| <Content Remove="wwwroot\**" /> | ||
| <EmbeddedResource Remove="wwwroot\**" /> | ||
| <None Remove="wwwroot\**" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <!-- Named-pipe transport currently ships as a 1.6 prerelease (beta) package. --> | ||
| <PackageReference Include="Microsoft.Agents.Hosting.AspNetCore" Version="1.6.109-beta" /> | ||
| <PackageReference Include="Microsoft.Agents.Hosting.DirectLine.NamedPipes" Version="1.6.109-beta" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,39 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.Hosting.AspNetCore; | ||
| using Microsoft.Agents.Hosting.DirectLine.NamedPipes; | ||
| using Microsoft.Agents.Storage; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using NamedPipeAgent; | ||
|
|
||
| WebApplicationBuilder builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.AddHttpClient(); | ||
|
|
||
| // Add the AgentApplication, which contains the logic for responding to | ||
| // user messages. | ||
| builder.AddAgent<MyAgent>(); | ||
|
|
||
| // Register IStorage. For development, MemoryStorage is suitable. | ||
| // For production Agents, persisted storage should be used so | ||
| // that state survives Agent restarts, and operates correctly | ||
| // in a cluster of Agent instances. | ||
| builder.Services.AddSingleton<IStorage, MemoryStorage>(); | ||
|
|
||
| // Add the named-pipe transport for the DirectLine App Service extension (DirectLineFlex). | ||
| // The pipe is a trusted channel — the sidecar handles external authentication, so this | ||
| // pipe-only agent does NOT need JWT token validation or any HTTP endpoint mapping. | ||
| // | ||
| // These calls (used by the HTTP-based quickstart sample) are intentionally omitted: | ||
| // builder.Services.AddAgentAspNetAuthentication(builder.Configuration); | ||
| // app.UseAuthentication(); | ||
| // app.UseAuthorization(); | ||
| // app.MapAgentRootEndpoint(); | ||
| // app.MapAgentApplicationEndpoints(); | ||
| builder.AddAgentNamedPipeTransport(); | ||
|
|
||
| WebApplication app = builder.Build(); | ||
|
|
||
| app.Run(); | ||
12 changes: 12 additions & 0 deletions
12
samples/dotnet/named-pipe-agent/Properties/launchSettings.json
This file contains hidden or 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,12 @@ | ||
| { | ||
| "profiles": { | ||
| "NamedPipeAgent": { | ||
| "commandName": "Project", | ||
| "launchBrowser": false, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| }, | ||
| "applicationUrl": "https://localhost:3979;http://localhost:3978" | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,80 @@ | ||
| # Named Pipe Agent Sample (.NET) | ||
|
|
||
| This sample demonstrates a **pipe-only** agent — it accepts activities exclusively over named pipes via the `Microsoft.Agents.Hosting.DirectLine.NamedPipes` library. It is the canonical shape used when deploying behind the **DirectLine App Service extension** (a.k.a. DirectLineFlex), where the sidecar relays traffic to the agent over a named pipe instead of HTTP. | ||
|
|
||
| The agent echoes the text of each request back to the caller, prefixed with a per-conversation counter. | ||
|
|
||
| > **Note:** The named-pipe transport currently is only designed to work with DirectLine App Service extension (DirectLineFlex). The DirectLine App Service extension (DirectLineFlex) that pairs with this agent over the pipe is only available on **Windows** App Service, so this sample is intended for Windows deployment targets. | ||
|
|
||
| ## What's different from the QuickStart sample | ||
|
|
||
| Compared to [quickstart](../quickstart/README.md), this sample: | ||
|
|
||
| - **Adds** the `Microsoft.Agents.Hosting.DirectLine.NamedPipes` package and calls `builder.AddAgentNamedPipeTransport();`. | ||
| - **Omits** HTTP endpoint mapping (`MapAgentRootEndpoint`, `MapAgentApplicationEndpoints`). | ||
| - **Omits** ASP.NET authentication wiring (`AddAgentAspNetAuthentication`, `UseAuthentication`, `UseAuthorization`). | ||
|
|
||
| `AddAgentNamedPipeTransport()` adds: | ||
|
|
||
| - A hosted service that listens on the named-pipe server pair (`bfv4.pipes.incoming` / `bfv4.pipes.outgoing`). | ||
| - A delegating handler that routes outbound HTTP calls to `urn:botframework:namedpipe:*` back through the pipe. | ||
|
|
||
| > Because no HTTP endpoint is mapped, this sample is not directly reachable from the Bot Framework Emulator or Agents Playground. If you need an HTTP endpoint as well (for health checks or local Emulator testing), start from [quickstart](../quickstart/README.md) and add `builder.AddAgentNamedPipeTransport()` to that project. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [.NET 8.0 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) | ||
| - **Windows** deployment target — the DirectLine App Service extension (DirectLineFlex) that connects to this agent is only available on Windows App Service. | ||
|
|
||
| ## Running locally | ||
|
|
||
| ```bash | ||
| dotnet run | ||
| ``` | ||
|
|
||
| When the process starts, the agent: | ||
|
|
||
| - **Does not** expose `/api/messages` (no HTTP handlers are mapped). | ||
| - **Does not** require external authentication configuration. | ||
| - Creates the named-pipe server pair (`bfv4.pipes.incoming` / `bfv4.pipes.outgoing`) and waits for a client to connect. | ||
|
|
||
| To exercise the agent locally over the named pipe, you need a process on the same machine that connects as the named-pipe client (the role normally played by the DirectLine App Service extension sidecar). For a no-pipe local interactive loop, use the [quickstart](../quickstart/README.md) sample instead. | ||
|
|
||
| ### Custom pipe name | ||
|
|
||
| The DirectLine App Service extension uses the pipe name `{WEBSITE_SITE_NAME}.directline`. To use a different pipe name, pass it to the extension: | ||
|
|
||
| ```csharp | ||
| builder.AddAgentNamedPipeTransport("my-custom-pipe"); | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| ┌──────────────────────┐ ┌─────────────────────┐ | ||
| │ DirectLineFlex │──named pipe──│ NamedPipeAgent │ | ||
| │ Sidecar (client) │ │ (this sample) │ | ||
| └──────────────────────┘ └─────────────────────┘ | ||
| Handles: Handles: | ||
| - External auth (JWT) - Activity processing | ||
| - TLS termination - Echo responses | ||
| - WebSocket ↔ Pipe relay - State management | ||
| ``` | ||
|
|
||
| The pipe is a trusted channel — the sidecar handles external authentication, so no JWT token validation is performed on the agent side. | ||
|
|
||
| ## Deployment to Azure App Service | ||
|
|
||
| When deployed to Azure App Service with the DirectLine App Service extension enabled: | ||
|
|
||
| 1. The App Service sidecar connects to your agent over the named pipe pair. | ||
| 2. External traffic, authentication, and TLS are handled by the sidecar. | ||
| 3. The pipe connection is treated as trusted; no JWT validation is performed on the pipe. | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [Microsoft 365 Agents SDK](https://learn.microsoft.com/en-us/microsoft-365/agents-sdk/) | ||
| - [`Microsoft.Agents.Hosting.DirectLine.NamedPipes` package](https://www.nuget.org/packages/Microsoft.Agents.Hosting.DirectLine.NamedPipes) | ||
| - [Configure .NET bot for extension](https://learn.microsoft.com/en-us/azure/bot-service/bot-service-channel-directline-extension-net-bot) | ||
| - [QuickStart sample](../quickstart/README.md) — HTTP-based base sample | ||
| - [Node.js Named Pipe Agent sample](../../nodejs/named-pipe-agent/README.md) — equivalent sample in JavaScript |
This file contains hidden or 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,22 @@ | ||
| { | ||
| "AgentApplication": { | ||
| "StartTypingTimer": false, | ||
| "RemoveRecipientMention": false, | ||
| "NormalizeMentions": false | ||
| }, | ||
|
|
||
| "Logging": { | ||
| "Debug": { | ||
| "IncludeScopes": true | ||
| }, | ||
| "Console": { | ||
| "IncludeScopes": true | ||
| }, | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning", | ||
| "Microsoft.Agents": "Debug", | ||
| "Microsoft.Hosting.Lifetime": "Information" | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,98 @@ | ||
| # Named Pipe Agent Sample (Node.js) | ||
|
|
||
| This sample demonstrates a **pipe-only** agent — it accepts activities exclusively over named pipes via the `@microsoft/agents-hosting-directline-namedpipes` package. It is the canonical shape used when deploying behind the **DirectLine App Service extension** (a.k.a. DirectLineFlex), where the sidecar relays traffic to the agent over a named pipe instead of HTTP. | ||
|
|
||
| The agent echoes the text of each request back to the caller, prefixed with a per-conversation counter. | ||
|
|
||
| > **Note:** The named-pipe transport currently is only designed to work with DirectLine App Service extension (DirectLineFlex). The DirectLine App Service extension (DirectLineFlex) that pairs with this agent over the pipe is only available on **Windows** App Service, so this sample is intended for Windows deployment targets. | ||
|
|
||
| ## What's different from the QuickStart sample | ||
|
|
||
| Compared to [quickstart](../quickstart/README.md), this sample: | ||
|
|
||
| - **Uses** `createLocalAdapter()` instead of `CloudAdapter` — no credentials needed. | ||
| - **Uses** `startNamedPipeServer()` instead of `startServer()` — no HTTP endpoint. | ||
| - **Omits** Express, `.env` auth config, and JWT middleware entirely. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [Node.js](https://nodejs.org) version 20 or higher | ||
|
|
||
| ```bash | ||
| node --version | ||
| ``` | ||
|
|
||
| - **Windows** — this sample is intended for Windows deployment targets (the DirectLine App Service extension / DirectLineFlex that connects to it is Windows-only). | ||
|
|
||
| ## Running locally | ||
|
|
||
| 1. Open this folder from your IDE or terminal of preference. | ||
| 1. Build the sample (this also installs dependencies via the `prebuild` script): | ||
|
|
||
| ```sh | ||
| npm run build | ||
| ``` | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| 1. Copy the environment template and start the agent: | ||
|
|
||
| ```powershell | ||
| Copy-Item env.TEMPLATE .env # edit if needed | ||
| npm start | ||
| ``` | ||
|
|
||
| When the process starts, the agent: | ||
|
|
||
| - **Does not** expose an HTTP endpoint (no Express, no `/api/messages`). | ||
| - **Does not** require Azure/Entra authentication configuration. | ||
| - Creates the named-pipe server pair (`bfv4.pipes.incoming` / `bfv4.pipes.outgoing`) and waits for a client to connect. | ||
|
|
||
| To exercise the agent locally over the named pipe, you need a process on the same machine that connects as the named-pipe client (the role normally played by the DirectLine App Service extension sidecar). For a no-pipe local interactive loop, use the [quickstart](../quickstart/README.md) sample instead. | ||
|
|
||
| ### Custom pipe name | ||
|
|
||
| The DirectLine App Service extension uses the pipe name `{WEBSITE_SITE_NAME}.directline`. Set the pipe name via the `PIPE_NAME` environment variable (defaults to `bfv4.pipes`): | ||
|
|
||
| ```powershell | ||
| $env:PIPE_NAME='{WEBSITE_SITE_NAME}.directline'; npm start | ||
| ``` | ||
|
matthewmeyer marked this conversation as resolved.
|
||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| ┌──────────────────────┐ ┌─────────────────────┐ | ||
| │ DirectLineFlex │──named pipe──│ NamedPipeAgent │ | ||
| │ Sidecar (client) │ │ (this sample) │ | ||
| └──────────────────────┘ └─────────────────────┘ | ||
| Handles: Handles: | ||
| - External auth (JWT) - Activity processing | ||
| - TLS termination - Echo responses | ||
| - WebSocket ↔ Pipe relay - State management | ||
| ``` | ||
|
|
||
| The pipe is a trusted channel — the sidecar handles external authentication, so no JWT token validation is needed on the agent side. | ||
|
|
||
| ## Key APIs used | ||
|
|
||
| | API | Purpose | | ||
| |-----|---------| | ||
| | `createLocalAdapter()` | Creates a `CloudAdapter` configured for pipe-only use (no credentials) | | ||
| | `startNamedPipeServer(adapter, logic, options)` | Starts the pipe server with auto-reconnect | | ||
| | `service.ready` | Promise that resolves when the first connection is established | | ||
| | `service.stop()` | Graceful shutdown | | ||
|
|
||
| ## Deployment to Azure App Service | ||
|
|
||
| When deployed to Azure App Service with the DirectLine App Service extension enabled: | ||
|
|
||
| 1. The App Service sidecar connects to your agent over the named pipe pair. | ||
| 2. External traffic, authentication, and TLS are handled by the sidecar. | ||
| 3. The pipe connection is treated as trusted; no JWT validation is performed on the pipe. | ||
| 4. Set the pipe name via the `PIPE_NAME` env var to match the platform expectation. | ||
|
|
||
| ## Further reading | ||
|
|
||
| - [Microsoft 365 Agents SDK](https://learn.microsoft.com/en-us/microsoft-365/agents-sdk/) | ||
| - [`@microsoft/agents-hosting-directline-namedpipes` package](https://www.npmjs.com/package/@microsoft/agents-hosting-directline-namedpipes) | ||
| - [Configure Node.js bot for extension](https://learn.microsoft.com/en-us/azure/bot-service/bot-service-channel-directline-extension-node-bot) | ||
| - [QuickStart sample](../quickstart/README.md) — HTTP-based base sample | ||
| - [.NET Named Pipe Agent sample](../../dotnet/named-pipe-agent/README.md) — equivalent sample in .NET | ||
This file contains hidden or 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 @@ | ||
| # rename to .env | ||
|
|
||
| # Named pipe name. The DirectLine App Service extension uses "{WEBSITE_SITE_NAME}.directline". | ||
| # Defaults to "bfv4.pipes" when unset. | ||
| PIPE_NAME=bfv4.pipes | ||
|
|
||
| DEBUG=agents:*:error |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.