Compare commits
18 Commits
fix/383-ba
...
fix/363-st
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af5bb8fed8 | ||
|
|
ad76b1174a | ||
|
|
c457d9db3c | ||
|
|
d1f79088a1 | ||
|
|
106f85d0bf | ||
|
|
3188f6ac66 | ||
|
|
69ea1f1e4a | ||
|
|
f9ce81bfb3 | ||
|
|
4975cfc2e0 | ||
|
|
600c01faf7 | ||
|
|
b07bafa5bd | ||
|
|
85aa8b0985 | ||
|
|
e365cb4010 | ||
|
|
52d33a87a0 | ||
|
|
b4bd95b477 | ||
|
|
1e057025d6 | ||
|
|
aff2bd87e4 | ||
|
|
72e6a945fe |
@@ -52,7 +52,11 @@ async function renderFinalFrame(node: React.ReactNode): Promise<string> {
|
|||||||
patchConsole: false,
|
patchConsole: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
await instance.waitUntilExit()
|
// Timeout guard: if render throws before exit effect fires, don't hang
|
||||||
|
await Promise.race([
|
||||||
|
instance.waitUntilExit(),
|
||||||
|
new Promise<void>(resolve => setTimeout(resolve, 3000)),
|
||||||
|
])
|
||||||
return stripAnsi(extractLastFrame(getOutput()))
|
return stripAnsi(extractLastFrame(getOutput()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
305
src/components/ProviderManager.test.tsx
Normal file
305
src/components/ProviderManager.test.tsx
Normal file
@@ -0,0 +1,305 @@
|
|||||||
|
import { PassThrough } from 'node:stream'
|
||||||
|
|
||||||
|
import { afterEach, expect, mock, test } from 'bun:test'
|
||||||
|
import React from 'react'
|
||||||
|
import stripAnsi from 'strip-ansi'
|
||||||
|
|
||||||
|
import { createRoot } from '../ink.js'
|
||||||
|
import { AppStateProvider } from '../state/AppState.js'
|
||||||
|
|
||||||
|
const SYNC_START = '\x1B[?2026h'
|
||||||
|
const SYNC_END = '\x1B[?2026l'
|
||||||
|
|
||||||
|
const ORIGINAL_ENV = {
|
||||||
|
CLAUDE_CODE_USE_GITHUB: process.env.CLAUDE_CODE_USE_GITHUB,
|
||||||
|
GITHUB_TOKEN: process.env.GITHUB_TOKEN,
|
||||||
|
GH_TOKEN: process.env.GH_TOKEN,
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractLastFrame(output: string): string {
|
||||||
|
let lastFrame: string | null = null
|
||||||
|
let cursor = 0
|
||||||
|
|
||||||
|
while (cursor < output.length) {
|
||||||
|
const start = output.indexOf(SYNC_START, cursor)
|
||||||
|
if (start === -1) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
const contentStart = start + SYNC_START.length
|
||||||
|
const end = output.indexOf(SYNC_END, contentStart)
|
||||||
|
if (end === -1) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
const frame = output.slice(contentStart, end)
|
||||||
|
if (frame.trim().length > 0) {
|
||||||
|
lastFrame = frame
|
||||||
|
}
|
||||||
|
cursor = end + SYNC_END.length
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastFrame ?? output
|
||||||
|
}
|
||||||
|
|
||||||
|
function createTestStreams(): {
|
||||||
|
stdout: PassThrough
|
||||||
|
stdin: PassThrough & {
|
||||||
|
isTTY: boolean
|
||||||
|
setRawMode: (mode: boolean) => void
|
||||||
|
ref: () => void
|
||||||
|
unref: () => void
|
||||||
|
}
|
||||||
|
getOutput: () => string
|
||||||
|
} {
|
||||||
|
let output = ''
|
||||||
|
const stdout = new PassThrough()
|
||||||
|
const stdin = new PassThrough() as PassThrough & {
|
||||||
|
isTTY: boolean
|
||||||
|
setRawMode: (mode: boolean) => void
|
||||||
|
ref: () => void
|
||||||
|
unref: () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
stdin.isTTY = true
|
||||||
|
stdin.setRawMode = () => {}
|
||||||
|
stdin.ref = () => {}
|
||||||
|
stdin.unref = () => {}
|
||||||
|
;(stdout as unknown as { columns: number }).columns = 120
|
||||||
|
stdout.on('data', chunk => {
|
||||||
|
output += chunk.toString()
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
stdout,
|
||||||
|
stdin,
|
||||||
|
getOutput: () => output,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function waitForCondition(
|
||||||
|
predicate: () => boolean,
|
||||||
|
options?: { timeoutMs?: number; intervalMs?: number },
|
||||||
|
): Promise<void> {
|
||||||
|
const timeoutMs = options?.timeoutMs ?? 2000
|
||||||
|
const intervalMs = options?.intervalMs ?? 10
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
|
while (Date.now() - startedAt < timeoutMs) {
|
||||||
|
if (predicate()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
await Bun.sleep(intervalMs)
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('Timed out waiting for ProviderManager test condition')
|
||||||
|
}
|
||||||
|
|
||||||
|
function createDeferred<T>(): {
|
||||||
|
promise: Promise<T>
|
||||||
|
resolve: (value: T) => void
|
||||||
|
} {
|
||||||
|
let resolve!: (value: T) => void
|
||||||
|
const promise = new Promise<T>(r => {
|
||||||
|
resolve = r
|
||||||
|
})
|
||||||
|
return { promise, resolve }
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockProviderProfilesModule(): void {
|
||||||
|
mock.module('../utils/providerProfiles.js', () => ({
|
||||||
|
addProviderProfile: () => null,
|
||||||
|
applyActiveProviderProfileFromConfig: () => {},
|
||||||
|
deleteProviderProfile: () => ({ removed: false, activeProfileId: null }),
|
||||||
|
getActiveProviderProfile: () => null,
|
||||||
|
getProviderPresetDefaults: () => ({
|
||||||
|
provider: 'openai',
|
||||||
|
name: 'Mock provider',
|
||||||
|
baseUrl: 'http://localhost:11434/v1',
|
||||||
|
model: 'mock-model',
|
||||||
|
apiKey: '',
|
||||||
|
}),
|
||||||
|
getProviderProfiles: () => [],
|
||||||
|
setActiveProviderProfile: () => null,
|
||||||
|
updateProviderProfile: () => null,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
function mockProviderManagerDependencies(
|
||||||
|
syncRead: () => string | undefined,
|
||||||
|
asyncRead: () => Promise<string | undefined>,
|
||||||
|
): void {
|
||||||
|
mockProviderProfilesModule()
|
||||||
|
|
||||||
|
mock.module('../utils/githubModelsCredentials.js', () => ({
|
||||||
|
clearGithubModelsToken: () => ({ success: true }),
|
||||||
|
GITHUB_MODELS_HYDRATED_ENV_MARKER: 'CLAUDE_CODE_GITHUB_TOKEN_HYDRATED',
|
||||||
|
hydrateGithubModelsTokenFromSecureStorage: () => {},
|
||||||
|
readGithubModelsToken: syncRead,
|
||||||
|
readGithubModelsTokenAsync: asyncRead,
|
||||||
|
}))
|
||||||
|
|
||||||
|
mock.module('../utils/settings/settings.js', () => ({
|
||||||
|
updateSettingsForSource: () => ({ error: null }),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async function waitForFrameOutput(
|
||||||
|
getOutput: () => string,
|
||||||
|
predicate: (output: string) => boolean,
|
||||||
|
timeoutMs = 2500,
|
||||||
|
): Promise<string> {
|
||||||
|
let output = ''
|
||||||
|
|
||||||
|
await waitForCondition(() => {
|
||||||
|
output = stripAnsi(extractLastFrame(getOutput()))
|
||||||
|
return predicate(output)
|
||||||
|
}, { timeoutMs })
|
||||||
|
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mountProviderManager(
|
||||||
|
ProviderManager: React.ComponentType<{
|
||||||
|
mode: 'first-run' | 'manage'
|
||||||
|
onDone: () => void
|
||||||
|
}>,
|
||||||
|
): Promise<{
|
||||||
|
getOutput: () => string
|
||||||
|
dispose: () => Promise<void>
|
||||||
|
}> {
|
||||||
|
const { stdout, stdin, getOutput } = createTestStreams()
|
||||||
|
const root = await createRoot({
|
||||||
|
stdout: stdout as unknown as NodeJS.WriteStream,
|
||||||
|
stdin: stdin as unknown as NodeJS.ReadStream,
|
||||||
|
patchConsole: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
root.render(
|
||||||
|
<AppStateProvider>
|
||||||
|
<ProviderManager
|
||||||
|
mode="manage"
|
||||||
|
onDone={() => {}}
|
||||||
|
/>
|
||||||
|
</AppStateProvider>,
|
||||||
|
)
|
||||||
|
|
||||||
|
return {
|
||||||
|
getOutput,
|
||||||
|
dispose: async () => {
|
||||||
|
root.unmount()
|
||||||
|
stdin.end()
|
||||||
|
stdout.end()
|
||||||
|
await Bun.sleep(0)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function renderProviderManagerFrame(
|
||||||
|
ProviderManager: React.ComponentType<{
|
||||||
|
mode: 'first-run' | 'manage'
|
||||||
|
onDone: () => void
|
||||||
|
}>,
|
||||||
|
options?: {
|
||||||
|
waitForOutput?: (output: string) => boolean
|
||||||
|
timeoutMs?: number
|
||||||
|
},
|
||||||
|
): Promise<string> {
|
||||||
|
const mounted = await mountProviderManager(ProviderManager)
|
||||||
|
const output = await waitForFrameOutput(
|
||||||
|
mounted.getOutput,
|
||||||
|
frame => {
|
||||||
|
if (!options?.waitForOutput) {
|
||||||
|
return frame.includes('Provider manager')
|
||||||
|
}
|
||||||
|
return options.waitForOutput(frame)
|
||||||
|
},
|
||||||
|
options?.timeoutMs ?? 2500,
|
||||||
|
)
|
||||||
|
|
||||||
|
await mounted.dispose()
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
mock.restore()
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(ORIGINAL_ENV)) {
|
||||||
|
if (value === undefined) {
|
||||||
|
delete process.env[key as keyof typeof ORIGINAL_ENV]
|
||||||
|
} else {
|
||||||
|
process.env[key as keyof typeof ORIGINAL_ENV] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test('ProviderManager resolves GitHub virtual provider from async storage without sync reads in render flow', async () => {
|
||||||
|
delete process.env.CLAUDE_CODE_USE_GITHUB
|
||||||
|
delete process.env.GITHUB_TOKEN
|
||||||
|
delete process.env.GH_TOKEN
|
||||||
|
|
||||||
|
const syncRead = mock(() => {
|
||||||
|
throw new Error('sync credential read should not run in ProviderManager render flow')
|
||||||
|
})
|
||||||
|
const asyncRead = mock(async () => 'stored-token')
|
||||||
|
|
||||||
|
mockProviderManagerDependencies(syncRead, asyncRead)
|
||||||
|
|
||||||
|
const nonce = `${Date.now()}-${Math.random()}`
|
||||||
|
const { ProviderManager } = await import(`./ProviderManager.js?ts=${nonce}`)
|
||||||
|
const output = await renderProviderManagerFrame(ProviderManager, {
|
||||||
|
waitForOutput: frame =>
|
||||||
|
frame.includes('Provider manager') &&
|
||||||
|
frame.includes('GitHub Models') &&
|
||||||
|
frame.includes('token stored'),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(output).toContain('Provider manager')
|
||||||
|
expect(output).toContain('GitHub Models')
|
||||||
|
expect(output).toContain('token stored')
|
||||||
|
expect(output).not.toContain('No provider profiles configured yet.')
|
||||||
|
|
||||||
|
expect(syncRead).not.toHaveBeenCalled()
|
||||||
|
expect(asyncRead).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('ProviderManager avoids first-frame false negative while stored-token lookup is pending', async () => {
|
||||||
|
delete process.env.CLAUDE_CODE_USE_GITHUB
|
||||||
|
delete process.env.GITHUB_TOKEN
|
||||||
|
delete process.env.GH_TOKEN
|
||||||
|
|
||||||
|
const syncRead = mock(() => {
|
||||||
|
throw new Error('sync credential read should not run in ProviderManager render flow')
|
||||||
|
})
|
||||||
|
const deferredStoredToken = createDeferred<string | undefined>()
|
||||||
|
const asyncRead = mock(async () => deferredStoredToken.promise)
|
||||||
|
|
||||||
|
mockProviderManagerDependencies(syncRead, asyncRead)
|
||||||
|
|
||||||
|
const nonce = `${Date.now()}-${Math.random()}`
|
||||||
|
const { ProviderManager } = await import(`./ProviderManager.js?ts=${nonce}`)
|
||||||
|
const mounted = await mountProviderManager(ProviderManager)
|
||||||
|
|
||||||
|
const firstFrame = await waitForFrameOutput(
|
||||||
|
mounted.getOutput,
|
||||||
|
frame => frame.includes('Provider manager'),
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(firstFrame).toContain('Checking GitHub Models credentials...')
|
||||||
|
expect(firstFrame).not.toContain('No provider profiles configured yet.')
|
||||||
|
|
||||||
|
deferredStoredToken.resolve('stored-token')
|
||||||
|
|
||||||
|
const resolvedFrame = await waitForFrameOutput(
|
||||||
|
mounted.getOutput,
|
||||||
|
frame => frame.includes('GitHub Models') && frame.includes('token stored'),
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(resolvedFrame).toContain('GitHub Models')
|
||||||
|
expect(resolvedFrame).toContain('token stored')
|
||||||
|
|
||||||
|
await mounted.dispose()
|
||||||
|
|
||||||
|
expect(syncRead).not.toHaveBeenCalled()
|
||||||
|
expect(asyncRead).toHaveBeenCalled()
|
||||||
|
})
|
||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
GITHUB_MODELS_HYDRATED_ENV_MARKER,
|
GITHUB_MODELS_HYDRATED_ENV_MARKER,
|
||||||
hydrateGithubModelsTokenFromSecureStorage,
|
hydrateGithubModelsTokenFromSecureStorage,
|
||||||
readGithubModelsToken,
|
readGithubModelsToken,
|
||||||
|
readGithubModelsTokenAsync,
|
||||||
} from '../utils/githubModelsCredentials.js'
|
} from '../utils/githubModelsCredentials.js'
|
||||||
import { isEnvTruthy } from '../utils/envUtils.js'
|
import { isEnvTruthy } from '../utils/envUtils.js'
|
||||||
import { updateSettingsForSource } from '../utils/settings/settings.js'
|
import { updateSettingsForSource } from '../utils/settings/settings.js'
|
||||||
@@ -118,25 +119,38 @@ function profileSummary(profile: ProviderProfile, isActive: boolean): string {
|
|||||||
return `${providerKind} · ${profile.baseUrl} · ${profile.model} · ${keyInfo}${activeSuffix}`
|
return `${providerKind} · ${profile.baseUrl} · ${profile.model} · ${keyInfo}${activeSuffix}`
|
||||||
}
|
}
|
||||||
|
|
||||||
function getGithubCredentialSource(
|
function getGithubCredentialSourceFromEnv(
|
||||||
processEnv: NodeJS.ProcessEnv = process.env,
|
processEnv: NodeJS.ProcessEnv = process.env,
|
||||||
): GithubCredentialSource {
|
): GithubCredentialSource {
|
||||||
if (readGithubModelsToken()?.trim()) {
|
|
||||||
return 'stored'
|
|
||||||
}
|
|
||||||
if (processEnv.GITHUB_TOKEN?.trim() || processEnv.GH_TOKEN?.trim()) {
|
if (processEnv.GITHUB_TOKEN?.trim() || processEnv.GH_TOKEN?.trim()) {
|
||||||
return 'env'
|
return 'env'
|
||||||
}
|
}
|
||||||
return 'none'
|
return 'none'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveGithubCredentialSource(
|
||||||
|
processEnv: NodeJS.ProcessEnv = process.env,
|
||||||
|
): Promise<GithubCredentialSource> {
|
||||||
|
const envSource = getGithubCredentialSourceFromEnv(processEnv)
|
||||||
|
if (envSource !== 'none') {
|
||||||
|
return envSource
|
||||||
|
}
|
||||||
|
|
||||||
|
if (await readGithubModelsTokenAsync()) {
|
||||||
|
return 'stored'
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'none'
|
||||||
|
}
|
||||||
|
|
||||||
function isGithubProviderAvailable(
|
function isGithubProviderAvailable(
|
||||||
|
credentialSource: GithubCredentialSource,
|
||||||
processEnv: NodeJS.ProcessEnv = process.env,
|
processEnv: NodeJS.ProcessEnv = process.env,
|
||||||
): boolean {
|
): boolean {
|
||||||
if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_GITHUB)) {
|
if (isEnvTruthy(processEnv.CLAUDE_CODE_USE_GITHUB)) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return getGithubCredentialSource(processEnv) !== 'none'
|
return credentialSource !== 'none'
|
||||||
}
|
}
|
||||||
|
|
||||||
function getGithubProviderModel(
|
function getGithubProviderModel(
|
||||||
@@ -164,19 +178,24 @@ function getGithubProviderSummary(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function ProviderManager({ mode, onDone }: Props): React.ReactNode {
|
export function ProviderManager({ mode, onDone }: Props): React.ReactNode {
|
||||||
|
const initialGithubCredentialSource = getGithubCredentialSourceFromEnv()
|
||||||
|
const initialIsGithubActive = isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
||||||
|
const initialHasGithubCredential = initialGithubCredentialSource !== 'none'
|
||||||
|
|
||||||
const [profiles, setProfiles] = React.useState(() => getProviderProfiles())
|
const [profiles, setProfiles] = React.useState(() => getProviderProfiles())
|
||||||
const [activeProfileId, setActiveProfileId] = React.useState(
|
const [activeProfileId, setActiveProfileId] = React.useState(
|
||||||
() => getActiveProviderProfile()?.id,
|
() => getActiveProviderProfile()?.id,
|
||||||
)
|
)
|
||||||
const [githubProviderAvailable, setGithubProviderAvailable] = React.useState(() =>
|
const [githubProviderAvailable, setGithubProviderAvailable] = React.useState(
|
||||||
isGithubProviderAvailable(),
|
() => isGithubProviderAvailable(initialGithubCredentialSource),
|
||||||
)
|
)
|
||||||
const [githubCredentialSource, setGithubCredentialSource] = React.useState<GithubCredentialSource>(
|
const [githubCredentialSource, setGithubCredentialSource] = React.useState<GithubCredentialSource>(
|
||||||
() => getGithubCredentialSource(),
|
() => initialGithubCredentialSource,
|
||||||
)
|
|
||||||
const [isGithubActive, setIsGithubActive] = React.useState(() =>
|
|
||||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB),
|
|
||||||
)
|
)
|
||||||
|
const [isGithubActive, setIsGithubActive] = React.useState(() => initialIsGithubActive)
|
||||||
|
const [isGithubCredentialSourceResolved, setIsGithubCredentialSourceResolved] =
|
||||||
|
React.useState(() => initialHasGithubCredential || initialIsGithubActive)
|
||||||
|
const githubRefreshEpochRef = React.useRef(0)
|
||||||
const [screen, setScreen] = React.useState<Screen>(
|
const [screen, setScreen] = React.useState<Screen>(
|
||||||
mode === 'first-run' ? 'select-preset' : 'menu',
|
mode === 'first-run' ? 'select-preset' : 'menu',
|
||||||
)
|
)
|
||||||
@@ -196,13 +215,48 @@ export function ProviderManager({ mode, onDone }: Props): React.ReactNode {
|
|||||||
const currentStepKey = currentStep.key
|
const currentStepKey = currentStep.key
|
||||||
const currentValue = draft[currentStepKey]
|
const currentValue = draft[currentStepKey]
|
||||||
|
|
||||||
|
const refreshGithubProviderState = React.useCallback((): void => {
|
||||||
|
const envCredentialSource = getGithubCredentialSourceFromEnv()
|
||||||
|
const githubActive = isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
||||||
|
const canResolveFromEnv = githubActive || envCredentialSource !== 'none'
|
||||||
|
|
||||||
|
if (canResolveFromEnv) {
|
||||||
|
githubRefreshEpochRef.current += 1
|
||||||
|
setGithubCredentialSource(envCredentialSource)
|
||||||
|
setGithubProviderAvailable(isGithubProviderAvailable(envCredentialSource))
|
||||||
|
setIsGithubActive(githubActive)
|
||||||
|
setIsGithubCredentialSourceResolved(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsGithubCredentialSourceResolved(false)
|
||||||
|
const refreshEpoch = ++githubRefreshEpochRef.current
|
||||||
|
void (async () => {
|
||||||
|
const credentialSource = await resolveGithubCredentialSource()
|
||||||
|
if (refreshEpoch !== githubRefreshEpochRef.current) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setGithubCredentialSource(credentialSource)
|
||||||
|
setGithubProviderAvailable(isGithubProviderAvailable(credentialSource))
|
||||||
|
setIsGithubActive(isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB))
|
||||||
|
setIsGithubCredentialSourceResolved(true)
|
||||||
|
})()
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
refreshGithubProviderState()
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
githubRefreshEpochRef.current += 1
|
||||||
|
}
|
||||||
|
}, [refreshGithubProviderState])
|
||||||
|
|
||||||
function refreshProfiles(): void {
|
function refreshProfiles(): void {
|
||||||
const nextProfiles = getProviderProfiles()
|
const nextProfiles = getProviderProfiles()
|
||||||
setProfiles(nextProfiles)
|
setProfiles(nextProfiles)
|
||||||
setActiveProfileId(getActiveProviderProfile()?.id)
|
setActiveProfileId(getActiveProviderProfile()?.id)
|
||||||
setGithubProviderAvailable(isGithubProviderAvailable())
|
refreshGithubProviderState()
|
||||||
setGithubCredentialSource(getGithubCredentialSource())
|
|
||||||
setIsGithubActive(isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearStartupProviderOverrideFromUserSettings(): string | null {
|
function clearStartupProviderOverrideFromUserSettings(): string | null {
|
||||||
@@ -640,7 +694,11 @@ export function ProviderManager({ mode, onDone }: Props): React.ReactNode {
|
|||||||
{statusMessage && <Text>{statusMessage}</Text>}
|
{statusMessage && <Text>{statusMessage}</Text>}
|
||||||
<Box flexDirection="column">
|
<Box flexDirection="column">
|
||||||
{profiles.length === 0 && !githubProviderAvailable ? (
|
{profiles.length === 0 && !githubProviderAvailable ? (
|
||||||
<Text dimColor>No provider profiles configured yet.</Text>
|
isGithubCredentialSourceResolved ? (
|
||||||
|
<Text dimColor>No provider profiles configured yet.</Text>
|
||||||
|
) : (
|
||||||
|
<Text dimColor>Checking GitHub Models credentials...</Text>
|
||||||
|
)
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
{profiles.map(profile => (
|
{profiles.map(profile => (
|
||||||
|
|||||||
@@ -68,11 +68,11 @@ When a user describes what they want an agent to do, you will:
|
|||||||
assistant: "Now let me use the test-runner agent to run the tests"
|
assistant: "Now let me use the test-runner agent to run the tests"
|
||||||
</example>
|
</example>
|
||||||
- <example>
|
- <example>
|
||||||
Context: User is creating an agent to respond to the word "hello" with a friendly jok.
|
Context: User is creating an agent for Claude Code product questions.
|
||||||
user: "Hello"
|
user: "How do I configure Claude Code hooks?"
|
||||||
assistant: "I'm going to use the ${AGENT_TOOL_NAME} tool to launch the greeting-responder agent to respond with a friendly joke"
|
assistant: "I'm going to use the ${AGENT_TOOL_NAME} tool to launch the claude-code-guide agent to answer the question"
|
||||||
<commentary>
|
<commentary>
|
||||||
Since the user is greeting, use the greeting-responder agent to respond with a friendly joke.
|
Since the user is asking how to use Claude Code, use the claude-code-guide agent.
|
||||||
</commentary>
|
</commentary>
|
||||||
</example>
|
</example>
|
||||||
- If the user mentioned or implied that the agent should be used proactively, you should include examples of this.
|
- If the user mentioned or implied that the agent should be used proactively, you should include examples of this.
|
||||||
|
|||||||
@@ -8,6 +8,34 @@ import {
|
|||||||
validateProviderEnvOrExit,
|
validateProviderEnvOrExit,
|
||||||
} from '../utils/providerValidation.js'
|
} from '../utils/providerValidation.js'
|
||||||
|
|
||||||
|
// OpenClaude: polyfill globalThis.File for Node < 20.
|
||||||
|
// undici v7 references `File` at module evaluation time (webidl type
|
||||||
|
// assertions). Node 18 lacks the global, causing a ReferenceError inside
|
||||||
|
// the bundled __commonJS require chain which deadlocks the process when a
|
||||||
|
// proxy is configured (configureGlobalAgents → require_undici).
|
||||||
|
// eslint-disable-next-line custom-rules/no-top-level-side-effects
|
||||||
|
if (typeof globalThis.File === 'undefined') {
|
||||||
|
try {
|
||||||
|
// Node 18.13+ exposes File in node:buffer but not as a global.
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||||
|
const { File: NodeFile } = require('node:buffer')
|
||||||
|
// @ts-expect-error -- polyfilling missing global
|
||||||
|
globalThis.File = NodeFile
|
||||||
|
} catch {
|
||||||
|
// Absolute fallback: stub so `MakeTypeAssertion(File)` doesn't throw.
|
||||||
|
// @ts-expect-error -- minimal polyfill
|
||||||
|
globalThis.File = class File extends Blob {
|
||||||
|
name: string
|
||||||
|
lastModified: number
|
||||||
|
constructor(parts: BlobPart[], name: string, opts?: FilePropertyBag) {
|
||||||
|
super(parts, opts)
|
||||||
|
this.name = name
|
||||||
|
this.lastModified = opts?.lastModified ?? Date.now()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// OpenClaude: disable experimental API betas by default.
|
// OpenClaude: disable experimental API betas by default.
|
||||||
// Tool search (defer_loading), global cache scope, and context management
|
// Tool search (defer_loading), global cache scope, and context management
|
||||||
// require internal API support not available to external accounts → 500.
|
// require internal API support not available to external accounts → 500.
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ export class GrpcServer {
|
|||||||
grpc.ServerCredentials.createInsecure(),
|
grpc.ServerCredentials.createInsecure(),
|
||||||
(error, boundPort) => {
|
(error, boundPort) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('Failed to start gRPC server', error)
|
console.error('Failed to start gRPC server')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
console.log(`gRPC Server running at ${host}:${boundPort}`)
|
console.log(`gRPC Server running at ${host}:${boundPort}`)
|
||||||
@@ -225,7 +225,7 @@ export class GrpcServer {
|
|||||||
call.end()
|
call.end()
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error("Error processing stream:", err)
|
console.error('Error processing stream')
|
||||||
call.write({
|
call.write({
|
||||||
error: {
|
error: {
|
||||||
message: err.message || "Internal server error",
|
message: err.message || "Internal server error",
|
||||||
|
|||||||
@@ -366,14 +366,12 @@ const reconciler = createReconciler<
|
|||||||
createTextInstance(
|
createTextInstance(
|
||||||
text: string,
|
text: string,
|
||||||
_root: DOMElement,
|
_root: DOMElement,
|
||||||
hostContext: HostContext,
|
_hostContext: HostContext,
|
||||||
): TextNode {
|
): TextNode {
|
||||||
if (!hostContext.isInsideText) {
|
// react-compiler memoization can reuse cached <Text> elements without
|
||||||
throw new Error(
|
// re-traversing getChildHostContext, so hostContext.isInsideText may be
|
||||||
`Text string "${text}" must be rendered inside <Text> component`,
|
// stale. Always create the text node — Ink will render it correctly
|
||||||
)
|
// regardless of the context tracking state.
|
||||||
}
|
|
||||||
|
|
||||||
return createTextNode(text)
|
return createTextNode(text)
|
||||||
},
|
},
|
||||||
resetTextContent() {},
|
resetTextContent() {},
|
||||||
|
|||||||
@@ -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 { shouldRunStartupChecks } 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';
|
||||||
@@ -792,10 +793,8 @@ export function REPL({
|
|||||||
// accepts, and only then is the REPL component mounted and this effect runs.
|
// accepts, and only then is the REPL component mounted and this effect runs.
|
||||||
// This ensures that plugin installations from repository and user settings only
|
// This ensures that plugin installations from repository and user settings only
|
||||||
// happen after explicit user consent to trust the current working directory.
|
// happen after explicit user consent to trust the current working directory.
|
||||||
useEffect(() => {
|
// Deferring startup checks is handled below (after promptTypingSuppressionActive
|
||||||
if (isRemoteSession) return;
|
// is declared) to avoid temporal dead zone issues.
|
||||||
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
|
||||||
@@ -1429,6 +1428,25 @@ export function REPL({
|
|||||||
const activeRemote = sshRemote.isRemoteMode ? sshRemote : directConnect.isRemoteMode ? directConnect : remoteSession;
|
const activeRemote = sshRemote.isRemoteMode ? sshRemote : directConnect.isRemoteMode ? directConnect : remoteSession;
|
||||||
const [pastedContents, setPastedContents] = useState<Record<number, PastedContent>>({});
|
const [pastedContents, setPastedContents] = useState<Record<number, PastedContent>>({});
|
||||||
const [submitCount, setSubmitCount] = useState(0);
|
const [submitCount, setSubmitCount] = useState(0);
|
||||||
|
|
||||||
|
// Defer startup checks until the user has submitted their first message.
|
||||||
|
// A timeout or grace period is insufficient (issue #363): if the user pauses
|
||||||
|
// before typing, startup checks can still fire and recommendation dialogs
|
||||||
|
// steal focus. Only the user's first submission guarantees the prompt was
|
||||||
|
// the first thing they interacted with.
|
||||||
|
const startupChecksStartedRef = React.useRef(false);
|
||||||
|
const hasHadFirstSubmission = (submitCount ?? 0) > 0;
|
||||||
|
useEffect(() => {
|
||||||
|
if (isRemoteSession) return;
|
||||||
|
if (startupChecksStartedRef.current) return;
|
||||||
|
if (!shouldRunStartupChecks({
|
||||||
|
isRemoteSession,
|
||||||
|
hasStarted: startupChecksStartedRef.current,
|
||||||
|
hasHadFirstSubmission,
|
||||||
|
})) return;
|
||||||
|
startupChecksStartedRef.current = true;
|
||||||
|
void performStartupChecks(setAppState);
|
||||||
|
}, [setAppState, isRemoteSession, hasHadFirstSubmission]);
|
||||||
// Ref instead of state to avoid triggering React re-renders on every
|
// Ref instead of state to avoid triggering React re-renders on every
|
||||||
// streaming text_delta. The spinner reads this via its animation timer.
|
// streaming text_delta. The spinner reads this via its animation timer.
|
||||||
const responseLengthRef = useRef(0);
|
const responseLengthRef = useRef(0);
|
||||||
@@ -2061,13 +2079,14 @@ export function REPL({
|
|||||||
if (allowDialogsWithAnimation && showRemoteCallout) return 'remote-callout';
|
if (allowDialogsWithAnimation && showRemoteCallout) return 'remote-callout';
|
||||||
|
|
||||||
// LSP plugin recommendation (lowest priority - non-blocking suggestion)
|
// LSP plugin recommendation (lowest priority - non-blocking suggestion)
|
||||||
if (allowDialogsWithAnimation && lspRecommendation) return 'lsp-recommendation';
|
// Suppress during startup window to prevent stealing focus from the prompt (issue #363)
|
||||||
|
if (allowDialogsWithAnimation && lspRecommendation && startupChecksStartedRef.current) return 'lsp-recommendation';
|
||||||
|
|
||||||
// Plugin hint from CLI/SDK stderr (same priority band as LSP rec)
|
// Plugin hint from CLI/SDK stderr (same priority band as LSP rec)
|
||||||
if (allowDialogsWithAnimation && hintRecommendation) return 'plugin-hint';
|
if (allowDialogsWithAnimation && hintRecommendation && startupChecksStartedRef.current) return 'plugin-hint';
|
||||||
|
|
||||||
// Desktop app upsell (max 3 launches, lowest priority)
|
// Desktop app upsell (max 3 launches, lowest priority)
|
||||||
if (allowDialogsWithAnimation && showDesktopUpsellStartup) return 'desktop-upsell';
|
if (allowDialogsWithAnimation && showDesktopUpsellStartup && startupChecksStartedRef.current) return 'desktop-upsell';
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const focusedInputDialog = getFocusedInputDialog();
|
const focusedInputDialog = getFocusedInputDialog();
|
||||||
|
|||||||
53
src/screens/replStartupGates.test.ts
Normal file
53
src/screens/replStartupGates.test.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
|
||||||
|
import { shouldRunStartupChecks } from './replStartupGates.js'
|
||||||
|
|
||||||
|
describe('shouldRunStartupChecks', () => {
|
||||||
|
test('runs checks after first message submission', () => {
|
||||||
|
expect(shouldRunStartupChecks({
|
||||||
|
isRemoteSession: false,
|
||||||
|
hasStarted: false,
|
||||||
|
hasHadFirstSubmission: true,
|
||||||
|
})).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('skips checks in remote sessions even after submission', () => {
|
||||||
|
expect(shouldRunStartupChecks({
|
||||||
|
isRemoteSession: true,
|
||||||
|
hasStarted: false,
|
||||||
|
hasHadFirstSubmission: true,
|
||||||
|
})).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('skips checks if already started', () => {
|
||||||
|
expect(shouldRunStartupChecks({
|
||||||
|
isRemoteSession: false,
|
||||||
|
hasStarted: true,
|
||||||
|
hasHadFirstSubmission: true,
|
||||||
|
})).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('does not run checks before first submission', () => {
|
||||||
|
expect(shouldRunStartupChecks({
|
||||||
|
isRemoteSession: false,
|
||||||
|
hasStarted: false,
|
||||||
|
hasHadFirstSubmission: false,
|
||||||
|
})).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('does not run checks when idle before first submission', () => {
|
||||||
|
expect(shouldRunStartupChecks({
|
||||||
|
isRemoteSession: false,
|
||||||
|
hasStarted: false,
|
||||||
|
hasHadFirstSubmission: false,
|
||||||
|
})).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('skips checks in remote session regardless of other conditions', () => {
|
||||||
|
expect(shouldRunStartupChecks({
|
||||||
|
isRemoteSession: true,
|
||||||
|
hasStarted: false,
|
||||||
|
hasHadFirstSubmission: false,
|
||||||
|
})).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
35
src/screens/replStartupGates.ts
Normal file
35
src/screens/replStartupGates.ts
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* Startup gates for the REPL.
|
||||||
|
*
|
||||||
|
* Prevents startup plugin checks and recommendation dialogs from stealing
|
||||||
|
* focus before the user has interacted with the prompt.
|
||||||
|
*
|
||||||
|
* This addresses the root cause of issue #363: on mount, performStartupChecks
|
||||||
|
* triggers plugin loading, which populates trackedFiles, which triggers
|
||||||
|
* useLspPluginRecommendation to surface an LSP recommendation dialog. Since
|
||||||
|
* promptTypingSuppressionActive is false before the user has typed anything,
|
||||||
|
* getFocusedInputDialog() returns the dialog, unmounting PromptInput entirely.
|
||||||
|
*
|
||||||
|
* The fix gates startup checks on actual prompt interaction. A pure timeout
|
||||||
|
* or grace period is insufficient because pausing before typing would still
|
||||||
|
* allow dialogs to steal focus. Only the user's first submission guarantees
|
||||||
|
* the prompt is no longer in the vulnerable pre-interaction window.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether startup checks should run.
|
||||||
|
*
|
||||||
|
* Startup checks are deferred until the user has submitted their first
|
||||||
|
* message. This guarantees the prompt was the first thing the user interacted
|
||||||
|
* with, so no recommendation dialog can steal focus before the first keystroke.
|
||||||
|
*/
|
||||||
|
export function shouldRunStartupChecks(options: {
|
||||||
|
isRemoteSession: boolean;
|
||||||
|
hasStarted: boolean;
|
||||||
|
hasHadFirstSubmission: boolean;
|
||||||
|
}): boolean {
|
||||||
|
if (options.isRemoteSession) return false;
|
||||||
|
if (options.hasStarted) return false;
|
||||||
|
if (!options.hasHadFirstSubmission) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -201,6 +201,117 @@ describe('Codex request translation', () => {
|
|||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('preserves Grep tool pattern field in Codex strict schemas', () => {
|
||||||
|
const tools = convertToolsToResponsesTools([
|
||||||
|
{
|
||||||
|
name: 'Grep',
|
||||||
|
description: 'Search file contents',
|
||||||
|
input_schema: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: { type: 'string', description: 'Search pattern' },
|
||||||
|
path: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['pattern'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(tools).toEqual([
|
||||||
|
{
|
||||||
|
type: 'function',
|
||||||
|
name: 'Grep',
|
||||||
|
description: 'Search file contents',
|
||||||
|
parameters: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: { type: 'string', description: 'Search pattern' },
|
||||||
|
path: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['pattern', 'path'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('preserves Glob tool pattern field in Codex strict schemas', () => {
|
||||||
|
const tools = convertToolsToResponsesTools([
|
||||||
|
{
|
||||||
|
name: 'Glob',
|
||||||
|
description: 'Find files by pattern',
|
||||||
|
input_schema: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: { type: 'string', description: 'Glob pattern' },
|
||||||
|
path: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['pattern'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(tools).toEqual([
|
||||||
|
{
|
||||||
|
type: 'function',
|
||||||
|
name: 'Glob',
|
||||||
|
description: 'Find files by pattern',
|
||||||
|
parameters: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: { type: 'string', description: 'Glob pattern' },
|
||||||
|
path: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['pattern', 'path'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('strips validator pattern keyword but keeps string field named pattern in Codex schemas', () => {
|
||||||
|
const tools = convertToolsToResponsesTools([
|
||||||
|
{
|
||||||
|
name: 'RegexProbe',
|
||||||
|
description: 'Probe regex schema handling',
|
||||||
|
input_schema: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: {
|
||||||
|
type: 'string',
|
||||||
|
pattern: '^[a-z]+$',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
required: ['pattern'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(tools).toEqual([
|
||||||
|
{
|
||||||
|
type: 'function',
|
||||||
|
name: 'RegexProbe',
|
||||||
|
description: 'Probe regex schema handling',
|
||||||
|
parameters: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
required: ['pattern'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
strict: true,
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
test('removes unsupported uri format from strict Responses schemas', () => {
|
test('removes unsupported uri format from strict Responses schemas', () => {
|
||||||
const tools = convertToolsToResponsesTools([
|
const tools = convertToolsToResponsesTools([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -261,6 +261,125 @@ test('preserves Gemini tool call extra_content in follow-up requests', async ()
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('preserves Grep tool pattern field in OpenAI-compatible schemas', async () => {
|
||||||
|
let requestBody: Record<string, unknown> | undefined
|
||||||
|
|
||||||
|
globalThis.fetch = (async (_input, init) => {
|
||||||
|
requestBody = JSON.parse(String(init?.body))
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 'chatcmpl-grep-schema',
|
||||||
|
model: 'qwen/qwen3.6-plus',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'done',
|
||||||
|
},
|
||||||
|
finish_reason: 'stop',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
usage: {
|
||||||
|
prompt_tokens: 12,
|
||||||
|
completion_tokens: 4,
|
||||||
|
total_tokens: 16,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}) as FetchType
|
||||||
|
|
||||||
|
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
||||||
|
|
||||||
|
await client.beta.messages.create({
|
||||||
|
model: 'qwen/qwen3.6-plus',
|
||||||
|
system: 'test system',
|
||||||
|
messages: [{ role: 'user', content: 'Use Grep' }],
|
||||||
|
tools: [
|
||||||
|
{
|
||||||
|
name: 'Grep',
|
||||||
|
description: 'Search file contents',
|
||||||
|
input_schema: {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: { type: 'string', description: 'Search pattern' },
|
||||||
|
path: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['pattern'],
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
max_tokens: 64,
|
||||||
|
stream: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
const tools = requestBody?.tools as Array<Record<string, unknown>> | undefined
|
||||||
|
const grepTool = tools?.find(tool => (tool.function as Record<string, unknown>)?.name === 'Grep') as
|
||||||
|
| { function?: { parameters?: { properties?: Record<string, unknown>; required?: string[] } } }
|
||||||
|
| undefined
|
||||||
|
|
||||||
|
expect(Object.keys(grepTool?.function?.parameters?.properties ?? {})).toContain('pattern')
|
||||||
|
expect(grepTool?.function?.parameters?.required).toContain('pattern')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('does not infer Gemini mode from OPENAI_BASE_URL path substrings', async () => {
|
||||||
|
let capturedAuthorization: string | null = null
|
||||||
|
|
||||||
|
process.env.OPENAI_BASE_URL =
|
||||||
|
'https://evil.example/generativelanguage.googleapis.com/v1beta/openai'
|
||||||
|
delete process.env.OPENAI_API_KEY
|
||||||
|
process.env.GEMINI_API_KEY = 'gemini-secret'
|
||||||
|
|
||||||
|
globalThis.fetch = (async (_input, init) => {
|
||||||
|
const headers = init?.headers as Record<string, string> | undefined
|
||||||
|
capturedAuthorization =
|
||||||
|
headers?.Authorization ?? headers?.authorization ?? null
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 'chatcmpl-1',
|
||||||
|
model: 'fake-model',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'ok',
|
||||||
|
},
|
||||||
|
finish_reason: 'stop',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
usage: {
|
||||||
|
prompt_tokens: 12,
|
||||||
|
completion_tokens: 4,
|
||||||
|
total_tokens: 16,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}) as FetchType
|
||||||
|
|
||||||
|
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
||||||
|
|
||||||
|
await client.beta.messages.create({
|
||||||
|
model: 'fake-model',
|
||||||
|
messages: [{ role: 'user', content: 'hello' }],
|
||||||
|
max_tokens: 64,
|
||||||
|
stream: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(capturedAuthorization).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
test('preserves image tool results as placeholders in follow-up requests', async () => {
|
test('preserves image tool results as placeholders in follow-up requests', async () => {
|
||||||
let requestBody: Record<string, unknown> | undefined
|
let requestBody: Record<string, unknown> | undefined
|
||||||
|
|
||||||
@@ -1769,3 +1888,237 @@ test('coalesces consecutive assistant messages preserving tool_calls (issue #202
|
|||||||
expect(assistantMsgs?.length).toBe(1) // two assistant turns merged into one
|
expect(assistantMsgs?.length).toBe(1) // two assistant turns merged into one
|
||||||
expect(assistantMsgs?.[0]?.tool_calls?.length).toBeGreaterThan(0)
|
expect(assistantMsgs?.[0]?.tool_calls?.length).toBeGreaterThan(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('non-streaming: reasoning_content emitted as thinking block, used as text when content is null', async () => {
|
||||||
|
globalThis.fetch = (async (_input, _init) => {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 'chatcmpl-1',
|
||||||
|
model: 'glm-5',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
role: 'assistant',
|
||||||
|
content: null,
|
||||||
|
reasoning_content: 'Let me think about this step by step.',
|
||||||
|
},
|
||||||
|
finish_reason: 'stop',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
usage: {
|
||||||
|
prompt_tokens: 10,
|
||||||
|
completion_tokens: 20,
|
||||||
|
total_tokens: 30,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}) as FetchType
|
||||||
|
|
||||||
|
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
||||||
|
|
||||||
|
const result = (await client.beta.messages.create({
|
||||||
|
model: 'glm-5',
|
||||||
|
system: 'test system',
|
||||||
|
messages: [{ role: 'user', content: 'hello' }],
|
||||||
|
max_tokens: 64,
|
||||||
|
stream: false,
|
||||||
|
})) as { content: Array<Record<string, unknown>> }
|
||||||
|
|
||||||
|
expect(result.content).toEqual([
|
||||||
|
{ type: 'thinking', thinking: 'Let me think about this step by step.' },
|
||||||
|
{ type: 'text', text: 'Let me think about this step by step.' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('non-streaming: empty string content does not fall through to reasoning_content as text', async () => {
|
||||||
|
globalThis.fetch = (async (_input, _init) => {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 'chatcmpl-1',
|
||||||
|
model: 'glm-5',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
role: 'assistant',
|
||||||
|
content: '',
|
||||||
|
reasoning_content: 'Chain of thought here.',
|
||||||
|
},
|
||||||
|
finish_reason: 'stop',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
usage: {
|
||||||
|
prompt_tokens: 10,
|
||||||
|
completion_tokens: 20,
|
||||||
|
total_tokens: 30,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}) as FetchType
|
||||||
|
|
||||||
|
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
||||||
|
|
||||||
|
const result = (await client.beta.messages.create({
|
||||||
|
model: 'glm-5',
|
||||||
|
system: 'test system',
|
||||||
|
messages: [{ role: 'user', content: 'hello' }],
|
||||||
|
max_tokens: 64,
|
||||||
|
stream: false,
|
||||||
|
})) as { content: Array<Record<string, unknown>> }
|
||||||
|
|
||||||
|
// reasoning_content should be a thinking block, and also used as text
|
||||||
|
// since content is empty string (treated as absent)
|
||||||
|
expect(result.content).toEqual([
|
||||||
|
{ type: 'thinking', thinking: 'Chain of thought here.' },
|
||||||
|
{ type: 'text', text: 'Chain of thought here.' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('non-streaming: real content takes precedence over reasoning_content', async () => {
|
||||||
|
globalThis.fetch = (async (_input, _init) => {
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
id: 'chatcmpl-1',
|
||||||
|
model: 'glm-5',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
message: {
|
||||||
|
role: 'assistant',
|
||||||
|
content: 'The answer is 42.',
|
||||||
|
reasoning_content: 'I need to calculate this.',
|
||||||
|
},
|
||||||
|
finish_reason: 'stop',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
usage: {
|
||||||
|
prompt_tokens: 10,
|
||||||
|
completion_tokens: 20,
|
||||||
|
total_tokens: 30,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}) as FetchType
|
||||||
|
|
||||||
|
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
||||||
|
|
||||||
|
const result = (await client.beta.messages.create({
|
||||||
|
model: 'glm-5',
|
||||||
|
system: 'test system',
|
||||||
|
messages: [{ role: 'user', content: 'hello' }],
|
||||||
|
max_tokens: 64,
|
||||||
|
stream: false,
|
||||||
|
})) as { content: Array<Record<string, unknown>> }
|
||||||
|
|
||||||
|
expect(result.content).toEqual([
|
||||||
|
{ type: 'thinking', thinking: 'I need to calculate this.' },
|
||||||
|
{ type: 'text', text: 'The answer is 42.' },
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('streaming: thinking block closed before tool call', async () => {
|
||||||
|
globalThis.fetch = (async (_input, _init) => {
|
||||||
|
const chunks = makeStreamChunks([
|
||||||
|
{
|
||||||
|
id: 'chatcmpl-1',
|
||||||
|
object: 'chat.completion.chunk',
|
||||||
|
model: 'glm-5',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
delta: { role: 'assistant', reasoning_content: 'Thinking...' },
|
||||||
|
finish_reason: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'chatcmpl-1',
|
||||||
|
object: 'chat.completion.chunk',
|
||||||
|
model: 'glm-5',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
delta: {
|
||||||
|
tool_calls: [
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
id: 'call-1',
|
||||||
|
type: 'function',
|
||||||
|
function: {
|
||||||
|
name: 'Bash',
|
||||||
|
arguments: '{"command":"ls"}',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
finish_reason: null,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'chatcmpl-1',
|
||||||
|
object: 'chat.completion.chunk',
|
||||||
|
model: 'glm-5',
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
delta: {},
|
||||||
|
finish_reason: 'tool_calls',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
])
|
||||||
|
|
||||||
|
return makeSseResponse(chunks)
|
||||||
|
}) as FetchType
|
||||||
|
|
||||||
|
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
||||||
|
|
||||||
|
const result = await client.beta.messages
|
||||||
|
.create({
|
||||||
|
model: 'glm-5',
|
||||||
|
system: 'test system',
|
||||||
|
messages: [{ role: 'user', content: 'Run ls' }],
|
||||||
|
max_tokens: 64,
|
||||||
|
stream: true,
|
||||||
|
})
|
||||||
|
.withResponse()
|
||||||
|
|
||||||
|
const events: Array<Record<string, unknown>> = []
|
||||||
|
for await (const event of result.data) {
|
||||||
|
events.push(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
const types = events.map(e => e.type)
|
||||||
|
|
||||||
|
// Verify thinking block is started, then closed, then tool call starts
|
||||||
|
const thinkingStartIdx = types.indexOf('content_block_start')
|
||||||
|
const firstStopIdx = types.indexOf('content_block_stop')
|
||||||
|
const toolStartIdx = types.indexOf(
|
||||||
|
'content_block_start',
|
||||||
|
thinkingStartIdx + 1,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(thinkingStartIdx).toBeGreaterThanOrEqual(0)
|
||||||
|
expect(firstStopIdx).toBeGreaterThan(thinkingStartIdx)
|
||||||
|
expect(toolStartIdx).toBeGreaterThan(firstStopIdx)
|
||||||
|
|
||||||
|
// Verify thinking block start content
|
||||||
|
const thinkingStart = events[thinkingStartIdx] as {
|
||||||
|
content_block?: Record<string, unknown>
|
||||||
|
}
|
||||||
|
expect(thinkingStart?.content_block?.type).toBe('thinking')
|
||||||
|
})
|
||||||
|
|||||||
@@ -60,11 +60,22 @@ const GITHUB_API_VERSION = '2022-11-28'
|
|||||||
const GITHUB_429_MAX_RETRIES = 3
|
const GITHUB_429_MAX_RETRIES = 3
|
||||||
const GITHUB_429_BASE_DELAY_SEC = 1
|
const GITHUB_429_BASE_DELAY_SEC = 1
|
||||||
const GITHUB_429_MAX_DELAY_SEC = 32
|
const GITHUB_429_MAX_DELAY_SEC = 32
|
||||||
|
const GEMINI_API_HOST = 'generativelanguage.googleapis.com'
|
||||||
|
|
||||||
function isGithubModelsMode(): boolean {
|
function isGithubModelsMode(): boolean {
|
||||||
return isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
return isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hasGeminiApiHost(baseUrl: string | undefined): boolean {
|
||||||
|
if (!baseUrl) return false
|
||||||
|
|
||||||
|
try {
|
||||||
|
return new URL(baseUrl).hostname.toLowerCase() === GEMINI_API_HOST
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function formatRetryAfterHint(response: Response): string {
|
function formatRetryAfterHint(response: Response): string {
|
||||||
const ra = response.headers.get('retry-after')
|
const ra = response.headers.get('retry-after')
|
||||||
return ra ? ` (Retry-After: ${ra})` : ''
|
return ra ? ` (Retry-After: ${ra})` : ''
|
||||||
@@ -184,10 +195,12 @@ function convertContentBlocks(
|
|||||||
// handled separately
|
// handled separately
|
||||||
break
|
break
|
||||||
case 'thinking':
|
case 'thinking':
|
||||||
// Append thinking as text with a marker for models that support reasoning
|
case 'redacted_thinking':
|
||||||
if (block.thinking) {
|
// Strip thinking blocks for OpenAI-compatible providers.
|
||||||
parts.push({ type: 'text', text: `<thinking>${block.thinking}</thinking>` })
|
// These are Anthropic-specific content types that 3P providers
|
||||||
}
|
// don't understand. Serializing them as <thinking> text corrupts
|
||||||
|
// multi-turn context: the model sees the tags as part of its
|
||||||
|
// previous reply and may mimic or misattribute them.
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
if (block.text) {
|
if (block.text) {
|
||||||
@@ -201,6 +214,13 @@ function convertContentBlocks(
|
|||||||
return parts
|
return parts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isGeminiMode(): boolean {
|
||||||
|
return (
|
||||||
|
isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI) ||
|
||||||
|
hasGeminiApiHost(process.env.OPENAI_BASE_URL)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
function convertMessages(
|
function convertMessages(
|
||||||
messages: Array<{ role: string; message?: { role?: string; content?: unknown }; content?: unknown }>,
|
messages: Array<{ role: string; message?: { role?: string; content?: unknown }; content?: unknown }>,
|
||||||
system: unknown,
|
system: unknown,
|
||||||
@@ -252,6 +272,7 @@ function convertMessages(
|
|||||||
// Check for tool_use blocks
|
// Check for tool_use blocks
|
||||||
if (Array.isArray(content)) {
|
if (Array.isArray(content)) {
|
||||||
const toolUses = content.filter((b: { type?: string }) => b.type === 'tool_use')
|
const toolUses = content.filter((b: { type?: string }) => b.type === 'tool_use')
|
||||||
|
const thinkingBlock = content.find((b: { type?: string }) => b.type === 'thinking')
|
||||||
const textContent = content.filter(
|
const textContent = content.filter(
|
||||||
(b: { type?: string }) => b.type !== 'tool_use' && b.type !== 'thinking',
|
(b: { type?: string }) => b.type !== 'tool_use' && b.type !== 'thinking',
|
||||||
)
|
)
|
||||||
@@ -271,18 +292,46 @@ function convertMessages(
|
|||||||
name?: string
|
name?: string
|
||||||
input?: unknown
|
input?: unknown
|
||||||
extra_content?: Record<string, unknown>
|
extra_content?: Record<string, unknown>
|
||||||
}) => ({
|
signature?: string
|
||||||
id: tu.id ?? `call_${crypto.randomUUID().replace(/-/g, '')}`,
|
}, index) => {
|
||||||
type: 'function' as const,
|
const toolCall: NonNullable<OpenAIMessage['tool_calls']>[number] = {
|
||||||
function: {
|
id: tu.id ?? `call_${crypto.randomUUID().replace(/-/g, '')}`,
|
||||||
name: tu.name ?? 'unknown',
|
type: 'function' as const,
|
||||||
arguments:
|
function: {
|
||||||
typeof tu.input === 'string'
|
name: tu.name ?? 'unknown',
|
||||||
? tu.input
|
arguments:
|
||||||
: JSON.stringify(tu.input ?? {}),
|
typeof tu.input === 'string'
|
||||||
},
|
? tu.input
|
||||||
...(tu.extra_content ? { extra_content: tu.extra_content } : {}),
|
: JSON.stringify(tu.input ?? {}),
|
||||||
}),
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preserve existing extra_content if present
|
||||||
|
if (tu.extra_content) {
|
||||||
|
toolCall.extra_content = { ...tu.extra_content }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle Gemini thought_signature
|
||||||
|
if (isGeminiMode()) {
|
||||||
|
// If the model provided a signature in the tool_use block itself (e.g. from a previous Turn/Step)
|
||||||
|
// Use thinkingBlock.signature for ALL tool calls in the same assistant turn if available.
|
||||||
|
// The API requires the same signature on every replayed function call part in a parallel set.
|
||||||
|
const signature = tu.signature ?? (thinkingBlock as any)?.signature
|
||||||
|
|
||||||
|
// Merge into existing google-specific metadata if present
|
||||||
|
const existingGoogle = (toolCall.extra_content?.google as Record<string, unknown>) ?? {}
|
||||||
|
|
||||||
|
toolCall.extra_content = {
|
||||||
|
...toolCall.extra_content,
|
||||||
|
google: {
|
||||||
|
...existingGoogle,
|
||||||
|
thought_signature: signature ?? "skip_thought_signature_validator"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return toolCall
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,7 +450,7 @@ function normalizeSchemaForOpenAI(
|
|||||||
function convertTools(
|
function convertTools(
|
||||||
tools: Array<{ name: string; description?: string; input_schema?: Record<string, unknown> }>,
|
tools: Array<{ name: string; description?: string; input_schema?: Record<string, unknown> }>,
|
||||||
): OpenAITool[] {
|
): OpenAITool[] {
|
||||||
const isGemini = isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI)
|
const isGemini = isGeminiMode()
|
||||||
|
|
||||||
return tools
|
return tools
|
||||||
.filter(t => t.name !== 'ToolSearchTool') // Not relevant for OpenAI
|
.filter(t => t.name !== 'ToolSearchTool') // Not relevant for OpenAI
|
||||||
@@ -443,6 +492,7 @@ interface OpenAIStreamChunk {
|
|||||||
delta: {
|
delta: {
|
||||||
role?: string
|
role?: string
|
||||||
content?: string | null
|
content?: string | null
|
||||||
|
reasoning_content?: string | null
|
||||||
tool_calls?: Array<{
|
tool_calls?: Array<{
|
||||||
index: number
|
index: number
|
||||||
id?: string
|
id?: string
|
||||||
@@ -525,6 +575,8 @@ async function* openaiStreamToAnthropic(
|
|||||||
}
|
}
|
||||||
>()
|
>()
|
||||||
let hasEmittedContentStart = false
|
let hasEmittedContentStart = false
|
||||||
|
let hasEmittedThinkingStart = false
|
||||||
|
let hasClosedThinking = false
|
||||||
let lastStopReason: 'tool_use' | 'max_tokens' | 'end_turn' | null = null
|
let lastStopReason: 'tool_use' | 'max_tokens' | 'end_turn' | null = null
|
||||||
let hasEmittedFinalUsage = false
|
let hasEmittedFinalUsage = false
|
||||||
let hasProcessedFinishReason = false
|
let hasProcessedFinishReason = false
|
||||||
@@ -581,9 +633,34 @@ async function* openaiStreamToAnthropic(
|
|||||||
for (const choice of chunk.choices ?? []) {
|
for (const choice of chunk.choices ?? []) {
|
||||||
const delta = choice.delta
|
const delta = choice.delta
|
||||||
|
|
||||||
|
// Reasoning models (e.g. GLM-5, DeepSeek) may stream chain-of-thought
|
||||||
|
// in `reasoning_content` before the actual reply appears in `content`.
|
||||||
|
// Emit reasoning as a thinking block and content as a text block.
|
||||||
|
if (delta.reasoning_content != null && delta.reasoning_content !== '') {
|
||||||
|
if (!hasEmittedThinkingStart) {
|
||||||
|
yield {
|
||||||
|
type: 'content_block_start',
|
||||||
|
index: contentBlockIndex,
|
||||||
|
content_block: { type: 'thinking', thinking: '' },
|
||||||
|
}
|
||||||
|
hasEmittedThinkingStart = true
|
||||||
|
}
|
||||||
|
yield {
|
||||||
|
type: 'content_block_delta',
|
||||||
|
index: contentBlockIndex,
|
||||||
|
delta: { type: 'thinking_delta', thinking: delta.reasoning_content },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Text content — use != null to distinguish absent field from empty string,
|
// Text content — use != null to distinguish absent field from empty string,
|
||||||
// some providers send "" as first delta to signal streaming start
|
// some providers send "" as first delta to signal streaming start
|
||||||
if (delta.content != null) {
|
if (delta.content != null && delta.content !== '') {
|
||||||
|
// Close thinking block if transitioning from reasoning to content
|
||||||
|
if (hasEmittedThinkingStart && !hasClosedThinking) {
|
||||||
|
yield { type: 'content_block_stop', index: contentBlockIndex }
|
||||||
|
contentBlockIndex++
|
||||||
|
hasClosedThinking = true
|
||||||
|
}
|
||||||
if (!hasEmittedContentStart) {
|
if (!hasEmittedContentStart) {
|
||||||
yield {
|
yield {
|
||||||
type: 'content_block_start',
|
type: 'content_block_start',
|
||||||
@@ -603,7 +680,12 @@ async function* openaiStreamToAnthropic(
|
|||||||
if (delta.tool_calls) {
|
if (delta.tool_calls) {
|
||||||
for (const tc of delta.tool_calls) {
|
for (const tc of delta.tool_calls) {
|
||||||
if (tc.id && tc.function?.name) {
|
if (tc.id && tc.function?.name) {
|
||||||
// New tool call starting
|
// New tool call starting — close any open thinking block first
|
||||||
|
if (hasEmittedThinkingStart && !hasClosedThinking) {
|
||||||
|
yield { type: 'content_block_stop', index: contentBlockIndex }
|
||||||
|
contentBlockIndex++
|
||||||
|
hasClosedThinking = true
|
||||||
|
}
|
||||||
if (hasEmittedContentStart) {
|
if (hasEmittedContentStart) {
|
||||||
yield {
|
yield {
|
||||||
type: 'content_block_stop',
|
type: 'content_block_stop',
|
||||||
@@ -633,6 +715,13 @@ async function* openaiStreamToAnthropic(
|
|||||||
name: tc.function.name,
|
name: tc.function.name,
|
||||||
input: {},
|
input: {},
|
||||||
...(tc.extra_content ? { extra_content: tc.extra_content } : {}),
|
...(tc.extra_content ? { extra_content: tc.extra_content } : {}),
|
||||||
|
// Extract Gemini signature from extra_content
|
||||||
|
...((tc.extra_content?.google as any)?.thought_signature
|
||||||
|
? {
|
||||||
|
signature: (tc.extra_content.google as any)
|
||||||
|
.thought_signature,
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
contentBlockIndex++
|
contentBlockIndex++
|
||||||
@@ -678,6 +767,12 @@ async function* openaiStreamToAnthropic(
|
|||||||
if (choice.finish_reason && !hasProcessedFinishReason) {
|
if (choice.finish_reason && !hasProcessedFinishReason) {
|
||||||
hasProcessedFinishReason = true
|
hasProcessedFinishReason = true
|
||||||
|
|
||||||
|
// Close any open thinking block that wasn't closed by content transition
|
||||||
|
if (hasEmittedThinkingStart && !hasClosedThinking) {
|
||||||
|
yield { type: 'content_block_stop', index: contentBlockIndex }
|
||||||
|
contentBlockIndex++
|
||||||
|
hasClosedThinking = true
|
||||||
|
}
|
||||||
// Close any open content blocks
|
// Close any open content blocks
|
||||||
if (hasEmittedContentStart) {
|
if (hasEmittedContentStart) {
|
||||||
yield {
|
yield {
|
||||||
@@ -1003,7 +1098,7 @@ class OpenAIShimMessages {
|
|||||||
...(options?.headers ?? {}),
|
...(options?.headers ?? {}),
|
||||||
}
|
}
|
||||||
|
|
||||||
const isGemini = isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI)
|
const isGemini = isGeminiMode()
|
||||||
const apiKey =
|
const apiKey =
|
||||||
this.providerOverride?.apiKey ?? process.env.OPENAI_API_KEY ?? ''
|
this.providerOverride?.apiKey ?? process.env.OPENAI_API_KEY ?? ''
|
||||||
// Detect Azure endpoints by hostname (not raw URL) to prevent bypass via
|
// Detect Azure endpoints by hostname (not raw URL) to prevent bypass via
|
||||||
@@ -1116,6 +1211,7 @@ class OpenAIShimMessages {
|
|||||||
| string
|
| string
|
||||||
| null
|
| null
|
||||||
| Array<{ type?: string; text?: string }>
|
| Array<{ type?: string; text?: string }>
|
||||||
|
reasoning_content?: string | null
|
||||||
tool_calls?: Array<{
|
tool_calls?: Array<{
|
||||||
id: string
|
id: string
|
||||||
function: { name: string; arguments: string }
|
function: { name: string; arguments: string }
|
||||||
@@ -1137,7 +1233,17 @@ class OpenAIShimMessages {
|
|||||||
const choice = data.choices?.[0]
|
const choice = data.choices?.[0]
|
||||||
const content: Array<Record<string, unknown>> = []
|
const content: Array<Record<string, unknown>> = []
|
||||||
|
|
||||||
const rawContent = choice?.message?.content
|
// Some reasoning models (e.g. GLM-5) put their reply in reasoning_content
|
||||||
|
// while content stays null — emit reasoning as a thinking block, then
|
||||||
|
// fall back to it for visible text if content is empty.
|
||||||
|
const reasoningText = choice?.message?.reasoning_content
|
||||||
|
if (typeof reasoningText === 'string' && reasoningText) {
|
||||||
|
content.push({ type: 'thinking', thinking: reasoningText })
|
||||||
|
}
|
||||||
|
const rawContent =
|
||||||
|
choice?.message?.content !== '' && choice?.message?.content != null
|
||||||
|
? choice?.message?.content
|
||||||
|
: choice?.message?.reasoning_content
|
||||||
if (typeof rawContent === 'string' && rawContent) {
|
if (typeof rawContent === 'string' && rawContent) {
|
||||||
content.push({ type: 'text', text: rawContent })
|
content.push({ type: 'text', text: rawContent })
|
||||||
} else if (Array.isArray(rawContent) && rawContent.length > 0) {
|
} else if (Array.isArray(rawContent) && rawContent.length > 0) {
|
||||||
@@ -1170,6 +1276,10 @@ class OpenAIShimMessages {
|
|||||||
name: tc.function.name,
|
name: tc.function.name,
|
||||||
input,
|
input,
|
||||||
...(tc.extra_content ? { extra_content: tc.extra_content } : {}),
|
...(tc.extra_content ? { extra_content: tc.extra_content } : {}),
|
||||||
|
// Extract Gemini signature from extra_content
|
||||||
|
...((tc.extra_content?.google as any)?.thought_signature
|
||||||
|
? { signature: (tc.extra_content.google as any).thought_signature }
|
||||||
|
: {}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
127
src/services/compact/microCompact.test.ts
Normal file
127
src/services/compact/microCompact.test.ts
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
|
||||||
|
import type { Message } from '../../types/message.js'
|
||||||
|
import { createAssistantMessage, createUserMessage } from '../../utils/messages.js'
|
||||||
|
|
||||||
|
// We test the exported collectCompactableToolIds behavior indirectly via
|
||||||
|
// the public microcompactMessages + time-based path. But first we need to
|
||||||
|
// verify the core predicate: MCP tools (prefixed 'mcp__') should be
|
||||||
|
// compactable alongside the built-in tool set.
|
||||||
|
|
||||||
|
// Import internals we can test
|
||||||
|
import { evaluateTimeBasedTrigger } from './microCompact.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper: build a minimal assistant message with a tool_use block.
|
||||||
|
*/
|
||||||
|
function assistantWithToolUse(toolName: string, toolId: string): Message {
|
||||||
|
return createAssistantMessage({
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'tool_use' as const,
|
||||||
|
id: toolId,
|
||||||
|
name: toolName,
|
||||||
|
input: {},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper: build a user message with a tool_result block.
|
||||||
|
*/
|
||||||
|
function userWithToolResult(toolId: string, output: string): Message {
|
||||||
|
return createUserMessage({
|
||||||
|
content: [
|
||||||
|
{
|
||||||
|
type: 'tool_result' as const,
|
||||||
|
tool_use_id: toolId,
|
||||||
|
content: output,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('microCompact MCP tool compaction', () => {
|
||||||
|
// We can't easily unit-test the private isCompactableTool directly,
|
||||||
|
// but we can test the full time-based microcompact path which exercises
|
||||||
|
// collectCompactableToolIds → isCompactableTool under the hood.
|
||||||
|
// The time-based path is the simplest to trigger: it content-clears
|
||||||
|
// old tool results when the gap since last assistant message exceeds
|
||||||
|
// the threshold.
|
||||||
|
|
||||||
|
// However, evaluateTimeBasedTrigger depends on config (GrowthBook).
|
||||||
|
// So instead, let's test the observable behavior by importing the
|
||||||
|
// microcompactMessages function and checking that MCP tool_use blocks
|
||||||
|
// are collected.
|
||||||
|
|
||||||
|
// Since collectCompactableToolIds is not exported, we test the predicate
|
||||||
|
// behavior by verifying that the module loads without error and that
|
||||||
|
// built-in and MCP tools are treated consistently.
|
||||||
|
|
||||||
|
test('module exports load correctly', async () => {
|
||||||
|
const mod = await import('./microCompact.js')
|
||||||
|
expect(mod.microcompactMessages).toBeFunction()
|
||||||
|
expect(mod.estimateMessageTokens).toBeFunction()
|
||||||
|
expect(mod.evaluateTimeBasedTrigger).toBeFunction()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('estimateMessageTokens counts MCP tool_use blocks', async () => {
|
||||||
|
const { estimateMessageTokens } = await import('./microCompact.js')
|
||||||
|
|
||||||
|
const builtinMessages: Message[] = [
|
||||||
|
assistantWithToolUse('Read', 'tool-builtin-1'),
|
||||||
|
userWithToolResult('tool-builtin-1', 'file contents here'),
|
||||||
|
]
|
||||||
|
|
||||||
|
const mcpMessages: Message[] = [
|
||||||
|
assistantWithToolUse('mcp__github__get_file_contents', 'tool-mcp-1'),
|
||||||
|
userWithToolResult('tool-mcp-1', 'file contents here'),
|
||||||
|
]
|
||||||
|
|
||||||
|
const builtinTokens = estimateMessageTokens(builtinMessages)
|
||||||
|
const mcpTokens = estimateMessageTokens(mcpMessages)
|
||||||
|
|
||||||
|
// Both should produce non-zero estimates
|
||||||
|
expect(builtinTokens).toBeGreaterThan(0)
|
||||||
|
expect(mcpTokens).toBeGreaterThan(0)
|
||||||
|
|
||||||
|
// The tool_result content is identical, so token estimates should be
|
||||||
|
// similar (tool_use name differs slightly, so not exactly equal)
|
||||||
|
expect(Math.abs(builtinTokens - mcpTokens)).toBeLessThan(50)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('microcompactMessages processes MCP tools without error', async () => {
|
||||||
|
const { microcompactMessages } = await import('./microCompact.js')
|
||||||
|
|
||||||
|
const messages: Message[] = [
|
||||||
|
assistantWithToolUse('mcp__slack__send_message', 'tool-mcp-2'),
|
||||||
|
userWithToolResult('tool-mcp-2', 'Message sent successfully'),
|
||||||
|
assistantWithToolUse('mcp__github__create_pull_request', 'tool-mcp-3'),
|
||||||
|
userWithToolResult('tool-mcp-3', JSON.stringify({ number: 42, url: 'https://github.com/org/repo/pull/42' })),
|
||||||
|
]
|
||||||
|
|
||||||
|
// Should not throw — MCP tools should be handled gracefully
|
||||||
|
const result = await microcompactMessages(messages)
|
||||||
|
expect(result).toBeDefined()
|
||||||
|
expect(result.messages).toBeDefined()
|
||||||
|
expect(result.messages.length).toBe(messages.length)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('microcompactMessages processes mixed built-in and MCP tools', async () => {
|
||||||
|
const { microcompactMessages } = await import('./microCompact.js')
|
||||||
|
|
||||||
|
const messages: Message[] = [
|
||||||
|
assistantWithToolUse('Read', 'tool-read-1'),
|
||||||
|
userWithToolResult('tool-read-1', 'some file content'),
|
||||||
|
assistantWithToolUse('mcp__playwright__screenshot', 'tool-mcp-4'),
|
||||||
|
userWithToolResult('tool-mcp-4', 'base64-encoded-screenshot-data'.repeat(100)),
|
||||||
|
assistantWithToolUse('Bash', 'tool-bash-1'),
|
||||||
|
userWithToolResult('tool-bash-1', 'command output'),
|
||||||
|
]
|
||||||
|
|
||||||
|
const result = await microcompactMessages(messages)
|
||||||
|
expect(result).toBeDefined()
|
||||||
|
expect(result.messages.length).toBe(messages.length)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -37,7 +37,7 @@ export const TIME_BASED_MC_CLEARED_MESSAGE = '[Old tool result content cleared]'
|
|||||||
|
|
||||||
const IMAGE_MAX_TOKEN_SIZE = 2000
|
const IMAGE_MAX_TOKEN_SIZE = 2000
|
||||||
|
|
||||||
// Only compact these tools
|
// Only compact these built-in tools (MCP tools are also compactable via prefix match)
|
||||||
const COMPACTABLE_TOOLS = new Set<string>([
|
const COMPACTABLE_TOOLS = new Set<string>([
|
||||||
FILE_READ_TOOL_NAME,
|
FILE_READ_TOOL_NAME,
|
||||||
...SHELL_TOOL_NAMES,
|
...SHELL_TOOL_NAMES,
|
||||||
@@ -49,7 +49,13 @@ const COMPACTABLE_TOOLS = new Set<string>([
|
|||||||
FILE_WRITE_TOOL_NAME,
|
FILE_WRITE_TOOL_NAME,
|
||||||
])
|
])
|
||||||
|
|
||||||
// --- Cached microcompact state (internal-only, gated by feature('CACHED_MICROCOMPACT')) ---
|
const MCP_TOOL_PREFIX = 'mcp__'
|
||||||
|
|
||||||
|
function isCompactableTool(name: string): boolean {
|
||||||
|
return COMPACTABLE_TOOLS.has(name) || name.startsWith(MCP_TOOL_PREFIX)
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Cached microcompact state (gated by feature('CACHED_MICROCOMPACT')) ---
|
||||||
|
|
||||||
// Lazy-initialized cached MC module and state to avoid importing in external builds.
|
// Lazy-initialized cached MC module and state to avoid importing in external builds.
|
||||||
// The imports and state live inside feature() checks for dead code elimination.
|
// The imports and state live inside feature() checks for dead code elimination.
|
||||||
@@ -231,7 +237,7 @@ function collectCompactableToolIds(messages: Message[]): string[] {
|
|||||||
Array.isArray(message.message.content)
|
Array.isArray(message.message.content)
|
||||||
) {
|
) {
|
||||||
for (const block of message.message.content) {
|
for (const block of message.message.content) {
|
||||||
if (block.type === 'tool_use' && COMPACTABLE_TOOLS.has(block.name)) {
|
if (block.type === 'tool_use' && isCompactableTool(block.name)) {
|
||||||
ids.push(block.id)
|
ids.push(block.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/services/tools/toolExecution.test.ts
Normal file
33
src/services/tools/toolExecution.test.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
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,6 +43,7 @@ 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,
|
||||||
@@ -596,6 +597,31 @@ 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,
|
||||||
@@ -614,7 +640,9 @@ 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) {
|
||||||
let errorContent = formatZodValidationError(tool.name, parsedInput.error)
|
const fallbackErrorContent = formatZodValidationError(tool.name, parsedInput.error)
|
||||||
|
let errorContent =
|
||||||
|
getSchemaValidationErrorOverride(tool, input) ?? fallbackErrorContent
|
||||||
|
|
||||||
const schemaHint = buildSchemaNotSentHint(
|
const schemaHint = buildSchemaNotSentHint(
|
||||||
tool,
|
tool,
|
||||||
@@ -672,7 +700,11 @@ async function checkPermissionsAndCallTool(
|
|||||||
tool_use_id: toolUseID,
|
tool_use_id: toolUseID,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
toolUseResult: `InputValidationError: ${parsedInput.error.message}`,
|
toolUseResult: getSchemaValidationToolUseResult(
|
||||||
|
tool,
|
||||||
|
input,
|
||||||
|
parsedInput.error.message,
|
||||||
|
),
|
||||||
sourceToolAssistantUUID: assistantMessage.uuid,
|
sourceToolAssistantUUID: assistantMessage.uuid,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -156,34 +156,24 @@ ${AGENT_TOOL_NAME}({
|
|||||||
const currentExamples = `Example usage:
|
const currentExamples = `Example usage:
|
||||||
|
|
||||||
<example_agent_descriptions>
|
<example_agent_descriptions>
|
||||||
"test-runner": use this agent after you are done writing code to run tests
|
"claude-code-guide": use this agent when the user asks how Claude Code works or how to use its features
|
||||||
"greeting-responder": use this agent to respond to user greetings with a friendly joke
|
"statusline-setup": use this agent to configure the user's Claude Code status line setting
|
||||||
</example_agent_descriptions>
|
</example_agent_descriptions>
|
||||||
|
|
||||||
<example>
|
<example>
|
||||||
user: "Please write a function that checks if a number is prime"
|
user: "How do I configure Claude Code hooks?"
|
||||||
assistant: I'm going to use the ${FILE_WRITE_TOOL_NAME} tool to write the following code:
|
|
||||||
<code>
|
|
||||||
function isPrime(n) {
|
|
||||||
if (n <= 1) return false
|
|
||||||
for (let i = 2; i * i <= n; i++) {
|
|
||||||
if (n % i === 0) return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
</code>
|
|
||||||
<commentary>
|
<commentary>
|
||||||
Since a significant piece of code was written and the task was completed, now use the test-runner agent to run the tests
|
This is a Claude Code usage question, so use the claude-code-guide agent
|
||||||
</commentary>
|
</commentary>
|
||||||
assistant: Uses the ${AGENT_TOOL_NAME} tool to launch the test-runner agent
|
assistant: Uses the ${AGENT_TOOL_NAME} tool to launch the claude-code-guide agent
|
||||||
</example>
|
</example>
|
||||||
|
|
||||||
<example>
|
<example>
|
||||||
user: "Hello"
|
user: "Set up my Claude Code status line"
|
||||||
<commentary>
|
<commentary>
|
||||||
Since the user is greeting, use the greeting-responder agent to respond with a friendly joke
|
This matches the statusline-setup agent, so use it to configure the setting
|
||||||
</commentary>
|
</commentary>
|
||||||
assistant: "I'm going to use the ${AGENT_TOOL_NAME} tool to launch the greeting-responder agent"
|
assistant: "I'm going to use the ${AGENT_TOOL_NAME} tool to launch the statusline-setup agent"
|
||||||
</example>
|
</example>
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|||||||
31
src/tools/SkillTool/SkillTool.test.ts
Normal file
31
src/tools/SkillTool/SkillTool.test.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
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,6 +352,16 @@ 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) {
|
||||||
@@ -434,7 +444,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.trim()
|
const trimmed = skill ?? ''
|
||||||
|
|
||||||
// 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
|
||||||
@@ -592,7 +602,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.trim()
|
const trimmed = skill ?? ''
|
||||||
|
|
||||||
// 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,6 +1,7 @@
|
|||||||
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 () => {
|
||||||
@@ -64,3 +65,16 @@ 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'],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@@ -94,3 +94,22 @@ test('gpt-5.4 family keeps large max output overrides within provider limits', (
|
|||||||
expect(getMaxOutputTokensForModel('gpt-5.4-mini')).toBe(128_000)
|
expect(getMaxOutputTokensForModel('gpt-5.4-mini')).toBe(128_000)
|
||||||
expect(getMaxOutputTokensForModel('gpt-5.4-nano')).toBe(128_000)
|
expect(getMaxOutputTokensForModel('gpt-5.4-nano')).toBe(128_000)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('MiniMax-M2.7 uses explicit provider-specific context and output caps', () => {
|
||||||
|
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||||
|
delete process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS
|
||||||
|
|
||||||
|
expect(getContextWindowForModel('MiniMax-M2.7')).toBe(204_800)
|
||||||
|
expect(getModelMaxOutputTokens('MiniMax-M2.7')).toEqual({
|
||||||
|
default: 131_072,
|
||||||
|
upperLimit: 131_072,
|
||||||
|
})
|
||||||
|
expect(getMaxOutputTokensForModel('MiniMax-M2.7')).toBe(131_072)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('unknown openai-compatible models still use the conservative fallback window', () => {
|
||||||
|
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||||
|
delete process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS
|
||||||
|
|
||||||
|
expect(getContextWindowForModel('some-unknown-3p-model')).toBe(8_000)
|
||||||
|
})
|
||||||
|
|||||||
@@ -72,16 +72,23 @@ export function getContextWindowForModel(
|
|||||||
return 1_000_000
|
return 1_000_000
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenAI-compatible provider — use known context windows for the model
|
// OpenAI-compatible provider — use known context windows for the model.
|
||||||
if (
|
// Unknown models get a conservative 8k default so auto-compact triggers
|
||||||
|
// before hitting a hard context_window_exceeded error.
|
||||||
|
const isOpenAIProvider =
|
||||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_OPENAI) ||
|
isEnvTruthy(process.env.CLAUDE_CODE_USE_OPENAI) ||
|
||||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI) ||
|
isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI) ||
|
||||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
||||||
) {
|
if (isOpenAIProvider) {
|
||||||
const openaiWindow = getOpenAIContextWindow(model)
|
const openaiWindow = getOpenAIContextWindow(model)
|
||||||
if (openaiWindow !== undefined) {
|
if (openaiWindow !== undefined) {
|
||||||
return openaiWindow
|
return openaiWindow
|
||||||
}
|
}
|
||||||
|
console.error(
|
||||||
|
`[context] Warning: model "${model}" not in context window table — using conservative 8k default. ` +
|
||||||
|
'Add it to src/utils/model/openaiContextWindows.ts for accurate compaction.',
|
||||||
|
)
|
||||||
|
return 8_000
|
||||||
}
|
}
|
||||||
|
|
||||||
const cap = getModelCapability(model)
|
const cap = getModelCapability(model)
|
||||||
|
|||||||
@@ -69,3 +69,93 @@ test('loadConversationForResume rejects oversized transcripts before resume hook
|
|||||||
)
|
)
|
||||||
expect(hookSpy).not.toHaveBeenCalled()
|
expect(hookSpy).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('deserializeMessagesWithInterruptDetection strips thinking blocks only for OpenAI-compatible providers', async () => {
|
||||||
|
const serializedMessages = [
|
||||||
|
user(id(10), 'hello'),
|
||||||
|
{
|
||||||
|
type: 'assistant',
|
||||||
|
uuid: id(11),
|
||||||
|
parentUuid: id(10),
|
||||||
|
timestamp: ts,
|
||||||
|
cwd: '/tmp',
|
||||||
|
sessionId,
|
||||||
|
version: 'test',
|
||||||
|
message: {
|
||||||
|
role: 'assistant',
|
||||||
|
content: [
|
||||||
|
{ type: 'thinking', thinking: 'secret reasoning' },
|
||||||
|
{ type: 'text', text: 'visible reply' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'assistant',
|
||||||
|
uuid: id(12),
|
||||||
|
parentUuid: id(11),
|
||||||
|
timestamp: ts,
|
||||||
|
cwd: '/tmp',
|
||||||
|
sessionId,
|
||||||
|
version: 'test',
|
||||||
|
message: {
|
||||||
|
role: 'assistant',
|
||||||
|
content: [{ type: 'thinking', thinking: 'only hidden reasoning' }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user(id(13), 'follow up'),
|
||||||
|
]
|
||||||
|
|
||||||
|
mock.module('./model/providers.js', () => ({
|
||||||
|
getAPIProvider: () => 'openai',
|
||||||
|
isOpenAICompatibleProvider: (provider: string) =>
|
||||||
|
provider === 'openai' ||
|
||||||
|
provider === 'gemini' ||
|
||||||
|
provider === 'github' ||
|
||||||
|
provider === 'codex',
|
||||||
|
}))
|
||||||
|
|
||||||
|
const openaiModule = await import(`./conversationRecovery.ts?provider=openai-${Date.now()}`)
|
||||||
|
const thirdParty = openaiModule.deserializeMessagesWithInterruptDetection(serializedMessages as never[])
|
||||||
|
const thirdPartyAssistantMessages = thirdParty.messages.filter(
|
||||||
|
message => message.type === 'assistant',
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(thirdPartyAssistantMessages).toHaveLength(2)
|
||||||
|
expect(thirdPartyAssistantMessages[0]?.message?.content).toEqual([
|
||||||
|
{ type: 'text', text: 'visible reply' },
|
||||||
|
])
|
||||||
|
expect(
|
||||||
|
JSON.stringify(thirdPartyAssistantMessages.map(message => message.message?.content)),
|
||||||
|
).not.toContain('secret reasoning')
|
||||||
|
expect(
|
||||||
|
JSON.stringify(thirdPartyAssistantMessages.map(message => message.message?.content)),
|
||||||
|
).not.toContain('only hidden reasoning')
|
||||||
|
|
||||||
|
mock.restore()
|
||||||
|
mock.module('./model/providers.js', () => ({
|
||||||
|
getAPIProvider: () => 'bedrock',
|
||||||
|
isOpenAICompatibleProvider: (provider: string) =>
|
||||||
|
provider === 'openai' ||
|
||||||
|
provider === 'gemini' ||
|
||||||
|
provider === 'github' ||
|
||||||
|
provider === 'codex',
|
||||||
|
}))
|
||||||
|
|
||||||
|
const bedrockModule = await import(`./conversationRecovery.ts?provider=bedrock-${Date.now()}`)
|
||||||
|
const anthropicCompatible = bedrockModule.deserializeMessagesWithInterruptDetection(serializedMessages as never[])
|
||||||
|
const anthropicAssistantMessages = anthropicCompatible.messages.filter(
|
||||||
|
message => message.type === 'assistant',
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(anthropicAssistantMessages).toHaveLength(2)
|
||||||
|
expect(anthropicAssistantMessages[0]?.message?.content).toEqual([
|
||||||
|
{ type: 'thinking', thinking: 'secret reasoning' },
|
||||||
|
{ type: 'text', text: 'visible reply' },
|
||||||
|
])
|
||||||
|
expect(
|
||||||
|
JSON.stringify(anthropicAssistantMessages.map(message => message.message?.content)),
|
||||||
|
).toContain('secret reasoning')
|
||||||
|
expect(
|
||||||
|
JSON.stringify(anthropicAssistantMessages.map(message => message.message?.content)),
|
||||||
|
).not.toContain('only hidden reasoning')
|
||||||
|
})
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const originalSimple = process.env.CLAUDE_CODE_SIMPLE
|
|||||||
const sessionId = '00000000-0000-4000-8000-000000001999'
|
const sessionId = '00000000-0000-4000-8000-000000001999'
|
||||||
const ts = '2026-04-02T00:00:00.000Z'
|
const ts = '2026-04-02T00:00:00.000Z'
|
||||||
|
|
||||||
|
|
||||||
function id(n: number): string {
|
function id(n: number): string {
|
||||||
return `00000000-0000-4000-8000-${String(n).padStart(12, '0')}`
|
return `00000000-0000-4000-8000-${String(n).padStart(12, '0')}`
|
||||||
}
|
}
|
||||||
@@ -76,4 +77,3 @@ test('loadConversationForResume rejects oversized reconstructed transcripts', as
|
|||||||
'Reconstructed transcript is too large to resume safely',
|
'Reconstructed transcript is too large to resume safely',
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import {
|
|||||||
type FileHistorySnapshot,
|
type FileHistorySnapshot,
|
||||||
} from './fileHistory.js'
|
} from './fileHistory.js'
|
||||||
import { logError } from './log.js'
|
import { logError } from './log.js'
|
||||||
|
import { getAPIProvider } from './model/providers.js'
|
||||||
import {
|
import {
|
||||||
createAssistantMessage,
|
createAssistantMessage,
|
||||||
createUserMessage,
|
createUserMessage,
|
||||||
@@ -177,6 +178,25 @@ export type DeserializeResult = {
|
|||||||
turnInterruptionState: TurnInterruptionState
|
turnInterruptionState: TurnInterruptionState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove thinking/redacted_thinking content blocks from assistant messages.
|
||||||
|
* Messages that become empty after stripping are removed entirely.
|
||||||
|
*/
|
||||||
|
function stripThinkingBlocks(messages: NormalizedMessage[]): NormalizedMessage[] {
|
||||||
|
return messages.reduce<NormalizedMessage[]>((acc, msg) => {
|
||||||
|
if (msg.type !== 'assistant' || !Array.isArray(msg.message?.content)) {
|
||||||
|
acc.push(msg)
|
||||||
|
return acc
|
||||||
|
}
|
||||||
|
const filtered = msg.message.content.filter(
|
||||||
|
(block: { type?: string }) => block.type !== 'thinking' && block.type !== 'redacted_thinking',
|
||||||
|
)
|
||||||
|
if (filtered.length === 0) return acc
|
||||||
|
acc.push({ ...msg, message: { ...msg.message, content: filtered } })
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserializes messages from a log file into the format expected by the REPL.
|
* Deserializes messages from a log file into the format expected by the REPL.
|
||||||
* Filters unresolved tool uses, orphaned thinking messages, and appends a
|
* Filters unresolved tool uses, orphaned thinking messages, and appends a
|
||||||
@@ -227,10 +247,19 @@ export function deserializeMessagesWithInterruptDetection(
|
|||||||
filteredToolUses,
|
filteredToolUses,
|
||||||
) as NormalizedMessage[]
|
) as NormalizedMessage[]
|
||||||
|
|
||||||
|
// Strip thinking/redacted_thinking content blocks from assistant messages
|
||||||
|
// when resuming against a 3P provider. These Anthropic-specific blocks cause
|
||||||
|
// 400 errors or context corruption on OpenAI-compatible providers (issue #248 finding 5).
|
||||||
|
const provider = getAPIProvider()
|
||||||
|
const isThirdPartyProvider = provider !== 'firstParty' && provider !== 'bedrock' && provider !== 'vertex' && provider !== 'foundry'
|
||||||
|
const thinkingStripped = isThirdPartyProvider
|
||||||
|
? stripThinkingBlocks(filteredThinking)
|
||||||
|
: filteredThinking
|
||||||
|
|
||||||
// Filter out assistant messages with only whitespace text content.
|
// Filter out assistant messages with only whitespace text content.
|
||||||
// This can happen when model outputs "\n\n" before thinking, user cancels mid-stream.
|
// This can happen when model outputs "\n\n" before thinking, user cancels mid-stream.
|
||||||
const filteredMessages = filterWhitespaceOnlyAssistantMessages(
|
const filteredMessages = filterWhitespaceOnlyAssistantMessages(
|
||||||
filteredThinking,
|
thinkingStripped,
|
||||||
) as NormalizedMessage[]
|
) as NormalizedMessage[]
|
||||||
|
|
||||||
const internalState = detectTurnInterruption(filteredMessages)
|
const internalState = detectTurnInterruption(filteredMessages)
|
||||||
|
|||||||
@@ -4,6 +4,10 @@ import { tmpdir } from 'os'
|
|||||||
import { join } from 'path'
|
import { join } from 'path'
|
||||||
import { extractDraggedFilePaths } from './dragDropPaths.js'
|
import { extractDraggedFilePaths } from './dragDropPaths.js'
|
||||||
|
|
||||||
|
function escapeFinderDraggedPath(filePath: string): string {
|
||||||
|
return filePath.replace(/([\\ ])/g, '\\$1')
|
||||||
|
}
|
||||||
|
|
||||||
describe('extractDraggedFilePaths', () => {
|
describe('extractDraggedFilePaths', () => {
|
||||||
// Paths that exist on any system.
|
// Paths that exist on any system.
|
||||||
const thisFile = import.meta.path
|
const thisFile = import.meta.path
|
||||||
@@ -80,6 +84,12 @@ describe('extractDraggedFilePaths', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('escapeFinderDraggedPath escapes spaces and backslashes', () => {
|
||||||
|
expect(escapeFinderDraggedPath('/tmp/my\\notes file.txt')).toBe(
|
||||||
|
'/tmp/my\\\\notes\\ file.txt',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
// Backslash-escaped paths are a Finder/macOS + Linux convention — on
|
// Backslash-escaped paths are a Finder/macOS + Linux convention — on
|
||||||
// Windows the shell-escape step is skipped, so these cases do not apply.
|
// Windows the shell-escape step is skipped, so these cases do not apply.
|
||||||
if (process.platform !== 'win32') {
|
if (process.platform !== 'win32') {
|
||||||
@@ -92,7 +102,7 @@ describe('extractDraggedFilePaths', () => {
|
|||||||
|
|
||||||
test('resolves an escaped real file with a space in its name', () => {
|
test('resolves an escaped real file with a space in its name', () => {
|
||||||
// Raw form matches what a terminal delivers on Finder drag.
|
// Raw form matches what a terminal delivers on Finder drag.
|
||||||
const escaped = spacedFile.replace(/ /g, '\\ ')
|
const escaped = escapeFinderDraggedPath(spacedFile)
|
||||||
expect(extractDraggedFilePaths(escaped)).toEqual([spacedFile])
|
expect(extractDraggedFilePaths(escaped)).toEqual([spacedFile])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ describe('hydrateGithubModelsTokenFromSecureStorage', () => {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
const { hydrateGithubModelsTokenFromSecureStorage } = await import(
|
const { hydrateGithubModelsTokenFromSecureStorage } = await import(
|
||||||
'./githubModelsCredentials.js'
|
'./githubModelsCredentials.js?hydrate=sets-token'
|
||||||
)
|
)
|
||||||
hydrateGithubModelsTokenFromSecureStorage()
|
hydrateGithubModelsTokenFromSecureStorage()
|
||||||
expect(process.env.GITHUB_TOKEN).toBe('stored-secret')
|
expect(process.env.GITHUB_TOKEN).toBe('stored-secret')
|
||||||
@@ -62,7 +62,7 @@ describe('hydrateGithubModelsTokenFromSecureStorage', () => {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
const { hydrateGithubModelsTokenFromSecureStorage } = await import(
|
const { hydrateGithubModelsTokenFromSecureStorage } = await import(
|
||||||
'./githubModelsCredentials.js'
|
'./githubModelsCredentials.js?hydrate=preserve-existing'
|
||||||
)
|
)
|
||||||
hydrateGithubModelsTokenFromSecureStorage()
|
hydrateGithubModelsTokenFromSecureStorage()
|
||||||
expect(process.env.GITHUB_TOKEN).toBe('already')
|
expect(process.env.GITHUB_TOKEN).toBe('already')
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
import { describe, expect, test } from 'bun:test'
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
|
||||||
import {
|
|
||||||
clearGithubModelsToken,
|
|
||||||
readGithubModelsToken,
|
|
||||||
saveGithubModelsToken,
|
|
||||||
} from './githubModelsCredentials.js'
|
|
||||||
|
|
||||||
describe('readGithubModelsToken', () => {
|
describe('readGithubModelsToken', () => {
|
||||||
test('returns undefined in bare mode', () => {
|
test('returns undefined in bare mode', async () => {
|
||||||
|
const { readGithubModelsToken } = await import(
|
||||||
|
'./githubModelsCredentials.js?read-bare-mode'
|
||||||
|
)
|
||||||
|
|
||||||
const prev = process.env.CLAUDE_CODE_SIMPLE
|
const prev = process.env.CLAUDE_CODE_SIMPLE
|
||||||
process.env.CLAUDE_CODE_SIMPLE = '1'
|
process.env.CLAUDE_CODE_SIMPLE = '1'
|
||||||
expect(readGithubModelsToken()).toBeUndefined()
|
expect(readGithubModelsToken()).toBeUndefined()
|
||||||
@@ -20,7 +18,11 @@ describe('readGithubModelsToken', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('saveGithubModelsToken / clearGithubModelsToken', () => {
|
describe('saveGithubModelsToken / clearGithubModelsToken', () => {
|
||||||
test('save returns failure in bare mode', () => {
|
test('save returns failure in bare mode', async () => {
|
||||||
|
const { saveGithubModelsToken } = await import(
|
||||||
|
'./githubModelsCredentials.js?save-bare-mode'
|
||||||
|
)
|
||||||
|
|
||||||
const prev = process.env.CLAUDE_CODE_SIMPLE
|
const prev = process.env.CLAUDE_CODE_SIMPLE
|
||||||
process.env.CLAUDE_CODE_SIMPLE = '1'
|
process.env.CLAUDE_CODE_SIMPLE = '1'
|
||||||
const r = saveGithubModelsToken('abc')
|
const r = saveGithubModelsToken('abc')
|
||||||
@@ -33,7 +35,11 @@ describe('saveGithubModelsToken / clearGithubModelsToken', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test('clear succeeds in bare mode', () => {
|
test('clear succeeds in bare mode', async () => {
|
||||||
|
const { clearGithubModelsToken } = await import(
|
||||||
|
'./githubModelsCredentials.js?clear-bare-mode'
|
||||||
|
)
|
||||||
|
|
||||||
const prev = process.env.CLAUDE_CODE_SIMPLE
|
const prev = process.env.CLAUDE_CODE_SIMPLE
|
||||||
process.env.CLAUDE_CODE_SIMPLE = '1'
|
process.env.CLAUDE_CODE_SIMPLE = '1'
|
||||||
expect(clearGithubModelsToken().success).toBe(true)
|
expect(clearGithubModelsToken().success).toBe(true)
|
||||||
|
|||||||
@@ -23,6 +23,19 @@ export function readGithubModelsToken(): string | undefined {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function readGithubModelsTokenAsync(): Promise<string | undefined> {
|
||||||
|
if (isBareMode()) return undefined
|
||||||
|
try {
|
||||||
|
const data = (await getSecureStorage().readAsync()) as
|
||||||
|
| ({ githubModels?: GithubModelsCredentialBlob } & Record<string, unknown>)
|
||||||
|
| null
|
||||||
|
const t = data?.githubModels?.accessToken?.trim()
|
||||||
|
return t || undefined
|
||||||
|
} catch {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If GitHub Models mode is on and no token is in the environment, copy the
|
* If GitHub Models mode is on and no token is in the environment, copy the
|
||||||
* stored token into process.env so the OpenAI shim and validation see it.
|
* stored token into process.env so the OpenAI shim and validation see it.
|
||||||
|
|||||||
@@ -44,6 +44,10 @@ const OPENAI_CONTEXT_WINDOWS: Record<string, number> = {
|
|||||||
'mistral-large-latest': 131_072,
|
'mistral-large-latest': 131_072,
|
||||||
'mistral-small-latest': 131_072,
|
'mistral-small-latest': 131_072,
|
||||||
|
|
||||||
|
// MiniMax
|
||||||
|
'MiniMax-M2.7': 204_800,
|
||||||
|
'minimax-m2.7': 204_800,
|
||||||
|
|
||||||
// Google (via OpenRouter)
|
// Google (via OpenRouter)
|
||||||
'google/gemini-2.0-flash':1_048_576,
|
'google/gemini-2.0-flash':1_048_576,
|
||||||
'google/gemini-2.5-pro': 1_048_576,
|
'google/gemini-2.5-pro': 1_048_576,
|
||||||
@@ -110,6 +114,10 @@ const OPENAI_MAX_OUTPUT_TOKENS: Record<string, number> = {
|
|||||||
'mistral-large-latest': 32_768,
|
'mistral-large-latest': 32_768,
|
||||||
'mistral-small-latest': 32_768,
|
'mistral-small-latest': 32_768,
|
||||||
|
|
||||||
|
// MiniMax
|
||||||
|
'MiniMax-M2.7': 131_072,
|
||||||
|
'minimax-m2.7': 131_072,
|
||||||
|
|
||||||
// Google (via OpenRouter)
|
// Google (via OpenRouter)
|
||||||
'google/gemini-2.0-flash': 8_192,
|
'google/gemini-2.0-flash': 8_192,
|
||||||
'google/gemini-2.5-pro': 65_536,
|
'google/gemini-2.5-pro': 65_536,
|
||||||
|
|||||||
@@ -1,11 +1,52 @@
|
|||||||
import { expect, test } from 'bun:test'
|
import { expect, test } from 'bun:test'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
import { wrapRipgrepUnavailableError } from './ripgrep.ts'
|
import { resolveRipgrepConfig, wrapRipgrepUnavailableError } from './ripgrep.js'
|
||||||
|
|
||||||
|
const MOCK_BUILTIN_PATH = path.normalize(
|
||||||
|
process.platform === 'win32'
|
||||||
|
? `vendor/ripgrep/${process.arch}-win32/rg.exe`
|
||||||
|
: `vendor/ripgrep/${process.arch}-${process.platform}/rg`,
|
||||||
|
)
|
||||||
|
|
||||||
|
test('ripgrepCommand falls back to system rg when builtin binary is missing', () => {
|
||||||
|
const config = resolveRipgrepConfig({
|
||||||
|
userWantsSystemRipgrep: false,
|
||||||
|
bundledMode: false,
|
||||||
|
builtinCommand: MOCK_BUILTIN_PATH,
|
||||||
|
builtinExists: false,
|
||||||
|
systemExecutablePath: '/usr/bin/rg',
|
||||||
|
processExecPath: '/fake/bun',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(config).toMatchObject({
|
||||||
|
mode: 'system',
|
||||||
|
command: 'rg',
|
||||||
|
args: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('ripgrepCommand keeps builtin mode when bundled binary exists', () => {
|
||||||
|
const config = resolveRipgrepConfig({
|
||||||
|
userWantsSystemRipgrep: false,
|
||||||
|
bundledMode: false,
|
||||||
|
builtinCommand: MOCK_BUILTIN_PATH,
|
||||||
|
builtinExists: true,
|
||||||
|
systemExecutablePath: '/usr/bin/rg',
|
||||||
|
processExecPath: '/fake/bun',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(config).toMatchObject({
|
||||||
|
mode: 'builtin',
|
||||||
|
command: MOCK_BUILTIN_PATH,
|
||||||
|
args: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
test('wrapRipgrepUnavailableError explains missing packaged fallback', () => {
|
test('wrapRipgrepUnavailableError explains missing packaged fallback', () => {
|
||||||
const error = wrapRipgrepUnavailableError(
|
const error = wrapRipgrepUnavailableError(
|
||||||
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
||||||
{ mode: 'builtin', command: 'C:\\fake\\vendor\\ripgrep\\rg.exe' },
|
{ mode: 'builtin', command: 'C:\\fake\\vendor\\ripgrep\\rg.exe', args: [] },
|
||||||
'win32',
|
'win32',
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +59,7 @@ test('wrapRipgrepUnavailableError explains missing packaged fallback', () => {
|
|||||||
test('wrapRipgrepUnavailableError explains missing system ripgrep', () => {
|
test('wrapRipgrepUnavailableError explains missing system ripgrep', () => {
|
||||||
const error = wrapRipgrepUnavailableError(
|
const error = wrapRipgrepUnavailableError(
|
||||||
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
||||||
{ mode: 'system', command: 'rg' },
|
{ mode: 'system', command: 'rg', args: [] },
|
||||||
'linux',
|
'linux',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import type { ChildProcess, ExecFileException } from 'child_process'
|
import type { ChildProcess, ExecFileException } from 'child_process'
|
||||||
import { execFile, spawn } from 'child_process'
|
import { execFile, spawn } from 'child_process'
|
||||||
|
import { existsSync } from 'fs'
|
||||||
import memoize from 'lodash-es/memoize.js'
|
import memoize from 'lodash-es/memoize.js'
|
||||||
import { homedir } from 'os'
|
import { homedir } from 'os'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
@@ -30,40 +31,72 @@ type RipgrepConfig = {
|
|||||||
|
|
||||||
type RipgrepErrorLike = Pick<NodeJS.ErrnoException, 'code' | 'message'>
|
type RipgrepErrorLike = Pick<NodeJS.ErrnoException, 'code' | 'message'>
|
||||||
|
|
||||||
const getRipgrepConfig = memoize((): RipgrepConfig => {
|
function isErrnoException(error: unknown): error is NodeJS.ErrnoException {
|
||||||
const userWantsSystemRipgrep = isEnvDefinedFalsy(
|
return error instanceof Error
|
||||||
process.env.USE_BUILTIN_RIPGREP,
|
}
|
||||||
)
|
|
||||||
|
|
||||||
// Try system ripgrep if user wants it
|
type ResolveRipgrepConfigArgs = {
|
||||||
if (userWantsSystemRipgrep) {
|
userWantsSystemRipgrep: boolean
|
||||||
const { cmd: systemPath } = findExecutable('rg', [])
|
bundledMode: boolean
|
||||||
if (systemPath !== 'rg') {
|
builtinCommand: string
|
||||||
// SECURITY: Use command name 'rg' instead of systemPath to prevent PATH hijacking
|
builtinExists: boolean
|
||||||
// If we used systemPath, a malicious ./rg.exe in current directory could be executed
|
systemExecutablePath: string
|
||||||
// Using just 'rg' lets the OS resolve it safely with NoDefaultCurrentDirectoryInExePath protection
|
processExecPath?: string
|
||||||
return { mode: 'system', command: 'rg', args: [] }
|
}
|
||||||
}
|
|
||||||
|
export function resolveRipgrepConfig({
|
||||||
|
userWantsSystemRipgrep,
|
||||||
|
bundledMode,
|
||||||
|
builtinCommand,
|
||||||
|
builtinExists,
|
||||||
|
systemExecutablePath,
|
||||||
|
processExecPath = process.execPath,
|
||||||
|
}: ResolveRipgrepConfigArgs): RipgrepConfig {
|
||||||
|
if (userWantsSystemRipgrep && systemExecutablePath !== 'rg') {
|
||||||
|
// SECURITY: Use command name 'rg' instead of systemExecutablePath to prevent PATH hijacking
|
||||||
|
return { mode: 'system', command: 'rg', args: [] }
|
||||||
}
|
}
|
||||||
|
|
||||||
// In bundled (native) mode, ripgrep is statically compiled into bun-internal
|
if (bundledMode) {
|
||||||
// and dispatches based on argv[0]. We spawn ourselves with argv0='rg'.
|
|
||||||
if (isInBundledMode()) {
|
|
||||||
return {
|
return {
|
||||||
mode: 'embedded',
|
mode: 'embedded',
|
||||||
command: process.execPath,
|
command: processExecPath,
|
||||||
args: ['--no-config'],
|
args: ['--no-config'],
|
||||||
argv0: 'rg',
|
argv0: 'rg',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (builtinExists) {
|
||||||
|
return { mode: 'builtin', command: builtinCommand, args: [] }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (systemExecutablePath !== 'rg') {
|
||||||
|
return { mode: 'system', command: 'rg', args: [] }
|
||||||
|
}
|
||||||
|
|
||||||
|
return { mode: 'builtin', command: builtinCommand, args: [] }
|
||||||
|
}
|
||||||
|
|
||||||
|
const getRipgrepConfig = memoize((): RipgrepConfig => {
|
||||||
|
const userWantsSystemRipgrep = isEnvDefinedFalsy(
|
||||||
|
process.env.USE_BUILTIN_RIPGREP,
|
||||||
|
)
|
||||||
|
const bundledMode = isInBundledMode()
|
||||||
const rgRoot = path.resolve(__dirname, 'vendor', 'ripgrep')
|
const rgRoot = path.resolve(__dirname, 'vendor', 'ripgrep')
|
||||||
const command =
|
const builtinCommand =
|
||||||
process.platform === 'win32'
|
process.platform === 'win32'
|
||||||
? path.resolve(rgRoot, `${process.arch}-win32`, 'rg.exe')
|
? path.resolve(rgRoot, `${process.arch}-win32`, 'rg.exe')
|
||||||
: path.resolve(rgRoot, `${process.arch}-${process.platform}`, 'rg')
|
: path.resolve(rgRoot, `${process.arch}-${process.platform}`, 'rg')
|
||||||
|
const builtinExists = existsSync(builtinCommand)
|
||||||
|
const { cmd: systemExecutablePath } = findExecutable('rg', [])
|
||||||
|
|
||||||
return { mode: 'builtin', command, args: [] }
|
return resolveRipgrepConfig({
|
||||||
|
userWantsSystemRipgrep,
|
||||||
|
bundledMode,
|
||||||
|
builtinCommand,
|
||||||
|
builtinExists,
|
||||||
|
systemExecutablePath,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
export function ripgrepCommand(): {
|
export function ripgrepCommand(): {
|
||||||
@@ -324,7 +357,9 @@ async function ripGrepFileCount(
|
|||||||
if (settled) return
|
if (settled) return
|
||||||
settled = true
|
settled = true
|
||||||
reject(
|
reject(
|
||||||
err.code === 'ENOENT' ? wrapRipgrepUnavailableError(err) : err,
|
isErrnoException(err) && err.code === 'ENOENT'
|
||||||
|
? wrapRipgrepUnavailableError(err)
|
||||||
|
: err,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -388,7 +423,9 @@ export async function ripGrepStream(
|
|||||||
if (settled) return
|
if (settled) return
|
||||||
settled = true
|
settled = true
|
||||||
reject(
|
reject(
|
||||||
err.code === 'ENOENT' ? wrapRipgrepUnavailableError(err) : err,
|
isErrnoException(err) && err.code === 'ENOENT'
|
||||||
|
? wrapRipgrepUnavailableError(err)
|
||||||
|
: err,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -436,7 +473,9 @@ export async function ripGrep(
|
|||||||
const CRITICAL_ERROR_CODES = ['ENOENT', 'EACCES', 'EPERM']
|
const CRITICAL_ERROR_CODES = ['ENOENT', 'EACCES', 'EPERM']
|
||||||
if (CRITICAL_ERROR_CODES.includes(error.code as string)) {
|
if (CRITICAL_ERROR_CODES.includes(error.code as string)) {
|
||||||
reject(
|
reject(
|
||||||
error.code === 'ENOENT' ? wrapRipgrepUnavailableError(error) : error,
|
isErrnoException(error) && error.code === 'ENOENT'
|
||||||
|
? wrapRipgrepUnavailableError(error)
|
||||||
|
: error,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
68
src/utils/schemaSanitizer.test.ts
Normal file
68
src/utils/schemaSanitizer.test.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
|
||||||
|
import { sanitizeSchemaForOpenAICompat } from './schemaSanitizer'
|
||||||
|
|
||||||
|
describe('sanitizeSchemaForOpenAICompat', () => {
|
||||||
|
test('preserves Grep-like properties.pattern while keeping it required', () => {
|
||||||
|
const schema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: {
|
||||||
|
type: 'string',
|
||||||
|
description: 'The regular expression pattern to search for in file contents',
|
||||||
|
},
|
||||||
|
path: { type: 'string' },
|
||||||
|
glob: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['pattern'],
|
||||||
|
}
|
||||||
|
|
||||||
|
const sanitized = sanitizeSchemaForOpenAICompat(schema)
|
||||||
|
const properties = sanitized.properties as Record<string, unknown> | undefined
|
||||||
|
|
||||||
|
expect(Object.keys(properties ?? {})).toEqual(['pattern', 'path', 'glob'])
|
||||||
|
expect(properties?.pattern).toEqual({
|
||||||
|
type: 'string',
|
||||||
|
description: 'The regular expression pattern to search for in file contents',
|
||||||
|
})
|
||||||
|
expect(sanitized.required).toEqual(['pattern'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('preserves Glob-like properties.pattern while keeping it required', () => {
|
||||||
|
const schema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
pattern: {
|
||||||
|
type: 'string',
|
||||||
|
description: 'The glob pattern to match files against',
|
||||||
|
},
|
||||||
|
path: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['pattern'],
|
||||||
|
}
|
||||||
|
|
||||||
|
const sanitized = sanitizeSchemaForOpenAICompat(schema)
|
||||||
|
const properties = sanitized.properties as Record<string, unknown> | undefined
|
||||||
|
|
||||||
|
expect(Object.keys(properties ?? {})).toEqual(['pattern', 'path'])
|
||||||
|
expect(properties?.pattern).toEqual({
|
||||||
|
type: 'string',
|
||||||
|
description: 'The glob pattern to match files against',
|
||||||
|
})
|
||||||
|
expect(sanitized.required).toEqual(['pattern'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('strips JSON Schema validator pattern from string schemas', () => {
|
||||||
|
const schema = {
|
||||||
|
type: 'string',
|
||||||
|
pattern: '^[a-z]+$',
|
||||||
|
minLength: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
const sanitized = sanitizeSchemaForOpenAICompat(schema)
|
||||||
|
|
||||||
|
expect(sanitized).toEqual({
|
||||||
|
type: 'string',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -33,6 +33,15 @@ function stripSchemaKeywords(schema: unknown, keywords: Set<string>): unknown {
|
|||||||
|
|
||||||
const result: Record<string, unknown> = {}
|
const result: Record<string, unknown> = {}
|
||||||
for (const [key, value] of Object.entries(schema)) {
|
for (const [key, value] of Object.entries(schema)) {
|
||||||
|
if (key === 'properties' && isSchemaRecord(value)) {
|
||||||
|
const sanitizedProps: Record<string, unknown> = {}
|
||||||
|
for (const [propName, propSchema] of Object.entries(value)) {
|
||||||
|
sanitizedProps[propName] = stripSchemaKeywords(propSchema, keywords)
|
||||||
|
}
|
||||||
|
result[key] = sanitizedProps
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if (keywords.has(key)) {
|
if (keywords.has(key)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -215,10 +224,13 @@ export function sanitizeSchemaForOpenAICompat(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(record.required) && isSchemaRecord(record.properties)) {
|
const properties = isSchemaRecord(record.properties)
|
||||||
|
? record.properties
|
||||||
|
: undefined
|
||||||
|
|
||||||
|
if (Array.isArray(record.required) && properties) {
|
||||||
record.required = record.required.filter(
|
record.required = record.required.filter(
|
||||||
(value): value is string =>
|
(value): value is string => typeof value === 'string' && value in properties,
|
||||||
typeof value === 'string' && value in record.properties,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,8 +97,12 @@ export function renderToAnsiString(node: React.ReactNode, columns?: number): Pro
|
|||||||
patchConsole: false
|
patchConsole: false
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for the component to exit naturally
|
// Wait for the component to exit naturally, with a timeout guard so
|
||||||
await instance.waitUntilExit();
|
// tests never hang indefinitely if a render error prevents exit().
|
||||||
|
await Promise.race([
|
||||||
|
instance.waitUntilExit(),
|
||||||
|
new Promise<void>(resolve => setTimeout(resolve, 3000)),
|
||||||
|
]);
|
||||||
|
|
||||||
// Extract only the first frame's content to avoid duplication
|
// Extract only the first frame's content to avoid duplication
|
||||||
// (Ink outputs multiple frames in non-TTY mode)
|
// (Ink outputs multiple frames in non-TTY mode)
|
||||||
|
|||||||
Reference in New Issue
Block a user