AI copilot composition
The AI copilot module connects Elsa Weaver to the GitHub Copilot SDK and its headless runtime. It turns Weaver's provider-neutral chat, tool, context, and stream contracts into Copilot sessions, then registers that provider with the Elsa hosting platform. See AI platform for the wider provider and host model.
Copilot provider boundary
The Copilot provider is an adapter between the Elsa AI contracts and the Copilot session model. The host selects it like any other IAIProvider, while the Copilot-specific SDK remains inside this module.
Session lifecycle
The provider creates a stable session identity from the Weaver conversation. For a new turn, it starts the Copilot client, resumes the saved Copilot session when possible, and creates a session when no usable session exists. This lets the host keep conversation ownership and provider session state separate.
The provider adds the selected model, provider endpoint, reasoning level, and stream settings to the Copilot session. It also adds the server-resolved Elsa context to the prompt so the model can use the current workflow or runtime view without resolving that context itself.
References: src/…/CopilotProvider.cs• L14-41 src/…/CopilotProvider.cs• L100-149 src/…/CopilotProvider.cs• L158-189
Tool bridge
The tool bridge presents each enabled Elsa tool as a Copilot function. It keeps the tool name, description, input schema, and Elsa safety metadata with the function. When Copilot calls the function, the bridge sends the call to the host's tool invoker instead of executing Elsa work inside the adapter.
This split preserves the host's visibility, tenant, permission, enablement, and audit rules. Copilot receives a text or error result plus tool status data that it can use to continue the turn.
References: src/…/CopilotProvider.cs• L126-156 src/…/ElsaCopilotToolFunction.cs• L10-49 src/…/AIToolDefinition.cs• L3-19
Event stream
Copilot session events are mapped to the provider-neutral event types used by Weaver. The map covers assistant text, reasoning, tool start, tool result, and conversation errors. The host can therefore stream Copilot output to the same client surface as output from another provider.
The event stream ends when the Copilot session becomes idle or reports an error. The host then assigns the final conversation sequence and handles persistence, audit, and reconnect behavior.
This diagram shows how a chat turn crosses the provider boundary and returns to the host stream.
flowchart LR
Request[Weaver chat request] --> Host[AI host orchestrator]
Host -->|context and enabled tools| Provider[Copilot provider]
Provider --> Session[Copilot session]
Session -->|messages, reasoning, tool events| Mapper[Event mapper]
Mapper -->|provider events| Host
Provider -->|tool call| Invoker[Host tool invoker]
Invoker --> Tools[Governed Elsa tools]
Tools -->|tool result| Session
Host --> Stream[Weaver event stream]
References: src/…/CopilotProvider.cs• L32-72 src/…/CopilotSessionEventMapper.cs• L7-88 src/…/AIOrchestrator.cs• L155-218
Copilot options
Copilot options describe how the adapter reaches the Copilot runtime and how it shapes a session. The application can configure the options through AddCopilotAIProvider, while the shell feature exposes the main deployment and model choices as platform feature settings.
Runtime connection
The adapter supports two runtime forms. A runtime URL uses a remote connection and token. A runtime path with arguments starts or addresses a local standard-input/output process. If neither form is set, the Copilot client uses its own default connection behavior.
The same options also set the working and base directories. These directories give the Copilot runtime its file and project context. Authentication can use a GitHub token or the logged-in user mode.
References: src/…/CopilotOptions.cs• L3-17 src/…/CopilotProvider.cs• L74-97
Session behavior
The module exposes the provider name, model, reasoning effort, and streaming controls. A request can supply a model or endpoint selection for the current provider configuration; otherwise the configured Copilot defaults apply.
Streaming is enabled by default, and sub-agent stream events are included by default. These settings affect what the provider sends to the host, not the host's permission checks or tool selection.
References: src/…/CopilotOptions.cs• L9-17 src/…/CopilotProvider.cs• L126-147
Hosting registration
The Copilot module is a provider package, not the owner of chat policy. The AI host owns provider choice, context resolution, tool governance, audit, conversation state, and the /ai/chat surface. Copilot supplies the provider implementation that the host can select.
Dependency registration
Applications register the adapter with AddCopilotAIProvider. This adds the Copilot options, event mapper, and provider to the shared IAIProvider collection. The host can then discover the provider by its configured name.
The host selects an enabled provider by request, agent, default provider, or the single available provider. It passes only visible and enabled tools to the selected provider, then routes tool calls back through the host.
References: src/…/ServiceCollectionExtensions.cs• L10-21 src/…/AIOrchestrator.cs• L273-308 src/…/AIOrchestrator.cs• L346-384
Shell feature
CopilotAIFeature is the platform-facing registration point for the provider. Its CopilotAI shell feature name places the feature in the AI category and its service setup calls the provider registration method with the feature values.
The feature exposes runtime location, runtime arguments, directory, authentication, model, reasoning, and provider-name settings. It does not own host-wide AI settings such as conversation retention, default agents, tool enablement, or audit behavior; those belong to the host AI feature.
References: src/…/CopilotAIFeature.cs• L8-48 src/…/AIFeature.cs• L12-53
Studio feature link
Elsa Studio links its Weaver AI module to the remote host feature name. This makes the Studio feature depend on the host's published AI capability rather than on the Copilot adapter directly. A host can therefore expose Weaver AI with a different provider while keeping the Studio integration stable.
References: src/…/Feature.cs• L6-13 src/…/AIFeature.cs• L12-19
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the Copilot event names and data fields aligned with the host stream mapper. A changed provider event shape can stop assistant text or tool results from appearing in Weaver.
- Keep the function schema and tool-call argument conversion aligned with
AIToolDefinition. A schema that differs from the host invocation payload can cause valid Copilot calls to fail. - Keep the provider name consistent across
CopilotOptions, shell feature settings, andAIHostOptions.Providers. A name mismatch can leave the provider registered but unselectable. - Keep the provider session ID format compatible with resume and normalization. A change can create new Copilot sessions instead of continuing saved conversations.
- Keep the Studio remote feature name aligned with the host shell feature when the host capability is renamed.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| No enabled provider matches the host selection, or several providers exist without a default | The chat completes with a no-provider response instead of calling Copilot | Set a matching provider name and enable it in host provider settings |
| The Copilot runtime cannot start or the remote connection fails | The provider turn emits a conversation error and the host marks the chat as failed | Check the runtime URL or path, connection token, authentication mode, and runtime availability |
| A saved Copilot session cannot be resumed | The provider creates a new session, which can lose provider-side pending context | Keep conversation and provider session IDs stable and inspect resume logs when continuity matters |
| A requested tool is hidden, disabled, or missing | Copilot receives a failed tool result and cannot use that action | Check tool permissions, tenant and agent scope, enablement, and host registration |
| A client disconnects during an active stream | The host keeps reconnect state only for the configured grace period | Reconnect with the same conversation ID before the grace period expires |
Key files
| File | Why to read it |
|---|---|
| src/…/CopilotProvider.cs | Shows the provider boundary, session lifecycle, prompt context, tool setup, and event flow |
| src/…/CopilotSessionEventMapper.cs | Defines the provider-neutral events that the host and clients receive |
| src/…/ElsaCopilotToolFunction.cs | Shows how Copilot function calls return to the governed Elsa tool path |
| src/…/CopilotOptions.cs | Defines the runtime, authentication, model, and stream settings |
| src/…/CopilotAIFeature.cs | Shows how the Copilot adapter becomes a configurable platform feature |
| src/…/ServiceCollectionExtensions.cs | Shows the application-level registration entry point |
| src/…/AIOrchestrator.cs | Shows provider selection, tool governance, persistence, audit, and host stream handling |
| src/…/AIFeature.cs | Shows the host-level AI capability that owns platform-wide policy |
| src/…/Feature.cs | Shows the Studio link to the remote host AI feature |