MCPcopy
hub / github.com/KeygraphHQ/shannon / runClaudePrompt

Function runClaudePrompt

apps/worker/src/ai/claude-executor.ts:127–316  ·  view source on GitHub ↗
(
  prompt: string,
  sourceDir: string,
  context: string = '',
  description: string = 'Claude analysis',
  _agentName: string | null = null,
  auditSession: AuditSession | null = null,
  logger: ActivityLogger,
  modelTier: ModelTier = 'medium',
  outputFormat?: JsonSchemaOutputFormat,
  apiKey?: string,
  deliverablesSubdir?: string,
  providerConfig?: import('../types/config.js').ProviderConfig,
  mcpServers?: Record<string, import('@anthropic-ai/claude-agent-sdk').McpServerConfig>,
)

Source from the content-addressed store, hash-verified

125// Low-level SDK execution. Handles message streaming, progress, and audit logging.
126// Exported for Temporal activities to call single-attempt execution.
127export async function runClaudePrompt(
128 prompt: string,
129 sourceDir: string,
130 context: string = '',
131 description: string = 'Claude analysis',
132 _agentName: string | null = null,
133 auditSession: AuditSession | null = null,
134 logger: ActivityLogger,
135 modelTier: ModelTier = 'medium',
136 outputFormat?: JsonSchemaOutputFormat,
137 apiKey?: string,
138 deliverablesSubdir?: string,
139 providerConfig?: import('../types/config.js').ProviderConfig,
140 mcpServers?: Record<string, import('@anthropic-ai/claude-agent-sdk').McpServerConfig>,
141): Promise<ClaudePromptResult> {
142 // 1. Initialize timing and prompt
143 const timer = new Timer(`agent-${description.toLowerCase().replace(/\s+/g, '-')}`);
144 const fullPrompt = context ? `${context}\n\n${prompt}` : prompt;
145
146 // 2. Set up progress and audit infrastructure
147 const execContext = detectExecutionContext(description);
148 const progress = createProgressManager(
149 { description, useCleanOutput: execContext.useCleanOutput },
150 global.SHANNON_DISABLE_LOADER ?? false,
151 );
152 const auditLogger = createAuditLogger(auditSession);
153
154 logger.info(`Running Claude Code: ${description}...`);
155
156 // 3. Build env vars to pass to SDK subprocesses
157 const sdkEnv: Record<string, string> = {
158 CLAUDE_CODE_MAX_OUTPUT_TOKENS: process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS || '64000',
159 PLAYWRIGHT_MCP_OUTPUT_DIR: deliverablesSubdir
160 ? path.join(sourceDir, path.dirname(deliverablesSubdir), '.playwright-cli')
161 : path.join(sourceDir, '.shannon', '.playwright-cli'),
162 // apiKey from ContainerConfig takes precedence over process.env
163 ...(apiKey && { ANTHROPIC_API_KEY: apiKey }),
164 // Deliverables subdir for save-deliverable CLI tool
165 ...(deliverablesSubdir && { SHANNON_DELIVERABLES_SUBDIR: deliverablesSubdir }),
166 };
167
168 // 3a. Apply structured provider config directly to sdkEnv (no process.env mutation)
169 if (providerConfig) {
170 switch (providerConfig.providerType) {
171 case 'bedrock':
172 sdkEnv.CLAUDE_CODE_USE_BEDROCK = '1';
173 if (providerConfig.awsRegion) sdkEnv.AWS_REGION = providerConfig.awsRegion;
174 if (providerConfig.awsAccessKeyId) sdkEnv.AWS_ACCESS_KEY_ID = providerConfig.awsAccessKeyId;
175 if (providerConfig.awsSecretAccessKey) sdkEnv.AWS_SECRET_ACCESS_KEY = providerConfig.awsSecretAccessKey;
176 break;
177 case 'vertex':
178 sdkEnv.CLAUDE_CODE_USE_VERTEX = '1';
179 if (providerConfig.gcpRegion) sdkEnv.CLOUD_ML_REGION = providerConfig.gcpRegion;
180 if (providerConfig.gcpProjectId) sdkEnv.ANTHROPIC_VERTEX_PROJECT_ID = providerConfig.gcpProjectId;
181 if (providerConfig.gcpCredentialsPath)
182 sdkEnv.GOOGLE_APPLICATION_CREDENTIALS = providerConfig.gcpCredentialsPath;
183 break;
184 case 'litellm_router':

Callers 2

validateAuthenticationFunction · 0.85
executeMethod · 0.85

Calls 15

stopMethod · 0.95
detectExecutionContextFunction · 0.85
createProgressManagerFunction · 0.85
createAuditLoggerFunction · 0.85
resolveModelFunction · 0.85
supportsAdaptiveThinkingFunction · 0.85
processMessageStreamFunction · 0.85
isSpendingCapBehaviorFunction · 0.85
formatCompletionMessageFunction · 0.85
formatErrorOutputFunction · 0.85
isRetryableErrorFunction · 0.85
writeErrorLogFunction · 0.85

Tested by

no test coverage detected