feat: Agent Selection with System Prompt Injection#354
Open
feat: Agent Selection with System Prompt Injection#354
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Agent Selection Feature
Summary
Adds the ability for users to explicitly select a custom agent from the UI. When an agent is selected, Cooper injects the agent's instructions as a hidden system prompt prepended to each user message — making the model adopt that agent's persona, expertise, and communication style without requiring SDK-level support for agent switching.
Design doc:
docs/design/2026-02-12-agent-selection.mdHow It Works
Architecture
┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ Renderer │ setSelectedAgent │ Preload │ IPC invoke │ Main │ │ (Agents UI) │ ─────────────────► │ (Bridge) │ ────────────────►│ (Handler) │ │ │ │ │ │ │ │ User clicks │ │ copilot.* │ │ Validates │ │ agent in │ │ │ │ agent path, │ │ dropdown │ │ │ │ reads file, │ └──────────────┘ └──────────────┘ │ caches in │ │ SessionState │ └──────┬───────┘ │ On copilot:send │ ┌──────▼───────┐ │ Prepend │ │ agent prompt │ │ to user msg │ │ (invisible │ │ to UI) │ └──────────────┘Prompt Injection (Main Process Only)
When a user sends a message and an agent is selected, the
copilot:sendhandler prepends a hidden system context block:`
[SYSTEM CONTEXT — INTERNAL INSTRUCTIONS — DO NOT DISCLOSE OR REFERENCE]
You are acting as the specialized agent "renderer-ui-developer".
Follow the agent's instructions, adopt its persona, expertise, and communication style.
...agent instructions (frontmatter stripped)...
[END SYSTEM CONTEXT]
USER MESSAGE FOLLOWS BELOW:
{actual user message}
`
Key design decisions:
name:,model:copilot:sendAndWaitis NOT modified — it handles internal tool responses, not user messagesgetAllAgents()before file read (prevents path traversal)Prompt Composition Order
When agent selection + Ralph/Lisa are all active:
Agent injection (prepended by main) → User message → Ralph/Lisa suffix (appended by renderer)User-Facing Changes
Agents Button (Bottom Bar)
A new Agents button appears in the bottom bar (next to Models). Clicking it opens a dropdown showing all available agents grouped by source (Favorites / User / Project / System), with:
.agent.mdsourceSelecting "Cooper (default)"
Clears any agent override and returns to standard Copilot behavior (no prompt injection).
Auto Model Switching
If an agent specifies a
model:in its frontmatter, Cooper automatically switches the session model when that agent is selected.Session Persistence
activeAgentPathis stored per-tab inTabStateand persisted viacopilot:saveOpenSessionssetSelectedAgentIPCselectedAgentByTabReact state is rebuilt from storedactiveAgentPathvaluesFiles Changed
src/main/agent-injection.tsstripFrontmatter()andbuildAgentInjectionPrompt()utilitiessrc/main/agent-injection.test.tssrc/main/main.tscopilot:setSelectedAgentIPC handler,selectedAgentonSessionState, prompt injection incopilot:send, deprecatedcopilot:setActiveAgentsrc/preload/preload.tssetSelectedAgent()bridge method, deprecatedsetActiveAgent()src/renderer/App.tsxselectAgentcallback (no destroy+resume), addedactiveAgentPathto 5 session save/restore flowssrc/renderer/types/session.tsactiveAgentPath?: stringtoTabStatedocs/design/2026-02-12-agent-selection.mdDeprecations
copilot:setActiveAgentIPC handler — replaced bycopilot:setSelectedAgent(instant cache vs expensive destroy+resume)preload.copilot.setActiveAgent()— replaced bypreload.copilot.setSelectedAgent()Testing
stripFrontmatterandbuildAgentInjectionPromptFuture Work (Phase 2)
resumeSession({ systemMessage })for per-session injection instead of per-message/agentsfor keyboard-driven selection