source dump of claude code
at main 95 lines 3.9 kB view raw
1// Critical system constants extracted to break circular dependencies 2 3import { feature } from 'bun:bundle' 4import { getFeatureValue_CACHED_MAY_BE_STALE } from '../services/analytics/growthbook.js' 5import { logForDebugging } from '../utils/debug.js' 6import { isEnvDefinedFalsy } from '../utils/envUtils.js' 7import { getAPIProvider } from '../utils/model/providers.js' 8import { getWorkload } from '../utils/workloadContext.js' 9 10const DEFAULT_PREFIX = `You are Claude Code, Anthropic's official CLI for Claude.` 11const AGENT_SDK_CLAUDE_CODE_PRESET_PREFIX = `You are Claude Code, Anthropic's official CLI for Claude, running within the Claude Agent SDK.` 12const AGENT_SDK_PREFIX = `You are a Claude agent, built on Anthropic's Claude Agent SDK.` 13 14const CLI_SYSPROMPT_PREFIX_VALUES = [ 15 DEFAULT_PREFIX, 16 AGENT_SDK_CLAUDE_CODE_PRESET_PREFIX, 17 AGENT_SDK_PREFIX, 18] as const 19 20export type CLISyspromptPrefix = (typeof CLI_SYSPROMPT_PREFIX_VALUES)[number] 21 22/** 23 * All possible CLI sysprompt prefix values, used by splitSysPromptPrefix 24 * to identify prefix blocks by content rather than position. 25 */ 26export const CLI_SYSPROMPT_PREFIXES: ReadonlySet<string> = new Set( 27 CLI_SYSPROMPT_PREFIX_VALUES, 28) 29 30export function getCLISyspromptPrefix(options?: { 31 isNonInteractive: boolean 32 hasAppendSystemPrompt: boolean 33}): CLISyspromptPrefix { 34 const apiProvider = getAPIProvider() 35 if (apiProvider === 'vertex') { 36 return DEFAULT_PREFIX 37 } 38 39 if (options?.isNonInteractive) { 40 if (options.hasAppendSystemPrompt) { 41 return AGENT_SDK_CLAUDE_CODE_PRESET_PREFIX 42 } 43 return AGENT_SDK_PREFIX 44 } 45 return DEFAULT_PREFIX 46} 47 48/** 49 * Check if attribution header is enabled. 50 * Enabled by default, can be disabled via env var or GrowthBook killswitch. 51 */ 52function isAttributionHeaderEnabled(): boolean { 53 if (isEnvDefinedFalsy(process.env.CLAUDE_CODE_ATTRIBUTION_HEADER)) { 54 return false 55 } 56 return getFeatureValue_CACHED_MAY_BE_STALE('tengu_attribution_header', true) 57} 58 59/** 60 * Get attribution header for API requests. 61 * Returns a header string with cc_version (including fingerprint) and cc_entrypoint. 62 * Enabled by default, can be disabled via env var or GrowthBook killswitch. 63 * 64 * When NATIVE_CLIENT_ATTESTATION is enabled, includes a `cch=00000` placeholder. 65 * Before the request is sent, Bun's native HTTP stack finds this placeholder 66 * in the request body and overwrites the zeros with a computed hash. The 67 * server verifies this token to confirm the request came from a real Claude 68 * Code client. See bun-anthropic/src/http/Attestation.zig for implementation. 69 * 70 * We use a placeholder (instead of injecting from Zig) because same-length 71 * replacement avoids Content-Length changes and buffer reallocation. 72 */ 73export function getAttributionHeader(fingerprint: string): string { 74 if (!isAttributionHeaderEnabled()) { 75 return '' 76 } 77 78 const version = `${MACRO.VERSION}.${fingerprint}` 79 const entrypoint = process.env.CLAUDE_CODE_ENTRYPOINT ?? 'unknown' 80 81 // cch=00000 placeholder is overwritten by Bun's HTTP stack with attestation token 82 const cch = feature('NATIVE_CLIENT_ATTESTATION') ? ' cch=00000;' : '' 83 // cc_workload: turn-scoped hint so the API can route e.g. cron-initiated 84 // requests to a lower QoS pool. Absent = interactive default. Safe re: 85 // fingerprint (computed from msg chars + version only, line 78 above) and 86 // cch attestation (placeholder overwritten in serialized body bytes after 87 // this string is built). Server _parse_cc_header tolerates unknown extra 88 // fields so old API deploys silently ignore this. 89 const workload = getWorkload() 90 const workloadPair = workload ? ` cc_workload=${workload};` : '' 91 const header = `x-anthropic-billing-header: cc_version=${version}; cc_entrypoint=${entrypoint};${cch}${workloadPair}` 92 93 logForDebugging(`attribution header ${header}`) 94 return header 95}