MCPcopy Index your code
hub / github.com/coder/mux / buildSystemMessage

Function buildSystemMessage

src/node/services/systemMessage.ts:497–647  ·  view source on GitHub ↗
(
  metadata: WorkspaceMetadata,
  runtime: Runtime,
  workspacePath: string,
  additionalSystemInstructions?: string,
  modelString?: string,
  mcpServers?: MCPServerMap,
  options?: {
    /**
     * Resolved agent prompt as independently-authored sections (agent body,
     * subagent append_prompt, advisor guidance, …). Per-section so a trailing
     * scoped heading in one section cannot swallow the next section's text.
     */
    agentSystemPromptSections?: readonly string[];
    /**
     * Active mode identifiers used to extract "Mode: <mode>" sections from
     * Mux-dedicated instruction sources: the effective mode (plan/exec/compact)
     * plus the agent id, so "Mode: plan" covers custom plan-like agents and
     * "Mode: <agent>" covers per-agent sections. The first entry names the
     * injected <mode-...> tag. Duplicates are ignored.
     */
    modes?: readonly string[];
  }
)

Source from the content-addressed store, hash-verified

495 * @throws Error if metadata or workspacePath invalid
496 */
497export async function buildSystemMessage(
498 metadata: WorkspaceMetadata,
499 runtime: Runtime,
500 workspacePath: string,
501 additionalSystemInstructions?: string,
502 modelString?: string,
503 mcpServers?: MCPServerMap,
504 options?: {
505 /**
506 * Resolved agent prompt as independently-authored sections (agent body,
507 * subagent append_prompt, advisor guidance, …). Per-section so a trailing
508 * scoped heading in one section cannot swallow the next section's text.
509 */
510 agentSystemPromptSections?: readonly string[];
511 /**
512 * Active mode identifiers used to extract "Mode: <mode>" sections from
513 * Mux-dedicated instruction sources: the effective mode (plan/exec/compact)
514 * plus the agent id, so "Mode: plan" covers custom plan-like agents and
515 * "Mode: <agent>" covers per-agent sections. The first entry names the
516 * injected <mode-...> tag. Duplicates are ignored.
517 */
518 modes?: readonly string[];
519 }
520): Promise<string> {
521 if (!metadata) throw new Error("Invalid workspace metadata: metadata is required");
522 if (!workspacePath) throw new Error("Invalid workspace path: workspacePath is required");
523
524 // Read instruction sets
525 // Get runtime type from metadata (defaults to "local" for legacy workspaces without runtimeConfig)
526 const runtimeType = metadata.runtimeConfig?.type ?? "local";
527
528 // Build system message
529 let systemMessage = `${PRELUDE.trim()}\n\n${buildEnvironmentContext(
530 workspacePath,
531 runtimeType,
532 metadata.bestOf
533 )}`;
534
535 // Add MCP context if servers are configured
536 if (mcpServers && Object.keys(mcpServers).length > 0) {
537 systemMessage += buildMCPContext(mcpServers);
538 }
539
540 // NOTE: Agent skills and available sub-agents are now injected into their respective
541 // tool descriptions (agent_skill_read, task) for better model attention per Anthropic
542 // best practices. See tools.ts ToolConfiguration.availableSkills/availableSubagents.
543
544 // Read instruction sets
545 // Sub-project workspaces pass the execution path (root + subProject); fall
546 // back to the resolved root so the parent project's AGENTS.md is still read.
547 // For non-sub-project workspaces this is a no-op (root === execution path).
548 const workspaceRootPath = subProjectAwareWorkspaceRoot(metadata, runtime, workspacePath);
549 const instructionSources = await loadInstructionSources(metadata, runtime, workspaceRootPath);
550 // Mux-dedicated per-file contents (<dir>/.mux/AGENTS.md context files, then
551 // the global ~/.mux/AGENTS.md set, which is Mux-dedicated by construction).
552 // Scoped Model:/Mode: directives are honored ONLY in Mux-dedicated sources
553 // so a "Model: …" heading in a shared AGENTS.md (read by non-Mux agents too)
554 // stays ordinary markdown. Extraction runs per file: a scoped section at the

Callers 2

buildStreamSystemContextFunction · 0.90

Calls 9

extractModelSectionFunction · 0.90
extractModeSectionFunction · 0.90
buildEnvironmentContextFunction · 0.85
buildMCPContextFunction · 0.85
loadInstructionSourcesFunction · 0.85
buildTaggedSectionFunction · 0.85

Tested by

no test coverage detected