feat(cli): add --append-system-prompt-file support

This commit is contained in:
Yehor Baklanov committed 2026-04-13 06:37:19 +00:00
1 parent efecd7c7d9
commit 45c231a717
8 files changed
+382 -34

No files matched your search

+54 -12
View File
@@ -1,10 +1,21 @@
/**
* Claude launch argument helpers for first-class Image Analysis.
*
* Uses the same prompt injection mode as the user to avoid mixing
* `--append-system-prompt` and `--append-system-prompt-file` in one request.
*/
const APPEND_SYSTEM_PROMPT_FLAG = '--append-system-prompt';
const IMAGE_ANALYSIS_STEERING_PROMPT =
'For local image or PDF files, prefer the CCS MCP tool ImageAnalysis instead of Read. Use Read for text, code, and other plain files. If the user asks a specific question about the visual, pass that question as the focus field when useful. If ImageAnalysis is unavailable or fails, you may fall back to Read.';
import {
buildSteeringArg,
PROMPT_FLAG_INLINE,
PROMPT_FLAG_FILE,
} from '../prompt-injection-strategy';
const IMAGE_ANALYSIS_STEERING_PROMPT = {
name: 'ccs-prompt-image-analysis-tool',
content:
'For local image or PDF files, prefer the CCS MCP tool ImageAnalysis instead of Read. Use Read for text, code, and other plain files. If the user asks a specific question about the visual, pass that question as the focus field when useful. If ImageAnalysis is unavailable or fails, you may fall back to Read.',
};
function splitArgsAtTerminator(args: string[]): { optionArgs: string[]; trailingArgs: string[] } {
const terminatorIndex = args.indexOf('--');
@@ -26,15 +37,28 @@ function getImmediateFlagValue(args: string[], index: number): string | null {
return value;
}
function hasExactFlagValue(args: string[], flag: string, expectedValue: string): boolean {
function hasExactFlagValue(params: {
args: string[];
flag: string;
expectedValue: string;
allowPartiallyMatch?: boolean;
}): boolean {
const { args, flag, expectedValue, allowPartiallyMatch } = params;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === flag) {
const value = getImmediateFlagValue(args, index);
if (value === expectedValue) {
return true;
}
if (allowPartiallyMatch && value?.includes(expectedValue)) {
return true;
}
continue;
}
@@ -53,16 +77,34 @@ function hasExactFlagValue(args: string[], flag: string, expectedValue: string):
function ensureImageAnalysisSteeringPrompt(args: string[]): string[] {
const { optionArgs, trailingArgs } = splitArgsAtTerminator(args);
if (hasExactFlagValue(optionArgs, APPEND_SYSTEM_PROMPT_FLAG, IMAGE_ANALYSIS_STEERING_PROMPT)) {
if (
hasExactFlagValue({
args: optionArgs,
flag: PROMPT_FLAG_INLINE,
expectedValue: IMAGE_ANALYSIS_STEERING_PROMPT.content,
})
) {
return args;
}
return [
...optionArgs,
APPEND_SYSTEM_PROMPT_FLAG,
IMAGE_ANALYSIS_STEERING_PROMPT,
...trailingArgs,
];
if (
hasExactFlagValue({
args: optionArgs,
flag: PROMPT_FLAG_FILE,
expectedValue: IMAGE_ANALYSIS_STEERING_PROMPT.name,
allowPartiallyMatch: true,
})
) {
return args;
}
const steeringArg = buildSteeringArg({
args: optionArgs,
promptName: IMAGE_ANALYSIS_STEERING_PROMPT.name,
promptContent: IMAGE_ANALYSIS_STEERING_PROMPT.content,
});
return [...optionArgs, ...steeringArg, ...trailingArgs];
}
export function appendThirdPartyImageAnalysisToolArgs(args: string[]): string[] {
@@ -70,5 +112,5 @@ export function appendThirdPartyImageAnalysisToolArgs(args: string[]): string[]
}
export function getImageAnalysisSteeringPrompt(): string {
return IMAGE_ANALYSIS_STEERING_PROMPT;
return IMAGE_ANALYSIS_STEERING_PROMPT.content;
}
+92
View File
@@ -0,0 +1,92 @@
/**
* Shared prompt injection strategy.
*
* Detects which prompt injection mode the user is using and ensures CCS
* always uses the SAME mode so Claude CLI never receives mixed
* `--append-system-prompt` and `--append-system-prompt-file` flags.
*
* Rules:
* - User passes `--append-system-prompt` → all CCS prompts use inline
* - User passes `--append-system-prompt-file` → all CCS prompts use file
* - Neither present → default to inline (`--append-system-prompt`)
*/
import * as fs from 'fs';
import * as path from 'path';
import { getCcsDir } from './config-manager';
export type PromptInjectionMode = 'inline' | 'file';
/** `--append-system-prompt` — inline prompt text */
export const PROMPT_FLAG_INLINE = '--append-system-prompt';
/** `--append-system-prompt-file` — prompt read from file */
export const PROMPT_FLAG_FILE = '--append-system-prompt-file';
/**
* Detect which prompt injection mode to use based on user-provided args.
*
* - `--append-system-prompt-file` found (space or `=` form) → 'file'
* - `--append-system-prompt` found (space or `=` form) → 'inline'
* - Neither → 'inline' (default)
*/
export function detectPromptInjectionMode(args: string[]): PromptInjectionMode {
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === PROMPT_FLAG_FILE || arg.startsWith(`${PROMPT_FLAG_FILE}=`)) {
return 'file';
}
}
return 'inline';
}
/**
* Build a `--append-system-prompt <text>` arg pair.
*/
export function buildInlineSteeringArg(params: { promptContent: string }): string[] {
return [PROMPT_FLAG_INLINE, params.promptContent];
}
/**
* Build a `--append-system-prompt-file <path>` arg pair.
* Writes the prompt to a temp file first.
*/
export function buildFileSteeringArg(params: {
promptFileName: string;
promptContent: string;
}): string[] {
const ccsDir = getCcsDir();
const promptsFolder = path.join(ccsDir, '/prompts');
if (!fs.existsSync(promptsFolder)) {
fs.mkdirSync(promptsFolder, { recursive: true });
}
const promptFile = path.join(promptsFolder, params.promptFileName);
fs.writeFileSync(promptFile, params.promptContent);
return [PROMPT_FLAG_FILE, promptFile];
}
/**
* Build steering prompt args in the given mode.
*/
export function buildSteeringArg(params: {
args: string[];
promptName: string;
promptContent: string;
}): string[] {
const mode = detectPromptInjectionMode(params.args);
if (mode === 'file') {
return buildFileSteeringArg({
promptFileName: `${params.promptName}.txt`,
promptContent: params.promptContent,
});
}
return buildInlineSteeringArg({ promptContent: params.promptContent });
}
+51 -11
View File
@@ -1,12 +1,23 @@
/**
* Claude launch argument helpers for third-party WebSearch.
*
* Uses the same prompt injection mode as the user to avoid mixing
* `--append-system-prompt` and `--append-system-prompt-file` in one request.
*/
import {
buildSteeringArg,
PROMPT_FLAG_INLINE,
PROMPT_FLAG_FILE,
} from '../prompt-injection-strategy';
const NATIVE_WEBSEARCH_TOOL = 'WebSearch';
const DISALLOWED_TOOLS_FLAG = '--disallowedTools';
const APPEND_SYSTEM_PROMPT_FLAG = '--append-system-prompt';
const THIRD_PARTY_WEBSEARCH_STEERING_PROMPT =
'For web lookup or current-information requests, prefer the CCS MCP tool WebSearch instead of Bash/curl/http fetches. If the user explicitly wants shell commands, or WebSearch is unavailable or fails, you may fall back to Bash/network tools.';
export const THIRD_PARTY_WEBSEARCH_STEERING_PROMPT = {
name: 'ccs-prompt-websearch-tool',
content:
'For web lookup or current-information requests, prefer the CCS MCP tool WebSearch instead of Bash/curl/http fetches. If the user explicitly wants shell commands, or WebSearch is unavailable or fails, you may fall back to Bash/network tools.',
};
function parseToolValue(rawValue: string): string[] {
return rawValue
@@ -68,15 +79,28 @@ function hasToolInFlag(args: string[], flag: string, toolName: string): boolean
return false;
}
function hasExactFlagValue(args: string[], flag: string, expectedValue: string): boolean {
function hasExactFlagValue(params: {
args: string[];
flag: string;
expectedValue: string;
allowPartiallyMatch?: boolean;
}): boolean {
const { args, flag, expectedValue, allowPartiallyMatch } = params;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === flag) {
const value = getImmediateFlagValue(args, index);
if (value === expectedValue) {
return true;
}
if (allowPartiallyMatch && value?.includes(expectedValue)) {
return true;
}
continue;
}
@@ -135,17 +159,33 @@ function ensureWebSearchSteeringPrompt(args: string[]): string[] {
const { optionArgs, trailingArgs } = splitArgsAtTerminator(args);
if (
hasExactFlagValue(optionArgs, APPEND_SYSTEM_PROMPT_FLAG, THIRD_PARTY_WEBSEARCH_STEERING_PROMPT)
hasExactFlagValue({
args: optionArgs,
flag: PROMPT_FLAG_INLINE,
expectedValue: THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.content,
})
) {
return args;
}
return [
...optionArgs,
APPEND_SYSTEM_PROMPT_FLAG,
THIRD_PARTY_WEBSEARCH_STEERING_PROMPT,
...trailingArgs,
];
if (
hasExactFlagValue({
args: optionArgs,
flag: PROMPT_FLAG_FILE,
expectedValue: THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.name,
allowPartiallyMatch: true,
})
) {
return args;
}
const steeringArgs = buildSteeringArg({
args: optionArgs,
promptName: THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.name,
promptContent: THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.content,
});
return [...optionArgs, ...steeringArgs, ...trailingArgs];
}
export function appendThirdPartyWebSearchToolArgs(args: string[]): string[] {
+44 -10
View File
@@ -11,13 +11,12 @@ import * as os from 'os';
import * as path from 'path';
import { getCcsDir } from '../config-manager';
import { createLogger } from '../../services/logging';
import { PROMPT_FLAG_INLINE, PROMPT_FLAG_FILE } from '../prompt-injection-strategy';
import { THIRD_PARTY_WEBSEARCH_STEERING_PROMPT } from './claude-tool-args';
const TRACE_FILE_NAME = 'websearch-trace.jsonl';
const NATIVE_WEBSEARCH_TOOL = 'WebSearch';
const DISALLOWED_TOOLS_FLAG = '--disallowedTools';
const APPEND_SYSTEM_PROMPT_FLAG = '--append-system-prompt';
const THIRD_PARTY_WEBSEARCH_STEERING_PROMPT =
'For web lookup or current-information requests, prefer the CCS MCP tool WebSearch instead of Bash/curl/http fetches. If the user explicitly wants shell commands, or WebSearch is unavailable or fails, you may fall back to Bash/network tools.';
const logger = createLogger('websearch');
function parseToolValue(rawValue: string): string[] {
@@ -58,14 +57,28 @@ function hasToolInFlag(args: string[], flag: string, toolName: string): boolean
return false;
}
function hasExactFlagValue(args: string[], flag: string, expectedValue: string): boolean {
function hasExactFlagValue(params: {
args: string[];
flag: string;
expectedValue: string;
allowIncludesValue?: boolean;
}): boolean {
const { args, flag, expectedValue, allowIncludesValue } = params;
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (arg === flag) {
if (getImmediateFlagValue(args, index) === expectedValue) {
const immediateFlagValue = getImmediateFlagValue(args, index);
if (immediateFlagValue === expectedValue) {
return true;
}
if (allowIncludesValue && immediateFlagValue?.includes(expectedValue)) {
return true;
}
continue;
}
@@ -179,16 +192,37 @@ function buildLaunchId(): string {
return `websearch-${Date.now()}-${process.pid}-${random}`;
}
function hasSteeringPromptInArgs(args: string[]): boolean {
if (
hasExactFlagValue({
args,
flag: PROMPT_FLAG_INLINE,
expectedValue: THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.content,
})
) {
return true;
}
if (
hasExactFlagValue({
args,
flag: PROMPT_FLAG_FILE,
expectedValue: THIRD_PARTY_WEBSEARCH_STEERING_PROMPT.name,
allowIncludesValue: true,
})
) {
return true;
}
return false;
}
function summarizeLaunchArgs(args: string[]): Record<string, unknown> {
return {
argCount: args.length,
hasSettingsFlag: args.includes('--settings'),
nativeWebSearchDisallowed: hasToolInFlag(args, DISALLOWED_TOOLS_FLAG, NATIVE_WEBSEARCH_TOOL),
steeringPromptApplied: hasExactFlagValue(
args,
APPEND_SYSTEM_PROMPT_FLAG,
THIRD_PARTY_WEBSEARCH_STEERING_PROMPT
),
steeringPromptApplied: hasSteeringPromptInArgs(args),
};
}