Compare commits
4 Commits
fix/skillt
...
fix/provid
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5dbb71a44 | ||
|
|
b2cabdd950 | ||
|
|
139610950c | ||
|
|
65dd19cf87 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,3 +10,4 @@ GEMINI.md
|
|||||||
package-lock.json
|
package-lock.json
|
||||||
/.claude
|
/.claude
|
||||||
coverage/
|
coverage/
|
||||||
|
.worktrees/
|
||||||
|
|||||||
@@ -238,6 +238,7 @@ import { usePromptsFromClaudeInChrome } from 'src/hooks/usePromptsFromClaudeInCh
|
|||||||
import { getTipToShowOnSpinner, recordShownTip } from 'src/services/tips/tipScheduler.js';
|
import { getTipToShowOnSpinner, recordShownTip } from 'src/services/tips/tipScheduler.js';
|
||||||
import type { Theme } from 'src/utils/theme.js';
|
import type { Theme } from 'src/utils/theme.js';
|
||||||
import { isPromptTypingSuppressionActive } from './replInputSuppression.js';
|
import { isPromptTypingSuppressionActive } from './replInputSuppression.js';
|
||||||
|
import { shouldStartStartupChecks } from './replStartupGates.js';
|
||||||
import { checkAndDisableBypassPermissionsIfNeeded, checkAndDisableAutoModeIfNeeded, useKickOffCheckAndDisableBypassPermissionsIfNeeded, useKickOffCheckAndDisableAutoModeIfNeeded } from 'src/utils/permissions/bypassPermissionsKillswitch.js';
|
import { checkAndDisableBypassPermissionsIfNeeded, checkAndDisableAutoModeIfNeeded, useKickOffCheckAndDisableBypassPermissionsIfNeeded, useKickOffCheckAndDisableAutoModeIfNeeded } from 'src/utils/permissions/bypassPermissionsKillswitch.js';
|
||||||
import { SandboxManager } from 'src/utils/sandbox/sandbox-adapter.js';
|
import { SandboxManager } from 'src/utils/sandbox/sandbox-adapter.js';
|
||||||
import { SANDBOX_NETWORK_ACCESS_TOOL_NAME } from 'src/cli/structuredIO.js';
|
import { SANDBOX_NETWORK_ACCESS_TOOL_NAME } from 'src/cli/structuredIO.js';
|
||||||
@@ -784,19 +785,6 @@ export function REPL({
|
|||||||
});
|
});
|
||||||
const tasksV2 = useTasksV2WithCollapseEffect();
|
const tasksV2 = useTasksV2WithCollapseEffect();
|
||||||
|
|
||||||
// Start background plugin installations
|
|
||||||
|
|
||||||
// SECURITY: This code is guaranteed to run ONLY after the "trust this folder" dialog
|
|
||||||
// has been confirmed by the user. The trust dialog is shown in cli.tsx (line ~387)
|
|
||||||
// before the REPL component is rendered. The dialog blocks execution until the user
|
|
||||||
// accepts, and only then is the REPL component mounted and this effect runs.
|
|
||||||
// This ensures that plugin installations from repository and user settings only
|
|
||||||
// happen after explicit user consent to trust the current working directory.
|
|
||||||
useEffect(() => {
|
|
||||||
if (isRemoteSession) return;
|
|
||||||
void performStartupChecks(setAppState);
|
|
||||||
}, [setAppState, isRemoteSession]);
|
|
||||||
|
|
||||||
// Allow Claude in Chrome MCP to send prompts through MCP notifications
|
// Allow Claude in Chrome MCP to send prompts through MCP notifications
|
||||||
// and sync permission mode changes to the Chrome extension
|
// and sync permission mode changes to the Chrome extension
|
||||||
usePromptsFromClaudeInChrome(isRemoteSession ? EMPTY_MCP_CLIENTS : mcpClients, toolPermissionContext.mode);
|
usePromptsFromClaudeInChrome(isRemoteSession ? EMPTY_MCP_CLIENTS : mcpClients, toolPermissionContext.mode);
|
||||||
@@ -1337,6 +1325,7 @@ export function REPL({
|
|||||||
const [inputValue, setInputValueRaw] = useState(() => consumeEarlyInput());
|
const [inputValue, setInputValueRaw] = useState(() => consumeEarlyInput());
|
||||||
const inputValueRef = useRef(inputValue);
|
const inputValueRef = useRef(inputValue);
|
||||||
inputValueRef.current = inputValue;
|
inputValueRef.current = inputValue;
|
||||||
|
const startupChecksStartedRef = useRef(false);
|
||||||
const promptTypingSuppressionActive = isPromptTypingSuppressionActive(isPromptInputActive, inputValue);
|
const promptTypingSuppressionActive = isPromptTypingSuppressionActive(isPromptInputActive, inputValue);
|
||||||
const insertTextRef = useRef<{
|
const insertTextRef = useRef<{
|
||||||
insert: (text: string) => void;
|
insert: (text: string) => void;
|
||||||
@@ -1344,6 +1333,24 @@ export function REPL({
|
|||||||
cursorOffset: number;
|
cursorOffset: number;
|
||||||
} | null>(null);
|
} | null>(null);
|
||||||
|
|
||||||
|
// Start background plugin installations after the initial input window is idle.
|
||||||
|
// SECURITY: This still runs only after the "trust this folder" dialog has been
|
||||||
|
// confirmed because the REPL is not mounted until that dialog completes.
|
||||||
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
!shouldStartStartupChecks({
|
||||||
|
isRemoteSession,
|
||||||
|
promptTypingSuppressionActive,
|
||||||
|
startupChecksStarted: startupChecksStartedRef.current,
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
startupChecksStartedRef.current = true;
|
||||||
|
void performStartupChecks(setAppState);
|
||||||
|
}, [isRemoteSession, promptTypingSuppressionActive, setAppState]);
|
||||||
|
|
||||||
// Wrap setInputValue to co-locate suppression state updates.
|
// Wrap setInputValue to co-locate suppression state updates.
|
||||||
// Both setState calls happen in the same synchronous context so React
|
// Both setState calls happen in the same synchronous context so React
|
||||||
// batches them into a single render, eliminating the extra render that
|
// batches them into a single render, eliminating the extra render that
|
||||||
|
|||||||
44
src/screens/replStartupGates.test.ts
Normal file
44
src/screens/replStartupGates.test.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
import { shouldStartStartupChecks } from './replStartupGates.js'
|
||||||
|
|
||||||
|
describe('shouldStartStartupChecks', () => {
|
||||||
|
test('returns false for remote sessions', () => {
|
||||||
|
expect(
|
||||||
|
shouldStartStartupChecks({
|
||||||
|
isRemoteSession: true,
|
||||||
|
promptTypingSuppressionActive: false,
|
||||||
|
startupChecksStarted: false,
|
||||||
|
}),
|
||||||
|
).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns false while prompt typing suppression is active', () => {
|
||||||
|
expect(
|
||||||
|
shouldStartStartupChecks({
|
||||||
|
isRemoteSession: false,
|
||||||
|
promptTypingSuppressionActive: true,
|
||||||
|
startupChecksStarted: false,
|
||||||
|
}),
|
||||||
|
).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns true once local startup is idle and checks have not started', () => {
|
||||||
|
expect(
|
||||||
|
shouldStartStartupChecks({
|
||||||
|
isRemoteSession: false,
|
||||||
|
promptTypingSuppressionActive: false,
|
||||||
|
startupChecksStarted: false,
|
||||||
|
}),
|
||||||
|
).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns false after startup checks have already started', () => {
|
||||||
|
expect(
|
||||||
|
shouldStartStartupChecks({
|
||||||
|
isRemoteSession: false,
|
||||||
|
promptTypingSuppressionActive: false,
|
||||||
|
startupChecksStarted: true,
|
||||||
|
}),
|
||||||
|
).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
11
src/screens/replStartupGates.ts
Normal file
11
src/screens/replStartupGates.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export function shouldStartStartupChecks(options: {
|
||||||
|
isRemoteSession: boolean
|
||||||
|
promptTypingSuppressionActive: boolean
|
||||||
|
startupChecksStarted: boolean
|
||||||
|
}): boolean {
|
||||||
|
return (
|
||||||
|
!options.isRemoteSession &&
|
||||||
|
!options.promptTypingSuppressionActive &&
|
||||||
|
!options.startupChecksStarted
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import { describe, expect, test } from 'bun:test'
|
|
||||||
|
|
||||||
import { SkillTool } from '../../tools/SkillTool/SkillTool.js'
|
|
||||||
import {
|
|
||||||
getSchemaValidationErrorOverride,
|
|
||||||
getSchemaValidationToolUseResult,
|
|
||||||
} from './toolExecution.js'
|
|
||||||
|
|
||||||
describe('getSchemaValidationErrorOverride', () => {
|
|
||||||
test('returns actionable missing-skill error for SkillTool', () => {
|
|
||||||
expect(getSchemaValidationErrorOverride(SkillTool, {})).toBe(
|
|
||||||
'Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('does not override unrelated tool schema failures', () => {
|
|
||||||
expect(getSchemaValidationErrorOverride({ name: 'Read' } as never, {})).toBe(
|
|
||||||
null,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('does not override SkillTool when skill is present', () => {
|
|
||||||
expect(
|
|
||||||
getSchemaValidationErrorOverride(SkillTool, { skill: 'commit' }),
|
|
||||||
).toBe(null)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('uses the actionable override for structured toolUseResult too', () => {
|
|
||||||
expect(getSchemaValidationToolUseResult(SkillTool, {} as never)).toBe(
|
|
||||||
'InputValidationError: Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -43,7 +43,6 @@ import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js'
|
|||||||
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
||||||
import { NOTEBOOK_EDIT_TOOL_NAME } from '../../tools/NotebookEditTool/constants.js'
|
import { NOTEBOOK_EDIT_TOOL_NAME } from '../../tools/NotebookEditTool/constants.js'
|
||||||
import { POWERSHELL_TOOL_NAME } from '../../tools/PowerShellTool/toolName.js'
|
import { POWERSHELL_TOOL_NAME } from '../../tools/PowerShellTool/toolName.js'
|
||||||
import { SKILL_TOOL_NAME } from '../../tools/SkillTool/constants.js'
|
|
||||||
import { parseGitCommitId } from '../../tools/shared/gitOperationTracking.js'
|
import { parseGitCommitId } from '../../tools/shared/gitOperationTracking.js'
|
||||||
import {
|
import {
|
||||||
isDeferredTool,
|
isDeferredTool,
|
||||||
@@ -597,31 +596,6 @@ export function buildSchemaNotSentHint(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSchemaValidationErrorOverride(
|
|
||||||
tool: Tool,
|
|
||||||
input: unknown,
|
|
||||||
): string | null {
|
|
||||||
if (tool.name !== SKILL_TOOL_NAME || !input || typeof input !== 'object') {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const skill = (input as { skill?: unknown }).skill
|
|
||||||
if (skill === undefined || skill === null) {
|
|
||||||
return 'Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).'
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getSchemaValidationToolUseResult(
|
|
||||||
tool: Tool,
|
|
||||||
input: unknown,
|
|
||||||
fallbackMessage?: string,
|
|
||||||
): string {
|
|
||||||
const override = getSchemaValidationErrorOverride(tool, input)
|
|
||||||
return `InputValidationError: ${override ?? fallbackMessage ?? ''}`
|
|
||||||
}
|
|
||||||
|
|
||||||
async function checkPermissionsAndCallTool(
|
async function checkPermissionsAndCallTool(
|
||||||
tool: Tool,
|
tool: Tool,
|
||||||
toolUseID: string,
|
toolUseID: string,
|
||||||
@@ -640,9 +614,7 @@ async function checkPermissionsAndCallTool(
|
|||||||
// Validate input types with zod (surprisingly, the model is not great at generating valid input)
|
// Validate input types with zod (surprisingly, the model is not great at generating valid input)
|
||||||
const parsedInput = tool.inputSchema.safeParse(input)
|
const parsedInput = tool.inputSchema.safeParse(input)
|
||||||
if (!parsedInput.success) {
|
if (!parsedInput.success) {
|
||||||
const fallbackErrorContent = formatZodValidationError(tool.name, parsedInput.error)
|
let errorContent = formatZodValidationError(tool.name, parsedInput.error)
|
||||||
let errorContent =
|
|
||||||
getSchemaValidationErrorOverride(tool, input) ?? fallbackErrorContent
|
|
||||||
|
|
||||||
const schemaHint = buildSchemaNotSentHint(
|
const schemaHint = buildSchemaNotSentHint(
|
||||||
tool,
|
tool,
|
||||||
@@ -700,11 +672,7 @@ async function checkPermissionsAndCallTool(
|
|||||||
tool_use_id: toolUseID,
|
tool_use_id: toolUseID,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
toolUseResult: getSchemaValidationToolUseResult(
|
toolUseResult: `InputValidationError: ${parsedInput.error.message}`,
|
||||||
tool,
|
|
||||||
input,
|
|
||||||
parsedInput.error.message,
|
|
||||||
),
|
|
||||||
sourceToolAssistantUUID: assistantMessage.uuid,
|
sourceToolAssistantUUID: assistantMessage.uuid,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
import { describe, expect, test } from 'bun:test'
|
|
||||||
|
|
||||||
import { SkillTool } from './SkillTool.js'
|
|
||||||
|
|
||||||
describe('SkillTool missing parameter handling', () => {
|
|
||||||
test('missing skill stays required at the schema level', async () => {
|
|
||||||
const parsed = SkillTool.inputSchema.safeParse({})
|
|
||||||
|
|
||||||
expect(parsed.success).toBe(false)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('validateInput still returns an actionable error when called with missing skill', async () => {
|
|
||||||
const result = await SkillTool.validateInput?.({} as never, {
|
|
||||||
options: { tools: [] },
|
|
||||||
messages: [],
|
|
||||||
} as never)
|
|
||||||
|
|
||||||
expect(result).toEqual({
|
|
||||||
result: false,
|
|
||||||
message:
|
|
||||||
'Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
|
||||||
errorCode: 1,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('valid skill input still parses and validates', async () => {
|
|
||||||
const parsed = SkillTool.inputSchema.safeParse({ skill: 'commit' })
|
|
||||||
|
|
||||||
expect(parsed.success).toBe(true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -352,16 +352,6 @@ export const SkillTool: Tool<InputSchema, Output, Progress> = buildTool({
|
|||||||
toAutoClassifierInput: ({ skill }) => skill ?? '',
|
toAutoClassifierInput: ({ skill }) => skill ?? '',
|
||||||
|
|
||||||
async validateInput({ skill }, context): Promise<ValidationResult> {
|
async validateInput({ skill }, context): Promise<ValidationResult> {
|
||||||
if (!skill || typeof skill !== 'string') {
|
|
||||||
return {
|
|
||||||
result: false,
|
|
||||||
message:
|
|
||||||
'Missing skill name. Pass the slash command name as the skill parameter ' +
|
|
||||||
'(e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
|
||||||
errorCode: 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skills are just skill names, no arguments
|
// Skills are just skill names, no arguments
|
||||||
const trimmed = skill.trim()
|
const trimmed = skill.trim()
|
||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
@@ -444,7 +434,7 @@ export const SkillTool: Tool<InputSchema, Output, Progress> = buildTool({
|
|||||||
context,
|
context,
|
||||||
): Promise<PermissionDecision> {
|
): Promise<PermissionDecision> {
|
||||||
// Skills are just skill names, no arguments
|
// Skills are just skill names, no arguments
|
||||||
const trimmed = skill ?? ''
|
const trimmed = skill.trim()
|
||||||
|
|
||||||
// Remove leading slash if present (for compatibility)
|
// Remove leading slash if present (for compatibility)
|
||||||
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
||||||
@@ -602,7 +592,7 @@ export const SkillTool: Tool<InputSchema, Output, Progress> = buildTool({
|
|||||||
// - Skill is a prompt-based skill
|
// - Skill is a prompt-based skill
|
||||||
|
|
||||||
// Skills are just names, with optional arguments
|
// Skills are just names, with optional arguments
|
||||||
const trimmed = skill ?? ''
|
const trimmed = skill.trim()
|
||||||
|
|
||||||
// Remove leading slash if present (for compatibility)
|
// Remove leading slash if present (for compatibility)
|
||||||
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { expect, test } from 'bun:test'
|
import { expect, test } from 'bun:test'
|
||||||
import { z } from 'zod/v4'
|
import { z } from 'zod/v4'
|
||||||
import { getEmptyToolPermissionContext, type Tool, type Tools } from '../Tool.js'
|
import { getEmptyToolPermissionContext, type Tool, type Tools } from '../Tool.js'
|
||||||
import { SkillTool } from '../tools/SkillTool/SkillTool.js'
|
|
||||||
import { toolToAPISchema } from './api.js'
|
import { toolToAPISchema } from './api.js'
|
||||||
|
|
||||||
test('toolToAPISchema preserves provider-specific schema keywords in input_schema', async () => {
|
test('toolToAPISchema preserves provider-specific schema keywords in input_schema', async () => {
|
||||||
@@ -65,16 +64,3 @@ test('toolToAPISchema preserves provider-specific schema keywords in input_schem
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('toolToAPISchema keeps skill required for SkillTool', async () => {
|
|
||||||
const schema = await toolToAPISchema(SkillTool, {
|
|
||||||
getToolPermissionContext: async () => getEmptyToolPermissionContext(),
|
|
||||||
tools: [] as unknown as Tools,
|
|
||||||
agents: [],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect((schema as { input_schema: unknown }).input_schema).toMatchObject({
|
|
||||||
type: 'object',
|
|
||||||
required: ['skill'],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
} from './managedEnvConstants.js'
|
} from './managedEnvConstants.js'
|
||||||
import { clearMTLSCache } from './mtls.js'
|
import { clearMTLSCache } from './mtls.js'
|
||||||
import { clearProxyCache, configureGlobalAgents } from './proxy.js'
|
import { clearProxyCache, configureGlobalAgents } from './proxy.js'
|
||||||
|
import { filterSettingsEnvForExplicitProvider } from './providerEnvSelection.js'
|
||||||
import { applyActiveProviderProfileFromConfig } from './providerProfiles.js'
|
import { applyActiveProviderProfileFromConfig } from './providerProfiles.js'
|
||||||
import { isSettingSourceEnabled } from './settings/constants.js'
|
import { isSettingSourceEnabled } from './settings/constants.js'
|
||||||
import {
|
import {
|
||||||
@@ -87,7 +88,9 @@ function filterSettingsEnv(
|
|||||||
env: Record<string, string> | undefined,
|
env: Record<string, string> | undefined,
|
||||||
): Record<string, string> {
|
): Record<string, string> {
|
||||||
return withoutCcdSpawnEnvKeys(
|
return withoutCcdSpawnEnvKeys(
|
||||||
|
filterSettingsEnvForExplicitProvider(
|
||||||
withoutHostManagedProviderVars(withoutSSHTunnelVars(env)),
|
withoutHostManagedProviderVars(withoutSSHTunnelVars(env)),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
116
src/utils/providerEnvSelection.test.ts
Normal file
116
src/utils/providerEnvSelection.test.ts
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
||||||
|
import { filterSettingsEnvForExplicitProvider } from './providerEnvSelection.js'
|
||||||
|
|
||||||
|
const originalEnv = { ...process.env }
|
||||||
|
|
||||||
|
const RESET_KEYS = [
|
||||||
|
'CLAUDE_CODE_EXPLICIT_PROVIDER',
|
||||||
|
'CLAUDE_CODE_USE_OPENAI',
|
||||||
|
'CLAUDE_CODE_USE_GEMINI',
|
||||||
|
'CLAUDE_CODE_USE_GITHUB',
|
||||||
|
'CLAUDE_CODE_USE_BEDROCK',
|
||||||
|
'CLAUDE_CODE_USE_VERTEX',
|
||||||
|
'CLAUDE_CODE_USE_FOUNDRY',
|
||||||
|
] as const
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
for (const key of RESET_KEYS) {
|
||||||
|
delete process.env[key]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const key of RESET_KEYS) {
|
||||||
|
if (originalEnv[key] === undefined) delete process.env[key]
|
||||||
|
else process.env[key] = originalEnv[key]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('filterSettingsEnvForExplicitProvider', () => {
|
||||||
|
test('does not treat plain provider flags as an explicit CLI override', () => {
|
||||||
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
|
|
||||||
|
expect(
|
||||||
|
filterSettingsEnvForExplicitProvider({
|
||||||
|
CLAUDE_CODE_USE_OPENAI: '1',
|
||||||
|
OPENAI_MODEL: 'gpt-4o',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
}),
|
||||||
|
).toEqual({
|
||||||
|
CLAUDE_CODE_USE_OPENAI: '1',
|
||||||
|
OPENAI_MODEL: 'gpt-4o',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('strips settings-sourced provider flags when CLI provider is explicit', () => {
|
||||||
|
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'openai'
|
||||||
|
|
||||||
|
expect(
|
||||||
|
filterSettingsEnvForExplicitProvider({
|
||||||
|
CLAUDE_CODE_USE_GITHUB: '1',
|
||||||
|
CLAUDE_CODE_USE_OPENAI: '1',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
}),
|
||||||
|
).toEqual({ OTHER: 'keep-me' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('strips a stale GitHub model when explicit provider is not github', () => {
|
||||||
|
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'openai'
|
||||||
|
|
||||||
|
expect(
|
||||||
|
filterSettingsEnvForExplicitProvider({
|
||||||
|
OPENAI_MODEL: 'github:copilot',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
}),
|
||||||
|
).toEqual({ OTHER: 'keep-me' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('keeps a normal OpenAI model when explicit provider is openai', () => {
|
||||||
|
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'openai'
|
||||||
|
|
||||||
|
expect(
|
||||||
|
filterSettingsEnvForExplicitProvider({
|
||||||
|
OPENAI_MODEL: 'gpt-4o',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
}),
|
||||||
|
).toEqual({ OPENAI_MODEL: 'gpt-4o', OTHER: 'keep-me' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('strips a non-GitHub OpenAI model when explicit provider is github', () => {
|
||||||
|
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'github'
|
||||||
|
|
||||||
|
expect(
|
||||||
|
filterSettingsEnvForExplicitProvider({
|
||||||
|
OPENAI_MODEL: 'gpt-4o',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
}),
|
||||||
|
).toEqual({ OTHER: 'keep-me' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('preserves anthropic startup intent by stripping stale GitHub/OpenAI settings', () => {
|
||||||
|
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'anthropic'
|
||||||
|
|
||||||
|
expect(
|
||||||
|
filterSettingsEnvForExplicitProvider({
|
||||||
|
CLAUDE_CODE_USE_GITHUB: '1',
|
||||||
|
CLAUDE_CODE_USE_OPENAI: '1',
|
||||||
|
OPENAI_MODEL: 'github:copilot',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
}),
|
||||||
|
).toEqual({ OTHER: 'keep-me' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('preserves explicit ollama startup intent by stripping OpenAI routing settings', () => {
|
||||||
|
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'ollama'
|
||||||
|
|
||||||
|
expect(
|
||||||
|
filterSettingsEnvForExplicitProvider({
|
||||||
|
OPENAI_BASE_URL: 'https://api.openai.com/v1',
|
||||||
|
OPENAI_MODEL: 'gpt-4o',
|
||||||
|
OPENAI_API_KEY: 'sk-test',
|
||||||
|
OTHER: 'keep-me',
|
||||||
|
}),
|
||||||
|
).toEqual({ OTHER: 'keep-me' })
|
||||||
|
})
|
||||||
|
})
|
||||||
63
src/utils/providerEnvSelection.ts
Normal file
63
src/utils/providerEnvSelection.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
export const EXPLICIT_PROVIDER_ENV_VAR = 'CLAUDE_CODE_EXPLICIT_PROVIDER'
|
||||||
|
|
||||||
|
const PROVIDER_FLAG_KEYS = [
|
||||||
|
'CLAUDE_CODE_USE_OPENAI',
|
||||||
|
'CLAUDE_CODE_USE_GEMINI',
|
||||||
|
'CLAUDE_CODE_USE_GITHUB',
|
||||||
|
'CLAUDE_CODE_USE_BEDROCK',
|
||||||
|
'CLAUDE_CODE_USE_VERTEX',
|
||||||
|
'CLAUDE_CODE_USE_FOUNDRY',
|
||||||
|
] as const
|
||||||
|
|
||||||
|
export function clearProviderSelectionFlags(
|
||||||
|
env: NodeJS.ProcessEnv = process.env,
|
||||||
|
): void {
|
||||||
|
for (const key of PROVIDER_FLAG_KEYS) {
|
||||||
|
delete env[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getExplicitProvider(processEnv: NodeJS.ProcessEnv): string | undefined {
|
||||||
|
return processEnv[EXPLICIT_PROVIDER_ENV_VAR]?.trim() || undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
function isGithubModel(model: string | undefined): boolean {
|
||||||
|
return (model ?? '').trim().toLowerCase().startsWith('github:')
|
||||||
|
}
|
||||||
|
|
||||||
|
export function filterSettingsEnvForExplicitProvider(
|
||||||
|
env: Record<string, string> | undefined,
|
||||||
|
processEnv: NodeJS.ProcessEnv = process.env,
|
||||||
|
): Record<string, string> {
|
||||||
|
if (!env) return {}
|
||||||
|
|
||||||
|
const explicitProvider = getExplicitProvider(processEnv)
|
||||||
|
if (!explicitProvider) {
|
||||||
|
return env
|
||||||
|
}
|
||||||
|
|
||||||
|
const filtered = { ...env }
|
||||||
|
for (const key of PROVIDER_FLAG_KEYS) {
|
||||||
|
delete filtered[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (explicitProvider === 'ollama') {
|
||||||
|
delete filtered.OPENAI_BASE_URL
|
||||||
|
delete filtered.OPENAI_MODEL
|
||||||
|
delete filtered.OPENAI_API_KEY
|
||||||
|
return filtered
|
||||||
|
}
|
||||||
|
|
||||||
|
if (explicitProvider === 'github') {
|
||||||
|
if (!isGithubModel(filtered.OPENAI_MODEL)) {
|
||||||
|
delete filtered.OPENAI_MODEL
|
||||||
|
}
|
||||||
|
return filtered
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isGithubModel(filtered.OPENAI_MODEL)) {
|
||||||
|
delete filtered.OPENAI_MODEL
|
||||||
|
}
|
||||||
|
|
||||||
|
return filtered
|
||||||
|
}
|
||||||
@@ -9,11 +9,13 @@ import {
|
|||||||
const originalEnv = { ...process.env }
|
const originalEnv = { ...process.env }
|
||||||
|
|
||||||
const RESET_KEYS = [
|
const RESET_KEYS = [
|
||||||
|
'CLAUDE_CODE_EXPLICIT_PROVIDER',
|
||||||
'CLAUDE_CODE_USE_OPENAI',
|
'CLAUDE_CODE_USE_OPENAI',
|
||||||
'CLAUDE_CODE_USE_GEMINI',
|
'CLAUDE_CODE_USE_GEMINI',
|
||||||
'CLAUDE_CODE_USE_GITHUB',
|
'CLAUDE_CODE_USE_GITHUB',
|
||||||
'CLAUDE_CODE_USE_BEDROCK',
|
'CLAUDE_CODE_USE_BEDROCK',
|
||||||
'CLAUDE_CODE_USE_VERTEX',
|
'CLAUDE_CODE_USE_VERTEX',
|
||||||
|
'CLAUDE_CODE_USE_FOUNDRY',
|
||||||
'OPENAI_BASE_URL',
|
'OPENAI_BASE_URL',
|
||||||
'OPENAI_API_KEY',
|
'OPENAI_API_KEY',
|
||||||
'OPENAI_MODEL',
|
'OPENAI_MODEL',
|
||||||
@@ -83,6 +85,16 @@ describe('applyProviderFlag - openai', () => {
|
|||||||
applyProviderFlag('openai', ['--model', 'gpt-4o'])
|
applyProviderFlag('openai', ['--model', 'gpt-4o'])
|
||||||
expect(process.env.OPENAI_MODEL).toBe('gpt-4o')
|
expect(process.env.OPENAI_MODEL).toBe('gpt-4o')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('clears a previously persisted GitHub flag', () => {
|
||||||
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
|
|
||||||
|
const result = applyProviderFlag('openai', [])
|
||||||
|
|
||||||
|
expect(result.error).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBe('1')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('applyProviderFlag - gemini', () => {
|
describe('applyProviderFlag - gemini', () => {
|
||||||
@@ -104,6 +116,16 @@ describe('applyProviderFlag - github', () => {
|
|||||||
expect(result.error).toBeUndefined()
|
expect(result.error).toBeUndefined()
|
||||||
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBe('1')
|
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBe('1')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('clears a previously set OpenAI flag', () => {
|
||||||
|
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||||
|
|
||||||
|
const result = applyProviderFlag('github', [])
|
||||||
|
|
||||||
|
expect(result.error).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBe('1')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('applyProviderFlag - bedrock', () => {
|
describe('applyProviderFlag - bedrock', () => {
|
||||||
@@ -151,6 +173,19 @@ describe('applyProviderFlag - invalid provider', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('applyProviderFlag - anthropic', () => {
|
||||||
|
test('clears third-party provider flags', () => {
|
||||||
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
|
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||||
|
|
||||||
|
const result = applyProviderFlag('anthropic', [])
|
||||||
|
|
||||||
|
expect(result.error).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('applyProviderFlagFromArgs', () => {
|
describe('applyProviderFlagFromArgs', () => {
|
||||||
test('applies ollama provider and model from argv in one step', () => {
|
test('applies ollama provider and model from argv in one step', () => {
|
||||||
const result = applyProviderFlagFromArgs([
|
const result = applyProviderFlagFromArgs([
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
import {
|
||||||
|
clearProviderSelectionFlags,
|
||||||
|
EXPLICIT_PROVIDER_ENV_VAR,
|
||||||
|
} from './providerEnvSelection.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* --provider CLI flag support.
|
* --provider CLI flag support.
|
||||||
*
|
*
|
||||||
@@ -77,6 +82,9 @@ export function applyProviderFlag(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clearProviderSelectionFlags()
|
||||||
|
process.env[EXPLICIT_PROVIDER_ENV_VAR] = provider
|
||||||
|
|
||||||
const model = parseModelFlag(args)
|
const model = parseModelFlag(args)
|
||||||
|
|
||||||
switch (provider as ProviderFlagName) {
|
switch (provider as ProviderFlagName) {
|
||||||
|
|||||||
@@ -485,6 +485,26 @@ test('buildStartupEnvFromProfile leaves explicit provider selections untouched',
|
|||||||
assert.equal(env.OPENAI_API_KEY, undefined)
|
assert.equal(env.OPENAI_API_KEY, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('buildStartupEnvFromProfile preserves explicit anthropic startup selection', async () => {
|
||||||
|
const processEnv = {
|
||||||
|
CLAUDE_CODE_EXPLICIT_PROVIDER: 'anthropic',
|
||||||
|
}
|
||||||
|
|
||||||
|
const env = await buildStartupEnvFromProfile({
|
||||||
|
persisted: profile('openai', {
|
||||||
|
CLAUDE_CODE_USE_GITHUB: '1',
|
||||||
|
OPENAI_MODEL: 'github:copilot',
|
||||||
|
}),
|
||||||
|
processEnv,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.equal(env, processEnv)
|
||||||
|
assert.equal(env.CLAUDE_CODE_EXPLICIT_PROVIDER, 'anthropic')
|
||||||
|
assert.equal(env.CLAUDE_CODE_USE_OPENAI, undefined)
|
||||||
|
assert.equal(env.CLAUDE_CODE_USE_GITHUB, undefined)
|
||||||
|
assert.equal(env.OPENAI_MODEL, undefined)
|
||||||
|
})
|
||||||
|
|
||||||
test('buildStartupEnvFromProfile leaves profile-managed env untouched', async () => {
|
test('buildStartupEnvFromProfile leaves profile-managed env untouched', async () => {
|
||||||
const processEnv = {
|
const processEnv = {
|
||||||
CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED: '1',
|
CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED: '1',
|
||||||
|
|||||||
@@ -412,6 +412,10 @@ export function hasExplicitProviderSelection(
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (processEnv.CLAUDE_CODE_EXPLICIT_PROVIDER?.trim()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
processEnv.CLAUDE_CODE_USE_OPENAI !== undefined ||
|
processEnv.CLAUDE_CODE_USE_OPENAI !== undefined ||
|
||||||
processEnv.CLAUDE_CODE_USE_GITHUB !== undefined ||
|
processEnv.CLAUDE_CODE_USE_GITHUB !== undefined ||
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ async function importFreshProvidersModule() {
|
|||||||
const originalEnv = { ...process.env }
|
const originalEnv = { ...process.env }
|
||||||
|
|
||||||
const RESTORED_KEYS = [
|
const RESTORED_KEYS = [
|
||||||
|
'CLAUDE_CODE_EXPLICIT_PROVIDER',
|
||||||
'CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED',
|
'CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED',
|
||||||
'CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID',
|
'CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID',
|
||||||
'CLAUDE_CODE_USE_OPENAI',
|
'CLAUDE_CODE_USE_OPENAI',
|
||||||
@@ -142,6 +143,29 @@ describe('applyProviderProfileToProcessEnv', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('applyActiveProviderProfileFromConfig', () => {
|
describe('applyActiveProviderProfileFromConfig', () => {
|
||||||
|
test('does not override explicit anthropic startup selection', async () => {
|
||||||
|
const { applyActiveProviderProfileFromConfig } =
|
||||||
|
await importFreshProviderProfileModules()
|
||||||
|
process.env.CLAUDE_CODE_EXPLICIT_PROVIDER = 'anthropic'
|
||||||
|
|
||||||
|
const applied = applyActiveProviderProfileFromConfig({
|
||||||
|
providerProfiles: [
|
||||||
|
buildProfile({
|
||||||
|
id: 'saved_github',
|
||||||
|
baseUrl: 'https://api.githubcopilot.com',
|
||||||
|
model: 'github:copilot',
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
activeProviderProfileId: 'saved_github',
|
||||||
|
} as any)
|
||||||
|
|
||||||
|
expect(applied).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_EXPLICIT_PROVIDER).toBe('anthropic')
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
|
||||||
|
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
|
||||||
|
expect(process.env.OPENAI_MODEL).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
test('does not override explicit startup provider selection', async () => {
|
test('does not override explicit startup provider selection', async () => {
|
||||||
const { applyActiveProviderProfileFromConfig } =
|
const { applyActiveProviderProfileFromConfig } =
|
||||||
await importFreshProviderProfileModules()
|
await importFreshProviderProfileModules()
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
type ProviderProfile,
|
type ProviderProfile,
|
||||||
} from './config.js'
|
} from './config.js'
|
||||||
import type { ModelOption } from './model/modelOptions.js'
|
import type { ModelOption } from './model/modelOptions.js'
|
||||||
|
import { EXPLICIT_PROVIDER_ENV_VAR } from './providerEnvSelection.js'
|
||||||
|
|
||||||
export type ProviderPreset =
|
export type ProviderPreset =
|
||||||
| 'anthropic'
|
| 'anthropic'
|
||||||
@@ -256,6 +257,7 @@ function hasProviderSelectionFlags(
|
|||||||
processEnv: NodeJS.ProcessEnv = process.env,
|
processEnv: NodeJS.ProcessEnv = process.env,
|
||||||
): boolean {
|
): boolean {
|
||||||
return (
|
return (
|
||||||
|
processEnv[EXPLICIT_PROVIDER_ENV_VAR] !== undefined ||
|
||||||
processEnv.CLAUDE_CODE_USE_OPENAI !== undefined ||
|
processEnv.CLAUDE_CODE_USE_OPENAI !== undefined ||
|
||||||
processEnv.CLAUDE_CODE_USE_GEMINI !== undefined ||
|
processEnv.CLAUDE_CODE_USE_GEMINI !== undefined ||
|
||||||
processEnv.CLAUDE_CODE_USE_GITHUB !== undefined ||
|
processEnv.CLAUDE_CODE_USE_GITHUB !== undefined ||
|
||||||
|
|||||||
Reference in New Issue
Block a user