Skip to content

feat(core): support MIDSCENE_CUSTOM_SYSTEM_PROMPT env var for custom system prompt injection#2488

Open
will- wants to merge 3 commits into
web-infra-dev:mainfrom
will-:feat/custom-system-prompt
Open

feat(core): support MIDSCENE_CUSTOM_SYSTEM_PROMPT env var for custom system prompt injection#2488
will- wants to merge 3 commits into
web-infra-dev:mainfrom
will-:feat/custom-system-prompt

Conversation

@will-
Copy link
Copy Markdown

@will- will- commented May 18, 2026

Summary

  • Add MIDSCENE_CUSTOM_SYSTEM_PROMPT environment variable support that prepends a custom system prompt to all AI calls (planning, locate, extract, assert)
  • Enable private/self-hosted models to receive model-specific system instructions without code changes
  • The custom prompt is injected at the callAI() entry point, covering all downstream paths including CodexAppServer

Motivation

When using private or self-hosted models, some require specific system prompt instructions (e.g., role definitions, output format constraints) that differ from Midscene's built-in prompts. Currently there is no way to inject custom system-level instructions without modifying source code.

This change allows users to simply set an environment variable:

export MIDSCENE_CUSTOM_SYSTEM_PROMPT="You are a UI automation assistant. Always respond in valid JSON."

Changes

  • packages/shared/src/env/types.ts — Declare MIDSCENE_CUSTOM_SYSTEM_PROMPT constant and register in STRING_ENV_KEYS
  • packages/core/src/ai-model/service-caller/index.ts — Add applyCustomSystemPrompt() function and call it at callAI() entry point
  • packages/core/tests/unit-test/custom-system-prompt.test.ts — 8 unit tests for the pure function
  • packages/core/tests/unit-test/custom-system-prompt-integration.test.ts — 4 integration tests mocking OpenAI client to verify end-to-end behavior

Test plan

  • pnpm run lint — passes (no new lint errors)
  • Unit tests: vitest --run tests/unit-test/custom-system-prompt.test.ts — 8 passed
  • Integration tests: vitest --run tests/unit-test/custom-system-prompt-integration.test.ts — 4 passed
  • Full core test suite: 67 test files, 846 tests passed, 0 failures

🤖 Generated with Claude Code

willyu and others added 2 commits May 18, 2026 16:33
…system prompt injection

Allow private/self-hosted models to receive model-specific system prompt
instructions via the MIDSCENE_CUSTOM_SYSTEM_PROMPT environment variable.
When set, the value is prepended to all system messages across all AI
call paths (planning, locate, extract, assert).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Mock the OpenAI client and verify that the custom system prompt is
actually prepended to system messages in the API call payload, covering
the full callAI → OpenAI path with multimodal and multi-intent scenarios.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…EM_PROMPT_EXTRA

Rename the env var to better communicate its prepend behavior —
"extra" makes it clear that this is additional content prepended
to the existing system prompt, not a replacement.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant